OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [java/] [parse.y] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Source code parsing and tree node generation for the GNU compiler
2
   for the Java(TM) language.
3
   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4
   Free Software Foundation, Inc.
5
   Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6
 
7
This file is part of GCC.
8
 
9
GCC is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2, or (at your option)
12
any later version.
13
 
14
GCC is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING.  If not, write to
21
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22
Boston, MA 02110-1301, USA.
23
 
24
Java and all Java-based marks are trademarks or registered trademarks
25
of Sun Microsystems, Inc. in the United States and other countries.
26
The Free Software Foundation is independent of Sun Microsystems, Inc.  */
27
 
28
/* This file parses java source code and issues a tree node image
29
suitable for code generation (byte code and targeted CPU assembly
30
language).
31
 
32
The grammar conforms to the Java grammar described in "The Java(TM)
33
Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34
1996, ISBN 0-201-63451-1"
35
 
36
The following modifications were brought to the original grammar:
37
 
38
method_body: added the rule '| block SC_TK'
39
static_initializer: added the rule 'static block SC_TK'.
40
 
41
Note: All the extra rules described above should go away when the
42
      empty_statement rule will work.
43
 
44
statement_nsi: 'nsi' should be read no_short_if.
45
 
46
Some rules have been modified to support JDK1.1 inner classes
47
definitions and other extensions.  */
48
 
49
%{
50
#include "config.h"
51
#include "system.h"
52
#include "coretypes.h"
53
#include "tm.h"
54
#include 
55
#include "tree.h"
56
#include "rtl.h"
57
#include "real.h"
58
#include "obstack.h"
59
#include "toplev.h"
60
#include "pretty-print.h"
61
#include "diagnostic.h"
62
#include "flags.h"
63
#include "java-tree.h"
64
#include "jcf.h"
65
#include "lex.h"
66
#include "parse.h"
67
#include "zipfile.h"
68
#include "convert.h"
69
#include "buffer.h"
70
#include "function.h"
71
#include "except.h"
72
#include "ggc.h"
73
#include "debug.h"
74
#include "tree-inline.h"
75
#include "tree-dump.h"
76
#include "cgraph.h"
77
#include "target.h"
78
 
79
/* Local function prototypes */
80
static char *java_accstring_lookup (int);
81
static const char *accessibility_string (int);
82
static void  classitf_redefinition_error (const char *,tree, tree, tree);
83
static void  variable_redefinition_error (tree, tree, tree, int);
84
static tree  create_class (int, tree, tree, tree);
85
static tree  create_interface (int, tree, tree);
86
static void  end_class_declaration (int);
87
static tree  find_field (tree, tree);
88
static tree lookup_field_wrapper (tree, tree);
89
static int   duplicate_declaration_error_p (tree, tree, tree);
90
static void  register_fields (int, tree, tree);
91
static tree parser_qualified_classname (tree);
92
static int  parser_check_super (tree, tree, tree);
93
static int  parser_check_super_interface (tree, tree, tree);
94
static void check_modifiers_consistency (int);
95
static tree lookup_cl (tree);
96
static tree lookup_java_method2 (tree, tree, int);
97
static tree method_header (int, tree, tree, tree);
98
static void fix_method_argument_names (tree ,tree);
99
static tree method_declarator (tree, tree);
100
static void parse_warning_context (tree cl, const char *gmsgid, ...) ATTRIBUTE_GCC_DIAG(2,3);
101
#ifdef USE_MAPPED_LOCATION
102
static void issue_warning_error_from_context
103
  (source_location, const char *gmsgid, va_list *);
104
#else
105
static void issue_warning_error_from_context
106
  (tree, const char *gmsgid, va_list *);
107
#endif
108
static void parse_ctor_invocation_error (void);
109
static tree parse_jdk1_1_error (const char *);
110
static void complete_class_report_errors (jdep *);
111
static int process_imports (void);
112
static void read_import_dir (tree);
113
static int find_in_imports_on_demand (tree, tree);
114
static void find_in_imports (tree, tree);
115
static bool inner_class_accessible (tree, tree);
116
static void check_inner_class_access (tree, tree, tree);
117
static int check_pkg_class_access (tree, tree, bool, tree);
118
static tree resolve_package (tree, tree *, tree *);
119
static tree resolve_class (tree, tree, tree, tree);
120
static void declare_local_variables (int, tree, tree);
121
static void dump_java_tree (enum tree_dump_index, tree);
122
static void source_start_java_method (tree);
123
static void source_end_java_method (void);
124
static tree find_name_in_single_imports (tree);
125
static void check_abstract_method_header (tree);
126
static tree lookup_java_interface_method2 (tree, tree);
127
static tree resolve_expression_name (tree, tree *);
128
static tree maybe_create_class_interface_decl (tree, tree, tree, tree);
129
static int check_class_interface_creation (int, int, tree, tree, tree, tree);
130
static tree patch_method_invocation (tree, tree, tree, int, int *, tree *);
131
static tree resolve_and_layout (tree, tree);
132
static tree qualify_and_find (tree, tree, tree);
133
static tree resolve_no_layout (tree, tree);
134
static int invocation_mode (tree, int);
135
static tree find_applicable_accessible_methods_list (int, tree, tree, tree);
136
static void search_applicable_methods_list (int, tree, tree, tree, tree *, tree *);
137
static tree find_most_specific_methods_list (tree);
138
static int argument_types_convertible (tree, tree);
139
static tree patch_invoke (tree, tree, tree);
140
static int maybe_use_access_method (int, tree *, tree *);
141
static tree lookup_method_invoke (int, tree, tree, tree, tree);
142
static tree register_incomplete_type (int, tree, tree, tree);
143
static tree check_inner_circular_reference (tree, tree);
144
static tree check_circular_reference (tree);
145
static tree obtain_incomplete_type (tree);
146
static tree java_complete_lhs (tree);
147
static tree java_complete_tree (tree);
148
static tree maybe_generate_pre_expand_clinit (tree);
149
static int analyze_clinit_body (tree, tree);
150
static int maybe_yank_clinit (tree);
151
static void start_complete_expand_method (tree);
152
static void java_complete_expand_method (tree);
153
static void java_expand_method_bodies (tree);
154
static int  unresolved_type_p (tree, tree *);
155
static void create_jdep_list (struct parser_ctxt *);
156
static tree build_expr_block (tree, tree);
157
static tree enter_block (void);
158
static tree exit_block (void);
159
static tree lookup_name_in_blocks (tree);
160
static void maybe_absorb_scoping_blocks (void);
161
static tree build_method_invocation (tree, tree);
162
static tree build_new_invocation (tree, tree);
163
static tree build_assignment (int, int, tree, tree);
164
static tree build_binop (enum tree_code, int, tree, tree);
165
static tree patch_assignment (tree, tree);
166
static tree patch_binop (tree, tree, tree, int);
167
static tree build_unaryop (int, int, tree);
168
static tree build_incdec (int, int, tree, int);
169
static tree patch_unaryop (tree, tree);
170
static tree build_cast (int, tree, tree);
171
static tree build_null_of_type (tree);
172
static tree patch_cast (tree, tree);
173
static int valid_ref_assignconv_cast_p (tree, tree, int);
174
static int valid_builtin_assignconv_identity_widening_p (tree, tree);
175
static int valid_cast_to_p (tree, tree);
176
static int valid_method_invocation_conversion_p (tree, tree);
177
static tree try_builtin_assignconv (tree, tree, tree);
178
static tree try_reference_assignconv (tree, tree);
179
static tree build_unresolved_array_type (tree);
180
static int build_type_name_from_array_name (tree, tree *);
181
static tree build_array_from_name (tree, tree, tree, tree *);
182
static tree build_array_ref (int, tree, tree);
183
static tree patch_array_ref (tree);
184
#ifdef USE_MAPPED_LOCATION
185
static tree make_qualified_name (tree, tree, source_location);
186
#else
187
static tree make_qualified_name (tree, tree, int);
188
#endif
189
static tree merge_qualified_name (tree, tree);
190
static tree make_qualified_primary (tree, tree, int);
191
static int resolve_qualified_expression_name (tree, tree *, tree *, tree *);
192
static void qualify_ambiguous_name (tree);
193
static tree resolve_field_access (tree, tree *, tree *);
194
static tree build_newarray_node (tree, tree, int);
195
static tree patch_newarray (tree);
196
static tree resolve_type_during_patch (tree);
197
static tree build_this (int);
198
static tree build_wfl_wrap (tree, int);
199
static tree build_return (int, tree);
200
static tree patch_return (tree);
201
static tree maybe_access_field (tree, tree, tree);
202
static int complete_function_arguments (tree);
203
static int check_for_static_method_reference (tree, tree, tree, tree, tree);
204
static int not_accessible_p (tree, tree, tree, int);
205
static void check_deprecation (tree, tree);
206
static int class_in_current_package (tree);
207
static tree build_if_else_statement (int, tree, tree, tree);
208
static tree patch_if_else_statement (tree);
209
static tree add_stmt_to_block (tree, tree, tree);
210
static tree patch_exit_expr (tree);
211
static tree build_labeled_block (int, tree);
212
static tree finish_labeled_statement (tree, tree);
213
static tree build_bc_statement (int, int, tree);
214
static tree patch_bc_statement (tree);
215
static tree patch_loop_statement (tree);
216
static tree build_new_loop (tree);
217
static tree build_loop_body (int, tree, int);
218
static tree finish_loop_body (int, tree, tree, int);
219
static tree build_debugable_stmt (int, tree);
220
static tree finish_for_loop (int, tree, tree, tree);
221
static tree patch_switch_statement (tree);
222
static tree string_constant_concatenation (tree, tree);
223
static tree build_string_concatenation (tree, tree);
224
static tree patch_string_cst (tree);
225
static tree patch_string (tree);
226
static tree encapsulate_with_try_catch (int, tree, tree, tree);
227
#ifdef USE_MAPPED_LOCATION
228
static tree build_assertion (source_location, tree, tree);
229
#else
230
static tree build_assertion (int, tree, tree);
231
#endif
232
static tree build_try_statement (int, tree, tree);
233
static tree build_try_finally_statement (int, tree, tree);
234
static tree patch_try_statement (tree);
235
static tree patch_synchronized_statement (tree, tree);
236
static tree patch_throw_statement (tree, tree);
237
static void add_exception_to_throws (tree, tree);
238
#ifdef USE_MAPPED_LOCATION
239
static void check_thrown_exceptions (source_location, tree, tree);
240
#else
241
static void check_thrown_exceptions (int, tree, tree);
242
#endif
243
static int check_thrown_exceptions_do (tree);
244
static bool ctors_unchecked_throws_clause_p (tree);
245
static void check_concrete_throws_clauses (tree, tree, tree, tree);
246
static void check_throws_clauses (tree, tree, tree);
247
static void finish_method_declaration (tree);
248
static tree build_super_invocation (tree);
249
static int verify_constructor_circularity (tree, tree);
250
static char *constructor_circularity_msg (tree, tree);
251
static tree build_this_super_qualified_invocation (int, tree, tree, int, int);
252
static const char *get_printable_method_name (tree);
253
static tree patch_conditional_expr (tree, tree, tree);
254
static tree generate_finit (tree);
255
static tree generate_instinit (tree);
256
static tree build_instinit_invocation (tree);
257
static void fix_constructors (tree);
258
static tree build_alias_initializer_parameter_list (int, tree, tree, int *);
259
static tree craft_constructor (tree, tree);
260
static tree get_constructor_super (tree);
261
static tree create_artificial_method (tree, int, tree, tree, tree);
262
static void start_artificial_method_body (tree);
263
static void end_artificial_method_body (tree);
264
static int check_method_redefinition (tree, tree);
265
static int check_method_types_complete (tree);
266
static bool hack_is_accessible_p (tree, tree);
267
static void java_check_regular_methods (tree);
268
static void check_interface_throws_clauses (tree, tree);
269
static void java_check_abstract_methods (tree);
270
static void unreachable_stmt_error (tree);
271
static int not_accessible_field_error (tree, tree);
272
static tree find_expr_with_wfl (tree);
273
static void missing_return_error (tree);
274
static tree build_new_array_init (int, tree);
275
static tree patch_new_array_init (tree, tree);
276
static tree maybe_build_array_element_wfl (tree);
277
static int array_constructor_check_entry (tree, constructor_elt *);
278
static const char *purify_type_name (const char *);
279
static tree fold_constant_for_init (tree, tree);
280
static jdeplist *reverse_jdep_list (struct parser_ctxt *);
281
static void static_ref_err (tree, tree, tree);
282
static void parser_add_interface (tree, tree, tree);
283
static void add_superinterfaces (tree, tree);
284
static tree jdep_resolve_class (jdep *);
285
static int note_possible_classname (const char *, int);
286
static void java_complete_expand_classes (void);
287
static void java_complete_expand_class (tree);
288
static void java_complete_expand_methods (tree);
289
static tree cut_identifier_in_qualified (tree);
290
static tree java_stabilize_reference (tree);
291
static tree do_unary_numeric_promotion (tree);
292
static char * operator_string (tree);
293
static tree do_merge_string_cste (tree, const char *, int, int);
294
static tree merge_string_cste (tree, tree, int);
295
static tree java_refold (tree);
296
static int java_decl_equiv (tree, tree);
297
static int binop_compound_p (enum tree_code);
298
static tree search_loop (tree);
299
static int labeled_block_contains_loop_p (tree, tree);
300
static int check_abstract_method_definitions (int, tree, tree);
301
static void java_check_abstract_method_definitions (tree);
302
static void java_debug_context_do (int);
303
static void java_parser_context_push_initialized_field (void);
304
static void java_parser_context_pop_initialized_field (void);
305
static tree reorder_static_initialized (tree);
306
static void java_parser_context_suspend (void);
307
static void java_parser_context_resume (void);
308
static int pop_current_osb (struct parser_ctxt *);
309
 
310
/* JDK 1.1 work. FIXME */
311
 
312
static tree maybe_make_nested_class_name (tree);
313
static int make_nested_class_name (tree);
314
static void link_nested_class_to_enclosing (void);
315
static tree resolve_inner_class (tree, tree, tree, tree);
316
static tree find_as_inner_class (tree, tree, tree);
317
static tree find_as_inner_class_do (tree, tree);
318
static int check_inner_class_redefinition (tree, tree);
319
 
320
static tree build_thisn_assign (void);
321
static tree build_current_thisn (tree);
322
static tree build_access_to_thisn (tree, tree, int);
323
static tree maybe_build_thisn_access_method (tree);
324
 
325
static tree build_nested_field_access (tree, tree);
326
static tree build_nested_field_access_methods (tree);
327
static tree build_nested_field_access_method (tree, tree, tree, tree, tree);
328
static tree build_nested_field_access_expr (int, tree, tree, tree, tree);
329
static tree build_nested_method_access_method (tree);
330
static tree build_new_access_id (void);
331
 
332
static int nested_member_access_p (tree, tree);
333
static int nested_field_expanded_access_p (tree, tree *, tree *, tree *);
334
static tree nested_field_access_fix (tree, tree, tree);
335
 
336
static tree build_incomplete_class_ref (int, tree);
337
static tree patch_incomplete_class_ref (tree);
338
static tree create_anonymous_class (tree);
339
static void patch_anonymous_class (tree, tree, tree);
340
static void add_inner_class_fields (tree, tree);
341
 
342
static tree build_dot_class_method (tree);
343
static tree build_dot_class_method_invocation (tree, tree);
344
static void create_new_parser_context (int);
345
static tree maybe_build_class_init_for_field (tree, tree);
346
 
347
static int emit_test_initialization (void **, void *);
348
 
349
static char *string_convert_int_cst (tree);
350
 
351
/* Number of error found so far. */
352
int java_error_count;
353
/* Number of warning found so far. */
354
int java_warning_count;
355
/* Cyclic inheritance report, as it can be set by layout_class */
356
const char *cyclic_inheritance_report;
357
 
358
/* The current parser context */
359
struct parser_ctxt *ctxp;
360
 
361
/* List of things that were analyzed for which code will be generated */
362
struct parser_ctxt *ctxp_for_generation = NULL;
363
struct parser_ctxt *ctxp_for_generation_last = NULL;
364
 
365
/* binop_lookup maps token to tree_code. It is used where binary
366
   operations are involved and required by the parser. RDIV_EXPR
367
   covers both integral/floating point division. The code is changed
368
   once the type of both operator is worked out.  */
369
 
370
static const enum tree_code binop_lookup[19] =
371
  {
372
    PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373
    LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374
    BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375
    TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376
    EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
377
   };
378
#define BINOP_LOOKUP(VALUE)                                             \
379
  binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
380
 
381
/* This is the end index for binary operators that can also be used
382
   in compound assignments. */
383
#define BINOP_COMPOUND_CANDIDATES 11
384
 
385
/* The "$L" identifier we use to create labels.  */
386
static GTY(()) tree label_id;
387
 
388
/* The "StringBuffer" identifier used for the String `+' operator. */
389
static GTY(()) tree wfl_string_buffer;
390
 
391
/* The "append" identifier used for String `+' operator.  */
392
static GTY(()) tree wfl_append;
393
 
394
/* The "toString" identifier used for String `+' operator. */
395
static GTY(()) tree wfl_to_string;
396
 
397
/* The "java.lang" import qualified name.  */
398
static GTY(()) tree java_lang_id;
399
 
400
/* The generated `inst$' identifier used for generated enclosing
401
   instance/field access functions.  */
402
static GTY(()) tree inst_id;
403
 
404
/* Context and flag for static blocks */
405
static GTY(()) tree current_static_block;
406
 
407
/* The generated `write_parm_value$' identifier.  */
408
static GTY(()) tree wpv_id;
409
 
410
/* Hold THIS for the scope of the current method decl.  */
411
static GTY(()) tree current_this;
412
 
413
/* Hold a list of catch clauses list. The first element of this list is
414
   the list of the catch clauses of the currently analyzed try block. */
415
static GTY(()) tree currently_caught_type_list;
416
 
417
/* This holds a linked list of all the case labels for the current
418
   switch statement.  It is only used when checking to see if there
419
   are duplicate labels.  FIXME: probably this should just be attached
420
   to the switch itself; then it could be referenced via
421
   `ctxp->current_loop'.  */
422
static GTY(()) tree case_label_list;
423
 
424
/* Anonymous class counter. Will be reset to 1 every time a non
425
   anonymous class gets created. */
426
static int anonymous_class_counter = 1;
427
 
428
static GTY(()) tree src_parse_roots[1];
429
 
430
/* All classes seen from source code */
431
#define gclass_list src_parse_roots[0]
432
 
433
/* Check modifiers. If one doesn't fit, retrieve it in its declaration
434
   line and point it out.  */
435
/* Should point out the one that don't fit. ASCII/unicode, going
436
   backward. FIXME */
437
 
438
#define check_modifiers(__message, __value, __mask) do {        \
439
  if ((__value) & ~(__mask))                                    \
440
    {                                                           \
441
      size_t i, remainder = (__value) & ~(__mask);         \
442
      for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
443
        if ((1 << i) & remainder)                               \
444
          parse_error_context (ctxp->modifier_ctx [i], (__message), \
445
                               java_accstring_lookup (1 << i)); \
446
    }                                                           \
447
} while (0)
448
 
449
%}
450
 
451
%union {
452
  tree node;
453
  int sub_token;
454
  struct {
455
    int token;
456
#ifdef USE_MAPPED_LOCATION
457
    source_location location;
458
#else
459
    int location;
460
#endif
461
  } operator;
462
  int value;
463
}
464
 
465
%{
466
#ifdef USE_MAPPED_LOCATION
467
#define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
468
  SET_EXPR_LOCATION(EXPR, (TOKEN).location)
469
#else
470
#define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471
  (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
472
#endif
473
 
474
#include "lex.c"
475
%}
476
 
477
%pure_parser
478
 
479
/* Things defined here have to match the order of what's in the
480
   binop_lookup table.  */
481
 
482
%token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
483
%token   LS_TK           SRS_TK          ZRS_TK
484
%token   AND_TK          XOR_TK          OR_TK
485
%token   BOOL_AND_TK BOOL_OR_TK
486
%token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
487
 
488
/* This maps to the same binop_lookup entry than the token above */
489
 
490
%token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
491
%token   REM_ASSIGN_TK
492
%token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
493
%token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
494
 
495
 
496
/* Modifier TOKEN have to be kept in this order. Don't scramble it */
497
 
498
%token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
499
%token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
500
%token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
501
%token   PAD_TK          ABSTRACT_TK        STRICT_TK
502
%token   MODIFIER_TK
503
 
504
/* Keep those two in order, too */
505
%token   DECR_TK INCR_TK
506
 
507
/* From now one, things can be in any order */
508
 
509
%token   DEFAULT_TK      IF_TK              THROW_TK
510
%token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
511
%token   THROWS_TK       BREAK_TK           IMPORT_TK
512
%token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
513
%token   VOID_TK         CATCH_TK           INTERFACE_TK
514
%token   CASE_TK         EXTENDS_TK         FINALLY_TK
515
%token   SUPER_TK        WHILE_TK           CLASS_TK
516
%token   SWITCH_TK       CONST_TK           TRY_TK
517
%token   FOR_TK          NEW_TK             CONTINUE_TK
518
%token   GOTO_TK         PACKAGE_TK         THIS_TK
519
%token   ASSERT_TK
520
 
521
%token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
522
%token   CHAR_TK         INTEGRAL_TK
523
 
524
%token   FLOAT_TK        DOUBLE_TK          FP_TK
525
 
526
%token   ID_TK
527
 
528
%token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
529
 
530
%token   ASSIGN_ANY_TK   ASSIGN_TK
531
%token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
532
 
533
%token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
534
%token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
535
 
536
%type           modifiers MODIFIER_TK final synchronized
537
 
538
%type                   super ID_TK identifier
539
%type                   name simple_name qualified_name
540
%type                   type_declaration compilation_unit
541
                        field_declaration method_declaration extends_interfaces
542
                        interfaces interface_type_list
543
                        import_declarations package_declaration
544
                        type_declarations interface_body
545
                        interface_member_declaration constant_declaration
546
                        interface_member_declarations interface_type
547
                        abstract_method_declaration
548
%type                   class_body_declaration class_member_declaration
549
                        static_initializer constructor_declaration block
550
%type                   class_body_declarations constructor_header
551
%type                   class_or_interface_type class_type class_type_list
552
                        constructor_declarator explicit_constructor_invocation
553
%type             dim_expr dim_exprs this_or_super throws
554
 
555
%type                   variable_declarator_id variable_declarator
556
                        variable_declarators variable_initializer
557
                        variable_initializers constructor_body
558
                        array_initializer
559
 
560
%type                   class_body block_end constructor_block_end
561
%type                   statement statement_without_trailing_substatement
562
                        labeled_statement if_then_statement label_decl
563
                        if_then_else_statement while_statement for_statement
564
                        statement_nsi labeled_statement_nsi do_statement
565
                        if_then_else_statement_nsi while_statement_nsi
566
                        for_statement_nsi statement_expression_list for_init
567
                        for_update statement_expression expression_statement
568
                        primary_no_new_array expression primary array_type
569
                        array_creation_initialized array_creation_uninitialized
570
                        class_instance_creation_expression field_access
571
                        method_invocation array_access something_dot_new
572
                        argument_list postfix_expression while_expression
573
                        post_increment_expression post_decrement_expression
574
                        unary_expression_not_plus_minus unary_expression
575
                        pre_increment_expression pre_decrement_expression
576
                        cast_expression
577
                        multiplicative_expression additive_expression
578
                        shift_expression relational_expression
579
                        equality_expression and_expression
580
                        exclusive_or_expression inclusive_or_expression
581
                        conditional_and_expression conditional_or_expression
582
                        conditional_expression assignment_expression
583
                        left_hand_side assignment for_header for_begin
584
                        constant_expression do_statement_begin empty_statement
585
                        switch_statement synchronized_statement throw_statement
586
                        try_statement assert_statement
587
                        switch_expression switch_block
588
                        catches catch_clause catch_clause_parameter finally
589
                        anonymous_class_creation trap_overflow_corner_case
590
%type             return_statement break_statement continue_statement
591
 
592
%type         ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
593
%type         REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
594
%type         LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
595
%type         AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
596
%type         ASSIGN_ANY_TK  assignment_operator
597
%token        EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
598
%token        BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
599
%token        DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
600
%token        NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
601
%token        OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
602
%type           THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
603
%type         CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
604
%type         NEW_TK ASSERT_TK
605
 
606
%type                   method_body
607
 
608
%type                   literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
609
                        STRING_LIT_TK NULL_TK VOID_TK
610
 
611
%type                   IF_TK WHILE_TK FOR_TK
612
 
613
%type             formal_parameter_list formal_parameter
614
                        method_declarator method_header
615
 
616
%type                   primitive_type reference_type type
617
                        BOOLEAN_TK INTEGRAL_TK FP_TK
618
 
619
/* Added or modified JDK 1.1 rule types  */
620
%type                   type_literals
621
 
622
%%
623
/* 19.2 Production from 2.3: The Syntactic Grammar  */
624
goal:  compilation_unit
625
                {}
626
;
627
 
628
/* 19.3 Productions from 3: Lexical structure  */
629
literal:
630
        INT_LIT_TK
631
|       FP_LIT_TK
632
|       BOOL_LIT_TK
633
|       CHAR_LIT_TK
634
|       STRING_LIT_TK
635
|       NULL_TK
636
;
637
 
638
/* 19.4 Productions from 4: Types, Values and Variables  */
639
type:
640
        primitive_type
641
|       reference_type
642
;
643
 
644
primitive_type:
645
        INTEGRAL_TK
646
|       FP_TK
647
|       BOOLEAN_TK
648
;
649
 
650
reference_type:
651
        class_or_interface_type
652
|       array_type
653
;
654
 
655
class_or_interface_type:
656
        name
657
;
658
 
659
class_type:
660
        class_or_interface_type /* Default rule */
661
;
662
 
663
interface_type:
664
         class_or_interface_type
665
;
666
 
667
array_type:
668
        primitive_type dims
669
                {
670
                  int osb = pop_current_osb (ctxp);
671
                  tree t = build_java_array_type (($1), -1);
672
                  while (--osb)
673
                    t = build_unresolved_array_type (t);
674
                  $$ = t;
675
                }
676
|       name dims
677
                {
678
                  int osb = pop_current_osb (ctxp);
679
                  tree t = $1;
680
                  while (osb--)
681
                    t = build_unresolved_array_type (t);
682
                  $$ = t;
683
                }
684
;
685
 
686
/* 19.5 Productions from 6: Names  */
687
name:
688
        simple_name             /* Default rule */
689
|       qualified_name          /* Default rule */
690
;
691
 
692
simple_name:
693
        identifier              /* Default rule */
694
;
695
 
696
qualified_name:
697
        name DOT_TK identifier
698
                { $$ = make_qualified_name ($1, $3, $2.location); }
699
;
700
 
701
identifier:
702
        ID_TK
703
;
704
 
705
/* 19.6: Production from 7: Packages  */
706
compilation_unit:
707
                {$$ = NULL;}
708
|       package_declaration
709
|       import_declarations
710
|       type_declarations
711
|       package_declaration import_declarations
712
|       package_declaration type_declarations
713
|       import_declarations type_declarations
714
|       package_declaration import_declarations type_declarations
715
;
716
 
717
import_declarations:
718
        import_declaration
719
                {
720
                  $$ = NULL;
721
                }
722
|       import_declarations import_declaration
723
                {
724
                  $$ = NULL;
725
                }
726
;
727
 
728
type_declarations:
729
        type_declaration
730
|       type_declarations type_declaration
731
;
732
 
733
package_declaration:
734
        PACKAGE_TK name SC_TK
735
                {
736
                  ctxp->package = EXPR_WFL_NODE ($2);
737
                }
738
|       PACKAGE_TK error
739
                {yyerror ("Missing name"); RECOVER;}
740
|       PACKAGE_TK name error
741
                {yyerror ("';' expected"); RECOVER;}
742
;
743
 
744
import_declaration:
745
        single_type_import_declaration
746
|       type_import_on_demand_declaration
747
;
748
 
749
single_type_import_declaration:
750
        IMPORT_TK name SC_TK
751
                {
752
                  tree name = EXPR_WFL_NODE ($2), last_name;
753
                  int   i = IDENTIFIER_LENGTH (name)-1;
754
                  const char *last = &IDENTIFIER_POINTER (name)[i];
755
                  while (last != IDENTIFIER_POINTER (name))
756
                    {
757
                      if (last [0] == '.')
758
                        break;
759
                      last--;
760
                    }
761
                  last_name = get_identifier (++last);
762
                  if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
763
                    {
764
                      tree err = find_name_in_single_imports (last_name);
765
                      if (err && err != name)
766
                        parse_error_context
767
                          ($2, "Ambiguous class: %qs and %qs",
768
                           IDENTIFIER_POINTER (name),
769
                           IDENTIFIER_POINTER (err));
770
                      else
771
                        REGISTER_IMPORT ($2, last_name);
772
                    }
773
                  else
774
                    REGISTER_IMPORT ($2, last_name);
775
                }
776
|       IMPORT_TK error
777
                {yyerror ("Missing name"); RECOVER;}
778
|       IMPORT_TK name error
779
                {yyerror ("';' expected"); RECOVER;}
780
;
781
 
782
type_import_on_demand_declaration:
783
        IMPORT_TK name DOT_TK MULT_TK SC_TK
784
                {
785
                  tree name = EXPR_WFL_NODE ($2);
786
                  tree it;
787
                  /* Search for duplicates. */
788
                  for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
789
                    if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
790
                      break;
791
                  /* Don't import the same thing more than once, just ignore
792
                     duplicates (7.5.2) */
793
                  if (! it)
794
                    {
795
                      read_import_dir ($2);
796
                      ctxp->import_demand_list =
797
                        chainon (ctxp->import_demand_list,
798
                                 build_tree_list ($2, NULL_TREE));
799
                    }
800
                }
801
|       IMPORT_TK name DOT_TK error
802
                {yyerror ("'*' expected"); RECOVER;}
803
|       IMPORT_TK name DOT_TK MULT_TK error
804
                {yyerror ("';' expected"); RECOVER;}
805
;
806
 
807
type_declaration:
808
        class_declaration
809
                { end_class_declaration (0); }
810
|       interface_declaration
811
                { end_class_declaration (0); }
812
|       empty_statement
813
|       error
814
                {
815
                  YYERROR_NOW;
816
                  yyerror ("Class or interface declaration expected");
817
                }
818
;
819
 
820
/* 19.7 Shortened from the original:
821
   modifiers: modifier | modifiers modifier
822
   modifier: any of public...  */
823
modifiers:
824
        MODIFIER_TK
825
                {
826
                  $$ = (1 << $1);
827
                }
828
|       modifiers MODIFIER_TK
829
                {
830
                  int acc = (1 << $2);
831
                  if ($$ & acc)
832
                    parse_error_context
833
                      (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
834
                       java_accstring_lookup (acc));
835
                  else
836
                    {
837
                      $$ |= acc;
838
                    }
839
                }
840
;
841
 
842
/* 19.8.1 Production from $8.1: Class Declaration */
843
class_declaration:
844
        modifiers CLASS_TK identifier super interfaces
845
                { create_class ($1, $3, $4, $5); }
846
        class_body
847
                {;}
848
|       CLASS_TK identifier super interfaces
849
                { create_class (0, $2, $3, $4); }
850
        class_body
851
                {;}
852
|       modifiers CLASS_TK error
853
                { yyerror ("Missing class name"); RECOVER; }
854
|       CLASS_TK error
855
                { yyerror ("Missing class name"); RECOVER; }
856
|       CLASS_TK identifier error
857
                {
858
                  if (!ctxp->class_err) yyerror ("'{' expected");
859
                  DRECOVER(class1);
860
                }
861
|       modifiers CLASS_TK identifier error
862
                { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
863
;
864
 
865
super:
866
                { $$ = NULL; }
867
|       EXTENDS_TK class_type
868
                { $$ = $2; }
869
|       EXTENDS_TK class_type error
870
                {yyerror ("'{' expected"); ctxp->class_err=1;}
871
|       EXTENDS_TK error
872
                {yyerror ("Missing super class name"); ctxp->class_err=1;}
873
;
874
 
875
interfaces:
876
                { $$ = NULL_TREE; }
877
|       IMPLEMENTS_TK interface_type_list
878
                { $$ = $2; }
879
|       IMPLEMENTS_TK error
880
                {
881
                  ctxp->class_err=1;
882
                  yyerror ("Missing interface name");
883
                }
884
;
885
 
886
interface_type_list:
887
        interface_type
888
                {
889
                  ctxp->interface_number = 1;
890
                  $$ = build_tree_list ($1, NULL_TREE);
891
                }
892
|       interface_type_list C_TK interface_type
893
                {
894
                  ctxp->interface_number++;
895
                  $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
896
                }
897
|       interface_type_list C_TK error
898
                {yyerror ("Missing interface name"); RECOVER;}
899
;
900
 
901
class_body:
902
        OCB_TK CCB_TK
903
                {
904
                  $$ = GET_CPC ();
905
                }
906
|       OCB_TK class_body_declarations CCB_TK
907
                {
908
                  $$ = GET_CPC ();
909
                }
910
;
911
 
912
class_body_declarations:
913
        class_body_declaration
914
|       class_body_declarations class_body_declaration
915
;
916
 
917
class_body_declaration:
918
        class_member_declaration
919
|       static_initializer
920
|       constructor_declaration
921
|       block                   /* Added, JDK1.1, instance initializer */
922
                {
923
                  if (!IS_EMPTY_STMT ($1))
924
                    {
925
                      TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
926
                      SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
927
                    }
928
                }
929
;
930
 
931
class_member_declaration:
932
        field_declaration
933
|       method_declaration
934
|       class_declaration       /* Added, JDK1.1 inner classes */
935
                { end_class_declaration (1); }
936
|       interface_declaration   /* Added, JDK1.1 inner interfaces */
937
                { end_class_declaration (1); }
938
|       empty_statement
939
;
940
 
941
/* 19.8.2 Productions from 8.3: Field Declarations  */
942
field_declaration:
943
        type variable_declarators SC_TK
944
                { register_fields (0, $1, $2); }
945
|       modifiers type variable_declarators SC_TK
946
                {
947
                  check_modifiers
948
                    ("Illegal modifier %qs for field declaration",
949
                     $1, FIELD_MODIFIERS);
950
                  check_modifiers_consistency ($1);
951
                  register_fields ($1, $2, $3);
952
                }
953
;
954
 
955
variable_declarators:
956
        /* Should we use build_decl_list () instead ? FIXME */
957
        variable_declarator     /* Default rule */
958
|       variable_declarators C_TK variable_declarator
959
                { $$ = chainon ($1, $3); }
960
|       variable_declarators C_TK error
961
                {yyerror ("Missing term"); RECOVER;}
962
;
963
 
964
variable_declarator:
965
        variable_declarator_id
966
                { $$ = build_tree_list ($1, NULL_TREE); }
967
|       variable_declarator_id ASSIGN_TK variable_initializer
968
                {
969
                  if (java_error_count)
970
                    $3 = NULL_TREE;
971
                  $$ = build_tree_list
972
                    ($1, build_assignment ($2.token, $2.location, $1, $3));
973
                }
974
|       variable_declarator_id ASSIGN_TK error
975
                {
976
                  yyerror ("Missing variable initializer");
977
                  $$ = build_tree_list ($1, NULL_TREE);
978
                  RECOVER;
979
                }
980
|       variable_declarator_id ASSIGN_TK variable_initializer error
981
                {
982
                  yyerror ("';' expected");
983
                  $$ = build_tree_list ($1, NULL_TREE);
984
                  RECOVER;
985
                }
986
;
987
 
988
variable_declarator_id:
989
        identifier
990
|       variable_declarator_id OSB_TK CSB_TK
991
                { $$ = build_unresolved_array_type ($1); }
992
|       identifier error
993
                {yyerror ("Invalid declaration"); DRECOVER(vdi);}
994
|       variable_declarator_id OSB_TK error
995
                {
996
                  yyerror ("']' expected");
997
                  DRECOVER(vdi);
998
                }
999
|       variable_declarator_id CSB_TK error
1000
                {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001
;
1002
 
1003
variable_initializer:
1004
        expression
1005
|       array_initializer
1006
;
1007
 
1008
/* 19.8.3 Productions from 8.4: Method Declarations  */
1009
method_declaration:
1010
        method_header
1011
                {
1012
                  current_function_decl = $1;
1013
                  if (current_function_decl
1014
                      && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015
                    source_start_java_method (current_function_decl);
1016
                  else
1017
                    current_function_decl = NULL_TREE;
1018
                }
1019
        method_body
1020
                { finish_method_declaration ($3); }
1021
|       method_header error
1022
                {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1023
;
1024
 
1025
method_header:
1026
        type method_declarator throws
1027
                { $$ = method_header (0, $1, $2, $3); }
1028
|       VOID_TK method_declarator throws
1029
                { $$ = method_header (0, void_type_node, $2, $3); }
1030
|       modifiers type method_declarator throws
1031
                { $$ = method_header ($1, $2, $3, $4); }
1032
|       modifiers VOID_TK method_declarator throws
1033
                { $$ = method_header ($1, void_type_node, $3, $4); }
1034
|       type error
1035
                {
1036
                  yyerror ("Invalid method declaration, method name required");
1037
                  $$ = NULL_TREE;
1038
                  RECOVER;
1039
                }
1040
|       modifiers type error
1041
                {
1042
                  yyerror ("Identifier expected");
1043
                  $$ = NULL_TREE;
1044
                  RECOVER;
1045
                }
1046
|       VOID_TK error
1047
                {
1048
                  yyerror ("Identifier expected");
1049
                  $$ = NULL_TREE;
1050
                  RECOVER;
1051
                }
1052
|       modifiers VOID_TK error
1053
                {
1054
                  yyerror ("Identifier expected");
1055
                  $$ = NULL_TREE;
1056
                  RECOVER;
1057
                }
1058
|       modifiers error
1059
                {
1060
                  yyerror ("Invalid method declaration, return type required");
1061
                  $$ = NULL_TREE;
1062
                  RECOVER;
1063
                }
1064
;
1065
 
1066
method_declarator:
1067
        identifier OP_TK CP_TK
1068
                {
1069
                  ctxp->formal_parameter_number = 0;
1070
                  $$ = method_declarator ($1, NULL_TREE);
1071
                }
1072
|       identifier OP_TK formal_parameter_list CP_TK
1073
                { $$ = method_declarator ($1, $3); }
1074
|       method_declarator OSB_TK CSB_TK
1075
                {
1076
                  SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1077
                  TREE_PURPOSE ($1) =
1078
                    build_unresolved_array_type (TREE_PURPOSE ($1));
1079
                  parse_warning_context
1080
                    (wfl_operator,
1081
                     "Discouraged form of returned type specification");
1082
                }
1083
|       identifier OP_TK error
1084
                {yyerror ("')' expected"); DRECOVER(method_declarator);}
1085
|       method_declarator OSB_TK error
1086
                {yyerror ("']' expected"); RECOVER;}
1087
;
1088
 
1089
formal_parameter_list:
1090
        formal_parameter
1091
                {
1092
                  ctxp->formal_parameter_number = 1;
1093
                }
1094
|       formal_parameter_list C_TK formal_parameter
1095
                {
1096
                  ctxp->formal_parameter_number += 1;
1097
                  $$ = chainon ($1, $3);
1098
                }
1099
|       formal_parameter_list C_TK error
1100
                { yyerror ("Missing formal parameter term"); RECOVER; }
1101
;
1102
 
1103
formal_parameter:
1104
        type variable_declarator_id
1105
                {
1106
                  $$ = build_tree_list ($2, $1);
1107
                }
1108
|       final type variable_declarator_id /* Added, JDK1.1 final parms */
1109
                {
1110
                  $$ = build_tree_list ($3, $2);
1111
                  ARG_FINAL_P ($$) = 1;
1112
                }
1113
|       type error
1114
                {
1115
                  yyerror ("Missing identifier"); RECOVER;
1116
                  $$ = NULL_TREE;
1117
                }
1118
|       final type error
1119
                {
1120
                  yyerror ("Missing identifier"); RECOVER;
1121
                  $$ = NULL_TREE;
1122
                }
1123
;
1124
 
1125
final:
1126
        modifiers
1127
                {
1128
                  check_modifiers ("Illegal modifier %qs. Only % was expected here",
1129
                                   $1, ACC_FINAL);
1130
                  if ($1 != ACC_FINAL)
1131
                    MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1132
                }
1133
;
1134
 
1135
throws:
1136
                { $$ = NULL_TREE; }
1137
|       THROWS_TK class_type_list
1138
                { $$ = $2; }
1139
|       THROWS_TK error
1140
                {yyerror ("Missing class type term"); RECOVER;}
1141
;
1142
 
1143
class_type_list:
1144
        class_type
1145
                { $$ = build_tree_list ($1, $1); }
1146
|       class_type_list C_TK class_type
1147
                { $$ = tree_cons ($3, $3, $1); }
1148
|       class_type_list C_TK error
1149
                {yyerror ("Missing class type term"); RECOVER;}
1150
;
1151
 
1152
method_body:
1153
        block
1154
|       SC_TK { $$ = NULL_TREE; }
1155
;
1156
 
1157
/* 19.8.4 Productions from 8.5: Static Initializers  */
1158
static_initializer:
1159
        static block
1160
                {
1161
                  TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1162
                  SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1163
                  current_static_block = NULL_TREE;
1164
                }
1165
;
1166
 
1167
static:                         /* Test lval.sub_token here */
1168
        modifiers
1169
                {
1170
                  check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1171
                  /* Can't have a static initializer in an innerclass */
1172
                  if ($1 | ACC_STATIC &&
1173
                      GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1174
                    parse_error_context
1175
                      (MODIFIER_WFL (STATIC_TK),
1176
                       "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1177
                       IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1178
                  SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1179
                }
1180
;
1181
 
1182
/* 19.8.5 Productions from 8.6: Constructor Declarations  */
1183
constructor_declaration:
1184
        constructor_header
1185
                {
1186
                  current_function_decl = $1;
1187
                  source_start_java_method (current_function_decl);
1188
                }
1189
        constructor_body
1190
                { finish_method_declaration ($3); }
1191
;
1192
 
1193
constructor_header:
1194
        constructor_declarator throws
1195
                { $$ = method_header (0, NULL_TREE, $1, $2); }
1196
|       modifiers constructor_declarator throws
1197
                { $$ = method_header ($1, NULL_TREE, $2, $3); }
1198
;
1199
 
1200
constructor_declarator:
1201
        simple_name OP_TK CP_TK
1202
                {
1203
                  ctxp->formal_parameter_number = 0;
1204
                  $$ = method_declarator ($1, NULL_TREE);
1205
                }
1206
|       simple_name OP_TK formal_parameter_list CP_TK
1207
                { $$ = method_declarator ($1, $3); }
1208
;
1209
 
1210
constructor_body:
1211
        /* Unlike regular method, we always need a complete (empty)
1212
           body so we can safely perform all the required code
1213
           addition (super invocation and field initialization) */
1214
        block_begin constructor_block_end
1215
                {
1216
                  BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1217
                  $$ = $2;
1218
                }
1219
|       block_begin explicit_constructor_invocation constructor_block_end
1220
                { $$ = $3; }
1221
|       block_begin block_statements constructor_block_end
1222
                { $$ = $3; }
1223
|       block_begin explicit_constructor_invocation block_statements constructor_block_end
1224
                { $$ = $4; }
1225
;
1226
 
1227
constructor_block_end:
1228
        block_end
1229
;
1230
 
1231
/* Error recovery for that rule moved down expression_statement: rule.  */
1232
explicit_constructor_invocation:
1233
        this_or_super OP_TK CP_TK SC_TK
1234
                {
1235
                  $$ = build_method_invocation ($1, NULL_TREE);
1236
                  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1237
                  $$ = java_method_add_stmt (current_function_decl, $$);
1238
                }
1239
|       this_or_super OP_TK argument_list CP_TK SC_TK
1240
                {
1241
                  $$ = build_method_invocation ($1, $3);
1242
                  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243
                  $$ = java_method_add_stmt (current_function_decl, $$);
1244
                }
1245
        /* Added, JDK1.1 inner classes. Modified because the rule
1246
           'primary' couldn't work.  */
1247
|       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1248
                {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1249
|       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1250
                {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1251
;
1252
 
1253
this_or_super:                  /* Added, simplifies error diagnostics */
1254
        THIS_TK
1255
                {
1256
                  tree wfl = build_wfl_node (this_identifier_node);
1257
                  SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1258
                  $$ = wfl;
1259
                }
1260
|       SUPER_TK
1261
                {
1262
                  tree wfl = build_wfl_node (super_identifier_node);
1263
                  SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1264
                  $$ = wfl;
1265
                }
1266
;
1267
 
1268
/* 19.9 Productions from 9: Interfaces  */
1269
/* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1270
interface_declaration:
1271
        INTERFACE_TK identifier
1272
                { create_interface (0, $2, NULL_TREE); }
1273
        interface_body
1274
                { ; }
1275
|       modifiers INTERFACE_TK identifier
1276
                { create_interface ($1, $3, NULL_TREE); }
1277
        interface_body
1278
                { ; }
1279
|       INTERFACE_TK identifier extends_interfaces
1280
                { create_interface (0, $2, $3); }
1281
        interface_body
1282
                { ; }
1283
|       modifiers INTERFACE_TK identifier extends_interfaces
1284
                { create_interface ($1, $3, $4); }
1285
        interface_body
1286
                { ; }
1287
|       INTERFACE_TK identifier error
1288
                { yyerror ("'{' expected"); RECOVER; }
1289
|       modifiers INTERFACE_TK identifier error
1290
                { yyerror ("'{' expected"); RECOVER; }
1291
;
1292
 
1293
extends_interfaces:
1294
        EXTENDS_TK interface_type
1295
                {
1296
                  ctxp->interface_number = 1;
1297
                  $$ = build_tree_list ($2, NULL_TREE);
1298
                }
1299
|       extends_interfaces C_TK interface_type
1300
                {
1301
                  ctxp->interface_number++;
1302
                  $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1303
                }
1304
|       EXTENDS_TK error
1305
                {yyerror ("Invalid interface type"); RECOVER;}
1306
|       extends_interfaces C_TK error
1307
                {yyerror ("Missing term"); RECOVER;}
1308
;
1309
 
1310
interface_body:
1311
        OCB_TK CCB_TK
1312
                { $$ = NULL_TREE; }
1313
|       OCB_TK interface_member_declarations CCB_TK
1314
                { $$ = NULL_TREE; }
1315
;
1316
 
1317
interface_member_declarations:
1318
        interface_member_declaration
1319
|       interface_member_declarations interface_member_declaration
1320
;
1321
 
1322
interface_member_declaration:
1323
        constant_declaration
1324
|       abstract_method_declaration
1325
|       class_declaration       /* Added, JDK1.1 inner classes */
1326
                { end_class_declaration (1); }
1327
|       interface_declaration   /* Added, JDK1.1 inner interfaces */
1328
                { end_class_declaration (1); }
1329
|       empty_statement
1330
;
1331
 
1332
constant_declaration:
1333
        field_declaration
1334
;
1335
 
1336
abstract_method_declaration:
1337
        method_header SC_TK
1338
                {
1339
                  check_abstract_method_header ($1);
1340
                  current_function_decl = NULL_TREE; /* FIXME ? */
1341
                }
1342
|       method_header error
1343
                {yyerror ("';' expected"); RECOVER;}
1344
;
1345
 
1346
/* 19.10 Productions from 10: Arrays  */
1347
array_initializer:
1348
        OCB_TK CCB_TK
1349
                { $$ = build_new_array_init ($1.location, NULL_TREE); }
1350
|       OCB_TK C_TK CCB_TK
1351
                { $$ = build_new_array_init ($1.location, NULL_TREE); }
1352
|       OCB_TK variable_initializers CCB_TK
1353
                { $$ = build_new_array_init ($1.location, $2); }
1354
|       OCB_TK variable_initializers C_TK CCB_TK
1355
                { $$ = build_new_array_init ($1.location, $2); }
1356
;
1357
 
1358
variable_initializers:
1359
        variable_initializer
1360
                {
1361
                  $$ = tree_cons (maybe_build_array_element_wfl ($1),
1362
                                  $1, NULL_TREE);
1363
                }
1364
|       variable_initializers C_TK variable_initializer
1365
                {
1366
                  $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1367
                }
1368
|       variable_initializers C_TK error
1369
                {yyerror ("Missing term"); RECOVER;}
1370
;
1371
 
1372
/* 19.11 Production from 14: Blocks and Statements  */
1373
block:
1374
        block_begin block_end
1375
                { $$ = $2; }
1376
|       block_begin block_statements block_end
1377
                { $$ = $3; }
1378
;
1379
 
1380
block_begin:
1381
        OCB_TK
1382
                { enter_block (); }
1383
;
1384
 
1385
block_end:
1386
        CCB_TK
1387
                {
1388
                  maybe_absorb_scoping_blocks ();
1389
                  $$ = exit_block ();
1390
                  if (!BLOCK_SUBBLOCKS ($$))
1391
                    BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1392
                }
1393
;
1394
 
1395
block_statements:
1396
        block_statement
1397
|       block_statements block_statement
1398
;
1399
 
1400
block_statement:
1401
        local_variable_declaration_statement
1402
|       statement
1403
                { java_method_add_stmt (current_function_decl, $1); }
1404
|       class_declaration       /* Added, JDK1.1 local classes */
1405
                {
1406
                  LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1407
                  end_class_declaration (1);
1408
                }
1409
;
1410
 
1411
local_variable_declaration_statement:
1412
        local_variable_declaration SC_TK /* Can't catch missing ';' here */
1413
;
1414
 
1415
local_variable_declaration:
1416
        type variable_declarators
1417
                { declare_local_variables (0, $1, $2); }
1418
|       final type variable_declarators /* Added, JDK1.1 final locals */
1419
                { declare_local_variables ($1, $2, $3); }
1420
;
1421
 
1422
statement:
1423
        statement_without_trailing_substatement
1424
|       labeled_statement
1425
|       if_then_statement
1426
|       if_then_else_statement
1427
|       while_statement
1428
|       for_statement
1429
                { $$ = exit_block (); }
1430
;
1431
 
1432
statement_nsi:
1433
        statement_without_trailing_substatement
1434
|       labeled_statement_nsi
1435
|       if_then_else_statement_nsi
1436
|       while_statement_nsi
1437
|       for_statement_nsi
1438
                { $$ = exit_block (); }
1439
;
1440
 
1441
statement_without_trailing_substatement:
1442
        block
1443
|       empty_statement
1444
|       expression_statement
1445
|       switch_statement
1446
|       do_statement
1447
|       break_statement
1448
|       continue_statement
1449
|       return_statement
1450
|       synchronized_statement
1451
|       throw_statement
1452
|       try_statement
1453
|       assert_statement
1454
;
1455
 
1456
empty_statement:
1457
        SC_TK
1458
                {
1459
                  if (flag_extraneous_semicolon
1460
                      && ! current_static_block
1461
                      && (! current_function_decl ||
1462
                          /* Verify we're not in a inner class declaration */
1463
                          (GET_CPC () != TYPE_NAME
1464
                           (DECL_CONTEXT (current_function_decl)))))
1465
 
1466
                    {
1467
#ifdef USE_MAPPED_LOCATION
1468
                      SET_EXPR_LOCATION (wfl_operator, input_location);
1469
#else
1470
                      EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1471
#endif
1472
                      parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1473
                    }
1474
                  $$ = build_java_empty_stmt ();
1475
                }
1476
;
1477
 
1478
label_decl:
1479
        identifier REL_CL_TK
1480
                {
1481
                  $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1482
                                            EXPR_WFL_NODE ($1));
1483
                  pushlevel (2);
1484
                  push_labeled_block ($$);
1485
                  PUSH_LABELED_BLOCK ($$);
1486
                }
1487
;
1488
 
1489
labeled_statement:
1490
        label_decl statement
1491
                { $$ = finish_labeled_statement ($1, $2); }
1492
|       identifier error
1493
                {yyerror ("':' expected"); RECOVER;}
1494
;
1495
 
1496
labeled_statement_nsi:
1497
        label_decl statement_nsi
1498
                { $$ = finish_labeled_statement ($1, $2); }
1499
;
1500
 
1501
/* We concentrate here a bunch of error handling rules that we couldn't write
1502
   earlier, because expression_statement catches a missing ';'.  */
1503
expression_statement:
1504
        statement_expression SC_TK
1505
                {
1506
                  /* We have a statement. Generate a WFL around it so
1507
                     we can debug it */
1508
#ifdef USE_MAPPED_LOCATION
1509
                  $$ = expr_add_location ($1, input_location, 1);
1510
#else
1511
                  $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1512
                  JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1513
#endif
1514
                  /* We know we have a statement, so set the debug
1515
                     info to be eventually generate here. */
1516
                }
1517
|       error SC_TK
1518
                {
1519
                  YYNOT_TWICE yyerror ("Invalid expression statement");
1520
                  DRECOVER (expr_stmt);
1521
                }
1522
|       error OCB_TK
1523
                {
1524
                  YYNOT_TWICE yyerror ("Invalid expression statement");
1525
                  DRECOVER (expr_stmt);
1526
                }
1527
|       error CCB_TK
1528
                {
1529
                  YYNOT_TWICE yyerror ("Invalid expression statement");
1530
                  DRECOVER (expr_stmt);
1531
                }
1532
|       this_or_super OP_TK error
1533
                {yyerror ("')' expected"); RECOVER;}
1534
|       this_or_super OP_TK CP_TK error
1535
                {
1536
                  parse_ctor_invocation_error ();
1537
                  RECOVER;
1538
                }
1539
|       this_or_super OP_TK argument_list error
1540
                {yyerror ("')' expected"); RECOVER;}
1541
|       this_or_super OP_TK argument_list CP_TK error
1542
                {
1543
                  parse_ctor_invocation_error ();
1544
                  RECOVER;
1545
                }
1546
|       name DOT_TK SUPER_TK error
1547
                {yyerror ("'(' expected"); RECOVER;}
1548
|       name DOT_TK SUPER_TK OP_TK error
1549
                {yyerror ("')' expected"); RECOVER;}
1550
|       name DOT_TK SUPER_TK OP_TK argument_list error
1551
                {yyerror ("')' expected"); RECOVER;}
1552
|       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1553
                {yyerror ("';' expected"); RECOVER;}
1554
|       name DOT_TK SUPER_TK OP_TK CP_TK error
1555
                {yyerror ("';' expected"); RECOVER;}
1556
;
1557
 
1558
statement_expression:
1559
        assignment
1560
|       pre_increment_expression
1561
|       pre_decrement_expression
1562
|       post_increment_expression
1563
|       post_decrement_expression
1564
|       method_invocation
1565
|       class_instance_creation_expression
1566
;
1567
 
1568
if_then_statement:
1569
        IF_TK OP_TK expression CP_TK statement
1570
                {
1571
                  $$ = build_if_else_statement ($2.location, $3,
1572
                                                $5, NULL_TREE);
1573
                }
1574
|       IF_TK error
1575
                {yyerror ("'(' expected"); RECOVER;}
1576
|       IF_TK OP_TK error
1577
                {yyerror ("Missing term"); RECOVER;}
1578
|       IF_TK OP_TK expression error
1579
                {yyerror ("')' expected"); RECOVER;}
1580
;
1581
 
1582
if_then_else_statement:
1583
        IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1584
                { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585
;
1586
 
1587
if_then_else_statement_nsi:
1588
        IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1589
                { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590
;
1591
 
1592
switch_statement:
1593
        switch_expression
1594
                {
1595
                  enter_block ();
1596
                }
1597
        switch_block
1598
                {
1599
                  /* Make into "proper list" of COMPOUND_EXPRs.
1600
                     I.e. make the last statement also have its own
1601
                     COMPOUND_EXPR. */
1602
                  maybe_absorb_scoping_blocks ();
1603
                  TREE_OPERAND ($1, 1) = exit_block ();
1604
                  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1605
                }
1606
;
1607
 
1608
switch_expression:
1609
        SWITCH_TK OP_TK expression CP_TK
1610
                {
1611
                  $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1612
                               NULL_TREE, NULL_TREE);
1613
                  SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1614
                }
1615
|       SWITCH_TK error
1616
                {yyerror ("'(' expected"); RECOVER;}
1617
|       SWITCH_TK OP_TK error
1618
                {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1619
|       SWITCH_TK OP_TK expression CP_TK error
1620
                {yyerror ("'{' expected"); RECOVER;}
1621
;
1622
 
1623
/* Default assignment is there to avoid type node on switch_block
1624
   node. */
1625
 
1626
switch_block:
1627
        OCB_TK CCB_TK
1628
                { $$ = NULL_TREE; }
1629
|       OCB_TK switch_labels CCB_TK
1630
                { $$ = NULL_TREE; }
1631
|       OCB_TK switch_block_statement_groups CCB_TK
1632
                { $$ = NULL_TREE; }
1633
|       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1634
                { $$ = NULL_TREE; }
1635
;
1636
 
1637
switch_block_statement_groups:
1638
        switch_block_statement_group
1639
|       switch_block_statement_groups switch_block_statement_group
1640
;
1641
 
1642
switch_block_statement_group:
1643
        switch_labels block_statements
1644
;
1645
 
1646
switch_labels:
1647
        switch_label
1648
|       switch_labels switch_label
1649
;
1650
 
1651
switch_label:
1652
        CASE_TK constant_expression REL_CL_TK
1653
                {
1654
                  tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1655
                  SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1656
                  java_method_add_stmt (current_function_decl, lab);
1657
                }
1658
|       DEFAULT_TK REL_CL_TK
1659
                {
1660
                  tree lab = make_node (DEFAULT_EXPR);
1661
                  SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1662
                  java_method_add_stmt (current_function_decl, lab);
1663
                }
1664
|       CASE_TK error
1665
                {yyerror ("Missing or invalid constant expression"); RECOVER;}
1666
|       CASE_TK constant_expression error
1667
                {yyerror ("':' expected"); RECOVER;}
1668
|       DEFAULT_TK error
1669
                {yyerror ("':' expected"); RECOVER;}
1670
;
1671
 
1672
while_expression:
1673
        WHILE_TK OP_TK expression CP_TK
1674
                {
1675
                  tree body = build_loop_body ($2.location, $3, 0);
1676
                  $$ = build_new_loop (body);
1677
                }
1678
;
1679
 
1680
while_statement:
1681
        while_expression statement
1682
                { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1683
|       WHILE_TK error
1684
                {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1685
|       WHILE_TK OP_TK error
1686
                {yyerror ("Missing term and ')' expected"); RECOVER;}
1687
|       WHILE_TK OP_TK expression error
1688
                {yyerror ("')' expected"); RECOVER;}
1689
;
1690
 
1691
while_statement_nsi:
1692
        while_expression statement_nsi
1693
                { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1694
;
1695
 
1696
do_statement_begin:
1697
        DO_TK
1698
                {
1699
                  tree body = build_loop_body (0, NULL_TREE, 1);
1700
                  $$ = build_new_loop (body);
1701
                }
1702
        /* Need error handing here. FIXME */
1703
;
1704
 
1705
do_statement:
1706
        do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1707
                { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1708
;
1709
 
1710
for_statement:
1711
        for_begin SC_TK expression SC_TK for_update CP_TK statement
1712
                {
1713
                  if (CONSTANT_CLASS_P ($3))
1714
                    $3 = build_wfl_node ($3);
1715
                  $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1716
                }
1717
|       for_begin SC_TK SC_TK for_update CP_TK statement
1718
                {
1719
                  $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1720
                  /* We have not condition, so we get rid of the EXIT_EXPR */
1721
                  LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1722
                    build_java_empty_stmt ();
1723
                }
1724
|       for_begin SC_TK error
1725
                {yyerror ("Invalid control expression"); RECOVER;}
1726
|       for_begin SC_TK expression SC_TK error
1727
                {yyerror ("Invalid update expression"); RECOVER;}
1728
|       for_begin SC_TK SC_TK error
1729
                {yyerror ("Invalid update expression"); RECOVER;}
1730
;
1731
 
1732
for_statement_nsi:
1733
        for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1734
                { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1735
|       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1736
                {
1737
                  $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1738
                  /* We have not condition, so we get rid of the EXIT_EXPR */
1739
                  LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1740
                    build_java_empty_stmt ();
1741
                }
1742
;
1743
 
1744
for_header:
1745
        FOR_TK OP_TK
1746
                {
1747
                  /* This scope defined for local variable that may be
1748
                     defined within the scope of the for loop */
1749
                  enter_block ();
1750
                }
1751
|       FOR_TK error
1752
                {yyerror ("'(' expected"); DRECOVER(for_1);}
1753
|       FOR_TK OP_TK error
1754
                {yyerror ("Invalid init statement"); RECOVER;}
1755
;
1756
 
1757
for_begin:
1758
        for_header for_init
1759
                {
1760
                  /* We now declare the loop body. The loop is
1761
                     declared as a for loop. */
1762
                  tree body = build_loop_body (0, NULL_TREE, 0);
1763
                  $$ =  build_new_loop (body);
1764
                  FOR_LOOP_P ($$) = 1;
1765
                  /* The loop is added to the current block the for
1766
                     statement is defined within */
1767
                  java_method_add_stmt (current_function_decl, $$);
1768
                }
1769
;
1770
for_init:                       /* Can be empty */
1771
                { $$ = build_java_empty_stmt (); }
1772
|       statement_expression_list
1773
                {
1774
                  /* Init statement recorded within the previously
1775
                     defined block scope */
1776
                  $$ = java_method_add_stmt (current_function_decl, $1);
1777
                }
1778
|       local_variable_declaration
1779
                {
1780
                  /* Local variable are recorded within the previously
1781
                     defined block scope */
1782
                  $$ = NULL_TREE;
1783
                }
1784
|       statement_expression_list error
1785
                {yyerror ("';' expected"); DRECOVER(for_init_1);}
1786
;
1787
 
1788
for_update:                     /* Can be empty */
1789
                {$$ = build_java_empty_stmt ();}
1790
|       statement_expression_list
1791
                { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1792
;
1793
 
1794
statement_expression_list:
1795
        statement_expression
1796
                { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1797
|       statement_expression_list C_TK statement_expression
1798
                { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1799
|       statement_expression_list C_TK error
1800
                {yyerror ("Missing term"); RECOVER;}
1801
;
1802
 
1803
break_statement:
1804
        BREAK_TK SC_TK
1805
                { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1806
|       BREAK_TK identifier SC_TK
1807
                { $$ = build_bc_statement ($1.location, 1, $2); }
1808
|       BREAK_TK error
1809
                {yyerror ("Missing term"); RECOVER;}
1810
|       BREAK_TK identifier error
1811
                {yyerror ("';' expected"); RECOVER;}
1812
;
1813
 
1814
continue_statement:
1815
        CONTINUE_TK SC_TK
1816
                { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1817
|       CONTINUE_TK identifier SC_TK
1818
                { $$ = build_bc_statement ($1.location, 0, $2); }
1819
|       CONTINUE_TK error
1820
                {yyerror ("Missing term"); RECOVER;}
1821
|       CONTINUE_TK identifier error
1822
                {yyerror ("';' expected"); RECOVER;}
1823
;
1824
 
1825
return_statement:
1826
        RETURN_TK SC_TK
1827
                { $$ = build_return ($1.location, NULL_TREE); }
1828
|       RETURN_TK expression SC_TK
1829
                { $$ = build_return ($1.location, $2); }
1830
|       RETURN_TK error
1831
                {yyerror ("Missing term"); RECOVER;}
1832
|       RETURN_TK expression error
1833
                {yyerror ("';' expected"); RECOVER;}
1834
;
1835
 
1836
throw_statement:
1837
        THROW_TK expression SC_TK
1838
                {
1839
                  $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1840
                  SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1841
                }
1842
|       THROW_TK error
1843
                {yyerror ("Missing term"); RECOVER;}
1844
|       THROW_TK expression error
1845
                {yyerror ("';' expected"); RECOVER;}
1846
;
1847
 
1848
assert_statement:
1849
        ASSERT_TK expression REL_CL_TK expression SC_TK
1850
                {
1851
                  $$ = build_assertion ($1.location, $2, $4);
1852
                }
1853
|       ASSERT_TK expression SC_TK
1854
                {
1855
                  $$ = build_assertion ($1.location, $2, NULL_TREE);
1856
                }
1857
|       ASSERT_TK error
1858
                {yyerror ("Missing term"); RECOVER;}
1859
|       ASSERT_TK expression error
1860
                {yyerror ("';' expected"); RECOVER;}
1861
;
1862
 
1863
synchronized_statement:
1864
        synchronized OP_TK expression CP_TK block
1865
                {
1866
                  $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1867
                  EXPR_WFL_LINECOL ($$) =
1868
                    EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1869
                }
1870
|       synchronized OP_TK expression CP_TK error
1871
                {yyerror ("'{' expected"); RECOVER;}
1872
|       synchronized error
1873
                {yyerror ("'(' expected"); RECOVER;}
1874
|       synchronized OP_TK error CP_TK
1875
                {yyerror ("Missing term"); RECOVER;}
1876
|       synchronized OP_TK error
1877
                {yyerror ("Missing term"); RECOVER;}
1878
;
1879
 
1880
synchronized:
1881
        modifiers
1882
                {
1883
                  check_modifiers (
1884
             "Illegal modifier %qs. Only % was expected here",
1885
                                   $1, ACC_SYNCHRONIZED);
1886
                  if ($1 != ACC_SYNCHRONIZED)
1887
                    MODIFIER_WFL (SYNCHRONIZED_TK) =
1888
                      build_wfl_node (NULL_TREE);
1889
                }
1890
;
1891
 
1892
try_statement:
1893
        TRY_TK block catches
1894
                { $$ = build_try_statement ($1.location, $2, $3); }
1895
|       TRY_TK block finally
1896
                { $$ = build_try_finally_statement ($1.location, $2, $3); }
1897
|       TRY_TK block catches finally
1898
                { $$ = build_try_finally_statement
1899
                    ($1.location, build_try_statement ($1.location,
1900
                                                       $2, $3), $4);
1901
                }
1902
|       TRY_TK error
1903
                {yyerror ("'{' expected"); DRECOVER (try_statement);}
1904
;
1905
 
1906
catches:
1907
        catch_clause
1908
|       catches catch_clause
1909
                {
1910
                  TREE_CHAIN ($2) = $1;
1911
                  $$ = $2;
1912
                }
1913
;
1914
 
1915
catch_clause:
1916
        catch_clause_parameter block
1917
                {
1918
                  java_method_add_stmt (current_function_decl, $2);
1919
                  exit_block ();
1920
                  $$ = $1;
1921
                }
1922
;
1923
 
1924
catch_clause_parameter:
1925
        CATCH_TK OP_TK formal_parameter CP_TK
1926
                {
1927
                  /* We add a block to define a scope for
1928
                     formal_parameter (CCBP). The formal parameter is
1929
                     declared initialized by the appropriate function
1930
                     call */
1931
                  tree ccpb;
1932
                  tree init;
1933
                  if ($3)
1934
                    {
1935
                      ccpb = enter_block ();
1936
                      init = build_assignment
1937
                        (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1938
                         build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1939
                      declare_local_variables (0, TREE_VALUE ($3),
1940
                                               build_tree_list
1941
                                               (TREE_PURPOSE ($3), init));
1942
                      $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1943
                      SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1944
                    }
1945
                  else
1946
                    {
1947
                      $$ = error_mark_node;
1948
                    }
1949
                }
1950
|       CATCH_TK error
1951
                {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1952
|       CATCH_TK OP_TK error
1953
                {
1954
                  yyerror ("Missing term or ')' expected");
1955
                  RECOVER; $$ = NULL_TREE;
1956
                }
1957
|       CATCH_TK OP_TK error CP_TK /* That's for () */
1958
                {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1959
;
1960
 
1961
finally:
1962
        FINALLY_TK block
1963
                { $$ = $2; }
1964
|       FINALLY_TK error
1965
                {yyerror ("'{' expected"); RECOVER; }
1966
;
1967
 
1968
/* 19.12 Production from 15: Expressions  */
1969
primary:
1970
        primary_no_new_array
1971
|       array_creation_uninitialized
1972
|       array_creation_initialized
1973
;
1974
 
1975
primary_no_new_array:
1976
        literal
1977
|       THIS_TK
1978
                { $$ = build_this ($1.location); }
1979
|       OP_TK expression CP_TK
1980
                {$$ = $2;}
1981
|       class_instance_creation_expression
1982
|       field_access
1983
|       method_invocation
1984
|       array_access
1985
|       type_literals
1986
        /* Added, JDK1.1 inner classes. Documentation is wrong
1987
           referring to a 'ClassName' (class_name) rule that doesn't
1988
           exist. Used name: instead.  */
1989
|       name DOT_TK THIS_TK
1990
                {
1991
                  tree wfl = build_wfl_node (this_identifier_node);
1992
                  $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1993
                }
1994
|       OP_TK expression error
1995
                {yyerror ("')' expected"); RECOVER;}
1996
|       name DOT_TK error
1997
                {yyerror ("'class' or 'this' expected" ); RECOVER;}
1998
|       primitive_type DOT_TK error
1999
                {yyerror ("'class' expected" ); RECOVER;}
2000
|       VOID_TK DOT_TK error
2001
                {yyerror ("'class' expected" ); RECOVER;}
2002
;
2003
 
2004
type_literals:
2005
        name DOT_TK CLASS_TK
2006
                { $$ = build_incomplete_class_ref ($2.location, $1); }
2007
|       array_type DOT_TK CLASS_TK
2008
                { $$ = build_incomplete_class_ref ($2.location, $1); }
2009
|       primitive_type DOT_TK CLASS_TK
2010
                { $$ = build_incomplete_class_ref ($2.location, $1); }
2011
|       VOID_TK DOT_TK CLASS_TK
2012
                {
2013
                   $$ = build_incomplete_class_ref ($2.location,
2014
                                                   void_type_node);
2015
                }
2016
;
2017
 
2018
class_instance_creation_expression:
2019
        NEW_TK class_type OP_TK argument_list CP_TK
2020
                { $$ = build_new_invocation ($2, $4); }
2021
|       NEW_TK class_type OP_TK CP_TK
2022
                { $$ = build_new_invocation ($2, NULL_TREE); }
2023
|       anonymous_class_creation
2024
        /* Added, JDK1.1 inner classes, modified to use name or
2025
           primary instead of primary solely which couldn't work in
2026
           all situations.  */
2027
|       something_dot_new identifier OP_TK CP_TK
2028
                {
2029
                  tree ctor = build_new_invocation ($2, NULL_TREE);
2030
                  $$ = make_qualified_primary ($1, ctor,
2031
                                               EXPR_WFL_LINECOL ($1));
2032
                }
2033
|       something_dot_new identifier OP_TK CP_TK class_body
2034
|       something_dot_new identifier OP_TK argument_list CP_TK
2035
                {
2036
                  tree ctor = build_new_invocation ($2, $4);
2037
                  $$ = make_qualified_primary ($1, ctor,
2038
                                               EXPR_WFL_LINECOL ($1));
2039
                }
2040
|       something_dot_new identifier OP_TK argument_list CP_TK class_body
2041
|       NEW_TK error SC_TK
2042
                {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2043
|       NEW_TK class_type error
2044
                {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2045
|       NEW_TK class_type OP_TK error
2046
                {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2047
|       NEW_TK class_type OP_TK argument_list error
2048
                {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2049
|       something_dot_new error
2050
                {
2051
                  $$ = NULL_TREE;
2052
                  YYERROR_NOW;
2053
                  yyerror ("Identifier expected");
2054
                  RECOVER;
2055
                }
2056
|       something_dot_new identifier error
2057
                {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2058
;
2059
 
2060
/* Created after JDK1.1 rules originally added to
2061
   class_instance_creation_expression, but modified to use
2062
   'class_type' instead of 'TypeName' (type_name) which is mentioned
2063
   in the documentation but doesn't exist. */
2064
 
2065
anonymous_class_creation:
2066
        NEW_TK class_type OP_TK argument_list CP_TK
2067
                { create_anonymous_class ($2); }
2068
        class_body
2069
                {
2070
                  tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2071
                  EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2072
 
2073
                  end_class_declaration (1);
2074
 
2075
                  /* Now we can craft the new expression */
2076
                  $$ = build_new_invocation (id, $4);
2077
 
2078
                  /* Note that we can't possibly be here if
2079
                     `class_type' is an interface (in which case the
2080
                     anonymous class extends Object and implements
2081
                     `class_type', hence its constructor can't have
2082
                     arguments.) */
2083
 
2084
                  /* Otherwise, the innerclass must feature a
2085
                     constructor matching `argument_list'. Anonymous
2086
                     classes are a bit special: it's impossible to
2087
                     define constructor for them, hence constructors
2088
                     must be generated following the hints provided by
2089
                     the `new' expression. Whether a super constructor
2090
                     of that nature exists or not is to be verified
2091
                     later on in get_constructor_super.
2092
 
2093
                     It's during the expansion of a `new' statement
2094
                     referring to an anonymous class that a ctor will
2095
                     be generated for the anonymous class, with the
2096
                     right arguments. */
2097
 
2098
                }
2099
|       NEW_TK class_type OP_TK CP_TK
2100
                { create_anonymous_class ($2); }
2101
        class_body
2102
                {
2103
                  tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2104
                  EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2105
 
2106
                  end_class_declaration (1);
2107
 
2108
                  /* Now we can craft the new expression. The
2109
                     statement doesn't need to be remember so that a
2110
                     constructor can be generated, since its signature
2111
                     is already known. */
2112
                  $$ = build_new_invocation (id, NULL_TREE);
2113
                }
2114
;
2115
 
2116
something_dot_new:              /* Added, not part of the specs. */
2117
        name DOT_TK NEW_TK
2118
                { $$ = $1; }
2119
|       primary DOT_TK NEW_TK
2120
                { $$ = $1; }
2121
;
2122
 
2123
argument_list:
2124
        expression
2125
                {
2126
                  $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2127
                  ctxp->formal_parameter_number = 1;
2128
                }
2129
|       argument_list C_TK expression
2130
                {
2131
                  ctxp->formal_parameter_number += 1;
2132
                  $$ = tree_cons (NULL_TREE, $3, $1);
2133
                }
2134
|       argument_list C_TK error
2135
                {yyerror ("Missing term"); RECOVER;}
2136
;
2137
 
2138
array_creation_uninitialized:
2139
        NEW_TK primitive_type dim_exprs
2140
                { $$ = build_newarray_node ($2, $3, 0); }
2141
|       NEW_TK class_or_interface_type dim_exprs
2142
                { $$ = build_newarray_node ($2, $3, 0); }
2143
|       NEW_TK primitive_type dim_exprs dims
2144
                { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2145
|       NEW_TK class_or_interface_type dim_exprs dims
2146
                { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2147
|       NEW_TK error CSB_TK
2148
                {yyerror ("'[' expected"); DRECOVER ("]");}
2149
|       NEW_TK error OSB_TK
2150
                {yyerror ("']' expected"); RECOVER;}
2151
;
2152
 
2153
array_creation_initialized:
2154
        /* Added, JDK1.1 anonymous array. Initial documentation rule
2155
           modified */
2156
        NEW_TK class_or_interface_type dims array_initializer
2157
                {
2158
                  char *sig;
2159
                  int osb = pop_current_osb (ctxp);
2160
                  while (osb--)
2161
                    obstack_grow (&temporary_obstack, "[]", 2);
2162
                  obstack_1grow (&temporary_obstack, '\0');
2163
                  sig = obstack_finish (&temporary_obstack);
2164
                  $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2165
                               $2, get_identifier (sig), $4);
2166
                }
2167
|       NEW_TK primitive_type dims array_initializer
2168
                {
2169
                  int osb = pop_current_osb (ctxp);
2170
                  tree type = $2;
2171
                  while (osb--)
2172
                    type = build_java_array_type (type, -1);
2173
                  $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2174
                               build_pointer_type (type), NULL_TREE, $4);
2175
                }
2176
|       NEW_TK error CSB_TK
2177
                {yyerror ("'[' expected"); DRECOVER ("]");}
2178
|       NEW_TK error OSB_TK
2179
                {yyerror ("']' expected"); RECOVER;}
2180
;
2181
 
2182
dim_exprs:
2183
        dim_expr
2184
                { $$ = build_tree_list (NULL_TREE, $1); }
2185
|       dim_exprs dim_expr
2186
                { $$ = tree_cons (NULL_TREE, $2, $$); }
2187
;
2188
 
2189
dim_expr:
2190
        OSB_TK expression CSB_TK
2191
                {
2192
                  if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2193
                    {
2194
                      $2 = build_wfl_node ($2);
2195
                      TREE_TYPE ($2) = NULL_TREE;
2196
                    }
2197
                  EXPR_WFL_LINECOL ($2) = $1.location;
2198
                  $$ = $2;
2199
                }
2200
|       OSB_TK expression error
2201
                {yyerror ("']' expected"); RECOVER;}
2202
|       OSB_TK error
2203
                {
2204
                  yyerror ("Missing term");
2205
                  yyerror ("']' expected");
2206
                  RECOVER;
2207
                }
2208
;
2209
 
2210
dims:
2211
        OSB_TK CSB_TK
2212
                {
2213
                  int allocate = 0;
2214
                  /* If not initialized, allocate memory for the osb
2215
                     numbers stack */
2216
                  if (!ctxp->osb_limit)
2217
                    {
2218
                      allocate = ctxp->osb_limit = 32;
2219
                      ctxp->osb_depth = -1;
2220
                    }
2221
                  /* If capacity overflown, reallocate a bigger chunk */
2222
                  else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2223
                    allocate = ctxp->osb_limit << 1;
2224
 
2225
                  if (allocate)
2226
                    {
2227
                      allocate *= sizeof (int);
2228
                      if (ctxp->osb_number)
2229
                        ctxp->osb_number = xrealloc (ctxp->osb_number,
2230
                                                     allocate);
2231
                      else
2232
                        ctxp->osb_number = xmalloc (allocate);
2233
                    }
2234
                  ctxp->osb_depth++;
2235
                  CURRENT_OSB (ctxp) = 1;
2236
                }
2237
|       dims OSB_TK CSB_TK
2238
                { CURRENT_OSB (ctxp)++; }
2239
|       dims OSB_TK error
2240
                { yyerror ("']' expected"); RECOVER;}
2241
;
2242
 
2243
field_access:
2244
        primary DOT_TK identifier
2245
                { $$ = make_qualified_primary ($1, $3, $2.location); }
2246
                /*  FIXME - REWRITE TO:
2247
                { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2248
|       SUPER_TK DOT_TK identifier
2249
                {
2250
                  tree super_wfl = build_wfl_node (super_identifier_node);
2251
                  SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2252
                  $$ = make_qualified_name (super_wfl, $3, $2.location);
2253
                }
2254
|       SUPER_TK error
2255
                {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2256
;
2257
 
2258
method_invocation:
2259
        name OP_TK CP_TK
2260
                { $$ = build_method_invocation ($1, NULL_TREE); }
2261
|       name OP_TK argument_list CP_TK
2262
                { $$ = build_method_invocation ($1, $3); }
2263
|       primary DOT_TK identifier OP_TK CP_TK
2264
                {
2265
                  if (TREE_CODE ($1) == THIS_EXPR)
2266
                    $$ = build_this_super_qualified_invocation
2267
                      (1, $3, NULL_TREE, 0, $2.location);
2268
                  else
2269
                    {
2270
                      tree invok = build_method_invocation ($3, NULL_TREE);
2271
                      $$ = make_qualified_primary ($1, invok, $2.location);
2272
                    }
2273
                }
2274
|       primary DOT_TK identifier OP_TK argument_list CP_TK
2275
                {
2276
                  if (TREE_CODE ($1) == THIS_EXPR)
2277
                    $$ = build_this_super_qualified_invocation
2278
                      (1, $3, $5, 0, $2.location);
2279
                  else
2280
                    {
2281
                      tree invok = build_method_invocation ($3, $5);
2282
                      $$ = make_qualified_primary ($1, invok, $2.location);
2283
                    }
2284
                }
2285
|       SUPER_TK DOT_TK identifier OP_TK CP_TK
2286
                {
2287
                  $$ = build_this_super_qualified_invocation
2288
                    (0, $3, NULL_TREE, $1.location, $2.location);
2289
                }
2290
|       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2291
                {
2292
                  $$ = build_this_super_qualified_invocation
2293
                    (0, $3, $5, $1.location, $2.location);
2294
                }
2295
        /* Screws up thing. I let it here until I'm convinced it can
2296
           be removed. FIXME
2297
|       primary DOT_TK error
2298
                {yyerror ("'(' expected"); DRECOVER(bad);} */
2299
|       SUPER_TK DOT_TK error CP_TK
2300
                { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2301
|       SUPER_TK DOT_TK error DOT_TK
2302
                { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2303
;
2304
 
2305
array_access:
2306
        name OSB_TK expression CSB_TK
2307
                { $$ = build_array_ref ($2.location, $1, $3); }
2308
|       primary_no_new_array OSB_TK expression CSB_TK
2309
                { $$ = build_array_ref ($2.location, $1, $3); }
2310
|       array_creation_initialized OSB_TK expression CSB_TK
2311
                { $$ = build_array_ref ($2.location, $1, $3); }
2312
|       name OSB_TK error
2313
                {
2314
                  yyerror ("Missing term and ']' expected");
2315
                  DRECOVER(array_access);
2316
                }
2317
|       name OSB_TK expression error
2318
                {
2319
                  yyerror ("']' expected");
2320
                  DRECOVER(array_access);
2321
                }
2322
|       primary_no_new_array OSB_TK error
2323
                {
2324
                  yyerror ("Missing term and ']' expected");
2325
                  DRECOVER(array_access);
2326
                }
2327
|       primary_no_new_array OSB_TK expression error
2328
                {
2329
                  yyerror ("']' expected");
2330
                  DRECOVER(array_access);
2331
                }
2332
|       array_creation_initialized OSB_TK error
2333
                {
2334
                  yyerror ("Missing term and ']' expected");
2335
                  DRECOVER(array_access);
2336
                }
2337
|       array_creation_initialized OSB_TK expression error
2338
                {
2339
                  yyerror ("']' expected");
2340
                  DRECOVER(array_access);
2341
                }
2342
;
2343
 
2344
postfix_expression:
2345
        primary
2346
|       name
2347
|       post_increment_expression
2348
|       post_decrement_expression
2349
;
2350
 
2351
post_increment_expression:
2352
        postfix_expression INCR_TK
2353
                { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2354
;
2355
 
2356
post_decrement_expression:
2357
        postfix_expression DECR_TK
2358
                { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2359
;
2360
 
2361
trap_overflow_corner_case:
2362
        pre_increment_expression
2363
|       pre_decrement_expression
2364
|       PLUS_TK unary_expression
2365
                {$$ = build_unaryop ($1.token, $1.location, $2); }
2366
|       unary_expression_not_plus_minus
2367
|       PLUS_TK error
2368
                {yyerror ("Missing term"); RECOVER}
2369
;
2370
 
2371
unary_expression:
2372
        trap_overflow_corner_case
2373
                {
2374
                  if ($1)
2375
                    error_if_numeric_overflow ($1);
2376
                  $$ = $1;
2377
                }
2378
|       MINUS_TK trap_overflow_corner_case
2379
                {$$ = build_unaryop ($1.token, $1.location, $2); }
2380
|       MINUS_TK error
2381
                {yyerror ("Missing term"); RECOVER}
2382
;
2383
 
2384
pre_increment_expression:
2385
        INCR_TK unary_expression
2386
                {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2387
|       INCR_TK error
2388
                {yyerror ("Missing term"); RECOVER}
2389
;
2390
 
2391
pre_decrement_expression:
2392
        DECR_TK unary_expression
2393
                {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2394
|       DECR_TK error
2395
                {yyerror ("Missing term"); RECOVER}
2396
;
2397
 
2398
unary_expression_not_plus_minus:
2399
        postfix_expression
2400
|       NOT_TK unary_expression
2401
                {$$ = build_unaryop ($1.token, $1.location, $2); }
2402
|       NEG_TK unary_expression
2403
                {$$ = build_unaryop ($1.token, $1.location, $2); }
2404
|       cast_expression
2405
|       NOT_TK error
2406
                {yyerror ("Missing term"); RECOVER}
2407
|       NEG_TK error
2408
                {yyerror ("Missing term"); RECOVER}
2409
;
2410
 
2411
cast_expression:                /* Error handling here is potentially weak */
2412
        OP_TK primitive_type dims CP_TK unary_expression
2413
                {
2414
                  tree type = $2;
2415
                  int osb = pop_current_osb (ctxp);
2416
                  while (osb--)
2417
                    type = build_java_array_type (type, -1);
2418
                  $$ = build_cast ($1.location, type, $5);
2419
                }
2420
|       OP_TK primitive_type CP_TK unary_expression
2421
                { $$ = build_cast ($1.location, $2, $4); }
2422
|       OP_TK expression CP_TK unary_expression_not_plus_minus
2423
                { $$ = build_cast ($1.location, $2, $4); }
2424
|       OP_TK name dims CP_TK unary_expression_not_plus_minus
2425
                {
2426
                  const char *ptr;
2427
                  int osb = pop_current_osb (ctxp);
2428
                  obstack_grow (&temporary_obstack,
2429
                                IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2430
                                IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2431
                  while (osb--)
2432
                    obstack_grow (&temporary_obstack, "[]", 2);
2433
                  obstack_1grow (&temporary_obstack, '\0');
2434
                  ptr = obstack_finish (&temporary_obstack);
2435
                  EXPR_WFL_NODE ($2) = get_identifier (ptr);
2436
                  $$ = build_cast ($1.location, $2, $5);
2437
                }
2438
|       OP_TK primitive_type OSB_TK error
2439
                {yyerror ("']' expected, invalid type expression");}
2440
|       OP_TK error
2441
                {
2442
                  YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2443
                  RECOVER;
2444
                }
2445
|       OP_TK primitive_type dims CP_TK error
2446
                {yyerror ("Missing term"); RECOVER;}
2447
|       OP_TK primitive_type CP_TK error
2448
                {yyerror ("Missing term"); RECOVER;}
2449
|       OP_TK name dims CP_TK error
2450
                {yyerror ("Missing term"); RECOVER;}
2451
;
2452
 
2453
multiplicative_expression:
2454
        unary_expression
2455
|       multiplicative_expression MULT_TK unary_expression
2456
                {
2457
                  $$ = build_binop (BINOP_LOOKUP ($2.token),
2458
                                    $2.location, $1, $3);
2459
                }
2460
|       multiplicative_expression DIV_TK unary_expression
2461
                {
2462
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2463
                                    $1, $3);
2464
                }
2465
|       multiplicative_expression REM_TK unary_expression
2466
                {
2467
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468
                                    $1, $3);
2469
                }
2470
|       multiplicative_expression MULT_TK error
2471
                {yyerror ("Missing term"); RECOVER;}
2472
|       multiplicative_expression DIV_TK error
2473
                {yyerror ("Missing term"); RECOVER;}
2474
|       multiplicative_expression REM_TK error
2475
                {yyerror ("Missing term"); RECOVER;}
2476
;
2477
 
2478
additive_expression:
2479
        multiplicative_expression
2480
|       additive_expression PLUS_TK multiplicative_expression
2481
                {
2482
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2483
                                    $1, $3);
2484
                }
2485
|       additive_expression MINUS_TK multiplicative_expression
2486
                {
2487
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488
                                    $1, $3);
2489
                }
2490
|       additive_expression PLUS_TK error
2491
                {yyerror ("Missing term"); RECOVER;}
2492
|       additive_expression MINUS_TK error
2493
                {yyerror ("Missing term"); RECOVER;}
2494
;
2495
 
2496
shift_expression:
2497
        additive_expression
2498
|       shift_expression LS_TK additive_expression
2499
                {
2500
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501
                                    $1, $3);
2502
                }
2503
|       shift_expression SRS_TK additive_expression
2504
                {
2505
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506
                                    $1, $3);
2507
                }
2508
|       shift_expression ZRS_TK additive_expression
2509
                {
2510
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511
                                    $1, $3);
2512
                }
2513
|       shift_expression LS_TK error
2514
                {yyerror ("Missing term"); RECOVER;}
2515
|       shift_expression SRS_TK error
2516
                {yyerror ("Missing term"); RECOVER;}
2517
|       shift_expression ZRS_TK error
2518
                {yyerror ("Missing term"); RECOVER;}
2519
;
2520
 
2521
relational_expression:
2522
        shift_expression
2523
|       relational_expression LT_TK shift_expression
2524
                {
2525
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526
                                    $1, $3);
2527
                }
2528
|       relational_expression GT_TK shift_expression
2529
                {
2530
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531
                                    $1, $3);
2532
                }
2533
|       relational_expression LTE_TK shift_expression
2534
                {
2535
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536
                                    $1, $3);
2537
                }
2538
|       relational_expression GTE_TK shift_expression
2539
                {
2540
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541
                                    $1, $3);
2542
                }
2543
|       relational_expression INSTANCEOF_TK reference_type
2544
                { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2545
|       relational_expression LT_TK error
2546
                {yyerror ("Missing term"); RECOVER;}
2547
|       relational_expression GT_TK error
2548
                {yyerror ("Missing term"); RECOVER;}
2549
|       relational_expression LTE_TK error
2550
                {yyerror ("Missing term"); RECOVER;}
2551
|       relational_expression GTE_TK error
2552
                {yyerror ("Missing term"); RECOVER;}
2553
|       relational_expression INSTANCEOF_TK error
2554
                {yyerror ("Invalid reference type"); RECOVER;}
2555
;
2556
 
2557
equality_expression:
2558
        relational_expression
2559
|       equality_expression EQ_TK relational_expression
2560
                {
2561
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2562
                                    $1, $3);
2563
                }
2564
|       equality_expression NEQ_TK relational_expression
2565
                {
2566
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2567
                                    $1, $3);
2568
                }
2569
|       equality_expression EQ_TK error
2570
                {yyerror ("Missing term"); RECOVER;}
2571
|       equality_expression NEQ_TK error
2572
                {yyerror ("Missing term"); RECOVER;}
2573
;
2574
 
2575
and_expression:
2576
        equality_expression
2577
|       and_expression AND_TK equality_expression
2578
                {
2579
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2580
                                    $1, $3);
2581
                }
2582
|       and_expression AND_TK error
2583
                {yyerror ("Missing term"); RECOVER;}
2584
;
2585
 
2586
exclusive_or_expression:
2587
        and_expression
2588
|       exclusive_or_expression XOR_TK and_expression
2589
                {
2590
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2591
                                    $1, $3);
2592
                }
2593
|       exclusive_or_expression XOR_TK error
2594
                {yyerror ("Missing term"); RECOVER;}
2595
;
2596
 
2597
inclusive_or_expression:
2598
        exclusive_or_expression
2599
|       inclusive_or_expression OR_TK exclusive_or_expression
2600
                {
2601
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2602
                                    $1, $3);
2603
                }
2604
|       inclusive_or_expression OR_TK error
2605
                {yyerror ("Missing term"); RECOVER;}
2606
;
2607
 
2608
conditional_and_expression:
2609
        inclusive_or_expression
2610
|       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2611
                {
2612
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2613
                                    $1, $3);
2614
                }
2615
|       conditional_and_expression BOOL_AND_TK error
2616
                {yyerror ("Missing term"); RECOVER;}
2617
;
2618
 
2619
conditional_or_expression:
2620
        conditional_and_expression
2621
|       conditional_or_expression BOOL_OR_TK conditional_and_expression
2622
                {
2623
                  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2624
                                    $1, $3);
2625
                }
2626
|       conditional_or_expression BOOL_OR_TK error
2627
                {yyerror ("Missing term"); RECOVER;}
2628
;
2629
 
2630
conditional_expression:         /* Error handling here is weak */
2631
        conditional_or_expression
2632
|       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2633
                {
2634
                  $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2635
                  SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2636
                }
2637
|       conditional_or_expression REL_QM_TK REL_CL_TK error
2638
                {
2639
                  YYERROR_NOW;
2640
                  yyerror ("Missing term");
2641
                  DRECOVER (1);
2642
                }
2643
|       conditional_or_expression REL_QM_TK error
2644
                {yyerror ("Missing term"); DRECOVER (2);}
2645
|       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2646
                {yyerror ("Missing term"); DRECOVER (3);}
2647
;
2648
 
2649
assignment_expression:
2650
        conditional_expression
2651
|       assignment
2652
;
2653
 
2654
assignment:
2655
        left_hand_side assignment_operator assignment_expression
2656
                { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2657
|       left_hand_side assignment_operator error
2658
                {
2659
                  YYNOT_TWICE yyerror ("Missing term");
2660
                  DRECOVER (assign);
2661
                }
2662
;
2663
 
2664
left_hand_side:
2665
        name
2666
|       field_access
2667
|       array_access
2668
;
2669
 
2670
assignment_operator:
2671
        ASSIGN_ANY_TK
2672
|       ASSIGN_TK
2673
;
2674
 
2675
expression:
2676
        assignment_expression
2677
;
2678
 
2679
constant_expression:
2680
        expression
2681
;
2682
 
2683
%%
2684
 
2685
/* Helper function to retrieve an OSB count. Should be used when the
2686
   `dims:' rule is being used.  */
2687
 
2688
static int
2689
pop_current_osb (struct parser_ctxt *ctxp)
2690
{
2691
  int to_return;
2692
 
2693
  if (ctxp->osb_depth < 0)
2694
    abort ();
2695
 
2696
  to_return = CURRENT_OSB (ctxp);
2697
  ctxp->osb_depth--;
2698
 
2699
  return to_return;
2700
}
2701
 
2702
 
2703
 
2704
/* This section of the code deal with save/restoring parser contexts.
2705
   Add mode documentation here. FIXME */
2706
 
2707
/* Helper function. Create a new parser context. With
2708
   COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2709
   context is copied, otherwise, the new context is zeroed. The newly
2710
   created context becomes the current one.  */
2711
 
2712
static void
2713
create_new_parser_context (int copy_from_previous)
2714
{
2715
  struct parser_ctxt *new;
2716
 
2717
  new = ggc_alloc (sizeof (struct parser_ctxt));
2718
  if (copy_from_previous)
2719
    {
2720
      memcpy (new, ctxp, sizeof (struct parser_ctxt));
2721
      /* This flag, indicating the context saves global values,
2722
         should only be set by java_parser_context_save_global.  */
2723
      new->saved_data_ctx = 0;
2724
    }
2725
  else
2726
    memset (new, 0, sizeof (struct parser_ctxt));
2727
 
2728
  new->next = ctxp;
2729
  ctxp = new;
2730
}
2731
 
2732
/* Create a new parser context and make it the current one. */
2733
 
2734
void
2735
java_push_parser_context (void)
2736
{
2737
  create_new_parser_context (0);
2738
}
2739
 
2740
void
2741
java_pop_parser_context (int generate)
2742
{
2743
  tree current;
2744
  struct parser_ctxt *next;
2745
 
2746
  if (!ctxp)
2747
    return;
2748
 
2749
  next = ctxp->next;
2750
  if (next)
2751
    {
2752
      input_location = ctxp->save_location;
2753
      current_class = ctxp->class_type;
2754
    }
2755
 
2756
  /* If the old and new lexers differ, then free the old one.  */
2757
  if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2758
    java_destroy_lexer (ctxp->lexer);
2759
 
2760
  /* Set the single import class file flag to 0 for the current list
2761
     of imported things */
2762
  for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2763
    IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2764
 
2765
  /* If we pushed a context to parse a class intended to be generated,
2766
     we keep it so we can remember the class. What we could actually
2767
     do is to just update a list of class names.  */
2768
  if (generate)
2769
    {
2770
      if (ctxp_for_generation_last == NULL)
2771
        ctxp_for_generation = ctxp;
2772
      else
2773
        ctxp_for_generation_last->next = ctxp;
2774
      ctxp->next = NULL;
2775
      ctxp_for_generation_last = ctxp;
2776
    }
2777
 
2778
  /* And restore those of the previous context */
2779
  if ((ctxp = next))            /* Assignment is really meant here */
2780
    for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2781
      IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2782
}
2783
 
2784
/* Create a parser context for the use of saving some global
2785
   variables.  */
2786
 
2787
void
2788
java_parser_context_save_global (void)
2789
{
2790
  if (!ctxp)
2791
    {
2792
      java_push_parser_context ();
2793
      ctxp->saved_data_ctx = 1;
2794
    }
2795
 
2796
  /* If this context already stores data, create a new one suitable
2797
     for data storage. */
2798
  else if (ctxp->saved_data)
2799
    {
2800
      create_new_parser_context (1);
2801
      ctxp->saved_data_ctx = 1;
2802
    }
2803
 
2804
  ctxp->save_location = input_location;
2805
  ctxp->class_type = current_class;
2806
  ctxp->function_decl = current_function_decl;
2807
  ctxp->saved_data = 1;
2808
}
2809
 
2810
/* Restore some global variables from the previous context. Make the
2811
   previous context the current one.  */
2812
 
2813
void
2814
java_parser_context_restore_global (void)
2815
{
2816
  input_location = ctxp->save_location;
2817
  current_class = ctxp->class_type;
2818
  if (wfl_operator)
2819
#ifdef USE_MAPPED_LOCATION
2820
    SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2821
#else
2822
    EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2823
#endif
2824
  current_function_decl = ctxp->function_decl;
2825
  ctxp->saved_data = 0;
2826
  if (ctxp->saved_data_ctx)
2827
    java_pop_parser_context (0);
2828
}
2829
 
2830
/* Suspend vital data for the current class/function being parsed so
2831
   that an other class can be parsed. Used to let local/anonymous
2832
   classes be parsed.  */
2833
 
2834
static void
2835
java_parser_context_suspend (void)
2836
{
2837
  /* This makes debugging through java_debug_context easier */
2838
  static const char *const name = "";
2839
 
2840
  /* Duplicate the previous context, use it to save the globals we're
2841
     interested in */
2842
  create_new_parser_context (1);
2843
  ctxp->function_decl = current_function_decl;
2844
  ctxp->class_type = current_class;
2845
 
2846
  /* Then create a new context which inherits all data from the
2847
     previous one. This will be the new current context  */
2848
  create_new_parser_context (1);
2849
 
2850
  /* Help debugging */
2851
  ctxp->next->filename = name;
2852
}
2853
 
2854
/* Resume vital data for the current class/function being parsed so
2855
   that an other class can be parsed. Used to let local/anonymous
2856
   classes be parsed.  The trick is the data storing file position
2857
   informations must be restored to their current value, so parsing
2858
   can resume as if no context was ever saved. */
2859
 
2860
static void
2861
java_parser_context_resume (void)
2862
{
2863
  struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2864
  struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2865
  struct parser_ctxt *restored = saver->next; /* This one is the old current */
2866
 
2867
  /* We need to inherit the list of classes to complete/generate */
2868
  restored->classd_list = old->classd_list;
2869
  restored->class_list = old->class_list;
2870
 
2871
  /* Restore the current class and function from the saver */
2872
  current_class = saver->class_type;
2873
  current_function_decl = saver->function_decl;
2874
 
2875
  /* Retrieve the restored context */
2876
  ctxp = restored;
2877
 
2878
  /* Re-installed the data for the parsing to carry on */
2879
  memcpy (&ctxp->marker_begining, &old->marker_begining,
2880
          (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2881
}
2882
 
2883
/* Add a new anchor node to which all statement(s) initializing static
2884
   and non static initialized upon declaration field(s) will be
2885
   linked.  */
2886
 
2887
static void
2888
java_parser_context_push_initialized_field (void)
2889
{
2890
  tree node;
2891
 
2892
  node = build_tree_list (NULL_TREE, NULL_TREE);
2893
  TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2894
  CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2895
 
2896
  node = build_tree_list (NULL_TREE, NULL_TREE);
2897
  TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2898
  CPC_INITIALIZER_LIST (ctxp) = node;
2899
 
2900
  node = build_tree_list (NULL_TREE, NULL_TREE);
2901
  TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2902
  CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2903
}
2904
 
2905
/* Pop the lists of initialized field. If this lists aren't empty,
2906
   remember them so we can use it to create and populate the finit$
2907
   or  functions. */
2908
 
2909
static void
2910
java_parser_context_pop_initialized_field (void)
2911
{
2912
  tree stmts;
2913
  tree class_type = TREE_TYPE (GET_CPC ());
2914
 
2915
  if (CPC_INITIALIZER_LIST (ctxp))
2916
    {
2917
      stmts = CPC_INITIALIZER_STMT (ctxp);
2918
      CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2919
      if (stmts && !java_error_count)
2920
        TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2921
    }
2922
 
2923
  if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2924
    {
2925
      stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2926
      CPC_STATIC_INITIALIZER_LIST (ctxp) =
2927
        TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2928
      /* Keep initialization in order to enforce 8.5 */
2929
      if (stmts && !java_error_count)
2930
        TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2931
    }
2932
 
2933
  /* JDK 1.1 instance initializers */
2934
  if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2935
    {
2936
      stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2937
      CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2938
        TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2939
      if (stmts && !java_error_count)
2940
        TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2941
    }
2942
}
2943
 
2944
static tree
2945
reorder_static_initialized (tree list)
2946
{
2947
  /* We have to keep things in order. The alias initializer have to
2948
     come first, then the initialized regular field, in reverse to
2949
     keep them in lexical order. */
2950
  tree marker, previous = NULL_TREE;
2951
  for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2952
    if (TREE_CODE (marker) == TREE_LIST
2953
        && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2954
      break;
2955
 
2956
  /* No static initialized, the list is fine as is */
2957
  if (!previous)
2958
    list = TREE_CHAIN (marker);
2959
 
2960
  /* No marker? reverse the whole list */
2961
  else if (!marker)
2962
    list = nreverse (list);
2963
 
2964
  /* Otherwise, reverse what's after the marker and the new reordered
2965
     sublist will replace the marker. */
2966
  else
2967
    {
2968
      TREE_CHAIN (previous) = NULL_TREE;
2969
      list = nreverse (list);
2970
      list = chainon (TREE_CHAIN (marker), list);
2971
    }
2972
  return list;
2973
}
2974
 
2975
/* Helper functions to dump the parser context stack.  */
2976
 
2977
#define TAB_CONTEXT(C) \
2978
  {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2979
 
2980
static void
2981
java_debug_context_do (int tab)
2982
{
2983
  struct parser_ctxt *copy = ctxp;
2984
  while (copy)
2985
    {
2986
      TAB_CONTEXT (tab);
2987
      fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2988
      TAB_CONTEXT (tab);
2989
      fprintf (stderr, "filename: %s\n", copy->filename);
2990
      TAB_CONTEXT (tab);
2991
      fprintf (stderr, "package: %s\n",
2992
               (copy->package ?
2993
                IDENTIFIER_POINTER (copy->package) : ""));
2994
      TAB_CONTEXT (tab);
2995
      fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2996
      TAB_CONTEXT (tab);
2997
      fprintf (stderr, "saved data: %d\n", copy->saved_data);
2998
      copy = copy->next;
2999
      tab += 2;
3000
    }
3001
}
3002
 
3003
/* Dump the stacked up parser contexts. Intended to be called from a
3004
   debugger.  */
3005
 
3006
void
3007
java_debug_context (void)
3008
{
3009
  java_debug_context_do (0);
3010
}
3011
 
3012
 
3013
 
3014
/* Flag for the error report routine to issue the error the first time
3015
   it's called (overriding the default behavior which is to drop the
3016
   first invocation and honor the second one, taking advantage of a
3017
   richer context.  */
3018
static int force_error = 0;
3019
 
3020
/* Reporting an constructor invocation error.  */
3021
static void
3022
parse_ctor_invocation_error (void)
3023
{
3024
  if (DECL_CONSTRUCTOR_P (current_function_decl))
3025
    yyerror ("Constructor invocation must be first thing in a constructor");
3026
  else
3027
    yyerror ("Only constructors can invoke constructors");
3028
}
3029
 
3030
/* Reporting JDK1.1 features not implemented.  */
3031
 
3032
static tree
3033
parse_jdk1_1_error (const char *msg)
3034
{
3035
  sorry (": %qs JDK1.1(TM) feature", msg);
3036
  java_error_count++;
3037
  return build_java_empty_stmt ();
3038
}
3039
 
3040
static int do_warning = 0;
3041
 
3042
void
3043
yyerror (const char *msgid)
3044
{
3045
#ifdef USE_MAPPED_LOCATION
3046
  static source_location elc;
3047
  expanded_location xloc = expand_location (input_location);
3048
  int current_line = xloc.line;
3049
#else
3050
  static java_lc elc;
3051
  int save_lineno;
3052
  int current_line = input_line;
3053
#endif
3054
  static int prev_lineno;
3055
  static const char *prev_msg;
3056
 
3057
  char *remainder, *code_from_source;
3058
 
3059
  if (!force_error && prev_lineno == current_line)
3060
    return;
3061
#ifndef USE_MAPPED_LOCATION
3062
  current_line = ctxp->lexer->token_start.line;
3063
#endif
3064
 
3065
  /* Save current error location but report latter, when the context is
3066
     richer.  */
3067
  if (ctxp->java_error_flag == 0)
3068
    {
3069
      ctxp->java_error_flag = 1;
3070
#ifdef USE_MAPPED_LOCATION
3071
      elc = input_location;
3072
#else
3073
      elc = ctxp->lexer->token_start;
3074
#endif
3075
      /* Do something to use the previous line if we're reaching the
3076
         end of the file... */
3077
#ifdef VERBOSE_SKELETON
3078
      printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3079
#endif
3080
      return;
3081
    }
3082
 
3083
  /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3084
  if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3085
    return;
3086
 
3087
  ctxp->java_error_flag = 0;
3088
  if (do_warning)
3089
    java_warning_count++;
3090
  else
3091
    java_error_count++;
3092
 
3093
#if 0 /* FIXME */
3094
  if (elc.col == 0 && msgid && msgid[1] == ';')
3095
    elc = ctxp->prev_line_end;
3096
#endif
3097
 
3098
  prev_msg = msgid;
3099
 
3100
#ifdef USE_MAPPED_LOCATION
3101
  prev_lineno = current_line;
3102
  code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3103
#else
3104
  save_lineno = input_line;
3105
  prev_lineno = input_line = current_line;
3106
  code_from_source = java_get_line_col (input_filename, current_line,
3107
                                        ctxp->lexer->token_start.col);
3108
#endif
3109
 
3110
 
3111
  obstack_grow0 (&temporary_obstack,
3112
                 code_from_source, strlen (code_from_source));
3113
  remainder = obstack_finish (&temporary_obstack);
3114
  if (do_warning)
3115
    warning (0, "%s.\n%s", msgid, remainder);
3116
  else
3117
    error ("%s.\n%s", msgid, remainder);
3118
 
3119
  /* This allow us to cheaply avoid an extra 'Invalid expression
3120
     statement' error report when errors have been already reported on
3121
     the same line. This occurs when we report an error but don't have
3122
     a synchronization point other than ';', which
3123
     expression_statement is the only one to take care of.  */
3124
#ifndef USE_MAPPED_LOCATION
3125
  input_line = save_lineno;
3126
#endif
3127
  ctxp->prevent_ese = input_line;
3128
}
3129
 
3130
static void
3131
issue_warning_error_from_context (
3132
#ifdef USE_MAPPED_LOCATION
3133
                                  source_location cl,
3134
#else
3135
                                  tree cl,
3136
#endif
3137
                                  const char *gmsgid, va_list *ap)
3138
{
3139
#ifdef USE_MAPPED_LOCATION
3140
  source_location saved_location = input_location;
3141
  expanded_location xloc = expand_location (cl);
3142
#else
3143
  java_lc save_lc = ctxp->lexer->token_start;
3144
  const char *saved = ctxp->filename, *saved_input_filename;
3145
#endif
3146
  char buffer [4096];
3147
  text_info text;
3148
 
3149
  text.err_no = errno;
3150
  text.args_ptr = ap;
3151
  text.format_spec = gmsgid;
3152
  pp_format (global_dc->printer, &text);
3153
  pp_output_formatted_text (global_dc->printer);
3154
  strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3155
  buffer[sizeof (buffer) - 1] = '\0';
3156
  pp_clear_output_area (global_dc->printer);
3157
 
3158
  force_error = 1;
3159
 
3160
#ifdef USE_MAPPED_LOCATION
3161
  if (xloc.file != NULL)
3162
    {
3163
      ctxp->filename = xloc.file;
3164
      input_location = cl;
3165
    }
3166
#else
3167
  ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3168
  ctxp->lexer->token_start.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3169
                                   : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3170
                                   : EXPR_WFL_COLNO (cl));
3171
 
3172
  /* We have a CL, that's a good reason for using it if it contains data */
3173
  if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3174
    ctxp->filename = EXPR_WFL_FILENAME (cl);
3175
  saved_input_filename = input_filename;
3176
  input_filename = ctxp->filename;
3177
#endif
3178
  java_error (NULL);
3179
  java_error (buffer);
3180
#ifdef USE_MAPPED_LOCATION
3181
  input_location = saved_location;
3182
#else
3183
  ctxp->filename = saved;
3184
  input_filename = saved_input_filename;
3185
  ctxp->lexer->token_start = save_lc;
3186
#endif
3187
  force_error = 0;
3188
}
3189
 
3190
/* Issue an error message at a current source line CL.
3191
   FUTURE/FIXME:  change cl to be a source_location. */
3192
 
3193
void
3194
parse_error_context (tree cl, const char *gmsgid, ...)
3195
{
3196
  va_list ap;
3197
  va_start (ap, gmsgid);
3198
#ifdef USE_MAPPED_LOCATION
3199
  issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3200
#else
3201
  issue_warning_error_from_context (cl, gmsgid, &ap);
3202
#endif
3203
  va_end (ap);
3204
}
3205
 
3206
/* Issue a warning at a current source line CL.
3207
   FUTURE/FIXME:  change cl to be a source_location. */
3208
 
3209
static void
3210
parse_warning_context (tree cl, const char *gmsgid, ...)
3211
{
3212
  va_list ap;
3213
  va_start (ap, gmsgid);
3214
 
3215
  do_warning = 1;
3216
#ifdef USE_MAPPED_LOCATION
3217
  issue_warning_error_from_context (EXPR_LOCATION (cl), gmsgid, &ap);
3218
#else
3219
  issue_warning_error_from_context (cl, gmsgid, &ap);
3220
#endif
3221
  do_warning = 0;
3222
  va_end (ap);
3223
}
3224
 
3225
static tree
3226
find_expr_with_wfl (tree node)
3227
{
3228
  while (node)
3229
    {
3230
      enum tree_code_class code;
3231
      tree to_return;
3232
 
3233
      switch (TREE_CODE (node))
3234
        {
3235
        case BLOCK:
3236
          node = BLOCK_EXPR_BODY (node);
3237
          continue;
3238
 
3239
        case COMPOUND_EXPR:
3240
          to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3241
          if (to_return)
3242
            return to_return;
3243
          node = TREE_OPERAND (node, 1);
3244
          continue;
3245
 
3246
        case LOOP_EXPR:
3247
          node = TREE_OPERAND (node, 0);
3248
          continue;
3249
 
3250
        case LABELED_BLOCK_EXPR:
3251
          node = LABELED_BLOCK_BODY (node);
3252
          continue;
3253
 
3254
        default:
3255
          code = TREE_CODE_CLASS (TREE_CODE (node));
3256
          if (((code == tcc_unary) || (code == tcc_binary)
3257
               || (code == tcc_expression))
3258
              && EXPR_WFL_LINECOL (node))
3259
            return node;
3260
          return NULL_TREE;
3261
        }
3262
    }
3263
  return NULL_TREE;
3264
}
3265
 
3266
/* Issue a missing return statement error. Uses METHOD to figure the
3267
   last line of the method the error occurs in.  */
3268
 
3269
static void
3270
missing_return_error (tree method)
3271
{
3272
#ifdef USE_MAPPED_LOCATION
3273
  SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3274
#else
3275
  EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3276
#endif
3277
  parse_error_context (wfl_operator, "Missing return statement");
3278
}
3279
 
3280
/* Issue an unreachable statement error. From NODE, find the next
3281
   statement to report appropriately.  */
3282
static void
3283
unreachable_stmt_error (tree node)
3284
{
3285
  /* Browse node to find the next expression node that has a WFL. Use
3286
     the location to report the error */
3287
  if (TREE_CODE (node) == COMPOUND_EXPR)
3288
    node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3289
  else
3290
    node = find_expr_with_wfl (node);
3291
 
3292
  if (node)
3293
    {
3294
#ifdef USE_MAPPED_LOCATION
3295
      SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3296
#else
3297
      EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3298
#endif
3299
      parse_error_context (wfl_operator, "Unreachable statement");
3300
    }
3301
  else
3302
    abort ();
3303
}
3304
 
3305
static int
3306
not_accessible_field_error (tree wfl, tree decl)
3307
{
3308
  parse_error_context
3309
    (wfl, "Can't access %s field %<%s.%s%> from %qs",
3310
     accessibility_string (get_access_flags_from_decl (decl)),
3311
     GET_TYPE_NAME (DECL_CONTEXT (decl)),
3312
     IDENTIFIER_POINTER (DECL_NAME (decl)),
3313
     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3314
  return 1;
3315
}
3316
 
3317
int
3318
java_report_errors (void)
3319
{
3320
  if (java_error_count)
3321
    fprintf (stderr, "%d error%s",
3322
             java_error_count, (java_error_count == 1 ? "" : "s"));
3323
  if (java_warning_count)
3324
    fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3325
             java_warning_count, (java_warning_count == 1 ? "" : "s"));
3326
  if (java_error_count || java_warning_count)
3327
    putc ('\n', stderr);
3328
  return java_error_count;
3329
}
3330
 
3331
static char *
3332
java_accstring_lookup (int flags)
3333
{
3334
  static char buffer [80];
3335
#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3336
 
3337
  /* Access modifier looked-up first for easier report on forbidden
3338
     access. */
3339
  if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3340
  if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3341
  if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3342
  if (flags & ACC_STATIC) COPY_RETURN ("static");
3343
  if (flags & ACC_FINAL) COPY_RETURN ("final");
3344
  if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3345
  if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3346
  if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3347
  if (flags & ACC_NATIVE) COPY_RETURN ("native");
3348
  if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3349
  if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3350
 
3351
  buffer [0] = '\0';
3352
  return buffer;
3353
#undef COPY_RETURN
3354
}
3355
 
3356
/* Returns a string denoting the accessibility of a class or a member as
3357
   indicated by FLAGS.  We need a separate function from
3358
   java_accstring_lookup, as the latter can return spurious "static", etc.
3359
   if package-private access is defined (in which case none of the
3360
   relevant access control bits in FLAGS is set).  */
3361
 
3362
static const char *
3363
accessibility_string (int flags)
3364
{
3365
  if (flags & ACC_PRIVATE) return "private";
3366
  if (flags & ACC_PROTECTED) return "protected";
3367
  if (flags & ACC_PUBLIC) return "public";
3368
 
3369
  return "package-private";
3370
}
3371
 
3372
/* Issuing error messages upon redefinition of classes, interfaces or
3373
   variables. */
3374
 
3375
static void
3376
classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3377
{
3378
  parse_error_context (cl, "%s %qs already defined in %s:%d",
3379
                       context, IDENTIFIER_POINTER (id),
3380
                       DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3381
  /* Here we should point out where its redefined. It's a unicode. FIXME */
3382
}
3383
 
3384
static void
3385
variable_redefinition_error (tree context, tree name, tree type, int line)
3386
{
3387
  const char *type_name;
3388
 
3389
  /* Figure a proper name for type. We might haven't resolved it */
3390
  if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3391
    type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3392
  else
3393
    type_name = lang_printable_name (type, 0);
3394
 
3395
  parse_error_context (context,
3396
                       "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3397
                       IDENTIFIER_POINTER (name),
3398
                       type_name, IDENTIFIER_POINTER (name), line);
3399
}
3400
 
3401
/* If ANAME is terminated with `[]', it indicates an array. This
3402
   function returns the number of `[]' found and if this number is
3403
   greater than zero, it extracts the array type name and places it in
3404
   the node pointed to by TRIMMED unless TRIMMED is null.  */
3405
 
3406
static int
3407
build_type_name_from_array_name (tree aname, tree *trimmed)
3408
{
3409
  const char *name = IDENTIFIER_POINTER (aname);
3410
  int len = IDENTIFIER_LENGTH (aname);
3411
  int array_dims;
3412
 
3413
  STRING_STRIP_BRACKETS (name, len, array_dims);
3414
 
3415
  if (array_dims && trimmed)
3416
    *trimmed = get_identifier_with_length (name, len);
3417
 
3418
  return array_dims;
3419
}
3420
 
3421
static tree
3422
build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3423
{
3424
  int more_dims = 0;
3425
 
3426
  /* Eventually get more dims */
3427
  more_dims = build_type_name_from_array_name (name, &name);
3428
 
3429
  /* If we have, then craft a new type for this variable */
3430
  if (more_dims)
3431
    {
3432
      tree save = type;
3433
 
3434
      /* If we have a pointer, use its type */
3435
      if (TREE_CODE (type) == POINTER_TYPE)
3436
        type = TREE_TYPE (type);
3437
 
3438
      /* Building the first dimension of a primitive type uses this
3439
         function */
3440
      if (JPRIMITIVE_TYPE_P (type))
3441
        {
3442
          type = build_java_array_type (type, -1);
3443
          more_dims--;
3444
        }
3445
      /* Otherwise, if we have a WFL for this type, use it (the type
3446
         is already an array on an unresolved type, and we just keep
3447
         on adding dimensions) */
3448
      else if (type_wfl)
3449
        {
3450
          type = type_wfl;
3451
          more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3452
                                                        NULL);
3453
        }
3454
 
3455
      /* Add all the dimensions */
3456
      while (more_dims--)
3457
        type = build_unresolved_array_type (type);
3458
 
3459
      /* The type may have been incomplete in the first place */
3460
      if (type_wfl)
3461
        type = obtain_incomplete_type (type);
3462
    }
3463
 
3464
  if (ret_name)
3465
    *ret_name = name;
3466
  return type;
3467
}
3468
 
3469
/* Build something that the type identifier resolver will identify as
3470
   being an array to an unresolved type. TYPE_WFL is a WFL on a
3471
   identifier. */
3472
 
3473
static tree
3474
build_unresolved_array_type (tree type_or_wfl)
3475
{
3476
  const char *ptr;
3477
  tree wfl;
3478
 
3479
  /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3480
     just create a array type */
3481
  if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3482
    return build_java_array_type (type_or_wfl, -1);
3483
 
3484
  obstack_grow (&temporary_obstack,
3485
                 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3486
                 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3487
  obstack_grow0 (&temporary_obstack, "[]", 2);
3488
  ptr = obstack_finish (&temporary_obstack);
3489
#ifdef USE_MAPPED_LOCATION
3490
  wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3491
#else
3492
  wfl = build_expr_wfl (get_identifier (ptr),
3493
                        EXPR_WFL_FILENAME (type_or_wfl),
3494
                        EXPR_WFL_LINENO (type_or_wfl),
3495
                        EXPR_WFL_COLNO (type_or_wfl));
3496
#endif
3497
  /* Re-install the existing qualifications so that the type can be
3498
     resolved properly. */
3499
  EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3500
  return wfl;
3501
}
3502
 
3503
static void
3504
parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3505
{
3506
  if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3507
    parse_error_context (wfl, "Interface %qs repeated",
3508
                         IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3509
}
3510
 
3511
/* Bulk of common class/interface checks. Return 1 if an error was
3512
   encountered. TAG is 0 for a class, 1 for an interface.  */
3513
 
3514
static int
3515
check_class_interface_creation (int is_interface, int flags, tree raw_name,
3516
                                tree qualified_name, tree decl, tree cl)
3517
{
3518
  tree node;
3519
  int sca = 0;                  /* Static class allowed */
3520
  int icaf = 0;                 /* Inner class allowed flags */
3521
  int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3522
 
3523
  if (!quiet_flag)
3524
    fprintf (stderr, " %s%s %s",
3525
             (CPC_INNER_P () ? "inner" : ""),
3526
             (is_interface ? "interface" : "class"),
3527
             IDENTIFIER_POINTER (qualified_name));
3528
 
3529
  /* Scope of an interface/class type name:
3530
       - Can't be imported by a single type import
3531
       - Can't already exists in the package */
3532
  if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3533
      && (node = find_name_in_single_imports (raw_name))
3534
      && !CPC_INNER_P ())
3535
    {
3536
      parse_error_context
3537
        (cl, "%s name %qs clashes with imported type %qs",
3538
         (is_interface ? "Interface" : "Class"),
3539
         IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3540
      return 1;
3541
    }
3542
  if (decl && CLASS_COMPLETE_P (decl))
3543
    {
3544
      classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3545
                                   qualified_name, decl, cl);
3546
      return 1;
3547
    }
3548
 
3549
  if (check_inner_class_redefinition (raw_name, cl))
3550
    return 1;
3551
 
3552
  /* If public, file name should match class/interface name, except
3553
     when dealing with an inner class */
3554
  if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3555
    {
3556
      const char *fname = input_filename;
3557
      const char *f;
3558
 
3559
      for (f = fname + strlen (fname);
3560
           f != fname && ! IS_DIR_SEPARATOR (*f);
3561
           f--)
3562
        ;
3563
      if (IS_DIR_SEPARATOR (*f))
3564
        f++;
3565
      if (strncmp (IDENTIFIER_POINTER (raw_name),
3566
                   f , IDENTIFIER_LENGTH (raw_name)) ||
3567
          f [IDENTIFIER_LENGTH (raw_name)] != '.')
3568
        parse_error_context
3569
          (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3570
                             (is_interface ? "interface" : "class"),
3571
                             IDENTIFIER_POINTER (qualified_name),
3572
                             IDENTIFIER_POINTER (raw_name));
3573
    }
3574
 
3575
  /* Static classes can be declared only in top level classes. Note:
3576
     once static, a inner class is a top level class. */
3577
  if (flags & ACC_STATIC)
3578
    {
3579
      /* Catch the specific error of declaring an class inner class
3580
         with no toplevel enclosing class. Prevent check_modifiers from
3581
         complaining a second time */
3582
      if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3583
        {
3584
          parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3585
                               IDENTIFIER_POINTER (qualified_name));
3586
          sca = ACC_STATIC;
3587
        }
3588
      /* Else, in the context of a top-level class declaration, let
3589
         `check_modifiers' do its job, otherwise, give it a go */
3590
      else
3591
        sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3592
    }
3593
 
3594
  /* Inner classes can be declared private or protected
3595
     within their enclosing classes. */
3596
  if (CPC_INNER_P ())
3597
    {
3598
      /* A class which is local to a block can't be public, private,
3599
         protected or static. But it is created final, so allow this
3600
         one. */
3601
      if (current_function_decl)
3602
        icaf = sca = uaaf = ACC_FINAL;
3603
      else
3604
        {
3605
          check_modifiers_consistency (flags);
3606
          icaf = ACC_PROTECTED;
3607
          if (! CLASS_INTERFACE (GET_CPC ()))
3608
            icaf |= ACC_PRIVATE;
3609
        }
3610
    }
3611
 
3612
  if (is_interface)
3613
    {
3614
      if (CPC_INNER_P ())
3615
        uaaf = INTERFACE_INNER_MODIFIERS;
3616
      else
3617
        uaaf = INTERFACE_MODIFIERS;
3618
 
3619
      check_modifiers ("Illegal modifier %qs for interface declaration",
3620
                       flags, uaaf);
3621
    }
3622
  else
3623
    check_modifiers ((current_function_decl ?
3624
                      "Illegal modifier %qs for local class declaration" :
3625
                      "Illegal modifier %qs for class declaration"),
3626
                     flags, uaaf|sca|icaf);
3627
  return 0;
3628
}
3629
 
3630
/* Construct a nested class name.  If the final component starts with
3631
   a digit, return true.  Otherwise return false.  */
3632
static int
3633
make_nested_class_name (tree cpc_list)
3634
{
3635
  tree name;
3636
 
3637
  if (!cpc_list)
3638
    return 0;
3639
 
3640
  make_nested_class_name (TREE_CHAIN (cpc_list));
3641
 
3642
  /* Pick the qualified name when dealing with the first upmost
3643
     enclosing class */
3644
  name = (TREE_CHAIN (cpc_list)
3645
          ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3646
  obstack_grow (&temporary_obstack,
3647
                IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3648
  obstack_1grow (&temporary_obstack, '$');
3649
 
3650
  return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3651
}
3652
 
3653
/* Can't redefine a class already defined in an earlier scope. */
3654
 
3655
static int
3656
check_inner_class_redefinition (tree raw_name, tree cl)
3657
{
3658
  tree scope_list;
3659
 
3660
  for (scope_list = GET_CPC_LIST (); scope_list;
3661
       scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3662
    if (raw_name == GET_CPC_UN_NODE (scope_list))
3663
      {
3664
        parse_error_context
3665
          (cl, "The class name %qs is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3666
           IDENTIFIER_POINTER (raw_name));
3667
        return 1;
3668
      }
3669
  return 0;
3670
}
3671
 
3672
/* Tries to find a decl for CLASS_TYPE within ENCLOSING.  May return an
3673
   invisible/non-accessible matching decl when an accessible one could not be
3674
   found, in order to give a better error message when accessibility is
3675
   checked later.  */
3676
 
3677
static tree
3678
resolve_inner_class (tree context, tree cl, tree enclosing, tree class_type)
3679
{
3680
  tree local_super = NULL_TREE;
3681
  tree candidate = NULL_TREE;
3682
 
3683
  /* This hash table is used to register the classes we're going
3684
     through when searching the current class as an inner class, in
3685
     order to detect circular references.  */
3686
  htab_t circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
3687
                                         NULL);
3688
 
3689
  while (enclosing)
3690
    {
3691
      tree decl;
3692
 
3693
      *htab_find_slot (circularity_hash, enclosing, INSERT) = enclosing;
3694
 
3695
      if ((decl = find_as_inner_class (enclosing, class_type, cl)))
3696
        {
3697
          if (inner_class_accessible (decl, context))
3698
            {
3699
              candidate = decl;
3700
              break;
3701
            }
3702
          else
3703
            if (candidate == NULL_TREE)
3704
              candidate = decl;
3705
        }
3706
 
3707
      /* Now go to the upper classes, bail out if necessary.  We will
3708
         analyze the returned SUPER and act accordingly (see
3709
         do_resolve_class).  */
3710
      if (JPRIMITIVE_TYPE_P (TREE_TYPE (enclosing))
3711
          || TREE_TYPE (enclosing) == void_type_node)
3712
        {
3713
          parse_error_context (cl, "Qualifier must be a reference");
3714
          enclosing = NULL_TREE;
3715
          break;
3716
        }
3717
      local_super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
3718
      if (!local_super || local_super == object_type_node)
3719
        break;
3720
 
3721
      if (TREE_CODE (local_super) == POINTER_TYPE)
3722
        local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3723
      else
3724
        local_super = TYPE_NAME (local_super);
3725
 
3726
      /* We may not have checked for circular inheritance yet, so do so
3727
         here to prevent an infinite loop. */
3728
      if (htab_find (circularity_hash, local_super) != NULL)
3729
        {
3730
          if (!cl)
3731
            cl = lookup_cl (enclosing);
3732
 
3733
          parse_error_context
3734
            (cl, "Cyclic inheritance involving %s",
3735
             IDENTIFIER_POINTER (DECL_NAME (enclosing)));
3736
          enclosing = NULL_TREE;
3737
        }
3738
      else
3739
        enclosing = local_super;
3740
    }
3741
 
3742
  htab_delete (circularity_hash);
3743
 
3744
  /* We failed, but we might have found a matching class that wasn't
3745
     accessible.  Return that to get a better error message.  */
3746
  return candidate;
3747
}
3748
 
3749
/* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3750
   qualified. */
3751
 
3752
static tree
3753
find_as_inner_class (tree enclosing, tree name, tree cl)
3754
{
3755
  tree qual, to_return;
3756
  if (!enclosing)
3757
    return NULL_TREE;
3758
 
3759
  name = TYPE_NAME (name);
3760
 
3761
  /* First search: within the scope of `enclosing', search for name */
3762
  if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3763
    qual = EXPR_WFL_QUALIFICATION (cl);
3764
  else if (cl)
3765
    qual = build_tree_list (cl, NULL_TREE);
3766
  else
3767
    qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3768
 
3769
  if ((to_return = find_as_inner_class_do (qual, enclosing)))
3770
    return to_return;
3771
 
3772
  /* We're dealing with a qualified name. Try to resolve thing until
3773
     we get something that is an enclosing class. */
3774
  if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3775
    {
3776
      tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3777
 
3778
      for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3779
           qual = TREE_CHAIN (qual))
3780
        {
3781
          acc = merge_qualified_name (acc,
3782
                                      EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3783
          BUILD_PTR_FROM_NAME (ptr, acc);
3784
          decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3785
        }
3786
 
3787
      /* A NULL qual and a decl means that the search ended
3788
         successfully?!? We have to do something then. FIXME */
3789
 
3790
      if (decl)
3791
        enclosing = decl;
3792
      else
3793
        qual = EXPR_WFL_QUALIFICATION (cl);
3794
    }
3795
  /* Otherwise, create a qual for the other part of the resolution. */
3796
  else
3797
    qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3798
 
3799
  return find_as_inner_class_do (qual, enclosing);
3800
}
3801
 
3802
/* We go inside the list of sub classes and try to find a way
3803
   through. */
3804
 
3805
static tree
3806
find_as_inner_class_do (tree qual, tree enclosing)
3807
{
3808
  if (!qual)
3809
    return NULL_TREE;
3810
 
3811
  for (; qual && enclosing; qual = TREE_CHAIN (qual))
3812
    {
3813
      tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3814
      tree next_enclosing = NULL_TREE;
3815
      tree inner_list;
3816
 
3817
      for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3818
           inner_list; inner_list = TREE_CHAIN (inner_list))
3819
        {
3820
          if (TREE_VALUE (inner_list) == name_to_match)
3821
            {
3822
              next_enclosing = TREE_PURPOSE (inner_list);
3823
              break;
3824
            }
3825
        }
3826
      enclosing = next_enclosing;
3827
    }
3828
 
3829
  return (!qual && enclosing ? enclosing : NULL_TREE);
3830
}
3831
 
3832
static void
3833
link_nested_class_to_enclosing (void)
3834
{
3835
  if (GET_ENCLOSING_CPC ())
3836
    {
3837
      tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3838
      DECL_INNER_CLASS_LIST (enclosing) =
3839
        tree_cons (GET_CPC (), GET_CPC_UN (),
3840
                   DECL_INNER_CLASS_LIST (enclosing));
3841
    }
3842
}
3843
 
3844
static tree
3845
maybe_make_nested_class_name (tree name)
3846
{
3847
  tree id = NULL_TREE;
3848
 
3849
  if (CPC_INNER_P ())
3850
    {
3851
      /* If we're in a function, we must append a number to create the
3852
         nested class name.  However, we don't do this if the class we
3853
         are constructing is anonymous, because in that case we'll
3854
         already have a number as the class name.  */
3855
      if (! make_nested_class_name (GET_CPC_LIST ())
3856
          && current_function_decl != NULL_TREE
3857
          && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3858
        {
3859
          char buf[10];
3860
          sprintf (buf, "%d", anonymous_class_counter);
3861
          ++anonymous_class_counter;
3862
          obstack_grow (&temporary_obstack, buf, strlen (buf));
3863
          obstack_1grow (&temporary_obstack, '$');
3864
        }
3865
      obstack_grow0 (&temporary_obstack,
3866
                     IDENTIFIER_POINTER (name),
3867
                     IDENTIFIER_LENGTH (name));
3868
      id = get_identifier (obstack_finish (&temporary_obstack));
3869
      if (ctxp->package)
3870
        QUALIFIED_P (id) = 1;
3871
    }
3872
  return id;
3873
}
3874
 
3875
/* If DECL is NULL, create and push a new DECL, record the current
3876
   line CL and do other maintenance things.  */
3877
 
3878
static tree
3879
maybe_create_class_interface_decl (tree decl, tree raw_name,
3880
                                   tree qualified_name, tree cl)
3881
{
3882
  if (!decl)
3883
    decl = push_class (make_class (), qualified_name);
3884
 
3885
  /* Take care of the file and line business */
3886
#ifdef USE_MAPPED_LOCATION
3887
  DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3888
#else
3889
  DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3890
  DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3891
#endif
3892
  CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3893
  CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3894
#ifdef USE_MAPPED_LOCATION
3895
  {
3896
    tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3897
    CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3898
      tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3899
  }
3900
#else
3901
  CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3902
    IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3903
#endif
3904
 
3905
  PUSH_CPC (decl, raw_name);
3906
  DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3907
 
3908
  /* Link the declaration to the already seen ones */
3909
  TREE_CHAIN (decl) = ctxp->class_list;
3910
  ctxp->class_list = decl;
3911
 
3912
  /* Create a new nodes in the global lists */
3913
  gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3914
  all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3915
 
3916
  /* Install a new dependency list element */
3917
  create_jdep_list (ctxp);
3918
 
3919
  /* We keep the compilation unit imports in the class so that
3920
     they can be used later to resolve type dependencies that
3921
     aren't necessary to solve now. */
3922
  TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
3923
  TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
3924
 
3925
  TYPE_PACKAGE (TREE_TYPE (decl)) = ctxp->package;
3926
 
3927
  SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3928
                          IDENTIFIER_POINTER (qualified_name)));
3929
  return decl;
3930
}
3931
 
3932
static void
3933
add_superinterfaces (tree decl, tree interface_list)
3934
{
3935
  tree node;
3936
  /* Superinterface(s): if present and defined, parser_check_super_interface ()
3937
     takes care of ensuring that:
3938
       - This is an accessible interface type,
3939
       - Circularity detection.
3940
   parser_add_interface is then called. If present but not defined,
3941
   the check operation is delayed until the super interface gets
3942
   defined.  */
3943
  for (node = interface_list; node; node = TREE_CHAIN (node))
3944
    {
3945
      tree current = TREE_PURPOSE (node);
3946
      tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3947
      if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3948
        {
3949
          if (!parser_check_super_interface (idecl, decl, current))
3950
            parser_add_interface (decl, idecl, current);
3951
        }
3952
      else
3953
        register_incomplete_type (JDEP_INTERFACE,
3954
                                  current, decl, NULL_TREE);
3955
    }
3956
}
3957
 
3958
/* Create an interface in pass1 and return its decl. Return the
3959
   interface's decl in pass 2.  */
3960
 
3961
static tree
3962
create_interface (int flags, tree id, tree super)
3963
{
3964
  tree raw_name = EXPR_WFL_NODE (id);
3965
  tree q_name = parser_qualified_classname (raw_name);
3966
  tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3967
 
3968
  /* Certain syntax errors are making SUPER be like ID. Avoid this
3969
     case. */
3970
  if (ctxp->class_err && id == super)
3971
    super = NULL;
3972
 
3973
  EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3974
 
3975
  /* Basic checks: scope, redefinition, modifiers */
3976
  if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3977
    {
3978
      PUSH_ERROR ();
3979
      return NULL_TREE;
3980
    }
3981
 
3982
  /* Suspend the current parsing context if we're parsing an inner
3983
     interface */
3984
  if (CPC_INNER_P ())
3985
    {
3986
      java_parser_context_suspend ();
3987
      /* Interface members are public. */
3988
      if (CLASS_INTERFACE (GET_CPC ()))
3989
        flags |= ACC_PUBLIC;
3990
    }
3991
 
3992
  /* Push a new context for (static) initialized upon declaration fields */
3993
  java_parser_context_push_initialized_field ();
3994
 
3995
  /* Interface modifiers check
3996
       - public/abstract allowed (already done at that point)
3997
       - abstract is obsolete (comes first, it's a warning, or should be)
3998
       - Can't use twice the same (checked in the modifier rule) */
3999
  if ((flags & ACC_ABSTRACT) && flag_redundant)
4000
    parse_warning_context
4001
      (MODIFIER_WFL (ABSTRACT_TK),
4002
       "Redundant use of % modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
4003
 
4004
  /* Create a new decl if DECL is NULL, otherwise fix it */
4005
  decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
4006
 
4007
  /* Interfaces are always abstract. */
4008
  flags |= ACC_ABSTRACT;
4009
 
4010
  /* Inner interfaces are always static.  */
4011
  if (INNER_CLASS_DECL_P (decl))
4012
    flags |= ACC_STATIC;
4013
 
4014
  /* Set super info and mark the class a complete */
4015
  set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
4016
                  object_type_node, ctxp->interface_number);
4017
  ctxp->interface_number = 0;
4018
  CLASS_COMPLETE_P (decl) = 1;
4019
  add_superinterfaces (decl, super);
4020
 
4021
  /* Eventually sets the @deprecated tag flag */
4022
  CHECK_DEPRECATED (decl);
4023
 
4024
  return decl;
4025
}
4026
 
4027
/* Patch anonymous class CLASS, by either extending or implementing
4028
   DEP.  */
4029
 
4030
static void
4031
patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4032
{
4033
  tree class = TREE_TYPE (class_decl);
4034
  tree type =  TREE_TYPE (type_decl);
4035
  tree binfo = TYPE_BINFO (class);
4036
 
4037
  /* If it's an interface, implement it */
4038
  if (CLASS_INTERFACE (type_decl))
4039
    {
4040
      if (parser_check_super_interface (type_decl, class_decl, wfl))
4041
        return;
4042
 
4043
      if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4044
        {
4045
           /* Extend the binfo - by reallocating and copying it. */
4046
          tree new_binfo;
4047
          tree base_binfo;
4048
          int i;
4049
 
4050
          new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4051
          for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4052
            BINFO_BASE_APPEND (new_binfo, base_binfo);
4053
          CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4054
          BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4055
          TYPE_BINFO (class) = new_binfo;
4056
        }
4057
 
4058
      /* And add the interface */
4059
      parser_add_interface (class_decl, type_decl, wfl);
4060
    }
4061
  /* Otherwise, it's a type we want to extend */
4062
  else
4063
    {
4064
      if (parser_check_super (type_decl, class_decl, wfl))
4065
        return;
4066
      BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4067
    }
4068
}
4069
 
4070
/* Create an anonymous class which extends/implements TYPE_NAME, and return
4071
   its decl.  */
4072
 
4073
static tree
4074
create_anonymous_class (tree type_name)
4075
{
4076
  char buffer [80];
4077
  tree super = NULL_TREE, itf = NULL_TREE;
4078
  tree id, type_decl, class;
4079
 
4080
  /* The unqualified name of the anonymous class. It's just a number. */
4081
  sprintf (buffer, "%d", anonymous_class_counter++);
4082
  id = build_wfl_node (get_identifier (buffer));
4083
  EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4084
 
4085
  /* We know about the type to extend/implement. We go ahead */
4086
  if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4087
    {
4088
      /* Create a class which either implements on extends the designated
4089
         class. The class bears an inaccessible name. */
4090
      if (CLASS_INTERFACE (type_decl))
4091
        {
4092
          /* It's OK to modify it here. It's been already used and
4093
             shouldn't be reused */
4094
          ctxp->interface_number = 1;
4095
          /* Interfaces should presented as a list of WFLs */
4096
          itf = build_tree_list (type_name, NULL_TREE);
4097
        }
4098
      else
4099
        super = type_name;
4100
    }
4101
 
4102
  class = create_class (ACC_FINAL, id, super, itf);
4103
 
4104
  /* We didn't know anything about the stuff. We register a dependence. */
4105
  if (!type_decl)
4106
    register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4107
 
4108
  ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4109
  return class;
4110
}
4111
 
4112
/* Create a class in pass1 and return its decl. Return class
4113
   interface's decl in pass 2.  */
4114
 
4115
static tree
4116
create_class (int flags, tree id, tree super, tree interfaces)
4117
{
4118
  tree raw_name = EXPR_WFL_NODE (id);
4119
  tree class_id, decl;
4120
  tree super_decl_type;
4121
 
4122
  /* Certain syntax errors are making SUPER be like ID. Avoid this
4123
     case. */
4124
  if (ctxp->class_err && id == super)
4125
    super = NULL;
4126
 
4127
  class_id = parser_qualified_classname (raw_name);
4128
  decl = IDENTIFIER_CLASS_VALUE (class_id);
4129
  EXPR_WFL_NODE (id) = class_id;
4130
 
4131
  /* Basic check: scope, redefinition, modifiers */
4132
  if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4133
    {
4134
      PUSH_ERROR ();
4135
      return NULL_TREE;
4136
    }
4137
 
4138
  /* Suspend the current parsing context if we're parsing an inner
4139
     class or an anonymous class. */
4140
  if (CPC_INNER_P ())
4141
    {
4142
      java_parser_context_suspend ();
4143
      /* Interface members are public. */
4144
      if (CLASS_INTERFACE (GET_CPC ()))
4145
        flags |= ACC_PUBLIC;
4146
    }
4147
 
4148
  /* Push a new context for (static) initialized upon declaration fields */
4149
  java_parser_context_push_initialized_field ();
4150
 
4151
  /* Class modifier check:
4152
       - Allowed modifier (already done at that point)
4153
       - abstract AND final forbidden
4154
       - Public classes defined in the correct file */
4155
  if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4156
    parse_error_context
4157
      (id, "Class %qs can't be declared both abstract and final",
4158
       IDENTIFIER_POINTER (raw_name));
4159
 
4160
  /* Create a new decl if DECL is NULL, otherwise fix it */
4161
  decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4162
 
4163
  /* If SUPER exists, use it, otherwise use Object */
4164
  if (super)
4165
    {
4166
      /* java.lang.Object can't extend anything.  */
4167
      if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4168
        {
4169
          parse_error_context (id, "% can't extend anything");
4170
          return NULL_TREE;
4171
        }
4172
 
4173
      super_decl_type =
4174
        register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4175
    }
4176
  else if (TREE_TYPE (decl) != object_type_node)
4177
    super_decl_type = object_type_node;
4178
  /* We're defining java.lang.Object */
4179
  else
4180
    super_decl_type = NULL_TREE;
4181
 
4182
  /* A class nested in an interface is implicitly static. */
4183
  if (INNER_CLASS_DECL_P (decl)
4184
      && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4185
    {
4186
      flags |= ACC_STATIC;
4187
    }
4188
 
4189
  /* Set super info and mark the class as complete. */
4190
  set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4191
                  ctxp->interface_number);
4192
  ctxp->interface_number = 0;
4193
  CLASS_COMPLETE_P (decl) = 1;
4194
  add_superinterfaces (decl, interfaces);
4195
 
4196
  /* TYPE_VFIELD' is a compiler-generated field used to point to
4197
     virtual function tables.  In gcj, every class has a common base
4198
     virtual function table in java.lang.object.  */
4199
  TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4200
 
4201
  /* Add the private this$ field, Replicate final locals still in
4202
     scope as private final fields mangled like val$.
4203
     This does not occur for top level (static) inner classes. */
4204
  if (PURE_INNER_CLASS_DECL_P (decl))
4205
    add_inner_class_fields (decl, current_function_decl);
4206
 
4207
  /* Eventually sets the @deprecated tag flag */
4208
  CHECK_DEPRECATED (decl);
4209
 
4210
  /* Reset the anonymous class counter when declaring non inner classes */
4211
  if (!INNER_CLASS_DECL_P (decl))
4212
    anonymous_class_counter = 1;
4213
 
4214
  return decl;
4215
}
4216
 
4217
/* End a class declaration: register the statements used to create
4218
   finit$ and , pop the current class and resume the prior
4219
   parser context if necessary.  */
4220
 
4221
static void
4222
end_class_declaration (int resume)
4223
{
4224
  /* If an error occurred, context weren't pushed and won't need to be
4225
     popped by a resume. */
4226
  int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4227
 
4228
  if (GET_CPC () != error_mark_node)
4229
    dump_java_tree (TDI_class, GET_CPC ());
4230
 
4231
  java_parser_context_pop_initialized_field ();
4232
  POP_CPC ();
4233
  if (resume && no_error_occurred)
4234
    java_parser_context_resume ();
4235
 
4236
  /* We're ending a class declaration, this is a good time to reset
4237
     the interface cout. Note that might have been already done in
4238
     create_interface, but if at that time an inner class was being
4239
     dealt with, the interface count was reset in a context created
4240
     for the sake of handling inner classes declaration. */
4241
  ctxp->interface_number = 0;
4242
}
4243
 
4244
static void
4245
add_inner_class_fields (tree class_decl, tree fct_decl)
4246
{
4247
  tree block, marker, f;
4248
 
4249
  f = add_field (TREE_TYPE (class_decl),
4250
                 build_current_thisn (TREE_TYPE (class_decl)),
4251
                 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4252
                 ACC_PRIVATE);
4253
  FIELD_THISN (f) = 1;
4254
 
4255
  if (!fct_decl)
4256
    return;
4257
 
4258
  for (block = GET_CURRENT_BLOCK (fct_decl);
4259
       block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4260
    {
4261
      tree decl;
4262
      for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4263
        {
4264
          tree name, pname;
4265
          tree wfl, init, list;
4266
 
4267
          /* Avoid non final arguments. */
4268
          if (!LOCAL_FINAL_P (decl))
4269
            continue;
4270
 
4271
          MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4272
          MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4273
          wfl = build_wfl_node (name);
4274
          init = build_wfl_node (pname);
4275
          /* Build an initialization for the field: it will be
4276
             initialized by a parameter added to finit$, bearing a
4277
             mangled name of the field itself (param$.) The
4278
             parameter is provided to finit$ by the constructor
4279
             invoking it (hence the constructor will also feature a
4280
             hidden parameter, set to the value of the outer context
4281
             local at the time the inner class is created.)
4282
 
4283
             Note: we take into account all possible locals that can
4284
             be accessed by the inner class. It's actually not trivial
4285
             to minimize these aliases down to the ones really
4286
             used. One way to do that would be to expand all regular
4287
             methods first, then finit$ to get a picture of what's
4288
             used.  It works with the exception that we would have to
4289
             go back on all constructor invoked in regular methods to
4290
             have their invocation reworked (to include the right amount
4291
             of alias initializer parameters.)
4292
 
4293
             The only real way around, I think, is a first pass to
4294
             identify locals really used in the inner class. We leave
4295
             the flag FIELD_LOCAL_ALIAS_USED around for that future
4296
             use.
4297
 
4298
             On the other hand, it only affect local inner classes,
4299
             whose constructors (and finit$ call) will be featuring
4300
             unnecessary arguments. It's easy for a developer to keep
4301
             this number of parameter down by using the `final'
4302
             keyword only when necessary. For the time being, we can
4303
             issue a warning on unnecessary finals. FIXME */
4304
          init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4305
                                   wfl, init);
4306
 
4307
          /* Register the field. The TREE_LIST holding the part
4308
             initialized/initializer will be marked ARG_FINAL_P so
4309
             that the created field can be marked
4310
             FIELD_LOCAL_ALIAS. */
4311
          list = build_tree_list (wfl, init);
4312
          ARG_FINAL_P (list) = 1;
4313
          register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4314
        }
4315
    }
4316
 
4317
  if (!CPC_INITIALIZER_STMT (ctxp))
4318
    return;
4319
 
4320
  /* If we ever registered an alias field, insert and marker to
4321
     remember where the list ends. The second part of the list (the one
4322
     featuring initialized fields) so it can be later reversed to
4323
     enforce 8.5. The marker will be removed during that operation. */
4324
  marker = build_tree_list (NULL_TREE, NULL_TREE);
4325
  TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4326
  SET_CPC_INITIALIZER_STMT (ctxp, marker);
4327
}
4328
 
4329
/* Can't use lookup_field () since we don't want to load the class and
4330
   can't set the CLASS_LOADED_P flag */
4331
 
4332
static tree
4333
find_field (tree class, tree name)
4334
{
4335
  tree decl;
4336
  for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4337
    {
4338
      if (DECL_NAME (decl) == name)
4339
        return decl;
4340
    }
4341
  return NULL_TREE;
4342
}
4343
 
4344
/* Wrap around lookup_field that doesn't potentially upset the value
4345
   of CLASS */
4346
 
4347
static tree
4348
lookup_field_wrapper (tree class, tree name)
4349
{
4350
  tree type = class;
4351
  tree decl = NULL_TREE;
4352
  java_parser_context_save_global ();
4353
 
4354
  /* Last chance: if we're within the context of an inner class, we
4355
     might be trying to access a local variable defined in an outer
4356
     context. We try to look for it now. */
4357
  if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4358
    {
4359
      tree new_name;
4360
      MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4361
      decl = lookup_field (&type, new_name);
4362
      if (decl && decl != error_mark_node)
4363
        FIELD_LOCAL_ALIAS_USED (decl) = 1;
4364
    }
4365
  if (!decl || decl == error_mark_node)
4366
    {
4367
      type = class;
4368
      decl = lookup_field (&type, name);
4369
    }
4370
 
4371
  /* If the field still hasn't been found, try the next enclosing context. */
4372
  if (!decl && INNER_CLASS_TYPE_P (class))
4373
    {
4374
      tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4375
      decl = lookup_field_wrapper (outer_type, name);
4376
    }
4377
 
4378
  java_parser_context_restore_global ();
4379
  return decl == error_mark_node ? NULL : decl;
4380
}
4381
 
4382
/* Find duplicate field within the same class declarations and report
4383
   the error. Returns 1 if a duplicated field was found, 0
4384
   otherwise.  */
4385
 
4386
static int
4387
duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4388
{
4389
  /* This might be modified to work with method decl as well */
4390
  tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4391
  if (decl)
4392
    {
4393
      char *t1 = xstrdup (purify_type_name
4394
                         ((TREE_CODE (new_type) == POINTER_TYPE
4395
                           && TREE_TYPE (new_type) == NULL_TREE) ?
4396
                          IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4397
                          lang_printable_name (new_type, 1)));
4398
      /* The type may not have been completed by the time we report
4399
         the error */
4400
      char *t2 = xstrdup (purify_type_name
4401
                         ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4402
                           && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4403
                          IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4404
                          lang_printable_name (TREE_TYPE (decl), 1)));
4405
      parse_error_context
4406
        (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4407
         t1, IDENTIFIER_POINTER (new_field_name),
4408
         t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4409
         DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4410
      free (t1);
4411
      free (t2);
4412
      return 1;
4413
    }
4414
  return 0;
4415
}
4416
 
4417
/* Field registration routine. If TYPE doesn't exist, field
4418
   declarations are linked to the undefined TYPE dependency list, to
4419
   be later resolved in java_complete_class () */
4420
 
4421
static void
4422
register_fields (int flags, tree type, tree variable_list)
4423
{
4424
  tree current, saved_type;
4425
  tree class_type = NULL_TREE;
4426
  location_t saved_location = input_location;
4427
  int must_chain = 0;
4428
  tree wfl = NULL_TREE;
4429
 
4430
  if (GET_CPC ())
4431
    class_type = TREE_TYPE (GET_CPC ());
4432
 
4433
  if (!class_type || class_type == error_mark_node)
4434
    return;
4435
 
4436
  /* If we're adding fields to interfaces, those fields are public,
4437
     static, final */
4438
  if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4439
    {
4440
      OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4441
                                 flags, ACC_PUBLIC, "interface field(s)");
4442
      OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4443
                                 flags, ACC_STATIC, "interface field(s)");
4444
      OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4445
                                 flags, ACC_FINAL, "interface field(s)");
4446
      check_modifiers ("Illegal interface member modifier %qs", flags,
4447
                       INTERFACE_FIELD_MODIFIERS);
4448
      flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4449
    }
4450
 
4451
  /* Obtain a suitable type for resolution, if necessary */
4452
  SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4453
 
4454
  /* If TYPE is fully resolved and we don't have a reference, make one */
4455
  PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4456
 
4457
  for (current = variable_list, saved_type = type; current;
4458
       current = TREE_CHAIN (current), type = saved_type)
4459
    {
4460
      tree real_type;
4461
      tree field_decl;
4462
      tree cl = TREE_PURPOSE (current);
4463
      tree init = TREE_VALUE (current);
4464
      tree current_name = EXPR_WFL_NODE (cl);
4465
 
4466
      /* Can't declare non-final static fields in inner classes */
4467
      if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4468
          && !(flags & ACC_FINAL))
4469
        parse_error_context
4470
          (cl, "Field %qs can't be static in inner class %qs unless it is final",
4471
           IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4472
           lang_printable_name (class_type, 0));
4473
 
4474
      /* Process NAME, as it may specify extra dimension(s) for it */
4475
      type = build_array_from_name (type, wfl, current_name, ¤t_name);
4476
 
4477
      /* Type adjustment. We may have just readjusted TYPE because
4478
         the variable specified more dimensions. Make sure we have
4479
         a reference if we can and don't have one already. Also
4480
         change the name if we have an init. */
4481
      if (type != saved_type)
4482
        {
4483
          PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4484
          if (init)
4485
            EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4486
        }
4487
 
4488
      real_type = GET_REAL_TYPE (type);
4489
      /* Check for redeclarations */
4490
      if (duplicate_declaration_error_p (current_name, real_type, cl))
4491
        continue;
4492
 
4493
      /* Set input_line to the line the field was found and create a
4494
         declaration for it. Eventually sets the @deprecated tag flag. */
4495
#ifdef USE_MAPPED_LOCATION
4496
      input_location = EXPR_LOCATION (cl);
4497
#else
4498
      input_line = EXPR_WFL_LINENO (cl);
4499
#endif
4500
      field_decl = add_field (class_type, current_name, real_type, flags);
4501
      CHECK_DEPRECATED_NO_RESET (field_decl);
4502
 
4503
      /* If the field denotes a final instance variable, then we
4504
         allocate a LANG_DECL_SPECIFIC part to keep track of its
4505
         initialization. We also mark whether the field was
4506
         initialized upon its declaration. We don't do that if the
4507
         created field is an alias to a final local. */
4508
      if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4509
        {
4510
          MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4511
          DECL_FIELD_FINAL_WFL (field_decl) = cl;
4512
        }
4513
 
4514
      /* If the couple initializer/initialized is marked ARG_FINAL_P,
4515
         we mark the created field FIELD_LOCAL_ALIAS, so that we can
4516
         hide parameters to this inner class finit$ and
4517
         constructors. It also means that the field isn't final per
4518
         say. */
4519
      if (ARG_FINAL_P (current))
4520
        {
4521
          FIELD_LOCAL_ALIAS (field_decl) = 1;
4522
          FIELD_FINAL (field_decl) = 0;
4523
        }
4524
 
4525
      /* Check if we must chain. */
4526
      if (must_chain)
4527
        register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4528
 
4529
      /* If we have an initialization value tied to the field */
4530
      if (init)
4531
        {
4532
          /* The field is declared static */
4533
          if (flags & ACC_STATIC)
4534
            {
4535
              /* We include the field and its initialization part into
4536
                 a list used to generate . After  is
4537
                 walked, field initializations will be processed and
4538
                 fields initialized with known constants will be taken
4539
                 out of  and have their DECL_INITIAL set
4540
                 appropriately. */
4541
              TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4542
              SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4543
              if (TREE_OPERAND (init, 1)
4544
                  && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4545
                TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4546
            }
4547
          /* A non-static field declared with an immediate initialization is
4548
             to be initialized in , if any.  This field is remembered
4549
             to be processed at the time of the generation of . */
4550
          else
4551
            {
4552
              TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4553
              SET_CPC_INITIALIZER_STMT (ctxp, init);
4554
            }
4555
          MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4556
          DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4557
        }
4558
    }
4559
 
4560
  CLEAR_DEPRECATED;
4561
  input_location = saved_location;
4562
}
4563
 
4564
/* Generate finit$, using the list of initialized fields to populate
4565
   its body. finit$'s parameter(s) list is adjusted to include the
4566
   one(s) used to initialized the field(s) caching outer context
4567
   local(s).  */
4568
 
4569
static tree
4570
generate_finit (tree class_type)
4571
{
4572
  int count = 0;
4573
  tree list = TYPE_FINIT_STMT_LIST (class_type);
4574
  tree mdecl, current, parms;
4575
 
4576
  parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4577
                                                  class_type, NULL_TREE,
4578
                                                  &count);
4579
  CRAFTED_PARAM_LIST_FIXUP (parms);
4580
  mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4581
                                    finit_identifier_node, parms);
4582
  fix_method_argument_names (parms, mdecl);
4583
  layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4584
                       mdecl, NULL_TREE);
4585
  DECL_FUNCTION_NAP (mdecl) = count;
4586
  start_artificial_method_body (mdecl);
4587
 
4588
  for (current = list; current; current = TREE_CHAIN (current))
4589
    java_method_add_stmt (mdecl,
4590
                          build_debugable_stmt (EXPR_WFL_LINECOL (current),
4591
                                                current));
4592
  end_artificial_method_body (mdecl);
4593
  return mdecl;
4594
}
4595
 
4596
/* Generate a function to run the instance initialization code. The
4597
   private method is called `instinit$'. Unless we're dealing with an
4598
   anonymous class, we determine whether all ctors of CLASS_TYPE
4599
   declare a checked exception in their `throws' clause in order to
4600
   see whether it's necessary to encapsulate the instance initializer
4601
   statements in a try/catch/rethrow sequence.  */
4602
 
4603
static tree
4604
generate_instinit (tree class_type)
4605
{
4606
  tree current;
4607
  tree compound = NULL_TREE;
4608
  tree parms = tree_cons (this_identifier_node,
4609
                          build_pointer_type (class_type), end_params_node);
4610
  tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4611
                                         void_type_node,
4612
                                         instinit_identifier_node, parms);
4613
 
4614
  layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4615
                       mdecl, NULL_TREE);
4616
 
4617
  /* Gather all the statements in a compound */
4618
  for (current = TYPE_II_STMT_LIST (class_type);
4619
       current; current = TREE_CHAIN (current))
4620
    compound = add_stmt_to_compound (compound, NULL_TREE, current);
4621
 
4622
  /* We need to encapsulate COMPOUND by a try/catch statement to
4623
     rethrow exceptions that might occur in the instance initializer.
4624
     We do that only if all ctors of CLASS_TYPE are set to catch a
4625
     checked exception. This doesn't apply to anonymous classes (since
4626
     they don't have declared ctors.) */
4627
  if (!ANONYMOUS_CLASS_P (class_type) &&
4628
      ctors_unchecked_throws_clause_p (class_type))
4629
    {
4630
      compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4631
                                             build1 (THROW_EXPR, NULL_TREE,
4632
                                                     build_wfl_node (wpv_id)));
4633
      DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4634
                                                      exception_type_node);
4635
    }
4636
 
4637
  start_artificial_method_body (mdecl);
4638
  java_method_add_stmt (mdecl, compound);
4639
  end_artificial_method_body (mdecl);
4640
 
4641
  return mdecl;
4642
}
4643
 
4644
/* FIXME */
4645
static tree
4646
build_instinit_invocation (tree class_type)
4647
{
4648
  tree to_return = NULL_TREE;
4649
 
4650
  if (TYPE_II_STMT_LIST (class_type))
4651
    {
4652
      tree parm = build_tree_list (NULL_TREE,
4653
                                   build_wfl_node (this_identifier_node));
4654
      to_return =
4655
        build_method_invocation (build_wfl_node (instinit_identifier_node),
4656
                                 parm);
4657
    }
4658
  return to_return;
4659
}
4660
 
4661
/* Shared across method_declarator and method_header to remember the
4662
   patch stage that was reached during the declaration of the method.
4663
   A method DECL is built differently is there is no patch
4664
   (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4665
   pending on the currently defined method.  */
4666
 
4667
static int patch_stage;
4668
 
4669
/* Check the method declaration and add the method to its current
4670
   class.  If the argument list is known to contain incomplete types,
4671
   the method is partially added and the registration will be resume
4672
   once the method arguments resolved. If TYPE is NULL, we're dealing
4673
   with a constructor.  */
4674
 
4675
static tree
4676
method_header (int flags, tree type, tree mdecl, tree throws)
4677
{
4678
  tree type_wfl = NULL_TREE;
4679
  tree meth_name = NULL_TREE;
4680
  tree current, orig_arg, this_class = NULL;
4681
  tree id, meth;
4682
  location_t saved_location;
4683
  int constructor_ok = 0, must_chain;
4684
  int count;
4685
 
4686
  if (mdecl == error_mark_node)
4687
    return error_mark_node;
4688
  meth = TREE_VALUE (mdecl);
4689
  id = TREE_PURPOSE (mdecl);
4690
 
4691
  check_modifiers_consistency (flags);
4692
 
4693
  if (GET_CPC ())
4694
    this_class = TREE_TYPE (GET_CPC ());
4695
 
4696
  if (!this_class || this_class == error_mark_node)
4697
    return NULL_TREE;
4698
 
4699
  /* There are some forbidden modifiers for an abstract method and its
4700
     class must be abstract as well.  */
4701
  if (type && (flags & ACC_ABSTRACT))
4702
    {
4703
      ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4704
      ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4705
      ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4706
      ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4707
      ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4708
      ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4709
      if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4710
          && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4711
        parse_error_context
4712
          (id,
4713
           "Class %qs must be declared abstract to define abstract method %qs",
4714
           IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4715
           IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4716
    }
4717
 
4718
  /* A native method can't be strictfp.  */
4719
  if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4720
    parse_error_context (id, "native method %qs can't be strictfp",
4721
                         IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4722
  /* No such thing as a transient or volatile method.  */
4723
  if ((flags & ACC_TRANSIENT))
4724
    parse_error_context (id, "method %qs can't be transient",
4725
                         IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4726
  if ((flags & ACC_VOLATILE))
4727
    parse_error_context (id, "method %qs can't be volatile",
4728
                         IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4729
 
4730
  /* Things to be checked when declaring a constructor */
4731
  if (!type)
4732
    {
4733
      int ec = java_error_count;
4734
      /* 8.6: Constructor declarations: we might be trying to define a
4735
         method without specifying a return type. */
4736
      if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4737
        parse_error_context
4738
          (id, "Invalid method declaration, return type required");
4739
      /* 8.6.3: Constructor modifiers */
4740
      else
4741
        {
4742
          JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4743
          JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4744
          JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4745
          JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4746
          JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4747
          JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4748
        }
4749
      /* If we found error here, we don't consider it's OK to tread
4750
         the method definition as a constructor, for the rest of this
4751
         function */
4752
      if (ec == java_error_count)
4753
        constructor_ok = 1;
4754
    }
4755
 
4756
  /* Method declared within the scope of an interface are implicitly
4757
     abstract and public. Conflicts with other erroneously provided
4758
     modifiers are checked right after. */
4759
 
4760
  if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4761
    {
4762
      /* If FLAGS isn't set because of a modifier, turn the
4763
         corresponding modifier WFL to NULL so we issue a warning on
4764
         the obsolete use of the modifier */
4765
      if (!(flags & ACC_PUBLIC))
4766
        MODIFIER_WFL (PUBLIC_TK) = NULL;
4767
      if (!(flags & ACC_ABSTRACT))
4768
        MODIFIER_WFL (ABSTRACT_TK) = NULL;
4769
      flags |= ACC_PUBLIC;
4770
      flags |= ACC_ABSTRACT;
4771
    }
4772
 
4773
  /* Inner class can't declare static methods */
4774
  if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4775
    {
4776
      parse_error_context
4777
        (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4778
         IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4779
         lang_printable_name (this_class, 0));
4780
    }
4781
 
4782
  /* Modifiers context reset moved up, so abstract method declaration
4783
     modifiers can be later checked.  */
4784
 
4785
  /* Set constructor returned type to void and method name to ,
4786
     unless we found an error identifier the constructor (in which
4787
     case we retain the original name) */
4788
  if (!type)
4789
    {
4790
      type = void_type_node;
4791
      if (constructor_ok)
4792
        meth_name = init_identifier_node;
4793
    }
4794
  else
4795
    meth_name = EXPR_WFL_NODE (id);
4796
 
4797
  /* Do the returned type resolution and registration if necessary */
4798
  SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4799
 
4800
  if (meth_name)
4801
    type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4802
  EXPR_WFL_NODE (id) = meth_name;
4803
  PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4804
 
4805
  if (must_chain)
4806
    {
4807
      patch_stage = JDEP_METHOD_RETURN;
4808
      register_incomplete_type (patch_stage, type_wfl, id, type);
4809
      TREE_TYPE (meth) = GET_REAL_TYPE (type);
4810
    }
4811
  else
4812
    TREE_TYPE (meth) = type;
4813
 
4814
  saved_location = input_location;
4815
  /* When defining an abstract or interface method, the curly
4816
     bracket at level 1 doesn't exist because there is no function
4817
     body */
4818
#ifdef USE_MAPPED_LOCATION
4819
  input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4820
                    EXPR_LOCATION (id));
4821
#else
4822
  input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4823
                EXPR_WFL_LINENO (id));
4824
#endif
4825
 
4826
  /* Remember the original argument list */
4827
  orig_arg = TYPE_ARG_TYPES (meth);
4828
 
4829
  if (patch_stage)              /* includes ret type and/or all args */
4830
    {
4831
      jdep *jdep;
4832
      meth = add_method_1 (this_class, flags, meth_name, meth);
4833
      /* Patch for the return type */
4834
      if (patch_stage == JDEP_METHOD_RETURN)
4835
        {
4836
          jdep = CLASSD_LAST (ctxp->classd_list);
4837
          JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4838
        }
4839
      /* This is the stop JDEP. METH allows the function's signature
4840
         to be computed. */
4841
      register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4842
    }
4843
  else
4844
    meth = add_method (this_class, flags, meth_name,
4845
                       build_java_signature (meth));
4846
 
4847
  /* Remember final parameters */
4848
  MARK_FINAL_PARMS (meth, orig_arg);
4849
 
4850
  /* Fix the method argument list so we have the argument name
4851
     information */
4852
  fix_method_argument_names (orig_arg, meth);
4853
 
4854
  /* Register the parameter number and re-install the current line
4855
     number */
4856
  DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4857
  input_location = saved_location;
4858
 
4859
  /* Register exception specified by the `throws' keyword for
4860
     resolution and set the method decl appropriate field to the list.
4861
     Note: the grammar ensures that what we get here are class
4862
     types. */
4863
  if (throws)
4864
    {
4865
      throws = nreverse (throws);
4866
      for (current = throws; current; current = TREE_CHAIN (current))
4867
        {
4868
          register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4869
                                    NULL_TREE, NULL_TREE);
4870
          JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4871
            &TREE_VALUE (current);
4872
        }
4873
      DECL_FUNCTION_THROWS (meth) = throws;
4874
    }
4875
 
4876
  if (TREE_TYPE (GET_CPC ()) != object_type_node)
4877
    DECL_FUNCTION_WFL (meth) = id;
4878
 
4879
  /* Set the flag if we correctly processed a constructor */
4880
  if (constructor_ok)
4881
    {
4882
      DECL_CONSTRUCTOR_P (meth) = 1;
4883
      /* Compute and store the number of artificial parameters declared
4884
         for this constructor */
4885
      for (count = 0, current = TYPE_FIELDS (this_class); current;
4886
           current = TREE_CHAIN (current))
4887
        if (FIELD_LOCAL_ALIAS (current))
4888
          count++;
4889
      DECL_FUNCTION_NAP (meth) = count;
4890
    }
4891
 
4892
  /* Eventually set the @deprecated tag flag */
4893
  CHECK_DEPRECATED (meth);
4894
 
4895
  return meth;
4896
}
4897
 
4898
static void
4899
fix_method_argument_names (tree orig_arg, tree meth)
4900
{
4901
  tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4902
  if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4903
    {
4904
      TREE_PURPOSE (arg) = this_identifier_node;
4905
      arg = TREE_CHAIN (arg);
4906
    }
4907
  while (orig_arg != end_params_node)
4908
    {
4909
      TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4910
      orig_arg = TREE_CHAIN (orig_arg);
4911
      arg = TREE_CHAIN (arg);
4912
    }
4913
}
4914
 
4915
/* Complete the method declaration with METHOD_BODY.  */
4916
 
4917
static void
4918
finish_method_declaration (tree method_body)
4919
{
4920
  int flags;
4921
 
4922
  if (!current_function_decl)
4923
    return;
4924
 
4925
  flags = get_access_flags_from_decl (current_function_decl);
4926
 
4927
  /* 8.4.5 Method Body */
4928
  if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4929
    {
4930
      tree name = DECL_NAME (current_function_decl);
4931
      parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4932
                           "%s method %qs can't have a body defined",
4933
                           (METHOD_NATIVE (current_function_decl) ?
4934
                            "Native" : "Abstract"),
4935
                           IDENTIFIER_POINTER (name));
4936
      method_body = NULL_TREE;
4937
    }
4938
  else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4939
    {
4940
      tree name = DECL_NAME (current_function_decl);
4941
      parse_error_context
4942
        (DECL_FUNCTION_WFL (current_function_decl),
4943
         "Non native and non abstract method %qs must have a body defined",
4944
         IDENTIFIER_POINTER (name));
4945
      method_body = NULL_TREE;
4946
    }
4947
 
4948
  if (flag_emit_class_files && method_body
4949
      && TREE_CODE (method_body) == NOP_EXPR
4950
      && TREE_TYPE (current_function_decl)
4951
      && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4952
    method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4953
 
4954
  BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4955
  maybe_absorb_scoping_blocks ();
4956
  /* Exit function's body */
4957
  exit_block ();
4958
  /* Merge last line of the function with first line, directly in the
4959
     function decl. It will be used to emit correct debug info. */
4960
  DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4961
 
4962
  /* Since function's argument's list are shared, reset the
4963
     ARG_FINAL_P parameter that might have been set on some of this
4964
     function parameters. */
4965
  UNMARK_FINAL_PARMS (current_function_decl);
4966
 
4967
  /* So we don't have an irrelevant function declaration context for
4968
     the next static block we'll see. */
4969
  current_function_decl = NULL_TREE;
4970
}
4971
 
4972
/* Build a an error message for constructor circularity errors.  */
4973
 
4974
static char *
4975
constructor_circularity_msg (tree from, tree to)
4976
{
4977
  static char string [4096];
4978
  char *t = xstrdup (lang_printable_name (from, 2));
4979
  sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4980
  free (t);
4981
  return string;
4982
}
4983
 
4984
/* Verify a circular call to METH. Return 1 if an error is found, 0
4985
   otherwise.  */
4986
 
4987
static GTY(()) tree vcc_list;
4988
static int
4989
verify_constructor_circularity (tree meth, tree current)
4990
{
4991
  tree c;
4992
 
4993
  for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4994
    {
4995
      if (TREE_VALUE (c) == meth)
4996
        {
4997
          char *t;
4998
          if (vcc_list)
4999
            {
5000
              tree liste;
5001
              vcc_list = nreverse (vcc_list);
5002
              for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
5003
                {
5004
                  parse_error_context
5005
                    (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
5006
                     constructor_circularity_msg
5007
                      (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
5008
                  java_error_count--;
5009
                }
5010
            }
5011
          t = xstrdup (lang_printable_name (meth, 2));
5012
          parse_error_context (TREE_PURPOSE (c),
5013
                               "%s: recursive invocation of constructor %qs",
5014
                               constructor_circularity_msg (current, meth), t);
5015
          free (t);
5016
          vcc_list = NULL_TREE;
5017
          return 1;
5018
        }
5019
    }
5020
  for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5021
    {
5022
      vcc_list = tree_cons (c, current, vcc_list);
5023
      if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5024
        return 1;
5025
      vcc_list = TREE_CHAIN (vcc_list);
5026
    }
5027
  return 0;
5028
}
5029
 
5030
/* Check modifiers that can be declared but exclusively */
5031
 
5032
static void
5033
check_modifiers_consistency (int flags)
5034
{
5035
  int acc_count = 0;
5036
  tree cl = NULL_TREE;
5037
 
5038
  THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5039
  THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5040
  THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5041
  if (acc_count > 1)
5042
    parse_error_context
5043
      (cl, "Inconsistent member declaration.  At most one of %, %, or % may be specified");
5044
 
5045
  acc_count = 0;
5046
  cl = NULL_TREE;
5047
  THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5048
  THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5049
  if (acc_count > 1)
5050
    parse_error_context (cl,
5051
                         "Inconsistent member declaration.  At most one of % or % may be specified");
5052
}
5053
 
5054
/* Check the methode header METH for abstract specifics features */
5055
 
5056
static void
5057
check_abstract_method_header (tree meth)
5058
{
5059
  int flags = get_access_flags_from_decl (meth);
5060
 
5061
  OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5062
                              ACC_ABSTRACT, "abstract method",
5063
                              IDENTIFIER_POINTER (DECL_NAME (meth)));
5064
  OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5065
                              ACC_PUBLIC, "abstract method",
5066
                              IDENTIFIER_POINTER (DECL_NAME (meth)));
5067
 
5068
  check_modifiers ("Illegal modifier %qs for interface method",
5069
                  flags, INTERFACE_METHOD_MODIFIERS);
5070
}
5071
 
5072
/* Create a FUNCTION_TYPE node and start augmenting it with the
5073
   declared function arguments. Arguments type that can't be resolved
5074
   are left as they are, but the returned node is marked as containing
5075
   incomplete types.  */
5076
 
5077
static tree
5078
method_declarator (tree id, tree list)
5079
{
5080
  tree arg_types = NULL_TREE, current, node;
5081
  tree meth = make_node (FUNCTION_TYPE);
5082
  jdep *jdep;
5083
 
5084
  patch_stage = JDEP_NO_PATCH;
5085
 
5086
  if (GET_CPC () == error_mark_node)
5087
    return error_mark_node;
5088
 
5089
  /* If we're dealing with an inner class constructor, we hide the
5090
     this$ decl in the name field of its parameter declaration.  We
5091
     also might have to hide the outer context local alias
5092
     initializers. Not done when the class is a toplevel class. */
5093
  if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5094
      && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5095
    {
5096
      tree aliases_list, type, thisn;
5097
      /* First the aliases, linked to the regular parameters */
5098
      aliases_list =
5099
        build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5100
                                                TREE_TYPE (GET_CPC ()),
5101
                                                NULL_TREE, NULL);
5102
      list = chainon (nreverse (aliases_list), list);
5103
 
5104
      /* Then this$ */
5105
      type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5106
      thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5107
      list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5108
                        list);
5109
    }
5110
 
5111
  for (current = list; current; current = TREE_CHAIN (current))
5112
    {
5113
      int must_chain = 0;
5114
      tree wfl_name = TREE_PURPOSE (current);
5115
      tree type = TREE_VALUE (current);
5116
      tree name = EXPR_WFL_NODE (wfl_name);
5117
      tree already, arg_node;
5118
      tree type_wfl = NULL_TREE;
5119
      tree real_type;
5120
 
5121
      /* Obtain a suitable type for resolution, if necessary */
5122
      SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5123
 
5124
      /* Process NAME, as it may specify extra dimension(s) for it */
5125
      type = build_array_from_name (type, type_wfl, name, &name);
5126
      EXPR_WFL_NODE (wfl_name) = name;
5127
 
5128
      real_type = GET_REAL_TYPE (type);
5129
      if (TREE_CODE (real_type) == RECORD_TYPE)
5130
        {
5131
          real_type = promote_type (real_type);
5132
          if (TREE_CODE (type) == TREE_LIST)
5133
            TREE_PURPOSE (type) = real_type;
5134
        }
5135
 
5136
      /* Check redefinition */
5137
      for (already = arg_types; already; already = TREE_CHAIN (already))
5138
        if (TREE_PURPOSE (already) == name)
5139
          {
5140
            parse_error_context
5141
              (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5142
               IDENTIFIER_POINTER (name),
5143
               IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5144
            break;
5145
          }
5146
 
5147
      /* If we've an incomplete argument type, we know there is a location
5148
         to patch when the type get resolved, later.  */
5149
      jdep = NULL;
5150
      if (must_chain)
5151
        {
5152
          patch_stage = JDEP_METHOD;
5153
          type = register_incomplete_type (patch_stage,
5154
                                           type_wfl, wfl_name, type);
5155
          jdep = CLASSD_LAST (ctxp->classd_list);
5156
          JDEP_MISC (jdep) = id;
5157
        }
5158
 
5159
      /* The argument node: a name and a (possibly) incomplete type.  */
5160
      arg_node = build_tree_list (name, real_type);
5161
      /* Remember arguments declared final. */
5162
      ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5163
 
5164
      if (jdep)
5165
        JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5166
      TREE_CHAIN (arg_node) = arg_types;
5167
      arg_types = arg_node;
5168
    }
5169
  TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5170
  node = build_tree_list (id, meth);
5171
  return node;
5172
}
5173
 
5174
static int
5175
unresolved_type_p (tree wfl, tree *returned)
5176
{
5177
  if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5178
    {
5179
      if (returned)
5180
        {
5181
          tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5182
          if (decl && current_class && (decl == TYPE_NAME (current_class)))
5183
            *returned = TREE_TYPE (decl);
5184
          else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5185
            *returned = TREE_TYPE (GET_CPC ());
5186
          else
5187
            *returned = NULL_TREE;
5188
        }
5189
      return 1;
5190
    }
5191
  if (returned)
5192
    *returned = wfl;
5193
  return 0;
5194
}
5195
 
5196
/* From NAME, build a qualified identifier node using the
5197
   qualification from the current package definition. */
5198
 
5199
static tree
5200
parser_qualified_classname (tree name)
5201
{
5202
  tree nested_class_name;
5203
 
5204
  if ((nested_class_name = maybe_make_nested_class_name (name)))
5205
    return nested_class_name;
5206
 
5207
  if (ctxp->package)
5208
    return merge_qualified_name (ctxp->package, name);
5209
  else
5210
    return name;
5211
}
5212
 
5213
/* Called once the type a interface extends is resolved. Returns 0 if
5214
   everything is OK.  */
5215
 
5216
static int
5217
parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5218
{
5219
  tree super_type = TREE_TYPE (super_decl);
5220
 
5221
  /* Has to be an interface */
5222
  if (!CLASS_INTERFACE (super_decl))
5223
    {
5224
      parse_error_context
5225
        (this_wfl, "%s %qs can't implement/extend %s %qs",
5226
         (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5227
          "Interface" : "Class"),
5228
         IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5229
         (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5230
         IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5231
      return 1;
5232
    }
5233
 
5234
  /* Check top-level interface access. Inner classes are subject to member
5235
     access rules (6.6.1). */
5236
  if (! INNER_CLASS_P (super_type)
5237
      && check_pkg_class_access (DECL_NAME (super_decl),
5238
                                 NULL_TREE, true, this_decl))
5239
    return 1;
5240
 
5241
  SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5242
                          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5243
                          IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5244
  return 0;
5245
}
5246
 
5247
/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5248
 
5249
 
5250
static int
5251
parser_check_super (tree super_decl, tree this_decl, tree wfl)
5252
{
5253
  tree super_type = TREE_TYPE (super_decl);
5254
 
5255
  /* SUPER should be a CLASS (neither an array nor an interface) */
5256
  if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5257
    {
5258
      parse_error_context
5259
        (wfl, "Class %qs can't subclass %s %qs",
5260
         IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5261
         (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5262
         IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5263
      return 1;
5264
    }
5265
 
5266
  if (CLASS_FINAL (TYPE_NAME (super_type)))
5267
    {
5268
      parse_error_context (wfl, "Can't subclass final classes: %s",
5269
                           IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5270
      return 1;
5271
    }
5272
 
5273
  /* Check top-level class scope. Inner classes are subject to member access
5274
     rules (6.6.1). */
5275
  if (! INNER_CLASS_P (super_type)
5276
      && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5277
    return 1;
5278
 
5279
  SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5280
                          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5281
                          IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5282
  return 0;
5283
}
5284
 
5285
/* Create a new dependency list and link it (in a LIFO manner) to the
5286
   CTXP list of type dependency list.  */
5287
 
5288
static void
5289
create_jdep_list (struct parser_ctxt *ctxp)
5290
{
5291
  jdeplist *new = xmalloc (sizeof (jdeplist));
5292
  new->first = new->last = NULL;
5293
  new->next = ctxp->classd_list;
5294
  ctxp->classd_list = new;
5295
}
5296
 
5297
static jdeplist *
5298
reverse_jdep_list (struct parser_ctxt *ctxp)
5299
{
5300
  jdeplist *prev = NULL, *current, *next;
5301
  for (current = ctxp->classd_list; current; current = next)
5302
    {
5303
      next = current->next;
5304
      current->next = prev;
5305
      prev = current;
5306
    }
5307
  return prev;
5308
}
5309
 
5310
/* Create a fake pointer based on the ID stored in
5311
   TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5312
   registered again. */
5313
 
5314
static tree
5315
obtain_incomplete_type (tree type_name)
5316
{
5317
  tree ptr = NULL_TREE, name;
5318
 
5319
  if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5320
    name = EXPR_WFL_NODE (type_name);
5321
  else if (INCOMPLETE_TYPE_P (type_name))
5322
    name = TYPE_NAME (type_name);
5323
  else
5324
    abort ();
5325
 
5326
  /* Workaround from build_pointer_type for incomplete types.  */
5327
  BUILD_PTR_FROM_NAME (ptr, name);
5328
  TYPE_MODE (ptr) = ptr_mode;
5329
  layout_type (ptr);
5330
 
5331
  return ptr;
5332
}
5333
 
5334
/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5335
   non NULL instead of computing a new fake type based on WFL. The new
5336
   dependency is inserted in the current type dependency list, in FIFO
5337
   manner.  */
5338
 
5339
static tree
5340
register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5341
{
5342
  jdep *new = xmalloc (sizeof (jdep));
5343
 
5344
  if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5345
    ptr = obtain_incomplete_type (wfl);
5346
 
5347
  JDEP_KIND (new) = kind;
5348
  JDEP_DECL (new) = decl;
5349
  JDEP_TO_RESOLVE (new) = ptr;
5350
  JDEP_WFL (new) = wfl;
5351
  JDEP_CHAIN (new) = NULL;
5352
  JDEP_MISC (new) = NULL_TREE;
5353
  /* For some dependencies, set the enclosing class of the current
5354
     class to be the enclosing context */
5355
  if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5356
      && GET_ENCLOSING_CPC ())
5357
    JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5358
  else
5359
    JDEP_ENCLOSING (new) = GET_CPC ();
5360
  JDEP_GET_PATCH (new) = (tree *)NULL;
5361
 
5362
  JDEP_INSERT (ctxp->classd_list, new);
5363
 
5364
  return ptr;
5365
}
5366
 
5367
/* This checks for circular references with innerclasses. We start
5368
   from SOURCE and should never reach TARGET. Extended/implemented
5369
   types in SOURCE have their enclosing context checked not to reach
5370
   TARGET. When the last enclosing context of SOURCE is reached, its
5371
   extended/implemented types are also checked not to reach TARGET.
5372
   In case of error, WFL of the offending type is returned; NULL_TREE
5373
   otherwise.  */
5374
 
5375
static tree
5376
check_inner_circular_reference (tree source, tree target)
5377
{
5378
  tree base_binfo;
5379
  tree ctx, cl;
5380
  int i;
5381
 
5382
  for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5383
    {
5384
      tree su;
5385
 
5386
      /* We can end up with a NULL_TREE or an incomplete type here if
5387
         we encountered previous type resolution errors. It's safe to
5388
         simply ignore these cases.  */
5389
      su = BINFO_TYPE (base_binfo);
5390
      if (INCOMPLETE_TYPE_P (su))
5391
        continue;
5392
 
5393
      if (inherits_from_p (su, target))
5394
        return lookup_cl (TYPE_NAME (su));
5395
 
5396
      for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5397
        {
5398
          /* An enclosing context shouldn't be TARGET */
5399
          if (ctx == TYPE_NAME (target))
5400
            return lookup_cl (TYPE_NAME (su));
5401
 
5402
          /* When we reach the enclosing last context, start a check
5403
             on it, with the same target */
5404
          if (! DECL_CONTEXT (ctx) &&
5405
              (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5406
            return cl;
5407
        }
5408
    }
5409
  return NULL_TREE;
5410
}
5411
 
5412
/* Explore TYPE's `extends' clause member(s) and return the WFL of the
5413
   offending type if a circularity is detected. NULL_TREE is returned
5414
   otherwise. TYPE can be an interface or a class.   */
5415
 
5416
static tree
5417
check_circular_reference (tree type)
5418
{
5419
  tree base_binfo;
5420
  int i;
5421
 
5422
  if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5423
    return NULL_TREE;
5424
 
5425
  if (! CLASS_INTERFACE (TYPE_NAME (type)))
5426
    {
5427
      if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5428
        return lookup_cl (TYPE_NAME (type));
5429
      return NULL_TREE;
5430
    }
5431
 
5432
  for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5433
    {
5434
      if (BINFO_TYPE (base_binfo) != object_type_node
5435
          && interface_of_p (type, BINFO_TYPE (base_binfo)))
5436
        return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5437
    }
5438
  return NULL_TREE;
5439
}
5440
 
5441
void
5442
java_check_circular_reference (void)
5443
{
5444
  tree current;
5445
  for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5446
    {
5447
      tree type = TREE_TYPE (current);
5448
      tree cl;
5449
 
5450
      cl = check_circular_reference (type);
5451
      if (! cl)
5452
        cl = check_inner_circular_reference (type, type);
5453
      if (cl)
5454
        parse_error_context (cl, "Cyclic class inheritance%s",
5455
                             (cyclic_inheritance_report ?
5456
                              cyclic_inheritance_report : ""));
5457
    }
5458
}
5459
 
5460
/* Augment the parameter list PARM with parameters crafted to
5461
   initialize outer context locals aliases. Through ARTIFICIAL, a
5462
   count is kept of the number of crafted parameters. MODE governs
5463
   what eventually gets created: something suitable for a function
5464
   creation or a function invocation, either the constructor or
5465
   finit$.  */
5466
 
5467
static tree
5468
build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5469
                                        int *artificial)
5470
{
5471
  tree field;
5472
  tree additional_parms = NULL_TREE;
5473
 
5474
  for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5475
    if (FIELD_LOCAL_ALIAS (field))
5476
      {
5477
        const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5478
        tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5479
        tree mangled_id;
5480
 
5481
        switch (mode)
5482
          {
5483
          case AIPL_FUNCTION_DECLARATION:
5484
            MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5485
                                                         &buffer [4]);
5486
            purpose = build_wfl_node (mangled_id);
5487
            if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5488
              value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5489
            else
5490
              value = TREE_TYPE (field);
5491
            break;
5492
 
5493
          case AIPL_FUNCTION_CREATION:
5494
            MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5495
                                                         &buffer [4]);
5496
            value = TREE_TYPE (field);
5497
            break;
5498
 
5499
          case AIPL_FUNCTION_FINIT_INVOCATION:
5500
            MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5501
                                                         &buffer [4]);
5502
            /* Now, this is wrong. purpose should always be the NAME
5503
               of something and value its matching value (decl, type,
5504
               etc...) FIXME -- but there is a lot to fix. */
5505
 
5506
            /* When invoked for this kind of operation, we already
5507
               know whether a field is used or not. */
5508
            purpose = TREE_TYPE (field);
5509
            value = build_wfl_node (mangled_id);
5510
            break;
5511
 
5512
          case AIPL_FUNCTION_CTOR_INVOCATION:
5513
            /* There are two case: the constructor invocation happens
5514
               outside the local inner, in which case, locales from the outer
5515
               context are directly used.
5516
 
5517
               Otherwise, we fold to using the alias directly. */
5518
            if (class_type == current_class)
5519
              value = field;
5520
            else
5521
              {
5522
                name = get_identifier (&buffer[4]);
5523
                value = IDENTIFIER_LOCAL_VALUE (name);
5524
              }
5525
            break;
5526
          }
5527
        additional_parms = tree_cons (purpose, value, additional_parms);
5528
        if (artificial)
5529
          *artificial +=1;
5530
      }
5531
  if (additional_parms)
5532
    {
5533
      if (ANONYMOUS_CLASS_P (class_type)
5534
          && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5535
        additional_parms = nreverse (additional_parms);
5536
      parm = chainon (additional_parms, parm);
5537
    }
5538
 
5539
   return parm;
5540
}
5541
 
5542
/* Craft a constructor for CLASS_DECL -- what we should do when none
5543
   where found. ARGS is non NULL when a special signature must be
5544
   enforced. This is the case for anonymous classes.  */
5545
 
5546
static tree
5547
craft_constructor (tree class_decl, tree args)
5548
{
5549
  tree class_type = TREE_TYPE (class_decl);
5550
  tree parm = NULL_TREE;
5551
  /* Inherit access flags for the constructor from its enclosing class. */
5552
  int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5553
  int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5554
  int i = 0, artificial = 0;
5555
  tree decl, ctor_name;
5556
  char buffer [80];
5557
 
5558
  ctor_name = init_identifier_node;
5559
 
5560
  /* If we're dealing with an inner class constructor, we hide the
5561
     this$ decl in the name field of its parameter declaration. */
5562
  if (PURE_INNER_CLASS_TYPE_P (class_type))
5563
    {
5564
      tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5565
      parm = tree_cons (build_current_thisn (class_type),
5566
                        build_pointer_type (type), parm);
5567
 
5568
      /* Some more arguments to be hidden here. The values of the local
5569
         variables of the outer context that the inner class needs to see. */
5570
      parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5571
                                                     class_type, parm,
5572
                                                     &artificial);
5573
    }
5574
 
5575
  /* Then if there are any args to be enforced, enforce them now */
5576
  for (; args && args != end_params_node; args = TREE_CHAIN (args))
5577
    {
5578
      /* If we see a `void *', we need to change it to Object.  */
5579
      if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5580
        TREE_VALUE (args) = object_ptr_type_node;
5581
 
5582
      sprintf (buffer, "parm%d", i++);
5583
      parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5584
    }
5585
 
5586
  CRAFTED_PARAM_LIST_FIXUP (parm);
5587
  decl = create_artificial_method (class_type, flags, void_type_node,
5588
                                   ctor_name, parm);
5589
  fix_method_argument_names (parm, decl);
5590
  /* Now, mark the artificial parameters. */
5591
  DECL_FUNCTION_NAP (decl) = artificial;
5592
  DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5593
  DECL_INLINE (decl) = 1;
5594
  return decl;
5595
}
5596
 
5597
 
5598
/* Fix the constructors. This will be called right after circular
5599
   references have been checked. It is necessary to fix constructors
5600
   early even if no code generation will take place for that class:
5601
   some generated constructor might be required by the class whose
5602
   compilation triggered this one to be simply loaded.  */
5603
 
5604
void
5605
java_fix_constructors (void)
5606
{
5607
  tree current;
5608
 
5609
  for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5610
    {
5611
      tree class_type = TREE_TYPE (current);
5612
      int saw_ctor = 0;
5613
      tree decl;
5614
 
5615
      if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5616
        continue;
5617
 
5618
      output_class = current_class = class_type;
5619
      for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5620
        {
5621
          if (DECL_CONSTRUCTOR_P (decl))
5622
            {
5623
              fix_constructors (decl);
5624
              saw_ctor = 1;
5625
            }
5626
        }
5627
 
5628
      /* Anonymous class constructor can't be generated that early. */
5629
      if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5630
        craft_constructor (current, NULL_TREE);
5631
    }
5632
}
5633
 
5634
/* safe_layout_class just makes sure that we can load a class without
5635
   disrupting the current_class, input_file, input_line, etc, information
5636
   about the class processed currently.  */
5637
 
5638
void
5639
safe_layout_class (tree class)
5640
{
5641
  tree save_current_class = current_class;
5642
  location_t save_location = input_location;
5643
 
5644
  layout_class (class);
5645
 
5646
  current_class = save_current_class;
5647
  input_location = save_location;
5648
}
5649
 
5650
static tree
5651
jdep_resolve_class (jdep *dep)
5652
{
5653
  tree decl;
5654
 
5655
  /* Set the correct context for class resolution.  */
5656
  current_class = TREE_TYPE (JDEP_ENCLOSING (dep));
5657
 
5658
  if (JDEP_RESOLVED_P (dep))
5659
    decl = JDEP_RESOLVED_DECL (dep);
5660
  else
5661
    {
5662
      decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5663
                            JDEP_DECL (dep), JDEP_WFL (dep));
5664
      JDEP_RESOLVED (dep, decl);
5665
      /* If there is no WFL, that's ok.  We generate this warning
5666
         elsewhere.  */
5667
      if (decl && JDEP_WFL (dep) != NULL_TREE)
5668
        check_deprecation (JDEP_WFL (dep), decl);
5669
    }
5670
 
5671
  if (!decl)
5672
    complete_class_report_errors (dep);
5673
  else if (INNER_CLASS_DECL_P (decl))
5674
    {
5675
      tree inner = TREE_TYPE (decl);
5676
      if (! CLASS_LOADED_P (inner))
5677
        {
5678
          safe_layout_class (inner);
5679
          if (TYPE_SIZE (inner) == error_mark_node)
5680
            TYPE_SIZE (inner) = NULL_TREE;
5681
        }
5682
      check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5683
    }
5684
  return decl;
5685
}
5686
 
5687
/* Complete unsatisfied class declaration and their dependencies */
5688
 
5689
void
5690
java_complete_class (void)
5691
{
5692
  tree cclass;
5693
  jdeplist *cclassd;
5694
  int error_found;
5695
  tree type;
5696
 
5697
  /* Process imports */
5698
  process_imports ();
5699
 
5700
  /* Reverse things so we have the right order */
5701
  ctxp->class_list = nreverse (ctxp->class_list);
5702
  ctxp->classd_list = reverse_jdep_list (ctxp);
5703
 
5704
  for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5705
       cclass && cclassd;
5706
       cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5707
    {
5708
      jdep *dep;
5709
 
5710
      for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5711
        {
5712
          tree decl;
5713
          if (!(decl = jdep_resolve_class (dep)))
5714
            continue;
5715
 
5716
          /* Now it's time to patch */
5717
          switch (JDEP_KIND (dep))
5718
            {
5719
            case JDEP_SUPER:
5720
              /* Simply patch super */
5721
              if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5722
                continue;
5723
              BINFO_TYPE (BINFO_BASE_BINFO
5724
                          (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5725
                = TREE_TYPE (decl);
5726
              break;
5727
 
5728
            case JDEP_FIELD:
5729
              {
5730
                /* We do part of the job done in add_field */
5731
                tree field_decl = JDEP_DECL (dep);
5732
                tree field_type = TREE_TYPE (decl);
5733
                if (TREE_CODE (field_type) == RECORD_TYPE)
5734
                  field_type = promote_type (field_type);
5735
                TREE_TYPE (field_decl) = field_type;
5736
                DECL_ALIGN (field_decl) = 0;
5737
                DECL_USER_ALIGN (field_decl) = 0;
5738
                layout_decl (field_decl, 0);
5739
                SOURCE_FRONTEND_DEBUG
5740
                  (("Completed field/var decl '%s' with '%s'",
5741
                    IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5742
                    IDENTIFIER_POINTER (DECL_NAME (decl))));
5743
                break;
5744
              }
5745
            case JDEP_METHOD:   /* We start patching a method */
5746
            case JDEP_METHOD_RETURN:
5747
              error_found = 0;
5748
              while (1)
5749
                {
5750
                  if (decl)
5751
                    {
5752
                      type = TREE_TYPE(decl);
5753
                      if (TREE_CODE (type) == RECORD_TYPE)
5754
                        type = promote_type (type);
5755
                      JDEP_APPLY_PATCH (dep, type);
5756
                      SOURCE_FRONTEND_DEBUG
5757
                        (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5758
                           "Completing fct '%s' with ret type '%s'":
5759
                           "Completing arg '%s' with type '%s'"),
5760
                          IDENTIFIER_POINTER (EXPR_WFL_NODE
5761
                                              (JDEP_DECL_WFL (dep))),
5762
                          IDENTIFIER_POINTER (DECL_NAME (decl))));
5763
                    }
5764
                  else
5765
                    error_found = 1;
5766
                  dep = JDEP_CHAIN (dep);
5767
                  if (JDEP_KIND (dep) == JDEP_METHOD_END)
5768
                    break;
5769
                  else
5770
                    decl = jdep_resolve_class (dep);
5771
                }
5772
              if (!error_found)
5773
                {
5774
                  tree mdecl = JDEP_DECL (dep), signature;
5775
                  /* Recompute and reset the signature, check first that
5776
                     all types are now defined. If they're not,
5777
                     don't build the signature. */
5778
                  if (check_method_types_complete (mdecl))
5779
                    {
5780
                      signature = build_java_signature (TREE_TYPE (mdecl));
5781
                      set_java_signature (TREE_TYPE (mdecl), signature);
5782
                    }
5783
                }
5784
              else
5785
                continue;
5786
              break;
5787
 
5788
            case JDEP_INTERFACE:
5789
              if (parser_check_super_interface (decl, JDEP_DECL (dep),
5790
                                                JDEP_WFL (dep)))
5791
                continue;
5792
              parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5793
              break;
5794
 
5795
            case JDEP_PARM:
5796
            case JDEP_VARIABLE:
5797
              type = TREE_TYPE(decl);
5798
              if (TREE_CODE (type) == RECORD_TYPE)
5799
                type = promote_type (type);
5800
              JDEP_APPLY_PATCH (dep, type);
5801
              break;
5802
 
5803
            case JDEP_TYPE:
5804
              JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5805
              SOURCE_FRONTEND_DEBUG
5806
                (("Completing a random type dependency on a '%s' node",
5807
                  tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5808
              break;
5809
 
5810
            case JDEP_EXCEPTION:
5811
              JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5812
              SOURCE_FRONTEND_DEBUG
5813
                (("Completing '%s' 'throws' argument node",
5814
                  IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5815
              break;
5816
 
5817
            case JDEP_ANONYMOUS:
5818
              patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5819
              break;
5820
 
5821
            default:
5822
              abort ();
5823
            }
5824
        }
5825
    }
5826
  return;
5827
}
5828
 
5829
/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5830
   array.  */
5831
 
5832
static tree
5833
resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5834
{
5835
  tree tname = TYPE_NAME (class_type);
5836
  tree resolved_type = TREE_TYPE (class_type);
5837
  int array_dims = 0;
5838
  tree resolved_type_decl;
5839
 
5840
  if (resolved_type != NULL_TREE)
5841
    {
5842
      tree resolved_type_decl = TYPE_NAME (resolved_type);
5843
      if (resolved_type_decl == NULL_TREE
5844
          || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5845
        {
5846
          resolved_type_decl = build_decl (TYPE_DECL,
5847
                                           TYPE_NAME (class_type),
5848
                                           resolved_type);
5849
        }
5850
      return resolved_type_decl;
5851
    }
5852
 
5853
  /* 1- Check to see if we have an array. If true, find what we really
5854
     want to resolve  */
5855
  if ((array_dims = build_type_name_from_array_name (tname,
5856
                                                     &TYPE_NAME (class_type))))
5857
    WFL_STRIP_BRACKET (cl, cl);
5858
 
5859
  /* 2- Resolve the bare type */
5860
  if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5861
                                               decl, cl)))
5862
    return NULL_TREE;
5863
  resolved_type = TREE_TYPE (resolved_type_decl);
5864
 
5865
  /* 3- If we have an array, reconstruct the array down to its nesting */
5866
  if (array_dims)
5867
    {
5868
      for (; array_dims; array_dims--)
5869
        resolved_type = build_java_array_type (resolved_type, -1);
5870
      resolved_type_decl = TYPE_NAME (resolved_type);
5871
    }
5872
  TREE_TYPE (class_type) = resolved_type;
5873
  return resolved_type_decl;
5874
}
5875
 
5876
/* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5877
   are used to report error messages; CL must either be NULL_TREE or a
5878
   WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5879
   by a variable, since it is changed by find_in_imports{_on_demand}
5880
   and (but it doesn't really matter) qualify_and_find.  */
5881
 
5882
tree
5883
do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5884
                  tree cl)
5885
{
5886
  tree new_class_decl = NULL_TREE;
5887
  tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5888
  tree candidate = NULL_TREE;
5889
  tree decl_result;
5890
 
5891
  if (QUALIFIED_P (TYPE_NAME (class_type)))
5892
    {
5893
      /* If the type name is of the form `Q . Id', then Q is either a
5894
         package name or a class name.  First we try to find Q as a
5895
         class and then treat Id as a member type.  If we can't find Q
5896
         as a class then we fall through.  */
5897
      tree q, left, left_type, right;
5898
      if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5899
        {
5900
          BUILD_PTR_FROM_NAME (left_type, left);
5901
          q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5902
          if (q)
5903
            {
5904
              enclosing = q;
5905
              saved_enclosing_type = TREE_TYPE (q);
5906
              BUILD_PTR_FROM_NAME (class_type, right);
5907
            }
5908
        }
5909
    }
5910
 
5911
  if (enclosing)
5912
    {
5913
      tree context = enclosing;
5914
 
5915
      /* 0- Search in the current class as an inner class.
5916
         Maybe some code here should be added to load the class or
5917
         something, at least if the class isn't an inner class and ended
5918
         being loaded from class file. FIXME. */
5919
      while (enclosing)
5920
        {
5921
          new_class_decl = resolve_inner_class (context, cl, enclosing, class_type);
5922
 
5923
          if (new_class_decl)
5924
            {
5925
              if (inner_class_accessible (new_class_decl, context))
5926
                break;
5927
              else
5928
                if (candidate == NULL_TREE)
5929
                  candidate = new_class_decl;
5930
                new_class_decl = NULL_TREE;
5931
            }
5932
 
5933
          /* Now that we've looked through all superclasses, try the enclosing
5934
             context. */
5935
          enclosing = DECL_CONTEXT (enclosing);
5936
        }
5937
 
5938
      if (new_class_decl)
5939
        return new_class_decl;
5940
    }
5941
 
5942
  /* 1- Check for the type in single imports.  Look at enclosing classes and,
5943
     if we're laying out a superclass, at the import list for the subclass.
5944
     This will change TYPE_NAME() if something relevant is found. */
5945
  if (import_type && TYPE_IMPORT_LIST (import_type))
5946
    find_in_imports (import_type, class_type);
5947
  find_in_imports (saved_enclosing_type, class_type);
5948
 
5949
  /* 2- And check for the type in the current compilation unit */
5950
  if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5951
    {
5952
      if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5953
        load_class (TYPE_NAME (class_type), 0);
5954
      return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5955
    }
5956
 
5957
  /* 3- Search according to the current package definition */
5958
  if (!QUALIFIED_P (TYPE_NAME (class_type)))
5959
    {
5960
      if ((new_class_decl = qualify_and_find (class_type,
5961
                TYPE_PACKAGE (current_class), TYPE_NAME (class_type))))
5962
        return new_class_decl;
5963
    }
5964
 
5965
  /* 4- Check the import on demands. Don't allow bar.baz to be
5966
     imported from foo.* */
5967
  if (!QUALIFIED_P (TYPE_NAME (class_type)))
5968
    {
5969
      if (import_type
5970
          && TYPE_IMPORT_DEMAND_LIST (import_type)
5971
          && find_in_imports_on_demand (import_type, class_type))
5972
        return NULL_TREE;
5973
      if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5974
        return NULL_TREE;
5975
    }
5976
 
5977
  /* If found in find_in_imports_on_demand, the type has already been
5978
     loaded. */
5979
  if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5980
    return new_class_decl;
5981
 
5982
  /* 5- Check another compilation unit that bears the name of type */
5983
  load_class (TYPE_NAME (class_type), 0);
5984
 
5985
  if (!cl)
5986
    cl = lookup_cl (decl);
5987
 
5988
  /* If we don't have a value for CL, then we're being called recursively.
5989
     We can't check package access just yet, but it will be taken care of
5990
     by the caller. */
5991
  if (cl)
5992
    {
5993
      if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5994
        return NULL_TREE;
5995
    }
5996
 
5997
  /* 6- Last call for a resolution */
5998
  decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5999
 
6000
  /* The final lookup might have registered a.b.c into a.b$c If we
6001
     failed at the first lookup, progressively change the name if
6002
     applicable and use the matching DECL instead. */
6003
  if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
6004
    {
6005
      char *separator;
6006
      tree name = TYPE_NAME (class_type);
6007
      char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
6008
 
6009
      strcpy (namebuffer, IDENTIFIER_POINTER (name));
6010
 
6011
      do {
6012
 
6013
       /* Reach the last '.', and if applicable, replace it by a `$' and
6014
          see if this exists as a type. */
6015
       if ((separator = strrchr (namebuffer, '.')))
6016
         {
6017
           *separator = '$';
6018
           name = get_identifier (namebuffer);
6019
           decl_result = IDENTIFIER_CLASS_VALUE (name);
6020
         }
6021
      } while (!decl_result && separator);
6022
    }
6023
  if (decl_result)
6024
    return decl_result;
6025
  else
6026
    return candidate;
6027
}
6028
 
6029
static tree
6030
qualify_and_find (tree class_type, tree package, tree name)
6031
{
6032
  tree new_qualified = merge_qualified_name (package, name);
6033
  tree new_class_decl;
6034
 
6035
  if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6036
    load_class (new_qualified, 0);
6037
  if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6038
    {
6039
      if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6040
        load_class (TREE_TYPE (new_class_decl), 0);
6041
      TYPE_NAME (class_type) = new_qualified;
6042
      return IDENTIFIER_CLASS_VALUE (new_qualified);
6043
    }
6044
  return NULL_TREE;
6045
}
6046
 
6047
/* Resolve NAME and lay it out (if not done and if not the current
6048
   parsed class). Return a decl node. This function is meant to be
6049
   called when type resolution is necessary during the walk pass.  */
6050
 
6051
static tree
6052
resolve_and_layout (tree something, tree cl)
6053
{
6054
  tree decl, decl_type;
6055
 
6056
  /* Don't do that on the current class */
6057
  if (something == current_class)
6058
    return TYPE_NAME (current_class);
6059
 
6060
  /* Don't do anything for void and other primitive types */
6061
  if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6062
    return NULL_TREE;
6063
 
6064
  /* Pointer types can be reall pointer types or fake pointers. When
6065
     finding a real pointer, recheck for primitive types */
6066
  if (TREE_CODE (something) == POINTER_TYPE)
6067
    {
6068
      if (TREE_TYPE (something))
6069
        {
6070
          something = TREE_TYPE (something);
6071
          if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6072
            return NULL_TREE;
6073
        }
6074
      else
6075
        something = TYPE_NAME (something);
6076
    }
6077
 
6078
  /* Don't do anything for arrays of primitive types */
6079
  if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6080
      && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6081
    return NULL_TREE;
6082
 
6083
  /* Something might be a WFL */
6084
  if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6085
    something = EXPR_WFL_NODE (something);
6086
 
6087
  /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a
6088
     TYPE_DECL or a real TYPE.  */
6089
  else if (TREE_CODE (something) != IDENTIFIER_NODE)
6090
    something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6091
            DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6092
 
6093
  if (!(decl = resolve_no_layout (something, cl)))
6094
    return NULL_TREE;
6095
 
6096
  /* Resolve and layout if necessary */
6097
  decl_type = TREE_TYPE (decl);
6098
  layout_class_methods (decl_type);
6099
  /* Check methods */
6100
  if (CLASS_FROM_SOURCE_P (decl_type))
6101
    java_check_methods (decl);
6102
  /* Layout the type if necessary */
6103
  if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6104
    safe_layout_class (decl_type);
6105
 
6106
  return decl;
6107
}
6108
 
6109
/* Resolve a class, returns its decl but doesn't perform any
6110
   layout. The current parsing context is saved and restored */
6111
 
6112
static tree
6113
resolve_no_layout (tree name, tree cl)
6114
{
6115
  tree ptr, decl;
6116
  BUILD_PTR_FROM_NAME (ptr, name);
6117
  java_parser_context_save_global ();
6118
  decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6119
  java_parser_context_restore_global ();
6120
 
6121
  return decl;
6122
}
6123
 
6124
/* Called when reporting errors. Skip the '[]'s in a complex array
6125
   type description that failed to be resolved. purify_type_name can't
6126
   use an identifier tree.  */
6127
 
6128
static const char *
6129
purify_type_name (const char *name)
6130
{
6131
  int len = strlen (name);
6132
  int bracket_found;
6133
 
6134
  STRING_STRIP_BRACKETS (name, len, bracket_found);
6135
  if (bracket_found)
6136
    {
6137
      char *stripped_name = xmemdup (name, len, len+1);
6138
      stripped_name [len] = '\0';
6139
      return stripped_name;
6140
    }
6141
  return name;
6142
}
6143
 
6144
/* The type CURRENT refers to can't be found. We print error messages.  */
6145
 
6146
static void
6147
complete_class_report_errors (jdep *dep)
6148
{
6149
  const char *name;
6150
 
6151
  if (!JDEP_WFL (dep))
6152
    return;
6153
 
6154
  name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6155
  switch (JDEP_KIND (dep))
6156
    {
6157
    case JDEP_SUPER:
6158
      parse_error_context
6159
        (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6160
         purify_type_name (name),
6161
         IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6162
      break;
6163
    case JDEP_FIELD:
6164
      parse_error_context
6165
        (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6166
         purify_type_name (name),
6167
         IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6168
      break;
6169
    case JDEP_METHOD:           /* Covers arguments */
6170
      parse_error_context
6171
        (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6172
         purify_type_name (name),
6173
         IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6174
         IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6175
      break;
6176
    case JDEP_METHOD_RETURN:    /* Covers return type */
6177
      parse_error_context
6178
        (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6179
         purify_type_name (name),
6180
         IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6181
      break;
6182
    case JDEP_INTERFACE:
6183
      parse_error_context
6184
        (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6185
         IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6186
         (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6187
         IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6188
      break;
6189
    case JDEP_VARIABLE:
6190
      parse_error_context
6191
        (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6192
         purify_type_name (IDENTIFIER_POINTER
6193
                           (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6194
         IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6195
      break;
6196
    case JDEP_EXCEPTION:        /* As specified by `throws' */
6197
      parse_error_context
6198
          (JDEP_WFL (dep), "Class %qs not found in %",
6199
         IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6200
      break;
6201
    default:
6202
      /* Fix for -Wall. Just break doing nothing. The error will be
6203
         caught later */
6204
      break;
6205
    }
6206
}
6207
 
6208
/* Return a static string containing the DECL prototype string. If
6209
   DECL is a constructor, use the class name instead of the form
6210
    */
6211
 
6212
static const char *
6213
get_printable_method_name (tree decl)
6214
{
6215
  const char *to_return;
6216
  tree name = NULL_TREE;
6217
 
6218
  if (DECL_CONSTRUCTOR_P (decl))
6219
    {
6220
      name = DECL_NAME (decl);
6221
      DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6222
    }
6223
 
6224
  to_return = lang_printable_name (decl, 2);
6225
  if (DECL_CONSTRUCTOR_P (decl))
6226
    DECL_NAME (decl) = name;
6227
 
6228
  return to_return;
6229
}
6230
 
6231
/* Track method being redefined inside the same class. As a side
6232
   effect, set DECL_NAME to an IDENTIFIER (prior entering this
6233
   function it's a FWL, so we can track errors more accurately.)  */
6234
 
6235
static int
6236
check_method_redefinition (tree class, tree method)
6237
{
6238
  tree redef, sig;
6239
 
6240
  /* There's no need to verify  and finit$ and instinit$ */
6241
  if (DECL_CLINIT_P (method)
6242
      || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6243
    return 0;
6244
 
6245
  sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6246
  for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6247
    {
6248
      if (redef == method)
6249
        break;
6250
      if (DECL_NAME (redef) == DECL_NAME (method)
6251
          && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6252
          && !DECL_ARTIFICIAL (method))
6253
        {
6254
          parse_error_context
6255
            (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6256
             (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6257
             get_printable_method_name (redef));
6258
          return 1;
6259
        }
6260
    }
6261
  return 0;
6262
}
6263
 
6264
/* Return 1 if check went ok, 0 otherwise.  */
6265
static int
6266
check_abstract_method_definitions (int do_interface, tree class_decl,
6267
                                   tree type)
6268
{
6269
  tree class = TREE_TYPE (class_decl);
6270
  tree method, end_type;
6271
  int ok = 1;
6272
 
6273
  end_type = (do_interface ? object_type_node : type);
6274
  for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6275
    {
6276
      tree other_super, other_method, method_sig, method_name;
6277
      int found = 0;
6278
      int end_type_reached = 0;
6279
 
6280
      if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6281
        continue;
6282
 
6283
      /* Now verify that somewhere in between TYPE and CLASS,
6284
         abstract method METHOD gets a non abstract definition
6285
         that is inherited by CLASS.  */
6286
 
6287
      method_sig = build_java_signature (TREE_TYPE (method));
6288
      method_name = DECL_NAME (method);
6289
      if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6290
        method_name = EXPR_WFL_NODE (method_name);
6291
 
6292
      other_super = class;
6293
      do {
6294
        if (other_super == end_type)
6295
          end_type_reached = 1;
6296
 
6297
        /* Method search */
6298
        for (other_method = TYPE_METHODS (other_super); other_method;
6299
            other_method = TREE_CHAIN (other_method))
6300
          {
6301
            tree s = build_java_signature (TREE_TYPE (other_method));
6302
            tree other_name = DECL_NAME (other_method);
6303
 
6304
            if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6305
              other_name = EXPR_WFL_NODE (other_name);
6306
            if (!DECL_CLINIT_P (other_method)
6307
                && !DECL_CONSTRUCTOR_P (other_method)
6308
                && method_name == other_name
6309
                && method_sig == s
6310
                && !METHOD_ABSTRACT (other_method))
6311
             {
6312
               found = 1;
6313
               break;
6314
             }
6315
          }
6316
        other_super = CLASSTYPE_SUPER (other_super);
6317
      } while (!end_type_reached);
6318
 
6319
      /* Report that abstract METHOD didn't find an implementation
6320
         that CLASS can use. */
6321
      if (!found)
6322
        {
6323
          char *t = xstrdup (lang_printable_name
6324
                            (TREE_TYPE (TREE_TYPE (method)), 0));
6325
          tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6326
 
6327
          parse_error_context
6328
            (lookup_cl (class_decl),
6329
             "Class %qs doesn't define the abstract method %<%s %s%> from %s %<%s%>. This method must be defined or %s %qs must be declared abstract",
6330
             IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6331
             t, lang_printable_name (method, 2),
6332
             (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6333
              "interface" : "class"),
6334
             IDENTIFIER_POINTER (ccn),
6335
             (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6336
             IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6337
          ok = 0;
6338
          free (t);
6339
        }
6340
    }
6341
 
6342
  if (ok && do_interface)
6343
    {
6344
      /* Check for implemented interfaces. */
6345
      int i;
6346
      tree base_binfo;
6347
 
6348
      for (i = 1;
6349
           ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6350
           i++)
6351
        ok = check_abstract_method_definitions (1, class_decl,
6352
                                                BINFO_TYPE (base_binfo));
6353
    }
6354
 
6355
  return ok;
6356
}
6357
 
6358
/* Check that CLASS_DECL somehow implements all inherited abstract
6359
   methods.  */
6360
 
6361
static void
6362
java_check_abstract_method_definitions (tree class_decl)
6363
{
6364
  tree class = TREE_TYPE (class_decl);
6365
  tree super, base_binfo;
6366
  int i;
6367
 
6368
  if (CLASS_ABSTRACT (class_decl))
6369
    return;
6370
 
6371
  /* Check for inherited types */
6372
  super = class;
6373
  do {
6374
    super = CLASSTYPE_SUPER (super);
6375
    check_abstract_method_definitions (0, class_decl, super);
6376
  } while (super != object_type_node);
6377
 
6378
  /* Check for implemented interfaces. */
6379
  for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6380
    check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6381
}
6382
 
6383
/* Check all the types method DECL uses and return 1 if all of them
6384
   are now complete, 0 otherwise. This is used to check whether its
6385
   safe to build a method signature or not.  */
6386
 
6387
static int
6388
check_method_types_complete (tree decl)
6389
{
6390
  tree type = TREE_TYPE (decl);
6391
  tree args;
6392
 
6393
  if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6394
    return 0;
6395
 
6396
  args = TYPE_ARG_TYPES (type);
6397
  if (TREE_CODE (type) == METHOD_TYPE)
6398
    args = TREE_CHAIN (args);
6399
  for (; args != end_params_node; args = TREE_CHAIN (args))
6400
    if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6401
      return 0;
6402
 
6403
  return 1;
6404
}
6405
 
6406
/* Visible interface to check methods contained in CLASS_DECL */
6407
 
6408
void
6409
java_check_methods (tree class_decl)
6410
{
6411
  if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6412
    return;
6413
 
6414
  if (CLASS_INTERFACE (class_decl))
6415
    java_check_abstract_methods (class_decl);
6416
  else
6417
    java_check_regular_methods (class_decl);
6418
 
6419
  CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6420
}
6421
 
6422
/* Like not_accessible_p, but doesn't refer to the current class at
6423
   all.  */
6424
static bool
6425
hack_is_accessible_p (tree member, tree from_where)
6426
{
6427
  int flags = get_access_flags_from_decl (member);
6428
 
6429
  if (from_where == DECL_CONTEXT (member)
6430
      || (flags & ACC_PUBLIC))
6431
    return true;
6432
 
6433
  if ((flags & ACC_PROTECTED))
6434
    {
6435
      if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6436
        return true;
6437
    }
6438
 
6439
  if ((flags & ACC_PRIVATE))
6440
    return false;
6441
 
6442
  /* Package private, or protected.  */
6443
  return in_same_package (TYPE_NAME (from_where),
6444
                          TYPE_NAME (DECL_CONTEXT (member)));
6445
}
6446
 
6447
/* Check all the methods of CLASS_DECL. Methods are first completed
6448
   then checked according to regular method existence rules.  If no
6449
   constructor for CLASS_DECL were encountered, then build its
6450
   declaration.  */
6451
static void
6452
java_check_regular_methods (tree class_decl)
6453
{
6454
  int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6455
  tree method;
6456
  tree class = TREE_TYPE (class_decl);
6457
  tree found = NULL_TREE;
6458
  tree mthrows;
6459
 
6460
  /* It is not necessary to check methods defined in java.lang.Object */
6461
  if (class == object_type_node)
6462
    return;
6463
 
6464
  if (!TYPE_NVIRTUALS (class))
6465
    TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6466
 
6467
  /* Should take interfaces into account. FIXME */
6468
  for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6469
    {
6470
      tree sig;
6471
      tree method_wfl = DECL_FUNCTION_WFL (method);
6472
      int aflags;
6473
 
6474
      /* Check for redefinitions */
6475
      if (check_method_redefinition (class, method))
6476
        continue;
6477
 
6478
      /* We verify things thrown by the method.  They must inherit from
6479
         java.lang.Throwable.  */
6480
      for (mthrows = DECL_FUNCTION_THROWS (method);
6481
           mthrows; mthrows = TREE_CHAIN (mthrows))
6482
        {
6483
          if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6484
            parse_error_context
6485
              (TREE_PURPOSE (mthrows), "Class %qs in % clause must be a subclass of class %",
6486
               IDENTIFIER_POINTER
6487
                 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6488
        }
6489
 
6490
      /* If we see one constructor a mark so we don't generate the
6491
         default one.  Also skip other verifications: constructors
6492
         can't be inherited hence hidden or overridden.  */
6493
      if (DECL_CONSTRUCTOR_P (method))
6494
        {
6495
          saw_constructor = 1;
6496
          continue;
6497
        }
6498
 
6499
      sig = build_java_argument_signature (TREE_TYPE (method));
6500
      found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6501
                                              SEARCH_SUPER | SEARCH_INTERFACE);
6502
 
6503
      /* Inner class can't declare static methods */
6504
      if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6505
        {
6506
          char *t = xstrdup (lang_printable_name (class, 0));
6507
          parse_error_context
6508
            (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6509
             lang_printable_name (method, 2), t);
6510
          free (t);
6511
        }
6512
 
6513
      /* Nothing overrides or it's a private method. */
6514
      if (!found)
6515
        continue;
6516
      if (METHOD_PRIVATE (found))
6517
        {
6518
          found = NULL_TREE;
6519
          continue;
6520
        }
6521
 
6522
      /* If `found' is declared in an interface, make sure the
6523
         modifier matches. */
6524
      if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6525
          && clinit_identifier_node != DECL_NAME (found)
6526
          && !METHOD_PUBLIC (method))
6527
        {
6528
          tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6529
          parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6530
                               IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6531
                               lang_printable_name (found, 0),
6532
                               IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6533
        }
6534
 
6535
      /* Can't override a method with the same name and different return
6536
         types. */
6537
      if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6538
        {
6539
          char *t = xstrdup
6540
            (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6541
          parse_error_context
6542
            (method_wfl,
6543
             "Method %qs was defined with return type %qs in class %qs",
6544
             lang_printable_name (found, 2), t,
6545
             IDENTIFIER_POINTER
6546
               (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6547
          free (t);
6548
        }
6549
 
6550
      aflags = get_access_flags_from_decl (found);
6551
 
6552
      /* Can't override final. Can't override static. */
6553
      if (METHOD_FINAL (found) || METHOD_STATIC (found))
6554
        {
6555
          /* Static *can* override static */
6556
          if (METHOD_STATIC (found) && METHOD_STATIC (method))
6557
            continue;
6558
          parse_error_context
6559
            (method_wfl,
6560
             "%s methods can't be overridden. Method %qs is %s in class %qs",
6561
             (METHOD_FINAL (found) ? "Final" : "Static"),
6562
             lang_printable_name (found, 2),
6563
             (METHOD_FINAL (found) ? "final" : "static"),
6564
             IDENTIFIER_POINTER
6565
               (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6566
          continue;
6567
        }
6568
 
6569
      /* Static method can't override instance method. */
6570
      if (METHOD_STATIC (method))
6571
        {
6572
          parse_error_context
6573
            (method_wfl,
6574
             "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6575
             lang_printable_name (found, 2),
6576
             IDENTIFIER_POINTER
6577
               (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6578
          continue;
6579
        }
6580
 
6581
      /* - Overriding/hiding public must be public
6582
         - Overriding/hiding protected must be protected or public
6583
         - If the overridden or hidden method has default (package)
6584
           access, then the overriding or hiding method must not be
6585
           private; otherwise, a compile-time error occurs.  If
6586
           `found' belongs to an interface, things have been already
6587
           taken care of.  */
6588
      if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6589
          && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6590
              || (METHOD_PROTECTED (found)
6591
                  && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6592
              || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6593
                  && METHOD_PRIVATE (method))))
6594
        {
6595
          parse_error_context
6596
            (method_wfl,
6597
             "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6598
             (METHOD_PUBLIC (method) ? "public" :
6599
              (METHOD_PRIVATE (method) ? "private" : "protected")),
6600
             IDENTIFIER_POINTER (DECL_NAME
6601
                                 (TYPE_NAME (DECL_CONTEXT (found)))));
6602
          continue;
6603
        }
6604
 
6605
      /* Check this method against all the other implementations it
6606
         overrides.  Here we only check the class hierarchy; the rest
6607
         of the checking is done later.  If this method is just a
6608
         Miranda method, we can skip the check.  */
6609
      if (! METHOD_INVISIBLE (method))
6610
        check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6611
    }
6612
 
6613
  /* The above throws clause check only looked at superclasses.  Now
6614
     we must also make sure that all methods declared in interfaces
6615
     have compatible throws clauses.  FIXME: there are more efficient
6616
     ways to organize this checking; we should implement one.  */
6617
  check_interface_throws_clauses (class, class);
6618
 
6619
  if (!TYPE_NVIRTUALS (class))
6620
    TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6621
 
6622
  /* Search for inherited abstract method not yet implemented in this
6623
     class.  */
6624
  java_check_abstract_method_definitions (class_decl);
6625
 
6626
  if (!saw_constructor)
6627
    abort ();
6628
}
6629
 
6630
/* Check to make sure that all the methods in all the interfaces
6631
   implemented by CLASS_DECL are compatible with the concrete
6632
   implementations available in CHECK_CLASS_DECL.  */
6633
static void
6634
check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6635
{
6636
  for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6637
    {
6638
      int i;
6639
 
6640
      if (! CLASS_LOADED_P (class_decl))
6641
        {
6642
          if (CLASS_FROM_SOURCE_P (class_decl))
6643
            safe_layout_class (class_decl);
6644
          else
6645
            load_class (class_decl, 1);
6646
        }
6647
 
6648
      for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6649
        {
6650
          tree interface
6651
            = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6652
          tree iface_method;
6653
 
6654
          for (iface_method = TYPE_METHODS (interface);
6655
               iface_method != NULL_TREE;
6656
               iface_method = TREE_CHAIN (iface_method))
6657
            {
6658
              tree sig, method;
6659
 
6660
              /* First look for a concrete method implemented or
6661
                 inherited by this class.  No need to search
6662
                 interfaces here, since we're already looking through
6663
                 all of them.  */
6664
              sig = build_java_argument_signature (TREE_TYPE (iface_method));
6665
              method
6666
                = lookup_argument_method_generic (check_class_decl,
6667
                                                  DECL_NAME (iface_method),
6668
                                                  sig, SEARCH_VISIBLE);
6669
              /* If we don't find an implementation, that is ok.  Any
6670
                 potential errors from that are diagnosed elsewhere.
6671
                 Also, multiple inheritance with conflicting throws
6672
                 clauses is fine in the absence of a concrete
6673
                 implementation.  */
6674
              if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6675
                  && !METHOD_INVISIBLE (iface_method))
6676
                {
6677
                  tree method_wfl = DECL_FUNCTION_WFL (method);
6678
                  check_throws_clauses (method, method_wfl, iface_method);
6679
                }
6680
            }
6681
 
6682
          /* Now check superinterfaces.  */
6683
          check_interface_throws_clauses (check_class_decl, interface);
6684
        }
6685
    }
6686
}
6687
 
6688
/* Check throws clauses of a method against the clauses of all the
6689
   methods it overrides.  We do this by searching up the class
6690
   hierarchy, examining all matching accessible methods.  */
6691
static void
6692
check_concrete_throws_clauses (tree class, tree self_method,
6693
                               tree name, tree signature)
6694
{
6695
  tree method = lookup_argument_method_generic (class, name, signature,
6696
                                                SEARCH_SUPER | SEARCH_VISIBLE);
6697
  while (method != NULL_TREE)
6698
    {
6699
      if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6700
        check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6701
                              method);
6702
 
6703
      method = lookup_argument_method_generic (DECL_CONTEXT (method),
6704
                                               name, signature,
6705
                                               SEARCH_SUPER | SEARCH_VISIBLE);
6706
    }
6707
}
6708
 
6709
/* Generate an error if the `throws' clause of METHOD (if any) is
6710
   incompatible with the `throws' clause of FOUND (if any).  */
6711
static void
6712
check_throws_clauses (tree method, tree method_wfl, tree found)
6713
{
6714
  tree mthrows;
6715
 
6716
  for (mthrows = DECL_FUNCTION_THROWS (method);
6717
       mthrows; mthrows = TREE_CHAIN (mthrows))
6718
    {
6719
      tree fthrows;
6720
 
6721
      /* We don't verify unchecked expressions */
6722
      if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6723
        continue;
6724
      /* Checked expression must be compatible */
6725
      for (fthrows = DECL_FUNCTION_THROWS (found);
6726
           fthrows; fthrows = TREE_CHAIN (fthrows))
6727
        {
6728
          if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6729
            break;
6730
        }
6731
      if (!fthrows)
6732
        {
6733
          parse_error_context
6734
            (method_wfl, "Invalid checked exception class %qs in % clause.  The exception must be a subclass of an exception thrown by %qs from class %qs",
6735
             IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6736
             lang_printable_name (found, 2),
6737
             IDENTIFIER_POINTER
6738
             (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6739
        }
6740
    }
6741
}
6742
 
6743
/* Check abstract method of interface INTERFACE */
6744
static void
6745
java_check_abstract_methods (tree interface_decl)
6746
{
6747
  int i;
6748
  tree method, found;
6749
  tree interface = TREE_TYPE (interface_decl);
6750
  tree base_binfo;
6751
 
6752
  for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6753
    {
6754
      /* 2- Check for double definition inside the defining interface */
6755
      if (check_method_redefinition (interface, method))
6756
        continue;
6757
 
6758
      /* 3- Overriding is OK as far as we preserve the return type.  */
6759
      found = lookup_java_interface_method2 (interface, method);
6760
      if (found)
6761
        {
6762
          char *t;
6763
          t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6764
          parse_error_context
6765
            (DECL_FUNCTION_WFL (found),
6766
             "Method %qs was defined with return type %qs in class %qs",
6767
             lang_printable_name (found, 2), t,
6768
             IDENTIFIER_POINTER
6769
               (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6770
          free (t);
6771
          continue;
6772
        }
6773
    }
6774
 
6775
  /* 4- Inherited methods can't differ by their returned types */
6776
  for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6777
    {
6778
      tree sub_interface_method, sub_interface;
6779
 
6780
      sub_interface = BINFO_TYPE (base_binfo);
6781
      for (sub_interface_method = TYPE_METHODS (sub_interface);
6782
           sub_interface_method;
6783
           sub_interface_method = TREE_CHAIN (sub_interface_method))
6784
        {
6785
          found = lookup_java_interface_method2 (interface,
6786
                                                 sub_interface_method);
6787
          if (found && (found != sub_interface_method))
6788
            {
6789
              parse_error_context
6790
                (lookup_cl (sub_interface_method),
6791
                 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6792
                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6793
                 lang_printable_name (found, 2),
6794
                 IDENTIFIER_POINTER
6795
                   (DECL_NAME (TYPE_NAME
6796
                               (DECL_CONTEXT (sub_interface_method)))),
6797
                 IDENTIFIER_POINTER
6798
                   (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6799
            }
6800
        }
6801
    }
6802
}
6803
 
6804
/* Lookup methods in interfaces using their name and partial
6805
   signature. Return a matching method only if their types differ.  */
6806
 
6807
static tree
6808
lookup_java_interface_method2 (tree class, tree method_decl)
6809
{
6810
  int i;
6811
  tree base_binfo;
6812
  tree to_return;
6813
 
6814
  for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6815
    {
6816
      if ((BINFO_TYPE (base_binfo) != object_type_node)
6817
          && (to_return =
6818
              lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6819
        return to_return;
6820
    }
6821
  for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6822
    {
6823
      to_return = lookup_java_interface_method2
6824
        (BINFO_TYPE (base_binfo), method_decl);
6825
      if (to_return)
6826
        return to_return;
6827
    }
6828
 
6829
  return NULL_TREE;
6830
}
6831
 
6832
/* Lookup method using their name and partial signature. Return a
6833
   matching method only if their types differ.  */
6834
 
6835
static tree
6836
lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6837
{
6838
  tree method, method_signature, method_name, method_type, name;
6839
 
6840
  method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6841
  name = DECL_NAME (method_decl);
6842
  method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6843
                 EXPR_WFL_NODE (name) : name);
6844
  method_type = TREE_TYPE (TREE_TYPE (method_decl));
6845
 
6846
  while (clas != NULL_TREE)
6847
    {
6848
      for (method = TYPE_METHODS (clas);
6849
           method != NULL_TREE;  method = TREE_CHAIN (method))
6850
        {
6851
          tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6852
          tree name = DECL_NAME (method);
6853
          if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6854
               EXPR_WFL_NODE (name) : name) == method_name
6855
              && method_sig == method_signature
6856
              && TREE_TYPE (TREE_TYPE (method)) != method_type)
6857
            return method;
6858
        }
6859
      clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6860
    }
6861
  return NULL_TREE;
6862
}
6863
 
6864
/* Return the line that matches DECL line number, and try its best to
6865
   position the column number. Used during error reports.
6866
   FUTURE/FIXME: return source_location instead of node. */
6867
 
6868
static GTY(()) tree cl_v;
6869
static tree
6870
lookup_cl (tree decl)
6871
{
6872
#ifndef USE_MAPPED_LOCATION
6873
  char *line, *found;
6874
#endif
6875
 
6876
  if (!decl)
6877
    return NULL_TREE;
6878
 
6879
  if (cl_v == NULL_TREE)
6880
    {
6881
      cl_v = build_unknown_wfl (NULL_TREE);
6882
    }
6883
 
6884
#ifdef USE_MAPPED_LOCATION
6885
  SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6886
#else
6887
  EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6888
  EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6889
 
6890
  line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6891
                            EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6892
 
6893
  found = strstr ((const char *)line,
6894
                  (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6895
  if (found)
6896
    EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6897
#endif
6898
 
6899
  return cl_v;
6900
}
6901
 
6902
/* Look for a simple name in the single-type import list */
6903
 
6904
static tree
6905
find_name_in_single_imports (tree name)
6906
{
6907
  tree node;
6908
 
6909
  for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6910
    if (TREE_VALUE (node) == name)
6911
      return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6912
 
6913
  return NULL_TREE;
6914
}
6915
 
6916
/* Process all single-type import. */
6917
 
6918
static int
6919
process_imports (void)
6920
{
6921
  tree import;
6922
  int error_found;
6923
 
6924
  for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6925
    {
6926
      tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6927
      char *original_name;
6928
 
6929
      /* Don't load twice something already defined. */
6930
      if (IDENTIFIER_CLASS_VALUE (to_be_found))
6931
        continue;
6932
 
6933
      original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6934
                               IDENTIFIER_LENGTH (to_be_found),
6935
                               IDENTIFIER_LENGTH (to_be_found) + 1);
6936
 
6937
      while (1)
6938
        {
6939
          tree left;
6940
 
6941
          QUALIFIED_P (to_be_found) = 1;
6942
          load_class (to_be_found, 0);
6943
          error_found =
6944
            check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6945
 
6946
          /* We found it, we can bail out */
6947
          if (IDENTIFIER_CLASS_VALUE (to_be_found))
6948
            {
6949
              check_deprecation (TREE_PURPOSE (import),
6950
                                 IDENTIFIER_CLASS_VALUE (to_be_found));
6951
              break;
6952
            }
6953
 
6954
          /* We haven't found it. Maybe we're trying to access an
6955
             inner class.  The only way for us to know is to try again
6956
             after having dropped a qualifier. If we can't break it further,
6957
             we have an error. */
6958
          if (split_qualified_name (&left, NULL, to_be_found))
6959
            break;
6960
 
6961
          to_be_found = left;
6962
        }
6963
      if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6964
        {
6965
          parse_error_context (TREE_PURPOSE (import),
6966
                               "Class or interface %qs not found in import",
6967
                               original_name);
6968
          error_found = 1;
6969
        }
6970
 
6971
      free (original_name);
6972
      if (error_found)
6973
        return 1;
6974
    }
6975
  return 0;
6976
}
6977
 
6978
/* Possibly find and mark a class imported by a single-type import
6979
   statement.  */
6980
 
6981
static void
6982
find_in_imports (tree enclosing_type, tree class_type)
6983
{
6984
  tree import;
6985
  if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6986
    import = TYPE_IMPORT_LIST (enclosing_type);
6987
  else
6988
    import = ctxp->import_list;
6989
 
6990
  while (import)
6991
    {
6992
      if (TREE_VALUE (import) == TYPE_NAME (class_type))
6993
        {
6994
          TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6995
          QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6996
          return;
6997
        }
6998
      import = TREE_CHAIN (import);
6999
    }
7000
}
7001
 
7002
static int
7003
note_possible_classname (const char *name, int len)
7004
{
7005
  tree node;
7006
  if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
7007
    len = len - 5;
7008
  else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
7009
    len = len - 6;
7010
  else
7011
    return 0;
7012
  node = ident_subst (name, len, "", '/', '.', "");
7013
  IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
7014
  QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
7015
  return 1;
7016
}
7017
 
7018
/* Read a import directory, gathering potential match for further type
7019
   references. Indifferently reads a filesystem or a ZIP archive
7020
   directory.  */
7021
 
7022
static void
7023
read_import_dir (tree wfl)
7024
{
7025
  tree package_id = EXPR_WFL_NODE (wfl);
7026
  const char *package_name = IDENTIFIER_POINTER (package_id);
7027
  int package_length = IDENTIFIER_LENGTH (package_id);
7028
  DIR *dirp = NULL;
7029
  JCF *saved_jcf = current_jcf;
7030
 
7031
  int found = 0;
7032
  int k;
7033
  void *entry;
7034
  struct buffer filename[1];
7035
 
7036
  if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7037
    return;
7038
  IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7039
 
7040
  BUFFER_INIT (filename);
7041
  buffer_grow (filename, package_length + 100);
7042
 
7043
  for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7044
    {
7045
      const char *entry_name = jcf_path_name (entry);
7046
      int entry_length = strlen (entry_name);
7047
      if (jcf_path_is_zipfile (entry))
7048
        {
7049
          ZipFile *zipf;
7050
          buffer_grow (filename, entry_length);
7051
          memcpy (filename->data, entry_name, entry_length - 1);
7052
          filename->data[entry_length-1] = '\0';
7053
          zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7054
          if (zipf == NULL)
7055
            error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7056
          else
7057
            {
7058
              ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7059
              BUFFER_RESET (filename);
7060
              for (k = 0; k < package_length; k++)
7061
                {
7062
                  char ch = package_name[k];
7063
                  *filename->ptr++ = ch == '.' ? '/' : ch;
7064
                }
7065
              *filename->ptr++ = '/';
7066
 
7067
              for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
7068
                {
7069
                  const char *current_entry = ZIPDIR_FILENAME (zipd);
7070
                  int current_entry_len = zipd->filename_length;
7071
 
7072
                  if (current_entry_len >= BUFFER_LENGTH (filename)
7073
                      && strncmp ((const char *) filename->data, current_entry,
7074
                                  BUFFER_LENGTH (filename)) != 0)
7075
                    continue;
7076
                  found |= note_possible_classname (current_entry,
7077
                                                    current_entry_len);
7078
                }
7079
            }
7080
        }
7081
      else
7082
        {
7083
          BUFFER_RESET (filename);
7084
          buffer_grow (filename, entry_length + package_length + 4);
7085
          strcpy ((char *) filename->data, entry_name);
7086
          filename->ptr = filename->data + entry_length;
7087
          for (k = 0; k < package_length; k++)
7088
            {
7089
              char ch = package_name[k];
7090
              *filename->ptr++ = ch == '.' ? '/' : ch;
7091
            }
7092
          *filename->ptr = '\0';
7093
 
7094
          dirp = opendir ((const char *) filename->data);
7095
          if (dirp == NULL)
7096
            continue;
7097
          *filename->ptr++ = '/';
7098
          for (;;)
7099
            {
7100
              int len;
7101
              const char *d_name;
7102
              struct dirent *direntp = readdir (dirp);
7103
              if (!direntp)
7104
                break;
7105
              d_name = direntp->d_name;
7106
              len = strlen (direntp->d_name);
7107
              buffer_grow (filename, len+1);
7108
              strcpy ((char *) filename->ptr, d_name);
7109
              found |= note_possible_classname ((const char *) filename->data + entry_length,
7110
                                                package_length+len+1);
7111
            }
7112
          if (dirp)
7113
            closedir (dirp);
7114
        }
7115
    }
7116
 
7117
  free (filename->data);
7118
 
7119
  /* Here we should have a unified way of retrieving an entry, to be
7120
     indexed. */
7121
  if (!found)
7122
    {
7123
      static int first = 1;
7124
      if (first)
7125
        {
7126
          error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7127
          java_error_count++;
7128
          first = 0;
7129
        }
7130
      else
7131
        parse_error_context (wfl, "Package %qs not found in import",
7132
                             package_name);
7133
      current_jcf = saved_jcf;
7134
      return;
7135
    }
7136
  current_jcf = saved_jcf;
7137
}
7138
 
7139
/* Possibly find a type in the import on demands specified
7140
   types. Returns 1 if an error occurred, 0 otherwise. Run through the
7141
   entire list, to detected potential double definitions.  */
7142
 
7143
static int
7144
find_in_imports_on_demand (tree enclosing_type, tree class_type)
7145
{
7146
  tree class_type_name = TYPE_NAME (class_type);
7147
  tree cl = NULL_TREE;
7148
  int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
7149
  int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
7150
  tree node;
7151
  tree import;
7152
 
7153
  if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7154
    import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7155
  else
7156
    import = ctxp->import_demand_list;
7157
 
7158
  for (; import; import = TREE_CHAIN (import))
7159
    {
7160
      location_t saved_location = input_location;
7161
      int access_check;
7162
      const char *id_name;
7163
      tree decl, type_name_copy;
7164
 
7165
      obstack_grow (&temporary_obstack,
7166
                    IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7167
                    IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7168
      obstack_1grow (&temporary_obstack, '.');
7169
      obstack_grow0 (&temporary_obstack,
7170
                     IDENTIFIER_POINTER (class_type_name),
7171
                     IDENTIFIER_LENGTH (class_type_name));
7172
      id_name = obstack_finish (&temporary_obstack);
7173
 
7174
      if (! (node = maybe_get_identifier (id_name)))
7175
        continue;
7176
 
7177
      /* Setup input_line so that it refers to the line of the import (in
7178
         case we parse a class file and encounter errors */
7179
#ifdef USE_MAPPED_LOCATION
7180
      input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7181
#else
7182
      input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7183
#endif
7184
 
7185
      type_name_copy = TYPE_NAME (class_type);
7186
      TYPE_NAME (class_type) = node;
7187
      QUALIFIED_P (node) = 1;
7188
      decl = IDENTIFIER_CLASS_VALUE (node);
7189
      access_check = -1;
7190
      /* If there is no DECL set for the class or if the class isn't
7191
         loaded and not seen in source yet, then load */
7192
      if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7193
        {
7194
          load_class (node, 0);
7195
          decl = IDENTIFIER_CLASS_VALUE (node);
7196
        }
7197
      if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7198
        access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7199
                                               false, NULL_TREE);
7200
      else
7201
        /* 6.6.1: Inner classes are subject to member access rules. */
7202
        access_check = 0;
7203
 
7204
      input_location = saved_location;
7205
 
7206
      /* If the loaded class is not accessible or couldn't be loaded,
7207
         we restore the original TYPE_NAME and process the next
7208
         import. */
7209
      if (access_check || !decl)
7210
        {
7211
          TYPE_NAME (class_type) = type_name_copy;
7212
          continue;
7213
        }
7214
 
7215
      /* If the loaded class is accessible, we keep a tab on it to
7216
         detect and report multiple inclusions. */
7217
      if (IS_A_CLASSFILE_NAME (node))
7218
        {
7219
          if (seen_once < 0)
7220
            {
7221
              cl = TREE_PURPOSE (import);
7222
              seen_once = 1;
7223
            }
7224
          else if (seen_once >= 0)
7225
            {
7226
              tree location = (cl ? cl : TREE_PURPOSE (import));
7227
              tree package = (cl ? EXPR_WFL_NODE (cl) :
7228
                              EXPR_WFL_NODE (TREE_PURPOSE (import)));
7229
              seen_once++;
7230
              parse_error_context
7231
                (location,
7232
                 "Type %qs also potentially defined in package %qs",
7233
                 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7234
                 IDENTIFIER_POINTER (package));
7235
            }
7236
        }
7237
      to_return = access_check;
7238
    }
7239
 
7240
  if (seen_once == 1)
7241
    return to_return;
7242
  else
7243
    return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7244
}
7245
 
7246
static tree
7247
resolve_package (tree pkg, tree *next, tree *type_name)
7248
{
7249
  tree current;
7250
  tree decl = NULL_TREE;
7251
  *type_name = NULL_TREE;
7252
 
7253
  /* The trick is to determine when the package name stops and were
7254
     the name of something contained in the package starts. Then we
7255
     return a fully qualified name of what we want to get. */
7256
 
7257
  *next = EXPR_WFL_QUALIFICATION (pkg);
7258
 
7259
  /* Try to progressively construct a type name */
7260
  if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7261
    for (current = EXPR_WFL_QUALIFICATION (pkg);
7262
         current; current = TREE_CHAIN (current))
7263
      {
7264
        /* If we don't have what we're expecting, exit now. TYPE_NAME
7265
           will be null and the error caught later. */
7266
        if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7267
          break;
7268
        *type_name =
7269
          merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7270
        if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7271
          {
7272
            /* resolve_package should be used in a loop, hence we
7273
               point at this one to naturally process the next one at
7274
               the next iteration. */
7275
            *next = current;
7276
            break;
7277
          }
7278
      }
7279
  return decl;
7280
}
7281
 
7282
/* Check accessibility of inner class DECL, from the context ENCLOSING_DECL,
7283
   according to member access rules.  */
7284
 
7285
static bool
7286
inner_class_accessible (tree decl, tree enclosing_decl)
7287
{
7288
  tree enclosing_decl_type;
7289
 
7290
  enclosing_decl_type = TREE_TYPE (enclosing_decl);
7291
 
7292
  if (CLASS_PRIVATE (decl))
7293
    {
7294
      /* Access is permitted only within the body of the top-level
7295
         class in which DECL is declared. */
7296
      tree top_level = decl;
7297
      while (DECL_CONTEXT (top_level))
7298
        top_level = DECL_CONTEXT (top_level);
7299
      while (DECL_CONTEXT (enclosing_decl))
7300
        enclosing_decl = DECL_CONTEXT (enclosing_decl);
7301
      if (top_level == enclosing_decl)
7302
        return true;
7303
    }
7304
  else if (CLASS_PROTECTED (decl))
7305
    {
7306
      tree decl_context;
7307
      /* Access is permitted from within the same package... */
7308
      if (in_same_package (decl, enclosing_decl))
7309
        return true;
7310
 
7311
      /* ... or from within the body of a subtype of the context in which
7312
         DECL is declared. */
7313
      decl_context = DECL_CONTEXT (decl);
7314
      while (enclosing_decl)
7315
        {
7316
          if (CLASS_INTERFACE (decl))
7317
            {
7318
              if (interface_of_p (TREE_TYPE (decl_context),
7319
                                  enclosing_decl_type))
7320
                return true;
7321
            }
7322
          else
7323
            {
7324
              /* Eww. The order of the arguments is different!! */
7325
              if (inherits_from_p (enclosing_decl_type,
7326
                                   TREE_TYPE (decl_context)))
7327
                return true;
7328
            }
7329
          enclosing_decl = DECL_CONTEXT (enclosing_decl);
7330
        }
7331
    }
7332
  else if (! CLASS_PUBLIC (decl))
7333
    {
7334
      /* Access is permitted only from within the same package as DECL. */
7335
      if (in_same_package (decl, enclosing_decl))
7336
        return true;
7337
    }
7338
  else
7339
    /* Class is public. */
7340
    return true;
7341
 
7342
  return false;
7343
}
7344
 
7345
/* Check accessibility of inner classes according to member access rules.
7346
   DECL is the inner class, ENCLOSING_DECL is the class from which the
7347
   access is being attempted. */
7348
 
7349
static void
7350
check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7351
{
7352
  const char *access;
7353
 
7354
  /* We don't issue an error message when CL is null. CL can be null
7355
     as a result of processing a JDEP crafted by source_start_java_method
7356
     for the purpose of patching its parm decl. But the error would
7357
     have been already trapped when fixing the method's signature.
7358
     DECL can also be NULL in case of earlier errors. */
7359
  if (!decl || !cl)
7360
    return;
7361
 
7362
  if (inner_class_accessible (decl, enclosing_decl))
7363
    return;
7364
 
7365
  if (CLASS_PRIVATE (decl))
7366
      access = "private";
7367
  else if (CLASS_PROTECTED (decl))
7368
      access = "protected";
7369
  else
7370
      access = "non-public";
7371
 
7372
  parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7373
                       (CLASS_INTERFACE (decl) ? "interface" : "class"),
7374
                       lang_printable_name (decl, 2), access);
7375
}
7376
 
7377
/* Accessibility check for top-level classes. If CLASS_NAME is in a
7378
   foreign package, it must be PUBLIC. Return 0 if no access
7379
   violations were found, 1 otherwise. If VERBOSE is true and an error
7380
   was found, it is reported and accounted for.  If CL is NULL then
7381
   look it up with THIS_DECL.  */
7382
 
7383
static int
7384
check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7385
{
7386
  tree type;
7387
 
7388
  if (!IDENTIFIER_CLASS_VALUE (class_name))
7389
    return 0;
7390
 
7391
  if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7392
    return 0;
7393
 
7394
  if (!CLASS_PUBLIC (TYPE_NAME (type)))
7395
    {
7396
      /* Access to a private class within the same package is
7397
         allowed. */
7398
      tree l, r;
7399
      split_qualified_name (&l, &r, class_name);
7400
      if (!QUALIFIED_P (class_name) && !ctxp->package)
7401
        /* Both in the empty package. */
7402
        return 0;
7403
      if (l == ctxp->package)
7404
        /* Both in the same package. */
7405
        return 0;
7406
 
7407
      if (verbose)
7408
        parse_error_context
7409
          (cl == NULL ? lookup_cl (this_decl): cl,
7410
           "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7411
           (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7412
           IDENTIFIER_POINTER (class_name));
7413
      return 1;
7414
    }
7415
  return 0;
7416
}
7417
 
7418
/* Local variable declaration. */
7419
 
7420
static void
7421
declare_local_variables (int modifier, tree type, tree vlist)
7422
{
7423
  tree decl, current, saved_type;
7424
  tree type_wfl = NULL_TREE;
7425
  int must_chain = 0;
7426
  int final_p = 0;
7427
 
7428
  /* Push a new block if statements were seen between the last time we
7429
     pushed a block and now. Keep a count of blocks to close */
7430
  if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7431
    {
7432
      tree b = enter_block ();
7433
      BLOCK_IS_IMPLICIT (b) = 1;
7434
    }
7435
 
7436
  if (modifier)
7437
    {
7438
      size_t i;
7439
      for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7440
        if (1 << i & modifier)
7441
          break;
7442
      if (modifier == ACC_FINAL)
7443
        final_p = 1;
7444
      else
7445
        {
7446
          parse_error_context
7447
            (ctxp->modifier_ctx [i],
7448
             "Only % is allowed as a local variables modifier");
7449
          return;
7450
        }
7451
    }
7452
 
7453
  /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7454
     hold the TYPE value if a new incomplete has to be created (as
7455
     opposed to being found already existing and reused). */
7456
  SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7457
 
7458
  /* If TYPE is fully resolved and we don't have a reference, make one */
7459
  PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7460
 
7461
  /* Go through all the declared variables */
7462
  for (current = vlist, saved_type = type; current;
7463
       current = TREE_CHAIN (current), type = saved_type)
7464
    {
7465
      tree other, real_type;
7466
      tree wfl  = TREE_PURPOSE (current);
7467
      tree name = EXPR_WFL_NODE (wfl);
7468
      tree init = TREE_VALUE (current);
7469
 
7470
      /* Process NAME, as it may specify extra dimension(s) for it */
7471
      type = build_array_from_name (type, type_wfl, name, &name);
7472
 
7473
      /* Variable redefinition check */
7474
      if ((other = lookup_name_in_blocks (name)))
7475
        {
7476
          variable_redefinition_error (wfl, name, TREE_TYPE (other),
7477
                                       DECL_SOURCE_LINE (other));
7478
          continue;
7479
        }
7480
 
7481
      /* Type adjustment. We may have just readjusted TYPE because
7482
         the variable specified more dimensions. Make sure we have
7483
         a reference if we can and don't have one already. */
7484
      PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7485
 
7486
      real_type = GET_REAL_TYPE (type);
7487
      /* Never layout this decl. This will be done when its scope
7488
         will be entered */
7489
      decl = build_decl (VAR_DECL, name, real_type);
7490
      MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7491
      DECL_FINAL (decl) = final_p;
7492
      BLOCK_CHAIN_DECL (decl);
7493
 
7494
      /* Don't try to use an INIT statement when an error was found */
7495
      if (init && java_error_count)
7496
        init = NULL_TREE;
7497
 
7498
      /* Remember it if this is an initialized-upon-declaration final
7499
         variable.  */
7500
      if (init && final_p)
7501
        {
7502
          DECL_LOCAL_FINAL_IUD (decl) = 1;
7503
        }
7504
 
7505
      /* Add the initialization function to the current function's code */
7506
      if (init)
7507
        {
7508
          /* Name might have been readjusted */
7509
          EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7510
          MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7511
          java_method_add_stmt (current_function_decl,
7512
                                build_debugable_stmt (EXPR_WFL_LINECOL (init),
7513
                                                      init));
7514
        }
7515
 
7516
      /* Setup dependency the type of the decl */
7517
      if (must_chain)
7518
        {
7519
          jdep *dep;
7520
          register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7521
          dep = CLASSD_LAST (ctxp->classd_list);
7522
          JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7523
        }
7524
    }
7525
  SOURCE_FRONTEND_DEBUG (("Defined locals"));
7526
}
7527
 
7528
/* Called during parsing. Build decls from argument list.  */
7529
 
7530
static void
7531
source_start_java_method (tree fndecl)
7532
{
7533
  tree tem;
7534
  tree parm_decl;
7535
  int i;
7536
 
7537
  if (!fndecl)
7538
    return;
7539
 
7540
  current_function_decl = fndecl;
7541
 
7542
  /* New scope for the function */
7543
  enter_block ();
7544
  for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7545
       tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7546
    {
7547
      tree type = TREE_VALUE (tem);
7548
      tree name = TREE_PURPOSE (tem);
7549
 
7550
      /* If type is incomplete. Create an incomplete decl and ask for
7551
         the decl to be patched later */
7552
      if (INCOMPLETE_TYPE_P (type))
7553
        {
7554
          jdep *jdep;
7555
          tree real_type = GET_REAL_TYPE (type);
7556
          parm_decl = build_decl (PARM_DECL, name, real_type);
7557
          type = obtain_incomplete_type (type);
7558
          register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7559
          jdep = CLASSD_LAST (ctxp->classd_list);
7560
          JDEP_MISC (jdep) = name;
7561
          JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7562
        }
7563
      else
7564
        parm_decl = build_decl (PARM_DECL, name, type);
7565
 
7566
      /* Remember if a local variable was declared final (via its
7567
         TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7568
      if (ARG_FINAL_P (tem))
7569
        {
7570
          MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7571
          DECL_FINAL (parm_decl) = 1;
7572
        }
7573
 
7574
      if (name == this_identifier_node)
7575
        DECL_ARTIFICIAL (parm_decl) = 1;
7576
 
7577
      BLOCK_CHAIN_DECL (parm_decl);
7578
    }
7579
  tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7580
  BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7581
    nreverse (tem);
7582
  DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7583
  DECL_MAX_LOCALS (current_function_decl) = i;
7584
}
7585
 
7586
/* Called during parsing. Creates an artificial method declaration.  */
7587
 
7588
static tree
7589
create_artificial_method (tree class, int flags, tree type,
7590
                          tree name, tree args)
7591
{
7592
  tree mdecl;
7593
  location_t save_location = input_location;
7594
 
7595
  input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7596
  mdecl = make_node (FUNCTION_TYPE);
7597
  TREE_TYPE (mdecl) = type;
7598
  TYPE_ARG_TYPES (mdecl) = args;
7599
  /* We used to compute the signature of MDECL here and then use
7600
     add_method(), but that failed because our caller might modify
7601
     the type of the returned method, which trashes the cache in
7602
     get_type_from_signature().  */
7603
  mdecl = add_method_1 (class, flags, name, mdecl);
7604
  input_location = save_location;
7605
  DECL_ARTIFICIAL (mdecl) = 1;
7606
  return mdecl;
7607
}
7608
 
7609
/* Starts the body if an artificial method.  */
7610
 
7611
static void
7612
start_artificial_method_body (tree mdecl)
7613
{
7614
#ifdef USE_MAPPED_LOCATION
7615
  DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7616
  DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7617
#else
7618
  DECL_SOURCE_LINE (mdecl) = 1;
7619
  DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7620
#endif
7621
  source_start_java_method (mdecl);
7622
  enter_block ();
7623
}
7624
 
7625
static void
7626
end_artificial_method_body (tree mdecl)
7627
{
7628
  /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7629
     It has to be evaluated first. (if mdecl is current_function_decl,
7630
     we have an undefined behavior if no temporary variable is used.) */
7631
  tree b = exit_block ();
7632
  BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7633
  exit_block ();
7634
}
7635
 
7636
/* Dump a tree of some kind.  This is a convenience wrapper for the
7637
   dump_* functions in tree-dump.c.  */
7638
static void
7639
dump_java_tree (enum tree_dump_index phase, tree t)
7640
{
7641
  FILE *stream;
7642
  int flags;
7643
 
7644
  stream = dump_begin (phase, &flags);
7645
  flags |= TDF_SLIM;
7646
  if (stream)
7647
    {
7648
      dump_node (t, flags, stream);
7649
      dump_end (phase, stream);
7650
    }
7651
}
7652
 
7653
/* Terminate a function and expand its body.  */
7654
 
7655
static void
7656
source_end_java_method (void)
7657
{
7658
  tree fndecl = current_function_decl;
7659
 
7660
  if (!fndecl)
7661
    return;
7662
 
7663
  java_parser_context_save_global ();
7664
#ifdef USE_MAPPED_LOCATION
7665
  input_location = ctxp->last_ccb_indent1;
7666
#else
7667
  input_line = ctxp->last_ccb_indent1;
7668
#endif
7669
 
7670
  /* Turn function bodies with only a NOP expr null, so they don't get
7671
     generated at all and we won't get warnings when using the -W
7672
     -Wall flags. */
7673
  if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7674
    BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7675
 
7676
  if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7677
      && ! flag_emit_class_files)
7678
    finish_method (fndecl);
7679
 
7680
  current_function_decl = NULL_TREE;
7681
  java_parser_context_restore_global ();
7682
  current_function_decl = NULL_TREE;
7683
}
7684
 
7685
/* Record EXPR in the current function block. Complements compound
7686
   expression second operand if necessary.  */
7687
 
7688
tree
7689
java_method_add_stmt (tree fndecl, tree expr)
7690
{
7691
  if (!GET_CURRENT_BLOCK (fndecl))
7692
    return NULL_TREE;
7693
  return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7694
}
7695
 
7696
static tree
7697
add_stmt_to_block (tree b, tree type, tree stmt)
7698
{
7699
  tree body = BLOCK_EXPR_BODY (b), c;
7700
 
7701
  if (java_error_count)
7702
    return body;
7703
 
7704
  if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7705
    return body;
7706
 
7707
  BLOCK_EXPR_BODY (b) = c;
7708
  TREE_SIDE_EFFECTS (c) = 1;
7709
  return c;
7710
}
7711
 
7712
/* Lays out the methods for the classes seen so far.  */
7713
 
7714
void
7715
java_layout_seen_class_methods (void)
7716
{
7717
  tree previous_list = all_class_list;
7718
  tree end = NULL_TREE;
7719
  tree current;
7720
 
7721
  while (1)
7722
    {
7723
      for (current = previous_list;
7724
           current != end; current = TREE_CHAIN (current))
7725
        {
7726
          tree decl = TREE_VALUE (current);
7727
          tree cls = TREE_TYPE (decl);
7728
 
7729
          input_location = DECL_SOURCE_LOCATION (decl);
7730
 
7731
          if (! CLASS_LOADED_P (cls))
7732
            load_class (cls, 0);
7733
 
7734
          layout_class_methods (cls);
7735
        }
7736
 
7737
      /* Note that new classes might have been added while laying out
7738
         methods, changing the value of all_class_list.  */
7739
 
7740
      if (previous_list != all_class_list)
7741
        {
7742
          end = previous_list;
7743
          previous_list = all_class_list;
7744
        }
7745
      else
7746
        break;
7747
    }
7748
}
7749
 
7750
static GTY(()) tree stop_reordering;
7751
void
7752
java_reorder_fields (void)
7753
{
7754
  tree current;
7755
 
7756
  for (current = gclass_list; current; current = TREE_CHAIN (current))
7757
    {
7758
      output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7759
 
7760
      if (current_class == stop_reordering)
7761
        break;
7762
 
7763
      /* Reverse the fields, but leave the dummy field in front.
7764
         Fields are already ordered for Object and Class */
7765
      if (TYPE_FIELDS (current_class) && current_class != object_type_node
7766
          && current_class != class_type_node)
7767
      {
7768
        /* If the dummy field is there, reverse the right fields and
7769
           just layout the type for proper fields offset */
7770
        if (!DECL_NAME (TYPE_FIELDS (current_class)))
7771
          {
7772
            tree fields = TYPE_FIELDS (current_class);
7773
            /* This works around a problem where on some platforms,
7774
               the field might be given its size incorrectly.  */
7775
            DECL_SIZE (fields) = NULL_TREE;
7776
            DECL_SIZE_UNIT (fields) = NULL_TREE;
7777
            TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7778
            TYPE_SIZE (current_class) = NULL_TREE;
7779
          }
7780
        /* We don't have a dummy field, we need to layout the class,
7781
           after having reversed the fields */
7782
        else
7783
          {
7784
            TYPE_FIELDS (current_class) =
7785
              nreverse (TYPE_FIELDS (current_class));
7786
            TYPE_SIZE (current_class) = NULL_TREE;
7787
          }
7788
      }
7789
    }
7790
  /* There are cases were gclass_list will be empty. */
7791
  if (gclass_list)
7792
    stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7793
}
7794
 
7795
/* Layout the methods of all classes loaded in one way or another.
7796
   Check methods of source parsed classes. Then reorder the
7797
   fields and layout the classes or the type of all source parsed
7798
   classes */
7799
 
7800
void
7801
java_layout_classes (void)
7802
{
7803
  tree current;
7804
  int save_error_count = java_error_count;
7805
 
7806
  /* Layout the methods of all classes seen so far */
7807
  java_layout_seen_class_methods ();
7808
  java_parse_abort_on_error ();
7809
  all_class_list = NULL_TREE;
7810
 
7811
  /* Then check the methods of all parsed classes */
7812
  for (current = gclass_list; current; current = TREE_CHAIN (current))
7813
    if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7814
      java_check_methods (TREE_VALUE (current));
7815
  java_parse_abort_on_error ();
7816
 
7817
  for (current = gclass_list; current; current = TREE_CHAIN (current))
7818
    {
7819
      output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7820
      layout_class (current_class);
7821
 
7822
      /* Error reported by the caller */
7823
      if (java_error_count)
7824
        return;
7825
    }
7826
 
7827
  /* We might have reloaded classes durign the process of laying out
7828
     classes for code generation. We must layout the methods of those
7829
     late additions, as constructor checks might use them */
7830
  java_layout_seen_class_methods ();
7831
  java_parse_abort_on_error ();
7832
}
7833
 
7834
/* Expand methods in the current set of classes remembered for
7835
   generation.  */
7836
 
7837
static void
7838
java_complete_expand_classes (void)
7839
{
7840
  tree current;
7841
 
7842
  for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7843
    if (!INNER_CLASS_DECL_P (current))
7844
      java_complete_expand_class (current);
7845
}
7846
 
7847
/* Expand the methods found in OUTER, starting first by OUTER's inner
7848
   classes, if any.  */
7849
 
7850
static void
7851
java_complete_expand_class (tree outer)
7852
{
7853
  tree inner_list;
7854
 
7855
  /* We need to go after all inner classes and start expanding them,
7856
     starting with most nested ones. We have to do that because nested
7857
     classes might add functions to outer classes */
7858
 
7859
  for (inner_list = DECL_INNER_CLASS_LIST (outer);
7860
       inner_list; inner_list = TREE_CHAIN (inner_list))
7861
    java_complete_expand_class (TREE_PURPOSE (inner_list));
7862
 
7863
  java_complete_expand_methods (outer);
7864
}
7865
 
7866
/* Expand methods registered in CLASS_DECL. The general idea is that
7867
   we expand regular methods first. This allows us get an estimate on
7868
   how outer context local alias fields are really used so we can add
7869
   to the constructor just enough code to initialize them properly (it
7870
   also lets us generate finit$ correctly.) Then we expand the
7871
   constructors and then .  */
7872
 
7873
static void
7874
java_complete_expand_methods (tree class_decl)
7875
{
7876
  tree clinit, decl, first_decl;
7877
 
7878
  output_class = current_class = TREE_TYPE (class_decl);
7879
 
7880
  /* Pre-expand  to figure whether we really need it or
7881
     not. If we do need it, we pre-expand the static fields so they're
7882
     ready to be used somewhere else.  will be fully expanded
7883
     after we processed the constructors. */
7884
  first_decl = TYPE_METHODS (current_class);
7885
  clinit = maybe_generate_pre_expand_clinit (current_class);
7886
 
7887
  /* Then generate finit$ (if we need to) because constructors will
7888
   try to use it.*/
7889
  if (TYPE_FINIT_STMT_LIST (current_class))
7890
    java_complete_expand_method (generate_finit (current_class));
7891
 
7892
  /* Then generate instinit$ (if we need to) because constructors will
7893
     try to use it. */
7894
  if (TYPE_II_STMT_LIST (current_class))
7895
    java_complete_expand_method (generate_instinit (current_class));
7896
 
7897
  /* Now do the constructors */
7898
  for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7899
    {
7900
      if (!DECL_CONSTRUCTOR_P (decl))
7901
        continue;
7902
      java_complete_expand_method (decl);
7903
    }
7904
 
7905
  /* First, do the ordinary methods. */
7906
  for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7907
    {
7908
      /* Ctors aren't part of this batch. */
7909
      if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7910
        continue;
7911
 
7912
      /* Skip abstract or native methods -- but do handle native
7913
         methods when generating JNI stubs.  */
7914
      if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7915
        {
7916
          DECL_FUNCTION_BODY (decl) = NULL_TREE;
7917
          continue;
7918
        }
7919
 
7920
      if (METHOD_NATIVE (decl))
7921
        {
7922
          tree body;
7923
          current_function_decl = decl;
7924
          body = build_jni_stub (decl);
7925
          BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7926
        }
7927
 
7928
      java_complete_expand_method (decl);
7929
    }
7930
 
7931
  /* If there is indeed a , fully expand it now */
7932
  if (clinit)
7933
    {
7934
      /* Prevent the use of `this' inside  */
7935
      ctxp->explicit_constructor_p = 1;
7936
      java_complete_expand_method (clinit);
7937
      ctxp->explicit_constructor_p = 0;
7938
    }
7939
 
7940
  /* We might have generated a class$ that we now want to expand */
7941
  if (TYPE_DOT_CLASS (current_class))
7942
    java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7943
 
7944
  /* Now verify constructor circularity (stop after the first one we
7945
     prove wrong.) */
7946
  if (!CLASS_INTERFACE (class_decl))
7947
    for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7948
      if (DECL_CONSTRUCTOR_P (decl)
7949
          && verify_constructor_circularity (decl, decl))
7950
        break;
7951
}
7952
 
7953
/* Attempt to create . Pre-expand static fields so they can be
7954
   safely used in some other methods/constructors.  */
7955
 
7956
static tree
7957
maybe_generate_pre_expand_clinit (tree class_type)
7958
{
7959
  tree current, mdecl;
7960
 
7961
  if (!TYPE_CLINIT_STMT_LIST (class_type))
7962
    return NULL_TREE;
7963
 
7964
  /* Go through all static fields and pre expand them */
7965
  for (current = TYPE_FIELDS (class_type); current;
7966
       current = TREE_CHAIN (current))
7967
    if (FIELD_STATIC (current))
7968
      build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7969
 
7970
  /* Then build the  method */
7971
  mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7972
                                    clinit_identifier_node, end_params_node);
7973
  layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7974
                       mdecl, NULL_TREE);
7975
  start_artificial_method_body (mdecl);
7976
 
7977
  /* We process the list of assignment we produced as the result of
7978
     the declaration of initialized static field and add them as
7979
     statement to the  method. */
7980
  for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7981
       current = TREE_CHAIN (current))
7982
    {
7983
      tree stmt = current;
7984
      /* We build the assignment expression that will initialize the
7985
         field to its value. There are strict rules on static
7986
         initializers (8.5). FIXME */
7987
      if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7988
        stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7989
      java_method_add_stmt (mdecl, stmt);
7990
    }
7991
 
7992
  end_artificial_method_body (mdecl);
7993
 
7994
  /* Now we want to place  as the last method (because we need
7995
     it at least for interface so that it doesn't interfere with the
7996
     dispatch table based lookup. */
7997
  if (TREE_CHAIN (TYPE_METHODS (class_type)))
7998
    {
7999
      current = TREE_CHAIN (TYPE_METHODS (class_type));
8000
      TYPE_METHODS (class_type) = current;
8001
 
8002
      while (TREE_CHAIN (current))
8003
        current = TREE_CHAIN (current);
8004
 
8005
      TREE_CHAIN (current) = mdecl;
8006
      TREE_CHAIN (mdecl) = NULL_TREE;
8007
    }
8008
 
8009
  return mdecl;
8010
}
8011
 
8012
/* Analyzes a method body and look for something that isn't a
8013
   MODIFY_EXPR with a constant value.  */
8014
 
8015
static int
8016
analyze_clinit_body (tree this_class, tree bbody)
8017
{
8018
  while (bbody)
8019
    switch (TREE_CODE (bbody))
8020
      {
8021
      case BLOCK:
8022
        bbody = BLOCK_EXPR_BODY (bbody);
8023
        break;
8024
 
8025
      case EXPR_WITH_FILE_LOCATION:
8026
        bbody = EXPR_WFL_NODE (bbody);
8027
        break;
8028
 
8029
      case COMPOUND_EXPR:
8030
        if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
8031
          return 1;
8032
        bbody = TREE_OPERAND (bbody, 1);
8033
        break;
8034
 
8035
      case MODIFY_EXPR:
8036
        /* If we're generating to class file and we're dealing with an
8037
           array initialization, we return 1 to keep  */
8038
        if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
8039
            && flag_emit_class_files)
8040
          return 1;
8041
 
8042
        /* There are a few cases where we're required to keep
8043
           :
8044
           - If this is an assignment whose operand is not constant,
8045
           - If this is an assignment to a non-initialized field,
8046
           - If this field is not a member of the current class.
8047
        */
8048
        return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8049
                || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8050
                || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8051
 
8052
      default:
8053
        return 1;
8054
      }
8055
  return 0;
8056
}
8057
 
8058
 
8059
/* See whether we could get rid of . Criteria are: all static
8060
   final fields have constant initial values and the body of 
8061
   is empty. Return 1 if  was discarded, 0 otherwise. */
8062
 
8063
static int
8064
maybe_yank_clinit (tree mdecl)
8065
{
8066
  tree type, current;
8067
  tree fbody, bbody;
8068
 
8069
  if (!DECL_CLINIT_P (mdecl))
8070
    return 0;
8071
 
8072
  /* If the body isn't empty, then we keep . Note that if
8073
     we're emitting classfiles, this isn't enough not to rule it
8074
     out. */
8075
  fbody = DECL_FUNCTION_BODY (mdecl);
8076
  bbody = BLOCK_EXPR_BODY (fbody);
8077
  if (bbody && bbody != error_mark_node)
8078
    bbody = BLOCK_EXPR_BODY (bbody);
8079
  else
8080
    return 0;
8081
  if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8082
    return 0;
8083
 
8084
  type = DECL_CONTEXT (mdecl);
8085
  current = TYPE_FIELDS (type);
8086
 
8087
  for (current = (current ? TREE_CHAIN (current) : current);
8088
       current; current = TREE_CHAIN (current))
8089
    {
8090
      tree f_init;
8091
 
8092
      /* We're not interested in non-static fields.  */
8093
      if (!FIELD_STATIC (current))
8094
        continue;
8095
 
8096
      /* Nor in fields without initializers. */
8097
      f_init = DECL_INITIAL (current);
8098
      if (f_init == NULL_TREE)
8099
        continue;
8100
 
8101
      /* Anything that isn't String or a basic type is ruled out -- or
8102
         if we know how to deal with it (when doing things natively) we
8103
         should generated an empty  so that SUID are computed
8104
         correctly. */
8105
      if (! JSTRING_TYPE_P (TREE_TYPE (current))
8106
          && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8107
        return 0;
8108
 
8109
      if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8110
        return 0;
8111
    }
8112
 
8113
  /* Now we analyze the method body and look for something that
8114
     isn't a MODIFY_EXPR */
8115
  if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8116
    return 0;
8117
 
8118
  /* Get rid of  in the class' list of methods */
8119
  if (TYPE_METHODS (type) == mdecl)
8120
    TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8121
  else
8122
    for (current = TYPE_METHODS (type); current;
8123
         current = TREE_CHAIN (current))
8124
      if (TREE_CHAIN (current) == mdecl)
8125
        {
8126
          TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8127
          break;
8128
        }
8129
 
8130
  return 1;
8131
}
8132
 
8133
/* Install the argument from MDECL. Suitable to completion and
8134
   expansion of mdecl's body.  */
8135
 
8136
void
8137
start_complete_expand_method (tree mdecl)
8138
{
8139
  tree tem;
8140
 
8141
  pushlevel (1);                /* Prepare for a parameter push */
8142
  tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8143
  DECL_ARGUMENTS (mdecl) = tem;
8144
 
8145
  for (; tem; tem = TREE_CHAIN (tem))
8146
    {
8147
      /* TREE_CHAIN (tem) will change after pushdecl. */
8148
      tree next = TREE_CHAIN (tem);
8149
      tree type = TREE_TYPE (tem);
8150
      if (targetm.calls.promote_prototypes (type)
8151
          && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8152
          && INTEGRAL_TYPE_P (type))
8153
        type = integer_type_node;
8154
      DECL_ARG_TYPE (tem) = type;
8155
      layout_decl (tem, 0);
8156
      pushdecl (tem);
8157
      /* Re-install the next so that the list is kept and the loop
8158
         advances. */
8159
      TREE_CHAIN (tem) = next;
8160
    }
8161
  pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8162
  input_location = DECL_SOURCE_LOCATION (mdecl);
8163
  build_result_decl (mdecl);
8164
}
8165
 
8166
 
8167
/* Complete and expand a method.  */
8168
 
8169
static void
8170
java_complete_expand_method (tree mdecl)
8171
{
8172
  tree fbody, block_body, exception_copy;
8173
 
8174
  current_function_decl = mdecl;
8175
  /* Fix constructors before expanding them */
8176
  if (DECL_CONSTRUCTOR_P (mdecl))
8177
    fix_constructors (mdecl);
8178
 
8179
  /* Expand functions that have a body */
8180
  if (!DECL_FUNCTION_BODY (mdecl))
8181
    return;
8182
 
8183
  fbody = DECL_FUNCTION_BODY (mdecl);
8184
  block_body = BLOCK_EXPR_BODY (fbody);
8185
  exception_copy = NULL_TREE;
8186
 
8187
  current_function_decl = mdecl;
8188
 
8189
  if (! quiet_flag)
8190
    fprintf (stderr, " [%s.",
8191
             lang_printable_name (DECL_CONTEXT (mdecl), 0));
8192
  announce_function (mdecl);
8193
  if (! quiet_flag)
8194
    fprintf (stderr, "]");
8195
 
8196
  /* Prepare the function for tree completion */
8197
  start_complete_expand_method (mdecl);
8198
 
8199
  /* Install the current this */
8200
  current_this = (!METHOD_STATIC (mdecl) ?
8201
                  BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8202
 
8203
  /* Install exceptions thrown with `throws' */
8204
  PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8205
 
8206
  if (block_body != NULL_TREE)
8207
    {
8208
      block_body = java_complete_tree (block_body);
8209
 
8210
      /* Before we check initialization, attached all class initialization
8211
         variable to the block_body */
8212
      htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8213
                     attach_init_test_initialization_flags, block_body);
8214
 
8215
      if (! METHOD_NATIVE (mdecl))
8216
        {
8217
          check_for_initialization (block_body, mdecl);
8218
 
8219
          /* Go through all the flags marking the initialization of
8220
             static variables and see whether they're definitively
8221
             assigned, in which case the type is remembered as
8222
             definitively initialized in MDECL. */
8223
          if (STATIC_CLASS_INIT_OPT_P ())
8224
            {
8225
              /* Always register the context as properly initialized in
8226
                 MDECL. This used with caution helps removing extra
8227
                 initialization of self. */
8228
              if (METHOD_STATIC (mdecl))
8229
                {
8230
                  *(htab_find_slot
8231
                    (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8232
                     DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8233
                }
8234
            }
8235
        }
8236
      ctxp->explicit_constructor_p = 0;
8237
    }
8238
 
8239
  BLOCK_EXPR_BODY (fbody) = block_body;
8240
 
8241
  /* If we saw a return but couldn't evaluate it properly, we'll have
8242
     an error_mark_node here. */
8243
  if (block_body != error_mark_node
8244
      && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8245
      && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8246
    missing_return_error (current_function_decl);
8247
 
8248
  /* See if we can get rid of  if MDECL happens to be  */
8249
  maybe_yank_clinit (mdecl);
8250
 
8251
  /* Pop the current level, with special measures if we found errors. */
8252
  if (java_error_count)
8253
    pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8254
  poplevel (1, 0, 1);
8255
 
8256
  /* Pop the exceptions and sanity check */
8257
  POP_EXCEPTIONS();
8258
  if (currently_caught_type_list)
8259
    abort ();
8260
}
8261
 
8262
/* For with each class for which there's code to generate. */
8263
 
8264
static void
8265
java_expand_method_bodies (tree class)
8266
{
8267
  tree decl;
8268
  for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8269
    {
8270
      tree block;
8271
 
8272
      if (! DECL_FUNCTION_BODY (decl))
8273
        continue;
8274
 
8275
      current_function_decl = decl;
8276
 
8277
      block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8278
 
8279
      /* Save the function body for gimplify and inlining.  */
8280
      DECL_SAVED_TREE (decl) = block;
8281
 
8282
      /* It's time to assign the variable flagging static class
8283
         initialization based on which classes invoked static methods
8284
         are definitely initializing. This should be flagged. */
8285
      if (STATIC_CLASS_INIT_OPT_P ())
8286
        {
8287
          tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8288
          for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8289
            {
8290
              /* Executed for each statement calling a static function.
8291
                 LIST is a TREE_LIST whose PURPOSE is the called function
8292
                 and VALUE is a compound whose second operand can be patched
8293
                 with static class initialization flag assignments.  */
8294
 
8295
              tree called_method = TREE_PURPOSE (list);
8296
              tree compound = TREE_VALUE (list);
8297
              tree assignment_compound_list
8298
                = build_tree_list (called_method, NULL);
8299
 
8300
              /* For each class definitely initialized in
8301
                 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8302
                 assignment to the class initialization flag. */
8303
              htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8304
                             emit_test_initialization,
8305
                             assignment_compound_list);
8306
 
8307
              if (TREE_VALUE (assignment_compound_list))
8308
                TREE_OPERAND (compound, 1)
8309
                  = TREE_VALUE (assignment_compound_list);
8310
            }
8311
        }
8312
 
8313
      /* Expand the function body.  */
8314
      source_end_java_method ();
8315
    }
8316
}
8317
 
8318
 
8319
 
8320
/* This section of the code deals with accessing enclosing context
8321
   fields either directly by using the relevant access to this$ or
8322
   by invoking an access method crafted for that purpose.  */
8323
 
8324
/* Build the necessary access across nested class boundaries.
8325
   This routine could be optimized to cache previous result
8326
   (decl, current_class and returned access).  When an access method
8327
   needs to be generated, it always takes the form of a read.  It might
8328
   be later turned into a write by calling nested_field_access_fix.  */
8329
 
8330
static tree
8331
build_nested_field_access (tree id, tree decl)
8332
{
8333
  tree access = NULL_TREE;
8334
  tree ctx = NULL_TREE;
8335
  tree decl_ctx = DECL_CONTEXT (decl);
8336
  bool is_static = FIELD_STATIC (decl);
8337
 
8338
  if (DECL_CONTEXT (TYPE_NAME (current_class)))
8339
    ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8340
 
8341
  /* For non-static fields, if the immediate enclosing context of the
8342
     current class is the field decl's class or inherits from it,
8343
     build the access as `this$.'.  Note that we will break
8344
     the `private' barrier if we're not emitting bytecodes.  */
8345
  if (!is_static
8346
      && ctx
8347
      && (ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8348
      && (!FIELD_PRIVATE (decl) || !flag_emit_class_files))
8349
    {
8350
      tree thisn = build_current_thisn (current_class);
8351
      access = make_qualified_primary (build_wfl_node (thisn),
8352
                                       id, EXPR_WFL_LINECOL (id));
8353
    }
8354
  /* Otherwise, generate and use accessor methods for the field as
8355
     needed.  */
8356
  else
8357
    {
8358
      int lc = EXPR_WFL_LINECOL (id);
8359
 
8360
      /* Now we chain the required number of calls to the access$0 to
8361
         get a hold to the enclosing instance we need for a non-static
8362
         field, and then we build the field access. */
8363
      if (!is_static)
8364
        access = build_access_to_thisn (current_class, decl_ctx, lc);
8365
 
8366
      /* If the field is private and we're generating bytecode, then
8367
         we generate an access method.  */
8368
      if (FIELD_PRIVATE (decl) && flag_emit_class_files)
8369
        {
8370
          tree name = build_nested_field_access_methods (decl);
8371
          access = build_nested_field_access_expr (lc, decl_ctx,
8372
                                                   name, access, NULL_TREE);
8373
        }
8374
      /* Otherwise we use `access$(this$). ... access$(this$).'
8375
         for non-static fields.
8376
         Once again we break the `private' access rule from a foreign
8377
         class.  */
8378
      else if (is_static)
8379
        {
8380
          tree class_name = DECL_NAME (TYPE_NAME (decl_ctx));
8381
          access
8382
            = make_qualified_primary (build_wfl_node (class_name), id, lc);
8383
        }
8384
      else
8385
        access = make_qualified_primary (access, id, lc);
8386
    }
8387
 
8388
  return resolve_expression_name (access, NULL);
8389
}
8390
 
8391
/* Return a nonzero value if DECL describes a member access across nested
8392
   class boundaries.  That is, DECL is in a class that either encloses,
8393
   is enclosed by or shares a common enclosing class with the class
8394
   TYPE.  */
8395
 
8396
static int
8397
nested_member_access_p (tree type, tree decl)
8398
{
8399
  bool is_static = false;
8400
  tree decl_type = DECL_CONTEXT (decl);
8401
  tree type_root, decl_type_root;
8402
 
8403
  if (decl_type == type
8404
      || (TREE_CODE (decl) != FIELD_DECL
8405
          && TREE_CODE (decl) != VAR_DECL
8406
          && TREE_CODE (decl) != FUNCTION_DECL))
8407
    return 0;
8408
 
8409
  if (!INNER_CLASS_TYPE_P (type)
8410
      && !(TREE_CODE (decl_type) == RECORD_TYPE
8411
           && INNER_CLASS_TYPE_P (decl_type)))
8412
    return 0;
8413
 
8414
  is_static = (TREE_CODE (decl) == FUNCTION_DECL)
8415
              ? METHOD_STATIC (decl)
8416
              : FIELD_STATIC (decl);
8417
 
8418
  /* If TYPE extends the declaration context of the non-static
8419
     member we're trying to access, then this isn't a nested member
8420
     access we need to worry about.  */
8421
  if (!is_static && inherits_from_p (type, decl_type))
8422
    return 0;
8423
 
8424
  for (type_root = type;
8425
       DECL_CONTEXT (TYPE_NAME (type_root));
8426
       type_root = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type_root))))
8427
    {
8428
      if (type_root == decl_type)
8429
        return 1;
8430
    }
8431
 
8432
  if (TREE_CODE (decl_type) == RECORD_TYPE
8433
      && INNER_CLASS_TYPE_P (decl_type))
8434
    {
8435
      for (decl_type_root = decl_type;
8436
           DECL_CONTEXT (TYPE_NAME (decl_type_root));
8437
           decl_type_root
8438
             = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (decl_type_root))))
8439
        {
8440
          if (decl_type_root == type)
8441
            return 1;
8442
        }
8443
    }
8444
  else
8445
    decl_type_root = decl_type;
8446
 
8447
  if (type_root == decl_type_root)
8448
    return 1;
8449
 
8450
  /* Before we give up, see whether it is a non-static field
8451
     inherited from the enclosing context we are considering.  */
8452
  if (!DECL_CONTEXT (TYPE_NAME (type_root))
8453
      && !is_static
8454
      && inherits_from_p (type_root, decl_type))
8455
    return 1;
8456
 
8457
  return 0;
8458
}
8459
 
8460
/* Return a nonzero value if NODE represents a cross-nested-class
8461
   access that has already been expanded.  As a side effect, it returns
8462
   the name of the field being accessed and the argument passed to the
8463
   access function, suitable for a regeneration of the access method
8464
   call if necessary.  */
8465
 
8466
static int
8467
nested_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8468
                                tree *arg)
8469
{
8470
  int identified = 0;
8471
 
8472
  if (TREE_CODE (node) != CALL_EXPR)
8473
    return 0;
8474
 
8475
  /* Well, GCJ generates slightly different tree nodes when compiling
8476
     to native or bytecodes.  It's the case for function calls.  */
8477
 
8478
  if (flag_emit_class_files
8479
      && TREE_CODE (node) == CALL_EXPR
8480
      && NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8481
    identified = 1;
8482
  else if (!flag_emit_class_files)
8483
    {
8484
      node = TREE_OPERAND (node, 0);
8485
 
8486
      if (node && TREE_OPERAND (node, 0)
8487
          && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8488
        {
8489
          node = TREE_OPERAND (node, 0);
8490
          if (TREE_OPERAND (node, 0)
8491
              && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8492
              && (NESTED_FIELD_ACCESS_IDENTIFIER_P
8493
                  (DECL_NAME (TREE_OPERAND (node, 0)))))
8494
            identified = 1;
8495
        }
8496
    }
8497
 
8498
  if (identified && name && arg_type && arg)
8499
    {
8500
      tree argument = TREE_OPERAND (node, 1);
8501
      *name = DECL_NAME (TREE_OPERAND (node, 0));
8502
 
8503
      /* The accessors for static fields do not take in a this$ argument,
8504
         so we take the class name from the accessor's context instead.  */
8505
      if (argument)
8506
        {
8507
          *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8508
          *arg = TREE_VALUE (argument);
8509
        }
8510
      else
8511
        {
8512
          *arg_type = DECL_CONTEXT (TREE_OPERAND (node, 0));
8513
          *arg = NULL_TREE;
8514
        }
8515
    }
8516
  return identified;
8517
}
8518
 
8519
/* Detect in NODE cross-nested-class field read access and
8520
   transform it into a write with RHS as an argument.  This function
8521
   is called from the java_complete_lhs when an assignment to a LHS can
8522
   be identified.  */
8523
 
8524
static tree
8525
nested_field_access_fix (tree wfl, tree node, tree rhs)
8526
{
8527
  tree name, arg_type, arg;
8528
 
8529
  if (nested_field_expanded_access_p (node, &name, &arg_type, &arg))
8530
    {
8531
      node = build_nested_field_access_expr (EXPR_WFL_LINECOL (wfl),
8532
                                             arg_type, name, arg, rhs);
8533
      return java_complete_tree (node);
8534
    }
8535
  return NULL_TREE;
8536
}
8537
 
8538
/* Construct the expression that calls an access method:
8539
     .access$( [, ]);
8540
 
8541
   ARG2 can be NULL and will be omitted in that case. It will denote a
8542
   read access.  */
8543
 
8544
static tree
8545
build_nested_field_access_expr (int lc, tree type, tree access_method_name,
8546
                                tree arg1, tree arg2)
8547
{
8548
  tree args, cn, access;
8549
 
8550
  if (arg1)
8551
    args = build_tree_list (NULL_TREE, arg1);
8552
  else
8553
    args = NULL_TREE;
8554
 
8555
  if (arg2)
8556
    {
8557
      if (args)
8558
        args = tree_cons (NULL_TREE, arg2, args);
8559
      else
8560
        args = build_tree_list (NULL_TREE, arg2);
8561
    }
8562
 
8563
  access
8564
    = build_method_invocation (build_wfl_node (access_method_name), args);
8565
  cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8566
 
8567
  return make_qualified_primary (cn, access, lc);
8568
}
8569
 
8570
/* Build the name of a synthetic accessor used to access class members
8571
   across nested class boundaries.  */
8572
 
8573
static tree
8574
build_new_access_id (void)
8575
{
8576
  static int access_n_counter = 1;
8577
  char buffer [128];
8578
 
8579
  sprintf (buffer, "access$%d", access_n_counter++);
8580
  return get_identifier (buffer);
8581
}
8582
 
8583
/* Create the static access functions for the cross-nested-class field DECL.
8584
   We define a read:
8585
     TREE_TYPE () access$ (DECL_CONTEXT () inst$) {
8586
       return inst$.field;
8587
     }
8588
   and a write access:
8589
     TREE_TYPE () access$ (DECL_CONTEXT () inst$,
8590
                                     TREE_TYPE () value$) {
8591
       return inst$.field = value$;
8592
     }
8593
   For static fields, these methods are generated without the instance
8594
   parameter.
8595
   We should have a usage flag on the DECL so we can lazily turn the ones
8596
   we're using for code generation.  FIXME.
8597
*/
8598
 
8599
static tree
8600
build_nested_field_access_methods (tree decl)
8601
{
8602
  tree id, args, stmt, mdecl, class_name = NULL_TREE;
8603
  bool is_static = FIELD_STATIC (decl);
8604
 
8605
  if (FIELD_NESTED_ACCESS_P (decl))
8606
    return FIELD_NESTED_ACCESS (decl);
8607
 
8608
  MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8609
 
8610
  /* Create the identifier and a function named after it.  */
8611
  id = build_new_access_id ();
8612
 
8613
  /* The identifier is marked as bearing the name of a generated write
8614
     access function for outer field accessed from inner classes.  */
8615
  NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8616
 
8617
  /* Create the read access.  */
8618
  if (!is_static)
8619
    {
8620
      args = build_tree_list (inst_id,
8621
                              build_pointer_type (DECL_CONTEXT (decl)));
8622
      TREE_CHAIN (args) = end_params_node;
8623
      stmt = make_qualified_primary (build_wfl_node (inst_id),
8624
                                     build_wfl_node (DECL_NAME (decl)), 0);
8625
    }
8626
  else
8627
    {
8628
      args = end_params_node;
8629
      class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
8630
      stmt = make_qualified_primary (build_wfl_node (class_name),
8631
                                     build_wfl_node (DECL_NAME (decl)), 0);
8632
    }
8633
  stmt = build_return (0, stmt);
8634
  mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8635
                                            TREE_TYPE (decl), id, args, stmt);
8636
  DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8637
 
8638
  /* Create the write access method.  No write access for final variable */
8639
  if (!FIELD_FINAL (decl))
8640
    {
8641
      if (!is_static)
8642
        {
8643
          args = build_tree_list (inst_id,
8644
                                  build_pointer_type (DECL_CONTEXT (decl)));
8645
          TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8646
          TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8647
          stmt = make_qualified_primary (build_wfl_node (inst_id),
8648
                                         build_wfl_node (DECL_NAME (decl)),
8649
                                         0);
8650
        }
8651
      else
8652
        {
8653
          args = build_tree_list (wpv_id, TREE_TYPE (decl));
8654
          TREE_CHAIN (args) = end_params_node;
8655
          stmt = make_qualified_primary (build_wfl_node (class_name),
8656
                                         build_wfl_node (DECL_NAME (decl)),
8657
                                         0);
8658
        }
8659
      stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8660
                                                build_wfl_node (wpv_id)));
8661
      mdecl = build_nested_field_access_method (DECL_CONTEXT (decl),
8662
                                                TREE_TYPE (decl), id,
8663
                                                args, stmt);
8664
    }
8665
  DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8666
 
8667
  /* Return the access name */
8668
  return FIELD_NESTED_ACCESS (decl) = id;
8669
}
8670
 
8671
/* Build a field access method NAME.  */
8672
 
8673
static tree
8674
build_nested_field_access_method (tree class, tree type, tree name,
8675
                                  tree args, tree body)
8676
{
8677
  tree saved_current_function_decl, mdecl;
8678
 
8679
  /* Create the method */
8680
  mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8681
  fix_method_argument_names (args, mdecl);
8682
  layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8683
 
8684
  /* Attach the method body. */
8685
  saved_current_function_decl = current_function_decl;
8686
  start_artificial_method_body (mdecl);
8687
  java_method_add_stmt (mdecl, body);
8688
  end_artificial_method_body (mdecl);
8689
  current_function_decl = saved_current_function_decl;
8690
 
8691
  return mdecl;
8692
}
8693
 
8694
 
8695
/* This section deals with building access function necessary for
8696
   certain kinds of method invocation across nested class boundaries.  */
8697
 
8698
static tree
8699
build_nested_method_access_method (tree decl)
8700
{
8701
  tree saved_current_function_decl, mdecl;
8702
  tree args = NULL_TREE, call_args = NULL_TREE;
8703
  tree carg, id, body, class;
8704
  char buffer [80];
8705
  int parm_id_count = 0;
8706
 
8707
  /* Test this abort with an access to a private field */
8708
  if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8709
    abort ();
8710
 
8711
  /* Check the cache first */
8712
  if (DECL_FUNCTION_INNER_ACCESS (decl))
8713
    return DECL_FUNCTION_INNER_ACCESS (decl);
8714
 
8715
  class = DECL_CONTEXT (decl);
8716
 
8717
  /* Obtain an access identifier and mark it */
8718
  id = build_new_access_id ();
8719
  NESTED_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8720
 
8721
  carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8722
  /* Create the arguments, as much as the original */
8723
  for (; carg && carg != end_params_node;
8724
       carg = TREE_CHAIN (carg))
8725
    {
8726
      sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8727
      args = chainon (args, build_tree_list (get_identifier (buffer),
8728
                                             TREE_VALUE (carg)));
8729
    }
8730
  args = chainon (args, end_params_node);
8731
 
8732
  /* Create the method */
8733
  mdecl = create_artificial_method (class, ACC_STATIC,
8734
                                    TREE_TYPE (TREE_TYPE (decl)), id, args);
8735
  layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8736
  /* There is a potential bug here. We should be able to use
8737
     fix_method_argument_names, but then arg names get mixed up and
8738
     eventually a constructor will have its this$0 altered and the
8739
     outer context won't be assignment properly. The testcase is
8740
     stub.java FIXME */
8741
  TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8742
 
8743
  /* Attach the method body. */
8744
  saved_current_function_decl = current_function_decl;
8745
  start_artificial_method_body (mdecl);
8746
 
8747
  /* The actual method invocation uses the same args. When invoking a
8748
     static methods that way, we don't want to skip the first argument.  */
8749
  carg = args;
8750
  if (!METHOD_STATIC (decl))
8751
    carg = TREE_CHAIN (carg);
8752
  for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8753
    call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8754
                           call_args);
8755
 
8756
  body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8757
                                  call_args);
8758
  if (!METHOD_STATIC (decl))
8759
    body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8760
                                   body, 0);
8761
  if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8762
    body = build_return (0, body);
8763
  java_method_add_stmt (mdecl,body);
8764
  end_artificial_method_body (mdecl);
8765
  current_function_decl = saved_current_function_decl;
8766
 
8767
  /* Back tag the access function so it know what it accesses.  */
8768
  DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8769
 
8770
  /* Tag the current method so it knows it has an access generated.  */
8771
  return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8772
}
8773
 
8774
 
8775
/* This section of the code deals with building expressions to access
8776
   the enclosing instance of an inner class. The enclosing instance is
8777
   kept in a generated field called this$, with  being the
8778
   inner class nesting level (starting from 0.)  */
8779
 
8780
/* Build an access to a given this$, always chaining access call to
8781
   others. Access methods to this$ are build on the fly if
8782
   necessary. This CAN'T be used to solely access this$ from
8783
   this$ (which alway yield to special cases and optimization, see
8784
   for example build_nested_field_access).  */
8785
 
8786
static tree
8787
build_access_to_thisn (tree from, tree to, int lc)
8788
{
8789
  tree access = NULL_TREE;
8790
 
8791
  while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8792
    {
8793
      if (!access)
8794
        {
8795
          access = build_current_thisn (from);
8796
          access = build_wfl_node (access);
8797
        }
8798
      else
8799
        {
8800
          tree access0_wfl, cn;
8801
 
8802
          maybe_build_thisn_access_method (from);
8803
          access0_wfl = build_wfl_node (access0_identifier_node);
8804
          cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8805
          EXPR_WFL_LINECOL (access0_wfl) = lc;
8806
          access = build_tree_list (NULL_TREE, access);
8807
          access = build_method_invocation (access0_wfl, access);
8808
          access = make_qualified_primary (cn, access, lc);
8809
        }
8810
 
8811
      /* If FROM isn't an inner class, that's fine, we've done enough.
8812
         What we're looking for can be accessed from there.  */
8813
      from = DECL_CONTEXT (TYPE_NAME (from));
8814
      if (!from)
8815
        break;
8816
      from = TREE_TYPE (from);
8817
    }
8818
  return access;
8819
}
8820
 
8821
/* Build an access function to the this$ local to TYPE. NULL_TREE
8822
   is returned if nothing needs to be generated. Otherwise, the method
8823
   generated and a method decl is returned.
8824
 
8825
   NOTE: These generated methods should be declared in a class file
8826
   attribute so that they can't be referred to directly.  */
8827
 
8828
static tree
8829
maybe_build_thisn_access_method (tree type)
8830
{
8831
  tree mdecl, args, stmt, rtype;
8832
  tree saved_current_function_decl;
8833
 
8834
  /* If TYPE is a top-level class, no access method is required.
8835
     If there already is such an access method, bail out. */
8836
  if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8837
    return NULL_TREE;
8838
 
8839
  /* We generate the method. The method looks like:
8840
     static  access$0 ( inst$) { return inst$.this$; }
8841
  */
8842
  args = build_tree_list (inst_id, build_pointer_type (type));
8843
  TREE_CHAIN (args) = end_params_node;
8844
  rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8845
  mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8846
                                    access0_identifier_node, args);
8847
  fix_method_argument_names (args, mdecl);
8848
  layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8849
  stmt = build_current_thisn (type);
8850
  stmt = make_qualified_primary (build_wfl_node (inst_id),
8851
                                 build_wfl_node (stmt), 0);
8852
  stmt = build_return (0, stmt);
8853
 
8854
  saved_current_function_decl = current_function_decl;
8855
  start_artificial_method_body (mdecl);
8856
  java_method_add_stmt (mdecl, stmt);
8857
  end_artificial_method_body (mdecl);
8858
  current_function_decl = saved_current_function_decl;
8859
 
8860
  CLASS_ACCESS0_GENERATED_P (type) = 1;
8861
 
8862
  return mdecl;
8863
}
8864
 
8865
/* Craft an correctly numbered `this$'string. this$0 is used for
8866
   the first level of innerclassing. this$1 for the next one, etc...
8867
   This function can be invoked with TYPE to NULL, available and then
8868
   has to count the parser context.  */
8869
 
8870
static GTY(()) tree saved_thisn;
8871
static GTY(()) tree saved_type;
8872
 
8873
static tree
8874
build_current_thisn (tree type)
8875
{
8876
  static int saved_i = -1;
8877
  static int saved_type_i = 0;
8878
  tree decl;
8879
  char buffer [24];
8880
  int i = 0;
8881
 
8882
  if (type)
8883
    {
8884
      if (type == saved_type)
8885
        i = saved_type_i;
8886
      else
8887
        {
8888
          for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8889
               decl; decl = DECL_CONTEXT (decl), i++)
8890
            ;
8891
 
8892
          saved_type = type;
8893
          saved_type_i = i;
8894
        }
8895
    }
8896
  else
8897
    i = list_length (GET_CPC_LIST ())-2;
8898
 
8899
  if (i == saved_i)
8900
    return saved_thisn;
8901
 
8902
  sprintf (buffer, "this$%d", i);
8903
  saved_i = i;
8904
  saved_thisn = get_identifier (buffer);
8905
  return saved_thisn;
8906
}
8907
 
8908
/* Return the assignment to the hidden enclosing context `this$'
8909
   by the second incoming parameter to the innerclass constructor. The
8910
   form used is `this.this$ = this$;'.  */
8911
 
8912
static tree
8913
build_thisn_assign (void)
8914
{
8915
  if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8916
    {
8917
      tree thisn = build_current_thisn (current_class);
8918
      tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8919
                                         build_wfl_node (thisn), 0);
8920
      tree rhs = build_wfl_node (thisn);
8921
#ifdef USE_MAPPED_LOCATION
8922
      SET_EXPR_LOCATION (lhs, input_location);
8923
#else
8924
      EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8925
#endif
8926
      return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8927
    }
8928
  return NULL_TREE;
8929
}
8930
 
8931
 
8932
/* Building the synthetic `class$' used to implement the `.class' 1.1
8933
   extension for non primitive types. This method looks like:
8934
 
8935
    static Class class$(String type) throws NoClassDefFoundError
8936
    {
8937
      try {return (java.lang.Class.forName (String));}
8938
      catch (ClassNotFoundException e) {
8939
        throw new NoClassDefFoundError(e.getMessage());}
8940
    } */
8941
 
8942
static GTY(()) tree get_message_wfl;
8943
static GTY(()) tree type_parm_wfl;
8944
 
8945
static tree
8946
build_dot_class_method (tree class)
8947
{
8948
#define BWF(S) build_wfl_node (get_identifier ((S)))
8949
#ifdef USE_MAPPED_LOCATION
8950
#define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8951
#else
8952
#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8953
#endif
8954
  tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8955
  tree stmt, throw_stmt;
8956
 
8957
  if (!get_message_wfl)
8958
    {
8959
      get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8960
      type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8961
    }
8962
 
8963
  /* Build the arguments */
8964
  args = build_tree_list (get_identifier ("type$"),
8965
                          build_pointer_type (string_type_node));
8966
  TREE_CHAIN (args) = end_params_node;
8967
 
8968
  /* Build the qualified name java.lang.Class.forName */
8969
  tmp = MQN (MQN (MQN (BWF ("java"),
8970
                       BWF ("lang")), BWF ("Class")), BWF ("forName"));
8971
 
8972
  /* Create the "class$" function */
8973
  mdecl = create_artificial_method (class, ACC_STATIC,
8974
                                    build_pointer_type (class_type_node),
8975
                                    classdollar_identifier_node, args);
8976
  qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8977
                   BWF ("NoClassDefFoundError"));
8978
  DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8979
  register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8980
  JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8981
    &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8982
 
8983
  /* We start by building the try block. We need to build:
8984
       return (java.lang.Class.forName (type)); */
8985
  stmt = build_method_invocation (tmp,
8986
                                  build_tree_list (NULL_TREE, type_parm_wfl));
8987
  stmt = build_return (0, stmt);
8988
 
8989
  /* Now onto the catch block. We start by building the expression
8990
     throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8991
#ifdef USE_MAPPED_LOCATION
8992
  throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8993
                                    get_message_wfl, UNKNOWN_LOCATION);
8994
#else
8995
  throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8996
                                    get_message_wfl, 0);
8997
#endif
8998
  throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8999
 
9000
  /* Build new NoClassDefFoundError (_.getMessage) */
9001
  throw_stmt = build_new_invocation
9002
    (build_wfl_node (get_identifier ("NoClassDefFoundError")),
9003
     build_tree_list (build_pointer_type (string_type_node), throw_stmt));
9004
 
9005
  /* Build the throw, (it's too early to use BUILD_THROW) */
9006
  throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
9007
 
9008
  /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
9009
  qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
9010
                   BWF ("ClassNotFoundException"));
9011
  stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
9012
 
9013
  fix_method_argument_names (args, mdecl);
9014
  layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
9015
  saved_current_function_decl = current_function_decl;
9016
  start_artificial_method_body (mdecl);
9017
  java_method_add_stmt (mdecl, stmt);
9018
  end_artificial_method_body (mdecl);
9019
  current_function_decl = saved_current_function_decl;
9020
  TYPE_DOT_CLASS (class) = mdecl;
9021
 
9022
  return mdecl;
9023
}
9024
 
9025
static tree
9026
build_dot_class_method_invocation (tree this_class, tree type)
9027
{
9028
  tree dot_class_method = TYPE_DOT_CLASS (this_class);
9029
  tree sig_id, s, t;
9030
 
9031
  if (TYPE_ARRAY_P (type))
9032
    sig_id = build_java_signature (type);
9033
  else
9034
    sig_id = DECL_NAME (TYPE_NAME (type));
9035
 
9036
  /* Ensure that the proper name separator is used */
9037
  sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
9038
                               IDENTIFIER_LENGTH (sig_id));
9039
 
9040
  s = build_string (IDENTIFIER_LENGTH (sig_id),
9041
                    IDENTIFIER_POINTER (sig_id));
9042
  t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
9043
                               build_tree_list (NULL_TREE, s));
9044
  if (DECL_CONTEXT (dot_class_method) != this_class)
9045
    {
9046
      tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
9047
      t = make_qualified_primary (build_wfl_node (class_name), t, 0);
9048
    }
9049
  return t;
9050
}
9051
 
9052
/* This section of the code deals with constructor.  */
9053
 
9054
/* Craft a body for default constructor. Patch existing constructor
9055
   bodies with call to super() and field initialization statements if
9056
   necessary.  */
9057
 
9058
static void
9059
fix_constructors (tree mdecl)
9060
{
9061
  tree iii;                     /* Instance Initializer Invocation */
9062
  tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
9063
  tree thisn_assign, compound = NULL_TREE;
9064
  tree class_type = DECL_CONTEXT (mdecl);
9065
 
9066
  if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
9067
    return;
9068
  DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
9069
 
9070
  if (!*bodyp)
9071
    {
9072
      /* It is an error for the compiler to generate a default
9073
         constructor if the superclass doesn't have a constructor that
9074
         takes no argument, or the same args for an anonymous class */
9075
      tree sdecl = get_constructor_super (mdecl);
9076
      if (sdecl == NULL_TREE)
9077
        {
9078
          tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
9079
          tree save = DECL_NAME (mdecl);
9080
          const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
9081
          DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
9082
          parse_error_context
9083
            (lookup_cl (TYPE_NAME (class_type)),
9084
             "No constructor matching %qs found in class %qs",
9085
             lang_printable_name (mdecl, 2), n);
9086
          DECL_NAME (mdecl) = save;
9087
        }
9088
 
9089
      if (ANONYMOUS_CLASS_P (class_type))
9090
        {
9091
          /* Copy throws clause from the super constructor.  */
9092
          tree throws = DECL_FUNCTION_THROWS (sdecl);
9093
          DECL_FUNCTION_THROWS (mdecl) = copy_list (throws);
9094
        }
9095
 
9096
      /* The constructor body must be crafted by hand. It's the
9097
         constructor we defined when we realize we didn't have the
9098
         CLASSNAME() constructor */
9099
      start_artificial_method_body (mdecl);
9100
 
9101
      /* Insert an assignment to the this$ hidden field, if
9102
         necessary */
9103
      if ((thisn_assign = build_thisn_assign ()))
9104
        java_method_add_stmt (mdecl, thisn_assign);
9105
 
9106
      /* We don't generate a super constructor invocation if we're
9107
         compiling java.lang.Object. build_super_invocation takes care
9108
         of that. */
9109
      java_method_add_stmt (mdecl, build_super_invocation (mdecl));
9110
 
9111
      /* FIXME */
9112
      if ((iii = build_instinit_invocation (class_type)))
9113
        java_method_add_stmt (mdecl, iii);
9114
 
9115
      end_artificial_method_body (mdecl);
9116
    }
9117
  /* Search for an explicit constructor invocation */
9118
  else
9119
    {
9120
      int found = 0;
9121
      int invokes_this = 0;
9122
      tree main_block = BLOCK_EXPR_BODY (*bodyp);
9123
 
9124
      while (*bodyp)
9125
        {
9126
          tree body = *bodyp;
9127
          switch (TREE_CODE (body))
9128
            {
9129
            case CALL_EXPR:
9130
              found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
9131
              if (CALL_THIS_CONSTRUCTOR_P (body))
9132
                invokes_this = 1;
9133
              break;
9134
            case COMPOUND_EXPR:
9135
            case EXPR_WITH_FILE_LOCATION:
9136
              bodyp = &TREE_OPERAND (body, 0);
9137
              continue;
9138
            case BLOCK:
9139
              bodyp = &BLOCK_EXPR_BODY (body);
9140
              continue;
9141
            default:
9142
              break;
9143
            }
9144
          break;
9145
        }
9146
 
9147
      /* Generate the assignment to this$, if necessary */
9148
      if ((thisn_assign = build_thisn_assign ()))
9149
        compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9150
 
9151
      /* The constructor is missing an invocation of super() */
9152
      if (!found)
9153
        compound = add_stmt_to_compound (compound, NULL_TREE,
9154
                                         build_super_invocation (mdecl));
9155
      /* Explicit super() invocation should take place before the
9156
         instance initializer blocks. */
9157
      else
9158
        {
9159
          compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9160
          *bodyp = build_java_empty_stmt ();
9161
        }
9162
 
9163
      DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9164
 
9165
      /* Insert the instance initializer block right after. */
9166
      if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9167
        compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9168
 
9169
      /* Fix the constructor main block if we're adding extra stmts */
9170
      if (compound)
9171
        {
9172
          compound = add_stmt_to_compound (compound, NULL_TREE,
9173
                                           BLOCK_EXPR_BODY (main_block));
9174
          BLOCK_EXPR_BODY (main_block) = compound;
9175
        }
9176
    }
9177
}
9178
 
9179
/* Browse constructors in the super class, searching for a constructor
9180
   that doesn't take any argument. Return the constructor if one is found,
9181
   NULL_TREE otherwise.  If the current class is an anonymous inner class,
9182
   look for something that has the same signature. */
9183
static tree
9184
get_constructor_super (tree mdecl)
9185
{
9186
  tree class = CLASSTYPE_SUPER (current_class);
9187
  int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9188
  tree sdecl;
9189
 
9190
  if (!class)
9191
    return NULL_TREE;
9192
 
9193
  if (ANONYMOUS_CLASS_P (current_class))
9194
    {
9195
      tree mdecl_arg_type;
9196
      SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9197
      for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9198
        if (DECL_CONSTRUCTOR_P (sdecl))
9199
          {
9200
            tree m_arg_type;
9201
            tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9202
            if (super_inner)
9203
              arg_type = TREE_CHAIN (arg_type);
9204
            for (m_arg_type = mdecl_arg_type;
9205
                 (arg_type != end_params_node
9206
                  && m_arg_type != end_params_node);
9207
                 arg_type = TREE_CHAIN (arg_type),
9208
                   m_arg_type = TREE_CHAIN (m_arg_type))
9209
              if (!valid_method_invocation_conversion_p
9210
                     (TREE_VALUE (arg_type),
9211
                      TREE_VALUE (m_arg_type)))
9212
                break;
9213
 
9214
            if (arg_type == end_params_node && m_arg_type == end_params_node)
9215
              return sdecl;
9216
          }
9217
    }
9218
  else
9219
    {
9220
      for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9221
        {
9222
          tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9223
          if (super_inner)
9224
            arg = TREE_CHAIN (arg);
9225
          if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9226
            return sdecl;
9227
        }
9228
    }
9229
  return NULL_TREE;
9230
}
9231
 
9232
/* Generate code for all context remembered for code generation.  */
9233
 
9234
static GTY(()) tree reversed_class_list;
9235
void
9236
java_expand_classes (void)
9237
{
9238
  int save_error_count = 0;
9239
  static struct parser_ctxt *cur_ctxp = NULL;
9240
  location_t save_location;
9241
 
9242
  java_parse_abort_on_error ();
9243
  if (!(ctxp = ctxp_for_generation))
9244
    return;
9245
  java_layout_classes ();
9246
  java_parse_abort_on_error ();
9247
  save_location = input_location;
9248
 
9249
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9250
    {
9251
      tree current;
9252
      for (current = cur_ctxp->class_list;
9253
           current;
9254
           current = TREE_CHAIN (current))
9255
        gen_indirect_dispatch_tables (TREE_TYPE (current));
9256
    }
9257
 
9258
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9259
    {
9260
      ctxp = cur_ctxp;
9261
      input_location = ctxp->file_start_location;
9262
      lang_init_source (2);            /* Error msgs have method prototypes */
9263
      java_complete_expand_classes (); /* Complete and expand classes */
9264
      java_parse_abort_on_error ();
9265
    }
9266
  input_location = save_location;
9267
 
9268
  /* Find anonymous classes and expand their constructor. This extra pass is
9269
     necessary because the constructor itself is only generated when the
9270
     method in which it is defined is expanded. */
9271
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9272
    {
9273
      tree current;
9274
      ctxp = cur_ctxp;
9275
      for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9276
        {
9277
          output_class = current_class = TREE_TYPE (current);
9278
          if (ANONYMOUS_CLASS_P (current_class))
9279
            {
9280
              tree d;
9281
              for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9282
                {
9283
                  if (DECL_CONSTRUCTOR_P (d))
9284
                    {
9285
                      java_complete_expand_method (d);
9286
                      break;    /* There is only one constructor. */
9287
                    }
9288
                }
9289
            }
9290
        }
9291
    }
9292
 
9293
  /* Expanding the constructors of anonymous classes generates access
9294
     methods.  Scan all the methods looking for null DECL_RESULTs --
9295
     this will be the case if a method hasn't been expanded.  */
9296
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9297
    {
9298
      tree current;
9299
      ctxp = cur_ctxp;
9300
      for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9301
        {
9302
          tree d;
9303
          output_class = current_class = TREE_TYPE (current);
9304
          for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9305
            {
9306
              if (DECL_RESULT (d) == NULL_TREE)
9307
                java_complete_expand_method (d);
9308
            }
9309
        }
9310
    }
9311
 
9312
  /* ???  Instead of all this we could iterate around the list of
9313
     classes until there were no more un-expanded methods.  It would
9314
     take a little longer -- one pass over the whole list of methods
9315
     -- but it would be simpler.  Like this:  */
9316
#if 0
9317
    {
9318
      int something_changed;
9319
 
9320
      do
9321
        {
9322
          something_changed = 0;
9323
          for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9324
            {
9325
              tree current;
9326
              ctxp = cur_ctxp;
9327
              for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9328
                {
9329
                  tree d;
9330
                  output_class = current_class = TREE_TYPE (current);
9331
                  for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9332
                    {
9333
                      if (DECL_RESULT (d) == NULL_TREE)
9334
                        {
9335
                          something_changed = 1;
9336
                          java_complete_expand_method (d);
9337
                        }
9338
                    }
9339
                }
9340
            }
9341
        }
9342
      while (something_changed);
9343
    }
9344
#endif
9345
 
9346
  /* If we've found error at that stage, don't try to generate
9347
     anything, unless we're checking the syntax only
9348
     (but not using -fsyntax-only for the purpose of generating
9349
     bytecode).  */
9350
  if (java_error_count
9351
      && (!flag_syntax_only && !flag_emit_class_files))
9352
    return;
9353
 
9354
  /* Now things are stable, go for generation of the class data. */
9355
 
9356
  /* We pessimistically marked all methods and fields external until
9357
     we knew what set of classes we were planning to compile.  Now mark
9358
     those that will be generated locally as not external.  */
9359
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9360
    {
9361
      tree current;
9362
      ctxp = cur_ctxp;
9363
      for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9364
        java_mark_class_local (TREE_TYPE (current));
9365
    }
9366
 
9367
  /* Compile the classes.  */
9368
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9369
    {
9370
      tree current;
9371
      reversed_class_list = NULL;
9372
 
9373
      ctxp = cur_ctxp;
9374
 
9375
      /* We write out the classes in reverse order.  This ensures that
9376
         inner classes are written before their containing classes,
9377
         which is important for parallel builds.  Otherwise, the
9378
         class file for the outer class may be found, but the class
9379
         file for the inner class may not be present.  In that
9380
         situation, the compiler cannot fall back to the original
9381
         source, having already read the outer class, so we must
9382
         prevent that situation.  */
9383
      for (current = ctxp->class_list;
9384
           current;
9385
           current = TREE_CHAIN (current))
9386
        reversed_class_list
9387
          = tree_cons (NULL_TREE, current, reversed_class_list);
9388
 
9389
      for (current = reversed_class_list;
9390
           current;
9391
           current = TREE_CHAIN (current))
9392
        {
9393
          output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9394
          if (flag_emit_class_files)
9395
            write_classfile (current_class);
9396
          else if (! flag_syntax_only)
9397
            java_expand_method_bodies (current_class);
9398
        }
9399
    }
9400
}
9401
 
9402
void
9403
java_finish_classes (void)
9404
{
9405
  static struct parser_ctxt *cur_ctxp = NULL;
9406
  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9407
    {
9408
      tree current;
9409
      ctxp = cur_ctxp;
9410
      for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9411
        {
9412
          output_class = current_class = TREE_TYPE (current);
9413
          finish_class ();
9414
        }
9415
    }
9416
}
9417
 
9418
/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9419
   a tree list node containing RIGHT. Fore coming RIGHTs will be
9420
   chained to this hook. LOCATION contains the location of the
9421
   separating `.' operator.  */
9422
 
9423
static tree
9424
make_qualified_primary (tree primary, tree right, int location)
9425
{
9426
  tree wfl;
9427
 
9428
  if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9429
    wfl = build_wfl_wrap (primary, location);
9430
  else
9431
    {
9432
      wfl = primary;
9433
      /* If wfl wasn't qualified, we build a first anchor */
9434
      if (!EXPR_WFL_QUALIFICATION (wfl))
9435
        EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9436
    }
9437
 
9438
  /* And chain them */
9439
  EXPR_WFL_LINECOL (right) = location;
9440
  chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9441
  PRIMARY_P (wfl) =  1;
9442
  return wfl;
9443
}
9444
 
9445
/* Simple merge of two name separated by a `.' */
9446
 
9447
static tree
9448
merge_qualified_name (tree left, tree right)
9449
{
9450
  tree node;
9451
  if (!left && !right)
9452
    return NULL_TREE;
9453
 
9454
  if (!left)
9455
    return right;
9456
 
9457
  if (!right)
9458
    return left;
9459
 
9460
  obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9461
                IDENTIFIER_LENGTH (left));
9462
  obstack_1grow (&temporary_obstack, '.');
9463
  obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9464
                 IDENTIFIER_LENGTH (right));
9465
  node =  get_identifier (obstack_base (&temporary_obstack));
9466
  obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9467
  QUALIFIED_P (node) = 1;
9468
  return node;
9469
}
9470
 
9471
/* Merge the two parts of a qualified name into LEFT.  Set the
9472
   location information of the resulting node to LOCATION, usually
9473
   inherited from the location information of the `.' operator. */
9474
 
9475
static tree
9476
make_qualified_name (tree left, tree right,
9477
#ifdef USE_MAPPED_LOCATION
9478
                     source_location location
9479
#else
9480
                     int location
9481
#endif
9482
                     )
9483
{
9484
#ifdef USE_COMPONENT_REF
9485
  tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9486
  SET_EXPR_LOCATION (node, location);
9487
  return node;
9488
#else
9489
  tree left_id = EXPR_WFL_NODE (left);
9490
  tree right_id = EXPR_WFL_NODE (right);
9491
  tree wfl, merge;
9492
 
9493
  merge = merge_qualified_name (left_id, right_id);
9494
 
9495
  /* Left wasn't qualified and is now qualified */
9496
#ifdef USE_MAPPED_LOCATION
9497
  if (!QUALIFIED_P (left_id))
9498
    {
9499
      tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9500
      EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9501
    }
9502
 
9503
  wfl = build_expr_wfl (right_id, location);
9504
#else
9505
  if (!QUALIFIED_P (left_id))
9506
    {
9507
      tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9508
      EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9509
      EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9510
    }
9511
 
9512
  wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9513
  EXPR_WFL_LINECOL (wfl) = location;
9514
#endif
9515
  chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9516
  EXPR_WFL_NODE (left) = merge;
9517
  return left;
9518
#endif
9519
}
9520
 
9521
/* Extract the last identifier component of the qualified in WFL. The
9522
   last identifier is removed from the linked list */
9523
 
9524
static tree
9525
cut_identifier_in_qualified (tree wfl)
9526
{
9527
  tree q;
9528
  tree previous = NULL_TREE;
9529
  for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9530
    if (!TREE_CHAIN (q))
9531
      {
9532
        if (!previous)
9533
          /* Operating on a non qualified qualified WFL.  */
9534
          abort ();
9535
 
9536
        TREE_CHAIN (previous) = NULL_TREE;
9537
        return TREE_PURPOSE (q);
9538
      }
9539
}
9540
 
9541
/* Resolve the expression name NAME. Return its decl.  */
9542
 
9543
static tree
9544
resolve_expression_name (tree id, tree *orig)
9545
{
9546
  tree name = EXPR_WFL_NODE (id);
9547
  tree decl;
9548
 
9549
  /* 6.5.5.1: Simple expression names */
9550
  if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9551
    {
9552
      /* 15.13.1: NAME can appear within the scope of a local variable
9553
         declaration */
9554
      if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9555
        return decl;
9556
 
9557
      /* 15.13.1: NAME can appear within a class declaration */
9558
      else
9559
        {
9560
          decl = lookup_field_wrapper (current_class, name);
9561
          if (decl)
9562
            {
9563
              tree access = NULL_TREE;
9564
              int fs = FIELD_STATIC (decl);
9565
 
9566
              /* If we're accessing an outer scope local alias, make
9567
                 sure we change the name of the field we're going to
9568
                 build access to. */
9569
              if (FIELD_LOCAL_ALIAS_USED (decl))
9570
                name = DECL_NAME (decl);
9571
 
9572
              check_deprecation (id, decl);
9573
 
9574
              /* Instance variable (8.3.1.1) can't appear within
9575
                 static method, static initializer or initializer for
9576
                 a static variable. */
9577
              if (!fs && METHOD_STATIC (current_function_decl))
9578
                {
9579
                  static_ref_err (id, name, current_class);
9580
                  return error_mark_node;
9581
                }
9582
              /* Instance variables can't appear as an argument of
9583
                 an explicit constructor invocation */
9584
              if (!fs && ctxp->explicit_constructor_p
9585
                  && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9586
                {
9587
                  parse_error_context
9588
                    (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9589
                  return error_mark_node;
9590
                }
9591
 
9592
              /* If we're processing an inner class and we're trying
9593
                 to access a field belonging to an outer class, build
9594
                 the access to the field.
9595
                 As usual, we have to treat initialized static final
9596
                 variables as a special case.  */
9597
              if (nested_member_access_p (current_class, decl)
9598
                  && ! (JDECL_P (decl) && CLASS_FINAL_VARIABLE_P (decl)
9599
                        && DECL_INITIAL (decl) != NULL_TREE
9600
                        && (JSTRING_TYPE_P (TREE_TYPE (decl))
9601
                            || JNUMERIC_TYPE_P (TREE_TYPE (decl)))
9602
                        && TREE_CONSTANT (DECL_INITIAL (decl))))
9603
                {
9604
                  if (!fs && CLASS_STATIC (TYPE_NAME (current_class)))
9605
                    {
9606
                      static_ref_err (id, DECL_NAME (decl), current_class);
9607
                      return error_mark_node;
9608
                    }
9609
                  access = build_nested_field_access (id, decl);
9610
                  if (orig)
9611
                    *orig = access;
9612
                  return access;
9613
                }
9614
 
9615
              /* Otherwise build what it takes to access the field */
9616
              access = build_field_ref ((fs ? NULL_TREE : current_this),
9617
                                        DECL_CONTEXT (decl), name);
9618
              if (fs)
9619
                access = maybe_build_class_init_for_field (decl, access);
9620
              /* We may be asked to save the real field access node */
9621
              if (orig)
9622
                *orig = access;
9623
              /* Last check: can we access the field? */
9624
              if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9625
                {
9626
                  not_accessible_field_error (id, decl);
9627
                  return error_mark_node;
9628
                }
9629
              /* And we return what we got */
9630
              return access;
9631
            }
9632
          /* Fall down to error report on undefined variable */
9633
        }
9634
    }
9635
  /* 6.5.5.2 Qualified Expression Names */
9636
  else
9637
    {
9638
      if (orig)
9639
        *orig = NULL_TREE;
9640
      qualify_ambiguous_name (id);
9641
      /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9642
      /* 15.10.2: Accessing Superclass Members using super */
9643
      return resolve_field_access (id, orig, NULL);
9644
    }
9645
 
9646
  /* We've got an error here */
9647
  if (INNER_CLASS_TYPE_P (current_class))
9648
    parse_error_context (id,
9649
                         "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9650
                         IDENTIFIER_POINTER (name),
9651
                         IDENTIFIER_POINTER (DECL_NAME
9652
                                             (TYPE_NAME (current_class))));
9653
  else
9654
    parse_error_context (id, "Undefined variable %qs",
9655
                         IDENTIFIER_POINTER (name));
9656
 
9657
  return error_mark_node;
9658
}
9659
 
9660
static void
9661
static_ref_err (tree wfl, tree field_id, tree class_type)
9662
{
9663
  parse_error_context
9664
    (wfl,
9665
     "Can't make a static reference to nonstatic variable %qs in class %qs",
9666
     IDENTIFIER_POINTER (field_id),
9667
     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9668
}
9669
 
9670
/* 15.10.1 Field Access Using a Primary and/or Expression Name.
9671
   We return something suitable to generate the field access. We also
9672
   return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9673
   recipient's address can be null. */
9674
 
9675
static tree
9676
resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9677
{
9678
  int is_static = 0;
9679
  tree field_ref;
9680
  tree decl = NULL_TREE, where_found, type_found;
9681
 
9682
  if (resolve_qualified_expression_name (qual_wfl, &decl,
9683
                                         &where_found, &type_found))
9684
    return error_mark_node;
9685
 
9686
  /* Resolve the LENGTH field of an array here */
9687
  if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9688
      && type_found && TYPE_ARRAY_P (type_found)
9689
      && ! flag_emit_class_files)
9690
    {
9691
      tree length = build_java_array_length_access (where_found);
9692
      field_ref = length;
9693
 
9694
      /* In case we're dealing with a static array, we need to
9695
         initialize its class before the array length can be fetched.  */
9696
      if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9697
        {
9698
          build_static_field_ref (where_found);
9699
          field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9700
        }
9701
    }
9702
  /* We might have been trying to resolve field.method(). In which
9703
     case, the resolution is over and decl is the answer */
9704
  else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9705
    field_ref = decl;
9706
  else if (JDECL_P (decl))
9707
    {
9708
      if (!type_found)
9709
        type_found = DECL_CONTEXT (decl);
9710
      is_static = FIELD_STATIC (decl);
9711
      field_ref = build_field_ref ((is_static ?
9712
                                    NULL_TREE : where_found),
9713
                                   type_found, DECL_NAME (decl));
9714
      if (field_ref == error_mark_node)
9715
        return error_mark_node;
9716
      if (is_static)
9717
        field_ref = maybe_build_class_init_for_field (decl, field_ref);
9718
 
9719
      /* If we're looking at a static field, we may need to generate a
9720
         class initialization for it.  This can happen when the access
9721
         looks like `field.ref', where `field' is a static field in an
9722
         interface we implement.  */
9723
      if (!flag_emit_class_files
9724
          && TREE_CODE (where_found) == VAR_DECL
9725
          && FIELD_STATIC (where_found))
9726
        {
9727
          build_static_field_ref (where_found);
9728
          field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9729
        }
9730
    }
9731
  else
9732
    field_ref = decl;
9733
 
9734
  if (field_decl)
9735
    *field_decl = decl;
9736
  if (field_type)
9737
    *field_type = (QUAL_DECL_TYPE (decl) ?
9738
                   QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9739
  return field_ref;
9740
}
9741
 
9742
/* If NODE is an access to a static field, strip out the class
9743
   initialization part and return the field decl, otherwise, return
9744
   NODE. */
9745
 
9746
tree
9747
extract_field_decl (tree node)
9748
{
9749
  if (TREE_CODE (node) == COMPOUND_EXPR)
9750
    {
9751
      tree op1 = TREE_OPERAND (node, 1);
9752
      if (TREE_CODE (op1) == COMPOUND_EXPR)
9753
         {
9754
           tree call = TREE_OPERAND (op1, 0);
9755
           if (TREE_CODE (call) == CALL_EXPR
9756
               && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9757
               && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9758
                   == soft_initclass_node))
9759
             return TREE_OPERAND (op1, 1);
9760
         }
9761
      else if (JDECL_P (op1))
9762
        return op1;
9763
    }
9764
  return node;
9765
}
9766
 
9767
/* 6.5.5.2: Qualified Expression Names */
9768
 
9769
static int
9770
resolve_qualified_expression_name (tree wfl, tree *found_decl,
9771
                                   tree *where_found, tree *type_found)
9772
{
9773
  int from_type = 0;            /* Field search initiated from a type */
9774
  int from_super = 0, from_cast = 0, from_qualified_this = 0;
9775
  int previous_call_static = 0;
9776
  int is_static;
9777
  tree decl = NULL_TREE, type = NULL_TREE, q;
9778
  /* For certain for of inner class instantiation */
9779
  tree saved_current, saved_this;
9780
#define RESTORE_THIS_AND_CURRENT_CLASS                          \
9781
  { current_class = saved_current; current_this = saved_this;}
9782
 
9783
  *type_found = *where_found = NULL_TREE;
9784
 
9785
  for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9786
    {
9787
      tree qual_wfl = QUAL_WFL (q);
9788
      tree ret_decl;            /* for EH checking */
9789
#ifdef USE_MAPPED_LOCATION
9790
      source_location location;  /* for EH checking */
9791
#else
9792
      int location;             /* for EH checking */
9793
#endif
9794
 
9795
      /* 15.10.1 Field Access Using a Primary */
9796
      switch (TREE_CODE (qual_wfl))
9797
        {
9798
        case CALL_EXPR:
9799
        case NEW_CLASS_EXPR:
9800
          /* If the access to the function call is a non static field,
9801
             build the code to access it. */
9802
          if (JDECL_P (decl) && !FIELD_STATIC (decl))
9803
            {
9804
              decl = maybe_access_field (decl, *where_found,
9805
                                         DECL_CONTEXT (decl));
9806
              if (decl == error_mark_node)
9807
                return 1;
9808
            }
9809
 
9810
          /* And code for the function call */
9811
          if (complete_function_arguments (qual_wfl))
9812
            return 1;
9813
 
9814
          /* We might have to setup a new current class and a new this
9815
             for the search of an inner class, relative to the type of
9816
             a expression resolved as `decl'. The current values are
9817
             saved and restored shortly after */
9818
          saved_current = current_class;
9819
          saved_this = current_this;
9820
          if (decl
9821
              && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9822
                  || from_qualified_this))
9823
            {
9824
              /* If we still have `from_qualified_this', we have the form
9825
                 .this.f() and we need to build .this */
9826
              if (from_qualified_this)
9827
                {
9828
                  decl = build_access_to_thisn (current_class, type, 0);
9829
                  decl = java_complete_tree (decl);
9830
                  type = TREE_TYPE (TREE_TYPE (decl));
9831
                }
9832
              current_class = type;
9833
              current_this = decl;
9834
              from_qualified_this = 0;
9835
            }
9836
 
9837
          if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9838
            CALL_USING_SUPER (qual_wfl) = 1;
9839
#ifdef USE_MAPPED_LOCATION
9840
          location = (TREE_CODE (qual_wfl) == CALL_EXPR
9841
                      ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9842
                      : UNKNOWN_LOCATION);
9843
#else
9844
          location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9845
                      EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9846
#endif
9847
          *where_found = patch_method_invocation (qual_wfl, decl, type,
9848
                                                  from_super,
9849
                                                  &is_static, &ret_decl);
9850
          from_super = 0;
9851
          if (*where_found == error_mark_node)
9852
            {
9853
              RESTORE_THIS_AND_CURRENT_CLASS;
9854
              return 1;
9855
            }
9856
          *type_found = type = QUAL_DECL_TYPE (*where_found);
9857
 
9858
          *where_found = force_evaluation_order (*where_found);
9859
 
9860
          /* If we're creating an inner class instance, check for that
9861
             an enclosing instance is in scope */
9862
          if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9863
              && INNER_ENCLOSING_SCOPE_CHECK (type))
9864
            {
9865
              parse_error_context
9866
                (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9867
                 lang_printable_name (type, 0),
9868
                 (!current_this ? "" :
9869
                  "; an explicit one must be provided when creating this inner class"));
9870
              RESTORE_THIS_AND_CURRENT_CLASS;
9871
              return 1;
9872
            }
9873
 
9874
          /* In case we had to change then to resolve a inner class
9875
             instantiation using a primary qualified by a `new' */
9876
          RESTORE_THIS_AND_CURRENT_CLASS;
9877
 
9878
#ifdef USE_MAPPED_LOCATION
9879
          if (location != UNKNOWN_LOCATION)
9880
#else
9881
          if (location)
9882
#endif
9883
            {
9884
              tree arguments = NULL_TREE;
9885
              if (TREE_CODE (qual_wfl) == CALL_EXPR
9886
                  && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9887
                arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9888
              check_thrown_exceptions (location, ret_decl, arguments);
9889
            }
9890
 
9891
          /* If the previous call was static and this one is too,
9892
             build a compound expression to hold the two (because in
9893
             that case, previous function calls aren't transported as
9894
             forcoming function's argument. */
9895
          if (previous_call_static && is_static)
9896
            {
9897
              /* We must set CAN_COMPLETE_NORMALLY for the first call
9898
                 since it is done nowhere else.  */
9899
              CAN_COMPLETE_NORMALLY (decl) = 1;
9900
              decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9901
                             decl, *where_found);
9902
              TREE_SIDE_EFFECTS (decl) = 1;
9903
            }
9904
          else
9905
            {
9906
              previous_call_static = is_static;
9907
              decl = *where_found;
9908
            }
9909
          from_type = 0;
9910
          continue;
9911
 
9912
        case NEW_ARRAY_EXPR:
9913
        case NEW_ANONYMOUS_ARRAY_EXPR:
9914
          *where_found = decl = java_complete_tree (qual_wfl);
9915
          if (decl == error_mark_node)
9916
            return 1;
9917
          *type_found = type = QUAL_DECL_TYPE (decl);
9918
          continue;
9919
 
9920
        case CONVERT_EXPR:
9921
          *where_found = decl = java_complete_tree (qual_wfl);
9922
          if (decl == error_mark_node)
9923
            return 1;
9924
          *type_found = type = QUAL_DECL_TYPE (decl);
9925
          from_cast = 1;
9926
          continue;
9927
 
9928
        case CONDITIONAL_EXPR:
9929
        case STRING_CST:
9930
        case MODIFY_EXPR:
9931
          *where_found = decl = java_complete_tree (qual_wfl);
9932
          if (decl == error_mark_node)
9933
            return 1;
9934
          *type_found = type = QUAL_DECL_TYPE (decl);
9935
          continue;
9936
 
9937
        case ARRAY_REF:
9938
          /* If the access to the function call is a non static field,
9939
             build the code to access it. */
9940
          if (JDECL_P (decl) && !FIELD_STATIC (decl))
9941
            {
9942
              decl = maybe_access_field (decl, *where_found, type);
9943
              if (decl == error_mark_node)
9944
                return 1;
9945
            }
9946
          /* And code for the array reference expression */
9947
          decl = java_complete_tree (qual_wfl);
9948
          if (decl == error_mark_node)
9949
            return 1;
9950
          type = QUAL_DECL_TYPE (decl);
9951
          continue;
9952
 
9953
        case PLUS_EXPR:
9954
          if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9955
            return 1;
9956
          if ((type = patch_string (decl)))
9957
            decl = type;
9958
          *where_found = QUAL_RESOLUTION (q) = decl;
9959
          *type_found = type = TREE_TYPE (decl);
9960
          break;
9961
 
9962
        case CLASS_LITERAL:
9963
          if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9964
            return 1;
9965
          *where_found = QUAL_RESOLUTION (q) = decl;
9966
          *type_found = type = TREE_TYPE (decl);
9967
          break;
9968
 
9969
        default:
9970
          /* Fix for -Wall Just go to the next statement. Don't
9971
             continue */
9972
          break;
9973
        }
9974
 
9975
      /* If we fall here, we weren't processing a (static) function call. */
9976
      previous_call_static = 0;
9977
 
9978
      /* It can be the keyword THIS */
9979
      if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9980
          && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9981
        {
9982
          if (!current_this)
9983
            {
9984
              parse_error_context
9985
                (wfl, "Keyword % used outside allowed context");
9986
              return 1;
9987
            }
9988
          if (ctxp->explicit_constructor_p
9989
              && type == current_class)
9990
            {
9991
              parse_error_context (wfl, "Can't reference % before the superclass constructor has been called");
9992
              return 1;
9993
            }
9994
          /* We have to generate code for intermediate access */
9995
          if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9996
            {
9997
              *where_found = decl = current_this;
9998
              *type_found = type = QUAL_DECL_TYPE (decl);
9999
            }
10000
          /* We're trying to access the this from somewhere else. Make sure
10001
             it's allowed before doing so. */
10002
          else
10003
            {
10004
              if (!enclosing_context_p (type, current_class))
10005
                {
10006
                  char *p  = xstrdup (lang_printable_name (type, 0));
10007
                  parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
10008
                                       p, p,
10009
                                       lang_printable_name (current_class, 0));
10010
                  free (p);
10011
                  return 1;
10012
                }
10013
              from_qualified_this = 1;
10014
              /* If there's nothing else after that, we need to
10015
                 produce something now, otherwise, the section of the
10016
                 code that needs to produce .this will generate
10017
                 what is necessary. */
10018
              if (!TREE_CHAIN (q))
10019
                {
10020
                  decl = build_access_to_thisn (current_class, type, 0);
10021
                  *where_found = decl = java_complete_tree (decl);
10022
                  *type_found = type = TREE_TYPE (decl);
10023
                }
10024
            }
10025
 
10026
          from_type = 0;
10027
          continue;
10028
        }
10029
 
10030
      /* 15.10.2 Accessing Superclass Members using SUPER */
10031
      if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
10032
          && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
10033
        {
10034
          tree node;
10035
          /* Check on the restricted use of SUPER */
10036
          if (METHOD_STATIC (current_function_decl)
10037
              || current_class == object_type_node)
10038
            {
10039
              parse_error_context
10040
                (wfl, "Keyword % used outside allowed context");
10041
              return 1;
10042
            }
10043
          /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
10044
          node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
10045
                             CLASSTYPE_SUPER (current_class),
10046
                             build_this (EXPR_WFL_LINECOL (qual_wfl)));
10047
          *where_found = decl = java_complete_tree (node);
10048
          if (decl == error_mark_node)
10049
            return 1;
10050
          *type_found = type = QUAL_DECL_TYPE (decl);
10051
          from_super = from_type = 1;
10052
          continue;
10053
        }
10054
 
10055
      /* 15.13.1: Can't search for field name in packages, so we
10056
         assume a variable/class name was meant. */
10057
      if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10058
        {
10059
          tree name;
10060
          if ((decl = resolve_package (wfl, &q, &name)))
10061
            {
10062
              tree list;
10063
              *where_found = decl;
10064
 
10065
              check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10066
 
10067
              /* We want to be absolutely sure that the class is laid
10068
                 out. We're going to search something inside it. */
10069
              *type_found = type = TREE_TYPE (decl);
10070
              layout_class (type);
10071
              from_type = 1;
10072
 
10073
              /* Fix them all the way down, if any are left. */
10074
              if (q)
10075
                {
10076
                  list = TREE_CHAIN (q);
10077
                  while (list)
10078
                    {
10079
                      RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
10080
                      list = TREE_CHAIN (list);
10081
                    }
10082
                }
10083
            }
10084
          else
10085
            {
10086
              if (from_super || from_cast)
10087
                parse_error_context
10088
                  ((from_cast ? qual_wfl : wfl),
10089
                   "No variable %qs defined in class %qs",
10090
                   IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10091
                   lang_printable_name (type, 0));
10092
              else
10093
                parse_error_context
10094
                  (qual_wfl, "Undefined variable or class name: %qs",
10095
                   IDENTIFIER_POINTER (name));
10096
              return 1;
10097
            }
10098
        }
10099
 
10100
      /* We have a type name. It's been already resolved when the
10101
         expression was qualified. */
10102
      else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
10103
        {
10104
          decl = QUAL_RESOLUTION (q);
10105
 
10106
          /* Sneak preview. If next we see a `new', we're facing a
10107
             qualification which resulted in a type being selected
10108
             instead of a field.  Report the error.  */
10109
          if(TREE_CHAIN (q)
10110
             && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
10111
            {
10112
              parse_error_context (qual_wfl, "Undefined variable %qs",
10113
                                   IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10114
              return 1;
10115
            }
10116
 
10117
          check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
10118
 
10119
          check_deprecation (qual_wfl, decl);
10120
 
10121
          type = TREE_TYPE (decl);
10122
          from_type = 1;
10123
        }
10124
      /* We resolve an expression name */
10125
      else
10126
        {
10127
          tree field_decl = NULL_TREE;
10128
 
10129
          /* If there exists an early resolution, use it. That occurs
10130
             only once and we know that there are more things to
10131
             come. Don't do that when processing something after SUPER
10132
             (we need more thing to be put in place below */
10133
          if (!from_super && QUAL_RESOLUTION (q))
10134
            {
10135
              decl = QUAL_RESOLUTION (q);
10136
              if (!type)
10137
                {
10138
                  if (TREE_CODE (decl) == FIELD_DECL
10139
                      || TREE_CODE (decl) == VAR_DECL)
10140
                    {
10141
                      if (TREE_CODE (decl) == FIELD_DECL
10142
                          && !FIELD_STATIC (decl))
10143
                        {
10144
                          if (current_this)
10145
                            *where_found = current_this;
10146
                          else
10147
                            {
10148
                              static_ref_err (qual_wfl, DECL_NAME (decl),
10149
                                              current_class);
10150
                              return 1;
10151
                            }
10152
                        }
10153
                      else
10154
                        {
10155
                          *where_found = TREE_TYPE (decl);
10156
                          if (TREE_CODE (*where_found) == POINTER_TYPE)
10157
                            *where_found = TREE_TYPE (*where_found);
10158
                        }
10159
                      if (nested_member_access_p (current_class, decl))
10160
                        decl = build_nested_field_access (qual_wfl, decl);
10161
                    }
10162
                  else
10163
                    {
10164
                      *where_found = TREE_TYPE (decl);
10165
                      if (TREE_CODE (*where_found) == POINTER_TYPE)
10166
                        *where_found = TREE_TYPE (*where_found);
10167
                    }
10168
                }
10169
            }
10170
 
10171
          /* Report and error if we're using a numerical literal as a
10172
             qualifier. It can only be an INTEGER_CST. */
10173
          else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10174
            {
10175
              parse_error_context
10176
                (wfl, "Can't use type %qs as a qualifier",
10177
                 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10178
              return 1;
10179
            }
10180
 
10181
          /* We have to search for a field, knowing the type of its
10182
             container. The flag FROM_TYPE indicates that we resolved
10183
             the last member of the expression as a type name, which
10184
             means that for the resolution of this field, we'll look
10185
             for other errors than if it was resolved as a member of
10186
             an other field. */
10187
          else
10188
            {
10189
              int is_static;
10190
              tree field_decl_type; /* For layout */
10191
 
10192
              if (!from_type && !JREFERENCE_TYPE_P (type))
10193
                {
10194
                  parse_error_context
10195
                    (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10196
                     IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10197
                     lang_printable_name (type, 0),
10198
                     IDENTIFIER_POINTER (DECL_NAME (decl)));
10199
                  return 1;
10200
                }
10201
 
10202
              field_decl = lookup_field_wrapper (type,
10203
                                                 EXPR_WFL_NODE (qual_wfl));
10204
 
10205
              /* Maybe what we're trying to access to is an inner
10206
                 class, only if decl is a TYPE_DECL. */
10207
              if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10208
                {
10209
                  tree ptr, inner_decl;
10210
 
10211
                  BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10212
                  inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10213
                  if (inner_decl)
10214
                    {
10215
                      check_inner_class_access (inner_decl, decl, qual_wfl);
10216
                      type = TREE_TYPE (inner_decl);
10217
                      decl = inner_decl;
10218
                      from_type = 1;
10219
                      continue;
10220
                    }
10221
                }
10222
 
10223
              if (field_decl == NULL_TREE)
10224
                {
10225
                  parse_error_context
10226
                    (qual_wfl, "No variable %qs defined in type %qs",
10227
                     IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10228
                     GET_TYPE_NAME (type));
10229
                  return 1;
10230
                }
10231
              if (field_decl == error_mark_node)
10232
                return 1;
10233
 
10234
              /* Layout the type of field_decl, since we may need
10235
                 it. Don't do primitive types or loaded classes. The
10236
                 situation of non primitive arrays may not handled
10237
                 properly here. FIXME */
10238
              if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10239
                field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10240
              else
10241
                field_decl_type = TREE_TYPE (field_decl);
10242
              if (!JPRIMITIVE_TYPE_P (field_decl_type)
10243
                  && !CLASS_LOADED_P (field_decl_type)
10244
                  && !TYPE_ARRAY_P (field_decl_type))
10245
                resolve_and_layout (field_decl_type, NULL_TREE);
10246
 
10247
              /* Check on accessibility here */
10248
              if (not_accessible_p (current_class, field_decl,
10249
                                    *type_found, from_super))
10250
                return not_accessible_field_error (qual_wfl,field_decl);
10251
              check_deprecation (qual_wfl, field_decl);
10252
 
10253
              /* There are things to check when fields are accessed
10254
                 from type. There are no restrictions on a static
10255
                 declaration of the field when it is accessed from an
10256
                 interface */
10257
              is_static = FIELD_STATIC (field_decl);
10258
              if (!from_super && from_type
10259
                  && !TYPE_INTERFACE_P (type)
10260
                  && !is_static
10261
                  && (current_function_decl
10262
                      && METHOD_STATIC (current_function_decl)))
10263
                {
10264
                  static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10265
                  return 1;
10266
                }
10267
              from_cast = from_super = 0;
10268
 
10269
              /* If it's an access from a type but isn't static, we
10270
                 make it relative to `this'. */
10271
              if (!is_static && from_type)
10272
                decl = current_this;
10273
 
10274
              /* If we need to generate something to get a proper
10275
                 handle on what this field is accessed from, do it
10276
                 now. */
10277
              if (!is_static)
10278
                {
10279
                  decl = maybe_access_field (decl, *where_found, *type_found);
10280
                  if (decl == error_mark_node)
10281
                    return 1;
10282
                }
10283
 
10284
              /* We want to keep the location where we found it, and the
10285
                 type we found.  */
10286
              *where_found = decl;
10287
              *type_found = type;
10288
 
10289
              /* Generate the correct expression for field access from
10290
                 qualified this */
10291
              if (from_qualified_this)
10292
                {
10293
                  field_decl
10294
                    = build_nested_field_access (qual_wfl, field_decl);
10295
                  from_qualified_this = 0;
10296
                }
10297
 
10298
              /* If needed, generate accessors for static field access.  */
10299
              if (is_static
10300
                  && FIELD_PRIVATE (field_decl)
10301
                  && flag_emit_class_files
10302
                  && nested_member_access_p (current_class, field_decl))
10303
                field_decl = build_nested_field_access (qual_wfl, field_decl);
10304
 
10305
              /* This is the decl found and eventually the next one to
10306
                 search from */
10307
              decl = field_decl;
10308
            }
10309
          from_type = 0;
10310
          type = QUAL_DECL_TYPE (decl);
10311
 
10312
          /* Sneak preview. If decl is qualified by a `new', report
10313
             the error here to be accurate on the peculiar construct */
10314
          if (TREE_CHAIN (q)
10315
              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10316
              && !JREFERENCE_TYPE_P (type))
10317
            {
10318
              parse_error_context (qual_wfl, "Attempt to reference field % in a %qs",
10319
                                   lang_printable_name (type, 0));
10320
              return 1;
10321
            }
10322
        }
10323
      /* `q' might have changed due to a after package resolution
10324
         re-qualification */
10325
      if (!q)
10326
        break;
10327
    }
10328
  *found_decl = decl;
10329
  return 0;
10330
}
10331
 
10332
/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10333
   can't be accessed from REFERENCE (a record type). If MEMBER
10334
   features a protected access, we then use WHERE which, if non null,
10335
   holds the type of MEMBER's access that is checked against
10336
   6.6.2.1. This function should be used when decl is a field or a
10337
   method.  */
10338
 
10339
static int
10340
not_accessible_p (tree reference, tree member, tree where, int from_super)
10341
{
10342
  int access_flag = get_access_flags_from_decl (member);
10343
  bool is_static = false;
10344
 
10345
  if (TREE_CODE (member) == FIELD_DECL ||
10346
      TREE_CODE (member) == VAR_DECL)
10347
    is_static = FIELD_STATIC (member);
10348
  else
10349
    is_static = METHOD_STATIC (member);
10350
 
10351
  /* Access always granted for members declared public */
10352
  if (access_flag & ACC_PUBLIC)
10353
    return 0;
10354
 
10355
  /* Check access on protected members */
10356
  if (access_flag & ACC_PROTECTED)
10357
    {
10358
      /* Access granted if it occurs from within the package
10359
         containing the class in which the protected member is
10360
         declared */
10361
      if (class_in_current_package (DECL_CONTEXT (member)))
10362
        return 0;
10363
 
10364
      /* If accessed with the form `super.member', then access is granted */
10365
      if (from_super)
10366
        return 0;
10367
 
10368
      /* If WHERE is active, access was made through a qualifier. For
10369
         non-static members, access is granted if the type of the qualifier
10370
         is or is a sublass of the type the access is made from (6.6.2.1.)  */
10371
      if (where && !is_static)
10372
        {
10373
          while (reference)
10374
            {
10375
              if (inherits_from_p (where, reference))
10376
                return 0;
10377
              if (INNER_CLASS_TYPE_P (reference))
10378
                reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10379
              else
10380
                break;
10381
            }
10382
          return 1;
10383
        }
10384
 
10385
      /* Otherwise, access is granted if occurring from within the class
10386
         where member is declared, or a subclass of it.  */
10387
      while (reference)
10388
        {
10389
          if (inherits_from_p (reference, DECL_CONTEXT (member)))
10390
            return 0;
10391
          if (INNER_CLASS_TYPE_P (reference))
10392
            reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10393
          else
10394
            break;
10395
        }
10396
      return 1;
10397
    }
10398
 
10399
  /* Check access on private members. Access is granted only if it
10400
     occurs from within the class in which it is declared -- that does
10401
     it for innerclasses too. */
10402
  if (access_flag & ACC_PRIVATE)
10403
    {
10404
      if (reference == DECL_CONTEXT (member) ||
10405
          common_enclosing_context_p (DECL_CONTEXT (member), reference))
10406
        return 0;
10407
      return 1;
10408
    }
10409
 
10410
  /* Default access is permitted only when occurring from within the
10411
     package in which the context (MEMBER) is declared.  */
10412
  return !class_in_current_package (DECL_CONTEXT (member));
10413
}
10414
 
10415
/* Test deprecated decl access.  */
10416
static void
10417
check_deprecation (tree wfl, tree decl)
10418
{
10419
  const char *file;
10420
  tree elt;
10421
 
10422
  if (! warn_deprecated)
10423
    return;
10424
 
10425
  /* We want to look at the element type of arrays here, so we strip
10426
     all surrounding array types.  */
10427
  if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10428
    {
10429
      elt = TREE_TYPE (decl);
10430
      while (TYPE_ARRAY_P (elt))
10431
        elt = TYPE_ARRAY_ELEMENT (elt);
10432
      /* We'll end up with a pointer type, so we use TREE_TYPE to go
10433
         to the record.  */
10434
      decl = TYPE_NAME (TREE_TYPE (elt));
10435
    }
10436
  file = DECL_SOURCE_FILE (decl);
10437
 
10438
  /* Complain if the field is deprecated and the file it was defined
10439
     in isn't compiled at the same time the file which contains its
10440
     use is */
10441
  if (DECL_DEPRECATED (decl)
10442
      && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10443
    {
10444
      const char *the;
10445
      switch (TREE_CODE (decl))
10446
        {
10447
        case FUNCTION_DECL:
10448
          the = "method";
10449
          break;
10450
        case FIELD_DECL:
10451
        case VAR_DECL:
10452
          the = "field";
10453
          break;
10454
        case TYPE_DECL:
10455
          parse_warning_context (wfl, "The class %qs has been deprecated",
10456
                                 IDENTIFIER_POINTER (DECL_NAME (decl)));
10457
          return;
10458
        default:
10459
          abort ();
10460
        }
10461
      /* Don't issue a message if the context as been deprecated as a
10462
         whole. */
10463
      if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10464
        parse_warning_context
10465
          (wfl, "The %s %qs in class %qs has been deprecated",
10466
           the, lang_printable_name (decl, 0),
10467
           IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10468
    }
10469
}
10470
 
10471
/* Returns 1 if class was declared in the current package, 0 otherwise */
10472
 
10473
static int
10474
class_in_current_package (tree class)
10475
{
10476
  if (TYPE_PACKAGE (current_class) == TYPE_PACKAGE (class))
10477
    return 1;
10478
  return 0;
10479
}
10480
 
10481
/* This function may generate code to access DECL from WHERE. This is
10482
   done only if certain conditions meet.  */
10483
 
10484
static tree
10485
maybe_access_field (tree decl, tree where, tree type)
10486
{
10487
  if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10488
      && !FIELD_STATIC (decl))
10489
    decl = build_field_ref (where ? where : current_this,
10490
                            (type ? type : DECL_CONTEXT (decl)),
10491
                            DECL_NAME (decl));
10492
  return decl;
10493
}
10494
 
10495
/* Build a method invocation, by patching PATCH. If non NULL
10496
   and according to the situation, PRIMARY and WHERE may be
10497
   used. IS_STATIC is set to 1 if the invoked function is static. */
10498
 
10499
static tree
10500
patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10501
                         int *is_static, tree *ret_decl)
10502
{
10503
  tree wfl = TREE_OPERAND (patch, 0);
10504
  tree args = TREE_OPERAND (patch, 1);
10505
  tree name = EXPR_WFL_NODE (wfl);
10506
  tree list;
10507
  int is_static_flag = 0;
10508
  int is_super_init = 0;
10509
  tree this_arg = NULL_TREE;
10510
  int is_array_clone_call = 0;
10511
 
10512
  /* Should be overridden if everything goes well. Otherwise, if
10513
     something fails, it should keep this value. It stop the
10514
     evaluation of a bogus assignment. See java_complete_tree,
10515
     MODIFY_EXPR: for the reasons why we sometimes want to keep on
10516
     evaluating an assignment */
10517
  TREE_TYPE (patch) = error_mark_node;
10518
 
10519
  /* Since lookup functions are messing with line numbers, save the
10520
     context now.  */
10521
  java_parser_context_save_global ();
10522
 
10523
  /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10524
 
10525
  /* Resolution of qualified name, excluding constructors */
10526
  if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10527
    {
10528
      tree identifier, identifier_wfl, type, resolved;
10529
      /* Extract the last IDENTIFIER of the qualified
10530
         expression. This is a wfl and we will use it's location
10531
         data during error report. */
10532
      identifier_wfl = cut_identifier_in_qualified (wfl);
10533
      identifier = EXPR_WFL_NODE (identifier_wfl);
10534
 
10535
      /* Given the context, IDENTIFIER is syntactically qualified
10536
         as a MethodName. We need to qualify what's before */
10537
      qualify_ambiguous_name (wfl);
10538
      resolved = resolve_field_access (wfl, NULL, NULL);
10539
 
10540
      if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10541
         && FIELD_FINAL (resolved)
10542
         && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10543
         && !flag_emit_class_files)
10544
       resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10545
 
10546
      if (resolved == error_mark_node)
10547
        PATCH_METHOD_RETURN_ERROR ();
10548
 
10549
      type = GET_SKIP_TYPE (resolved);
10550
      resolve_and_layout (type, NULL_TREE);
10551
 
10552
      if (JPRIMITIVE_TYPE_P (type))
10553
        {
10554
          parse_error_context
10555
            (identifier_wfl,
10556
             "Can't invoke a method on primitive type %qs",
10557
             IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10558
          PATCH_METHOD_RETURN_ERROR ();
10559
        }
10560
 
10561
      list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10562
      args = nreverse (args);
10563
 
10564
      /* We're resolving a call from a type */
10565
      if (TREE_CODE (resolved) == TYPE_DECL)
10566
        {
10567
          if (CLASS_INTERFACE (resolved))
10568
            {
10569
              parse_error_context
10570
                (identifier_wfl,
10571
                "Can't make static reference to method %qs in interface %qs",
10572
                 IDENTIFIER_POINTER (identifier),
10573
                 IDENTIFIER_POINTER (name));
10574
              PATCH_METHOD_RETURN_ERROR ();
10575
            }
10576
          if (list)
10577
            {
10578
              if (METHOD_STATIC (list))
10579
                maybe_use_access_method (0, &list, NULL);
10580
              else
10581
                {
10582
                  char *fct_name = xstrdup (lang_printable_name (list, 2));
10583
                  parse_error_context
10584
                    (identifier_wfl,
10585
                     "Can't make static reference to method %<%s %s%> in class %qs",
10586
                     lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10587
                     fct_name,
10588
                     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10589
                  free (fct_name);
10590
                  PATCH_METHOD_RETURN_ERROR ();
10591
                }
10592
            }
10593
        }
10594
      else
10595
        this_arg = primary = resolved;
10596
 
10597
      if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10598
        is_array_clone_call = 1;
10599
 
10600
      /* IDENTIFIER_WFL will be used to report any problem further */
10601
      wfl = identifier_wfl;
10602
    }
10603
  /* Resolution of simple names, names generated after a primary: or
10604
     constructors */
10605
  else
10606
    {
10607
      tree class_to_search = NULL_TREE;
10608
      int lc;                   /* Looking for Constructor */
10609
 
10610
      /* We search constructor in their target class */
10611
      if (CALL_CONSTRUCTOR_P (patch))
10612
        {
10613
          if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10614
            class_to_search = EXPR_WFL_NODE (wfl);
10615
          else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10616
                   this_identifier_node)
10617
            class_to_search = NULL_TREE;
10618
          else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10619
                   super_identifier_node)
10620
            {
10621
              is_super_init = 1;
10622
              if (CLASSTYPE_SUPER (current_class))
10623
                class_to_search =
10624
                  DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10625
              else
10626
                {
10627
                  parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10628
                  PATCH_METHOD_RETURN_ERROR ();
10629
                }
10630
            }
10631
 
10632
          /* Class to search is NULL if we're searching the current one */
10633
          if (class_to_search)
10634
            {
10635
              class_to_search = resolve_and_layout (class_to_search, wfl);
10636
 
10637
              if (!class_to_search)
10638
                {
10639
                  parse_error_context
10640
                    (wfl, "Class %qs not found in type declaration",
10641
                     IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10642
                  PATCH_METHOD_RETURN_ERROR ();
10643
                }
10644
 
10645
              /* Can't instantiate an abstract class, but we can
10646
                 invoke it's constructor. It's use within the `new'
10647
                 context is denied here. */
10648
              if (CLASS_ABSTRACT (class_to_search)
10649
                  && TREE_CODE (patch) == NEW_CLASS_EXPR)
10650
                {
10651
                  parse_error_context
10652
                    (wfl, "Class %qs is an abstract class. It can't be instantiated",
10653
                     IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10654
                  PATCH_METHOD_RETURN_ERROR ();
10655
                }
10656
 
10657
              class_to_search = TREE_TYPE (class_to_search);
10658
            }
10659
          else
10660
            class_to_search = current_class;
10661
          lc = 1;
10662
        }
10663
      /* This is a regular search in the local class, unless an
10664
         alternate class is specified. */
10665
      else
10666
        {
10667
          if (where != NULL_TREE)
10668
            class_to_search = where;
10669
          else if (QUALIFIED_P (name))
10670
            class_to_search = current_class;
10671
          else
10672
            {
10673
              class_to_search = current_class;
10674
 
10675
              for (;;)
10676
                {
10677
                  if (has_method (class_to_search, name))
10678
                    break;
10679
                  if (! INNER_CLASS_TYPE_P (class_to_search))
10680
                    {
10681
                      parse_error_context (wfl,
10682
                                           "No method named %qs in scope",
10683
                                           IDENTIFIER_POINTER (name));
10684
                      PATCH_METHOD_RETURN_ERROR ();
10685
                    }
10686
                  class_to_search
10687
                    = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10688
                }
10689
            }
10690
          lc = 0;
10691
        }
10692
 
10693
      /* NAME is a simple identifier or comes from a primary. Search
10694
         in the class whose declaration contain the method being
10695
         invoked. */
10696
      resolve_and_layout (class_to_search, NULL_TREE);
10697
 
10698
      list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10699
      /* Don't continue if no method were found, as the next statement
10700
         can't be executed then. */
10701
      if (!list)
10702
        PATCH_METHOD_RETURN_ERROR ();
10703
 
10704
      if (TYPE_ARRAY_P (class_to_search)
10705
          && DECL_NAME (list) == get_identifier ("clone"))
10706
        is_array_clone_call = 1;
10707
 
10708
      /* Check for static reference of non static methods.  */
10709
      if (check_for_static_method_reference (wfl, patch, list,
10710
                                             class_to_search, primary))
10711
        PATCH_METHOD_RETURN_ERROR ();
10712
 
10713
      /* Check for inner classes creation from illegal contexts */
10714
      if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10715
                 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10716
          && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10717
          && !DECL_INIT_P (current_function_decl))
10718
        {
10719
          parse_error_context
10720
            (wfl, "No enclosing instance for inner class %qs is in scope%s",
10721
             lang_printable_name (class_to_search, 0),
10722
             (!current_this ? "" :
10723
              "; an explicit one must be provided when creating this inner class"));
10724
          PATCH_METHOD_RETURN_ERROR ();
10725
        }
10726
 
10727
      /* Non static methods are called with the current object extra
10728
         argument.  If PATCH is a `new TYPE()', the argument is the value
10729
         returned by the object allocator.  If method is resolved as a
10730
         primary, use the primary otherwise use the current THIS. */
10731
      args = nreverse (args);
10732
      if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10733
        {
10734
          this_arg = primary ? primary : current_this;
10735
 
10736
          /* If we're using an access method, things are different.
10737
             There are two family of cases:
10738
 
10739
             1) We're not generating bytecodes:
10740
 
10741
             - LIST is non-static.  Its invocation is transformed from
10742
               x(a1,...,an) into this$.x(a1,....an).
10743
             - LIST is static.  Its invocation is transformed from
10744
               x(a1,...,an) into TYPE_OF(this$).x(a1,....an)
10745
 
10746
             2) We're generating bytecodes:
10747
 
10748
             - LIST is non-static.  Its invocation is transformed from
10749
               x(a1,....,an) into access$(this$,a1,...,an).
10750
             - LIST is static.  Its invocation is transformed from
10751
               x(a1,....,an) into TYPE_OF(this$).x(a1,....an).
10752
 
10753
             Of course, this$ can be arbitrarily complex, ranging from
10754
             this$0 (the immediate outer context) to
10755
             access$0(access$0(...(this$0))).
10756
 
10757
             maybe_use_access_method returns a nonzero value if the
10758
             this_arg has to be moved into the (then generated) stub
10759
             argument list.  In the meantime, the selected function
10760
             might have been replaced by a generated stub.  */
10761
          if (METHOD_STATIC (list))
10762
            maybe_use_access_method (0, &list, NULL);
10763
          else if (!primary &&
10764
                   maybe_use_access_method (is_super_init, &list, &this_arg))
10765
            {
10766
              args = tree_cons (NULL_TREE, this_arg, args);
10767
              this_arg = NULL_TREE; /* So it doesn't get chained twice */
10768
            }
10769
        }
10770
    }
10771
 
10772
  /* Merge point of all resolution schemes. If we have nothing, this
10773
     is an error, already signaled */
10774
  if (!list)
10775
    PATCH_METHOD_RETURN_ERROR ();
10776
 
10777
  /* Check accessibility, position the is_static flag, build and
10778
     return the call */
10779
  if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10780
                        (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10781
                         NULL_TREE), from_super)
10782
      /* Calls to clone() on array types are permitted as a special-case. */
10783
      && !is_array_clone_call)
10784
    {
10785
      const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10786
      const char *const access =
10787
        accessibility_string (get_access_flags_from_decl (list));
10788
      const char *const klass =
10789
        IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10790
      const char *const refklass =
10791
        IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10792
      const char *const what = (DECL_CONSTRUCTOR_P (list)
10793
                                ? "constructor" : "method");
10794
      parse_error_context (wfl,
10795
                           "Can't access %s %s %<%s.%s%> from %qs",
10796
                           access, what, klass, fct_name, refklass);
10797
      PATCH_METHOD_RETURN_ERROR ();
10798
    }
10799
 
10800
  /* Deprecation check: check whether the method being invoked or the
10801
     instance-being-created's type are deprecated.  */
10802
  if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10803
    check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10804
  check_deprecation (wfl, list);
10805
 
10806
  /* If invoking a innerclass constructor, there are hidden parameters
10807
     to pass */
10808
  if (TREE_CODE (patch) == NEW_CLASS_EXPR
10809
      && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10810
    {
10811
      /* And make sure we add the accessed local variables to be saved
10812
         in field aliases. */
10813
      args = build_alias_initializer_parameter_list
10814
        (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10815
 
10816
      /* Secretly pass the current_this/primary as a second argument */
10817
      if (primary || current_this)
10818
        {
10819
          tree extra_arg;
10820
          tree this_type = (current_this ?
10821
                            TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10822
          /* Method's (list) enclosing context */
10823
          tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10824
          /* If we have a primary, use it. */
10825
          if (primary)
10826
            extra_arg = primary;
10827
          /* The current `this' is an inner class but isn't a direct
10828
             enclosing context for the inner class we're trying to
10829
             create. Build an access to the proper enclosing context
10830
             and use it. */
10831
          else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10832
                   && this_type != TREE_TYPE (mec))
10833
            {
10834
 
10835
              extra_arg = build_access_to_thisn (current_class,
10836
                                                 TREE_TYPE (mec), 0);
10837
              extra_arg = java_complete_tree (extra_arg);
10838
            }
10839
          /* Otherwise, just use the current `this' as an enclosing
10840
             context. */
10841
          else
10842
            extra_arg = current_this;
10843
          args = tree_cons (NULL_TREE, extra_arg, args);
10844
        }
10845
      else
10846
        args = tree_cons (NULL_TREE, integer_zero_node, args);
10847
    }
10848
 
10849
  /* This handles the situation where a constructor invocation needs
10850
     to have an enclosing context passed as a second parameter (the
10851
     constructor is one of an inner class). */
10852
  if ((is_super_init ||
10853
       (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10854
      && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10855
    {
10856
      tree dest = TYPE_NAME (DECL_CONTEXT (list));
10857
      tree extra_arg =
10858
        build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10859
      extra_arg = java_complete_tree (extra_arg);
10860
      args = tree_cons (NULL_TREE, extra_arg, args);
10861
    }
10862
 
10863
  is_static_flag = METHOD_STATIC (list);
10864
  if (! is_static_flag && this_arg != NULL_TREE)
10865
    args = tree_cons (NULL_TREE, this_arg, args);
10866
 
10867
  /* In the context of an explicit constructor invocation, we can't
10868
     invoke any method relying on `this'. Exceptions are: we're
10869
     invoking a static function, primary exists and is not the current
10870
     this, we're creating a new object. */
10871
  if (ctxp->explicit_constructor_p
10872
      && !is_static_flag
10873
      && (!primary || primary == current_this)
10874
      && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10875
    {
10876
      parse_error_context (wfl, "Can't reference % before the superclass constructor has been called");
10877
      PATCH_METHOD_RETURN_ERROR ();
10878
    }
10879
  java_parser_context_restore_global ();
10880
  if (is_static)
10881
    *is_static = is_static_flag;
10882
  /* Sometimes, we want the decl of the selected method. Such as for
10883
     EH checking */
10884
  if (ret_decl)
10885
    *ret_decl = list;
10886
  patch = patch_invoke (patch, list, args);
10887
 
10888
  /* Now is a good time to insert the call to finit$ */
10889
  if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10890
    {
10891
      tree finit_parms, finit_call;
10892
 
10893
      /* Prepare to pass hidden parameters to finit$, if any. */
10894
      finit_parms = build_alias_initializer_parameter_list
10895
        (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10896
 
10897
      finit_call =
10898
        build_method_invocation (build_wfl_node (finit_identifier_node),
10899
                                 finit_parms);
10900
 
10901
      /* Generate the code used to initialize fields declared with an
10902
         initialization statement and build a compound statement along
10903
         with the super constructor invocation. */
10904
      CAN_COMPLETE_NORMALLY (patch) = 1;
10905
      patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10906
                      java_complete_tree (finit_call));
10907
    }
10908
  return patch;
10909
}
10910
 
10911
/* Check that we're not trying to do a static reference to a method in
10912
   non static method. Return 1 if it's the case, 0 otherwise. */
10913
 
10914
static int
10915
check_for_static_method_reference (tree wfl, tree node, tree method,
10916
                                   tree where, tree primary)
10917
{
10918
  if (METHOD_STATIC (current_function_decl)
10919
      && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10920
    {
10921
      char *fct_name = xstrdup (lang_printable_name (method, 0));
10922
      parse_error_context
10923
        (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10924
         lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10925
         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10926
      free (fct_name);
10927
      return 1;
10928
    }
10929
  return 0;
10930
}
10931
 
10932
/* Fix the invocation of *MDECL if necessary in the case of an
10933
   invocation across a nested class.  *THIS_ARG might be modified
10934
   appropriately and an alternative access to *MDECL might be
10935
   returned.  */
10936
 
10937
static int
10938
maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10939
{
10940
  tree ctx;
10941
  tree md = *mdecl, ta = NULL_TREE;
10942
  int to_return = 0;
10943
  int non_static_context = !METHOD_STATIC (md);
10944
 
10945
  if (is_super_init
10946
      || DECL_FINIT_P (md)
10947
      || DECL_INSTINIT_P (md)
10948
      || !nested_member_access_p (current_class, md))
10949
    return 0;
10950
 
10951
  /* If we're calling a method found in an enclosing class, generate
10952
     what it takes to retrieve the right `this'.  Don't do that if we're
10953
     invoking a static method.  Note that if MD's type is unrelated to
10954
     CURRENT_CLASS, then the current this can be used. */
10955
 
10956
  if (non_static_context
10957
      && !inherits_from_p (current_class, DECL_CONTEXT (md))
10958
      && DECL_CONTEXT (TYPE_NAME (current_class)))
10959
    {
10960
      ta = *this_arg;
10961
      ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10962
      if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10963
        {
10964
          ta = build_current_thisn (current_class);
10965
          ta = build_wfl_node (ta);
10966
        }
10967
      else
10968
        {
10969
          tree type = ctx;
10970
          while (type)
10971
            {
10972
              maybe_build_thisn_access_method (type);
10973
              if (inherits_from_p (type, DECL_CONTEXT (md)))
10974
                {
10975
                  ta = build_access_to_thisn (ctx, type, 0);
10976
                  break;
10977
                }
10978
              type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10979
                      TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10980
            }
10981
        }
10982
      ta = java_complete_tree (ta);
10983
    }
10984
 
10985
  /* We might have to use an access method to get to MD. We can
10986
     break the method access rule as long as we're not generating
10987
     bytecode.  */
10988
  if (METHOD_PRIVATE (md) && flag_emit_class_files)
10989
    {
10990
      md = build_nested_method_access_method (md);
10991
      to_return = 1;
10992
    }
10993
 
10994
  *mdecl = md;
10995
  if (this_arg)
10996
    *this_arg = ta;
10997
 
10998
  /* Returning a nonzero value indicates we were doing a non static
10999
     method invocation that is now a static invocation. It will have
11000
     callee displace `this' to insert it in the regular argument
11001
     list. */
11002
  return (non_static_context && to_return);
11003
}
11004
 
11005
/* Patch an invoke expression METHOD and ARGS, based on its invocation
11006
   mode.  */
11007
 
11008
static tree
11009
patch_invoke (tree patch, tree method, tree args)
11010
{
11011
  tree dtable, func;
11012
  tree original_call, t, ta;
11013
  tree check = NULL_TREE;
11014
 
11015
  /* Last step for args: convert build-in types. If we're dealing with
11016
     a new TYPE() type call, the first argument to the constructor
11017
     isn't found in the incoming argument list, but delivered by
11018
     `new' */
11019
  t = TYPE_ARG_TYPES (TREE_TYPE (method));
11020
  if (TREE_CODE (patch) == NEW_CLASS_EXPR)
11021
    t = TREE_CHAIN (t);
11022
  for (ta = args; t != end_params_node && ta;
11023
       t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
11024
    if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
11025
        TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
11026
      TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
11027
 
11028
  /* Resolve unresolved returned type issues */
11029
  t = TREE_TYPE (TREE_TYPE (method));
11030
  if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
11031
    resolve_and_layout (TREE_TYPE (t), NULL);
11032
 
11033
  if (flag_emit_class_files)
11034
    func = method;
11035
  else
11036
    {
11037
      switch (invocation_mode (method, CALL_USING_SUPER (patch)))
11038
        {
11039
        case INVOKE_VIRTUAL:
11040
          dtable = invoke_build_dtable (0, args);
11041
          func = build_invokevirtual (dtable, method);
11042
          break;
11043
 
11044
        case INVOKE_NONVIRTUAL:
11045
          /* If the object for the method call is null, we throw an
11046
             exception.  We don't do this if the object is the current
11047
             method's `this'.  In other cases we just rely on an
11048
             optimization pass to eliminate redundant checks.  */
11049
          if (TREE_VALUE (args) != current_this)
11050
            {
11051
              /* We use a save_expr here to make sure we only evaluate
11052
                 the new `self' expression once.  */
11053
              tree save_arg = save_expr (TREE_VALUE (args));
11054
              TREE_VALUE (args) = save_arg;
11055
              check = java_check_reference (save_arg, 1);
11056
            }
11057
          /* Fall through.  */
11058
 
11059
        case INVOKE_SUPER:
11060
        case INVOKE_STATIC:
11061
          {
11062
            tree signature = build_java_signature (TREE_TYPE (method));
11063
            func = build_known_method_ref (method, TREE_TYPE (method),
11064
                                           DECL_CONTEXT (method),
11065
                                           signature, args);
11066
          }
11067
          break;
11068
 
11069
        case INVOKE_INTERFACE:
11070
          dtable = invoke_build_dtable (1, args);
11071
          func = build_invokeinterface (dtable, method);
11072
          break;
11073
 
11074
        default:
11075
          abort ();
11076
        }
11077
 
11078
      /* Ensure self_type is initialized, (invokestatic). FIXME */
11079
      func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
11080
    }
11081
 
11082
  TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
11083
  TREE_OPERAND (patch, 0) = func;
11084
  TREE_OPERAND (patch, 1) = args;
11085
  patch = check_for_builtin (method, patch);
11086
  original_call = patch;
11087
 
11088
  /* We're processing a `new TYPE ()' form. New is called and its
11089
     returned value is the first argument to the constructor. We build
11090
     a COMPOUND_EXPR and use saved expression so that the overall NEW
11091
     expression value is a pointer to a newly created and initialized
11092
     class. */
11093
  if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
11094
    {
11095
      tree class = DECL_CONTEXT (method);
11096
      tree c1, saved_new, new;
11097
      tree alloc_node;
11098
 
11099
      if (flag_emit_class_files)
11100
        {
11101
          TREE_TYPE (patch) = build_pointer_type (class);
11102
          return patch;
11103
        }
11104
      if (!TYPE_SIZE (class))
11105
        safe_layout_class (class);
11106
      alloc_node =
11107
        (class_has_finalize_method (class) ? alloc_object_node
11108
                                           : alloc_no_finalizer_node);
11109
      new = build3 (CALL_EXPR, promote_type (class),
11110
                    build_address_of (alloc_node),
11111
                    build_tree_list (NULL_TREE, build_class_ref (class)),
11112
                    NULL_TREE);
11113
      saved_new = save_expr (new);
11114
      c1 = build_tree_list (NULL_TREE, saved_new);
11115
      TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
11116
      TREE_OPERAND (original_call, 1) = c1;
11117
      TREE_SET_CODE (original_call, CALL_EXPR);
11118
      patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
11119
    }
11120
 
11121
  /* If CHECK is set, then we are building a check to see if the object
11122
     is NULL.  */
11123
  if (check != NULL_TREE)
11124
    {
11125
      /* We have to call force_evaluation_order now because creating a
11126
         COMPOUND_EXPR wraps the arg list in a way that makes it
11127
         unrecognizable by force_evaluation_order later.  Yuk.  */
11128
      patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
11129
                      force_evaluation_order (patch));
11130
      TREE_SIDE_EFFECTS (patch) = 1;
11131
    }
11132
 
11133
  /* In order to be able to modify PATCH later, we SAVE_EXPR it and
11134
     put it as the first expression of a COMPOUND_EXPR. The second
11135
     expression being an empty statement to be later patched if
11136
     necessary. We remember a TREE_LIST (the PURPOSE is the method,
11137
     the VALUE is the compound) in a hashtable and return a
11138
     COMPOUND_EXPR built so that the result of the evaluation of the
11139
     original PATCH node is returned. */
11140
  if (STATIC_CLASS_INIT_OPT_P ()
11141
      && current_function_decl && METHOD_STATIC (method))
11142
    {
11143
      tree list;
11144
      tree fndecl = current_function_decl;
11145
      /* We have to call force_evaluation_order now because creating a
11146
         COMPOUND_EXPR wraps the arg list in a way that makes it
11147
         unrecognizable by force_evaluation_order later.  Yuk.  */
11148
      tree save = force_evaluation_order (patch);
11149
      tree type = TREE_TYPE (patch);
11150
 
11151
      patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11152
      list = tree_cons (method, patch,
11153
                        DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11154
 
11155
      DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11156
 
11157
      patch = build2 (COMPOUND_EXPR, type, patch, save);
11158
    }
11159
 
11160
  return patch;
11161
}
11162
 
11163
static int
11164
invocation_mode (tree method, int super)
11165
{
11166
  int access = get_access_flags_from_decl (method);
11167
 
11168
  if (super)
11169
    return INVOKE_SUPER;
11170
 
11171
  if (access & ACC_STATIC)
11172
    return INVOKE_STATIC;
11173
 
11174
  /* We have to look for a constructor before we handle nonvirtual
11175
     calls; otherwise the constructor will look nonvirtual.  */
11176
  if (DECL_CONSTRUCTOR_P (method))
11177
    return INVOKE_STATIC;
11178
 
11179
  if (access & ACC_PRIVATE)
11180
    return INVOKE_NONVIRTUAL;
11181
 
11182
  /* Binary compatibility: just because it's final today, that doesn't
11183
     mean it'll be final tomorrow.  */
11184
  if (! flag_indirect_dispatch
11185
      || DECL_CONTEXT (method) == object_type_node)
11186
    {
11187
      if (access & ACC_FINAL)
11188
        return INVOKE_NONVIRTUAL;
11189
 
11190
      if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11191
        return INVOKE_NONVIRTUAL;
11192
    }
11193
 
11194
  if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11195
    return INVOKE_INTERFACE;
11196
 
11197
  return INVOKE_VIRTUAL;
11198
}
11199
 
11200
/* Retrieve a refined list of matching methods. It covers the step
11201
   15.11.2 (Compile-Time Step 2) */
11202
 
11203
static tree
11204
lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11205
{
11206
  tree atl = end_params_node;           /* Arg Type List */
11207
  tree method, signature, list, node;
11208
  const char *candidates;               /* Used for error report */
11209
  char *dup;
11210
 
11211
  /* Fix the arguments */
11212
  for (node = arg_list; node; node = TREE_CHAIN (node))
11213
    {
11214
      tree current_arg = TREE_TYPE (TREE_VALUE (node));
11215
      /* Non primitive type may have to be resolved */
11216
      if (!JPRIMITIVE_TYPE_P (current_arg))
11217
        resolve_and_layout (current_arg, NULL_TREE);
11218
      /* And promoted */
11219
      if (TREE_CODE (current_arg) == RECORD_TYPE)
11220
        current_arg = promote_type (current_arg);
11221
      /* If we're building an anonymous constructor call, and one of
11222
         the arguments has array type, cast it to a size-less array
11223
         type.  This prevents us from getting a strange gcj-specific
11224
         "sized array" signature in the constructor's signature.  */
11225
      if (lc && ANONYMOUS_CLASS_P (class)
11226
          && TREE_CODE (current_arg) == POINTER_TYPE
11227
          && TYPE_ARRAY_P (TREE_TYPE (current_arg)))
11228
        {
11229
          tree elt = TYPE_ARRAY_ELEMENT (TREE_TYPE (current_arg));
11230
          current_arg = build_pointer_type (build_java_array_type (elt, -1));
11231
        }
11232
      atl = tree_cons (NULL_TREE, current_arg, atl);
11233
    }
11234
 
11235
  /* Presto. If we're dealing with an anonymous class and a
11236
     constructor call, generate the right constructor now, since we
11237
     know the arguments' types. */
11238
 
11239
  if (lc && ANONYMOUS_CLASS_P (class))
11240
    {
11241
      tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11242
      /* The anonymous class may have already been laid out, so make sure
11243
         the new constructor is laid out here.  */
11244
      layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11245
    }
11246
 
11247
  /* Find all candidates and then refine the list, searching for the
11248
     most specific method. */
11249
  list = find_applicable_accessible_methods_list (lc, class, name, atl);
11250
  list = find_most_specific_methods_list (list);
11251
  if (list && !TREE_CHAIN (list))
11252
    return TREE_VALUE (list);
11253
 
11254
  /* Issue an error. List candidates if any. Candidates are listed
11255
     only if accessible (non accessible methods may end-up here for
11256
     the sake of a better error report). */
11257
  candidates = NULL;
11258
  if (list)
11259
    {
11260
      tree current;
11261
      obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11262
      for (current = list; current; current = TREE_CHAIN (current))
11263
        {
11264
          tree cm = TREE_VALUE (current);
11265
          char string [4096];
11266
          if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11267
            continue;
11268
          sprintf
11269
            (string, "  '%s' in '%s'%s",
11270
             get_printable_method_name (cm),
11271
             IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11272
             (TREE_CHAIN (current) ? "\n" : ""));
11273
          obstack_grow (&temporary_obstack, string, strlen (string));
11274
        }
11275
      obstack_1grow (&temporary_obstack, '\0');
11276
      candidates = obstack_finish (&temporary_obstack);
11277
    }
11278
  /* Issue the error message */
11279
  method = make_node (FUNCTION_TYPE);
11280
  TYPE_ARG_TYPES (method) = atl;
11281
  signature = build_java_argument_signature (method);
11282
  dup = xstrdup (lang_printable_name (class, 0));
11283
  parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11284
                       (lc ? "constructor" : "method"),
11285
                       (lc ? dup : IDENTIFIER_POINTER (name)),
11286
                       IDENTIFIER_POINTER (signature), dup,
11287
                       (candidates ? candidates : ""));
11288
  free (dup);
11289
  return NULL_TREE;
11290
}
11291
 
11292
/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11293
   when we're looking for a constructor. */
11294
 
11295
static tree
11296
find_applicable_accessible_methods_list (int lc, tree class, tree name,
11297
                                         tree arglist)
11298
{
11299
  static htab_t searched_classes;
11300
  static int search_not_done = 0;
11301
  tree list = NULL_TREE, all_list = NULL_TREE;
11302
  tree base_binfo;
11303
  int i;
11304
 
11305
  /* Check the hash table to determine if this class has been searched
11306
     already. */
11307
  if (searched_classes)
11308
    {
11309
      if (htab_find (searched_classes, class) != NULL)
11310
        return NULL;
11311
    }
11312
  else
11313
    {
11314
      searched_classes = htab_create (10, htab_hash_pointer,
11315
                                      htab_eq_pointer, NULL);
11316
    }
11317
 
11318
  search_not_done++;
11319
  *htab_find_slot (searched_classes, class, INSERT) = class;
11320
 
11321
  if (!CLASS_LOADED_P (class))
11322
    {
11323
      load_class (class, 1);
11324
      safe_layout_class (class);
11325
    }
11326
 
11327
  /* Search interfaces */
11328
  if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11329
      && CLASS_INTERFACE (TYPE_NAME (class)))
11330
    {
11331
      search_applicable_methods_list (lc, TYPE_METHODS (class),
11332
                                      name, arglist, &list, &all_list);
11333
      for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11334
        {
11335
          tree t = BINFO_TYPE (base_binfo);
11336
          tree rlist;
11337
 
11338
          rlist = find_applicable_accessible_methods_list (lc,  t, name,
11339
                                                           arglist);
11340
          list = chainon (rlist, list);
11341
        }
11342
    }
11343
  /* Search classes */
11344
  else
11345
    {
11346
      search_applicable_methods_list (lc, TYPE_METHODS (class),
11347
                                      name, arglist, &list, &all_list);
11348
 
11349
      /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11350
         that we only search in class. Note that we should have found
11351
         something at this point. */
11352
      if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11353
        {
11354
          lc = 1;
11355
          if (!list)
11356
            abort ();
11357
        }
11358
 
11359
      /* We must search all interfaces of this class */
11360
      if (!lc)
11361
        {
11362
          for (i = 1;
11363
               BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11364
            {
11365
              tree t = BINFO_TYPE (base_binfo);
11366
              if (t != object_type_node)
11367
                {
11368
                  tree rlist
11369
                    = find_applicable_accessible_methods_list (lc, t,
11370
                                                               name, arglist);
11371
                  list = chainon (rlist, list);
11372
                }
11373
            }
11374
        }
11375
 
11376
      /* Search superclass */
11377
      if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11378
        {
11379
          tree rlist;
11380
          class = CLASSTYPE_SUPER (class);
11381
          rlist = find_applicable_accessible_methods_list (lc, class,
11382
                                                           name, arglist);
11383
          list = chainon (rlist, list);
11384
        }
11385
    }
11386
 
11387
  search_not_done--;
11388
 
11389
  /* We're done. Reset the searched classes list and finally search
11390
     java.lang.Object if it wasn't searched already. */
11391
  if (!search_not_done)
11392
    {
11393
      if (!lc
11394
          && TYPE_METHODS (object_type_node)
11395
          && htab_find (searched_classes, object_type_node) == NULL)
11396
        {
11397
          search_applicable_methods_list (lc,
11398
                                          TYPE_METHODS (object_type_node),
11399
                                          name, arglist, &list, &all_list);
11400
        }
11401
      htab_delete (searched_classes);
11402
      searched_classes = NULL;
11403
    }
11404
 
11405
  /* Either return the list obtained or all selected (but
11406
     inaccessible) methods for better error report. */
11407
  return (!list ? all_list : list);
11408
}
11409
 
11410
/* Effectively search for the appropriate method in method */
11411
 
11412
static void
11413
search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11414
                                tree *list, tree *all_list)
11415
{
11416
  for (; method; method = TREE_CHAIN (method))
11417
    {
11418
      /* When dealing with constructor, stop here, otherwise search
11419
         other classes */
11420
      if (lc && !DECL_CONSTRUCTOR_P (method))
11421
        continue;
11422
      else if (!lc && (DECL_CONSTRUCTOR_P (method)
11423
                       || (DECL_NAME (method) != name)))
11424
        continue;
11425
 
11426
      if (argument_types_convertible (method, arglist))
11427
        {
11428
          /* Retain accessible methods only */
11429
          if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11430
                                 method, NULL_TREE, 0))
11431
            *list = tree_cons (NULL_TREE, method, *list);
11432
          else
11433
            /* Also retain all selected method here */
11434
            *all_list = tree_cons (NULL_TREE, method, *list);
11435
        }
11436
    }
11437
}
11438
 
11439
/* 15.11.2.2 Choose the Most Specific Method */
11440
 
11441
static tree
11442
find_most_specific_methods_list (tree list)
11443
{
11444
  int max = 0;
11445
  int abstract, candidates;
11446
  tree current, new_list = NULL_TREE;
11447
  for (current = list; current; current = TREE_CHAIN (current))
11448
    {
11449
      tree method;
11450
      DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11451
 
11452
      for (method = list; method; method = TREE_CHAIN (method))
11453
        {
11454
          tree method_v, current_v;
11455
          /* Don't test a method against itself */
11456
          if (method == current)
11457
            continue;
11458
 
11459
          method_v = TREE_VALUE (method);
11460
          current_v = TREE_VALUE (current);
11461
 
11462
          /* Compare arguments and location where methods where declared */
11463
          if (argument_types_convertible (method_v, current_v))
11464
            {
11465
              if (valid_method_invocation_conversion_p
11466
                  (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v)))
11467
                {
11468
                  int v = (DECL_SPECIFIC_COUNT (current_v) += 1);
11469
                  max = (v > max ? v : max);
11470
                }
11471
            }
11472
        }
11473
    }
11474
 
11475
  /* Review the list and select the maximally specific methods */
11476
  for (current = list, abstract = -1, candidates = -1;
11477
       current; current = TREE_CHAIN (current))
11478
    if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11479
      {
11480
        new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11481
        abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11482
        candidates++;
11483
      }
11484
 
11485
  /* If we have several and they're all abstract, just pick the
11486
     closest one. */
11487
  if (candidates > 0 && candidates == abstract)
11488
    {
11489
      /* FIXME: merge the throws clauses.  There is no convenient way
11490
         to do this in gcj right now, since ideally we'd like to
11491
         introduce a new METHOD_DECL here, but that is really not
11492
         possible.  */
11493
      new_list = nreverse (new_list);
11494
      TREE_CHAIN (new_list) = NULL_TREE;
11495
      return new_list;
11496
    }
11497
 
11498
  /* We have several (we couldn't find a most specific), all but one
11499
     are abstract, we pick the only non abstract one. */
11500
  if (candidates > 0 && (candidates == abstract+1))
11501
    {
11502
      for (current = new_list; current; current = TREE_CHAIN (current))
11503
        if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11504
          {
11505
            TREE_CHAIN (current) = NULL_TREE;
11506
            new_list = current;
11507
          }
11508
    }
11509
 
11510
  /* If we can't find one, lower expectations and try to gather multiple
11511
     maximally specific methods */
11512
  while (!new_list && max)
11513
    {
11514
      while (--max > 0)
11515
        {
11516
          if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11517
            new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11518
        }
11519
    }
11520
 
11521
  return new_list;
11522
}
11523
 
11524
/* Make sure that the type of each M2_OR_ARGLIST arguments can be
11525
   converted by method invocation conversion (5.3) to the type of the
11526
   corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11527
   to change less often than M1. */
11528
 
11529
static GTY(()) tree m2_arg_value;
11530
static GTY(()) tree m2_arg_cache;
11531
 
11532
static int
11533
argument_types_convertible (tree m1, tree m2_or_arglist)
11534
{
11535
  tree m1_arg, m2_arg;
11536
 
11537
  SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11538
 
11539
  if (m2_arg_value == m2_or_arglist)
11540
    m2_arg = m2_arg_cache;
11541
  else
11542
    {
11543
      /* M2_OR_ARGLIST can be a function DECL or a raw list of
11544
         argument types */
11545
      if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11546
        {
11547
          m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11548
          if (!METHOD_STATIC (m2_or_arglist))
11549
            m2_arg = TREE_CHAIN (m2_arg);
11550
        }
11551
      else
11552
        m2_arg = m2_or_arglist;
11553
 
11554
      m2_arg_value = m2_or_arglist;
11555
      m2_arg_cache = m2_arg;
11556
    }
11557
 
11558
  while (m1_arg != end_params_node && m2_arg != end_params_node)
11559
    {
11560
      resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11561
      if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11562
                                                 TREE_VALUE (m2_arg)))
11563
        break;
11564
      m1_arg = TREE_CHAIN (m1_arg);
11565
      m2_arg = TREE_CHAIN (m2_arg);
11566
    }
11567
  return m1_arg == end_params_node && m2_arg == end_params_node;
11568
}
11569
 
11570
/* Qualification routines */
11571
 
11572
/* Given a name x.y.z, look up x locally.  If it's found, save the
11573
   decl.  If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11574
   so that we later try and load the appropriate classes.  */
11575
static void
11576
qualify_ambiguous_name (tree id)
11577
{
11578
  tree name, decl;
11579
 
11580
  /* We inspect the first item of the qualification list.  As a sanity
11581
     check, make sure that it is an identfier node.  */
11582
  tree qual = EXPR_WFL_QUALIFICATION (id);
11583
  tree qual_wfl = QUAL_WFL (qual);
11584
 
11585
  if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11586
    return;
11587
 
11588
  name = EXPR_WFL_NODE (qual_wfl);
11589
 
11590
  /* If we don't have an identifier, or we have a 'this' or 'super',
11591
     then field access processing is all we need : there is nothing
11592
     for us to do.  */
11593
  if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11594
      name == this_identifier_node ||
11595
      name == super_identifier_node)
11596
    return;
11597
 
11598
  /* If name appears within the scope of a local variable declaration
11599
     or parameter declaration, or is a field within an enclosing
11600
     class, then it is an expression name.  Save the decl and let
11601
     resolve_field_access do it's work.  */
11602
  if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11603
      (decl = lookup_field_wrapper (current_class, name)))
11604
    {
11605
      QUAL_RESOLUTION (qual) = decl;
11606
      return;
11607
    }
11608
 
11609
  /* If name is a known class name (either declared or imported), mark
11610
     us as a type name.  */
11611
  if ((decl = resolve_and_layout (name, NULL_TREE)))
11612
    {
11613
      RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11614
      QUAL_RESOLUTION (qual) = decl;
11615
    }
11616
 
11617
  /* Check here that NAME isn't declared by more than one
11618
     type-import-on-demand declaration of the compilation unit
11619
     containing NAME. FIXME */
11620
 
11621
  /* We couldn't find a declaration for the name.  Assume for now that
11622
     we have a qualified class name that needs to be loaded from an
11623
     external class file.  */
11624
  else
11625
    RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11626
 
11627
  /* Propagate the qualification across other components of the
11628
     qualified name */
11629
  for (qual = TREE_CHAIN (qual); qual;
11630
       qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11631
    {
11632
      if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11633
        RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11634
    }
11635
 
11636
  /* Store the global qualification for the ambiguous part of ID back
11637
     into ID fields */
11638
  if (RESOLVE_TYPE_NAME_P (qual_wfl))
11639
    RESOLVE_TYPE_NAME_P (id) = 1;
11640
  else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11641
    RESOLVE_PACKAGE_NAME_P (id) = 1;
11642
}
11643
 
11644
/* Patch tree nodes in a function body. When a BLOCK is found, push
11645
   local variable decls if present.
11646
   Same as java_complete_lhs, but does resolve static finals to values. */
11647
 
11648
static tree
11649
java_complete_tree (tree node)
11650
{
11651
  node = java_complete_lhs (node);
11652
  if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11653
      && DECL_INITIAL (node) != NULL_TREE)
11654
    {
11655
      tree value = fold_constant_for_init (node, node);
11656
      if (value != NULL_TREE)
11657
        return value;
11658
    }
11659
  return node;
11660
}
11661
 
11662
static tree
11663
java_stabilize_reference (tree node)
11664
{
11665
  if (TREE_CODE (node) == COMPOUND_EXPR)
11666
    {
11667
      tree op0 = TREE_OPERAND (node, 0);
11668
      tree op1 = TREE_OPERAND (node, 1);
11669
      TREE_OPERAND (node, 0) = save_expr (op0);
11670
      TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11671
      return node;
11672
    }
11673
  return stabilize_reference (node);
11674
}
11675
 
11676
/* Patch tree nodes in a function body. When a BLOCK is found, push
11677
   local variable decls if present.
11678
   Same as java_complete_tree, but does not resolve static finals to values. */
11679
 
11680
static tree
11681
java_complete_lhs (tree node)
11682
{
11683
  tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11684
  int flag;
11685
 
11686
  /* CONVERT_EXPR always has its type set, even though it needs to be
11687
     worked out. */
11688
  if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11689
    return node;
11690
 
11691
  /* The switch block implements cases processing container nodes
11692
     first.  Contained nodes are always written back. Leaves come
11693
     next and return a value. */
11694
  switch (TREE_CODE (node))
11695
    {
11696
    case BLOCK:
11697
 
11698
      /* 1- Block section.
11699
         Set the local values on decl names so we can identify them
11700
         faster when they're referenced. At that stage, identifiers
11701
         are legal so we don't check for declaration errors. */
11702
      for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11703
        {
11704
          DECL_CONTEXT (cn) = current_function_decl;
11705
          IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11706
        }
11707
      if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11708
          CAN_COMPLETE_NORMALLY (node) = 1;
11709
      else
11710
        {
11711
          tree stmt = BLOCK_EXPR_BODY (node);
11712
          tree *ptr;
11713
          int error_seen = 0;
11714
          if (TREE_CODE (stmt) == COMPOUND_EXPR)
11715
            {
11716
              /* Re-order from (((A; B); C); ...; Z) to
11717
                 (A; (B; (C ; (...; Z)))).
11718
                 This makes it easier to scan the statements left-to-right
11719
                 without using recursion (which might overflow the stack
11720
                 if the block has many statements. */
11721
              for (;;)
11722
                {
11723
                  tree left = TREE_OPERAND (stmt, 0);
11724
                  if (TREE_CODE (left) != COMPOUND_EXPR)
11725
                    break;
11726
                  TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11727
                  TREE_OPERAND (left, 1) = stmt;
11728
                  stmt = left;
11729
                }
11730
              BLOCK_EXPR_BODY (node) = stmt;
11731
            }
11732
 
11733
          /* Now do the actual complete, without deep recursion for
11734
             long blocks. */
11735
          ptr = &BLOCK_EXPR_BODY (node);
11736
          while (TREE_CODE (*ptr) == COMPOUND_EXPR
11737
                 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11738
            {
11739
              tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11740
              tree *next = &TREE_OPERAND (*ptr, 1);
11741
              TREE_OPERAND (*ptr, 0) = cur;
11742
              if (IS_EMPTY_STMT (cur))
11743
                {
11744
                  /* Optimization;  makes it easier to detect empty bodies.
11745
                     Most useful for  with all-constant initializer. */
11746
                  *ptr = *next;
11747
                  continue;
11748
                }
11749
              if (TREE_CODE (cur) == ERROR_MARK)
11750
                error_seen++;
11751
              else if (! CAN_COMPLETE_NORMALLY (cur))
11752
                {
11753
                  wfl_op2 = *next;
11754
                  for (;;)
11755
                    {
11756
                      if (TREE_CODE (wfl_op2) == BLOCK)
11757
                        wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11758
                      else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11759
                        wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11760
                      else
11761
                        break;
11762
                    }
11763
                  if (TREE_CODE (wfl_op2) != CASE_EXPR
11764
                      && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11765
                    unreachable_stmt_error (*ptr);
11766
                }
11767
              if (TREE_TYPE (*ptr) == NULL_TREE)
11768
                TREE_TYPE (*ptr) = void_type_node;
11769
              ptr = next;
11770
            }
11771
          *ptr = java_complete_tree (*ptr);
11772
 
11773
          if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11774
            return error_mark_node;
11775
          CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11776
        }
11777
      /* Turn local bindings to null */
11778
      for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11779
        IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11780
 
11781
      TREE_TYPE (node) = void_type_node;
11782
      break;
11783
 
11784
      /* 2- They are expressions but ultimately deal with statements */
11785
 
11786
    case THROW_EXPR:
11787
      wfl_op1 = TREE_OPERAND (node, 0);
11788
      COMPLETE_CHECK_OP_0 (node);
11789
      /* 14.19 A throw statement cannot complete normally. */
11790
      CAN_COMPLETE_NORMALLY (node) = 0;
11791
      return patch_throw_statement (node, wfl_op1);
11792
 
11793
    case SYNCHRONIZED_EXPR:
11794
      wfl_op1 = TREE_OPERAND (node, 0);
11795
      return patch_synchronized_statement (node, wfl_op1);
11796
 
11797
    case TRY_EXPR:
11798
      return patch_try_statement (node);
11799
 
11800
    case TRY_FINALLY_EXPR:
11801
      COMPLETE_CHECK_OP_0 (node);
11802
      COMPLETE_CHECK_OP_1 (node);
11803
      if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11804
        /* Reduce try/finally nodes with an empty try block.  */
11805
        return TREE_OPERAND (node, 1);
11806
      if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11807
        /* Likewise for an empty finally block.  */
11808
        return TREE_OPERAND (node, 0);
11809
      CAN_COMPLETE_NORMALLY (node)
11810
        = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11811
           && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11812
      TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11813
      return node;
11814
 
11815
    case LABELED_BLOCK_EXPR:
11816
      PUSH_LABELED_BLOCK (node);
11817
      if (LABELED_BLOCK_BODY (node))
11818
        COMPLETE_CHECK_OP_1 (node);
11819
      TREE_TYPE (node) = void_type_node;
11820
      POP_LABELED_BLOCK ();
11821
 
11822
      if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11823
        {
11824
          LABELED_BLOCK_BODY (node) = NULL_TREE;
11825
          CAN_COMPLETE_NORMALLY (node) = 1;
11826
        }
11827
      else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11828
        CAN_COMPLETE_NORMALLY (node) = 1;
11829
      return node;
11830
 
11831
    case EXIT_BLOCK_EXPR:
11832
      return patch_bc_statement (node);
11833
 
11834
    case CASE_EXPR:
11835
      cn = java_complete_tree (TREE_OPERAND (node, 0));
11836
      if (cn == error_mark_node)
11837
        return cn;
11838
 
11839
      /* First, the case expression must be constant. Values of final
11840
         fields are accepted. */
11841
      nn = fold_constant_for_init (cn, NULL_TREE);
11842
      if (nn != NULL_TREE)
11843
        cn = nn;
11844
 
11845
      cn = fold (cn);
11846
      if ((TREE_CODE (cn) == COMPOUND_EXPR
11847
           || TREE_CODE (cn) == COMPONENT_REF)
11848
          && JDECL_P (TREE_OPERAND (cn, 1))
11849
          && FIELD_FINAL (TREE_OPERAND (cn, 1))
11850
          && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11851
        {
11852
          cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11853
                                       TREE_OPERAND (cn, 1));
11854
        }
11855
      /* Accept final locals too. */
11856
      else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11857
               && DECL_INITIAL (cn))
11858
        cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11859
 
11860
      if (!TREE_CONSTANT (cn))
11861
        {
11862
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11863
          parse_error_context (node, "Constant expression required");
11864
          return error_mark_node;
11865
        }
11866
 
11867
      nn = ctxp->current_loop;
11868
 
11869
      /* It must be assignable to the type of the switch expression. */
11870
      if (!try_builtin_assignconv (NULL_TREE,
11871
                                   TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11872
        {
11873
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11874
          parse_error_context
11875
            (wfl_operator,
11876
             "Incompatible type for case. Can't convert %qs to %",
11877
             lang_printable_name (TREE_TYPE (cn), 0));
11878
          return error_mark_node;
11879
        }
11880
 
11881
      cn = fold (convert (int_type_node, cn));
11882
      TREE_CONSTANT_OVERFLOW (cn) = 0;
11883
      CAN_COMPLETE_NORMALLY (cn) = 1;
11884
 
11885
      /* Save the label on a list so that we can later check for
11886
         duplicates.  */
11887
      case_label_list = tree_cons (node, cn, case_label_list);
11888
 
11889
      /* Multiple instance of a case label bearing the same value is
11890
         checked later. The case expression is all right so far. */
11891
      if (TREE_CODE (cn) == VAR_DECL)
11892
        cn = DECL_INITIAL (cn);
11893
      TREE_OPERAND (node, 0) = cn;
11894
      TREE_TYPE (node) = void_type_node;
11895
      CAN_COMPLETE_NORMALLY (node) = 1;
11896
      TREE_SIDE_EFFECTS (node) = 1;
11897
      break;
11898
 
11899
    case DEFAULT_EXPR:
11900
      nn = ctxp->current_loop;
11901
      /* Only one default label is allowed per switch statement */
11902
      if (SWITCH_HAS_DEFAULT (nn))
11903
        {
11904
#ifdef USE_MAPPED_LOCATION
11905
          SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11906
#else
11907
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11908
#endif
11909
          parse_error_context (wfl_operator,
11910
                               "Duplicate case label: %");
11911
          return error_mark_node;
11912
        }
11913
      else
11914
        SWITCH_HAS_DEFAULT (nn) = 1;
11915
      TREE_TYPE (node) = void_type_node;
11916
      TREE_SIDE_EFFECTS (node) = 1;
11917
      CAN_COMPLETE_NORMALLY (node) = 1;
11918
      break;
11919
 
11920
    case SWITCH_EXPR:
11921
    case LOOP_EXPR:
11922
      PUSH_LOOP (node);
11923
      /* Check whether the loop was enclosed in a labeled
11924
         statement. If not, create one, insert the loop in it and
11925
         return the node */
11926
      nn = patch_loop_statement (node);
11927
 
11928
      /* Anyways, walk the body of the loop */
11929
      if (TREE_CODE (node) == LOOP_EXPR)
11930
        TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11931
      /* Switch statement: walk the switch expression and the cases */
11932
      else
11933
        node = patch_switch_statement (node);
11934
 
11935
      if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11936
        nn = error_mark_node;
11937
      else
11938
        {
11939
          TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11940
          /* If we returned something different, that's because we
11941
             inserted a label. Pop the label too. */
11942
          if (nn != node)
11943
            {
11944
              if (CAN_COMPLETE_NORMALLY (node))
11945
                CAN_COMPLETE_NORMALLY (nn) = 1;
11946
              POP_LABELED_BLOCK ();
11947
            }
11948
        }
11949
      POP_LOOP ();
11950
      return nn;
11951
 
11952
    case EXIT_EXPR:
11953
      TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11954
      return patch_exit_expr (node);
11955
 
11956
    case COND_EXPR:
11957
      /* Condition */
11958
      TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11959
      if (TREE_OPERAND (node, 0) == error_mark_node)
11960
        return error_mark_node;
11961
      /* then-else branches */
11962
      TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11963
      if (TREE_OPERAND (node, 1) == error_mark_node)
11964
        return error_mark_node;
11965
      {
11966
        /* This is a special case due to build_assertion().  When
11967
           assertions are disabled we build a COND_EXPR in which
11968
           Operand 1 is the body of the assertion.  If that happens to
11969
           be a string concatenation we'll need to patch it here.  */
11970
        tree patched = patch_string (TREE_OPERAND (node, 1));
11971
        if (patched)
11972
          TREE_OPERAND (node, 1) = patched;
11973
      }
11974
     TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11975
      if (TREE_OPERAND (node, 2) == error_mark_node)
11976
        return error_mark_node;
11977
      return patch_if_else_statement (node);
11978
      break;
11979
 
11980
    case CONDITIONAL_EXPR:
11981
      /* Condition */
11982
      wfl_op1 = TREE_OPERAND (node, 0);
11983
      COMPLETE_CHECK_OP_0 (node);
11984
      wfl_op2 = TREE_OPERAND (node, 1);
11985
      COMPLETE_CHECK_OP_1 (node);
11986
      wfl_op3 = TREE_OPERAND (node, 2);
11987
      COMPLETE_CHECK_OP_2 (node);
11988
      return patch_conditional_expr (node, wfl_op1, wfl_op2);
11989
 
11990
      /* 3- Expression section */
11991
    case COMPOUND_EXPR:
11992
      wfl_op2 = TREE_OPERAND (node, 1);
11993
      TREE_OPERAND (node, 0) = nn =
11994
        java_complete_tree (TREE_OPERAND (node, 0));
11995
      if (IS_EMPTY_STMT (wfl_op2))
11996
        CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11997
      else
11998
        {
11999
          if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
12000
            {
12001
              /* An unreachable condition in a do-while statement
12002
                 is *not* (technically) an unreachable statement. */
12003
              nn = wfl_op2;
12004
              if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
12005
                nn = EXPR_WFL_NODE (nn);
12006
              /* NN can be NULL_TREE exactly when UPDATE is, in
12007
                 finish_for_loop.  */
12008
              if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
12009
                {
12010
                  SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12011
                  if (SUPPRESS_UNREACHABLE_ERROR (nn))
12012
                    {
12013
                      /* Perhaps this warning should have an
12014
                         associated flag.  The code being compiled is
12015
                         pedantically correct, but useless.  */
12016
                      parse_warning_context (wfl_operator,
12017
                                             "Unreachable statement");
12018
                    }
12019
                  else
12020
                    parse_error_context (wfl_operator,
12021
                                         "Unreachable statement");
12022
                }
12023
            }
12024
          TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
12025
          if (TREE_OPERAND (node, 1) == error_mark_node)
12026
            return error_mark_node;
12027
          /* Even though we might allow the case where the first
12028
             operand doesn't return normally, we still should compute
12029
             CAN_COMPLETE_NORMALLY correctly.  */
12030
          CAN_COMPLETE_NORMALLY (node)
12031
            = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
12032
               && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
12033
        }
12034
      TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
12035
      break;
12036
 
12037
    case RETURN_EXPR:
12038
      /* CAN_COMPLETE_NORMALLY (node) = 0; */
12039
      return patch_return (node);
12040
 
12041
    case EXPR_WITH_FILE_LOCATION:
12042
      if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
12043
          || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
12044
        {
12045
          node = resolve_expression_name (node, NULL);
12046
          if (node == error_mark_node)
12047
            return node;
12048
          CAN_COMPLETE_NORMALLY (node) = 1;
12049
        }
12050
      else
12051
        {
12052
          tree body;
12053
          location_t save_location = input_location;
12054
#ifdef USE_MAPPED_LOCATION
12055
          input_location = EXPR_LOCATION (node);
12056
          if (input_location == UNKNOWN_LOCATION)
12057
            input_location = save_location;
12058
#else
12059
          input_line = EXPR_WFL_LINENO (node);
12060
#endif
12061
          body = java_complete_tree (EXPR_WFL_NODE (node));
12062
          input_location = save_location;
12063
          EXPR_WFL_NODE (node) = body;
12064
          TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12065
          CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12066
          if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
12067
            {
12068
              /* Makes it easier to constant fold, detect empty bodies. */
12069
              return body;
12070
            }
12071
          if (body == error_mark_node)
12072
            {
12073
              /* Its important for the evaluation of assignment that
12074
                 this mark on the TREE_TYPE is propagated. */
12075
              TREE_TYPE (node) = error_mark_node;
12076
              return error_mark_node;
12077
            }
12078
          else
12079
            TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12080
 
12081
        }
12082
      break;
12083
 
12084
    case NEW_ARRAY_EXPR:
12085
      /* Patch all the dimensions */
12086
      flag = 0;
12087
      for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12088
        {
12089
          int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12090
          tree dim = convert (int_type_node,
12091
                              java_complete_tree (TREE_VALUE (cn)));
12092
          if (dim == error_mark_node)
12093
            {
12094
              flag = 1;
12095
              continue;
12096
            }
12097
          else
12098
            {
12099
              TREE_VALUE (cn) = dim;
12100
              /* Setup the location of the current dimension, for
12101
                 later error report. */
12102
#ifdef USE_MAPPED_LOCATION
12103
              TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
12104
#else
12105
              TREE_PURPOSE (cn) =
12106
                build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12107
              EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12108
#endif
12109
            }
12110
        }
12111
      /* They complete the array creation expression, if no errors
12112
         were found. */
12113
      CAN_COMPLETE_NORMALLY (node) = 1;
12114
      return (flag ? error_mark_node
12115
              : force_evaluation_order (patch_newarray (node)));
12116
 
12117
    case NEW_ANONYMOUS_ARRAY_EXPR:
12118
      /* Create the array type if necessary. */
12119
      if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12120
        {
12121
          tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12122
          if (!(type = resolve_type_during_patch (type)))
12123
            return error_mark_node;
12124
          type = build_array_from_name (type, NULL_TREE,
12125
                                        ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12126
          ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12127
        }
12128
      node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12129
                                   ANONYMOUS_ARRAY_INITIALIZER (node));
12130
      if (node == error_mark_node)
12131
        return error_mark_node;
12132
      CAN_COMPLETE_NORMALLY (node) = 1;
12133
      return node;
12134
 
12135
    case NEW_CLASS_EXPR:
12136
    case CALL_EXPR:
12137
      /* Complete function's argument(s) first */
12138
      if (complete_function_arguments (node))
12139
        return error_mark_node;
12140
      else
12141
        {
12142
          tree decl, wfl = TREE_OPERAND (node, 0);
12143
          int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12144
          int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12145
                           super_identifier_node);
12146
          tree arguments;
12147
#ifdef USE_MAPPED_LOCATION
12148
          source_location location = EXPR_LOCATION (node);
12149
#else
12150
          int location = EXPR_WFL_LINECOL (node);
12151
#endif
12152
 
12153
          node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12154
                                          from_super, 0, &decl);
12155
          if (node == error_mark_node)
12156
            return error_mark_node;
12157
 
12158
          if (TREE_CODE (node) == CALL_EXPR
12159
              && TREE_OPERAND (node, 1) != NULL_TREE)
12160
            arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12161
          else
12162
            arguments = NULL_TREE;
12163
          check_thrown_exceptions (location, decl, arguments);
12164
          /* If we call this(...), register signature and positions */
12165
          if (in_this)
12166
            DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12167
              tree_cons (wfl, decl,
12168
                         DECL_CONSTRUCTOR_CALLS (current_function_decl));
12169
          CAN_COMPLETE_NORMALLY (node) = 1;
12170
          return force_evaluation_order (node);
12171
        }
12172
 
12173
    case MODIFY_EXPR:
12174
      /* Save potential wfls */
12175
      wfl_op1 = TREE_OPERAND (node, 0);
12176
      TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12177
 
12178
      if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12179
          && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12180
          && DECL_INITIAL (nn) != NULL_TREE)
12181
        {
12182
          tree value;
12183
 
12184
          value = fold_constant_for_init (nn, nn);
12185
 
12186
          /* When we have a primitype type, or a string and we're not
12187
             emitting a class file, we actually don't want to generate
12188
             anything for the assignment. */
12189
          if (value != NULL_TREE &&
12190
              (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12191
               (TREE_TYPE (value) == string_ptr_type_node &&
12192
                ! flag_emit_class_files)))
12193
            {
12194
              /* Prepare node for patch_assignment */
12195
              TREE_OPERAND (node, 1) = value;
12196
              /* Call patch assignment to verify the assignment */
12197
              if (patch_assignment (node, wfl_op1) == error_mark_node)
12198
                return error_mark_node;
12199
              /* Set DECL_INITIAL properly (a conversion might have
12200
                 been decided by patch_assignment) and return the
12201
                 empty statement. */
12202
              else
12203
                {
12204
                  tree patched = patch_string (TREE_OPERAND (node, 1));
12205
                  if (patched)
12206
                    DECL_INITIAL (nn) = patched;
12207
                  else
12208
                    DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12209
                  DECL_FIELD_FINAL_IUD (nn) = 1;
12210
                  return build_java_empty_stmt ();
12211
                }
12212
            }
12213
          if (! flag_emit_class_files)
12214
            DECL_INITIAL (nn) = NULL_TREE;
12215
        }
12216
      wfl_op2 = TREE_OPERAND (node, 1);
12217
 
12218
      if (TREE_OPERAND (node, 0) == error_mark_node)
12219
        return error_mark_node;
12220
 
12221
      flag = COMPOUND_ASSIGN_P (wfl_op2);
12222
      if (flag)
12223
        {
12224
          /* This might break when accessing outer field from inner
12225
             class. TESTME, FIXME */
12226
          tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12227
 
12228
          /* Hand stabilize the lhs on both places */
12229
          TREE_OPERAND (node, 0) = lvalue;
12230
          TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12231
            (flag_emit_class_files ? lvalue : save_expr (lvalue));
12232
 
12233
          /* 15.25.2.a: Left hand is not an array access. FIXME */
12234
          /* Now complete the RHS. We write it back later on. */
12235
          nn = java_complete_tree (TREE_OPERAND (node, 1));
12236
 
12237
          if ((cn = patch_string (nn)))
12238
            nn = cn;
12239
 
12240
          /* The last part of the rewrite for E1 op= E2 is to have
12241
             E1 = (T)(E1 op E2), with T being the type of E1. */
12242
          nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12243
                                               TREE_TYPE (lvalue), nn));
12244
 
12245
          /* If the assignment is compound and has reference type,
12246
             then ensure the LHS has type String and nothing else.  */
12247
          if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12248
              && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12249
            parse_error_context (wfl_op2,
12250
                                 "Incompatible type for %<+=%>. Can't convert %qs to %",
12251
                                 lang_printable_name (TREE_TYPE (lvalue), 0));
12252
 
12253
          /* 15.25.2.b: Left hand is an array access. FIXME */
12254
        }
12255
 
12256
      /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12257
         function to complete this RHS. Note that a NEW_ARRAY_INIT
12258
         might have been already fully expanded if created as a result
12259
         of processing an anonymous array initializer. We avoid doing
12260
         the operation twice by testing whether the node already bears
12261
         a type. */
12262
      else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12263
        nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12264
                                   TREE_OPERAND (node, 1));
12265
      /* Otherwise we simply complete the RHS */
12266
      else
12267
        nn = java_complete_tree (TREE_OPERAND (node, 1));
12268
 
12269
      if (nn == error_mark_node)
12270
        return error_mark_node;
12271
 
12272
      /* Write back the RHS as we evaluated it. */
12273
      TREE_OPERAND (node, 1) = nn;
12274
 
12275
      /* In case we're handling = with a String as a RHS, we need to
12276
         produce a String out of the RHS (it might still be a
12277
         STRING_CST or a StringBuffer at this stage */
12278
      if ((nn = patch_string (TREE_OPERAND (node, 1))))
12279
        TREE_OPERAND (node, 1) = nn;
12280
 
12281
      if ((nn = nested_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12282
                                         TREE_OPERAND (node, 1))))
12283
        {
12284
          /* We return error_mark_node if nested_field_access_fix
12285
             detects we write into a final. */
12286
          if (nn == error_mark_node)
12287
            return error_mark_node;
12288
          node = nn;
12289
        }
12290
      else
12291
        {
12292
          node = patch_assignment (node, wfl_op1);
12293
          if (node == error_mark_node)
12294
            return error_mark_node;
12295
          /* Reorganize the tree if necessary. */
12296
          if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12297
                       || JSTRING_P (TREE_TYPE (node))))
12298
            node = java_refold (node);
12299
        }
12300
 
12301
      /* Seek to set DECL_INITIAL to a proper value, since it might have
12302
         undergone a conversion in patch_assignment. We do that only when
12303
         it's necessary to have DECL_INITIAL properly set. */
12304
      nn = TREE_OPERAND (node, 0);
12305
      if (TREE_CODE (nn) == VAR_DECL
12306
          && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12307
          && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12308
          && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12309
              || TREE_TYPE (nn) == string_ptr_type_node))
12310
        DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12311
 
12312
      CAN_COMPLETE_NORMALLY (node) = 1;
12313
      return node;
12314
 
12315
    case MULT_EXPR:
12316
    case PLUS_EXPR:
12317
    case MINUS_EXPR:
12318
    case LSHIFT_EXPR:
12319
    case RSHIFT_EXPR:
12320
    case URSHIFT_EXPR:
12321
    case BIT_AND_EXPR:
12322
    case BIT_XOR_EXPR:
12323
    case BIT_IOR_EXPR:
12324
    case TRUNC_MOD_EXPR:
12325
    case TRUNC_DIV_EXPR:
12326
    case RDIV_EXPR:
12327
    case TRUTH_ANDIF_EXPR:
12328
    case TRUTH_ORIF_EXPR:
12329
    case EQ_EXPR:
12330
    case NE_EXPR:
12331
    case GT_EXPR:
12332
    case GE_EXPR:
12333
    case LT_EXPR:
12334
    case LE_EXPR:
12335
      /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12336
         knows how to handle those cases. */
12337
      wfl_op1 = TREE_OPERAND (node, 0);
12338
      wfl_op2 = TREE_OPERAND (node, 1);
12339
 
12340
      CAN_COMPLETE_NORMALLY (node) = 1;
12341
      /* Don't complete string nodes if dealing with the PLUS operand. */
12342
      if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12343
        {
12344
          nn = java_complete_tree (wfl_op1);
12345
          if (nn == error_mark_node)
12346
            return error_mark_node;
12347
 
12348
          TREE_OPERAND (node, 0) = nn;
12349
        }
12350
      if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12351
        {
12352
          nn = java_complete_tree (wfl_op2);
12353
          if (nn == error_mark_node)
12354
            return error_mark_node;
12355
 
12356
          TREE_OPERAND (node, 1) = nn;
12357
        }
12358
      return patch_binop (node, wfl_op1, wfl_op2, 0);
12359
 
12360
    case INSTANCEOF_EXPR:
12361
      wfl_op1 = TREE_OPERAND (node, 0);
12362
      COMPLETE_CHECK_OP_0 (node);
12363
      return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1), 0);
12364
 
12365
    case UNARY_PLUS_EXPR:
12366
    case NEGATE_EXPR:
12367
    case TRUTH_NOT_EXPR:
12368
    case BIT_NOT_EXPR:
12369
    case PREDECREMENT_EXPR:
12370
    case PREINCREMENT_EXPR:
12371
    case POSTDECREMENT_EXPR:
12372
    case POSTINCREMENT_EXPR:
12373
    case CONVERT_EXPR:
12374
      /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12375
         how to handle those cases. */
12376
      wfl_op1 = TREE_OPERAND (node, 0);
12377
      CAN_COMPLETE_NORMALLY (node) = 1;
12378
      TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12379
      if (TREE_OPERAND (node, 0) == error_mark_node)
12380
        return error_mark_node;
12381
      node = patch_unaryop (node, wfl_op1);
12382
      CAN_COMPLETE_NORMALLY (node) = 1;
12383
      break;
12384
 
12385
    case ARRAY_REF:
12386
      /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12387
         how to handle those cases. */
12388
      wfl_op1 = TREE_OPERAND (node, 0);
12389
      TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12390
      if (TREE_OPERAND (node, 0) == error_mark_node)
12391
        return error_mark_node;
12392
      if (!flag_emit_class_files)
12393
        TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12394
      /* The same applies to wfl_op2 */
12395
      wfl_op2 = TREE_OPERAND (node, 1);
12396
      TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12397
      if (TREE_OPERAND (node, 1) == error_mark_node)
12398
        return error_mark_node;
12399
      if (!flag_emit_class_files)
12400
        TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12401
      return patch_array_ref (node);
12402
 
12403
    case RECORD_TYPE:
12404
      return node;;
12405
 
12406
    case COMPONENT_REF:
12407
      /* The first step in the re-write of qualified name handling.  FIXME.
12408
         So far, this is only to support PRIMTYPE.class ->
12409
         PRIMCLASS.TYPE. */
12410
      {
12411
        tree prim_class = TREE_OPERAND (node, 0);
12412
        tree name = TREE_OPERAND (node, 1);
12413
        tree field;
12414
 
12415
        gcc_assert (TREE_CODE (prim_class) == NOP_EXPR);
12416
        prim_class = java_complete_tree (TREE_TYPE (prim_class));
12417
        gcc_assert (TREE_CODE (prim_class) == RECORD_TYPE);
12418
        field = lookup_field_wrapper (prim_class, name);
12419
 
12420
        if (field == NULL_TREE)
12421
          {
12422
            error ("missing static field %qs", IDENTIFIER_POINTER (name));
12423
            return error_mark_node;
12424
          }
12425
        if (! FIELD_STATIC (field))
12426
          {
12427
            error ("not a static field %qs", IDENTIFIER_POINTER (name));
12428
            return error_mark_node;
12429
          }
12430
        return field;
12431
      }
12432
      break;
12433
 
12434
    case THIS_EXPR:
12435
      /* Can't use THIS in a static environment */
12436
      if (!current_this)
12437
        {
12438
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12439
          parse_error_context (wfl_operator,
12440
                               "Keyword % used outside allowed context");
12441
          TREE_TYPE (node) = error_mark_node;
12442
          return error_mark_node;
12443
        }
12444
      if (ctxp->explicit_constructor_p)
12445
        {
12446
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12447
          parse_error_context
12448
            (wfl_operator, "Can't reference % or % before the superclass constructor has been called");
12449
          TREE_TYPE (node) = error_mark_node;
12450
          return error_mark_node;
12451
        }
12452
      return current_this;
12453
 
12454
    case CLASS_LITERAL:
12455
      CAN_COMPLETE_NORMALLY (node) = 1;
12456
      node = patch_incomplete_class_ref (node);
12457
      if (node == error_mark_node)
12458
        return error_mark_node;
12459
      break;
12460
 
12461
    default:
12462
      CAN_COMPLETE_NORMALLY (node) = 1;
12463
      /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12464
         and it's time to turn it into the appropriate String object */
12465
      if ((nn = patch_string (node)))
12466
        node = nn;
12467
      else
12468
        internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12469
    }
12470
  return node;
12471
}
12472
 
12473
/* Complete function call's argument. Return a nonzero value is an
12474
   error was found.  */
12475
 
12476
static int
12477
complete_function_arguments (tree node)
12478
{
12479
  int flag = 0;
12480
  tree cn;
12481
 
12482
  ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12483
  for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12484
    {
12485
      tree wfl = TREE_VALUE (cn), parm, temp;
12486
      parm = java_complete_tree (wfl);
12487
 
12488
      if (parm == error_mark_node)
12489
        {
12490
          flag = 1;
12491
          continue;
12492
        }
12493
      /* If we have a string literal that we haven't transformed yet or a
12494
         crafted string buffer, as a result of the use of the String
12495
         `+' operator. Build `parm.toString()' and expand it. */
12496
      if ((temp = patch_string (parm)))
12497
        parm = temp;
12498
 
12499
      TREE_VALUE (cn) = parm;
12500
    }
12501
  ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12502
  return flag;
12503
}
12504
 
12505
/* Sometimes (for loops and variable initialized during their
12506
   declaration), we want to wrap a statement around a WFL and turn it
12507
   debugable.  */
12508
 
12509
static tree
12510
build_debugable_stmt (int location, tree stmt)
12511
{
12512
  if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12513
    {
12514
#ifdef USE_MAPPED_LOCATION
12515
      stmt = expr_add_location (stmt, location, 1);
12516
#else
12517
      stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12518
      EXPR_WFL_LINECOL (stmt) = location;
12519
      JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12520
#endif
12521
    }
12522
  return stmt;
12523
}
12524
 
12525
static tree
12526
build_expr_block (tree body, tree decls)
12527
{
12528
  tree node = make_node (BLOCK);
12529
  BLOCK_EXPR_DECLS (node) = decls;
12530
  BLOCK_EXPR_BODY (node) = body;
12531
  if (body)
12532
    TREE_TYPE (node) = TREE_TYPE (body);
12533
  TREE_SIDE_EFFECTS (node) = 1;
12534
  return node;
12535
}
12536
 
12537
/* Create a new function block and link it appropriately to current
12538
   function block chain */
12539
 
12540
static tree
12541
enter_block (void)
12542
{
12543
  tree b = build_expr_block (NULL_TREE, NULL_TREE);
12544
 
12545
  /* Link block B supercontext to the previous block. The current
12546
     function DECL is used as supercontext when enter_a_block is called
12547
     for the first time for a given function. The current function body
12548
     (DECL_FUNCTION_BODY) is set to be block B.  */
12549
 
12550
  tree fndecl = current_function_decl;
12551
 
12552
  if (!fndecl) {
12553
    BLOCK_SUPERCONTEXT (b) = current_static_block;
12554
    current_static_block = b;
12555
  }
12556
 
12557
  else if (!DECL_FUNCTION_BODY (fndecl))
12558
    {
12559
      BLOCK_SUPERCONTEXT (b) = fndecl;
12560
      DECL_FUNCTION_BODY (fndecl) = b;
12561
    }
12562
  else
12563
    {
12564
      BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12565
      DECL_FUNCTION_BODY (fndecl) = b;
12566
    }
12567
  return b;
12568
}
12569
 
12570
/* Exit a block by changing the current function body
12571
   (DECL_FUNCTION_BODY) to the current block super context, only if
12572
   the block being exited isn't the method's top level one.  */
12573
 
12574
static tree
12575
exit_block (void)
12576
{
12577
  tree b;
12578
  if (current_function_decl)
12579
    {
12580
      b = DECL_FUNCTION_BODY (current_function_decl);
12581
      if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12582
        DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12583
    }
12584
  else
12585
    {
12586
      b = current_static_block;
12587
 
12588
      if (BLOCK_SUPERCONTEXT (b))
12589
        current_static_block = BLOCK_SUPERCONTEXT (b);
12590
    }
12591
  return b;
12592
}
12593
 
12594
/* Lookup for NAME in the nested function's blocks, all the way up to
12595
   the current toplevel one. It complies with Java's local variable
12596
   scoping rules.  */
12597
 
12598
static tree
12599
lookup_name_in_blocks (tree name)
12600
{
12601
  tree b = GET_CURRENT_BLOCK (current_function_decl);
12602
 
12603
  while (b != current_function_decl)
12604
    {
12605
      tree current;
12606
 
12607
      /* Paranoid sanity check. To be removed */
12608
      if (TREE_CODE (b) != BLOCK)
12609
        abort ();
12610
 
12611
      for (current = BLOCK_EXPR_DECLS (b); current;
12612
           current = TREE_CHAIN (current))
12613
        if (DECL_NAME (current) == name)
12614
          return current;
12615
      b = BLOCK_SUPERCONTEXT (b);
12616
    }
12617
  return NULL_TREE;
12618
}
12619
 
12620
static void
12621
maybe_absorb_scoping_blocks (void)
12622
{
12623
  while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12624
    {
12625
      tree b = exit_block ();
12626
      java_method_add_stmt (current_function_decl, b);
12627
      SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12628
    }
12629
}
12630
 
12631
 
12632
/* This section of the source is reserved to build_* functions that
12633
   are building incomplete tree nodes and the patch_* functions that
12634
   are completing them.  */
12635
 
12636
/* Wrap a non WFL node around a WFL.  */
12637
 
12638
static tree
12639
build_wfl_wrap (tree node, int location)
12640
{
12641
  tree wfl, node_to_insert = node;
12642
 
12643
  /* We want to process THIS . xxx symbolically, to keep it consistent
12644
     with the way we're processing SUPER. A THIS from a primary as a
12645
     different form than a SUPER. Turn THIS into something symbolic */
12646
  if (TREE_CODE (node) == THIS_EXPR)
12647
    node_to_insert = wfl = build_wfl_node (this_identifier_node);
12648
  else
12649
#ifdef USE_MAPPED_LOCATION
12650
    wfl = build_unknown_wfl (NULL_TREE);
12651
 
12652
  SET_EXPR_LOCATION (wfl, location);
12653
#else
12654
    wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12655
 
12656
  EXPR_WFL_LINECOL (wfl) = location;
12657
#endif
12658
  EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12659
  return wfl;
12660
}
12661
 
12662
/* Build a super() constructor invocation. Returns an empty statement if
12663
   we're currently dealing with the class java.lang.Object. */
12664
 
12665
static tree
12666
build_super_invocation (tree mdecl)
12667
{
12668
  if (DECL_CONTEXT (mdecl) == object_type_node)
12669
    return build_java_empty_stmt ();
12670
  else
12671
    {
12672
      tree super_wfl = build_wfl_node (super_identifier_node);
12673
      tree a = NULL_TREE, t;
12674
 
12675
      /* This is called after parsing is done, so the parser context
12676
         won't be accurate. Set location info from current_class decl. */
12677
      tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12678
      EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12679
 
12680
      /* If we're dealing with an anonymous class, pass the arguments
12681
         of the crafted constructor along. */
12682
      if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12683
        {
12684
          SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12685
          for (; t != end_params_node; t = TREE_CHAIN (t))
12686
            a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12687
        }
12688
      return build_method_invocation (super_wfl, a);
12689
    }
12690
}
12691
 
12692
/* Build a SUPER/THIS qualified method invocation.  */
12693
 
12694
static tree
12695
build_this_super_qualified_invocation (int use_this, tree name, tree args,
12696
                                       int lloc, int rloc)
12697
{
12698
  tree invok;
12699
  tree wfl =
12700
    build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12701
  EXPR_WFL_LINECOL (wfl) = lloc;
12702
  invok = build_method_invocation (name, args);
12703
  return make_qualified_primary (wfl, invok, rloc);
12704
}
12705
 
12706
/* Build an incomplete CALL_EXPR node. */
12707
 
12708
static tree
12709
build_method_invocation (tree name, tree args)
12710
{
12711
  tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12712
  TREE_SIDE_EFFECTS (call) = 1;
12713
  EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12714
  return call;
12715
}
12716
 
12717
/* Build an incomplete new xxx(...) node. */
12718
 
12719
static tree
12720
build_new_invocation (tree name, tree args)
12721
{
12722
  tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12723
  TREE_SIDE_EFFECTS (call) = 1;
12724
  EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12725
  return call;
12726
}
12727
 
12728
/* Build an incomplete assignment expression. */
12729
 
12730
static tree
12731
build_assignment (int op, int op_location, tree lhs, tree rhs)
12732
{
12733
  tree assignment;
12734
  /* Build the corresponding binop if we deal with a Compound
12735
     Assignment operator. Mark the binop sub-tree as part of a
12736
     Compound Assignment expression */
12737
  if (op != ASSIGN_TK)
12738
    {
12739
      rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12740
      COMPOUND_ASSIGN_P (rhs) = 1;
12741
    }
12742
  assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12743
  TREE_SIDE_EFFECTS (assignment) = 1;
12744
  EXPR_WFL_LINECOL (assignment) = op_location;
12745
  return assignment;
12746
}
12747
 
12748
/* Print an INTEGER_CST node as decimal in a static buffer, and return
12749
   the buffer.  This is used only for string conversion.  */
12750
static char *
12751
string_convert_int_cst (tree node)
12752
{
12753
  /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12754
  static char buffer[21];
12755
 
12756
  unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12757
  unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12758
  char *p = buffer + sizeof (buffer);
12759
  int neg = 0;
12760
 
12761
  unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12762
                                  << (HOST_BITS_PER_WIDE_INT - 1));
12763
 
12764
  *--p = '\0';
12765
 
12766
  /* If negative, note the fact and negate the value.  */
12767
  if ((hi & hibit))
12768
    {
12769
      lo = ~lo;
12770
      hi = ~hi;
12771
      if (++lo == 0)
12772
        ++hi;
12773
      neg = 1;
12774
    }
12775
 
12776
  /* Divide by 10 until there are no bits left.  */
12777
  do
12778
    {
12779
      unsigned HOST_WIDE_INT acc = 0;
12780
      unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12781
      unsigned int i;
12782
 
12783
      /* Use long division to compute the result and the remainder.  */
12784
      for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12785
        {
12786
          /* Shift a bit into accumulator.  */
12787
          acc <<= 1;
12788
          if ((hi & hibit))
12789
            acc |= 1;
12790
 
12791
          /* Shift the value.  */
12792
          hi <<= 1;
12793
          if ((lo & hibit))
12794
            hi |= 1;
12795
          lo <<= 1;
12796
 
12797
          /* Shift the correct bit into the result.  */
12798
          outhi <<= 1;
12799
          if ((outlo & hibit))
12800
            outhi |= 1;
12801
          outlo <<= 1;
12802
          if (acc >= 10)
12803
            {
12804
              acc -= 10;
12805
              outlo |= 1;
12806
            }
12807
        }
12808
 
12809
      /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12810
      *--p = '\060' + acc;
12811
 
12812
      hi = outhi;
12813
      lo = outlo;
12814
    }
12815
  while (hi || lo);
12816
 
12817
  if (neg)
12818
    *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12819
 
12820
  return p;
12821
}
12822
 
12823
/* Print an INTEGER_CST node in a static buffer, and return the
12824
   buffer.  This is used only for error handling.  */
12825
char *
12826
print_int_node (tree node)
12827
{
12828
  static char buffer [80];
12829
  if (TREE_CONSTANT_OVERFLOW (node))
12830
    sprintf (buffer, "");
12831
 
12832
  if (TREE_INT_CST_HIGH (node) == 0)
12833
    sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12834
             TREE_INT_CST_LOW (node));
12835
  else if (TREE_INT_CST_HIGH (node) == -1
12836
           && TREE_INT_CST_LOW (node) != 0)
12837
    sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12838
             -TREE_INT_CST_LOW (node));
12839
  else
12840
    sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12841
             TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12842
 
12843
  return buffer;
12844
}
12845
 
12846
 
12847
/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12848
   context.  */
12849
 
12850
/* 15.25 Assignment operators. */
12851
 
12852
static tree
12853
patch_assignment (tree node, tree wfl_op1)
12854
{
12855
  tree rhs = TREE_OPERAND (node, 1);
12856
  tree lvalue = TREE_OPERAND (node, 0), llvalue;
12857
  tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12858
  int error_found = 0;
12859
  int lvalue_from_array = 0;
12860
  int is_return = 0;
12861
 
12862
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12863
 
12864
  /* Lhs can be a named variable */
12865
  if (JDECL_P (lvalue))
12866
    {
12867
      lhs_type = TREE_TYPE (lvalue);
12868
    }
12869
  /* Or Lhs can be an array access. */
12870
  else if (TREE_CODE (lvalue) == ARRAY_REF)
12871
    {
12872
      lhs_type = TREE_TYPE (lvalue);
12873
      lvalue_from_array = 1;
12874
    }
12875
  /* Or a field access */
12876
  else if (TREE_CODE (lvalue) == COMPONENT_REF)
12877
    lhs_type = TREE_TYPE (lvalue);
12878
  /* Or a function return slot */
12879
  else if (TREE_CODE (lvalue) == RESULT_DECL)
12880
    {
12881
      /* If the return type is an integral type, then we create the
12882
         RESULT_DECL with a promoted type, but we need to do these
12883
         checks against the unpromoted type to ensure type safety.  So
12884
         here we look at the real type, not the type of the decl we
12885
         are modifying.  */
12886
      lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12887
      is_return = 1;
12888
    }
12889
  /* Otherwise, we might want to try to write into an optimized static
12890
     final, this is an of a different nature, reported further on. */
12891
  else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12892
           && resolve_expression_name (wfl_op1, &llvalue))
12893
    {
12894
      lhs_type = TREE_TYPE (lvalue);
12895
    }
12896
  else
12897
    {
12898
      parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12899
      error_found = 1;
12900
    }
12901
 
12902
  rhs_type = TREE_TYPE (rhs);
12903
 
12904
  /* 5.1 Try the assignment conversion for builtin type. */
12905
  new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12906
 
12907
  /* 5.2 If it failed, try a reference conversion */
12908
  if (!new_rhs)
12909
    new_rhs = try_reference_assignconv (lhs_type, rhs);
12910
 
12911
  /* 15.25.2 If we have a compound assignment, convert RHS into the
12912
     type of the LHS */
12913
  else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12914
    new_rhs = convert (lhs_type, rhs);
12915
 
12916
  /* Explicit cast required. This is an error */
12917
  if (!new_rhs)
12918
    {
12919
      char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12920
      char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12921
      tree wfl;
12922
      char operation [32];      /* Max size known */
12923
 
12924
      /* If the assignment is part of a declaration, we use the WFL of
12925
         the declared variable to point out the error and call it a
12926
         declaration problem. If the assignment is a genuine =
12927
         operator, we call is a operator `=' problem, otherwise we
12928
         call it an assignment problem. In both of these last cases,
12929
         we use the WFL of the operator to indicate the error. */
12930
 
12931
      if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12932
        {
12933
          wfl = wfl_op1;
12934
          strcpy (operation, "declaration");
12935
        }
12936
      else
12937
        {
12938
          wfl = wfl_operator;
12939
          if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12940
            strcpy (operation, "assignment");
12941
          else if (is_return)
12942
            strcpy (operation, "'return'");
12943
          else
12944
            strcpy (operation, "'='");
12945
        }
12946
 
12947
      if (!valid_cast_to_p (rhs_type, lhs_type))
12948
        parse_error_context
12949
          (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12950
           operation, t1, t2);
12951
      else
12952
        parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12953
                             operation, t1, t2);
12954
      free (t1); free (t2);
12955
      error_found = 1;
12956
    }
12957
 
12958
  if (error_found)
12959
    return error_mark_node;
12960
 
12961
  /* If we're processing a `return' statement, promote the actual type
12962
     to the promoted type.  */
12963
  if (is_return)
12964
    new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12965
 
12966
  /* 10.10: Array Store Exception runtime check */
12967
  if (!flag_emit_class_files
12968
      && lvalue_from_array
12969
      && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12970
    {
12971
      tree array, store_check, base, index_expr;
12972
 
12973
      /* Save RHS so that it doesn't get re-evaluated by the store check. */
12974
      new_rhs = save_expr (new_rhs);
12975
 
12976
      /* Get the INDIRECT_REF. */
12977
      array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12978
      /* Get the array pointer expr. */
12979
      array = TREE_OPERAND (array, 0);
12980
      store_check = build_java_arraystore_check (array, new_rhs);
12981
 
12982
      index_expr = TREE_OPERAND (lvalue, 1);
12983
 
12984
      if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12985
        {
12986
          /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12987
             happen before the store check, so prepare to insert the store
12988
             check within the second operand of the existing COMPOUND_EXPR. */
12989
          base = index_expr;
12990
        }
12991
      else
12992
        base = lvalue;
12993
 
12994
      index_expr = TREE_OPERAND (base, 1);
12995
      TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12996
                                       store_check, index_expr);
12997
    }
12998
 
12999
  /* Final locals can be used as case values in switch
13000
     statement. Prepare them for this eventuality. */
13001
  if (TREE_CODE (lvalue) == VAR_DECL
13002
      && DECL_FINAL (lvalue)
13003
      && TREE_CONSTANT (new_rhs)
13004
      && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
13005
      && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
13006
      )
13007
    {
13008
      TREE_CONSTANT (lvalue) = 1;
13009
      TREE_INVARIANT (lvalue) = 1;
13010
      DECL_INITIAL (lvalue) = new_rhs;
13011
    }
13012
 
13013
  /* Copy the rhs if it's a reference.  */
13014
  if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
13015
    {
13016
      switch (TREE_CODE (new_rhs))
13017
        {
13018
        case ARRAY_REF:
13019
        case INDIRECT_REF:
13020
        case COMPONENT_REF:
13021
          /* Transform a = foo.bar
13022
             into a = ({int tmp; tmp = foo.bar;}).
13023
             We need to ensure that if a read from memory fails
13024
             because of a NullPointerException, a destination variable
13025
             will remain unchanged.  An explicit temporary does what
13026
             we need.
13027
 
13028
             If flag_check_references is set, this is unnecessary
13029
             because we'll check each reference before doing any
13030
             reads.  If optimize is not set the result will never be
13031
             written to a stack slot that contains the LHS.  */
13032
          {
13033
            tree tmp = build_decl (VAR_DECL, get_identifier (""),
13034
                                   TREE_TYPE (new_rhs));
13035
            tree block = make_node (BLOCK);
13036
            tree assignment
13037
              = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
13038
            DECL_CONTEXT (tmp) = current_function_decl;
13039
            TREE_TYPE (block) = TREE_TYPE (new_rhs);
13040
            BLOCK_VARS (block) = tmp;
13041
            BLOCK_EXPR_BODY (block) = assignment;
13042
            TREE_SIDE_EFFECTS (block) = 1;
13043
            new_rhs = block;
13044
          }
13045
          break;
13046
        default:
13047
          break;
13048
        }
13049
    }
13050
 
13051
  TREE_OPERAND (node, 0) = lvalue;
13052
  TREE_OPERAND (node, 1) = new_rhs;
13053
  TREE_TYPE (node) = lhs_type;
13054
  return node;
13055
}
13056
 
13057
/* Check that type SOURCE can be cast into type DEST. If the cast
13058
   can't occur at all, return NULL; otherwise, return a possibly
13059
   modified rhs.  */
13060
 
13061
static tree
13062
try_reference_assignconv (tree lhs_type, tree rhs)
13063
{
13064
  tree new_rhs = NULL_TREE;
13065
  tree rhs_type = TREE_TYPE (rhs);
13066
 
13067
  if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13068
    {
13069
      /* `null' may be assigned to any reference type */
13070
      if (rhs == null_pointer_node)
13071
        new_rhs = null_pointer_node;
13072
      /* Try the reference assignment conversion */
13073
      else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13074
        new_rhs = rhs;
13075
      /* This is a magic assignment that we process differently */
13076
      else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13077
        new_rhs = rhs;
13078
    }
13079
  return new_rhs;
13080
}
13081
 
13082
/* Check that RHS can be converted into LHS_TYPE by the assignment
13083
   conversion (5.2), for the cases of RHS being a builtin type. Return
13084
   NULL_TREE if the conversion fails or if because RHS isn't of a
13085
   builtin type. Return a converted RHS if the conversion is possible.  */
13086
 
13087
static tree
13088
try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
13089
{
13090
  tree new_rhs = NULL_TREE;
13091
  tree rhs_type = TREE_TYPE (rhs);
13092
 
13093
  /* Handle boolean specially.  */
13094
  if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13095
      || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13096
    {
13097
      if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13098
          && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13099
        new_rhs = rhs;
13100
    }
13101
 
13102
  /* 5.1.1 Try Identity Conversion,
13103
     5.1.2 Try Widening Primitive Conversion */
13104
  else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13105
    new_rhs = convert (lhs_type, rhs);
13106
 
13107
  /* Try a narrowing primitive conversion (5.1.3):
13108
       - expression is a constant expression of type byte, short, char,
13109
         or int, AND
13110
       - variable is byte, short or char AND
13111
       - The value of the expression is representable in the type of the
13112
         variable */
13113
  else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13114
            || rhs_type == char_type_node || rhs_type == int_type_node)
13115
            && TREE_CONSTANT (rhs)
13116
           && (lhs_type == byte_type_node || lhs_type == char_type_node
13117
               || lhs_type == short_type_node))
13118
    {
13119
      if (int_fits_type_p (rhs, lhs_type))
13120
        new_rhs = convert (lhs_type, rhs);
13121
      else if (wfl_op1)         /* Might be called with a NULL */
13122
        parse_warning_context
13123
          (wfl_op1,
13124
           "Constant expression %qs too wide for narrowing primitive conversion to %qs",
13125
           print_int_node (rhs), lang_printable_name (lhs_type, 0));
13126
      /* Reported a warning that will turn into an error further
13127
         down, so we don't return */
13128
    }
13129
 
13130
  return new_rhs;
13131
}
13132
 
13133
/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13134
   conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13135
 
13136
   invocation conversion (5.3).  */
13137
 
13138
static int
13139
valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
13140
{
13141
  /* 5.1.1: This is the identity conversion part. */
13142
  if (lhs_type == rhs_type)
13143
    return 1;
13144
 
13145
  /* Reject non primitive types and boolean conversions.  */
13146
  if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13147
    return 0;
13148
 
13149
  /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13150
     than a char can't be converted into a char. Short can't too, but
13151
     the < test below takes care of that */
13152
  if (lhs_type == char_type_node && rhs_type == byte_type_node)
13153
    return 0;
13154
 
13155
  /* Accept all promoted type here. Note, we can't use <= in the test
13156
     below, because we still need to bounce out assignments of short
13157
     to char and the likes */
13158
  if (lhs_type == int_type_node
13159
      && (rhs_type == promoted_byte_type_node
13160
          || rhs_type == promoted_short_type_node
13161
          || rhs_type == promoted_char_type_node
13162
          || rhs_type == promoted_boolean_type_node))
13163
    return 1;
13164
 
13165
  /* From here, an integral is widened if its precision is smaller
13166
     than the precision of the LHS or if the LHS is a floating point
13167
     type, or the RHS is a float and the RHS a double. */
13168
  if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13169
       && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13170
      || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13171
      || (rhs_type == float_type_node && lhs_type == double_type_node))
13172
    return 1;
13173
 
13174
  return 0;
13175
}
13176
 
13177
/* Check that something of SOURCE type can be assigned or cast to
13178
   something of DEST type at runtime. Return 1 if the operation is
13179
   valid, 0 otherwise. If CAST is set to 1, we're treating the case
13180
   were SOURCE is cast into DEST, which borrows a lot of the
13181
   assignment check. */
13182
 
13183
static int
13184
valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13185
{
13186
  /* SOURCE or DEST might be null if not from a declared entity. */
13187
  if (!source || !dest)
13188
    return 0;
13189
  if (JNULLP_TYPE_P (source))
13190
    return 1;
13191
  if (TREE_CODE (source) == POINTER_TYPE)
13192
    source = TREE_TYPE (source);
13193
  if (TREE_CODE (dest) == POINTER_TYPE)
13194
    dest = TREE_TYPE (dest);
13195
 
13196
  /* If source and dest are being compiled from bytecode, they may need to
13197
     be loaded. */
13198
  if (CLASS_P (source) && !CLASS_LOADED_P (source))
13199
    {
13200
      load_class (source, 1);
13201
      safe_layout_class (source);
13202
    }
13203
  if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13204
    {
13205
      load_class (dest, 1);
13206
      safe_layout_class (dest);
13207
    }
13208
 
13209
  /* Case where SOURCE is a class type */
13210
  if (TYPE_CLASS_P (source))
13211
    {
13212
      if (TYPE_CLASS_P (dest))
13213
        return  (source == dest
13214
                 || inherits_from_p (source, dest)
13215
                 || (cast && inherits_from_p (dest, source)));
13216
      if (TYPE_INTERFACE_P (dest))
13217
        {
13218
          /* If doing a cast and SOURCE is final, the operation is
13219
             always correct a compile time (because even if SOURCE
13220
             does not implement DEST, a subclass of SOURCE might). */
13221
          if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13222
            return 1;
13223
          /* Otherwise, SOURCE must implement DEST */
13224
          return interface_of_p (dest, source);
13225
        }
13226
      /* DEST is an array, cast permitted if SOURCE is of Object type */
13227
      return (cast && source == object_type_node ? 1 : 0);
13228
    }
13229
  if (TYPE_INTERFACE_P (source))
13230
    {
13231
      if (TYPE_CLASS_P (dest))
13232
        {
13233
          /* If not casting, DEST must be the Object type */
13234
          if (!cast)
13235
            return dest == object_type_node;
13236
          /* We're doing a cast. The cast is always valid is class
13237
             DEST is not final, otherwise, DEST must implement SOURCE */
13238
          else if (!CLASS_FINAL (TYPE_NAME (dest)))
13239
            return 1;
13240
          else
13241
            return interface_of_p (source, dest);
13242
        }
13243
      if (TYPE_INTERFACE_P (dest))
13244
        {
13245
          /* If doing a cast, then if SOURCE and DEST contain method
13246
             with the same signature but different return type, then
13247
             this is a (compile time) error */
13248
          if (cast)
13249
            {
13250
              tree method_source, method_dest;
13251
              tree source_type;
13252
              tree source_sig;
13253
              tree source_name;
13254
              for (method_source = TYPE_METHODS (source); method_source;
13255
                   method_source = TREE_CHAIN (method_source))
13256
                {
13257
                  source_sig =
13258
                    build_java_argument_signature (TREE_TYPE (method_source));
13259
                  source_type = TREE_TYPE (TREE_TYPE (method_source));
13260
                  source_name = DECL_NAME (method_source);
13261
                  for (method_dest = TYPE_METHODS (dest);
13262
                       method_dest; method_dest = TREE_CHAIN (method_dest))
13263
                    if (source_sig ==
13264
                        build_java_argument_signature (TREE_TYPE (method_dest))
13265
                        && source_name == DECL_NAME (method_dest)
13266
                        && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13267
                      return 0;
13268
                }
13269
              return 1;
13270
            }
13271
          else
13272
            return source == dest || interface_of_p (dest, source);
13273
        }
13274
      else
13275
        {
13276
          /* Array */
13277
          return (cast
13278
                  && (DECL_NAME (TYPE_NAME (source))
13279
                      == java_lang_cloneable_identifier_node
13280
                      || (DECL_NAME (TYPE_NAME (source))
13281
                          == java_io_serializable_identifier_node)));
13282
        }
13283
    }
13284
  if (TYPE_ARRAY_P (source))
13285
    {
13286
      if (TYPE_CLASS_P (dest))
13287
        return dest == object_type_node;
13288
      /* Can't cast an array to an interface unless the interface is
13289
         java.lang.Cloneable or java.io.Serializable.  */
13290
      if (TYPE_INTERFACE_P (dest))
13291
        return (DECL_NAME (TYPE_NAME (dest))
13292
                == java_lang_cloneable_identifier_node
13293
                || (DECL_NAME (TYPE_NAME (dest))
13294
                    == java_io_serializable_identifier_node));
13295
      else                      /* Arrays */
13296
        {
13297
          tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13298
          tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13299
 
13300
          /* In case of severe errors, they turn out null */
13301
          if (!dest_element_type || !source_element_type)
13302
            return 0;
13303
          if (source_element_type == dest_element_type)
13304
            return 1;
13305
          return valid_ref_assignconv_cast_p (source_element_type,
13306
                                              dest_element_type, cast);
13307
        }
13308
      return 0;
13309
    }
13310
  return 0;
13311
}
13312
 
13313
static int
13314
valid_cast_to_p (tree source, tree dest)
13315
{
13316
  if (TREE_CODE (source) == POINTER_TYPE)
13317
    source = TREE_TYPE (source);
13318
  if (TREE_CODE (dest) == POINTER_TYPE)
13319
    dest = TREE_TYPE (dest);
13320
 
13321
  if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13322
    return valid_ref_assignconv_cast_p (source, dest, 1);
13323
 
13324
  else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13325
    return 1;
13326
 
13327
  else if (TREE_CODE (source) == BOOLEAN_TYPE
13328
           && TREE_CODE (dest) == BOOLEAN_TYPE)
13329
    return 1;
13330
 
13331
  return 0;
13332
}
13333
 
13334
static tree
13335
do_unary_numeric_promotion (tree arg)
13336
{
13337
  tree type = TREE_TYPE (arg);
13338
  if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13339
      || TREE_CODE (type) == CHAR_TYPE)
13340
    arg = convert (int_type_node, arg);
13341
  return arg;
13342
}
13343
 
13344
/* Return a nonzero value if SOURCE can be converted into DEST using
13345
   the method invocation conversion rule (5.3).  */
13346
static int
13347
valid_method_invocation_conversion_p (tree dest, tree source)
13348
{
13349
  return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13350
           && valid_builtin_assignconv_identity_widening_p (dest, source))
13351
          || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13352
              && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13353
              && valid_ref_assignconv_cast_p (source, dest, 0)));
13354
}
13355
 
13356
/* Build an incomplete binop expression. */
13357
 
13358
static tree
13359
build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13360
{
13361
  tree binop = build2 (op, NULL_TREE, op1, op2);
13362
  TREE_SIDE_EFFECTS (binop) = 1;
13363
  /* Store the location of the operator, for better error report. The
13364
     string of the operator will be rebuild based on the OP value. */
13365
  EXPR_WFL_LINECOL (binop) = op_location;
13366
  return binop;
13367
}
13368
 
13369
/* Build the string of the operator retained by NODE. If NODE is part
13370
   of a compound expression, add an '=' at the end of the string. This
13371
   function is called when an error needs to be reported on an
13372
   operator. The string is returned as a pointer to a static character
13373
   buffer. */
13374
 
13375
static char *
13376
operator_string (tree node)
13377
{
13378
#define BUILD_OPERATOR_STRING(S)                                        \
13379
  {                                                                     \
13380
    sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13381
    return buffer;                                                      \
13382
  }
13383
 
13384
  static char buffer [10];
13385
  switch (TREE_CODE (node))
13386
    {
13387
    case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13388
    case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13389
    case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13390
    case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13391
    case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13392
    case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13393
    case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13394
    case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13395
    case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13396
    case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13397
    case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13398
    case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13399
    case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13400
    case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13401
    case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13402
    case GT_EXPR: BUILD_OPERATOR_STRING (">");
13403
    case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13404
    case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13405
    case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13406
    case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13407
    case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13408
    case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13409
    case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13410
    case PREINCREMENT_EXPR:     /* Fall through */
13411
    case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13412
    case PREDECREMENT_EXPR:     /* Fall through */
13413
    case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13414
    default:
13415
      internal_error ("unregistered operator %s",
13416
                      tree_code_name [TREE_CODE (node)]);
13417
    }
13418
  return NULL;
13419
#undef BUILD_OPERATOR_STRING
13420
}
13421
 
13422
/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13423
 
13424
static int
13425
java_decl_equiv (tree var_acc1, tree var_acc2)
13426
{
13427
  if (JDECL_P (var_acc1))
13428
    return (var_acc1 == var_acc2);
13429
 
13430
  return (TREE_CODE (var_acc1) == COMPONENT_REF
13431
          && TREE_CODE (var_acc2) == COMPONENT_REF
13432
          && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13433
             == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13434
          && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13435
}
13436
 
13437
/* Return a nonzero value if CODE is one of the operators that can be
13438
   used in conjunction with the `=' operator in a compound assignment.  */
13439
 
13440
static int
13441
binop_compound_p (enum tree_code code)
13442
{
13443
  int i;
13444
  for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13445
    if (binop_lookup [i] == code)
13446
      break;
13447
 
13448
  return i < BINOP_COMPOUND_CANDIDATES;
13449
}
13450
 
13451
/* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13452
 
13453
static tree
13454
java_refold (tree t)
13455
{
13456
  tree c, b, ns, decl;
13457
 
13458
  if (TREE_CODE (t) != MODIFY_EXPR)
13459
    return t;
13460
 
13461
  c = TREE_OPERAND (t, 1);
13462
  if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13463
         && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13464
         && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13465
    return t;
13466
 
13467
  /* Now the left branch of the binary operator. */
13468
  b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13469
  if (! (b && TREE_CODE (b) == NOP_EXPR
13470
         && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13471
    return t;
13472
 
13473
  ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13474
  if (! (ns && TREE_CODE (ns) == NOP_EXPR
13475
         && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13476
    return t;
13477
 
13478
  decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13479
  if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13480
      /* It's got to be the an equivalent decl */
13481
      && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13482
    {
13483
      /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13484
      TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13485
      /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13486
      TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13487
      /* Change the right part of the BINOP_EXPR */
13488
      TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13489
    }
13490
 
13491
  return t;
13492
}
13493
 
13494
/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13495
   errors but we modify NODE so that it contains the type computed
13496
   according to the expression, when it's fixed. Otherwise, we write
13497
   error_mark_node as the type. It allows us to further the analysis
13498
   of remaining nodes and detects more errors in certain cases.  */
13499
 
13500
static tree
13501
patch_binop (tree node, tree wfl_op1, tree wfl_op2, int folding)
13502
{
13503
  tree op1 = TREE_OPERAND (node, 0);
13504
  tree op2 = TREE_OPERAND (node, 1);
13505
  tree op1_type = TREE_TYPE (op1);
13506
  tree op2_type = TREE_TYPE (op2);
13507
  tree prom_type = NULL_TREE, cn;
13508
  enum tree_code code = TREE_CODE (node);
13509
 
13510
  /* If 1, tell the routine that we have to return error_mark_node
13511
     after checking for the initialization of the RHS */
13512
  int error_found = 0;
13513
 
13514
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13515
 
13516
  /* If either op_type are NULL, this might be early signs of an
13517
     error situation, unless it's too early to tell (in case we're
13518
     handling a `+', `==', `!=' or `instanceof'.) We want to set op_type
13519
     correctly so the error can be later on reported accurately. */
13520
  if (! (code == PLUS_EXPR || code == NE_EXPR
13521
         || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13522
    {
13523
      tree n;
13524
      if (! op1_type)
13525
        {
13526
          n = java_complete_tree (op1);
13527
          op1_type = TREE_TYPE (n);
13528
        }
13529
      if (! op2_type)
13530
        {
13531
          n = java_complete_tree (op2);
13532
          op2_type = TREE_TYPE (n);
13533
        }
13534
    }
13535
 
13536
  switch (code)
13537
    {
13538
    /* 15.16 Multiplicative operators */
13539
    case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13540
    case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13541
    case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13542
    case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13543
      if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13544
        {
13545
          if (!JNUMERIC_TYPE_P (op1_type))
13546
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13547
          if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13548
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13549
          TREE_TYPE (node) = error_mark_node;
13550
          error_found = 1;
13551
          break;
13552
        }
13553
      prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13554
 
13555
      /* Detect integral division by zero */
13556
      if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13557
          && TREE_CODE (prom_type) == INTEGER_TYPE
13558
          && (op2 == integer_zero_node || op2 == long_zero_node ||
13559
              (TREE_CODE (op2) == INTEGER_CST &&
13560
               ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13561
        {
13562
          parse_warning_context
13563
            (wfl_operator,
13564
             "Evaluating this expression will result in an arithmetic exception being thrown");
13565
          TREE_CONSTANT (node) = 0;
13566
          TREE_INVARIANT (node) = 0;
13567
        }
13568
 
13569
      /* Change the division operator if necessary */
13570
      if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13571
        TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13572
 
13573
      /* Before divisions as is disappear, try to simplify and bail if
13574
         applicable, otherwise we won't perform even simple
13575
         simplifications like (1-1)/3. We can't do that with floating
13576
         point number, folds can't handle them at this stage. */
13577
      if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13578
          && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13579
        {
13580
          TREE_TYPE (node) = prom_type;
13581
          node = fold (node);
13582
          if (TREE_CODE (node) != code)
13583
            return node;
13584
        }
13585
 
13586
      if (TREE_CODE (prom_type) == INTEGER_TYPE
13587
          && flag_use_divide_subroutine
13588
          && ! flag_emit_class_files
13589
          && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13590
        return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13591
 
13592
      /* This one is more complicated. FLOATs are processed by a
13593
         function call to soft_fmod. Duplicate the value of the
13594
         COMPOUND_ASSIGN_P flag. */
13595
      if (code == TRUNC_MOD_EXPR)
13596
        {
13597
          tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13598
          COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13599
          return mod;
13600
        }
13601
      break;
13602
 
13603
    /* 15.17 Additive Operators */
13604
    case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13605
 
13606
      /* Operation is valid if either one argument is a string
13607
         constant, a String object or a StringBuffer crafted for the
13608
         purpose of the a previous usage of the String concatenation
13609
         operator */
13610
 
13611
      if (TREE_CODE (op1) == STRING_CST
13612
          || TREE_CODE (op2) == STRING_CST
13613
          || JSTRING_TYPE_P (op1_type)
13614
          || JSTRING_TYPE_P (op2_type)
13615
          || IS_CRAFTED_STRING_BUFFER_P (op1)
13616
          || IS_CRAFTED_STRING_BUFFER_P (op2))
13617
        return build_string_concatenation (op1, op2);
13618
 
13619
    case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13620
                                   Numeric Types */
13621
      if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13622
        {
13623
          if (!JNUMERIC_TYPE_P (op1_type))
13624
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13625
          if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13626
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13627
          TREE_TYPE (node) = error_mark_node;
13628
          error_found = 1;
13629
          break;
13630
        }
13631
      prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13632
      break;
13633
 
13634
    /* 15.18 Shift Operators */
13635
    case LSHIFT_EXPR:
13636
    case RSHIFT_EXPR:
13637
    case URSHIFT_EXPR:
13638
      if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13639
        {
13640
          if (!JINTEGRAL_TYPE_P (op1_type))
13641
            ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13642
          else
13643
            {
13644
              if (JNUMERIC_TYPE_P (op2_type))
13645
                parse_error_context (wfl_operator,
13646
                                     "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13647
                                     operator_string (node),
13648
                                     lang_printable_name (op2_type, 0));
13649
              else
13650
                parse_error_context (wfl_operator,
13651
                                     "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13652
                                     operator_string (node),
13653
                                     lang_printable_name (op2_type, 0));
13654
            }
13655
          TREE_TYPE (node) = error_mark_node;
13656
          error_found = 1;
13657
          break;
13658
        }
13659
 
13660
      /* Unary numeric promotion (5.6.1) is performed on each operand
13661
         separately */
13662
      op1 = do_unary_numeric_promotion (op1);
13663
      op2 = do_unary_numeric_promotion (op2);
13664
 
13665
      /* If the right hand side is of type `long', first cast it to
13666
         `int'.  */
13667
      if (TREE_TYPE (op2) == long_type_node)
13668
        op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13669
 
13670
      /* The type of the shift expression is the type of the promoted
13671
         type of the left-hand operand */
13672
      prom_type = TREE_TYPE (op1);
13673
 
13674
      /* Shift int only up to 0x1f and long up to 0x3f */
13675
      if (prom_type == int_type_node)
13676
        op2 = fold_build2 (BIT_AND_EXPR, int_type_node, op2,
13677
                           build_int_cst (NULL_TREE, 0x1f));
13678
      else
13679
        op2 = fold_build2 (BIT_AND_EXPR, int_type_node, op2,
13680
                           build_int_cst (NULL_TREE, 0x3f));
13681
 
13682
      /* The >>> operator is a >> operating on unsigned quantities */
13683
      if (code == URSHIFT_EXPR && (folding || ! flag_emit_class_files))
13684
        {
13685
          tree to_return;
13686
          tree utype = java_unsigned_type (prom_type);
13687
          op1 = convert (utype, op1);
13688
 
13689
          to_return = fold_build2 (RSHIFT_EXPR, utype, op1, op2);
13690
          to_return = convert (prom_type, to_return);
13691
          /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13692
          COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13693
          TREE_SIDE_EFFECTS (to_return)
13694
            = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13695
          return to_return;
13696
        }
13697
      break;
13698
 
13699
      /* 15.19.1 Type Comparison Operator instanceof */
13700
    case INSTANCEOF_EXPR:
13701
 
13702
      TREE_TYPE (node) = boolean_type_node;
13703
 
13704
      /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13705
      if ((cn = patch_string (op1)))
13706
        {
13707
          op1 = cn;
13708
          op1_type = TREE_TYPE (op1);
13709
        }
13710
      if (op1_type == NULL_TREE)
13711
        abort ();
13712
 
13713
      if (!(op2_type = resolve_type_during_patch (op2)))
13714
        return error_mark_node;
13715
 
13716
      /* The first operand must be a reference type or the null type */
13717
      if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13718
        error_found = 1;        /* Error reported further below */
13719
 
13720
      /* The second operand must be a reference type */
13721
      if (!JREFERENCE_TYPE_P (op2_type))
13722
        {
13723
          SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13724
          parse_error_context
13725
            (wfl_operator, "Invalid argument %qs for %",
13726
             lang_printable_name (op2_type, 0));
13727
          error_found = 1;
13728
        }
13729
 
13730
      if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13731
        {
13732
          /* If the first operand is null, the result is always false */
13733
          if (op1 == null_pointer_node)
13734
            return boolean_false_node;
13735
          else if (flag_emit_class_files)
13736
            {
13737
              TREE_OPERAND (node, 1) = op2_type;
13738
              TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13739
              return node;
13740
            }
13741
          /* Otherwise we have to invoke instance of to figure it out */
13742
          else
13743
            return build_instanceof (op1, op2_type);
13744
        }
13745
      /* There is no way the expression operand can be an instance of
13746
         the type operand. This is a compile time error. */
13747
      else
13748
        {
13749
          char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13750
          SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13751
          parse_error_context
13752
            (wfl_operator, "Impossible for %qs to be instance of %qs",
13753
             t1, lang_printable_name (op2_type, 0));
13754
          free (t1);
13755
          error_found = 1;
13756
        }
13757
 
13758
      break;
13759
 
13760
      /* 15.21 Bitwise and Logical Operators */
13761
    case BIT_AND_EXPR:
13762
    case BIT_XOR_EXPR:
13763
    case BIT_IOR_EXPR:
13764
      if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13765
        /* Binary numeric promotion is performed on both operand and the
13766
           expression retain that type */
13767
        prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13768
 
13769
      else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13770
               && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13771
        /* The type of the bitwise operator expression is BOOLEAN */
13772
        prom_type = boolean_type_node;
13773
      else
13774
        {
13775
          if (!JINTEGRAL_TYPE_P (op1_type))
13776
            ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13777
          if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13778
            ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13779
          TREE_TYPE (node) = error_mark_node;
13780
          error_found = 1;
13781
          /* Insert a break here if adding thing before the switch's
13782
             break for this case */
13783
        }
13784
      break;
13785
 
13786
      /* 15.22 Conditional-And Operator */
13787
    case TRUTH_ANDIF_EXPR:
13788
      /* 15.23 Conditional-Or Operator */
13789
    case TRUTH_ORIF_EXPR:
13790
      /* Operands must be of BOOLEAN type */
13791
      if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13792
          TREE_CODE (op2_type) != BOOLEAN_TYPE)
13793
        {
13794
          if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13795
            ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13796
          if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13797
            ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13798
          TREE_TYPE (node) = boolean_type_node;
13799
          error_found = 1;
13800
          break;
13801
        }
13802
      else if (integer_zerop (op1))
13803
        {
13804
          return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13805
        }
13806
      else if (integer_onep (op1))
13807
        {
13808
          return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13809
        }
13810
      /* The type of the conditional operators is BOOLEAN */
13811
      prom_type = boolean_type_node;
13812
      break;
13813
 
13814
      /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13815
    case LT_EXPR:
13816
    case GT_EXPR:
13817
    case LE_EXPR:
13818
    case GE_EXPR:
13819
      /* The type of each of the operands must be a primitive numeric
13820
         type */
13821
      if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13822
        {
13823
          if (!JNUMERIC_TYPE_P (op1_type))
13824
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13825
          if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13826
            ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13827
          TREE_TYPE (node) = boolean_type_node;
13828
          error_found = 1;
13829
          break;
13830
        }
13831
      /* Binary numeric promotion is performed on the operands */
13832
      binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13833
      /* The type of the relation expression is always BOOLEAN */
13834
      prom_type = boolean_type_node;
13835
      break;
13836
 
13837
      /* 15.20 Equality Operator */
13838
    case EQ_EXPR:
13839
    case NE_EXPR:
13840
      /* It's time for us to patch the strings. */
13841
      if ((cn = patch_string (op1)))
13842
       {
13843
         op1 = cn;
13844
         op1_type = TREE_TYPE (op1);
13845
       }
13846
      if ((cn = patch_string (op2)))
13847
       {
13848
         op2 = cn;
13849
         op2_type = TREE_TYPE (op2);
13850
       }
13851
 
13852
      /* 15.20.1 Numerical Equality Operators == and != */
13853
      /* Binary numeric promotion is performed on the operands */
13854
      if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13855
        binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13856
 
13857
      /* 15.20.2 Boolean Equality Operators == and != */
13858
      else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13859
          TREE_CODE (op2_type) == BOOLEAN_TYPE)
13860
        ;                       /* Nothing to do here */
13861
 
13862
      /* 15.20.3 Reference Equality Operators == and != */
13863
      /* Types have to be either references or the null type. If
13864
         they're references, it must be possible to convert either
13865
         type to the other by casting conversion. */
13866
      else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13867
               || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13868
               || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13869
               || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13870
                   && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13871
                       || valid_ref_assignconv_cast_p (op2_type,
13872
                                                       op1_type, 1))))
13873
        ;                       /* Nothing to do here */
13874
 
13875
      /* Else we have an error figure what can't be converted into
13876
         what and report the error */
13877
      else
13878
        {
13879
          char *t1;
13880
          t1 = xstrdup (lang_printable_name (op1_type, 0));
13881
          parse_error_context
13882
            (wfl_operator,
13883
             "Incompatible type for %qs. Can't convert %qs to %qs",
13884
             operator_string (node), t1,
13885
             lang_printable_name (op2_type, 0));
13886
          free (t1);
13887
          TREE_TYPE (node) = boolean_type_node;
13888
          error_found = 1;
13889
          break;
13890
        }
13891
      prom_type = boolean_type_node;
13892
      break;
13893
    default:
13894
      abort ();
13895
    }
13896
 
13897
  if (error_found)
13898
    return error_mark_node;
13899
 
13900
  TREE_OPERAND (node, 0) = op1;
13901
  TREE_OPERAND (node, 1) = op2;
13902
  TREE_TYPE (node) = prom_type;
13903
  TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13904
 
13905
  /* fold does not respect side-effect order as required for Java but not C.
13906
   * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13907
   * bytecode.
13908
   */
13909
  if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13910
      : ! TREE_SIDE_EFFECTS (node))
13911
    node = fold (node);
13912
  return node;
13913
}
13914
 
13915
/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13916
   zero value, the value of CSTE comes after the valude of STRING */
13917
 
13918
static tree
13919
do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13920
{
13921
  const char *old = TREE_STRING_POINTER (cste);
13922
  int old_len = TREE_STRING_LENGTH (cste);
13923
  int len = old_len + string_len;
13924
  char *new = alloca (len+1);
13925
 
13926
  if (after)
13927
    {
13928
      memcpy (new, string, string_len);
13929
      memcpy (&new [string_len], old, old_len);
13930
    }
13931
  else
13932
    {
13933
      memcpy (new, old, old_len);
13934
      memcpy (&new [old_len], string, string_len);
13935
    }
13936
  new [len] = '\0';
13937
  return build_string (len, new);
13938
}
13939
 
13940
/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13941
   new STRING_CST on success, NULL_TREE on failure.  */
13942
 
13943
static tree
13944
merge_string_cste (tree op1, tree op2, int after)
13945
{
13946
  /* Handle two string constants right away.  */
13947
  if (TREE_CODE (op2) == STRING_CST)
13948
    return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13949
                                 TREE_STRING_LENGTH (op2), after);
13950
 
13951
  /* Reasonable integer constant can be treated right away.  */
13952
  if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13953
    {
13954
      static const char *const boolean_true = "true";
13955
      static const char *const boolean_false = "false";
13956
      static const char *const null_pointer = "null";
13957
      char ch[4];
13958
      const char *string;
13959
 
13960
      if (op2 == boolean_true_node)
13961
        string = boolean_true;
13962
      else if (op2 == boolean_false_node)
13963
        string = boolean_false;
13964
      else if (op2 == null_pointer_node
13965
               || (integer_zerop (op2)
13966
                   && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13967
        /* FIXME: null is not a compile-time constant, so it is only safe to
13968
           merge if the overall expression is non-constant. However, this
13969
           code always merges without checking the overall expression.  */
13970
        string = null_pointer;
13971
      else if (TREE_TYPE (op2) == char_type_node)
13972
        {
13973
          /* Convert the character into UTF-8.  */
13974
          unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13975
          unsigned char *p = (unsigned char *) ch;
13976
          if (0x01 <= c && c <= 0x7f)
13977
            *p++ = (unsigned char) c;
13978
          else if (c < 0x7ff)
13979
            {
13980
              *p++ = (unsigned char) (c >> 6 | 0xc0);
13981
              *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13982
            }
13983
          else
13984
            {
13985
              *p++ = (unsigned char) (c >> 12 | 0xe0);
13986
              *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13987
              *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13988
            }
13989
          *p = '\0';
13990
 
13991
          string = ch;
13992
        }
13993
      else
13994
        string = string_convert_int_cst (op2);
13995
 
13996
      return do_merge_string_cste (op1, string, strlen (string), after);
13997
    }
13998
  return NULL_TREE;
13999
}
14000
 
14001
/* Tries to statically concatenate OP1 and OP2 if possible. Either one
14002
   has to be a STRING_CST and the other part must be a STRING_CST or a
14003
   INTEGRAL constant. Return a new STRING_CST if the operation
14004
   succeed, NULL_TREE otherwise.
14005
 
14006
   If the case we want to optimize for space, we might want to return
14007
   NULL_TREE for each invocation of this routine. FIXME */
14008
 
14009
static tree
14010
string_constant_concatenation (tree op1, tree op2)
14011
{
14012
  if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
14013
    {
14014
      tree string, rest;
14015
      int invert;
14016
 
14017
      string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
14018
      rest   = (string == op1 ? op2 : op1);
14019
      invert = (string == op1 ? 0 : 1 );
14020
 
14021
      /* Walk REST, only if it looks reasonable */
14022
      if (TREE_CODE (rest) != STRING_CST
14023
          && !IS_CRAFTED_STRING_BUFFER_P (rest)
14024
          && !JSTRING_TYPE_P (TREE_TYPE (rest))
14025
          && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
14026
        {
14027
          rest = java_complete_tree (rest);
14028
          if (rest == error_mark_node)
14029
            return error_mark_node;
14030
          rest = fold (rest);
14031
        }
14032
      return merge_string_cste (string, rest, invert);
14033
    }
14034
  return NULL_TREE;
14035
}
14036
 
14037
/* Implement the `+' operator. Does static optimization if possible,
14038
   otherwise create (if necessary) and append elements to a
14039
   StringBuffer. The StringBuffer will be carried around until it is
14040
   used for a function call or an assignment. Then toString() will be
14041
   called on it to turn it into a String object. */
14042
 
14043
static tree
14044
build_string_concatenation (tree op1, tree op2)
14045
{
14046
  tree result;
14047
  int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14048
 
14049
  /* Try to do some static optimization */
14050
  if ((result = string_constant_concatenation (op1, op2)))
14051
    return result;
14052
 
14053
  /* Discard empty strings on either side of the expression */
14054
  if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14055
    {
14056
      op1 = op2;
14057
      op2 = NULL_TREE;
14058
    }
14059
  else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14060
    op2 = NULL_TREE;
14061
 
14062
  /* If operands are string constant, turn then into object references */
14063
  if (TREE_CODE (op1) == STRING_CST)
14064
    op1 = patch_string_cst (op1);
14065
  if (op2 && TREE_CODE (op2) == STRING_CST)
14066
    op2 = patch_string_cst (op2);
14067
 
14068
  /* If either one of the constant is null and the other non null
14069
     operand is a String constant, return it. */
14070
  if ((TREE_CODE (op1) == STRING_CST) && !op2)
14071
    return op1;
14072
 
14073
  /* If OP1 isn't already a StringBuffer, create and
14074
     initialize a new one */
14075
  if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14076
    {
14077
      /* Two solutions here:
14078
         1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14079
         2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14080
      if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14081
        op1 = BUILD_STRING_BUFFER (op1);
14082
      else
14083
        {
14084
          tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14085
          op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14086
        }
14087
    }
14088
 
14089
  if (op2)
14090
    {
14091
      /* OP1 is no longer the last node holding a crafted StringBuffer */
14092
      IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14093
      /* Create a node for `{new...,xxx}.append (op2)' */
14094
      op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14095
    }
14096
 
14097
  /* Mark the last node holding a crafted StringBuffer */
14098
  IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14099
 
14100
  TREE_SIDE_EFFECTS (op1) = side_effects;
14101
  return op1;
14102
}
14103
 
14104
/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14105
   StringBuffer. If no string were found to be patched, return
14106
   NULL. */
14107
 
14108
static tree
14109
patch_string (tree node)
14110
{
14111
  if (node == error_mark_node)
14112
    return error_mark_node;
14113
  if (TREE_CODE (node) == STRING_CST)
14114
    return patch_string_cst (node);
14115
  else if (IS_CRAFTED_STRING_BUFFER_P (node))
14116
    {
14117
      int saved = ctxp->explicit_constructor_p;
14118
      tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14119
      tree ret;
14120
      /* Temporary disable forbid the use of `this'. */
14121
      ctxp->explicit_constructor_p = 0;
14122
      ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14123
      /* String concatenation arguments must be evaluated in order too. */
14124
      ret = force_evaluation_order (ret);
14125
      /* Restore it at its previous value */
14126
      ctxp->explicit_constructor_p = saved;
14127
      return ret;
14128
    }
14129
  return NULL_TREE;
14130
}
14131
 
14132
/* Build the internal representation of a string constant.  */
14133
 
14134
static tree
14135
patch_string_cst (tree node)
14136
{
14137
  int location;
14138
  if (! flag_emit_class_files)
14139
    {
14140
      node = get_identifier (TREE_STRING_POINTER (node));
14141
      location = alloc_name_constant (CONSTANT_String, node);
14142
      node = build_ref_from_constant_pool (location);
14143
    }
14144
  TREE_CONSTANT (node) = 1;
14145
  TREE_INVARIANT (node) = 1;
14146
 
14147
  /* ??? Guessing that the class file code can't handle casts.  */
14148
  if (! flag_emit_class_files)
14149
    node = convert (string_ptr_type_node, node);
14150
  else
14151
    TREE_TYPE (node) = string_ptr_type_node;
14152
 
14153
  return node;
14154
}
14155
 
14156
/* Build an incomplete unary operator expression. */
14157
 
14158
static tree
14159
build_unaryop (int op_token, int op_location, tree op1)
14160
{
14161
  enum tree_code op;
14162
  tree unaryop;
14163
  switch (op_token)
14164
    {
14165
    case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14166
    case MINUS_TK: op = NEGATE_EXPR; break;
14167
    case NEG_TK: op = TRUTH_NOT_EXPR; break;
14168
    case NOT_TK: op = BIT_NOT_EXPR; break;
14169
    default: abort ();
14170
    }
14171
 
14172
  unaryop = build1 (op, NULL_TREE, op1);
14173
  TREE_SIDE_EFFECTS (unaryop) = 1;
14174
  /* Store the location of the operator, for better error report. The
14175
     string of the operator will be rebuild based on the OP value. */
14176
  EXPR_WFL_LINECOL (unaryop) = op_location;
14177
  return unaryop;
14178
}
14179
 
14180
/* Special case for the ++/-- operators, since they require an extra
14181
   argument to build, which is set to NULL and patched
14182
   later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14183
 
14184
static tree
14185
build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14186
{
14187
  static const enum tree_code lookup [2][2] =
14188
    {
14189
      { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14190
      { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14191
    };
14192
  tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14193
                      NULL_TREE, op1, NULL_TREE);
14194
  TREE_SIDE_EFFECTS (node) = 1;
14195
  /* Store the location of the operator, for better error report. The
14196
     string of the operator will be rebuild based on the OP value. */
14197
  EXPR_WFL_LINECOL (node) = op_location;
14198
  return node;
14199
}
14200
 
14201
/* Build an incomplete cast operator, based on the use of the
14202
   CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14203
   set. java_complete_tree is trained to walk a CONVERT_EXPR even
14204
   though its type is already set.  */
14205
 
14206
static tree
14207
build_cast (int location, tree type, tree exp)
14208
{
14209
  tree node = build1 (CONVERT_EXPR, type, exp);
14210
  EXPR_WFL_LINECOL (node) = location;
14211
  return node;
14212
}
14213
 
14214
/* Build an incomplete class reference operator.  */
14215
static tree
14216
build_incomplete_class_ref (int location, tree class_name)
14217
{
14218
  tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14219
  tree class_decl = GET_CPC ();
14220
  tree this_class = TREE_TYPE (class_decl);
14221
 
14222
  /* Generate the synthetic static method `class$'.  (Previously we
14223
     deferred this, causing different method tables to be emitted
14224
     for native code and bytecode.)  */
14225
  if (!TYPE_DOT_CLASS (this_class)
14226
      && !JPRIMITIVE_TYPE_P (class_name)
14227
      && !(TREE_CODE (class_name) == VOID_TYPE))
14228
    {
14229
      tree cpc_list = GET_CPC_LIST();
14230
      tree cpc = cpc_list;
14231
      tree target_class;
14232
 
14233
      /* For inner classes, add a 'class$' method to their outermost
14234
         context, creating it if necessary.  */
14235
 
14236
      while (GET_NEXT_ENCLOSING_CPC(cpc))
14237
        cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14238
      class_decl = TREE_VALUE (cpc);
14239
 
14240
      target_class = TREE_TYPE (class_decl);
14241
 
14242
      if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14243
        {
14244
          /* For interfaces, adding a static 'class$' method directly
14245
             is illegal.  So create an inner class to contain the new
14246
             method.  Empirically this matches the behavior of javac.  */
14247
          tree t, inner;
14248
          /* We want the generated inner class inside the outermost class. */
14249
          GET_CPC_LIST() = cpc;
14250
          t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14251
          inner = create_anonymous_class (t);
14252
          target_class = TREE_TYPE (inner);
14253
          end_class_declaration (1);
14254
          GET_CPC_LIST() = cpc_list;
14255
        }
14256
 
14257
      if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14258
        build_dot_class_method (target_class);
14259
 
14260
      if (this_class != target_class)
14261
        TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14262
    }
14263
 
14264
  EXPR_WFL_LINECOL (node) = location;
14265
  return node;
14266
}
14267
 
14268
/* Complete an incomplete class reference operator.  */
14269
static tree
14270
patch_incomplete_class_ref (tree node)
14271
{
14272
  tree type = TREE_OPERAND (node, 0);
14273
  tree ref_type;
14274
 
14275
  if (!(ref_type = resolve_type_during_patch (type)))
14276
    return error_mark_node;
14277
 
14278
  /* If we're not emitting class files and we know ref_type is a
14279
     compiled class, build a direct reference.  */
14280
  if ((! flag_emit_class_files && is_compiled_class (ref_type))
14281
      || JPRIMITIVE_TYPE_P (ref_type)
14282
      || TREE_CODE (ref_type) == VOID_TYPE)
14283
    {
14284
      tree dot = build_class_ref (ref_type);
14285
      /* A class referenced by `foo.class' is initialized.  */
14286
      if (!flag_emit_class_files)
14287
       dot = build_class_init (ref_type, dot);
14288
      return java_complete_tree (dot);
14289
    }
14290
 
14291
  /* If we're emitting class files and we have to deal with non
14292
     primitive types, we invoke the synthetic static method `class$'.  */
14293
  ref_type = build_dot_class_method_invocation (current_class, ref_type);
14294
  return java_complete_tree (ref_type);
14295
}
14296
 
14297
/* 15.14 Unary operators. We return error_mark_node in case of error,
14298
   but preserve the type of NODE if the type is fixed.  */
14299
 
14300
static tree
14301
patch_unaryop (tree node, tree wfl_op)
14302
{
14303
  tree op = TREE_OPERAND (node, 0);
14304
  tree op_type = TREE_TYPE (op);
14305
  tree prom_type = NULL_TREE, value, decl;
14306
  int nested_field_flag = 0;
14307
  int code = TREE_CODE (node);
14308
  int error_found = 0;
14309
 
14310
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14311
 
14312
  switch (code)
14313
    {
14314
      /* 15.13.2 Postfix Increment Operator ++ */
14315
    case POSTINCREMENT_EXPR:
14316
      /* 15.13.3 Postfix Increment Operator -- */
14317
    case POSTDECREMENT_EXPR:
14318
      /* 15.14.1 Prefix Increment Operator ++ */
14319
    case PREINCREMENT_EXPR:
14320
      /* 15.14.2 Prefix Decrement Operator -- */
14321
    case PREDECREMENT_EXPR:
14322
      op = decl = extract_field_decl (op);
14323
      nested_field_flag
14324
        = nested_field_expanded_access_p (op, NULL, NULL, NULL);
14325
      /* We might be trying to change an outer field accessed using
14326
         access method. */
14327
      if (nested_field_flag)
14328
        {
14329
          /* Retrieve the decl of the field we're trying to access. We
14330
             do that by first retrieving the function we would call to
14331
             access the field. It has been already verified that this
14332
             field isn't final */
14333
          if (flag_emit_class_files)
14334
            decl = TREE_OPERAND (op, 0);
14335
          else
14336
            decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14337
          decl = DECL_FUNCTION_ACCESS_DECL (decl);
14338
        }
14339
      /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14340
      else if (!JDECL_P (decl)
14341
          && TREE_CODE (decl) != COMPONENT_REF
14342
          && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14343
          && TREE_CODE (decl) != INDIRECT_REF
14344
          && !(TREE_CODE (decl) == COMPOUND_EXPR
14345
               && TREE_OPERAND (decl, 1)
14346
               && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14347
        {
14348
          TREE_TYPE (node) = error_mark_node;
14349
          error_found = 1;
14350
        }
14351
 
14352
      /* From now on, we know that op if a variable and that it has a
14353
         valid wfl. We use wfl_op to locate errors related to the
14354
         ++/-- operand. */
14355
      if (!JNUMERIC_TYPE_P (op_type))
14356
        {
14357
          parse_error_context
14358
            (wfl_op, "Invalid argument type %qs to %qs",
14359
             lang_printable_name (op_type, 0), operator_string (node));
14360
          TREE_TYPE (node) = error_mark_node;
14361
          error_found = 1;
14362
        }
14363
      else
14364
        {
14365
          /* Before the addition, binary numeric promotion is performed on
14366
             both operands, if really necessary */
14367
          if (JINTEGRAL_TYPE_P (op_type))
14368
            {
14369
              value = build_int_cst (op_type, 1);
14370
              TREE_TYPE (node) = op_type;
14371
            }
14372
          else
14373
            {
14374
              value = build_int_cst (NULL_TREE, 1);
14375
              TREE_TYPE (node) =
14376
                binary_numeric_promotion (op_type,
14377
                                          TREE_TYPE (value), &op, &value);
14378
            }
14379
 
14380
          /* We remember we might be accessing an outer field */
14381
          if (nested_field_flag)
14382
            {
14383
              /* We re-generate an access to the field */
14384
              value = build2 (PLUS_EXPR, TREE_TYPE (op),
14385
                              build_nested_field_access (wfl_op, decl), value);
14386
 
14387
              /* And we patch the original access$() into a write
14388
                 with plus_op as a rhs */
14389
              return nested_field_access_fix (node, op, value);
14390
            }
14391
 
14392
          /* And write back into the node. */
14393
          TREE_OPERAND (node, 0) = op;
14394
          TREE_OPERAND (node, 1) = value;
14395
          /* Convert the overall back into its original type, if
14396
             necessary, and return */
14397
          if (JINTEGRAL_TYPE_P (op_type))
14398
            return fold (node);
14399
          else
14400
            return fold (convert (op_type, node));
14401
        }
14402
      break;
14403
 
14404
      /* 15.14.3 Unary Plus Operator + */
14405
    case UNARY_PLUS_EXPR:
14406
      /* 15.14.4 Unary Minus Operator - */
14407
    case NEGATE_EXPR:
14408
      if (!JNUMERIC_TYPE_P (op_type))
14409
        {
14410
          ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14411
          TREE_TYPE (node) = error_mark_node;
14412
          error_found = 1;
14413
        }
14414
      /* Unary numeric promotion is performed on operand */
14415
      else
14416
        {
14417
          op = do_unary_numeric_promotion (op);
14418
          prom_type = TREE_TYPE (op);
14419
          if (code == UNARY_PLUS_EXPR)
14420
            return fold (op);
14421
        }
14422
      break;
14423
 
14424
      /* 15.14.5 Bitwise Complement Operator ~ */
14425
    case BIT_NOT_EXPR:
14426
      if (!JINTEGRAL_TYPE_P (op_type))
14427
        {
14428
          ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14429
          TREE_TYPE (node) = error_mark_node;
14430
          error_found = 1;
14431
        }
14432
      else
14433
        {
14434
          op = do_unary_numeric_promotion (op);
14435
          prom_type = TREE_TYPE (op);
14436
        }
14437
      break;
14438
 
14439
      /* 15.14.6 Logical Complement Operator ! */
14440
    case TRUTH_NOT_EXPR:
14441
      if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14442
        {
14443
          ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14444
          /* But the type is known. We will report an error if further
14445
             attempt of a assignment is made with this rhs */
14446
          TREE_TYPE (node) = boolean_type_node;
14447
          error_found = 1;
14448
        }
14449
      else
14450
        prom_type = boolean_type_node;
14451
      break;
14452
 
14453
      /* 15.15 Cast Expression */
14454
    case CONVERT_EXPR:
14455
      value = patch_cast (node, wfl_operator);
14456
      if (value == error_mark_node)
14457
        {
14458
          /* If this cast is part of an assignment, we tell the code
14459
             that deals with it not to complain about a mismatch,
14460
             because things have been cast, anyways */
14461
          TREE_TYPE (node) = error_mark_node;
14462
          error_found = 1;
14463
        }
14464
      else
14465
        {
14466
          value = fold (value);
14467
          return value;
14468
        }
14469
      break;
14470
 
14471
    case NOP_EXPR:
14472
      /* This can only happen when the type is already known.  */
14473
      gcc_assert (TREE_TYPE (node) != NULL_TREE);
14474
      prom_type = TREE_TYPE (node);
14475
      break;
14476
    }
14477
 
14478
  if (error_found)
14479
    return error_mark_node;
14480
 
14481
  /* There are cases where node has been replaced by something else
14482
     and we don't end up returning here: UNARY_PLUS_EXPR,
14483
     CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14484
  TREE_OPERAND (node, 0) = fold (op);
14485
  TREE_TYPE (node) = prom_type;
14486
  TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14487
  return fold (node);
14488
}
14489
 
14490
/* Generic type resolution that sometimes takes place during node
14491
   patching. Returned the resolved type or generate an error
14492
   message. Return the resolved type or NULL_TREE.  */
14493
 
14494
static tree
14495
resolve_type_during_patch (tree type)
14496
{
14497
  if (unresolved_type_p (type, NULL))
14498
    {
14499
      tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14500
      if (!type_decl)
14501
        {
14502
          parse_error_context (type,
14503
                               "Class %qs not found in type declaration",
14504
                               IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14505
          return NULL_TREE;
14506
        }
14507
 
14508
      check_deprecation (type, type_decl);
14509
 
14510
      return TREE_TYPE (type_decl);
14511
    }
14512
  return type;
14513
}
14514
 
14515
/* 5.5 Casting Conversion. error_mark_node is returned if an error is
14516
   found. Otherwise NODE or something meant to replace it is returned.  */
14517
 
14518
static tree
14519
patch_cast (tree node, tree wfl_op)
14520
{
14521
  tree op = TREE_OPERAND (node, 0);
14522
  tree cast_type = TREE_TYPE (node);
14523
  tree patched, op_type;
14524
  char *t1;
14525
 
14526
  /* Some string patching might be necessary at this stage */
14527
  if ((patched = patch_string (op)))
14528
    TREE_OPERAND (node, 0) = op = patched;
14529
  op_type = TREE_TYPE (op);
14530
 
14531
  /* First resolve OP_TYPE if unresolved */
14532
  if (!(cast_type = resolve_type_during_patch (cast_type)))
14533
    return error_mark_node;
14534
 
14535
  /* Check on cast that are proven correct at compile time */
14536
  if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14537
    {
14538
      /* Same type */
14539
      if (cast_type == op_type)
14540
        return node;
14541
 
14542
      /* A narrowing conversion from a floating-point number to an
14543
         integral type requires special handling (5.1.3).  */
14544
      if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14545
        if (cast_type != long_type_node)
14546
          op = convert (integer_type_node, op);
14547
 
14548
      /* Try widening/narrowing conversion.  Potentially, things need
14549
         to be worked out in gcc so we implement the extreme cases
14550
         correctly.  fold_convert() needs to be fixed.  */
14551
      return convert (cast_type, op);
14552
    }
14553
 
14554
  /* It's also valid to cast a boolean into a boolean */
14555
  if (op_type == boolean_type_node && cast_type == boolean_type_node)
14556
    return node;
14557
 
14558
  /* null can be casted to references */
14559
  if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14560
    return build_null_of_type (cast_type);
14561
 
14562
  /* The remaining legal casts involve conversion between reference
14563
     types. Check for their compile time correctness. */
14564
  if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14565
      && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14566
    {
14567
      TREE_TYPE (node) = promote_type (cast_type);
14568
      /* Now, the case can be determined correct at compile time if
14569
         OP_TYPE can be converted into CAST_TYPE by assignment
14570
         conversion (5.2) */
14571
 
14572
      if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14573
        {
14574
          TREE_SET_CODE (node, NOP_EXPR);
14575
          return node;
14576
        }
14577
 
14578
      if (flag_emit_class_files)
14579
        {
14580
          TREE_SET_CODE (node, CONVERT_EXPR);
14581
          return node;
14582
        }
14583
 
14584
      /* The cast requires a run-time check */
14585
      return build3 (CALL_EXPR, promote_type (cast_type),
14586
                     build_address_of (soft_checkcast_node),
14587
                     tree_cons (NULL_TREE, build_class_ref (cast_type),
14588
                                build_tree_list (NULL_TREE, op)),
14589
                     NULL_TREE);
14590
    }
14591
 
14592
  /* Any other casts are proven incorrect at compile time */
14593
  t1 = xstrdup (lang_printable_name (op_type, 0));
14594
  parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14595
                       t1, lang_printable_name (cast_type, 0));
14596
  free (t1);
14597
  return error_mark_node;
14598
}
14599
 
14600
/* Build a null constant and give it the type TYPE.  */
14601
 
14602
static tree
14603
build_null_of_type (tree type)
14604
{
14605
  tree node = build_int_cst (promote_type (type), 0);
14606
  return node;
14607
}
14608
 
14609
/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14610
   a list of indices. */
14611
static tree
14612
build_array_ref (int location, tree array, tree index)
14613
{
14614
  tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14615
                      NULL_TREE, NULL_TREE);
14616
  EXPR_WFL_LINECOL (node) = location;
14617
  return node;
14618
}
14619
 
14620
/* 15.12 Array Access Expression */
14621
 
14622
static tree
14623
patch_array_ref (tree node)
14624
{
14625
  tree array = TREE_OPERAND (node, 0);
14626
  tree array_type  = TREE_TYPE (array);
14627
  tree index = TREE_OPERAND (node, 1);
14628
  tree index_type = TREE_TYPE (index);
14629
  int error_found = 0;
14630
 
14631
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14632
 
14633
  if (TREE_CODE (array_type) == POINTER_TYPE)
14634
    array_type = TREE_TYPE (array_type);
14635
 
14636
  /* The array reference must be an array */
14637
  if (!TYPE_ARRAY_P (array_type))
14638
    {
14639
      parse_error_context
14640
        (wfl_operator,
14641
         "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14642
         lang_printable_name (array_type, 0));
14643
      TREE_TYPE (node) = error_mark_node;
14644
      error_found = 1;
14645
    }
14646
 
14647
  /* The array index undergoes unary numeric promotion. The promoted
14648
     type must be int */
14649
  index = do_unary_numeric_promotion (index);
14650
  if (TREE_TYPE (index) != int_type_node)
14651
    {
14652
      if (valid_cast_to_p (index_type, int_type_node))
14653
        parse_error_context (wfl_operator,
14654
   "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %",
14655
                             lang_printable_name (index_type, 0));
14656
      else
14657
        parse_error_context (wfl_operator,
14658
          "Incompatible type for %<[]%>. Can't convert %qs to %",
14659
                             lang_printable_name (index_type, 0));
14660
      TREE_TYPE (node) = error_mark_node;
14661
      error_found = 1;
14662
    }
14663
 
14664
  if (error_found)
14665
    return error_mark_node;
14666
 
14667
  array_type = TYPE_ARRAY_ELEMENT (array_type);
14668
 
14669
  if (flag_emit_class_files)
14670
    {
14671
      TREE_OPERAND (node, 0) = array;
14672
      TREE_OPERAND (node, 1) = index;
14673
    }
14674
  else
14675
    node = build_java_arrayaccess (array, array_type, index);
14676
  TREE_TYPE (node) = array_type;
14677
  return node;
14678
}
14679
 
14680
/* 15.9 Array Creation Expressions */
14681
 
14682
static tree
14683
build_newarray_node (tree type, tree dims, int extra_dims)
14684
{
14685
  tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14686
                      nreverse (dims),
14687
                      build_int_cst (NULL_TREE, extra_dims));
14688
  return node;
14689
}
14690
 
14691
static tree
14692
patch_newarray (tree node)
14693
{
14694
  tree type = TREE_OPERAND (node, 0);
14695
  tree dims = TREE_OPERAND (node, 1);
14696
  tree cdim, array_type;
14697
  int error_found = 0;
14698
  int ndims = 0;
14699
  int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14700
 
14701
  /* Dimension types are verified. It's better for the types to be
14702
     verified in order. */
14703
  for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14704
    {
14705
      int dim_error = 0;
14706
      tree dim = TREE_VALUE (cdim);
14707
 
14708
      /* Dim might have been saved during its evaluation */
14709
      dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14710
 
14711
      /* The type of each specified dimension must be an integral type. */
14712
      if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14713
        dim_error = 1;
14714
 
14715
      /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14716
         promoted type must be int. */
14717
      else
14718
        {
14719
          dim = do_unary_numeric_promotion (dim);
14720
          if (TREE_TYPE (dim) != int_type_node)
14721
            dim_error = 1;
14722
        }
14723
 
14724
      /* Report errors on types here */
14725
      if (dim_error)
14726
        {
14727
          parse_error_context
14728
            (TREE_PURPOSE (cdim),
14729
             "Incompatible type for dimension in array creation expression. %s convert %qs to %",
14730
             (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14731
              "Explicit cast needed to" : "Can't"),
14732
             lang_printable_name (TREE_TYPE (dim), 0));
14733
          error_found = 1;
14734
        }
14735
 
14736
      TREE_PURPOSE (cdim) = NULL_TREE;
14737
    }
14738
 
14739
  /* Resolve array base type if unresolved */
14740
  if (!(type = resolve_type_during_patch (type)))
14741
    error_found = 1;
14742
 
14743
  if (error_found)
14744
    {
14745
      /* We don't want further evaluation of this bogus array creation
14746
         operation */
14747
      TREE_TYPE (node) = error_mark_node;
14748
      return error_mark_node;
14749
    }
14750
 
14751
  /* Set array_type to the actual (promoted) array type of the result. */
14752
  if (TREE_CODE (type) == RECORD_TYPE)
14753
    type = build_pointer_type (type);
14754
  while (--xdims >= 0)
14755
    {
14756
      type = promote_type (build_java_array_type (type, -1));
14757
    }
14758
  dims = nreverse (dims);
14759
  array_type = type;
14760
  for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14761
    {
14762
      type = array_type;
14763
      array_type
14764
        = build_java_array_type (type,
14765
                                 TREE_CODE (cdim) == INTEGER_CST
14766
                                 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14767
                                 : -1);
14768
      array_type = promote_type (array_type);
14769
    }
14770
  dims = nreverse (dims);
14771
 
14772
  /* The node is transformed into a function call. Things are done
14773
     differently according to the number of dimensions. If the number
14774
     of dimension is equal to 1, then the nature of the base type
14775
     (primitive or not) matters. */
14776
  if (ndims == 1)
14777
    return build_new_array (type, TREE_VALUE (dims));
14778
 
14779
  /* Can't reuse what's already written in expr.c because it uses the
14780
     JVM stack representation. Provide a build_multianewarray. FIXME */
14781
  return build3 (CALL_EXPR, array_type,
14782
                 build_address_of (soft_multianewarray_node),
14783
                 tree_cons (NULL_TREE,
14784
                            build_class_ref (TREE_TYPE (array_type)),
14785
                            tree_cons (NULL_TREE,
14786
                                       build_int_cst (NULL_TREE, ndims),
14787
                                       dims)),
14788
                 NULL_TREE);
14789
}
14790
 
14791
/* 10.6 Array initializer.  */
14792
 
14793
/* Build a wfl for array element that don't have one, so we can
14794
   pin-point errors.  */
14795
 
14796
static tree
14797
maybe_build_array_element_wfl (tree node)
14798
{
14799
  if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14800
    {
14801
      /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14802
      return build_expr_wfl (NULL_TREE,
14803
#ifdef USE_MAPPED_LOCATION
14804
                             input_location
14805
#else
14806
                             ctxp->filename,
14807
                             ctxp->lexer->token_start.line,
14808
                             ctxp->lexer->token_start.col
14809
#endif
14810
                             );
14811
    }
14812
  else
14813
    return NULL_TREE;
14814
}
14815
 
14816
/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14817
   identification of initialized arrays easier to detect during walk
14818
   and expansion.  */
14819
 
14820
static tree
14821
build_new_array_init (int location, tree values)
14822
{
14823
  tree constructor = build_constructor_from_list (NULL_TREE,
14824
                                                  nreverse (values));
14825
  tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14826
  EXPR_WFL_LINECOL (to_return) = location;
14827
  return to_return;
14828
}
14829
 
14830
/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14831
   occurred.  Otherwise return NODE after having set its type
14832
   appropriately.  */
14833
 
14834
static tree
14835
patch_new_array_init (tree type, tree node)
14836
{
14837
  int error_seen = 0;
14838
  tree element_type;
14839
  unsigned HOST_WIDE_INT length;
14840
  constructor_elt *current;
14841
  int all_constant = 1;
14842
  tree init = TREE_OPERAND (node, 0);
14843
 
14844
  if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14845
    {
14846
      parse_error_context (node,
14847
                           "Invalid array initializer for non-array type %qs",
14848
                           lang_printable_name (type, 1));
14849
      return error_mark_node;
14850
    }
14851
  type = TREE_TYPE (type);
14852
  element_type = TYPE_ARRAY_ELEMENT (type);
14853
 
14854
  for (length = 0;
14855
       VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init),
14856
                    length, current);
14857
       length++)
14858
    {
14859
      tree elt = current->value;
14860
      if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14861
        {
14862
          error_seen |= array_constructor_check_entry (element_type, current);
14863
          elt = current->value;
14864
          /* When compiling to native code, STRING_CST is converted to
14865
             INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14866
          if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14867
            all_constant = 0;
14868
        }
14869
      else
14870
        {
14871
          current->value = patch_new_array_init (element_type, elt);
14872
          current->index = NULL_TREE;
14873
          all_constant = 0;
14874
        }
14875
      if (elt && TREE_CODE (elt) == TREE_LIST
14876
          && TREE_VALUE (elt) == error_mark_node)
14877
        error_seen = 1;
14878
    }
14879
 
14880
  if (error_seen)
14881
    return error_mark_node;
14882
 
14883
  /* Create a new type. We can't reuse the one we have here by
14884
     patching its dimension because it originally is of dimension -1
14885
     hence reused by gcc. This would prevent triangular arrays. */
14886
  type = build_java_array_type (element_type, length);
14887
  TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14888
  TREE_TYPE (node) = promote_type (type);
14889
  TREE_CONSTANT (init) = all_constant;
14890
  TREE_INVARIANT (init) = all_constant;
14891
  TREE_CONSTANT (node) = all_constant;
14892
  TREE_INVARIANT (node) = all_constant;
14893
  return node;
14894
}
14895
 
14896
/* Verify that one entry of the initializer element list can be
14897
   assigned to the array base type. Report 1 if an error occurred, 0
14898
   otherwise.  */
14899
 
14900
static int
14901
array_constructor_check_entry (tree type, constructor_elt *entry)
14902
{
14903
  char *array_type_string = NULL;       /* For error reports */
14904
  tree value, type_value, new_value, wfl_value, patched;
14905
  int error_seen = 0;
14906
 
14907
  new_value = NULL_TREE;
14908
  wfl_value = entry->value;
14909
 
14910
  value = java_complete_tree (entry->value);
14911
  /* patch_string return error_mark_node if arg is error_mark_node */
14912
  if ((patched = patch_string (value)))
14913
    value = patched;
14914
  if (value == error_mark_node)
14915
    return 1;
14916
 
14917
  type_value = TREE_TYPE (value);
14918
 
14919
  /* At anytime, try_builtin_assignconv can report a warning on
14920
     constant overflow during narrowing. */
14921
  SET_WFL_OPERATOR (wfl_operator, entry->index, wfl_value);
14922
  new_value = try_builtin_assignconv (wfl_operator, type, value);
14923
  if (!new_value && (new_value = try_reference_assignconv (type, value)))
14924
    type_value = promote_type (type);
14925
 
14926
  /* Check and report errors */
14927
  if (!new_value)
14928
    {
14929
      const char *const msg = (!valid_cast_to_p (type_value, type) ?
14930
                   "Can't" : "Explicit cast needed to");
14931
      if (!array_type_string)
14932
        array_type_string = xstrdup (lang_printable_name (type, 1));
14933
      parse_error_context
14934
        (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14935
         msg, lang_printable_name (type_value, 1), array_type_string);
14936
      error_seen = 1;
14937
    }
14938
 
14939
  if (new_value)
14940
    entry->value = new_value;
14941
 
14942
  if (array_type_string)
14943
    free (array_type_string);
14944
 
14945
  entry->index = NULL_TREE;
14946
  return error_seen;
14947
}
14948
 
14949
static tree
14950
build_this (int location)
14951
{
14952
  tree node = build_wfl_node (this_identifier_node);
14953
  TREE_SET_CODE (node, THIS_EXPR);
14954
  EXPR_WFL_LINECOL (node) = location;
14955
  return node;
14956
}
14957
 
14958
/* 14.15 The return statement. It builds a modify expression that
14959
   assigns the returned value to the RESULT_DECL that hold the value
14960
   to be returned. */
14961
 
14962
static tree
14963
build_return (int location, tree op)
14964
{
14965
  tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14966
  EXPR_WFL_LINECOL (node) = location;
14967
  node = build_debugable_stmt (location, node);
14968
  return node;
14969
}
14970
 
14971
static tree
14972
patch_return (tree node)
14973
{
14974
  tree return_exp = TREE_OPERAND (node, 0);
14975
  tree meth = current_function_decl;
14976
  tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14977
  int error_found = 0;
14978
 
14979
  TREE_TYPE (node) = error_mark_node;
14980
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14981
 
14982
  /* It's invalid to have a return value within a function that is
14983
     declared with the keyword void or that is a constructor */
14984
  if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14985
    error_found = 1;
14986
 
14987
  /* It's invalid to use a return statement in a static block */
14988
  if (DECL_CLINIT_P (current_function_decl))
14989
    error_found = 1;
14990
 
14991
  /* It's invalid to have a no return value within a function that
14992
     isn't declared with the keyword `void' */
14993
  if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14994
    error_found = 2;
14995
 
14996
  if (DECL_INSTINIT_P (current_function_decl))
14997
    error_found = 1;
14998
 
14999
  if (error_found)
15000
    {
15001
      if (DECL_INSTINIT_P (current_function_decl))
15002
        parse_error_context (wfl_operator,
15003
                             "% inside instance initializer");
15004
 
15005
      else if (DECL_CLINIT_P (current_function_decl))
15006
        parse_error_context (wfl_operator,
15007
                             "% inside static initializer");
15008
 
15009
      else if (!DECL_CONSTRUCTOR_P (meth))
15010
        {
15011
          char *t = xstrdup (lang_printable_name (mtype, 0));
15012
          parse_error_context (wfl_operator,
15013
                               "% with%s value from %<%s %s%>",
15014
                               (error_found == 1 ? "" : "out"),
15015
                               t, lang_printable_name (meth, 2));
15016
          free (t);
15017
        }
15018
      else
15019
        parse_error_context (wfl_operator,
15020
                             "% with value from constructor %qs",
15021
                             lang_printable_name (meth, 2));
15022
      return error_mark_node;
15023
    }
15024
 
15025
  /* If we have a return_exp, build a modify expression and expand
15026
     it. Note: at that point, the assignment is declared valid, but we
15027
     may want to carry some more hacks */
15028
  if (return_exp)
15029
    {
15030
      tree exp = java_complete_tree (return_exp);
15031
      tree modify, patched;
15032
 
15033
      if ((patched = patch_string (exp)))
15034
        exp = patched;
15035
 
15036
      modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
15037
      EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
15038
      modify = java_complete_tree (modify);
15039
 
15040
      if (modify != error_mark_node)
15041
        {
15042
          TREE_SIDE_EFFECTS (modify) = 1;
15043
          TREE_OPERAND (node, 0) = modify;
15044
        }
15045
      else
15046
        return error_mark_node;
15047
    }
15048
  TREE_TYPE (node) = void_type_node;
15049
  TREE_SIDE_EFFECTS (node) = 1;
15050
  return node;
15051
}
15052
 
15053
/* 14.8 The if Statement */
15054
 
15055
static tree
15056
build_if_else_statement (int location, tree expression, tree if_body,
15057
                         tree else_body)
15058
{
15059
  tree node;
15060
  if (!else_body)
15061
    else_body = build_java_empty_stmt ();
15062
  node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
15063
  EXPR_WFL_LINECOL (node) = location;
15064
  node = build_debugable_stmt (location, node);
15065
  return node;
15066
}
15067
 
15068
static tree
15069
patch_if_else_statement (tree node)
15070
{
15071
  tree expression = TREE_OPERAND (node, 0);
15072
  int can_complete_normally
15073
    = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15074
       | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15075
 
15076
  TREE_TYPE (node) = error_mark_node;
15077
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15078
 
15079
  /* The type of expression must be boolean */
15080
  if (TREE_TYPE (expression) != boolean_type_node
15081
      && TREE_TYPE (expression) != promoted_boolean_type_node)
15082
    {
15083
      parse_error_context
15084
        (wfl_operator,
15085
         "Incompatible type for %. Can't convert %qs to %",
15086
         lang_printable_name (TREE_TYPE (expression), 0));
15087
      return error_mark_node;
15088
    }
15089
 
15090
  TREE_TYPE (node) = void_type_node;
15091
  TREE_SIDE_EFFECTS (node) = 1;
15092
  CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15093
  return node;
15094
}
15095
 
15096
/* 14.6 Labeled Statements */
15097
 
15098
/* Action taken when a labeled statement is parsed. a new
15099
   LABELED_BLOCK_EXPR is created. No statement is attached to the
15100
   label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15101
 
15102
static tree
15103
build_labeled_block (int location, tree label)
15104
{
15105
  tree label_name ;
15106
  tree label_decl, node;
15107
  if (label == NULL_TREE || label == continue_identifier_node)
15108
    label_name = label;
15109
  else
15110
    {
15111
      label_name = merge_qualified_name (label_id, label);
15112
      /* Issue an error if we try to reuse a label that was previously
15113
         declared */
15114
      if (IDENTIFIER_LOCAL_VALUE (label_name))
15115
        {
15116
          EXPR_WFL_LINECOL (wfl_operator) = location;
15117
          parse_error_context (wfl_operator,
15118
            "Declaration of %qs shadows a previous label declaration",
15119
                               IDENTIFIER_POINTER (label));
15120
          EXPR_WFL_LINECOL (wfl_operator) =
15121
            EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15122
          parse_error_context (wfl_operator,
15123
            "This is the location of the previous declaration of label %qs",
15124
                               IDENTIFIER_POINTER (label));
15125
          java_error_count--;
15126
        }
15127
    }
15128
 
15129
  label_decl = create_label_decl (label_name);
15130
  node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15131
  EXPR_WFL_LINECOL (node) = location;
15132
  TREE_SIDE_EFFECTS (node) = 1;
15133
  return node;
15134
}
15135
 
15136
/* A labeled statement LBE is attached a statement.  */
15137
 
15138
static tree
15139
finish_labeled_statement (tree lbe, /* Labeled block expr */
15140
                          tree statement)
15141
{
15142
  /* In anyways, tie the loop to its statement */
15143
  LABELED_BLOCK_BODY (lbe) = statement;
15144
  pop_labeled_block ();
15145
  POP_LABELED_BLOCK ();
15146
  return lbe;
15147
}
15148
 
15149
/* 14.10, 14.11, 14.12 Loop Statements */
15150
 
15151
/* Create an empty LOOP_EXPR and make it the last in the nested loop
15152
   list. */
15153
 
15154
static tree
15155
build_new_loop (tree loop_body)
15156
{
15157
  tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
15158
  TREE_SIDE_EFFECTS (loop) = 1;
15159
  PUSH_LOOP (loop);
15160
  return loop;
15161
}
15162
 
15163
/* Create a loop body according to the following structure:
15164
     COMPOUND_EXPR
15165
       COMPOUND_EXPR            (loop main body)
15166
         EXIT_EXPR              (this order is for while/for loops.
15167
         LABELED_BLOCK_EXPR      the order is reversed for do loops)
15168
           LABEL_DECL           (a continue occurring here branches at the
15169
           BODY                  end of this labeled block)
15170
       INCREMENT                (if any)
15171
 
15172
  REVERSED, if nonzero, tells that the loop condition expr comes
15173
  after the body, like in the do-while loop.
15174
 
15175
  To obtain a loop, the loop body structure described above is
15176
  encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15177
 
15178
   LABELED_BLOCK_EXPR
15179
     LABEL_DECL                   (use this label to exit the loop)
15180
     LOOP_EXPR
15181
        */
15182
 
15183
static tree
15184
build_loop_body (int location, tree condition, int reversed)
15185
{
15186
  tree first, second, body;
15187
 
15188
  condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15189
  EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15190
  condition = build_debugable_stmt (location, condition);
15191
  TREE_SIDE_EFFECTS (condition) = 1;
15192
 
15193
  body = build_labeled_block (0, continue_identifier_node);
15194
  first = (reversed ? body : condition);
15195
  second = (reversed ? condition : body);
15196
  return build2 (COMPOUND_EXPR, NULL_TREE,
15197
                 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15198
                 build_java_empty_stmt ());
15199
}
15200
 
15201
/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15202
   their order) on the current loop. Unlink the current loop from the
15203
   loop list.  */
15204
 
15205
static tree
15206
finish_loop_body (int location, tree condition, tree body, int reversed)
15207
{
15208
  tree to_return = ctxp->current_loop;
15209
  tree loop_body = LOOP_EXPR_BODY (to_return);
15210
  if (condition)
15211
    {
15212
      tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15213
      /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15214
         The real EXIT_EXPR is one operand further. */
15215
      EXPR_WFL_LINECOL (cnode) = location;
15216
      if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15217
        {
15218
          cnode = EXPR_WFL_NODE (cnode);
15219
          /* This one is for accurate error reports */
15220
          EXPR_WFL_LINECOL (cnode) = location;
15221
        }
15222
      TREE_OPERAND (cnode, 0) = condition;
15223
    }
15224
  LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15225
  POP_LOOP ();
15226
  return to_return;
15227
}
15228
 
15229
/* Tailored version of finish_loop_body for FOR loops, when FOR
15230
   loops feature the condition part */
15231
 
15232
static tree
15233
finish_for_loop (int location, tree condition, tree update, tree body)
15234
{
15235
  /* Put the condition and the loop body in place */
15236
  tree loop = finish_loop_body (location, condition, body, 0);
15237
  /* LOOP is the current loop which has been now popped of the loop
15238
     stack.  Mark the update block as reachable and install it.  We do
15239
     this because the (current interpretation of the) JLS requires
15240
     that the update expression be considered reachable even if the
15241
     for loop's body doesn't complete normally.  */
15242
  if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15243
    {
15244
      tree up2 = update;
15245
      if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15246
        up2 = EXPR_WFL_NODE (up2);
15247
      /* It is possible for the update expression to be an
15248
         EXPR_WFL_NODE wrapping nothing.  */
15249
      if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15250
        {
15251
          /* Try to detect constraint violations.  These would be
15252
             programming errors somewhere.  */
15253
          if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15254
            abort ();
15255
          SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15256
        }
15257
    }
15258
  LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15259
  return loop;
15260
}
15261
 
15262
/* Try to find the loop a block might be related to. This comprises
15263
   the case where the LOOP_EXPR is found as the second operand of a
15264
   COMPOUND_EXPR, because the loop happens to have an initialization
15265
   part, then expressed as the first operand of the COMPOUND_EXPR. If
15266
   the search finds something, 1 is returned. Otherwise, 0 is
15267
   returned. The search is assumed to start from a
15268
   LABELED_BLOCK_EXPR's block.  */
15269
 
15270
static tree
15271
search_loop (tree statement)
15272
{
15273
  if (TREE_CODE (statement) == LOOP_EXPR)
15274
    return statement;
15275
 
15276
  if (TREE_CODE (statement) == BLOCK)
15277
    statement = BLOCK_SUBBLOCKS (statement);
15278
  else
15279
    return NULL_TREE;
15280
 
15281
  if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15282
    while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15283
      statement = TREE_OPERAND (statement, 1);
15284
 
15285
  return (TREE_CODE (statement) == LOOP_EXPR
15286
          && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15287
}
15288
 
15289
/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15290
   returned otherwise.  */
15291
 
15292
static int
15293
labeled_block_contains_loop_p (tree block, tree loop)
15294
{
15295
  if (!block)
15296
    return 0;
15297
 
15298
  if (LABELED_BLOCK_BODY (block) == loop)
15299
    return 1;
15300
 
15301
  if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15302
    return 1;
15303
 
15304
  return 0;
15305
}
15306
 
15307
/* If the loop isn't surrounded by a labeled statement, create one and
15308
   insert LOOP as its body.  */
15309
 
15310
static tree
15311
patch_loop_statement (tree loop)
15312
{
15313
  tree loop_label;
15314
 
15315
  TREE_TYPE (loop) = void_type_node;
15316
  if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15317
    return loop;
15318
 
15319
  loop_label = build_labeled_block (0, NULL_TREE);
15320
  /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15321
     that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15322
  LABELED_BLOCK_BODY (loop_label) = loop;
15323
  PUSH_LABELED_BLOCK (loop_label);
15324
  return loop_label;
15325
}
15326
 
15327
/* 14.13, 14.14: break and continue Statements */
15328
 
15329
/* Build a break or a continue statement. a null NAME indicates an
15330
   unlabeled break/continue statement.  */
15331
 
15332
static tree
15333
build_bc_statement (int location, int is_break, tree name)
15334
{
15335
  tree break_continue, label_block_expr = NULL_TREE;
15336
 
15337
  if (name)
15338
    {
15339
      if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15340
            (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15341
        /* Null means that we don't have a target for this named
15342
           break/continue. In this case, we make the target to be the
15343
           label name, so that the error can be reported accurately in
15344
           patch_bc_statement. */
15345
        label_block_expr = EXPR_WFL_NODE (name);
15346
    }
15347
  /* Unlabeled break/continue will be handled during the
15348
     break/continue patch operation */
15349
  break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15350
 
15351
  IS_BREAK_STMT_P (break_continue) = is_break;
15352
  TREE_SIDE_EFFECTS (break_continue) = 1;
15353
  EXPR_WFL_LINECOL (break_continue) = location;
15354
  break_continue = build_debugable_stmt (location, break_continue);
15355
  return break_continue;
15356
}
15357
 
15358
/* Verification of a break/continue statement. */
15359
 
15360
static tree
15361
patch_bc_statement (tree node)
15362
{
15363
  tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15364
  tree labeled_block = ctxp->current_labeled_block;
15365
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15366
 
15367
  /* Having an identifier here means that the target is unknown. */
15368
  if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15369
    {
15370
      parse_error_context (wfl_operator, "No label definition found for %qs",
15371
                           IDENTIFIER_POINTER (bc_label));
15372
      return error_mark_node;
15373
    }
15374
  if (! IS_BREAK_STMT_P (node))
15375
    {
15376
      /* It's a continue statement. */
15377
      for (;; labeled_block = TREE_CHAIN (labeled_block))
15378
        {
15379
          if (labeled_block == NULL_TREE)
15380
            {
15381
              if (bc_label == NULL_TREE)
15382
                parse_error_context (wfl_operator,
15383
                                     "% must be in loop");
15384
              else
15385
                parse_error_context
15386
                  (wfl_operator, "continue label %qs does not name a loop",
15387
                   IDENTIFIER_POINTER (bc_label));
15388
              return error_mark_node;
15389
            }
15390
          if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15391
               == continue_identifier_node)
15392
              && (bc_label == NULL_TREE
15393
                  || TREE_CHAIN (labeled_block) == bc_label))
15394
            {
15395
              bc_label = labeled_block;
15396
              break;
15397
            }
15398
        }
15399
    }
15400
  else if (!bc_label)
15401
    {
15402
      for (;; labeled_block = TREE_CHAIN (labeled_block))
15403
        {
15404
          if (labeled_block == NULL_TREE)
15405
            {
15406
              parse_error_context (wfl_operator,
15407
                                     "% must be in loop or switch");
15408
              return error_mark_node;
15409
            }
15410
          target_stmt = LABELED_BLOCK_BODY (labeled_block);
15411
          if (TREE_CODE (target_stmt) == SWITCH_EXPR
15412
              || search_loop (target_stmt))
15413
            {
15414
              bc_label = labeled_block;
15415
              break;
15416
            }
15417
        }
15418
    }
15419
 
15420
  EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15421
  CAN_COMPLETE_NORMALLY (bc_label) = 1;
15422
 
15423
  /* Our break/continue don't return values. */
15424
  TREE_TYPE (node) = void_type_node;
15425
  /* Encapsulate the break within a compound statement so that it's
15426
     expanded all the times by expand_expr (and not clobbered
15427
     sometimes, like after a if statement) */
15428
  node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15429
  TREE_SIDE_EFFECTS (node) = 1;
15430
  return node;
15431
}
15432
 
15433
/* Process the exit expression belonging to a loop. Its type must be
15434
   boolean.  */
15435
 
15436
static tree
15437
patch_exit_expr (tree node)
15438
{
15439
  tree expression = TREE_OPERAND (node, 0);
15440
  TREE_TYPE (node) = error_mark_node;
15441
  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15442
 
15443
  /* The type of expression must be boolean */
15444
  if (TREE_TYPE (expression) != boolean_type_node)
15445
    {
15446
      parse_error_context
15447
        (wfl_operator,
15448
    "Incompatible type for loop conditional. Can't convert %qs to %",
15449
         lang_printable_name (TREE_TYPE (expression), 0));
15450
      return error_mark_node;
15451
    }
15452
  /* Now we know things are allright, invert the condition, fold and
15453
     return */
15454
  TREE_OPERAND (node, 0) =
15455
    fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, expression);
15456
 
15457
  if (! integer_zerop (TREE_OPERAND (node, 0))
15458
      && ctxp->current_loop != NULL_TREE
15459
      && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15460
    CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15461
  if (! integer_onep (TREE_OPERAND (node, 0)))
15462
    CAN_COMPLETE_NORMALLY (node) = 1;
15463
 
15464
 
15465
  TREE_TYPE (node) = void_type_node;
15466
  return node;
15467
}
15468
 
15469
/* 14.9 Switch statement */
15470
 
15471
static tree
15472
patch_switch_statement (tree node)
15473
{
15474
  tree se = TREE_OPERAND (node, 0), se_type;
15475
  tree save, iter;
15476
 
15477
  /* Complete the switch expression */
15478
  se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15479
  se_type = TREE_TYPE (se);
15480
  /* The type of the switch expression must be char, byte, short or
15481
     int */
15482
  if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15483
    {
15484
      EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15485
      parse_error_context (wfl_operator,
15486
          "Incompatible type for %. Can't convert %qs to %",
15487
                           lang_printable_name (se_type, 0));
15488
      /* This is what java_complete_tree will check */
15489
      TREE_OPERAND (node, 0) = error_mark_node;
15490
      return error_mark_node;
15491
    }
15492
 
15493
  /* Save and restore the outer case label list.  */
15494
  save = case_label_list;
15495
  case_label_list = NULL_TREE;
15496
 
15497
  TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15498
 
15499
  /* See if we've found a duplicate label.  We can't leave this until
15500
     code generation, because in `--syntax-only' and `-C' modes we
15501
     don't do ordinary code generation.  */
15502
  for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15503
    {
15504
      HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15505
      tree subiter;
15506
      for (subiter = TREE_CHAIN (iter);
15507
           subiter != NULL_TREE;
15508
           subiter = TREE_CHAIN (subiter))
15509
        {
15510
          HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15511
          if (val == subval)
15512
            {
15513
              EXPR_WFL_LINECOL (wfl_operator)
15514
                = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15515
              /* The case_label_list is in reverse order, so print the
15516
                 outer label first.  */
15517
              parse_error_context (wfl_operator, "duplicate case label: %<"
15518
                                   HOST_WIDE_INT_PRINT_DEC "%>", subval);
15519
              EXPR_WFL_LINECOL (wfl_operator)
15520
                = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15521
              parse_error_context (wfl_operator, "original label is here");
15522
 
15523
              break;
15524
            }
15525
        }
15526
    }
15527
 
15528
  case_label_list = save;
15529
 
15530
  /* Ready to return */
15531
  if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15532
    {
15533
      TREE_TYPE (node) = error_mark_node;
15534
      return error_mark_node;
15535
    }
15536
  TREE_TYPE (node) = void_type_node;
15537
  TREE_SIDE_EFFECTS (node) = 1;
15538
  CAN_COMPLETE_NORMALLY (node)
15539
    = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15540
      || ! SWITCH_HAS_DEFAULT (node);
15541
  return node;
15542
}
15543
 
15544
/* Assertions.  */
15545
 
15546
/* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15547
   might be NULL_TREE.  */
15548
static tree
15549
build_assertion (
15550
#ifdef USE_MAPPED_LOCATION
15551
                 source_location location,
15552
#else
15553
                 int location,
15554
#endif
15555
                 tree condition, tree value)
15556
{
15557
  tree node;
15558
  tree klass = GET_CPC ();
15559
 
15560
  if (! enable_assertions (klass))
15561
    {
15562
      condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15563
                          boolean_false_node, condition);
15564
      if (value == NULL_TREE)
15565
        value = build_java_empty_stmt ();
15566
      return build_if_else_statement (location, condition,
15567
                                      value, NULL_TREE);
15568
    }
15569
 
15570
  if (! CLASS_USES_ASSERTIONS (klass))
15571
    {
15572
      tree field, classdollar, id, call;
15573
      tree class_type = TREE_TYPE (klass);
15574
 
15575
      field = add_field (class_type,
15576
                         get_identifier ("$assertionsDisabled"),
15577
                         boolean_type_node,
15578
                         ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15579
      MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15580
      FIELD_SYNTHETIC (field) = 1;
15581
 
15582
      classdollar = build_incomplete_class_ref (location, class_type);
15583
 
15584
      /* Call CLASS.desiredAssertionStatus().  */
15585
      id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15586
      call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15587
      call = make_qualified_primary (classdollar, call, location);
15588
      TREE_SIDE_EFFECTS (call) = 1;
15589
 
15590
      /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15591
         seem odd, but we do it to generate code identical to that of
15592
         the JDK.  */
15593
      call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15594
      TREE_SIDE_EFFECTS (call) = 1;
15595
      DECL_INITIAL (field) = call;
15596
 
15597
      /* Record the initializer in the initializer statement list.  */
15598
      call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15599
      TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15600
      SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15601
      MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15602
 
15603
      CLASS_USES_ASSERTIONS (klass) = 1;
15604
    }
15605
 
15606
  if (value != NULL_TREE)
15607
    value = tree_cons (NULL_TREE, value, NULL_TREE);
15608
 
15609
  node = build_wfl_node (get_identifier ("java"));
15610
  node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15611
                              location);
15612
  node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15613
                              location);
15614
 
15615
  node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15616
  TREE_SIDE_EFFECTS (node) = 1;
15617
  /* It is too early to use BUILD_THROW.  */
15618
  node = build1 (THROW_EXPR, NULL_TREE, node);
15619
  TREE_SIDE_EFFECTS (node) = 1;
15620
 
15621
  /* We invert the condition; if we just put NODE as the `else' part
15622
     then we generate weird-looking bytecode.  */
15623
  condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15624
  /* Check $assertionsDisabled.  */
15625
  condition
15626
    = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15627
              build1 (TRUTH_NOT_EXPR, NULL_TREE,
15628
                      build_wfl_node (get_identifier ("$assertionsDisabled"))),
15629
              condition);
15630
  node = build_if_else_statement (location, condition, node, NULL_TREE);
15631
  return node;
15632
}
15633
 
15634
/* 14.18 The try/catch statements */
15635
 
15636
/* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15637
   catches TYPE and executes CATCH_STMTS.  */
15638
 
15639
static tree
15640
encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15641
                            tree catch_stmts)
15642
{
15643
  tree try_block, catch_clause_param, catch_block, catch;
15644
 
15645
  /* First build a try block */
15646
  try_block = build_expr_block (try_stmts, NULL_TREE);
15647
 
15648
  /* Build a catch block: we need a catch clause parameter */
15649
  if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15650
    {
15651
      tree catch_type = obtain_incomplete_type (type_or_name);
15652
      jdep *dep;
15653
      catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15654
      register_incomplete_type (JDEP_VARIABLE, type_or_name,
15655
                                catch_clause_param, catch_type);
15656
      dep = CLASSD_LAST (ctxp->classd_list);
15657
      JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15658
    }
15659
  else
15660
    catch_clause_param = build_decl (VAR_DECL, wpv_id,
15661
                                     build_pointer_type (type_or_name));
15662
 
15663
  /* And a block */
15664
  catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15665
 
15666
  /* Initialize the variable and store in the block */
15667
  catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15668
                  build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15669
  add_stmt_to_block (catch_block, NULL_TREE, catch);
15670
 
15671
  /* Add the catch statements */
15672
  add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15673
 
15674
  /* Now we can build a JAVA_CATCH_EXPR */
15675
  catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15676
 
15677
  return build_try_statement (location, try_block, catch_block);
15678
}
15679
 
15680
static tree
15681
build_try_statement (int location, tree try_block, tree catches)
15682
{
15683
  tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15684
  EXPR_WFL_LINECOL (node) = location;
15685
  return node;
15686
}
15687
 
15688
static tree
15689
build_try_finally_statement (int location, tree try_block, tree finally)
15690
{
15691
  tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15692
  EXPR_WFL_LINECOL (node) = location;
15693
  return node;
15694
}
15695
 
15696
static tree
15697
patch_try_statement (tree node)
15698
{
15699
  int error_found = 0;
15700
  tree try = TREE_OPERAND (node, 0);
15701
  /* Exception handlers are considered in left to right order */
15702
  tree catch = nreverse (TREE_OPERAND (node, 1));
15703
  tree current, caught_type_list = NULL_TREE;
15704
 
15705
  /* Check catch clauses, if any. Every time we find an error, we try
15706
     to process the next catch clause. We process the catch clause before
15707
     the try block so that when processing the try block we can check thrown
15708
     exceptions against the caught type list. */
15709
  for (current = catch; current; current = TREE_CHAIN (current))
15710
    {
15711
      tree carg_decl, carg_type;
15712
      tree sub_current, catch_block, catch_clause;
15713
      int unreachable;
15714
 
15715
      /* At this point, the structure of the catch clause is
15716
           JAVA_CATCH_EXPR              (catch node)
15717
             BLOCK              (with the decl of the parameter)
15718
               COMPOUND_EXPR
15719
                 MODIFY_EXPR   (assignment of the catch parameter)
15720
                 BLOCK          (catch clause block)
15721
       */
15722
      catch_clause = TREE_OPERAND (current, 0);
15723
      carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15724
      carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15725
 
15726
      /* Catch clauses can't have more than one parameter declared,
15727
         but it's already enforced by the grammar. Make sure that the
15728
         only parameter of the clause statement in of class Throwable
15729
         or a subclass of Throwable, but that was done earlier. The
15730
         catch clause parameter type has also been resolved. */
15731
 
15732
      /* Just make sure that the catch clause parameter type inherits
15733
         from java.lang.Throwable */
15734
      if (!inherits_from_p (carg_type, throwable_type_node))
15735
        {
15736
          EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15737
          parse_error_context (wfl_operator,
15738
                               "Can't catch class %qs. Catch clause parameter type must be a subclass of class %",
15739
                               lang_printable_name (carg_type, 0));
15740
          error_found = 1;
15741
          continue;
15742
        }
15743
 
15744
      /* Partial check for unreachable catch statement: The catch
15745
         clause is reachable iff is no earlier catch block A in
15746
         the try statement such that the type of the catch
15747
         clause's parameter is the same as or a subclass of the
15748
         type of A's parameter */
15749
      unreachable = 0;
15750
      for (sub_current = catch;
15751
           sub_current != current; sub_current = TREE_CHAIN (sub_current))
15752
        {
15753
          tree sub_catch_clause, decl;
15754
          sub_catch_clause = TREE_OPERAND (sub_current, 0);
15755
          decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15756
 
15757
          if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15758
            {
15759
              EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15760
              parse_error_context
15761
                (wfl_operator,
15762
                 "% not reached because of the catch clause at line %d",
15763
                 EXPR_WFL_LINENO (sub_current));
15764
              unreachable = error_found = 1;
15765
              break;
15766
            }
15767
        }
15768
      /* Complete the catch clause block */
15769
      catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15770
      if (catch_block == error_mark_node)
15771
        {
15772
          error_found = 1;
15773
          continue;
15774
        }
15775
      if (CAN_COMPLETE_NORMALLY (catch_block))
15776
        CAN_COMPLETE_NORMALLY (node) = 1;
15777
      TREE_OPERAND (current, 0) = catch_block;
15778
 
15779
      if (unreachable)
15780
        continue;
15781
 
15782
      /* Things to do here: the exception must be thrown */
15783
 
15784
      /* Link this type to the caught type list */
15785
      caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15786
    }
15787
 
15788
  PUSH_EXCEPTIONS (caught_type_list);
15789
  if ((try = java_complete_tree (try)) == error_mark_node)
15790
    error_found = 1;
15791
  if (CAN_COMPLETE_NORMALLY (try))
15792
    CAN_COMPLETE_NORMALLY (node) = 1;
15793
  POP_EXCEPTIONS ();
15794
 
15795
  /* Verification ends here */
15796
  if (error_found)
15797
    return error_mark_node;
15798
 
15799
  TREE_OPERAND (node, 0) = try;
15800
  TREE_OPERAND (node, 1) = catch;
15801
  TREE_TYPE (node) = void_type_node;
15802
  return node;
15803
}
15804
 
15805
/* 14.17 The synchronized Statement */
15806
 
15807
static tree
15808
patch_synchronized_statement (tree node, tree wfl_op1)
15809
{
15810
  tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15811
  tree block = TREE_OPERAND (node, 1);
15812
 
15813
  tree tmp, enter, exit, expr_decl, assignment;
15814
 
15815
  if (expr == error_mark_node)
15816
    {
15817
      block = java_complete_tree (block);
15818
      return expr;
15819
    }
15820
 
15821
  /* We might be trying to synchronize on a STRING_CST */
15822
  if ((tmp = patch_string (expr)))
15823
    expr = tmp;
15824
 
15825
  /* The TYPE of expr must be a reference type */
15826
  if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15827
    {
15828
      SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15829
      parse_error_context (wfl_operator, "Incompatible type for %. Can't convert %qs to %",
15830
                           lang_printable_name (TREE_TYPE (expr), 0));
15831
      return error_mark_node;
15832
    }
15833
 
15834
  /* Generate a try-finally for the synchronized statement, except
15835
     that the handler that catches all throw exception calls
15836
     _Jv_MonitorExit and then rethrow the exception.
15837
     The synchronized statement is then implemented as:
15838
     TRY
15839
       {
15840
         _Jv_MonitorEnter (expression)
15841
         synchronized_block
15842
         _Jv_MonitorExit (expression)
15843
       }
15844
     CATCH_ALL
15845
       {
15846
         e = _Jv_exception_info ();
15847
         _Jv_MonitorExit (expression)
15848
         Throw (e);
15849
       } */
15850
 
15851
  expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15852
  BUILD_MONITOR_ENTER (enter, expr_decl);
15853
  BUILD_MONITOR_EXIT (exit, expr_decl);
15854
  CAN_COMPLETE_NORMALLY (enter) = 1;
15855
  CAN_COMPLETE_NORMALLY (exit) = 1;
15856
  assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15857
  TREE_SIDE_EFFECTS (assignment) = 1;
15858
  node = build2 (COMPOUND_EXPR, NULL_TREE,
15859
                 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15860
                 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15861
  node = build_expr_block (node, expr_decl);
15862
 
15863
  return java_complete_tree (node);
15864
}
15865
 
15866
/* 14.16 The throw Statement */
15867
 
15868
static tree
15869
patch_throw_statement (tree node, tree wfl_op1)
15870
{
15871
  tree expr = TREE_OPERAND (node, 0);
15872
  tree type = TREE_TYPE (expr);
15873
  int unchecked_ok = 0, tryblock_throws_ok = 0;
15874
 
15875
  /* Thrown expression must be assignable to java.lang.Throwable */
15876
  if (!try_reference_assignconv (throwable_type_node, expr))
15877
    {
15878
      SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15879
      parse_error_context (wfl_operator,
15880
    "Can't throw %qs; it must be a subclass of class %",
15881
                           lang_printable_name (type, 0));
15882
      /* If the thrown expression was a reference, we further the
15883
         compile-time check. */
15884
      if (!JREFERENCE_TYPE_P (type))
15885
        return error_mark_node;
15886
    }
15887
 
15888
  /* At least one of the following must be true */
15889
 
15890
  /* The type of the throw expression is a not checked exception,
15891
     i.e. is a unchecked expression. */
15892
  unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15893
 
15894
  SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15895
  /* An instance can't throw a checked exception unless that exception
15896
     is explicitly declared in the `throws' clause of each
15897
     constructor. This doesn't apply to anonymous classes, since they
15898
     don't have declared constructors. */
15899
  if (!unchecked_ok
15900
      && DECL_INSTINIT_P (current_function_decl)
15901
      && !ANONYMOUS_CLASS_P (current_class))
15902
    {
15903
      tree current;
15904
      for (current = TYPE_METHODS (current_class); current;
15905
           current = TREE_CHAIN (current))
15906
        if (DECL_CONSTRUCTOR_P (current)
15907
            && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15908
          {
15909
            parse_error_context (wfl_operator, "Checked exception %qs can't be thrown in instance initializer (not all declared constructor are declaring it in their % clause)",
15910
                                 lang_printable_name (TREE_TYPE (expr), 0));
15911
            return error_mark_node;
15912
          }
15913
    }
15914
 
15915
  /* Throw is contained in a try statement and at least one catch
15916
     clause can receive the thrown expression or the current method is
15917
     declared to throw such an exception. Or, the throw statement is
15918
     contained in a method or constructor declaration and the type of
15919
     the Expression is assignable to at least one type listed in the
15920
     throws clause the declaration. */
15921
  if (!unchecked_ok)
15922
    tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15923
  if (!(unchecked_ok || tryblock_throws_ok))
15924
    {
15925
      /* If there is a surrounding try block that has no matching
15926
         clatch clause, report it first. A surrounding try block exits
15927
         only if there is something after the list of checked
15928
         exception thrown by the current function (if any). */
15929
      if (IN_TRY_BLOCK_P ())
15930
        parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding % block",
15931
                             lang_printable_name (type, 0));
15932
      /* If we have no surrounding try statement and the method doesn't have
15933
         any throws, report it now. FIXME */
15934
 
15935
      /* We report that the exception can't be throw from a try block
15936
         in all circumstances but when the `throw' is inside a static
15937
         block. */
15938
      else if (!EXCEPTIONS_P (currently_caught_type_list)
15939
               && !tryblock_throws_ok)
15940
        {
15941
          if (DECL_CLINIT_P (current_function_decl))
15942
            parse_error_context (wfl_operator,
15943
                   "Checked exception %qs can't be thrown in initializer",
15944
                                 lang_printable_name (type, 0));
15945
          else
15946
            parse_error_context (wfl_operator,
15947
                   "Checked exception %qs isn't thrown from a % block",
15948
                                 lang_printable_name (type, 0));
15949
        }
15950
      /* Otherwise, the current method doesn't have the appropriate
15951
         throws declaration */
15952
      else
15953
        parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's % declaration(s)",
15954
                             lang_printable_name (type, 0));
15955
      return error_mark_node;
15956
    }
15957
 
15958
  if (! flag_emit_class_files)
15959
    BUILD_THROW (node, expr);
15960
 
15961
  return node;
15962
}
15963
 
15964
/* Add EXCEPTION to the throws clause of MDECL.  If MDECL already throws
15965
   a super-class of EXCEPTION, keep the superclass instead.  If MDECL already
15966
   throws a sub-class of EXCEPTION, replace the sub-class with EXCEPTION.  */
15967
static void
15968
add_exception_to_throws (tree mdecl, tree exception)
15969
{
15970
  tree mthrows;
15971
 
15972
  /* Ignore unchecked exceptions. */
15973
  if (IS_UNCHECKED_EXCEPTION_P (exception))
15974
    return;
15975
 
15976
  for (mthrows = DECL_FUNCTION_THROWS (mdecl);
15977
       mthrows; mthrows = TREE_CHAIN (mthrows))
15978
    {
15979
      if (inherits_from_p (exception, TREE_VALUE (mthrows)))
15980
        return;
15981
      if (inherits_from_p (TREE_VALUE (mthrows), exception))
15982
        {
15983
          TREE_VALUE (mthrows) = exception;
15984
          return;
15985
        }
15986
    }
15987
 
15988
  mthrows = DECL_FUNCTION_THROWS (mdecl);
15989
  DECL_FUNCTION_THROWS (mdecl) = build_tree_list (mthrows, exception);
15990
}
15991
 
15992
/* Check that exception said to be thrown by method DECL can be
15993
   effectively caught from where DECL is invoked.  THIS_EXPR is the
15994
   expression that computes `this' for the method call.  */
15995
static void
15996
check_thrown_exceptions (
15997
#ifdef USE_MAPPED_LOCATION
15998
                         source_location location,
15999
#else
16000
 
16001
                         int location,
16002
#endif
16003
                         tree decl, tree this_expr)
16004
{
16005
  tree throws;
16006
  int is_array_call = 0;
16007
 
16008
  /* Skip check within generated methods, such as access$.  */
16009
  if (NESTED_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
16010
    return;
16011
 
16012
  if (this_expr != NULL_TREE
16013
      && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
16014
      && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
16015
    is_array_call = 1;
16016
 
16017
  /* For all the unchecked exceptions thrown by DECL.  */
16018
  for (throws = DECL_FUNCTION_THROWS (decl); throws;
16019
       throws = TREE_CHAIN (throws))
16020
    if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
16021
      {
16022
        /* Suppress errors about cloning arrays.  */
16023
        if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
16024
          continue;
16025
 
16026
#ifdef USE_MAPPED_LOCATION
16027
        SET_EXPR_LOCATION (wfl_operator, location);
16028
#else
16029
        EXPR_WFL_LINECOL (wfl_operator) = location;
16030
#endif
16031
        if (ANONYMOUS_CLASS_P (DECL_CONTEXT (current_function_decl))
16032
            && (DECL_FINIT_P (current_function_decl)
16033
                || DECL_INIT_P (current_function_decl)
16034
                || DECL_CONSTRUCTOR_P (current_function_decl)))
16035
          {
16036
            /* Add "throws" to the initializer's exception list */
16037
            tree exception = TREE_VALUE (throws);
16038
            add_exception_to_throws (current_function_decl, exception);
16039
          }
16040
        else if (DECL_FINIT_P (current_function_decl))
16041
          {
16042
            parse_error_context
16043
              (wfl_operator, "Exception %qs can't be thrown in initializer",
16044
               lang_printable_name (TREE_VALUE (throws), 0));
16045
          }
16046
        else
16047
          {
16048
            parse_error_context
16049
              (wfl_operator, "Exception %qs must be caught, or it must be declared in the % clause of %qs",
16050
               lang_printable_name (TREE_VALUE (throws), 0),
16051
               (DECL_INIT_P (current_function_decl) ?
16052
                IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
16053
                IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
16054
          }
16055
      }
16056
}
16057
 
16058
/* Return 1 if checked EXCEPTION is caught at the current nesting level of
16059
   try-catch blocks, OR is listed in the `throws' clause of the
16060
   current method.  */
16061
 
16062
static int
16063
check_thrown_exceptions_do (tree exception)
16064
{
16065
  tree list = currently_caught_type_list;
16066
  resolve_and_layout (exception, NULL_TREE);
16067
  /* First, all the nested try-catch-finally at that stage. The
16068
     last element contains `throws' clause exceptions, if any. */
16069
  if (IS_UNCHECKED_EXCEPTION_P (exception))
16070
    return 1;
16071
  while (list)
16072
    {
16073
      tree caught;
16074
      for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
16075
        if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
16076
          return 1;
16077
      list = TREE_CHAIN (list);
16078
    }
16079
  return 0;
16080
}
16081
 
16082
/* This function goes over all of CLASS_TYPE ctors and checks whether
16083
   each of them features at least one unchecked exception in its
16084
   `throws' clause. If it's the case, it returns `true', `false'
16085
   otherwise.  */
16086
 
16087
static bool
16088
ctors_unchecked_throws_clause_p (tree class_type)
16089
{
16090
  tree current;
16091
 
16092
  for (current = TYPE_METHODS (class_type); current;
16093
       current = TREE_CHAIN (current))
16094
    {
16095
      bool ctu = false; /* Ctor Throws Unchecked */
16096
      if (DECL_CONSTRUCTOR_P (current))
16097
        {
16098
          tree throws;
16099
          for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16100
               throws = TREE_CHAIN (throws))
16101
            if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16102
              ctu = true;
16103
        }
16104
      /* We return false as we found one ctor that is unfit. */
16105
      if (!ctu && DECL_CONSTRUCTOR_P (current))
16106
        return false;
16107
    }
16108
  /* All ctors feature at least one unchecked exception in their
16109
     `throws' clause. */
16110
  return true;
16111
}
16112
 
16113
/* 15.24 Conditional Operator ?: */
16114
 
16115
static tree
16116
patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
16117
{
16118
  tree cond = TREE_OPERAND (node, 0);
16119
  tree op1 = TREE_OPERAND (node, 1);
16120
  tree op2 = TREE_OPERAND (node, 2);
16121
  tree resulting_type = NULL_TREE;
16122
  tree t1, t2, patched;
16123
  int error_found = 0;
16124
 
16125
  /* The condition and operands of ?: might be StringBuffers crafted
16126
     as a result of a string concatenation.  Obtain decent ones here.  */
16127
  if ((patched = patch_string (cond)))
16128
    TREE_OPERAND (node, 0) = cond = patched;
16129
  if ((patched = patch_string (op1)))
16130
    TREE_OPERAND (node, 1) = op1 = patched;
16131
  if ((patched = patch_string (op2)))
16132
    TREE_OPERAND (node, 2) = op2 = patched;
16133
 
16134
  t1 = TREE_TYPE (op1);
16135
  t2 = TREE_TYPE (op2);
16136
 
16137
  /* The first expression must be a boolean */
16138
  if (TREE_TYPE (cond) != boolean_type_node)
16139
    {
16140
      SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16141
      parse_error_context (wfl_operator,
16142
               "Incompatible type for %. Can't convert %qs to %",
16143
                           lang_printable_name (TREE_TYPE (cond), 0));
16144
      error_found = 1;
16145
    }
16146
 
16147
  /* Second and third can be numeric, boolean (i.e. primitive),
16148
     references or null. Anything else results in an error */
16149
  if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16150
        || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16151
            && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16152
        || (t1 == boolean_type_node && t2 == boolean_type_node)))
16153
    error_found = 1;
16154
 
16155
  /* Determine the type of the conditional expression. Same types are
16156
     easy to deal with */
16157
  else if (t1 == t2)
16158
    resulting_type = t1;
16159
 
16160
  /* There are different rules for numeric types */
16161
  else if (JNUMERIC_TYPE_P (t1))
16162
    {
16163
      /* if byte/short found, the resulting type is short */
16164
      if ((t1 == byte_type_node && t2 == short_type_node)
16165
          || (t1 == short_type_node && t2 == byte_type_node))
16166
        resulting_type = short_type_node;
16167
 
16168
      /* If t1 is a constant int and t2 is of type byte, short or char
16169
         and t1's value fits in t2, then the resulting type is t2 */
16170
      else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16171
          && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16172
        resulting_type = t2;
16173
 
16174
      /* If t2 is a constant int and t1 is of type byte, short or char
16175
         and t2's value fits in t1, then the resulting type is t1 */
16176
      else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16177
          && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16178
        resulting_type = t1;
16179
 
16180
      /* Otherwise, binary numeric promotion is applied and the
16181
         resulting type is the promoted type of operand 1 and 2 */
16182
      else
16183
        resulting_type = binary_numeric_promotion (t1, t2,
16184
                                                   &TREE_OPERAND (node, 1),
16185
                                                   &TREE_OPERAND (node, 2));
16186
    }
16187
 
16188
  /* Cases of a reference and a null type */
16189
  else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16190
    resulting_type = t1;
16191
 
16192
  else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16193
    resulting_type = t2;
16194
 
16195
  /* Last case: different reference types. If a type can be converted
16196
     into the other one by assignment conversion, the latter
16197
     determines the type of the expression */
16198
  else if ((resulting_type = try_reference_assignconv (t1, op2)))
16199
    resulting_type = promote_type (t1);
16200
 
16201
  else if ((resulting_type = try_reference_assignconv (t2, op1)))
16202
    resulting_type = promote_type (t2);
16203
 
16204
  /* If we don't have any resulting type, we're in trouble */
16205
  if (!resulting_type)
16206
    {
16207
      char *t = xstrdup (lang_printable_name (t1, 0));
16208
      SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16209
      parse_error_context (wfl_operator,
16210
                 "Incompatible type for %. Can't convert %qs to %qs",
16211
                           t, lang_printable_name (t2, 0));
16212
      free (t);
16213
      error_found = 1;
16214
    }
16215
 
16216
  if (error_found)
16217
    {
16218
      TREE_TYPE (node) = error_mark_node;
16219
      return error_mark_node;
16220
    }
16221
 
16222
  TREE_TYPE (node) = resulting_type;
16223
  TREE_SET_CODE (node, COND_EXPR);
16224
  CAN_COMPLETE_NORMALLY (node) = 1;
16225
  return node;
16226
}
16227
 
16228
/* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16229
 
16230
static tree
16231
maybe_build_class_init_for_field (tree decl, tree expr)
16232
{
16233
  tree clas = DECL_CONTEXT (decl);
16234
  if (flag_emit_class_files)
16235
    return expr;
16236
 
16237
  if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16238
      && FIELD_FINAL (decl))
16239
    {
16240
      tree init = DECL_INITIAL (decl);
16241
      if (init != NULL_TREE)
16242
        init = fold_constant_for_init (init, decl);
16243
      if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16244
        return expr;
16245
    }
16246
 
16247
  return build_class_init (clas, expr);
16248
}
16249
 
16250
/* Try to constant fold NODE.
16251
   If NODE is not a constant expression, return NULL_EXPR.
16252
   CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16253
 
16254
static tree
16255
fold_constant_for_init (tree node, tree context)
16256
{
16257
  tree op0, op1, val;
16258
  enum tree_code code = TREE_CODE (node);
16259
 
16260
  switch (code)
16261
    {
16262
    case INTEGER_CST:
16263
      if (node == null_pointer_node)
16264
        return NULL_TREE;
16265
    case STRING_CST:
16266
    case REAL_CST:
16267
      return node;
16268
 
16269
    case PLUS_EXPR:
16270
    case MINUS_EXPR:
16271
    case MULT_EXPR:
16272
    case TRUNC_MOD_EXPR:
16273
    case RDIV_EXPR:
16274
    case LSHIFT_EXPR:
16275
    case RSHIFT_EXPR:
16276
    case URSHIFT_EXPR:
16277
    case BIT_AND_EXPR:
16278
    case BIT_XOR_EXPR:
16279
    case BIT_IOR_EXPR:
16280
    case TRUTH_ANDIF_EXPR:
16281
    case TRUTH_ORIF_EXPR:
16282
    case EQ_EXPR:
16283
    case NE_EXPR:
16284
    case GT_EXPR:
16285
    case GE_EXPR:
16286
    case LT_EXPR:
16287
    case LE_EXPR:
16288
      op0 = TREE_OPERAND (node, 0);
16289
      op1 = TREE_OPERAND (node, 1);
16290
      val = fold_constant_for_init (op0, context);
16291
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16292
        return NULL_TREE;
16293
      TREE_OPERAND (node, 0) = val;
16294
      val = fold_constant_for_init (op1, context);
16295
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16296
        return NULL_TREE;
16297
      TREE_OPERAND (node, 1) = val;
16298
      return patch_binop (node, op0, op1, 1);
16299
 
16300
    case UNARY_PLUS_EXPR:
16301
    case NEGATE_EXPR:
16302
    case TRUTH_NOT_EXPR:
16303
    case BIT_NOT_EXPR:
16304
    case CONVERT_EXPR:
16305
    case NOP_EXPR:
16306
      op0 = TREE_OPERAND (node, 0);
16307
      val = fold_constant_for_init (op0, context);
16308
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16309
        return NULL_TREE;
16310
      TREE_OPERAND (node, 0) = val;
16311
      val = patch_unaryop (node, op0);
16312
      if (! TREE_CONSTANT (val))
16313
        return NULL_TREE;
16314
      return val;
16315
 
16316
      break;
16317
 
16318
    case COND_EXPR:
16319
      val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16320
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16321
        return NULL_TREE;
16322
      TREE_OPERAND (node, 0) = val;
16323
      val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16324
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16325
        return NULL_TREE;
16326
      TREE_OPERAND (node, 1) = val;
16327
      val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16328
      if (val == NULL_TREE || ! TREE_CONSTANT (val))
16329
        return NULL_TREE;
16330
      TREE_OPERAND (node, 2) = val;
16331
      return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 2)
16332
        : TREE_OPERAND (node, 1);
16333
 
16334
    case VAR_DECL:
16335
    case FIELD_DECL:
16336
      if (! FIELD_FINAL (node)
16337
          || DECL_INITIAL (node) == NULL_TREE)
16338
        return NULL_TREE;
16339
      val = DECL_INITIAL (node);
16340
      /* Guard against infinite recursion. */
16341
      DECL_INITIAL (node) = NULL_TREE;
16342
      val = fold_constant_for_init (val, node);
16343
      if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16344
        val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16345
      DECL_INITIAL (node) = val;
16346
      return val;
16347
 
16348
    case EXPR_WITH_FILE_LOCATION:
16349
      /* Compare java_complete_tree and resolve_expression_name. */
16350
      if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16351
          || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16352
        {
16353
          tree name = EXPR_WFL_NODE (node);
16354
          tree decl;
16355
          if (PRIMARY_P (node))
16356
            return NULL_TREE;
16357
          else if (! QUALIFIED_P (name))
16358
            {
16359
              decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16360
              if (decl == NULL_TREE
16361
                  || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16362
                return NULL_TREE;
16363
              return fold_constant_for_init (decl, decl);
16364
            }
16365
          else
16366
            {
16367
              tree r = NULL_TREE;
16368
              /* Install the proper context for the field resolution.  */
16369
              tree saved_current_class = current_class;
16370
              /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16371
              current_class = DECL_CONTEXT (context);
16372
              qualify_ambiguous_name (node);
16373
              r = resolve_field_access (node, &decl, NULL);
16374
              /* Restore prior context.  */
16375
              current_class = saved_current_class;
16376
              if (r != error_mark_node && decl != NULL_TREE)
16377
                return fold_constant_for_init (decl, decl);
16378
              return NULL_TREE;
16379
            }
16380
        }
16381
      else
16382
        {
16383
          op0 = TREE_OPERAND (node, 0);
16384
          val = fold_constant_for_init (op0, context);
16385
          if (val == NULL_TREE || ! TREE_CONSTANT (val))
16386
            return NULL_TREE;
16387
          TREE_OPERAND (node, 0) = val;
16388
          return val;
16389
        }
16390
 
16391
#ifdef USE_COMPONENT_REF
16392
    case IDENTIFIER:
16393
    case COMPONENT_REF:
16394
      ?;
16395
#endif
16396
 
16397
    default:
16398
      return NULL_TREE;
16399
    }
16400
}
16401
 
16402
#ifdef USE_COMPONENT_REF
16403
/* Context is 'T' for TypeName, 'P' for PackageName,
16404
   'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16405
 
16406
tree
16407
resolve_simple_name (tree name, int context)
16408
{
16409
}
16410
 
16411
tree
16412
resolve_qualified_name (tree name, int context)
16413
{
16414
}
16415
#endif
16416
 
16417
void
16418
init_src_parse (void)
16419
{
16420
  /* Sanity check; we've been bit by this before.  */
16421
  if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16422
    abort ();
16423
}
16424
 
16425
 
16426
 
16427
/* This section deals with the functions that are called when tables
16428
   recording class initialization information are traversed.  */
16429
 
16430
/* This function is called for each class that is known definitely
16431
   initialized when a given static method was called. This function
16432
   augments a compound expression (INFO) storing all assignment to
16433
   initialized static class flags if a flag already existed, otherwise
16434
   a new one is created.  */
16435
 
16436
static int
16437
emit_test_initialization (void **entry_p, void *info)
16438
{
16439
  tree l = (tree) info;
16440
  tree decl, init;
16441
  tree key = (tree) *entry_p;
16442
  tree *ite;
16443
  htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16444
 
16445
  /* If we haven't found a flag and we're dealing with self registered
16446
     with current_function_decl, then don't do anything. Self is
16447
     always added as definitely initialized but this information is
16448
     valid only if used outside the current function. */
16449
  if (current_function_decl == TREE_PURPOSE (l)
16450
      && java_treetreehash_find (cf_ht, key) == NULL)
16451
    return true;
16452
 
16453
  ite = java_treetreehash_new (cf_ht, key);
16454
 
16455
  /* If we don't have a variable, create one and install it. */
16456
  if (*ite == NULL)
16457
    {
16458
      tree block;
16459
 
16460
      decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16461
      MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16462
      LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16463
      DECL_CONTEXT (decl) = current_function_decl;
16464
      DECL_INITIAL (decl) = boolean_true_node;
16465
      /* Don't emit any symbolic debugging info for this decl.  */
16466
      DECL_IGNORED_P (decl) = 1;
16467
 
16468
      /* The trick is to find the right context for it. */
16469
      block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16470
      TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16471
      BLOCK_EXPR_DECLS (block) = decl;
16472
      *ite = decl;
16473
    }
16474
  else
16475
    decl = *ite;
16476
 
16477
  /* Now simply augment the compound that holds all the assignments
16478
     pertaining to this method invocation. */
16479
  init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16480
  TREE_SIDE_EFFECTS (init) = 1;
16481
  TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16482
  TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16483
 
16484
  return true;
16485
}
16486
 
16487
#ifdef __XGETTEXT__
16488
/* Depending on the version of Bison used to compile this grammar,
16489
   it may issue generic diagnostics spelled "syntax error" or
16490
   "parse error".  To prevent this from changing the translation
16491
   template randomly, we list all the variants of this particular
16492
   diagnostic here.  Translators: there is no fine distinction
16493
   between diagnostics with "syntax error" in them, and diagnostics
16494
   with "parse error" in them.  It's okay to give them both the same
16495
   translation.  */
16496
const char d1[] = N_("syntax error");
16497
const char d2[] = N_("parse error");
16498
const char d3[] = N_("syntax error; also virtual memory exhausted");
16499
const char d4[] = N_("parse error; also virtual memory exhausted");
16500
const char d5[] = N_("syntax error: cannot back up");
16501
const char d6[] = N_("parse error: cannot back up");
16502
#endif
16503
 
16504
#include "gt-java-parse.h"
16505
#include "gtype-java.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.