OSDN Git Service

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