OSDN Git Service

* tree.h (BLOCK_TYPE_TAGS): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / java / decl.c
1 /* Process declarations and variables for the GNU compiler for the
2    Java(TM) language.
3
4    Copyright (C) 1996, 97-98, 1999 Free Software Foundation, Inc.
5
6 This file is part of GNU CC.
7
8 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
32 #include "java-tree.h"
33 #include "jcf.h"
34 #include "toplev.h"
35 #include "function.h"
36 #include "except.h"
37 #include "defaults.h"
38
39 static tree push_jvm_slot PROTO ((int, tree));
40 static tree lookup_name_current_level PROTO ((tree));
41 static tree push_promoted_type PROTO ((const char *, tree));
42 static struct binding_level *make_binding_level PROTO ((void));
43
44 #ifndef INT_TYPE_SIZE
45 #define INT_TYPE_SIZE BITS_PER_WORD
46 #endif
47
48 /* The DECL_MAP is a mapping from (index, type) to a decl node.
49    If index < max_locals, it is the index of a local variable.
50    if index >= max_locals, then index-max_locals is a stack slot.
51    The DECL_MAP mapping is represented as a TREE_VEC whose elements
52    are a list of decls (VAR_DECL or PARM_DECL) chained by
53    DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
54    we search the chain for a decl with a matching TREE_TYPE. */
55
56 tree decl_map;
57
58 /* A list of local variables VAR_DECLs for this method that we have seen
59    debug information, but we have not reached their starting (byte) PC yet. */
60
61 tree pending_local_decls = NULL_TREE;
62
63 /* Push a local variable or stack slot into the decl_map,
64    and assign it an rtl. */
65
66 static tree
67 push_jvm_slot (index, decl)
68      int index;
69      tree decl;
70 {
71   struct rtx_def *rtl = NULL;
72   tree type = TREE_TYPE (decl);
73   tree tmp;
74
75   DECL_CONTEXT (decl) = current_function_decl;
76   layout_decl (decl, 0);
77
78   /* See if we have an appropriate rtl (i.e. same mode) at this index.
79      If so, we must use it. */ 
80   tmp = TREE_VEC_ELT (decl_map, index);
81   while (tmp != NULL_TREE)
82     {
83       if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (tmp)))
84         rtl = DECL_RTL (tmp);
85       if (rtl != NULL)
86         break;
87      tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
88     }
89   if (rtl != NULL)
90     DECL_RTL (decl) = rtl;
91   else
92     {
93       if (index >= DECL_MAX_LOCALS (current_function_decl))
94         DECL_REGISTER (decl) = 1;
95       expand_decl (decl);
96     }
97
98   /* Now link the decl into the decl_map. */
99   if (DECL_LANG_SPECIFIC (decl) == NULL)
100     {
101       DECL_LANG_SPECIFIC (decl)
102         = (struct lang_decl *) permalloc (sizeof (struct lang_decl_var));
103       DECL_LOCAL_START_PC (decl) = 0;
104       DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
105       DECL_LOCAL_SLOT_NUMBER (decl) = index;
106     }
107   DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
108   TREE_VEC_ELT (decl_map, index) = decl;
109   return decl;
110 }
111
112 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
113    that is valid at PC (or -1 if any pc).
114    If there is no existing matching decl, allocate one.
115    If we find a decl with matching modes but different types,
116    we re-use the rtl, but create a new decl. */
117
118 tree
119 find_local_variable (index, type, pc)
120      int index;
121      tree type;
122      int pc;
123 {
124   tree decl = TREE_VEC_ELT (decl_map, index);
125   tree best = NULL_TREE;
126   while (decl != NULL_TREE)
127     {
128       int in_range;
129       in_range = pc < 0
130         || (pc >= DECL_LOCAL_START_PC (decl)
131             && pc < DECL_LOCAL_END_PC (decl));
132
133       if ((TREE_TYPE (decl) == type
134            || (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
135                && type == ptr_type_node))
136            && in_range)
137         {
138           if (best == NULL_TREE
139               || (TREE_TYPE (decl) == type && TREE_TYPE (best) != type)
140               || DECL_LOCAL_START_PC (decl) > DECL_LOCAL_START_PC (best)
141               || DECL_LOCAL_END_PC (decl) < DECL_LOCAL_START_PC (decl))
142             best = decl;
143         }
144       decl = DECL_LOCAL_SLOT_CHAIN (decl);
145     }
146   if (best != NULL_TREE)
147     return best;
148   return push_jvm_slot (index, build_decl (VAR_DECL, NULL_TREE, type));
149 }
150
151
152 /* Same as find_local_index, except that INDEX is a stack index. */
153
154 tree
155 find_stack_slot (index, type)
156      int index;
157      tree type;
158 {
159   return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
160                               type, -1);
161 }
162
163 struct binding_level
164   {
165     /* A chain of _DECL nodes for all variables, constants, functions,
166      * and typedef types.  These are in the reverse of the order supplied.
167      */
168     tree names;
169
170     /* For each level, a list of shadowed outer-level local definitions
171        to be restored when this level is popped.
172        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
173        whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
174     tree shadowed;
175
176     /* For each level (except not the global one),
177        a chain of BLOCK nodes for all the levels
178        that were entered and exited one level down.  */
179     tree blocks;
180
181     /* The BLOCK node for this level, if one has been preallocated.
182        If 0, the BLOCK is allocated (if needed) when the level is popped.  */
183     tree this_block;
184
185     /* The binding level which this one is contained in (inherits from).  */
186     struct binding_level *level_chain;
187
188     /* 1 means make a BLOCK for this level regardless of all else.
189        2 for temporary binding contours created by the compiler.  */
190     char keep;
191
192     /* Nonzero means make a BLOCK if this level has any subblocks.  */
193     char keep_if_subblocks;
194
195     /* Nonzero if this level can safely have additional
196        cleanup-needing variables added to it.  */
197     char more_cleanups_ok;
198     char have_cleanups;
199
200     /* The bytecode PC that marks the end of this level. */
201     int end_pc;
202   };
203
204 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
205
206 /* The binding level currently in effect.  */
207
208 static struct binding_level *current_binding_level;
209
210 /* A chain of binding_level structures awaiting reuse.  */
211
212 static struct binding_level *free_binding_level;
213
214 /* The outermost binding level, for names of file scope.
215    This is created when the compiler is started and exists
216    through the entire run.  */
217
218 static struct binding_level *global_binding_level;
219
220 /* Binding level structures are initialized by copying this one.  */
221
222 static struct binding_level clear_binding_level
223   = {NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
224        NULL_BINDING_LEVEL, 0, 0, 0, 0, 1000000000};
225
226 #if 0
227 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
228    that have names.  Here so we can clear out their names' definitions
229    at the end of the function.  */
230
231 static tree named_labels;
232
233 /* A list of LABEL_DECLs from outer contexts that are currently shadowed.  */
234
235 static tree shadowed_labels;
236 #endif
237
238 int flag_traditional;
239
240 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
241
242 static int keep_next_level_flag;
243
244 /* Nonzero means make a BLOCK for the next level pushed
245    if it has subblocks.  */
246
247 static int keep_next_if_subblocks;
248   
249 /* The FUNCTION_DECL for the function currently being compiled,
250    or 0 if between functions.  */
251 tree current_function_decl;
252
253 tree object_type_node;
254 tree unqualified_object_id_node;
255 tree object_ptr_type_node;
256 tree string_type_node;
257 tree string_ptr_type_node;
258 tree throwable_type_node;
259 tree runtime_exception_type_node;
260 tree error_exception_type_node;
261 tree *predef_filenames;
262 int  predef_filenames_size;
263
264 tree boolean_type_node;
265
266 tree return_address_type_node;
267
268 tree byte_type_node;
269 tree short_type_node;
270 tree int_type_node;
271 tree long_type_node;
272
273 tree promoted_byte_type_node;
274 tree promoted_short_type_node;
275 tree promoted_char_type_node;
276 tree promoted_boolean_type_node;
277
278 tree unsigned_byte_type_node;
279 tree unsigned_short_type_node;
280 tree unsigned_int_type_node;
281 tree unsigned_long_type_node;
282
283 /* The type for struct methodtable. */
284 tree methodtable_type;
285 tree methodtable_ptr_type;
286
287 tree utf8const_type;
288 tree utf8const_ptr_type;
289 tree class_type_node;
290 tree class_ptr_type;
291 tree field_type_node;
292 tree field_ptr_type_node;
293 tree field_info_union_node;
294 tree jexception_type;
295 tree jexception_ptr_type;
296 tree lineNumberEntry_type;
297 tree lineNumbers_type;
298 tree constants_type_node;
299 tree dtable_type;
300 tree dtable_ptr_type;
301 tree method_type_node;
302 tree method_ptr_type_node;
303 tree nativecode_ptr_array_type_node;
304 tree one_elt_array_domain_type;
305 tree access_flags_type_node;
306 tree class_dtable_decl;
307
308 /* Expressions that are constants with value zero, of types
309    `long', `float' and `double'.  */
310 tree long_zero_node;
311 tree float_zero_node;
312 tree double_zero_node;
313
314 tree empty_stmt_node;
315
316 /* Nodes for boolean constants TRUE and FALSE. */
317 tree boolean_true_node, boolean_false_node;
318
319 tree TYPE_identifier_node;
320 tree init_identifier_node;
321 tree clinit_identifier_node;
322 tree finit_identifier_node;
323 tree void_signature_node;
324 tree length_identifier_node;
325 tree this_identifier_node;
326 tree super_identifier_node;
327 tree continue_identifier_node;
328
329 tree end_params_node;
330
331 /* References to internal libjava functions we use. */
332 tree alloc_object_node;
333 tree soft_instanceof_node;
334 tree soft_checkcast_node;
335 tree soft_initclass_node;
336 tree soft_newarray_node;
337 tree soft_anewarray_node;
338 tree soft_multianewarray_node;
339 tree soft_badarrayindex_node;
340 tree throw_node;
341 tree soft_checkarraystore_node;
342 tree soft_monitorenter_node;
343 tree soft_monitorexit_node;
344 tree soft_lookupinterfacemethod_node;
345 tree soft_fmod_node;
346 tree soft_exceptioninfo_call_node;
347 tree soft_idiv_node;
348 tree soft_irem_node;
349 tree soft_ldiv_node;
350 tree soft_lrem_node;
351
352 /* Build (and pushdecl) a "promoted type" for all standard
353    types shorter than int.  */
354
355 static tree
356 push_promoted_type (name, actual_type)
357      const char *name;
358      tree actual_type;
359 {
360   tree type = make_node (TREE_CODE (actual_type));
361 #if 1
362   tree in_min = TYPE_MIN_VALUE (int_type_node);
363   tree in_max = TYPE_MAX_VALUE (int_type_node);
364 #else
365   tree in_min = TYPE_MIN_VALUE (actual_type);
366   tree in_max = TYPE_MAX_VALUE (actual_type);
367 #endif
368   TYPE_MIN_VALUE (type) = build_int_2 (TREE_INT_CST_LOW (in_min),
369                                        TREE_INT_CST_HIGH (in_min));
370   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
371   TYPE_MAX_VALUE (type) = build_int_2 (TREE_INT_CST_LOW (in_max),
372                                        TREE_INT_CST_HIGH (in_max));
373   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
374   TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
375   layout_type (type);
376   pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
377   return type;
378 }
379
380 /* Nodes for integer constants.  */
381 tree integer_two_node, integer_four_node;
382 tree integer_negative_one_node;
383
384 /* Return a definition for a builtin function named NAME and whose data type
385    is TYPE.  TYPE should be a function type with argument types.
386    FUNCTION_CODE tells later passes how to compile calls to this function.
387    See tree.h for its possible values.
388
389    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
390    the name to be called if we can't opencode the function.  */
391
392 tree
393 builtin_function (name, type, function_code, class, library_name)
394      const char *name;
395      tree type;
396      int function_code;
397      enum built_in_class class;
398      const char *library_name;
399 {
400   tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
401   DECL_EXTERNAL (decl) = 1;
402   TREE_PUBLIC (decl) = 1;
403   if (library_name)
404     DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
405   make_decl_rtl (decl, NULL_PTR, 1);
406   pushdecl (decl);
407   DECL_BUILT_IN_CLASS (decl) = class;
408   DECL_FUNCTION_CODE (decl) = function_code;
409   return decl;
410 }
411
412 void
413 init_decl_processing ()
414 {
415   register tree endlink;
416   tree field = NULL_TREE;
417   tree t;
418
419   current_function_decl = NULL;
420   current_binding_level = NULL_BINDING_LEVEL;
421   free_binding_level = NULL_BINDING_LEVEL;
422   pushlevel (0);        /* make the binding_level structure for global names */
423   global_binding_level = current_binding_level;
424
425   error_mark_node = make_node (ERROR_MARK);
426   TREE_TYPE (error_mark_node) = error_mark_node;
427
428   /* Create sizetype first - needed for other types. */
429   sizetype = make_unsigned_type (POINTER_SIZE);
430   size_zero_node = build_int_2 (0, 0);
431   TREE_TYPE (size_zero_node) = sizetype;
432   size_one_node = build_int_2 (1, 0);
433   TREE_TYPE (size_one_node) = sizetype;
434
435   byte_type_node = make_signed_type (8);
436   pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
437   short_type_node = make_signed_type (16);
438   pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
439   int_type_node = make_signed_type (32);
440   pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
441   long_type_node = make_signed_type (64);
442   pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
443
444   unsigned_byte_type_node = make_unsigned_type (8);
445   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
446                         unsigned_byte_type_node));
447   unsigned_short_type_node = make_unsigned_type (16);
448   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
449                         unsigned_short_type_node));
450   unsigned_int_type_node = make_unsigned_type (32);
451   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
452                         unsigned_int_type_node));
453   unsigned_long_type_node = make_unsigned_type (64);
454   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
455                         unsigned_long_type_node));
456
457   integer_type_node = type_for_size (INT_TYPE_SIZE, 0);
458
459   integer_zero_node = build_int_2 (0, 0);
460   integer_one_node = build_int_2 (1, 0);
461   integer_two_node = build_int_2 (2, 0);
462   integer_four_node = build_int_2 (4, 0);
463   integer_negative_one_node = build_int_2 (-1, 0);
464
465   long_zero_node = build_int_2 (0, 0);
466   TREE_TYPE (long_zero_node) = long_type_node;
467
468   void_type_node = make_node (VOID_TYPE);
469   pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
470   layout_type (void_type_node); /* Uses size_zero_node */
471   ptr_type_node = build_pointer_type (void_type_node);
472   t = make_node (VOID_TYPE);
473   layout_type (t); /* Uses size_zero_node */
474   return_address_type_node = build_pointer_type (t);
475
476   null_pointer_node = build_int_2 (0, 0);
477   TREE_TYPE (null_pointer_node) = ptr_type_node;
478
479   /* Used by the parser to represent empty statements and blocks. */
480   empty_stmt_node = build1 (NOP_EXPR, void_type_node, size_zero_node);
481   CAN_COMPLETE_NORMALLY (empty_stmt_node) = 1;
482
483 #if 0
484   /* Make a type to be the domain of a few array types
485      whose domains don't really matter.
486      200 is small enough that it always fits in size_t
487      and large enough that it can hold most function names for the
488      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
489   short_array_type_node = build_prim_array_type (short_type_node, 200);
490 #endif
491   char_type_node = make_node (CHAR_TYPE);
492   TYPE_PRECISION (char_type_node) = 16;
493   fixup_unsigned_type (char_type_node);
494   pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
495
496   boolean_type_node = make_node (BOOLEAN_TYPE);
497   TYPE_PRECISION (boolean_type_node) = 1;
498   fixup_unsigned_type (boolean_type_node);
499   pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
500                         boolean_type_node));
501   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
502   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
503
504   promoted_byte_type_node
505     = push_promoted_type ("promoted_byte", byte_type_node);
506   promoted_short_type_node
507     = push_promoted_type ("promoted_short", short_type_node);
508   promoted_char_type_node
509     = push_promoted_type ("promoted_char", char_type_node);
510   promoted_boolean_type_node
511     = push_promoted_type ("promoted_boolean", boolean_type_node);
512
513   float_type_node = make_node (REAL_TYPE);
514   TYPE_PRECISION (float_type_node) = 32;
515   pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
516                         float_type_node));
517   layout_type (float_type_node);
518
519   double_type_node = make_node (REAL_TYPE);
520   TYPE_PRECISION (double_type_node) = 64;
521   pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
522                         double_type_node));
523   layout_type (double_type_node);
524
525   float_zero_node = build_real (float_type_node, dconst0);
526   double_zero_node = build_real (double_type_node, dconst0);
527
528   /* As your adding items here, please update the code right after
529      this section, so that the filename containing the source code of
530      the pre-defined class gets registered correctly. */
531   unqualified_object_id_node = get_identifier ("Object");
532   object_type_node = lookup_class (get_identifier ("java.lang.Object"));
533   object_ptr_type_node = promote_type (object_type_node);
534   string_type_node = lookup_class (get_identifier ("java.lang.String"));
535   string_ptr_type_node = promote_type (string_type_node);
536   class_type_node = lookup_class (get_identifier ("java.lang.Class"));
537   throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
538   runtime_exception_type_node = 
539     lookup_class (get_identifier ("java.lang.RuntimeException"));
540   error_exception_type_node = 
541     lookup_class (get_identifier ("java.lang.Error"));
542
543   /* This section has to be updated as items are added to the previous
544      section. */
545   predef_filenames_size = 6;
546   predef_filenames = (tree *)xmalloc (predef_filenames_size * sizeof (tree));
547   predef_filenames [0] = get_identifier ("java/lang/Class.java");
548   predef_filenames [1] = get_identifier ("java/lang/Error.java");
549   predef_filenames [2] = get_identifier ("java/lang/Object.java");
550   predef_filenames [3] = get_identifier ("java/lang/RuntimeException.java");
551   predef_filenames [4] = get_identifier ("java/lang/String.java");
552   predef_filenames [5] = get_identifier ("java/lang/Throwable.java");
553
554   methodtable_type = make_node (RECORD_TYPE);
555   layout_type (methodtable_type);
556   build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
557   methodtable_ptr_type = build_pointer_type (methodtable_type);
558
559   TYPE_identifier_node = get_identifier ("TYPE");
560   init_identifier_node = get_identifier ("<init>");
561   clinit_identifier_node = get_identifier ("<clinit>");
562   finit_identifier_node = get_identifier ("$finit$");
563   void_signature_node = get_identifier ("()V");
564   length_identifier_node = get_identifier ("length");
565   this_identifier_node = get_identifier ("this");
566   super_identifier_node = get_identifier ("super");
567   continue_identifier_node = get_identifier ("continue");
568
569   /* for lack of a better place to put this stub call */
570   init_expr_processing();
571
572   utf8const_type = make_node (RECORD_TYPE);
573   PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
574   PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
575   FINISH_RECORD (utf8const_type);
576   utf8const_ptr_type = build_pointer_type (utf8const_type);
577
578   constants_type_node = make_node (RECORD_TYPE);
579   PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
580   PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
581   PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
582   FINISH_RECORD (constants_type_node);
583   build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
584
585   access_flags_type_node = unsigned_short_type_node;
586
587   dtable_type = make_node (RECORD_TYPE);
588   dtable_ptr_type = build_pointer_type (dtable_type);
589
590   PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
591   PUSH_FIELD (object_type_node, field, "sync_info", ptr_type_node);
592   for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
593     FIELD_PRIVATE (t) = 1;
594   FINISH_RECORD (object_type_node);
595
596   class_dtable_decl = build_dtable_decl (class_type_node);
597   TREE_STATIC (class_dtable_decl) = 1;
598   DECL_ARTIFICIAL (class_dtable_decl) = 1;
599   DECL_IGNORED_P (class_dtable_decl) = 1;
600   rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
601
602   field_type_node = make_node (RECORD_TYPE);
603   field_ptr_type_node = build_pointer_type (field_type_node);
604   method_type_node = make_node (RECORD_TYPE);
605   method_ptr_type_node = build_pointer_type (method_type_node);
606
607   set_super_info (0, class_type_node, object_type_node, 0);
608   set_super_info (0, string_type_node, object_type_node, 0);
609   class_ptr_type = build_pointer_type (class_type_node);
610
611   PUSH_FIELD (class_type_node, field, "next", class_ptr_type);
612   PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
613   PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
614   PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
615   PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
616   PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
617   PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
618   PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
619   PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
620   PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
621   PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
622   PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
623   PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
624   PUSH_FIELD (class_type_node, field, "interfaces",
625               build_pointer_type (class_ptr_type));
626   PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
627   PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
628   PUSH_FIELD (class_type_node, field, "state", byte_type_node);
629   PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
630   for (t = TYPE_FIELDS (class_type_node);  t != NULL_TREE;  t = TREE_CHAIN (t))
631     FIELD_PRIVATE (t) = 1;
632   push_super_field (class_type_node, object_type_node);
633   FINISH_RECORD (class_type_node);
634   build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
635
636   field_info_union_node = make_node (UNION_TYPE);
637   PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
638   PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
639 #if 0
640   PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
641 #endif
642   layout_type (field_info_union_node);
643
644   PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
645   PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
646   PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
647   PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
648   PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
649   FINISH_RECORD (field_type_node);
650   CLASS_LOADED_P (field_type_node) = 1;
651   build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
652
653   one_elt_array_domain_type = build_index_type (integer_one_node);
654   nativecode_ptr_array_type_node
655     = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
656
657   PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
658   PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
659   FINISH_RECORD (dtable_type);
660   build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
661
662 #define jint_type int_type_node
663 #define jint_ptr_type ptr_type_node
664
665   jexception_type = make_node (RECORD_TYPE);
666   PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
667   PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
668   PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
669   PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
670   FINISH_RECORD (jexception_type);
671   build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
672   jexception_ptr_type = build_pointer_type (jexception_type);
673
674   lineNumberEntry_type = make_node (RECORD_TYPE);
675   PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
676   PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
677   FINISH_RECORD (lineNumberEntry_type);
678
679   lineNumbers_type = make_node (RECORD_TYPE);
680   PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
681   FINISH_RECORD (lineNumbers_type);
682
683 #define instn_ptr_type_node ptr_type_node       /* XXX JH */
684
685 #define lineNumbers_ptr_type_node build_pointer_type(lineNumbers_type)
686
687   PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
688   PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
689   PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
690   PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
691   FINISH_RECORD (method_type_node);
692   CLASS_LOADED_P (method_type_node) = 1;
693   build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
694
695   endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
696
697   t = tree_cons (NULL_TREE, class_ptr_type,
698                  tree_cons (NULL_TREE, int_type_node, endlink));
699   alloc_object_node = builtin_function ("_Jv_AllocObject",
700                                         build_function_type (ptr_type_node, t),
701                                         0, NOT_BUILT_IN, NULL_PTR);
702   soft_initclass_node = builtin_function ("_Jv_InitClass",
703                                           build_function_type (void_type_node,
704                                                                t),
705                                           0, NOT_BUILT_IN,
706                                           NULL_PTR);
707   t = tree_cons (NULL_TREE, ptr_type_node, endlink);
708   throw_node = builtin_function ("_Jv_Throw",
709                                  build_function_type (ptr_type_node, t),
710                                  0, NOT_BUILT_IN, NULL_PTR);
711   t = build_function_type (int_type_node, endlink);
712   soft_monitorenter_node 
713     = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
714                         NULL_PTR);
715   soft_monitorexit_node 
716     = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
717                         NULL_PTR);
718   
719   t = tree_cons (NULL_TREE, int_type_node, 
720                  tree_cons (NULL_TREE, int_type_node, endlink));
721   soft_newarray_node
722       = builtin_function ("_Jv_NewArray",
723                           build_function_type(ptr_type_node, t),
724                           0, NOT_BUILT_IN, NULL_PTR);
725
726   t = tree_cons (NULL_TREE, int_type_node,
727                  tree_cons (NULL_TREE, class_ptr_type,
728                             tree_cons (NULL_TREE, object_ptr_type_node, endlink)));
729   soft_anewarray_node
730       = builtin_function ("_Jv_NewObjectArray",
731                           build_function_type (ptr_type_node, t),
732                           0, NOT_BUILT_IN, NULL_PTR);
733
734   t = tree_cons (NULL_TREE, ptr_type_node,
735                  tree_cons (NULL_TREE, int_type_node, endlink));
736   soft_multianewarray_node
737       = builtin_function ("_Jv_NewMultiArray",
738                           build_function_type (ptr_type_node, t),
739                           0, NOT_BUILT_IN, NULL_PTR);
740
741   t = build_function_type (void_type_node, 
742                            tree_cons (NULL_TREE, int_type_node, endlink));
743   soft_badarrayindex_node
744       = builtin_function ("_Jv_ThrowBadArrayIndex", t, 
745                           0, NOT_BUILT_IN, NULL_PTR);
746   TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
747   TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
748
749   t = tree_cons (NULL_TREE, class_ptr_type,
750                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
751   soft_checkcast_node
752     = builtin_function ("_Jv_CheckCast",
753                         build_function_type (ptr_type_node, t),
754                         0, NOT_BUILT_IN, NULL_PTR);
755   t = tree_cons (NULL_TREE, object_ptr_type_node,
756                  tree_cons (NULL_TREE, class_ptr_type, endlink));
757   soft_instanceof_node
758     = builtin_function ("_Jv_IsInstanceOf",
759                         build_function_type (promoted_boolean_type_node, t),
760                         0, NOT_BUILT_IN, NULL_PTR);
761   t = tree_cons (NULL_TREE, object_ptr_type_node,
762                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
763   soft_checkarraystore_node
764     = builtin_function ("_Jv_CheckArrayStore",
765                         build_function_type (void_type_node, t),
766                         0, NOT_BUILT_IN, NULL_PTR);
767   t = tree_cons (NULL_TREE, ptr_type_node,
768                  tree_cons (NULL_TREE, ptr_type_node,
769                             tree_cons (NULL_TREE, ptr_type_node, endlink)));
770   soft_lookupinterfacemethod_node 
771     = builtin_function ("_Jv_LookupInterfaceMethod",
772                         build_function_type (ptr_type_node, t),
773                         0, NOT_BUILT_IN, NULL_PTR);
774   t = tree_cons (NULL_TREE, double_type_node,
775                  tree_cons (NULL_TREE, double_type_node, endlink));
776   soft_fmod_node
777     = builtin_function ("__builtin_fmod",
778                         build_function_type (double_type_node, t),
779                         BUILT_IN_FMOD, BUILT_IN_NORMAL, "fmod");
780
781   soft_exceptioninfo_call_node
782     = build (CALL_EXPR, 
783              ptr_type_node,
784              build_address_of 
785                (builtin_function ("_Jv_exception_info", 
786                                   build_function_type (ptr_type_node, endlink),
787                                   0, NOT_BUILT_IN, NULL_PTR)),
788              NULL_TREE, NULL_TREE);
789   TREE_SIDE_EFFECTS (soft_exceptioninfo_call_node) = 1;
790 #if 0
791   t = tree_cons (NULL_TREE, float_type_node,
792                  tree_cons (NULL_TREE, float_type_node, endlink));
793   soft_fmodf_node
794     = builtin_function ("__builtin_fmodf",
795                         build_function_type (float_type_node, t),
796                         BUILT_IN_FMOD, BUILT_IN_NORMAL, "fmodf");
797 #endif
798     
799   soft_idiv_node
800     = builtin_function ("_Jv_divI",
801                         build_function_type (int_type_node, t),
802                         0, NOT_BUILT_IN, NULL_PTR);
803
804   soft_irem_node
805     = builtin_function ("_Jv_remI",
806                         build_function_type (int_type_node, t),
807                         0, NOT_BUILT_IN, NULL_PTR);
808
809   soft_ldiv_node
810     = builtin_function ("_Jv_divJ",
811                         build_function_type (long_type_node, t),
812                         0, NOT_BUILT_IN, NULL_PTR);
813
814   soft_lrem_node
815     = builtin_function ("_Jv_remJ",
816                         build_function_type (long_type_node, t),
817                         0, NOT_BUILT_IN, NULL_PTR);
818
819   init_class_processing ();
820 }
821
822
823 /* Look up NAME in the current binding level and its superiors
824    in the namespace of variables, functions and typedefs.
825    Return a ..._DECL node of some kind representing its definition,
826    or return 0 if it is undefined.  */
827
828 tree
829 lookup_name (name)
830      tree name;
831 {
832   register tree val;
833   if (current_binding_level != global_binding_level
834       && IDENTIFIER_LOCAL_VALUE (name))
835     val = IDENTIFIER_LOCAL_VALUE (name);
836   else
837     val = IDENTIFIER_GLOBAL_VALUE (name);
838   return val;
839 }
840
841 /* Similar to `lookup_name' but look only at current binding level and
842    the previous one if its the parameter level.  */
843
844 static tree
845 lookup_name_current_level (name)
846      tree name;
847 {
848   register tree t;
849
850   if (current_binding_level == global_binding_level)
851     return IDENTIFIER_GLOBAL_VALUE (name);
852
853   if (IDENTIFIER_LOCAL_VALUE (name) == 0)
854     return 0;
855
856   for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
857     if (DECL_NAME (t) == name)
858       break;
859
860   return t;
861 }
862
863 /* Use a binding level to record a labeled block declaration */
864
865 void
866 push_labeled_block (lb)
867     tree lb;
868 {
869   register tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
870   register struct binding_level *b = current_binding_level;
871   tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
872   if (oldlocal != 0)
873       b->shadowed = tree_cons (name, oldlocal, b->shadowed);
874   TREE_CHAIN (lb) = b->names;
875   b->names = lb;
876   IDENTIFIER_LOCAL_VALUE (name) = lb;
877 }
878
879 /* Pop the current binding level, reinstalling values for the previous
880    labeled block */
881
882 void
883 pop_labeled_block ()
884 {
885   struct binding_level *b = current_binding_level;
886   tree label =  b->names;
887   IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) = 
888     NULL_TREE;
889   if (b->shadowed)
890     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) = 
891       TREE_VALUE (b->shadowed);
892
893   /* Pop the current level, and free the structure for reuse.  */
894   current_binding_level = current_binding_level->level_chain;
895   b->level_chain = free_binding_level;
896   free_binding_level = b;
897 }
898
899 /* Record a decl-node X as belonging to the current lexical scope.
900    Check for errors (such as an incompatible declaration for the same
901    name already seen in the same scope).
902
903    Returns either X or an old decl for the same name.
904    If an old decl is returned, it may have been smashed
905    to agree with what X says.  */
906
907 tree
908 pushdecl (x)
909      tree x;
910 {
911   register tree t;
912   register tree name = DECL_NAME (x);
913   register struct binding_level *b = current_binding_level;
914
915   DECL_CONTEXT (x) = current_function_decl;
916   if (name)
917     {
918       const char *file;
919       int line;
920
921       t = lookup_name_current_level (name);
922       if (t != 0 && t == error_mark_node)
923         /* error_mark_node is 0 for a while during initialization!  */
924         {
925           t = 0;
926           error_with_decl (x, "`%s' used prior to declaration");
927         }
928
929       if (t != 0)
930         {
931           file = DECL_SOURCE_FILE (t);
932           line = DECL_SOURCE_LINE (t);
933         }
934
935       /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
936          to point to the TYPE_DECL.
937          Since Java does not have typedefs, a type can only have
938          one (true) name, given by a class, interface, or builtin. */
939       if (TREE_CODE (x) == TYPE_DECL
940           && TYPE_NAME (TREE_TYPE (x)) == 0
941           && TREE_TYPE (x) != error_mark_node)
942         {
943           TYPE_NAME (TREE_TYPE (x)) = x;
944           TYPE_STUB_DECL (TREE_TYPE (x)) = x;
945         }
946
947       /* This name is new in its binding level.
948          Install the new declaration and return it.  */
949       if (b == global_binding_level)
950         {
951           /* Install a global value.  */
952           
953           IDENTIFIER_GLOBAL_VALUE (name) = x;
954         }
955       else
956         {
957           /* Here to install a non-global value.  */
958           tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
959           IDENTIFIER_LOCAL_VALUE (name) = x;
960
961 #if 0
962           /* Warn if shadowing an argument at the top level of the body.  */
963           if (oldlocal != 0 && !DECL_EXTERNAL (x)
964               /* This warning doesn't apply to the parms of a nested fcn.  */
965               && ! current_binding_level->parm_flag
966               /* Check that this is one level down from the parms.  */
967               && current_binding_level->level_chain->parm_flag
968               /* Check that the decl being shadowed
969                  comes from the parm level, one level up.  */
970               && chain_member (oldlocal, current_binding_level->level_chain->names))
971             {
972               if (TREE_CODE (oldlocal) == PARM_DECL)
973                 pedwarn ("declaration of `%s' shadows a parameter",
974                          IDENTIFIER_POINTER (name));
975               else
976                 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
977                          IDENTIFIER_POINTER (name));
978             }
979
980           /* Maybe warn if shadowing something else.  */
981           else if (warn_shadow && !DECL_EXTERNAL (x)
982                    /* No shadow warnings for internally generated vars.  */
983                    && DECL_SOURCE_LINE (x) != 0
984                    /* No shadow warnings for vars made for inlining.  */
985                    && ! DECL_FROM_INLINE (x))
986             {
987               const char *warnstring = 0;
988
989               if (TREE_CODE (x) == PARM_DECL
990                   && current_binding_level->level_chain->parm_flag)
991                 /* Don't warn about the parm names in function declarator
992                    within a function declarator.
993                    It would be nice to avoid warning in any function
994                    declarator in a declaration, as opposed to a definition,
995                    but there is no way to tell it's not a definition.  */
996                 ;
997               else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
998                 warnstring = "declaration of `%s' shadows a parameter";
999               else if (oldlocal != 0)
1000                 warnstring = "declaration of `%s' shadows previous local";
1001               else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1002                        && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1003                 warnstring = "declaration of `%s' shadows global declaration";
1004
1005               if (warnstring)
1006                 warning (warnstring, IDENTIFIER_POINTER (name));
1007             }
1008 #endif
1009
1010           /* If storing a local value, there may already be one (inherited).
1011              If so, record it for restoration when this binding level ends.  */
1012           if (oldlocal != 0)
1013             b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1014         }
1015     }
1016
1017   /* Put decls on list in reverse order.
1018      We will reverse them later if necessary.  */
1019   TREE_CHAIN (x) = b->names;
1020   b->names = x;
1021
1022   return x;
1023 }
1024 void
1025 pushdecl_force_head (x)
1026      tree x;
1027 {
1028   current_binding_level->names = x;
1029 }
1030
1031 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate.  */
1032
1033 tree
1034 pushdecl_top_level (x)
1035      tree x;
1036 {
1037   register tree t;
1038   register struct binding_level *b = current_binding_level;
1039
1040   current_binding_level = global_binding_level;
1041   t = pushdecl (x);
1042   current_binding_level = b;
1043   return t;
1044 }
1045
1046 /* Nonzero if we are currently in the global binding level.  */
1047
1048 int
1049 global_bindings_p ()
1050 {
1051   return current_binding_level == global_binding_level;
1052 }
1053
1054 /* Return the list of declarations of the current level.
1055    Note that this list is in reverse order unless/until
1056    you nreverse it; and when you do nreverse it, you must
1057    store the result back using `storedecls' or you will lose.  */
1058
1059 tree
1060 getdecls ()
1061 {
1062   return current_binding_level->names;
1063 }
1064
1065 /* Create a new `struct binding_level'.  */
1066
1067 static
1068 struct binding_level *
1069 make_binding_level ()
1070 {
1071   /* NOSTRICT */
1072   return (struct binding_level *) xmalloc (sizeof (struct binding_level));
1073 }
1074
1075 void
1076 pushlevel (unused)
1077   int unused ATTRIBUTE_UNUSED;
1078 {
1079   register struct binding_level *newlevel = NULL_BINDING_LEVEL;
1080
1081 #if 0
1082   /* If this is the top level of a function,
1083      just make sure that NAMED_LABELS is 0.  */
1084
1085   if (current_binding_level == global_binding_level)
1086     named_labels = 0;
1087 #endif
1088
1089   /* Reuse or create a struct for this binding level.  */
1090
1091   if (free_binding_level)
1092     {
1093       newlevel = free_binding_level;
1094       free_binding_level = free_binding_level->level_chain;
1095     }
1096   else
1097     {
1098       newlevel = make_binding_level ();
1099     }
1100
1101   /* Add this level to the front of the chain (stack) of levels that
1102      are active.  */
1103
1104   *newlevel = clear_binding_level;
1105   newlevel->level_chain = current_binding_level;
1106   current_binding_level = newlevel;
1107   newlevel->keep = keep_next_level_flag;
1108   keep_next_level_flag = 0;
1109   newlevel->keep_if_subblocks = keep_next_if_subblocks;
1110   keep_next_if_subblocks = 0;
1111 }
1112
1113 /* Exit a binding level.
1114    Pop the level off, and restore the state of the identifier-decl mappings
1115    that were in effect when this level was entered.
1116
1117    If KEEP is nonzero, this level had explicit declarations, so
1118    and create a "block" (a BLOCK node) for the level
1119    to record its declarations and subblocks for symbol table output.
1120
1121    If FUNCTIONBODY is nonzero, this level is the body of a function,
1122    so create a block as if KEEP were set and also clear out all
1123    label names.
1124
1125    If REVERSE is nonzero, reverse the order of decls before putting
1126    them into the BLOCK.  */
1127
1128 tree
1129 poplevel (keep, reverse, functionbody)
1130      int keep;
1131      int reverse;
1132      int functionbody;
1133 {
1134   register tree link;
1135   /* The chain of decls was accumulated in reverse order.
1136      Put it into forward order, just for cleanliness.  */
1137   tree decls;
1138   tree subblocks = current_binding_level->blocks;
1139   tree block = 0;
1140   tree decl;
1141   int block_previously_created;
1142
1143   keep |= current_binding_level->keep;
1144
1145   /* Get the decls in the order they were written.
1146      Usually current_binding_level->names is in reverse order.
1147      But parameter decls were previously put in forward order.  */
1148
1149   if (reverse)
1150     current_binding_level->names
1151       = decls = nreverse (current_binding_level->names);
1152   else
1153     decls = current_binding_level->names;
1154
1155   /* Output any nested inline functions within this block
1156      if they weren't already output.  */
1157
1158   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1159     if (TREE_CODE (decl) == FUNCTION_DECL
1160         && ! TREE_ASM_WRITTEN (decl)
1161         && DECL_INITIAL (decl) != 0
1162         && TREE_ADDRESSABLE (decl))
1163       {
1164         /* If this decl was copied from a file-scope decl
1165            on account of a block-scope extern decl,
1166            propagate TREE_ADDRESSABLE to the file-scope decl.
1167
1168            DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
1169            true, since then the decl goes through save_for_inline_copying.  */
1170         if (DECL_ABSTRACT_ORIGIN (decl) != 0
1171             && DECL_ABSTRACT_ORIGIN (decl) != decl)
1172           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1173         else
1174           {
1175             push_function_context ();
1176             output_inline_function (decl);
1177             pop_function_context ();
1178           }
1179       }
1180
1181   /* If there were any declarations in that level,
1182      or if this level is a function body,
1183      create a BLOCK to record them for the life of this function.  */
1184
1185   block = 0;
1186   block_previously_created = (current_binding_level->this_block != 0);
1187   if (block_previously_created)
1188     block = current_binding_level->this_block;
1189   else if (keep || functionbody
1190            || (current_binding_level->keep_if_subblocks && subblocks != 0))
1191     block = make_node (BLOCK);
1192   if (block != 0)
1193     {
1194       BLOCK_VARS (block) = decls;
1195       BLOCK_SUBBLOCKS (block) = subblocks;
1196     }
1197
1198   /* In each subblock, record that this is its superior.  */
1199
1200   for (link = subblocks; link; link = TREE_CHAIN (link))
1201     BLOCK_SUPERCONTEXT (link) = block;
1202
1203   /* Clear out the meanings of the local variables of this level.  */
1204
1205   for (link = decls; link; link = TREE_CHAIN (link))
1206     {
1207       tree name = DECL_NAME (link);
1208       if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1209         {
1210           /* If the ident. was used or addressed via a local extern decl,
1211              don't forget that fact.  */
1212           if (DECL_EXTERNAL (link))
1213             {
1214               if (TREE_USED (link))
1215                 TREE_USED (name) = 1;
1216               if (TREE_ADDRESSABLE (link))
1217                 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1218             }
1219           IDENTIFIER_LOCAL_VALUE (name) = 0;
1220         }
1221     }
1222
1223   /* Restore all name-meanings of the outer levels
1224      that were shadowed by this level.  */
1225
1226   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1227     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1228
1229   /* If the level being exited is the top level of a function,
1230      check over all the labels, and clear out the current
1231      (function local) meanings of their names.  */
1232
1233   if (functionbody)
1234     {
1235       /* If this is the top level block of a function,
1236          the vars are the function's parameters.
1237          Don't leave them in the BLOCK because they are
1238          found in the FUNCTION_DECL instead.  */
1239
1240       BLOCK_VARS (block) = 0;
1241
1242       /* Clear out the definitions of all label names,
1243          since their scopes end here,
1244          and add them to BLOCK_VARS.  */
1245
1246 #if 0
1247       for (link = named_labels; link; link = TREE_CHAIN (link))
1248         {
1249           register tree label = TREE_VALUE (link);
1250
1251           if (DECL_INITIAL (label) == 0)
1252             {
1253               error_with_decl (label, "label `%s' used but not defined");
1254               /* Avoid crashing later.  */
1255               define_label (input_filename, lineno,
1256                             DECL_NAME (label));
1257             }
1258           else if (warn_unused && !TREE_USED (label))
1259             warning_with_decl (label, "label `%s' defined but not used");
1260           IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1261
1262           /* Put the labels into the "variables" of the
1263              top-level block, so debugger can see them.  */
1264           TREE_CHAIN (label) = BLOCK_VARS (block);
1265           BLOCK_VARS (block) = label;
1266         }
1267 #endif
1268     }
1269
1270   /* Pop the current level, and free the structure for reuse.  */
1271
1272   {
1273     register struct binding_level *level = current_binding_level;
1274     current_binding_level = current_binding_level->level_chain;
1275
1276     level->level_chain = free_binding_level;
1277     free_binding_level = level;
1278   }
1279
1280   /* Dispose of the block that we just made inside some higher level.  */
1281   if (functionbody)
1282     DECL_INITIAL (current_function_decl) = block;
1283   else if (block)
1284     {
1285       if (!block_previously_created)
1286         current_binding_level->blocks
1287           = chainon (current_binding_level->blocks, block);
1288     }
1289   /* If we did not make a block for the level just exited,
1290      any blocks made for inner levels
1291      (since they cannot be recorded as subblocks in that level)
1292      must be carried forward so they will later become subblocks
1293      of something else.  */
1294   else if (subblocks)
1295     current_binding_level->blocks
1296       = chainon (current_binding_level->blocks, subblocks);
1297
1298   /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1299      binding contour so that they point to the appropriate construct, i.e.
1300      either to the current FUNCTION_DECL node, or else to the BLOCK node
1301      we just constructed.
1302
1303      Note that for tagged types whose scope is just the formal parameter
1304      list for some function type specification, we can't properly set
1305      their TYPE_CONTEXTs here, because we don't have a pointer to the
1306      appropriate FUNCTION_TYPE node readily available to us.  For those
1307      cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1308      in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1309      node which will represent the "scope" for these "parameter list local"
1310      tagged types.
1311   */
1312
1313   if (block)
1314     TREE_USED (block) = 1;
1315   return block;
1316 }
1317
1318 void
1319 maybe_pushlevels (pc)
1320      int pc;
1321 {
1322   while (pending_local_decls != NULL_TREE &&
1323          DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1324     {
1325       tree *ptr = &pending_local_decls;
1326       tree decl = *ptr;
1327       int end_pc = DECL_LOCAL_END_PC (decl);
1328
1329       while (*ptr != NULL_TREE
1330              && DECL_LOCAL_START_PC (*ptr) <= pc
1331              && DECL_LOCAL_END_PC (*ptr) == end_pc)
1332         ptr = &TREE_CHAIN (*ptr);
1333       pending_local_decls = *ptr;
1334       *ptr = NULL_TREE;
1335
1336       /* Force non-nested range to be nested in current range. */
1337       if (end_pc > current_binding_level->end_pc)
1338         end_pc = current_binding_level->end_pc;
1339
1340       pushlevel (1);
1341       expand_start_bindings (0);
1342       current_binding_level->end_pc = end_pc;
1343       
1344       current_binding_level->names = decl;
1345       for ( ; decl != NULL_TREE;  decl = TREE_CHAIN (decl))
1346         {
1347           push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1348         }
1349     }
1350 }
1351
1352 void
1353 maybe_poplevels (pc)
1354      int pc;
1355 {
1356   while (current_binding_level->end_pc <= pc)
1357     {
1358       expand_end_bindings (getdecls (), 1, 0);
1359       poplevel (1, 0, 0);
1360     }
1361 }
1362
1363 /* Insert BLOCK at the end of the list of subblocks of the
1364    current binding level.  This is used when a BIND_EXPR is expanded,
1365    to handle the BLOCK node inside the BIND_EXPR.  */
1366
1367 void
1368 insert_block (block)
1369      tree block;
1370 {
1371   TREE_USED (block) = 1;
1372   current_binding_level->blocks
1373     = chainon (current_binding_level->blocks, block);
1374 }
1375
1376 /* Set the BLOCK node for the innermost scope
1377    (the one we are currently in).  */
1378
1379 void
1380 set_block (block)
1381      register tree block;
1382 {
1383   current_binding_level->this_block = block;
1384 }
1385
1386 /* integrate_decl_tree calls this function. */
1387
1388 void
1389 copy_lang_decl (node)
1390      tree node;
1391 {
1392   int lang_decl_size
1393     = TREE_CODE (node) == VAR_DECL ? sizeof (struct lang_decl_var)
1394     : sizeof (struct lang_decl);
1395   struct lang_decl *x = (struct lang_decl *) oballoc (lang_decl_size);
1396   bcopy ((PTR) DECL_LANG_SPECIFIC (node), (PTR) x, lang_decl_size);
1397   DECL_LANG_SPECIFIC (node) = x;
1398 }
1399
1400 /* If DECL has a cleanup, build and return that cleanup here.
1401    This is a callback called by expand_expr.  */
1402
1403 tree
1404 maybe_build_cleanup (decl)
1405   tree decl ATTRIBUTE_UNUSED;
1406 {
1407   /* There are no cleanups in Java (I think).  */
1408   return NULL_TREE;
1409 }
1410
1411 void
1412 give_name_to_locals (jcf)
1413      JCF *jcf;
1414 {
1415   int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1416   tree parm;
1417   pending_local_decls = NULL_TREE;
1418   if (n == 0)
1419     return;
1420   JCF_SEEK (jcf, n);
1421   n = JCF_readu2 (jcf);
1422   for (i = 0; i < n; i++)
1423     {
1424       int start_pc = JCF_readu2 (jcf);
1425       int length = JCF_readu2 (jcf);
1426       int name_index = JCF_readu2 (jcf);
1427       int signature_index = JCF_readu2 (jcf);
1428       int slot = JCF_readu2 (jcf);
1429       tree name = get_name_constant (jcf, name_index);
1430       tree type = parse_signature (jcf, signature_index);
1431       if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1432           && start_pc == 0
1433           && length == DECL_CODE_LENGTH (current_function_decl))
1434         {
1435           tree decl = TREE_VEC_ELT (decl_map, slot);
1436           DECL_NAME (decl) = name;
1437           DECL_ASSEMBLER_NAME (decl) = name;
1438           if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1439             warning ("bad type in parameter debug info");
1440         }
1441       else
1442         {
1443           tree *ptr;
1444           int end_pc = start_pc + length;
1445           tree decl = build_decl (VAR_DECL, name, type);
1446           if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1447             {
1448               warning_with_decl (decl,
1449                          "bad PC range for debug info for local `%s'");
1450               end_pc = DECL_CODE_LENGTH (current_function_decl);
1451             }
1452           DECL_LANG_SPECIFIC (decl)
1453             = (struct lang_decl *) permalloc (sizeof (struct lang_decl_var));
1454           DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1455           DECL_LOCAL_START_PC (decl) = start_pc;
1456           DECL_LOCAL_END_PC (decl) = end_pc;
1457
1458           /* Now insert the new decl in the proper place in
1459              pending_local_decls.  We are essentially doing an insertion sort,
1460              which works fine, since the list input will normally already
1461              be sorted. */
1462           ptr = &pending_local_decls;
1463           while (*ptr != NULL_TREE
1464                  && (DECL_LOCAL_START_PC (*ptr) > start_pc
1465                      || (DECL_LOCAL_START_PC (*ptr) == start_pc
1466                          && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1467             ptr = &TREE_CHAIN (*ptr);
1468           TREE_CHAIN (decl) = *ptr;
1469           *ptr = decl;
1470         }
1471     }
1472
1473   pending_local_decls = nreverse (pending_local_decls);
1474
1475   /* Fill in default names for the parameters. */ 
1476   for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1477        parm != NULL_TREE;  parm = TREE_CHAIN (parm), i++)
1478     {
1479       if (DECL_NAME (parm) == NULL_TREE)
1480         {
1481           int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1482           if (arg_i == 0)
1483             DECL_NAME (parm) = get_identifier ("this");
1484           else
1485             {
1486               char buffer[12];
1487               sprintf (buffer, "ARG_%d", arg_i);
1488               DECL_NAME (parm) = get_identifier (buffer);
1489             }
1490           DECL_ASSEMBLER_NAME (parm) = DECL_NAME (parm);
1491         }
1492     }
1493 }
1494
1495 tree
1496 build_result_decl (fndecl)
1497   tree fndecl;
1498 {
1499   tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1500   /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1501   if (INTEGRAL_TYPE_P (restype)
1502       && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1503     restype = integer_type_node;
1504   return (DECL_RESULT (fndecl) = build_decl (RESULT_DECL, NULL_TREE, restype));
1505 }
1506
1507 void
1508 complete_start_java_method (fndecl)
1509   tree fndecl;
1510 {
1511   if (! flag_emit_class_files)
1512     {
1513       /* Initialize the RTL code for the function.  */
1514       init_function_start (fndecl, input_filename, lineno);
1515
1516       /* Set up parameters and prepare for return, for the function.  */
1517       expand_function_start (fndecl, 0);
1518     }
1519
1520   /* Allocate further tree nodes temporarily during compilation
1521      of this function only.  */
1522   temporary_allocation ();
1523
1524 #if 0
1525       /* If this fcn was already referenced via a block-scope `extern' decl (or
1526          an implicit decl), propagate certain information about the usage. */
1527       if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
1528         TREE_ADDRESSABLE (current_function_decl) = 1;
1529
1530 #endif
1531
1532   if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
1533       && ! flag_emit_class_files
1534       && ! CLASS_INTERFACE (TYPE_NAME (current_class)))
1535     {
1536       tree clas = DECL_CONTEXT (fndecl);
1537       tree init = build (CALL_EXPR, void_type_node,
1538                          build_address_of (soft_initclass_node),
1539                          build_tree_list (NULL_TREE, build_class_ref (clas)),
1540                          NULL_TREE);
1541       TREE_SIDE_EFFECTS (init) = 1;
1542       expand_expr_stmt (init);
1543     }
1544
1545   /* Push local variables. Function compiled from source code are
1546      using a different local variables management, and for them,
1547      pushlevel shouldn't be called from here.  */
1548   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (fndecl)))
1549     {
1550       pushlevel (2);
1551       if (! flag_emit_class_files)
1552         expand_start_bindings (1);
1553     }
1554
1555   if (METHOD_SYNCHRONIZED (fndecl) && ! flag_emit_class_files)
1556     {
1557       /* Warp function body with a monitorenter plus monitorexit cleanup. */
1558       tree enter, exit, lock;
1559       if (METHOD_STATIC (fndecl))
1560         lock = build_class_ref (DECL_CONTEXT (fndecl));
1561       else
1562         lock = DECL_ARGUMENTS (fndecl);
1563       BUILD_MONITOR_ENTER (enter, lock);
1564       BUILD_MONITOR_EXIT (exit, lock);
1565       if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (fndecl)))
1566         {
1567           expand_expr_stmt (enter);
1568           expand_decl_cleanup (NULL_TREE, exit);
1569         }
1570       else
1571         {
1572           tree function_body = DECL_FUNCTION_BODY (fndecl);
1573           tree body = BLOCK_EXPR_BODY (function_body);
1574           lock = build (WITH_CLEANUP_EXPR, void_type_node,
1575                         enter,  NULL_TREE, exit);
1576           TREE_SIDE_EFFECTS (lock) = 1;
1577           lock = build (COMPOUND_EXPR, TREE_TYPE (body), lock, body);
1578           TREE_SIDE_EFFECTS (lock) = 1;
1579           lock = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (body), lock);
1580           TREE_SIDE_EFFECTS (lock) = 1;
1581           BLOCK_EXPR_BODY (function_body) = lock;
1582         }
1583     }
1584 }
1585
1586 void
1587 start_java_method (fndecl)
1588      tree fndecl;
1589 {
1590   tree tem, *ptr;
1591   int i;
1592
1593   current_function_decl = fndecl;
1594   announce_function (fndecl);
1595
1596   i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1597   decl_map = make_tree_vec (i);
1598   type_map = (tree *) oballoc (i * sizeof (tree));
1599
1600   pushlevel (1);  /* Push parameters. */
1601
1602   ptr = &DECL_ARGUMENTS (fndecl);
1603   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1604        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1605     {
1606       tree parm_name = NULL_TREE, parm_decl;
1607       tree parm_type = TREE_VALUE (tem);
1608       if (i >= DECL_MAX_LOCALS(fndecl))
1609         fatal ("function has more parameters than local slots");
1610
1611       parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1612       DECL_CONTEXT (parm_decl) = fndecl;
1613       if (PROMOTE_PROTOTYPES
1614           && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1615           && INTEGRAL_TYPE_P (parm_type))
1616         parm_type = integer_type_node;
1617       DECL_ARG_TYPE (parm_decl) = parm_type;
1618
1619       *ptr = parm_decl;
1620       ptr = &TREE_CHAIN (parm_decl);
1621
1622       /* Add parm_decl to the decl_map. */
1623       push_jvm_slot (i, parm_decl);
1624
1625       type_map[i] = TREE_TYPE (parm_decl);
1626       if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1627         {
1628           i++;
1629           type_map[i] = void_type_node;
1630         }
1631     }
1632   *ptr = NULL_TREE;
1633   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1634
1635   while (i < DECL_MAX_LOCALS(fndecl))
1636     type_map[i++] = NULL_TREE;
1637
1638   build_result_decl (fndecl);
1639   complete_start_java_method (fndecl);
1640 }
1641
1642 void
1643 end_java_method ()
1644 {
1645   tree fndecl = current_function_decl;
1646   int flag_asynchronous_exceptions = asynchronous_exceptions;
1647
1648   expand_end_bindings (getdecls (), 1, 0);
1649   /* pop out of function */
1650   poplevel (1, 1, 0);
1651
1652   /* pop out of its parameters */
1653   poplevel (1, 0, 1);
1654
1655   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1656
1657   emit_handlers ();
1658
1659   /* Generate rtl for function exit.  */
1660   expand_function_end (input_filename, lineno, 0);
1661
1662   /* FIXME: If the current method contains any exception handlers,
1663      force asynchronous_exceptions: this is necessary because signal
1664      handlers in libjava may throw exceptions.  This is far from being
1665      a perfect solution, but it's better than doing nothing at all.*/
1666   if (catch_clauses)
1667     asynchronous_exceptions = 1;
1668
1669   /* Run the optimizers and output assembler code for this function. */
1670   rest_of_compilation (fndecl);
1671
1672   current_function_decl = NULL_TREE;
1673   permanent_allocation (1);
1674   asynchronous_exceptions = flag_asynchronous_exceptions;
1675 }