OSDN Git Service

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