OSDN Git Service

* Make-lang.in (java/jcf-write.o): Depend on $(TM_P_H).
[pf3gnuchains/gcc-fork.git] / gcc / java / parse.y
1 /* Source code parsing and tree node generation for the GNU compiler
2    for the Java(TM) language.
3    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    Free Software Foundation, Inc.
5    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
27
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
31
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
35
36 The following modifications were brought to the original grammar:
37
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
40
41 Note: All the extra rules described above should go away when the
42       empty_statement rule will work.
43
44 statement_nsi: 'nsi' should be read no_short_if.
45
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions.  */
48
49 %{
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "flags.h"
61 #include "java-tree.h"
62 #include "jcf.h"
63 #include "lex.h"
64 #include "parse.h"
65 #include "zipfile.h"
66 #include "convert.h"
67 #include "buffer.h"
68 #include "xref.h"
69 #include "function.h"
70 #include "except.h"
71 #include "ggc.h"
72 #include "debug.h"
73 #include "tree-inline.h"
74
75 /* Local function prototypes */
76 static char *java_accstring_lookup (int);
77 static void  classitf_redefinition_error (const char *,tree, tree, tree);
78 static void  variable_redefinition_error (tree, tree, tree, int);
79 static tree  create_class (int, tree, tree, tree);
80 static tree  create_interface (int, tree, tree);
81 static void  end_class_declaration (int);
82 static tree  find_field (tree, tree);
83 static tree lookup_field_wrapper (tree, tree);
84 static int   duplicate_declaration_error_p (tree, tree, tree);
85 static void  register_fields (int, tree, tree);
86 static tree parser_qualified_classname (tree);
87 static int  parser_check_super (tree, tree, tree);
88 static int  parser_check_super_interface (tree, tree, tree);
89 static void check_modifiers_consistency (int);
90 static tree lookup_cl (tree);
91 static tree lookup_java_method2 (tree, tree, int);
92 static tree method_header (int, tree, tree, tree);
93 static void fix_method_argument_names (tree ,tree);
94 static tree method_declarator (tree, tree);
95 static void parse_warning_context (tree cl, const char *msg, ...)
96   ATTRIBUTE_PRINTF_2;
97 static void issue_warning_error_from_context (tree, const char *msg, va_list)
98   ATTRIBUTE_PRINTF (2, 0);
99 static void parse_ctor_invocation_error (void);
100 static tree parse_jdk1_1_error (const char *);
101 static void complete_class_report_errors (jdep *);
102 static int process_imports (void);
103 static void read_import_dir (tree);
104 static int find_in_imports_on_demand (tree, tree);
105 static void find_in_imports (tree, tree);
106 static void check_inner_class_access (tree, tree, tree);
107 static int check_pkg_class_access (tree, tree, bool);
108 static void register_package (tree);
109 static tree resolve_package (tree, tree *, tree *);
110 static tree resolve_class (tree, tree, tree, tree);
111 static void declare_local_variables (int, tree, tree);
112 static void dump_java_tree (enum tree_dump_index, tree);
113 static void source_start_java_method (tree);
114 static void source_end_java_method (void);
115 static tree find_name_in_single_imports (tree);
116 static void check_abstract_method_header (tree);
117 static tree lookup_java_interface_method2 (tree, tree);
118 static tree resolve_expression_name (tree, tree *);
119 static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
120 static int check_class_interface_creation (int, int, tree, tree, tree, tree);
121 static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
122 static int breakdown_qualified (tree *, tree *, tree);
123 static int in_same_package (tree, tree);
124 static tree resolve_and_layout (tree, tree);
125 static tree qualify_and_find (tree, tree, tree);
126 static tree resolve_no_layout (tree, tree);
127 static int invocation_mode (tree, int);
128 static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
129 static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
130 static tree find_most_specific_methods_list (tree);
131 static int argument_types_convertible (tree, tree);
132 static tree patch_invoke (tree, tree, tree);
133 static int maybe_use_access_method (int, tree *, tree *);
134 static tree lookup_method_invoke (int, tree, tree, tree, tree);
135 static tree register_incomplete_type (int, tree, tree, tree);
136 static tree check_inner_circular_reference (tree, tree);
137 static tree check_circular_reference (tree);
138 static tree obtain_incomplete_type (tree);
139 static tree java_complete_lhs (tree);
140 static tree java_complete_tree (tree);
141 static tree maybe_generate_pre_expand_clinit (tree);
142 static int analyze_clinit_body (tree, tree);
143 static int maybe_yank_clinit (tree);
144 static void start_complete_expand_method (tree);
145 static void java_complete_expand_method (tree);
146 static void java_expand_method_bodies (tree);
147 static int  unresolved_type_p (tree, tree *);
148 static void create_jdep_list (struct parser_ctxt *);
149 static tree build_expr_block (tree, tree);
150 static tree enter_block (void);
151 static tree exit_block (void);
152 static tree lookup_name_in_blocks (tree);
153 static void maybe_absorb_scoping_blocks (void);
154 static tree build_method_invocation (tree, tree);
155 static tree build_new_invocation (tree, tree);
156 static tree build_assignment (int, int, tree, tree);
157 static tree build_binop (enum tree_code, int, tree, tree);
158 static tree patch_assignment (tree, tree);
159 static tree patch_binop (tree, tree, tree);
160 static tree build_unaryop (int, int, tree);
161 static tree build_incdec (int, int, tree, int);
162 static tree patch_unaryop (tree, tree);
163 static tree build_cast (int, tree, tree);
164 static tree build_null_of_type (tree);
165 static tree patch_cast (tree, tree);
166 static int valid_ref_assignconv_cast_p (tree, tree, int);
167 static int valid_builtin_assignconv_identity_widening_p (tree, tree);
168 static int valid_cast_to_p (tree, tree);
169 static int valid_method_invocation_conversion_p (tree, tree);
170 static tree try_builtin_assignconv (tree, tree, tree);
171 static tree try_reference_assignconv (tree, tree);
172 static tree build_unresolved_array_type (tree);
173 static int build_type_name_from_array_name (tree, tree *);
174 static tree build_array_from_name (tree, tree, tree, tree *);
175 static tree build_array_ref (int, tree, tree);
176 static tree patch_array_ref (tree);
177 static tree make_qualified_name (tree, tree, int);
178 static tree merge_qualified_name (tree, tree);
179 static tree make_qualified_primary (tree, tree, int);
180 static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
181 static void qualify_ambiguous_name (tree);
182 static tree resolve_field_access (tree, tree *, tree *);
183 static tree build_newarray_node (tree, tree, int);
184 static tree patch_newarray (tree);
185 static tree resolve_type_during_patch (tree);
186 static tree build_this (int);
187 static tree build_wfl_wrap (tree, int);
188 static tree build_return (int, tree);
189 static tree patch_return (tree);
190 static tree maybe_access_field (tree, tree, tree);
191 static int complete_function_arguments (tree);
192 static int check_for_static_method_reference (tree, tree, tree, tree, tree);
193 static int not_accessible_p (tree, tree, tree, int);
194 static void check_deprecation (tree, tree);
195 static int class_in_current_package (tree);
196 static tree build_if_else_statement (int, tree, tree, tree);
197 static tree patch_if_else_statement (tree);
198 static tree add_stmt_to_compound (tree, tree, tree);
199 static tree add_stmt_to_block (tree, tree, tree);
200 static tree patch_exit_expr (tree);
201 static tree build_labeled_block (int, tree);
202 static tree finish_labeled_statement (tree, tree);
203 static tree build_bc_statement (int, int, tree);
204 static tree patch_bc_statement (tree);
205 static tree patch_loop_statement (tree);
206 static tree build_new_loop (tree);
207 static tree build_loop_body (int, tree, int);
208 static tree finish_loop_body (int, tree, tree, int);
209 static tree build_debugable_stmt (int, tree);
210 static tree finish_for_loop (int, tree, tree, tree);
211 static tree patch_switch_statement (tree);
212 static tree string_constant_concatenation (tree, tree);
213 static tree build_string_concatenation (tree, tree);
214 static tree patch_string_cst (tree);
215 static tree patch_string (tree);
216 static tree encapsulate_with_try_catch (int, tree, tree, tree);
217 static tree build_assertion (int, tree, tree);
218 static tree build_try_statement (int, tree, tree);
219 static tree build_try_finally_statement (int, tree, tree);
220 static tree patch_try_statement (tree);
221 static tree patch_synchronized_statement (tree, tree);
222 static tree patch_throw_statement (tree, tree);
223 static void check_thrown_exceptions (int, tree);
224 static int check_thrown_exceptions_do (tree);
225 static void purge_unchecked_exceptions (tree);
226 static bool ctors_unchecked_throws_clause_p (tree);
227 static void check_throws_clauses (tree, tree, tree);
228 static void finish_method_declaration (tree);
229 static tree build_super_invocation (tree);
230 static int verify_constructor_circularity (tree, tree);
231 static char *constructor_circularity_msg (tree, tree);
232 static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
233 static const char *get_printable_method_name (tree);
234 static tree patch_conditional_expr (tree, tree, tree);
235 static tree generate_finit (tree);
236 static tree generate_instinit (tree);
237 static tree build_instinit_invocation (tree);
238 static void fix_constructors (tree);
239 static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
240 static tree craft_constructor (tree, tree);
241 static int verify_constructor_super (tree);
242 static tree create_artificial_method (tree, int, tree, tree, tree);
243 static void start_artificial_method_body (tree);
244 static void end_artificial_method_body (tree);
245 static int check_method_redefinition (tree, tree);
246 static int check_method_types_complete (tree);
247 static void java_check_regular_methods (tree);
248 static void java_check_abstract_methods (tree);
249 static void unreachable_stmt_error (tree);
250 static tree find_expr_with_wfl (tree);
251 static void missing_return_error (tree);
252 static tree build_new_array_init (int, tree);
253 static tree patch_new_array_init (tree, tree);
254 static tree maybe_build_array_element_wfl (tree);
255 static int array_constructor_check_entry (tree, tree);
256 static const char *purify_type_name (const char *);
257 static tree fold_constant_for_init (tree, tree);
258 static tree strip_out_static_field_access_decl (tree);
259 static jdeplist *reverse_jdep_list (struct parser_ctxt *);
260 static void static_ref_err (tree, tree, tree);
261 static void parser_add_interface (tree, tree, tree);
262 static void add_superinterfaces (tree, tree);
263 static tree jdep_resolve_class (jdep *);
264 static int note_possible_classname (const char *, int);
265 static void java_complete_expand_classes (void);
266 static void java_complete_expand_class (tree);
267 static void java_complete_expand_methods (tree);
268 static tree cut_identifier_in_qualified (tree);
269 static tree java_stabilize_reference (tree);
270 static tree do_unary_numeric_promotion (tree);
271 static char * operator_string (tree);
272 static tree do_merge_string_cste (tree, const char *, int, int);
273 static tree merge_string_cste (tree, tree, int);
274 static tree java_refold (tree);
275 static int java_decl_equiv (tree, tree);
276 static int binop_compound_p (enum tree_code);
277 static tree search_loop (tree);
278 static int labeled_block_contains_loop_p (tree, tree);
279 static int check_abstract_method_definitions (int, tree, tree);
280 static void java_check_abstract_method_definitions (tree);
281 static void java_debug_context_do (int);
282 static void java_parser_context_push_initialized_field (void);
283 static void java_parser_context_pop_initialized_field (void);
284 static tree reorder_static_initialized (tree);
285 static void java_parser_context_suspend (void);
286 static void java_parser_context_resume (void);
287 static int pop_current_osb (struct parser_ctxt *);
288
289 /* JDK 1.1 work. FIXME */
290
291 static tree maybe_make_nested_class_name (tree);
292 static int make_nested_class_name (tree);
293 static void set_nested_class_simple_name_value (tree, int);
294 static void link_nested_class_to_enclosing (void);
295 static tree resolve_inner_class (htab_t, tree, tree *, tree *, tree);
296 static tree find_as_inner_class (tree, tree, tree);
297 static tree find_as_inner_class_do (tree, tree);
298 static int check_inner_class_redefinition (tree, tree);
299
300 static tree build_thisn_assign (void);
301 static tree build_current_thisn (tree);
302 static tree build_access_to_thisn (tree, tree, int);
303 static tree maybe_build_thisn_access_method (tree);
304
305 static tree build_outer_field_access (tree, tree);
306 static tree build_outer_field_access_methods (tree);
307 static tree build_outer_field_access_expr (int, tree, tree,
308                                                   tree, tree);
309 static tree build_outer_method_access_method (tree);
310 static tree build_new_access_id (void);
311 static tree build_outer_field_access_method (tree, tree, tree,
312                                                     tree, tree);
313
314 static int outer_field_access_p (tree, tree);
315 static int outer_field_expanded_access_p (tree, tree *,
316                                                  tree *, tree *);
317 static tree outer_field_access_fix (tree, tree, tree);
318 static tree build_incomplete_class_ref (int, tree);
319 static tree patch_incomplete_class_ref (tree);
320 static tree create_anonymous_class (int, tree);
321 static void patch_anonymous_class (tree, tree, tree);
322 static void add_inner_class_fields (tree, tree);
323
324 static tree build_dot_class_method (tree);
325 static tree build_dot_class_method_invocation (tree);
326 static void create_new_parser_context (int);
327 static tree maybe_build_class_init_for_field (tree, tree);
328
329 static int attach_init_test_initialization_flags (void **, void *);
330 static int emit_test_initialization (void **, void *);
331
332 static char *string_convert_int_cst (tree);
333
334 /* Number of error found so far. */
335 int java_error_count;
336 /* Number of warning found so far. */
337 int java_warning_count;
338 /* Tell when not to fold, when doing xrefs */
339 int do_not_fold;
340 /* Cyclic inheritance report, as it can be set by layout_class */
341 const char *cyclic_inheritance_report;
342
343 /* The current parser context */
344 struct parser_ctxt *ctxp;
345
346 /* List of things that were analyzed for which code will be generated */
347 struct parser_ctxt *ctxp_for_generation = NULL;
348
349 /* binop_lookup maps token to tree_code. It is used where binary
350    operations are involved and required by the parser. RDIV_EXPR
351    covers both integral/floating point division. The code is changed
352    once the type of both operator is worked out.  */
353
354 static const enum tree_code binop_lookup[19] =
355   {
356     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
357     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
358     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
359     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
360     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
361    };
362 #define BINOP_LOOKUP(VALUE)                                             \
363   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
364
365 /* This is the end index for binary operators that can also be used
366    in compound assignements. */
367 #define BINOP_COMPOUND_CANDIDATES 11
368
369 /* The "$L" identifier we use to create labels.  */
370 static GTY(()) tree label_id;
371
372 /* The "StringBuffer" identifier used for the String `+' operator. */
373 static GTY(()) tree wfl_string_buffer;
374
375 /* The "append" identifier used for String `+' operator.  */
376 static GTY(()) tree wfl_append;
377
378 /* The "toString" identifier used for String `+' operator. */
379 static GTY(()) tree wfl_to_string;
380
381 /* The "java.lang" import qualified name.  */
382 static GTY(()) tree java_lang_id;
383
384 /* The generated `inst$' identifier used for generated enclosing
385    instance/field access functions.  */
386 static GTY(()) tree inst_id;
387
388 /* Context and flag for static blocks */
389 static GTY(()) tree current_static_block;
390
391 /* The generated `write_parm_value$' identifier.  */
392 static GTY(()) tree wpv_id;
393
394 /* The list of all packages we've seen so far */
395 static GTY(()) tree package_list;
396
397 /* Hold THIS for the scope of the current method decl.  */
398 static GTY(()) tree current_this;
399
400 /* Hold a list of catch clauses list. The first element of this list is
401    the list of the catch clauses of the currently analysed try block. */
402 static GTY(()) tree currently_caught_type_list;
403
404 /* This holds a linked list of all the case labels for the current
405    switch statement.  It is only used when checking to see if there
406    are duplicate labels.  FIXME: probably this should just be attached
407    to the switch itself; then it could be referenced via
408    `ctxp->current_loop'.  */
409 static GTY(()) tree case_label_list;
410
411 /* Anonymous class counter. Will be reset to 1 every time a non
412    anonymous class gets created. */
413 static int anonymous_class_counter = 1;
414
415 static GTY(()) tree src_parse_roots[1];
416
417 /* All classes seen from source code */
418 #define gclass_list src_parse_roots[0]
419
420 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
421    line and point it out.  */
422 /* Should point out the one that don't fit. ASCII/unicode, going
423    backward. FIXME */
424
425 #define check_modifiers(__message, __value, __mask) do {        \
426   if ((__value) & ~(__mask))                                    \
427     {                                                           \
428       size_t i, remainder = (__value) & ~(__mask);              \
429       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
430         if ((1 << i) & remainder)                               \
431           parse_error_context (ctxp->modifier_ctx [i], (__message), \
432                                java_accstring_lookup (1 << i)); \
433     }                                                           \
434 } while (0)
435
436 %}
437
438 %union {
439   tree node;
440   int sub_token;
441   struct {
442     int token;
443     int location;
444   } operator;
445   int value;
446 }
447
448 %{
449 #include "lex.c"
450 %}
451
452 %pure_parser
453
454 /* Things defined here have to match the order of what's in the
455    binop_lookup table.  */
456
457 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
458 %token   LS_TK           SRS_TK          ZRS_TK
459 %token   AND_TK          XOR_TK          OR_TK
460 %token   BOOL_AND_TK BOOL_OR_TK
461 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
462
463 /* This maps to the same binop_lookup entry than the token above */
464
465 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
466 %token   REM_ASSIGN_TK
467 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
468 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
469
470
471 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
472
473 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
474 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
475 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
476 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
477 %token   MODIFIER_TK
478
479 /* Keep those two in order, too */
480 %token   DECR_TK INCR_TK
481
482 /* From now one, things can be in any order */
483
484 %token   DEFAULT_TK      IF_TK              THROW_TK
485 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
486 %token   THROWS_TK       BREAK_TK           IMPORT_TK
487 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
488 %token   VOID_TK         CATCH_TK           INTERFACE_TK
489 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
490 %token   SUPER_TK        WHILE_TK           CLASS_TK
491 %token   SWITCH_TK       CONST_TK           TRY_TK
492 %token   FOR_TK          NEW_TK             CONTINUE_TK
493 %token   GOTO_TK         PACKAGE_TK         THIS_TK
494 %token   ASSERT_TK
495
496 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
497 %token   CHAR_TK         INTEGRAL_TK
498
499 %token   FLOAT_TK        DOUBLE_TK          FP_TK
500
501 %token   ID_TK
502
503 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
504
505 %token   ASSIGN_ANY_TK   ASSIGN_TK
506 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
507
508 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
509 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
510
511 %type    <value>        modifiers MODIFIER_TK final synchronized
512
513 %type    <node>         super ID_TK identifier
514 %type    <node>         name simple_name qualified_name
515 %type    <node>         type_declaration compilation_unit
516                         field_declaration method_declaration extends_interfaces
517                         interfaces interface_type_list
518                         import_declarations package_declaration
519                         type_declarations interface_body
520                         interface_member_declaration constant_declaration
521                         interface_member_declarations interface_type
522                         abstract_method_declaration
523 %type    <node>         class_body_declaration class_member_declaration
524                         static_initializer constructor_declaration block
525 %type    <node>         class_body_declarations constructor_header
526 %type    <node>         class_or_interface_type class_type class_type_list
527                         constructor_declarator explicit_constructor_invocation
528 %type    <node>         dim_expr dim_exprs this_or_super throws
529
530 %type    <node>         variable_declarator_id variable_declarator
531                         variable_declarators variable_initializer
532                         variable_initializers constructor_body
533                         array_initializer
534
535 %type    <node>         class_body block_end constructor_block_end
536 %type    <node>         statement statement_without_trailing_substatement
537                         labeled_statement if_then_statement label_decl
538                         if_then_else_statement while_statement for_statement
539                         statement_nsi labeled_statement_nsi do_statement
540                         if_then_else_statement_nsi while_statement_nsi
541                         for_statement_nsi statement_expression_list for_init
542                         for_update statement_expression expression_statement
543                         primary_no_new_array expression primary
544                         array_creation_expression array_type
545                         class_instance_creation_expression field_access
546                         method_invocation array_access something_dot_new
547                         argument_list postfix_expression while_expression
548                         post_increment_expression post_decrement_expression
549                         unary_expression_not_plus_minus unary_expression
550                         pre_increment_expression pre_decrement_expression
551                         cast_expression
552                         multiplicative_expression additive_expression
553                         shift_expression relational_expression
554                         equality_expression and_expression
555                         exclusive_or_expression inclusive_or_expression
556                         conditional_and_expression conditional_or_expression
557                         conditional_expression assignment_expression
558                         left_hand_side assignment for_header for_begin
559                         constant_expression do_statement_begin empty_statement
560                         switch_statement synchronized_statement throw_statement
561                         try_statement assert_statement
562                         switch_expression switch_block
563                         catches catch_clause catch_clause_parameter finally
564                         anonymous_class_creation trap_overflow_corner_case
565 %type    <node>         return_statement break_statement continue_statement
566
567 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
568 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
569 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
570 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
571 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
572 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
573 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
574 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
575 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
576 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
577 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
578 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
579 %type    <operator>     NEW_TK ASSERT_TK
580
581 %type    <node>         method_body
582
583 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
584                         STRING_LIT_TK NULL_TK VOID_TK
585
586 %type    <node>         IF_TK WHILE_TK FOR_TK
587
588 %type    <node>         formal_parameter_list formal_parameter
589                         method_declarator method_header
590
591 %type    <node>         primitive_type reference_type type
592                         BOOLEAN_TK INTEGRAL_TK FP_TK
593
594 /* Added or modified JDK 1.1 rule types  */
595 %type    <node>         type_literals
596
597 %%
598 /* 19.2 Production from 2.3: The Syntactic Grammar  */
599 goal:  compilation_unit
600                 {}
601 ;
602
603 /* 19.3 Productions from 3: Lexical structure  */
604 literal:
605         INT_LIT_TK
606 |       FP_LIT_TK
607 |       BOOL_LIT_TK
608 |       CHAR_LIT_TK
609 |       STRING_LIT_TK
610 |       NULL_TK
611 ;
612
613 /* 19.4 Productions from 4: Types, Values and Variables  */
614 type:
615         primitive_type
616 |       reference_type
617 ;
618
619 primitive_type:
620         INTEGRAL_TK
621 |       FP_TK
622 |       BOOLEAN_TK
623 ;
624
625 reference_type:
626         class_or_interface_type
627 |       array_type
628 ;
629
630 class_or_interface_type:
631         name
632 ;
633
634 class_type:
635         class_or_interface_type /* Default rule */
636 ;
637
638 interface_type:
639          class_or_interface_type
640 ;
641
642 array_type:
643         primitive_type dims
644                 {
645                   int osb = pop_current_osb (ctxp);
646                   tree t = build_java_array_type (($1), -1);
647                   while (--osb)
648                     t = build_unresolved_array_type (t);
649                   $$ = t;
650                 }
651 |       name dims
652                 {
653                   int osb = pop_current_osb (ctxp);
654                   tree t = $1;
655                   while (osb--)
656                     t = build_unresolved_array_type (t);
657                   $$ = t;
658                 }
659 ;
660
661 /* 19.5 Productions from 6: Names  */
662 name:
663         simple_name             /* Default rule */
664 |       qualified_name          /* Default rule */
665 ;
666
667 simple_name:
668         identifier              /* Default rule */
669 ;
670
671 qualified_name:
672         name DOT_TK identifier
673                 { $$ = make_qualified_name ($1, $3, $2.location); }
674 ;
675
676 identifier:
677         ID_TK
678 ;
679
680 /* 19.6: Production from 7: Packages  */
681 compilation_unit:
682                 {$$ = NULL;}
683 |       package_declaration
684 |       import_declarations
685 |       type_declarations
686 |       package_declaration import_declarations
687 |       package_declaration type_declarations
688 |       import_declarations type_declarations
689 |       package_declaration import_declarations type_declarations
690 ;
691
692 import_declarations:
693         import_declaration
694                 {
695                   $$ = NULL;
696                 }
697 |       import_declarations import_declaration
698                 {
699                   $$ = NULL;
700                 }
701 ;
702
703 type_declarations:
704         type_declaration
705 |       type_declarations type_declaration
706 ;
707
708 package_declaration:
709         PACKAGE_TK name SC_TK
710                 {
711                   ctxp->package = EXPR_WFL_NODE ($2);
712                   register_package (ctxp->package);
713                 }
714 |       PACKAGE_TK error
715                 {yyerror ("Missing name"); RECOVER;}
716 |       PACKAGE_TK name error
717                 {yyerror ("';' expected"); RECOVER;}
718 ;
719
720 import_declaration:
721         single_type_import_declaration
722 |       type_import_on_demand_declaration
723 ;
724
725 single_type_import_declaration:
726         IMPORT_TK name SC_TK
727                 {
728                   tree name = EXPR_WFL_NODE ($2), last_name;
729                   int   i = IDENTIFIER_LENGTH (name)-1;
730                   const char *last = &IDENTIFIER_POINTER (name)[i];
731                   while (last != IDENTIFIER_POINTER (name))
732                     {
733                       if (last [0] == '.')
734                         break;
735                       last--;
736                     }
737                   last_name = get_identifier (++last);
738                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
739                     {
740                       tree err = find_name_in_single_imports (last_name);
741                       if (err && err != name)
742                         parse_error_context
743                           ($2, "Ambiguous class: `%s' and `%s'",
744                            IDENTIFIER_POINTER (name),
745                            IDENTIFIER_POINTER (err));
746                       else
747                         REGISTER_IMPORT ($2, last_name);
748                     }
749                   else
750                     REGISTER_IMPORT ($2, last_name);
751                 }
752 |       IMPORT_TK error
753                 {yyerror ("Missing name"); RECOVER;}
754 |       IMPORT_TK name error
755                 {yyerror ("';' expected"); RECOVER;}
756 ;
757
758 type_import_on_demand_declaration:
759         IMPORT_TK name DOT_TK MULT_TK SC_TK
760                 {
761                   tree name = EXPR_WFL_NODE ($2);
762                   tree it;
763                   /* Search for duplicates. */
764                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
765                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
766                       break;
767                   /* Don't import the same thing more than once, just ignore
768                      duplicates (7.5.2) */
769                   if (! it)
770                     {
771                       read_import_dir ($2);
772                       ctxp->import_demand_list =
773                         chainon (ctxp->import_demand_list,
774                                  build_tree_list ($2, NULL_TREE));
775                     }
776                 }
777 |       IMPORT_TK name DOT_TK error
778                 {yyerror ("'*' expected"); RECOVER;}
779 |       IMPORT_TK name DOT_TK MULT_TK error
780                 {yyerror ("';' expected"); RECOVER;}
781 ;
782
783 type_declaration:
784         class_declaration
785                 { end_class_declaration (0); }
786 |       interface_declaration
787                 { end_class_declaration (0); }
788 |       empty_statement
789 |       error
790                 {
791                   YYERROR_NOW;
792                   yyerror ("Class or interface declaration expected");
793                 }
794 ;
795
796 /* 19.7 Shortened from the original:
797    modifiers: modifier | modifiers modifier
798    modifier: any of public...  */
799 modifiers:
800         MODIFIER_TK
801                 {
802                   $$ = (1 << $1);
803                 }
804 |       modifiers MODIFIER_TK
805                 {
806                   int acc = (1 << $2);
807                   if ($$ & acc)
808                     parse_error_context
809                       (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
810                        java_accstring_lookup (acc));
811                   else
812                     {
813                       $$ |= acc;
814                     }
815                 }
816 ;
817
818 /* 19.8.1 Production from $8.1: Class Declaration */
819 class_declaration:
820         modifiers CLASS_TK identifier super interfaces
821                 { create_class ($1, $3, $4, $5); }
822         class_body
823                 {;}
824 |       CLASS_TK identifier super interfaces
825                 { create_class (0, $2, $3, $4); }
826         class_body
827                 {;}
828 |       modifiers CLASS_TK error
829                 { yyerror ("Missing class name"); RECOVER; }
830 |       CLASS_TK error
831                 { yyerror ("Missing class name"); RECOVER; }
832 |       CLASS_TK identifier error
833                 {
834                   if (!ctxp->class_err) yyerror ("'{' expected");
835                   DRECOVER(class1);
836                 }
837 |       modifiers CLASS_TK identifier error
838                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
839 ;
840
841 super:
842                 { $$ = NULL; }
843 |       EXTENDS_TK class_type
844                 { $$ = $2; }
845 |       EXTENDS_TK class_type error
846                 {yyerror ("'{' expected"); ctxp->class_err=1;}
847 |       EXTENDS_TK error
848                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
849 ;
850
851 interfaces:
852                 { $$ = NULL_TREE; }
853 |       IMPLEMENTS_TK interface_type_list
854                 { $$ = $2; }
855 |       IMPLEMENTS_TK error
856                 {
857                   ctxp->class_err=1;
858                   yyerror ("Missing interface name");
859                 }
860 ;
861
862 interface_type_list:
863         interface_type
864                 {
865                   ctxp->interface_number = 1;
866                   $$ = build_tree_list ($1, NULL_TREE);
867                 }
868 |       interface_type_list C_TK interface_type
869                 {
870                   ctxp->interface_number++;
871                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
872                 }
873 |       interface_type_list C_TK error
874                 {yyerror ("Missing interface name"); RECOVER;}
875 ;
876
877 class_body:
878         OCB_TK CCB_TK
879                 {
880                   /* Store the location of the `}' when doing xrefs */
881                   if (flag_emit_xref)
882                     DECL_END_SOURCE_LINE (GET_CPC ()) =
883                       EXPR_WFL_ADD_COL ($2.location, 1);
884                   $$ = GET_CPC ();
885                 }
886 |       OCB_TK class_body_declarations CCB_TK
887                 {
888                   /* Store the location of the `}' when doing xrefs */
889                   if (flag_emit_xref)
890                     DECL_END_SOURCE_LINE (GET_CPC ()) =
891                       EXPR_WFL_ADD_COL ($3.location, 1);
892                   $$ = GET_CPC ();
893                 }
894 ;
895
896 class_body_declarations:
897         class_body_declaration
898 |       class_body_declarations class_body_declaration
899 ;
900
901 class_body_declaration:
902         class_member_declaration
903 |       static_initializer
904 |       constructor_declaration
905 |       block                   /* Added, JDK1.1, instance initializer */
906                 {
907                   if ($1 != empty_stmt_node)
908                     {
909                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
910                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
911                     }
912                 }
913 ;
914
915 class_member_declaration:
916         field_declaration
917 |       method_declaration
918 |       class_declaration       /* Added, JDK1.1 inner classes */
919                 { end_class_declaration (1); }
920 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
921                 { end_class_declaration (1); }
922 |       empty_statement
923 ;
924
925 /* 19.8.2 Productions from 8.3: Field Declarations  */
926 field_declaration:
927         type variable_declarators SC_TK
928                 { register_fields (0, $1, $2); }
929 |       modifiers type variable_declarators SC_TK
930                 {
931                   check_modifiers
932                     ("Illegal modifier `%s' for field declaration",
933                      $1, FIELD_MODIFIERS);
934                   check_modifiers_consistency ($1);
935                   register_fields ($1, $2, $3);
936                 }
937 ;
938
939 variable_declarators:
940         /* Should we use build_decl_list () instead ? FIXME */
941         variable_declarator     /* Default rule */
942 |       variable_declarators C_TK variable_declarator
943                 { $$ = chainon ($1, $3); }
944 |       variable_declarators C_TK error
945                 {yyerror ("Missing term"); RECOVER;}
946 ;
947
948 variable_declarator:
949         variable_declarator_id
950                 { $$ = build_tree_list ($1, NULL_TREE); }
951 |       variable_declarator_id ASSIGN_TK variable_initializer
952                 {
953                   if (java_error_count)
954                     $3 = NULL_TREE;
955                   $$ = build_tree_list
956                     ($1, build_assignment ($2.token, $2.location, $1, $3));
957                 }
958 |       variable_declarator_id ASSIGN_TK error
959                 {
960                   yyerror ("Missing variable initializer");
961                   $$ = build_tree_list ($1, NULL_TREE);
962                   RECOVER;
963                 }
964 |       variable_declarator_id ASSIGN_TK variable_initializer error
965                 {
966                   yyerror ("';' expected");
967                   $$ = build_tree_list ($1, NULL_TREE);
968                   RECOVER;
969                 }
970 ;
971
972 variable_declarator_id:
973         identifier
974 |       variable_declarator_id OSB_TK CSB_TK
975                 { $$ = build_unresolved_array_type ($1); }
976 |       identifier error
977                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
978 |       variable_declarator_id OSB_TK error
979                 {
980                   yyerror ("']' expected");
981                   DRECOVER(vdi);
982                 }
983 |       variable_declarator_id CSB_TK error
984                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
985 ;
986
987 variable_initializer:
988         expression
989 |       array_initializer
990 ;
991
992 /* 19.8.3 Productions from 8.4: Method Declarations  */
993 method_declaration:
994         method_header
995                 {
996                   current_function_decl = $1;
997                   if (current_function_decl
998                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
999                     source_start_java_method (current_function_decl);
1000                   else
1001                     current_function_decl = NULL_TREE;
1002                 }
1003         method_body
1004                 { finish_method_declaration ($3); }
1005 |       method_header error
1006                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1007 ;
1008
1009 method_header:
1010         type method_declarator throws
1011                 { $$ = method_header (0, $1, $2, $3); }
1012 |       VOID_TK method_declarator throws
1013                 { $$ = method_header (0, void_type_node, $2, $3); }
1014 |       modifiers type method_declarator throws
1015                 { $$ = method_header ($1, $2, $3, $4); }
1016 |       modifiers VOID_TK method_declarator throws
1017                 { $$ = method_header ($1, void_type_node, $3, $4); }
1018 |       type error
1019                 {
1020                   yyerror ("Invalid method declaration, method name required");
1021                   RECOVER;
1022                 }
1023 |       modifiers type error
1024                 {
1025                   yyerror ("Identifier expected");
1026                   RECOVER;
1027                 }
1028 |       VOID_TK error
1029                 {
1030                   yyerror ("Identifier expected");
1031                   RECOVER;
1032                 }
1033 |       modifiers VOID_TK error
1034                 {
1035                   yyerror ("Identifier expected");
1036                   RECOVER;
1037                 }
1038 |       modifiers error
1039                 {
1040                   yyerror ("Invalid method declaration, return type required");
1041                   RECOVER;
1042                 }
1043 ;
1044
1045 method_declarator:
1046         identifier OP_TK CP_TK
1047                 {
1048                   ctxp->formal_parameter_number = 0;
1049                   $$ = method_declarator ($1, NULL_TREE);
1050                 }
1051 |       identifier OP_TK formal_parameter_list CP_TK
1052                 { $$ = method_declarator ($1, $3); }
1053 |       method_declarator OSB_TK CSB_TK
1054                 {
1055                   EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1056                   TREE_PURPOSE ($1) =
1057                     build_unresolved_array_type (TREE_PURPOSE ($1));
1058                   parse_warning_context
1059                     (wfl_operator,
1060                      "Discouraged form of returned type specification");
1061                 }
1062 |       identifier OP_TK error
1063                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1064 |       method_declarator OSB_TK error
1065                 {yyerror ("']' expected"); RECOVER;}
1066 ;
1067
1068 formal_parameter_list:
1069         formal_parameter
1070                 {
1071                   ctxp->formal_parameter_number = 1;
1072                 }
1073 |       formal_parameter_list C_TK formal_parameter
1074                 {
1075                   ctxp->formal_parameter_number += 1;
1076                   $$ = chainon ($1, $3);
1077                 }
1078 |       formal_parameter_list C_TK error
1079                 { yyerror ("Missing formal parameter term"); RECOVER; }
1080 ;
1081
1082 formal_parameter:
1083         type variable_declarator_id
1084                 {
1085                   $$ = build_tree_list ($2, $1);
1086                 }
1087 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1088                 {
1089                   $$ = build_tree_list ($3, $2);
1090                   ARG_FINAL_P ($$) = 1;
1091                 }
1092 |       type error
1093                 {
1094                   yyerror ("Missing identifier"); RECOVER;
1095                   $$ = NULL_TREE;
1096                 }
1097 |       final type error
1098                 {
1099                   yyerror ("Missing identifier"); RECOVER;
1100                   $$ = NULL_TREE;
1101                 }
1102 ;
1103
1104 final:
1105         modifiers
1106                 {
1107                   check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1108                                    $1, ACC_FINAL);
1109                   if ($1 != ACC_FINAL)
1110                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1111                 }
1112 ;
1113
1114 throws:
1115                 { $$ = NULL_TREE; }
1116 |       THROWS_TK class_type_list
1117                 { $$ = $2; }
1118 |       THROWS_TK error
1119                 {yyerror ("Missing class type term"); RECOVER;}
1120 ;
1121
1122 class_type_list:
1123         class_type
1124                 { $$ = build_tree_list ($1, $1); }
1125 |       class_type_list C_TK class_type
1126                 { $$ = tree_cons ($3, $3, $1); }
1127 |       class_type_list C_TK error
1128                 {yyerror ("Missing class type term"); RECOVER;}
1129 ;
1130
1131 method_body:
1132         block
1133 |       SC_TK { $$ = NULL_TREE; }
1134 ;
1135
1136 /* 19.8.4 Productions from 8.5: Static Initializers  */
1137 static_initializer:
1138         static block
1139                 {
1140                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1141                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1142                   current_static_block = NULL_TREE;
1143                 }
1144 ;
1145
1146 static:                         /* Test lval.sub_token here */
1147         modifiers
1148                 {
1149                   check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1150                   /* Can't have a static initializer in an innerclass */
1151                   if ($1 | ACC_STATIC &&
1152                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1153                     parse_error_context
1154                       (MODIFIER_WFL (STATIC_TK),
1155                        "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1156                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1157                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1158                 }
1159 ;
1160
1161 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1162 constructor_declaration:
1163         constructor_header
1164                 {
1165                   current_function_decl = $1;
1166                   source_start_java_method (current_function_decl);
1167                 }
1168         constructor_body
1169                 { finish_method_declaration ($3); }
1170 ;
1171
1172 constructor_header:
1173         constructor_declarator throws
1174                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1175 |       modifiers constructor_declarator throws
1176                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1177 ;
1178
1179 constructor_declarator:
1180         simple_name OP_TK CP_TK
1181                 {
1182                   ctxp->formal_parameter_number = 0;
1183                   $$ = method_declarator ($1, NULL_TREE);
1184                 }
1185 |       simple_name OP_TK formal_parameter_list CP_TK
1186                 { $$ = method_declarator ($1, $3); }
1187 ;
1188
1189 constructor_body:
1190         /* Unlike regular method, we always need a complete (empty)
1191            body so we can safely perform all the required code
1192            addition (super invocation and field initialization) */
1193         block_begin constructor_block_end
1194                 {
1195                   BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1196                   $$ = $2;
1197                 }
1198 |       block_begin explicit_constructor_invocation constructor_block_end
1199                 { $$ = $3; }
1200 |       block_begin block_statements constructor_block_end
1201                 { $$ = $3; }
1202 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1203                 { $$ = $4; }
1204 ;
1205
1206 constructor_block_end:
1207         block_end
1208 ;
1209
1210 /* Error recovery for that rule moved down expression_statement: rule.  */
1211 explicit_constructor_invocation:
1212         this_or_super OP_TK CP_TK SC_TK
1213                 {
1214                   $$ = build_method_invocation ($1, NULL_TREE);
1215                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1216                   $$ = java_method_add_stmt (current_function_decl, $$);
1217                 }
1218 |       this_or_super OP_TK argument_list CP_TK SC_TK
1219                 {
1220                   $$ = build_method_invocation ($1, $3);
1221                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1222                   $$ = java_method_add_stmt (current_function_decl, $$);
1223                 }
1224         /* Added, JDK1.1 inner classes. Modified because the rule
1225            'primary' couldn't work.  */
1226 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1227                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1228 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1229                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1230 ;
1231
1232 this_or_super:                  /* Added, simplifies error diagnostics */
1233         THIS_TK
1234                 {
1235                   tree wfl = build_wfl_node (this_identifier_node);
1236                   EXPR_WFL_LINECOL (wfl) = $1.location;
1237                   $$ = wfl;
1238                 }
1239 |       SUPER_TK
1240                 {
1241                   tree wfl = build_wfl_node (super_identifier_node);
1242                   EXPR_WFL_LINECOL (wfl) = $1.location;
1243                   $$ = wfl;
1244                 }
1245 ;
1246
1247 /* 19.9 Productions from 9: Interfaces  */
1248 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1249 interface_declaration:
1250         INTERFACE_TK identifier
1251                 { create_interface (0, $2, NULL_TREE); }
1252         interface_body
1253                 { ; }
1254 |       modifiers INTERFACE_TK identifier
1255                 { create_interface ($1, $3, NULL_TREE); }
1256         interface_body
1257                 { ; }
1258 |       INTERFACE_TK identifier extends_interfaces
1259                 { create_interface (0, $2, $3); }
1260         interface_body
1261                 { ; }
1262 |       modifiers INTERFACE_TK identifier extends_interfaces
1263                 { create_interface ($1, $3, $4); }
1264         interface_body
1265                 { ; }
1266 |       INTERFACE_TK identifier error
1267                 { yyerror ("'{' expected"); RECOVER; }
1268 |       modifiers INTERFACE_TK identifier error
1269                 { yyerror ("'{' expected"); RECOVER; }
1270 ;
1271
1272 extends_interfaces:
1273         EXTENDS_TK interface_type
1274                 {
1275                   ctxp->interface_number = 1;
1276                   $$ = build_tree_list ($2, NULL_TREE);
1277                 }
1278 |       extends_interfaces C_TK interface_type
1279                 {
1280                   ctxp->interface_number++;
1281                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1282                 }
1283 |       EXTENDS_TK error
1284                 {yyerror ("Invalid interface type"); RECOVER;}
1285 |       extends_interfaces C_TK error
1286                 {yyerror ("Missing term"); RECOVER;}
1287 ;
1288
1289 interface_body:
1290         OCB_TK CCB_TK
1291                 { $$ = NULL_TREE; }
1292 |       OCB_TK interface_member_declarations CCB_TK
1293                 { $$ = NULL_TREE; }
1294 ;
1295
1296 interface_member_declarations:
1297         interface_member_declaration
1298 |       interface_member_declarations interface_member_declaration
1299 ;
1300
1301 interface_member_declaration:
1302         constant_declaration
1303 |       abstract_method_declaration
1304 |       class_declaration       /* Added, JDK1.1 inner classes */
1305                 { end_class_declaration (1); }
1306 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1307                 { end_class_declaration (1); }
1308 ;
1309
1310 constant_declaration:
1311         field_declaration
1312 ;
1313
1314 abstract_method_declaration:
1315         method_header SC_TK
1316                 {
1317                   check_abstract_method_header ($1);
1318                   current_function_decl = NULL_TREE; /* FIXME ? */
1319                 }
1320 |       method_header error
1321                 {yyerror ("';' expected"); RECOVER;}
1322 ;
1323
1324 /* 19.10 Productions from 10: Arrays  */
1325 array_initializer:
1326         OCB_TK CCB_TK
1327                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1328 |       OCB_TK C_TK CCB_TK
1329                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1330 |       OCB_TK variable_initializers CCB_TK
1331                 { $$ = build_new_array_init ($1.location, $2); }
1332 |       OCB_TK variable_initializers C_TK CCB_TK
1333                 { $$ = build_new_array_init ($1.location, $2); }
1334 ;
1335
1336 variable_initializers:
1337         variable_initializer
1338                 {
1339                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1340                                   $1, NULL_TREE);
1341                 }
1342 |       variable_initializers C_TK variable_initializer
1343                 {
1344                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1345                 }
1346 |       variable_initializers C_TK error
1347                 {yyerror ("Missing term"); RECOVER;}
1348 ;
1349
1350 /* 19.11 Production from 14: Blocks and Statements  */
1351 block:
1352         OCB_TK CCB_TK
1353                 {
1354                   /* Store the location of the `}' when doing xrefs */
1355                   if (current_function_decl && flag_emit_xref)
1356                     DECL_END_SOURCE_LINE (current_function_decl) =
1357                       EXPR_WFL_ADD_COL ($2.location, 1);
1358                   $$ = empty_stmt_node;
1359                 }
1360 |       block_begin block_statements block_end
1361                 { $$ = $3; }
1362 ;
1363
1364 block_begin:
1365         OCB_TK
1366                 { enter_block (); }
1367 ;
1368
1369 block_end:
1370         CCB_TK
1371                 {
1372                   maybe_absorb_scoping_blocks ();
1373                   /* Store the location of the `}' when doing xrefs */
1374                   if (current_function_decl && flag_emit_xref)
1375                     DECL_END_SOURCE_LINE (current_function_decl) =
1376                       EXPR_WFL_ADD_COL ($1.location, 1);
1377                   $$ = exit_block ();
1378                   if (!BLOCK_SUBBLOCKS ($$))
1379                     BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1380                 }
1381 ;
1382
1383 block_statements:
1384         block_statement
1385 |       block_statements block_statement
1386 ;
1387
1388 block_statement:
1389         local_variable_declaration_statement
1390 |       statement
1391                 { java_method_add_stmt (current_function_decl, $1); }
1392 |       class_declaration       /* Added, JDK1.1 local classes */
1393                 {
1394                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1395                   end_class_declaration (1);
1396                 }
1397 ;
1398
1399 local_variable_declaration_statement:
1400         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1401 ;
1402
1403 local_variable_declaration:
1404         type variable_declarators
1405                 { declare_local_variables (0, $1, $2); }
1406 |       final type variable_declarators /* Added, JDK1.1 final locals */
1407                 { declare_local_variables ($1, $2, $3); }
1408 ;
1409
1410 statement:
1411         statement_without_trailing_substatement
1412 |       labeled_statement
1413 |       if_then_statement
1414 |       if_then_else_statement
1415 |       while_statement
1416 |       for_statement
1417                 { $$ = exit_block (); }
1418 ;
1419
1420 statement_nsi:
1421         statement_without_trailing_substatement
1422 |       labeled_statement_nsi
1423 |       if_then_else_statement_nsi
1424 |       while_statement_nsi
1425 |       for_statement_nsi
1426                 { $$ = exit_block (); }
1427 ;
1428
1429 statement_without_trailing_substatement:
1430         block
1431 |       empty_statement
1432 |       expression_statement
1433 |       switch_statement
1434 |       do_statement
1435 |       break_statement
1436 |       continue_statement
1437 |       return_statement
1438 |       synchronized_statement
1439 |       throw_statement
1440 |       try_statement
1441 |       assert_statement
1442 ;
1443
1444 empty_statement:
1445         SC_TK
1446                 {
1447                   if (flag_extraneous_semicolon
1448                       && ! current_static_block
1449                       && (! current_function_decl ||
1450                           /* Verify we're not in a inner class declaration */
1451                           (GET_CPC () != TYPE_NAME
1452                            (DECL_CONTEXT (current_function_decl)))))
1453
1454                     {
1455                       EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1456                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1457                     }
1458                   $$ = empty_stmt_node;
1459                 }
1460 ;
1461
1462 label_decl:
1463         identifier REL_CL_TK
1464                 {
1465                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1466                                             EXPR_WFL_NODE ($1));
1467                   pushlevel (2);
1468                   push_labeled_block ($$);
1469                   PUSH_LABELED_BLOCK ($$);
1470                 }
1471 ;
1472
1473 labeled_statement:
1474         label_decl statement
1475                 { $$ = finish_labeled_statement ($1, $2); }
1476 |       identifier error
1477                 {yyerror ("':' expected"); RECOVER;}
1478 ;
1479
1480 labeled_statement_nsi:
1481         label_decl statement_nsi
1482                 { $$ = finish_labeled_statement ($1, $2); }
1483 ;
1484
1485 /* We concentrate here a bunch of error handling rules that we couldn't write
1486    earlier, because expression_statement catches a missing ';'.  */
1487 expression_statement:
1488         statement_expression SC_TK
1489                 {
1490                   /* We have a statement. Generate a WFL around it so
1491                      we can debug it */
1492                   $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1493                   /* We know we have a statement, so set the debug
1494                      info to be eventually generate here. */
1495                   $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1496                 }
1497 |       error SC_TK
1498                 {
1499                   YYNOT_TWICE yyerror ("Invalid expression statement");
1500                   DRECOVER (expr_stmt);
1501                 }
1502 |       error OCB_TK
1503                 {
1504                   YYNOT_TWICE yyerror ("Invalid expression statement");
1505                   DRECOVER (expr_stmt);
1506                 }
1507 |       error CCB_TK
1508                 {
1509                   YYNOT_TWICE yyerror ("Invalid expression statement");
1510                   DRECOVER (expr_stmt);
1511                 }
1512 |       this_or_super OP_TK error
1513                 {yyerror ("')' expected"); RECOVER;}
1514 |       this_or_super OP_TK CP_TK error
1515                 {
1516                   parse_ctor_invocation_error ();
1517                   RECOVER;
1518                 }
1519 |       this_or_super OP_TK argument_list error
1520                 {yyerror ("')' expected"); RECOVER;}
1521 |       this_or_super OP_TK argument_list CP_TK error
1522                 {
1523                   parse_ctor_invocation_error ();
1524                   RECOVER;
1525                 }
1526 |       name DOT_TK SUPER_TK error
1527                 {yyerror ("'(' expected"); RECOVER;}
1528 |       name DOT_TK SUPER_TK OP_TK error
1529                 {yyerror ("')' expected"); RECOVER;}
1530 |       name DOT_TK SUPER_TK OP_TK argument_list error
1531                 {yyerror ("')' expected"); RECOVER;}
1532 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1533                 {yyerror ("';' expected"); RECOVER;}
1534 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1535                 {yyerror ("';' expected"); RECOVER;}
1536 ;
1537
1538 statement_expression:
1539         assignment
1540 |       pre_increment_expression
1541 |       pre_decrement_expression
1542 |       post_increment_expression
1543 |       post_decrement_expression
1544 |       method_invocation
1545 |       class_instance_creation_expression
1546 ;
1547
1548 if_then_statement:
1549         IF_TK OP_TK expression CP_TK statement
1550                 {
1551                   $$ = build_if_else_statement ($2.location, $3,
1552                                                 $5, NULL_TREE);
1553                 }
1554 |       IF_TK error
1555                 {yyerror ("'(' expected"); RECOVER;}
1556 |       IF_TK OP_TK error
1557                 {yyerror ("Missing term"); RECOVER;}
1558 |       IF_TK OP_TK expression error
1559                 {yyerror ("')' expected"); RECOVER;}
1560 ;
1561
1562 if_then_else_statement:
1563         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1564                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1565 ;
1566
1567 if_then_else_statement_nsi:
1568         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1569                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1570 ;
1571
1572 switch_statement:
1573         switch_expression
1574                 {
1575                   enter_block ();
1576                 }
1577         switch_block
1578                 {
1579                   /* Make into "proper list" of COMPOUND_EXPRs.
1580                      I.e. make the last statement also have its own
1581                      COMPOUND_EXPR. */
1582                   maybe_absorb_scoping_blocks ();
1583                   TREE_OPERAND ($1, 1) = exit_block ();
1584                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1585                 }
1586 ;
1587
1588 switch_expression:
1589         SWITCH_TK OP_TK expression CP_TK
1590                 {
1591                   $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1592                   EXPR_WFL_LINECOL ($$) = $2.location;
1593                 }
1594 |       SWITCH_TK error
1595                 {yyerror ("'(' expected"); RECOVER;}
1596 |       SWITCH_TK OP_TK error
1597                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1598 |       SWITCH_TK OP_TK expression CP_TK error
1599                 {yyerror ("'{' expected"); RECOVER;}
1600 ;
1601
1602 /* Default assignment is there to avoid type node on switch_block
1603    node. */
1604
1605 switch_block:
1606         OCB_TK CCB_TK
1607                 { $$ = NULL_TREE; }
1608 |       OCB_TK switch_labels CCB_TK
1609                 { $$ = NULL_TREE; }
1610 |       OCB_TK switch_block_statement_groups CCB_TK
1611                 { $$ = NULL_TREE; }
1612 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1613                 { $$ = NULL_TREE; }
1614 ;
1615
1616 switch_block_statement_groups:
1617         switch_block_statement_group
1618 |       switch_block_statement_groups switch_block_statement_group
1619 ;
1620
1621 switch_block_statement_group:
1622         switch_labels block_statements
1623 ;
1624
1625 switch_labels:
1626         switch_label
1627 |       switch_labels switch_label
1628 ;
1629
1630 switch_label:
1631         CASE_TK constant_expression REL_CL_TK
1632                 {
1633                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1634                   EXPR_WFL_LINECOL (lab) = $1.location;
1635                   java_method_add_stmt (current_function_decl, lab);
1636                 }
1637 |       DEFAULT_TK REL_CL_TK
1638                 {
1639                   tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1640                   EXPR_WFL_LINECOL (lab) = $1.location;
1641                   java_method_add_stmt (current_function_decl, lab);
1642                 }
1643 |       CASE_TK error
1644                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1645 |       CASE_TK constant_expression error
1646                 {yyerror ("':' expected"); RECOVER;}
1647 |       DEFAULT_TK error
1648                 {yyerror ("':' expected"); RECOVER;}
1649 ;
1650
1651 while_expression:
1652         WHILE_TK OP_TK expression CP_TK
1653                 {
1654                   tree body = build_loop_body ($2.location, $3, 0);
1655                   $$ = build_new_loop (body);
1656                 }
1657 ;
1658
1659 while_statement:
1660         while_expression statement
1661                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1662 |       WHILE_TK error
1663                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1664 |       WHILE_TK OP_TK error
1665                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1666 |       WHILE_TK OP_TK expression error
1667                 {yyerror ("')' expected"); RECOVER;}
1668 ;
1669
1670 while_statement_nsi:
1671         while_expression statement_nsi
1672                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1673 ;
1674
1675 do_statement_begin:
1676         DO_TK
1677                 {
1678                   tree body = build_loop_body (0, NULL_TREE, 1);
1679                   $$ = build_new_loop (body);
1680                 }
1681         /* Need error handing here. FIXME */
1682 ;
1683
1684 do_statement:
1685         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1686                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1687 ;
1688
1689 for_statement:
1690         for_begin SC_TK expression SC_TK for_update CP_TK statement
1691                 {
1692                   if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1693                     $3 = build_wfl_node ($3);
1694                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1695                 }
1696 |       for_begin SC_TK SC_TK for_update CP_TK statement
1697                 {
1698                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1699                   /* We have not condition, so we get rid of the EXIT_EXPR */
1700                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1701                     empty_stmt_node;
1702                 }
1703 |       for_begin SC_TK error
1704                 {yyerror ("Invalid control expression"); RECOVER;}
1705 |       for_begin SC_TK expression SC_TK error
1706                 {yyerror ("Invalid update expression"); RECOVER;}
1707 |       for_begin SC_TK SC_TK error
1708                 {yyerror ("Invalid update expression"); RECOVER;}
1709 ;
1710
1711 for_statement_nsi:
1712         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1713                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1714 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1715                 {
1716                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1717                   /* We have not condition, so we get rid of the EXIT_EXPR */
1718                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1719                     empty_stmt_node;
1720                 }
1721 ;
1722
1723 for_header:
1724         FOR_TK OP_TK
1725                 {
1726                   /* This scope defined for local variable that may be
1727                      defined within the scope of the for loop */
1728                   enter_block ();
1729                 }
1730 |       FOR_TK error
1731                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1732 |       FOR_TK OP_TK error
1733                 {yyerror ("Invalid init statement"); RECOVER;}
1734 ;
1735
1736 for_begin:
1737         for_header for_init
1738                 {
1739                   /* We now declare the loop body. The loop is
1740                      declared as a for loop. */
1741                   tree body = build_loop_body (0, NULL_TREE, 0);
1742                   $$ =  build_new_loop (body);
1743                   FOR_LOOP_P ($$) = 1;
1744                   /* The loop is added to the current block the for
1745                      statement is defined within */
1746                   java_method_add_stmt (current_function_decl, $$);
1747                 }
1748 ;
1749 for_init:                       /* Can be empty */
1750                 { $$ = empty_stmt_node; }
1751 |       statement_expression_list
1752                 {
1753                   /* Init statement recorded within the previously
1754                      defined block scope */
1755                   $$ = java_method_add_stmt (current_function_decl, $1);
1756                 }
1757 |       local_variable_declaration
1758                 {
1759                   /* Local variable are recorded within the previously
1760                      defined block scope */
1761                   $$ = NULL_TREE;
1762                 }
1763 |       statement_expression_list error
1764                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1765 ;
1766
1767 for_update:                     /* Can be empty */
1768                 {$$ = empty_stmt_node;}
1769 |       statement_expression_list
1770                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1771 ;
1772
1773 statement_expression_list:
1774         statement_expression
1775                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1776 |       statement_expression_list C_TK statement_expression
1777                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1778 |       statement_expression_list C_TK error
1779                 {yyerror ("Missing term"); RECOVER;}
1780 ;
1781
1782 break_statement:
1783         BREAK_TK SC_TK
1784                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1785 |       BREAK_TK identifier SC_TK
1786                 { $$ = build_bc_statement ($1.location, 1, $2); }
1787 |       BREAK_TK error
1788                 {yyerror ("Missing term"); RECOVER;}
1789 |       BREAK_TK identifier error
1790                 {yyerror ("';' expected"); RECOVER;}
1791 ;
1792
1793 continue_statement:
1794         CONTINUE_TK SC_TK
1795                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1796 |       CONTINUE_TK identifier SC_TK
1797                 { $$ = build_bc_statement ($1.location, 0, $2); }
1798 |       CONTINUE_TK error
1799                 {yyerror ("Missing term"); RECOVER;}
1800 |       CONTINUE_TK identifier error
1801                 {yyerror ("';' expected"); RECOVER;}
1802 ;
1803
1804 return_statement:
1805         RETURN_TK SC_TK
1806                 { $$ = build_return ($1.location, NULL_TREE); }
1807 |       RETURN_TK expression SC_TK
1808                 { $$ = build_return ($1.location, $2); }
1809 |       RETURN_TK error
1810                 {yyerror ("Missing term"); RECOVER;}
1811 |       RETURN_TK expression error
1812                 {yyerror ("';' expected"); RECOVER;}
1813 ;
1814
1815 throw_statement:
1816         THROW_TK expression SC_TK
1817                 {
1818                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1819                   EXPR_WFL_LINECOL ($$) = $1.location;
1820                 }
1821 |       THROW_TK error
1822                 {yyerror ("Missing term"); RECOVER;}
1823 |       THROW_TK expression error
1824                 {yyerror ("';' expected"); RECOVER;}
1825 ;
1826
1827 assert_statement:
1828         ASSERT_TK expression REL_CL_TK expression SC_TK
1829                 {
1830                   $$ = build_assertion ($1.location, $2, $4);
1831                 }
1832 |       ASSERT_TK expression SC_TK
1833                 {
1834                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1835                 }
1836 |       ASSERT_TK error
1837                 {yyerror ("Missing term"); RECOVER;}
1838 |       ASSERT_TK expression error
1839                 {yyerror ("';' expected"); RECOVER;}
1840 ;
1841
1842 synchronized_statement:
1843         synchronized OP_TK expression CP_TK block
1844                 {
1845                   $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1846                   EXPR_WFL_LINECOL ($$) =
1847                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1848                 }
1849 |       synchronized OP_TK expression CP_TK error
1850                 {yyerror ("'{' expected"); RECOVER;}
1851 |       synchronized error
1852                 {yyerror ("'(' expected"); RECOVER;}
1853 |       synchronized OP_TK error CP_TK
1854                 {yyerror ("Missing term"); RECOVER;}
1855 |       synchronized OP_TK error
1856                 {yyerror ("Missing term"); RECOVER;}
1857 ;
1858
1859 synchronized:
1860         modifiers
1861                 {
1862                   check_modifiers (
1863              "Illegal modifier `%s'. Only `synchronized' was expected here",
1864                                    $1, ACC_SYNCHRONIZED);
1865                   if ($1 != ACC_SYNCHRONIZED)
1866                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1867                       build_wfl_node (NULL_TREE);
1868                 }
1869 ;
1870
1871 try_statement:
1872         TRY_TK block catches
1873                 { $$ = build_try_statement ($1.location, $2, $3); }
1874 |       TRY_TK block finally
1875                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1876 |       TRY_TK block catches finally
1877                 { $$ = build_try_finally_statement
1878                     ($1.location, build_try_statement ($1.location,
1879                                                        $2, $3), $4);
1880                 }
1881 |       TRY_TK error
1882                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1883 ;
1884
1885 catches:
1886         catch_clause
1887 |       catches catch_clause
1888                 {
1889                   TREE_CHAIN ($2) = $1;
1890                   $$ = $2;
1891                 }
1892 ;
1893
1894 catch_clause:
1895         catch_clause_parameter block
1896                 {
1897                   java_method_add_stmt (current_function_decl, $2);
1898                   exit_block ();
1899                   $$ = $1;
1900                 }
1901 ;
1902
1903 catch_clause_parameter:
1904         CATCH_TK OP_TK formal_parameter CP_TK
1905                 {
1906                   /* We add a block to define a scope for
1907                      formal_parameter (CCBP). The formal parameter is
1908                      declared initialized by the appropriate function
1909                      call */
1910                   tree ccpb = enter_block ();
1911                   tree init = build_assignment
1912                     (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1913                      build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1914                   declare_local_variables (0, TREE_VALUE ($3),
1915                                            build_tree_list (TREE_PURPOSE ($3),
1916                                                             init));
1917                   $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1918                   EXPR_WFL_LINECOL ($$) = $1.location;
1919                 }
1920 |       CATCH_TK error
1921                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1922 |       CATCH_TK OP_TK error
1923                 {
1924                   yyerror ("Missing term or ')' expected");
1925                   RECOVER; $$ = NULL_TREE;
1926                 }
1927 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1928                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1929 ;
1930
1931 finally:
1932         FINALLY_TK block
1933                 { $$ = $2; }
1934 |       FINALLY_TK error
1935                 {yyerror ("'{' expected"); RECOVER; }
1936 ;
1937
1938 /* 19.12 Production from 15: Expressions  */
1939 primary:
1940         primary_no_new_array
1941 |       array_creation_expression
1942 ;
1943
1944 primary_no_new_array:
1945         literal
1946 |       THIS_TK
1947                 { $$ = build_this ($1.location); }
1948 |       OP_TK expression CP_TK
1949                 {$$ = $2;}
1950 |       class_instance_creation_expression
1951 |       field_access
1952 |       method_invocation
1953 |       array_access
1954 |       type_literals
1955         /* Added, JDK1.1 inner classes. Documentation is wrong
1956            refering to a 'ClassName' (class_name) rule that doesn't
1957            exist. Used name: instead.  */
1958 |       name DOT_TK THIS_TK
1959                 {
1960                   tree wfl = build_wfl_node (this_identifier_node);
1961                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1962                 }
1963 |       OP_TK expression error
1964                 {yyerror ("')' expected"); RECOVER;}
1965 |       name DOT_TK error
1966                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1967 |       primitive_type DOT_TK error
1968                 {yyerror ("'class' expected" ); RECOVER;}
1969 |       VOID_TK DOT_TK error
1970                 {yyerror ("'class' expected" ); RECOVER;}
1971 ;
1972
1973 type_literals:
1974         name DOT_TK CLASS_TK
1975                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1976 |       array_type DOT_TK CLASS_TK
1977                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1978 |       primitive_type DOT_TK CLASS_TK
1979                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1980 |       VOID_TK DOT_TK CLASS_TK
1981                 {
1982                    $$ = build_incomplete_class_ref ($2.location,
1983                                                    void_type_node);
1984                 }
1985 ;
1986
1987 class_instance_creation_expression:
1988         NEW_TK class_type OP_TK argument_list CP_TK
1989                 { $$ = build_new_invocation ($2, $4); }
1990 |       NEW_TK class_type OP_TK CP_TK
1991                 { $$ = build_new_invocation ($2, NULL_TREE); }
1992 |       anonymous_class_creation
1993         /* Added, JDK1.1 inner classes, modified to use name or
1994            primary instead of primary solely which couldn't work in
1995            all situations.  */
1996 |       something_dot_new identifier OP_TK CP_TK
1997                 {
1998                   tree ctor = build_new_invocation ($2, NULL_TREE);
1999                   $$ = make_qualified_primary ($1, ctor,
2000                                                EXPR_WFL_LINECOL ($1));
2001                 }
2002 |       something_dot_new identifier OP_TK CP_TK class_body
2003 |       something_dot_new identifier OP_TK argument_list CP_TK
2004                 {
2005                   tree ctor = build_new_invocation ($2, $4);
2006                   $$ = make_qualified_primary ($1, ctor,
2007                                                EXPR_WFL_LINECOL ($1));
2008                 }
2009 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2010 |       NEW_TK error SC_TK
2011                 {yyerror ("'(' expected"); DRECOVER(new_1);}
2012 |       NEW_TK class_type error
2013                 {yyerror ("'(' expected"); RECOVER;}
2014 |       NEW_TK class_type OP_TK error
2015                 {yyerror ("')' or term expected"); RECOVER;}
2016 |       NEW_TK class_type OP_TK argument_list error
2017                 {yyerror ("')' expected"); RECOVER;}
2018 |       something_dot_new error
2019                 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2020 |       something_dot_new identifier error
2021                 {yyerror ("'(' expected"); RECOVER;}
2022 ;
2023
2024 /* Created after JDK1.1 rules originally added to
2025    class_instance_creation_expression, but modified to use
2026    'class_type' instead of 'TypeName' (type_name) which is mentionned
2027    in the documentation but doesn't exist. */
2028
2029 anonymous_class_creation:
2030         NEW_TK class_type OP_TK argument_list CP_TK
2031                 { create_anonymous_class ($1.location, $2); }
2032         class_body
2033                 {
2034                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2035                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2036
2037                   end_class_declaration (1);
2038
2039                   /* Now we can craft the new expression */
2040                   $$ = build_new_invocation (id, $4);
2041
2042                   /* Note that we can't possibly be here if
2043                      `class_type' is an interface (in which case the
2044                      anonymous class extends Object and implements
2045                      `class_type', hence its constructor can't have
2046                      arguments.) */
2047
2048                   /* Otherwise, the innerclass must feature a
2049                      constructor matching `argument_list'. Anonymous
2050                      classes are a bit special: it's impossible to
2051                      define constructor for them, hence constructors
2052                      must be generated following the hints provided by
2053                      the `new' expression. Whether a super constructor
2054                      of that nature exists or not is to be verified
2055                      later on in verify_constructor_super.
2056
2057                      It's during the expansion of a `new' statement
2058                      refering to an anonymous class that a ctor will
2059                      be generated for the anonymous class, with the
2060                      right arguments. */
2061
2062                 }
2063 |       NEW_TK class_type OP_TK CP_TK
2064                 { create_anonymous_class ($1.location, $2); }
2065         class_body
2066                 {
2067                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2068                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2069
2070                   end_class_declaration (1);
2071
2072                   /* Now we can craft the new expression. The
2073                      statement doesn't need to be remember so that a
2074                      constructor can be generated, since its signature
2075                      is already known. */
2076                   $$ = build_new_invocation (id, NULL_TREE);
2077                 }
2078 ;
2079
2080 something_dot_new:              /* Added, not part of the specs. */
2081         name DOT_TK NEW_TK
2082                 { $$ = $1; }
2083 |       primary DOT_TK NEW_TK
2084                 { $$ = $1; }
2085 ;
2086
2087 argument_list:
2088         expression
2089                 {
2090                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2091                   ctxp->formal_parameter_number = 1;
2092                 }
2093 |       argument_list C_TK expression
2094                 {
2095                   ctxp->formal_parameter_number += 1;
2096                   $$ = tree_cons (NULL_TREE, $3, $1);
2097                 }
2098 |       argument_list C_TK error
2099                 {yyerror ("Missing term"); RECOVER;}
2100 ;
2101
2102 array_creation_expression:
2103         NEW_TK primitive_type dim_exprs
2104                 { $$ = build_newarray_node ($2, $3, 0); }
2105 |       NEW_TK class_or_interface_type dim_exprs
2106                 { $$ = build_newarray_node ($2, $3, 0); }
2107 |       NEW_TK primitive_type dim_exprs dims
2108                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2109 |       NEW_TK class_or_interface_type dim_exprs dims
2110                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2111         /* Added, JDK1.1 anonymous array. Initial documentation rule
2112            modified */
2113 |       NEW_TK class_or_interface_type dims array_initializer
2114                 {
2115                   char *sig;
2116                   int osb = pop_current_osb (ctxp);
2117                   while (osb--)
2118                     obstack_grow (&temporary_obstack, "[]", 2);
2119                   obstack_1grow (&temporary_obstack, '\0');
2120                   sig = obstack_finish (&temporary_obstack);
2121                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2122                               $2, get_identifier (sig), $4);
2123                 }
2124 |       NEW_TK primitive_type dims array_initializer
2125                 {
2126                   int osb = pop_current_osb (ctxp);
2127                   tree type = $2;
2128                   while (osb--)
2129                     type = build_java_array_type (type, -1);
2130                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2131                               build_pointer_type (type), NULL_TREE, $4);
2132                 }
2133 |       NEW_TK error CSB_TK
2134                 {yyerror ("'[' expected"); DRECOVER ("]");}
2135 |       NEW_TK error OSB_TK
2136                 {yyerror ("']' expected"); RECOVER;}
2137 ;
2138
2139 dim_exprs:
2140         dim_expr
2141                 { $$ = build_tree_list (NULL_TREE, $1); }
2142 |       dim_exprs dim_expr
2143                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2144 ;
2145
2146 dim_expr:
2147         OSB_TK expression CSB_TK
2148                 {
2149                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2150                     {
2151                       $2 = build_wfl_node ($2);
2152                       TREE_TYPE ($2) = NULL_TREE;
2153                     }
2154                   EXPR_WFL_LINECOL ($2) = $1.location;
2155                   $$ = $2;
2156                 }
2157 |       OSB_TK expression error
2158                 {yyerror ("']' expected"); RECOVER;}
2159 |       OSB_TK error
2160                 {
2161                   yyerror ("Missing term");
2162                   yyerror ("']' expected");
2163                   RECOVER;
2164                 }
2165 ;
2166
2167 dims:
2168         OSB_TK CSB_TK
2169                 {
2170                   int allocate = 0;
2171                   /* If not initialized, allocate memory for the osb
2172                      numbers stack */
2173                   if (!ctxp->osb_limit)
2174                     {
2175                       allocate = ctxp->osb_limit = 32;
2176                       ctxp->osb_depth = -1;
2177                     }
2178                   /* If capacity overflown, reallocate a bigger chunk */
2179                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2180                     allocate = ctxp->osb_limit << 1;
2181
2182                   if (allocate)
2183                     {
2184                       allocate *= sizeof (int);
2185                       if (ctxp->osb_number)
2186                         ctxp->osb_number = xrealloc (ctxp->osb_number,
2187                                                      allocate);
2188                       else
2189                         ctxp->osb_number = xmalloc (allocate);
2190                     }
2191                   ctxp->osb_depth++;
2192                   CURRENT_OSB (ctxp) = 1;
2193                 }
2194 |       dims OSB_TK CSB_TK
2195                 { CURRENT_OSB (ctxp)++; }
2196 |       dims OSB_TK error
2197                 { yyerror ("']' expected"); RECOVER;}
2198 ;
2199
2200 field_access:
2201         primary DOT_TK identifier
2202                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2203                 /*  FIXME - REWRITE TO:
2204                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2205 |       SUPER_TK DOT_TK identifier
2206                 {
2207                   tree super_wfl = build_wfl_node (super_identifier_node);
2208                   EXPR_WFL_LINECOL (super_wfl) = $1.location;
2209                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2210                 }
2211 |       SUPER_TK error
2212                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2213 ;
2214
2215 method_invocation:
2216         name OP_TK CP_TK
2217                 { $$ = build_method_invocation ($1, NULL_TREE); }
2218 |       name OP_TK argument_list CP_TK
2219                 { $$ = build_method_invocation ($1, $3); }
2220 |       primary DOT_TK identifier OP_TK CP_TK
2221                 {
2222                   if (TREE_CODE ($1) == THIS_EXPR)
2223                     $$ = build_this_super_qualified_invocation
2224                       (1, $3, NULL_TREE, 0, $2.location);
2225                   else
2226                     {
2227                       tree invok = build_method_invocation ($3, NULL_TREE);
2228                       $$ = make_qualified_primary ($1, invok, $2.location);
2229                     }
2230                 }
2231 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2232                 {
2233                   if (TREE_CODE ($1) == THIS_EXPR)
2234                     $$ = build_this_super_qualified_invocation
2235                       (1, $3, $5, 0, $2.location);
2236                   else
2237                     {
2238                       tree invok = build_method_invocation ($3, $5);
2239                       $$ = make_qualified_primary ($1, invok, $2.location);
2240                     }
2241                 }
2242 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2243                 {
2244                   $$ = build_this_super_qualified_invocation
2245                     (0, $3, NULL_TREE, $1.location, $2.location);
2246                 }
2247 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2248                 {
2249                   $$ = build_this_super_qualified_invocation
2250                     (0, $3, $5, $1.location, $2.location);
2251                 }
2252         /* Screws up thing. I let it here until I'm convinced it can
2253            be removed. FIXME
2254 |       primary DOT_TK error
2255                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2256 |       SUPER_TK DOT_TK error CP_TK
2257                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2258 |       SUPER_TK DOT_TK error DOT_TK
2259                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2260 ;
2261
2262 array_access:
2263         name OSB_TK expression CSB_TK
2264                 { $$ = build_array_ref ($2.location, $1, $3); }
2265 |       primary_no_new_array OSB_TK expression CSB_TK
2266                 { $$ = build_array_ref ($2.location, $1, $3); }
2267 |       name OSB_TK error
2268                 {
2269                   yyerror ("Missing term and ']' expected");
2270                   DRECOVER(array_access);
2271                 }
2272 |       name OSB_TK expression error
2273                 {
2274                   yyerror ("']' expected");
2275                   DRECOVER(array_access);
2276                 }
2277 |       primary_no_new_array OSB_TK error
2278                 {
2279                   yyerror ("Missing term and ']' expected");
2280                   DRECOVER(array_access);
2281                 }
2282 |       primary_no_new_array OSB_TK expression error
2283                 {
2284                   yyerror ("']' expected");
2285                   DRECOVER(array_access);
2286                 }
2287 ;
2288
2289 postfix_expression:
2290         primary
2291 |       name
2292 |       post_increment_expression
2293 |       post_decrement_expression
2294 ;
2295
2296 post_increment_expression:
2297         postfix_expression INCR_TK
2298                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2299 ;
2300
2301 post_decrement_expression:
2302         postfix_expression DECR_TK
2303                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2304 ;
2305
2306 trap_overflow_corner_case:
2307         pre_increment_expression
2308 |       pre_decrement_expression
2309 |       PLUS_TK unary_expression
2310                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 |       unary_expression_not_plus_minus
2312 |       PLUS_TK error
2313                 {yyerror ("Missing term"); RECOVER}
2314 ;
2315
2316 unary_expression:
2317         trap_overflow_corner_case
2318                 {
2319                   error_if_numeric_overflow ($1);
2320                   $$ = $1;
2321                 }
2322 |       MINUS_TK trap_overflow_corner_case
2323                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2324 |       MINUS_TK error
2325                 {yyerror ("Missing term"); RECOVER}
2326 ;
2327
2328 pre_increment_expression:
2329         INCR_TK unary_expression
2330                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2331 |       INCR_TK error
2332                 {yyerror ("Missing term"); RECOVER}
2333 ;
2334
2335 pre_decrement_expression:
2336         DECR_TK unary_expression
2337                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2338 |       DECR_TK error
2339                 {yyerror ("Missing term"); RECOVER}
2340 ;
2341
2342 unary_expression_not_plus_minus:
2343         postfix_expression
2344 |       NOT_TK unary_expression
2345                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2346 |       NEG_TK unary_expression
2347                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2348 |       cast_expression
2349 |       NOT_TK error
2350                 {yyerror ("Missing term"); RECOVER}
2351 |       NEG_TK error
2352                 {yyerror ("Missing term"); RECOVER}
2353 ;
2354
2355 cast_expression:                /* Error handling here is potentially weak */
2356         OP_TK primitive_type dims CP_TK unary_expression
2357                 {
2358                   tree type = $2;
2359                   int osb = pop_current_osb (ctxp);
2360                   while (osb--)
2361                     type = build_java_array_type (type, -1);
2362                   $$ = build_cast ($1.location, type, $5);
2363                 }
2364 |       OP_TK primitive_type CP_TK unary_expression
2365                 { $$ = build_cast ($1.location, $2, $4); }
2366 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2367                 { $$ = build_cast ($1.location, $2, $4); }
2368 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2369                 {
2370                   const char *ptr;
2371                   int osb = pop_current_osb (ctxp);
2372                   obstack_grow (&temporary_obstack,
2373                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2374                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2375                   while (osb--)
2376                     obstack_grow (&temporary_obstack, "[]", 2);
2377                   obstack_1grow (&temporary_obstack, '\0');
2378                   ptr = obstack_finish (&temporary_obstack);
2379                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2380                   $$ = build_cast ($1.location, $2, $5);
2381                 }
2382 |       OP_TK primitive_type OSB_TK error
2383                 {yyerror ("']' expected, invalid type expression");}
2384 |       OP_TK error
2385                 {
2386                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2387                   RECOVER;
2388                 }
2389 |       OP_TK primitive_type dims CP_TK error
2390                 {yyerror ("Missing term"); RECOVER;}
2391 |       OP_TK primitive_type CP_TK error
2392                 {yyerror ("Missing term"); RECOVER;}
2393 |       OP_TK name dims CP_TK error
2394                 {yyerror ("Missing term"); RECOVER;}
2395 ;
2396
2397 multiplicative_expression:
2398         unary_expression
2399 |       multiplicative_expression MULT_TK unary_expression
2400                 {
2401                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2402                                     $2.location, $1, $3);
2403                 }
2404 |       multiplicative_expression DIV_TK unary_expression
2405                 {
2406                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2407                                     $1, $3);
2408                 }
2409 |       multiplicative_expression REM_TK unary_expression
2410                 {
2411                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412                                     $1, $3);
2413                 }
2414 |       multiplicative_expression MULT_TK error
2415                 {yyerror ("Missing term"); RECOVER;}
2416 |       multiplicative_expression DIV_TK error
2417                 {yyerror ("Missing term"); RECOVER;}
2418 |       multiplicative_expression REM_TK error
2419                 {yyerror ("Missing term"); RECOVER;}
2420 ;
2421
2422 additive_expression:
2423         multiplicative_expression
2424 |       additive_expression PLUS_TK multiplicative_expression
2425                 {
2426                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2427                                     $1, $3);
2428                 }
2429 |       additive_expression MINUS_TK multiplicative_expression
2430                 {
2431                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2432                                     $1, $3);
2433                 }
2434 |       additive_expression PLUS_TK error
2435                 {yyerror ("Missing term"); RECOVER;}
2436 |       additive_expression MINUS_TK error
2437                 {yyerror ("Missing term"); RECOVER;}
2438 ;
2439
2440 shift_expression:
2441         additive_expression
2442 |       shift_expression LS_TK additive_expression
2443                 {
2444                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445                                     $1, $3);
2446                 }
2447 |       shift_expression SRS_TK additive_expression
2448                 {
2449                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2450                                     $1, $3);
2451                 }
2452 |       shift_expression ZRS_TK additive_expression
2453                 {
2454                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2455                                     $1, $3);
2456                 }
2457 |       shift_expression LS_TK error
2458                 {yyerror ("Missing term"); RECOVER;}
2459 |       shift_expression SRS_TK error
2460                 {yyerror ("Missing term"); RECOVER;}
2461 |       shift_expression ZRS_TK error
2462                 {yyerror ("Missing term"); RECOVER;}
2463 ;
2464
2465 relational_expression:
2466         shift_expression
2467 |       relational_expression LT_TK shift_expression
2468                 {
2469                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2470                                     $1, $3);
2471                 }
2472 |       relational_expression GT_TK shift_expression
2473                 {
2474                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475                                     $1, $3);
2476                 }
2477 |       relational_expression LTE_TK shift_expression
2478                 {
2479                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2480                                     $1, $3);
2481                 }
2482 |       relational_expression GTE_TK shift_expression
2483                 {
2484                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2485                                     $1, $3);
2486                 }
2487 |       relational_expression INSTANCEOF_TK reference_type
2488                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2489 |       relational_expression LT_TK error
2490                 {yyerror ("Missing term"); RECOVER;}
2491 |       relational_expression GT_TK error
2492                 {yyerror ("Missing term"); RECOVER;}
2493 |       relational_expression LTE_TK error
2494                 {yyerror ("Missing term"); RECOVER;}
2495 |       relational_expression GTE_TK error
2496                 {yyerror ("Missing term"); RECOVER;}
2497 |       relational_expression INSTANCEOF_TK error
2498                 {yyerror ("Invalid reference type"); RECOVER;}
2499 ;
2500
2501 equality_expression:
2502         relational_expression
2503 |       equality_expression EQ_TK relational_expression
2504                 {
2505                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506                                     $1, $3);
2507                 }
2508 |       equality_expression NEQ_TK relational_expression
2509                 {
2510                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511                                     $1, $3);
2512                 }
2513 |       equality_expression EQ_TK error
2514                 {yyerror ("Missing term"); RECOVER;}
2515 |       equality_expression NEQ_TK error
2516                 {yyerror ("Missing term"); RECOVER;}
2517 ;
2518
2519 and_expression:
2520         equality_expression
2521 |       and_expression AND_TK equality_expression
2522                 {
2523                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524                                     $1, $3);
2525                 }
2526 |       and_expression AND_TK error
2527                 {yyerror ("Missing term"); RECOVER;}
2528 ;
2529
2530 exclusive_or_expression:
2531         and_expression
2532 |       exclusive_or_expression XOR_TK and_expression
2533                 {
2534                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535                                     $1, $3);
2536                 }
2537 |       exclusive_or_expression XOR_TK error
2538                 {yyerror ("Missing term"); RECOVER;}
2539 ;
2540
2541 inclusive_or_expression:
2542         exclusive_or_expression
2543 |       inclusive_or_expression OR_TK exclusive_or_expression
2544                 {
2545                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2546                                     $1, $3);
2547                 }
2548 |       inclusive_or_expression OR_TK error
2549                 {yyerror ("Missing term"); RECOVER;}
2550 ;
2551
2552 conditional_and_expression:
2553         inclusive_or_expression
2554 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2555                 {
2556                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2557                                     $1, $3);
2558                 }
2559 |       conditional_and_expression BOOL_AND_TK error
2560                 {yyerror ("Missing term"); RECOVER;}
2561 ;
2562
2563 conditional_or_expression:
2564         conditional_and_expression
2565 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2566                 {
2567                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2568                                     $1, $3);
2569                 }
2570 |       conditional_or_expression BOOL_OR_TK error
2571                 {yyerror ("Missing term"); RECOVER;}
2572 ;
2573
2574 conditional_expression:         /* Error handling here is weak */
2575         conditional_or_expression
2576 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2577                 {
2578                   $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2579                   EXPR_WFL_LINECOL ($$) = $2.location;
2580                 }
2581 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2582                 {
2583                   YYERROR_NOW;
2584                   yyerror ("Missing term");
2585                   DRECOVER (1);
2586                 }
2587 |       conditional_or_expression REL_QM_TK error
2588                 {yyerror ("Missing term"); DRECOVER (2);}
2589 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2590                 {yyerror ("Missing term"); DRECOVER (3);}
2591 ;
2592
2593 assignment_expression:
2594         conditional_expression
2595 |       assignment
2596 ;
2597
2598 assignment:
2599         left_hand_side assignment_operator assignment_expression
2600                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2601 |       left_hand_side assignment_operator error
2602                 {
2603                   YYNOT_TWICE yyerror ("Missing term");
2604                   DRECOVER (assign);
2605                 }
2606 ;
2607
2608 left_hand_side:
2609         name
2610 |       field_access
2611 |       array_access
2612 ;
2613
2614 assignment_operator:
2615         ASSIGN_ANY_TK
2616 |       ASSIGN_TK
2617 ;
2618
2619 expression:
2620         assignment_expression
2621 ;
2622
2623 constant_expression:
2624         expression
2625 ;
2626
2627 %%
2628
2629 /* Helper function to retrieve an OSB count. Should be used when the
2630    `dims:' rule is being used.  */
2631
2632 static int
2633 pop_current_osb (struct parser_ctxt *ctxp)
2634 {
2635   int to_return;
2636
2637   if (ctxp->osb_depth < 0)
2638     abort ();
2639
2640   to_return = CURRENT_OSB (ctxp);
2641   ctxp->osb_depth--;
2642
2643   return to_return;
2644 }
2645
2646 \f
2647
2648 /* This section of the code deal with save/restoring parser contexts.
2649    Add mode documentation here. FIXME */
2650
2651 /* Helper function. Create a new parser context. With
2652    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2653    context is copied, otherwise, the new context is zeroed. The newly
2654    created context becomes the current one.  */
2655
2656 static void
2657 create_new_parser_context (int copy_from_previous)
2658 {
2659   struct parser_ctxt *new;
2660
2661   new = ggc_alloc (sizeof (struct parser_ctxt));
2662   if (copy_from_previous)
2663     {
2664       memcpy (new, ctxp, sizeof (struct parser_ctxt));
2665       new->saved_data_ctx = 1;
2666     }
2667   else
2668     memset (new, 0, sizeof (struct parser_ctxt));
2669
2670   new->next = ctxp;
2671   ctxp = new;
2672 }
2673
2674 /* Create a new parser context and make it the current one. */
2675
2676 void
2677 java_push_parser_context (void)
2678 {
2679   create_new_parser_context (0);
2680 }
2681
2682 void
2683 java_pop_parser_context (int generate)
2684 {
2685   tree current;
2686   struct parser_ctxt *toFree, *next;
2687
2688   if (!ctxp)
2689     return;
2690
2691   toFree = ctxp;
2692   next = ctxp->next;
2693   if (next)
2694     {
2695       lineno = ctxp->lineno;
2696       current_class = ctxp->class_type;
2697     }
2698
2699   /* If the old and new lexers differ, then free the old one.  */
2700   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2701     java_destroy_lexer (ctxp->lexer);
2702
2703   /* Set the single import class file flag to 0 for the current list
2704      of imported things */
2705   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2706     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2707
2708   /* And restore those of the previous context */
2709   if ((ctxp = next))            /* Assignment is really meant here */
2710     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2711       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2712
2713   /* If we pushed a context to parse a class intended to be generated,
2714      we keep it so we can remember the class. What we could actually
2715      do is to just update a list of class names.  */
2716   if (generate)
2717     {
2718       toFree->next = ctxp_for_generation;
2719       ctxp_for_generation = toFree;
2720     }
2721 }
2722
2723 /* Create a parser context for the use of saving some global
2724    variables.  */
2725
2726 void
2727 java_parser_context_save_global (void)
2728 {
2729   if (!ctxp)
2730     {
2731       java_push_parser_context ();
2732       ctxp->saved_data_ctx = 1;
2733     }
2734
2735   /* If this context already stores data, create a new one suitable
2736      for data storage. */
2737   else if (ctxp->saved_data)
2738     create_new_parser_context (1);
2739
2740   ctxp->lineno = lineno;
2741   ctxp->class_type = current_class;
2742   ctxp->filename = input_filename;
2743   ctxp->function_decl = current_function_decl;
2744   ctxp->saved_data = 1;
2745 }
2746
2747 /* Restore some global variables from the previous context. Make the
2748    previous context the current one.  */
2749
2750 void
2751 java_parser_context_restore_global (void)
2752 {
2753   lineno = ctxp->lineno;
2754   current_class = ctxp->class_type;
2755   input_filename = ctxp->filename;
2756   if (wfl_operator)
2757     {
2758       tree s;
2759       BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2760       EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2761     }
2762   current_function_decl = ctxp->function_decl;
2763   ctxp->saved_data = 0;
2764   if (ctxp->saved_data_ctx)
2765     java_pop_parser_context (0);
2766 }
2767
2768 /* Suspend vital data for the current class/function being parsed so
2769    that an other class can be parsed. Used to let local/anonymous
2770    classes be parsed.  */
2771
2772 static void
2773 java_parser_context_suspend (void)
2774 {
2775   /* This makes debugging through java_debug_context easier */
2776   static const char *const name = "<inner buffer context>";
2777
2778   /* Duplicate the previous context, use it to save the globals we're
2779      interested in */
2780   create_new_parser_context (1);
2781   ctxp->function_decl = current_function_decl;
2782   ctxp->class_type = current_class;
2783
2784   /* Then create a new context which inherits all data from the
2785      previous one. This will be the new current context  */
2786   create_new_parser_context (1);
2787
2788   /* Help debugging */
2789   ctxp->next->filename = name;
2790 }
2791
2792 /* Resume vital data for the current class/function being parsed so
2793    that an other class can be parsed. Used to let local/anonymous
2794    classes be parsed.  The trick is the data storing file position
2795    informations must be restored to their current value, so parsing
2796    can resume as if no context was ever saved. */
2797
2798 static void
2799 java_parser_context_resume (void)
2800 {
2801   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2802   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2803   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2804
2805   /* We need to inherit the list of classes to complete/generate */
2806   restored->classd_list = old->classd_list;
2807   restored->class_list = old->class_list;
2808
2809   /* Restore the current class and function from the saver */
2810   current_class = saver->class_type;
2811   current_function_decl = saver->function_decl;
2812
2813   /* Retrive the restored context */
2814   ctxp = restored;
2815
2816   /* Re-installed the data for the parsing to carry on */
2817   memcpy (&ctxp->marker_begining, &old->marker_begining,
2818           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2819 }
2820
2821 /* Add a new anchor node to which all statement(s) initializing static
2822    and non static initialized upon declaration field(s) will be
2823    linked.  */
2824
2825 static void
2826 java_parser_context_push_initialized_field (void)
2827 {
2828   tree node;
2829
2830   node = build_tree_list (NULL_TREE, NULL_TREE);
2831   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2832   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2833
2834   node = build_tree_list (NULL_TREE, NULL_TREE);
2835   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2836   CPC_INITIALIZER_LIST (ctxp) = node;
2837
2838   node = build_tree_list (NULL_TREE, NULL_TREE);
2839   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2840   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2841 }
2842
2843 /* Pop the lists of initialized field. If this lists aren't empty,
2844    remember them so we can use it to create and populate the finit$
2845    or <clinit> functions. */
2846
2847 static void
2848 java_parser_context_pop_initialized_field (void)
2849 {
2850   tree stmts;
2851   tree class_type = TREE_TYPE (GET_CPC ());
2852
2853   if (CPC_INITIALIZER_LIST (ctxp))
2854     {
2855       stmts = CPC_INITIALIZER_STMT (ctxp);
2856       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2857       if (stmts && !java_error_count)
2858         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2859     }
2860
2861   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2862     {
2863       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2864       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2865         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2866       /* Keep initialization in order to enforce 8.5 */
2867       if (stmts && !java_error_count)
2868         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2869     }
2870
2871   /* JDK 1.1 instance initializers */
2872   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2873     {
2874       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2875       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2876         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2877       if (stmts && !java_error_count)
2878         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2879     }
2880 }
2881
2882 static tree
2883 reorder_static_initialized (tree list)
2884 {
2885   /* We have to keep things in order. The alias initializer have to
2886      come first, then the initialized regular field, in reverse to
2887      keep them in lexical order. */
2888   tree marker, previous = NULL_TREE;
2889   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2890     if (TREE_CODE (marker) == TREE_LIST
2891         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2892       break;
2893
2894   /* No static initialized, the list is fine as is */
2895   if (!previous)
2896     list = TREE_CHAIN (marker);
2897
2898   /* No marker? reverse the whole list */
2899   else if (!marker)
2900     list = nreverse (list);
2901
2902   /* Otherwise, reverse what's after the marker and the new reordered
2903      sublist will replace the marker. */
2904   else
2905     {
2906       TREE_CHAIN (previous) = NULL_TREE;
2907       list = nreverse (list);
2908       list = chainon (TREE_CHAIN (marker), list);
2909     }
2910   return list;
2911 }
2912
2913 /* Helper functions to dump the parser context stack.  */
2914
2915 #define TAB_CONTEXT(C) \
2916   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2917
2918 static void
2919 java_debug_context_do (int tab)
2920 {
2921   struct parser_ctxt *copy = ctxp;
2922   while (copy)
2923     {
2924       TAB_CONTEXT (tab);
2925       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2926       TAB_CONTEXT (tab);
2927       fprintf (stderr, "filename: %s\n", copy->filename);
2928       TAB_CONTEXT (tab);
2929       fprintf (stderr, "lineno: %d\n", copy->lineno);
2930       TAB_CONTEXT (tab);
2931       fprintf (stderr, "package: %s\n",
2932                (copy->package ?
2933                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2934       TAB_CONTEXT (tab);
2935       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2936       TAB_CONTEXT (tab);
2937       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2938       copy = copy->next;
2939       tab += 2;
2940     }
2941 }
2942
2943 /* Dump the stacked up parser contexts. Intended to be called from a
2944    debugger.  */
2945
2946 void
2947 java_debug_context (void)
2948 {
2949   java_debug_context_do (0);
2950 }
2951
2952 \f
2953
2954 /* Flag for the error report routine to issue the error the first time
2955    it's called (overriding the default behavior which is to drop the
2956    first invocation and honor the second one, taking advantage of a
2957    richer context.  */
2958 static int force_error = 0;
2959
2960 /* Reporting an constructor invocation error.  */
2961 static void
2962 parse_ctor_invocation_error (void)
2963 {
2964   if (DECL_CONSTRUCTOR_P (current_function_decl))
2965     yyerror ("Constructor invocation must be first thing in a constructor");
2966   else
2967     yyerror ("Only constructors can invoke constructors");
2968 }
2969
2970 /* Reporting JDK1.1 features not implemented.  */
2971
2972 static tree
2973 parse_jdk1_1_error (const char *msg)
2974 {
2975   sorry (": `%s' JDK1.1(TM) feature", msg);
2976   java_error_count++;
2977   return empty_stmt_node;
2978 }
2979
2980 static int do_warning = 0;
2981
2982 void
2983 yyerror (const char *msg)
2984 {
2985   static java_lc elc;
2986   static int  prev_lineno;
2987   static const char *prev_msg;
2988
2989   int save_lineno;
2990   char *remainder, *code_from_source;
2991
2992   if (!force_error && prev_lineno == lineno)
2993     return;
2994
2995   /* Save current error location but report latter, when the context is
2996      richer.  */
2997   if (ctxp->java_error_flag == 0)
2998     {
2999       ctxp->java_error_flag = 1;
3000       elc = ctxp->elc;
3001       /* Do something to use the previous line if we're reaching the
3002          end of the file... */
3003 #ifdef VERBOSE_SKELETON
3004       printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3005 #endif
3006       return;
3007     }
3008
3009   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3010   if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3011     return;
3012
3013   ctxp->java_error_flag = 0;
3014   if (do_warning)
3015     java_warning_count++;
3016   else
3017     java_error_count++;
3018
3019   if (elc.col == 0 && msg && msg[1] == ';')
3020     {
3021       elc.col  = ctxp->p_line->char_col-1;
3022       elc.line = ctxp->p_line->lineno;
3023     }
3024
3025   save_lineno = lineno;
3026   prev_lineno = lineno = elc.line;
3027   prev_msg = msg;
3028
3029   code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3030   obstack_grow0 (&temporary_obstack,
3031                  code_from_source, strlen (code_from_source));
3032   remainder = obstack_finish (&temporary_obstack);
3033   if (do_warning)
3034     warning ("%s.\n%s", msg, remainder);
3035   else
3036     error ("%s.\n%s", msg, remainder);
3037
3038   /* This allow us to cheaply avoid an extra 'Invalid expression
3039      statement' error report when errors have been already reported on
3040      the same line. This occurs when we report an error but don't have
3041      a synchronization point other than ';', which
3042      expression_statement is the only one to take care of.  */
3043   ctxp->prevent_ese = lineno = save_lineno;
3044 }
3045
3046 static void
3047 issue_warning_error_from_context (tree cl, const char *msg, va_list ap)
3048 {
3049   const char *saved, *saved_input_filename;
3050   char buffer [4096];
3051   vsprintf (buffer, msg, ap);
3052   force_error = 1;
3053
3054   ctxp->elc.line = EXPR_WFL_LINENO (cl);
3055   ctxp->elc.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3056                     (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3057
3058   /* We have a CL, that's a good reason for using it if it contains data */
3059   saved = ctxp->filename;
3060   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3061     ctxp->filename = EXPR_WFL_FILENAME (cl);
3062   saved_input_filename = input_filename;
3063   input_filename = ctxp->filename;
3064   java_error (NULL);
3065   java_error (buffer);
3066   ctxp->filename = saved;
3067   input_filename = saved_input_filename;
3068   force_error = 0;
3069 }
3070
3071 /* Issue an error message at a current source line CL */
3072
3073 void
3074 parse_error_context (tree cl, const char *msg, ...)
3075 {
3076   va_list ap;
3077   va_start (ap, msg);
3078   issue_warning_error_from_context (cl, msg, ap);
3079   va_end (ap);
3080 }
3081
3082 /* Issue a warning at a current source line CL */
3083
3084 static void
3085 parse_warning_context (tree cl, const char *msg, ...)
3086 {
3087   va_list ap;
3088   va_start (ap, msg);
3089
3090   force_error = do_warning = 1;
3091   issue_warning_error_from_context (cl, msg, ap);
3092   do_warning = force_error = 0;
3093   va_end (ap);
3094 }
3095
3096 static tree
3097 find_expr_with_wfl (tree node)
3098 {
3099   while (node)
3100     {
3101       char code;
3102       tree to_return;
3103
3104       switch (TREE_CODE (node))
3105         {
3106         case BLOCK:
3107           node = BLOCK_EXPR_BODY (node);
3108           continue;
3109
3110         case COMPOUND_EXPR:
3111           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3112           if (to_return)
3113             return to_return;
3114           node = TREE_OPERAND (node, 1);
3115           continue;
3116
3117         case LOOP_EXPR:
3118           node = TREE_OPERAND (node, 0);
3119           continue;
3120
3121         case LABELED_BLOCK_EXPR:
3122           node = TREE_OPERAND (node, 1);
3123           continue;
3124
3125         default:
3126           code = TREE_CODE_CLASS (TREE_CODE (node));
3127           if (((code == '1') || (code == '2') || (code == 'e'))
3128               && EXPR_WFL_LINECOL (node))
3129             return node;
3130           return NULL_TREE;
3131         }
3132     }
3133   return NULL_TREE;
3134 }
3135
3136 /* Issue a missing return statement error. Uses METHOD to figure the
3137    last line of the method the error occurs in.  */
3138
3139 static void
3140 missing_return_error (tree method)
3141 {
3142   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3143   parse_error_context (wfl_operator, "Missing return statement");
3144 }
3145
3146 /* Issue an unreachable statement error. From NODE, find the next
3147    statement to report appropriately.  */
3148 static void
3149 unreachable_stmt_error (tree node)
3150 {
3151   /* Browse node to find the next expression node that has a WFL. Use
3152      the location to report the error */
3153   if (TREE_CODE (node) == COMPOUND_EXPR)
3154     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3155   else
3156     node = find_expr_with_wfl (node);
3157
3158   if (node)
3159     {
3160       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3161       parse_error_context (wfl_operator, "Unreachable statement");
3162     }
3163   else
3164     abort ();
3165 }
3166
3167 int
3168 java_report_errors (void)
3169 {
3170   if (java_error_count)
3171     fprintf (stderr, "%d error%s",
3172              java_error_count, (java_error_count == 1 ? "" : "s"));
3173   if (java_warning_count)
3174     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3175              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3176   if (java_error_count || java_warning_count)
3177     putc ('\n', stderr);
3178   return java_error_count;
3179 }
3180
3181 static char *
3182 java_accstring_lookup (int flags)
3183 {
3184   static char buffer [80];
3185 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3186
3187   /* Access modifier looked-up first for easier report on forbidden
3188      access. */
3189   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3190   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3191   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3192   if (flags & ACC_STATIC) COPY_RETURN ("static");
3193   if (flags & ACC_FINAL) COPY_RETURN ("final");
3194   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3195   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3196   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3197   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3198   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3199   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3200
3201   buffer [0] = '\0';
3202   return buffer;
3203 #undef COPY_RETURN
3204 }
3205
3206 /* Issuing error messages upon redefinition of classes, interfaces or
3207    variables. */
3208
3209 static void
3210 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3211 {
3212   parse_error_context (cl, "%s `%s' already defined in %s:%d",
3213                        context, IDENTIFIER_POINTER (id),
3214                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3215   /* Here we should point out where its redefined. It's a unicode. FIXME */
3216 }
3217
3218 static void
3219 variable_redefinition_error (tree context, tree name, tree type, int line)
3220 {
3221   const char *type_name;
3222
3223   /* Figure a proper name for type. We might haven't resolved it */
3224   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3225     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3226   else
3227     type_name = lang_printable_name (type, 0);
3228
3229   parse_error_context (context,
3230                        "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3231                        IDENTIFIER_POINTER (name),
3232                        type_name, IDENTIFIER_POINTER (name), line);
3233 }
3234
3235 /* If ANAME is terminated with `[]', it indicates an array. This
3236    function returns the number of `[]' found and if this number is
3237    greater than zero, it extracts the array type name and places it in
3238    the node pointed to by TRIMMED unless TRIMMED is null.  */
3239
3240 static int
3241 build_type_name_from_array_name (tree aname, tree *trimmed)
3242 {
3243   const char *name = IDENTIFIER_POINTER (aname);
3244   int len = IDENTIFIER_LENGTH (aname);
3245   int array_dims;
3246
3247   STRING_STRIP_BRACKETS (name, len, array_dims);
3248
3249   if (array_dims && trimmed)
3250     *trimmed = get_identifier_with_length (name, len);
3251
3252   return array_dims;
3253 }
3254
3255 static tree
3256 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3257 {
3258   int more_dims = 0;
3259
3260   /* Eventually get more dims */
3261   more_dims = build_type_name_from_array_name (name, &name);
3262
3263   /* If we have, then craft a new type for this variable */
3264   if (more_dims)
3265     {
3266       tree save = type;
3267
3268       /* If we have a pointer, use its type */
3269       if (TREE_CODE (type) == POINTER_TYPE)
3270         type = TREE_TYPE (type);
3271
3272       /* Building the first dimension of a primitive type uses this
3273          function */
3274       if (JPRIMITIVE_TYPE_P (type))
3275         {
3276           type = build_java_array_type (type, -1);
3277           more_dims--;
3278         }
3279       /* Otherwise, if we have a WFL for this type, use it (the type
3280          is already an array on an unresolved type, and we just keep
3281          on adding dimensions) */
3282       else if (type_wfl)
3283         {
3284           type = type_wfl;
3285           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3286                                                         NULL);
3287         }
3288
3289       /* Add all the dimensions */
3290       while (more_dims--)
3291         type = build_unresolved_array_type (type);
3292
3293       /* The type may have been incomplete in the first place */
3294       if (type_wfl)
3295         type = obtain_incomplete_type (type);
3296     }
3297
3298   if (ret_name)
3299     *ret_name = name;
3300   return type;
3301 }
3302
3303 /* Build something that the type identifier resolver will identify as
3304    being an array to an unresolved type. TYPE_WFL is a WFL on a
3305    identifier. */
3306
3307 static tree
3308 build_unresolved_array_type (tree type_or_wfl)
3309 {
3310   const char *ptr;
3311   tree wfl;
3312
3313   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3314      just create a array type */
3315   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3316     return build_java_array_type (type_or_wfl, -1);
3317
3318   obstack_grow (&temporary_obstack,
3319                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3320                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3321   obstack_grow0 (&temporary_obstack, "[]", 2);
3322   ptr = obstack_finish (&temporary_obstack);
3323   wfl = build_expr_wfl (get_identifier (ptr),
3324                         EXPR_WFL_FILENAME (type_or_wfl),
3325                         EXPR_WFL_LINENO (type_or_wfl),
3326                         EXPR_WFL_COLNO (type_or_wfl));
3327   /* Re-install the existing qualifications so that the type can be
3328      resolved properly. */
3329   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3330   return wfl;
3331 }
3332
3333 static void
3334 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3335 {
3336   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3337     parse_error_context (wfl, "Interface `%s' repeated",
3338                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3339 }
3340
3341 /* Bulk of common class/interface checks. Return 1 if an error was
3342    encountered. TAG is 0 for a class, 1 for an interface.  */
3343
3344 static int
3345 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3346                                 tree qualified_name, tree decl, tree cl)
3347 {
3348   tree node;
3349   int sca = 0;                  /* Static class allowed */
3350   int icaf = 0;                 /* Inner class allowed flags */
3351   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3352
3353   if (!quiet_flag)
3354     fprintf (stderr, " %s%s %s",
3355              (CPC_INNER_P () ? "inner" : ""),
3356              (is_interface ? "interface" : "class"),
3357              IDENTIFIER_POINTER (qualified_name));
3358
3359   /* Scope of an interface/class type name:
3360        - Can't be imported by a single type import
3361        - Can't already exists in the package */
3362   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3363       && (node = find_name_in_single_imports (raw_name))
3364       && !CPC_INNER_P ())
3365     {
3366       parse_error_context
3367         (cl, "%s name `%s' clashes with imported type `%s'",
3368          (is_interface ? "Interface" : "Class"),
3369          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3370       return 1;
3371     }
3372   if (decl && CLASS_COMPLETE_P (decl))
3373     {
3374       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3375                                    qualified_name, decl, cl);
3376       return 1;
3377     }
3378
3379   if (check_inner_class_redefinition (raw_name, cl))
3380     return 1;
3381
3382   /* If public, file name should match class/interface name, except
3383      when dealing with an inner class */
3384   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3385     {
3386       const char *f;
3387
3388       for (f = &input_filename [strlen (input_filename)];
3389            f != input_filename && ! IS_DIR_SEPARATOR (f[0]);
3390            f--)
3391         ;
3392       if (IS_DIR_SEPARATOR (f[0]))
3393         f++;
3394       if (strncmp (IDENTIFIER_POINTER (raw_name),
3395                    f , IDENTIFIER_LENGTH (raw_name)) ||
3396           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3397         parse_error_context
3398           (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3399                              (is_interface ? "interface" : "class"),
3400                              IDENTIFIER_POINTER (qualified_name),
3401                              IDENTIFIER_POINTER (raw_name));
3402     }
3403
3404   /* Static classes can be declared only in top level classes. Note:
3405      once static, a inner class is a top level class. */
3406   if (flags & ACC_STATIC)
3407     {
3408       /* Catch the specific error of declaring an class inner class
3409          with no toplevel enclosing class. Prevent check_modifiers from
3410          complaining a second time */
3411       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3412         {
3413           parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3414                                IDENTIFIER_POINTER (qualified_name));
3415           sca = ACC_STATIC;
3416         }
3417       /* Else, in the context of a top-level class declaration, let
3418          `check_modifiers' do its job, otherwise, give it a go */
3419       else
3420         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3421     }
3422
3423   /* Inner classes can be declared private or protected
3424      within their enclosing classes. */
3425   if (CPC_INNER_P ())
3426     {
3427       /* A class which is local to a block can't be public, private,
3428          protected or static. But it is created final, so allow this
3429          one. */
3430       if (current_function_decl)
3431         icaf = sca = uaaf = ACC_FINAL;
3432       else
3433         {
3434           check_modifiers_consistency (flags);
3435           icaf = ACC_PROTECTED;
3436           if (! CLASS_INTERFACE (GET_CPC ()))
3437             icaf |= ACC_PRIVATE;
3438         }
3439     }
3440
3441   if (is_interface)
3442     {
3443       if (CPC_INNER_P ())
3444         uaaf = INTERFACE_INNER_MODIFIERS;
3445       else
3446         uaaf = INTERFACE_MODIFIERS;
3447
3448       check_modifiers ("Illegal modifier `%s' for interface declaration",
3449                        flags, uaaf);
3450     }
3451   else
3452     check_modifiers ((current_function_decl ?
3453                       "Illegal modifier `%s' for local class declaration" :
3454                       "Illegal modifier `%s' for class declaration"),
3455                      flags, uaaf|sca|icaf);
3456   return 0;
3457 }
3458
3459 /* Construct a nested class name.  If the final component starts with
3460    a digit, return true.  Otherwise return false.  */
3461 static int
3462 make_nested_class_name (tree cpc_list)
3463 {
3464   tree name;
3465
3466   if (!cpc_list)
3467     return 0;
3468
3469   make_nested_class_name (TREE_CHAIN (cpc_list));
3470
3471   /* Pick the qualified name when dealing with the first upmost
3472      enclosing class */
3473   name = (TREE_CHAIN (cpc_list)
3474           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3475   obstack_grow (&temporary_obstack,
3476                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3477   obstack_1grow (&temporary_obstack, '$');
3478
3479   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3480 }
3481
3482 /* Can't redefine a class already defined in an earlier scope. */
3483
3484 static int
3485 check_inner_class_redefinition (tree raw_name, tree cl)
3486 {
3487   tree scope_list;
3488
3489   for (scope_list = GET_CPC_LIST (); scope_list;
3490        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3491     if (raw_name == GET_CPC_UN_NODE (scope_list))
3492       {
3493         parse_error_context
3494           (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3495            IDENTIFIER_POINTER (raw_name));
3496         return 1;
3497       }
3498   return 0;
3499 }
3500
3501 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3502    we remember ENCLOSING and SUPER.  */
3503
3504 static tree
3505 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3506                      tree *super, tree class_type)
3507 {
3508   tree local_enclosing = *enclosing;
3509   tree local_super = NULL_TREE;
3510
3511   while (local_enclosing)
3512     {
3513       tree intermediate, decl;
3514
3515       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3516         local_enclosing;
3517
3518       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3519         return decl;
3520
3521       intermediate = local_enclosing;
3522       /* Explore enclosing contexts. */
3523       while (INNER_CLASS_DECL_P (intermediate))
3524         {
3525           intermediate = DECL_CONTEXT (intermediate);
3526           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3527             return decl;
3528         }
3529
3530       /* Now go to the upper classes, bail out if necessary. We will
3531          analyze the returned SUPER and act accordingly (see
3532          do_resolve_class.) */
3533       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3534       if (!local_super || local_super == object_type_node)
3535         break;
3536
3537       if (TREE_CODE (local_super) == POINTER_TYPE)
3538         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3539       else
3540         local_super = TYPE_NAME (local_super);
3541
3542       /* We may not have checked for circular inheritance yet, so do so
3543          here to prevent an infinite loop. */
3544       if (htab_find (circularity_hash, local_super) != NULL)
3545         {
3546           if (!cl)
3547             cl = lookup_cl (local_enclosing);
3548
3549           parse_error_context
3550             (cl, "Cyclic inheritance involving %s",
3551              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3552           local_enclosing = NULL_TREE;
3553         }
3554       else
3555         local_enclosing = local_super;
3556     }
3557
3558   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3559   *super = local_super;
3560   *enclosing = local_enclosing;
3561
3562   return NULL_TREE;
3563 }
3564
3565 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3566    qualified. */
3567
3568 static tree
3569 find_as_inner_class (tree enclosing, tree name, tree cl)
3570 {
3571   tree qual, to_return;
3572   if (!enclosing)
3573     return NULL_TREE;
3574
3575   name = TYPE_NAME (name);
3576
3577   /* First search: within the scope of `enclosing', search for name */
3578   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3579     qual = EXPR_WFL_QUALIFICATION (cl);
3580   else if (cl)
3581     qual = build_tree_list (cl, NULL_TREE);
3582   else
3583     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3584
3585   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3586     return to_return;
3587
3588   /* We're dealing with a qualified name. Try to resolve thing until
3589      we get something that is an enclosing class. */
3590   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3591     {
3592       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3593
3594       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3595            qual = TREE_CHAIN (qual))
3596         {
3597           acc = merge_qualified_name (acc,
3598                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3599           BUILD_PTR_FROM_NAME (ptr, acc);
3600           decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3601         }
3602
3603       /* A NULL qual and a decl means that the search ended
3604          successfully?!? We have to do something then. FIXME */
3605
3606       if (decl)
3607         enclosing = decl;
3608       else
3609         qual = EXPR_WFL_QUALIFICATION (cl);
3610     }
3611   /* Otherwise, create a qual for the other part of the resolution. */
3612   else
3613     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3614
3615   return find_as_inner_class_do (qual, enclosing);
3616 }
3617
3618 /* We go inside the list of sub classes and try to find a way
3619    through. */
3620
3621 static tree
3622 find_as_inner_class_do (tree qual, tree enclosing)
3623 {
3624   if (!qual)
3625     return NULL_TREE;
3626
3627   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3628     {
3629       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3630       tree next_enclosing = NULL_TREE;
3631       tree inner_list;
3632
3633       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3634            inner_list; inner_list = TREE_CHAIN (inner_list))
3635         {
3636           if (TREE_VALUE (inner_list) == name_to_match)
3637             {
3638               next_enclosing = TREE_PURPOSE (inner_list);
3639               break;
3640             }
3641         }
3642       enclosing = next_enclosing;
3643     }
3644
3645   return (!qual && enclosing ? enclosing : NULL_TREE);
3646 }
3647
3648 /* Reach all inner classes and tie their unqualified name to a
3649    DECL. */
3650
3651 static void
3652 set_nested_class_simple_name_value (tree outer, int set)
3653 {
3654   tree l;
3655
3656   for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3657     IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3658                                                 TREE_PURPOSE (l) : NULL_TREE);
3659 }
3660
3661 static void
3662 link_nested_class_to_enclosing (void)
3663 {
3664   if (GET_ENCLOSING_CPC ())
3665     {
3666       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3667       DECL_INNER_CLASS_LIST (enclosing) =
3668         tree_cons (GET_CPC (), GET_CPC_UN (),
3669                    DECL_INNER_CLASS_LIST (enclosing));
3670     }
3671 }
3672
3673 static tree
3674 maybe_make_nested_class_name (tree name)
3675 {
3676   tree id = NULL_TREE;
3677
3678   if (CPC_INNER_P ())
3679     {
3680       /* If we're in a function, we must append a number to create the
3681          nested class name.  However, we don't do this if the class we
3682          are constructing is anonymous, because in that case we'll
3683          already have a number as the class name.  */
3684       if (! make_nested_class_name (GET_CPC_LIST ())
3685           && current_function_decl != NULL_TREE
3686           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3687         {
3688           char buf[10];
3689           sprintf (buf, "%d", anonymous_class_counter);
3690           ++anonymous_class_counter;
3691           obstack_grow (&temporary_obstack, buf, strlen (buf));
3692           obstack_1grow (&temporary_obstack, '$');
3693         }
3694       obstack_grow0 (&temporary_obstack,
3695                      IDENTIFIER_POINTER (name),
3696                      IDENTIFIER_LENGTH (name));
3697       id = get_identifier (obstack_finish (&temporary_obstack));
3698       if (ctxp->package)
3699         QUALIFIED_P (id) = 1;
3700     }
3701   return id;
3702 }
3703
3704 /* If DECL is NULL, create and push a new DECL, record the current
3705    line CL and do other maintenance things.  */
3706
3707 static tree
3708 maybe_create_class_interface_decl (tree decl, tree raw_name,
3709                                    tree qualified_name, tree cl)
3710 {
3711   if (!decl)
3712     decl = push_class (make_class (), qualified_name);
3713
3714   /* Take care of the file and line business */
3715   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3716   /* If we're emiting xrefs, store the line/col number information */
3717   if (flag_emit_xref)
3718     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3719   else
3720     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3721   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3722   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3723   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3724     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3725
3726   PUSH_CPC (decl, raw_name);
3727   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3728
3729   /* Link the declaration to the already seen ones */
3730   TREE_CHAIN (decl) = ctxp->class_list;
3731   ctxp->class_list = decl;
3732
3733   /* Create a new nodes in the global lists */
3734   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3735   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3736
3737   /* Install a new dependency list element */
3738   create_jdep_list (ctxp);
3739
3740   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3741                           IDENTIFIER_POINTER (qualified_name)));
3742   return decl;
3743 }
3744
3745 static void
3746 add_superinterfaces (tree decl, tree interface_list)
3747 {
3748   tree node;
3749   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3750      takes care of ensuring that:
3751        - This is an accessible interface type,
3752        - Circularity detection.
3753    parser_add_interface is then called. If present but not defined,
3754    the check operation is delayed until the super interface gets
3755    defined.  */
3756   for (node = interface_list; node; node = TREE_CHAIN (node))
3757     {
3758       tree current = TREE_PURPOSE (node);
3759       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3760       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3761         {
3762           if (!parser_check_super_interface (idecl, decl, current))
3763             parser_add_interface (decl, idecl, current);
3764         }
3765       else
3766         register_incomplete_type (JDEP_INTERFACE,
3767                                   current, decl, NULL_TREE);
3768     }
3769 }
3770
3771 /* Create an interface in pass1 and return its decl. Return the
3772    interface's decl in pass 2.  */
3773
3774 static tree
3775 create_interface (int flags, tree id, tree super)
3776 {
3777   tree raw_name = EXPR_WFL_NODE (id);
3778   tree q_name = parser_qualified_classname (raw_name);
3779   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3780
3781   /* Certain syntax errors are making SUPER be like ID. Avoid this
3782      case. */
3783   if (ctxp->class_err && id == super)
3784     super = NULL;
3785
3786   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3787
3788   /* Basic checks: scope, redefinition, modifiers */
3789   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3790     {
3791       PUSH_ERROR ();
3792       return NULL_TREE;
3793     }
3794
3795   /* Suspend the current parsing context if we're parsing an inner
3796      interface */
3797   if (CPC_INNER_P ())
3798     {
3799       java_parser_context_suspend ();
3800       /* Interface members are public. */
3801       if (CLASS_INTERFACE (GET_CPC ()))
3802         flags |= ACC_PUBLIC;
3803     }
3804
3805   /* Push a new context for (static) initialized upon declaration fields */
3806   java_parser_context_push_initialized_field ();
3807
3808   /* Interface modifiers check
3809        - public/abstract allowed (already done at that point)
3810        - abstract is obsolete (comes first, it's a warning, or should be)
3811        - Can't use twice the same (checked in the modifier rule) */
3812   if ((flags & ACC_ABSTRACT) && flag_redundant)
3813     parse_warning_context
3814       (MODIFIER_WFL (ABSTRACT_TK),
3815        "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3816
3817   /* Create a new decl if DECL is NULL, otherwise fix it */
3818   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3819
3820   /* Set super info and mark the class a complete */
3821   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3822                   object_type_node, ctxp->interface_number);
3823   ctxp->interface_number = 0;
3824   CLASS_COMPLETE_P (decl) = 1;
3825   add_superinterfaces (decl, super);
3826
3827   return decl;
3828 }
3829
3830 /* Patch anonymous class CLASS, by either extending or implementing
3831    DEP.  */
3832
3833 static void
3834 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
3835 {
3836   tree class = TREE_TYPE (class_decl);
3837   tree type =  TREE_TYPE (type_decl);
3838   tree binfo = TYPE_BINFO (class);
3839
3840   /* If it's an interface, implement it */
3841   if (CLASS_INTERFACE (type_decl))
3842     {
3843       tree s_binfo;
3844       int length;
3845
3846       if (parser_check_super_interface (type_decl, class_decl, wfl))
3847         return;
3848
3849       s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3850       length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3851       TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3852       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3853       /* And add the interface */
3854       parser_add_interface (class_decl, type_decl, wfl);
3855     }
3856   /* Otherwise, it's a type we want to extend */
3857   else
3858     {
3859       if (parser_check_super (type_decl, class_decl, wfl))
3860         return;
3861       BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3862     }
3863 }
3864
3865 static tree
3866 create_anonymous_class (int location, tree type_name)
3867 {
3868   char buffer [80];
3869   tree super = NULL_TREE, itf = NULL_TREE;
3870   tree id, type_decl, class;
3871
3872   /* The unqualified name of the anonymous class. It's just a number. */
3873   sprintf (buffer, "%d", anonymous_class_counter++);
3874   id = build_wfl_node (get_identifier (buffer));
3875   EXPR_WFL_LINECOL (id) = location;
3876
3877   /* We know about the type to extend/implement. We go ahead */
3878   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3879     {
3880       /* Create a class which either implements on extends the designated
3881          class. The class bears an innacessible name. */
3882       if (CLASS_INTERFACE (type_decl))
3883         {
3884           /* It's OK to modify it here. It's been already used and
3885              shouldn't be reused */
3886           ctxp->interface_number = 1;
3887           /* Interfaces should presented as a list of WFLs */
3888           itf = build_tree_list (type_name, NULL_TREE);
3889         }
3890       else
3891         super = type_name;
3892     }
3893
3894   class = create_class (ACC_FINAL, id, super, itf);
3895
3896   /* We didn't know anything about the stuff. We register a dependence. */
3897   if (!type_decl)
3898     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3899
3900   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3901   return class;
3902 }
3903
3904 /* Create a class in pass1 and return its decl. Return class
3905    interface's decl in pass 2.  */
3906
3907 static tree
3908 create_class (int flags, tree id, tree super, tree interfaces)
3909 {
3910   tree raw_name = EXPR_WFL_NODE (id);
3911   tree class_id, decl;
3912   tree super_decl_type;
3913
3914   /* Certain syntax errors are making SUPER be like ID. Avoid this
3915      case. */
3916   if (ctxp->class_err && id == super)
3917     super = NULL;
3918
3919   class_id = parser_qualified_classname (raw_name);
3920   decl = IDENTIFIER_CLASS_VALUE (class_id);
3921   EXPR_WFL_NODE (id) = class_id;
3922
3923   /* Basic check: scope, redefinition, modifiers */
3924   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3925     {
3926       PUSH_ERROR ();
3927       return NULL_TREE;
3928     }
3929
3930   /* Suspend the current parsing context if we're parsing an inner
3931      class or an anonymous class. */
3932   if (CPC_INNER_P ())
3933     {
3934       java_parser_context_suspend ();
3935       /* Interface members are public. */
3936       if (CLASS_INTERFACE (GET_CPC ()))
3937         flags |= ACC_PUBLIC;
3938     }
3939
3940   /* Push a new context for (static) initialized upon declaration fields */
3941   java_parser_context_push_initialized_field ();
3942
3943   /* Class modifier check:
3944        - Allowed modifier (already done at that point)
3945        - abstract AND final forbidden
3946        - Public classes defined in the correct file */
3947   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3948     parse_error_context
3949       (id, "Class `%s' can't be declared both abstract and final",
3950        IDENTIFIER_POINTER (raw_name));
3951
3952   /* Create a new decl if DECL is NULL, otherwise fix it */
3953   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3954
3955   /* If SUPER exists, use it, otherwise use Object */
3956   if (super)
3957     {
3958       /* Can't extend java.lang.Object */
3959       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3960         {
3961           parse_error_context (id, "Can't extend `java.lang.Object'");
3962           return NULL_TREE;
3963         }
3964
3965       super_decl_type =
3966         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3967     }
3968   else if (TREE_TYPE (decl) != object_type_node)
3969     super_decl_type = object_type_node;
3970   /* We're defining java.lang.Object */
3971   else
3972     super_decl_type = NULL_TREE;
3973
3974   /* A class nested in an interface is implicitly static. */
3975   if (INNER_CLASS_DECL_P (decl)
3976       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3977     {
3978       flags |= ACC_STATIC;
3979     }
3980
3981   /* Set super info and mark the class as complete. */
3982   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3983                   ctxp->interface_number);
3984   ctxp->interface_number = 0;
3985   CLASS_COMPLETE_P (decl) = 1;
3986   add_superinterfaces (decl, interfaces);
3987
3988   /* Add the private this$<n> field, Replicate final locals still in
3989      scope as private final fields mangled like val$<local_name>.
3990      This doesn't not occur for top level (static) inner classes. */
3991   if (PURE_INNER_CLASS_DECL_P (decl))
3992     add_inner_class_fields (decl, current_function_decl);
3993
3994   /* If doing xref, store the location at which the inherited class
3995      (if any) was seen. */
3996   if (flag_emit_xref && super)
3997     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3998
3999   /* Eventually sets the @deprecated tag flag */
4000   CHECK_DEPRECATED (decl);
4001
4002   /* Reset the anonymous class counter when declaring non inner classes */
4003   if (!INNER_CLASS_DECL_P (decl))
4004     anonymous_class_counter = 1;
4005
4006   return decl;
4007 }
4008
4009 /* End a class declaration: register the statements used to create
4010    finit$ and <clinit>, pop the current class and resume the prior
4011    parser context if necessary.  */
4012
4013 static void
4014 end_class_declaration (int resume)
4015 {
4016   /* If an error occurred, context weren't pushed and won't need to be
4017      popped by a resume. */
4018   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4019
4020   if (GET_CPC () != error_mark_node)
4021     dump_java_tree (TDI_class, GET_CPC ());
4022
4023   java_parser_context_pop_initialized_field ();
4024   POP_CPC ();
4025   if (resume && no_error_occurred)
4026     java_parser_context_resume ();
4027
4028   /* We're ending a class declaration, this is a good time to reset
4029      the interface cout. Note that might have been already done in
4030      create_interface, but if at that time an inner class was being
4031      dealt with, the interface count was reset in a context created
4032      for the sake of handling inner classes declaration. */
4033   ctxp->interface_number = 0;
4034 }
4035
4036 static void
4037 add_inner_class_fields (tree class_decl, tree fct_decl)
4038 {
4039   tree block, marker, f;
4040
4041   f = add_field (TREE_TYPE (class_decl),
4042                  build_current_thisn (TREE_TYPE (class_decl)),
4043                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4044                  ACC_PRIVATE);
4045   FIELD_THISN (f) = 1;
4046
4047   if (!fct_decl)
4048     return;
4049
4050   for (block = GET_CURRENT_BLOCK (fct_decl);
4051        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4052     {
4053       tree decl;
4054       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4055         {
4056           tree name, pname;
4057           tree wfl, init, list;
4058
4059           /* Avoid non final arguments. */
4060           if (!LOCAL_FINAL_P (decl))
4061             continue;
4062
4063           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4064           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4065           wfl = build_wfl_node (name);
4066           init = build_wfl_node (pname);
4067           /* Build an initialization for the field: it will be
4068              initialized by a parameter added to finit$, bearing a
4069              mangled name of the field itself (param$<n>.) The
4070              parameter is provided to finit$ by the constructor
4071              invoking it (hence the constructor will also feature a
4072              hidden parameter, set to the value of the outer context
4073              local at the time the inner class is created.)
4074
4075              Note: we take into account all possible locals that can
4076              be accessed by the inner class. It's actually not trivial
4077              to minimize these aliases down to the ones really
4078              used. One way to do that would be to expand all regular
4079              methods first, then finit$ to get a picture of what's
4080              used.  It works with the exception that we would have to
4081              go back on all constructor invoked in regular methods to
4082              have their invokation reworked (to include the right amount
4083              of alias initializer parameters.)
4084
4085              The only real way around, I think, is a first pass to
4086              identify locals really used in the inner class. We leave
4087              the flag FIELD_LOCAL_ALIAS_USED around for that future
4088              use.
4089
4090              On the other hand, it only affect local inner classes,
4091              whose constructors (and finit$ call) will be featuring
4092              unecessary arguments. It's easy for a developper to keep
4093              this number of parameter down by using the `final'
4094              keyword only when necessary. For the time being, we can
4095              issue a warning on unecessary finals. FIXME */
4096           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4097                                    wfl, init);
4098
4099           /* Register the field. The TREE_LIST holding the part
4100              initialized/initializer will be marked ARG_FINAL_P so
4101              that the created field can be marked
4102              FIELD_LOCAL_ALIAS. */
4103           list = build_tree_list (wfl, init);
4104           ARG_FINAL_P (list) = 1;
4105           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4106         }
4107     }
4108
4109   if (!CPC_INITIALIZER_STMT (ctxp))
4110     return;
4111
4112   /* If we ever registered an alias field, insert and marker to
4113      remeber where the list ends. The second part of the list (the one
4114      featuring initialized fields) so it can be later reversed to
4115      enforce 8.5. The marker will be removed during that operation. */
4116   marker = build_tree_list (NULL_TREE, NULL_TREE);
4117   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4118   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4119 }
4120
4121 /* Can't use lookup_field () since we don't want to load the class and
4122    can't set the CLASS_LOADED_P flag */
4123
4124 static tree
4125 find_field (tree class, tree name)
4126 {
4127   tree decl;
4128   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4129     {
4130       if (DECL_NAME (decl) == name)
4131         return decl;
4132     }
4133   return NULL_TREE;
4134 }
4135
4136 /* Wrap around lookup_field that doesn't potentially upset the value
4137    of CLASS */
4138
4139 static tree
4140 lookup_field_wrapper (tree class, tree name)
4141 {
4142   tree type = class;
4143   tree decl = NULL_TREE;
4144   java_parser_context_save_global ();
4145
4146   /* Last chance: if we're within the context of an inner class, we
4147      might be trying to access a local variable defined in an outer
4148      context. We try to look for it now. */
4149   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4150     {
4151       tree new_name;
4152       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4153       decl = lookup_field (&type, new_name);
4154       if (decl && decl != error_mark_node)
4155         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4156     }
4157   if (!decl || decl == error_mark_node)
4158     {
4159       type = class;
4160       decl = lookup_field (&type, name);
4161     }
4162
4163   /* If the field still hasn't been found, try the next enclosing context. */
4164   if (!decl && INNER_CLASS_TYPE_P (class))
4165     {
4166       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4167       decl = lookup_field_wrapper (outer_type, name);
4168     }
4169
4170   java_parser_context_restore_global ();
4171   return decl == error_mark_node ? NULL : decl;
4172 }
4173
4174 /* Find duplicate field within the same class declarations and report
4175    the error. Returns 1 if a duplicated field was found, 0
4176    otherwise.  */
4177
4178 static int
4179 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4180 {
4181   /* This might be modified to work with method decl as well */
4182   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4183   if (decl)
4184     {
4185       char *t1 = xstrdup (purify_type_name
4186                          ((TREE_CODE (new_type) == POINTER_TYPE
4187                            && TREE_TYPE (new_type) == NULL_TREE) ?
4188                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4189                           lang_printable_name (new_type, 1)));
4190       /* The type may not have been completed by the time we report
4191          the error */
4192       char *t2 = xstrdup (purify_type_name
4193                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4194                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4195                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4196                           lang_printable_name (TREE_TYPE (decl), 1)));
4197       parse_error_context
4198         (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4199          t1, IDENTIFIER_POINTER (new_field_name),
4200          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4201          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4202       free (t1);
4203       free (t2);
4204       return 1;
4205     }
4206   return 0;
4207 }
4208
4209 /* Field registration routine. If TYPE doesn't exist, field
4210    declarations are linked to the undefined TYPE dependency list, to
4211    be later resolved in java_complete_class () */
4212
4213 static void
4214 register_fields (int flags, tree type, tree variable_list)
4215 {
4216   tree current, saved_type;
4217   tree class_type = NULL_TREE;
4218   int saved_lineno = lineno;
4219   int must_chain = 0;
4220   tree wfl = NULL_TREE;
4221
4222   if (GET_CPC ())
4223     class_type = TREE_TYPE (GET_CPC ());
4224
4225   if (!class_type || class_type == error_mark_node)
4226     return;
4227
4228   /* If we're adding fields to interfaces, those fields are public,
4229      static, final */
4230   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4231     {
4232       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4233                                  flags, ACC_PUBLIC, "interface field(s)");
4234       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4235                                  flags, ACC_STATIC, "interface field(s)");
4236       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4237                                  flags, ACC_FINAL, "interface field(s)");
4238       check_modifiers ("Illegal interface member modifier `%s'", flags,
4239                        INTERFACE_FIELD_MODIFIERS);
4240       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4241     }
4242
4243   /* Obtain a suitable type for resolution, if necessary */
4244   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4245
4246   /* If TYPE is fully resolved and we don't have a reference, make one */
4247   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4248
4249   for (current = variable_list, saved_type = type; current;
4250        current = TREE_CHAIN (current), type = saved_type)
4251     {
4252       tree real_type;
4253       tree field_decl;
4254       tree cl = TREE_PURPOSE (current);
4255       tree init = TREE_VALUE (current);
4256       tree current_name = EXPR_WFL_NODE (cl);
4257
4258       /* Can't declare non-final static fields in inner classes */
4259       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4260           && !(flags & ACC_FINAL))
4261         parse_error_context
4262           (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4263            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4264            lang_printable_name (class_type, 0));
4265
4266       /* Process NAME, as it may specify extra dimension(s) for it */
4267       type = build_array_from_name (type, wfl, current_name, &current_name);
4268
4269       /* Type adjustment. We may have just readjusted TYPE because
4270          the variable specified more dimensions. Make sure we have
4271          a reference if we can and don't have one already. Also
4272          change the name if we have an init. */
4273       if (type != saved_type)
4274         {
4275           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4276           if (init)
4277             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4278         }
4279
4280       real_type = GET_REAL_TYPE (type);
4281       /* Check for redeclarations */
4282       if (duplicate_declaration_error_p (current_name, real_type, cl))
4283         continue;
4284
4285       /* Set lineno to the line the field was found and create a
4286          declaration for it. Eventually sets the @deprecated tag flag. */
4287       if (flag_emit_xref)
4288         lineno = EXPR_WFL_LINECOL (cl);
4289       else
4290         lineno = EXPR_WFL_LINENO (cl);
4291       field_decl = add_field (class_type, current_name, real_type, flags);
4292       CHECK_DEPRECATED (field_decl);
4293
4294       /* If the field denotes a final instance variable, then we
4295          allocate a LANG_DECL_SPECIFIC part to keep track of its
4296          initialization. We also mark whether the field was
4297          initialized upon its declaration. We don't do that if the
4298          created field is an alias to a final local. */
4299       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4300         {
4301           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4302           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4303         }
4304
4305       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4306          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4307          hide parameters to this inner class finit$ and
4308          constructors. It also means that the field isn't final per
4309          say. */
4310       if (ARG_FINAL_P (current))
4311         {
4312           FIELD_LOCAL_ALIAS (field_decl) = 1;
4313           FIELD_FINAL (field_decl) = 0;
4314         }
4315
4316       /* Check if we must chain. */
4317       if (must_chain)
4318         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4319
4320       /* If we have an initialization value tied to the field */
4321       if (init)
4322         {
4323           /* The field is declared static */
4324           if (flags & ACC_STATIC)
4325             {
4326               /* We include the field and its initialization part into
4327                  a list used to generate <clinit>. After <clinit> is
4328                  walked, field initializations will be processed and
4329                  fields initialized with known constants will be taken
4330                  out of <clinit> and have their DECL_INITIAL set
4331                  appropriately. */
4332               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4333               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4334               if (TREE_OPERAND (init, 1)
4335                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4336                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4337             }
4338           /* A non-static field declared with an immediate initialization is
4339              to be initialized in <init>, if any.  This field is remembered
4340              to be processed at the time of the generation of <init>. */
4341           else
4342             {
4343               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4344               SET_CPC_INITIALIZER_STMT (ctxp, init);
4345             }
4346           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4347           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4348         }
4349     }
4350   lineno = saved_lineno;
4351 }
4352
4353 /* Generate finit$, using the list of initialized fields to populate
4354    its body. finit$'s parameter(s) list is adjusted to include the
4355    one(s) used to initialized the field(s) caching outer context
4356    local(s).  */
4357
4358 static tree
4359 generate_finit (tree class_type)
4360 {
4361   int count = 0;
4362   tree list = TYPE_FINIT_STMT_LIST (class_type);
4363   tree mdecl, current, parms;
4364
4365   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4366                                                   class_type, NULL_TREE,
4367                                                   &count);
4368   CRAFTED_PARAM_LIST_FIXUP (parms);
4369   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4370                                     finit_identifier_node, parms);
4371   fix_method_argument_names (parms, mdecl);
4372   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4373                        mdecl, NULL_TREE);
4374   DECL_FUNCTION_NAP (mdecl) = count;
4375   start_artificial_method_body (mdecl);
4376
4377   for (current = list; current; current = TREE_CHAIN (current))
4378     java_method_add_stmt (mdecl,
4379                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4380                                                 current));
4381   end_artificial_method_body (mdecl);
4382   return mdecl;
4383 }
4384
4385 /* Generate a function to run the instance initialization code. The
4386    private method is called `instinit$'. Unless we're dealing with an
4387    anonymous class, we determine whether all ctors of CLASS_TYPE
4388    declare a checked exception in their `throws' clause in order to
4389    see whether it's necessary to encapsulate the instance initializer
4390    statements in a try/catch/rethrow sequence.  */
4391
4392 static tree
4393 generate_instinit (tree class_type)
4394 {
4395   tree current;
4396   tree compound = NULL_TREE;
4397   tree parms = tree_cons (this_identifier_node,
4398                           build_pointer_type (class_type), end_params_node);
4399   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4400                                          void_type_node,
4401                                          instinit_identifier_node, parms);
4402
4403   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4404                        mdecl, NULL_TREE);
4405
4406   /* Gather all the statements in a compound */
4407   for (current = TYPE_II_STMT_LIST (class_type);
4408        current; current = TREE_CHAIN (current))
4409     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4410
4411   /* We need to encapsulate COMPOUND by a try/catch statement to
4412      rethrow exceptions that might occur in the instance initializer.
4413      We do that only if all ctors of CLASS_TYPE are set to catch a
4414      checked exception. This doesn't apply to anonymous classes (since
4415      they don't have declared ctors.) */
4416   if (!ANONYMOUS_CLASS_P (class_type) &&
4417       ctors_unchecked_throws_clause_p (class_type))
4418     {
4419       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4420                                              build1 (THROW_EXPR, NULL_TREE,
4421                                                      build_wfl_node (wpv_id)));
4422       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4423                                                       exception_type_node);
4424     }
4425
4426   start_artificial_method_body (mdecl);
4427   java_method_add_stmt (mdecl, compound);
4428   end_artificial_method_body (mdecl);
4429
4430   return mdecl;
4431 }
4432
4433 /* FIXME */
4434 static tree
4435 build_instinit_invocation (tree class_type)
4436 {
4437   tree to_return = NULL_TREE;
4438
4439   if (TYPE_II_STMT_LIST (class_type))
4440     {
4441       tree parm = build_tree_list (NULL_TREE,
4442                                    build_wfl_node (this_identifier_node));
4443       to_return =
4444         build_method_invocation (build_wfl_node (instinit_identifier_node),
4445                                  parm);
4446     }
4447   return to_return;
4448 }
4449
4450 /* Shared accros method_declarator and method_header to remember the
4451    patch stage that was reached during the declaration of the method.
4452    A method DECL is built differently is there is no patch
4453    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4454    pending on the currently defined method.  */
4455
4456 static int patch_stage;
4457
4458 /* Check the method declaration and add the method to its current
4459    class.  If the argument list is known to contain incomplete types,
4460    the method is partially added and the registration will be resume
4461    once the method arguments resolved. If TYPE is NULL, we're dealing
4462    with a constructor.  */
4463
4464 static tree
4465 method_header (int flags, tree type, tree mdecl, tree throws)
4466 {
4467   tree type_wfl = NULL_TREE;
4468   tree meth_name = NULL_TREE;
4469   tree current, orig_arg, this_class = NULL;
4470   tree id, meth;
4471   int saved_lineno;
4472   int constructor_ok = 0, must_chain;
4473   int count;
4474
4475   if (mdecl == error_mark_node)
4476     return error_mark_node;
4477   meth = TREE_VALUE (mdecl);
4478   id = TREE_PURPOSE (mdecl);
4479
4480   check_modifiers_consistency (flags);
4481
4482   if (GET_CPC ())
4483     this_class = TREE_TYPE (GET_CPC ());
4484
4485   if (!this_class || this_class == error_mark_node)
4486     return NULL_TREE;
4487
4488   /* There are some forbidden modifiers for an abstract method and its
4489      class must be abstract as well.  */
4490   if (type && (flags & ACC_ABSTRACT))
4491     {
4492       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4493       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4494       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4495       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4496       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4497       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4498       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4499           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4500         parse_error_context
4501           (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4502            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4503            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4504     }
4505
4506   /* Things to be checked when declaring a constructor */
4507   if (!type)
4508     {
4509       int ec = java_error_count;
4510       /* 8.6: Constructor declarations: we might be trying to define a
4511          method without specifying a return type. */
4512       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4513         parse_error_context
4514           (id, "Invalid method declaration, return type required");
4515       /* 8.6.3: Constructor modifiers */
4516       else
4517         {
4518           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4519           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4520           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4521           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4522           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4523           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4524         }
4525       /* If we found error here, we don't consider it's OK to tread
4526          the method definition as a constructor, for the rest of this
4527          function */
4528       if (ec == java_error_count)
4529         constructor_ok = 1;
4530     }
4531
4532   /* Method declared within the scope of an interface are implicitly
4533      abstract and public. Conflicts with other erroneously provided
4534      modifiers are checked right after. */
4535
4536   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4537     {
4538       /* If FLAGS isn't set because of a modifier, turn the
4539          corresponding modifier WFL to NULL so we issue a warning on
4540          the obsolete use of the modifier */
4541       if (!(flags & ACC_PUBLIC))
4542         MODIFIER_WFL (PUBLIC_TK) = NULL;
4543       if (!(flags & ACC_ABSTRACT))
4544         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4545       flags |= ACC_PUBLIC;
4546       flags |= ACC_ABSTRACT;
4547     }
4548
4549   /* Inner class can't declare static methods */
4550   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4551     {
4552       parse_error_context
4553         (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4554          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4555          lang_printable_name (this_class, 0));
4556     }
4557
4558   /* Modifiers context reset moved up, so abstract method declaration
4559      modifiers can be later checked.  */
4560
4561   /* Set constructor returned type to void and method name to <init>,
4562      unless we found an error identifier the constructor (in which
4563      case we retain the original name) */
4564   if (!type)
4565     {
4566       type = void_type_node;
4567       if (constructor_ok)
4568         meth_name = init_identifier_node;
4569     }
4570   else
4571     meth_name = EXPR_WFL_NODE (id);
4572
4573   /* Do the returned type resolution and registration if necessary */
4574   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4575
4576   if (meth_name)
4577     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4578   EXPR_WFL_NODE (id) = meth_name;
4579   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4580
4581   if (must_chain)
4582     {
4583       patch_stage = JDEP_METHOD_RETURN;
4584       register_incomplete_type (patch_stage, type_wfl, id, type);
4585       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4586     }
4587   else
4588     TREE_TYPE (meth) = type;
4589
4590   saved_lineno = lineno;
4591   /* When defining an abstract or interface method, the curly
4592      bracket at level 1 doesn't exist because there is no function
4593      body */
4594   lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4595             EXPR_WFL_LINENO (id));
4596
4597   /* Remember the original argument list */
4598   orig_arg = TYPE_ARG_TYPES (meth);
4599
4600   if (patch_stage)              /* includes ret type and/or all args */
4601     {
4602       jdep *jdep;
4603       meth = add_method_1 (this_class, flags, meth_name, meth);
4604       /* Patch for the return type */
4605       if (patch_stage == JDEP_METHOD_RETURN)
4606         {
4607           jdep = CLASSD_LAST (ctxp->classd_list);
4608           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4609         }
4610       /* This is the stop JDEP. METH allows the function's signature
4611          to be computed. */
4612       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4613     }
4614   else
4615     meth = add_method (this_class, flags, meth_name,
4616                        build_java_signature (meth));
4617
4618   /* Remember final parameters */
4619   MARK_FINAL_PARMS (meth, orig_arg);
4620
4621   /* Fix the method argument list so we have the argument name
4622      information */
4623   fix_method_argument_names (orig_arg, meth);
4624
4625   /* Register the parameter number and re-install the current line
4626      number */
4627   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4628   lineno = saved_lineno;
4629
4630   /* Register exception specified by the `throws' keyword for
4631      resolution and set the method decl appropriate field to the list.
4632      Note: the grammar ensures that what we get here are class
4633      types. */
4634   if (throws)
4635     {
4636       throws = nreverse (throws);
4637       for (current = throws; current; current = TREE_CHAIN (current))
4638         {
4639           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4640                                     NULL_TREE, NULL_TREE);
4641           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4642             &TREE_VALUE (current);
4643         }
4644       DECL_FUNCTION_THROWS (meth) = throws;
4645     }
4646
4647   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4648     DECL_FUNCTION_WFL (meth) = id;
4649
4650   /* Set the flag if we correctly processed a constructor */
4651   if (constructor_ok)
4652     {
4653       DECL_CONSTRUCTOR_P (meth) = 1;
4654       /* Compute and store the number of artificial parameters declared
4655          for this constructor */
4656       for (count = 0, current = TYPE_FIELDS (this_class); current;
4657            current = TREE_CHAIN (current))
4658         if (FIELD_LOCAL_ALIAS (current))
4659           count++;
4660       DECL_FUNCTION_NAP (meth) = count;
4661     }
4662
4663   /* Eventually set the @deprecated tag flag */
4664   CHECK_DEPRECATED (meth);
4665
4666   /* If doing xref, store column and line number information instead
4667      of the line number only. */
4668   if (flag_emit_xref)
4669     DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4670
4671   return meth;
4672 }
4673
4674 static void
4675 fix_method_argument_names (tree orig_arg, tree meth)
4676 {
4677   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4678   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4679     {
4680       TREE_PURPOSE (arg) = this_identifier_node;
4681       arg = TREE_CHAIN (arg);
4682     }
4683   while (orig_arg != end_params_node)
4684     {
4685       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4686       orig_arg = TREE_CHAIN (orig_arg);
4687       arg = TREE_CHAIN (arg);
4688     }
4689 }
4690
4691 /* Complete the method declaration with METHOD_BODY.  */
4692
4693 static void
4694 finish_method_declaration (tree method_body)
4695 {
4696   int flags;
4697
4698   if (!current_function_decl)
4699     return;
4700
4701   flags = get_access_flags_from_decl (current_function_decl);
4702
4703   /* 8.4.5 Method Body */
4704   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4705     {
4706       tree name = DECL_NAME (current_function_decl);
4707       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4708                            "%s method `%s' can't have a body defined",
4709                            (METHOD_NATIVE (current_function_decl) ?
4710                             "Native" : "Abstract"),
4711                            IDENTIFIER_POINTER (name));
4712       method_body = NULL_TREE;
4713     }
4714   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4715     {
4716       tree name = DECL_NAME (current_function_decl);
4717       parse_error_context
4718         (DECL_FUNCTION_WFL (current_function_decl),
4719          "Non native and non abstract method `%s' must have a body defined",
4720          IDENTIFIER_POINTER (name));
4721       method_body = NULL_TREE;
4722     }
4723
4724   if (flag_emit_class_files && method_body
4725       && TREE_CODE (method_body) == NOP_EXPR
4726       && TREE_TYPE (current_function_decl)
4727       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4728     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4729
4730   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4731   maybe_absorb_scoping_blocks ();
4732   /* Exit function's body */
4733   exit_block ();
4734   /* Merge last line of the function with first line, directly in the
4735      function decl. It will be used to emit correct debug info. */
4736   if (!flag_emit_xref)
4737     DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4738
4739   /* Since function's argument's list are shared, reset the
4740      ARG_FINAL_P parameter that might have been set on some of this
4741      function parameters. */
4742   UNMARK_FINAL_PARMS (current_function_decl);
4743
4744   /* So we don't have an irrelevant function declaration context for
4745      the next static block we'll see. */
4746   current_function_decl = NULL_TREE;
4747 }
4748
4749 /* Build a an error message for constructor circularity errors.  */
4750
4751 static char *
4752 constructor_circularity_msg (tree from, tree to)
4753 {
4754   static char string [4096];
4755   char *t = xstrdup (lang_printable_name (from, 0));
4756   sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4757   free (t);
4758   return string;
4759 }
4760
4761 /* Verify a circular call to METH. Return 1 if an error is found, 0
4762    otherwise.  */
4763
4764 static GTY(()) tree vcc_list;
4765 static int
4766 verify_constructor_circularity (tree meth, tree current)
4767 {
4768   tree c;
4769
4770   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4771     {
4772       if (TREE_VALUE (c) == meth)
4773         {
4774           char *t;
4775           if (vcc_list)
4776             {
4777               tree liste;
4778               vcc_list = nreverse (vcc_list);
4779               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4780                 {
4781                   parse_error_context
4782                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4783                      constructor_circularity_msg
4784                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4785                   java_error_count--;
4786                 }
4787             }
4788           t = xstrdup (lang_printable_name (meth, 0));
4789           parse_error_context (TREE_PURPOSE (c),
4790                                "%s: recursive invocation of constructor `%s'",
4791                                constructor_circularity_msg (current, meth), t);
4792           free (t);
4793           vcc_list = NULL_TREE;
4794           return 1;
4795         }
4796     }
4797   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4798     {
4799       vcc_list = tree_cons (c, current, vcc_list);
4800       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4801         return 1;
4802       vcc_list = TREE_CHAIN (vcc_list);
4803     }
4804   return 0;
4805 }
4806
4807 /* Check modifiers that can be declared but exclusively */
4808
4809 static void
4810 check_modifiers_consistency (int flags)
4811 {
4812   int acc_count = 0;
4813   tree cl = NULL_TREE;
4814
4815   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4816   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4817   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4818   if (acc_count > 1)
4819     parse_error_context
4820       (cl, "Inconsistent member declaration.  At most one of `public', `private', or `protected' may be specified");
4821
4822   acc_count = 0;
4823   cl = NULL_TREE;
4824   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4825   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4826   if (acc_count > 1)
4827     parse_error_context (cl,
4828                          "Inconsistent member declaration.  At most one of `final' or `volatile' may be specified");
4829 }
4830
4831 /* Check the methode header METH for abstract specifics features */
4832
4833 static void
4834 check_abstract_method_header (tree meth)
4835 {
4836   int flags = get_access_flags_from_decl (meth);
4837
4838   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4839                               ACC_ABSTRACT, "abstract method",
4840                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4841   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4842                               ACC_PUBLIC, "abstract method",
4843                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4844
4845   check_modifiers ("Illegal modifier `%s' for interface method",
4846                   flags, INTERFACE_METHOD_MODIFIERS);
4847 }
4848
4849 /* Create a FUNCTION_TYPE node and start augmenting it with the
4850    declared function arguments. Arguments type that can't be resolved
4851    are left as they are, but the returned node is marked as containing
4852    incomplete types.  */
4853
4854 static tree
4855 method_declarator (tree id, tree list)
4856 {
4857   tree arg_types = NULL_TREE, current, node;
4858   tree meth = make_node (FUNCTION_TYPE);
4859   jdep *jdep;
4860
4861   patch_stage = JDEP_NO_PATCH;
4862
4863   if (GET_CPC () == error_mark_node)
4864     return error_mark_node;
4865
4866   /* If we're dealing with an inner class constructor, we hide the
4867      this$<n> decl in the name field of its parameter declaration.  We
4868      also might have to hide the outer context local alias
4869      initializers. Not done when the class is a toplevel class. */
4870   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4871       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4872     {
4873       tree aliases_list, type, thisn;
4874       /* First the aliases, linked to the regular parameters */
4875       aliases_list =
4876         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4877                                                 TREE_TYPE (GET_CPC ()),
4878                                                 NULL_TREE, NULL);
4879       list = chainon (nreverse (aliases_list), list);
4880
4881       /* Then this$<n> */
4882       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4883       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4884       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4885                         list);
4886     }
4887
4888   for (current = list; current; current = TREE_CHAIN (current))
4889     {
4890       int must_chain = 0;
4891       tree wfl_name = TREE_PURPOSE (current);
4892       tree type = TREE_VALUE (current);
4893       tree name = EXPR_WFL_NODE (wfl_name);
4894       tree already, arg_node;
4895       tree type_wfl = NULL_TREE;
4896       tree real_type;
4897
4898       /* Obtain a suitable type for resolution, if necessary */
4899       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4900
4901       /* Process NAME, as it may specify extra dimension(s) for it */
4902       type = build_array_from_name (type, type_wfl, name, &name);
4903       EXPR_WFL_NODE (wfl_name) = name;
4904
4905       real_type = GET_REAL_TYPE (type);
4906       if (TREE_CODE (real_type) == RECORD_TYPE)
4907         {
4908           real_type = promote_type (real_type);
4909           if (TREE_CODE (type) == TREE_LIST)
4910             TREE_PURPOSE (type) = real_type;
4911         }
4912
4913       /* Check redefinition */
4914       for (already = arg_types; already; already = TREE_CHAIN (already))
4915         if (TREE_PURPOSE (already) == name)
4916           {
4917             parse_error_context
4918               (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4919                IDENTIFIER_POINTER (name),
4920                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4921             break;
4922           }
4923
4924       /* If we've an incomplete argument type, we know there is a location
4925          to patch when the type get resolved, later.  */
4926       jdep = NULL;
4927       if (must_chain)
4928         {
4929           patch_stage = JDEP_METHOD;
4930           type = register_incomplete_type (patch_stage,
4931                                            type_wfl, wfl_name, type);
4932           jdep = CLASSD_LAST (ctxp->classd_list);
4933           JDEP_MISC (jdep) = id;
4934         }
4935
4936       /* The argument node: a name and a (possibly) incomplete type.  */
4937       arg_node = build_tree_list (name, real_type);
4938       /* Remeber arguments declared final. */
4939       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4940
4941       if (jdep)
4942         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4943       TREE_CHAIN (arg_node) = arg_types;
4944       arg_types = arg_node;
4945     }
4946   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4947   node = build_tree_list (id, meth);
4948   return node;
4949 }
4950
4951 static int
4952 unresolved_type_p (tree wfl, tree *returned)
4953
4954 {
4955   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4956     {
4957       if (returned)
4958         {
4959           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4960           if (decl && current_class && (decl == TYPE_NAME (current_class)))
4961             *returned = TREE_TYPE (decl);
4962           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4963             *returned = TREE_TYPE (GET_CPC ());
4964           else
4965             *returned = NULL_TREE;
4966         }
4967       return 1;
4968     }
4969   if (returned)
4970     *returned = wfl;
4971   return 0;
4972 }
4973
4974 /* From NAME, build a qualified identifier node using the
4975    qualification from the current package definition. */
4976
4977 static tree
4978 parser_qualified_classname (tree name)
4979 {
4980   tree nested_class_name;
4981
4982   if ((nested_class_name = maybe_make_nested_class_name (name)))
4983     return nested_class_name;
4984
4985   if (ctxp->package)
4986     return merge_qualified_name (ctxp->package, name);
4987   else
4988     return name;
4989 }
4990
4991 /* Called once the type a interface extends is resolved. Returns 0 if
4992    everything is OK.  */
4993
4994 static int
4995 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
4996 {
4997   tree super_type = TREE_TYPE (super_decl);
4998
4999   /* Has to be an interface */
5000   if (!CLASS_INTERFACE (super_decl))
5001     {
5002       parse_error_context
5003         (this_wfl, "%s `%s' can't implement/extend %s `%s'",
5004          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5005           "Interface" : "Class"),
5006          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5007          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5008          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5009       return 1;
5010     }
5011
5012   /* Check top-level interface access. Inner classes are subject to member
5013      access rules (6.6.1). */
5014   if (! INNER_CLASS_P (super_type)
5015       && check_pkg_class_access (DECL_NAME (super_decl),
5016                                  lookup_cl (this_decl), true))
5017     return 1;
5018
5019   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5020                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5021                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5022   return 0;
5023 }
5024
5025 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5026    0 if everthing is OK.  */
5027
5028 static int
5029 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5030 {
5031   tree super_type = TREE_TYPE (super_decl);
5032
5033   /* SUPER should be a CLASS (neither an array nor an interface) */
5034   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5035     {
5036       parse_error_context
5037         (wfl, "Class `%s' can't subclass %s `%s'",
5038          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5039          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5040          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5041       return 1;
5042     }
5043
5044   if (CLASS_FINAL (TYPE_NAME (super_type)))
5045     {
5046       parse_error_context (wfl, "Can't subclass final classes: %s",
5047                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5048       return 1;
5049     }
5050
5051   /* Check top-level class scope. Inner classes are subject to member access
5052      rules (6.6.1). */
5053   if (! INNER_CLASS_P (super_type)
5054       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5055     return 1;
5056
5057   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5058                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5059                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5060   return 0;
5061 }
5062
5063 /* Create a new dependency list and link it (in a LIFO manner) to the
5064    CTXP list of type dependency list.  */
5065
5066 static void
5067 create_jdep_list (struct parser_ctxt *ctxp)
5068 {
5069   jdeplist *new = xmalloc (sizeof (jdeplist));
5070   new->first = new->last = NULL;
5071   new->next = ctxp->classd_list;
5072   ctxp->classd_list = new;
5073 }
5074
5075 static jdeplist *
5076 reverse_jdep_list (struct parser_ctxt *ctxp)
5077 {
5078   register jdeplist *prev = NULL, *current, *next;
5079   for (current = ctxp->classd_list; current; current = next)
5080     {
5081       next = current->next;
5082       current->next = prev;
5083       prev = current;
5084     }
5085   return prev;
5086 }
5087
5088 /* Create a fake pointer based on the ID stored in
5089    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5090    registered again. */
5091
5092 static tree
5093 obtain_incomplete_type (tree type_name)
5094 {
5095   tree ptr = NULL_TREE, name;
5096
5097   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5098     name = EXPR_WFL_NODE (type_name);
5099   else if (INCOMPLETE_TYPE_P (type_name))
5100     name = TYPE_NAME (type_name);
5101   else
5102     abort ();
5103
5104   /* Workaround from build_pointer_type for incomplete types.  */
5105   BUILD_PTR_FROM_NAME (ptr, name);
5106   TYPE_MODE (ptr) = ptr_mode;
5107   layout_type (ptr);
5108
5109   return ptr;
5110 }
5111
5112 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5113    non NULL instead of computing a new fake type based on WFL. The new
5114    dependency is inserted in the current type dependency list, in FIFO
5115    manner.  */
5116
5117 static tree
5118 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5119 {
5120   jdep *new = xmalloc (sizeof (jdep));
5121
5122   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5123     ptr = obtain_incomplete_type (wfl);
5124
5125   JDEP_KIND (new) = kind;
5126   JDEP_DECL (new) = decl;
5127   JDEP_TO_RESOLVE (new) = ptr;
5128   JDEP_WFL (new) = wfl;
5129   JDEP_CHAIN (new) = NULL;
5130   JDEP_MISC (new) = NULL_TREE;
5131   /* For some dependencies, set the enclosing class of the current
5132      class to be the enclosing context */
5133   if ((kind == JDEP_INTERFACE  || kind == JDEP_ANONYMOUS)
5134       && GET_ENCLOSING_CPC ())
5135     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5136   else if (kind == JDEP_SUPER)
5137     JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5138                             TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5139   else
5140     JDEP_ENCLOSING (new) = GET_CPC ();
5141   JDEP_GET_PATCH (new) = (tree *)NULL;
5142
5143   JDEP_INSERT (ctxp->classd_list, new);
5144
5145   return ptr;
5146 }
5147
5148 /* This checks for circular references with innerclasses. We start
5149    from SOURCE and should never reach TARGET. Extended/implemented
5150    types in SOURCE have their enclosing context checked not to reach
5151    TARGET. When the last enclosing context of SOURCE is reached, its
5152    extended/implemented types are also checked not to reach TARGET.
5153    In case of error, WFL of the offending type is returned; NULL_TREE
5154    otherwise.  */
5155
5156 static tree
5157 check_inner_circular_reference (tree source, tree target)
5158 {
5159   tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5160   tree ctx, cl;
5161   int i;
5162
5163   if (!basetype_vec)
5164     return NULL_TREE;
5165
5166   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5167     {
5168       tree su;
5169
5170       /* We can end up with a NULL_TREE or an incomplete type here if
5171          we encountered previous type resolution errors. It's safe to
5172          simply ignore these cases.  */
5173       if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
5174         continue;
5175       su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5176       if (INCOMPLETE_TYPE_P (su))
5177         continue;
5178
5179       if (inherits_from_p (su, target))
5180         return lookup_cl (TYPE_NAME (su));
5181
5182       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5183         {
5184           /* An enclosing context shouldn't be TARGET */
5185           if (ctx == TYPE_NAME (target))
5186             return lookup_cl (TYPE_NAME (su));
5187
5188           /* When we reach the enclosing last context, start a check
5189              on it, with the same target */
5190           if (! DECL_CONTEXT (ctx) &&
5191               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5192             return cl;
5193         }
5194     }
5195   return NULL_TREE;
5196 }
5197
5198 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5199    offending type if a circularity is detected. NULL_TREE is returned
5200    otherwise. TYPE can be an interface or a class.   */
5201
5202 static tree
5203 check_circular_reference (tree type)
5204 {
5205   tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5206   int i;
5207
5208   if (!basetype_vec)
5209     return NULL_TREE;
5210
5211   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5212     {
5213       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5214         return lookup_cl (TYPE_NAME (type));
5215       return NULL_TREE;
5216     }
5217
5218   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5219     {
5220       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5221       if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5222           && interface_of_p (type, BINFO_TYPE (vec_elt)))
5223         return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5224     }
5225   return NULL_TREE;
5226 }
5227
5228 void
5229 java_check_circular_reference (void)
5230 {
5231   tree current;
5232   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5233     {
5234       tree type = TREE_TYPE (current);
5235       tree cl;
5236
5237       cl = check_circular_reference (type);
5238       if (! cl)
5239         cl = check_inner_circular_reference (type, type);
5240       if (cl)
5241         parse_error_context (cl, "Cyclic class inheritance%s",
5242                              (cyclic_inheritance_report ?
5243                               cyclic_inheritance_report : ""));
5244     }
5245 }
5246
5247 /* Augment the parameter list PARM with parameters crafted to
5248    initialize outer context locals aliases. Through ARTIFICIAL, a
5249    count is kept of the number of crafted parameters. MODE governs
5250    what eventually gets created: something suitable for a function
5251    creation or a function invocation, either the constructor or
5252    finit$.  */
5253
5254 static tree
5255 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5256                                         int *artificial)
5257 {
5258   tree field;
5259   tree additional_parms = NULL_TREE;
5260
5261   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5262     if (FIELD_LOCAL_ALIAS (field))
5263       {
5264         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5265         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5266         tree mangled_id;
5267
5268         switch (mode)
5269           {
5270           case AIPL_FUNCTION_DECLARATION:
5271             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5272                                                          &buffer [4]);
5273             purpose = build_wfl_node (mangled_id);
5274             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5275               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5276             else
5277               value = TREE_TYPE (field);
5278             break;
5279
5280           case AIPL_FUNCTION_CREATION:
5281             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5282                                                          &buffer [4]);
5283             value = TREE_TYPE (field);
5284             break;
5285
5286           case AIPL_FUNCTION_FINIT_INVOCATION:
5287             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5288                                                          &buffer [4]);
5289             /* Now, this is wrong. purpose should always be the NAME
5290                of something and value its matching value (decl, type,
5291                etc...) FIXME -- but there is a lot to fix. */
5292
5293             /* When invoked for this kind of operation, we already
5294                know whether a field is used or not. */
5295             purpose = TREE_TYPE (field);
5296             value = build_wfl_node (mangled_id);
5297             break;
5298
5299           case AIPL_FUNCTION_CTOR_INVOCATION:
5300             /* There are two case: the constructor invokation happends
5301                outside the local inner, in which case, locales from the outer
5302                context are directly used.
5303
5304                Otherwise, we fold to using the alias directly. */
5305             if (class_type == current_class)
5306               value = field;
5307             else
5308               {
5309                 name = get_identifier (&buffer[4]);
5310                 value = IDENTIFIER_LOCAL_VALUE (name);
5311               }
5312             break;
5313           }
5314         additional_parms = tree_cons (purpose, value, additional_parms);
5315         if (artificial)
5316           *artificial +=1;
5317       }
5318   if (additional_parms)
5319     {
5320       if (ANONYMOUS_CLASS_P (class_type)
5321           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5322         additional_parms = nreverse (additional_parms);
5323       parm = chainon (additional_parms, parm);
5324     }
5325
5326    return parm;
5327 }
5328
5329 /* Craft a constructor for CLASS_DECL -- what we should do when none
5330    where found. ARGS is non NULL when a special signature must be
5331    enforced. This is the case for anonymous classes.  */
5332
5333 static tree
5334 craft_constructor (tree class_decl, tree args)
5335 {
5336   tree class_type = TREE_TYPE (class_decl);
5337   tree parm = NULL_TREE;
5338   int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5339                ACC_PUBLIC : 0);
5340   int i = 0, artificial = 0;
5341   tree decl, ctor_name;
5342   char buffer [80];
5343
5344   /* The constructor name is <init> unless we're dealing with an
5345      anonymous class, in which case the name will be fixed after having
5346      be expanded. */
5347   if (ANONYMOUS_CLASS_P (class_type))
5348     ctor_name = DECL_NAME (class_decl);
5349   else
5350     ctor_name = init_identifier_node;
5351
5352   /* If we're dealing with an inner class constructor, we hide the
5353      this$<n> decl in the name field of its parameter declaration. */
5354   if (PURE_INNER_CLASS_TYPE_P (class_type))
5355     {
5356       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5357       parm = tree_cons (build_current_thisn (class_type),
5358                         build_pointer_type (type), parm);
5359
5360       /* Some more arguments to be hidden here. The values of the local
5361          variables of the outer context that the inner class needs to see. */
5362       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5363                                                      class_type, parm,
5364                                                      &artificial);
5365     }
5366
5367   /* Then if there are any args to be enforced, enforce them now */
5368   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5369     {
5370       sprintf (buffer, "parm%d", i++);
5371       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5372     }
5373
5374   CRAFTED_PARAM_LIST_FIXUP (parm);
5375   decl = create_artificial_method (class_type, flags, void_type_node,
5376                                    ctor_name, parm);
5377   fix_method_argument_names (parm, decl);
5378   /* Now, mark the artificial parameters. */
5379   DECL_FUNCTION_NAP (decl) = artificial;
5380   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5381   return decl;
5382 }
5383
5384
5385 /* Fix the constructors. This will be called right after circular
5386    references have been checked. It is necessary to fix constructors
5387    early even if no code generation will take place for that class:
5388    some generated constructor might be required by the class whose
5389    compilation triggered this one to be simply loaded.  */
5390
5391 void
5392 java_fix_constructors (void)
5393 {
5394   tree current;
5395
5396   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5397     {
5398       tree class_type = TREE_TYPE (current);
5399       int saw_ctor = 0;
5400       tree decl;
5401
5402       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5403         continue;
5404
5405       current_class = class_type;
5406       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5407         {
5408           if (DECL_CONSTRUCTOR_P (decl))
5409             {
5410               fix_constructors (decl);
5411               saw_ctor = 1;
5412             }
5413         }
5414
5415       /* Anonymous class constructor can't be generated that early. */
5416       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5417         craft_constructor (current, NULL_TREE);
5418     }
5419 }
5420
5421 /* safe_layout_class just makes sure that we can load a class without
5422    disrupting the current_class, input_file, lineno, etc, information
5423    about the class processed currently.  */
5424
5425 void
5426 safe_layout_class (tree class)
5427 {
5428   tree save_current_class = current_class;
5429   const char *save_input_filename = input_filename;
5430   int save_lineno = lineno;
5431
5432   layout_class (class);
5433
5434   current_class = save_current_class;
5435   input_filename = save_input_filename;
5436   lineno = save_lineno;
5437 }
5438
5439 static tree
5440 jdep_resolve_class (jdep *dep)
5441 {
5442   tree decl;
5443
5444   if (JDEP_RESOLVED_P (dep))
5445     decl = JDEP_RESOLVED_DECL (dep);
5446   else
5447     {
5448       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5449                             JDEP_DECL (dep), JDEP_WFL (dep));
5450       JDEP_RESOLVED (dep, decl);
5451     }
5452
5453   if (!decl)
5454     complete_class_report_errors (dep);
5455   else if (PURE_INNER_CLASS_DECL_P (decl))
5456     {
5457       tree inner = TREE_TYPE (decl);
5458       if (! CLASS_LOADED_P (inner))
5459         {
5460           safe_layout_class (inner);
5461           if (TYPE_SIZE (inner) == error_mark_node)
5462             TYPE_SIZE (inner) = NULL_TREE;
5463         }
5464       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5465     }
5466   return decl;
5467 }
5468
5469 /* Complete unsatisfied class declaration and their dependencies */
5470
5471 void
5472 java_complete_class (void)
5473 {
5474   tree cclass;
5475   jdeplist *cclassd;
5476   int error_found;
5477   tree type;
5478
5479   /* Process imports */
5480   process_imports ();
5481
5482   /* Reverse things so we have the right order */
5483   ctxp->class_list = nreverse (ctxp->class_list);
5484   ctxp->classd_list = reverse_jdep_list (ctxp);
5485
5486   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5487        cclass && cclassd;
5488        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5489     {
5490       jdep *dep;
5491
5492       /* We keep the compilation unit imports in the class so that
5493          they can be used later to resolve type dependencies that
5494          aren't necessary to solve now. */
5495       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5496       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5497
5498       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5499         {
5500           tree decl;
5501           if (!(decl = jdep_resolve_class (dep)))
5502             continue;
5503
5504           /* Now it's time to patch */
5505           switch (JDEP_KIND (dep))
5506             {
5507             case JDEP_SUPER:
5508               /* Simply patch super */
5509               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5510                 continue;
5511               BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5512                 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5513               break;
5514
5515             case JDEP_FIELD:
5516               {
5517                 /* We do part of the job done in add_field */
5518                 tree field_decl = JDEP_DECL (dep);
5519                 tree field_type = TREE_TYPE (decl);
5520                 if (TREE_CODE (field_type) == RECORD_TYPE)
5521                   field_type = promote_type (field_type);
5522                 TREE_TYPE (field_decl) = field_type;
5523                 DECL_ALIGN (field_decl) = 0;
5524                 DECL_USER_ALIGN (field_decl) = 0;
5525                 layout_decl (field_decl, 0);
5526                 SOURCE_FRONTEND_DEBUG
5527                   (("Completed field/var decl `%s' with `%s'",
5528                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5529                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5530                 break;
5531               }
5532             case JDEP_METHOD:   /* We start patching a method */
5533             case JDEP_METHOD_RETURN:
5534               error_found = 0;
5535               while (1)
5536                 {
5537                   if (decl)
5538                     {
5539                       type = TREE_TYPE(decl);
5540                       if (TREE_CODE (type) == RECORD_TYPE)
5541                         type = promote_type (type);
5542                       JDEP_APPLY_PATCH (dep, type);
5543                       SOURCE_FRONTEND_DEBUG
5544                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5545                            "Completing fct `%s' with ret type `%s'":
5546                            "Completing arg `%s' with type `%s'"),
5547                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5548                                               (JDEP_DECL_WFL (dep))),
5549                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5550                     }
5551                   else
5552                     error_found = 1;
5553                   dep = JDEP_CHAIN (dep);
5554                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5555                     break;
5556                   else
5557                     decl = jdep_resolve_class (dep);
5558                 }
5559               if (!error_found)
5560                 {
5561                   tree mdecl = JDEP_DECL (dep), signature;
5562                   /* Recompute and reset the signature, check first that
5563                      all types are now defined. If they're not,
5564                      don't build the signature. */
5565                   if (check_method_types_complete (mdecl))
5566                     {
5567                       signature = build_java_signature (TREE_TYPE (mdecl));
5568                       set_java_signature (TREE_TYPE (mdecl), signature);
5569                     }
5570                 }
5571               else
5572                 continue;
5573               break;
5574
5575             case JDEP_INTERFACE:
5576               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5577                                                 JDEP_WFL (dep)))
5578                 continue;
5579               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5580               break;
5581
5582             case JDEP_PARM:
5583             case JDEP_VARIABLE:
5584               type = TREE_TYPE(decl);
5585               if (TREE_CODE (type) == RECORD_TYPE)
5586                 type = promote_type (type);
5587               JDEP_APPLY_PATCH (dep, type);
5588               break;
5589
5590             case JDEP_TYPE:
5591               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5592               SOURCE_FRONTEND_DEBUG
5593                 (("Completing a random type dependency on a '%s' node",
5594                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5595               break;
5596
5597             case JDEP_EXCEPTION:
5598               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5599               SOURCE_FRONTEND_DEBUG
5600                 (("Completing `%s' `throws' argument node",
5601                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5602               break;
5603
5604             case JDEP_ANONYMOUS:
5605               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5606               break;
5607
5608             default:
5609               abort ();
5610             }
5611         }
5612     }
5613   return;
5614 }
5615
5616 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5617    array.  */
5618
5619 static tree
5620 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5621 {
5622   tree tname = TYPE_NAME (class_type);
5623   tree resolved_type = TREE_TYPE (class_type);
5624   int array_dims = 0;
5625   tree resolved_type_decl;
5626
5627   if (resolved_type != NULL_TREE)
5628     {
5629       tree resolved_type_decl = TYPE_NAME (resolved_type);
5630       if (resolved_type_decl == NULL_TREE
5631           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5632         {
5633           resolved_type_decl = build_decl (TYPE_DECL,
5634                                            TYPE_NAME (class_type),
5635                                            resolved_type);
5636         }
5637       return resolved_type_decl;
5638     }
5639
5640   /* 1- Check to see if we have an array. If true, find what we really
5641      want to resolve  */
5642   if ((array_dims = build_type_name_from_array_name (tname,
5643                                                      &TYPE_NAME (class_type))))
5644     WFL_STRIP_BRACKET (cl, cl);
5645
5646   /* 2- Resolve the bare type */
5647   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5648                                                decl, cl)))
5649     return NULL_TREE;
5650   resolved_type = TREE_TYPE (resolved_type_decl);
5651
5652   /* 3- If we have an array, reconstruct the array down to its nesting */
5653   if (array_dims)
5654     {
5655       for (; array_dims; array_dims--)
5656         resolved_type = build_java_array_type (resolved_type, -1);
5657       resolved_type_decl = TYPE_NAME (resolved_type);
5658     }
5659   TREE_TYPE (class_type) = resolved_type;
5660   return resolved_type_decl;
5661 }
5662
5663 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5664    are used to report error messages. Do not try to replace TYPE_NAME
5665    (class_type) by a variable, since it is changed by
5666    find_in_imports{_on_demand} and (but it doesn't really matter)
5667    qualify_and_find.  */
5668
5669 tree
5670 do_resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5671 {
5672   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5673   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5674   tree decl_result;
5675   htab_t circularity_hash;
5676
5677   if (QUALIFIED_P (TYPE_NAME (class_type)))
5678     {
5679       /* If the type name is of the form `Q . Id', then Q is either a
5680          package name or a class name.  First we try to find Q as a
5681          class and then treat Id as a member type.  If we can't find Q
5682          as a class then we fall through.  */
5683       tree q, left, left_type, right;
5684       breakdown_qualified (&left, &right, TYPE_NAME (class_type));
5685       BUILD_PTR_FROM_NAME (left_type, left);
5686       q = do_resolve_class (enclosing, left_type, decl, cl);
5687       if (q)
5688         {
5689           enclosing = q;
5690           saved_enclosing_type = TREE_TYPE (q);
5691           BUILD_PTR_FROM_NAME (class_type, right);
5692         }
5693     }
5694
5695   if (enclosing)
5696     {
5697       /* This hash table is used to register the classes we're going
5698          through when searching the current class as an inner class, in
5699          order to detect circular references. Remember to free it before
5700          returning the section 0- of this function. */
5701       circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5702                                       NULL);
5703
5704       /* 0- Search in the current class as an inner class.
5705          Maybe some code here should be added to load the class or
5706          something, at least if the class isn't an inner class and ended
5707          being loaded from class file. FIXME. */
5708       while (enclosing)
5709         {
5710           new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5711                                                 &super, class_type);
5712           if (new_class_decl)
5713             break;
5714
5715           /* If we haven't found anything because SUPER reached Object and
5716              ENCLOSING happens to be an innerclass, try the enclosing context. */
5717           if ((!super || super == object_type_node) &&
5718               enclosing && INNER_CLASS_DECL_P (enclosing))
5719             enclosing = DECL_CONTEXT (enclosing);
5720           else
5721             enclosing = NULL_TREE;
5722         }
5723
5724       htab_delete (circularity_hash);
5725
5726       if (new_class_decl)
5727         return new_class_decl;
5728     }
5729
5730   /* 1- Check for the type in single imports. This will change
5731      TYPE_NAME() if something relevant is found */
5732   find_in_imports (saved_enclosing_type, class_type);
5733
5734   /* 2- And check for the type in the current compilation unit */
5735   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5736     {
5737       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5738           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5739         load_class (TYPE_NAME (class_type), 0);
5740       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5741     }
5742
5743   /* 3- Search according to the current package definition */
5744   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5745     {
5746       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5747                                              TYPE_NAME (class_type))))
5748         return new_class_decl;
5749     }
5750
5751   /* 4- Check the import on demands. Don't allow bar.baz to be
5752      imported from foo.* */
5753   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5754     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5755       return NULL_TREE;
5756
5757   /* If found in find_in_imports_on_demand, the type has already been
5758      loaded. */
5759   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5760     return new_class_decl;
5761
5762   /* 5- Try with a name qualified with the package name we've seen so far */
5763   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5764     {
5765       tree package;
5766
5767       /* If there is a current package (ctxp->package), it's the first
5768          element of package_list and we can skip it. */
5769       for (package = (ctxp->package ?
5770                       TREE_CHAIN (package_list) : package_list);
5771            package; package = TREE_CHAIN (package))
5772         if ((new_class_decl = qualify_and_find (class_type,
5773                                                TREE_PURPOSE (package),
5774                                                TYPE_NAME (class_type))))
5775           return new_class_decl;
5776     }
5777
5778   /* 5- Check another compilation unit that bears the name of type */
5779   load_class (TYPE_NAME (class_type), 0);
5780
5781   if (!cl)
5782     cl = lookup_cl (decl);
5783
5784   /* If we don't have a value for CL, then we're being called recursively.
5785      We can't check package access just yet, but it will be taken care of
5786      by the caller. */
5787   if (cl)
5788     {
5789       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5790         return NULL_TREE;
5791     }
5792
5793   /* 6- Last call for a resolution */
5794   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5795
5796   /* The final lookup might have registered a.b.c into a.b$c If we
5797      failed at the first lookup, progressively change the name if
5798      applicable and use the matching DECL instead. */
5799   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5800     {
5801       char *separator;
5802       tree name = TYPE_NAME (class_type);
5803       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5804
5805       strcpy (namebuffer, IDENTIFIER_POINTER (name));
5806
5807       do {
5808
5809        /* Reach the last '.', and if applicable, replace it by a `$' and
5810           see if this exists as a type. */
5811        if ((separator = strrchr (namebuffer, '.')))
5812          {
5813            *separator = '$';
5814            name = get_identifier (namebuffer);
5815            decl_result = IDENTIFIER_CLASS_VALUE (name);
5816          }
5817       } while (!decl_result && separator);
5818     }
5819   return decl_result;
5820 }
5821
5822 static tree
5823 qualify_and_find (tree class_type, tree package, tree name)
5824 {
5825   tree new_qualified = merge_qualified_name (package, name);
5826   tree new_class_decl;
5827
5828   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5829     load_class (new_qualified, 0);
5830   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5831     {
5832       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5833           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5834         load_class (new_qualified, 0);
5835       TYPE_NAME (class_type) = new_qualified;
5836       return IDENTIFIER_CLASS_VALUE (new_qualified);
5837     }
5838   return NULL_TREE;
5839 }
5840
5841 /* Resolve NAME and lay it out (if not done and if not the current
5842    parsed class). Return a decl node. This function is meant to be
5843    called when type resolution is necessary during the walk pass.  */
5844
5845 static tree
5846 resolve_and_layout (tree something, tree cl)
5847 {
5848   tree decl, decl_type;
5849
5850   /* Don't do that on the current class */
5851   if (something == current_class)
5852     return TYPE_NAME (current_class);
5853
5854   /* Don't do anything for void and other primitive types */
5855   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5856     return NULL_TREE;
5857
5858   /* Pointer types can be reall pointer types or fake pointers. When
5859      finding a real pointer, recheck for primitive types */
5860   if (TREE_CODE (something) == POINTER_TYPE)
5861     {
5862       if (TREE_TYPE (something))
5863         {
5864           something = TREE_TYPE (something);
5865           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5866             return NULL_TREE;
5867         }
5868       else
5869         something = TYPE_NAME (something);
5870     }
5871
5872   /* Don't do anything for arrays of primitive types */
5873   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5874       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5875     return NULL_TREE;
5876
5877   /* Something might be a WFL */
5878   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5879     something = EXPR_WFL_NODE (something);
5880
5881   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5882      TYPE_DECL or a real TYPE */
5883   else if (TREE_CODE (something) != IDENTIFIER_NODE)
5884     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5885             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5886
5887   if (!(decl = resolve_no_layout (something, cl)))
5888     return NULL_TREE;
5889
5890   /* Resolve and layout if necessary */
5891   decl_type = TREE_TYPE (decl);
5892   layout_class_methods (decl_type);
5893   /* Check methods */
5894   if (CLASS_FROM_SOURCE_P (decl_type))
5895     java_check_methods (decl);
5896   /* Layout the type if necessary */
5897   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5898     safe_layout_class (decl_type);
5899
5900   return decl;
5901 }
5902
5903 /* Resolve a class, returns its decl but doesn't perform any
5904    layout. The current parsing context is saved and restored */
5905
5906 static tree
5907 resolve_no_layout (tree name, tree cl)
5908 {
5909   tree ptr, decl;
5910   BUILD_PTR_FROM_NAME (ptr, name);
5911   java_parser_context_save_global ();
5912   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5913   java_parser_context_restore_global ();
5914
5915   return decl;
5916 }
5917
5918 /* Called when reporting errors. Skip the '[]'s in a complex array
5919    type description that failed to be resolved. purify_type_name can't
5920    use an identifier tree.  */
5921
5922 static const char *
5923 purify_type_name (const char *name)
5924 {
5925   int len = strlen (name);
5926   int bracket_found;
5927
5928   STRING_STRIP_BRACKETS (name, len, bracket_found);
5929   if (bracket_found)
5930     {
5931       char *stripped_name = xmemdup (name, len, len+1);
5932       stripped_name [len] = '\0';
5933       return stripped_name;
5934     }
5935   return name;
5936 }
5937
5938 /* The type CURRENT refers to can't be found. We print error messages.  */
5939
5940 static void
5941 complete_class_report_errors (jdep *dep)
5942 {
5943   const char *name;
5944
5945   if (!JDEP_WFL (dep))
5946     return;
5947
5948   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5949   switch (JDEP_KIND (dep))
5950     {
5951     case JDEP_SUPER:
5952       parse_error_context
5953         (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5954          purify_type_name (name),
5955          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5956       break;
5957     case JDEP_FIELD:
5958       parse_error_context
5959         (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5960          purify_type_name (name),
5961          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5962       break;
5963     case JDEP_METHOD:           /* Covers arguments */
5964       parse_error_context
5965         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5966          purify_type_name (name),
5967          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5968          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5969       break;
5970     case JDEP_METHOD_RETURN:    /* Covers return type */
5971       parse_error_context
5972         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5973          purify_type_name (name),
5974          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5975       break;
5976     case JDEP_INTERFACE:
5977       parse_error_context
5978         (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5979          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5980          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5981          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5982       break;
5983     case JDEP_VARIABLE:
5984       parse_error_context
5985         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5986          purify_type_name (IDENTIFIER_POINTER
5987                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5988          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5989       break;
5990     case JDEP_EXCEPTION:        /* As specified by `throws' */
5991       parse_error_context
5992           (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5993          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5994       break;
5995     default:
5996       /* Fix for -Wall. Just break doing nothing. The error will be
5997          caught later */
5998       break;
5999     }
6000 }
6001
6002 /* Return a static string containing the DECL prototype string. If
6003    DECL is a constructor, use the class name instead of the form
6004    <init> */
6005
6006 static const char *
6007 get_printable_method_name (tree decl)
6008 {
6009   const char *to_return;
6010   tree name = NULL_TREE;
6011
6012   if (DECL_CONSTRUCTOR_P (decl))
6013     {
6014       name = DECL_NAME (decl);
6015       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6016     }
6017
6018   to_return = lang_printable_name (decl, 0);
6019   if (DECL_CONSTRUCTOR_P (decl))
6020     DECL_NAME (decl) = name;
6021
6022   return to_return;
6023 }
6024
6025 /* Track method being redefined inside the same class. As a side
6026    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6027    function it's a FWL, so we can track errors more accurately.)  */
6028
6029 static int
6030 check_method_redefinition (tree class, tree method)
6031 {
6032   tree redef, sig;
6033
6034   /* There's no need to verify <clinit> and finit$ and instinit$ */
6035   if (DECL_CLINIT_P (method)
6036       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6037     return 0;
6038
6039   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6040   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6041     {
6042       if (redef == method)
6043         break;
6044       if (DECL_NAME (redef) == DECL_NAME (method)
6045           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6046           && !DECL_ARTIFICIAL (method))
6047         {
6048           parse_error_context
6049             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6050              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6051              get_printable_method_name (redef));
6052           return 1;
6053         }
6054     }
6055   return 0;
6056 }
6057
6058 /* Return 1 if check went ok, 0 otherwise.  */
6059 static int
6060 check_abstract_method_definitions (int do_interface, tree class_decl,
6061                                    tree type)
6062 {
6063   tree class = TREE_TYPE (class_decl);
6064   tree method, end_type;
6065   int ok = 1;
6066
6067   end_type = (do_interface ? object_type_node : type);
6068   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6069     {
6070       tree other_super, other_method, method_sig, method_name;
6071       int found = 0;
6072       int end_type_reached = 0;
6073
6074       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6075         continue;
6076
6077       /* Now verify that somewhere in between TYPE and CLASS,
6078          abstract method METHOD gets a non abstract definition
6079          that is inherited by CLASS.  */
6080
6081       method_sig = build_java_signature (TREE_TYPE (method));
6082       method_name = DECL_NAME (method);
6083       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6084         method_name = EXPR_WFL_NODE (method_name);
6085
6086       other_super = class;
6087       do {
6088         if (other_super == end_type)
6089           end_type_reached = 1;
6090
6091         /* Method search */
6092         for (other_method = TYPE_METHODS (other_super); other_method;
6093             other_method = TREE_CHAIN (other_method))
6094           {
6095             tree s = build_java_signature (TREE_TYPE (other_method));
6096             tree other_name = DECL_NAME (other_method);
6097
6098             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6099               other_name = EXPR_WFL_NODE (other_name);
6100             if (!DECL_CLINIT_P (other_method)
6101                 && !DECL_CONSTRUCTOR_P (other_method)
6102                 && method_name == other_name
6103                 && method_sig == s
6104                 && !METHOD_ABSTRACT (other_method))
6105              {
6106                found = 1;
6107                break;
6108              }
6109           }
6110         other_super = CLASSTYPE_SUPER (other_super);
6111       } while (!end_type_reached);
6112
6113       /* Report that abstract METHOD didn't find an implementation
6114          that CLASS can use. */
6115       if (!found)
6116         {
6117           char *t = xstrdup (lang_printable_name
6118                             (TREE_TYPE (TREE_TYPE (method)), 0));
6119           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6120
6121           parse_error_context
6122             (lookup_cl (class_decl),
6123              "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract",
6124              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6125              t, lang_printable_name (method, 0),
6126              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6127               "interface" : "class"),
6128              IDENTIFIER_POINTER (ccn),
6129              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6130              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6131           ok = 0;
6132           free (t);
6133         }
6134     }
6135
6136   if (ok && do_interface)
6137     {
6138       /* Check for implemented interfaces. */
6139       int i;
6140       tree vector = TYPE_BINFO_BASETYPES (type);
6141       for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6142         {
6143           tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6144           ok = check_abstract_method_definitions (1, class_decl, super);
6145         }
6146     }
6147
6148   return ok;
6149 }
6150
6151 /* Check that CLASS_DECL somehow implements all inherited abstract
6152    methods.  */
6153
6154 static void
6155 java_check_abstract_method_definitions (tree class_decl)
6156 {
6157   tree class = TREE_TYPE (class_decl);
6158   tree super, vector;
6159   int i;
6160
6161   if (CLASS_ABSTRACT (class_decl))
6162     return;
6163
6164   /* Check for inherited types */
6165   super = class;
6166   do {
6167     super = CLASSTYPE_SUPER (super);
6168     check_abstract_method_definitions (0, class_decl, super);
6169   } while (super != object_type_node);
6170
6171   /* Check for implemented interfaces. */
6172   vector = TYPE_BINFO_BASETYPES (class);
6173   for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6174     {
6175       super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6176       check_abstract_method_definitions (1, class_decl, super);
6177     }
6178 }
6179
6180 /* Check all the types method DECL uses and return 1 if all of them
6181    are now complete, 0 otherwise. This is used to check whether its
6182    safe to build a method signature or not.  */
6183
6184 static int
6185 check_method_types_complete (tree decl)
6186 {
6187   tree type = TREE_TYPE (decl);
6188   tree args;
6189
6190   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6191     return 0;
6192
6193   args = TYPE_ARG_TYPES (type);
6194   if (TREE_CODE (type) == METHOD_TYPE)
6195     args = TREE_CHAIN (args);
6196   for (; args != end_params_node; args = TREE_CHAIN (args))
6197     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6198       return 0;
6199
6200   return 1;
6201 }
6202
6203 /* Visible interface to check methods contained in CLASS_DECL */
6204
6205 void
6206 java_check_methods (tree class_decl)
6207 {
6208   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6209     return;
6210
6211   if (CLASS_INTERFACE (class_decl))
6212     java_check_abstract_methods (class_decl);
6213   else
6214     java_check_regular_methods (class_decl);
6215
6216   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6217 }
6218
6219 /* Check all the methods of CLASS_DECL. Methods are first completed
6220    then checked according to regular method existence rules.  If no
6221    constructor for CLASS_DECL were encountered, then build its
6222    declaration.  */
6223
6224 static void
6225 java_check_regular_methods (tree class_decl)
6226 {
6227   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6228   tree method;
6229   tree class = TREE_TYPE (class_decl);
6230   tree found = NULL_TREE;
6231   tree mthrows;
6232
6233   /* It is not necessary to check methods defined in java.lang.Object */
6234   if (class == object_type_node)
6235     return;
6236
6237   if (!TYPE_NVIRTUALS (class))
6238     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6239
6240   /* Should take interfaces into account. FIXME */
6241   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6242     {
6243       tree sig;
6244       tree method_wfl = DECL_FUNCTION_WFL (method);
6245       int aflags;
6246
6247       /* Check for redefinitions */
6248       if (check_method_redefinition (class, method))
6249         continue;
6250
6251       /* If we see one constructor a mark so we don't generate the
6252          default one. Also skip other verifications: constructors
6253          can't be inherited hence hiden or overriden */
6254      if (DECL_CONSTRUCTOR_P (method))
6255        {
6256          saw_constructor = 1;
6257          continue;
6258        }
6259
6260       /* We verify things thrown by the method. They must inherits from
6261          java.lang.Throwable */
6262       for (mthrows = DECL_FUNCTION_THROWS (method);
6263            mthrows; mthrows = TREE_CHAIN (mthrows))
6264         {
6265           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6266             parse_error_context
6267               (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6268                IDENTIFIER_POINTER
6269                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6270         }
6271
6272       sig = build_java_argument_signature (TREE_TYPE (method));
6273       found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6274
6275       /* Inner class can't declare static methods */
6276       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6277         {
6278           char *t = xstrdup (lang_printable_name (class, 0));
6279           parse_error_context
6280             (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6281              lang_printable_name (method, 0), t);
6282           free (t);
6283         }
6284
6285       /* Nothing overrides or it's a private method. */
6286       if (!found)
6287         continue;
6288       if (METHOD_PRIVATE (found))
6289         {
6290           found = NULL_TREE;
6291           continue;
6292         }
6293
6294       /* If `found' is declared in an interface, make sure the
6295          modifier matches. */
6296       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6297           && clinit_identifier_node != DECL_NAME (found)
6298           && !METHOD_PUBLIC (method))
6299         {
6300           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6301           parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6302                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6303                                lang_printable_name (method, 0),
6304                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6305         }
6306
6307       /* Can't override a method with the same name and different return
6308          types. */
6309       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6310         {
6311           char *t = xstrdup
6312             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6313           parse_error_context
6314             (method_wfl,
6315              "Method `%s' was defined with return type `%s' in class `%s'",
6316              lang_printable_name (found, 0), t,
6317              IDENTIFIER_POINTER
6318                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6319           free (t);
6320         }
6321
6322       aflags = get_access_flags_from_decl (found);
6323
6324       /* Can't override final. Can't override static. */
6325       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6326         {
6327           /* Static *can* override static */
6328           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6329             continue;
6330           parse_error_context
6331             (method_wfl,
6332              "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6333              (METHOD_FINAL (found) ? "Final" : "Static"),
6334              lang_printable_name (found, 0),
6335              (METHOD_FINAL (found) ? "final" : "static"),
6336              IDENTIFIER_POINTER
6337                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6338           continue;
6339         }
6340
6341       /* Static method can't override instance method. */
6342       if (METHOD_STATIC (method))
6343         {
6344           parse_error_context
6345             (method_wfl,
6346              "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6347              lang_printable_name (found, 0),
6348              IDENTIFIER_POINTER
6349                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6350           continue;
6351         }
6352
6353       /* - Overriding/hiding public must be public
6354          - Overriding/hiding protected must be protected or public
6355          - If the overriden or hidden method has default (package)
6356            access, then the overriding or hiding method must not be
6357            private; otherwise, a compile-time error occurs.  If
6358            `found' belongs to an interface, things have been already
6359            taken care of.  */
6360       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6361           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6362               || (METHOD_PROTECTED (found)
6363                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6364               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6365                   && METHOD_PRIVATE (method))))
6366         {
6367           parse_error_context
6368             (method_wfl,
6369              "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6370              (METHOD_PUBLIC (method) ? "public" :
6371               (METHOD_PRIVATE (method) ? "private" : "protected")),
6372              IDENTIFIER_POINTER (DECL_NAME
6373                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6374           continue;
6375         }
6376
6377       /* Overriding methods must have compatible `throws' clauses on checked
6378          exceptions, if any */
6379       check_throws_clauses (method, method_wfl, found);
6380
6381       /* Inheriting multiple methods with the same signature. FIXME */
6382     }
6383
6384   if (!TYPE_NVIRTUALS (class))
6385     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6386
6387   /* Search for inherited abstract method not yet implemented in this
6388      class.  */
6389   java_check_abstract_method_definitions (class_decl);
6390
6391   if (!saw_constructor)
6392     abort ();
6393 }
6394
6395 /* Return a nonzero value if the `throws' clause of METHOD (if any)
6396    is incompatible with the `throws' clause of FOUND (if any).  */
6397
6398 static void
6399 check_throws_clauses (tree method, tree method_wfl, tree found)
6400 {
6401   tree mthrows, fthrows;
6402
6403   /* Can't check these things with class loaded from bytecode. FIXME */
6404   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6405     return;
6406
6407   for (mthrows = DECL_FUNCTION_THROWS (method);
6408        mthrows; mthrows = TREE_CHAIN (mthrows))
6409     {
6410       /* We don't verify unchecked expressions */
6411       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6412         continue;
6413       /* Checked expression must be compatible */
6414       for (fthrows = DECL_FUNCTION_THROWS (found);
6415            fthrows; fthrows = TREE_CHAIN (fthrows))
6416         if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6417           break;
6418       if (!fthrows)
6419         {
6420           parse_error_context
6421             (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'",
6422              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6423              lang_printable_name (found, 0),
6424              IDENTIFIER_POINTER
6425                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6426         }
6427     }
6428 }
6429
6430 /* Check abstract method of interface INTERFACE */
6431
6432 static void
6433 java_check_abstract_methods (tree interface_decl)
6434 {
6435   int i, n;
6436   tree method, basetype_vec, found;
6437   tree interface = TREE_TYPE (interface_decl);
6438
6439   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6440     {
6441       /* 2- Check for double definition inside the defining interface */
6442       if (check_method_redefinition (interface, method))
6443         continue;
6444
6445       /* 3- Overriding is OK as far as we preserve the return type and
6446          the thrown exceptions (FIXME) */
6447       found = lookup_java_interface_method2 (interface, method);
6448       if (found)
6449         {
6450           char *t;
6451           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6452           parse_error_context
6453             (DECL_FUNCTION_WFL (found),
6454              "Method `%s' was defined with return type `%s' in class `%s'",
6455              lang_printable_name (found, 0), t,
6456              IDENTIFIER_POINTER
6457                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6458           free (t);
6459           continue;
6460         }
6461     }
6462
6463   /* 4- Inherited methods can't differ by their returned types */
6464   if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6465     return;
6466   n = TREE_VEC_LENGTH (basetype_vec);
6467   for (i = 0; i < n; i++)
6468     {
6469       tree sub_interface_method, sub_interface;
6470       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6471       if (!vec_elt)
6472         continue;
6473       sub_interface = BINFO_TYPE (vec_elt);
6474       for (sub_interface_method = TYPE_METHODS (sub_interface);
6475            sub_interface_method;
6476            sub_interface_method = TREE_CHAIN (sub_interface_method))
6477         {
6478           found = lookup_java_interface_method2 (interface,
6479                                                  sub_interface_method);
6480           if (found && (found != sub_interface_method))
6481             {
6482               parse_error_context
6483                 (lookup_cl (sub_interface_method),
6484                  "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6485                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6486                  lang_printable_name (found, 0),
6487                  IDENTIFIER_POINTER
6488                    (DECL_NAME (TYPE_NAME
6489                                (DECL_CONTEXT (sub_interface_method)))),
6490                  IDENTIFIER_POINTER
6491                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6492             }
6493         }
6494     }
6495 }
6496
6497 /* Lookup methods in interfaces using their name and partial
6498    signature. Return a matching method only if their types differ.  */
6499
6500 static tree
6501 lookup_java_interface_method2 (tree class, tree method_decl)
6502 {
6503   int i, n;
6504   tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6505
6506   if (!basetype_vec)
6507     return NULL_TREE;
6508
6509   n = TREE_VEC_LENGTH (basetype_vec);
6510   for (i = 0; i < n; i++)
6511     {
6512       tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6513       if ((BINFO_TYPE (vec_elt) != object_type_node)
6514           && (to_return =
6515               lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6516         return to_return;
6517     }
6518   for (i = 0; i < n; i++)
6519     {
6520       to_return = lookup_java_interface_method2
6521         (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6522       if (to_return)
6523         return to_return;
6524     }
6525
6526   return NULL_TREE;
6527 }
6528
6529 /* Lookup method using their name and partial signature. Return a
6530    matching method only if their types differ.  */
6531
6532 static tree
6533 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6534 {
6535   tree method, method_signature, method_name, method_type, name;
6536
6537   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6538   name = DECL_NAME (method_decl);
6539   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6540                  EXPR_WFL_NODE (name) : name);
6541   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6542
6543   while (clas != NULL_TREE)
6544     {
6545       for (method = TYPE_METHODS (clas);
6546            method != NULL_TREE;  method = TREE_CHAIN (method))
6547         {
6548           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6549           tree name = DECL_NAME (method);
6550           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6551                EXPR_WFL_NODE (name) : name) == method_name
6552               && method_sig == method_signature
6553               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6554             return method;
6555         }
6556       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6557     }
6558   return NULL_TREE;
6559 }
6560
6561 /* Return the line that matches DECL line number, and try its best to
6562    position the column number. Used during error reports.  */
6563
6564 static GTY(()) tree cl_v;
6565 static tree
6566 lookup_cl (tree decl)
6567 {
6568   char *line, *found;
6569
6570   if (!decl)
6571     return NULL_TREE;
6572
6573   if (cl_v == NULL_TREE)
6574     {
6575       cl_v = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6576     }
6577
6578   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6579   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE_FIRST (decl), -1);
6580
6581   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6582                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6583
6584   found = strstr ((const char *)line,
6585                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6586   if (found)
6587     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6588
6589   return cl_v;
6590 }
6591
6592 /* Look for a simple name in the single-type import list */
6593
6594 static tree
6595 find_name_in_single_imports (tree name)
6596 {
6597   tree node;
6598
6599   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6600     if (TREE_VALUE (node) == name)
6601       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6602
6603   return NULL_TREE;
6604 }
6605
6606 /* Process all single-type import. */
6607
6608 static int
6609 process_imports (void)
6610 {
6611   tree import;
6612   int error_found;
6613
6614   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6615     {
6616       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6617       char *original_name;
6618
6619       original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6620                                IDENTIFIER_LENGTH (to_be_found),
6621                                IDENTIFIER_LENGTH (to_be_found) + 1);
6622
6623       /* Don't load twice something already defined. */
6624       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6625         continue;
6626
6627       while (1)
6628         {
6629           tree left;
6630
6631           QUALIFIED_P (to_be_found) = 1;
6632           load_class (to_be_found, 0);
6633           error_found =
6634             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6635
6636           /* We found it, we can bail out */
6637           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6638             break;
6639
6640           /* We haven't found it. Maybe we're trying to access an
6641              inner class.  The only way for us to know is to try again
6642              after having dropped a qualifier. If we can't break it further,
6643              we have an error. */
6644           if (breakdown_qualified (&left, NULL, to_be_found))
6645             break;
6646
6647           to_be_found = left;
6648         }
6649       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6650         {
6651           parse_error_context (TREE_PURPOSE (import),
6652                                "Class or interface `%s' not found in import",
6653                                original_name);
6654           error_found = 1;
6655         }
6656
6657       free (original_name);
6658       if (error_found)
6659         return 1;
6660     }
6661   return 0;
6662 }
6663
6664 /* Possibly find and mark a class imported by a single-type import
6665    statement.  */
6666
6667 static void
6668 find_in_imports (tree enclosing_type, tree class_type)
6669 {
6670   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6671                  ctxp->import_list);
6672   while (import)
6673     {
6674       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6675         {
6676           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6677           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6678           return;
6679         }
6680       import = TREE_CHAIN (import);
6681     }
6682 }
6683
6684 static int
6685 note_possible_classname (const char *name, int len)
6686 {
6687   tree node;
6688   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6689     len = len - 5;
6690   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6691     len = len - 6;
6692   else
6693     return 0;
6694   node = ident_subst (name, len, "", '/', '.', "");
6695   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6696   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6697   return 1;
6698 }
6699
6700 /* Read a import directory, gathering potential match for further type
6701    references. Indifferently reads a filesystem or a ZIP archive
6702    directory.  */
6703
6704 static void
6705 read_import_dir (tree wfl)
6706 {
6707   tree package_id = EXPR_WFL_NODE (wfl);
6708   const char *package_name = IDENTIFIER_POINTER (package_id);
6709   int package_length = IDENTIFIER_LENGTH (package_id);
6710   DIR *dirp = NULL;
6711   JCF *saved_jcf = current_jcf;
6712
6713   int found = 0;
6714   int k;
6715   void *entry;
6716   struct buffer filename[1];
6717
6718   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6719     return;
6720   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6721
6722   BUFFER_INIT (filename);
6723   buffer_grow (filename, package_length + 100);
6724
6725   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6726     {
6727       const char *entry_name = jcf_path_name (entry);
6728       int entry_length = strlen (entry_name);
6729       if (jcf_path_is_zipfile (entry))
6730         {
6731           ZipFile *zipf;
6732           buffer_grow (filename, entry_length);
6733           memcpy (filename->data, entry_name, entry_length - 1);
6734           filename->data[entry_length-1] = '\0';
6735           zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6736           if (zipf == NULL)
6737             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6738           else
6739             {
6740               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6741               BUFFER_RESET (filename);
6742               for (k = 0; k < package_length; k++)
6743                 {
6744                   char ch = package_name[k];
6745                   *filename->ptr++ = ch == '.' ? '/' : ch;
6746                 }
6747               *filename->ptr++ = '/';
6748
6749               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
6750                 {
6751                   const char *current_entry = ZIPDIR_FILENAME (zipd);
6752                   int current_entry_len = zipd->filename_length;
6753
6754                   if (current_entry_len >= BUFFER_LENGTH (filename)
6755                       && strncmp (filename->data, current_entry,
6756                                   BUFFER_LENGTH (filename)) != 0)
6757                     continue;
6758                   found |= note_possible_classname (current_entry,
6759                                                     current_entry_len);
6760                 }
6761             }
6762         }
6763       else
6764         {
6765           BUFFER_RESET (filename);
6766           buffer_grow (filename, entry_length + package_length + 4);
6767           strcpy (filename->data, entry_name);
6768           filename->ptr = filename->data + entry_length;
6769           for (k = 0; k < package_length; k++)
6770             {
6771               char ch = package_name[k];
6772               *filename->ptr++ = ch == '.' ? '/' : ch;
6773             }
6774           *filename->ptr = '\0';
6775
6776           dirp = opendir (filename->data);
6777           if (dirp == NULL)
6778             continue;
6779           *filename->ptr++ = '/';
6780           for (;;)
6781             {
6782               int len;
6783               const char *d_name;
6784               struct dirent *direntp = readdir (dirp);
6785               if (!direntp)
6786                 break;
6787               d_name = direntp->d_name;
6788               len = strlen (direntp->d_name);
6789               buffer_grow (filename, len+1);
6790               strcpy (filename->ptr, d_name);
6791               found |= note_possible_classname (filename->data + entry_length,
6792                                                 package_length+len+1);
6793             }
6794           if (dirp)
6795             closedir (dirp);
6796         }
6797     }
6798
6799   free (filename->data);
6800
6801   /* Here we should have a unified way of retrieving an entry, to be
6802      indexed. */
6803   if (!found)
6804     {
6805       static int first = 1;
6806       if (first)
6807         {
6808           error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6809           java_error_count++;
6810           first = 0;
6811         }
6812       else
6813         parse_error_context (wfl, "Package `%s' not found in import",
6814                              package_name);
6815       current_jcf = saved_jcf;
6816       return;
6817     }
6818   current_jcf = saved_jcf;
6819 }
6820
6821 /* Possibly find a type in the import on demands specified
6822    types. Returns 1 if an error occurred, 0 otherwise. Run through the
6823    entire list, to detected potential double definitions.  */
6824
6825 static int
6826 find_in_imports_on_demand (tree enclosing_type, tree class_type)
6827 {
6828   tree class_type_name = TYPE_NAME (class_type);
6829   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6830                   ctxp->import_demand_list);
6831   tree cl = NULL_TREE;
6832   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
6833   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
6834   tree node;
6835
6836   for (; import; import = TREE_CHAIN (import))
6837     {
6838       int saved_lineno = lineno;
6839       int access_check;
6840       const char *id_name;
6841       tree decl, type_name_copy;
6842
6843       obstack_grow (&temporary_obstack,
6844                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6845                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6846       obstack_1grow (&temporary_obstack, '.');
6847       obstack_grow0 (&temporary_obstack,
6848                      IDENTIFIER_POINTER (class_type_name),
6849                      IDENTIFIER_LENGTH (class_type_name));
6850       id_name = obstack_finish (&temporary_obstack);
6851
6852       if (! (node = maybe_get_identifier (id_name)))
6853         continue;
6854
6855       /* Setup lineno so that it refers to the line of the import (in
6856          case we parse a class file and encounter errors */
6857       lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6858
6859       type_name_copy = TYPE_NAME (class_type);
6860       TYPE_NAME (class_type) = node;
6861       QUALIFIED_P (node) = 1;
6862       decl = IDENTIFIER_CLASS_VALUE (node);
6863       access_check = -1;
6864       /* If there is no DECL set for the class or if the class isn't
6865          loaded and not seen in source yet, then load */
6866       if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6867                     && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6868         {
6869           load_class (node, 0);
6870           decl = IDENTIFIER_CLASS_VALUE (node);
6871         }
6872       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
6873         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
6874                                                false);
6875       else
6876         /* 6.6.1: Inner classes are subject to member access rules. */
6877         access_check = 0;
6878
6879       lineno = saved_lineno;
6880
6881       /* If the loaded class is not accessible or couldn't be loaded,
6882          we restore the original TYPE_NAME and process the next
6883          import. */
6884       if (access_check || !decl)
6885         {
6886           TYPE_NAME (class_type) = type_name_copy;
6887           continue;
6888         }
6889
6890       /* If the loaded class is accessible, we keep a tab on it to
6891          detect and report multiple inclusions. */
6892       if (IS_A_CLASSFILE_NAME (node))
6893         {
6894           if (seen_once < 0)
6895             {
6896               cl = TREE_PURPOSE (import);
6897               seen_once = 1;
6898             }
6899           else if (seen_once >= 0)
6900             {
6901               tree location = (cl ? cl : TREE_PURPOSE (import));
6902               tree package = (cl ? EXPR_WFL_NODE (cl) :
6903                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
6904               seen_once++;
6905               parse_error_context
6906                 (location,
6907                  "Type `%s' also potentially defined in package `%s'",
6908                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6909                  IDENTIFIER_POINTER (package));
6910             }
6911         }
6912       to_return = access_check;
6913     }
6914
6915   if (seen_once == 1)
6916     return to_return;
6917   else
6918     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6919 }
6920
6921 /* Add package NAME to the list of packages encountered so far. To
6922    speed up class lookup in do_resolve_class, we make sure a
6923    particular package is added only once.  */
6924
6925 static void
6926 register_package (tree name)
6927 {
6928   static htab_t pht;
6929   void **e;
6930
6931   if (pht == NULL)
6932     pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
6933
6934   e = htab_find_slot (pht, name, INSERT);
6935   if (*e == NULL)
6936     {
6937       package_list = chainon (package_list, build_tree_list (name, NULL));
6938       *e = name;
6939     }
6940 }
6941
6942 static tree
6943 resolve_package (tree pkg, tree *next, tree *type_name)
6944 {
6945   tree current;
6946   tree decl = NULL_TREE;
6947   *type_name = NULL_TREE;
6948
6949   /* The trick is to determine when the package name stops and were
6950      the name of something contained in the package starts. Then we
6951      return a fully qualified name of what we want to get. */
6952
6953   *next = EXPR_WFL_QUALIFICATION (pkg);
6954
6955   /* Try to progressively construct a type name */
6956   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6957     for (current = EXPR_WFL_QUALIFICATION (pkg);
6958          current; current = TREE_CHAIN (current))
6959       {
6960         /* If we don't have what we're expecting, exit now. TYPE_NAME
6961            will be null and the error caught later. */
6962         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
6963           break;
6964         *type_name =
6965           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
6966         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
6967           {
6968             /* resolve_package should be used in a loop, hence we
6969                point at this one to naturally process the next one at
6970                the next iteration. */
6971             *next = current;
6972             break;
6973           }
6974       }
6975   return decl;
6976 }
6977
6978
6979 /* Check accessibility of inner classes according to member access rules.
6980    DECL is the inner class, ENCLOSING_DECL is the class from which the
6981    access is being attempted. */
6982
6983 static void
6984 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
6985 {
6986   const char *access;
6987   tree enclosing_decl_type;
6988
6989   /* We don't issue an error message when CL is null. CL can be null
6990      as a result of processing a JDEP crafted by source_start_java_method
6991      for the purpose of patching its parm decl. But the error would
6992      have been already trapped when fixing the method's signature.
6993      DECL can also be NULL in case of earlier errors. */
6994   if (!decl || !cl)
6995     return;
6996
6997   enclosing_decl_type = TREE_TYPE (enclosing_decl);
6998
6999   if (CLASS_PRIVATE (decl))
7000     {
7001       /* Access is permitted only within the body of the top-level
7002          class in which DECL is declared. */
7003       tree top_level = decl;
7004       while (DECL_CONTEXT (top_level))
7005         top_level = DECL_CONTEXT (top_level);
7006       while (DECL_CONTEXT (enclosing_decl))
7007         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7008       if (top_level == enclosing_decl)
7009         return;
7010       access = "private";
7011     }
7012   else if (CLASS_PROTECTED (decl))
7013     {
7014       tree decl_context;
7015       /* Access is permitted from within the same package... */
7016       if (in_same_package (decl, enclosing_decl))
7017         return;
7018
7019       /* ... or from within the body of a subtype of the context in which
7020          DECL is declared. */
7021       decl_context = DECL_CONTEXT (decl);
7022       while (enclosing_decl)
7023         {
7024           if (CLASS_INTERFACE (decl))
7025             {
7026               if (interface_of_p (TREE_TYPE (decl_context),
7027                                   enclosing_decl_type))
7028                 return;
7029             }
7030           else
7031             {
7032               /* Eww. The order of the arguments is different!! */
7033               if (inherits_from_p (enclosing_decl_type,
7034                                    TREE_TYPE (decl_context)))
7035                 return;
7036             }
7037           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7038         }
7039       access = "protected";
7040     }
7041   else if (! CLASS_PUBLIC (decl))
7042     {
7043       /* Access is permitted only from within the same package as DECL. */
7044       if (in_same_package (decl, enclosing_decl))
7045         return;
7046       access = "non-public";
7047     }
7048   else
7049     /* Class is public. */
7050     return;
7051
7052   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7053                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7054                        lang_printable_name (decl, 0), access);
7055 }
7056
7057 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7058    foreign package, it must be PUBLIC. Return 0 if no access
7059    violations were found, 1 otherwise. If VERBOSE is true and an error
7060    was found, it is reported and accounted for.  */
7061
7062 static int
7063 check_pkg_class_access (tree class_name, tree cl, bool verbose)
7064 {
7065   tree type;
7066
7067   if (!IDENTIFIER_CLASS_VALUE (class_name))
7068     return 0;
7069
7070   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7071     return 0;
7072
7073   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7074     {
7075       /* Access to a private class within the same package is
7076          allowed. */
7077       tree l, r;
7078       breakdown_qualified (&l, &r, class_name);
7079       if (!QUALIFIED_P (class_name) && !ctxp->package)
7080         /* Both in the empty package. */
7081         return 0;
7082       if (l == ctxp->package)
7083         /* Both in the same package. */
7084         return 0;
7085
7086       if (verbose)
7087         parse_error_context
7088           (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7089            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7090            IDENTIFIER_POINTER (class_name));
7091       return 1;
7092     }
7093   return 0;
7094 }
7095
7096 /* Local variable declaration. */
7097
7098 static void
7099 declare_local_variables (int modifier, tree type, tree vlist)
7100 {
7101   tree decl, current, saved_type;
7102   tree type_wfl = NULL_TREE;
7103   int must_chain = 0;
7104   int final_p = 0;
7105
7106   /* Push a new block if statements were seen between the last time we
7107      pushed a block and now. Keep a count of blocks to close */
7108   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7109     {
7110       tree b = enter_block ();
7111       BLOCK_IS_IMPLICIT (b) = 1;
7112     }
7113
7114   if (modifier)
7115     {
7116       size_t i;
7117       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7118         if (1 << i & modifier)
7119           break;
7120       if (modifier == ACC_FINAL)
7121         final_p = 1;
7122       else
7123         {
7124           parse_error_context
7125             (ctxp->modifier_ctx [i],
7126              "Only `final' is allowed as a local variables modifier");
7127           return;
7128         }
7129     }
7130
7131   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7132      hold the TYPE value if a new incomplete has to be created (as
7133      opposed to being found already existing and reused). */
7134   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7135
7136   /* If TYPE is fully resolved and we don't have a reference, make one */
7137   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7138
7139   /* Go through all the declared variables */
7140   for (current = vlist, saved_type = type; current;
7141        current = TREE_CHAIN (current), type = saved_type)
7142     {
7143       tree other, real_type;
7144       tree wfl  = TREE_PURPOSE (current);
7145       tree name = EXPR_WFL_NODE (wfl);
7146       tree init = TREE_VALUE (current);
7147
7148       /* Process NAME, as it may specify extra dimension(s) for it */
7149       type = build_array_from_name (type, type_wfl, name, &name);
7150
7151       /* Variable redefinition check */
7152       if ((other = lookup_name_in_blocks (name)))
7153         {
7154           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7155                                        DECL_SOURCE_LINE (other));
7156           continue;
7157         }
7158
7159       /* Type adjustment. We may have just readjusted TYPE because
7160          the variable specified more dimensions. Make sure we have
7161          a reference if we can and don't have one already. */
7162       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7163
7164       real_type = GET_REAL_TYPE (type);
7165       /* Never layout this decl. This will be done when its scope
7166          will be entered */
7167       decl = build_decl (VAR_DECL, name, real_type);
7168       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7169       DECL_FINAL (decl) = final_p;
7170       BLOCK_CHAIN_DECL (decl);
7171
7172       /* If doing xreferencing, replace the line number with the WFL
7173          compound value */
7174       if (flag_emit_xref)
7175         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7176
7177       /* Don't try to use an INIT statement when an error was found */
7178       if (init && java_error_count)
7179         init = NULL_TREE;
7180
7181       /* Add the initialization function to the current function's code */
7182       if (init)
7183         {
7184           /* Name might have been readjusted */
7185           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7186           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7187           java_method_add_stmt (current_function_decl,
7188                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7189                                                       init));
7190         }
7191
7192       /* Setup dependency the type of the decl */
7193       if (must_chain)
7194         {
7195           jdep *dep;
7196           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7197           dep = CLASSD_LAST (ctxp->classd_list);
7198           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7199         }
7200     }
7201   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7202 }
7203
7204 /* Called during parsing. Build decls from argument list.  */
7205
7206 static void
7207 source_start_java_method (tree fndecl)
7208 {
7209   tree tem;
7210   tree parm_decl;
7211   int i;
7212
7213   if (!fndecl)
7214     return;
7215
7216   current_function_decl = fndecl;
7217
7218   /* New scope for the function */
7219   enter_block ();
7220   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7221        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7222     {
7223       tree type = TREE_VALUE (tem);
7224       tree name = TREE_PURPOSE (tem);
7225
7226       /* If type is incomplete. Create an incomplete decl and ask for
7227          the decl to be patched later */
7228       if (INCOMPLETE_TYPE_P (type))
7229         {
7230           jdep *jdep;
7231           tree real_type = GET_REAL_TYPE (type);
7232           parm_decl = build_decl (PARM_DECL, name, real_type);
7233           type = obtain_incomplete_type (type);
7234           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7235           jdep = CLASSD_LAST (ctxp->classd_list);
7236           JDEP_MISC (jdep) = name;
7237           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7238         }
7239       else
7240         parm_decl = build_decl (PARM_DECL, name, type);
7241
7242       /* Remember if a local variable was declared final (via its
7243          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7244       if (ARG_FINAL_P (tem))
7245         {
7246           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7247           DECL_FINAL (parm_decl) = 1;
7248         }
7249
7250       BLOCK_CHAIN_DECL (parm_decl);
7251     }
7252   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7253   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7254     nreverse (tem);
7255   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7256   DECL_MAX_LOCALS (current_function_decl) = i;
7257 }
7258
7259 /* Called during parsing. Creates an artificial method declaration.  */
7260
7261 static tree
7262 create_artificial_method (tree class, int flags, tree type,
7263                           tree name, tree args)
7264 {
7265   tree mdecl;
7266
7267   java_parser_context_save_global ();
7268   lineno = 0;
7269   mdecl = make_node (FUNCTION_TYPE);
7270   TREE_TYPE (mdecl) = type;
7271   TYPE_ARG_TYPES (mdecl) = args;
7272   mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7273   java_parser_context_restore_global ();
7274   DECL_ARTIFICIAL (mdecl) = 1;
7275   return mdecl;
7276 }
7277
7278 /* Starts the body if an artificial method.  */
7279
7280 static void
7281 start_artificial_method_body (tree mdecl)
7282 {
7283   DECL_SOURCE_LINE (mdecl) = 1;
7284   DECL_SOURCE_LINE_MERGE (mdecl, 1);
7285   source_start_java_method (mdecl);
7286   enter_block ();
7287 }
7288
7289 static void
7290 end_artificial_method_body (tree mdecl)
7291 {
7292   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7293      It has to be evaluated first. (if mdecl is current_function_decl,
7294      we have an undefined behavior if no temporary variable is used.) */
7295   tree b = exit_block ();
7296   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7297   exit_block ();
7298 }
7299
7300 /* Dump a tree of some kind.  This is a convenience wrapper for the
7301    dump_* functions in tree-dump.c.  */
7302 static void
7303 dump_java_tree (enum tree_dump_index phase, tree t)
7304 {
7305   FILE *stream;
7306   int flags;
7307
7308   stream = dump_begin (phase, &flags);
7309   flags |= TDF_SLIM;
7310   if (stream)
7311     {
7312       dump_node (t, flags, stream);
7313       dump_end (phase, stream);
7314     }
7315 }
7316
7317 /* Terminate a function and expand its body.  */
7318
7319 static void
7320 source_end_java_method (void)
7321 {
7322   tree fndecl = current_function_decl;
7323
7324   if (!fndecl)
7325     return;
7326
7327   java_parser_context_save_global ();
7328   lineno = ctxp->last_ccb_indent1;
7329
7330   /* Turn function bodies with only a NOP expr null, so they don't get
7331      generated at all and we won't get warnings when using the -W
7332      -Wall flags. */
7333   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7334     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7335
7336   /* We've generated all the trees for this function, and it has been
7337      patched.  Dump it to a file if the user requested it.  */
7338   dump_java_tree (TDI_original, fndecl);
7339
7340   java_optimize_inline (fndecl);
7341
7342   /* Generate function's code */
7343   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7344       && ! flag_emit_class_files
7345       && ! flag_emit_xref)
7346     expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7347
7348   /* pop out of its parameters */
7349   pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7350   poplevel (1, 0, 1);
7351   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7352
7353   /* Generate rtl for function exit.  */
7354   if (! flag_emit_class_files && ! flag_emit_xref)
7355     {
7356       lineno = DECL_SOURCE_LINE_LAST (fndecl);
7357       expand_function_end (input_filename, lineno, 0);
7358
7359       DECL_SOURCE_LINE (fndecl) = DECL_SOURCE_LINE_FIRST (fndecl);
7360
7361       /* Run the optimizers and output assembler code for this function. */
7362       rest_of_compilation (fndecl);
7363     }
7364
7365   current_function_decl = NULL_TREE;
7366   java_parser_context_restore_global ();
7367 }
7368
7369 /* Record EXPR in the current function block. Complements compound
7370    expression second operand if necessary.  */
7371
7372 tree
7373 java_method_add_stmt (tree fndecl, tree expr)
7374 {
7375   if (!GET_CURRENT_BLOCK (fndecl))
7376     return NULL_TREE;
7377   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7378 }
7379
7380 static tree
7381 add_stmt_to_block (tree b, tree type, tree stmt)
7382 {
7383   tree body = BLOCK_EXPR_BODY (b), c;
7384
7385   if (java_error_count)
7386     return body;
7387
7388   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7389     return body;
7390
7391   BLOCK_EXPR_BODY (b) = c;
7392   TREE_SIDE_EFFECTS (c) = 1;
7393   return c;
7394 }
7395
7396 /* Add STMT to EXISTING if possible, otherwise create a new
7397    COMPOUND_EXPR and add STMT to it. */
7398
7399 static tree
7400 add_stmt_to_compound (tree existing, tree type, tree stmt)
7401 {
7402   /* Keep track of this for inlining.  */
7403   if (current_function_decl)
7404     ++DECL_NUM_STMTS (current_function_decl);
7405
7406   if (existing)
7407     return build (COMPOUND_EXPR, type, existing, stmt);
7408   else
7409     return stmt;
7410 }
7411
7412 void java_layout_seen_class_methods (void)
7413 {
7414   tree previous_list = all_class_list;
7415   tree end = NULL_TREE;
7416   tree current;
7417
7418   while (1)
7419     {
7420       for (current = previous_list;
7421            current != end; current = TREE_CHAIN (current))
7422         layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7423
7424       if (previous_list != all_class_list)
7425         {
7426           end = previous_list;
7427           previous_list = all_class_list;
7428         }
7429       else
7430         break;
7431     }
7432 }
7433
7434 static GTY(()) tree stop_reordering;
7435 void
7436 java_reorder_fields (void)
7437 {
7438   tree current;
7439
7440   for (current = gclass_list; current; current = TREE_CHAIN (current))
7441     {
7442       current_class = TREE_TYPE (TREE_VALUE (current));
7443
7444       if (current_class == stop_reordering)
7445         break;
7446
7447       /* Reverse the fields, but leave the dummy field in front.
7448          Fields are already ordered for Object and Class */
7449       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7450           && current_class != class_type_node)
7451       {
7452         /* If the dummy field is there, reverse the right fields and
7453            just layout the type for proper fields offset */
7454         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7455           {
7456             tree fields = TYPE_FIELDS (current_class);
7457             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7458             TYPE_SIZE (current_class) = NULL_TREE;
7459           }
7460         /* We don't have a dummy field, we need to layout the class,
7461            after having reversed the fields */
7462         else
7463           {
7464             TYPE_FIELDS (current_class) =
7465               nreverse (TYPE_FIELDS (current_class));
7466             TYPE_SIZE (current_class) = NULL_TREE;
7467           }
7468       }
7469     }
7470   /* There are cases were gclass_list will be empty. */
7471   if (gclass_list)
7472     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7473 }
7474
7475 /* Layout the methods of all classes loaded in one way or another.
7476    Check methods of source parsed classes. Then reorder the
7477    fields and layout the classes or the type of all source parsed
7478    classes */
7479
7480 void
7481 java_layout_classes (void)
7482 {
7483   tree current;
7484   int save_error_count = java_error_count;
7485
7486   /* Layout the methods of all classes seen so far */
7487   java_layout_seen_class_methods ();
7488   java_parse_abort_on_error ();
7489   all_class_list = NULL_TREE;
7490
7491   /* Then check the methods of all parsed classes */
7492   for (current = gclass_list; current; current = TREE_CHAIN (current))
7493     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7494       java_check_methods (TREE_VALUE (current));
7495   java_parse_abort_on_error ();
7496
7497   for (current = gclass_list; current; current = TREE_CHAIN (current))
7498     {
7499       current_class = TREE_TYPE (TREE_VALUE (current));
7500       layout_class (current_class);
7501
7502       /* Error reported by the caller */
7503       if (java_error_count)
7504         return;
7505     }
7506
7507   /* We might have reloaded classes durign the process of laying out
7508      classes for code generation. We must layout the methods of those
7509      late additions, as constructor checks might use them */
7510   java_layout_seen_class_methods ();
7511   java_parse_abort_on_error ();
7512 }
7513
7514 /* Expand methods in the current set of classes rememebered for
7515    generation.  */
7516
7517 static void
7518 java_complete_expand_classes (void)
7519 {
7520   tree current;
7521
7522   do_not_fold = flag_emit_xref;
7523
7524   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7525     if (!INNER_CLASS_DECL_P (current))
7526       java_complete_expand_class (current);
7527 }
7528
7529 /* Expand the methods found in OUTER, starting first by OUTER's inner
7530    classes, if any.  */
7531
7532 static void
7533 java_complete_expand_class (tree outer)
7534 {
7535   tree inner_list;
7536
7537   set_nested_class_simple_name_value (outer, 1); /* Set */
7538
7539   /* We need to go after all inner classes and start expanding them,
7540      starting with most nested ones. We have to do that because nested
7541      classes might add functions to outer classes */
7542
7543   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7544        inner_list; inner_list = TREE_CHAIN (inner_list))
7545     java_complete_expand_class (TREE_PURPOSE (inner_list));
7546
7547   java_complete_expand_methods (outer);
7548   set_nested_class_simple_name_value (outer, 0); /* Reset */
7549 }
7550
7551 /* Expand methods registered in CLASS_DECL. The general idea is that
7552    we expand regular methods first. This allows us get an estimate on
7553    how outer context local alias fields are really used so we can add
7554    to the constructor just enough code to initialize them properly (it
7555    also lets us generate finit$ correctly.) Then we expand the
7556    constructors and then <clinit>.  */
7557
7558 static void
7559 java_complete_expand_methods (tree class_decl)
7560 {
7561   tree clinit, decl, first_decl;
7562
7563   current_class = TREE_TYPE (class_decl);
7564
7565   /* Initialize a new constant pool */
7566   init_outgoing_cpool ();
7567
7568   /* Pre-expand <clinit> to figure whether we really need it or
7569      not. If we do need it, we pre-expand the static fields so they're
7570      ready to be used somewhere else. <clinit> will be fully expanded
7571      after we processed the constructors. */
7572   first_decl = TYPE_METHODS (current_class);
7573   clinit = maybe_generate_pre_expand_clinit (current_class);
7574
7575   /* Then generate finit$ (if we need to) because constructors will
7576    try to use it.*/
7577   if (TYPE_FINIT_STMT_LIST (current_class))
7578     java_complete_expand_method (generate_finit (current_class));
7579
7580   /* Then generate instinit$ (if we need to) because constructors will
7581      try to use it. */
7582   if (TYPE_II_STMT_LIST (current_class))
7583     java_complete_expand_method (generate_instinit (current_class));
7584
7585   /* Now do the constructors */
7586   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7587     {
7588       int no_body;
7589
7590       if (!DECL_CONSTRUCTOR_P (decl))
7591         continue;
7592
7593       no_body = !DECL_FUNCTION_BODY (decl);
7594       /* Don't generate debug info on line zero when expanding a
7595          generated constructor. */
7596       if (no_body)
7597         restore_line_number_status (1);
7598
7599       java_complete_expand_method (decl);
7600
7601       if (no_body)
7602         restore_line_number_status (0);
7603     }
7604
7605   /* First, do the ordinary methods. */
7606   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7607     {
7608       /* Ctors aren't part of this batch. */
7609       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7610         continue;
7611
7612       /* Skip abstract or native methods -- but do handle native
7613          methods when generating JNI stubs.  */
7614       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7615         {
7616           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7617           continue;
7618         }
7619
7620       if (METHOD_NATIVE (decl))
7621         {
7622           tree body;
7623           current_function_decl = decl;
7624           body = build_jni_stub (decl);
7625           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7626         }
7627
7628       java_complete_expand_method (decl);
7629     }
7630
7631   /* If there is indeed a <clinit>, fully expand it now */
7632   if (clinit)
7633     {
7634       /* Prevent the use of `this' inside <clinit> */
7635       ctxp->explicit_constructor_p = 1;
7636       java_complete_expand_method (clinit);
7637       ctxp->explicit_constructor_p = 0;
7638     }
7639
7640   /* We might have generated a class$ that we now want to expand */
7641   if (TYPE_DOT_CLASS (current_class))
7642     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7643
7644   /* Now verify constructor circularity (stop after the first one we
7645      prove wrong.) */
7646   if (!CLASS_INTERFACE (class_decl))
7647     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7648       if (DECL_CONSTRUCTOR_P (decl)
7649           && verify_constructor_circularity (decl, decl))
7650         break;
7651
7652   /* Save the constant pool. We'll need to restore it later. */
7653   TYPE_CPOOL (current_class) = outgoing_cpool;
7654 }
7655
7656 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7657    safely used in some other methods/constructors.  */
7658
7659 static tree
7660 maybe_generate_pre_expand_clinit (tree class_type)
7661 {
7662   tree current, mdecl;
7663
7664   if (!TYPE_CLINIT_STMT_LIST (class_type))
7665     return NULL_TREE;
7666
7667   /* Go through all static fields and pre expand them */
7668   for (current = TYPE_FIELDS (class_type); current;
7669        current = TREE_CHAIN (current))
7670     if (FIELD_STATIC (current))
7671       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7672
7673   /* Then build the <clinit> method */
7674   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7675                                     clinit_identifier_node, end_params_node);
7676   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7677                        mdecl, NULL_TREE);
7678   start_artificial_method_body (mdecl);
7679
7680   /* We process the list of assignment we produced as the result of
7681      the declaration of initialized static field and add them as
7682      statement to the <clinit> method. */
7683   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7684        current = TREE_CHAIN (current))
7685     {
7686       tree stmt = current;
7687       /* We build the assignment expression that will initialize the
7688          field to its value. There are strict rules on static
7689          initializers (8.5). FIXME */
7690       if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7691         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7692       java_method_add_stmt (mdecl, stmt);
7693     }
7694
7695   end_artificial_method_body (mdecl);
7696
7697   /* Now we want to place <clinit> as the last method (because we need
7698      it at least for interface so that it doesn't interfere with the
7699      dispatch table based lookup. */
7700   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7701     {
7702       current = TREE_CHAIN (TYPE_METHODS (class_type));
7703       TYPE_METHODS (class_type) = current;
7704
7705       while (TREE_CHAIN (current))
7706         current = TREE_CHAIN (current);
7707
7708       TREE_CHAIN (current) = mdecl;
7709       TREE_CHAIN (mdecl) = NULL_TREE;
7710     }
7711
7712   return mdecl;
7713 }
7714
7715 /* Analyzes a method body and look for something that isn't a
7716    MODIFY_EXPR with a constant value.  */
7717
7718 static int
7719 analyze_clinit_body (tree this_class, tree bbody)
7720 {
7721   while (bbody)
7722     switch (TREE_CODE (bbody))
7723       {
7724       case BLOCK:
7725         bbody = BLOCK_EXPR_BODY (bbody);
7726         break;
7727
7728       case EXPR_WITH_FILE_LOCATION:
7729         bbody = EXPR_WFL_NODE (bbody);
7730         break;
7731
7732       case COMPOUND_EXPR:
7733         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7734           return 1;
7735         bbody = TREE_OPERAND (bbody, 1);
7736         break;
7737
7738       case MODIFY_EXPR:
7739         /* If we're generating to class file and we're dealing with an
7740            array initialization, we return 1 to keep <clinit> */
7741         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7742             && flag_emit_class_files)
7743           return 1;
7744
7745         /* There are a few cases where we're required to keep
7746            <clinit>:
7747            - If this is an assignment whose operand is not constant,
7748            - If this is an assignment to a non-initialized field,
7749            - If this field is not a member of the current class.
7750         */
7751         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7752                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7753                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7754
7755       default:
7756         return 1;
7757       }
7758   return 0;
7759 }
7760
7761
7762 /* See whether we could get rid of <clinit>. Criteria are: all static
7763    final fields have constant initial values and the body of <clinit>
7764    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7765
7766 static int
7767 maybe_yank_clinit (tree mdecl)
7768 {
7769   tree type, current;
7770   tree fbody, bbody;
7771
7772   if (!DECL_CLINIT_P (mdecl))
7773     return 0;
7774
7775   /* If the body isn't empty, then we keep <clinit>. Note that if
7776      we're emitting classfiles, this isn't enough not to rule it
7777      out. */
7778   fbody = DECL_FUNCTION_BODY (mdecl);
7779   bbody = BLOCK_EXPR_BODY (fbody);
7780   if (bbody && bbody != error_mark_node)
7781     bbody = BLOCK_EXPR_BODY (bbody);
7782   else
7783     return 0;
7784   if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7785     return 0;
7786
7787   type = DECL_CONTEXT (mdecl);
7788   current = TYPE_FIELDS (type);
7789
7790   for (current = (current ? TREE_CHAIN (current) : current);
7791        current; current = TREE_CHAIN (current))
7792     {
7793       tree f_init;
7794
7795       /* We're not interested in non-static fields.  */
7796       if (!FIELD_STATIC (current))
7797         continue;
7798
7799       /* Nor in fields without initializers. */
7800       f_init = DECL_INITIAL (current);
7801       if (f_init == NULL_TREE)
7802         continue;
7803
7804       /* Anything that isn't String or a basic type is ruled out -- or
7805          if we know how to deal with it (when doing things natively) we
7806          should generated an empty <clinit> so that SUID are computed
7807          correctly. */
7808       if (! JSTRING_TYPE_P (TREE_TYPE (current))
7809           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7810         return 0;
7811
7812       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7813         return 0;
7814     }
7815
7816   /* Now we analyze the method body and look for something that
7817      isn't a MODIFY_EXPR */
7818   if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7819     return 0;
7820
7821   /* Get rid of <clinit> in the class' list of methods */
7822   if (TYPE_METHODS (type) == mdecl)
7823     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7824   else
7825     for (current = TYPE_METHODS (type); current;
7826          current = TREE_CHAIN (current))
7827       if (TREE_CHAIN (current) == mdecl)
7828         {
7829           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7830           break;
7831         }
7832
7833   return 1;
7834 }
7835
7836 /* Install the argument from MDECL. Suitable to completion and
7837    expansion of mdecl's body.  */
7838
7839 static void
7840 start_complete_expand_method (tree mdecl)
7841 {
7842   tree tem;
7843
7844   pushlevel (1);                /* Prepare for a parameter push */
7845   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7846   DECL_ARGUMENTS (mdecl) = tem;
7847
7848   for (; tem; tem = TREE_CHAIN (tem))
7849     {
7850       /* TREE_CHAIN (tem) will change after pushdecl. */
7851       tree next = TREE_CHAIN (tem);
7852       tree type = TREE_TYPE (tem);
7853       if (PROMOTE_PROTOTYPES
7854           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7855           && INTEGRAL_TYPE_P (type))
7856         type = integer_type_node;
7857       DECL_ARG_TYPE (tem) = type;
7858       layout_decl (tem, 0);
7859       pushdecl (tem);
7860       /* Re-install the next so that the list is kept and the loop
7861          advances. */
7862       TREE_CHAIN (tem) = next;
7863     }
7864   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7865   lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7866   build_result_decl (mdecl);
7867 }
7868
7869
7870 /* Complete and expand a method.  */
7871
7872 static void
7873 java_complete_expand_method (tree mdecl)
7874 {
7875   tree fbody, block_body, exception_copy;
7876
7877   current_function_decl = mdecl;
7878   /* Fix constructors before expanding them */
7879   if (DECL_CONSTRUCTOR_P (mdecl))
7880     fix_constructors (mdecl);
7881
7882   /* Expand functions that have a body */
7883   if (!DECL_FUNCTION_BODY (mdecl))
7884     return;
7885
7886   fbody = DECL_FUNCTION_BODY (mdecl);
7887   block_body = BLOCK_EXPR_BODY (fbody);
7888   exception_copy = NULL_TREE;
7889
7890   current_function_decl = mdecl;
7891
7892   if (! quiet_flag)
7893     fprintf (stderr, " [%s.",
7894              lang_printable_name (DECL_CONTEXT (mdecl), 0));
7895   announce_function (mdecl);
7896   if (! quiet_flag)
7897     fprintf (stderr, "]");
7898
7899   /* Prepare the function for tree completion */
7900   start_complete_expand_method (mdecl);
7901
7902   /* Install the current this */
7903   current_this = (!METHOD_STATIC (mdecl) ?
7904                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7905
7906   /* Purge the `throws' list of unchecked exceptions (we save a copy
7907      of the list and re-install it later.) */
7908   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7909   purge_unchecked_exceptions (mdecl);
7910
7911   /* Install exceptions thrown with `throws' */
7912   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7913
7914   if (block_body != NULL_TREE)
7915     {
7916       block_body = java_complete_tree (block_body);
7917
7918       /* Before we check initialization, attached all class initialization
7919          variable to the block_body */
7920       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
7921                      attach_init_test_initialization_flags, block_body);
7922
7923       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7924         {
7925           check_for_initialization (block_body, mdecl);
7926
7927           /* Go through all the flags marking the initialization of
7928              static variables and see whether they're definitively
7929              assigned, in which case the type is remembered as
7930              definitively initialized in MDECL. */
7931           if (STATIC_CLASS_INIT_OPT_P ())
7932             {
7933               /* Always register the context as properly initialized in
7934                  MDECL. This used with caution helps removing extra
7935                  initialization of self. */
7936               if (METHOD_STATIC (mdecl))
7937                 {
7938                   *(htab_find_slot
7939                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
7940                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
7941                 }
7942             }
7943         }
7944       ctxp->explicit_constructor_p = 0;
7945     }
7946
7947   BLOCK_EXPR_BODY (fbody) = block_body;
7948
7949   /* If we saw a return but couldn't evaluate it properly, we'll have
7950      an error_mark_node here. */
7951   if (block_body != error_mark_node
7952       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7953       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7954       && !flag_emit_xref)
7955     missing_return_error (current_function_decl);
7956
7957   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
7958   maybe_yank_clinit (mdecl);
7959
7960   /* Pop the current level, with special measures if we found errors. */
7961   if (java_error_count)
7962     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7963   poplevel (1, 0, 1);
7964
7965   /* Pop the exceptions and sanity check */
7966   POP_EXCEPTIONS();
7967   if (currently_caught_type_list)
7968     abort ();
7969
7970   /* Restore the copy of the list of exceptions if emitting xrefs. */
7971   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7972 }
7973
7974 /* For with each class for which there's code to generate. */
7975
7976 static void
7977 java_expand_method_bodies (tree class)
7978 {
7979   tree decl;
7980   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
7981     {
7982       if (!DECL_FUNCTION_BODY (decl))
7983         continue;
7984
7985       current_function_decl = decl;
7986
7987       /* Save the function for inlining.  */
7988       if (flag_inline_trees)
7989         DECL_SAVED_TREE (decl) =
7990           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
7991
7992       /* It's time to assign the variable flagging static class
7993          initialization based on which classes invoked static methods
7994          are definitely initializing. This should be flagged. */
7995       if (STATIC_CLASS_INIT_OPT_P ())
7996         {
7997           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
7998           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
7999             {
8000               /* Executed for each statement calling a static function.
8001                  LIST is a TREE_LIST whose PURPOSE is the called function
8002                  and VALUE is a compound whose second operand can be patched
8003                  with static class initialization flag assignments.  */
8004
8005               tree called_method = TREE_PURPOSE (list);
8006               tree compound = TREE_VALUE (list);
8007               tree assignment_compound_list
8008                 = build_tree_list (called_method, NULL);
8009
8010               /* For each class definitely initialized in
8011                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8012                  assignment to the class initialization flag. */
8013               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8014                              emit_test_initialization,
8015                              assignment_compound_list);
8016
8017               if (TREE_VALUE (assignment_compound_list))
8018                 TREE_OPERAND (compound, 1)
8019                   = TREE_VALUE (assignment_compound_list);
8020             }
8021         }
8022
8023       /* Prepare the function for RTL expansion */
8024       start_complete_expand_method (decl);
8025
8026       /* Expand function start, generate initialization flag
8027          assignment, and handle synchronized methods. */
8028       complete_start_java_method (decl);
8029
8030       /* Expand the rest of the function body and terminate
8031          expansion. */
8032       source_end_java_method ();
8033     }
8034 }
8035
8036 \f
8037
8038 /* This section of the code deals with accessing enclosing context
8039    fields either directly by using the relevant access to this$<n> or
8040    by invoking an access method crafted for that purpose.  */
8041
8042 /* Build the necessary access from an inner class to an outer
8043    class. This routine could be optimized to cache previous result
8044    (decl, current_class and returned access).  When an access method
8045    needs to be generated, it always takes the form of a read. It might
8046    be later turned into a write by calling outer_field_access_fix.  */
8047
8048 static tree
8049 build_outer_field_access (tree id, tree decl)
8050 {
8051   tree access = NULL_TREE;
8052   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8053   tree decl_ctx = DECL_CONTEXT (decl);
8054
8055   /* If the immediate enclosing context of the current class is the
8056      field decl's class or inherits from it; build the access as
8057      `this$<n>.<field>'. Note that we will break the `private' barrier
8058      if we're not emitting bytecodes. */
8059   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8060       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8061     {
8062       tree thisn = build_current_thisn (current_class);
8063       access = make_qualified_primary (build_wfl_node (thisn),
8064                                        id, EXPR_WFL_LINECOL (id));
8065     }
8066   /* Otherwise, generate access methods to outer this and access the
8067      field (either using an access method or by direct access.) */
8068   else
8069     {
8070       int lc = EXPR_WFL_LINECOL (id);
8071
8072       /* Now we chain the required number of calls to the access$0 to
8073          get a hold to the enclosing instance we need, and then we
8074          build the field access. */
8075       access = build_access_to_thisn (current_class, decl_ctx, lc);
8076
8077       /* If the field is private and we're generating bytecode, then
8078          we generate an access method */
8079       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8080         {
8081           tree name = build_outer_field_access_methods (decl);
8082           access = build_outer_field_access_expr (lc, decl_ctx,
8083                                                   name, access, NULL_TREE);
8084         }
8085       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8086          Once again we break the `private' access rule from a foreign
8087          class. */
8088       else
8089         access = make_qualified_primary (access, id, lc);
8090     }
8091   return resolve_expression_name (access, NULL);
8092 }
8093
8094 /* Return a nonzero value if NODE describes an outer field inner
8095    access.  */
8096
8097 static int
8098 outer_field_access_p (tree type, tree decl)
8099 {
8100   if (!INNER_CLASS_TYPE_P (type)
8101       || TREE_CODE (decl) != FIELD_DECL
8102       || DECL_CONTEXT (decl) == type)
8103     return 0;
8104
8105   /* If the inner class extends the declaration context of the field
8106      we're try to acces, then this isn't an outer field access */
8107   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8108     return 0;
8109
8110   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8111        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8112     {
8113       if (type == DECL_CONTEXT (decl))
8114         return 1;
8115
8116       if (!DECL_CONTEXT (TYPE_NAME (type)))
8117         {
8118           /* Before we give up, see whether the field is inherited from
8119              the enclosing context we're considering. */
8120           if (inherits_from_p (type, DECL_CONTEXT (decl)))
8121             return 1;
8122           break;
8123         }
8124     }
8125
8126   return 0;
8127 }
8128
8129 /* Return a nonzero value if NODE represents an outer field inner
8130    access that was been already expanded. As a side effect, it returns
8131    the name of the field being accessed and the argument passed to the
8132    access function, suitable for a regeneration of the access method
8133    call if necessary. */
8134
8135 static int
8136 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8137                                tree *arg)
8138 {
8139   int identified = 0;
8140
8141   if (TREE_CODE (node) != CALL_EXPR)
8142     return 0;
8143
8144   /* Well, gcj generates slightly different tree nodes when compiling
8145      to native or bytecodes. It's the case for function calls. */
8146
8147   if (flag_emit_class_files
8148       && TREE_CODE (node) == CALL_EXPR
8149       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8150     identified = 1;
8151   else if (!flag_emit_class_files)
8152     {
8153       node = TREE_OPERAND (node, 0);
8154
8155       if (node && TREE_OPERAND (node, 0)
8156           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8157         {
8158           node = TREE_OPERAND (node, 0);
8159           if (TREE_OPERAND (node, 0)
8160               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8161               && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8162                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8163             identified = 1;
8164         }
8165     }
8166
8167   if (identified && name && arg_type && arg)
8168     {
8169       tree argument = TREE_OPERAND (node, 1);
8170       *name = DECL_NAME (TREE_OPERAND (node, 0));
8171       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8172       *arg = TREE_VALUE (argument);
8173     }
8174   return identified;
8175 }
8176
8177 /* Detect in NODE an outer field read access from an inner class and
8178    transform it into a write with RHS as an argument. This function is
8179    called from the java_complete_lhs when an assignment to a LHS can
8180    be identified. */
8181
8182 static tree
8183 outer_field_access_fix (tree wfl, tree node, tree rhs)
8184 {
8185   tree name, arg_type, arg;
8186
8187   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8188     {
8189       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8190                                             arg_type, name, arg, rhs);
8191       return java_complete_tree (node);
8192     }
8193   return NULL_TREE;
8194 }
8195
8196 /* Construct the expression that calls an access method:
8197      <type>.access$<n>(<arg1> [, <arg2>]);
8198
8199    ARG2 can be NULL and will be omitted in that case. It will denote a
8200    read access.  */
8201
8202 static tree
8203 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8204                                tree arg1, tree arg2)
8205 {
8206   tree args, cn, access;
8207
8208   args = arg1 ? arg1 :
8209     build_wfl_node (build_current_thisn (current_class));
8210   args = build_tree_list (NULL_TREE, args);
8211
8212   if (arg2)
8213     args = tree_cons (NULL_TREE, arg2, args);
8214
8215   access = build_method_invocation (build_wfl_node (access_method_name), args);
8216   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8217   return make_qualified_primary (cn, access, lc);
8218 }
8219
8220 static tree
8221 build_new_access_id (void)
8222 {
8223   static int access_n_counter = 1;
8224   char buffer [128];
8225
8226   sprintf (buffer, "access$%d", access_n_counter++);
8227   return get_identifier (buffer);
8228 }
8229
8230 /* Create the static access functions for the outer field DECL. We define a
8231    read:
8232      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8233        return inst$.field;
8234      }
8235    and a write access:
8236      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8237                                      TREE_TYPE (<field>) value$) {
8238        return inst$.field = value$;
8239      }
8240    We should have a usage flags on the DECL so we can lazily turn the ones
8241    we're using for code generation. FIXME.
8242 */
8243
8244 static tree
8245 build_outer_field_access_methods (tree decl)
8246 {
8247   tree id, args, stmt, mdecl;
8248
8249   if (FIELD_INNER_ACCESS_P (decl))
8250     return FIELD_INNER_ACCESS (decl);
8251
8252   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8253
8254   /* Create the identifier and a function named after it. */
8255   id = build_new_access_id ();
8256
8257   /* The identifier is marked as bearing the name of a generated write
8258      access function for outer field accessed from inner classes. */
8259   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8260
8261   /* Create the read access */
8262   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8263   TREE_CHAIN (args) = end_params_node;
8264   stmt = make_qualified_primary (build_wfl_node (inst_id),
8265                                  build_wfl_node (DECL_NAME (decl)), 0);
8266   stmt = build_return (0, stmt);
8267   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8268                                            TREE_TYPE (decl), id, args, stmt);
8269   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8270
8271   /* Create the write access method. No write access for final variable */
8272   if (!FIELD_FINAL (decl))
8273     {
8274       args = build_tree_list (inst_id,
8275                               build_pointer_type (DECL_CONTEXT (decl)));
8276       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8277       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8278       stmt = make_qualified_primary (build_wfl_node (inst_id),
8279                                      build_wfl_node (DECL_NAME (decl)), 0);
8280       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8281                                                 build_wfl_node (wpv_id)));
8282       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8283                                                TREE_TYPE (decl), id,
8284                                                args, stmt);
8285     }
8286   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8287
8288   /* Return the access name */
8289   return FIELD_INNER_ACCESS (decl) = id;
8290 }
8291
8292 /* Build an field access method NAME.  */
8293
8294 static tree
8295 build_outer_field_access_method (tree class, tree type, tree name,
8296                                  tree args, tree body)
8297 {
8298   tree saved_current_function_decl, mdecl;
8299
8300   /* Create the method */
8301   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8302   fix_method_argument_names (args, mdecl);
8303   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8304
8305   /* Attach the method body. */
8306   saved_current_function_decl = current_function_decl;
8307   start_artificial_method_body (mdecl);
8308   java_method_add_stmt (mdecl, body);
8309   end_artificial_method_body (mdecl);
8310   current_function_decl = saved_current_function_decl;
8311
8312   return mdecl;
8313 }
8314
8315 \f
8316 /* This section deals with building access function necessary for
8317    certain kinds of method invocation from inner classes.  */
8318
8319 static tree
8320 build_outer_method_access_method (tree decl)
8321 {
8322   tree saved_current_function_decl, mdecl;
8323   tree args = NULL_TREE, call_args = NULL_TREE;
8324   tree carg, id, body, class;
8325   char buffer [80];
8326   int parm_id_count = 0;
8327
8328   /* Test this abort with an access to a private field */
8329   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8330     abort ();
8331
8332   /* Check the cache first */
8333   if (DECL_FUNCTION_INNER_ACCESS (decl))
8334     return DECL_FUNCTION_INNER_ACCESS (decl);
8335
8336   class = DECL_CONTEXT (decl);
8337
8338   /* Obtain an access identifier and mark it */
8339   id = build_new_access_id ();
8340   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8341
8342   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8343   /* Create the arguments, as much as the original */
8344   for (; carg && carg != end_params_node;
8345        carg = TREE_CHAIN (carg))
8346     {
8347       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8348       args = chainon (args, build_tree_list (get_identifier (buffer),
8349                                              TREE_VALUE (carg)));
8350     }
8351   args = chainon (args, end_params_node);
8352
8353   /* Create the method */
8354   mdecl = create_artificial_method (class, ACC_STATIC,
8355                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8356   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8357   /* There is a potential bug here. We should be able to use
8358      fix_method_argument_names, but then arg names get mixed up and
8359      eventually a constructor will have its this$0 altered and the
8360      outer context won't be assignment properly. The test case is
8361      stub.java FIXME */
8362   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8363
8364   /* Attach the method body. */
8365   saved_current_function_decl = current_function_decl;
8366   start_artificial_method_body (mdecl);
8367
8368   /* The actual method invocation uses the same args. When invoking a
8369      static methods that way, we don't want to skip the first
8370      argument. */
8371   carg = args;
8372   if (!METHOD_STATIC (decl))
8373     carg = TREE_CHAIN (carg);
8374   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8375     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8376                            call_args);
8377
8378   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8379                                   call_args);
8380   if (!METHOD_STATIC (decl))
8381     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8382                                    body, 0);
8383   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8384     body = build_return (0, body);
8385   java_method_add_stmt (mdecl,body);
8386   end_artificial_method_body (mdecl);
8387   current_function_decl = saved_current_function_decl;
8388
8389   /* Back tag the access function so it know what it accesses */
8390   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8391
8392   /* Tag the current method so it knows it has an access generated */
8393   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8394 }
8395
8396 \f
8397 /* This section of the code deals with building expressions to access
8398    the enclosing instance of an inner class. The enclosing instance is
8399    kept in a generated field called this$<n>, with <n> being the
8400    inner class nesting level (starting from 0.)  */
8401
8402 /* Build an access to a given this$<n>, always chaining access call to
8403    others. Access methods to this$<n> are build on the fly if
8404    necessary. This CAN'T be used to solely access this$<n-1> from
8405    this$<n> (which alway yield to special cases and optimization, see
8406    for example build_outer_field_access).  */
8407
8408 static tree
8409 build_access_to_thisn (tree from, tree to, int lc)
8410 {
8411   tree access = NULL_TREE;
8412
8413   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8414     {
8415       if (!access)
8416         {
8417           access = build_current_thisn (from);
8418           access = build_wfl_node (access);
8419         }
8420       else
8421         {
8422           tree access0_wfl, cn;
8423
8424           maybe_build_thisn_access_method (from);
8425           access0_wfl = build_wfl_node (access0_identifier_node);
8426           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8427           EXPR_WFL_LINECOL (access0_wfl) = lc;
8428           access = build_tree_list (NULL_TREE, access);
8429           access = build_method_invocation (access0_wfl, access);
8430           access = make_qualified_primary (cn, access, lc);
8431         }
8432
8433       /* If FROM isn't an inner class, that's fine, we've done enough.
8434          What we're looking for can be accessed from there.  */
8435       from = DECL_CONTEXT (TYPE_NAME (from));
8436       if (!from)
8437         break;
8438       from = TREE_TYPE (from);
8439     }
8440   return access;
8441 }
8442
8443 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8444    is returned if nothing needs to be generated. Otherwise, the method
8445    generated and a method decl is returned.
8446
8447    NOTE: These generated methods should be declared in a class file
8448    attribute so that they can't be referred to directly.  */
8449
8450 static tree
8451 maybe_build_thisn_access_method (tree type)
8452 {
8453   tree mdecl, args, stmt, rtype;
8454   tree saved_current_function_decl;
8455
8456   /* If TYPE is a top-level class, no access method is required.
8457      If there already is such an access method, bail out. */
8458   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8459     return NULL_TREE;
8460
8461   /* We generate the method. The method looks like:
8462      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8463   */
8464   args = build_tree_list (inst_id, build_pointer_type (type));
8465   TREE_CHAIN (args) = end_params_node;
8466   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8467   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8468                                     access0_identifier_node, args);
8469   fix_method_argument_names (args, mdecl);
8470   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8471   stmt = build_current_thisn (type);
8472   stmt = make_qualified_primary (build_wfl_node (inst_id),
8473                                  build_wfl_node (stmt), 0);
8474   stmt = build_return (0, stmt);
8475
8476   saved_current_function_decl = current_function_decl;
8477   start_artificial_method_body (mdecl);
8478   java_method_add_stmt (mdecl, stmt);
8479   end_artificial_method_body (mdecl);
8480   current_function_decl = saved_current_function_decl;
8481
8482   CLASS_ACCESS0_GENERATED_P (type) = 1;
8483
8484   return mdecl;
8485 }
8486
8487 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8488    the first level of innerclassing. this$1 for the next one, etc...
8489    This function can be invoked with TYPE to NULL, available and then
8490    has to count the parser context.  */
8491
8492 static GTY(()) tree saved_thisn;
8493 static GTY(()) tree saved_type;
8494
8495 static tree
8496 build_current_thisn (tree type)
8497 {
8498   static int saved_i = -1;
8499   static int saved_type_i = 0;
8500   tree decl;
8501   char buffer [24];
8502   int i = 0;
8503
8504   if (type)
8505     {
8506       if (type == saved_type)
8507         i = saved_type_i;
8508       else
8509         {
8510           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8511                decl; decl = DECL_CONTEXT (decl), i++)
8512             ;
8513
8514           saved_type = type;
8515           saved_type_i = i;
8516         }
8517     }
8518   else
8519     i = list_length (GET_CPC_LIST ())-2;
8520
8521   if (i == saved_i)
8522     return saved_thisn;
8523
8524   sprintf (buffer, "this$%d", i);
8525   saved_i = i;
8526   saved_thisn = get_identifier (buffer);
8527   return saved_thisn;
8528 }
8529
8530 /* Return the assignement to the hidden enclosing context `this$<n>'
8531    by the second incoming parameter to the innerclass constructor. The
8532    form used is `this.this$<n> = this$<n>;'.  */
8533
8534 static tree
8535 build_thisn_assign (void)
8536 {
8537   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8538     {
8539       tree thisn = build_current_thisn (current_class);
8540       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8541                                          build_wfl_node (thisn), 0);
8542       tree rhs = build_wfl_node (thisn);
8543       EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8544       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8545     }
8546   return NULL_TREE;
8547 }
8548
8549 \f
8550 /* Building the synthetic `class$' used to implement the `.class' 1.1
8551    extension for non primitive types. This method looks like:
8552
8553     static Class class$(String type) throws NoClassDefFoundError
8554     {
8555       try {return (java.lang.Class.forName (String));}
8556       catch (ClassNotFoundException e) {
8557         throw new NoClassDefFoundError(e.getMessage());}
8558     } */
8559
8560 static GTY(()) tree get_message_wfl;
8561 static GTY(()) tree type_parm_wfl;
8562
8563 static tree
8564 build_dot_class_method (tree class)
8565 {
8566 #define BWF(S) build_wfl_node (get_identifier ((S)))
8567 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8568   tree args, tmp, saved_current_function_decl, mdecl;
8569   tree stmt, throw_stmt;
8570
8571   if (!get_message_wfl)
8572     {
8573       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8574       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8575     }
8576
8577   /* Build the arguments */
8578   args = build_tree_list (get_identifier ("type$"),
8579                           build_pointer_type (string_type_node));
8580   TREE_CHAIN (args) = end_params_node;
8581
8582   /* Build the qualified name java.lang.Class.forName */
8583   tmp = MQN (MQN (MQN (BWF ("java"),
8584                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8585   load_class (class_not_found_type_node, 1);
8586   load_class (no_class_def_found_type_node, 1);
8587
8588   /* Create the "class$" function */
8589   mdecl = create_artificial_method (class, ACC_STATIC,
8590                                     build_pointer_type (class_type_node),
8591                                     classdollar_identifier_node, args);
8592   DECL_FUNCTION_THROWS (mdecl) =
8593     build_tree_list (NULL_TREE, no_class_def_found_type_node);
8594
8595   /* We start by building the try block. We need to build:
8596        return (java.lang.Class.forName (type)); */
8597   stmt = build_method_invocation (tmp,
8598                                   build_tree_list (NULL_TREE, type_parm_wfl));
8599   stmt = build_return (0, stmt);
8600
8601   /* Now onto the catch block. We start by building the expression
8602      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8603   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8604                                     get_message_wfl, 0);
8605   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8606
8607   /* Build new NoClassDefFoundError (_.getMessage) */
8608   throw_stmt = build_new_invocation
8609     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8610      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8611
8612   /* Build the throw, (it's too early to use BUILD_THROW) */
8613   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8614
8615   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8616   stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8617                                      stmt, throw_stmt);
8618
8619   fix_method_argument_names (args, mdecl);
8620   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8621   saved_current_function_decl = current_function_decl;
8622   start_artificial_method_body (mdecl);
8623   java_method_add_stmt (mdecl, stmt);
8624   end_artificial_method_body (mdecl);
8625   current_function_decl = saved_current_function_decl;
8626   TYPE_DOT_CLASS (class) = mdecl;
8627
8628   return mdecl;
8629 }
8630
8631 static tree
8632 build_dot_class_method_invocation (tree type)
8633 {
8634   tree sig_id, s;
8635
8636   if (TYPE_ARRAY_P (type))
8637     sig_id = build_java_signature (type);
8638   else
8639     sig_id = DECL_NAME (TYPE_NAME (type));
8640
8641   /* Ensure that the proper name separator is used */
8642   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8643                                IDENTIFIER_LENGTH (sig_id));
8644
8645   s = build_string (IDENTIFIER_LENGTH (sig_id),
8646                     IDENTIFIER_POINTER (sig_id));
8647   return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8648                                   build_tree_list (NULL_TREE, s));
8649 }
8650
8651 /* This section of the code deals with constructor.  */
8652
8653 /* Craft a body for default constructor. Patch existing constructor
8654    bodies with call to super() and field initialization statements if
8655    necessary.  */
8656
8657 static void
8658 fix_constructors (tree mdecl)
8659 {
8660   tree iii;                     /* Instance Initializer Invocation */
8661   tree body = DECL_FUNCTION_BODY (mdecl);
8662   tree thisn_assign, compound = NULL_TREE;
8663   tree class_type = DECL_CONTEXT (mdecl);
8664
8665   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8666     return;
8667   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8668
8669   if (!body)
8670     {
8671       /* It is an error for the compiler to generate a default
8672          constructor if the superclass doesn't have a constructor that
8673          takes no argument, or the same args for an anonymous class */
8674       if (verify_constructor_super (mdecl))
8675         {
8676           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8677           tree save = DECL_NAME (mdecl);
8678           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8679           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8680           parse_error_context
8681             (lookup_cl (TYPE_NAME (class_type)),
8682              "No constructor matching `%s' found in class `%s'",
8683              lang_printable_name (mdecl, 0), n);
8684           DECL_NAME (mdecl) = save;
8685         }
8686
8687       /* The constructor body must be crafted by hand. It's the
8688          constructor we defined when we realize we didn't have the
8689          CLASSNAME() constructor */
8690       start_artificial_method_body (mdecl);
8691
8692       /* Insert an assignment to the this$<n> hidden field, if
8693          necessary */
8694       if ((thisn_assign = build_thisn_assign ()))
8695         java_method_add_stmt (mdecl, thisn_assign);
8696
8697       /* We don't generate a super constructor invocation if we're
8698          compiling java.lang.Object. build_super_invocation takes care
8699          of that. */
8700       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8701
8702       /* FIXME */
8703       if ((iii = build_instinit_invocation (class_type)))
8704         java_method_add_stmt (mdecl, iii);
8705
8706       end_artificial_method_body (mdecl);
8707     }
8708   /* Search for an explicit constructor invocation */
8709   else
8710     {
8711       int found = 0;
8712       int invokes_this = 0;
8713       tree found_call = NULL_TREE;
8714       tree main_block = BLOCK_EXPR_BODY (body);
8715
8716       while (body)
8717         switch (TREE_CODE (body))
8718           {
8719           case CALL_EXPR:
8720             found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8721             if (CALL_THIS_CONSTRUCTOR_P (body))
8722               invokes_this = 1;
8723             body = NULL_TREE;
8724             break;
8725           case COMPOUND_EXPR:
8726           case EXPR_WITH_FILE_LOCATION:
8727             found_call = body;
8728             body = TREE_OPERAND (body, 0);
8729             break;
8730           case BLOCK:
8731             found_call = body;
8732             body = BLOCK_EXPR_BODY (body);
8733             break;
8734           default:
8735             found = 0;
8736             body = NULL_TREE;
8737           }
8738
8739       /* Generate the assignment to this$<n>, if necessary */
8740       if ((thisn_assign = build_thisn_assign ()))
8741         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8742
8743       /* The constructor is missing an invocation of super() */
8744       if (!found)
8745         compound = add_stmt_to_compound (compound, NULL_TREE,
8746                                          build_super_invocation (mdecl));
8747       /* Explicit super() invokation should take place before the
8748          instance initializer blocks. */
8749       else
8750         {
8751           compound = add_stmt_to_compound (compound, NULL_TREE,
8752                                            TREE_OPERAND (found_call, 0));
8753           TREE_OPERAND (found_call, 0) = empty_stmt_node;
8754         }
8755
8756       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8757
8758       /* Insert the instance initializer block right after. */
8759       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8760         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8761
8762       /* Fix the constructor main block if we're adding extra stmts */
8763       if (compound)
8764         {
8765           compound = add_stmt_to_compound (compound, NULL_TREE,
8766                                            BLOCK_EXPR_BODY (main_block));
8767           BLOCK_EXPR_BODY (main_block) = compound;
8768         }
8769     }
8770 }
8771
8772 /* Browse constructors in the super class, searching for a constructor
8773    that doesn't take any argument. Return 0 if one is found, 1
8774    otherwise.  If the current class is an anonymous inner class, look
8775    for something that has the same signature. */
8776
8777 static int
8778 verify_constructor_super (tree mdecl)
8779 {
8780   tree class = CLASSTYPE_SUPER (current_class);
8781   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8782   tree sdecl;
8783
8784   if (!class)
8785     return 0;
8786
8787   if (ANONYMOUS_CLASS_P (current_class))
8788     {
8789       tree mdecl_arg_type;
8790       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8791       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8792         if (DECL_CONSTRUCTOR_P (sdecl))
8793           {
8794             tree m_arg_type;
8795             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8796             if (super_inner)
8797               arg_type = TREE_CHAIN (arg_type);
8798             for (m_arg_type = mdecl_arg_type;
8799                  (arg_type != end_params_node
8800                   && m_arg_type != end_params_node);
8801                  arg_type = TREE_CHAIN (arg_type),
8802                    m_arg_type = TREE_CHAIN (m_arg_type))
8803               if (!valid_method_invocation_conversion_p
8804                      (TREE_VALUE (arg_type),
8805                       TREE_VALUE (m_arg_type)))
8806                 break;
8807
8808             if (arg_type == end_params_node && m_arg_type == end_params_node)
8809               return 0;
8810           }
8811     }
8812   else
8813     {
8814       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8815         {
8816           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8817           if (super_inner)
8818             arg = TREE_CHAIN (arg);
8819           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8820             return 0;
8821         }
8822     }
8823   return 1;
8824 }
8825
8826 /* Generate code for all context remembered for code generation.  */
8827
8828 static GTY(()) tree reversed_class_list;
8829 void
8830 java_expand_classes (void)
8831 {
8832   int save_error_count = 0;
8833   static struct parser_ctxt *cur_ctxp = NULL;
8834
8835   java_parse_abort_on_error ();
8836   if (!(ctxp = ctxp_for_generation))
8837     return;
8838   java_layout_classes ();
8839   java_parse_abort_on_error ();
8840
8841   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8842     {
8843       ctxp = cur_ctxp;
8844       input_filename = ctxp->filename;
8845       lang_init_source (2);            /* Error msgs have method prototypes */
8846       java_complete_expand_classes (); /* Complete and expand classes */
8847       java_parse_abort_on_error ();
8848     }
8849   input_filename = main_input_filename;
8850
8851
8852   /* Find anonymous classes and expand their constructor. This extra pass is
8853      neccessary because the constructor itself is only generated when the
8854      method in which it is defined is expanded. */
8855   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8856     {
8857       tree current;
8858       ctxp = cur_ctxp;
8859       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8860         {
8861           current_class = TREE_TYPE (current);
8862           if (ANONYMOUS_CLASS_P (current_class))
8863             {
8864               tree d;
8865               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8866                 {
8867                   if (DECL_CONSTRUCTOR_P (d))
8868                     {
8869                       restore_line_number_status (1);
8870                       java_complete_expand_method (d);
8871                       restore_line_number_status (0);
8872                       break;    /* There is only one constructor. */
8873                     }
8874                 }
8875             }
8876         }
8877     }
8878
8879   /* If we've found error at that stage, don't try to generate
8880      anything, unless we're emitting xrefs or checking the syntax only
8881      (but not using -fsyntax-only for the purpose of generating
8882      bytecode. */
8883   if (java_error_count && !flag_emit_xref
8884       && (!flag_syntax_only && !flag_emit_class_files))
8885     return;
8886
8887   /* Now things are stable, go for generation of the class data. */
8888
8889   /* We pessimistically marked all fields external until we knew
8890      what set of classes we were planning to compile.  Now mark
8891      those that will be generated locally as not external.  */
8892   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8893     {
8894       tree current;
8895       ctxp = cur_ctxp;
8896       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8897         {
8898           tree class = TREE_TYPE (current);
8899           tree field;
8900           for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
8901             if (FIELD_STATIC (field))
8902               DECL_EXTERNAL (field) = 0;
8903         }
8904     }
8905
8906   /* Compile the classes.  */
8907   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8908     {
8909       tree current;
8910       reversed_class_list = NULL;
8911
8912       ctxp = cur_ctxp;
8913
8914       /* We write out the classes in reverse order.  This ensures that
8915          inner classes are written before their containing classes,
8916          which is important for parallel builds.  Otherwise, the
8917          class file for the outer class may be found, but the class
8918          file for the inner class may not be present.  In that
8919          situation, the compiler cannot fall back to the original
8920          source, having already read the outer class, so we must
8921          prevent that situation.  */
8922       for (current = ctxp->class_list;
8923            current;
8924            current = TREE_CHAIN (current))
8925         reversed_class_list
8926           = tree_cons (NULL_TREE, current, reversed_class_list);
8927
8928       for (current = reversed_class_list;
8929            current;
8930            current = TREE_CHAIN (current))
8931         {
8932           current_class = TREE_TYPE (TREE_VALUE (current));
8933           outgoing_cpool = TYPE_CPOOL (current_class);
8934           if (flag_emit_class_files)
8935             write_classfile (current_class);
8936           if (flag_emit_xref)
8937             expand_xref (current_class);
8938           else if (! flag_syntax_only)
8939             {
8940               java_expand_method_bodies (current_class);
8941               finish_class ();
8942             }
8943         }
8944     }
8945 }
8946
8947 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8948    a tree list node containing RIGHT. Fore coming RIGHTs will be
8949    chained to this hook. LOCATION contains the location of the
8950    separating `.' operator.  */
8951
8952 static tree
8953 make_qualified_primary (tree primary, tree right, int location)
8954 {
8955   tree wfl;
8956
8957   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8958     wfl = build_wfl_wrap (primary, location);
8959   else
8960     {
8961       wfl = primary;
8962       /* If wfl wasn't qualified, we build a first anchor */
8963       if (!EXPR_WFL_QUALIFICATION (wfl))
8964         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8965     }
8966
8967   /* And chain them */
8968   EXPR_WFL_LINECOL (right) = location;
8969   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8970   PRIMARY_P (wfl) =  1;
8971   return wfl;
8972 }
8973
8974 /* Simple merge of two name separated by a `.' */
8975
8976 static tree
8977 merge_qualified_name (tree left, tree right)
8978 {
8979   tree node;
8980   if (!left && !right)
8981     return NULL_TREE;
8982
8983   if (!left)
8984     return right;
8985
8986   if (!right)
8987     return left;
8988
8989   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8990                 IDENTIFIER_LENGTH (left));
8991   obstack_1grow (&temporary_obstack, '.');
8992   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8993                  IDENTIFIER_LENGTH (right));
8994   node =  get_identifier (obstack_base (&temporary_obstack));
8995   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8996   QUALIFIED_P (node) = 1;
8997   return node;
8998 }
8999
9000 /* Merge the two parts of a qualified name into LEFT.  Set the
9001    location information of the resulting node to LOCATION, usually
9002    inherited from the location information of the `.' operator. */
9003
9004 static tree
9005 make_qualified_name (tree left, tree right, int location)
9006 {
9007 #ifdef USE_COMPONENT_REF
9008   tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9009   EXPR_WFL_LINECOL (node) = location;
9010   return node;
9011 #else
9012   tree left_id = EXPR_WFL_NODE (left);
9013   tree right_id = EXPR_WFL_NODE (right);
9014   tree wfl, merge;
9015
9016   merge = merge_qualified_name (left_id, right_id);
9017
9018   /* Left wasn't qualified and is now qualified */
9019   if (!QUALIFIED_P (left_id))
9020     {
9021       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9022       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9023       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9024     }
9025
9026   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9027   EXPR_WFL_LINECOL (wfl) = location;
9028   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9029
9030   EXPR_WFL_NODE (left) = merge;
9031   return left;
9032 #endif
9033 }
9034
9035 /* Extract the last identifier component of the qualified in WFL. The
9036    last identifier is removed from the linked list */
9037
9038 static tree
9039 cut_identifier_in_qualified (tree wfl)
9040 {
9041   tree q;
9042   tree previous = NULL_TREE;
9043   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9044     if (!TREE_CHAIN (q))
9045       {
9046         if (!previous)
9047           /* Operating on a non qualified qualified WFL.  */
9048           abort ();
9049
9050         TREE_CHAIN (previous) = NULL_TREE;
9051         return TREE_PURPOSE (q);
9052       }
9053 }
9054
9055 /* Resolve the expression name NAME. Return its decl.  */
9056
9057 static tree
9058 resolve_expression_name (tree id, tree *orig)
9059 {
9060   tree name = EXPR_WFL_NODE (id);
9061   tree decl;
9062
9063   /* 6.5.5.1: Simple expression names */
9064   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9065     {
9066       /* 15.13.1: NAME can appear within the scope of a local variable
9067          declaration */
9068       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9069         return decl;
9070
9071       /* 15.13.1: NAME can appear within a class declaration */
9072       else
9073         {
9074           decl = lookup_field_wrapper (current_class, name);
9075           if (decl)
9076             {
9077               tree access = NULL_TREE;
9078               int fs = FIELD_STATIC (decl);
9079
9080               /* If we're accessing an outer scope local alias, make
9081                  sure we change the name of the field we're going to
9082                  build access to. */
9083               if (FIELD_LOCAL_ALIAS_USED (decl))
9084                 name = DECL_NAME (decl);
9085
9086               /* Instance variable (8.3.1.1) can't appear within
9087                  static method, static initializer or initializer for
9088                  a static variable. */
9089               if (!fs && METHOD_STATIC (current_function_decl))
9090                 {
9091                   static_ref_err (id, name, current_class);
9092                   return error_mark_node;
9093                 }
9094               /* Instance variables can't appear as an argument of
9095                  an explicit constructor invocation */
9096               if (!fs && ctxp->explicit_constructor_p
9097                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9098                 {
9099                   parse_error_context
9100                     (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9101                   return error_mark_node;
9102                 }
9103
9104               /* If we're processing an inner class and we're trying
9105                  to access a field belonging to an outer class, build
9106                  the access to the field */
9107               if (!fs && outer_field_access_p (current_class, decl))
9108                 {
9109                   if (CLASS_STATIC (TYPE_NAME (current_class)))
9110                     {
9111                       static_ref_err (id, DECL_NAME (decl), current_class);
9112                       return error_mark_node;
9113                     }
9114                   access = build_outer_field_access (id, decl);
9115                   if (orig)
9116                     *orig = access;
9117                   return access;
9118                 }
9119
9120               /* Otherwise build what it takes to access the field */
9121               access = build_field_ref ((fs ? NULL_TREE : current_this),
9122                                         DECL_CONTEXT (decl), name);
9123               if (fs)
9124                 access = maybe_build_class_init_for_field (decl, access);
9125               /* We may be asked to save the real field access node */
9126               if (orig)
9127                 *orig = access;
9128               /* And we return what we got */
9129               return access;
9130             }
9131           /* Fall down to error report on undefined variable */
9132         }
9133     }
9134   /* 6.5.5.2 Qualified Expression Names */
9135   else
9136     {
9137       if (orig)
9138         *orig = NULL_TREE;
9139       qualify_ambiguous_name (id);
9140       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9141       /* 15.10.2: Accessing Superclass Members using super */
9142       return resolve_field_access (id, orig, NULL);
9143     }
9144
9145   /* We've got an error here */
9146   if (INNER_CLASS_TYPE_P (current_class))
9147     parse_error_context (id,
9148                          "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9149                          IDENTIFIER_POINTER (name),
9150                          IDENTIFIER_POINTER (DECL_NAME
9151                                              (TYPE_NAME (current_class))));
9152   else
9153     parse_error_context (id, "Undefined variable `%s'",
9154                          IDENTIFIER_POINTER (name));
9155
9156   return error_mark_node;
9157 }
9158
9159 static void
9160 static_ref_err (tree wfl, tree field_id, tree class_type)
9161 {
9162   parse_error_context
9163     (wfl,
9164      "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9165      IDENTIFIER_POINTER (field_id),
9166      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9167 }
9168
9169 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9170    We return something suitable to generate the field access. We also
9171    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9172    recipient's address can be null. */
9173
9174 static tree
9175 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9176 {
9177   int is_static = 0;
9178   tree field_ref;
9179   tree decl, where_found, type_found;
9180
9181   if (resolve_qualified_expression_name (qual_wfl, &decl,
9182                                          &where_found, &type_found))
9183     return error_mark_node;
9184
9185   /* Resolve the LENGTH field of an array here */
9186   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9187       && type_found && TYPE_ARRAY_P (type_found)
9188       && ! flag_emit_class_files && ! flag_emit_xref)
9189     {
9190       tree length = build_java_array_length_access (where_found);
9191       field_ref = length;
9192
9193       /* In case we're dealing with a static array, we need to
9194          initialize its class before the array length can be fetched.
9195          It's also a good time to create a DECL_RTL for the field if
9196          none already exists, otherwise if the field was declared in a
9197          class found in an external file and hasn't been (and won't
9198          be) accessed for its value, none will be created. */
9199       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9200         {
9201           build_static_field_ref (where_found);
9202           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9203         }
9204     }
9205   /* We might have been trying to resolve field.method(). In which
9206      case, the resolution is over and decl is the answer */
9207   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9208     field_ref = decl;
9209   else if (JDECL_P (decl))
9210     {
9211       if (!type_found)
9212         type_found = DECL_CONTEXT (decl);
9213       is_static = FIELD_STATIC (decl);
9214       field_ref = build_field_ref ((is_static && !flag_emit_xref?
9215                                     NULL_TREE : where_found),
9216                                    type_found, DECL_NAME (decl));
9217       if (field_ref == error_mark_node)
9218         return error_mark_node;
9219       if (is_static)
9220         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9221     }
9222   else
9223     field_ref = decl;
9224
9225   if (field_decl)
9226     *field_decl = decl;
9227   if (field_type)
9228     *field_type = (QUAL_DECL_TYPE (decl) ?
9229                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9230   return field_ref;
9231 }
9232
9233 /* If NODE is an access to f static field, strip out the class
9234    initialization part and return the field decl, otherwise, return
9235    NODE. */
9236
9237 static tree
9238 strip_out_static_field_access_decl (tree node)
9239 {
9240   if (TREE_CODE (node) == COMPOUND_EXPR)
9241     {
9242       tree op1 = TREE_OPERAND (node, 1);
9243       if (TREE_CODE (op1) == COMPOUND_EXPR)
9244          {
9245            tree call = TREE_OPERAND (op1, 0);
9246            if (TREE_CODE (call) == CALL_EXPR
9247                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9248                && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9249                == soft_initclass_node)
9250              return TREE_OPERAND (op1, 1);
9251          }
9252       else if (JDECL_P (op1))
9253         return op1;
9254     }
9255   return node;
9256 }
9257
9258 /* 6.5.5.2: Qualified Expression Names */
9259
9260 static int
9261 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9262                                    tree *where_found, tree *type_found)
9263 {
9264   int from_type = 0;            /* Field search initiated from a type */
9265   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9266   int previous_call_static = 0;
9267   int is_static;
9268   tree decl = NULL_TREE, type = NULL_TREE, q;
9269   /* For certain for of inner class instantiation */
9270   tree saved_current, saved_this;
9271 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9272   { current_class = saved_current; current_this = saved_this;}
9273
9274   *type_found = *where_found = NULL_TREE;
9275
9276   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9277     {
9278       tree qual_wfl = QUAL_WFL (q);
9279       tree ret_decl;            /* for EH checking */
9280       int location;             /* for EH checking */
9281
9282       /* 15.10.1 Field Access Using a Primary */
9283       switch (TREE_CODE (qual_wfl))
9284         {
9285         case CALL_EXPR:
9286         case NEW_CLASS_EXPR:
9287           /* If the access to the function call is a non static field,
9288              build the code to access it. */
9289           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9290             {
9291               decl = maybe_access_field (decl, *where_found,
9292                                          DECL_CONTEXT (decl));
9293               if (decl == error_mark_node)
9294                 return 1;
9295             }
9296
9297           /* And code for the function call */
9298           if (complete_function_arguments (qual_wfl))
9299             return 1;
9300
9301           /* We might have to setup a new current class and a new this
9302              for the search of an inner class, relative to the type of
9303              a expression resolved as `decl'. The current values are
9304              saved and restored shortly after */
9305           saved_current = current_class;
9306           saved_this = current_this;
9307           if (decl
9308               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9309                   || from_qualified_this))
9310             {
9311               /* If we still have `from_qualified_this', we have the form
9312                  <T>.this.f() and we need to build <T>.this */
9313               if (from_qualified_this)
9314                 {
9315                   decl = build_access_to_thisn (current_class, type, 0);
9316                   decl = java_complete_tree (decl);
9317                   type = TREE_TYPE (TREE_TYPE (decl));
9318                 }
9319               current_class = type;
9320               current_this = decl;
9321               from_qualified_this = 0;
9322             }
9323
9324           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9325             CALL_USING_SUPER (qual_wfl) = 1;
9326           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9327                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9328           *where_found = patch_method_invocation (qual_wfl, decl, type,
9329                                                   from_super,
9330                                                   &is_static, &ret_decl);
9331           from_super = 0;
9332           if (*where_found == error_mark_node)
9333             {
9334               RESTORE_THIS_AND_CURRENT_CLASS;
9335               return 1;
9336             }
9337           *type_found = type = QUAL_DECL_TYPE (*where_found);
9338
9339           *where_found = force_evaluation_order (*where_found);
9340
9341           /* If we're creating an inner class instance, check for that
9342              an enclosing instance is in scope */
9343           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9344               && INNER_ENCLOSING_SCOPE_CHECK (type))
9345             {
9346               parse_error_context
9347                 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9348                  lang_printable_name (type, 0),
9349                  (!current_this ? "" :
9350                   "; an explicit one must be provided when creating this inner class"));
9351               RESTORE_THIS_AND_CURRENT_CLASS;
9352               return 1;
9353             }
9354
9355           /* In case we had to change then to resolve a inner class
9356              instantiation using a primary qualified by a `new' */
9357           RESTORE_THIS_AND_CURRENT_CLASS;
9358
9359           /* EH check. No check on access$<n> functions */
9360           if (location
9361               && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9362                     (DECL_NAME (current_function_decl)))
9363             check_thrown_exceptions (location, ret_decl);
9364
9365           /* If the previous call was static and this one is too,
9366              build a compound expression to hold the two (because in
9367              that case, previous function calls aren't transported as
9368              forcoming function's argument. */
9369           if (previous_call_static && is_static)
9370             {
9371               decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9372                             decl, *where_found);
9373               TREE_SIDE_EFFECTS (decl) = 1;
9374             }
9375           else
9376             {
9377               previous_call_static = is_static;
9378               decl = *where_found;
9379             }
9380           from_type = 0;
9381           continue;
9382
9383         case NEW_ARRAY_EXPR:
9384         case NEW_ANONYMOUS_ARRAY_EXPR:
9385           *where_found = decl = java_complete_tree (qual_wfl);
9386           if (decl == error_mark_node)
9387             return 1;
9388           *type_found = type = QUAL_DECL_TYPE (decl);
9389           continue;
9390
9391         case CONVERT_EXPR:
9392           *where_found = decl = java_complete_tree (qual_wfl);
9393           if (decl == error_mark_node)
9394             return 1;
9395           *type_found = type = QUAL_DECL_TYPE (decl);
9396           from_cast = 1;
9397           continue;
9398
9399         case CONDITIONAL_EXPR:
9400         case STRING_CST:
9401         case MODIFY_EXPR:
9402           *where_found = decl = java_complete_tree (qual_wfl);
9403           if (decl == error_mark_node)
9404             return 1;
9405           *type_found = type = QUAL_DECL_TYPE (decl);
9406           continue;
9407
9408         case ARRAY_REF:
9409           /* If the access to the function call is a non static field,
9410              build the code to access it. */
9411           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9412             {
9413               decl = maybe_access_field (decl, *where_found, type);
9414               if (decl == error_mark_node)
9415                 return 1;
9416             }
9417           /* And code for the array reference expression */
9418           decl = java_complete_tree (qual_wfl);
9419           if (decl == error_mark_node)
9420             return 1;
9421           type = QUAL_DECL_TYPE (decl);
9422           continue;
9423
9424         case PLUS_EXPR:
9425           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9426             return 1;
9427           if ((type = patch_string (decl)))
9428             decl = type;
9429           *where_found = QUAL_RESOLUTION (q) = decl;
9430           *type_found = type = TREE_TYPE (decl);
9431           break;
9432
9433         case CLASS_LITERAL:
9434           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9435             return 1;
9436           *where_found = QUAL_RESOLUTION (q) = decl;
9437           *type_found = type = TREE_TYPE (decl);
9438           break;
9439
9440         default:
9441           /* Fix for -Wall Just go to the next statement. Don't
9442              continue */
9443           break;
9444         }
9445
9446       /* If we fall here, we weren't processing a (static) function call. */
9447       previous_call_static = 0;
9448
9449       /* It can be the keyword THIS */
9450       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9451           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9452         {
9453           if (!current_this)
9454             {
9455               parse_error_context
9456                 (wfl, "Keyword `this' used outside allowed context");
9457               return 1;
9458             }
9459           if (ctxp->explicit_constructor_p
9460               && type == current_class)
9461             {
9462               parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9463               return 1;
9464             }
9465           /* We have to generate code for intermediate access */
9466           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9467             {
9468               *where_found = decl = current_this;
9469               *type_found = type = QUAL_DECL_TYPE (decl);
9470             }
9471           /* We're trying to access the this from somewhere else. Make sure
9472              it's allowed before doing so. */
9473           else
9474             {
9475               if (!enclosing_context_p (type, current_class))
9476                 {
9477                   char *p  = xstrdup (lang_printable_name (type, 0));
9478                   parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9479                                        p, p,
9480                                        lang_printable_name (current_class, 0));
9481                   free (p);
9482                   return 1;
9483                 }
9484               from_qualified_this = 1;
9485               /* If there's nothing else after that, we need to
9486                  produce something now, otherwise, the section of the
9487                  code that needs to produce <T>.this will generate
9488                  what is necessary. */
9489               if (!TREE_CHAIN (q))
9490                 {
9491                   decl = build_access_to_thisn (current_class, type, 0);
9492                   *where_found = decl = java_complete_tree (decl);
9493                   *type_found = type = TREE_TYPE (decl);
9494                 }
9495             }
9496
9497           from_type = 0;
9498           continue;
9499         }
9500
9501       /* 15.10.2 Accessing Superclass Members using SUPER */
9502       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9503           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9504         {
9505           tree node;
9506           /* Check on the restricted use of SUPER */
9507           if (METHOD_STATIC (current_function_decl)
9508               || current_class == object_type_node)
9509             {
9510               parse_error_context
9511                 (wfl, "Keyword `super' used outside allowed context");
9512               return 1;
9513             }
9514           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9515           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9516                              CLASSTYPE_SUPER (current_class),
9517                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9518           *where_found = decl = java_complete_tree (node);
9519           if (decl == error_mark_node)
9520             return 1;
9521           *type_found = type = QUAL_DECL_TYPE (decl);
9522           from_super = from_type = 1;
9523           continue;
9524         }
9525
9526       /* 15.13.1: Can't search for field name in packages, so we
9527          assume a variable/class name was meant. */
9528       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9529         {
9530           tree name;
9531           if ((decl = resolve_package (wfl, &q, &name)))
9532             {
9533               tree list;
9534               *where_found = decl;
9535
9536               /* We want to be absolutely sure that the class is laid
9537                  out. We're going to search something inside it. */
9538               *type_found = type = TREE_TYPE (decl);
9539               layout_class (type);
9540               from_type = 1;
9541
9542               /* Fix them all the way down, if any are left. */
9543               if (q)
9544                 {
9545                   list = TREE_CHAIN (q);
9546                   while (list)
9547                     {
9548                       RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9549                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9550                       list = TREE_CHAIN (list);
9551                     }
9552                 }
9553             }
9554           else
9555             {
9556               if (from_super || from_cast)
9557                 parse_error_context
9558                   ((from_cast ? qual_wfl : wfl),
9559                    "No variable `%s' defined in class `%s'",
9560                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9561                    lang_printable_name (type, 0));
9562               else
9563                 parse_error_context
9564                   (qual_wfl, "Undefined variable or class name: `%s'",
9565                    IDENTIFIER_POINTER (name));
9566               return 1;
9567             }
9568         }
9569
9570       /* We have a type name. It's been already resolved when the
9571          expression was qualified. */
9572       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9573         {
9574           decl = QUAL_RESOLUTION (q);
9575
9576           /* Sneak preview. If next we see a `new', we're facing a
9577              qualification with resulted in a type being selected
9578              instead of a field.  Report the error */
9579           if(TREE_CHAIN (q)
9580              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9581             {
9582               parse_error_context (qual_wfl, "Undefined variable `%s'",
9583                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9584               return 1;
9585             }
9586
9587           if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9588             {
9589               parse_error_context
9590                 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9591                  java_accstring_lookup (get_access_flags_from_decl (decl)),
9592                  GET_TYPE_NAME (type),
9593                  IDENTIFIER_POINTER (DECL_NAME (decl)),
9594                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9595               return 1;
9596             }
9597           check_deprecation (qual_wfl, decl);
9598
9599           type = TREE_TYPE (decl);
9600           from_type = 1;
9601         }
9602       /* We resolve an expression name */
9603       else
9604         {
9605           tree field_decl = NULL_TREE;
9606
9607           /* If there exists an early resolution, use it. That occurs
9608              only once and we know that there are more things to
9609              come. Don't do that when processing something after SUPER
9610              (we need more thing to be put in place below */
9611           if (!from_super && QUAL_RESOLUTION (q))
9612             {
9613               decl = QUAL_RESOLUTION (q);
9614               if (!type)
9615                 {
9616                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9617                     {
9618                       if (current_this)
9619                         *where_found = current_this;
9620                       else
9621                         {
9622                           static_ref_err (qual_wfl, DECL_NAME (decl),
9623                                           current_class);
9624                           return 1;
9625                         }
9626                       if (outer_field_access_p (current_class, decl))
9627                         decl = build_outer_field_access (qual_wfl, decl);
9628                     }
9629                   else
9630                     {
9631                       *where_found = TREE_TYPE (decl);
9632                       if (TREE_CODE (*where_found) == POINTER_TYPE)
9633                         *where_found = TREE_TYPE (*where_found);
9634                     }
9635                 }
9636             }
9637
9638           /* Report and error if we're using a numerical litteral as a
9639              qualifier. It can only be an INTEGER_CST. */
9640           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9641             {
9642               parse_error_context
9643                 (wfl, "Can't use type `%s' as a qualifier",
9644                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
9645               return 1;
9646             }
9647
9648           /* We have to search for a field, knowing the type of its
9649              container. The flag FROM_TYPE indicates that we resolved
9650              the last member of the expression as a type name, which
9651              means that for the resolution of this field, we'll look
9652              for other errors than if it was resolved as a member of
9653              an other field. */
9654           else
9655             {
9656               int is_static;
9657               tree field_decl_type; /* For layout */
9658
9659               if (!from_type && !JREFERENCE_TYPE_P (type))
9660                 {
9661                   parse_error_context
9662                     (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9663                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9664                      lang_printable_name (type, 0),
9665                      IDENTIFIER_POINTER (DECL_NAME (decl)));
9666                   return 1;
9667                 }
9668
9669               field_decl = lookup_field_wrapper (type,
9670                                                  EXPR_WFL_NODE (qual_wfl));
9671
9672               /* Maybe what we're trying to access to is an inner
9673                  class, only if decl is a TYPE_DECL. */
9674               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9675                 {
9676                   tree ptr, inner_decl;
9677
9678                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9679                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9680                   if (inner_decl)
9681                     {
9682                       check_inner_class_access (inner_decl, decl, qual_wfl);
9683                       type = TREE_TYPE (inner_decl);
9684                       decl = inner_decl;
9685                       from_type = 1;
9686                       continue;
9687                     }
9688                 }
9689
9690               if (field_decl == NULL_TREE)
9691                 {
9692                   parse_error_context
9693                     (qual_wfl, "No variable `%s' defined in type `%s'",
9694                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9695                      GET_TYPE_NAME (type));
9696                   return 1;
9697                 }
9698               if (field_decl == error_mark_node)
9699                 return 1;
9700
9701               /* Layout the type of field_decl, since we may need
9702                  it. Don't do primitive types or loaded classes. The
9703                  situation of non primitive arrays may not handled
9704                  properly here. FIXME */
9705               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9706                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9707               else
9708                 field_decl_type = TREE_TYPE (field_decl);
9709               if (!JPRIMITIVE_TYPE_P (field_decl_type)
9710                   && !CLASS_LOADED_P (field_decl_type)
9711                   && !TYPE_ARRAY_P (field_decl_type))
9712                 resolve_and_layout (field_decl_type, NULL_TREE);
9713
9714               /* Check on accessibility here */
9715               if (not_accessible_p (current_class, field_decl,
9716                                     DECL_CONTEXT (field_decl), from_super))
9717                 {
9718                   parse_error_context
9719                     (qual_wfl,
9720                      "Can't access %s field `%s.%s' from `%s'",
9721                      java_accstring_lookup
9722                        (get_access_flags_from_decl (field_decl)),
9723                      GET_TYPE_NAME (type),
9724                      IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9725                      IDENTIFIER_POINTER
9726                        (DECL_NAME (TYPE_NAME (current_class))));
9727                   return 1;
9728                 }
9729               check_deprecation (qual_wfl, field_decl);
9730
9731               /* There are things to check when fields are accessed
9732                  from type. There are no restrictions on a static
9733                  declaration of the field when it is accessed from an
9734                  interface */
9735               is_static = FIELD_STATIC (field_decl);
9736               if (!from_super && from_type
9737                   && !TYPE_INTERFACE_P (type)
9738                   && !is_static
9739                   && (current_function_decl
9740                       && METHOD_STATIC (current_function_decl)))
9741                 {
9742                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9743                   return 1;
9744                 }
9745               from_cast = from_super = 0;
9746
9747               /* It's an access from a type but it isn't static, we
9748                  make it relative to `this'. */
9749               if (!is_static && from_type)
9750                 decl = current_this;
9751
9752               /* If we need to generate something to get a proper
9753                  handle on what this field is accessed from, do it
9754                  now. */
9755               if (!is_static)
9756                 {
9757                   decl = maybe_access_field (decl, *where_found, *type_found);
9758                   if (decl == error_mark_node)
9759                     return 1;
9760                 }
9761
9762               /* We want to keep the location were found it, and the type
9763                  we found. */
9764               *where_found = decl;
9765               *type_found = type;
9766
9767               /* Generate the correct expression for field access from
9768                  qualified this */
9769               if (from_qualified_this)
9770                 {
9771                   field_decl = build_outer_field_access (qual_wfl, field_decl);
9772                   from_qualified_this = 0;
9773                 }
9774
9775               /* This is the decl found and eventually the next one to
9776                  search from */
9777               decl = field_decl;
9778             }
9779           from_type = 0;
9780           type = QUAL_DECL_TYPE (decl);
9781
9782           /* Sneak preview. If decl is qualified by a `new', report
9783              the error here to be accurate on the peculiar construct */
9784           if (TREE_CHAIN (q)
9785               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9786               && !JREFERENCE_TYPE_P (type))
9787             {
9788               parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9789                                    lang_printable_name (type, 0));
9790               return 1;
9791             }
9792         }
9793       /* `q' might have changed due to a after package resolution
9794          re-qualification */
9795       if (!q)
9796         break;
9797     }
9798   *found_decl = decl;
9799   return 0;
9800 }
9801
9802 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9803    can't be accessed from REFERENCE (a record type). If MEMBER
9804    features a protected access, we then use WHERE which, if non null,
9805    holds the type of MEMBER's access that is checked against
9806    6.6.2.1. This function should be used when decl is a field or a
9807    method.  */
9808
9809 static int
9810 not_accessible_p (tree reference, tree member, tree where, int from_super)
9811 {
9812   int access_flag = get_access_flags_from_decl (member);
9813
9814   /* Inner classes are processed by check_inner_class_access */
9815   if (INNER_CLASS_TYPE_P (reference))
9816     return 0;
9817
9818   /* Access always granted for members declared public */
9819   if (access_flag & ACC_PUBLIC)
9820     return 0;
9821
9822   /* Check access on protected members */
9823   if (access_flag & ACC_PROTECTED)
9824     {
9825       /* Access granted if it occurs from within the package
9826          containing the class in which the protected member is
9827          declared */
9828       if (class_in_current_package (DECL_CONTEXT (member)))
9829         return 0;
9830
9831       /* If accessed with the form `super.member', then access is granted */
9832       if (from_super)
9833         return 0;
9834
9835       /* If where is active, access was made through a
9836          qualifier. Access is granted if the type of the qualifier is
9837          or is a sublass of the type the access made from (6.6.2.1.)  */
9838       if (where && !inherits_from_p (reference, where))
9839         return 1;
9840
9841       /* Otherwise, access is granted if occurring from the class where
9842          member is declared or a subclass of it. Find the right
9843          context to perform the check */
9844       if (PURE_INNER_CLASS_TYPE_P (reference))
9845         {
9846           while (INNER_CLASS_TYPE_P (reference))
9847             {
9848               if (inherits_from_p (reference, DECL_CONTEXT (member)))
9849                 return 0;
9850               reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9851             }
9852         }
9853       if (inherits_from_p (reference, DECL_CONTEXT (member)))
9854         return 0;
9855       return 1;
9856     }
9857
9858   /* Check access on private members. Access is granted only if it
9859      occurs from within the class in which it is declared -- that does
9860      it for innerclasses too. */
9861   if (access_flag & ACC_PRIVATE)
9862     {
9863       if (reference == DECL_CONTEXT (member))
9864         return 0;
9865       if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9866         return 0;
9867       return 1;
9868     }
9869
9870   /* Default access are permitted only when occurring within the
9871      package in which the type (REFERENCE) is declared. In other words,
9872      REFERENCE is defined in the current package */
9873   if (ctxp->package)
9874     return !class_in_current_package (reference);
9875
9876   /* Otherwise, access is granted */
9877   return 0;
9878 }
9879
9880 /* Test deprecated decl access.  */
9881 static void
9882 check_deprecation (tree wfl, tree decl)
9883 {
9884   const char *file = DECL_SOURCE_FILE (decl);
9885   /* Complain if the field is deprecated and the file it was defined
9886      in isn't compiled at the same time the file which contains its
9887      use is */
9888   if (DECL_DEPRECATED (decl)
9889       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9890     {
9891       char the [20];
9892       switch (TREE_CODE (decl))
9893         {
9894         case FUNCTION_DECL:
9895           strcpy (the, "method");
9896           break;
9897         case FIELD_DECL:
9898         case VAR_DECL:
9899           strcpy (the, "field");
9900           break;
9901         case TYPE_DECL:
9902           parse_warning_context (wfl, "The class `%s' has been deprecated",
9903                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
9904           return;
9905         default:
9906           abort ();
9907         }
9908       /* Don't issue a message if the context as been deprecated as a
9909          whole. */
9910       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
9911         parse_warning_context
9912           (wfl, "The %s `%s' in class `%s' has been deprecated",
9913            the, lang_printable_name (decl, 0),
9914            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9915     }
9916 }
9917
9918 /* Returns 1 if class was declared in the current package, 0 otherwise */
9919
9920 static GTY(()) tree cicp_cache;
9921 static int
9922 class_in_current_package (tree class)
9923 {
9924   int qualified_flag;
9925   tree left;
9926
9927   if (cicp_cache == class)
9928     return 1;
9929
9930   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9931
9932   /* If the current package is empty and the name of CLASS is
9933      qualified, class isn't in the current package.  If there is a
9934      current package and the name of the CLASS is not qualified, class
9935      isn't in the current package */
9936   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9937     return 0;
9938
9939   /* If there is not package and the name of CLASS isn't qualified,
9940      they belong to the same unnamed package */
9941   if (!ctxp->package && !qualified_flag)
9942     return 1;
9943
9944   /* Compare the left part of the name of CLASS with the package name */
9945   breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9946   if (ctxp->package == left)
9947     {
9948       cicp_cache = class;
9949       return 1;
9950     }
9951   return 0;
9952 }
9953
9954 /* This function may generate code to access DECL from WHERE. This is
9955    done only if certain conditions meet.  */
9956
9957 static tree
9958 maybe_access_field (tree decl, tree where, tree type)
9959 {
9960   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9961       && !FIELD_STATIC (decl))
9962     decl = build_field_ref (where ? where : current_this,
9963                             (type ? type : DECL_CONTEXT (decl)),
9964                             DECL_NAME (decl));
9965   return decl;
9966 }
9967
9968 /* Build a method invocation, by patching PATCH. If non NULL
9969    and according to the situation, PRIMARY and WHERE may be
9970    used. IS_STATIC is set to 1 if the invoked function is static. */
9971
9972 static tree
9973 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
9974                          int *is_static, tree *ret_decl)
9975 {
9976   tree wfl = TREE_OPERAND (patch, 0);
9977   tree args = TREE_OPERAND (patch, 1);
9978   tree name = EXPR_WFL_NODE (wfl);
9979   tree list;
9980   int is_static_flag = 0;
9981   int is_super_init = 0;
9982   tree this_arg = NULL_TREE;
9983   int is_array_clone_call = 0;
9984
9985   /* Should be overriden if everything goes well. Otherwise, if
9986      something fails, it should keep this value. It stop the
9987      evaluation of a bogus assignment. See java_complete_tree,
9988      MODIFY_EXPR: for the reasons why we sometimes want to keep on
9989      evaluating an assignment */
9990   TREE_TYPE (patch) = error_mark_node;
9991
9992   /* Since lookup functions are messing with line numbers, save the
9993      context now.  */
9994   java_parser_context_save_global ();
9995
9996   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9997
9998   /* Resolution of qualified name, excluding constructors */
9999   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10000     {
10001       tree identifier, identifier_wfl, type, resolved;
10002       /* Extract the last IDENTIFIER of the qualified
10003          expression. This is a wfl and we will use it's location
10004          data during error report. */
10005       identifier_wfl = cut_identifier_in_qualified (wfl);
10006       identifier = EXPR_WFL_NODE (identifier_wfl);
10007
10008       /* Given the context, IDENTIFIER is syntactically qualified
10009          as a MethodName. We need to qualify what's before */
10010       qualify_ambiguous_name (wfl);
10011       resolved = resolve_field_access (wfl, NULL, NULL);
10012
10013       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10014          && FIELD_FINAL (resolved)
10015          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10016          && !flag_emit_class_files && !flag_emit_xref)
10017        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10018
10019       if (resolved == error_mark_node)
10020         PATCH_METHOD_RETURN_ERROR ();
10021
10022       type = GET_SKIP_TYPE (resolved);
10023       resolve_and_layout (type, NULL_TREE);
10024
10025       if (JPRIMITIVE_TYPE_P (type))
10026         {
10027           parse_error_context
10028             (identifier_wfl,
10029              "Can't invoke a method on primitive type `%s'",
10030              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10031           PATCH_METHOD_RETURN_ERROR ();
10032         }
10033
10034       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10035       args = nreverse (args);
10036
10037       /* We're resolving a call from a type */
10038       if (TREE_CODE (resolved) == TYPE_DECL)
10039         {
10040           if (CLASS_INTERFACE (resolved))
10041             {
10042               parse_error_context
10043                 (identifier_wfl,
10044                 "Can't make static reference to method `%s' in interface `%s'",
10045                  IDENTIFIER_POINTER (identifier),
10046                  IDENTIFIER_POINTER (name));
10047               PATCH_METHOD_RETURN_ERROR ();
10048             }
10049           if (list && !METHOD_STATIC (list))
10050             {
10051               char *fct_name = xstrdup (lang_printable_name (list, 0));
10052               parse_error_context
10053                 (identifier_wfl,
10054                  "Can't make static reference to method `%s %s' in class `%s'",
10055                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10056                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10057               free (fct_name);
10058               PATCH_METHOD_RETURN_ERROR ();
10059             }
10060         }
10061       else
10062         this_arg = primary = resolved;
10063
10064       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10065         is_array_clone_call = 1;
10066
10067       /* IDENTIFIER_WFL will be used to report any problem further */
10068       wfl = identifier_wfl;
10069     }
10070   /* Resolution of simple names, names generated after a primary: or
10071      constructors */
10072   else
10073     {
10074       tree class_to_search = NULL_TREE;
10075       int lc;                   /* Looking for Constructor */
10076
10077       /* We search constructor in their target class */
10078       if (CALL_CONSTRUCTOR_P (patch))
10079         {
10080           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10081             class_to_search = EXPR_WFL_NODE (wfl);
10082           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10083                    this_identifier_node)
10084             class_to_search = NULL_TREE;
10085           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10086                    super_identifier_node)
10087             {
10088               is_super_init = 1;
10089               if (CLASSTYPE_SUPER (current_class))
10090                 class_to_search =
10091                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10092               else
10093                 {
10094                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10095                   PATCH_METHOD_RETURN_ERROR ();
10096                 }
10097             }
10098
10099           /* Class to search is NULL if we're searching the current one */
10100           if (class_to_search)
10101             {
10102               class_to_search = resolve_and_layout (class_to_search, wfl);
10103
10104               if (!class_to_search)
10105                 {
10106                   parse_error_context
10107                     (wfl, "Class `%s' not found in type declaration",
10108                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10109                   PATCH_METHOD_RETURN_ERROR ();
10110                 }
10111
10112               /* Can't instantiate an abstract class, but we can
10113                  invoke it's constructor. It's use within the `new'
10114                  context is denied here. */
10115               if (CLASS_ABSTRACT (class_to_search)
10116                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10117                 {
10118                   parse_error_context
10119                     (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10120                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10121                   PATCH_METHOD_RETURN_ERROR ();
10122                 }
10123
10124               class_to_search = TREE_TYPE (class_to_search);
10125             }
10126           else
10127             class_to_search = current_class;
10128           lc = 1;
10129         }
10130       /* This is a regular search in the local class, unless an
10131          alternate class is specified. */
10132       else
10133         {
10134           if (where != NULL_TREE)
10135             class_to_search = where;
10136           else if (QUALIFIED_P (name))
10137             class_to_search = current_class;
10138           else
10139             {
10140               class_to_search = current_class;
10141
10142               for (;;)
10143                 {
10144                   if (has_method (class_to_search, name))
10145                     break;
10146                   if (! INNER_CLASS_TYPE_P (class_to_search))
10147                     {
10148                       parse_error_context (wfl,
10149                                            "No method named `%s' in scope",
10150                                            IDENTIFIER_POINTER (name));
10151                       PATCH_METHOD_RETURN_ERROR ();
10152                     }
10153                   class_to_search
10154                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10155                 }
10156             }
10157           lc = 0;
10158         }
10159
10160       /* NAME is a simple identifier or comes from a primary. Search
10161          in the class whose declaration contain the method being
10162          invoked. */
10163       resolve_and_layout (class_to_search, NULL_TREE);
10164
10165       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10166       /* Don't continue if no method were found, as the next statement
10167          can't be executed then. */
10168       if (!list)
10169         PATCH_METHOD_RETURN_ERROR ();
10170
10171       if (TYPE_ARRAY_P (class_to_search)
10172           && DECL_NAME (list) == get_identifier ("clone"))
10173         is_array_clone_call = 1;
10174
10175       /* Check for static reference if non static methods */
10176       if (check_for_static_method_reference (wfl, patch, list,
10177                                              class_to_search, primary))
10178         PATCH_METHOD_RETURN_ERROR ();
10179
10180       /* Check for inner classes creation from illegal contexts */
10181       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10182                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10183           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10184           && !DECL_INIT_P (current_function_decl))
10185         {
10186           parse_error_context
10187             (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10188              lang_printable_name (class_to_search, 0),
10189              (!current_this ? "" :
10190               "; an explicit one must be provided when creating this inner class"));
10191           PATCH_METHOD_RETURN_ERROR ();
10192         }
10193
10194       /* Non static methods are called with the current object extra
10195          argument. If patch a `new TYPE()', the argument is the value
10196          returned by the object allocator. If method is resolved as a
10197          primary, use the primary otherwise use the current THIS. */
10198       args = nreverse (args);
10199       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10200         {
10201           this_arg = primary ? primary : current_this;
10202
10203           /* If we're using an access method, things are different.
10204              There are two familly of cases:
10205
10206              1) We're not generating bytecodes:
10207
10208              - LIST is non static. It's invocation is transformed from
10209                x(a1,...,an) into this$<n>.x(a1,....an).
10210              - LIST is static. It's invocation is transformed from
10211                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10212
10213              2) We're generating bytecodes:
10214
10215              - LIST is non static. It's invocation is transformed from
10216                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10217              - LIST is static. It's invocation is transformed from
10218                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10219
10220              Of course, this$<n> can be abitrary complex, ranging from
10221              this$0 (the immediate outer context) to
10222              access$0(access$0(...(this$0))).
10223
10224              maybe_use_access_method returns a nonzero value if the
10225              this_arg has to be moved into the (then generated) stub
10226              argument list. In the meantime, the selected function
10227              might have be replaced by a generated stub. */
10228           if (!primary &&
10229               maybe_use_access_method (is_super_init, &list, &this_arg))
10230             {
10231               args = tree_cons (NULL_TREE, this_arg, args);
10232               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10233             }
10234         }
10235     }
10236
10237   /* Merge point of all resolution schemes. If we have nothing, this
10238      is an error, already signaled */
10239   if (!list)
10240     PATCH_METHOD_RETURN_ERROR ();
10241
10242   /* Check accessibility, position the is_static flag, build and
10243      return the call */
10244   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10245                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10246                          NULL_TREE), from_super)
10247       /* Calls to clone() on array types are permitted as a special-case. */
10248       && !is_array_clone_call)
10249     {
10250       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10251       const char *const access =
10252         java_accstring_lookup (get_access_flags_from_decl (list));
10253       const char *const klass =
10254         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10255       const char *const refklass =
10256         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10257       const char *const what = (DECL_CONSTRUCTOR_P (list)
10258                                 ? "constructor" : "method");
10259       /* FIXME: WFL yields the wrong message here but I don't know
10260          what else to use.  */
10261       parse_error_context (wfl,
10262                            "Can't access %s %s `%s.%s' from `%s'",
10263                            access, what, klass, fct_name, refklass);
10264       PATCH_METHOD_RETURN_ERROR ();
10265     }
10266
10267   /* Deprecation check: check whether the method being invoked or the
10268      instance-being-created's type are deprecated. */
10269   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10270     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10271   else
10272     check_deprecation (wfl, list);
10273
10274   /* If invoking a innerclass constructor, there are hidden parameters
10275      to pass */
10276   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10277       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10278     {
10279       /* And make sure we add the accessed local variables to be saved
10280          in field aliases. */
10281       args = build_alias_initializer_parameter_list
10282         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10283
10284       /* Secretly pass the current_this/primary as a second argument */
10285       if (primary || current_this)
10286         {
10287           tree extra_arg;
10288           tree this_type = (current_this ?
10289                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10290           /* Method's (list) enclosing context */
10291           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10292           /* If we have a primary, use it. */
10293           if (primary)
10294             extra_arg = primary;
10295           /* The current `this' is an inner class but isn't a direct
10296              enclosing context for the inner class we're trying to
10297              create. Build an access to the proper enclosing context
10298              and use it. */
10299           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10300                    && this_type != TREE_TYPE (mec))
10301             {
10302
10303               extra_arg = build_access_to_thisn (current_class,
10304                                                  TREE_TYPE (mec), 0);
10305               extra_arg = java_complete_tree (extra_arg);
10306             }
10307           /* Otherwise, just use the current `this' as an enclosing
10308              context. */
10309           else
10310             extra_arg = current_this;
10311           args = tree_cons (NULL_TREE, extra_arg, args);
10312         }
10313       else
10314         args = tree_cons (NULL_TREE, integer_zero_node, args);
10315     }
10316
10317   /* This handles the situation where a constructor invocation needs
10318      to have an enclosing context passed as a second parameter (the
10319      constructor is one of an inner class). */
10320   if ((is_super_init ||
10321        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10322       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10323     {
10324       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10325       tree extra_arg =
10326         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10327       extra_arg = java_complete_tree (extra_arg);
10328       args = tree_cons (NULL_TREE, extra_arg, args);
10329     }
10330
10331   is_static_flag = METHOD_STATIC (list);
10332   if (! is_static_flag && this_arg != NULL_TREE)
10333     args = tree_cons (NULL_TREE, this_arg, args);
10334
10335   /* In the context of an explicit constructor invocation, we can't
10336      invoke any method relying on `this'. Exceptions are: we're
10337      invoking a static function, primary exists and is not the current
10338      this, we're creating a new object. */
10339   if (ctxp->explicit_constructor_p
10340       && !is_static_flag
10341       && (!primary || primary == current_this)
10342       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10343     {
10344       parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10345       PATCH_METHOD_RETURN_ERROR ();
10346     }
10347   java_parser_context_restore_global ();
10348   if (is_static)
10349     *is_static = is_static_flag;
10350   /* Sometimes, we want the decl of the selected method. Such as for
10351      EH checking */
10352   if (ret_decl)
10353     *ret_decl = list;
10354   patch = patch_invoke (patch, list, args);
10355
10356   /* Now is a good time to insert the call to finit$ */
10357   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10358     {
10359       tree finit_parms, finit_call;
10360
10361       /* Prepare to pass hidden parameters to finit$, if any. */
10362       finit_parms = build_alias_initializer_parameter_list
10363         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10364
10365       finit_call =
10366         build_method_invocation (build_wfl_node (finit_identifier_node),
10367                                  finit_parms);
10368
10369       /* Generate the code used to initialize fields declared with an
10370          initialization statement and build a compound statement along
10371          with the super constructor invocation. */
10372       CAN_COMPLETE_NORMALLY (patch) = 1;
10373       patch = build (COMPOUND_EXPR, void_type_node, patch,
10374                      java_complete_tree (finit_call));
10375     }
10376   return patch;
10377 }
10378
10379 /* Check that we're not trying to do a static reference to a method in
10380    non static method. Return 1 if it's the case, 0 otherwise. */
10381
10382 static int
10383 check_for_static_method_reference (tree wfl, tree node, tree method,
10384                                    tree where, tree primary)
10385 {
10386   if (METHOD_STATIC (current_function_decl)
10387       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10388     {
10389       char *fct_name = xstrdup (lang_printable_name (method, 0));
10390       parse_error_context
10391         (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10392          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10393          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10394       free (fct_name);
10395       return 1;
10396     }
10397   return 0;
10398 }
10399
10400 /* Fix the invocation of *MDECL if necessary in the case of a
10401    invocation from an inner class. *THIS_ARG might be modified
10402    appropriately and an alternative access to *MDECL might be
10403    returned.  */
10404
10405 static int
10406 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10407 {
10408   tree ctx;
10409   tree md = *mdecl, ta = *this_arg;
10410   int to_return = 0;
10411   int non_static_context = !METHOD_STATIC (md);
10412
10413   if (is_super_init
10414       || DECL_CONTEXT (md) == current_class
10415       || !PURE_INNER_CLASS_TYPE_P (current_class)
10416       || DECL_FINIT_P (md)
10417       || DECL_INSTINIT_P (md))
10418     return 0;
10419
10420   /* If we're calling a method found in an enclosing class, generate
10421      what it takes to retrieve the right this. Don't do that if we're
10422      invoking a static method. Note that if MD's type is unrelated to
10423      CURRENT_CLASS, then the current this can be used. */
10424
10425   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10426     {
10427       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10428       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10429         {
10430           ta = build_current_thisn (current_class);
10431           ta = build_wfl_node (ta);
10432         }
10433       else
10434         {
10435           tree type = ctx;
10436           while (type)
10437             {
10438               maybe_build_thisn_access_method (type);
10439               if (inherits_from_p (type, DECL_CONTEXT (md)))
10440                 {
10441                   ta = build_access_to_thisn (ctx, type, 0);
10442                   break;
10443                 }
10444               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10445                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10446             }
10447         }
10448       ta = java_complete_tree (ta);
10449     }
10450
10451   /* We might have to use an access method to get to MD. We can
10452      break the method access rule as far as we're not generating
10453      bytecode */
10454   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10455     {
10456       md = build_outer_method_access_method (md);
10457       to_return = 1;
10458     }
10459
10460   *mdecl = md;
10461   *this_arg = ta;
10462
10463   /* Returnin a nonzero value indicates we were doing a non static
10464      method invokation that is now a static invocation. It will have
10465      callee displace `this' to insert it in the regular argument
10466      list. */
10467   return (non_static_context && to_return);
10468 }
10469
10470 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10471    mode.  */
10472
10473 static tree
10474 patch_invoke (tree patch, tree method, tree args)
10475 {
10476   tree dtable, func;
10477   tree original_call, t, ta;
10478   tree check = NULL_TREE;
10479
10480   /* Last step for args: convert build-in types. If we're dealing with
10481      a new TYPE() type call, the first argument to the constructor
10482      isn't found in the incoming argument list, but delivered by
10483      `new' */
10484   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10485   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10486     t = TREE_CHAIN (t);
10487   for (ta = args; t != end_params_node && ta;
10488        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10489     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10490         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10491       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10492
10493   /* Resolve unresolved returned type isses */
10494   t = TREE_TYPE (TREE_TYPE (method));
10495   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10496     resolve_and_layout (TREE_TYPE (t), NULL);
10497
10498   if (flag_emit_class_files || flag_emit_xref)
10499     func = method;
10500   else
10501     {
10502       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10503         {
10504         case INVOKE_VIRTUAL:
10505           dtable = invoke_build_dtable (0, args);
10506           func = build_invokevirtual (dtable, method);
10507           break;
10508
10509         case INVOKE_NONVIRTUAL:
10510           /* If the object for the method call is null, we throw an
10511              exception.  We don't do this if the object is the current
10512              method's `this'.  In other cases we just rely on an
10513              optimization pass to eliminate redundant checks.  */
10514           if (TREE_VALUE (args) != current_this)
10515             {
10516               /* We use a save_expr here to make sure we only evaluate
10517                  the new `self' expression once.  */
10518               tree save_arg = save_expr (TREE_VALUE (args));
10519               TREE_VALUE (args) = save_arg;
10520               check = java_check_reference (save_arg, 1);
10521             }
10522           /* Fall through.  */
10523
10524         case INVOKE_SUPER:
10525         case INVOKE_STATIC:
10526           {
10527             tree signature = build_java_signature (TREE_TYPE (method));
10528             func = build_known_method_ref (method, TREE_TYPE (method),
10529                                            DECL_CONTEXT (method),
10530                                            signature, args);
10531           }
10532           break;
10533
10534         case INVOKE_INTERFACE:
10535           dtable = invoke_build_dtable (1, args);
10536           func = build_invokeinterface (dtable, method);
10537           break;
10538
10539         default:
10540           abort ();
10541         }
10542
10543       /* Ensure self_type is initialized, (invokestatic). FIXME */
10544       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10545     }
10546
10547   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10548   TREE_OPERAND (patch, 0) = func;
10549   TREE_OPERAND (patch, 1) = args;
10550   patch = check_for_builtin (method, patch);
10551   original_call = patch;
10552
10553   /* We're processing a `new TYPE ()' form. New is called and its
10554      returned value is the first argument to the constructor. We build
10555      a COMPOUND_EXPR and use saved expression so that the overall NEW
10556      expression value is a pointer to a newly created and initialized
10557      class. */
10558   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10559     {
10560       tree class = DECL_CONTEXT (method);
10561       tree c1, saved_new, size, new;
10562       tree alloc_node;
10563
10564       if (flag_emit_class_files || flag_emit_xref)
10565         {
10566           TREE_TYPE (patch) = build_pointer_type (class);
10567           return patch;
10568         }
10569       if (!TYPE_SIZE (class))
10570         safe_layout_class (class);
10571       size = size_in_bytes (class);
10572       alloc_node =
10573         (class_has_finalize_method (class) ? alloc_object_node
10574                                            : alloc_no_finalizer_node);
10575       new = build (CALL_EXPR, promote_type (class),
10576                    build_address_of (alloc_node),
10577                    tree_cons (NULL_TREE, build_class_ref (class),
10578                               build_tree_list (NULL_TREE,
10579                                                size_in_bytes (class))),
10580                    NULL_TREE);
10581       saved_new = save_expr (new);
10582       c1 = build_tree_list (NULL_TREE, saved_new);
10583       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10584       TREE_OPERAND (original_call, 1) = c1;
10585       TREE_SET_CODE (original_call, CALL_EXPR);
10586       patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10587     }
10588
10589   /* If CHECK is set, then we are building a check to see if the object
10590      is NULL.  */
10591   if (check != NULL_TREE)
10592     {
10593       /* We have to call force_evaluation_order now because creating a
10594          COMPOUND_EXPR wraps the arg list in a way that makes it
10595          unrecognizable by force_evaluation_order later.  Yuk.  */
10596       patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, 
10597                      force_evaluation_order (patch));
10598       TREE_SIDE_EFFECTS (patch) = 1;
10599     }
10600
10601   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10602      put it as the first expression of a COMPOUND_EXPR. The second
10603      expression being an empty statement to be later patched if
10604      necessary. We remember a TREE_LIST (the PURPOSE is the method,
10605      the VALUE is the compound) in a hashtable and return a
10606      COMPOUND_EXPR built so that the result of the evaluation of the
10607      original PATCH node is returned. */
10608   if (STATIC_CLASS_INIT_OPT_P ()
10609       && current_function_decl && METHOD_STATIC (method))
10610     {
10611       tree list;
10612       tree fndecl = current_function_decl;
10613       /* We have to call force_evaluation_order now because creating a
10614          COMPOUND_EXPR wraps the arg list in a way that makes it
10615          unrecognizable by force_evaluation_order later.  Yuk.  */
10616       tree save = save_expr (force_evaluation_order (patch));
10617       tree type = TREE_TYPE (patch);
10618
10619       patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10620       list = tree_cons (method, patch,
10621                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10622
10623       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10624
10625       patch = build (COMPOUND_EXPR, type, patch, save);
10626     }
10627
10628   return patch;
10629 }
10630
10631 static int
10632 invocation_mode (tree method, int super)
10633 {
10634   int access = get_access_flags_from_decl (method);
10635
10636   if (super)
10637     return INVOKE_SUPER;
10638
10639   if (access & ACC_STATIC)
10640     return INVOKE_STATIC;
10641
10642   /* We have to look for a constructor before we handle nonvirtual
10643      calls; otherwise the constructor will look nonvirtual.  */
10644   if (DECL_CONSTRUCTOR_P (method))
10645     return INVOKE_STATIC;
10646
10647   if (access & ACC_FINAL || access & ACC_PRIVATE)
10648     return INVOKE_NONVIRTUAL;
10649
10650   if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10651     return INVOKE_NONVIRTUAL;
10652
10653   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10654     return INVOKE_INTERFACE;
10655
10656   return INVOKE_VIRTUAL;
10657 }
10658
10659 /* Retrieve a refined list of matching methods. It covers the step
10660    15.11.2 (Compile-Time Step 2) */
10661
10662 static tree
10663 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
10664 {
10665   tree atl = end_params_node;           /* Arg Type List */
10666   tree method, signature, list, node;
10667   const char *candidates;               /* Used for error report */
10668   char *dup;
10669
10670   /* Fix the arguments */
10671   for (node = arg_list; node; node = TREE_CHAIN (node))
10672     {
10673       tree current_arg = TREE_TYPE (TREE_VALUE (node));
10674       /* Non primitive type may have to be resolved */
10675       if (!JPRIMITIVE_TYPE_P (current_arg))
10676         resolve_and_layout (current_arg, NULL_TREE);
10677       /* And promoted */
10678       if (TREE_CODE (current_arg) == RECORD_TYPE)
10679         current_arg = promote_type (current_arg);
10680       atl = tree_cons (NULL_TREE, current_arg, atl);
10681     }
10682
10683   /* Presto. If we're dealing with an anonymous class and a
10684      constructor call, generate the right constructor now, since we
10685      know the arguments' types. */
10686
10687   if (lc && ANONYMOUS_CLASS_P (class))
10688     {
10689       tree saved_current_class;
10690       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
10691       saved_current_class = current_class;
10692       current_class = class;
10693       fix_constructors (mdecl);
10694       current_class = saved_current_class;
10695     }
10696
10697   /* Find all candidates and then refine the list, searching for the
10698      most specific method. */
10699   list = find_applicable_accessible_methods_list (lc, class, name, atl);
10700   list = find_most_specific_methods_list (list);
10701   if (list && !TREE_CHAIN (list))
10702     return TREE_VALUE (list);
10703
10704   /* Issue an error. List candidates if any. Candidates are listed
10705      only if accessible (non accessible methods may end-up here for
10706      the sake of a better error report). */
10707   candidates = NULL;
10708   if (list)
10709     {
10710       tree current;
10711       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10712       for (current = list; current; current = TREE_CHAIN (current))
10713         {
10714           tree cm = TREE_VALUE (current);
10715           char string [4096];
10716           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10717             continue;
10718           sprintf
10719             (string, "  `%s' in `%s'%s",
10720              get_printable_method_name (cm),
10721              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10722              (TREE_CHAIN (current) ? "\n" : ""));
10723           obstack_grow (&temporary_obstack, string, strlen (string));
10724         }
10725       obstack_1grow (&temporary_obstack, '\0');
10726       candidates = obstack_finish (&temporary_obstack);
10727     }
10728   /* Issue the error message */
10729   method = make_node (FUNCTION_TYPE);
10730   TYPE_ARG_TYPES (method) = atl;
10731   signature = build_java_argument_signature (method);
10732   dup = xstrdup (lang_printable_name (class, 0));
10733   parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10734                        (lc ? "constructor" : "method"),
10735                        (lc ? dup : IDENTIFIER_POINTER (name)),
10736                        IDENTIFIER_POINTER (signature), dup,
10737                        (candidates ? candidates : ""));
10738   free (dup);
10739   return NULL_TREE;
10740 }
10741
10742 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10743    when we're looking for a constructor. */
10744
10745 static tree
10746 find_applicable_accessible_methods_list (int lc, tree class, tree name,
10747                                          tree arglist)
10748 {
10749   static htab_t searched_classes;
10750   static int search_not_done = 0;
10751   tree list = NULL_TREE, all_list = NULL_TREE;
10752
10753   /* Check the hash table to determine if this class has been searched
10754      already. */
10755   if (searched_classes)
10756     {
10757       if (htab_find (searched_classes, class) != NULL)
10758         return NULL;
10759     }
10760   else
10761     {
10762       searched_classes = htab_create (10, htab_hash_pointer,
10763                                       htab_eq_pointer, NULL);
10764     }
10765
10766   search_not_done++;
10767   *htab_find_slot (searched_classes, class, INSERT) = class;
10768
10769   if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10770     {
10771       load_class (class, 1);
10772       safe_layout_class (class);
10773     }
10774
10775   /* Search interfaces */
10776   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10777       && CLASS_INTERFACE (TYPE_NAME (class)))
10778     {
10779       int i, n;
10780       tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10781       search_applicable_methods_list (lc, TYPE_METHODS (class),
10782                                       name, arglist, &list, &all_list);
10783       n = TREE_VEC_LENGTH (basetype_vec);
10784       for (i = 1; i < n; i++)
10785         {
10786           tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10787           tree rlist;
10788
10789           rlist = find_applicable_accessible_methods_list (lc,  t, name,
10790                                                            arglist);
10791           list = chainon (rlist, list);
10792         }
10793     }
10794   /* Search classes */
10795   else
10796     {
10797       search_applicable_methods_list (lc, TYPE_METHODS (class),
10798                                       name, arglist, &list, &all_list);
10799
10800       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10801          that we only search in class. Note that we should have found
10802          something at this point. */
10803       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10804         {
10805           lc = 1;
10806           if (!list)
10807             abort ();
10808         }
10809
10810       /* We must search all interfaces of this class */
10811       if (!lc)
10812       {
10813         tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10814         int n = TREE_VEC_LENGTH (basetype_vec), i;
10815         for (i = 1; i < n; i++)
10816           {
10817             tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10818             if (t != object_type_node)
10819               {
10820                 tree rlist
10821                   = find_applicable_accessible_methods_list (lc, t,
10822                                                              name, arglist);
10823                 list = chainon (rlist, list);
10824               }
10825           }
10826       }
10827
10828       /* Search superclass */
10829       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10830         {
10831           tree rlist;
10832           class = CLASSTYPE_SUPER (class);
10833           rlist = find_applicable_accessible_methods_list (lc, class,
10834                                                            name, arglist);
10835           list = chainon (rlist, list);
10836         }
10837     }
10838
10839   search_not_done--;
10840
10841   /* We're done. Reset the searched classes list and finally search
10842      java.lang.Object if it wasn't searched already. */
10843   if (!search_not_done)
10844     {
10845       if (!lc
10846           && TYPE_METHODS (object_type_node)
10847           && htab_find (searched_classes, object_type_node) == NULL)
10848         {
10849           search_applicable_methods_list (lc,
10850                                           TYPE_METHODS (object_type_node),
10851                                           name, arglist, &list, &all_list);
10852         }
10853       htab_delete (searched_classes);
10854       searched_classes = NULL;
10855     }
10856
10857   /* Either return the list obtained or all selected (but
10858      inaccessible) methods for better error report. */
10859   return (!list ? all_list : list);
10860 }
10861
10862 /* Effectively search for the appropriate method in method */
10863
10864 static void
10865 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
10866                                 tree *list, tree *all_list)
10867 {
10868   for (; method; method = TREE_CHAIN (method))
10869     {
10870       /* When dealing with constructor, stop here, otherwise search
10871          other classes */
10872       if (lc && !DECL_CONSTRUCTOR_P (method))
10873         continue;
10874       else if (!lc && (DECL_CONSTRUCTOR_P (method)
10875                        || (DECL_NAME (method) != name)))
10876         continue;
10877
10878       if (argument_types_convertible (method, arglist))
10879         {
10880           /* Retain accessible methods only */
10881           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10882                                  method, NULL_TREE, 0))
10883             *list = tree_cons (NULL_TREE, method, *list);
10884           else
10885             /* Also retain all selected method here */
10886             *all_list = tree_cons (NULL_TREE, method, *list);
10887         }
10888     }
10889 }
10890
10891 /* 15.11.2.2 Choose the Most Specific Method */
10892
10893 static tree
10894 find_most_specific_methods_list (tree list)
10895 {
10896   int max = 0;
10897   int abstract, candidates;
10898   tree current, new_list = NULL_TREE;
10899   for (current = list; current; current = TREE_CHAIN (current))
10900     {
10901       tree method;
10902       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10903
10904       for (method = list; method; method = TREE_CHAIN (method))
10905         {
10906           tree method_v, current_v;
10907           /* Don't test a method against itself */
10908           if (method == current)
10909             continue;
10910
10911           method_v = TREE_VALUE (method);
10912           current_v = TREE_VALUE (current);
10913
10914           /* Compare arguments and location where methods where declared */
10915           if (argument_types_convertible (method_v, current_v))
10916             {
10917               if (valid_method_invocation_conversion_p
10918                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10919                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10920                       && enclosing_context_p (DECL_CONTEXT (method_v),
10921                                               DECL_CONTEXT (current_v))))
10922                 {
10923                   int v = (DECL_SPECIFIC_COUNT (current_v) +=
10924                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10925                   max = (v > max ? v : max);
10926                 }
10927             }
10928         }
10929     }
10930
10931   /* Review the list and select the maximally specific methods */
10932   for (current = list, abstract = -1, candidates = -1;
10933        current; current = TREE_CHAIN (current))
10934     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10935       {
10936         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10937         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10938         candidates++;
10939       }
10940
10941   /* If we have several and they're all abstract, just pick the
10942      closest one. */
10943   if (candidates > 0 && (candidates == abstract))
10944     {
10945       new_list = nreverse (new_list);
10946       TREE_CHAIN (new_list) = NULL_TREE;
10947     }
10948
10949   /* We have several (we couldn't find a most specific), all but one
10950      are abstract, we pick the only non abstract one. */
10951   if (candidates > 0 && (candidates == abstract+1))
10952     {
10953       for (current = new_list; current; current = TREE_CHAIN (current))
10954         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10955           {
10956             TREE_CHAIN (current) = NULL_TREE;
10957             new_list = current;
10958           }
10959     }
10960
10961   /* If we can't find one, lower expectations and try to gather multiple
10962      maximally specific methods */
10963   while (!new_list && max)
10964     {
10965       while (--max > 0)
10966         {
10967           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10968             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10969         }
10970     }
10971
10972   return new_list;
10973 }
10974
10975 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10976    converted by method invocation conversion (5.3) to the type of the
10977    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10978    to change less often than M1. */
10979
10980 static GTY(()) tree m2_arg_value;
10981 static GTY(()) tree m2_arg_cache;
10982
10983 static int
10984 argument_types_convertible (tree m1, tree m2_or_arglist)
10985 {
10986   register tree m1_arg, m2_arg;
10987
10988   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10989
10990   if (m2_arg_value == m2_or_arglist)
10991     m2_arg = m2_arg_cache;
10992   else
10993     {
10994       /* M2_OR_ARGLIST can be a function DECL or a raw list of
10995          argument types */
10996       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10997         {
10998           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10999           if (!METHOD_STATIC (m2_or_arglist))
11000             m2_arg = TREE_CHAIN (m2_arg);
11001         }
11002       else
11003         m2_arg = m2_or_arglist;
11004
11005       m2_arg_value = m2_or_arglist;
11006       m2_arg_cache = m2_arg;
11007     }
11008
11009   while (m1_arg != end_params_node && m2_arg != end_params_node)
11010     {
11011       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11012       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11013                                                  TREE_VALUE (m2_arg)))
11014         break;
11015       m1_arg = TREE_CHAIN (m1_arg);
11016       m2_arg = TREE_CHAIN (m2_arg);
11017     }
11018   return m1_arg == end_params_node && m2_arg == end_params_node;
11019 }
11020
11021 /* Qualification routines */
11022
11023 static void
11024 qualify_ambiguous_name (tree id)
11025 {
11026   tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11027     saved_current_class;
11028   int again, super_found = 0, this_found = 0, new_array_found = 0;
11029   int code;
11030
11031   /* We first qualify the first element, then derive qualification of
11032      others based on the first one. If the first element is qualified
11033      by a resolution (field or type), this resolution is stored in the
11034      QUAL_RESOLUTION of the qual element being examined. We need to
11035      save the current_class since the use of SUPER might change the
11036      its value. */
11037   saved_current_class = current_class;
11038   qual = EXPR_WFL_QUALIFICATION (id);
11039   do {
11040
11041     /* Simple qualified expression feature a qual_wfl that is a
11042        WFL. Expression derived from a primary feature more complicated
11043        things like a CALL_EXPR. Expression from primary need to be
11044        worked out to extract the part on which the qualification will
11045        take place. */
11046     qual_wfl = QUAL_WFL (qual);
11047     switch (TREE_CODE (qual_wfl))
11048       {
11049       case CALL_EXPR:
11050         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11051         if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION
11052             || (EXPR_WFL_QUALIFICATION (qual_wfl)
11053                 && TREE_CODE (EXPR_WFL_QUALIFICATION (qual_wfl)) == TREE_LIST))
11054           {
11055             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11056             qual_wfl = QUAL_WFL (qual);
11057           }
11058         break;
11059       case NEW_ARRAY_EXPR:
11060       case NEW_ANONYMOUS_ARRAY_EXPR:
11061         qual = TREE_CHAIN (qual);
11062         again = new_array_found = 1;
11063         continue;
11064       case CONVERT_EXPR:
11065         break;
11066       case NEW_CLASS_EXPR:
11067         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11068         break;
11069       case ARRAY_REF:
11070         while (TREE_CODE (qual_wfl) == ARRAY_REF)
11071           qual_wfl = TREE_OPERAND (qual_wfl, 0);
11072         break;
11073       case STRING_CST:
11074         qual = TREE_CHAIN (qual);
11075         qual_wfl = QUAL_WFL (qual);
11076         break;
11077       case CLASS_LITERAL:
11078         qual = TREE_CHAIN (qual);
11079         qual_wfl = QUAL_WFL (qual);
11080       break;
11081       default:
11082         /* Fix for -Wall. Just break doing nothing */
11083         break;
11084       }
11085
11086     ptr_type = current_class;
11087     again = 0;
11088     code = TREE_CODE (qual_wfl);
11089
11090     /* Pos evaluation: non WFL leading expression nodes */
11091     if (code == CONVERT_EXPR
11092         && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11093       name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11094
11095     else if (code == INTEGER_CST)
11096       name = qual_wfl;
11097
11098     else if (code == CONVERT_EXPR &&
11099              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11100       name = TREE_OPERAND (qual_wfl, 0);
11101
11102     else if (code == CONVERT_EXPR
11103              && TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == CALL_EXPR
11104              && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0))
11105                  == EXPR_WITH_FILE_LOCATION))
11106       name = TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0);
11107
11108     else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11109              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11110       name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11111
11112     else if (code == TREE_LIST)
11113       name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11114
11115     else if (code == STRING_CST || code == CONDITIONAL_EXPR
11116              || code == PLUS_EXPR)
11117       {
11118         qual = TREE_CHAIN (qual);
11119         qual_wfl = QUAL_WFL (qual);
11120         again = 1;
11121       }
11122     else
11123       {
11124         name = EXPR_WFL_NODE (qual_wfl);
11125         if (!name)
11126           {
11127             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11128             again = 1;
11129           }
11130       }
11131
11132     /* If we have a THIS (from a primary), we set the context accordingly */
11133     if (name == this_identifier_node)
11134       {
11135         /* This isn't really elegant. One more added irregularity
11136            before I start using COMPONENT_REF (hopefully very soon.)  */
11137         if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11138             && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11139                EXPR_WITH_FILE_LOCATION
11140             && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11141                this_identifier_node)
11142             {
11143               qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11144               qual = EXPR_WFL_QUALIFICATION (qual);
11145             }
11146         qual = TREE_CHAIN (qual);
11147         qual_wfl = QUAL_WFL (qual);
11148         if (TREE_CODE (qual_wfl) == CALL_EXPR)
11149           again = 1;
11150         else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11151           name = EXPR_WFL_NODE (qual_wfl);
11152         else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11153           name = TREE_OPERAND (qual_wfl, 0);
11154         this_found = 1;
11155       }
11156     /* If we have a SUPER, we set the context accordingly */
11157     if (name == super_identifier_node)
11158       {
11159         current_class = CLASSTYPE_SUPER (ptr_type);
11160         /* Check that there is such a thing as a super class. If not,
11161            return.  The error will be caught later on, during the
11162            resolution */
11163         if (!current_class)
11164           {
11165             current_class = saved_current_class;
11166             return;
11167           }
11168         qual = TREE_CHAIN (qual);
11169         /* Do one more interation to set things up */
11170         super_found = again = 1;
11171       }
11172   } while (again);
11173
11174   /* If name appears within the scope of a local variable declaration
11175      or parameter declaration, then it is an expression name. We don't
11176      carry this test out if we're in the context of the use of SUPER
11177      or THIS */
11178   if (!this_found && !super_found
11179       && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11180       && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11181     {
11182       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11183       QUAL_RESOLUTION (qual) = decl;
11184     }
11185
11186   /* If within the class/interface NAME was found to be used there
11187      exists a (possibly inherited) field named NAME, then this is an
11188      expression name. If we saw a NEW_ARRAY_EXPR before and want to
11189      address length, it is OK. */
11190   else if ((decl = lookup_field_wrapper (ptr_type, name))
11191            || name == length_identifier_node)
11192     {
11193       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11194       QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11195     }
11196
11197   /* We reclassify NAME as yielding to a type name resolution if:
11198      - NAME is a class/interface declared within the compilation
11199        unit containing NAME,
11200      - NAME is imported via a single-type-import declaration,
11201      - NAME is declared in an another compilation unit of the package
11202        of the compilation unit containing NAME,
11203      - NAME is declared by exactly on type-import-on-demand declaration
11204      of the compilation unit containing NAME.
11205      - NAME is actually a STRING_CST.
11206      This can't happen if the expression was qualified by `this.' */
11207   else if (! this_found &&
11208            (TREE_CODE (name) == STRING_CST ||
11209             TREE_CODE (name) == INTEGER_CST ||
11210             (decl = resolve_and_layout (name, NULL_TREE))))
11211     {
11212       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11213       QUAL_RESOLUTION (qual) = decl;
11214     }
11215
11216   /* Method call, array references and cast are expression name */
11217   else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11218            || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11219            || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11220            || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11221     RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11222
11223   /* Check here that NAME isn't declared by more than one
11224      type-import-on-demand declaration of the compilation unit
11225      containing NAME. FIXME */
11226
11227   /* Otherwise, NAME is reclassified as a package name */
11228   else
11229     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11230
11231   /* Propagate the qualification accross other components of the
11232      qualified name */
11233   for (qual = TREE_CHAIN (qual); qual;
11234        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11235     {
11236       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11237         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11238       else
11239         RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11240     }
11241
11242   /* Store the global qualification for the ambiguous part of ID back
11243      into ID fields */
11244   if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11245     RESOLVE_EXPRESSION_NAME_P (id) = 1;
11246   else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11247     RESOLVE_TYPE_NAME_P (id) = 1;
11248   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11249     RESOLVE_PACKAGE_NAME_P (id) = 1;
11250
11251   /* Restore the current class */
11252   current_class = saved_current_class;
11253 }
11254
11255 static int
11256 breakdown_qualified (tree *left, tree *right, tree source)
11257 {
11258   char *p, *base;
11259   int l = IDENTIFIER_LENGTH (source);
11260
11261   base = alloca (l + 1);
11262   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11263
11264   /* Breakdown NAME into REMAINDER . IDENTIFIER.  */
11265   p = base + l - 1;
11266   while (*p != '.' && p != base)
11267     p--;
11268
11269   /* We didn't find a '.'. Return an error.  */
11270   if (p == base)
11271     return 1;
11272
11273   *p = '\0';
11274   if (right)
11275     *right = get_identifier (p+1);
11276   *left = get_identifier (base);
11277
11278   return 0;
11279 }
11280
11281 /* Return TRUE if two classes are from the same package. */
11282
11283 static int
11284 in_same_package (tree name1, tree name2)
11285 {
11286   tree tmp;
11287   tree pkg1;
11288   tree pkg2;
11289
11290   if (TREE_CODE (name1) == TYPE_DECL)
11291     name1 = DECL_NAME (name1);
11292   if (TREE_CODE (name2) == TYPE_DECL)
11293     name2 = DECL_NAME (name2);
11294
11295   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11296     /* One in empty package. */
11297     return 0;
11298
11299   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11300     /* Both in empty package. */
11301     return 1;
11302
11303   breakdown_qualified (&pkg1, &tmp, name1);
11304   breakdown_qualified (&pkg2, &tmp, name2);
11305
11306   return (pkg1 == pkg2);
11307 }
11308
11309 /* Patch tree nodes in a function body. When a BLOCK is found, push
11310    local variable decls if present.
11311    Same as java_complete_lhs, but does resolve static finals to values. */
11312
11313 static tree
11314 java_complete_tree (tree node)
11315 {
11316   node = java_complete_lhs (node);
11317   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11318       && DECL_INITIAL (node) != NULL_TREE
11319       && !flag_emit_xref)
11320     {
11321       tree value = fold_constant_for_init (node, node);
11322       if (value != NULL_TREE)
11323         return value;
11324     }
11325   return node;
11326 }
11327
11328 static tree
11329 java_stabilize_reference (tree node)
11330 {
11331   if (TREE_CODE (node) == COMPOUND_EXPR)
11332     {
11333       tree op0 = TREE_OPERAND (node, 0);
11334       tree op1 = TREE_OPERAND (node, 1);
11335       TREE_OPERAND (node, 0) = save_expr (op0);
11336       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11337       return node;
11338     }
11339   return stabilize_reference (node);
11340 }
11341
11342 /* Patch tree nodes in a function body. When a BLOCK is found, push
11343    local variable decls if present.
11344    Same as java_complete_tree, but does not resolve static finals to values. */
11345
11346 static tree
11347 java_complete_lhs (tree node)
11348 {
11349   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11350   int flag;
11351
11352   /* CONVERT_EXPR always has its type set, even though it needs to be
11353      worked out. */
11354   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11355     return node;
11356
11357   /* The switch block implements cases processing container nodes
11358      first.  Contained nodes are always written back. Leaves come
11359      next and return a value. */
11360   switch (TREE_CODE (node))
11361     {
11362     case BLOCK:
11363
11364       /* 1- Block section.
11365          Set the local values on decl names so we can identify them
11366          faster when they're referenced. At that stage, identifiers
11367          are legal so we don't check for declaration errors. */
11368       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11369         {
11370           DECL_CONTEXT (cn) = current_function_decl;
11371           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11372         }
11373       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11374           CAN_COMPLETE_NORMALLY (node) = 1;
11375       else
11376         {
11377           tree stmt = BLOCK_EXPR_BODY (node);
11378           tree *ptr;
11379           int error_seen = 0;
11380           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11381             {
11382               /* Re-order from (((A; B); C); ...; Z) to
11383                  (A; (B; (C ; (...; Z)))).
11384                  This makes it easier to scan the statements left-to-right
11385                  without using recursion (which might overflow the stack
11386                  if the block has many statements. */
11387               for (;;)
11388                 {
11389                   tree left = TREE_OPERAND (stmt, 0);
11390                   if (TREE_CODE (left) != COMPOUND_EXPR)
11391                     break;
11392                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11393                   TREE_OPERAND (left, 1) = stmt;
11394                   stmt = left;
11395                 }
11396               BLOCK_EXPR_BODY (node) = stmt;
11397             }
11398
11399           /* Now do the actual complete, without deep recursion for
11400              long blocks. */
11401           ptr = &BLOCK_EXPR_BODY (node);
11402           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11403                  && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11404             {
11405               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11406               tree *next = &TREE_OPERAND (*ptr, 1);
11407               TREE_OPERAND (*ptr, 0) = cur;
11408               if (cur == empty_stmt_node)
11409                 {
11410                   /* Optimization;  makes it easier to detect empty bodies.
11411                      Most useful for <clinit> with all-constant initializer. */
11412                   *ptr = *next;
11413                   continue;
11414                 }
11415               if (TREE_CODE (cur) == ERROR_MARK)
11416                 error_seen++;
11417               else if (! CAN_COMPLETE_NORMALLY (cur))
11418                 {
11419                   wfl_op2 = *next;
11420                   for (;;)
11421                     {
11422                       if (TREE_CODE (wfl_op2) == BLOCK)
11423                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11424                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11425                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11426                       else
11427                         break;
11428                     }
11429                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11430                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11431                     unreachable_stmt_error (*ptr);
11432                 }
11433               ptr = next;
11434             }
11435           *ptr = java_complete_tree (*ptr);
11436
11437           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11438             return error_mark_node;
11439           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11440         }
11441       /* Turn local bindings to null */
11442       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11443         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11444
11445       TREE_TYPE (node) = void_type_node;
11446       break;
11447
11448       /* 2- They are expressions but ultimately deal with statements */
11449
11450     case THROW_EXPR:
11451       wfl_op1 = TREE_OPERAND (node, 0);
11452       COMPLETE_CHECK_OP_0 (node);
11453       /* 14.19 A throw statement cannot complete normally. */
11454       CAN_COMPLETE_NORMALLY (node) = 0;
11455       return patch_throw_statement (node, wfl_op1);
11456
11457     case SYNCHRONIZED_EXPR:
11458       wfl_op1 = TREE_OPERAND (node, 0);
11459       return patch_synchronized_statement (node, wfl_op1);
11460
11461     case TRY_EXPR:
11462       return patch_try_statement (node);
11463
11464     case TRY_FINALLY_EXPR:
11465       COMPLETE_CHECK_OP_0 (node);
11466       COMPLETE_CHECK_OP_1 (node);
11467       if (TREE_OPERAND (node, 0) == empty_stmt_node)
11468         return TREE_OPERAND (node, 1);
11469       if (TREE_OPERAND (node, 1) == empty_stmt_node)
11470         return TREE_OPERAND (node, 0);
11471       CAN_COMPLETE_NORMALLY (node)
11472         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11473            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11474       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11475       return node;
11476
11477     case LABELED_BLOCK_EXPR:
11478       PUSH_LABELED_BLOCK (node);
11479       if (LABELED_BLOCK_BODY (node))
11480         COMPLETE_CHECK_OP_1 (node);
11481       TREE_TYPE (node) = void_type_node;
11482       POP_LABELED_BLOCK ();
11483
11484       if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11485         {
11486           LABELED_BLOCK_BODY (node) = NULL_TREE;
11487           CAN_COMPLETE_NORMALLY (node) = 1;
11488         }
11489       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11490         CAN_COMPLETE_NORMALLY (node) = 1;
11491       return node;
11492
11493     case EXIT_BLOCK_EXPR:
11494       /* We don't complete operand 1, because it's the return value of
11495          the EXIT_BLOCK_EXPR which doesn't exist it Java */
11496       return patch_bc_statement (node);
11497
11498     case CASE_EXPR:
11499       cn = java_complete_tree (TREE_OPERAND (node, 0));
11500       if (cn == error_mark_node)
11501         return cn;
11502
11503       /* First, the case expression must be constant. Values of final
11504          fields are accepted. */
11505       cn = fold (cn);
11506       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11507           && JDECL_P (TREE_OPERAND (cn, 1))
11508           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11509           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11510         {
11511           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11512                                        TREE_OPERAND (cn, 1));
11513         }
11514       /* Accept final locals too. */
11515       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11516         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11517
11518       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11519         {
11520           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11521           parse_error_context (node, "Constant expression required");
11522           return error_mark_node;
11523         }
11524
11525       nn = ctxp->current_loop;
11526
11527       /* It must be assignable to the type of the switch expression. */
11528       if (!try_builtin_assignconv (NULL_TREE,
11529                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11530         {
11531           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11532           parse_error_context
11533             (wfl_operator,
11534              "Incompatible type for case. Can't convert `%s' to `int'",
11535              lang_printable_name (TREE_TYPE (cn), 0));
11536           return error_mark_node;
11537         }
11538
11539       cn = fold (convert (int_type_node, cn));
11540       TREE_CONSTANT_OVERFLOW (cn) = 0;
11541       CAN_COMPLETE_NORMALLY (cn) = 1;
11542
11543       /* Save the label on a list so that we can later check for
11544          duplicates.  */
11545       case_label_list = tree_cons (node, cn, case_label_list);
11546
11547       /* Multiple instance of a case label bearing the same value is
11548          checked later. The case expression is all right so far. */
11549       if (TREE_CODE (cn) == VAR_DECL)
11550         cn = DECL_INITIAL (cn);
11551       TREE_OPERAND (node, 0) = cn;
11552       TREE_TYPE (node) = void_type_node;
11553       CAN_COMPLETE_NORMALLY (node) = 1;
11554       TREE_SIDE_EFFECTS (node) = 1;
11555       break;
11556
11557     case DEFAULT_EXPR:
11558       nn = ctxp->current_loop;
11559       /* Only one default label is allowed per switch statement */
11560       if (SWITCH_HAS_DEFAULT (nn))
11561         {
11562           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11563           parse_error_context (wfl_operator,
11564                                "Duplicate case label: `default'");
11565           return error_mark_node;
11566         }
11567       else
11568         SWITCH_HAS_DEFAULT (nn) = 1;
11569       TREE_TYPE (node) = void_type_node;
11570       TREE_SIDE_EFFECTS (node) = 1;
11571       CAN_COMPLETE_NORMALLY (node) = 1;
11572       break;
11573
11574     case SWITCH_EXPR:
11575     case LOOP_EXPR:
11576       PUSH_LOOP (node);
11577       /* Check whether the loop was enclosed in a labeled
11578          statement. If not, create one, insert the loop in it and
11579          return the node */
11580       nn = patch_loop_statement (node);
11581
11582       /* Anyways, walk the body of the loop */
11583       if (TREE_CODE (node) == LOOP_EXPR)
11584         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11585       /* Switch statement: walk the switch expression and the cases */
11586       else
11587         node = patch_switch_statement (node);
11588
11589       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11590         nn = error_mark_node;
11591       else
11592         {
11593           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11594           /* If we returned something different, that's because we
11595              inserted a label. Pop the label too. */
11596           if (nn != node)
11597             {
11598               if (CAN_COMPLETE_NORMALLY (node))
11599                 CAN_COMPLETE_NORMALLY (nn) = 1;
11600               POP_LABELED_BLOCK ();
11601             }
11602         }
11603       POP_LOOP ();
11604       return nn;
11605
11606     case EXIT_EXPR:
11607       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11608       return patch_exit_expr (node);
11609
11610     case COND_EXPR:
11611       /* Condition */
11612       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11613       if (TREE_OPERAND (node, 0) == error_mark_node)
11614         return error_mark_node;
11615       /* then-else branches */
11616       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11617       if (TREE_OPERAND (node, 1) == error_mark_node)
11618         return error_mark_node;
11619       TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11620       if (TREE_OPERAND (node, 2) == error_mark_node)
11621         return error_mark_node;
11622       return patch_if_else_statement (node);
11623       break;
11624
11625     case CONDITIONAL_EXPR:
11626       /* Condition */
11627       wfl_op1 = TREE_OPERAND (node, 0);
11628       COMPLETE_CHECK_OP_0 (node);
11629       wfl_op2 = TREE_OPERAND (node, 1);
11630       COMPLETE_CHECK_OP_1 (node);
11631       wfl_op3 = TREE_OPERAND (node, 2);
11632       COMPLETE_CHECK_OP_2 (node);
11633       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11634
11635       /* 3- Expression section */
11636     case COMPOUND_EXPR:
11637       wfl_op2 = TREE_OPERAND (node, 1);
11638       TREE_OPERAND (node, 0) = nn =
11639         java_complete_tree (TREE_OPERAND (node, 0));
11640       if (wfl_op2 == empty_stmt_node)
11641         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11642       else
11643         {
11644           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11645             {
11646               /* An unreachable condition in a do-while statement
11647                  is *not* (technically) an unreachable statement. */
11648               nn = wfl_op2;
11649               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11650                 nn = EXPR_WFL_NODE (nn);
11651               if (TREE_CODE (nn) != EXIT_EXPR)
11652                 {
11653                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11654                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11655                     {
11656                       /* Perhaps this warning should have an
11657                          associated flag.  The code being compiled is
11658                          pedantically correct, but useless.  */
11659                       parse_warning_context (wfl_operator,
11660                                              "Unreachable statement");
11661                     }
11662                   else
11663                     parse_error_context (wfl_operator,
11664                                          "Unreachable statement");
11665                 }
11666             }
11667           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11668           if (TREE_OPERAND (node, 1) == error_mark_node)
11669             return error_mark_node;
11670           /* Even though we might allow the case where the first
11671              operand doesn't return normally, we still should compute
11672              CAN_COMPLETE_NORMALLY correctly.  */
11673           CAN_COMPLETE_NORMALLY (node)
11674             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11675                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11676         }
11677       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11678       break;
11679
11680     case RETURN_EXPR:
11681       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11682       return patch_return (node);
11683
11684     case EXPR_WITH_FILE_LOCATION:
11685       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11686           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11687         {
11688           tree wfl = node;
11689           node = resolve_expression_name (node, NULL);
11690           if (node == error_mark_node)
11691             return node;
11692           /* Keep line number information somewhere were it doesn't
11693              disrupt the completion process. */
11694           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11695             {
11696               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11697               TREE_OPERAND (node, 1) = wfl;
11698             }
11699           CAN_COMPLETE_NORMALLY (node) = 1;
11700         }
11701       else
11702         {
11703           tree body;
11704           int save_lineno = lineno;
11705           lineno = EXPR_WFL_LINENO (node);
11706           body = java_complete_tree (EXPR_WFL_NODE (node));
11707           lineno = save_lineno;
11708           EXPR_WFL_NODE (node) = body;
11709           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11710           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11711           if (body == empty_stmt_node || TREE_CONSTANT (body))
11712             {
11713               /* Makes it easier to constant fold, detect empty bodies. */
11714               return body;
11715             }
11716           if (body == error_mark_node)
11717             {
11718               /* Its important for the evaluation of assignment that
11719                  this mark on the TREE_TYPE is propagated. */
11720               TREE_TYPE (node) = error_mark_node;
11721               return error_mark_node;
11722             }
11723           else
11724             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11725
11726         }
11727       break;
11728
11729     case NEW_ARRAY_EXPR:
11730       /* Patch all the dimensions */
11731       flag = 0;
11732       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11733         {
11734           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11735           tree dim = convert (int_type_node,
11736                               java_complete_tree (TREE_VALUE (cn)));
11737           if (dim == error_mark_node)
11738             {
11739               flag = 1;
11740               continue;
11741             }
11742           else
11743             {
11744               TREE_VALUE (cn) = dim;
11745               /* Setup the location of the current dimension, for
11746                  later error report. */
11747               TREE_PURPOSE (cn) =
11748                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11749               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11750             }
11751         }
11752       /* They complete the array creation expression, if no errors
11753          were found. */
11754       CAN_COMPLETE_NORMALLY (node) = 1;
11755       return (flag ? error_mark_node
11756               : force_evaluation_order (patch_newarray (node)));
11757
11758     case NEW_ANONYMOUS_ARRAY_EXPR:
11759       /* Create the array type if necessary. */
11760       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11761         {
11762           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11763           if (!(type = resolve_type_during_patch (type)))
11764             return error_mark_node;
11765           type = build_array_from_name (type, NULL_TREE,
11766                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11767           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11768         }
11769       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11770                                    ANONYMOUS_ARRAY_INITIALIZER (node));
11771       if (node == error_mark_node)
11772         return error_mark_node;
11773       CAN_COMPLETE_NORMALLY (node) = 1;
11774       return node;
11775
11776     case NEW_CLASS_EXPR:
11777     case CALL_EXPR:
11778       /* Complete function's argument(s) first */
11779       if (complete_function_arguments (node))
11780         return error_mark_node;
11781       else
11782         {
11783           tree decl, wfl = TREE_OPERAND (node, 0);
11784           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11785           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11786                            super_identifier_node);
11787
11788           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11789                                           from_super, 0, &decl);
11790           if (node == error_mark_node)
11791             return error_mark_node;
11792
11793           check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11794           /* If we call this(...), register signature and positions */
11795           if (in_this)
11796             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11797               tree_cons (wfl, decl,
11798                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
11799           CAN_COMPLETE_NORMALLY (node) = 1;
11800           return force_evaluation_order (node);
11801         }
11802
11803     case MODIFY_EXPR:
11804       /* Save potential wfls */
11805       wfl_op1 = TREE_OPERAND (node, 0);
11806       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11807
11808       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11809           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11810           && DECL_INITIAL (nn) != NULL_TREE)
11811         {
11812           tree value;
11813
11814           value = fold_constant_for_init (nn, nn);
11815
11816           /* When we have a primitype type, or a string and we're not
11817              emitting a class file, we actually don't want to generate
11818              anything for the assignment. */
11819           if (value != NULL_TREE &&
11820               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
11821                (TREE_TYPE (value) == string_ptr_type_node &&
11822                 ! flag_emit_class_files)))
11823             {
11824               /* Prepare node for patch_assignment */
11825               TREE_OPERAND (node, 1) = value;
11826               /* Call patch assignment to verify the assignment */
11827               if (patch_assignment (node, wfl_op1) == error_mark_node)
11828                 return error_mark_node;
11829               /* Set DECL_INITIAL properly (a conversion might have
11830                  been decided by patch_assignment) and return the
11831                  empty statement. */
11832               else
11833                 {
11834                   tree patched = patch_string (TREE_OPERAND (node, 1));
11835                   if (patched)
11836                     DECL_INITIAL (nn) = patched;
11837                   else
11838                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11839                   DECL_FIELD_FINAL_IUD (nn) = 1;
11840                   return empty_stmt_node;
11841                 }
11842             }
11843           if (! flag_emit_class_files)
11844             DECL_INITIAL (nn) = NULL_TREE;
11845         }
11846       wfl_op2 = TREE_OPERAND (node, 1);
11847
11848       if (TREE_OPERAND (node, 0) == error_mark_node)
11849         return error_mark_node;
11850
11851       flag = COMPOUND_ASSIGN_P (wfl_op2);
11852       if (flag)
11853         {
11854           /* This might break when accessing outer field from inner
11855              class. TESTME, FIXME */
11856           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11857
11858           /* Hand stabilize the lhs on both places */
11859           TREE_OPERAND (node, 0) = lvalue;
11860           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11861             (flag_emit_class_files ? lvalue : save_expr (lvalue));
11862
11863           /* 15.25.2.a: Left hand is not an array access. FIXME */
11864           /* Now complete the RHS. We write it back later on. */
11865           nn = java_complete_tree (TREE_OPERAND (node, 1));
11866
11867           if ((cn = patch_string (nn)))
11868             nn = cn;
11869
11870           /* The last part of the rewrite for E1 op= E2 is to have
11871              E1 = (T)(E1 op E2), with T being the type of E1. */
11872           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11873                                                TREE_TYPE (lvalue), nn));
11874
11875           /* If the assignment is compound and has reference type,
11876              then ensure the LHS has type String and nothing else.  */
11877           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11878               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11879             parse_error_context (wfl_op2,
11880                                  "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11881                                  lang_printable_name (TREE_TYPE (lvalue), 0));
11882
11883           /* 15.25.2.b: Left hand is an array access. FIXME */
11884         }
11885
11886       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11887          function to complete this RHS. Note that a NEW_ARRAY_INIT
11888          might have been already fully expanded if created as a result
11889          of processing an anonymous array initializer. We avoid doing
11890          the operation twice by testing whether the node already bears
11891          a type. */
11892       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11893         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11894                                    TREE_OPERAND (node, 1));
11895       /* Otherwise we simply complete the RHS */
11896       else
11897         nn = java_complete_tree (TREE_OPERAND (node, 1));
11898
11899       if (nn == error_mark_node)
11900         return error_mark_node;
11901
11902       /* Write back the RHS as we evaluated it. */
11903       TREE_OPERAND (node, 1) = nn;
11904
11905       /* In case we're handling = with a String as a RHS, we need to
11906          produce a String out of the RHS (it might still be a
11907          STRING_CST or a StringBuffer at this stage */
11908       if ((nn = patch_string (TREE_OPERAND (node, 1))))
11909         TREE_OPERAND (node, 1) = nn;
11910
11911       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11912                                         TREE_OPERAND (node, 1))))
11913         {
11914           /* We return error_mark_node if outer_field_access_fix
11915              detects we write into a final. */
11916           if (nn == error_mark_node)
11917             return error_mark_node;
11918           node = nn;
11919         }
11920       else
11921         {
11922           node = patch_assignment (node, wfl_op1);
11923           if (node == error_mark_node)
11924             return error_mark_node;
11925           /* Reorganize the tree if necessary. */
11926           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11927                        || JSTRING_P (TREE_TYPE (node))))
11928             node = java_refold (node);
11929         }
11930
11931       /* Seek to set DECL_INITIAL to a proper value, since it might have
11932          undergone a conversion in patch_assignment. We do that only when
11933          it's necessary to have DECL_INITIAL properly set. */
11934       nn = TREE_OPERAND (node, 0);
11935       if (TREE_CODE (nn) == VAR_DECL
11936           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
11937           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
11938           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
11939               || TREE_TYPE (nn) == string_ptr_type_node))
11940         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11941
11942       CAN_COMPLETE_NORMALLY (node) = 1;
11943       return node;
11944
11945     case MULT_EXPR:
11946     case PLUS_EXPR:
11947     case MINUS_EXPR:
11948     case LSHIFT_EXPR:
11949     case RSHIFT_EXPR:
11950     case URSHIFT_EXPR:
11951     case BIT_AND_EXPR:
11952     case BIT_XOR_EXPR:
11953     case BIT_IOR_EXPR:
11954     case TRUNC_MOD_EXPR:
11955     case TRUNC_DIV_EXPR:
11956     case RDIV_EXPR:
11957     case TRUTH_ANDIF_EXPR:
11958     case TRUTH_ORIF_EXPR:
11959     case EQ_EXPR:
11960     case NE_EXPR:
11961     case GT_EXPR:
11962     case GE_EXPR:
11963     case LT_EXPR:
11964     case LE_EXPR:
11965       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11966          knows how to handle those cases. */
11967       wfl_op1 = TREE_OPERAND (node, 0);
11968       wfl_op2 = TREE_OPERAND (node, 1);
11969
11970       CAN_COMPLETE_NORMALLY (node) = 1;
11971       /* Don't complete string nodes if dealing with the PLUS operand. */
11972       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11973         {
11974           nn = java_complete_tree (wfl_op1);
11975           if (nn == error_mark_node)
11976             return error_mark_node;
11977
11978           TREE_OPERAND (node, 0) = nn;
11979         }
11980       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11981         {
11982           nn = java_complete_tree (wfl_op2);
11983           if (nn == error_mark_node)
11984             return error_mark_node;
11985
11986           TREE_OPERAND (node, 1) = nn;
11987         }
11988       return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11989
11990     case INSTANCEOF_EXPR:
11991       wfl_op1 = TREE_OPERAND (node, 0);
11992       COMPLETE_CHECK_OP_0 (node);
11993       if (flag_emit_xref)
11994         {
11995           TREE_TYPE (node) = boolean_type_node;
11996           return node;
11997         }
11998       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11999
12000     case UNARY_PLUS_EXPR:
12001     case NEGATE_EXPR:
12002     case TRUTH_NOT_EXPR:
12003     case BIT_NOT_EXPR:
12004     case PREDECREMENT_EXPR:
12005     case PREINCREMENT_EXPR:
12006     case POSTDECREMENT_EXPR:
12007     case POSTINCREMENT_EXPR:
12008     case CONVERT_EXPR:
12009       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12010          how to handle those cases. */
12011       wfl_op1 = TREE_OPERAND (node, 0);
12012       CAN_COMPLETE_NORMALLY (node) = 1;
12013       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12014       if (TREE_OPERAND (node, 0) == error_mark_node)
12015         return error_mark_node;
12016       node = patch_unaryop (node, wfl_op1);
12017       CAN_COMPLETE_NORMALLY (node) = 1;
12018       break;
12019
12020     case ARRAY_REF:
12021       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12022          how to handle those cases. */
12023       wfl_op1 = TREE_OPERAND (node, 0);
12024       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12025       if (TREE_OPERAND (node, 0) == error_mark_node)
12026         return error_mark_node;
12027       if (!flag_emit_class_files && !flag_emit_xref)
12028         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12029       /* The same applies to wfl_op2 */
12030       wfl_op2 = TREE_OPERAND (node, 1);
12031       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12032       if (TREE_OPERAND (node, 1) == error_mark_node)
12033         return error_mark_node;
12034       if (!flag_emit_class_files && !flag_emit_xref)
12035         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12036       return patch_array_ref (node);
12037
12038     case RECORD_TYPE:
12039       return node;;
12040
12041     case COMPONENT_REF:
12042       /* The first step in the re-write of qualified name handling.  FIXME.
12043          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12044       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12045       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12046         {
12047           tree name = TREE_OPERAND (node, 1);
12048           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12049           if (field == NULL_TREE)
12050             {
12051               error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12052               return error_mark_node;
12053             }
12054           if (! FIELD_STATIC (field))
12055             {
12056               error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12057               return error_mark_node;
12058             }
12059           return field;
12060         }
12061       else
12062         abort ();
12063       break;
12064
12065     case THIS_EXPR:
12066       /* Can't use THIS in a static environment */
12067       if (!current_this)
12068         {
12069           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12070           parse_error_context (wfl_operator,
12071                                "Keyword `this' used outside allowed context");
12072           TREE_TYPE (node) = error_mark_node;
12073           return error_mark_node;
12074         }
12075       if (ctxp->explicit_constructor_p)
12076         {
12077           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12078           parse_error_context
12079             (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12080           TREE_TYPE (node) = error_mark_node;
12081           return error_mark_node;
12082         }
12083       return current_this;
12084
12085     case CLASS_LITERAL:
12086       CAN_COMPLETE_NORMALLY (node) = 1;
12087       node = patch_incomplete_class_ref (node);
12088       if (node == error_mark_node)
12089         return error_mark_node;
12090       break;
12091
12092     default:
12093       CAN_COMPLETE_NORMALLY (node) = 1;
12094       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12095          and it's time to turn it into the appropriate String object */
12096       if ((nn = patch_string (node)))
12097         node = nn;
12098       else
12099         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12100     }
12101   return node;
12102 }
12103
12104 /* Complete function call's argument. Return a nonzero value is an
12105    error was found.  */
12106
12107 static int
12108 complete_function_arguments (tree node)
12109 {
12110   int flag = 0;
12111   tree cn;
12112
12113   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12114   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12115     {
12116       tree wfl = TREE_VALUE (cn), parm, temp;
12117       parm = java_complete_tree (wfl);
12118
12119       if (parm == error_mark_node)
12120         {
12121           flag = 1;
12122           continue;
12123         }
12124       /* If have a string literal that we haven't transformed yet or a
12125          crafted string buffer, as a result of use of the the String
12126          `+' operator. Build `parm.toString()' and expand it. */
12127       if ((temp = patch_string (parm)))
12128         parm = temp;
12129
12130       TREE_VALUE (cn) = parm;
12131     }
12132   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12133   return flag;
12134 }
12135
12136 /* Sometimes (for loops and variable initialized during their
12137    declaration), we want to wrap a statement around a WFL and turn it
12138    debugable.  */
12139
12140 static tree
12141 build_debugable_stmt (int location, tree stmt)
12142 {
12143   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12144     {
12145       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12146       EXPR_WFL_LINECOL (stmt) = location;
12147     }
12148   JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12149   return stmt;
12150 }
12151
12152 static tree
12153 build_expr_block (tree body, tree decls)
12154
12155 {
12156   tree node = make_node (BLOCK);
12157   BLOCK_EXPR_DECLS (node) = decls;
12158   BLOCK_EXPR_BODY (node) = body;
12159   if (body)
12160     TREE_TYPE (node) = TREE_TYPE (body);
12161   TREE_SIDE_EFFECTS (node) = 1;
12162   return node;
12163 }
12164
12165 /* Create a new function block and link it appropriately to current
12166    function block chain */
12167
12168 static tree
12169 enter_block (void)
12170 {
12171   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12172
12173   /* Link block B supercontext to the previous block. The current
12174      function DECL is used as supercontext when enter_a_block is called
12175      for the first time for a given function. The current function body
12176      (DECL_FUNCTION_BODY) is set to be block B.  */
12177
12178   tree fndecl = current_function_decl;
12179
12180   if (!fndecl) {
12181     BLOCK_SUPERCONTEXT (b) = current_static_block;
12182     current_static_block = b;
12183   }
12184
12185   else if (!DECL_FUNCTION_BODY (fndecl))
12186     {
12187       BLOCK_SUPERCONTEXT (b) = fndecl;
12188       DECL_FUNCTION_BODY (fndecl) = b;
12189     }
12190   else
12191     {
12192       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12193       DECL_FUNCTION_BODY (fndecl) = b;
12194     }
12195   return b;
12196 }
12197
12198 /* Exit a block by changing the current function body
12199    (DECL_FUNCTION_BODY) to the current block super context, only if
12200    the block being exited isn't the method's top level one.  */
12201
12202 static tree
12203 exit_block (void)
12204 {
12205   tree b;
12206   if (current_function_decl)
12207     {
12208       b = DECL_FUNCTION_BODY (current_function_decl);
12209       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12210         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12211     }
12212   else
12213     {
12214       b = current_static_block;
12215
12216       if (BLOCK_SUPERCONTEXT (b))
12217         current_static_block = BLOCK_SUPERCONTEXT (b);
12218     }
12219   return b;
12220 }
12221
12222 /* Lookup for NAME in the nested function's blocks, all the way up to
12223    the current toplevel one. It complies with Java's local variable
12224    scoping rules.  */
12225
12226 static tree
12227 lookup_name_in_blocks (tree name)
12228 {
12229   tree b = GET_CURRENT_BLOCK (current_function_decl);
12230
12231   while (b != current_function_decl)
12232     {
12233       tree current;
12234
12235       /* Paranoid sanity check. To be removed */
12236       if (TREE_CODE (b) != BLOCK)
12237         abort ();
12238
12239       for (current = BLOCK_EXPR_DECLS (b); current;
12240            current = TREE_CHAIN (current))
12241         if (DECL_NAME (current) == name)
12242           return current;
12243       b = BLOCK_SUPERCONTEXT (b);
12244     }
12245   return NULL_TREE;
12246 }
12247
12248 static void
12249 maybe_absorb_scoping_blocks (void)
12250 {
12251   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12252     {
12253       tree b = exit_block ();
12254       java_method_add_stmt (current_function_decl, b);
12255       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12256     }
12257 }
12258
12259 \f
12260 /* This section of the source is reserved to build_* functions that
12261    are building incomplete tree nodes and the patch_* functions that
12262    are completing them.  */
12263
12264 /* Wrap a non WFL node around a WFL.  */
12265
12266 static tree
12267 build_wfl_wrap (tree node, int location)
12268 {
12269   tree wfl, node_to_insert = node;
12270
12271   /* We want to process THIS . xxx symbolicaly, to keep it consistent
12272      with the way we're processing SUPER. A THIS from a primary as a
12273      different form than a SUPER. Turn THIS into something symbolic */
12274   if (TREE_CODE (node) == THIS_EXPR)
12275     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12276   else
12277     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12278
12279   EXPR_WFL_LINECOL (wfl) = location;
12280   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12281   return wfl;
12282 }
12283
12284 /* Build a super() constructor invocation. Returns empty_stmt_node if
12285    we're currently dealing with the class java.lang.Object. */
12286
12287 static tree
12288 build_super_invocation (tree mdecl)
12289 {
12290   if (DECL_CONTEXT (mdecl) == object_type_node)
12291     return empty_stmt_node;
12292   else
12293     {
12294       tree super_wfl = build_wfl_node (super_identifier_node);
12295       tree a = NULL_TREE, t;
12296       /* If we're dealing with an anonymous class, pass the arguments
12297          of the crafted constructor along. */
12298       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12299         {
12300           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12301           for (; t != end_params_node; t = TREE_CHAIN (t))
12302             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12303         }
12304       return build_method_invocation (super_wfl, a);
12305     }
12306 }
12307
12308 /* Build a SUPER/THIS qualified method invocation.  */
12309
12310 static tree
12311 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12312                                        int lloc, int rloc)
12313 {
12314   tree invok;
12315   tree wfl =
12316     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12317   EXPR_WFL_LINECOL (wfl) = lloc;
12318   invok = build_method_invocation (name, args);
12319   return make_qualified_primary (wfl, invok, rloc);
12320 }
12321
12322 /* Build an incomplete CALL_EXPR node. */
12323
12324 static tree
12325 build_method_invocation (tree name, tree args)
12326 {
12327   tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12328   TREE_SIDE_EFFECTS (call) = 1;
12329   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12330   return call;
12331 }
12332
12333 /* Build an incomplete new xxx(...) node. */
12334
12335 static tree
12336 build_new_invocation (tree name, tree args)
12337 {
12338   tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12339   TREE_SIDE_EFFECTS (call) = 1;
12340   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12341   return call;
12342 }
12343
12344 /* Build an incomplete assignment expression. */
12345
12346 static tree
12347 build_assignment (int op, int op_location, tree lhs, tree rhs)
12348 {
12349   tree assignment;
12350   /* Build the corresponding binop if we deal with a Compound
12351      Assignment operator. Mark the binop sub-tree as part of a
12352      Compound Assignment expression */
12353   if (op != ASSIGN_TK)
12354     {
12355       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12356       COMPOUND_ASSIGN_P (rhs) = 1;
12357     }
12358   assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12359   TREE_SIDE_EFFECTS (assignment) = 1;
12360   EXPR_WFL_LINECOL (assignment) = op_location;
12361   return assignment;
12362 }
12363
12364 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12365    the buffer.  This is used only for string conversion.  */
12366 static char *
12367 string_convert_int_cst (tree node)
12368 {
12369   static char buffer[80];
12370
12371   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12372   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12373   char *p = buffer + sizeof (buffer) - 1;
12374   int neg = 0;
12375
12376   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12377                                   << (HOST_BITS_PER_WIDE_INT - 1));
12378
12379   *p-- = '\0';
12380
12381   /* If negative, note the fact and negate the value.  */
12382   if ((hi & hibit))
12383     {
12384       lo = ~lo;
12385       hi = ~hi;
12386       if (++lo == 0)
12387         ++hi;
12388       neg = 1;
12389     }
12390
12391   /* Divide by 10 until there are no bits left.  */
12392   while (hi || lo)
12393     {
12394       unsigned HOST_WIDE_INT acc = 0;
12395       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12396       unsigned int i;
12397
12398       /* Use long division to compute the result and the remainder.  */
12399       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12400         {
12401           /* Shift a bit into accumulator.  */
12402           acc <<= 1;
12403           if ((hi & hibit))
12404             acc |= 1;
12405
12406           /* Shift the value.  */
12407           hi <<= 1;
12408           if ((lo & hibit))
12409             hi |= 1;
12410           lo <<= 1;
12411
12412           /* Shift the correct bit into the result.  */
12413           outhi <<= 1;
12414           if ((outlo & hibit))
12415             outhi |= 1;
12416           outlo <<= 1;
12417           if (acc >= 10)
12418             {
12419               acc -= 10;
12420               outlo |= 1;
12421             }
12422         }
12423
12424       /* FIXME: ASCII assumption.  */
12425       *p-- = '0' + acc;
12426
12427       hi = outhi;
12428       lo = outlo;
12429     }
12430
12431   if (neg)
12432     *p-- = '-';
12433
12434   return p + 1;
12435 }
12436
12437 /* Print an INTEGER_CST node in a static buffer, and return the
12438    buffer.  This is used only for error handling.  */
12439 char *
12440 print_int_node (tree node)
12441 {
12442   static char buffer [80];
12443   if (TREE_CONSTANT_OVERFLOW (node))
12444     sprintf (buffer, "<overflow>");
12445
12446   if (TREE_INT_CST_HIGH (node) == 0)
12447     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12448              TREE_INT_CST_LOW (node));
12449   else if (TREE_INT_CST_HIGH (node) == -1
12450            && TREE_INT_CST_LOW (node) != 0)
12451     {
12452       buffer [0] = '-';
12453       sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12454                -TREE_INT_CST_LOW (node));
12455     }
12456   else
12457     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12458              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12459
12460   return buffer;
12461 }
12462
12463 \f
12464 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12465    context.  */
12466
12467 /* 15.25 Assignment operators. */
12468
12469 static tree
12470 patch_assignment (tree node, tree wfl_op1)
12471 {
12472   tree rhs = TREE_OPERAND (node, 1);
12473   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12474   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12475   int error_found = 0;
12476   int lvalue_from_array = 0;
12477   int is_return = 0;
12478
12479   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12480
12481   /* Lhs can be a named variable */
12482   if (JDECL_P (lvalue))
12483     {
12484       lhs_type = TREE_TYPE (lvalue);
12485     }
12486   /* Or Lhs can be an array access. */
12487   else if (TREE_CODE (lvalue) == ARRAY_REF)
12488     {
12489       lhs_type = TREE_TYPE (lvalue);
12490       lvalue_from_array = 1;
12491     }
12492   /* Or a field access */
12493   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12494     lhs_type = TREE_TYPE (lvalue);
12495   /* Or a function return slot */
12496   else if (TREE_CODE (lvalue) == RESULT_DECL)
12497     {
12498       /* If the return type is an integral type, then we create the
12499          RESULT_DECL with a promoted type, but we need to do these
12500          checks against the unpromoted type to ensure type safety.  So
12501          here we look at the real type, not the type of the decl we
12502          are modifying.  */
12503       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12504       is_return = 1;
12505     }
12506   /* Otherwise, we might want to try to write into an optimized static
12507      final, this is an of a different nature, reported further on. */
12508   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12509            && resolve_expression_name (wfl_op1, &llvalue))
12510     {
12511       lhs_type = TREE_TYPE (lvalue);
12512     }
12513   else
12514     {
12515       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12516       error_found = 1;
12517     }
12518
12519   rhs_type = TREE_TYPE (rhs);
12520
12521   /* 5.1 Try the assignment conversion for builtin type. */
12522   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12523
12524   /* 5.2 If it failed, try a reference conversion */
12525   if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12526     lhs_type = promote_type (rhs_type);
12527
12528   /* 15.25.2 If we have a compound assignment, convert RHS into the
12529      type of the LHS */
12530   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12531     new_rhs = convert (lhs_type, rhs);
12532
12533   /* Explicit cast required. This is an error */
12534   if (!new_rhs)
12535     {
12536       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12537       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12538       tree wfl;
12539       char operation [32];      /* Max size known */
12540
12541       /* If the assignment is part of a declaration, we use the WFL of
12542          the declared variable to point out the error and call it a
12543          declaration problem. If the assignment is a genuine =
12544          operator, we call is a operator `=' problem, otherwise we
12545          call it an assignment problem. In both of these last cases,
12546          we use the WFL of the operator to indicate the error. */
12547
12548       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12549         {
12550           wfl = wfl_op1;
12551           strcpy (operation, "declaration");
12552         }
12553       else
12554         {
12555           wfl = wfl_operator;
12556           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12557             strcpy (operation, "assignment");
12558           else if (is_return)
12559             strcpy (operation, "`return'");
12560           else
12561             strcpy (operation, "`='");
12562         }
12563
12564       if (!valid_cast_to_p (rhs_type, lhs_type))
12565         parse_error_context
12566           (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12567            operation, t1, t2);
12568       else
12569         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12570                              operation, t1, t2);
12571       free (t1); free (t2);
12572       error_found = 1;
12573     }
12574
12575   if (error_found)
12576     return error_mark_node;
12577
12578   /* If we're processing a `return' statement, promote the actual type
12579      to the promoted type.  */
12580   if (is_return)
12581     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12582
12583   /* 10.10: Array Store Exception runtime check */
12584   if (!flag_emit_class_files
12585       && !flag_emit_xref
12586       && lvalue_from_array
12587       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12588     {
12589       tree array, store_check, base, index_expr;
12590
12591       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12592       new_rhs = save_expr (new_rhs);
12593
12594       /* Get the INDIRECT_REF. */
12595       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12596       /* Get the array pointer expr. */
12597       array = TREE_OPERAND (array, 0);
12598       store_check = build_java_arraystore_check (array, new_rhs);
12599
12600       index_expr = TREE_OPERAND (lvalue, 1);
12601
12602       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12603         {
12604           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12605              happen before the store check, so prepare to insert the store
12606              check within the second operand of the existing COMPOUND_EXPR. */
12607           base = index_expr;
12608         }
12609       else
12610         base = lvalue;
12611
12612       index_expr = TREE_OPERAND (base, 1);
12613       TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr),
12614                                       store_check, index_expr);
12615     }
12616
12617   /* Final locals can be used as case values in switch
12618      statement. Prepare them for this eventuality. */
12619   if (TREE_CODE (lvalue) == VAR_DECL
12620       && DECL_FINAL (lvalue)
12621       && TREE_CONSTANT (new_rhs)
12622       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12623       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12624       )
12625     {
12626       TREE_CONSTANT (lvalue) = 1;
12627       DECL_INITIAL (lvalue) = new_rhs;
12628     }
12629
12630   /* Copy the rhs if it's a reference.  */
12631   if (! flag_check_references && optimize > 0)
12632     {
12633       switch (TREE_CODE (new_rhs))
12634         {
12635         case ARRAY_REF:
12636         case INDIRECT_REF:
12637         case COMPONENT_REF:
12638           /* Transform a = foo.bar 
12639              into a = { int tmp; tmp = foo.bar; tmp; ).              
12640              We need to ensure that if a read from memory fails
12641              because of a NullPointerException, a destination variable
12642              will remain unchanged.  An explicit temporary does what
12643              we need.  
12644
12645              If flag_check_references is set, this is unnecessary
12646              because we'll check each reference before doing any
12647              reads.  If optimize is not set the result will never be
12648              written to a stack slot that contains the LHS.  */
12649           {
12650             tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), 
12651                                    TREE_TYPE (new_rhs));
12652             tree block = build (BLOCK, TREE_TYPE (new_rhs), NULL);
12653             tree assignment 
12654               = build (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12655             BLOCK_VARS (block) = tmp;
12656             BLOCK_EXPR_BODY (block) 
12657               = build (COMPOUND_EXPR, TREE_TYPE (new_rhs), assignment, tmp);
12658             TREE_SIDE_EFFECTS (block) = 1;
12659             new_rhs = block;
12660           }
12661           break;
12662         default:
12663           break;
12664         }
12665     }
12666
12667   TREE_OPERAND (node, 0) = lvalue;
12668   TREE_OPERAND (node, 1) = new_rhs;
12669   TREE_TYPE (node) = lhs_type;
12670   return node;
12671 }
12672
12673 /* Check that type SOURCE can be cast into type DEST. If the cast
12674    can't occur at all, return NULL; otherwise, return a possibly
12675    modified rhs.  */
12676
12677 static tree
12678 try_reference_assignconv (tree lhs_type, tree rhs)
12679 {
12680   tree new_rhs = NULL_TREE;
12681   tree rhs_type = TREE_TYPE (rhs);
12682
12683   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12684     {
12685       /* `null' may be assigned to any reference type */
12686       if (rhs == null_pointer_node)
12687         new_rhs = null_pointer_node;
12688       /* Try the reference assignment conversion */
12689       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12690         new_rhs = rhs;
12691       /* This is a magic assignment that we process differently */
12692       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12693         new_rhs = rhs;
12694     }
12695   return new_rhs;
12696 }
12697
12698 /* Check that RHS can be converted into LHS_TYPE by the assignment
12699    conversion (5.2), for the cases of RHS being a builtin type. Return
12700    NULL_TREE if the conversion fails or if because RHS isn't of a
12701    builtin type. Return a converted RHS if the conversion is possible.  */
12702
12703 static tree
12704 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
12705 {
12706   tree new_rhs = NULL_TREE;
12707   tree rhs_type = TREE_TYPE (rhs);
12708
12709   /* Handle boolean specially.  */
12710   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12711       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12712     {
12713       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12714           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12715         new_rhs = rhs;
12716     }
12717
12718   /* 5.1.1 Try Identity Conversion,
12719      5.1.2 Try Widening Primitive Conversion */
12720   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12721     new_rhs = convert (lhs_type, rhs);
12722
12723   /* Try a narrowing primitive conversion (5.1.3):
12724        - expression is a constant expression of type byte, short, char,
12725          or int, AND
12726        - variable is byte, short or char AND
12727        - The value of the expression is representable in the type of the
12728          variable */
12729   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12730             || rhs_type == char_type_node || rhs_type == int_type_node)
12731             && TREE_CONSTANT (rhs)
12732            && (lhs_type == byte_type_node || lhs_type == char_type_node
12733                || lhs_type == short_type_node))
12734     {
12735       if (int_fits_type_p (rhs, lhs_type))
12736         new_rhs = convert (lhs_type, rhs);
12737       else if (wfl_op1)         /* Might be called with a NULL */
12738         parse_warning_context
12739           (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12740            print_int_node (rhs), lang_printable_name (lhs_type, 0));
12741       /* Reported a warning that will turn into an error further
12742          down, so we don't return */
12743     }
12744
12745   return new_rhs;
12746 }
12747
12748 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12749    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
12750    0 is the conversion test fails.  This implements parts the method
12751    invocation convertion (5.3).  */
12752
12753 static int
12754 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
12755 {
12756   /* 5.1.1: This is the identity conversion part. */
12757   if (lhs_type == rhs_type)
12758     return 1;
12759
12760   /* Reject non primitive types and boolean conversions.  */
12761   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12762     return 0;
12763
12764   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12765      than a char can't be converted into a char. Short can't too, but
12766      the < test below takes care of that */
12767   if (lhs_type == char_type_node && rhs_type == byte_type_node)
12768     return 0;
12769
12770   /* Accept all promoted type here. Note, we can't use <= in the test
12771      below, because we still need to bounce out assignments of short
12772      to char and the likes */
12773   if (lhs_type == int_type_node
12774       && (rhs_type == promoted_byte_type_node
12775           || rhs_type == promoted_short_type_node
12776           || rhs_type == promoted_char_type_node
12777           || rhs_type == promoted_boolean_type_node))
12778     return 1;
12779
12780   /* From here, an integral is widened if its precision is smaller
12781      than the precision of the LHS or if the LHS is a floating point
12782      type, or the RHS is a float and the RHS a double. */
12783   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12784        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12785       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12786       || (rhs_type == float_type_node && lhs_type == double_type_node))
12787     return 1;
12788
12789   return 0;
12790 }
12791
12792 /* Check that something of SOURCE type can be assigned or cast to
12793    something of DEST type at runtime. Return 1 if the operation is
12794    valid, 0 otherwise. If CAST is set to 1, we're treating the case
12795    were SOURCE is cast into DEST, which borrows a lot of the
12796    assignment check. */
12797
12798 static int
12799 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
12800 {
12801   /* SOURCE or DEST might be null if not from a declared entity. */
12802   if (!source || !dest)
12803     return 0;
12804   if (JNULLP_TYPE_P (source))
12805     return 1;
12806   if (TREE_CODE (source) == POINTER_TYPE)
12807     source = TREE_TYPE (source);
12808   if (TREE_CODE (dest) == POINTER_TYPE)
12809     dest = TREE_TYPE (dest);
12810
12811   /* If source and dest are being compiled from bytecode, they may need to
12812      be loaded. */
12813   if (CLASS_P (source) && !CLASS_LOADED_P (source))
12814     {
12815       load_class (source, 1);
12816       safe_layout_class (source);
12817     }
12818   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12819     {
12820       load_class (dest, 1);
12821       safe_layout_class (dest);
12822     }
12823
12824   /* Case where SOURCE is a class type */
12825   if (TYPE_CLASS_P (source))
12826     {
12827       if (TYPE_CLASS_P (dest))
12828         return  (source == dest
12829                  || inherits_from_p (source, dest)
12830                  || (cast && inherits_from_p (dest, source)));
12831       if (TYPE_INTERFACE_P (dest))
12832         {
12833           /* If doing a cast and SOURCE is final, the operation is
12834              always correct a compile time (because even if SOURCE
12835              does not implement DEST, a subclass of SOURCE might). */
12836           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12837             return 1;
12838           /* Otherwise, SOURCE must implement DEST */
12839           return interface_of_p (dest, source);
12840         }
12841       /* DEST is an array, cast permited if SOURCE is of Object type */
12842       return (cast && source == object_type_node ? 1 : 0);
12843     }
12844   if (TYPE_INTERFACE_P (source))
12845     {
12846       if (TYPE_CLASS_P (dest))
12847         {
12848           /* If not casting, DEST must be the Object type */
12849           if (!cast)
12850             return dest == object_type_node;
12851           /* We're doing a cast. The cast is always valid is class
12852              DEST is not final, otherwise, DEST must implement SOURCE */
12853           else if (!CLASS_FINAL (TYPE_NAME (dest)))
12854             return 1;
12855           else
12856             return interface_of_p (source, dest);
12857         }
12858       if (TYPE_INTERFACE_P (dest))
12859         {
12860           /* If doing a cast, then if SOURCE and DEST contain method
12861              with the same signature but different return type, then
12862              this is a (compile time) error */
12863           if (cast)
12864             {
12865               tree method_source, method_dest;
12866               tree source_type;
12867               tree source_sig;
12868               tree source_name;
12869               for (method_source = TYPE_METHODS (source); method_source;
12870                    method_source = TREE_CHAIN (method_source))
12871                 {
12872                   source_sig =
12873                     build_java_argument_signature (TREE_TYPE (method_source));
12874                   source_type = TREE_TYPE (TREE_TYPE (method_source));
12875                   source_name = DECL_NAME (method_source);
12876                   for (method_dest = TYPE_METHODS (dest);
12877                        method_dest; method_dest = TREE_CHAIN (method_dest))
12878                     if (source_sig ==
12879                         build_java_argument_signature (TREE_TYPE (method_dest))
12880                         && source_name == DECL_NAME (method_dest)
12881                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12882                       return 0;
12883                 }
12884               return 1;
12885             }
12886           else
12887             return source == dest || interface_of_p (dest, source);
12888         }
12889       else
12890         {
12891           /* Array */
12892           return (cast
12893                   && (DECL_NAME (TYPE_NAME (source))
12894                       == java_lang_cloneable_identifier_node
12895                       || (DECL_NAME (TYPE_NAME (source))
12896                           == java_io_serializable_identifier_node)));
12897         }
12898     }
12899   if (TYPE_ARRAY_P (source))
12900     {
12901       if (TYPE_CLASS_P (dest))
12902         return dest == object_type_node;
12903       /* Can't cast an array to an interface unless the interface is
12904          java.lang.Cloneable or java.io.Serializable.  */
12905       if (TYPE_INTERFACE_P (dest))
12906         return (DECL_NAME (TYPE_NAME (dest))
12907                 == java_lang_cloneable_identifier_node
12908                 || (DECL_NAME (TYPE_NAME (dest))
12909                     == java_io_serializable_identifier_node));
12910       else                      /* Arrays */
12911         {
12912           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12913           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12914
12915           /* In case of severe errors, they turn out null */
12916           if (!dest_element_type || !source_element_type)
12917             return 0;
12918           if (source_element_type == dest_element_type)
12919             return 1;
12920           return valid_ref_assignconv_cast_p (source_element_type,
12921                                               dest_element_type, cast);
12922         }
12923       return 0;
12924     }
12925   return 0;
12926 }
12927
12928 static int
12929 valid_cast_to_p (tree source, tree dest)
12930 {
12931   if (TREE_CODE (source) == POINTER_TYPE)
12932     source = TREE_TYPE (source);
12933   if (TREE_CODE (dest) == POINTER_TYPE)
12934     dest = TREE_TYPE (dest);
12935
12936   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12937     return valid_ref_assignconv_cast_p (source, dest, 1);
12938
12939   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12940     return 1;
12941
12942   else if (TREE_CODE (source) == BOOLEAN_TYPE
12943            && TREE_CODE (dest) == BOOLEAN_TYPE)
12944     return 1;
12945
12946   return 0;
12947 }
12948
12949 static tree
12950 do_unary_numeric_promotion (tree arg)
12951 {
12952   tree type = TREE_TYPE (arg);
12953   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12954       || TREE_CODE (type) == CHAR_TYPE)
12955     arg = convert (int_type_node, arg);
12956   return arg;
12957 }
12958
12959 /* Return a nonzero value if SOURCE can be converted into DEST using
12960    the method invocation conversion rule (5.3).  */
12961 static int
12962 valid_method_invocation_conversion_p (tree dest, tree source)
12963 {
12964   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12965            && valid_builtin_assignconv_identity_widening_p (dest, source))
12966           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12967               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12968               && valid_ref_assignconv_cast_p (source, dest, 0)));
12969 }
12970
12971 /* Build an incomplete binop expression. */
12972
12973 static tree
12974 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
12975 {
12976   tree binop = build (op, NULL_TREE, op1, op2);
12977   TREE_SIDE_EFFECTS (binop) = 1;
12978   /* Store the location of the operator, for better error report. The
12979      string of the operator will be rebuild based on the OP value. */
12980   EXPR_WFL_LINECOL (binop) = op_location;
12981   return binop;
12982 }
12983
12984 /* Build the string of the operator retained by NODE. If NODE is part
12985    of a compound expression, add an '=' at the end of the string. This
12986    function is called when an error needs to be reported on an
12987    operator. The string is returned as a pointer to a static character
12988    buffer. */
12989
12990 static char *
12991 operator_string (tree node)
12992 {
12993 #define BUILD_OPERATOR_STRING(S)                                        \
12994   {                                                                     \
12995     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12996     return buffer;                                                      \
12997   }
12998
12999   static char buffer [10];
13000   switch (TREE_CODE (node))
13001     {
13002     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13003     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13004     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13005     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13006     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13007     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13008     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13009     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13010     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13011     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13012     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13013     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13014     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13015     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13016     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13017     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13018     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13019     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13020     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13021     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13022     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13023     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13024     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13025     case PREINCREMENT_EXPR:     /* Fall through */
13026     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13027     case PREDECREMENT_EXPR:     /* Fall through */
13028     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13029     default:
13030       internal_error ("unregistered operator %s",
13031                       tree_code_name [TREE_CODE (node)]);
13032     }
13033   return NULL;
13034 #undef BUILD_OPERATOR_STRING
13035 }
13036
13037 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13038
13039 static int
13040 java_decl_equiv (tree var_acc1, tree var_acc2)
13041 {
13042   if (JDECL_P (var_acc1))
13043     return (var_acc1 == var_acc2);
13044
13045   return (TREE_CODE (var_acc1) == COMPONENT_REF
13046           && TREE_CODE (var_acc2) == COMPONENT_REF
13047           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13048              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13049           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13050 }
13051
13052 /* Return a nonzero value if CODE is one of the operators that can be
13053    used in conjunction with the `=' operator in a compound assignment.  */
13054
13055 static int
13056 binop_compound_p (enum tree_code code)
13057 {
13058   int i;
13059   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13060     if (binop_lookup [i] == code)
13061       break;
13062
13063   return i < BINOP_COMPOUND_CANDIDATES;
13064 }
13065
13066 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13067
13068 static tree
13069 java_refold (tree t)
13070 {
13071   tree c, b, ns, decl;
13072
13073   if (TREE_CODE (t) != MODIFY_EXPR)
13074     return t;
13075
13076   c = TREE_OPERAND (t, 1);
13077   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13078          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13079          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13080     return t;
13081
13082   /* Now the left branch of the binary operator. */
13083   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13084   if (! (b && TREE_CODE (b) == NOP_EXPR
13085          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13086     return t;
13087
13088   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13089   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13090          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13091     return t;
13092
13093   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13094   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13095       /* It's got to be the an equivalent decl */
13096       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13097     {
13098       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13099       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13100       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13101       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13102       /* Change the right part of the BINOP_EXPR */
13103       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13104     }
13105
13106   return t;
13107 }
13108
13109 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13110    errors but we modify NODE so that it contains the type computed
13111    according to the expression, when it's fixed. Otherwise, we write
13112    error_mark_node as the type. It allows us to further the analysis
13113    of remaining nodes and detects more errors in certain cases.  */
13114
13115 static tree
13116 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13117 {
13118   tree op1 = TREE_OPERAND (node, 0);
13119   tree op2 = TREE_OPERAND (node, 1);
13120   tree op1_type = TREE_TYPE (op1);
13121   tree op2_type = TREE_TYPE (op2);
13122   tree prom_type = NULL_TREE, cn;
13123   enum tree_code code = TREE_CODE (node);
13124
13125   /* If 1, tell the routine that we have to return error_mark_node
13126      after checking for the initialization of the RHS */
13127   int error_found = 0;
13128
13129   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13130
13131   /* If either op<n>_type are NULL, this might be early signs of an
13132      error situation, unless it's too early to tell (in case we're
13133      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13134      correctly so the error can be later on reported accurately. */
13135   if (! (code == PLUS_EXPR || code == NE_EXPR
13136          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13137     {
13138       tree n;
13139       if (! op1_type)
13140         {
13141           n = java_complete_tree (op1);
13142           op1_type = TREE_TYPE (n);
13143         }
13144       if (! op2_type)
13145         {
13146           n = java_complete_tree (op2);
13147           op2_type = TREE_TYPE (n);
13148         }
13149     }
13150
13151   switch (code)
13152     {
13153     /* 15.16 Multiplicative operators */
13154     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13155     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13156     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13157     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13158       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13159         {
13160           if (!JNUMERIC_TYPE_P (op1_type))
13161             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13162           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13163             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13164           TREE_TYPE (node) = error_mark_node;
13165           error_found = 1;
13166           break;
13167         }
13168       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13169
13170       /* Detect integral division by zero */
13171       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13172           && TREE_CODE (prom_type) == INTEGER_TYPE
13173           && (op2 == integer_zero_node || op2 == long_zero_node ||
13174               (TREE_CODE (op2) == INTEGER_CST &&
13175                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13176         {
13177           parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13178           TREE_CONSTANT (node) = 0;
13179         }
13180
13181       /* Change the division operator if necessary */
13182       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13183         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13184
13185       /* Before divisions as is disapear, try to simplify and bail if
13186          applicable, otherwise we won't perform even simple
13187          simplifications like (1-1)/3. We can't do that with floating
13188          point number, folds can't handle them at this stage. */
13189       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13190           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13191         {
13192           TREE_TYPE (node) = prom_type;
13193           node = fold (node);
13194           if (TREE_CODE (node) != code)
13195             return node;
13196         }
13197
13198       if (TREE_CODE (prom_type) == INTEGER_TYPE
13199           && flag_use_divide_subroutine
13200           && ! flag_emit_class_files
13201           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13202         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13203
13204       /* This one is more complicated. FLOATs are processed by a
13205          function call to soft_fmod. Duplicate the value of the
13206          COMPOUND_ASSIGN_P flag. */
13207       if (code == TRUNC_MOD_EXPR)
13208         {
13209           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13210           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13211           TREE_SIDE_EFFECTS (mod)
13212             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13213           return mod;
13214         }
13215       break;
13216
13217     /* 15.17 Additive Operators */
13218     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13219
13220       /* Operation is valid if either one argument is a string
13221          constant, a String object or a StringBuffer crafted for the
13222          purpose of the a previous usage of the String concatenation
13223          operator */
13224
13225       if (TREE_CODE (op1) == STRING_CST
13226           || TREE_CODE (op2) == STRING_CST
13227           || JSTRING_TYPE_P (op1_type)
13228           || JSTRING_TYPE_P (op2_type)
13229           || IS_CRAFTED_STRING_BUFFER_P (op1)
13230           || IS_CRAFTED_STRING_BUFFER_P (op2))
13231         return build_string_concatenation (op1, op2);
13232
13233     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13234                                    Numeric Types */
13235       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13236         {
13237           if (!JNUMERIC_TYPE_P (op1_type))
13238             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13239           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13240             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13241           TREE_TYPE (node) = error_mark_node;
13242           error_found = 1;
13243           break;
13244         }
13245       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13246       break;
13247
13248     /* 15.18 Shift Operators */
13249     case LSHIFT_EXPR:
13250     case RSHIFT_EXPR:
13251     case URSHIFT_EXPR:
13252       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13253         {
13254           if (!JINTEGRAL_TYPE_P (op1_type))
13255             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13256           else
13257             {
13258               if (JNUMERIC_TYPE_P (op2_type))
13259                 parse_error_context (wfl_operator,
13260                                      "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13261                                      operator_string (node),
13262                                      lang_printable_name (op2_type, 0));
13263               else
13264                 parse_error_context (wfl_operator,
13265                                      "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13266                                      operator_string (node),
13267                                      lang_printable_name (op2_type, 0));
13268             }
13269           TREE_TYPE (node) = error_mark_node;
13270           error_found = 1;
13271           break;
13272         }
13273
13274       /* Unary numeric promotion (5.6.1) is performed on each operand
13275          separately */
13276       op1 = do_unary_numeric_promotion (op1);
13277       op2 = do_unary_numeric_promotion (op2);
13278
13279       /* If the right hand side is of type `long', first cast it to
13280          `int'.  */
13281       if (TREE_TYPE (op2) == long_type_node)
13282         op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13283
13284       /* The type of the shift expression is the type of the promoted
13285          type of the left-hand operand */
13286       prom_type = TREE_TYPE (op1);
13287
13288       /* Shift int only up to 0x1f and long up to 0x3f */
13289       if (prom_type == int_type_node)
13290         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13291                            build_int_2 (0x1f, 0)));
13292       else
13293         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13294                            build_int_2 (0x3f, 0)));
13295
13296       /* The >>> operator is a >> operating on unsigned quantities */
13297       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13298         {
13299           tree to_return;
13300           tree utype = java_unsigned_type (prom_type);
13301           op1 = convert (utype, op1);
13302           TREE_SET_CODE (node, RSHIFT_EXPR);
13303           TREE_OPERAND (node, 0) = op1;
13304           TREE_OPERAND (node, 1) = op2;
13305           TREE_TYPE (node) = utype;
13306           to_return = convert (prom_type, node);
13307           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13308           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13309           TREE_SIDE_EFFECTS (to_return)
13310             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13311           return to_return;
13312         }
13313       break;
13314
13315       /* 15.19.1 Type Comparison Operator instaceof */
13316     case INSTANCEOF_EXPR:
13317
13318       TREE_TYPE (node) = boolean_type_node;
13319
13320       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13321       if ((cn = patch_string (op1)))
13322         {
13323           op1 = cn;
13324           op1_type = TREE_TYPE (op1);
13325         }
13326       if (op1_type == NULL_TREE)
13327         abort ();
13328
13329       if (!(op2_type = resolve_type_during_patch (op2)))
13330         return error_mark_node;
13331
13332       /* The first operand must be a reference type or the null type */
13333       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13334         error_found = 1;        /* Error reported further below */
13335
13336       /* The second operand must be a reference type */
13337       if (!JREFERENCE_TYPE_P (op2_type))
13338         {
13339           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13340           parse_error_context
13341             (wfl_operator, "Invalid argument `%s' for `instanceof'",
13342              lang_printable_name (op2_type, 0));
13343           error_found = 1;
13344         }
13345
13346       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13347         {
13348           /* If the first operand is null, the result is always false */
13349           if (op1 == null_pointer_node)
13350             return boolean_false_node;
13351           else if (flag_emit_class_files)
13352             {
13353               TREE_OPERAND (node, 1) = op2_type;
13354               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13355               return node;
13356             }
13357           /* Otherwise we have to invoke instance of to figure it out */
13358           else
13359             return build_instanceof (op1, op2_type);
13360         }
13361       /* There is no way the expression operand can be an instance of
13362          the type operand. This is a compile time error. */
13363       else
13364         {
13365           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13366           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13367           parse_error_context
13368             (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13369              t1, lang_printable_name (op2_type, 0));
13370           free (t1);
13371           error_found = 1;
13372         }
13373
13374       break;
13375
13376       /* 15.21 Bitwise and Logical Operators */
13377     case BIT_AND_EXPR:
13378     case BIT_XOR_EXPR:
13379     case BIT_IOR_EXPR:
13380       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13381         /* Binary numeric promotion is performed on both operand and the
13382            expression retain that type */
13383         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13384
13385       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13386                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13387         /* The type of the bitwise operator expression is BOOLEAN */
13388         prom_type = boolean_type_node;
13389       else
13390         {
13391           if (!JINTEGRAL_TYPE_P (op1_type))
13392             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13393           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13394             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13395           TREE_TYPE (node) = error_mark_node;
13396           error_found = 1;
13397           /* Insert a break here if adding thing before the switch's
13398              break for this case */
13399         }
13400       break;
13401
13402       /* 15.22 Conditional-And Operator */
13403     case TRUTH_ANDIF_EXPR:
13404       /* 15.23 Conditional-Or Operator */
13405     case TRUTH_ORIF_EXPR:
13406       /* Operands must be of BOOLEAN type */
13407       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13408           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13409         {
13410           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13411             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13412           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13413             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13414           TREE_TYPE (node) = boolean_type_node;
13415           error_found = 1;
13416           break;
13417         }
13418       else if (integer_zerop (op1))
13419         {
13420           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13421         }
13422       else if (integer_onep (op1))
13423         {
13424           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13425         }
13426       /* The type of the conditional operators is BOOLEAN */
13427       prom_type = boolean_type_node;
13428       break;
13429
13430       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13431     case LT_EXPR:
13432     case GT_EXPR:
13433     case LE_EXPR:
13434     case GE_EXPR:
13435       /* The type of each of the operands must be a primitive numeric
13436          type */
13437       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13438         {
13439           if (!JNUMERIC_TYPE_P (op1_type))
13440             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13441           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13442             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13443           TREE_TYPE (node) = boolean_type_node;
13444           error_found = 1;
13445           break;
13446         }
13447       /* Binary numeric promotion is performed on the operands */
13448       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13449       /* The type of the relation expression is always BOOLEAN */
13450       prom_type = boolean_type_node;
13451       break;
13452
13453       /* 15.20 Equality Operator */
13454     case EQ_EXPR:
13455     case NE_EXPR:
13456       /* It's time for us to patch the strings. */
13457       if ((cn = patch_string (op1)))
13458        {
13459          op1 = cn;
13460          op1_type = TREE_TYPE (op1);
13461        }
13462       if ((cn = patch_string (op2)))
13463        {
13464          op2 = cn;
13465          op2_type = TREE_TYPE (op2);
13466        }
13467
13468       /* 15.20.1 Numerical Equality Operators == and != */
13469       /* Binary numeric promotion is performed on the operands */
13470       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13471         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13472
13473       /* 15.20.2 Boolean Equality Operators == and != */
13474       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13475           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13476         ;                       /* Nothing to do here */
13477
13478       /* 15.20.3 Reference Equality Operators == and != */
13479       /* Types have to be either references or the null type. If
13480          they're references, it must be possible to convert either
13481          type to the other by casting conversion. */
13482       else if (op1 == null_pointer_node || op2 == null_pointer_node
13483                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13484                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13485                        || valid_ref_assignconv_cast_p (op2_type,
13486                                                        op1_type, 1))))
13487         ;                       /* Nothing to do here */
13488
13489       /* Else we have an error figure what can't be converted into
13490          what and report the error */
13491       else
13492         {
13493           char *t1;
13494           t1 = xstrdup (lang_printable_name (op1_type, 0));
13495           parse_error_context
13496             (wfl_operator,
13497              "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13498              operator_string (node), t1,
13499              lang_printable_name (op2_type, 0));
13500           free (t1);
13501           TREE_TYPE (node) = boolean_type_node;
13502           error_found = 1;
13503           break;
13504         }
13505       prom_type = boolean_type_node;
13506       break;
13507     default:
13508       abort ();
13509     }
13510
13511   if (error_found)
13512     return error_mark_node;
13513
13514   TREE_OPERAND (node, 0) = op1;
13515   TREE_OPERAND (node, 1) = op2;
13516   TREE_TYPE (node) = prom_type;
13517   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13518
13519   if (flag_emit_xref)
13520     return node;
13521
13522   /* fold does not respect side-effect order as required for Java but not C.
13523    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13524    * bytecode.
13525    */
13526   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13527       : ! TREE_SIDE_EFFECTS (node))
13528     node = fold (node);
13529   return node;
13530 }
13531
13532 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13533    zero value, the value of CSTE comes after the valude of STRING */
13534
13535 static tree
13536 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13537 {
13538   const char *old = TREE_STRING_POINTER (cste);
13539   int old_len = TREE_STRING_LENGTH (cste);
13540   int len = old_len + string_len;
13541   char *new = alloca (len+1);
13542
13543   if (after)
13544     {
13545       memcpy (new, string, string_len);
13546       memcpy (&new [string_len], old, old_len);
13547     }
13548   else
13549     {
13550       memcpy (new, old, old_len);
13551       memcpy (&new [old_len], string, string_len);
13552     }
13553   new [len] = '\0';
13554   return build_string (len, new);
13555 }
13556
13557 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13558    new STRING_CST on success, NULL_TREE on failure */
13559
13560 static tree
13561 merge_string_cste (tree op1, tree op2, int after)
13562 {
13563   /* Handle two string constants right away */
13564   if (TREE_CODE (op2) == STRING_CST)
13565     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13566                                  TREE_STRING_LENGTH (op2), after);
13567
13568   /* Reasonable integer constant can be treated right away */
13569   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13570     {
13571       static const char *const boolean_true = "true";
13572       static const char *const boolean_false = "false";
13573       static const char *const null_pointer = "null";
13574       char ch[3];
13575       const char *string;
13576
13577       if (op2 == boolean_true_node)
13578         string = boolean_true;
13579       else if (op2 == boolean_false_node)
13580         string = boolean_false;
13581       else if (op2 == null_pointer_node)
13582         string = null_pointer;
13583       else if (TREE_TYPE (op2) == char_type_node)
13584         {
13585           /* Convert the character into UTF-8.  */
13586           unsigned char c = (unsigned char) TREE_INT_CST_LOW (op2);
13587           unsigned char *p = (unsigned char *) ch;
13588           if (0x01 <= c
13589               && c <= 0x7f)
13590             *p++ = c;
13591           else
13592             {
13593               *p++ = c >> 6 | 0xc0;
13594               *p++ = (c & 0x3f) | 0x80;
13595             }
13596           *p = '\0';
13597  
13598           string = ch;
13599         }
13600       else
13601         string = string_convert_int_cst (op2);
13602
13603       return do_merge_string_cste (op1, string, strlen (string), after);
13604     }
13605   return NULL_TREE;
13606 }
13607
13608 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13609    has to be a STRING_CST and the other part must be a STRING_CST or a
13610    INTEGRAL constant. Return a new STRING_CST if the operation
13611    succeed, NULL_TREE otherwise.
13612
13613    If the case we want to optimize for space, we might want to return
13614    NULL_TREE for each invocation of this routine. FIXME */
13615
13616 static tree
13617 string_constant_concatenation (tree op1, tree op2)
13618 {
13619   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13620     {
13621       tree string, rest;
13622       int invert;
13623
13624       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13625       rest   = (string == op1 ? op2 : op1);
13626       invert = (string == op1 ? 0 : 1 );
13627
13628       /* Walk REST, only if it looks reasonable */
13629       if (TREE_CODE (rest) != STRING_CST
13630           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13631           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13632           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13633         {
13634           rest = java_complete_tree (rest);
13635           if (rest == error_mark_node)
13636             return error_mark_node;
13637           rest = fold (rest);
13638         }
13639       return merge_string_cste (string, rest, invert);
13640     }
13641   return NULL_TREE;
13642 }
13643
13644 /* Implement the `+' operator. Does static optimization if possible,
13645    otherwise create (if necessary) and append elements to a
13646    StringBuffer. The StringBuffer will be carried around until it is
13647    used for a function call or an assignment. Then toString() will be
13648    called on it to turn it into a String object. */
13649
13650 static tree
13651 build_string_concatenation (tree op1, tree op2)
13652 {
13653   tree result;
13654   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13655
13656   if (flag_emit_xref)
13657     return build (PLUS_EXPR, string_type_node, op1, op2);
13658
13659   /* Try to do some static optimization */
13660   if ((result = string_constant_concatenation (op1, op2)))
13661     return result;
13662
13663   /* Discard empty strings on either side of the expression */
13664   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13665     {
13666       op1 = op2;
13667       op2 = NULL_TREE;
13668     }
13669   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13670     op2 = NULL_TREE;
13671
13672   /* If operands are string constant, turn then into object references */
13673   if (TREE_CODE (op1) == STRING_CST)
13674     op1 = patch_string_cst (op1);
13675   if (op2 && TREE_CODE (op2) == STRING_CST)
13676     op2 = patch_string_cst (op2);
13677
13678   /* If either one of the constant is null and the other non null
13679      operand is a String constant, return it. */
13680   if ((TREE_CODE (op1) == STRING_CST) && !op2)
13681     return op1;
13682
13683   /* If OP1 isn't already a StringBuffer, create and
13684      initialize a new one */
13685   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13686     {
13687       /* Two solutions here:
13688          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13689          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
13690       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13691         op1 = BUILD_STRING_BUFFER (op1);
13692       else
13693         {
13694           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13695           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13696         }
13697     }
13698
13699   if (op2)
13700     {
13701       /* OP1 is no longer the last node holding a crafted StringBuffer */
13702       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13703       /* Create a node for `{new...,xxx}.append (op2)' */
13704       if (op2)
13705         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13706     }
13707
13708   /* Mark the last node holding a crafted StringBuffer */
13709   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13710
13711   TREE_SIDE_EFFECTS (op1) = side_effects;
13712   return op1;
13713 }
13714
13715 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13716    StringBuffer. If no string were found to be patched, return
13717    NULL. */
13718
13719 static tree
13720 patch_string (tree node)
13721 {
13722   if (node == error_mark_node)
13723     return error_mark_node;
13724   if (TREE_CODE (node) == STRING_CST)
13725     return patch_string_cst (node);
13726   else if (IS_CRAFTED_STRING_BUFFER_P (node))
13727     {
13728       int saved = ctxp->explicit_constructor_p;
13729       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13730       tree ret;
13731       /* Temporary disable forbid the use of `this'. */
13732       ctxp->explicit_constructor_p = 0;
13733       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13734       /* String concatenation arguments must be evaluated in order too. */
13735       ret = force_evaluation_order (ret);
13736       /* Restore it at its previous value */
13737       ctxp->explicit_constructor_p = saved;
13738       return ret;
13739     }
13740   return NULL_TREE;
13741 }
13742
13743 /* Build the internal representation of a string constant.  */
13744
13745 static tree
13746 patch_string_cst (tree node)
13747 {
13748   int location;
13749   if (! flag_emit_class_files)
13750     {
13751       node = get_identifier (TREE_STRING_POINTER (node));
13752       location = alloc_name_constant (CONSTANT_String, node);
13753       node = build_ref_from_constant_pool (location);
13754     }
13755   TREE_TYPE (node) = string_ptr_type_node;
13756   TREE_CONSTANT (node) = 1;
13757   return node;
13758 }
13759
13760 /* Build an incomplete unary operator expression. */
13761
13762 static tree
13763 build_unaryop (int op_token, int op_location, tree op1)
13764 {
13765   enum tree_code op;
13766   tree unaryop;
13767   switch (op_token)
13768     {
13769     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13770     case MINUS_TK: op = NEGATE_EXPR; break;
13771     case NEG_TK: op = TRUTH_NOT_EXPR; break;
13772     case NOT_TK: op = BIT_NOT_EXPR; break;
13773     default: abort ();
13774     }
13775
13776   unaryop = build1 (op, NULL_TREE, op1);
13777   TREE_SIDE_EFFECTS (unaryop) = 1;
13778   /* Store the location of the operator, for better error report. The
13779      string of the operator will be rebuild based on the OP value. */
13780   EXPR_WFL_LINECOL (unaryop) = op_location;
13781   return unaryop;
13782 }
13783
13784 /* Special case for the ++/-- operators, since they require an extra
13785    argument to build, which is set to NULL and patched
13786    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
13787
13788 static tree
13789 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
13790 {
13791   static const enum tree_code lookup [2][2] =
13792     {
13793       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13794       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13795     };
13796   tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13797                      NULL_TREE, op1, NULL_TREE);
13798   TREE_SIDE_EFFECTS (node) = 1;
13799   /* Store the location of the operator, for better error report. The
13800      string of the operator will be rebuild based on the OP value. */
13801   EXPR_WFL_LINECOL (node) = op_location;
13802   return node;
13803 }
13804
13805 /* Build an incomplete cast operator, based on the use of the
13806    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13807    set. java_complete_tree is trained to walk a CONVERT_EXPR even
13808    though its type is already set.  */
13809
13810 static tree
13811 build_cast (int location, tree type, tree exp)
13812 {
13813   tree node = build1 (CONVERT_EXPR, type, exp);
13814   EXPR_WFL_LINECOL (node) = location;
13815   return node;
13816 }
13817
13818 /* Build an incomplete class reference operator.  */
13819 static tree
13820 build_incomplete_class_ref (int location, tree class_name)
13821 {
13822   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13823   EXPR_WFL_LINECOL (node) = location;
13824   return node;
13825 }
13826
13827 /* Complete an incomplete class reference operator.  */
13828 static tree
13829 patch_incomplete_class_ref (tree node)
13830 {
13831   tree type = TREE_OPERAND (node, 0);
13832   tree ref_type;
13833
13834   if (!(ref_type = resolve_type_during_patch (type)))
13835     return error_mark_node;
13836
13837   /* Generate the synthetic static method `class$'.  (Previously we
13838      deferred this, causing different method tables to be emitted
13839      for native code and bytecode.)  */
13840   if (!TYPE_DOT_CLASS (current_class))
13841       build_dot_class_method (current_class);
13842
13843   /* If we're not emitting class files and we know ref_type is a
13844      compiled class, build a direct reference.  */
13845   if ((! flag_emit_class_files && is_compiled_class (ref_type))
13846       || JPRIMITIVE_TYPE_P (ref_type)
13847       || TREE_CODE (ref_type) == VOID_TYPE)
13848     {
13849       tree dot = build_class_ref (ref_type);
13850       /* A class referenced by `foo.class' is initialized.  */
13851       if (!flag_emit_class_files)
13852        dot = build_class_init (ref_type, dot);
13853       return java_complete_tree (dot);
13854     }
13855
13856   /* If we're emitting class files and we have to deal with non
13857      primitive types, we invoke the synthetic static method `class$'.  */
13858   ref_type = build_dot_class_method_invocation (ref_type);
13859   return java_complete_tree (ref_type);
13860 }
13861
13862 /* 15.14 Unary operators. We return error_mark_node in case of error,
13863    but preserve the type of NODE if the type is fixed.  */
13864
13865 static tree
13866 patch_unaryop (tree node, tree wfl_op)
13867 {
13868   tree op = TREE_OPERAND (node, 0);
13869   tree op_type = TREE_TYPE (op);
13870   tree prom_type = NULL_TREE, value, decl;
13871   int outer_field_flag = 0;
13872   int code = TREE_CODE (node);
13873   int error_found = 0;
13874
13875   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13876
13877   switch (code)
13878     {
13879       /* 15.13.2 Postfix Increment Operator ++ */
13880     case POSTINCREMENT_EXPR:
13881       /* 15.13.3 Postfix Increment Operator -- */
13882     case POSTDECREMENT_EXPR:
13883       /* 15.14.1 Prefix Increment Operator ++ */
13884     case PREINCREMENT_EXPR:
13885       /* 15.14.2 Prefix Decrement Operator -- */
13886     case PREDECREMENT_EXPR:
13887       op = decl = strip_out_static_field_access_decl (op);
13888       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13889       /* We might be trying to change an outer field accessed using
13890          access method. */
13891       if (outer_field_flag)
13892         {
13893           /* Retrieve the decl of the field we're trying to access. We
13894              do that by first retrieving the function we would call to
13895              access the field. It has been already verified that this
13896              field isn't final */
13897           if (flag_emit_class_files)
13898             decl = TREE_OPERAND (op, 0);
13899           else
13900             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13901           decl = DECL_FUNCTION_ACCESS_DECL (decl);
13902         }
13903       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13904       else if (!JDECL_P (decl)
13905           && TREE_CODE (decl) != COMPONENT_REF
13906           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13907           && TREE_CODE (decl) != INDIRECT_REF
13908           && !(TREE_CODE (decl) == COMPOUND_EXPR
13909                && TREE_OPERAND (decl, 1)
13910                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13911         {
13912           TREE_TYPE (node) = error_mark_node;
13913           error_found = 1;
13914         }
13915
13916       /* From now on, we know that op if a variable and that it has a
13917          valid wfl. We use wfl_op to locate errors related to the
13918          ++/-- operand. */
13919       if (!JNUMERIC_TYPE_P (op_type))
13920         {
13921           parse_error_context
13922             (wfl_op, "Invalid argument type `%s' to `%s'",
13923              lang_printable_name (op_type, 0), operator_string (node));
13924           TREE_TYPE (node) = error_mark_node;
13925           error_found = 1;
13926         }
13927       else
13928         {
13929           /* Before the addition, binary numeric promotion is performed on
13930              both operands, if really necessary */
13931           if (JINTEGRAL_TYPE_P (op_type))
13932             {
13933               value = build_int_2 (1, 0);
13934               TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13935             }
13936           else
13937             {
13938               value = build_int_2 (1, 0);
13939               TREE_TYPE (node) =
13940                 binary_numeric_promotion (op_type,
13941                                           TREE_TYPE (value), &op, &value);
13942             }
13943
13944           /* We remember we might be accessing an outer field */
13945           if (outer_field_flag)
13946             {
13947               /* We re-generate an access to the field */
13948               value = build (PLUS_EXPR, TREE_TYPE (op),
13949                              build_outer_field_access (wfl_op, decl), value);
13950
13951               /* And we patch the original access$() into a write
13952                  with plus_op as a rhs */
13953               return outer_field_access_fix (node, op, value);
13954             }
13955
13956           /* And write back into the node. */
13957           TREE_OPERAND (node, 0) = op;
13958           TREE_OPERAND (node, 1) = value;
13959           /* Convert the overall back into its original type, if
13960              necessary, and return */
13961           if (JINTEGRAL_TYPE_P (op_type))
13962             return fold (node);
13963           else
13964             return fold (convert (op_type, node));
13965         }
13966       break;
13967
13968       /* 15.14.3 Unary Plus Operator + */
13969     case UNARY_PLUS_EXPR:
13970       /* 15.14.4 Unary Minus Operator - */
13971     case NEGATE_EXPR:
13972       if (!JNUMERIC_TYPE_P (op_type))
13973         {
13974           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13975           TREE_TYPE (node) = error_mark_node;
13976           error_found = 1;
13977         }
13978       /* Unary numeric promotion is performed on operand */
13979       else
13980         {
13981           op = do_unary_numeric_promotion (op);
13982           prom_type = TREE_TYPE (op);
13983           if (code == UNARY_PLUS_EXPR)
13984             return fold (op);
13985         }
13986       break;
13987
13988       /* 15.14.5 Bitwise Complement Operator ~ */
13989     case BIT_NOT_EXPR:
13990       if (!JINTEGRAL_TYPE_P (op_type))
13991         {
13992           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13993           TREE_TYPE (node) = error_mark_node;
13994           error_found = 1;
13995         }
13996       else
13997         {
13998           op = do_unary_numeric_promotion (op);
13999           prom_type = TREE_TYPE (op);
14000         }
14001       break;
14002
14003       /* 15.14.6 Logical Complement Operator ! */
14004     case TRUTH_NOT_EXPR:
14005       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14006         {
14007           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14008           /* But the type is known. We will report an error if further
14009              attempt of a assignment is made with this rhs */
14010           TREE_TYPE (node) = boolean_type_node;
14011           error_found = 1;
14012         }
14013       else
14014         prom_type = boolean_type_node;
14015       break;
14016
14017       /* 15.15 Cast Expression */
14018     case CONVERT_EXPR:
14019       value = patch_cast (node, wfl_operator);
14020       if (value == error_mark_node)
14021         {
14022           /* If this cast is part of an assignment, we tell the code
14023              that deals with it not to complain about a mismatch,
14024              because things have been cast, anyways */
14025           TREE_TYPE (node) = error_mark_node;
14026           error_found = 1;
14027         }
14028       else
14029         {
14030           value = fold (value);
14031           TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14032           return value;
14033         }
14034       break;
14035     }
14036
14037   if (error_found)
14038     return error_mark_node;
14039
14040   /* There are cases where node has been replaced by something else
14041      and we don't end up returning here: UNARY_PLUS_EXPR,
14042      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14043   TREE_OPERAND (node, 0) = fold (op);
14044   TREE_TYPE (node) = prom_type;
14045   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14046   return fold (node);
14047 }
14048
14049 /* Generic type resolution that sometimes takes place during node
14050    patching. Returned the resolved type or generate an error
14051    message. Return the resolved type or NULL_TREE.  */
14052
14053 static tree
14054 resolve_type_during_patch (tree type)
14055 {
14056   if (unresolved_type_p (type, NULL))
14057     {
14058       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14059       if (!type_decl)
14060         {
14061           parse_error_context (type,
14062                                "Class `%s' not found in type declaration",
14063                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14064           return NULL_TREE;
14065         }
14066       return TREE_TYPE (type_decl);
14067     }
14068   return type;
14069 }
14070 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14071    found. Otherwise NODE or something meant to replace it is returned.  */
14072
14073 static tree
14074 patch_cast (tree node, tree wfl_op)
14075 {
14076   tree op = TREE_OPERAND (node, 0);
14077   tree cast_type = TREE_TYPE (node);
14078   tree patched, op_type;
14079   char *t1;
14080
14081   /* Some string patching might be necessary at this stage */
14082   if ((patched = patch_string (op)))
14083     TREE_OPERAND (node, 0) = op = patched;
14084   op_type = TREE_TYPE (op);
14085
14086   /* First resolve OP_TYPE if unresolved */
14087   if (!(cast_type = resolve_type_during_patch (cast_type)))
14088     return error_mark_node;
14089
14090   /* Check on cast that are proven correct at compile time */
14091   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14092     {
14093       /* Same type */
14094       if (cast_type == op_type)
14095         return node;
14096
14097       /* float and double type are converted to the original type main
14098          variant and then to the target type. */
14099       if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14100         op = convert (integer_type_node, op);
14101
14102       /* Try widening/narowwing convertion. Potentially, things need
14103          to be worked out in gcc so we implement the extreme cases
14104          correctly. fold_convert() needs to be fixed. */
14105       return convert (cast_type, op);
14106     }
14107
14108   /* It's also valid to cast a boolean into a boolean */
14109   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14110     return node;
14111
14112   /* null can be casted to references */
14113   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14114     return build_null_of_type (cast_type);
14115
14116   /* The remaining legal casts involve conversion between reference
14117      types. Check for their compile time correctness. */
14118   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14119       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14120     {
14121       TREE_TYPE (node) = promote_type (cast_type);
14122       /* Now, the case can be determined correct at compile time if
14123          OP_TYPE can be converted into CAST_TYPE by assignment
14124          conversion (5.2) */
14125
14126       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14127         {
14128           TREE_SET_CODE (node, NOP_EXPR);
14129           return node;
14130         }
14131
14132       if (flag_emit_class_files)
14133         {
14134           TREE_SET_CODE (node, CONVERT_EXPR);
14135           return node;
14136         }
14137
14138       /* The cast requires a run-time check */
14139       return build (CALL_EXPR, promote_type (cast_type),
14140                     build_address_of (soft_checkcast_node),
14141                     tree_cons (NULL_TREE, build_class_ref (cast_type),
14142                                build_tree_list (NULL_TREE, op)),
14143                     NULL_TREE);
14144     }
14145
14146   /* Any other casts are proven incorrect at compile time */
14147   t1 = xstrdup (lang_printable_name (op_type, 0));
14148   parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14149                        t1, lang_printable_name (cast_type, 0));
14150   free (t1);
14151   return error_mark_node;
14152 }
14153
14154 /* Build a null constant and give it the type TYPE.  */
14155
14156 static tree
14157 build_null_of_type (tree type)
14158 {
14159   tree node = build_int_2 (0, 0);
14160   TREE_TYPE (node) = promote_type (type);
14161   return node;
14162 }
14163
14164 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14165    a list of indices. */
14166 static tree
14167 build_array_ref (int location, tree array, tree index)
14168 {
14169   tree node = build (ARRAY_REF, NULL_TREE, array, index);
14170   EXPR_WFL_LINECOL (node) = location;
14171   return node;
14172 }
14173
14174 /* 15.12 Array Access Expression */
14175
14176 static tree
14177 patch_array_ref (tree node)
14178 {
14179   tree array = TREE_OPERAND (node, 0);
14180   tree array_type  = TREE_TYPE (array);
14181   tree index = TREE_OPERAND (node, 1);
14182   tree index_type = TREE_TYPE (index);
14183   int error_found = 0;
14184
14185   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14186
14187   if (TREE_CODE (array_type) == POINTER_TYPE)
14188     array_type = TREE_TYPE (array_type);
14189
14190   /* The array reference must be an array */
14191   if (!TYPE_ARRAY_P (array_type))
14192     {
14193       parse_error_context
14194         (wfl_operator,
14195          "`[]' can only be applied to arrays. It can't be applied to `%s'",
14196          lang_printable_name (array_type, 0));
14197       TREE_TYPE (node) = error_mark_node;
14198       error_found = 1;
14199     }
14200
14201   /* The array index undergoes unary numeric promotion. The promoted
14202      type must be int */
14203   index = do_unary_numeric_promotion (index);
14204   if (TREE_TYPE (index) != int_type_node)
14205     {
14206       if (valid_cast_to_p (index_type, int_type_node))
14207         parse_error_context (wfl_operator,
14208    "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14209                              lang_printable_name (index_type, 0));
14210       else
14211         parse_error_context (wfl_operator,
14212           "Incompatible type for `[]'. Can't convert `%s' to `int'",
14213                              lang_printable_name (index_type, 0));
14214       TREE_TYPE (node) = error_mark_node;
14215       error_found = 1;
14216     }
14217
14218   if (error_found)
14219     return error_mark_node;
14220
14221   array_type = TYPE_ARRAY_ELEMENT (array_type);
14222
14223   if (flag_emit_class_files || flag_emit_xref)
14224     {
14225       TREE_OPERAND (node, 0) = array;
14226       TREE_OPERAND (node, 1) = index;
14227     }
14228   else
14229     node = build_java_arrayaccess (array, array_type, index);
14230   TREE_TYPE (node) = array_type;
14231   return node;
14232 }
14233
14234 /* 15.9 Array Creation Expressions */
14235
14236 static tree
14237 build_newarray_node (tree type, tree dims, int extra_dims)
14238 {
14239   tree node =
14240     build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14241            build_int_2 (extra_dims, 0));
14242   return node;
14243 }
14244
14245 static tree
14246 patch_newarray (tree node)
14247 {
14248   tree type = TREE_OPERAND (node, 0);
14249   tree dims = TREE_OPERAND (node, 1);
14250   tree cdim, array_type;
14251   int error_found = 0;
14252   int ndims = 0;
14253   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14254
14255   /* Dimension types are verified. It's better for the types to be
14256      verified in order. */
14257   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14258     {
14259       int dim_error = 0;
14260       tree dim = TREE_VALUE (cdim);
14261
14262       /* Dim might have been saved during its evaluation */
14263       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14264
14265       /* The type of each specified dimension must be an integral type. */
14266       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14267         dim_error = 1;
14268
14269       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14270          promoted type must be int. */
14271       else
14272         {
14273           dim = do_unary_numeric_promotion (dim);
14274           if (TREE_TYPE (dim) != int_type_node)
14275             dim_error = 1;
14276         }
14277
14278       /* Report errors on types here */
14279       if (dim_error)
14280         {
14281           parse_error_context
14282             (TREE_PURPOSE (cdim),
14283              "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14284              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14285               "Explicit cast needed to" : "Can't"),
14286              lang_printable_name (TREE_TYPE (dim), 0));
14287           error_found = 1;
14288         }
14289
14290       TREE_PURPOSE (cdim) = NULL_TREE;
14291     }
14292
14293   /* Resolve array base type if unresolved */
14294   if (!(type = resolve_type_during_patch (type)))
14295     error_found = 1;
14296
14297   if (error_found)
14298     {
14299       /* We don't want further evaluation of this bogus array creation
14300          operation */
14301       TREE_TYPE (node) = error_mark_node;
14302       return error_mark_node;
14303     }
14304
14305   /* Set array_type to the actual (promoted) array type of the result. */
14306   if (TREE_CODE (type) == RECORD_TYPE)
14307     type = build_pointer_type (type);
14308   while (--xdims >= 0)
14309     {
14310       type = promote_type (build_java_array_type (type, -1));
14311     }
14312   dims = nreverse (dims);
14313   array_type = type;
14314   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14315     {
14316       type = array_type;
14317       array_type
14318         = build_java_array_type (type,
14319                                  TREE_CODE (cdim) == INTEGER_CST
14320                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14321                                  : -1);
14322       array_type = promote_type (array_type);
14323     }
14324   dims = nreverse (dims);
14325
14326   /* The node is transformed into a function call. Things are done
14327      differently according to the number of dimensions. If the number
14328      of dimension is equal to 1, then the nature of the base type
14329      (primitive or not) matters. */
14330   if (ndims == 1)
14331     return build_new_array (type, TREE_VALUE (dims));
14332
14333   /* Can't reuse what's already written in expr.c because it uses the
14334      JVM stack representation. Provide a build_multianewarray. FIXME */
14335   return build (CALL_EXPR, array_type,
14336                 build_address_of (soft_multianewarray_node),
14337                 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14338                            tree_cons (NULL_TREE,
14339                                       build_int_2 (ndims, 0), dims )),
14340                 NULL_TREE);
14341 }
14342
14343 /* 10.6 Array initializer.  */
14344
14345 /* Build a wfl for array element that don't have one, so we can
14346    pin-point errors.  */
14347
14348 static tree
14349 maybe_build_array_element_wfl (tree node)
14350 {
14351   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14352     return build_expr_wfl (NULL_TREE, ctxp->filename,
14353                            ctxp->elc.line, ctxp->elc.prev_col);
14354   else
14355     return NULL_TREE;
14356 }
14357
14358 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14359    identification of initialized arrays easier to detect during walk
14360    and expansion.  */
14361
14362 static tree
14363 build_new_array_init (int location, tree values)
14364 {
14365   tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14366   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14367   EXPR_WFL_LINECOL (to_return) = location;
14368   return to_return;
14369 }
14370
14371 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14372    occurred.  Otherwise return NODE after having set its type
14373    appropriately.  */
14374
14375 static tree
14376 patch_new_array_init (tree type, tree node)
14377 {
14378   int error_seen = 0;
14379   tree current, element_type;
14380   HOST_WIDE_INT length;
14381   int all_constant = 1;
14382   tree init = TREE_OPERAND (node, 0);
14383
14384   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14385     {
14386       parse_error_context (node,
14387                            "Invalid array initializer for non-array type `%s'",
14388                            lang_printable_name (type, 1));
14389       return error_mark_node;
14390     }
14391   type = TREE_TYPE (type);
14392   element_type = TYPE_ARRAY_ELEMENT (type);
14393
14394   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14395
14396   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14397        current;  length++, current = TREE_CHAIN (current))
14398     {
14399       tree elt = TREE_VALUE (current);
14400       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14401         {
14402           error_seen |= array_constructor_check_entry (element_type, current);
14403           elt = TREE_VALUE (current);
14404           /* When compiling to native code, STRING_CST is converted to
14405              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14406           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14407             all_constant = 0;
14408         }
14409       else
14410         {
14411           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14412           TREE_PURPOSE (current) = NULL_TREE;
14413           all_constant = 0;
14414         }
14415       if (elt && TREE_CODE (elt) == TREE_LIST
14416           && TREE_VALUE (elt) == error_mark_node)
14417         error_seen = 1;
14418     }
14419
14420   if (error_seen)
14421     return error_mark_node;
14422
14423   /* Create a new type. We can't reuse the one we have here by
14424      patching its dimension because it originally is of dimension -1
14425      hence reused by gcc. This would prevent triangular arrays. */
14426   type = build_java_array_type (element_type, length);
14427   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14428   TREE_TYPE (node) = promote_type (type);
14429   TREE_CONSTANT (init) = all_constant;
14430   TREE_CONSTANT (node) = all_constant;
14431   return node;
14432 }
14433
14434 /* Verify that one entry of the initializer element list can be
14435    assigned to the array base type. Report 1 if an error occurred, 0
14436    otherwise.  */
14437
14438 static int
14439 array_constructor_check_entry (tree type, tree entry)
14440 {
14441   char *array_type_string = NULL;       /* For error reports */
14442   tree value, type_value, new_value, wfl_value, patched;
14443   int error_seen = 0;
14444
14445   new_value = NULL_TREE;
14446   wfl_value = TREE_VALUE (entry);
14447
14448   value = java_complete_tree (TREE_VALUE (entry));
14449   /* patch_string return error_mark_node if arg is error_mark_node */
14450   if ((patched = patch_string (value)))
14451     value = patched;
14452   if (value == error_mark_node)
14453     return 1;
14454
14455   type_value = TREE_TYPE (value);
14456
14457   /* At anytime, try_builtin_assignconv can report a warning on
14458      constant overflow during narrowing. */
14459   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14460   new_value = try_builtin_assignconv (wfl_operator, type, value);
14461   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14462     type_value = promote_type (type);
14463
14464   /* Check and report errors */
14465   if (!new_value)
14466     {
14467       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14468                    "Can't" : "Explicit cast needed to");
14469       if (!array_type_string)
14470         array_type_string = xstrdup (lang_printable_name (type, 1));
14471       parse_error_context
14472         (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14473          msg, lang_printable_name (type_value, 1), array_type_string);
14474       error_seen = 1;
14475     }
14476
14477   if (new_value)
14478     TREE_VALUE (entry) = new_value;
14479
14480   if (array_type_string)
14481     free (array_type_string);
14482
14483   TREE_PURPOSE (entry) = NULL_TREE;
14484   return error_seen;
14485 }
14486
14487 static tree
14488 build_this (int location)
14489 {
14490   tree node = build_wfl_node (this_identifier_node);
14491   TREE_SET_CODE (node, THIS_EXPR);
14492   EXPR_WFL_LINECOL (node) = location;
14493   return node;
14494 }
14495
14496 /* 14.15 The return statement. It builds a modify expression that
14497    assigns the returned value to the RESULT_DECL that hold the value
14498    to be returned. */
14499
14500 static tree
14501 build_return (int location, tree op)
14502 {
14503   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14504   EXPR_WFL_LINECOL (node) = location;
14505   node = build_debugable_stmt (location, node);
14506   return node;
14507 }
14508
14509 static tree
14510 patch_return (tree node)
14511 {
14512   tree return_exp = TREE_OPERAND (node, 0);
14513   tree meth = current_function_decl;
14514   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14515   int error_found = 0;
14516
14517   TREE_TYPE (node) = error_mark_node;
14518   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14519
14520   /* It's invalid to have a return value within a function that is
14521      declared with the keyword void or that is a constructor */
14522   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14523     error_found = 1;
14524
14525   /* It's invalid to use a return statement in a static block */
14526   if (DECL_CLINIT_P (current_function_decl))
14527     error_found = 1;
14528
14529   /* It's invalid to have a no return value within a function that
14530      isn't declared with the keyword `void' */
14531   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14532     error_found = 2;
14533
14534   if (DECL_INSTINIT_P (current_function_decl))
14535     error_found = 1;
14536
14537   if (error_found)
14538     {
14539       if (DECL_INSTINIT_P (current_function_decl))
14540         parse_error_context (wfl_operator,
14541                              "`return' inside instance initializer");
14542
14543       else if (DECL_CLINIT_P (current_function_decl))
14544         parse_error_context (wfl_operator,
14545                              "`return' inside static initializer");
14546
14547       else if (!DECL_CONSTRUCTOR_P (meth))
14548         {
14549           char *t = xstrdup (lang_printable_name (mtype, 0));
14550           parse_error_context (wfl_operator,
14551                                "`return' with%s value from `%s %s'",
14552                                (error_found == 1 ? "" : "out"),
14553                                t, lang_printable_name (meth, 0));
14554           free (t);
14555         }
14556       else
14557         parse_error_context (wfl_operator,
14558                              "`return' with value from constructor `%s'",
14559                              lang_printable_name (meth, 0));
14560       return error_mark_node;
14561     }
14562
14563   /* If we have a return_exp, build a modify expression and expand
14564      it. Note: at that point, the assignment is declared valid, but we
14565      may want to carry some more hacks */
14566   if (return_exp)
14567     {
14568       tree exp = java_complete_tree (return_exp);
14569       tree modify, patched;
14570
14571       if ((patched = patch_string (exp)))
14572         exp = patched;
14573
14574       modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14575       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14576       modify = java_complete_tree (modify);
14577
14578       if (modify != error_mark_node)
14579         {
14580           TREE_SIDE_EFFECTS (modify) = 1;
14581           TREE_OPERAND (node, 0) = modify;
14582         }
14583       else
14584         return error_mark_node;
14585     }
14586   TREE_TYPE (node) = void_type_node;
14587   TREE_SIDE_EFFECTS (node) = 1;
14588   return node;
14589 }
14590
14591 /* 14.8 The if Statement */
14592
14593 static tree
14594 build_if_else_statement (int location, tree expression, tree if_body,
14595                          tree else_body)
14596 {
14597   tree node;
14598   if (!else_body)
14599     else_body = empty_stmt_node;
14600   node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14601   EXPR_WFL_LINECOL (node) = location;
14602   node = build_debugable_stmt (location, node);
14603   return node;
14604 }
14605
14606 static tree
14607 patch_if_else_statement (tree node)
14608 {
14609   tree expression = TREE_OPERAND (node, 0);
14610   int can_complete_normally
14611     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14612        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14613
14614   TREE_TYPE (node) = error_mark_node;
14615   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14616
14617   /* The type of expression must be boolean */
14618   if (TREE_TYPE (expression) != boolean_type_node
14619       && TREE_TYPE (expression) != promoted_boolean_type_node)
14620     {
14621       parse_error_context
14622         (wfl_operator,
14623          "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14624          lang_printable_name (TREE_TYPE (expression), 0));
14625       return error_mark_node;
14626     }
14627
14628   if (TREE_CODE (expression) == INTEGER_CST)
14629     {
14630       if (integer_zerop (expression))
14631         node = TREE_OPERAND (node, 2);
14632       else
14633         node = TREE_OPERAND (node, 1);
14634       if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14635         {
14636           node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14637           CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14638         }
14639       return node;
14640     }
14641   TREE_TYPE (node) = void_type_node;
14642   TREE_SIDE_EFFECTS (node) = 1;
14643   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14644   return node;
14645 }
14646
14647 /* 14.6 Labeled Statements */
14648
14649 /* Action taken when a lableled statement is parsed. a new
14650    LABELED_BLOCK_EXPR is created. No statement is attached to the
14651    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
14652
14653 static tree
14654 build_labeled_block (int location, tree label)
14655 {
14656   tree label_name ;
14657   tree label_decl, node;
14658   if (label == NULL_TREE || label == continue_identifier_node)
14659     label_name = label;
14660   else
14661     {
14662       label_name = merge_qualified_name (label_id, label);
14663       /* Issue an error if we try to reuse a label that was previously
14664          declared */
14665       if (IDENTIFIER_LOCAL_VALUE (label_name))
14666         {
14667           EXPR_WFL_LINECOL (wfl_operator) = location;
14668           parse_error_context (wfl_operator,
14669             "Declaration of `%s' shadows a previous label declaration",
14670                                IDENTIFIER_POINTER (label));
14671           EXPR_WFL_LINECOL (wfl_operator) =
14672             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14673           parse_error_context (wfl_operator,
14674             "This is the location of the previous declaration of label `%s'",
14675                                IDENTIFIER_POINTER (label));
14676           java_error_count--;
14677         }
14678     }
14679
14680   label_decl = create_label_decl (label_name);
14681   node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14682   EXPR_WFL_LINECOL (node) = location;
14683   TREE_SIDE_EFFECTS (node) = 1;
14684   return node;
14685 }
14686
14687 /* A labeled statement LBE is attached a statement.  */
14688
14689 static tree
14690 finish_labeled_statement (tree lbe, /* Labeled block expr */
14691                           tree statement)
14692 {
14693   /* In anyways, tie the loop to its statement */
14694   LABELED_BLOCK_BODY (lbe) = statement;
14695   pop_labeled_block ();
14696   POP_LABELED_BLOCK ();
14697   return lbe;
14698 }
14699
14700 /* 14.10, 14.11, 14.12 Loop Statements */
14701
14702 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14703    list. */
14704
14705 static tree
14706 build_new_loop (tree loop_body)
14707 {
14708   tree loop =  build (LOOP_EXPR, NULL_TREE, loop_body);
14709   TREE_SIDE_EFFECTS (loop) = 1;
14710   PUSH_LOOP (loop);
14711   return loop;
14712 }
14713
14714 /* Create a loop body according to the following structure:
14715      COMPOUND_EXPR
14716        COMPOUND_EXPR            (loop main body)
14717          EXIT_EXPR              (this order is for while/for loops.
14718          LABELED_BLOCK_EXPR      the order is reversed for do loops)
14719            LABEL_DECL           (a continue occurring here branches at the
14720            BODY                  end of this labeled block)
14721        INCREMENT                (if any)
14722
14723   REVERSED, if nonzero, tells that the loop condition expr comes
14724   after the body, like in the do-while loop.
14725
14726   To obtain a loop, the loop body structure described above is
14727   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14728
14729    LABELED_BLOCK_EXPR
14730      LABEL_DECL                   (use this label to exit the loop)
14731      LOOP_EXPR
14732        <structure described above> */
14733
14734 static tree
14735 build_loop_body (int location, tree condition, int reversed)
14736 {
14737   tree first, second, body;
14738
14739   condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14740   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14741   condition = build_debugable_stmt (location, condition);
14742   TREE_SIDE_EFFECTS (condition) = 1;
14743
14744   body = build_labeled_block (0, continue_identifier_node);
14745   first = (reversed ? body : condition);
14746   second = (reversed ? condition : body);
14747   return
14748     build (COMPOUND_EXPR, NULL_TREE,
14749            build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14750 }
14751
14752 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14753    their order) on the current loop. Unlink the current loop from the
14754    loop list.  */
14755
14756 static tree
14757 finish_loop_body (int location, tree condition, tree body, int reversed)
14758 {
14759   tree to_return = ctxp->current_loop;
14760   tree loop_body = LOOP_EXPR_BODY (to_return);
14761   if (condition)
14762     {
14763       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14764       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14765          The real EXIT_EXPR is one operand further. */
14766       EXPR_WFL_LINECOL (cnode) = location;
14767       /* This one is for accurate error reports */
14768       EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14769       TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14770     }
14771   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14772   POP_LOOP ();
14773   return to_return;
14774 }
14775
14776 /* Tailored version of finish_loop_body for FOR loops, when FOR
14777    loops feature the condition part */
14778
14779 static tree
14780 finish_for_loop (int location, tree condition, tree update, tree body)
14781 {
14782   /* Put the condition and the loop body in place */
14783   tree loop = finish_loop_body (location, condition, body, 0);
14784   /* LOOP is the current loop which has been now popped of the loop
14785      stack.  Mark the update block as reachable and install it.  We do
14786      this because the (current interpretation of the) JLS requires
14787      that the update expression be considered reachable even if the
14788      for loop's body doesn't complete normally.  */
14789   if (update != NULL_TREE && update != empty_stmt_node)
14790     {
14791       tree up2 = update;
14792       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
14793         up2 = EXPR_WFL_NODE (up2);
14794       /* Try to detect constraint violations.  These would be
14795          programming errors somewhere.  */
14796       if (! IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (up2)))
14797           || TREE_CODE (up2) == LOOP_EXPR)
14798         abort ();
14799       SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
14800     }
14801   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14802   return loop;
14803 }
14804
14805 /* Try to find the loop a block might be related to. This comprises
14806    the case where the LOOP_EXPR is found as the second operand of a
14807    COMPOUND_EXPR, because the loop happens to have an initialization
14808    part, then expressed as the first operand of the COMPOUND_EXPR. If
14809    the search finds something, 1 is returned. Otherwise, 0 is
14810    returned. The search is assumed to start from a
14811    LABELED_BLOCK_EXPR's block.  */
14812
14813 static tree
14814 search_loop (tree statement)
14815 {
14816   if (TREE_CODE (statement) == LOOP_EXPR)
14817     return statement;
14818
14819   if (TREE_CODE (statement) == BLOCK)
14820     statement = BLOCK_SUBBLOCKS (statement);
14821   else
14822     return NULL_TREE;
14823
14824   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14825     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14826       statement = TREE_OPERAND (statement, 1);
14827
14828   return (TREE_CODE (statement) == LOOP_EXPR
14829           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14830 }
14831
14832 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14833    returned otherwise.  */
14834
14835 static int
14836 labeled_block_contains_loop_p (tree block, tree loop)
14837 {
14838   if (!block)
14839     return 0;
14840
14841   if (LABELED_BLOCK_BODY (block) == loop)
14842     return 1;
14843
14844   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14845     return 1;
14846
14847   return 0;
14848 }
14849
14850 /* If the loop isn't surrounded by a labeled statement, create one and
14851    insert LOOP as its body.  */
14852
14853 static tree
14854 patch_loop_statement (tree loop)
14855 {
14856   tree loop_label;
14857
14858   TREE_TYPE (loop) = void_type_node;
14859   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14860     return loop;
14861
14862   loop_label = build_labeled_block (0, NULL_TREE);
14863   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14864      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14865   LABELED_BLOCK_BODY (loop_label) = loop;
14866   PUSH_LABELED_BLOCK (loop_label);
14867   return loop_label;
14868 }
14869
14870 /* 14.13, 14.14: break and continue Statements */
14871
14872 /* Build a break or a continue statement. a null NAME indicates an
14873    unlabeled break/continue statement.  */
14874
14875 static tree
14876 build_bc_statement (int location, int is_break, tree name)
14877 {
14878   tree break_continue, label_block_expr = NULL_TREE;
14879
14880   if (name)
14881     {
14882       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14883             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14884         /* Null means that we don't have a target for this named
14885            break/continue. In this case, we make the target to be the
14886            label name, so that the error can be reported accuratly in
14887            patch_bc_statement. */
14888         label_block_expr = EXPR_WFL_NODE (name);
14889     }
14890   /* Unlabeled break/continue will be handled during the
14891      break/continue patch operation */
14892   break_continue
14893     = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14894
14895   IS_BREAK_STMT_P (break_continue) = is_break;
14896   TREE_SIDE_EFFECTS (break_continue) = 1;
14897   EXPR_WFL_LINECOL (break_continue) = location;
14898   break_continue = build_debugable_stmt (location, break_continue);
14899   return break_continue;
14900 }
14901
14902 /* Verification of a break/continue statement. */
14903
14904 static tree
14905 patch_bc_statement (tree node)
14906 {
14907   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14908   tree labeled_block = ctxp->current_labeled_block;
14909   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14910
14911   /* Having an identifier here means that the target is unknown. */
14912   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14913     {
14914       parse_error_context (wfl_operator, "No label definition found for `%s'",
14915                            IDENTIFIER_POINTER (bc_label));
14916       return error_mark_node;
14917     }
14918   if (! IS_BREAK_STMT_P (node))
14919     {
14920       /* It's a continue statement. */
14921       for (;; labeled_block = TREE_CHAIN (labeled_block))
14922         {
14923           if (labeled_block == NULL_TREE)
14924             {
14925               if (bc_label == NULL_TREE)
14926                 parse_error_context (wfl_operator,
14927                                      "`continue' must be in loop");
14928               else
14929                 parse_error_context
14930                   (wfl_operator, "continue label `%s' does not name a loop",
14931                    IDENTIFIER_POINTER (bc_label));
14932               return error_mark_node;
14933             }
14934           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14935                == continue_identifier_node)
14936               && (bc_label == NULL_TREE
14937                   || TREE_CHAIN (labeled_block) == bc_label))
14938             {
14939               bc_label = labeled_block;
14940               break;
14941             }
14942         }
14943     }
14944   else if (!bc_label)
14945     {
14946       for (;; labeled_block = TREE_CHAIN (labeled_block))
14947         {
14948           if (labeled_block == NULL_TREE)
14949             {
14950               parse_error_context (wfl_operator,
14951                                      "`break' must be in loop or switch");
14952               return error_mark_node;
14953             }
14954           target_stmt = LABELED_BLOCK_BODY (labeled_block);
14955           if (TREE_CODE (target_stmt) == SWITCH_EXPR
14956               || search_loop (target_stmt))
14957             {
14958               bc_label = labeled_block;
14959               break;
14960             }
14961         }
14962     }
14963
14964   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14965   CAN_COMPLETE_NORMALLY (bc_label) = 1;
14966
14967   /* Our break/continue don't return values. */
14968   TREE_TYPE (node) = void_type_node;
14969   /* Encapsulate the break within a compound statement so that it's
14970      expanded all the times by expand_expr (and not clobbered
14971      sometimes, like after a if statement) */
14972   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14973   TREE_SIDE_EFFECTS (node) = 1;
14974   return node;
14975 }
14976
14977 /* Process the exit expression belonging to a loop. Its type must be
14978    boolean.  */
14979
14980 static tree
14981 patch_exit_expr (tree node)
14982 {
14983   tree expression = TREE_OPERAND (node, 0);
14984   TREE_TYPE (node) = error_mark_node;
14985   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14986
14987   /* The type of expression must be boolean */
14988   if (TREE_TYPE (expression) != boolean_type_node)
14989     {
14990       parse_error_context
14991         (wfl_operator,
14992     "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14993          lang_printable_name (TREE_TYPE (expression), 0));
14994       return error_mark_node;
14995     }
14996   /* Now we know things are allright, invert the condition, fold and
14997      return */
14998   TREE_OPERAND (node, 0) =
14999     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15000
15001   if (! integer_zerop (TREE_OPERAND (node, 0))
15002       && ctxp->current_loop != NULL_TREE
15003       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15004     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15005   if (! integer_onep (TREE_OPERAND (node, 0)))
15006     CAN_COMPLETE_NORMALLY (node) = 1;
15007
15008
15009   TREE_TYPE (node) = void_type_node;
15010   return node;
15011 }
15012
15013 /* 14.9 Switch statement */
15014
15015 static tree
15016 patch_switch_statement (tree node)
15017 {
15018   tree se = TREE_OPERAND (node, 0), se_type;
15019   tree save, iter;
15020
15021   /* Complete the switch expression */
15022   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15023   se_type = TREE_TYPE (se);
15024   /* The type of the switch expression must be char, byte, short or
15025      int */
15026   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15027     {
15028       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15029       parse_error_context (wfl_operator,
15030           "Incompatible type for `switch'. Can't convert `%s' to `int'",
15031                            lang_printable_name (se_type, 0));
15032       /* This is what java_complete_tree will check */
15033       TREE_OPERAND (node, 0) = error_mark_node;
15034       return error_mark_node;
15035     }
15036
15037   /* Save and restore the outer case label list.  */
15038   save = case_label_list;
15039   case_label_list = NULL_TREE;
15040
15041   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15042
15043   /* See if we've found a duplicate label.  We can't leave this until
15044      code generation, because in `--syntax-only' and `-C' modes we
15045      don't do ordinary code generation.  */
15046   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15047     {
15048       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15049       tree subiter;
15050       for (subiter = TREE_CHAIN (iter);
15051            subiter != NULL_TREE;
15052            subiter = TREE_CHAIN (subiter))
15053         {
15054           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15055           if (val == subval)
15056             {
15057               EXPR_WFL_LINECOL (wfl_operator)
15058                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15059               /* The case_label_list is in reverse order, so print the
15060                  outer label first.  */
15061               parse_error_context (wfl_operator, "duplicate case label: `"
15062                                    HOST_WIDE_INT_PRINT_DEC "'", subval);
15063               EXPR_WFL_LINECOL (wfl_operator)
15064                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15065               parse_error_context (wfl_operator, "original label is here");
15066
15067               break;
15068             }
15069         }
15070     }
15071
15072   case_label_list = save;
15073
15074   /* Ready to return */
15075   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15076     {
15077       TREE_TYPE (node) = error_mark_node;
15078       return error_mark_node;
15079     }
15080   TREE_TYPE (node) = void_type_node;
15081   TREE_SIDE_EFFECTS (node) = 1;
15082   CAN_COMPLETE_NORMALLY (node)
15083     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15084       || ! SWITCH_HAS_DEFAULT (node);
15085   return node;
15086 }
15087
15088 /* Assertions.  */
15089
15090 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15091    might be NULL_TREE.  */
15092 static tree
15093 build_assertion (int location, tree condition, tree value)
15094 {
15095   tree node;
15096   tree klass = GET_CPC ();
15097
15098   if (! CLASS_USES_ASSERTIONS (klass))
15099     {
15100       tree field, classdollar, id, call;
15101       tree class_type = TREE_TYPE (klass);
15102
15103       field = add_field (class_type,
15104                          get_identifier ("$assertionsDisabled"),
15105                          boolean_type_node,
15106                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15107       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15108       FIELD_SYNTHETIC (field) = 1;
15109
15110       if (!TYPE_DOT_CLASS (class_type))
15111         build_dot_class_method (class_type);
15112       classdollar = build_dot_class_method_invocation (class_type);
15113
15114       /* Call CLASS.desiredAssertionStatus().  */
15115       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15116       call = build (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15117       call = make_qualified_primary (classdollar, call, location);
15118       TREE_SIDE_EFFECTS (call) = 1;
15119
15120       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15121          seem odd, but we do it to generate code identical to that of
15122          the JDK.  */
15123       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15124       TREE_SIDE_EFFECTS (call) = 1;
15125       DECL_INITIAL (field) = call;
15126
15127       /* Record the initializer in the initializer statement list.  */
15128       call = build (MODIFY_EXPR, NULL_TREE, field, call);
15129       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15130       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15131       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15132
15133       CLASS_USES_ASSERTIONS (klass) = 1;
15134     }
15135
15136   if (value != NULL_TREE)
15137     value = tree_cons (NULL_TREE, value, NULL_TREE);
15138
15139   node = build_wfl_node (get_identifier ("java"));
15140   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15141                               location);
15142   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15143                               location);
15144
15145   node = build (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15146   TREE_SIDE_EFFECTS (node) = 1;
15147   /* It is too early to use BUILD_THROW.  */
15148   node = build1 (THROW_EXPR, NULL_TREE, node);
15149   TREE_SIDE_EFFECTS (node) = 1;
15150
15151   /* We invert the condition; if we just put NODE as the `else' part
15152      then we generate weird-looking bytecode.  */
15153   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15154   /* Check $assertionsDisabled.  */
15155   condition
15156     = build (TRUTH_ANDIF_EXPR, NULL_TREE,
15157              build1 (TRUTH_NOT_EXPR, NULL_TREE,
15158                      build_wfl_node (get_identifier ("$assertionsDisabled"))),
15159              condition);
15160   node = build_if_else_statement (location, condition, node, NULL_TREE);
15161   return node;
15162 }
15163
15164 /* 14.18 The try/catch statements */
15165
15166 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15167    catches TYPE and executes CATCH_STMTS.  */
15168
15169 static tree
15170 encapsulate_with_try_catch (int location, tree type, tree try_stmts,
15171                             tree catch_stmts)
15172 {
15173   tree try_block, catch_clause_param, catch_block, catch;
15174
15175   /* First build a try block */
15176   try_block = build_expr_block (try_stmts, NULL_TREE);
15177
15178   /* Build a catch block: we need a catch clause parameter */
15179   catch_clause_param = build_decl (VAR_DECL,
15180                                    wpv_id, build_pointer_type (type));
15181   /* And a block */
15182   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15183
15184   /* Initialize the variable and store in the block */
15185   catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15186                  build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15187   add_stmt_to_block (catch_block, NULL_TREE, catch);
15188
15189   /* Add the catch statements */
15190   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15191
15192   /* Now we can build a CATCH_EXPR */
15193   catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15194
15195   return build_try_statement (location, try_block, catch_block);
15196 }
15197
15198 static tree
15199 build_try_statement (int location, tree try_block, tree catches)
15200 {
15201   tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15202   EXPR_WFL_LINECOL (node) = location;
15203   return node;
15204 }
15205
15206 static tree
15207 build_try_finally_statement (int location, tree try_block, tree finally)
15208 {
15209   tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15210   EXPR_WFL_LINECOL (node) = location;
15211   return node;
15212 }
15213
15214 static tree
15215 patch_try_statement (tree node)
15216 {
15217   int error_found = 0;
15218   tree try = TREE_OPERAND (node, 0);
15219   /* Exception handlers are considered in left to right order */
15220   tree catch = nreverse (TREE_OPERAND (node, 1));
15221   tree current, caught_type_list = NULL_TREE;
15222
15223   /* Check catch clauses, if any. Every time we find an error, we try
15224      to process the next catch clause. We process the catch clause before
15225      the try block so that when processing the try block we can check thrown
15226      exceptions againts the caught type list. */
15227   for (current = catch; current; current = TREE_CHAIN (current))
15228     {
15229       tree carg_decl, carg_type;
15230       tree sub_current, catch_block, catch_clause;
15231       int unreachable;
15232
15233       /* At this point, the structure of the catch clause is
15234            CATCH_EXPR           (catch node)
15235              BLOCK              (with the decl of the parameter)
15236                COMPOUND_EXPR
15237                  MODIFY_EXPR   (assignment of the catch parameter)
15238                  BLOCK          (catch clause block)
15239        */
15240       catch_clause = TREE_OPERAND (current, 0);
15241       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15242       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15243
15244       /* Catch clauses can't have more than one parameter declared,
15245          but it's already enforced by the grammar. Make sure that the
15246          only parameter of the clause statement in of class Throwable
15247          or a subclass of Throwable, but that was done earlier. The
15248          catch clause parameter type has also been resolved. */
15249
15250       /* Just make sure that the catch clause parameter type inherits
15251          from java.lang.Throwable */
15252       if (!inherits_from_p (carg_type, throwable_type_node))
15253         {
15254           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15255           parse_error_context (wfl_operator,
15256                                "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15257                                lang_printable_name (carg_type, 0));
15258           error_found = 1;
15259           continue;
15260         }
15261
15262       /* Partial check for unreachable catch statement: The catch
15263          clause is reachable iff is no earlier catch block A in
15264          the try statement such that the type of the catch
15265          clause's parameter is the same as or a subclass of the
15266          type of A's parameter */
15267       unreachable = 0;
15268       for (sub_current = catch;
15269            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15270         {
15271           tree sub_catch_clause, decl;
15272           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15273           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15274
15275           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15276             {
15277               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15278               parse_error_context
15279                 (wfl_operator,
15280                  "`catch' not reached because of the catch clause at line %d",
15281                  EXPR_WFL_LINENO (sub_current));
15282               unreachable = error_found = 1;
15283               break;
15284             }
15285         }
15286       /* Complete the catch clause block */
15287       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15288       if (catch_block == error_mark_node)
15289         {
15290           error_found = 1;
15291           continue;
15292         }
15293       if (CAN_COMPLETE_NORMALLY (catch_block))
15294         CAN_COMPLETE_NORMALLY (node) = 1;
15295       TREE_OPERAND (current, 0) = catch_block;
15296
15297       if (unreachable)
15298         continue;
15299
15300       /* Things to do here: the exception must be thrown */
15301
15302       /* Link this type to the caught type list */
15303       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15304     }
15305
15306   PUSH_EXCEPTIONS (caught_type_list);
15307   if ((try = java_complete_tree (try)) == error_mark_node)
15308     error_found = 1;
15309   if (CAN_COMPLETE_NORMALLY (try))
15310     CAN_COMPLETE_NORMALLY (node) = 1;
15311   POP_EXCEPTIONS ();
15312
15313   /* Verification ends here */
15314   if (error_found)
15315     return error_mark_node;
15316
15317   TREE_OPERAND (node, 0) = try;
15318   TREE_OPERAND (node, 1) = catch;
15319   TREE_TYPE (node) = void_type_node;
15320   return node;
15321 }
15322
15323 /* 14.17 The synchronized Statement */
15324
15325 static tree
15326 patch_synchronized_statement (tree node, tree wfl_op1)
15327 {
15328   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15329   tree block = TREE_OPERAND (node, 1);
15330
15331   tree tmp, enter, exit, expr_decl, assignment;
15332
15333   if (expr == error_mark_node)
15334     {
15335       block = java_complete_tree (block);
15336       return expr;
15337     }
15338
15339   /* We might be trying to synchronize on a STRING_CST */
15340   if ((tmp = patch_string (expr)))
15341     expr = tmp;
15342
15343   /* The TYPE of expr must be a reference type */
15344   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15345     {
15346       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15347       parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15348                            lang_printable_name (TREE_TYPE (expr), 0));
15349       return error_mark_node;
15350     }
15351
15352   if (flag_emit_xref)
15353     {
15354       TREE_OPERAND (node, 0) = expr;
15355       TREE_OPERAND (node, 1) = java_complete_tree (block);
15356       CAN_COMPLETE_NORMALLY (node) = 1;
15357       return node;
15358     }
15359
15360   /* Generate a try-finally for the synchronized statement, except
15361      that the handler that catches all throw exception calls
15362      _Jv_MonitorExit and then rethrow the exception.
15363      The synchronized statement is then implemented as:
15364      TRY
15365        {
15366          _Jv_MonitorEnter (expression)
15367          synchronized_block
15368          _Jv_MonitorExit (expression)
15369        }
15370      CATCH_ALL
15371        {
15372          e = _Jv_exception_info ();
15373          _Jv_MonitorExit (expression)
15374          Throw (e);
15375        } */
15376
15377   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15378   BUILD_MONITOR_ENTER (enter, expr_decl);
15379   BUILD_MONITOR_EXIT (exit, expr_decl);
15380   CAN_COMPLETE_NORMALLY (enter) = 1;
15381   CAN_COMPLETE_NORMALLY (exit) = 1;
15382   assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15383   TREE_SIDE_EFFECTS (assignment) = 1;
15384   node = build (COMPOUND_EXPR, NULL_TREE,
15385                 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15386                 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15387   node = build_expr_block (node, expr_decl);
15388
15389   return java_complete_tree (node);
15390 }
15391
15392 /* 14.16 The throw Statement */
15393
15394 static tree
15395 patch_throw_statement (tree node, tree wfl_op1)
15396 {
15397   tree expr = TREE_OPERAND (node, 0);
15398   tree type = TREE_TYPE (expr);
15399   int unchecked_ok = 0, tryblock_throws_ok = 0;
15400
15401   /* Thrown expression must be assignable to java.lang.Throwable */
15402   if (!try_reference_assignconv (throwable_type_node, expr))
15403     {
15404       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15405       parse_error_context (wfl_operator,
15406     "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15407                            lang_printable_name (type, 0));
15408       /* If the thrown expression was a reference, we further the
15409          compile-time check. */
15410       if (!JREFERENCE_TYPE_P (type))
15411         return error_mark_node;
15412     }
15413
15414   /* At least one of the following must be true */
15415
15416   /* The type of the throw expression is a not checked exception,
15417      i.e. is a unchecked expression. */
15418   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15419
15420   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15421   /* An instance can't throw a checked exception unless that exception
15422      is explicitly declared in the `throws' clause of each
15423      constructor. This doesn't apply to anonymous classes, since they
15424      don't have declared constructors. */
15425   if (!unchecked_ok
15426       && DECL_INSTINIT_P (current_function_decl)
15427       && !ANONYMOUS_CLASS_P (current_class))
15428     {
15429       tree current;
15430       for (current = TYPE_METHODS (current_class); current;
15431            current = TREE_CHAIN (current))
15432         if (DECL_CONSTRUCTOR_P (current)
15433             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15434           {
15435             parse_error_context (wfl_operator, "Checked exception `%s' can't be thrown in instance initializer (not all declared constructor are declaring it in their `throws' clause)",
15436                                  lang_printable_name (TREE_TYPE (expr), 0));
15437             return error_mark_node;
15438           }
15439     }
15440
15441   /* Throw is contained in a try statement and at least one catch
15442      clause can receive the thrown expression or the current method is
15443      declared to throw such an exception. Or, the throw statement is
15444      contained in a method or constructor declaration and the type of
15445      the Expression is assignable to at least one type listed in the
15446      throws clause the declaration. */
15447   if (!unchecked_ok)
15448     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15449   if (!(unchecked_ok || tryblock_throws_ok))
15450     {
15451       /* If there is a surrounding try block that has no matching
15452          clatch clause, report it first. A surrounding try block exits
15453          only if there is something after the list of checked
15454          exception thrown by the current function (if any). */
15455       if (IN_TRY_BLOCK_P ())
15456         parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15457                              lang_printable_name (type, 0));
15458       /* If we have no surrounding try statement and the method doesn't have
15459          any throws, report it now. FIXME */
15460
15461       /* We report that the exception can't be throw from a try block
15462          in all circumstances but when the `throw' is inside a static
15463          block. */
15464       else if (!EXCEPTIONS_P (currently_caught_type_list)
15465                && !tryblock_throws_ok)
15466         {
15467           if (DECL_CLINIT_P (current_function_decl))
15468             parse_error_context (wfl_operator,
15469                    "Checked exception `%s' can't be thrown in initializer",
15470                                  lang_printable_name (type, 0));
15471           else
15472             parse_error_context (wfl_operator,
15473                    "Checked exception `%s' isn't thrown from a `try' block",
15474                                  lang_printable_name (type, 0));
15475         }
15476       /* Otherwise, the current method doesn't have the appropriate
15477          throws declaration */
15478       else
15479         parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15480                              lang_printable_name (type, 0));
15481       return error_mark_node;
15482     }
15483
15484   if (! flag_emit_class_files && ! flag_emit_xref)
15485     BUILD_THROW (node, expr);
15486
15487   /* If doing xrefs, keep the location where the `throw' was seen. */
15488   if (flag_emit_xref)
15489     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15490   return node;
15491 }
15492
15493 /* Check that exception said to be thrown by method DECL can be
15494    effectively caught from where DECL is invoked.  */
15495
15496 static void
15497 check_thrown_exceptions (int location, tree decl)
15498 {
15499   tree throws;
15500   /* For all the unchecked exceptions thrown by DECL */
15501   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15502        throws = TREE_CHAIN (throws))
15503     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15504       {
15505 #if 1
15506         /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15507         if (DECL_NAME (decl) == get_identifier ("clone"))
15508           continue;
15509 #endif
15510         EXPR_WFL_LINECOL (wfl_operator) = location;
15511         if (DECL_FINIT_P (current_function_decl))
15512           parse_error_context
15513             (wfl_operator, "Exception `%s' can't be thrown in initializer",
15514              lang_printable_name (TREE_VALUE (throws), 0));
15515         else
15516           {
15517             parse_error_context
15518               (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15519                lang_printable_name (TREE_VALUE (throws), 0),
15520                (DECL_INIT_P (current_function_decl) ?
15521                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15522                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15523           }
15524       }
15525 }
15526
15527 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15528    try-catch blocks, OR is listed in the `throws' clause of the
15529    current method.  */
15530
15531 static int
15532 check_thrown_exceptions_do (tree exception)
15533 {
15534   tree list = currently_caught_type_list;
15535   resolve_and_layout (exception, NULL_TREE);
15536   /* First, all the nested try-catch-finally at that stage. The
15537      last element contains `throws' clause exceptions, if any. */
15538   if (IS_UNCHECKED_EXCEPTION_P (exception))
15539     return 1;
15540   while (list)
15541     {
15542       tree caught;
15543       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15544         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15545           return 1;
15546       list = TREE_CHAIN (list);
15547     }
15548   return 0;
15549 }
15550
15551 static void
15552 purge_unchecked_exceptions (tree mdecl)
15553 {
15554   tree throws = DECL_FUNCTION_THROWS (mdecl);
15555   tree new = NULL_TREE;
15556
15557   while (throws)
15558     {
15559       tree next = TREE_CHAIN (throws);
15560       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15561         {
15562           TREE_CHAIN (throws) = new;
15563           new = throws;
15564         }
15565       throws = next;
15566     }
15567   /* List is inverted here, but it doesn't matter */
15568   DECL_FUNCTION_THROWS (mdecl) = new;
15569 }
15570
15571 /* This function goes over all of CLASS_TYPE ctors and checks whether
15572    each of them features at least one unchecked exception in its
15573    `throws' clause. If it's the case, it returns `true', `false'
15574    otherwise.  */
15575
15576 static bool
15577 ctors_unchecked_throws_clause_p (tree class_type)
15578 {
15579   tree current;
15580
15581   for (current = TYPE_METHODS (class_type); current;
15582        current = TREE_CHAIN (current))
15583     {
15584       bool ctu = false; /* Ctor Throws Unchecked */
15585       if (DECL_CONSTRUCTOR_P (current))
15586         {
15587           tree throws;
15588           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15589                throws = TREE_CHAIN (throws))
15590             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15591               ctu = true;
15592         }
15593       /* We return false as we found one ctor that is unfit. */
15594       if (!ctu && DECL_CONSTRUCTOR_P (current))
15595         return false;
15596     }
15597   /* All ctors feature at least one unchecked exception in their
15598      `throws' clause. */
15599   return true;
15600 }
15601
15602 /* 15.24 Conditional Operator ?: */
15603
15604 static tree
15605 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
15606 {
15607   tree cond = TREE_OPERAND (node, 0);
15608   tree op1 = TREE_OPERAND (node, 1);
15609   tree op2 = TREE_OPERAND (node, 2);
15610   tree resulting_type = NULL_TREE;
15611   tree t1, t2, patched;
15612   int error_found = 0;
15613
15614   /* Operands of ?: might be StringBuffers crafted as a result of a
15615      string concatenation. Obtain a descent operand here.  */
15616   if ((patched = patch_string (op1)))
15617     TREE_OPERAND (node, 1) = op1 = patched;
15618   if ((patched = patch_string (op2)))
15619     TREE_OPERAND (node, 2) = op2 = patched;
15620
15621   t1 = TREE_TYPE (op1);
15622   t2 = TREE_TYPE (op2);
15623
15624   /* The first expression must be a boolean */
15625   if (TREE_TYPE (cond) != boolean_type_node)
15626     {
15627       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15628       parse_error_context (wfl_operator,
15629                "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15630                            lang_printable_name (TREE_TYPE (cond), 0));
15631       error_found = 1;
15632     }
15633
15634   /* Second and third can be numeric, boolean (i.e. primitive),
15635      references or null. Anything else results in an error */
15636   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15637         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15638             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15639         || (t1 == boolean_type_node && t2 == boolean_type_node)))
15640     error_found = 1;
15641
15642   /* Determine the type of the conditional expression. Same types are
15643      easy to deal with */
15644   else if (t1 == t2)
15645     resulting_type = t1;
15646
15647   /* There are different rules for numeric types */
15648   else if (JNUMERIC_TYPE_P (t1))
15649     {
15650       /* if byte/short found, the resulting type is short */
15651       if ((t1 == byte_type_node && t2 == short_type_node)
15652           || (t1 == short_type_node && t2 == byte_type_node))
15653         resulting_type = short_type_node;
15654
15655       /* If t1 is a constant int and t2 is of type byte, short or char
15656          and t1's value fits in t2, then the resulting type is t2 */
15657       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15658           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15659         resulting_type = t2;
15660
15661       /* If t2 is a constant int and t1 is of type byte, short or char
15662          and t2's value fits in t1, then the resulting type is t1 */
15663       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15664           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15665         resulting_type = t1;
15666
15667       /* Otherwise, binary numeric promotion is applied and the
15668          resulting type is the promoted type of operand 1 and 2 */
15669       else
15670         resulting_type = binary_numeric_promotion (t1, t2,
15671                                                    &TREE_OPERAND (node, 1),
15672                                                    &TREE_OPERAND (node, 2));
15673     }
15674
15675   /* Cases of a reference and a null type */
15676   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15677     resulting_type = t1;
15678
15679   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15680     resulting_type = t2;
15681
15682   /* Last case: different reference types. If a type can be converted
15683      into the other one by assignment conversion, the latter
15684      determines the type of the expression */
15685   else if ((resulting_type = try_reference_assignconv (t1, op2)))
15686     resulting_type = promote_type (t1);
15687
15688   else if ((resulting_type = try_reference_assignconv (t2, op1)))
15689     resulting_type = promote_type (t2);
15690
15691   /* If we don't have any resulting type, we're in trouble */
15692   if (!resulting_type)
15693     {
15694       char *t = xstrdup (lang_printable_name (t1, 0));
15695       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15696       parse_error_context (wfl_operator,
15697                  "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15698                            t, lang_printable_name (t2, 0));
15699       free (t);
15700       error_found = 1;
15701     }
15702
15703   if (error_found)
15704     {
15705       TREE_TYPE (node) = error_mark_node;
15706       return error_mark_node;
15707     }
15708
15709   TREE_TYPE (node) = resulting_type;
15710   TREE_SET_CODE (node, COND_EXPR);
15711   CAN_COMPLETE_NORMALLY (node) = 1;
15712   return node;
15713 }
15714
15715 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15716
15717 static tree
15718 maybe_build_class_init_for_field (tree decl, tree expr)
15719 {
15720   tree clas = DECL_CONTEXT (decl);
15721   if (flag_emit_class_files || flag_emit_xref)
15722     return expr;
15723
15724   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15725       && FIELD_FINAL (decl))
15726     {
15727       tree init = DECL_INITIAL (decl);
15728       if (init != NULL_TREE)
15729         init = fold_constant_for_init (init, decl);
15730       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15731         return expr;
15732     }
15733
15734   return build_class_init (clas, expr);
15735 }
15736
15737 /* Try to constant fold NODE.
15738    If NODE is not a constant expression, return NULL_EXPR.
15739    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15740
15741 static tree
15742 fold_constant_for_init (tree node, tree context)
15743 {
15744   tree op0, op1, val;
15745   enum tree_code code = TREE_CODE (node);
15746
15747   switch (code)
15748     {
15749     case INTEGER_CST:
15750       if (node == null_pointer_node)
15751         return NULL_TREE;
15752     case STRING_CST:
15753     case REAL_CST:
15754       return node;
15755
15756     case PLUS_EXPR:
15757     case MINUS_EXPR:
15758     case MULT_EXPR:
15759     case TRUNC_MOD_EXPR:
15760     case RDIV_EXPR:
15761     case LSHIFT_EXPR:
15762     case RSHIFT_EXPR:
15763     case URSHIFT_EXPR:
15764     case BIT_AND_EXPR:
15765     case BIT_XOR_EXPR:
15766     case BIT_IOR_EXPR:
15767     case TRUTH_ANDIF_EXPR:
15768     case TRUTH_ORIF_EXPR:
15769     case EQ_EXPR:
15770     case NE_EXPR:
15771     case GT_EXPR:
15772     case GE_EXPR:
15773     case LT_EXPR:
15774     case LE_EXPR:
15775       op0 = TREE_OPERAND (node, 0);
15776       op1 = TREE_OPERAND (node, 1);
15777       val = fold_constant_for_init (op0, context);
15778       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15779         return NULL_TREE;
15780       TREE_OPERAND (node, 0) = val;
15781       val = fold_constant_for_init (op1, context);
15782       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15783         return NULL_TREE;
15784       TREE_OPERAND (node, 1) = val;
15785       return patch_binop (node, op0, op1);
15786
15787     case UNARY_PLUS_EXPR:
15788     case NEGATE_EXPR:
15789     case TRUTH_NOT_EXPR:
15790     case BIT_NOT_EXPR:
15791     case CONVERT_EXPR:
15792       op0 = TREE_OPERAND (node, 0);
15793       val = fold_constant_for_init (op0, context);
15794       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15795         return NULL_TREE;
15796       TREE_OPERAND (node, 0) = val;
15797       return patch_unaryop (node, op0);
15798       break;
15799
15800     case COND_EXPR:
15801       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15802       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15803         return NULL_TREE;
15804       TREE_OPERAND (node, 0) = val;
15805       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15806       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15807         return NULL_TREE;
15808       TREE_OPERAND (node, 1) = val;
15809       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15810       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15811         return NULL_TREE;
15812       TREE_OPERAND (node, 2) = val;
15813       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15814         : TREE_OPERAND (node, 2);
15815
15816     case VAR_DECL:
15817     case FIELD_DECL:
15818       if (! FIELD_FINAL (node)
15819           || DECL_INITIAL (node) == NULL_TREE)
15820         return NULL_TREE;
15821       val = DECL_INITIAL (node);
15822       /* Guard against infinite recursion. */
15823       DECL_INITIAL (node) = NULL_TREE;
15824       val = fold_constant_for_init (val, node);
15825       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
15826         val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
15827       DECL_INITIAL (node) = val;
15828       return val;
15829
15830     case EXPR_WITH_FILE_LOCATION:
15831       /* Compare java_complete_tree and resolve_expression_name. */
15832       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15833           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15834         {
15835           tree name = EXPR_WFL_NODE (node);
15836           tree decl;
15837           if (PRIMARY_P (node))
15838             return NULL_TREE;
15839           else if (! QUALIFIED_P (name))
15840             {
15841               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15842               if (decl == NULL_TREE
15843                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15844                 return NULL_TREE;
15845               return fold_constant_for_init (decl, decl);
15846             }
15847           else
15848             {
15849               /* Install the proper context for the field resolution.
15850                  The prior context is restored once the name is
15851                  properly qualified. */
15852               tree saved_current_class = current_class;
15853               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
15854               current_class = DECL_CONTEXT (context);
15855               qualify_ambiguous_name (node);
15856               current_class = saved_current_class;
15857               if (resolve_field_access (node, &decl, NULL)
15858                   && decl != NULL_TREE)
15859                 return fold_constant_for_init (decl, decl);
15860               return NULL_TREE;
15861             }
15862         }
15863       else
15864         {
15865           op0 = TREE_OPERAND (node, 0);
15866           val = fold_constant_for_init (op0, context);
15867           if (val == NULL_TREE || ! TREE_CONSTANT (val))
15868             return NULL_TREE;
15869           TREE_OPERAND (node, 0) = val;
15870           return val;
15871         }
15872
15873 #ifdef USE_COMPONENT_REF
15874     case IDENTIFIER:
15875     case COMPONENT_REF:
15876       ?;
15877 #endif
15878
15879     default:
15880       return NULL_TREE;
15881     }
15882 }
15883
15884 #ifdef USE_COMPONENT_REF
15885 /* Context is 'T' for TypeName, 'P' for PackageName,
15886    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15887
15888 tree
15889 resolve_simple_name (tree name, int context)
15890 {
15891 }
15892
15893 tree
15894 resolve_qualified_name (tree name, int context)
15895 {
15896 }
15897 #endif
15898
15899 void
15900 init_src_parse (void)
15901 {
15902   /* Sanity check; we've been bit by this before.  */
15903   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
15904     abort ();
15905 }
15906
15907 \f
15908
15909 /* This section deals with the functions that are called when tables
15910    recording class initialization information are traversed.  */
15911
15912 /* Attach to PTR (a block) the declaration found in ENTRY. */
15913
15914 static int
15915 attach_init_test_initialization_flags (void **entry, void *ptr)
15916 {
15917   tree block = (tree)ptr;
15918   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
15919
15920   if (block != error_mark_node)
15921     {
15922       TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
15923       BLOCK_EXPR_DECLS (block) = ite->value;
15924     }
15925   return true;
15926 }
15927
15928 /* This function is called for each classes that is known definitely
15929    assigned when a given static method was called. This function
15930    augments a compound expression (INFO) storing all assignment to
15931    initialized static class flags if a flag already existed, otherwise
15932    a new one is created.  */
15933
15934 static int
15935 emit_test_initialization (void **entry_p, void *info)
15936 {
15937   tree l = (tree) info;
15938   tree decl, init;
15939   tree key = (tree) *entry_p;
15940   tree *ite;
15941   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
15942
15943   /* If we haven't found a flag and we're dealing with self registered
15944      with current_function_decl, then don't do anything. Self is
15945      always added as definitely initialized but this information is
15946      valid only if used outside the current function. */
15947   if (current_function_decl == TREE_PURPOSE (l)
15948       && java_treetreehash_find (cf_ht, key) == NULL)
15949     return true;
15950
15951   ite = java_treetreehash_new (cf_ht, key);
15952
15953   /* If we don't have a variable, create one and install it. */
15954   if (*ite == NULL)
15955     {
15956       tree block;
15957
15958       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
15959       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
15960       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
15961       DECL_CONTEXT (decl) = current_function_decl;
15962       DECL_INITIAL (decl) = boolean_true_node;
15963
15964       /* The trick is to find the right context for it. */
15965       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
15966       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
15967       BLOCK_EXPR_DECLS (block) = decl;
15968       *ite = decl;
15969     }
15970   else
15971     decl = *ite;
15972
15973   /* Now simply augment the compound that holds all the assignments
15974      pertaining to this method invocation. */
15975   init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
15976   TREE_SIDE_EFFECTS (init) = 1;
15977   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
15978   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
15979
15980   return true;
15981 }
15982
15983 #include "gt-java-parse.h"
15984 #include "gtype-java.h"