OSDN Git Service

v3: fix config.status instantiation of subdir makefiles.
[pf3gnuchains/gcc-fork.git] / gcc / java / decl.c
1 /* Process declarations and variables for the GNU compiler for the
2    Java(TM) language.
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007,
4    2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "real.h"
35 #include "toplev.h"
36 #include "flags.h"
37 #include "java-tree.h"
38 #include "jcf.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "libfuncs.h"
42 #include "except.h"
43 #include "java-except.h"
44 #include "ggc.h"
45 #include "timevar.h"
46 #include "cgraph.h"
47 #include "tree-inline.h"
48 #include "target.h"
49 #include "version.h"
50 #include "tree-iterator.h"
51 #include "langhooks.h"
52 #include "cgraph.h"
53
54 #if defined (DEBUG_JAVA_BINDING_LEVELS)
55 extern void indent (void);
56 #endif
57
58 static tree push_jvm_slot (int, tree);
59 static tree lookup_name_current_level (tree);
60 static tree push_promoted_type (const char *, tree);
61 static struct binding_level *make_binding_level (void);
62 static tree create_primitive_vtable (const char *);
63 static tree check_local_unnamed_variable (tree, tree, tree);
64 static void parse_version (void);
65
66
67 /* The following ABI flags are used in the high-order bits of the version
68    ID field. The version ID number itself should never be larger than 
69    0xfffff, so it should be safe to use top 12 bits for these flags. */
70
71 #define FLAG_BINARYCOMPAT_ABI (1<<31)  /* Class is built with the BC-ABI. */
72
73 #define FLAG_BOOTSTRAP_LOADER (1<<30)  /* Used when defining a class that 
74                                           should be loaded by the bootstrap
75                                           loader.  */
76
77 /* If an ABI change is made within a GCC release series, rendering current
78    binaries incompatible with the old runtimes, this number must be set to
79    enforce the compatibility rules. */
80 #define MINOR_BINARYCOMPAT_ABI_VERSION 1
81
82 /* The runtime may recognize a variety of BC ABIs (objects generated by 
83    different version of gcj), but will probably always require strict 
84    matching for the ordinary (C++) ABI.  */
85
86 /* The version ID of the BC ABI that we generate.  This must be kept in
87    sync with parse_version(), libgcj, and reality (if the BC format changes, 
88    this must change).  */
89 #define GCJ_CURRENT_BC_ABI_VERSION \
90   (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
91
92 /* The ABI version number.  */
93 tree gcj_abi_version;
94
95 /* Name of the Cloneable class.  */
96 tree java_lang_cloneable_identifier_node;
97
98 /* Name of the Serializable class.  */
99 tree java_io_serializable_identifier_node;
100
101 /* The DECL_MAP is a mapping from (index, type) to a decl node.
102    If index < max_locals, it is the index of a local variable.
103    if index >= max_locals, then index-max_locals is a stack slot.
104    The DECL_MAP mapping is represented as a TREE_VEC whose elements
105    are a list of decls (VAR_DECL or PARM_DECL) chained by
106    DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
107    we search the chain for a decl with a matching TREE_TYPE. */
108
109 static GTY(()) tree decl_map;
110
111 /* The base_decl_map is contains one variable of ptr_type: this is
112    used to contain every variable of reference type that is ever
113    stored in a local variable slot.  */
114
115 static GTY(()) tree base_decl_map;
116
117 /* An index used to make temporary identifiers unique.  */
118 static int uniq;
119
120 /* A list of local variables VAR_DECLs for this method that we have seen
121    debug information, but we have not reached their starting (byte) PC yet. */
122
123 static GTY(()) tree pending_local_decls;
124
125 /* The decl for "_Jv_ResolvePoolEntry".  */
126 tree soft_resolvepoolentry_node;
127
128 /* The decl for the .constants field of an instance of Class.  */
129 tree constants_field_decl_node;
130
131 /* The decl for the .data field of an instance of Class.  */
132 tree constants_data_field_decl_node;
133
134 #if defined(DEBUG_JAVA_BINDING_LEVELS)
135 int binding_depth = 0;
136 int is_class_level = 0;
137 int current_pc;
138
139 void
140 indent (void)
141 {
142   int i;
143
144   for (i = 0; i < binding_depth*2; i++)
145     putc (' ', stderr);
146 }
147 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
148
149 /* True if decl is a named local variable, i.e. if it is an alias
150    that's used only for debugging purposes.  */
151
152 static bool
153 debug_variable_p (tree decl)
154 {
155   if (TREE_CODE (decl) == PARM_DECL)
156     return false;
157
158   if (LOCAL_SLOT_P (decl))
159     return false;
160
161   return true;
162 }
163  
164 static tree
165 push_jvm_slot (int index, tree decl)
166 {
167   DECL_CONTEXT (decl) = current_function_decl;
168   layout_decl (decl, 0);
169
170   /* Now link the decl into the decl_map. */
171   if (DECL_LANG_SPECIFIC (decl) == NULL)
172     {
173       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
174       DECL_LOCAL_START_PC (decl) = 0;
175       DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
176       DECL_LOCAL_SLOT_NUMBER (decl) = index;
177     }
178   DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
179   TREE_VEC_ELT (decl_map, index) = decl;
180
181   return decl;
182 }
183
184 /* Find the best declaration based upon type.  If 'decl' fits 'type' better
185    than 'best', return 'decl'.  Otherwise return 'best'.  */
186
187 static tree
188 check_local_unnamed_variable (tree best, tree decl, tree type)
189 {
190   tree decl_type = TREE_TYPE (decl);
191   
192   gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
193
194   /* Use the same decl for all integer types <= 32 bits.  This is
195      necessary because sometimes a value is stored as (for example)
196      boolean but loaded as int.  */
197   if (decl_type == type
198       || (INTEGRAL_TYPE_P (decl_type)
199           && INTEGRAL_TYPE_P (type)
200           && TYPE_PRECISION (decl_type) <= 32
201           && TYPE_PRECISION (type) <= 32
202           && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))      
203       /*  ptr_type_node is used for null pointers, which are
204           assignment compatible with everything.  */
205       || (TREE_CODE (decl_type) == POINTER_TYPE
206           && type == ptr_type_node)
207       /* Whenever anyone wants to use a slot that is initially
208          occupied by a PARM_DECL of pointer type they must get that
209          decl, even if they asked for a pointer to a different type.
210          However, if someone wants a scalar variable in a slot that
211          initially held a pointer arg -- or vice versa -- we create a
212          new VAR_DECL.  
213
214          ???: As long as verification is correct, this will be a
215          compatible type.  But maybe we should create a dummy variable
216          and replace all references to it with the DECL and a
217          NOP_EXPR.  
218       */
219       || (TREE_CODE (decl_type) == POINTER_TYPE
220           && TREE_CODE (decl) == PARM_DECL
221           && TREE_CODE (type) == POINTER_TYPE))
222     {
223       if (best == NULL_TREE
224           || (decl_type == type && TREE_TYPE (best) != type))
225         return decl;
226     }
227
228   return best;
229 }
230
231
232 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
233    that is valid at PC (or -1 if any pc).
234    If there is no existing matching decl, allocate one.  */
235
236 tree
237 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
238 {
239   tree tmp = TREE_VEC_ELT (decl_map, index);
240   tree decl = NULL_TREE;
241
242   /* Scan through every declaration that has been created in this
243      slot.  We're only looking for variables that correspond to local
244      index declarations and PARM_DECLs, not named variables: such
245      local variables are used only for debugging information.  */
246   while (tmp != NULL_TREE)
247     {
248       if (! debug_variable_p (tmp))
249         decl = check_local_unnamed_variable (decl, tmp, type);
250       tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
251     }
252
253   /* gcj has a function called promote_type(), which is used by both
254      the bytecode compiler and the source compiler.  Unfortunately,
255      the type systems for the Java VM and the Java language are not
256      the same: a boolean in the VM promotes to an int, not to a wide
257      boolean.  If our caller wants something to hold a boolean, that
258      had better be an int, because that slot might be re-used
259      later in integer context.  */
260   if (TREE_CODE (type) == BOOLEAN_TYPE)
261     type = integer_type_node;
262
263   /* If we don't find a match, create one with the type passed in.
264      The name of the variable is #n#m, which n is the variable index
265      in the local variable area and m is a dummy identifier for
266      uniqueness -- multiple variables may share the same local
267      variable index.  We don't call pushdecl() to push pointer types
268      into a binding expr because they'll all be replaced by a single
269      variable that is used for every reference in that local variable
270      slot.  */
271   if (! decl)
272     {
273       char buf[64];
274       tree name;
275       sprintf (buf, "#slot#%d#%d", index, uniq++);
276       name = get_identifier (buf);
277       decl = build_decl (input_location, VAR_DECL, name, type);
278       DECL_IGNORED_P (decl) = 1;
279       DECL_ARTIFICIAL (decl) = 1;
280       decl = push_jvm_slot (index, decl);
281       LOCAL_SLOT_P (decl) = 1;
282
283       if (TREE_CODE (type) != POINTER_TYPE)
284         pushdecl_function_level (decl);
285     }
286
287   /* As well as creating a local variable that matches the type, we
288      also create a base variable (of ptr_type) that will hold all its
289      aliases.  */
290   if (TREE_CODE (type) == POINTER_TYPE
291       && ! TREE_VEC_ELT (base_decl_map, index))
292     {
293       char buf[64];
294       tree name;
295       tree base_decl;
296       sprintf (buf, "#ref#%d#%d", index, uniq++);
297       name = get_identifier (buf);
298       base_decl
299         = TREE_VEC_ELT (base_decl_map, index)
300         = build_decl (input_location, VAR_DECL, name, ptr_type_node);
301       pushdecl_function_level (base_decl);
302       DECL_IGNORED_P (base_decl) = 1;
303       DECL_ARTIFICIAL (base_decl) = 1;
304     }
305
306   return decl;
307 }
308
309 /* Called during genericization for every variable.  If the variable
310    is a temporary of pointer type, replace it with a common variable
311    thath is used to hold all pointer types that are ever stored in
312    that slot.  Set WANT_LVALUE if you want a variable that is to be
313    written to.  */
314
315 static tree 
316 java_replace_reference (tree var_decl, bool want_lvalue)
317 {
318   tree decl_type;
319
320   if (! base_decl_map)
321     return var_decl;
322
323   decl_type = TREE_TYPE (var_decl);
324
325   if (TREE_CODE (decl_type) == POINTER_TYPE)
326     {
327       if (DECL_LANG_SPECIFIC (var_decl)
328           && LOCAL_SLOT_P (var_decl))
329         {
330           int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
331           tree base_decl = TREE_VEC_ELT (base_decl_map, index); 
332
333           gcc_assert (base_decl);
334           if (! want_lvalue)
335             base_decl = build1 (NOP_EXPR, decl_type, base_decl);
336
337           return base_decl;
338         }
339     }
340
341   return var_decl;
342 }
343
344 /* Helper for java_genericize.  */
345
346 tree
347 java_replace_references (tree *tp, int *walk_subtrees,
348                          void *data ATTRIBUTE_UNUSED)
349 {
350   if (TREE_CODE (*tp) == MODIFY_EXPR)
351     {
352       source_location loc = EXPR_LOCATION (*tp);
353       tree lhs = TREE_OPERAND (*tp, 0);
354       /* This is specific to the bytecode compiler.  If a variable has
355          LOCAL_SLOT_P set, replace an assignment to it with an assignment
356          to the corresponding variable that holds all its aliases.  */
357       if (TREE_CODE (lhs) == VAR_DECL
358           && DECL_LANG_SPECIFIC (lhs)
359           && LOCAL_SLOT_P (lhs)
360           && TREE_CODE (TREE_TYPE (lhs)) == POINTER_TYPE)
361         {
362           tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
363           tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs),
364                                  TREE_OPERAND (*tp, 1));
365           tree tem = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
366                              new_lhs, new_rhs);
367           *tp = build1 (NOP_EXPR, TREE_TYPE (lhs), tem);
368           SET_EXPR_LOCATION (tem, loc);
369           SET_EXPR_LOCATION (new_rhs, loc);
370           SET_EXPR_LOCATION (*tp, loc);
371         }
372     }
373   if (TREE_CODE (*tp) == VAR_DECL)
374     {
375       *tp = java_replace_reference (*tp, /* want_lvalue */ false);
376       *walk_subtrees = 0;
377     }
378
379   return NULL_TREE;
380 }
381
382 /* Same as find_local_index, except that INDEX is a stack index. */
383
384 tree
385 find_stack_slot (int index, tree type)
386 {
387   return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
388                               type, -1);
389 }
390
391 struct GTY(())
392   binding_level {
393     /* A chain of _DECL nodes for all variables, constants, functions,
394      * and typedef types.  These are in the reverse of the order supplied.
395      */
396     tree names;
397
398     /* For each level, a list of shadowed outer-level local definitions
399        to be restored when this level is popped.
400        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
401        whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
402     tree shadowed;
403
404     /* For each level (except not the global one),
405        a chain of BLOCK nodes for all the levels
406        that were entered and exited one level down.  */
407     tree blocks;
408
409     /* The binding level which this one is contained in (inherits from).  */
410     struct binding_level *level_chain;
411
412     /* The bytecode PC that marks the end of this level. */
413     int end_pc;
414     /* The bytecode PC that marks the start of this level. */
415     int start_pc;
416
417     /* The statements in this binding level.  */
418     tree stmts;
419
420     /* An exception range associated with this binding level.  */
421     struct eh_range * GTY((skip (""))) exception_range;
422
423     /* Binding depth at which this level began.  Used only for debugging.  */
424     unsigned binding_depth;
425
426     /* The location at which this level began.  */
427     source_location loc;
428   };
429
430 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
431
432 /* The binding level currently in effect.  */
433
434 static GTY(()) struct binding_level *current_binding_level;
435
436 /* A chain of binding_level structures awaiting reuse.  */
437
438 static GTY(()) struct binding_level *free_binding_level;
439
440 /* The outermost binding level, for names of file scope.
441    This is created when the compiler is started and exists
442    through the entire run.  */
443
444 static GTY(()) struct binding_level *global_binding_level;
445
446 /* The binding level that holds variables declared at the outermost
447    level within a function body.  */
448
449 static struct binding_level *function_binding_level;
450
451 /* A PC value bigger than any PC value we may ever may encounter. */
452
453 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
454
455 /* Binding level structures are initialized by copying this one.  */
456
457 static const struct binding_level clear_binding_level
458 = {
459     NULL_TREE, /* names */
460     NULL_TREE, /* shadowed */
461     NULL_TREE, /* blocks */
462     NULL_BINDING_LEVEL, /* level_chain */
463     LARGEST_PC, /* end_pc */
464     0, /* start_pc */
465     NULL, /* stmts */
466     NULL, /* exception_range */
467     0, /* binding_depth */
468     0, /* loc */
469   };
470
471 tree java_global_trees[JTI_MAX];
472
473 /* Build (and pushdecl) a "promoted type" for all standard
474    types shorter than int.  */
475
476 static tree
477 push_promoted_type (const char *name, tree actual_type)
478 {
479   tree type = make_node (TREE_CODE (actual_type));
480 #if 1
481   tree in_min = TYPE_MIN_VALUE (int_type_node);
482   tree in_max = TYPE_MAX_VALUE (int_type_node);
483 #else
484   tree in_min = TYPE_MIN_VALUE (actual_type);
485   tree in_max = TYPE_MAX_VALUE (actual_type);
486 #endif
487   TYPE_MIN_VALUE (type) = copy_node (in_min);
488   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
489   TYPE_MAX_VALUE (type) = copy_node (in_max);
490   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
491   TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
492   TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
493   layout_type (type);
494   pushdecl (build_decl (input_location,
495                         TYPE_DECL, get_identifier (name), type));
496   return type;
497 }
498
499 /* Return tree that represents a vtable for a primitive array.  */
500 static tree
501 create_primitive_vtable (const char *name)
502 {
503   tree r;
504   char buf[50];
505
506   sprintf (buf, "_Jv_%sVTable", name);
507   r = build_decl (input_location,
508                   VAR_DECL, get_identifier (buf), ptr_type_node);
509   DECL_EXTERNAL (r) = 1;
510   return r;
511 }
512
513 static tree
514 do_nothing (tree t)
515 {
516   return t;
517 }
518
519 /* Parse the version string and compute the ABI version number.  */
520 static void
521 parse_version (void)
522 {
523   const char *p = version_string;
524   unsigned int major = 0, minor = 0;
525   unsigned int abi_version;
526
527   /* Skip leading junk.  */
528   while (*p && !ISDIGIT (*p))
529     ++p;
530   gcc_assert (*p);
531
532   /* Extract major version.  */
533   while (ISDIGIT (*p))
534     {
535       major = major * 10 + *p - '0';
536       ++p;
537     }
538
539   gcc_assert (*p == '.' && ISDIGIT (p[1]));
540   ++p;
541
542   /* Extract minor version.  */
543   while (ISDIGIT (*p))
544     {
545       minor = minor * 10 + *p - '0';
546       ++p;
547     }
548
549   if (flag_indirect_dispatch)
550     {
551       abi_version = GCJ_CURRENT_BC_ABI_VERSION;
552       abi_version |= FLAG_BINARYCOMPAT_ABI;
553     }
554   else /* C++ ABI */
555     {
556       /* Implicit in this computation is the idea that we won't break the
557          old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
558          4.0.1).  */
559       abi_version = 100000 * major + 1000 * minor;
560     }
561   if (flag_bootstrap_classes)
562     abi_version |= FLAG_BOOTSTRAP_LOADER;
563
564   gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
565 }
566
567 void
568 java_init_decl_processing (void)
569 {
570   tree endlink;
571   tree field = NULL_TREE;
572   tree t;
573
574   init_class_processing ();
575
576   current_function_decl = NULL;
577   current_binding_level = NULL_BINDING_LEVEL;
578   free_binding_level = NULL_BINDING_LEVEL;
579   pushlevel (0);        /* make the binding_level structure for global names */
580   global_binding_level = current_binding_level;
581
582   /* The code here must be similar to build_common_tree_nodes{,_2} in
583      tree.c, especially as to the order of initializing common nodes.  */
584   error_mark_node = make_node (ERROR_MARK);
585   TREE_TYPE (error_mark_node) = error_mark_node;
586
587   /* Create sizetype first - needed for other types. */
588   initialize_sizetypes (false);
589
590   byte_type_node = make_signed_type (8);
591   pushdecl (build_decl (BUILTINS_LOCATION,
592                         TYPE_DECL, get_identifier ("byte"), byte_type_node));
593   short_type_node = make_signed_type (16);
594   pushdecl (build_decl (BUILTINS_LOCATION,
595                         TYPE_DECL, get_identifier ("short"), short_type_node));
596   int_type_node = make_signed_type (32);
597   pushdecl (build_decl (BUILTINS_LOCATION,
598                         TYPE_DECL, get_identifier ("int"), int_type_node));
599   long_type_node = make_signed_type (64);
600   pushdecl (build_decl (BUILTINS_LOCATION,
601                         TYPE_DECL, get_identifier ("long"), long_type_node));
602
603   unsigned_byte_type_node = make_unsigned_type (8);
604   pushdecl (build_decl (BUILTINS_LOCATION,
605                         TYPE_DECL, get_identifier ("unsigned byte"),
606                         unsigned_byte_type_node));
607   unsigned_short_type_node = make_unsigned_type (16);
608   pushdecl (build_decl (BUILTINS_LOCATION,
609                         TYPE_DECL, get_identifier ("unsigned short"),
610                         unsigned_short_type_node));
611   unsigned_int_type_node = make_unsigned_type (32);
612   pushdecl (build_decl (BUILTINS_LOCATION,
613                         TYPE_DECL, get_identifier ("unsigned int"),
614                         unsigned_int_type_node));
615   unsigned_long_type_node = make_unsigned_type (64);
616   pushdecl (build_decl (BUILTINS_LOCATION,
617                         TYPE_DECL, get_identifier ("unsigned long"),
618                         unsigned_long_type_node));
619
620   /* This is not a java type, however tree-dfa requires a definition for
621      size_type_node.  */
622   size_type_node = make_unsigned_type (POINTER_SIZE);
623   set_sizetype (size_type_node);
624
625   /* Define these next since types below may used them.  */
626   integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
627   integer_zero_node = build_int_cst (NULL_TREE, 0);
628   integer_one_node = build_int_cst (NULL_TREE, 1);
629   integer_two_node = build_int_cst (NULL_TREE, 2);
630   integer_four_node = build_int_cst (NULL_TREE, 4);
631   integer_minus_one_node = build_int_cst (NULL_TREE, -1);
632
633   /* A few values used for range checking in the lexer.  */
634   decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
635 #if HOST_BITS_PER_WIDE_INT == 64
636   decimal_long_max = build_int_cstu (unsigned_long_type_node,
637                                      0x8000000000000000LL);
638 #elif HOST_BITS_PER_WIDE_INT == 32
639   decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
640                                          0, 0x80000000);
641 #else
642  #error "unsupported size"
643 #endif
644
645   size_zero_node = size_int (0);
646   size_one_node = size_int (1);
647   bitsize_zero_node = bitsize_int (0);
648   bitsize_one_node = bitsize_int (1);
649   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
650
651   long_zero_node = build_int_cst (long_type_node, 0);
652
653   void_type_node = make_node (VOID_TYPE);
654   pushdecl (build_decl (BUILTINS_LOCATION,
655                         TYPE_DECL, get_identifier ("void"), void_type_node));
656   layout_type (void_type_node); /* Uses size_zero_node */
657
658   ptr_type_node = build_pointer_type (void_type_node);
659   const_ptr_type_node
660     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
661
662   t = make_node (VOID_TYPE);
663   layout_type (t); /* Uses size_zero_node */
664   return_address_type_node = build_pointer_type (t);
665
666   null_pointer_node = build_int_cst (ptr_type_node, 0);
667
668   char_type_node = make_node (INTEGER_TYPE);
669   TYPE_STRING_FLAG (char_type_node) = 1;
670   TYPE_PRECISION (char_type_node) = 16;
671   fixup_unsigned_type (char_type_node);
672   pushdecl (build_decl (BUILTINS_LOCATION,
673                         TYPE_DECL, get_identifier ("char"), char_type_node));
674
675   boolean_type_node = make_node (BOOLEAN_TYPE);
676   TYPE_PRECISION (boolean_type_node) = 1;
677   fixup_unsigned_type (boolean_type_node);
678   pushdecl (build_decl (BUILTINS_LOCATION,
679                         TYPE_DECL, get_identifier ("boolean"),
680                         boolean_type_node));
681   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
682   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
683
684   promoted_byte_type_node
685     = push_promoted_type ("promoted_byte", byte_type_node);
686   promoted_short_type_node
687     = push_promoted_type ("promoted_short", short_type_node);
688   promoted_char_type_node
689     = push_promoted_type ("promoted_char", char_type_node);
690   promoted_boolean_type_node
691     = push_promoted_type ("promoted_boolean", boolean_type_node);
692
693   float_type_node = make_node (REAL_TYPE);
694   TYPE_PRECISION (float_type_node) = 32;
695   pushdecl (build_decl (BUILTINS_LOCATION,
696                         TYPE_DECL, get_identifier ("float"),
697                         float_type_node));
698   layout_type (float_type_node);
699
700   double_type_node = make_node (REAL_TYPE);
701   TYPE_PRECISION (double_type_node) = 64;
702   pushdecl (build_decl (BUILTINS_LOCATION,
703                         TYPE_DECL, get_identifier ("double"),
704                         double_type_node));
705   layout_type (double_type_node);
706
707   float_zero_node = build_real (float_type_node, dconst0);
708   double_zero_node = build_real (double_type_node, dconst0);
709
710   /* These are the vtables for arrays of primitives.  */
711   boolean_array_vtable = create_primitive_vtable ("boolean");
712   byte_array_vtable = create_primitive_vtable ("byte");
713   char_array_vtable = create_primitive_vtable ("char");
714   short_array_vtable = create_primitive_vtable ("short");
715   int_array_vtable = create_primitive_vtable ("int");
716   long_array_vtable = create_primitive_vtable ("long");
717   float_array_vtable = create_primitive_vtable ("float");
718   double_array_vtable = create_primitive_vtable ("double");
719
720   one_elt_array_domain_type = build_index_type (integer_one_node);
721   utf8const_type = make_node (RECORD_TYPE);
722   PUSH_FIELD (input_location,
723               utf8const_type, field, "hash", unsigned_short_type_node);
724   PUSH_FIELD (input_location,
725               utf8const_type, field, "length", unsigned_short_type_node);
726   FINISH_RECORD (utf8const_type);
727   utf8const_ptr_type = build_pointer_type (utf8const_type);
728
729   atable_type = build_array_type (ptr_type_node, 
730                                   one_elt_array_domain_type);
731   TYPE_NONALIASED_COMPONENT (atable_type) = 1;
732   atable_ptr_type = build_pointer_type (atable_type);
733
734   itable_type = build_array_type (ptr_type_node, 
735                                   one_elt_array_domain_type);
736   TYPE_NONALIASED_COMPONENT (itable_type) = 1;
737   itable_ptr_type = build_pointer_type (itable_type);
738
739   symbol_type = make_node (RECORD_TYPE);
740   PUSH_FIELD (input_location,
741               symbol_type, field, "clname", utf8const_ptr_type);
742   PUSH_FIELD (input_location, symbol_type, field, "name", utf8const_ptr_type);
743   PUSH_FIELD (input_location,
744               symbol_type, field, "signature", utf8const_ptr_type);
745   FINISH_RECORD (symbol_type);
746
747   symbols_array_type = build_array_type (symbol_type, 
748                                          one_elt_array_domain_type);
749   symbols_array_ptr_type = build_pointer_type (symbols_array_type);
750
751   assertion_entry_type = make_node (RECORD_TYPE);
752   PUSH_FIELD (input_location,
753               assertion_entry_type, field, "assertion_code", integer_type_node);
754   PUSH_FIELD (input_location,
755               assertion_entry_type, field, "op1", utf8const_ptr_type);
756   PUSH_FIELD (input_location,
757               assertion_entry_type, field, "op2", utf8const_ptr_type);
758   FINISH_RECORD (assertion_entry_type);
759   
760   assertion_table_type = build_array_type (assertion_entry_type,
761                                            one_elt_array_domain_type);
762
763   /* As you're adding items here, please update the code right after
764      this section, so that the filename containing the source code of
765      the pre-defined class gets registered correctly. */
766   unqualified_object_id_node = get_identifier ("Object");
767   object_type_node = lookup_class (get_identifier ("java.lang.Object"));
768   object_ptr_type_node = promote_type (object_type_node);
769   string_type_node = lookup_class (get_identifier ("java.lang.String"));
770   string_ptr_type_node = promote_type (string_type_node);
771   class_type_node = lookup_class (get_identifier ("java.lang.Class"));
772   throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
773   exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
774   runtime_exception_type_node = 
775     lookup_class (get_identifier ("java.lang.RuntimeException"));
776   error_exception_type_node = 
777     lookup_class (get_identifier ("java.lang.Error"));
778
779   rawdata_ptr_type_node
780     = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
781
782   add_predefined_file (get_identifier ("java/lang/Class.java"));
783   add_predefined_file (get_identifier ("java/lang/Error.java"));
784   add_predefined_file (get_identifier ("java/lang/Object.java"));
785   add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
786   add_predefined_file (get_identifier ("java/lang/String.java"));
787   add_predefined_file (get_identifier ("java/lang/Throwable.java"));
788   add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
789   add_predefined_file (get_identifier ("java/lang/Exception.java"));
790   add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
791   add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
792
793   methodtable_type = make_node (RECORD_TYPE);
794   layout_type (methodtable_type);
795   build_decl (BUILTINS_LOCATION,
796               TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
797   methodtable_ptr_type = build_pointer_type (methodtable_type);
798
799   TYPE_identifier_node = get_identifier ("TYPE");
800   init_identifier_node = get_identifier ("<init>");
801   clinit_identifier_node = get_identifier ("<clinit>");
802   void_signature_node = get_identifier ("()V");
803   finalize_identifier_node = get_identifier ("finalize");
804   this_identifier_node = get_identifier ("this");
805
806   java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
807   java_io_serializable_identifier_node =
808     get_identifier ("java.io.Serializable");
809
810   /* for lack of a better place to put this stub call */
811   init_expr_processing();
812
813   constants_type_node = make_node (RECORD_TYPE);
814   PUSH_FIELD (input_location,
815               constants_type_node, field, "size", unsigned_int_type_node);
816   PUSH_FIELD (input_location,
817               constants_type_node, field, "tags", ptr_type_node);
818   PUSH_FIELD (input_location,
819               constants_type_node, field, "data", ptr_type_node);
820   constants_data_field_decl_node = field;
821   FINISH_RECORD (constants_type_node);
822   build_decl (BUILTINS_LOCATION,
823               TYPE_DECL, get_identifier ("constants"), constants_type_node);
824
825   access_flags_type_node = unsigned_short_type_node;
826
827   dtable_type = make_node (RECORD_TYPE);
828   dtable_ptr_type = build_pointer_type (dtable_type);
829
830   otable_type = build_array_type (integer_type_node, 
831                                   one_elt_array_domain_type);
832   TYPE_NONALIASED_COMPONENT (otable_type) = 1;
833   otable_ptr_type = build_pointer_type (otable_type);
834
835   PUSH_FIELD (input_location,
836               object_type_node, field, "vtable", dtable_ptr_type);
837   DECL_FCONTEXT (field) = object_type_node;
838   TYPE_VFIELD (object_type_node) = field;
839
840   /* This isn't exactly true, but it is what we have in the source.
841      There is an unresolved issue here, which is whether the vtable
842      should be marked by the GC.  */
843   if (! flag_hash_synchronization)
844     PUSH_FIELD (input_location, object_type_node, field, "sync_info",
845                 build_pointer_type (object_type_node));
846   for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
847     FIELD_PRIVATE (t) = 1;
848   FINISH_RECORD (object_type_node);
849
850   field_type_node = make_node (RECORD_TYPE);
851   field_ptr_type_node = build_pointer_type (field_type_node);
852   method_type_node = make_node (RECORD_TYPE);
853   method_ptr_type_node = build_pointer_type (method_type_node);
854
855   set_super_info (0, class_type_node, object_type_node, 0);
856   set_super_info (0, string_type_node, object_type_node, 0);
857   class_ptr_type = build_pointer_type (class_type_node);
858
859   PUSH_FIELD (input_location,
860               class_type_node, field, "next_or_version", class_ptr_type);
861   PUSH_FIELD (input_location,
862               class_type_node, field, "name", utf8const_ptr_type);
863   PUSH_FIELD (input_location,
864               class_type_node, field, "accflags", access_flags_type_node);
865   PUSH_FIELD (input_location,
866               class_type_node, field, "superclass", class_ptr_type);
867   PUSH_FIELD (input_location,
868               class_type_node, field, "constants", constants_type_node);
869   constants_field_decl_node = field;
870   PUSH_FIELD (input_location,
871               class_type_node, field, "methods", method_ptr_type_node);
872   PUSH_FIELD (input_location,
873               class_type_node, field, "method_count", short_type_node);
874   PUSH_FIELD (input_location,
875               class_type_node, field, "vtable_method_count", short_type_node);
876   PUSH_FIELD (input_location,
877               class_type_node, field, "fields", field_ptr_type_node);
878   PUSH_FIELD (input_location,
879               class_type_node, field, "size_in_bytes", int_type_node);
880   PUSH_FIELD (input_location,
881               class_type_node, field, "field_count", short_type_node);
882   PUSH_FIELD (input_location,
883               class_type_node, field, "static_field_count", short_type_node);
884   PUSH_FIELD (input_location,
885               class_type_node, field, "vtable", dtable_ptr_type);
886   PUSH_FIELD (input_location,
887               class_type_node, field, "otable", otable_ptr_type);
888   PUSH_FIELD (input_location,
889               class_type_node, field, "otable_syms", 
890               symbols_array_ptr_type);
891   PUSH_FIELD (input_location,
892               class_type_node, field, "atable", atable_ptr_type);
893   PUSH_FIELD (input_location,
894               class_type_node, field, "atable_syms", 
895               symbols_array_ptr_type);
896   PUSH_FIELD (input_location,
897               class_type_node, field, "itable", itable_ptr_type);
898   PUSH_FIELD (input_location, class_type_node, field, "itable_syms", 
899               symbols_array_ptr_type);
900   PUSH_FIELD (input_location,
901               class_type_node, field, "catch_classes", ptr_type_node);
902   PUSH_FIELD (input_location, class_type_node, field, "interfaces",
903               build_pointer_type (class_ptr_type));
904   PUSH_FIELD (input_location, class_type_node, field, "loader", ptr_type_node);
905   PUSH_FIELD (input_location,
906               class_type_node, field, "interface_count", short_type_node);
907   PUSH_FIELD (input_location, class_type_node, field, "state", byte_type_node);
908   PUSH_FIELD (input_location, class_type_node, field, "thread", ptr_type_node);
909   PUSH_FIELD (input_location,
910               class_type_node, field, "depth", short_type_node);
911   PUSH_FIELD (input_location,
912               class_type_node, field, "ancestors", ptr_type_node);
913   PUSH_FIELD (input_location, class_type_node, field, "idt", ptr_type_node);  
914   PUSH_FIELD (input_location,
915               class_type_node, field, "arrayclass", ptr_type_node);  
916   PUSH_FIELD (input_location,
917               class_type_node, field, "protectionDomain", ptr_type_node);
918   PUSH_FIELD (input_location,
919               class_type_node, field, "assertion_table", ptr_type_node);
920   PUSH_FIELD (input_location,
921               class_type_node, field, "hack_signers", ptr_type_node);
922   PUSH_FIELD (input_location, class_type_node, field, "chain", ptr_type_node);
923   PUSH_FIELD (input_location,
924               class_type_node, field, "aux_info", ptr_type_node);
925   PUSH_FIELD (input_location, class_type_node, field, "engine", ptr_type_node);
926   PUSH_FIELD (input_location,
927               class_type_node, field, "reflection_data", ptr_type_node);
928   for (t = TYPE_FIELDS (class_type_node);  t != NULL_TREE;  t = TREE_CHAIN (t))
929     FIELD_PRIVATE (t) = 1;
930   push_super_field (class_type_node, object_type_node);
931
932   FINISH_RECORD (class_type_node);
933   build_decl (BUILTINS_LOCATION,
934               TYPE_DECL, get_identifier ("Class"), class_type_node);
935
936   field_info_union_node = make_node (UNION_TYPE);
937   PUSH_FIELD (input_location,
938               field_info_union_node, field, "boffset", int_type_node);
939   PUSH_FIELD (input_location,
940               field_info_union_node, field, "addr", ptr_type_node);
941   layout_type (field_info_union_node);
942
943   PUSH_FIELD (input_location,
944               field_type_node, field, "name", utf8const_ptr_type);
945   PUSH_FIELD (input_location, field_type_node, field, "type", class_ptr_type);
946   PUSH_FIELD (input_location,
947               field_type_node, field, "accflags", access_flags_type_node);
948   PUSH_FIELD (input_location,
949               field_type_node, field, "bsize", unsigned_short_type_node);
950   PUSH_FIELD (input_location,
951               field_type_node, field, "info", field_info_union_node);
952   FINISH_RECORD (field_type_node);
953   build_decl (BUILTINS_LOCATION,
954               TYPE_DECL, get_identifier ("Field"), field_type_node);
955
956   nativecode_ptr_array_type_node
957     = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
958
959   PUSH_FIELD (input_location,
960               dtable_type, field, "class", class_ptr_type);
961   PUSH_FIELD (input_location,
962               dtable_type, field, "methods", nativecode_ptr_array_type_node);
963   FINISH_RECORD (dtable_type);
964   build_decl (BUILTINS_LOCATION,
965               TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
966
967   jexception_type = make_node (RECORD_TYPE);
968   PUSH_FIELD (input_location,
969               jexception_type, field, "start_pc", ptr_type_node);
970   PUSH_FIELD (input_location, jexception_type, field, "end_pc", ptr_type_node);
971   PUSH_FIELD (input_location,
972               jexception_type, field, "handler_pc", ptr_type_node);
973   PUSH_FIELD (input_location,
974               jexception_type, field, "catch_type", class_ptr_type);
975   FINISH_RECORD (jexception_type);
976   build_decl (BUILTINS_LOCATION,
977               TYPE_DECL, get_identifier ("jexception"), field_type_node);
978   jexception_ptr_type = build_pointer_type (jexception_type);
979
980   lineNumberEntry_type = make_node (RECORD_TYPE);
981   PUSH_FIELD (input_location,
982               lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
983   PUSH_FIELD (input_location,
984               lineNumberEntry_type, field, "start_pc", ptr_type_node);
985   FINISH_RECORD (lineNumberEntry_type);
986
987   lineNumbers_type = make_node (RECORD_TYPE);
988   PUSH_FIELD (input_location,
989               lineNumbers_type, field, "length", unsigned_int_type_node);
990   FINISH_RECORD (lineNumbers_type);
991
992   PUSH_FIELD (input_location,
993               method_type_node, field, "name", utf8const_ptr_type);
994   PUSH_FIELD (input_location,
995               method_type_node, field, "signature", utf8const_ptr_type);
996   PUSH_FIELD (input_location,
997               method_type_node, field, "accflags", access_flags_type_node);
998   PUSH_FIELD (input_location,
999               method_type_node, field, "index", unsigned_short_type_node);
1000   PUSH_FIELD (input_location,
1001               method_type_node, field, "ncode", nativecode_ptr_type_node);
1002   PUSH_FIELD (input_location,
1003               method_type_node, field, "throws", ptr_type_node);
1004   FINISH_RECORD (method_type_node);
1005   build_decl (BUILTINS_LOCATION,
1006               TYPE_DECL, get_identifier ("Method"), method_type_node);
1007
1008   endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
1009
1010   t = tree_cons (NULL_TREE, class_ptr_type, endlink);
1011   alloc_object_node = add_builtin_function ("_Jv_AllocObject",
1012                                             build_function_type (ptr_type_node, t),
1013                                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1014   DECL_IS_MALLOC (alloc_object_node) = 1;
1015   alloc_no_finalizer_node =
1016     add_builtin_function ("_Jv_AllocObjectNoFinalizer",
1017                           build_function_type (ptr_type_node, t),
1018                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1019   DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
1020
1021   t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1022   soft_initclass_node = add_builtin_function ("_Jv_InitClass",
1023                                               build_function_type (void_type_node,
1024                                                                    t),
1025                                               0, NOT_BUILT_IN, NULL, NULL_TREE);
1026   t = tree_cons (NULL_TREE, class_ptr_type,
1027                  tree_cons (NULL_TREE, int_type_node, endlink));
1028   soft_resolvepoolentry_node
1029     = add_builtin_function ("_Jv_ResolvePoolEntry",
1030                             build_function_type (ptr_type_node, t),
1031                             0,NOT_BUILT_IN, NULL, NULL_TREE);
1032   DECL_PURE_P (soft_resolvepoolentry_node) = 1;
1033   throw_node = add_builtin_function ("_Jv_Throw",
1034                                      build_function_type (void_type_node, t),
1035                                      0, NOT_BUILT_IN, NULL, NULL_TREE);
1036   /* Mark throw_nodes as `noreturn' functions with side effects.  */
1037   TREE_THIS_VOLATILE (throw_node) = 1;
1038   TREE_SIDE_EFFECTS (throw_node) = 1;
1039
1040   t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
1041                                                       endlink));
1042   soft_monitorenter_node
1043     = add_builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1044                             NULL, NULL_TREE);
1045   soft_monitorexit_node
1046     = add_builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1047                             NULL, NULL_TREE);
1048
1049   t = tree_cons (NULL_TREE, ptr_type_node,
1050                  tree_cons (NULL_TREE, int_type_node, endlink));
1051   soft_newarray_node
1052       = add_builtin_function ("_Jv_NewPrimArray",
1053                               build_function_type (ptr_type_node, t),
1054                               0, NOT_BUILT_IN, NULL, NULL_TREE);
1055   DECL_IS_MALLOC (soft_newarray_node) = 1;
1056
1057   t = tree_cons (NULL_TREE, int_type_node,
1058                  tree_cons (NULL_TREE, class_ptr_type,
1059                             tree_cons (NULL_TREE, object_ptr_type_node,
1060                                        endlink)));
1061   soft_anewarray_node
1062       = add_builtin_function ("_Jv_NewObjectArray",
1063                               build_function_type (ptr_type_node, t),
1064                               0, NOT_BUILT_IN, NULL, NULL_TREE);
1065   DECL_IS_MALLOC (soft_anewarray_node) = 1;
1066
1067   /* There is no endlink here because _Jv_NewMultiArray is a varargs
1068      function.  */
1069   t = tree_cons (NULL_TREE, ptr_type_node,
1070                  tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1071   soft_multianewarray_node
1072       = add_builtin_function ("_Jv_NewMultiArray",
1073                               build_function_type (ptr_type_node, t),
1074                               0, NOT_BUILT_IN, NULL, NULL_TREE);
1075   DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1076
1077   t = build_function_type (void_type_node, 
1078                            tree_cons (NULL_TREE, int_type_node, endlink));
1079   soft_badarrayindex_node
1080       = add_builtin_function ("_Jv_ThrowBadArrayIndex", t,
1081                               0, NOT_BUILT_IN, NULL, NULL_TREE);
1082   /* Mark soft_badarrayindex_node as a `noreturn' function with side
1083      effects.  */
1084   TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1085   TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1086
1087   soft_nullpointer_node
1088     = add_builtin_function ("_Jv_ThrowNullPointerException",
1089                             build_function_type (void_type_node, endlink),
1090                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1091   /* Mark soft_nullpointer_node as a `noreturn' function with side
1092      effects.  */
1093   TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1094   TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1095
1096   soft_abstractmethod_node
1097     = add_builtin_function ("_Jv_ThrowAbstractMethodError",
1098                             build_function_type (void_type_node, endlink),
1099                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1100   /* Mark soft_abstractmethod_node as a `noreturn' function with side
1101      effects.  */
1102   TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1103   TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1104
1105   soft_nosuchfield_node
1106     = add_builtin_function ("_Jv_ThrowNoSuchFieldError",
1107                             build_function_type (void_type_node, endlink),
1108                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1109   /* Mark soft_nosuchfield_node as a `noreturn' function with side
1110      effects.  */
1111   TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1112   TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1113
1114   t = tree_cons (NULL_TREE, class_ptr_type,
1115                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1116   soft_checkcast_node
1117     = add_builtin_function ("_Jv_CheckCast",
1118                             build_function_type (ptr_type_node, t),
1119                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1120   t = tree_cons (NULL_TREE, object_ptr_type_node,
1121                  tree_cons (NULL_TREE, class_ptr_type, endlink));
1122   soft_instanceof_node
1123     = add_builtin_function ("_Jv_IsInstanceOf",
1124                             build_function_type (boolean_type_node, t),
1125                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1126   DECL_PURE_P (soft_instanceof_node) = 1;
1127   t = tree_cons (NULL_TREE, object_ptr_type_node,
1128                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1129   soft_checkarraystore_node
1130     = add_builtin_function ("_Jv_CheckArrayStore",
1131                             build_function_type (void_type_node, t),
1132                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1133   t = tree_cons (NULL_TREE, ptr_type_node,
1134                  tree_cons (NULL_TREE, ptr_type_node,
1135                             tree_cons (NULL_TREE, int_type_node, endlink)));
1136   soft_lookupinterfacemethod_node
1137     = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
1138                             build_function_type (ptr_type_node, t),
1139                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1140   DECL_PURE_P (soft_lookupinterfacemethod_node) = 1;
1141   t = tree_cons (NULL_TREE, ptr_type_node,
1142                  tree_cons (NULL_TREE, ptr_type_node,
1143                             tree_cons (NULL_TREE, ptr_type_node, endlink)));
1144   soft_lookupinterfacemethodbyname_node
1145     = add_builtin_function ("_Jv_LookupInterfaceMethod",
1146                             build_function_type (ptr_type_node, t),
1147                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1148   t = tree_cons (NULL_TREE, object_ptr_type_node,
1149                  tree_cons (NULL_TREE, ptr_type_node,
1150                             tree_cons (NULL_TREE, ptr_type_node, 
1151                                        tree_cons (NULL_TREE, int_type_node, 
1152                                                   endlink))));
1153   soft_lookupjnimethod_node
1154     = add_builtin_function ("_Jv_LookupJNIMethod",
1155                             build_function_type (ptr_type_node, t),
1156                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1157   t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1158   soft_getjnienvnewframe_node
1159     = add_builtin_function ("_Jv_GetJNIEnvNewFrame",
1160                             build_function_type (ptr_type_node, t),
1161                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1162   soft_jnipopsystemframe_node
1163     = add_builtin_function ("_Jv_JNI_PopSystemFrame",
1164                             build_function_type (void_type_node, t),
1165                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1166
1167   t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
1168   soft_unwrapjni_node
1169     = add_builtin_function ("_Jv_UnwrapJNIweakReference",
1170                             build_function_type (object_ptr_type_node, t),
1171                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1172
1173   t = tree_cons (NULL_TREE, int_type_node,
1174                  tree_cons (NULL_TREE, int_type_node, endlink));
1175   soft_idiv_node
1176     = add_builtin_function ("_Jv_divI",
1177                             build_function_type (int_type_node, t),
1178                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1179
1180   soft_irem_node
1181     = add_builtin_function ("_Jv_remI",
1182                             build_function_type (int_type_node, t),
1183                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1184
1185   t = tree_cons (NULL_TREE, long_type_node,
1186                  tree_cons (NULL_TREE, long_type_node, endlink));
1187   soft_ldiv_node
1188     = add_builtin_function ("_Jv_divJ",
1189                             build_function_type (long_type_node, t),
1190                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1191
1192   soft_lrem_node
1193     = add_builtin_function ("_Jv_remJ",
1194                             build_function_type (long_type_node, t),
1195                             0, NOT_BUILT_IN, NULL, NULL_TREE);
1196
1197   /* Initialize variables for except.c.  */
1198   eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1199                                              ? "__gcj_personality_sj0"
1200                                              : "__gcj_personality_v0");
1201   if (targetm.arm_eabi_unwinder)
1202     unwind_resume_libfunc = init_one_libfunc ("__cxa_end_cleanup");
1203   else
1204     default_init_unwind_resume_libfunc ();
1205
1206   lang_eh_runtime_type = do_nothing;
1207
1208   initialize_builtins ();
1209   soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1210
1211   parse_version ();
1212 }
1213
1214
1215 /* Look up NAME in the current binding level and its superiors
1216    in the namespace of variables, functions and typedefs.
1217    Return a ..._DECL node of some kind representing its definition,
1218    or return 0 if it is undefined.  */
1219
1220 tree
1221 lookup_name (tree name)
1222 {
1223   tree val;
1224   if (current_binding_level != global_binding_level
1225       && IDENTIFIER_LOCAL_VALUE (name))
1226     val = IDENTIFIER_LOCAL_VALUE (name);
1227   else
1228     val = IDENTIFIER_GLOBAL_VALUE (name);
1229   return val;
1230 }
1231
1232 /* Similar to `lookup_name' but look only at current binding level and
1233    the previous one if it's the parameter level.  */
1234
1235 static tree
1236 lookup_name_current_level (tree name)
1237 {
1238   tree t;
1239
1240   if (current_binding_level == global_binding_level)
1241     return IDENTIFIER_GLOBAL_VALUE (name);
1242
1243   if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1244     return 0;
1245
1246   for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1247     if (DECL_NAME (t) == name)
1248       break;
1249
1250   return t;
1251 }
1252
1253 /* Record a decl-node X as belonging to the current lexical scope.
1254    Check for errors (such as an incompatible declaration for the same
1255    name already seen in the same scope).
1256
1257    Returns either X or an old decl for the same name.
1258    If an old decl is returned, it may have been smashed
1259    to agree with what X says.  */
1260
1261 tree
1262 pushdecl (tree x)
1263 {
1264   tree t;
1265   tree name = DECL_NAME (x);
1266   struct binding_level *b = current_binding_level;
1267   
1268   if (TREE_CODE (x) != TYPE_DECL)
1269     DECL_CONTEXT (x) = current_function_decl;
1270   if (name)
1271     {
1272       t = lookup_name_current_level (name);
1273       if (t != 0 && t == error_mark_node)
1274         /* error_mark_node is 0 for a while during initialization!  */
1275         {
1276           t = 0;
1277           error ("%q+D used prior to declaration", x);
1278         }
1279
1280       /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1281          to point to the TYPE_DECL.
1282          Since Java does not have typedefs, a type can only have
1283          one (true) name, given by a class, interface, or builtin. */
1284       if (TREE_CODE (x) == TYPE_DECL
1285           && TYPE_NAME (TREE_TYPE (x)) == 0
1286           && TREE_TYPE (x) != error_mark_node)
1287         {
1288           TYPE_NAME (TREE_TYPE (x)) = x;
1289           TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1290         }
1291
1292       /* This name is new in its binding level.
1293          Install the new declaration and return it.  */
1294       if (b == global_binding_level)
1295         {
1296           /* Install a global value.  */
1297           
1298           IDENTIFIER_GLOBAL_VALUE (name) = x;
1299         }
1300       else
1301         {
1302           /* Here to install a non-global value.  */
1303           tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1304           IDENTIFIER_LOCAL_VALUE (name) = x;
1305
1306           /* If storing a local value, there may already be one (inherited).
1307              If so, record it for restoration when this binding level ends.  */
1308           if (oldlocal != 0)
1309             b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1310         }
1311     }
1312
1313   /* Put decls on list in reverse order.
1314      We will reverse them later if necessary.  */
1315   TREE_CHAIN (x) = b->names;
1316   b->names = x;
1317
1318   return x;
1319 }
1320
1321 void
1322 pushdecl_force_head (tree x)
1323 {
1324   current_binding_level->names = x;
1325 }
1326
1327 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate.  */
1328
1329 tree
1330 pushdecl_top_level (tree x)
1331 {
1332   tree t;
1333   struct binding_level *b = current_binding_level;
1334
1335   current_binding_level = global_binding_level;
1336   t = pushdecl (x);
1337   current_binding_level = b;
1338   return t;
1339 }
1340
1341 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate.  */
1342
1343 tree
1344 pushdecl_function_level (tree x)
1345 {
1346   tree t;
1347   struct binding_level *b = current_binding_level;
1348
1349   current_binding_level = function_binding_level;
1350   t = pushdecl (x);
1351   current_binding_level = b;
1352   return t;
1353 }
1354
1355 /* Nonzero if we are currently in the global binding level.  */
1356
1357 int
1358 global_bindings_p (void)
1359 {
1360   return current_binding_level == global_binding_level;
1361 }
1362
1363 /* Return the list of declarations of the current level.
1364    Note that this list is in reverse order unless/until
1365    you nreverse it; and when you do nreverse it, you must
1366    store the result back using `storedecls' or you will lose.  */
1367
1368 tree
1369 getdecls (void)
1370 {
1371   return current_binding_level->names;
1372 }
1373
1374 /* Create a new `struct binding_level'.  */
1375
1376 static struct binding_level *
1377 make_binding_level (void)
1378 {
1379   /* NOSTRICT */
1380   return GGC_CNEW (struct binding_level);
1381 }
1382
1383 void
1384 pushlevel (int unused ATTRIBUTE_UNUSED)
1385 {
1386   struct binding_level *newlevel = NULL_BINDING_LEVEL;
1387
1388   /* Reuse or create a struct for this binding level.  */
1389
1390   if (free_binding_level)
1391     {
1392       newlevel = free_binding_level;
1393       free_binding_level = free_binding_level->level_chain;
1394     }
1395   else
1396     {
1397       newlevel = make_binding_level ();
1398     }
1399
1400   /* Add this level to the front of the chain (stack) of levels that
1401      are active.  */
1402
1403   *newlevel = clear_binding_level;
1404   newlevel->level_chain = current_binding_level;
1405   newlevel->loc = input_location;
1406   current_binding_level = newlevel;  
1407 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1408   newlevel->binding_depth = binding_depth;
1409   indent ();
1410   fprintf (stderr, "push %s level %p pc %d\n",
1411            (is_class_level) ? "class" : "block", newlevel, current_pc);
1412   is_class_level = 0;
1413   binding_depth++;
1414 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1415 }
1416
1417 /* Exit a binding level.
1418    Pop the level off, and restore the state of the identifier-decl mappings
1419    that were in effect when this level was entered.
1420
1421    If KEEP is nonzero, this level had explicit declarations, so
1422    and create a "block" (a BLOCK node) for the level
1423    to record its declarations and subblocks for symbol table output.
1424
1425    If FUNCTIONBODY is nonzero, this level is the body of a function,
1426    so create a block as if KEEP were set and also clear out all
1427    label names.
1428
1429    If REVERSE is nonzero, reverse the order of decls before putting
1430    them into the BLOCK.  */
1431
1432 tree
1433 poplevel (int keep, int reverse, int functionbody)
1434 {
1435   tree link;
1436   /* The chain of decls was accumulated in reverse order.
1437      Put it into forward order, just for cleanliness.  */
1438   tree decls;
1439   tree subblocks = current_binding_level->blocks;
1440   tree block = 0;
1441   tree decl;
1442   tree bind = 0;
1443
1444 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1445   binding_depth--;
1446   indent ();
1447   if (current_binding_level->end_pc != LARGEST_PC)
1448     fprintf (stderr, "pop  %s level %p pc %d (end pc %d)\n",
1449              (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1450              current_binding_level->end_pc);
1451   else
1452     fprintf (stderr, "pop  %s level %p pc %d\n",
1453              (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1454 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1455
1456   /* Get the decls in the order they were written.
1457      Usually current_binding_level->names is in reverse order.
1458      But parameter decls were previously put in forward order.  */
1459
1460   if (reverse)
1461     current_binding_level->names
1462       = decls = nreverse (current_binding_level->names);
1463   else
1464     decls = current_binding_level->names;
1465
1466   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1467     if (TREE_CODE (decl) == VAR_DECL
1468         && DECL_LANG_SPECIFIC (decl) != NULL
1469         && DECL_LOCAL_SLOT_NUMBER (decl))
1470       LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1471
1472   /* If there were any declarations in that level,
1473      or if this level is a function body,
1474      create a BLOCK to record them for the life of this function.  */
1475
1476   block = 0;
1477   if (keep || functionbody)
1478     {
1479       block = make_node (BLOCK);
1480       TREE_TYPE (block) = void_type_node;
1481     }
1482
1483   if (current_binding_level->exception_range)
1484     expand_end_java_handler (current_binding_level->exception_range);
1485
1486   if (block != 0)
1487     {
1488       /* If any statements have been generated at this level, create a
1489          BIND_EXPR to hold them and copy the variables to it.  This
1490          only applies to the bytecode compiler.  */
1491       if (current_binding_level->stmts)
1492         {
1493           tree decl = decls;
1494           tree *var = &BLOCK_VARS (block);
1495
1496           /* Copy decls from names list, ignoring labels.  */
1497           while (decl)
1498             {
1499               tree next = TREE_CHAIN (decl);
1500               if (TREE_CODE (decl) != LABEL_DECL)
1501                 {
1502                   *var = decl;
1503                   var = &TREE_CHAIN (decl);
1504                 }
1505               decl = next;
1506             }
1507           *var = NULL;
1508             
1509           bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block), 
1510                          BLOCK_EXPR_BODY (block), block);
1511           BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1512           
1513           if (BIND_EXPR_BODY (bind)
1514               && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1515             TREE_SIDE_EFFECTS (bind) = 1;
1516           
1517           /* FIXME: gimplifier brain damage.  */
1518           if (BIND_EXPR_BODY (bind) == NULL)
1519             BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1520
1521           SET_EXPR_LOCATION (bind, current_binding_level->loc);
1522
1523           current_binding_level->stmts = NULL;
1524         }
1525       else
1526         {
1527           BLOCK_VARS (block) = decls;
1528         }
1529       BLOCK_SUBBLOCKS (block) = subblocks;
1530     }   
1531
1532   /* In each subblock, record that this is its superior.  */
1533
1534   for (link = subblocks; link; link = TREE_CHAIN (link))
1535     BLOCK_SUPERCONTEXT (link) = block;
1536
1537   /* Clear out the meanings of the local variables of this level.  */
1538
1539   for (link = decls; link; link = TREE_CHAIN (link))
1540     {
1541       tree name = DECL_NAME (link);
1542       if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1543         {
1544           /* If the ident. was used or addressed via a local extern decl,
1545              don't forget that fact.  */
1546           if (DECL_EXTERNAL (link))
1547             {
1548               if (TREE_USED (link))
1549                 TREE_USED (name) = 1;
1550               if (TREE_ADDRESSABLE (link))
1551                 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1552             }
1553           IDENTIFIER_LOCAL_VALUE (name) = 0;
1554         }
1555     }
1556
1557   /* Restore all name-meanings of the outer levels
1558      that were shadowed by this level.  */
1559
1560   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1561     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1562
1563   /* If the level being exited is the top level of a function,
1564      check over all the labels, and clear out the current
1565      (function local) meanings of their names.  */
1566
1567   if (functionbody)
1568     {
1569       /* If this is the top level block of a function,
1570          the vars are the function's parameters.
1571          Don't leave them in the BLOCK because they are
1572          found in the FUNCTION_DECL instead.  */
1573
1574       BLOCK_VARS (block) = 0;
1575     }
1576
1577   /* Pop the current level, and free the structure for reuse.  */
1578
1579   {
1580     struct binding_level *level = current_binding_level;
1581     current_binding_level = current_binding_level->level_chain;
1582
1583     level->level_chain = free_binding_level;
1584     free_binding_level = level;
1585   }
1586
1587   /* Dispose of the block that we just made inside some higher level.  */
1588   if (functionbody)
1589     {
1590       DECL_INITIAL (current_function_decl) = block;
1591       DECL_SAVED_TREE (current_function_decl) = bind;
1592     }
1593   else 
1594     {
1595       if (block)
1596         {
1597           current_binding_level->blocks
1598             = chainon (current_binding_level->blocks, block);
1599         }
1600       /* If we did not make a block for the level just exited,
1601          any blocks made for inner levels
1602          (since they cannot be recorded as subblocks in that level)
1603          must be carried forward so they will later become subblocks
1604          of something else.  */
1605       else if (subblocks)
1606         current_binding_level->blocks
1607           = chainon (current_binding_level->blocks, subblocks);
1608
1609       if (bind)
1610         java_add_stmt (bind);
1611     }
1612
1613   if (block)
1614     TREE_USED (block) = 1;
1615   return block;
1616 }
1617
1618 void
1619 maybe_pushlevels (int pc)
1620 {
1621 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1622   current_pc = pc;
1623 #endif
1624
1625   while (pending_local_decls != NULL_TREE &&
1626          DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1627     {
1628       tree *ptr = &pending_local_decls;
1629       tree decl = *ptr, next;
1630       int end_pc = DECL_LOCAL_END_PC (decl);
1631
1632       while (*ptr != NULL_TREE
1633              && DECL_LOCAL_START_PC (*ptr) <= pc
1634              && DECL_LOCAL_END_PC (*ptr) == end_pc)
1635         ptr = &TREE_CHAIN (*ptr);
1636       pending_local_decls = *ptr;
1637       *ptr = NULL_TREE;
1638
1639       /* Force non-nested range to be nested in current range by
1640          truncating variable lifetimes. */
1641       if (end_pc > current_binding_level->end_pc)
1642         {
1643           tree t;
1644           end_pc = current_binding_level->end_pc;
1645           for (t = decl; t != NULL_TREE; t = TREE_CHAIN (t))
1646             DECL_LOCAL_END_PC (t) = end_pc;
1647         }
1648
1649       maybe_start_try (pc, end_pc);
1650       
1651       pushlevel (1);
1652
1653       current_binding_level->end_pc = end_pc;
1654       current_binding_level->start_pc = pc;      
1655       current_binding_level->names = NULL;
1656       for ( ; decl != NULL_TREE; decl = next)
1657         {
1658           int index = DECL_LOCAL_SLOT_NUMBER (decl);
1659           tree base_decl;
1660           next = TREE_CHAIN (decl);
1661           push_jvm_slot (index, decl);
1662           pushdecl (decl);
1663           base_decl
1664             = find_local_variable (index, TREE_TYPE (decl), pc);
1665           if (TREE_CODE (TREE_TYPE (base_decl)) == POINTER_TYPE)
1666             base_decl = TREE_VEC_ELT (base_decl_map, index);
1667           SET_DECL_VALUE_EXPR (decl, base_decl);
1668           DECL_HAS_VALUE_EXPR_P (decl) = 1;
1669         }
1670     }      
1671
1672   maybe_start_try (pc, 0);
1673 }
1674
1675 void
1676 maybe_poplevels (int pc)
1677 {
1678 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1679   current_pc = pc;
1680 #endif
1681
1682   /* FIXME: I'm pretty sure that this is wrong.  Variable scopes are
1683      inclusive, so a variable is live if pc == end_pc.  Here, we
1684      terminate a range if the current pc is equal to the end of the
1685      range, and this is *before* we have generated code for the
1686      instruction at end_pc.  We're closing a binding level one
1687      instruction too early.*/
1688   while (current_binding_level->end_pc <= pc)
1689     poplevel (1, 0, 0);
1690 }
1691
1692 /* Terminate any binding which began during the range beginning at
1693    start_pc.  This tidies up improperly nested local variable ranges
1694    and exception handlers; a variable declared within an exception
1695    range is forcibly terminated when that exception ends. */
1696
1697 void
1698 force_poplevels (int start_pc)
1699 {
1700   while (current_binding_level->start_pc > start_pc)
1701     {
1702       if (pedantic && current_binding_level->start_pc > start_pc)
1703         warning (0, "In %+D: overlapped variable and exception ranges at %d",
1704                  current_function_decl,
1705                  current_binding_level->start_pc);
1706       poplevel (1, 0, 0);
1707     }
1708 }
1709
1710 /* integrate_decl_tree calls this function. */
1711
1712 void
1713 java_dup_lang_specific_decl (tree node)
1714 {
1715   int lang_decl_size;
1716   struct lang_decl *x;
1717
1718   if (!DECL_LANG_SPECIFIC (node))
1719     return;
1720
1721   lang_decl_size = sizeof (struct lang_decl);
1722   x = GGC_NEW (struct lang_decl);
1723   memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1724   DECL_LANG_SPECIFIC (node) = x;
1725 }
1726
1727 void
1728 give_name_to_locals (JCF *jcf)
1729 {
1730   int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1731   int code_offset = DECL_CODE_OFFSET (current_function_decl);
1732   tree parm;
1733   pending_local_decls = NULL_TREE;
1734   if (n == 0)
1735     return;
1736   JCF_SEEK (jcf, n);
1737   n = JCF_readu2 (jcf);
1738   for (i = 0; i < n; i++)
1739     {
1740       int start_pc = JCF_readu2 (jcf);
1741       int length = JCF_readu2 (jcf);
1742       int name_index = JCF_readu2 (jcf);
1743       int signature_index = JCF_readu2 (jcf);
1744       int slot = JCF_readu2 (jcf);
1745       tree name = get_name_constant (jcf, name_index);
1746       tree type = parse_signature (jcf, signature_index);
1747       if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1748           && start_pc == 0
1749           && length == DECL_CODE_LENGTH (current_function_decl))
1750         {
1751           tree decl = TREE_VEC_ELT (decl_map, slot);
1752           DECL_NAME (decl) = name;
1753           if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1754             warning (0, "bad type in parameter debug info");
1755         }
1756       else
1757         {
1758           tree *ptr;
1759           int end_pc = start_pc + length;
1760           tree decl = build_decl (input_location, VAR_DECL, name, type);
1761           if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1762             {
1763               warning (0, "bad PC range for debug info for local %q+D",
1764                        decl);
1765               end_pc = DECL_CODE_LENGTH (current_function_decl);
1766             }
1767
1768           /* Adjust start_pc if necessary so that the local's first
1769              store operation will use the relevant DECL as a
1770              destination. Fore more information, read the leading
1771              comments for expr.c:maybe_adjust_start_pc. */
1772           start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1773
1774           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1775           DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1776           DECL_LOCAL_START_PC (decl) = start_pc;
1777           DECL_LOCAL_END_PC (decl) = end_pc;
1778
1779           /* Now insert the new decl in the proper place in
1780              pending_local_decls.  We are essentially doing an insertion sort,
1781              which works fine, since the list input will normally already
1782              be sorted. */
1783           ptr = &pending_local_decls;
1784           while (*ptr != NULL_TREE
1785                  && (DECL_LOCAL_START_PC (*ptr) > start_pc
1786                      || (DECL_LOCAL_START_PC (*ptr) == start_pc
1787                          && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1788             ptr = &TREE_CHAIN (*ptr);
1789           TREE_CHAIN (decl) = *ptr;
1790           *ptr = decl;
1791         }
1792     }
1793
1794   pending_local_decls = nreverse (pending_local_decls);
1795
1796   /* Fill in default names for the parameters. */ 
1797   for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1798        parm != NULL_TREE;  parm = TREE_CHAIN (parm), i++)
1799     {
1800       if (DECL_NAME (parm) == NULL_TREE)
1801         {
1802           int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1803           if (arg_i == 0)
1804             DECL_NAME (parm) = get_identifier ("this");
1805           else
1806             {
1807               char buffer[12];
1808               sprintf (buffer, "ARG_%d", arg_i);
1809               DECL_NAME (parm) = get_identifier (buffer);
1810             }
1811         }
1812     }
1813 }
1814
1815 tree
1816 build_result_decl (tree fndecl)
1817 {
1818   tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1819   tree result = DECL_RESULT (fndecl);
1820   if (! result)
1821     {
1822       result = build_decl (DECL_SOURCE_LOCATION (fndecl),
1823                            RESULT_DECL, NULL_TREE, restype);
1824       DECL_ARTIFICIAL (result) = 1;
1825       DECL_IGNORED_P (result) = 1;
1826       DECL_CONTEXT (result) = fndecl;
1827       DECL_RESULT (fndecl) = result;
1828     }
1829   return result;
1830 }
1831
1832 void
1833 start_java_method (tree fndecl)
1834 {
1835   tree tem, *ptr;
1836   int i;
1837
1838   uniq = 0;
1839
1840   current_function_decl = fndecl;
1841   announce_function (fndecl);
1842
1843   i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1844   decl_map = make_tree_vec (i);
1845   base_decl_map = make_tree_vec (i);
1846   type_map = XRESIZEVEC (tree, type_map, i);
1847
1848 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1849   fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1850   current_pc = 0;
1851 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1852   pushlevel (1);  /* Push parameters. */
1853
1854   ptr = &DECL_ARGUMENTS (fndecl);
1855   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1856        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1857     {
1858       tree parm_name = NULL_TREE, parm_decl;
1859       tree parm_type = TREE_VALUE (tem);
1860       gcc_assert (i < DECL_MAX_LOCALS (fndecl));
1861
1862       parm_decl = build_decl (input_location, PARM_DECL, parm_name, parm_type);
1863       DECL_CONTEXT (parm_decl) = fndecl;
1864       if (targetm.calls.promote_prototypes (parm_type)
1865           && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1866           && INTEGRAL_TYPE_P (parm_type))
1867         parm_type = integer_type_node;
1868       DECL_ARG_TYPE (parm_decl) = parm_type;
1869
1870       *ptr = parm_decl;
1871       ptr = &TREE_CHAIN (parm_decl);
1872
1873       /* Add parm_decl to the decl_map. */
1874       push_jvm_slot (i, parm_decl);
1875
1876       type_map[i] = TREE_TYPE (parm_decl);
1877       if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1878         {
1879           i++;
1880           type_map[i] = void_type_node;
1881         }
1882     }
1883   *ptr = NULL_TREE;
1884   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1885
1886   while (i < DECL_MAX_LOCALS(fndecl))
1887     type_map[i++] = NULL_TREE;
1888
1889   build_result_decl (fndecl);
1890   DECL_SOURCE_LOCATION (fndecl) = input_location;
1891
1892   /* Push local variables.  */
1893   pushlevel (2);
1894
1895   function_binding_level = current_binding_level;
1896 }
1897
1898 void
1899 end_java_method (void)
1900 {
1901   tree fndecl = current_function_decl;
1902
1903   /* pop out of function */
1904   poplevel (1, 1, 0);
1905
1906   /* pop out of its parameters */
1907   poplevel (1, 0, 1);
1908
1909   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1910   
1911   if (DECL_SAVED_TREE (fndecl))
1912     {
1913       tree fbody, block_body;
1914       /* Before we check initialization, attached all class initialization
1915          variable to the block_body */
1916       fbody = DECL_SAVED_TREE (fndecl);
1917       block_body = BIND_EXPR_BODY (fbody);
1918       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
1919                      attach_init_test_initialization_flags, block_body);
1920     }
1921
1922   finish_method (fndecl);
1923
1924   current_function_decl = NULL_TREE;
1925   base_decl_map = NULL_TREE;
1926 }
1927
1928 /* Prepare a method for expansion.  */
1929
1930 void
1931 finish_method (tree fndecl)
1932 {
1933   tree *tp = &DECL_SAVED_TREE (fndecl);
1934
1935   /* Wrap body of synchronized methods in a monitorenter,
1936      plus monitorexit cleanup.  */
1937   if (METHOD_SYNCHRONIZED (fndecl))
1938     {
1939       tree enter, exit, lock;
1940       if (METHOD_STATIC (fndecl))
1941         lock = build_class_ref (DECL_CONTEXT (fndecl));
1942       else
1943         lock = DECL_ARGUMENTS (fndecl);
1944       BUILD_MONITOR_ENTER (enter, lock);
1945       BUILD_MONITOR_EXIT (exit, lock);
1946       *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
1947                     build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
1948     }
1949
1950   /* Convert function tree to GENERIC prior to inlining.  */
1951   java_genericize (fndecl);
1952
1953   /* Store the end of the function, so that we get good line number
1954      info for the epilogue.  */
1955   if (DECL_STRUCT_FUNCTION (fndecl))
1956     set_cfun (DECL_STRUCT_FUNCTION (fndecl));
1957   else
1958     allocate_struct_function (fndecl, false);
1959   cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
1960
1961   /* Defer inlining and expansion to the cgraph optimizers.  */
1962   cgraph_finalize_function (fndecl, false);
1963 }
1964
1965 /* We pessimistically marked all methods and fields external until we
1966    knew what set of classes we were planning to compile.  Now mark those
1967    associated with CLASS to be generated locally as not external.  */
1968
1969 static void
1970 java_mark_decl_local (tree decl)
1971 {
1972   DECL_EXTERNAL (decl) = 0;
1973
1974 #ifdef ENABLE_CHECKING
1975   /* Double check that we didn't pass the function to the callgraph early.  */
1976   if (TREE_CODE (decl) == FUNCTION_DECL)
1977     gcc_assert (!cgraph_node (decl)->local.finalized);
1978 #endif
1979   gcc_assert (!DECL_RTL_SET_P (decl));
1980 }
1981
1982 /* Given appropriate target support, G++ will emit hidden aliases for native
1983    methods.  Using this hidden name is required for proper operation of
1984    _Jv_Method::ncode, but it doesn't hurt to use it everywhere.  Look for
1985    proper target support, then mark the method for aliasing.  */
1986
1987 static void
1988 java_mark_cni_decl_local (tree decl)
1989 {
1990 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
1991   return;
1992 #endif
1993
1994   DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1995   DECL_LOCAL_CNI_METHOD_P (decl) = 1;
1996
1997   /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the
1998      mangler.  We might have already referenced this native method and
1999      therefore created its name, but even if we have it won't hurt.
2000      We'll just go via its externally visible name, rather than its
2001      hidden alias.  However, we must force things so that the correct
2002      mangling is done.  */
2003
2004   if (DECL_ASSEMBLER_NAME_SET_P (decl))
2005     java_mangle_decl (decl);
2006   if (DECL_RTL_SET_P (decl))
2007     {
2008       SET_DECL_RTL (decl, 0);
2009       make_decl_rtl (decl);
2010     }
2011 }
2012
2013 /* Use the preceding two functions and mark all members of the class.  */
2014
2015 void
2016 java_mark_class_local (tree klass)
2017 {
2018   tree t;
2019
2020   for (t = TYPE_FIELDS (klass); t ; t = TREE_CHAIN (t))
2021     if (FIELD_STATIC (t))
2022       java_mark_decl_local (t);
2023
2024   for (t = TYPE_METHODS (klass); t ; t = TREE_CHAIN (t))
2025     if (!METHOD_ABSTRACT (t))
2026       {
2027         if (METHOD_NATIVE (t) && !flag_jni)
2028           java_mark_cni_decl_local (t);
2029         else
2030           java_mark_decl_local (t);
2031       }
2032 }
2033
2034 /* Add a statement to a compound_expr.  */
2035
2036 tree
2037 add_stmt_to_compound (tree existing, tree type, tree stmt)
2038 {
2039   if (!stmt)
2040     return existing;
2041   else if (existing)
2042     {
2043       tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2044       TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2045                                  | TREE_SIDE_EFFECTS (stmt);
2046       return expr;
2047     }
2048   else
2049     return stmt;
2050 }
2051
2052 /* If this node is an expr, mark its input location.  Called from
2053    walk_tree().  */
2054
2055 static tree
2056 set_input_location (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2057                     void *data ATTRIBUTE_UNUSED)
2058 {
2059   tree t = *tp;
2060
2061   if (CAN_HAVE_LOCATION_P (t))
2062     {
2063       if (EXPR_HAS_LOCATION(t))
2064         return t;  /* Don't walk any further into this expr.   */
2065       else
2066         SET_EXPR_LOCATION (t, input_location);
2067     }
2068
2069   return NULL_TREE;  /* Continue walking this expr.   */
2070 }
2071
2072 /* Add a statement to the statement_list currently being constructed.
2073    If the statement_list is null, we don't create a singleton list.
2074    This is necessary because poplevel() assumes that adding a
2075    statement to a null statement_list returns the statement.  */
2076
2077 tree
2078 java_add_stmt (tree new_stmt)
2079 {
2080   tree stmts = current_binding_level->stmts;
2081   tree_stmt_iterator i;
2082
2083   if (input_filename)
2084     walk_tree (&new_stmt, set_input_location, NULL, NULL);
2085
2086   if (stmts == NULL)
2087     return current_binding_level->stmts = new_stmt;
2088
2089   /* Force STMTS to be a statement_list.  */
2090   if (TREE_CODE (stmts) != STATEMENT_LIST)
2091     {
2092       tree t = make_node (STATEMENT_LIST);
2093       i = tsi_last (t);
2094       tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
2095       stmts = t;
2096     }  
2097       
2098   i = tsi_last (stmts);
2099   tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
2100   TREE_TYPE (stmts) = void_type_node;
2101
2102   return current_binding_level->stmts = stmts;
2103 }
2104
2105 /* Add a variable to the current scope.  */
2106
2107 tree
2108 java_add_local_var (tree decl)
2109 {
2110   tree *vars = &current_binding_level->names;
2111   tree next = *vars;
2112   TREE_CHAIN (decl) = next;
2113   *vars = decl;
2114   DECL_CONTEXT (decl) = current_function_decl;
2115   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2116   return decl;
2117 }
2118
2119 /* Return a pointer to the compound_expr currently being
2120    constructed.  */
2121
2122 tree *
2123 get_stmts (void)
2124 {
2125   return &current_binding_level->stmts;
2126 }
2127
2128 /* Register an exception range as belonging to the current binding
2129    level.  There may only be one: if there are more, we'll create more
2130    binding levels.  However, each range can have multiple handlers,
2131    and these are expanded when we call expand_end_java_handler().  */
2132
2133 void
2134 register_exception_range (struct eh_range *range, int pc, int end_pc)
2135 {
2136   gcc_assert (! current_binding_level->exception_range);
2137   current_binding_level->exception_range = range;
2138   current_binding_level->end_pc = end_pc;
2139   current_binding_level->start_pc = pc;      
2140 }
2141
2142 #include "gt-java-decl.h"