OSDN Git Service

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