OSDN Git Service

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