OSDN Git Service

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