OSDN Git Service

gcc/ChangeLog:
[pf3gnuchains/gcc-fork.git] / gcc / java / class.c
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 /* Written by Per Bothner <bothner@cygnus.com> */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "java-tree.h"
36 #include "jcf.h"
37 #include "obstack.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "parse.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "stdio.h"
44 #include "target.h"
45 #include "except.h"
46
47 /* DOS brain-damage */
48 #ifndef O_BINARY
49 #define O_BINARY 0 /* MS-DOS brain-damage */
50 #endif
51
52 static tree make_method_value (tree);
53 static tree build_java_method_type (tree, tree, int);
54 static int32 hashUtf8String (const char *, int);
55 static tree make_field_value (tree);
56 static tree get_dispatch_vector (tree);
57 static tree get_dispatch_table (tree, tree);
58 static int supers_all_compiled (tree type);
59 static void add_interface_do (tree, tree, int);
60 static tree maybe_layout_super_class (tree, tree);
61 static void add_miranda_methods (tree, tree);
62 static int assume_compiled (const char *);
63 static tree build_symbol_entry (tree);
64
65 static GTY(()) rtx registerClass_libfunc;
66
67 struct obstack temporary_obstack;
68
69 /* The compiler generates different code depending on whether or not
70    it can assume certain classes have been compiled down to native
71    code or not.  The compiler options -fassume-compiled= and
72    -fno-assume-compiled= are used to create a tree of
73    class_flag_node objects.  This tree is queried to determine if
74    a class is assume to be compiled or not.  Each node in the tree
75    represents either a package or a specific class.  */
76
77 typedef struct class_flag_node_struct
78 {
79   /* The class or package name.  */
80   const char *ident;
81
82   /* Nonzero if this represents an exclusion.  */
83   int value;
84
85   /* Pointers to other nodes in the tree.  */
86   struct class_flag_node_struct *parent;
87   struct class_flag_node_struct *sibling;
88   struct class_flag_node_struct *child;
89 } class_flag_node;
90
91 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
92 static void add_class_flag (class_flag_node **, const char *, int);
93
94 /* This is the root of the include/exclude tree.  */
95
96 static class_flag_node *assume_compiled_tree;
97
98 static class_flag_node *enable_assert_tree;
99
100 static GTY(()) tree class_roots[5];
101 #define registered_class class_roots[0]
102 #define fields_ident class_roots[1]  /* get_identifier ("fields") */
103 #define info_ident class_roots[2]  /* get_identifier ("info") */
104 #define class_list class_roots[3]
105 #define class_dtable_decl class_roots[4]
106
107 /* Return the node that most closely represents the class whose name
108    is IDENT.  Start the search from NODE (followed by its siblings).
109    Return NULL if an appropriate node does not exist.  */
110
111 static class_flag_node *
112 find_class_flag_node (class_flag_node *node, const char *ident)
113 {
114   while (node)
115     {
116       size_t node_ident_length = strlen (node->ident);
117
118       /* node_ident_length is zero at the root of the tree.  If the
119          identifiers are the same length, then we have matching
120          classes.  Otherwise check if we've matched an enclosing
121          package name.  */
122
123       if (node_ident_length == 0
124           || (strncmp (ident, node->ident, node_ident_length) == 0
125               && (ident[node_ident_length] == '\0'
126                   || ident[node_ident_length] == '.')))
127         {
128           /* We've found a match, however, there might be a more
129              specific match.  */
130
131           class_flag_node *found = find_class_flag_node (node->child, ident);
132           if (found)
133             return found;
134           else
135             return node;
136         }
137
138       /* No match yet.  Continue through the sibling list.  */
139       node = node->sibling;
140     }
141
142   /* No match at all in this tree.  */
143   return NULL;
144 }
145
146 void
147 add_class_flag (class_flag_node **rootp, const char *ident, int value)
148 {
149   class_flag_node *root = *rootp;
150   class_flag_node *parent, *node;
151
152   /* Create the root of the tree if it doesn't exist yet.  */
153
154   if (NULL == root)
155     {
156       root = xmalloc (sizeof (class_flag_node));
157       root->ident = "";
158       root->value = 0;
159       root->sibling = NULL;
160       root->child = NULL;
161       root->parent = NULL;
162       *rootp = root;
163     }
164
165   /* Calling the function with the empty string means we're setting
166      value for the root of the hierarchy.  */
167
168   if (0 == ident[0])
169     {
170       root->value = value;
171       return;
172     }
173
174   /* Find the parent node for this new node.  PARENT will either be a
175      class or a package name.  Adjust PARENT accordingly.  */
176
177   parent = find_class_flag_node (root, ident);
178   if (strcmp (ident, parent->ident) == 0)
179     parent->value = value;
180   else
181     {
182       /* Insert new node into the tree.  */
183       node = xmalloc (sizeof (class_flag_node));
184
185       node->ident = xstrdup (ident);
186       node->value = value;
187       node->child = NULL;
188
189       node->parent = parent;
190       node->sibling = parent->child;
191       parent->child = node;
192     }
193 }
194
195 /* Add a new IDENT to the include/exclude tree.  It's an exclusion
196    if EXCLUDEP is nonzero.  */
197
198 void
199 add_assume_compiled (const char *ident, int excludep)
200 {
201   add_class_flag (&assume_compiled_tree, ident, excludep);
202 }
203
204 /* The default value returned by enable_assertions. */
205
206 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
207
208 /* Enter IDENT (a class or package name) into the enable-assertions table.
209    VALUE is true to enable and false to disable. */
210
211 void
212 add_enable_assert (const char *ident, int value)
213 {
214   if (enable_assert_tree == NULL)
215     add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
216   add_class_flag (&enable_assert_tree, ident, value);
217 }
218
219 /* Returns nonzero if IDENT is the name of a class that the compiler
220    should assume has been compiled to object code.  */
221
222 static int
223 assume_compiled (const char *ident)
224 {
225   class_flag_node *i;
226   int result;
227   
228   if (NULL == assume_compiled_tree)
229     return 1;
230
231   i = find_class_flag_node (assume_compiled_tree, ident);
232
233   result = ! i->value;
234   
235   return (result);
236 }
237
238 /* Return true if we should generate code to check assertions within KLASS. */
239
240 bool
241 enable_assertions (tree klass)
242 {
243   /* Check if command-line specifies whether we should check assertions. */
244
245   if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
246     {
247       const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
248       class_flag_node *node
249         = find_class_flag_node (enable_assert_tree, ident);
250       return node->value;
251     }
252
253   /* The default is to enable assertions if generating class files,
254      or not optimizing. */
255   return DEFAULT_ENABLE_ASSERT;
256 }
257
258 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
259    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
260    Also, PREFIX is prepended, and SUFFIX is appended. */
261
262 tree
263 ident_subst (const char* old_name,
264              int old_length,
265              const char *prefix,
266              int old_char,
267              int new_char,
268              const char *suffix)
269 {
270   int prefix_len = strlen (prefix);
271   int suffix_len = strlen (suffix);
272   int i = prefix_len + old_length + suffix_len + 1;
273 #ifdef __GNUC__
274   char buffer[i];
275 #else
276   char *buffer = alloca (i);
277 #endif
278   strcpy (buffer, prefix);
279   for (i = 0; i < old_length; i++)
280     {
281       char ch = old_name[i];
282       if (ch == old_char)
283         ch = new_char;
284       buffer[prefix_len + i] = ch;
285     }
286   strcpy (buffer + prefix_len + old_length, suffix);
287   return get_identifier (buffer);
288 }
289
290 /* Return an IDENTIFIER_NODE the same as OLD_ID,
291    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
292    Also, PREFIX is prepended, and SUFFIX is appended. */
293
294 tree
295 identifier_subst (const tree old_id,
296                   const char *prefix,
297                   int old_char,
298                   int new_char,
299                   const char *suffix)
300 {
301   return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
302                       prefix, old_char, new_char, suffix);
303 }
304
305 /* Generate a valid C identifier from the name of the class TYPE,
306    prefixed by PREFIX. */
307
308 tree
309 mangled_classname (const char *prefix, tree type)
310 {
311   tree ident = TYPE_NAME (type);
312   if (TREE_CODE (ident) != IDENTIFIER_NODE)
313     ident = DECL_NAME (ident);
314   return identifier_subst (ident, prefix, '.', '_', "");
315 }
316
317 tree
318 make_class (void)
319 {
320   tree type;
321   type = make_node (RECORD_TYPE);
322   TYPE_BINFO (type) = make_tree_vec (BINFO_ELTS);
323   MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
324
325   return type;
326 }
327
328 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
329    and where each of the constituents is separated by '/',
330    return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
331
332 tree
333 unmangle_classname (const char *name, int name_length)
334 {
335   tree to_return = ident_subst (name, name_length, "", '/', '.', "");
336   /* It's not sufficient to compare to_return and get_identifier
337      (name) to determine whether to_return is qualified. There are
338      cases in signature analysis where name will be stripped of a
339      trailing ';'. */
340   name = IDENTIFIER_POINTER (to_return);
341   while (*name)
342     if (*name++ == '.') 
343       {
344         QUALIFIED_P (to_return) = 1;
345         break;
346       }
347   
348   return to_return;
349 }
350
351
352 /* Given a class, create the DECLs for all its associated indirect dispatch tables.  */
353 void
354 gen_indirect_dispatch_tables (tree type)
355 {
356   const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
357   {  
358     tree field = NULL;
359     char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
360     tree catch_class_type = make_node (RECORD_TYPE);
361
362     sprintf (buf, "_catch_classes_%s", typename);
363     PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
364     PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
365     FINISH_RECORD (catch_class_type);
366     
367     TYPE_CTABLE_DECL (type) 
368       = build_decl (VAR_DECL, get_identifier (buf),
369                     build_array_type (catch_class_type, 0));
370     DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
371     TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
372     TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
373     TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
374     DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
375     pushdecl (TYPE_CTABLE_DECL (type));  
376   }
377
378   if (flag_indirect_dispatch)
379     {
380       {
381         char *buf = alloca (strlen (typename) + strlen ("_otable_syms_") + 1);
382
383         sprintf (buf, "_otable_%s", typename);
384         TYPE_OTABLE_DECL (type) = 
385           build_decl (VAR_DECL, get_identifier (buf), otable_type);
386         DECL_EXTERNAL (TYPE_OTABLE_DECL (type)) = 1;
387         TREE_STATIC (TYPE_OTABLE_DECL (type)) = 1;
388         TREE_READONLY (TYPE_OTABLE_DECL (type)) = 1;
389         TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
390         DECL_IGNORED_P (TYPE_OTABLE_DECL (type)) = 1;
391         pushdecl (TYPE_OTABLE_DECL (type));  
392         sprintf (buf, "_otable_syms_%s", typename);
393         TYPE_OTABLE_SYMS_DECL (type) = 
394           build_decl (VAR_DECL, get_identifier (buf), symbols_array_type);
395         TREE_STATIC (TYPE_OTABLE_SYMS_DECL (type)) = 1;
396         TREE_CONSTANT (TYPE_OTABLE_SYMS_DECL (type)) = 1;
397         DECL_IGNORED_P(TYPE_OTABLE_SYMS_DECL (type)) = 1;
398         pushdecl (TYPE_OTABLE_SYMS_DECL (type));
399       }
400
401       {
402         char *buf = alloca (strlen (typename) + strlen ("_atable_syms_") + 1);
403         tree decl;
404
405         sprintf (buf, "_atable_%s", typename);
406         TYPE_ATABLE_DECL (type) = decl =
407           build_decl (VAR_DECL, get_identifier (buf), atable_type);
408         DECL_EXTERNAL (decl) = 1;
409         TREE_STATIC (decl) = 1;
410         TREE_READONLY (decl) = 1;
411         TREE_CONSTANT (decl) = 1;
412         DECL_IGNORED_P (decl) = 1;
413         /* Mark the atable as belonging to this class.  */
414         pushdecl (decl);  
415         MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
416         DECL_OWNER (decl) = type;
417         sprintf (buf, "_atable_syms_%s", typename);
418         TYPE_ATABLE_SYMS_DECL (type) = 
419           build_decl (VAR_DECL, get_identifier (buf), symbols_array_type);
420         TREE_STATIC (TYPE_ATABLE_SYMS_DECL (type)) = 1;
421         TREE_CONSTANT (TYPE_ATABLE_SYMS_DECL (type)) = 1;
422         DECL_IGNORED_P (TYPE_ATABLE_SYMS_DECL (type)) = 1;
423         pushdecl (TYPE_ATABLE_SYMS_DECL (type));
424       }
425     }
426 }
427
428 tree
429 push_class (tree class_type, tree class_name)
430 {
431   tree decl, signature;
432   location_t saved_loc = input_location;
433   tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
434   CLASS_P (class_type) = 1;
435   input_filename = IDENTIFIER_POINTER (source_name);
436   input_line = 0;
437   decl = build_decl (TYPE_DECL, class_name, class_type);
438
439   /* dbxout needs a DECL_SIZE if in gstabs mode */
440   DECL_SIZE (decl) = integer_zero_node;
441
442   input_location = saved_loc;
443   signature = identifier_subst (class_name, "L", '.', '/', ";");
444   IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
445
446   /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
447      both a typedef and in the struct name-space.  We may want to re-visit
448      this later, but for now it reduces the changes needed for gdb. */
449   DECL_ARTIFICIAL (decl) = 1;
450
451   pushdecl_top_level (decl);
452
453   return decl;
454 }
455
456 /* Finds the (global) class named NAME.  Creates the class if not found.
457    Also creates associated TYPE_DECL.
458    Does not check if the class actually exists, load the class,
459    fill in field or methods, or do layout_type. */
460
461 tree
462 lookup_class (tree name)
463 {
464   tree decl = IDENTIFIER_CLASS_VALUE (name);
465   if (decl == NULL_TREE)
466     decl = push_class (make_class (), name);
467   return TREE_TYPE (decl);
468 }
469
470 void
471 set_super_info (int access_flags, tree this_class,
472                 tree super_class, int interfaces_count)
473 {
474   int total_supers = interfaces_count;
475   tree class_decl = TYPE_NAME (this_class);
476   if (super_class)
477     total_supers++;
478
479   TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
480   TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
481   if (super_class)
482     {
483       tree super_binfo = make_tree_vec (BINFO_ELTS);
484       BINFO_TYPE (super_binfo) = super_class;
485       BINFO_OFFSET (super_binfo) = integer_zero_node;
486       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
487         = super_binfo;
488       CLASS_HAS_SUPER (this_class) = 1;
489     }
490
491   set_class_decl_access_flags (access_flags, class_decl);
492 }
493
494 void
495 set_class_decl_access_flags (int access_flags, tree class_decl)
496 {
497   if (access_flags & ACC_PUBLIC)    CLASS_PUBLIC (class_decl) = 1;
498   if (access_flags & ACC_FINAL)     CLASS_FINAL (class_decl) = 1;
499   if (access_flags & ACC_SUPER)     CLASS_SUPER (class_decl) = 1;
500   if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
501   if (access_flags & ACC_ABSTRACT)  CLASS_ABSTRACT (class_decl) = 1;
502   if (access_flags & ACC_STATIC)    CLASS_STATIC (class_decl) = 1;
503   if (access_flags & ACC_PRIVATE)   CLASS_PRIVATE (class_decl) = 1;
504   if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
505   if (access_flags & ACC_STRICT)    CLASS_STRICTFP (class_decl) = 1;
506 }
507
508 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
509    direct sub-classes of Object are 1, and so on. */
510
511 int
512 class_depth (tree clas)
513 {
514   int depth = 0;
515   if (! CLASS_LOADED_P (clas))
516     load_class (clas, 1);
517   if (TYPE_SIZE (clas) == error_mark_node)
518     return -1;
519   while (clas != object_type_node)
520     {
521       depth++;
522       clas = TYPE_BINFO_BASETYPE (clas, 0);
523     }
524   return depth;
525 }
526
527 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
528
529 int
530 interface_of_p (tree type1, tree type2)
531 {
532   int n, i;
533   tree basetype_vec;
534
535   if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
536     return 0;
537   n = TREE_VEC_LENGTH (basetype_vec);
538   for (i = 0; i < n; i++)
539     {
540       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
541       if (vec_elt && BINFO_TYPE (vec_elt) == type1)
542         return 1;
543     }
544   for (i = 0; i < n; i++)
545     {
546       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
547       if (vec_elt && BINFO_TYPE (vec_elt) 
548           && interface_of_p (type1, BINFO_TYPE (vec_elt)))
549         return 1;
550     }
551   return 0;
552 }
553
554 /* Return true iff TYPE1 inherits from TYPE2. */
555
556 int
557 inherits_from_p (tree type1, tree type2)
558 {
559   while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
560     {
561       if (type1 == type2)
562         return 1;
563       type1 = CLASSTYPE_SUPER (type1);
564     }
565   return 0;
566 }
567
568 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
569
570 int
571 enclosing_context_p (tree type1, tree type2)
572 {
573   if (!INNER_CLASS_TYPE_P (type2))
574     return 0;
575
576   for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
577        type2; 
578        type2 = (INNER_CLASS_TYPE_P (type2) ?
579                 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
580     {
581       if (type2 == type1)
582         return 1;
583     }
584
585   return 0;
586 }
587
588 /* Return 1 iff there exists a common enclosing context between TYPE1
589    and TYPE2.  */
590
591 int common_enclosing_context_p (tree type1, tree type2)
592 {
593   if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
594     return 0;
595   
596   for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1; 
597        type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
598                 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
599     {
600       tree current;
601       for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
602            current = (PURE_INNER_CLASS_TYPE_P (current) ?
603                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) : 
604                       NULL_TREE))
605         if (type1 == current)
606           return 1;
607     }
608   return 0;
609 }
610
611 static void
612 add_interface_do (tree basetype_vec, tree interface_class, int i)
613 {
614   tree interface_binfo = make_tree_vec (BINFO_ELTS);
615   BINFO_TYPE (interface_binfo) = interface_class;
616   BINFO_OFFSET (interface_binfo) = integer_zero_node;
617   BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
618   TREE_VIA_VIRTUAL (interface_binfo) = 1;
619   TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
620 }
621
622 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
623    found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
624    if attempt is made to add it twice. */
625
626 tree
627 maybe_add_interface (tree this_class, tree interface_class)
628 {
629   tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
630   int i;
631   int n = TREE_VEC_LENGTH (basetype_vec);
632   for (i = 0; ; i++)
633     {
634       if (i >= n)
635         {
636           error ("internal error - too many interface type");
637           return NULL_TREE;
638         }
639       else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
640         break;
641       else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
642         return interface_class;
643     } 
644   add_interface_do (basetype_vec, interface_class, i);
645   return NULL_TREE;
646 }
647
648 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
649
650 void
651 add_interface (tree this_class, tree interface_class)
652 {
653   tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
654   int i;
655   int n = TREE_VEC_LENGTH (basetype_vec);
656   for (i = 0; ; i++)
657     {
658       if (i >= n)
659         {
660           error ("internal error - too many interface type");
661           return;
662         }
663       else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
664         break;
665     }
666   add_interface_do (basetype_vec, interface_class, i);
667 }
668
669 #if 0
670 /* Return the address of a pointer to the first FUNCTION_DECL
671    in the list (*LIST) whose DECL_NAME is NAME. */
672
673 static tree *
674 find_named_method (tree *list, tree name)
675 {
676   while (*list && DECL_NAME (*list) != name)
677     list = &TREE_CHAIN (*list);
678   return list;
679 }
680 #endif
681
682 static tree
683 build_java_method_type (tree fntype, tree this_class, int access_flags)
684 {
685   if (access_flags & ACC_STATIC)
686     return fntype;
687   return build_method_type (this_class, fntype);
688 }
689
690 tree
691 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
692 {
693   tree method_type, fndecl;
694
695   method_type = build_java_method_type (function_type,
696                                         this_class, access_flags);
697
698   fndecl = build_decl (FUNCTION_DECL, name, method_type);
699   DECL_CONTEXT (fndecl) = this_class;
700
701   DECL_LANG_SPECIFIC (fndecl)
702     = ggc_alloc_cleared (sizeof (struct lang_decl));
703   DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
704
705   /* Initialize the static initializer test table.  */
706   
707   DECL_FUNCTION_INIT_TEST_TABLE (fndecl) = 
708     java_treetreehash_create (10, 1);
709
710   /* Initialize the initialized (static) class table. */
711   if (access_flags & ACC_STATIC)
712     DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
713       htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
714
715   /* Initialize the static method invocation compound list */
716   DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
717
718   TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
719   TYPE_METHODS (this_class) = fndecl;
720
721   /* Notice that this is a finalizer and update the class type
722      accordingly. This is used to optimize instance allocation. */
723   if (name == finalize_identifier_node
724       && TREE_TYPE (function_type) == void_type_node
725       && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
726     HAS_FINALIZER_P (this_class) = 1;
727
728   if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
729   if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
730   if (access_flags & ACC_PRIVATE)
731     METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
732   if (access_flags & ACC_NATIVE)
733     {
734       METHOD_NATIVE (fndecl) = 1;
735       DECL_EXTERNAL (fndecl) = 1;
736     }
737   if (access_flags & ACC_STATIC) 
738     METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
739   if (access_flags & ACC_FINAL) 
740     METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
741   if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
742   if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
743   if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
744   return fndecl;
745 }
746
747 /* Add a method to THIS_CLASS.
748    The method's name is NAME.
749    Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
750
751 tree
752 add_method (tree this_class, int access_flags, tree name, tree method_sig)
753 {
754   tree function_type, fndecl;
755   const unsigned char *sig
756     = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
757
758   if (sig[0] != '(')
759     fatal_error ("bad method signature");
760
761   function_type = get_type_from_signature (method_sig);
762   fndecl = add_method_1 (this_class, access_flags, name, function_type);
763   set_java_signature (TREE_TYPE (fndecl), method_sig);
764   return fndecl;
765 }
766
767 tree
768 add_field (tree class, tree name, tree field_type, int flags)
769 {
770   int is_static = (flags & ACC_STATIC) != 0;
771   tree field;
772   field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
773   TREE_CHAIN (field) = TYPE_FIELDS (class);
774   TYPE_FIELDS (class) = field;
775   DECL_CONTEXT (field) = class;
776
777   if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
778   if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
779   if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
780   if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
781   if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
782   if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
783   if (is_static)
784     {
785       FIELD_STATIC (field) = 1;
786       /* Always make field externally visible.  This is required so
787          that native methods can always access the field.  */
788       TREE_PUBLIC (field) = 1;
789       /* Considered external until we know what classes are being
790          compiled into this object file.  */
791       DECL_EXTERNAL (field) = 1;
792     }
793
794   return field;
795 }
796
797 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
798
799 void
800 set_constant_value (tree field, tree constant)
801 {
802   if (field == NULL_TREE)
803     warning ("misplaced ConstantValue attribute (not in any field)");
804   else if (DECL_INITIAL (field) != NULL_TREE)
805     warning ("duplicate ConstantValue attribute for field '%s'",
806              IDENTIFIER_POINTER (DECL_NAME (field)));
807   else
808     {
809       DECL_INITIAL (field) = constant;
810       if (TREE_TYPE (constant) != TREE_TYPE (field)
811           && ! (TREE_TYPE (constant) == int_type_node
812                 && INTEGRAL_TYPE_P (TREE_TYPE (field))
813                 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
814           && ! (TREE_TYPE (constant) == utf8const_ptr_type
815                 && TREE_TYPE (field) == string_ptr_type_node))
816         error ("ConstantValue attribute of field '%s' has wrong type",
817                IDENTIFIER_POINTER (DECL_NAME (field)));
818       if (FIELD_FINAL (field))
819         DECL_FIELD_FINAL_IUD (field) = 1;
820     }
821 }
822
823 /* Count the number of Unicode chars encoded in a given Ut8 string. */
824
825 #if 0
826 int
827 strLengthUtf8 (char *str, int len)
828 {
829   register unsigned char* ptr = (unsigned char*) str;
830   register unsigned char *limit = ptr + len;
831   int str_length = 0;
832   for (; ptr < limit; str_length++) {
833     if (UTF8_GET (ptr, limit) < 0)
834       return -1;
835   }
836   return str_length;
837 }
838 #endif
839
840
841 /* Calculate a hash value for a string encoded in Utf8 format.
842  * This returns the same hash value as specified for java.lang.String.hashCode.
843  */
844
845 static int32
846 hashUtf8String (const char *str, int len)
847 {
848   const unsigned char* ptr = (const unsigned char*) str;
849   const unsigned char *limit = ptr + len;
850   int32 hash = 0;
851   for (; ptr < limit;)
852     {
853       int ch = UTF8_GET (ptr, limit);
854       /* Updated specification from
855          http://www.javasoft.com/docs/books/jls/clarify.html. */
856       hash = (31 * hash) + ch;
857     }
858   return hash;
859 }
860
861 static GTY(()) tree utf8_decl_list = NULL_TREE;
862
863 tree
864 build_utf8_ref (tree name)
865 {
866   const char * name_ptr = IDENTIFIER_POINTER(name);
867   int name_len = IDENTIFIER_LENGTH(name);
868   char buf[60];
869   tree ctype, field = NULL_TREE, str_type, cinit, string;
870   static int utf8_count = 0;
871   int name_hash;
872   tree ref = IDENTIFIER_UTF8_REF (name);
873   tree decl;
874   if (ref != NULL_TREE)
875     return ref;
876
877   ctype = make_node (RECORD_TYPE);
878   str_type = build_prim_array_type (unsigned_byte_type_node,
879                                     name_len + 1); /* Allow for final '\0'. */
880   PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
881   PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
882   PUSH_FIELD (ctype, field, "data", str_type);
883   FINISH_RECORD (ctype);
884   START_RECORD_CONSTRUCTOR (cinit, ctype);
885   name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
886   PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
887   PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
888   string = build_string (name_len, name_ptr);
889   TREE_TYPE (string) = str_type;
890   PUSH_FIELD_VALUE (cinit, "data", string);
891   FINISH_RECORD_CONSTRUCTOR (cinit);
892   TREE_CONSTANT (cinit) = 1;
893   TREE_INVARIANT (cinit) = 1;
894
895   /* Generate a unique-enough identifier.  */
896   sprintf(buf, "_Utf%d", ++utf8_count);
897
898   decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
899   TREE_STATIC (decl) = 1;
900   DECL_ARTIFICIAL (decl) = 1;
901   DECL_IGNORED_P (decl) = 1;
902   TREE_READONLY (decl) = 1;
903   TREE_THIS_VOLATILE (decl) = 0;
904   DECL_INITIAL (decl) = cinit;
905
906   if (HAVE_GAS_SHF_MERGE)
907     {
908       int decl_size;
909       /* Ensure decl_size is a multiple of utf8const_type's alignment. */
910       decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
911         & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
912       if (flag_merge_constants && decl_size < 256)
913         {
914           char buf[32];
915           int flags = (SECTION_OVERRIDE
916                        | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
917           sprintf (buf, ".rodata.jutf8.%d", decl_size);
918           named_section_flags (buf, flags);
919           DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
920         }
921     }
922
923   TREE_CHAIN (decl) = utf8_decl_list;
924   layout_decl (decl, 0);
925   pushdecl (decl);
926   rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
927   utf8_decl_list = decl;
928   make_decl_rtl (decl, (char*) 0);
929   ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
930   IDENTIFIER_UTF8_REF (name) = ref;
931   return ref;
932 }
933
934 /* Like build_class_ref, but instead of a direct reference generate a
935    pointer into the constant pool.  */
936
937 static tree
938 build_indirect_class_ref (tree type)
939 {
940   int index;
941   tree cl;
942   index = alloc_class_constant (type);
943   cl = build_ref_from_constant_pool (index); 
944   return convert (promote_type (class_ptr_type), cl);
945 }
946
947 /* Build a reference to the class TYPE.
948    Also handles primitive types and array types. */
949
950 tree
951 build_class_ref (tree type)
952 {
953   int is_compiled = is_compiled_class (type);
954   if (is_compiled)
955     {
956       tree ref, decl_name, decl;
957       if (TREE_CODE (type) == POINTER_TYPE)
958         type = TREE_TYPE (type);
959
960       /* FIXME: we really want an indirect reference to our
961          superclass.  However, libgcj assumes that a superclass
962          pointer always points directly to a class.  As a workaround
963          we always emit this hard superclass reference.  */
964       if  (flag_indirect_dispatch
965            && type != output_class
966            && type != CLASSTYPE_SUPER (output_class)
967            && TREE_CODE (type) == RECORD_TYPE)
968         return build_indirect_class_ref (type);
969
970       if (TREE_CODE (type) == RECORD_TYPE)
971         {
972           if (TYPE_SIZE (type) == error_mark_node)
973             return null_pointer_node;
974           decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
975                                         "", '/', '/', ".class");
976           decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
977           if (decl == NULL_TREE)
978             {
979               decl = build_decl (VAR_DECL, decl_name, class_type_node);
980               DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
981               DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
982               TREE_STATIC (decl) = 1;
983               TREE_PUBLIC (decl) = 1;
984               DECL_IGNORED_P (decl) = 1;
985               DECL_ARTIFICIAL (decl) = 1;
986               if (is_compiled == 1)
987                 DECL_EXTERNAL (decl) = 1;
988               SET_DECL_ASSEMBLER_NAME (decl, 
989                                        java_mangle_class_field
990                                        (&temporary_obstack, type));
991               make_decl_rtl (decl, NULL);
992               pushdecl_top_level (decl);
993             }
994         }
995       else
996         {
997           const char *name;
998           char buffer[25];
999           if (flag_emit_class_files)
1000             {
1001               const char *prim_class_name;
1002               tree prim_class;
1003               if (type == char_type_node)
1004                 prim_class_name = "java.lang.Character";
1005               else if (type == boolean_type_node)
1006                 prim_class_name = "java.lang.Boolean";
1007               else if (type == byte_type_node)
1008                 prim_class_name = "java.lang.Byte";
1009               else if (type == short_type_node)
1010                 prim_class_name = "java.lang.Short";
1011               else if (type == int_type_node)
1012                 prim_class_name = "java.lang.Integer";
1013               else if (type == long_type_node)
1014                 prim_class_name = "java.lang.Long";
1015               else if (type == float_type_node)
1016                 prim_class_name = "java.lang.Float";
1017               else if (type == double_type_node)
1018                 prim_class_name = "java.lang.Double";
1019               else if (type == void_type_node)
1020                 prim_class_name = "java.lang.Void";
1021               else
1022                 abort ();
1023
1024               prim_class = lookup_class (get_identifier (prim_class_name));
1025               return build (COMPONENT_REF, NULL_TREE,
1026                             prim_class, TYPE_identifier_node);
1027             }
1028           decl_name = TYPE_NAME (type);
1029           if (TREE_CODE (decl_name) == TYPE_DECL)
1030             decl_name = DECL_NAME (decl_name);
1031           name = IDENTIFIER_POINTER (decl_name);
1032           if (strncmp (name, "promoted_", 9) == 0)
1033             name += 9;
1034           sprintf (buffer, "_Jv_%sClass", name);
1035           decl_name = get_identifier (buffer);
1036           decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1037           if (decl == NULL_TREE)
1038             {
1039               decl = build_decl (VAR_DECL, decl_name, class_type_node);
1040               TREE_STATIC (decl) = 1;
1041               TREE_PUBLIC (decl) = 1;
1042               DECL_EXTERNAL (decl) = 1;
1043               make_decl_rtl (decl, NULL);
1044               pushdecl_top_level (decl);
1045             }
1046         }
1047
1048       ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1049       return ref;
1050     }
1051   else
1052     return build_indirect_class_ref (type);
1053 }
1054
1055 tree
1056 build_static_field_ref (tree fdecl)
1057 {
1058   tree fclass = DECL_CONTEXT (fdecl);
1059   int is_compiled = is_compiled_class (fclass);
1060
1061   /* Allow static final fields to fold to a constant.  When using
1062      -fno-assume-compiled, gcj will sometimes try to fold a field from
1063      an uncompiled class.  This is required when the field in question
1064      meets the appropriate criteria for a compile-time constant.
1065      However, currently sometimes gcj is too eager and will end up
1066      returning the field itself, leading to an incorrect external
1067      reference being generated.  */
1068   if ((is_compiled 
1069        && (! flag_indirect_dispatch || current_class == fclass))
1070       || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1071           && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1072               || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1073           && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1074     {
1075       if (!DECL_RTL_SET_P (fdecl))
1076         {
1077           if (is_compiled == 1)
1078             DECL_EXTERNAL (fdecl) = 1;
1079           make_decl_rtl (fdecl, NULL);
1080         }
1081       return fdecl;
1082     }
1083
1084   if (flag_indirect_dispatch)
1085     {
1086       tree table_index 
1087         = build_int_2 (get_symbol_table_index 
1088                        (fdecl, &TYPE_ATABLE_METHODS (output_class)), 0);
1089       tree field_address
1090         = build (ARRAY_REF, build_pointer_type (TREE_TYPE (fdecl)), 
1091                  TYPE_ATABLE_DECL (output_class), table_index);
1092       return fold (build1 (INDIRECT_REF, TREE_TYPE (fdecl), 
1093                            field_address));
1094     }
1095   else  
1096     {
1097       /* Compile as:
1098        * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1099       tree ref = build_class_ref (fclass);
1100       tree fld;
1101       int field_index = 0;
1102       ref = build1 (INDIRECT_REF, class_type_node, ref);
1103       ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1104                    lookup_field (&class_type_node, fields_ident));
1105
1106       for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1107         {
1108           if (fld == fdecl)
1109             break;
1110           if (fld == NULL_TREE)
1111             fatal_error ("field '%s' not found in class",
1112                          IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1113           if (FIELD_STATIC (fld))
1114             field_index++;
1115         }
1116       field_index *= int_size_in_bytes (field_type_node);
1117       ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1118                          ref, build_int_2 (field_index, 0)));
1119       ref = build1 (INDIRECT_REF, field_type_node, ref);
1120       ref = build (COMPONENT_REF, field_info_union_node,
1121                    ref, lookup_field (&field_type_node, info_ident));
1122       ref = build (COMPONENT_REF, ptr_type_node,
1123                    ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1124       return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1125     }
1126 }
1127
1128 int
1129 get_access_flags_from_decl (tree decl)
1130 {
1131   int access_flags = 0;
1132   if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1133     {
1134       if (FIELD_STATIC (decl))
1135         access_flags |= ACC_STATIC;
1136       if (FIELD_PUBLIC (decl))
1137         access_flags |= ACC_PUBLIC;
1138       if (FIELD_PROTECTED (decl))
1139         access_flags |= ACC_PROTECTED;
1140       if (FIELD_PRIVATE (decl))
1141         access_flags |= ACC_PRIVATE;
1142       if (FIELD_FINAL (decl))
1143         access_flags |= ACC_FINAL;
1144       if (FIELD_VOLATILE (decl))
1145         access_flags |= ACC_VOLATILE;
1146       if (FIELD_TRANSIENT (decl))
1147         access_flags |= ACC_TRANSIENT;
1148       return access_flags;
1149     }
1150   if (TREE_CODE (decl) == TYPE_DECL)
1151     {
1152       if (CLASS_PUBLIC (decl))
1153         access_flags |= ACC_PUBLIC;
1154       if (CLASS_FINAL (decl))
1155         access_flags |= ACC_FINAL;
1156       if (CLASS_SUPER (decl))
1157         access_flags |= ACC_SUPER;
1158       if (CLASS_INTERFACE (decl))
1159         access_flags |= ACC_INTERFACE;
1160       if (CLASS_ABSTRACT (decl))
1161         access_flags |= ACC_ABSTRACT;
1162       if (CLASS_STATIC (decl))
1163         access_flags |= ACC_STATIC;
1164       if (CLASS_PRIVATE (decl))
1165         access_flags |= ACC_PRIVATE;
1166       if (CLASS_PROTECTED (decl))
1167         access_flags |= ACC_PROTECTED;
1168       if (CLASS_STRICTFP (decl))
1169         access_flags |= ACC_STRICT;
1170       return access_flags;
1171     }
1172   if (TREE_CODE (decl) == FUNCTION_DECL)
1173     {
1174       if (METHOD_PUBLIC (decl))
1175         access_flags |= ACC_PUBLIC;
1176       if (METHOD_PRIVATE (decl))
1177         access_flags |= ACC_PRIVATE;
1178       if (METHOD_PROTECTED (decl))
1179         access_flags |= ACC_PROTECTED;
1180       if (METHOD_STATIC (decl))
1181         access_flags |= ACC_STATIC;
1182       if (METHOD_FINAL (decl))
1183         access_flags |= ACC_FINAL;
1184       if (METHOD_SYNCHRONIZED (decl))
1185         access_flags |= ACC_SYNCHRONIZED;
1186       if (METHOD_NATIVE (decl))
1187         access_flags |= ACC_NATIVE;
1188       if (METHOD_ABSTRACT (decl))
1189         access_flags |= ACC_ABSTRACT;
1190       if (METHOD_STRICTFP (decl))
1191         access_flags |= ACC_STRICT;
1192       if (METHOD_INVISIBLE (decl))
1193         access_flags |= ACC_INVISIBLE;
1194       return access_flags;
1195     }
1196   abort ();
1197 }
1198
1199 static tree
1200 make_field_value (tree fdecl)
1201 {
1202   tree finit;
1203   int flags;
1204   tree type = TREE_TYPE (fdecl);
1205   int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1206
1207   START_RECORD_CONSTRUCTOR (finit, field_type_node);
1208   PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1209   if (resolved)
1210     type = build_class_ref (type);
1211   else
1212     {
1213       tree signature = build_java_signature (type);
1214
1215       type = build_utf8_ref (unmangle_classname 
1216                              (IDENTIFIER_POINTER (signature),
1217                               IDENTIFIER_LENGTH (signature)));
1218     }
1219   PUSH_FIELD_VALUE (finit, "type", type);
1220
1221   flags = get_access_flags_from_decl (fdecl);
1222   if (! resolved)
1223     flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1224
1225   PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1226   PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1227
1228   PUSH_FIELD_VALUE
1229     (finit, "info",
1230      build_constructor (field_info_union_node,
1231             build_tree_list
1232             ((FIELD_STATIC (fdecl)
1233               ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1234               : TYPE_FIELDS (field_info_union_node)),
1235              (FIELD_STATIC (fdecl)
1236               ? build_address_of (build_static_field_ref (fdecl))
1237               : byte_position (fdecl)))));
1238
1239   FINISH_RECORD_CONSTRUCTOR (finit);
1240   return finit;
1241 }
1242
1243 static tree
1244 make_method_value (tree mdecl)
1245 {
1246   static int method_name_count = 0;
1247   tree minit;
1248   tree index;
1249   tree code;
1250   tree class_decl;
1251 #define ACC_TRANSLATED          0x4000
1252   int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1253
1254   class_decl = DECL_CONTEXT (mdecl);
1255   /* For interfaces, the index field contains the dispatch index. */
1256   if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1257     index = build_int_2 (get_interface_method_index (mdecl, class_decl), 0);
1258   else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1259     index = get_method_index (mdecl);
1260   else
1261     index = integer_minus_one_node;
1262
1263   code = null_pointer_node;
1264   if (DECL_RTL_SET_P (mdecl))
1265     code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1266   START_RECORD_CONSTRUCTOR (minit, method_type_node);
1267   PUSH_FIELD_VALUE (minit, "name",
1268                     build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1269                                     init_identifier_node
1270                                     : DECL_NAME (mdecl)));
1271   {
1272     tree signature = build_java_signature (TREE_TYPE (mdecl));
1273     PUSH_FIELD_VALUE (minit, "signature", 
1274                       (build_utf8_ref 
1275                        (unmangle_classname 
1276                         (IDENTIFIER_POINTER(signature),
1277                          IDENTIFIER_LENGTH(signature)))));
1278   }
1279   PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1280   PUSH_FIELD_VALUE (minit, "index", index);
1281   PUSH_FIELD_VALUE (minit, "ncode", code);
1282
1283   {
1284     /* Compute the `throws' information for the method.  */
1285     tree table = null_pointer_node;
1286     if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1287       {
1288         int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1289         tree iter, type, array;
1290         char buf[60];
1291
1292         table = tree_cons (NULL_TREE, table, NULL_TREE);
1293         for (iter = DECL_FUNCTION_THROWS (mdecl);
1294              iter != NULL_TREE;
1295              iter = TREE_CHAIN (iter))
1296           {
1297             tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1298             tree utf8
1299               = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1300                                                     IDENTIFIER_LENGTH (sig)));
1301             table = tree_cons (NULL_TREE, utf8, table);
1302           }
1303         type = build_prim_array_type (ptr_type_node, length);
1304         table = build_constructor (type, table);
1305         /* Compute something unique enough.  */
1306         sprintf (buf, "_methods%d", method_name_count++);
1307         array = build_decl (VAR_DECL, get_identifier (buf), type);
1308         DECL_INITIAL (array) = table;
1309         TREE_STATIC (array) = 1;
1310         DECL_ARTIFICIAL (array) = 1;
1311         DECL_IGNORED_P (array) = 1;
1312         rest_of_decl_compilation (array, (char*) 0, 1, 0);
1313
1314         table = build1 (ADDR_EXPR, ptr_type_node, array);
1315       }
1316
1317     PUSH_FIELD_VALUE (minit, "throws", table);
1318   }
1319
1320   FINISH_RECORD_CONSTRUCTOR (minit);
1321   return minit;
1322 }
1323
1324 static tree
1325 get_dispatch_vector (tree type)
1326 {
1327   tree vtable = TYPE_VTABLE (type);
1328
1329   if (vtable == NULL_TREE)
1330     {
1331       HOST_WIDE_INT i;
1332       tree method;
1333       tree super = CLASSTYPE_SUPER (type);
1334       HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1335       vtable = make_tree_vec (nvirtuals);
1336       TYPE_VTABLE (type) = vtable;
1337       if (super != NULL_TREE)
1338         {
1339           tree super_vtable = get_dispatch_vector (super);
1340
1341           for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1342             TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1343         }
1344
1345       for (method = TYPE_METHODS (type);  method != NULL_TREE;
1346            method = TREE_CHAIN (method))
1347         {
1348           tree method_index = get_method_index (method);
1349           if (method_index != NULL_TREE
1350               && host_integerp (method_index, 0))
1351             TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1352         }
1353     }
1354
1355   return vtable;
1356 }
1357
1358 static tree
1359 get_dispatch_table (tree type, tree this_class_addr)
1360 {
1361   int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1362   tree vtable = get_dispatch_vector (type);
1363   int i, j;
1364   tree list = NULL_TREE;
1365   int nvirtuals = TREE_VEC_LENGTH (vtable);
1366   int arraysize;
1367   tree gc_descr;
1368
1369   for (i = nvirtuals;  --i >= 0; )
1370     {
1371       tree method = TREE_VEC_ELT (vtable, i);
1372       if (METHOD_ABSTRACT (method))
1373         {
1374           if (! abstract_p)
1375             warning ("%Jabstract method in non-abstract class", method);
1376
1377           if (TARGET_VTABLE_USES_DESCRIPTORS)
1378             for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1379               list = tree_cons (NULL_TREE, null_pointer_node, list);
1380           else
1381             list = tree_cons (NULL_TREE, null_pointer_node, list);
1382         }
1383       else
1384         {
1385           if (!DECL_RTL_SET_P (method))
1386             make_decl_rtl (method, NULL);
1387
1388           if (TARGET_VTABLE_USES_DESCRIPTORS)
1389             for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1390               {
1391                 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node, 
1392                                     method, build_int_2 (j, 0));
1393                 TREE_CONSTANT (fdesc) = 1;
1394                 TREE_INVARIANT (fdesc) = 1;
1395                 list = tree_cons (NULL_TREE, fdesc, list);
1396               }
1397           else
1398             list = tree_cons (NULL_TREE,
1399                               build1 (ADDR_EXPR, nativecode_ptr_type_node,
1400                                       method),
1401                               list);
1402         }
1403     }
1404
1405   /* Dummy entry for compatibility with G++ -fvtable-thunks.  When
1406      using the Boehm GC we sometimes stash a GC type descriptor
1407      there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1408      the emitted byte count during the output to the assembly file. */
1409   /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1410      fake "function descriptor".  It's first word is the is the class
1411      pointer, and subsequent words (usually one) contain the GC descriptor.
1412      In all other cases, we reserve two extra vtable slots. */
1413   gc_descr =  get_boehm_type_descriptor (type);
1414   list = tree_cons (NULL_TREE, gc_descr, list);
1415   for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1416     list = tree_cons (NULL_TREE, gc_descr, list);
1417   list = tree_cons (NULL_TREE, this_class_addr, list);
1418
1419   /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1420   list = tree_cons (NULL_TREE, null_pointer_node, list);
1421   /** Offset to start of whole object.  Always (ptrdiff_t)0 for Java. */
1422   list = tree_cons (integer_zero_node, null_pointer_node, list);
1423
1424   arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1425   if (TARGET_VTABLE_USES_DESCRIPTORS)
1426     arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1427   arraysize += 2;
1428   return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1429                                                    arraysize), list);
1430 }
1431
1432
1433 /* Set the method_index for a method decl.  */
1434 void
1435 set_method_index (tree decl, tree method_index)
1436 {
1437   method_index = fold (convert (sizetype, method_index));
1438
1439   if (TARGET_VTABLE_USES_DESCRIPTORS)
1440     /* Add one to skip bogus descriptor for class and GC descriptor. */
1441     method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1442   else
1443     /* Add 1 to skip "class" field of dtable, and 1 to skip GC descriptor.  */
1444     method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1445
1446   DECL_VINDEX (decl) = method_index;
1447 }
1448
1449 /* Get the method_index for a method decl.  */
1450 tree
1451 get_method_index (tree decl)
1452 {
1453   tree method_index = DECL_VINDEX (decl);
1454
1455   if (! method_index)
1456     return NULL;
1457
1458   if (TARGET_VTABLE_USES_DESCRIPTORS)
1459     /* Sub one to skip bogus descriptor for class and GC descriptor. */
1460     method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1461   else
1462     /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor.  */
1463     method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1464
1465   return method_index;
1466 }
1467
1468 static int
1469 supers_all_compiled (tree type)
1470 {
1471   while (type != NULL_TREE)
1472     {
1473       if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1474         return 0;
1475       type = CLASSTYPE_SUPER (type);
1476     }
1477   return 1;
1478 }
1479
1480 void
1481 make_class_data (tree type)
1482 {
1483   tree decl, cons, temp;
1484   tree field, fields_decl;
1485   tree static_fields = NULL_TREE;
1486   tree instance_fields = NULL_TREE;
1487   HOST_WIDE_INT static_field_count = 0;
1488   HOST_WIDE_INT instance_field_count = 0;
1489   HOST_WIDE_INT field_count;
1490   tree field_array_type;
1491   tree method;
1492   tree methods = NULL_TREE;
1493   tree dtable_decl = NULL_TREE;
1494   HOST_WIDE_INT method_count = 0;
1495   tree method_array_type;
1496   tree methods_decl;
1497   tree super;
1498   tree this_class_addr;
1499   tree constant_pool_constructor;
1500   tree interfaces = null_pointer_node;
1501   int interface_len = 0;
1502   tree type_decl = TYPE_NAME (type);
1503   /** Offset from start of virtual function table declaration
1504       to where objects actually point at, following new g++ ABI. */
1505   tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1506
1507   this_class_addr = build_class_ref (type);
1508   decl = TREE_OPERAND (this_class_addr, 0);
1509
1510   /* Build Field array. */
1511   field = TYPE_FIELDS (type);
1512   if (DECL_NAME (field) == NULL_TREE)
1513     field = TREE_CHAIN (field);  /* Skip dummy field for inherited data. */
1514   for ( ;  field != NULL_TREE;  field = TREE_CHAIN (field))
1515     {
1516       if (! DECL_ARTIFICIAL (field))
1517         {
1518           tree init = make_field_value (field);
1519           if (FIELD_STATIC (field))
1520             {
1521               tree initial = DECL_INITIAL (field);
1522               static_field_count++;
1523               static_fields = tree_cons (NULL_TREE, init, static_fields);
1524               /* If the initial value is a string constant,
1525                  prevent output_constant from trying to assemble the value. */
1526               if (initial != NULL_TREE
1527                   && TREE_TYPE (initial) == string_ptr_type_node)
1528                 DECL_INITIAL (field) = NULL_TREE;
1529               rest_of_decl_compilation (field, (char*) 0, 1, 1);
1530               DECL_INITIAL (field) = initial;
1531             }
1532           else
1533             {
1534               instance_field_count++;
1535               instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1536             }
1537         }
1538     }
1539   field_count = static_field_count + instance_field_count;
1540   if (field_count > 0)
1541     {
1542       static_fields = nreverse (static_fields);
1543       instance_fields = nreverse (instance_fields);
1544       static_fields = chainon (static_fields, instance_fields);
1545       field_array_type = build_prim_array_type (field_type_node, field_count);
1546       fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1547                                 field_array_type);
1548       DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1549                                                       static_fields);
1550       TREE_STATIC (fields_decl) = 1;
1551       DECL_ARTIFICIAL (fields_decl) = 1;
1552       DECL_IGNORED_P (fields_decl) = 1;
1553       rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1554     }
1555   else
1556     fields_decl = NULL_TREE;
1557
1558   /* Build Method array. */
1559   for (method = TYPE_METHODS (type);
1560        method != NULL_TREE; method = TREE_CHAIN (method))
1561     {
1562       tree init;
1563       if (METHOD_PRIVATE (method)
1564           && ! flag_keep_inline_functions
1565           && (flag_inline_functions || optimize))
1566         continue;
1567       init = make_method_value (method);
1568       method_count++;
1569       methods = tree_cons (NULL_TREE, init, methods);
1570     }
1571   method_array_type = build_prim_array_type (method_type_node, method_count);
1572   methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1573                              method_array_type);
1574   DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1575                                                    nreverse (methods));
1576   TREE_STATIC (methods_decl) = 1;
1577   DECL_ARTIFICIAL (methods_decl) = 1;
1578   DECL_IGNORED_P (methods_decl) = 1;
1579   rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1580
1581   if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1582       && !flag_indirect_dispatch)
1583     {
1584       tree dtable = get_dispatch_table (type, this_class_addr);
1585       dtable_decl = build_dtable_decl (type);
1586       DECL_INITIAL (dtable_decl) = dtable;
1587       TREE_STATIC (dtable_decl) = 1;
1588       DECL_ARTIFICIAL (dtable_decl) = 1;
1589       DECL_IGNORED_P (dtable_decl) = 1;
1590       TREE_PUBLIC (dtable_decl) = 1;
1591       rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1592       if (type == class_type_node)
1593         class_dtable_decl = dtable_decl;
1594     }
1595
1596   if (class_dtable_decl == NULL_TREE)
1597     {
1598       class_dtable_decl = build_dtable_decl (class_type_node);
1599       TREE_STATIC (class_dtable_decl) = 1;
1600       DECL_ARTIFICIAL (class_dtable_decl) = 1;
1601       DECL_IGNORED_P (class_dtable_decl) = 1;
1602       if (is_compiled_class (class_type_node) != 2)
1603         DECL_EXTERNAL (class_dtable_decl) = 1;
1604       rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1605     }
1606
1607   super = CLASSTYPE_SUPER (type);
1608   if (super == NULL_TREE)
1609     super = null_pointer_node;
1610   else if (/* FIXME: we should also test for (!
1611               flag_indirect_dispatch) here, but libgcj can't cope with
1612               a symbolic reference a superclass in the class data.  */
1613            assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1614            && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1615     super = build_class_ref (super);
1616   else
1617     {
1618       int super_index = alloc_class_constant (super);
1619       super = build_int_2 (super_index, 0);
1620       TREE_TYPE (super) = ptr_type_node;
1621     }
1622
1623   /* Build and emit the array of implemented interfaces. */
1624   if (type != object_type_node)
1625       interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1626   if (interface_len > 0)
1627     {
1628       tree init = NULL_TREE;
1629       int i;
1630       tree interface_array_type, idecl;
1631       interface_array_type
1632         = build_prim_array_type (class_ptr_type, interface_len);
1633       idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1634                           interface_array_type);
1635       for (i = interface_len;  i > 0; i--)
1636         {
1637           tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1638           tree iclass = BINFO_TYPE (child);
1639           tree index;
1640           if (! flag_indirect_dispatch
1641               && (assume_compiled 
1642                   (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1643             index = build_class_ref (iclass);
1644           else
1645             {
1646               int int_index = alloc_class_constant (iclass);
1647               index = build_int_2 (int_index, 0);
1648               TREE_TYPE (index) = ptr_type_node;
1649             }
1650           init = tree_cons (NULL_TREE, index, init); 
1651         }
1652       DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1653       TREE_STATIC (idecl) = 1;
1654       DECL_ARTIFICIAL (idecl) = 1;
1655       DECL_IGNORED_P (idecl) = 1;
1656       interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1657       rest_of_decl_compilation (idecl,  (char*) 0, 1, 0);
1658     }
1659
1660   constant_pool_constructor = build_constants_constructor ();
1661
1662   if (flag_indirect_dispatch)
1663     {
1664       TYPE_OTABLE_DECL (type) 
1665         = emit_symbol_table 
1666         (DECL_NAME (TYPE_OTABLE_DECL (type)), 
1667          TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type), 
1668          TYPE_OTABLE_SYMS_DECL (type), integer_type_node);
1669        
1670       TYPE_ATABLE_DECL (type) 
1671         = emit_symbol_table 
1672         (DECL_NAME (TYPE_ATABLE_DECL (type)), 
1673          TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type), 
1674          TYPE_ATABLE_SYMS_DECL (type), ptr_type_node);
1675     }
1676   
1677   TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1678
1679   START_RECORD_CONSTRUCTOR (temp, object_type_node);
1680   PUSH_FIELD_VALUE (temp, "vtable",
1681                     build (PLUS_EXPR, dtable_ptr_type,
1682                            build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1683                            dtable_start_offset));
1684   if (! flag_hash_synchronization)
1685     PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1686   FINISH_RECORD_CONSTRUCTOR (temp);
1687   START_RECORD_CONSTRUCTOR (cons, class_type_node);
1688   PUSH_SUPER_VALUE (cons, temp);
1689   PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1690   PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1691   PUSH_FIELD_VALUE (cons, "accflags",
1692                     build_int_2 (get_access_flags_from_decl (type_decl), 0));
1693
1694   PUSH_FIELD_VALUE (cons, "superclass", 
1695                     CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1696   PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1697   PUSH_FIELD_VALUE (cons, "methods",
1698                     build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1699   PUSH_FIELD_VALUE (cons, "method_count",  build_int_2 (method_count, 0));
1700
1701   if (flag_indirect_dispatch)
1702     PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1703   else
1704     PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1705     
1706   PUSH_FIELD_VALUE (cons, "fields",
1707                     fields_decl == NULL_TREE ? null_pointer_node
1708                     : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1709   PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1710   PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1711   PUSH_FIELD_VALUE (cons, "static_field_count",
1712                     build_int_2 (static_field_count, 0));
1713
1714   if (flag_indirect_dispatch)
1715     PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1716   else
1717     PUSH_FIELD_VALUE (cons, "vtable",
1718                       dtable_decl == NULL_TREE ? null_pointer_node
1719                       : build (PLUS_EXPR, dtable_ptr_type,
1720                                build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1721                                dtable_start_offset));
1722   if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1723     {
1724       PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1725       PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1726     }
1727   else
1728     {
1729       PUSH_FIELD_VALUE (cons, "otable",
1730                         build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1731       PUSH_FIELD_VALUE (cons, "otable_syms",
1732                         build1 (ADDR_EXPR, symbols_array_ptr_type,
1733                                 TYPE_OTABLE_SYMS_DECL (type)));
1734       TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1735       TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1736     }
1737   if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1738     {
1739       PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1740       PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1741     }
1742   else
1743     {
1744       PUSH_FIELD_VALUE (cons, "atable",
1745                         build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1746       PUSH_FIELD_VALUE (cons, "atable_syms",
1747                         build1 (ADDR_EXPR, symbols_array_ptr_type,
1748                                 TYPE_ATABLE_SYMS_DECL (type)));
1749       TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1750       TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1751     }
1752  
1753   PUSH_FIELD_VALUE (cons, "catch_classes",
1754                     build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type))); 
1755   PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1756   PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1757   PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1758   PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1759
1760   PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1761   PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1762   PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1763   PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1764   PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1765   PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1766   PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1767   PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1768   PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1769
1770   FINISH_RECORD_CONSTRUCTOR (cons);
1771
1772   DECL_INITIAL (decl) = cons;
1773   
1774   /* Hash synchronization requires at least 64-bit alignment. */
1775   if (flag_hash_synchronization && POINTER_SIZE < 64)
1776     DECL_ALIGN (decl) = 64; 
1777   
1778   rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1779 }
1780
1781 void
1782 finish_class (void)
1783 {
1784   java_expand_catch_classes (current_class);
1785
1786   current_function_decl = NULL_TREE;
1787   make_class_data (current_class);
1788   register_class ();
1789   rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1790 }
1791
1792 /* Return 2 if CLASS is compiled by this compilation job;
1793    return 1 if CLASS can otherwise be assumed to be compiled;
1794    return 0 if we cannot assume that CLASS is compiled.
1795    Returns 1 for primitive and 0 for array types.  */
1796 int
1797 is_compiled_class (tree class)
1798 {
1799   int seen_in_zip;
1800   if (TREE_CODE (class) == POINTER_TYPE)
1801     class = TREE_TYPE (class);
1802   if (TREE_CODE (class) != RECORD_TYPE)  /* Primitive types are static. */
1803     return 1;
1804   if (TYPE_ARRAY_P (class))
1805     return 0;
1806   if (class == current_class)
1807     return 2;
1808
1809   seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1810   if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1811     {
1812       /* The class was seen in the current ZIP file and will be
1813          available as a compiled class in the future but may not have
1814          been loaded already. Load it if necessary. This prevent
1815          build_class_ref () from crashing. */
1816
1817       if (seen_in_zip && !CLASS_LOADED_P (class))
1818         load_class (class, 1);
1819
1820       /* We return 2 for class seen in ZIP and class from files
1821          belonging to the same compilation unit */
1822       return 2;
1823     }
1824
1825   if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1826     {
1827       if (!CLASS_LOADED_P (class))
1828         {
1829           if (CLASS_FROM_SOURCE_P (class))
1830             safe_layout_class (class);
1831           else
1832             load_class (class, 1);
1833         }
1834       return 1;
1835     }
1836
1837   return 0;
1838 }
1839
1840 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1841
1842 tree
1843 build_dtable_decl (tree type)
1844 {
1845   tree dtype;
1846
1847   /* We need to build a new dtable type so that its size is uniquely
1848      computed when we're dealing with the class for real and not just
1849      faking it (like java.lang.Class during the initialization of the
1850      compiler.) We know we're not faking a class when CURRENT_CLASS is
1851      TYPE. */
1852   if (current_class == type)
1853     {
1854       tree dummy = NULL_TREE;
1855       int n;
1856
1857       dtype = make_node (RECORD_TYPE);
1858
1859       PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1860       PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1861
1862       PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1863       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1864         {
1865           tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1866           TREE_CHAIN (dummy) = tmp_field;
1867           DECL_CONTEXT (tmp_field) = dtype;
1868           DECL_ARTIFICIAL (tmp_field) = 1;
1869           dummy = tmp_field;
1870         }
1871
1872       PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1873       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1874         {
1875           tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1876           TREE_CHAIN (dummy) = tmp_field;
1877           DECL_CONTEXT (tmp_field) = dtype;
1878           DECL_ARTIFICIAL (tmp_field) = 1;
1879           dummy = tmp_field;
1880         }
1881
1882       n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1883       if (TARGET_VTABLE_USES_DESCRIPTORS)
1884         n *= TARGET_VTABLE_USES_DESCRIPTORS;
1885
1886       PUSH_FIELD (dtype, dummy, "methods",
1887                   build_prim_array_type (nativecode_ptr_type_node, n));
1888       layout_type (dtype);
1889     }
1890   else
1891     dtype = dtable_type;
1892
1893   return build_decl (VAR_DECL, 
1894                      java_mangle_vtable (&temporary_obstack, type), dtype);
1895 }
1896
1897 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1898    fields inherited from SUPER_CLASS. */
1899
1900 void
1901 push_super_field (tree this_class, tree super_class)
1902 {
1903   tree base_decl;
1904   /* Don't insert the field if we're just re-laying the class out. */ 
1905   if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1906     return;
1907   base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1908   DECL_IGNORED_P (base_decl) = 1;
1909   TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1910   TYPE_FIELDS (this_class) = base_decl;
1911   DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1912   DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1913 }
1914
1915 /* Handle the different manners we may have to lay out a super class.  */
1916
1917 static tree
1918 maybe_layout_super_class (tree super_class, tree this_class)
1919 {
1920   if (TREE_CODE (super_class) == RECORD_TYPE)
1921     {
1922       if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1923         safe_layout_class (super_class);
1924       if (!CLASS_LOADED_P (super_class))
1925         load_class (super_class, 1);
1926     }
1927   /* We might have to layout the class before its dependency on
1928      the super class gets resolved by java_complete_class  */
1929   else if (TREE_CODE (super_class) == POINTER_TYPE)
1930     {
1931       if (TREE_TYPE (super_class) != NULL_TREE)
1932         super_class = TREE_TYPE (super_class);
1933       else
1934         {
1935           /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
1936              we give it one.  */
1937           tree this_wrap = NULL_TREE;
1938
1939           if (this_class)
1940             {
1941               tree this_decl = TYPE_NAME (this_class);
1942               this_wrap = build_expr_wfl (this_class,
1943                                           DECL_SOURCE_FILE (this_decl),
1944                                           DECL_SOURCE_LINE (this_decl), 0);
1945             }
1946           super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1947                                           super_class, NULL_TREE, this_wrap);
1948           if (!super_class)
1949             return NULL_TREE;   /* FIXME, NULL_TREE not checked by caller. */
1950           super_class = TREE_TYPE (super_class);
1951         }
1952     }
1953   if (!TYPE_SIZE (super_class))
1954     safe_layout_class (super_class);
1955
1956   return super_class;
1957 }
1958
1959 void
1960 layout_class (tree this_class)
1961 {
1962   tree super_class = CLASSTYPE_SUPER (this_class);
1963   tree field;
1964
1965   class_list = tree_cons (this_class, NULL_TREE, class_list);
1966   if (CLASS_BEING_LAIDOUT (this_class))
1967     {
1968       char buffer [1024];
1969       char *report;
1970       tree current;
1971
1972       sprintf (buffer, " with `%s'",
1973                IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1974       obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1975
1976       for (current = TREE_CHAIN (class_list); current; 
1977            current = TREE_CHAIN (current))
1978         {
1979           tree decl = TYPE_NAME (TREE_PURPOSE (current));
1980           sprintf (buffer, "\n  which inherits from `%s' (%s:%d)",
1981                    IDENTIFIER_POINTER (DECL_NAME (decl)),
1982                    DECL_SOURCE_FILE (decl),
1983                    DECL_SOURCE_LINE (decl));
1984           obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1985         }
1986       obstack_1grow (&temporary_obstack, '\0');
1987       report = obstack_finish (&temporary_obstack);
1988       cyclic_inheritance_report = ggc_strdup (report);
1989       obstack_free (&temporary_obstack, report);
1990       TYPE_SIZE (this_class) = error_mark_node;
1991       return;
1992     }
1993   CLASS_BEING_LAIDOUT (this_class) = 1;
1994
1995   if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1996     {
1997       tree maybe_super_class 
1998         = maybe_layout_super_class (super_class, this_class);
1999       if (maybe_super_class == NULL
2000           || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2001         {
2002           TYPE_SIZE (this_class) = error_mark_node;
2003           CLASS_BEING_LAIDOUT (this_class) = 0;
2004           class_list = TREE_CHAIN (class_list);
2005           return;
2006         }
2007       if (TYPE_SIZE (this_class) == NULL_TREE)
2008         push_super_field (this_class, maybe_super_class);
2009     }
2010
2011   for (field = TYPE_FIELDS (this_class);
2012        field != NULL_TREE;  field = TREE_CHAIN (field))
2013     {
2014       if (FIELD_STATIC (field))
2015         {
2016           /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2017           SET_DECL_ASSEMBLER_NAME (field,
2018                                    java_mangle_decl
2019                                    (&temporary_obstack, field));
2020         }
2021     }
2022
2023   layout_type (this_class);
2024
2025   /* Also recursively load/layout any superinterfaces, but only if
2026      class was loaded from bytecode.  The source parser will take care
2027      of this itself.  */
2028   if (!CLASS_FROM_SOURCE_P (this_class))
2029     {
2030       tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
2031
2032       if (basetype_vec)
2033         {
2034           int n = TREE_VEC_LENGTH (basetype_vec) - 1;
2035           int i;
2036           for (i = n; i > 0; i--)
2037             {
2038               tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
2039               tree super_interface = BINFO_TYPE (vec_elt);
2040
2041               tree maybe_super_interface 
2042                 = maybe_layout_super_class (super_interface, NULL_TREE);
2043               if (maybe_super_interface == NULL
2044                   || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2045                 {
2046                   TYPE_SIZE (this_class) = error_mark_node;
2047                   CLASS_BEING_LAIDOUT (this_class) = 0;
2048                   class_list = TREE_CHAIN (class_list);
2049                   return;
2050                 }
2051             }
2052         }
2053     }
2054
2055   /* Convert the size back to an SI integer value.  */
2056   TYPE_SIZE_UNIT (this_class) =
2057     fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2058
2059   CLASS_BEING_LAIDOUT (this_class) = 0;
2060   class_list = TREE_CHAIN (class_list);
2061 }
2062
2063 static void
2064 add_miranda_methods (tree base_class, tree search_class)
2065 {
2066   tree basetype_vec = TYPE_BINFO_BASETYPES (search_class);
2067   int i, n = TREE_VEC_LENGTH (basetype_vec);
2068   for (i = 1; i < n; ++i)
2069     {
2070       tree method_decl;
2071       tree elt = TREE_VEC_ELT (basetype_vec, i);
2072       if (elt == NULL_TREE)
2073         break;
2074       elt = BINFO_TYPE (elt);
2075
2076       /* Ensure that interface methods are seen in declared order.  */
2077       layout_class_methods (elt);
2078
2079       /* All base classes will have been laid out at this point, so the order 
2080          will be correct.  This code must match similar layout code in the 
2081          runtime.  */
2082       for (method_decl = TYPE_METHODS (elt);
2083            method_decl; method_decl = TREE_CHAIN (method_decl))
2084         {
2085           tree sig, override;
2086
2087           /* An interface can have <clinit>.  */
2088           if (ID_CLINIT_P (DECL_NAME (method_decl)))
2089             continue;
2090
2091           sig = build_java_argument_signature (TREE_TYPE (method_decl));
2092           override = lookup_argument_method (base_class,
2093                                              DECL_NAME (method_decl), sig);
2094           if (override == NULL_TREE)
2095             {
2096               /* Found a Miranda method.  Add it.  */
2097               tree new_method;
2098               sig = build_java_signature (TREE_TYPE (method_decl));
2099               new_method
2100                 = add_method (base_class,
2101                               get_access_flags_from_decl (method_decl),
2102                               DECL_NAME (method_decl), sig);
2103               METHOD_INVISIBLE (new_method) = 1;
2104             }
2105         }
2106
2107       /* Try superinterfaces.  */
2108       add_miranda_methods (base_class, elt);
2109     }
2110 }
2111
2112 void
2113 layout_class_methods (tree this_class)
2114 {
2115   tree method_decl, dtable_count;
2116   tree super_class, type_name;
2117
2118   if (TYPE_NVIRTUALS (this_class))
2119     return;
2120
2121   super_class = CLASSTYPE_SUPER (this_class);
2122
2123   if (super_class)
2124     {
2125       super_class = maybe_layout_super_class (super_class, this_class);
2126       if (!TYPE_NVIRTUALS (super_class))
2127         layout_class_methods (super_class);
2128       dtable_count = TYPE_NVIRTUALS (super_class);
2129     }
2130   else
2131     dtable_count = integer_zero_node;
2132
2133   type_name = TYPE_NAME (this_class);
2134   if (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name))
2135     {
2136       /* An abstract class can have methods which are declared only in
2137          an implemented interface.  These are called "Miranda
2138          methods".  We make a dummy method entry for such methods
2139          here.  */
2140       add_miranda_methods (this_class, this_class);
2141     }
2142
2143   TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2144
2145   for (method_decl = TYPE_METHODS (this_class);
2146        method_decl; method_decl = TREE_CHAIN (method_decl))
2147     dtable_count = layout_class_method (this_class, super_class,
2148                                         method_decl, dtable_count);
2149
2150   TYPE_NVIRTUALS (this_class) = dtable_count;
2151 }
2152
2153 /* Return the index of METHOD in INTERFACE.  This index begins at 1 and is used as an
2154    argument for _Jv_LookupInterfaceMethodIdx(). */
2155 int
2156 get_interface_method_index (tree method, tree interface)
2157 {
2158   tree meth;
2159   int i = 1;
2160
2161   for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2162     {
2163       if (meth == method)
2164         return i;
2165       if (meth == NULL_TREE)
2166         abort ();
2167     }
2168 }
2169
2170 /* Lay METHOD_DECL out, returning a possibly new value of
2171    DTABLE_COUNT. Also mangle the method's name. */
2172
2173 tree
2174 layout_class_method (tree this_class, tree super_class,
2175                      tree method_decl, tree dtable_count)
2176 {
2177   tree method_name = DECL_NAME (method_decl);
2178
2179   TREE_PUBLIC (method_decl) = 1;
2180   /* Considered external until we know what classes are being
2181      compiled into this object file.  */
2182   DECL_EXTERNAL (method_decl) = 1;
2183
2184   /* This is a good occasion to mangle the method's name */
2185   SET_DECL_ASSEMBLER_NAME (method_decl,
2186                            java_mangle_decl (&temporary_obstack, 
2187                                              method_decl));
2188   /* We don't generate a RTL for the method if it's abstract, or if
2189      it's an interface method that isn't clinit. */
2190   if (! METHOD_ABSTRACT (method_decl) 
2191       || (CLASS_INTERFACE (TYPE_NAME (this_class)) 
2192           && (DECL_CLINIT_P (method_decl))))
2193     make_decl_rtl (method_decl, NULL);
2194
2195   if (ID_INIT_P (method_name))
2196     {
2197       const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2198       const char *ptr;
2199       for (ptr = p; *ptr; )
2200         {
2201           if (*ptr++ == '.')
2202             p = ptr;
2203         }
2204       DECL_CONSTRUCTOR_P (method_decl) = 1;
2205       build_java_argument_signature (TREE_TYPE (method_decl));
2206     }
2207   else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2208     {
2209       tree method_sig =
2210         build_java_argument_signature (TREE_TYPE (method_decl));
2211       tree super_method = lookup_argument_method (super_class, method_name,
2212                                                   method_sig);
2213       if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2214         {
2215           tree method_index = get_method_index (super_method);
2216           set_method_index (method_decl, method_index);
2217           if (method_index == NULL_TREE 
2218               && !CLASS_FROM_SOURCE_P (this_class))
2219             error ("%Jnon-static method '%D' overrides static method",
2220                    method_decl, method_decl);
2221         }
2222       else if (! METHOD_FINAL (method_decl)
2223                && ! METHOD_PRIVATE (method_decl)
2224                && ! CLASS_FINAL (TYPE_NAME (this_class))
2225                && dtable_count)
2226         {
2227           set_method_index (method_decl, dtable_count);
2228           dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2229                                       dtable_count, integer_one_node));
2230         }
2231     }
2232
2233   return dtable_count;
2234 }
2235
2236 void
2237 register_class (void)
2238 {
2239   /* END does not need to be registered with the garbage collector
2240      because it always points into the list given by REGISTERED_CLASS,
2241      and that variable is registered with the collector.  */
2242   static tree end;
2243   tree node    = TREE_OPERAND (build_class_ref (current_class), 0);
2244   tree current = copy_node (node);
2245
2246   XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2247   if (!registered_class)
2248     registered_class = current;
2249   else
2250     TREE_CHAIN (end) = current;
2251
2252   end = current;
2253 }
2254
2255 /* Emit something to register classes at start-up time.
2256
2257    The preferred mechanism is through the .jcr section, which contain
2258    a list of pointers to classes which get registered during
2259    constructor invocation time.  The fallback mechanism is to generate
2260    a `constructor' function which calls _Jv_RegisterClass for each
2261    class in this file.  */
2262
2263 void
2264 emit_register_classes (void)
2265 {
2266   /* ??? This isn't quite the correct test.  We also have to know
2267      that the target is using gcc's crtbegin/crtend objects rather
2268      than the ones that come with the operating system.  */
2269   if (SUPPORTS_WEAK && targetm.have_named_sections)
2270     {
2271 #ifdef JCR_SECTION_NAME
2272       tree t;
2273       named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2274       assemble_align (POINTER_SIZE);
2275       for (t = registered_class; t; t = TREE_CHAIN (t))
2276         assemble_integer (XEXP (DECL_RTL (t), 0),
2277                           POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2278 #else
2279       abort ();
2280 #endif
2281     }
2282   else
2283     {
2284       extern tree get_file_function_name (int);
2285       tree init_name = get_file_function_name ('I');
2286       tree init_type = build_function_type (void_type_node, end_params_node);
2287       tree init_decl;
2288       tree t;
2289       location_t saved_loc = input_location;
2290       
2291       init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2292       SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2293       DECL_SOURCE_LINE (init_decl) = 0;
2294       TREE_STATIC (init_decl) = 1;
2295       current_function_decl = init_decl;
2296       DECL_INLINE (init_decl) = 0;
2297       DECL_UNINLINABLE (init_decl) = 1;
2298       DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2299                                             void_type_node);
2300
2301       /* It can be a static function as long as collect2 does not have
2302          to scan the object file to find its ctor/dtor routine.  */
2303       TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2304
2305       /* Suppress spurious warnings.  */
2306       TREE_USED (init_decl) = 1;
2307
2308       pushlevel (0);
2309       make_decl_rtl (init_decl, NULL);
2310       init_function_start (init_decl);
2311       expand_function_start (init_decl, 0);
2312
2313       for ( t = registered_class; t; t = TREE_CHAIN (t))
2314         emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2315                            XEXP (DECL_RTL (t), 0), Pmode);
2316       input_location = DECL_SOURCE_LOCATION (init_decl);
2317       expand_function_end ();
2318       poplevel (1, 0, 1);
2319       rest_of_compilation (init_decl);
2320       current_function_decl = NULL_TREE;
2321
2322       if (targetm.have_ctors_dtors)
2323         (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2324                                          DEFAULT_INIT_PRIORITY);
2325       input_location = saved_loc;
2326     }
2327 }
2328
2329 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2330
2331 static tree
2332 build_symbol_entry (tree decl)
2333 {
2334   tree clname, name, signature, sym;
2335   
2336   clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2337   name = build_utf8_ref (DECL_NAME (decl));
2338   signature = build_java_signature (TREE_TYPE (decl));
2339   signature = build_utf8_ref (unmangle_classname 
2340                               (IDENTIFIER_POINTER (signature),
2341                                IDENTIFIER_LENGTH (signature)));
2342
2343   START_RECORD_CONSTRUCTOR (sym, symbol_type);
2344   PUSH_FIELD_VALUE (sym, "clname", clname);
2345   PUSH_FIELD_VALUE (sym, "name", name);
2346   PUSH_FIELD_VALUE (sym, "signature", signature);
2347   FINISH_RECORD_CONSTRUCTOR (sym);
2348   TREE_CONSTANT (sym) = 1;
2349   TREE_INVARIANT (sym) = 1;
2350
2351   return sym;
2352
2353
2354 /* Emit a symbol table: used by -findirect-dispatch.  */
2355
2356 tree
2357 emit_symbol_table (tree name, tree the_table, tree decl_list, tree the_syms_decl, 
2358                           tree the_array_element_type)
2359 {
2360   tree method_list, method, table, list, null_symbol;
2361   tree table_size, the_array_type;
2362   int index;
2363   
2364   /* Only emit a table if this translation unit actually made any
2365      references via it. */
2366   if (decl_list == NULL_TREE)
2367     return the_table;
2368
2369   /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2370   index = 0;
2371   method_list = decl_list;
2372   list = NULL_TREE;  
2373   while (method_list != NULL_TREE)
2374     {
2375       method = TREE_VALUE (method_list);
2376       list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2377       method_list = TREE_CHAIN (method_list);
2378       index++;
2379     }
2380
2381   /* Terminate the list with a "null" entry. */
2382   START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2383   PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2384   PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2385   PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2386   FINISH_RECORD_CONSTRUCTOR (null_symbol);
2387   TREE_CONSTANT (null_symbol) = 1;  
2388   TREE_INVARIANT (null_symbol) = 1;  
2389   list = tree_cons (NULL_TREE, null_symbol, list);
2390
2391   /* Put the list in the right order and make it a constructor. */
2392   list = nreverse (list);
2393   table = build_constructor (symbols_array_type, list);  
2394
2395   /* Make it the initial value for otable_syms and emit the decl. */
2396   DECL_INITIAL (the_syms_decl) = table;
2397   DECL_ARTIFICIAL (the_syms_decl) = 1;
2398   DECL_IGNORED_P (the_syms_decl) = 1;
2399   rest_of_decl_compilation (the_syms_decl, NULL, 1, 0);
2400   
2401   /* Now that its size is known, redefine the table as an
2402      uninitialized static array of INDEX + 1 elements. The extra entry
2403      is used by the runtime to track whether the table has been
2404      initialized. */
2405   table_size = build_index_type (build_int_2 (index, 0));
2406   the_array_type = build_array_type (the_array_element_type, table_size);
2407   the_table = build_decl (VAR_DECL, name, the_array_type);
2408   TREE_STATIC (the_table) = 1;
2409   TREE_READONLY (the_table) = 1;  
2410   rest_of_decl_compilation (the_table, NULL, 1, 0);
2411
2412   return the_table;
2413 }
2414
2415 /* make an entry for the catch_classes list.  */
2416 tree
2417 make_catch_class_record (tree catch_class, tree classname)
2418 {
2419   tree entry;
2420   tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2421   START_RECORD_CONSTRUCTOR (entry, type);
2422   PUSH_FIELD_VALUE (entry, "address", catch_class);
2423   PUSH_FIELD_VALUE (entry, "classname", classname);
2424   FINISH_RECORD_CONSTRUCTOR (entry);
2425   return entry;
2426 }
2427
2428
2429 /* Generate the list of Throwable classes that are caught by exception
2430    handlers in this class.  */
2431 tree 
2432 emit_catch_table (tree this_class)
2433 {
2434   tree table, table_size, array_type;
2435   TYPE_CATCH_CLASSES (this_class) =
2436     tree_cons (NULL,
2437                make_catch_class_record (null_pointer_node, null_pointer_node),
2438                TYPE_CATCH_CLASSES (this_class));
2439   TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2440   TYPE_CATCH_CLASSES (this_class) = 
2441     tree_cons (NULL,
2442                make_catch_class_record (null_pointer_node, null_pointer_node),
2443                TYPE_CATCH_CLASSES (this_class));
2444   table_size = 
2445     build_index_type (build_int_2 
2446                       (list_length (TYPE_CATCH_CLASSES (this_class)), 0));
2447   array_type 
2448     = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2449                         table_size);
2450   table = 
2451     build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2452   DECL_INITIAL (table) = 
2453     build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2454   TREE_STATIC (table) = 1;
2455   TREE_READONLY (table) = 1;  
2456   DECL_IGNORED_P (table) = 1;
2457   rest_of_decl_compilation (table, NULL, 1, 0);
2458   return table;
2459 }
2460  
2461
2462 void
2463 init_class_processing (void)
2464 {
2465   registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2466   fields_ident = get_identifier ("fields");
2467   info_ident = get_identifier ("info");
2468   gcc_obstack_init (&temporary_obstack);
2469 }
2470 \f
2471 static hashval_t java_treetreehash_hash (const void *);
2472 static int java_treetreehash_compare (const void *, const void *);
2473
2474 /* A hash table mapping trees to trees.  Used generally.  */
2475
2476 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2477
2478 static hashval_t
2479 java_treetreehash_hash (const void *k_p)
2480 {
2481   struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2482   return JAVA_TREEHASHHASH_H (k->key);
2483 }
2484
2485 static int
2486 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2487 {
2488   struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2489   tree k2 = (tree) k2_p;
2490   return (k1->key == k2);
2491 }
2492
2493 tree 
2494 java_treetreehash_find (htab_t ht, tree t)
2495 {
2496   struct treetreehash_entry *e;
2497   hashval_t hv = JAVA_TREEHASHHASH_H (t);
2498   e = htab_find_with_hash (ht, t, hv);
2499   if (e == NULL)
2500     return NULL;
2501   else
2502     return e->value;
2503 }
2504
2505 tree *
2506 java_treetreehash_new (htab_t ht, tree t)
2507 {
2508   void **e;
2509   struct treetreehash_entry *tthe;
2510   hashval_t hv = JAVA_TREEHASHHASH_H (t);
2511
2512   e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2513   if (*e == NULL)
2514     {
2515       tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2516       tthe->key = t;
2517       *e = tthe;
2518     }
2519   else
2520     tthe = (struct treetreehash_entry *) *e;
2521   return &tthe->value;
2522 }
2523
2524 htab_t
2525 java_treetreehash_create (size_t size, int gc)
2526 {
2527   if (gc)
2528     return htab_create_ggc (size, java_treetreehash_hash,
2529                             java_treetreehash_compare, NULL);
2530   else
2531     return htab_create_alloc (size, java_treetreehash_hash,
2532                               java_treetreehash_compare, free, xcalloc, free);
2533 }
2534
2535 #include "gt-java-class.h"