OSDN Git Service

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