OSDN Git Service

* system.h: Include "safe-ctype.h" instead of <safe-ctype.h>.
[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    2005, 2006, 2007, 2008, 2010 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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "java-tree.h"
33 #include "jcf.h"
34 #include "obstack.h"
35 #include "diagnostic-core.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "parse.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "cgraph.h"
42 #include "tree-iterator.h"
43 #include "vecprim.h"
44 #include "tm.h"         /* FIXME: For gcc_obstack_init from defaults.h.  */
45 #include "target.h"
46
47 static tree make_method_value (tree);
48 static tree build_java_method_type (tree, tree, int);
49 static int32 hashUtf8String (const char *, int);
50 static tree make_field_value (tree);
51 static tree get_dispatch_vector (tree);
52 static tree get_dispatch_table (tree, tree);
53 static int supers_all_compiled (tree type);
54 static tree maybe_layout_super_class (tree, tree);
55 static void add_miranda_methods (tree, tree);
56 static int assume_compiled (const char *);
57 static tree build_symbol_entry (tree, tree);
58 static tree emit_assertion_table (tree);
59 static void register_class (void);
60
61 struct obstack temporary_obstack;
62
63 static const char *cyclic_inheritance_report;
64
65 /* The compiler generates different code depending on whether or not
66    it can assume certain classes have been compiled down to native
67    code or not.  The compiler options -fassume-compiled= and
68    -fno-assume-compiled= are used to create a tree of
69    class_flag_node objects.  This tree is queried to determine if
70    a class is assume to be compiled or not.  Each node in the tree
71    represents either a package or a specific class.  */
72
73 typedef struct class_flag_node_struct
74 {
75   /* The class or package name.  */
76   const char *ident;
77
78   /* Nonzero if this represents an exclusion.  */
79   int value;
80
81   /* Pointers to other nodes in the tree.  */
82   struct class_flag_node_struct *parent;
83   struct class_flag_node_struct *sibling;
84   struct class_flag_node_struct *child;
85 } class_flag_node;
86
87 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
88 static void add_class_flag (class_flag_node **, const char *, int);
89
90 /* This is the root of the include/exclude tree.  */
91
92 static class_flag_node *assume_compiled_tree;
93
94 static class_flag_node *enable_assert_tree;
95
96 static GTY(()) tree class_roots[4];
97 #define fields_ident class_roots[0]  /* get_identifier ("fields") */
98 #define info_ident class_roots[1]  /* get_identifier ("info") */
99 #define class_list class_roots[2]
100 #define class_dtable_decl class_roots[3]
101
102 static GTY(()) VEC(tree,gc) *registered_class;
103
104 /* A tree that returns the address of the class$ of the class
105    currently being compiled.  */
106 static GTY(()) tree this_classdollar;
107
108 /* A list of static class fields.  This is to emit proper debug
109    info for them.  */
110 VEC(tree,gc) *pending_static_fields;
111
112 /* Return the node that most closely represents the class whose name
113    is IDENT.  Start the search from NODE (followed by its siblings).
114    Return NULL if an appropriate node does not exist.  */
115
116 static class_flag_node *
117 find_class_flag_node (class_flag_node *node, const char *ident)
118 {
119   while (node)
120     {
121       size_t node_ident_length = strlen (node->ident);
122
123       /* node_ident_length is zero at the root of the tree.  If the
124          identifiers are the same length, then we have matching
125          classes.  Otherwise check if we've matched an enclosing
126          package name.  */
127
128       if (node_ident_length == 0
129           || (strncmp (ident, node->ident, node_ident_length) == 0
130               && (ident[node_ident_length] == '\0'
131                   || ident[node_ident_length] == '.')))
132         {
133           /* We've found a match, however, there might be a more
134              specific match.  */
135
136           class_flag_node *found = find_class_flag_node (node->child, ident);
137           if (found)
138             return found;
139           else
140             return node;
141         }
142
143       /* No match yet.  Continue through the sibling list.  */
144       node = node->sibling;
145     }
146
147   /* No match at all in this tree.  */
148   return NULL;
149 }
150
151 void
152 add_class_flag (class_flag_node **rootp, const char *ident, int value)
153 {
154   class_flag_node *root = *rootp;
155   class_flag_node *parent, *node;
156
157   /* Create the root of the tree if it doesn't exist yet.  */
158
159   if (NULL == root)
160     {
161       root = XNEW (class_flag_node);
162       root->ident = "";
163       root->value = 0;
164       root->sibling = NULL;
165       root->child = NULL;
166       root->parent = NULL;
167       *rootp = root;
168     }
169
170   /* Calling the function with the empty string means we're setting
171      value for the root of the hierarchy.  */
172
173   if (0 == ident[0])
174     {
175       root->value = value;
176       return;
177     }
178
179   /* Find the parent node for this new node.  PARENT will either be a
180      class or a package name.  Adjust PARENT accordingly.  */
181
182   parent = find_class_flag_node (root, ident);
183   if (strcmp (ident, parent->ident) == 0)
184     parent->value = value;
185   else
186     {
187       /* Insert new node into the tree.  */
188       node = XNEW (class_flag_node);
189
190       node->ident = xstrdup (ident);
191       node->value = value;
192       node->child = NULL;
193
194       node->parent = parent;
195       node->sibling = parent->child;
196       parent->child = node;
197     }
198 }
199
200 /* Add a new IDENT to the include/exclude tree.  It's an exclusion
201    if EXCLUDEP is nonzero.  */
202
203 void
204 add_assume_compiled (const char *ident, int excludep)
205 {
206   add_class_flag (&assume_compiled_tree, ident, excludep);
207 }
208
209 /* The default value returned by enable_assertions. */
210
211 #define DEFAULT_ENABLE_ASSERT (optimize == 0)
212
213 /* Enter IDENT (a class or package name) into the enable-assertions table.
214    VALUE is true to enable and false to disable. */
215
216 void
217 add_enable_assert (const char *ident, int value)
218 {
219   if (enable_assert_tree == NULL)
220     add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
221   add_class_flag (&enable_assert_tree, ident, value);
222 }
223
224 /* Returns nonzero if IDENT is the name of a class that the compiler
225    should assume has been compiled to object code.  */
226
227 static int
228 assume_compiled (const char *ident)
229 {
230   class_flag_node *i;
231   int result;
232   
233   if (NULL == assume_compiled_tree)
234     return 1;
235
236   i = find_class_flag_node (assume_compiled_tree, ident);
237
238   result = ! i->value;
239   
240   return (result);
241 }
242
243 /* Return true if we should generate code to check assertions within KLASS. */
244
245 bool
246 enable_assertions (tree klass)
247 {
248   /* Check if command-line specifies whether we should check assertions. */
249
250   if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
251     {
252       const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
253       class_flag_node *node
254         = find_class_flag_node (enable_assert_tree, ident);
255       return node->value;
256     }
257
258   /* The default is to enable assertions if generating class files,
259      or not optimizing. */
260   return DEFAULT_ENABLE_ASSERT;
261 }
262
263 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
264    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
265    Also, PREFIX is prepended, and SUFFIX is appended. */
266
267 tree
268 ident_subst (const char* old_name,
269              int old_length,
270              const char *prefix,
271              int old_char,
272              int new_char,
273              const char *suffix)
274 {
275   int prefix_len = strlen (prefix);
276   int suffix_len = strlen (suffix);
277   int i = prefix_len + old_length + suffix_len + 1;
278   char *buffer = (char *) alloca (i);
279
280   strcpy (buffer, prefix);
281   for (i = 0; i < old_length; i++)
282     {
283       char ch = old_name[i];
284       if (ch == old_char)
285         ch = new_char;
286       buffer[prefix_len + i] = ch;
287     }
288   strcpy (buffer + prefix_len + old_length, suffix);
289   return get_identifier (buffer);
290 }
291
292 /* Return an IDENTIFIER_NODE the same as OLD_ID,
293    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
294    Also, PREFIX is prepended, and SUFFIX is appended. */
295
296 tree
297 identifier_subst (const tree old_id,
298                   const char *prefix,
299                   int old_char,
300                   int new_char,
301                   const char *suffix)
302 {
303   return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
304                       prefix, old_char, new_char, suffix);
305 }
306
307 /* Generate a valid C identifier from the name of the class TYPE,
308    prefixed by PREFIX. */
309
310 tree
311 mangled_classname (const char *prefix, tree type)
312 {
313   tree result;
314   tree ident = TYPE_NAME (type);
315   if (TREE_CODE (ident) != IDENTIFIER_NODE)
316     ident = DECL_NAME (ident);
317   result = identifier_subst (ident, prefix, '.', '_', "");
318
319   /* Replace any characters that aren't in the set [0-9a-zA-Z_$] with
320      "_0xXX".  Class names containing such chracters are uncommon, but
321      they do sometimes occur in class files.  Without this check,
322      these names cause assembly errors.
323
324      There is a possibility that a real class name could conflict with
325      the identifier we generate, but it is unlikely and will
326      immediately be detected as an assembler error.  At some point we
327      should do something more elaborate (perhaps using the full
328      unicode mangling scheme) in order to prevent such a conflict.  */
329   {
330     int i;
331     const int len = IDENTIFIER_LENGTH (result);
332     const char *p = IDENTIFIER_POINTER (result);
333     int illegal_chars = 0;
334
335     /* Make two passes over the identifier.  The first pass is merely
336        to count illegal characters; we need to do this in order to
337        allocate a buffer.  */
338     for (i = 0; i < len; i++)
339       {
340         char c = p[i];
341         illegal_chars += (! ISALNUM (c) && c != '_' && c != '$');
342       }
343
344     /* And the second pass, which is rarely executed, does the
345        rewriting.  */
346     if (illegal_chars != 0)
347       {
348         char *buffer = (char *) alloca (illegal_chars * 4 + len + 1);
349         int j;
350
351         for (i = 0, j = 0; i < len; i++)
352           {
353             char c = p[i];
354             if (! ISALNUM (c) && c != '_' && c != '$')
355               {
356                 buffer[j++] = '_';
357                 sprintf (&buffer[j], "0x%02x", c);
358                 j += 4;
359               }
360             else
361               buffer[j++] = c;
362           }
363
364         buffer[j] = 0;
365         result = get_identifier (buffer);
366       }
367   }
368
369   return result;
370 }
371
372 tree
373 make_class (void)
374 {
375   tree type;
376   type = make_node (RECORD_TYPE);
377   /* Unfortunately we must create the binfo here, so that class
378      loading works.  */
379   TYPE_BINFO (type) = make_tree_binfo (0);
380   MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
381   TYPE_CATCH_CLASSES (type) = NULL;
382   /* Push a dummy entry; we can't call make_catch_class_record here
383      because other infrastructure may not be set up yet.  We'll come
384      back and fill it in later once said infrastructure is
385      initialized.  */
386   CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (type), NULL_TREE, NULL_TREE);
387
388   return type;
389 }
390
391 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
392    and where each of the constituents is separated by '/',
393    return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
394
395 tree
396 unmangle_classname (const char *name, int name_length)
397 {
398   tree to_return = ident_subst (name, name_length, "", '/', '.', "");
399   /* It's not sufficient to compare to_return and get_identifier
400      (name) to determine whether to_return is qualified. There are
401      cases in signature analysis where name will be stripped of a
402      trailing ';'. */
403   name = IDENTIFIER_POINTER (to_return);
404   while (*name)
405     if (*name++ == '.') 
406       {
407         QUALIFIED_P (to_return) = 1;
408         break;
409       }
410   
411   return to_return;
412 }
413
414 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE)                        \
415 do                                                                      \
416 {                                                                       \
417   const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
418   char *buf = (char *) alloca (strlen (type_name)                       \
419                                + strlen (#NAME "_syms_") + 1);          \
420   tree decl;                                                            \
421                                                                         \
422   sprintf (buf, #NAME "_%s", type_name);                                \
423   TYPE_## TABLE ##_DECL (type) = decl =                                 \
424     build_decl (input_location, VAR_DECL, get_identifier (buf), TABLE_TYPE); \
425   DECL_EXTERNAL (decl) = 1;                                             \
426   TREE_STATIC (decl) = 1;                                               \
427   TREE_READONLY (decl) = 1;                                             \
428   TREE_CONSTANT (decl) = 1;                                             \
429   DECL_IGNORED_P (decl) = 1;                                            \
430   /* Mark the table as belonging to this class.  */                     \
431   pushdecl (decl);                                                      \
432   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);                           \
433   DECL_OWNER (decl) = TYPE;                                             \
434   sprintf (buf, #NAME "_syms_%s", type_name);                           \
435   TYPE_## TABLE ##_SYMS_DECL (TYPE) =                                   \
436     build_decl (input_location, VAR_DECL, get_identifier (buf), symbols_array_type); \
437   TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;                  \
438   TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;                \
439   DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;               \
440 }                                                                       \
441 while (0)
442
443 /* Given a class, create the DECLs for all its associated indirect
444    dispatch tables.  */
445 void
446 gen_indirect_dispatch_tables (tree type)
447 {
448   const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", type));
449   {  
450     tree field = NULL;
451     char *buf = (char *) alloca (strlen (type_name)
452                                  + strlen ("_catch_classes_") + 1);
453     tree catch_class_type = make_node (RECORD_TYPE);
454
455     sprintf (buf, "_catch_classes_%s", type_name);
456     PUSH_FIELD (input_location,
457                 catch_class_type, field, "address", utf8const_ptr_type);
458     PUSH_FIELD (input_location,
459                 catch_class_type, field, "classname", ptr_type_node);
460     FINISH_RECORD (catch_class_type);
461     
462     TYPE_CTABLE_DECL (type) 
463       = build_decl (input_location, VAR_DECL, get_identifier (buf),
464                     build_array_type (catch_class_type, 0));
465     DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
466     TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
467     TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
468     TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
469     DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
470     pushdecl (TYPE_CTABLE_DECL (type));  
471   }
472
473   if (flag_indirect_dispatch)
474     {
475       GEN_TABLE (ATABLE, _atable, atable_type, type);
476       GEN_TABLE (OTABLE, _otable, otable_type, type);
477       GEN_TABLE (ITABLE, _itable, itable_type, type);
478     }
479 }
480
481 #undef GEN_TABLE
482
483 tree
484 push_class (tree class_type, tree class_name)
485 {
486   tree decl, signature;
487   location_t saved_loc = input_location;
488   CLASS_P (class_type) = 1;
489   decl = build_decl (input_location, TYPE_DECL, class_name, class_type);
490   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
491
492   /* dbxout needs a DECL_SIZE if in gstabs mode */
493   DECL_SIZE (decl) = integer_zero_node;
494
495   input_location = saved_loc;
496   signature = identifier_subst (class_name, "L", '.', '/', ";");
497   IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
498
499   /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
500      both a typedef and in the struct name-space.  We may want to re-visit
501      this later, but for now it reduces the changes needed for gdb. */
502   DECL_ARTIFICIAL (decl) = 1;
503
504   pushdecl_top_level (decl);
505
506   return decl;
507 }
508
509 /* Finds the (global) class named NAME.  Creates the class if not found.
510    Also creates associated TYPE_DECL.
511    Does not check if the class actually exists, load the class,
512    fill in field or methods, or do layout_type. */
513
514 tree
515 lookup_class (tree name)
516 {
517   tree decl = IDENTIFIER_CLASS_VALUE (name);
518   if (decl == NULL_TREE)
519     decl = push_class (make_class (), name);
520   return TREE_TYPE (decl);
521 }
522
523 void
524 set_super_info (int access_flags, tree this_class,
525                 tree super_class, int interfaces_count)
526 {
527   int total_supers = interfaces_count;
528   tree class_decl = TYPE_NAME (this_class);
529   
530   if (super_class)
531     total_supers++;
532
533   if (total_supers)
534     TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
535   TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
536   if (super_class)
537     {
538       tree super_binfo = make_tree_binfo (0);
539       BINFO_TYPE (super_binfo) = super_class;
540       BINFO_OFFSET (super_binfo) = integer_zero_node;
541       BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
542       CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
543     }
544
545   set_class_decl_access_flags (access_flags, class_decl);
546 }
547
548 void
549 set_class_decl_access_flags (int access_flags, tree class_decl)
550 {
551   if (access_flags & ACC_PUBLIC)    CLASS_PUBLIC (class_decl) = 1;
552   if (access_flags & ACC_FINAL)     CLASS_FINAL (class_decl) = 1;
553   if (access_flags & ACC_SUPER)     CLASS_SUPER (class_decl) = 1;
554   if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
555   if (access_flags & ACC_ABSTRACT)  CLASS_ABSTRACT (class_decl) = 1;
556   if (access_flags & ACC_STATIC)    CLASS_STATIC (class_decl) = 1;
557   if (access_flags & ACC_PRIVATE)   CLASS_PRIVATE (class_decl) = 1;
558   if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
559   if (access_flags & ACC_STRICT)    CLASS_STRICTFP (class_decl) = 1;
560   if (access_flags & ACC_ENUM)      CLASS_ENUM (class_decl) = 1;
561   if (access_flags & ACC_SYNTHETIC) CLASS_SYNTHETIC (class_decl) = 1;
562   if (access_flags & ACC_ANNOTATION) CLASS_ANNOTATION (class_decl) = 1;
563 }
564
565 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
566    direct sub-classes of Object are 1, and so on. */
567
568 int
569 class_depth (tree clas)
570 {
571   int depth = 0;
572   if (! CLASS_LOADED_P (clas))
573     load_class (clas, 1);
574   if (TYPE_SIZE (clas) == error_mark_node)
575     return -1;
576   while (clas != object_type_node)
577     {
578       depth++;
579       clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
580     }
581   return depth;
582 }
583
584 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
585
586 int
587 interface_of_p (tree type1, tree type2)
588 {
589   int i;
590   tree binfo, base_binfo;
591
592   if (! TYPE_BINFO (type2))
593     return 0;
594
595   for (binfo = TYPE_BINFO (type2), i = 0;
596        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
597     if (BINFO_TYPE (base_binfo) == type1)
598       return 1;
599   
600   for (binfo = TYPE_BINFO (type2), i = 0;
601        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /*  */
602     if (BINFO_TYPE (base_binfo)
603         && interface_of_p (type1, BINFO_TYPE (base_binfo)))
604       return 1;
605   
606   return 0;
607 }
608
609 /* Return true iff TYPE1 inherits from TYPE2. */
610
611 int
612 inherits_from_p (tree type1, tree type2)
613 {
614   while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
615     {
616       if (type1 == type2)
617         return 1;
618
619       if (! CLASS_LOADED_P (type1))
620         load_class (type1, 1);
621
622       type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
623     }
624   return 0;
625 }
626
627 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
628
629 int
630 enclosing_context_p (tree type1, tree type2)
631 {
632   if (!INNER_CLASS_TYPE_P (type2))
633     return 0;
634
635   for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
636        type2; 
637        type2 = (INNER_CLASS_TYPE_P (type2) ?
638                 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
639     {
640       if (type2 == type1)
641         return 1;
642     }
643
644   return 0;
645 }
646
647
648 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
649    nesting level.  */
650
651 int
652 common_enclosing_context_p (tree type1, tree type2)
653 {
654   while (type1)
655     {
656       tree current;
657       for (current = type2; current;
658            current = (INNER_CLASS_TYPE_P (current) ?
659                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) : 
660                       NULL_TREE))
661         if (type1 == current)
662           return 1;
663
664       if (INNER_CLASS_TYPE_P (type1))
665         type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
666       else
667         break;
668     }
669   return 0;
670 }
671
672 /* Return 1 iff there exists a common enclosing "this" between TYPE1
673    and TYPE2, without crossing any static context.  */
674
675 int
676 common_enclosing_instance_p (tree type1, tree type2)
677 {
678   if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
679     return 0;
680   
681   for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1; 
682        type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
683                 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
684     {
685       tree current;
686       for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
687            current = (PURE_INNER_CLASS_TYPE_P (current) ?
688                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) : 
689                       NULL_TREE))
690         if (type1 == current)
691           return 1;
692     }
693   return 0;
694 }
695
696 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
697    found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
698    if attempt is made to add it twice. */
699
700 tree
701 maybe_add_interface (tree this_class, tree interface_class)
702 {
703   tree binfo, base_binfo;
704   int i;
705
706   for (binfo = TYPE_BINFO (this_class), i = 0;
707        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
708     if (BINFO_TYPE (base_binfo) == interface_class)
709       return interface_class;
710   add_interface (this_class, interface_class);
711   return NULL_TREE;
712 }
713
714 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
715
716 void
717 add_interface (tree this_class, tree interface_class)
718 {
719   tree interface_binfo = make_tree_binfo (0);
720   
721   BINFO_TYPE (interface_binfo) = interface_class;
722   BINFO_OFFSET (interface_binfo) = integer_zero_node;
723   BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
724   BINFO_VIRTUAL_P (interface_binfo) = 1;
725   
726   BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
727 }
728
729 static tree
730 build_java_method_type (tree fntype, tree this_class, int access_flags)
731 {
732   if (access_flags & ACC_STATIC)
733     return fntype;
734   fntype = build_method_type (this_class, fntype);
735
736   /* We know that arg 1 of every nonstatic method is non-null; tell
737      the back-end so.  */
738   TYPE_ATTRIBUTES (fntype) = (tree_cons 
739                               (get_identifier ("nonnull"),
740                                tree_cons (NULL_TREE, 
741                                           build_int_cst (NULL_TREE, 1),
742                                           NULL_TREE),
743                                TYPE_ATTRIBUTES (fntype)));
744   return fntype;
745 }
746
747 void
748 java_hide_decl (tree decl ATTRIBUTE_UNUSED)
749 {
750 #ifdef HAVE_GAS_HIDDEN
751   DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
752   DECL_VISIBILITY_SPECIFIED (decl) = 1;
753 #endif
754 }
755
756 tree
757 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
758 {
759   tree method_type, fndecl;
760
761   method_type = build_java_method_type (function_type,
762                                         this_class, access_flags);
763
764   fndecl = build_decl (input_location, FUNCTION_DECL, name, method_type);
765   DECL_CONTEXT (fndecl) = this_class;
766
767   DECL_LANG_SPECIFIC (fndecl)
768     = ggc_alloc_cleared_lang_decl(sizeof (struct lang_decl));
769   DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
770
771   /* Initialize the static initializer test table.  */
772
773   DECL_FUNCTION_INIT_TEST_TABLE (fndecl) = java_treetreehash_create (10);
774
775   /* Initialize the initialized (static) class table. */
776   if (access_flags & ACC_STATIC)
777     DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
778       htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
779
780   DECL_CHAIN (fndecl) = TYPE_METHODS (this_class);
781   TYPE_METHODS (this_class) = fndecl;
782
783   /* If pointers to member functions use the least significant bit to
784      indicate whether a function is virtual, ensure a pointer
785      to this function will have that bit clear.  */
786   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
787       && !(access_flags & ACC_STATIC)
788       && DECL_ALIGN (fndecl) < 2 * BITS_PER_UNIT)
789     DECL_ALIGN (fndecl) = 2 * BITS_PER_UNIT;
790
791   /* Notice that this is a finalizer and update the class type
792      accordingly. This is used to optimize instance allocation. */
793   if (name == finalize_identifier_node
794       && TREE_TYPE (function_type) == void_type_node
795       && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
796     HAS_FINALIZER_P (this_class) = 1;
797
798   if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
799   if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
800   if (access_flags & ACC_PRIVATE)
801     METHOD_PRIVATE (fndecl) = 1;
802   if (access_flags & ACC_NATIVE)
803     {
804       METHOD_NATIVE (fndecl) = 1;
805       DECL_EXTERNAL (fndecl) = 1;
806     }
807   else
808     /* FNDECL is external unless we are compiling it into this object
809        file.  */
810     DECL_EXTERNAL (fndecl) = CLASS_FROM_CURRENTLY_COMPILED_P (this_class) == 0;
811   if (access_flags & ACC_STATIC) 
812     METHOD_STATIC (fndecl) = 1;
813   if (access_flags & ACC_FINAL) 
814     METHOD_FINAL (fndecl) = 1;
815   if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
816   if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
817   if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
818   if (access_flags & ACC_SYNTHETIC) DECL_ARTIFICIAL (fndecl) = 1;
819   if (access_flags & ACC_BRIDGE) METHOD_BRIDGE (fndecl) = 1;
820   if (access_flags & ACC_VARARGS) METHOD_VARARGS (fndecl) = 1;
821   return fndecl;
822 }
823
824 /* Add a method to THIS_CLASS.
825    The method's name is NAME.
826    Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
827
828 tree
829 add_method (tree this_class, int access_flags, tree name, tree method_sig)
830 {
831   tree function_type, fndecl;
832   const unsigned char *sig
833     = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
834
835   if (sig[0] != '(')
836     fatal_error ("bad method signature");
837
838   function_type = get_type_from_signature (method_sig);
839   fndecl = add_method_1 (this_class, access_flags, name, function_type);
840   set_java_signature (TREE_TYPE (fndecl), method_sig);
841   return fndecl;
842 }
843
844 tree
845 add_field (tree klass, tree name, tree field_type, int flags)
846 {
847   int is_static = (flags & ACC_STATIC) != 0;
848   tree field;
849   field = build_decl (input_location,
850                       is_static ? VAR_DECL : FIELD_DECL, name, field_type);
851   DECL_CHAIN (field) = TYPE_FIELDS (klass);
852   TYPE_FIELDS (klass) = field;
853   DECL_CONTEXT (field) = klass;
854   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
855
856   if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
857   if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
858   if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
859   if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
860   if (flags & ACC_VOLATILE) 
861     {
862       FIELD_VOLATILE (field) = 1;
863       TREE_THIS_VOLATILE (field) = 1;
864     }
865   if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
866   if (flags & ACC_ENUM) FIELD_ENUM (field) = 1;
867   if (flags & ACC_SYNTHETIC) FIELD_SYNTHETIC (field) = 1;
868   if (is_static)
869     {
870       FIELD_STATIC (field) = 1;
871       /* Always make field externally visible.  This is required so
872          that native methods can always access the field.  */
873       TREE_PUBLIC (field) = 1;
874       /* Hide everything that shouldn't be visible outside a DSO.  */
875       if (flag_indirect_classes
876           || (FIELD_PRIVATE (field)))
877         java_hide_decl (field);
878       /* Considered external unless we are compiling it into this
879          object file.  */
880       DECL_EXTERNAL (field) = (is_compiled_class (klass) != 2);
881       if (!DECL_EXTERNAL (field))
882         VEC_safe_push (tree, gc, pending_static_fields, field);
883     }
884
885   return field;
886 }
887
888 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
889
890 void
891 set_constant_value (tree field, tree constant)
892 {
893   if (field == NULL_TREE)
894     warning (OPT_Wattributes,
895              "misplaced ConstantValue attribute (not in any field)");
896   else if (DECL_INITIAL (field) != NULL_TREE)
897     warning (OPT_Wattributes,
898              "duplicate ConstantValue attribute for field '%s'",
899              IDENTIFIER_POINTER (DECL_NAME (field)));
900   else
901     {
902       DECL_INITIAL (field) = constant;
903       if (TREE_TYPE (constant) != TREE_TYPE (field)
904           && ! (TREE_TYPE (constant) == int_type_node
905                 && INTEGRAL_TYPE_P (TREE_TYPE (field))
906                 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
907           && ! (TREE_TYPE (constant) == utf8const_ptr_type
908                 && TREE_TYPE (field) == string_ptr_type_node))
909         error ("ConstantValue attribute of field '%s' has wrong type",
910                IDENTIFIER_POINTER (DECL_NAME (field)));
911     }
912 }
913
914 /* Calculate a hash value for a string encoded in Utf8 format.
915  * This returns the same hash value as specified for java.lang.String.hashCode.
916  */
917
918 static int32
919 hashUtf8String (const char *str, int len)
920 {
921   const unsigned char* ptr = (const unsigned char*) str;
922   const unsigned char *limit = ptr + len;
923   int32 hash = 0;
924   for (; ptr < limit;)
925     {
926       int ch = UTF8_GET (ptr, limit);
927       /* Updated specification from
928          http://www.javasoft.com/docs/books/jls/clarify.html. */
929       hash = (31 * hash) + ch;
930     }
931   return hash;
932 }
933
934 tree
935 build_utf8_ref (tree name)
936 {
937   const char * name_ptr = IDENTIFIER_POINTER (name);
938   int name_len = IDENTIFIER_LENGTH (name), name_pad;
939   char buf[60];
940   tree ctype, field = NULL_TREE, str_type, cinit, string;
941   static int utf8_count = 0;
942   int name_hash;
943   tree ref = IDENTIFIER_UTF8_REF (name);
944   tree decl;
945   VEC(constructor_elt,gc) *v = NULL;
946   if (ref != NULL_TREE)
947     return ref;
948
949   ctype = make_node (RECORD_TYPE);
950   /* '\0' byte plus padding to utf8const_type's alignment.  */
951   name_pad = TYPE_ALIGN_UNIT (utf8const_type)
952              - (name_len & (TYPE_ALIGN_UNIT (utf8const_type) - 1));
953   str_type = build_prim_array_type (unsigned_byte_type_node,
954                                     name_len + name_pad);
955   PUSH_FIELD (input_location, ctype, field, "hash", unsigned_short_type_node);
956   PUSH_FIELD (input_location,
957               ctype, field, "length", unsigned_short_type_node);
958   PUSH_FIELD (input_location, ctype, field, "data", str_type);
959   FINISH_RECORD (ctype);
960   START_RECORD_CONSTRUCTOR (v, ctype);
961   name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
962   PUSH_FIELD_VALUE (v, "hash", build_int_cst (NULL_TREE, name_hash));
963   PUSH_FIELD_VALUE (v, "length", build_int_cst (NULL_TREE, name_len));
964   string = build_string (name_len, name_ptr);
965   TREE_TYPE (string) = str_type;
966   PUSH_FIELD_VALUE (v, "data", string);
967   FINISH_RECORD_CONSTRUCTOR (cinit, v, ctype);
968   TREE_CONSTANT (cinit) = 1;
969
970   /* Generate a unique-enough identifier.  */
971   sprintf(buf, "_Utf%d", ++utf8_count);
972
973   decl = build_decl (input_location,
974                      VAR_DECL, get_identifier (buf), utf8const_type);
975   TREE_STATIC (decl) = 1;
976   DECL_ARTIFICIAL (decl) = 1;
977   DECL_IGNORED_P (decl) = 1;
978   TREE_READONLY (decl) = 1;
979   TREE_THIS_VOLATILE (decl) = 0;
980   DECL_INITIAL (decl) = cinit;
981   DECL_USER_ALIGN (decl) = 1;
982
983   if (HAVE_GAS_SHF_MERGE)
984     {
985       int decl_size;
986       /* Ensure decl_size is a multiple of utf8const_type's alignment. */
987       decl_size = name_len + 4 + name_pad;
988       if (flag_merge_constants && decl_size < 256)
989         {
990           char buf[32];
991           int flags = (SECTION_OVERRIDE
992                        | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
993           sprintf (buf, ".rodata.jutf8.%d", decl_size);
994           switch_to_section (get_section (buf, flags, NULL));
995           DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
996         }
997     }
998
999   layout_decl (decl, 0);
1000   DECL_SIZE (decl) = TYPE_SIZE (ctype);
1001   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (ctype);
1002   pushdecl (decl);
1003   rest_of_decl_compilation (decl, global_bindings_p (), 0);
1004   varpool_mark_needed_node (varpool_node (decl));
1005   ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
1006   IDENTIFIER_UTF8_REF (name) = ref;
1007   return ref;
1008 }
1009
1010 /* Like build_class_ref, but instead of a direct reference generate a
1011    pointer into the constant pool.  */
1012
1013 static tree
1014 build_indirect_class_ref (tree type)
1015 {
1016   int index;
1017   tree cl;
1018   index = alloc_class_constant (type);
1019   cl = build_ref_from_constant_pool (index); 
1020   return convert (promote_type (class_ptr_type), cl);
1021 }
1022
1023 static tree
1024 build_static_class_ref (tree type)
1025 {
1026   tree decl_name, decl, ref;
1027
1028   if (TYPE_SIZE (type) == error_mark_node)
1029     return null_pointer_node;
1030   decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1031                                 "", '/', '/', ".class$$");
1032   decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1033   if (decl == NULL_TREE)
1034     {
1035       decl = build_decl (input_location, VAR_DECL, decl_name, class_type_node);
1036       TREE_STATIC (decl) = 1;
1037       if (! flag_indirect_classes)
1038         {
1039           TREE_PUBLIC (decl) = 1;
1040           if (CLASS_PRIVATE (TYPE_NAME (type)))
1041             java_hide_decl (decl);
1042         }
1043       DECL_IGNORED_P (decl) = 1;
1044       DECL_ARTIFICIAL (decl) = 1;
1045       if (is_compiled_class (type) == 1)
1046         DECL_EXTERNAL (decl) = 1;
1047       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1048       DECL_CLASS_FIELD_P (decl) = 1;
1049       DECL_CONTEXT (decl) = type;
1050
1051       /* ??? We want to preserve the DECL_CONTEXT we set just above,
1052          that that means not calling pushdecl_top_level.  */
1053       IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
1054     }
1055
1056   ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1057   return ref;
1058 }
1059
1060 static tree
1061 build_classdollar_field (tree type)
1062 {
1063   tree decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1064                                      "", '/', '/', ".class$");
1065   tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1066
1067   if (decl == NULL_TREE)
1068     {
1069       decl 
1070         = build_decl (input_location,
1071                       VAR_DECL, decl_name, 
1072                       (build_type_variant 
1073                        (build_pointer_type 
1074                         (build_type_variant (class_type_node, 
1075                                              /* const */ 1, 0)),
1076                         /* const */ 1, 0)));
1077       TREE_STATIC (decl) = 1;
1078       TREE_CONSTANT (decl) = 1;
1079       TREE_READONLY (decl) = 1;
1080       TREE_PUBLIC (decl) = 1;
1081       java_hide_decl (decl);
1082       DECL_IGNORED_P (decl) = 1;
1083       DECL_ARTIFICIAL (decl) = 1;
1084       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1085       IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
1086       DECL_CLASS_FIELD_P (decl) = 1;
1087       DECL_CONTEXT (decl) = type;
1088     }
1089
1090   return decl;
1091 }
1092
1093 /* Create a local variable that holds the current class$.  */
1094
1095 void
1096 cache_this_class_ref (tree fndecl)
1097 {
1098   if (optimize)
1099     {
1100       tree classdollar_field;
1101       if (flag_indirect_classes)
1102         classdollar_field = build_classdollar_field (output_class);
1103       else
1104         classdollar_field = build_static_class_ref (output_class);
1105
1106       this_classdollar = build_decl (input_location,
1107                                      VAR_DECL, NULL_TREE, 
1108                                      TREE_TYPE (classdollar_field));
1109       
1110       java_add_local_var (this_classdollar);
1111       java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (this_classdollar), 
1112                              this_classdollar, classdollar_field));
1113     }
1114   else
1115     this_classdollar = build_classdollar_field (output_class);
1116
1117   /* Prepend class initialization for static methods reachable from
1118      other classes.  */
1119   if (METHOD_STATIC (fndecl)
1120       && (! METHOD_PRIVATE (fndecl)
1121           || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
1122       && ! DECL_CLINIT_P (fndecl)
1123       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
1124     {
1125       tree init = build_call_expr (soft_initclass_node, 1,
1126                                    this_classdollar);
1127       java_add_stmt (init);
1128     }
1129 }
1130
1131 /* Remove the reference to the local variable that holds the current
1132    class$.  */
1133
1134 void
1135 uncache_this_class_ref (tree fndecl ATTRIBUTE_UNUSED)
1136 {
1137   this_classdollar = build_classdollar_field (output_class);
1138 }
1139
1140 /* Build a reference to the class TYPE.
1141    Also handles primitive types and array types. */
1142
1143 tree
1144 build_class_ref (tree type)
1145 {
1146   int is_compiled = is_compiled_class (type);
1147   if (is_compiled)
1148     {
1149       tree ref, decl;
1150       if (TREE_CODE (type) == POINTER_TYPE)
1151         type = TREE_TYPE (type);
1152
1153       if (flag_indirect_dispatch
1154           && type != output_class
1155           && TREE_CODE (type) == RECORD_TYPE)
1156         return build_indirect_class_ref (type);
1157
1158       if (type == output_class && flag_indirect_classes)
1159         {
1160           /* This can be NULL if we see a JNI stub before we see any
1161              other method.  */
1162           if (! this_classdollar)
1163             this_classdollar = build_classdollar_field (output_class);
1164           return this_classdollar;
1165         }
1166       
1167       if (TREE_CODE (type) == RECORD_TYPE)
1168         return build_static_class_ref (type);
1169       else
1170         {
1171           const char *name;
1172           tree decl_name;
1173           char buffer[25];
1174           decl_name = TYPE_NAME (type);
1175           if (TREE_CODE (decl_name) == TYPE_DECL)
1176             decl_name = DECL_NAME (decl_name);
1177           name = IDENTIFIER_POINTER (decl_name);
1178           if (strncmp (name, "promoted_", 9) == 0)
1179             name += 9;
1180           sprintf (buffer, "_Jv_%sClass", name);
1181           decl_name = get_identifier (buffer);
1182           decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1183           if (decl == NULL_TREE)
1184             {
1185               decl = build_decl (input_location,
1186                                  VAR_DECL, decl_name, class_type_node);
1187               TREE_STATIC (decl) = 1;
1188               TREE_PUBLIC (decl) = 1;
1189               DECL_EXTERNAL (decl) = 1;
1190               DECL_ARTIFICIAL (decl) = 1;
1191               pushdecl_top_level (decl);
1192             }
1193         }
1194
1195       ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1196       return ref;
1197     }
1198   else
1199     return build_indirect_class_ref (type);
1200 }
1201
1202 /* Create a local statically allocated variable that will hold a
1203    pointer to a static field.  */
1204
1205 static tree
1206 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1207 {
1208   tree decl, decl_name;
1209   const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1210   char *buf = (char *) alloca (strlen (name) + 20);
1211   sprintf (buf, "%s_%d_ref", name, index);
1212   decl_name = get_identifier (buf);
1213   decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1214   if (decl == NULL_TREE)
1215     {
1216       decl = build_decl (input_location,
1217                          VAR_DECL, decl_name, ptr_type_node);
1218       TREE_STATIC (decl) = 1;
1219       TREE_PUBLIC (decl) = 0;
1220       DECL_EXTERNAL (decl) = 0;
1221       DECL_ARTIFICIAL (decl) = 1;
1222       DECL_IGNORED_P (decl) = 1;
1223       pushdecl_top_level (decl);
1224     }
1225   return decl;
1226 }
1227
1228 tree
1229 build_static_field_ref (tree fdecl)
1230 {
1231   tree fclass = DECL_CONTEXT (fdecl);
1232   int is_compiled = is_compiled_class (fclass);
1233
1234   /* Allow static final fields to fold to a constant.  When using
1235      -findirect-dispatch, we simply never do this folding if compiling
1236      from .class; in the .class file constants will be referred to via
1237      the constant pool.  */
1238   if (!flag_indirect_dispatch
1239       && (is_compiled
1240           || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1241               && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1242                   || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1243               && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
1244     {
1245       if (is_compiled == 1)
1246         DECL_EXTERNAL (fdecl) = 1;
1247     }
1248   else
1249     {
1250       /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1251          and a class local static variable CACHE_ENTRY, then
1252       
1253       *(fdecl **)((__builtin_expect (cache_entry == null, false)) 
1254                   ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1255                   : cache_entry)
1256
1257       This can mostly be optimized away, so that the usual path is a
1258       load followed by a test and branch.  _Jv_ResolvePoolEntry is
1259       only called once for each constant pool entry.
1260
1261       There is an optimization that we don't do: at the start of a
1262       method, create a local copy of CACHE_ENTRY and use that instead.
1263
1264       */
1265
1266       int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1267       tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1268       tree test
1269         = build_call_expr (built_in_decls[BUILT_IN_EXPECT], 2,
1270                            build2 (EQ_EXPR, boolean_type_node,
1271                                    cache_entry, null_pointer_node),
1272                            boolean_false_node);
1273       tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1274       tree init
1275         = build_call_expr (soft_resolvepoolentry_node, 2,
1276                            build_class_ref (output_class),
1277                            cpool_index_cst);
1278       init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1279       init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1280       init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1281       fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1282     }
1283   return fdecl;
1284 }
1285
1286 int
1287 get_access_flags_from_decl (tree decl)
1288 {
1289   int access_flags = 0;
1290   if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1291     {
1292       if (FIELD_STATIC (decl))
1293         access_flags |= ACC_STATIC;
1294       if (FIELD_PUBLIC (decl))
1295         access_flags |= ACC_PUBLIC;
1296       if (FIELD_PROTECTED (decl))
1297         access_flags |= ACC_PROTECTED;
1298       if (FIELD_PRIVATE (decl))
1299         access_flags |= ACC_PRIVATE;
1300       if (FIELD_FINAL (decl))
1301         access_flags |= ACC_FINAL;
1302       if (FIELD_VOLATILE (decl))
1303         access_flags |= ACC_VOLATILE;
1304       if (FIELD_TRANSIENT (decl))
1305         access_flags |= ACC_TRANSIENT;
1306       if (FIELD_ENUM (decl))
1307         access_flags |= ACC_ENUM;
1308       if (FIELD_SYNTHETIC (decl))
1309         access_flags |= ACC_SYNTHETIC;
1310       return access_flags;
1311     }
1312   if (TREE_CODE (decl) == TYPE_DECL)
1313     {
1314       if (CLASS_PUBLIC (decl))
1315         access_flags |= ACC_PUBLIC;
1316       if (CLASS_FINAL (decl))
1317         access_flags |= ACC_FINAL;
1318       if (CLASS_SUPER (decl))
1319         access_flags |= ACC_SUPER;
1320       if (CLASS_INTERFACE (decl))
1321         access_flags |= ACC_INTERFACE;
1322       if (CLASS_ABSTRACT (decl))
1323         access_flags |= ACC_ABSTRACT;
1324       if (CLASS_STATIC (decl))
1325         access_flags |= ACC_STATIC;
1326       if (CLASS_PRIVATE (decl))
1327         access_flags |= ACC_PRIVATE;
1328       if (CLASS_PROTECTED (decl))
1329         access_flags |= ACC_PROTECTED;
1330       if (CLASS_STRICTFP (decl))
1331         access_flags |= ACC_STRICT;
1332       if (CLASS_ENUM (decl))
1333         access_flags |= ACC_ENUM;
1334       if (CLASS_SYNTHETIC (decl))
1335         access_flags |= ACC_SYNTHETIC;
1336       if (CLASS_ANNOTATION (decl))
1337         access_flags |= ACC_ANNOTATION;
1338       return access_flags;
1339     }
1340   if (TREE_CODE (decl) == FUNCTION_DECL)
1341     {
1342       if (METHOD_PUBLIC (decl))
1343         access_flags |= ACC_PUBLIC;
1344       if (METHOD_PRIVATE (decl))
1345         access_flags |= ACC_PRIVATE;
1346       if (METHOD_PROTECTED (decl))
1347         access_flags |= ACC_PROTECTED;
1348       if (METHOD_STATIC (decl))
1349         access_flags |= ACC_STATIC;
1350       if (METHOD_FINAL (decl))
1351         access_flags |= ACC_FINAL;
1352       if (METHOD_SYNCHRONIZED (decl))
1353         access_flags |= ACC_SYNCHRONIZED;
1354       if (METHOD_NATIVE (decl))
1355         access_flags |= ACC_NATIVE;
1356       if (METHOD_ABSTRACT (decl))
1357         access_flags |= ACC_ABSTRACT;
1358       if (METHOD_STRICTFP (decl))
1359         access_flags |= ACC_STRICT;
1360       if (METHOD_INVISIBLE (decl))
1361         access_flags |= ACC_INVISIBLE;
1362       if (DECL_ARTIFICIAL (decl))
1363         access_flags |= ACC_SYNTHETIC;
1364       if (METHOD_BRIDGE (decl))
1365         access_flags |= ACC_BRIDGE;
1366       if (METHOD_VARARGS (decl))
1367         access_flags |= ACC_VARARGS;
1368       return access_flags;
1369     }
1370   gcc_unreachable ();
1371 }
1372
1373 static GTY (()) int alias_labelno = 0;
1374
1375 /* Create a private alias for METHOD. Using this alias instead of the method
1376    decl ensures that ncode entries in the method table point to the real function 
1377    at runtime, not a PLT entry.  */
1378
1379 static tree
1380 make_local_function_alias (tree method)
1381 {
1382 #ifdef ASM_OUTPUT_DEF
1383   tree alias;
1384   
1385   const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1386   char *name = (char *) alloca (strlen (method_name) + 2);
1387   char *buf = (char *) alloca (strlen (method_name) + 128);
1388
1389   /* Only create aliases for local functions.  */
1390   if (DECL_EXTERNAL (method))
1391     return method;
1392     
1393   /* Prefix method_name with 'L' for the alias label.  */
1394   *name = 'L';
1395   strcpy (name + 1, method_name);
1396
1397   targetm.asm_out.generate_internal_label (buf, name, alias_labelno++);  
1398   alias = build_decl (input_location,
1399                       FUNCTION_DECL, get_identifier (buf),
1400                       TREE_TYPE (method));
1401   DECL_CONTEXT (alias) = NULL;
1402   TREE_READONLY (alias) = TREE_READONLY (method);
1403   TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1404   TREE_PUBLIC (alias) = 0;
1405   DECL_EXTERNAL (alias) = 0;
1406   DECL_ARTIFICIAL (alias) = 1;
1407   DECL_INITIAL (alias) = error_mark_node;
1408   TREE_ADDRESSABLE (alias) = 1;
1409   TREE_USED (alias) = 1;
1410   TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1411   if (!flag_syntax_only)
1412     assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1413   return alias;
1414 #else
1415   return method;
1416 #endif
1417 }
1418
1419 /** Make reflection data (_Jv_Field) for field FDECL. */
1420
1421 static tree
1422 make_field_value (tree fdecl)
1423 {
1424   tree finit;
1425   int flags;
1426   tree type = TREE_TYPE (fdecl);
1427   int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1428   VEC(constructor_elt,gc) *v = NULL;
1429
1430   START_RECORD_CONSTRUCTOR (v, field_type_node);
1431   PUSH_FIELD_VALUE (v, "name", build_utf8_ref (DECL_NAME (fdecl)));
1432   if (resolved)
1433     type = build_class_ref (type);
1434   else
1435     {
1436       tree signature = build_java_signature (type);
1437
1438       type = build_utf8_ref (unmangle_classname 
1439                              (IDENTIFIER_POINTER (signature),
1440                               IDENTIFIER_LENGTH (signature)));
1441     }
1442   PUSH_FIELD_VALUE (v, "type", type);
1443
1444   flags = get_access_flags_from_decl (fdecl);
1445   if (! resolved)
1446     flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1447
1448   PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, flags));
1449   PUSH_FIELD_VALUE (v, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1450
1451   {
1452     tree field_address = integer_zero_node;
1453     tree index, value;
1454     if ((DECL_INITIAL (fdecl) || ! flag_indirect_classes) 
1455         && FIELD_STATIC (fdecl))
1456       field_address = build_address_of (fdecl);
1457
1458     index = (FIELD_STATIC (fdecl)
1459              ? DECL_CHAIN (TYPE_FIELDS (field_info_union_node))
1460              : TYPE_FIELDS (field_info_union_node));
1461     value = (FIELD_STATIC (fdecl)
1462              ? field_address
1463              : byte_position (fdecl));
1464
1465     PUSH_FIELD_VALUE
1466       (v, "info",
1467        build_constructor_single (field_info_union_node, index, value));
1468   }
1469
1470   FINISH_RECORD_CONSTRUCTOR (finit, v, field_type_node);
1471   return finit;
1472 }
1473
1474 /** Make reflection data (_Jv_Method) for method MDECL. */
1475
1476 static tree
1477 make_method_value (tree mdecl)
1478 {
1479   static int method_name_count = 0;
1480   tree minit;
1481   tree index;
1482   tree code;
1483   tree class_decl;
1484 #define ACC_TRANSLATED          0x4000
1485   int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1486   VEC(constructor_elt,gc) *v = NULL;
1487
1488   class_decl = DECL_CONTEXT (mdecl);
1489   /* For interfaces, the index field contains the dispatch index. */
1490   if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1491     index = build_int_cst (NULL_TREE,
1492                            get_interface_method_index (mdecl, class_decl));
1493   else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1494     index = get_method_index (mdecl);
1495   else
1496     index = integer_minus_one_node;
1497
1498   code = null_pointer_node;
1499   if (METHOD_ABSTRACT (mdecl))
1500     code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1501                    soft_abstractmethod_node);
1502   else
1503     code = build1 (ADDR_EXPR, nativecode_ptr_type_node, 
1504                    make_local_function_alias (mdecl));
1505   START_RECORD_CONSTRUCTOR (v, method_type_node);
1506   PUSH_FIELD_VALUE (v, "name",
1507                     build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1508                                     init_identifier_node
1509                                     : DECL_NAME (mdecl)));
1510   {
1511     tree signature = build_java_signature (TREE_TYPE (mdecl));
1512     PUSH_FIELD_VALUE (v, "signature", 
1513                       (build_utf8_ref 
1514                        (unmangle_classname 
1515                         (IDENTIFIER_POINTER(signature),
1516                          IDENTIFIER_LENGTH(signature)))));
1517   }
1518   PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, accflags));
1519   PUSH_FIELD_VALUE (v, "index", index);
1520   PUSH_FIELD_VALUE (v, "ncode", code);
1521
1522   {
1523     /* Compute the `throws' information for the method.  */
1524     tree table = null_pointer_node;
1525
1526     if (!VEC_empty (tree, DECL_FUNCTION_THROWS (mdecl)))
1527       {
1528         int length = 1 + VEC_length (tree, DECL_FUNCTION_THROWS (mdecl));
1529         tree t, type, array;
1530         char buf[60];
1531         VEC(constructor_elt,gc) *v = NULL;
1532         int idx = length - 1;
1533         unsigned ix;
1534         constructor_elt *e;
1535
1536         v = VEC_alloc (constructor_elt, gc, length);
1537         VEC_safe_grow_cleared (constructor_elt, gc, v, length);
1538
1539         e = VEC_index (constructor_elt, v, idx--);
1540         e->value = null_pointer_node;
1541
1542         FOR_EACH_VEC_ELT (tree, DECL_FUNCTION_THROWS (mdecl), ix, t)
1543           {
1544             tree sig = DECL_NAME (TYPE_NAME (t));
1545             tree utf8
1546               = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1547                                                     IDENTIFIER_LENGTH (sig)));
1548             e = VEC_index (constructor_elt, v, idx--);
1549             e->value = utf8;
1550           }
1551         gcc_assert (idx == -1);
1552         type = build_prim_array_type (ptr_type_node, length);
1553         table = build_constructor (type, v);
1554         /* Compute something unique enough.  */
1555         sprintf (buf, "_methods%d", method_name_count++);
1556         array = build_decl (input_location,
1557                             VAR_DECL, get_identifier (buf), type);
1558         DECL_INITIAL (array) = table;
1559         TREE_STATIC (array) = 1;
1560         DECL_ARTIFICIAL (array) = 1;
1561         DECL_IGNORED_P (array) = 1;
1562         rest_of_decl_compilation (array, 1, 0);
1563
1564         table = build1 (ADDR_EXPR, ptr_type_node, array);
1565       }
1566
1567     PUSH_FIELD_VALUE (v, "throws", table);
1568   }
1569
1570   FINISH_RECORD_CONSTRUCTOR (minit, v, method_type_node);
1571   return minit;
1572 }
1573
1574 static tree
1575 get_dispatch_vector (tree type)
1576 {
1577   tree vtable = TYPE_VTABLE (type);
1578
1579   if (vtable == NULL_TREE)
1580     {
1581       HOST_WIDE_INT i;
1582       tree method;
1583       tree super = CLASSTYPE_SUPER (type);
1584       HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1585       vtable = make_tree_vec (nvirtuals);
1586       TYPE_VTABLE (type) = vtable;
1587       if (super != NULL_TREE)
1588         {
1589           tree super_vtable = get_dispatch_vector (super);
1590
1591           for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1592             TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1593         }
1594
1595       for (method = TYPE_METHODS (type);  method != NULL_TREE;
1596            method = DECL_CHAIN (method))
1597         {
1598           tree method_index = get_method_index (method);
1599           if (method_index != NULL_TREE
1600               && host_integerp (method_index, 0))
1601             TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1602         }
1603     }
1604
1605   return vtable;
1606 }
1607
1608 static tree
1609 get_dispatch_table (tree type, tree this_class_addr)
1610 {
1611   int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1612   tree vtable = get_dispatch_vector (type);
1613   int i, j;
1614   int nvirtuals = TREE_VEC_LENGTH (vtable);
1615   int arraysize;
1616   tree gc_descr;
1617   VEC(constructor_elt,gc) *v = NULL;
1618   constructor_elt *e;
1619   tree arraytype;
1620
1621   arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1622   if (TARGET_VTABLE_USES_DESCRIPTORS)
1623     arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1624   arraysize += 2;
1625
1626   VEC_safe_grow_cleared (constructor_elt, gc, v, arraysize);
1627   e = VEC_index (constructor_elt, v, arraysize - 1);
1628
1629 #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
1630   for (i = nvirtuals;  --i >= 0; )
1631     {
1632       tree method = TREE_VEC_ELT (vtable, i);
1633       if (METHOD_ABSTRACT (method))
1634         {
1635           if (! abstract_p)
1636             warning_at (DECL_SOURCE_LOCATION (method), 0,
1637                         "abstract method in non-abstract class");
1638
1639           if (TARGET_VTABLE_USES_DESCRIPTORS)
1640             for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1641               CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1642           else
1643             CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1644         }
1645       else
1646         {
1647           if (TARGET_VTABLE_USES_DESCRIPTORS)
1648             for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1649               {
1650                 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node, 
1651                                      method, build_int_cst (NULL_TREE, j));
1652                 TREE_CONSTANT (fdesc) = 1;
1653                 CONSTRUCTOR_PREPEND_VALUE (e, fdesc);
1654               }
1655           else
1656             CONSTRUCTOR_PREPEND_VALUE (e,
1657                                        build1 (ADDR_EXPR,
1658                                                nativecode_ptr_type_node,
1659                                                method));
1660         }
1661     }
1662
1663   /* Dummy entry for compatibility with G++ -fvtable-thunks.  When
1664      using the Boehm GC we sometimes stash a GC type descriptor
1665      there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1666      the emitted byte count during the output to the assembly file. */
1667   /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1668      fake "function descriptor".  It's first word is the is the class
1669      pointer, and subsequent words (usually one) contain the GC descriptor.
1670      In all other cases, we reserve two extra vtable slots. */
1671   gc_descr =  get_boehm_type_descriptor (type);
1672   CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
1673   for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1674     CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
1675   CONSTRUCTOR_PREPEND_VALUE (e, this_class_addr);
1676
1677   /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1678   CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1679   /** Offset to start of whole object.  Always (ptrdiff_t)0 for Java. */
1680   gcc_assert (e == VEC_address (constructor_elt, v));
1681   e->index = integer_zero_node;
1682   e->value = null_pointer_node;
1683 #undef CONSTRUCTOR_PREPEND_VALUE
1684
1685   arraytype = build_prim_array_type (nativecode_ptr_type_node, arraysize);
1686   return build_constructor (arraytype, v);
1687 }
1688
1689
1690 /* Set the method_index for a method decl.  */
1691 void
1692 set_method_index (tree decl, tree method_index)
1693 {
1694   if (method_index != NULL_TREE)
1695     {
1696       /* method_index is null if we're using indirect dispatch.  */
1697       method_index = fold (convert (sizetype, method_index));
1698
1699       if (TARGET_VTABLE_USES_DESCRIPTORS)
1700         /* Add one to skip bogus descriptor for class and GC descriptor. */
1701         method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1702       else
1703         /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1704            descriptor.  */
1705         method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1706     }
1707
1708   DECL_VINDEX (decl) = method_index;
1709 }
1710
1711 /* Get the method_index for a method decl.  */
1712 tree
1713 get_method_index (tree decl)
1714 {
1715   tree method_index = DECL_VINDEX (decl);
1716
1717   if (! method_index)
1718     return NULL;
1719
1720   if (TARGET_VTABLE_USES_DESCRIPTORS)
1721     /* Sub one to skip bogus descriptor for class and GC descriptor. */
1722     method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1723   else
1724     /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor.  */
1725     method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1726
1727   return method_index;
1728 }
1729
1730 static int
1731 supers_all_compiled (tree type)
1732 {
1733   while (type != NULL_TREE)
1734     {
1735       if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1736         return 0;
1737       type = CLASSTYPE_SUPER (type);
1738     }
1739   return 1;
1740 }
1741
1742 static void
1743 add_table_and_syms (VEC(constructor_elt,gc) **v,
1744                     VEC(method_entry,gc) *methods,
1745                     const char *table_name, tree table_slot, tree table_type,
1746                     const char *syms_name, tree syms_slot)
1747 {
1748   if (methods == NULL)
1749     {
1750       PUSH_FIELD_VALUE (*v, table_name, null_pointer_node);
1751       PUSH_FIELD_VALUE (*v, syms_name, null_pointer_node);
1752     }
1753   else
1754     {
1755       pushdecl_top_level (syms_slot);
1756       PUSH_FIELD_VALUE (*v, table_name,
1757                         build1 (ADDR_EXPR, table_type, table_slot));
1758       PUSH_FIELD_VALUE (*v, syms_name,
1759                         build1 (ADDR_EXPR, symbols_array_ptr_type,
1760                                 syms_slot));
1761       TREE_CONSTANT (table_slot) = 1;
1762     }
1763 }
1764                     
1765 void
1766 make_class_data (tree type)
1767 {
1768   tree decl, cons, temp;
1769   tree field, fields_decl;
1770   HOST_WIDE_INT static_field_count = 0;
1771   HOST_WIDE_INT instance_field_count = 0;
1772   HOST_WIDE_INT field_count;
1773   tree field_array_type;
1774   tree method;
1775   tree dtable_decl = NULL_TREE;
1776   HOST_WIDE_INT method_count = 0;
1777   tree method_array_type;
1778   tree methods_decl;
1779   tree super;
1780   tree this_class_addr;
1781   tree constant_pool_constructor;
1782   tree interfaces = null_pointer_node;
1783   int interface_len = 0;
1784   int uses_jv_markobj = 0;
1785   tree type_decl = TYPE_NAME (type);
1786   tree id_main = get_identifier("main");
1787   tree id_class = get_identifier("java.lang.Class");
1788   /** Offset from start of virtual function table declaration
1789       to where objects actually point at, following new g++ ABI. */
1790   tree dtable_start_offset = size_int (2 * POINTER_SIZE / BITS_PER_UNIT);
1791   VEC(int, heap) *field_indexes;
1792   tree first_real_field;
1793   VEC(constructor_elt,gc) *v1 = NULL, *v2 = NULL;
1794   tree reflection_data;
1795   VEC(constructor_elt,gc) *static_fields = NULL;
1796   VEC(constructor_elt,gc) *instance_fields = NULL;
1797   VEC(constructor_elt,gc) *methods = NULL;
1798
1799   this_class_addr = build_static_class_ref (type);
1800   decl = TREE_OPERAND (this_class_addr, 0);
1801
1802   if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1803       && !flag_indirect_dispatch)
1804     {
1805       tree dtable = get_dispatch_table (type, this_class_addr);
1806       uses_jv_markobj = uses_jv_markobj_p (dtable);
1807       if (type == class_type_node && class_dtable_decl != NULL_TREE)
1808         {
1809           /* We've already created some other class, and consequently
1810              we made class_dtable_decl.  Now we just want to fill it
1811              in.  */
1812           dtable_decl = class_dtable_decl;
1813         }
1814       else
1815         {
1816           dtable_decl = build_dtable_decl (type);
1817           TREE_STATIC (dtable_decl) = 1;
1818           DECL_ARTIFICIAL (dtable_decl) = 1;
1819           DECL_IGNORED_P (dtable_decl) = 1;
1820         }
1821
1822       TREE_PUBLIC (dtable_decl) = 1;
1823       DECL_INITIAL (dtable_decl) = dtable;
1824       /* The only dispatch table exported from a DSO is the dispatch
1825          table for java.lang.Class.  */
1826       if (DECL_NAME (type_decl) != id_class)
1827         java_hide_decl (dtable_decl);
1828       if (! flag_indirect_classes)
1829         rest_of_decl_compilation (dtable_decl, 1, 0);
1830       /* Maybe we're compiling Class as the first class.  If so, set
1831          class_dtable_decl to the decl we just made.  */
1832       if (type == class_type_node && class_dtable_decl == NULL_TREE)
1833         class_dtable_decl = dtable_decl;
1834     }
1835
1836   /* Build Field array. */
1837   field = TYPE_FIELDS (type);
1838   while (field && DECL_ARTIFICIAL (field))
1839     field = DECL_CHAIN (field);  /* Skip dummy fields.  */
1840   if (field && DECL_NAME (field) == NULL_TREE)
1841     field = DECL_CHAIN (field);  /* Skip dummy field for inherited data. */
1842   first_real_field = field;
1843
1844   /* First count static and instance fields.  */
1845   for ( ; field != NULL_TREE; field = DECL_CHAIN (field))
1846     {
1847       if (! DECL_ARTIFICIAL (field))
1848         {
1849           if (FIELD_STATIC (field))
1850             static_field_count++;
1851           else if (uses_jv_markobj || !flag_reduced_reflection)
1852             instance_field_count++;
1853         }
1854     }
1855   field_count = static_field_count + instance_field_count;
1856   field_indexes = VEC_alloc (int, heap, field_count);
1857   
1858   /* gcj sorts fields so that static fields come first, followed by
1859      instance fields.  Unfortunately, by the time this takes place we
1860      have already generated the reflection_data for this class, and
1861      that data contains indexes into the fields.  So, we generate a
1862      permutation that maps each original field index to its final
1863      position.  Then we pass this permutation to
1864      rewrite_reflection_indexes(), which fixes up the reflection
1865      data.  */
1866   {
1867     int i;
1868     int static_count = 0;
1869     int instance_count = static_field_count;
1870     int field_index;
1871
1872     for (i = 0, field = first_real_field; 
1873          field != NULL_TREE; 
1874          field = DECL_CHAIN (field), i++)
1875     {
1876       if (! DECL_ARTIFICIAL (field))
1877         {
1878           field_index = 0;
1879           if (FIELD_STATIC (field))
1880             field_index = static_count++;
1881           else if (uses_jv_markobj || !flag_reduced_reflection)
1882             field_index = instance_count++;
1883           else
1884             continue;
1885           VEC_quick_push (int, field_indexes, field_index);
1886         }
1887     }
1888   }
1889
1890   for (field = first_real_field; field != NULL_TREE; 
1891        field = DECL_CHAIN (field))
1892     {
1893       if (! DECL_ARTIFICIAL (field))
1894         {
1895           if (FIELD_STATIC (field))
1896             {
1897               /* We must always create reflection data for static fields
1898                  as it is used in the creation of the field itself. */
1899               tree init = make_field_value (field);
1900               tree initial = DECL_INITIAL (field);
1901               CONSTRUCTOR_APPEND_ELT (static_fields, NULL_TREE, init);
1902               /* If the initial value is a string constant,
1903                  prevent output_constant from trying to assemble the value. */
1904               if (initial != NULL_TREE
1905                   && TREE_TYPE (initial) == string_ptr_type_node)
1906                 DECL_INITIAL (field) = NULL_TREE;
1907               rest_of_decl_compilation (field, 1, 1);
1908               DECL_INITIAL (field) = initial;
1909             }
1910           else if (uses_jv_markobj || !flag_reduced_reflection)
1911             {
1912               tree init = make_field_value (field);
1913               CONSTRUCTOR_APPEND_ELT (instance_fields, NULL_TREE, init);
1914             }
1915         }
1916     }
1917
1918   gcc_assert (static_field_count
1919               == (int) VEC_length (constructor_elt, static_fields));
1920   gcc_assert (instance_field_count
1921               == (int) VEC_length (constructor_elt, instance_fields));
1922
1923   if (field_count > 0)
1924     {
1925       VEC_safe_splice (constructor_elt, gc, static_fields, instance_fields);
1926       field_array_type = build_prim_array_type (field_type_node, field_count);
1927       fields_decl = build_decl (input_location,
1928                                 VAR_DECL, mangled_classname ("_FL_", type),
1929                                 field_array_type);
1930       DECL_INITIAL (fields_decl)
1931         = build_constructor (field_array_type, static_fields);
1932       TREE_STATIC (fields_decl) = 1;
1933       DECL_ARTIFICIAL (fields_decl) = 1;
1934       DECL_IGNORED_P (fields_decl) = 1;
1935       rest_of_decl_compilation (fields_decl, 1, 0);
1936     }
1937   else
1938     fields_decl = NULL_TREE;
1939
1940   /* Build Method array. */
1941   for (method = TYPE_METHODS (type);
1942        method != NULL_TREE; method = DECL_CHAIN (method))
1943     {
1944       tree init;
1945       if (METHOD_PRIVATE (method)
1946           && ! flag_keep_inline_functions
1947           && optimize)
1948         continue;
1949       /* Even if we have a decl, we don't necessarily have the code.
1950          This can happen if we inherit a method from a superclass for
1951          which we don't have a .class file.  */
1952       if (METHOD_DUMMY (method))
1953         continue;
1954
1955       /* Generate method reflection data if:
1956
1957           - !flag_reduced_reflection.
1958
1959           - <clinit> -- The runtime uses reflection to initialize the
1960             class.
1961
1962           - Any method in class java.lang.Class -- Class.forName() and
1963             perhaps other things require it.
1964
1965           - class$ -- It does not work if reflection data missing.
1966
1967           - main -- Reflection is used to find main(String[]) methods.
1968
1969           - public not static -- It is potentially part of an
1970             interface.  The runtime uses reflection data to build
1971             interface dispatch tables.  */
1972       if (!flag_reduced_reflection
1973           || DECL_CLINIT_P (method)
1974           || DECL_NAME (type_decl) == id_class
1975           || DECL_NAME (method) == id_main
1976           || (METHOD_PUBLIC (method) && !METHOD_STATIC (method)))
1977         {
1978           init = make_method_value (method);
1979           method_count++;
1980           CONSTRUCTOR_APPEND_ELT (methods, NULL_TREE, init);
1981         }
1982     }
1983   method_array_type = build_prim_array_type (method_type_node, method_count);
1984   methods_decl = build_decl (input_location,
1985                              VAR_DECL, mangled_classname ("_MT_", type),
1986                              method_array_type);
1987   DECL_INITIAL (methods_decl) = build_constructor (method_array_type, methods);
1988   TREE_STATIC (methods_decl) = 1;
1989   DECL_ARTIFICIAL (methods_decl) = 1;
1990   DECL_IGNORED_P (methods_decl) = 1;
1991   rest_of_decl_compilation (methods_decl, 1, 0);
1992
1993   if (class_dtable_decl == NULL_TREE)
1994     {
1995       class_dtable_decl = build_dtable_decl (class_type_node);
1996       TREE_STATIC (class_dtable_decl) = 1;
1997       DECL_ARTIFICIAL (class_dtable_decl) = 1;
1998       DECL_IGNORED_P (class_dtable_decl) = 1;
1999       if (is_compiled_class (class_type_node) != 2)
2000         {
2001           DECL_EXTERNAL (class_dtable_decl) = 1;
2002           rest_of_decl_compilation (class_dtable_decl, 1, 0);
2003         }
2004     }
2005
2006   super = CLASSTYPE_SUPER (type);
2007   if (super == NULL_TREE)
2008     super = null_pointer_node;
2009   else if (! flag_indirect_dispatch
2010            && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
2011            && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
2012     super = build_class_ref (super);
2013   else
2014     {
2015       int super_index = alloc_class_constant (super);
2016       super = build_int_cst (ptr_type_node, super_index);
2017     }
2018
2019   /* Build and emit the array of implemented interfaces. */
2020   if (type != object_type_node)
2021     interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
2022   
2023   if (interface_len > 0)
2024     {
2025       int i;
2026       tree interface_array_type, idecl;
2027       VEC(constructor_elt,gc) *init = VEC_alloc (constructor_elt, gc,
2028                                                  interface_len);
2029       interface_array_type
2030         = build_prim_array_type (class_ptr_type, interface_len);
2031       idecl = build_decl (input_location,
2032                           VAR_DECL, mangled_classname ("_IF_", type),
2033                           interface_array_type);
2034       
2035       for (i = 1; i <= interface_len; i++)
2036         {
2037           tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
2038           tree iclass = BINFO_TYPE (child);
2039           tree index;
2040           if (! flag_indirect_dispatch
2041               && (assume_compiled 
2042                   (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
2043             index = build_class_ref (iclass);
2044           else
2045             {
2046               int int_index = alloc_class_constant (iclass);
2047               index = build_int_cst (ptr_type_node, int_index);
2048             }
2049           CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, index);
2050         }
2051       DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
2052       TREE_STATIC (idecl) = 1;
2053       DECL_ARTIFICIAL (idecl) = 1;
2054       DECL_IGNORED_P (idecl) = 1;
2055       interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
2056       rest_of_decl_compilation (idecl, 1, 0);
2057     }
2058
2059   constant_pool_constructor = build_constants_constructor ();
2060
2061   if (flag_indirect_dispatch)
2062     {
2063       TYPE_OTABLE_DECL (type) 
2064         = emit_symbol_table 
2065         (DECL_NAME (TYPE_OTABLE_DECL (type)), 
2066          TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type), 
2067          TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
2068        
2069       TYPE_ATABLE_DECL (type) 
2070         = emit_symbol_table 
2071         (DECL_NAME (TYPE_ATABLE_DECL (type)), 
2072          TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type), 
2073          TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
2074        
2075       TYPE_ITABLE_DECL (type) 
2076         = emit_symbol_table 
2077         (DECL_NAME (TYPE_ITABLE_DECL (type)), 
2078          TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type), 
2079          TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
2080     }
2081   
2082   TYPE_CTABLE_DECL (type) = emit_catch_table (type);
2083
2084   START_RECORD_CONSTRUCTOR (v1, object_type_node);
2085   PUSH_FIELD_VALUE (v1, "vtable",
2086                     (flag_indirect_classes 
2087                      ? null_pointer_node
2088                      : build2 (POINTER_PLUS_EXPR, dtable_ptr_type,
2089                                build1 (ADDR_EXPR, dtable_ptr_type,
2090                                        class_dtable_decl),
2091                                dtable_start_offset)));
2092   if (! flag_hash_synchronization)
2093     PUSH_FIELD_VALUE (v1, "sync_info", null_pointer_node);
2094   FINISH_RECORD_CONSTRUCTOR (temp, v1, object_type_node);
2095   START_RECORD_CONSTRUCTOR (v2, class_type_node);
2096   PUSH_SUPER_VALUE (v2, temp);
2097   PUSH_FIELD_VALUE (v2, "next_or_version", gcj_abi_version);
2098   PUSH_FIELD_VALUE (v2, "name", build_utf8_ref (DECL_NAME (type_decl)));
2099   PUSH_FIELD_VALUE (v2, "accflags",
2100                     build_int_cst (NULL_TREE,
2101                                    get_access_flags_from_decl (type_decl)));
2102
2103   PUSH_FIELD_VALUE (v2, "superclass", 
2104                     CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
2105   PUSH_FIELD_VALUE (v2, "constants", constant_pool_constructor);
2106   PUSH_FIELD_VALUE (v2, "methods",
2107                     methods_decl == NULL_TREE ? null_pointer_node
2108                     : build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
2109   PUSH_FIELD_VALUE (v2, "method_count",
2110                     build_int_cst (NULL_TREE, method_count));
2111
2112   PUSH_FIELD_VALUE (v2, "vtable_method_count",
2113                     (flag_indirect_dispatch
2114                      ? integer_minus_one_node
2115                      : TYPE_NVIRTUALS (type)));
2116     
2117   PUSH_FIELD_VALUE (v2, "fields",
2118                     fields_decl == NULL_TREE ? null_pointer_node
2119                     : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
2120   /* If we're using the binary compatibility ABI we don't know the
2121      size until load time.  */
2122   PUSH_FIELD_VALUE (v2, "size_in_bytes", 
2123                     (flag_indirect_dispatch 
2124                      ? integer_minus_one_node 
2125                      : size_in_bytes (type)));
2126   PUSH_FIELD_VALUE (v2, "field_count", 
2127                     build_int_cst (NULL_TREE, field_count));
2128   PUSH_FIELD_VALUE (v2, "static_field_count",
2129                     build_int_cst (NULL_TREE, static_field_count));
2130
2131   PUSH_FIELD_VALUE (v2, "vtable",
2132                     (flag_indirect_dispatch || dtable_decl == NULL_TREE
2133                      ? null_pointer_node
2134                      : build2 (POINTER_PLUS_EXPR, dtable_ptr_type,
2135                                 build1 (ADDR_EXPR, dtable_ptr_type,
2136                                         dtable_decl),
2137                                dtable_start_offset)));
2138   add_table_and_syms (&v2, TYPE_OTABLE_METHODS (type),
2139                       "otable", TYPE_OTABLE_DECL (type), otable_ptr_type,
2140                       "otable_syms", TYPE_OTABLE_SYMS_DECL (type));
2141   add_table_and_syms (&v2, TYPE_ATABLE_METHODS (type),
2142                       "atable", TYPE_ATABLE_DECL (type), atable_ptr_type,
2143                       "atable_syms", TYPE_ATABLE_SYMS_DECL (type));
2144   add_table_and_syms (&v2, TYPE_ITABLE_METHODS (type),
2145                       "itable", TYPE_ITABLE_DECL (type), itable_ptr_type,
2146                       "itable_syms", TYPE_ITABLE_SYMS_DECL (type));
2147  
2148   PUSH_FIELD_VALUE (v2, "catch_classes",
2149                     build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type))); 
2150   PUSH_FIELD_VALUE (v2, "interfaces", interfaces);
2151   PUSH_FIELD_VALUE (v2, "loader", null_pointer_node);
2152   PUSH_FIELD_VALUE (v2, "interface_count",
2153                     build_int_cst (NULL_TREE, interface_len));
2154   PUSH_FIELD_VALUE (v2, "state",
2155                     convert (byte_type_node,
2156                              build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
2157
2158   PUSH_FIELD_VALUE (v2, "thread", null_pointer_node);
2159   PUSH_FIELD_VALUE (v2, "depth", integer_zero_node);
2160   PUSH_FIELD_VALUE (v2, "ancestors", null_pointer_node);
2161   PUSH_FIELD_VALUE (v2, "idt", null_pointer_node);
2162   PUSH_FIELD_VALUE (v2, "arrayclass", null_pointer_node);
2163   PUSH_FIELD_VALUE (v2, "protectionDomain", null_pointer_node);
2164
2165   {
2166     tree assertion_table_ref;
2167     if (TYPE_ASSERTIONS (type) == NULL)
2168       assertion_table_ref = null_pointer_node;
2169     else
2170       assertion_table_ref = build1 (ADDR_EXPR, 
2171                                     build_pointer_type (assertion_table_type),
2172                                     emit_assertion_table (type));
2173     
2174     PUSH_FIELD_VALUE (v2, "assertion_table", assertion_table_ref);
2175   }
2176
2177   PUSH_FIELD_VALUE (v2, "hack_signers", null_pointer_node);
2178   PUSH_FIELD_VALUE (v2, "chain", null_pointer_node);
2179   PUSH_FIELD_VALUE (v2, "aux_info", null_pointer_node);
2180   PUSH_FIELD_VALUE (v2, "engine", null_pointer_node);
2181
2182   if (TYPE_REFLECTION_DATA (current_class))
2183     {
2184       int i;
2185       int count = TYPE_REFLECTION_DATASIZE (current_class);
2186       VEC (constructor_elt, gc) *v
2187         = VEC_alloc (constructor_elt, gc, count);
2188       unsigned char *data = TYPE_REFLECTION_DATA (current_class);
2189       tree max_index = build_int_cst (sizetype, count);
2190       tree index = build_index_type (max_index);
2191       tree type = build_array_type (unsigned_byte_type_node, index);
2192       char buf[64];
2193       tree array;
2194       static int reflection_data_count;
2195
2196       sprintf (buf, "_reflection_data_%d", reflection_data_count++);
2197       array = build_decl (input_location,
2198                           VAR_DECL, get_identifier (buf), type);
2199
2200       rewrite_reflection_indexes (field_indexes);
2201
2202       for (i = 0; i < count; i++)
2203         {
2204           constructor_elt *elt = VEC_quick_push (constructor_elt, v, NULL);
2205           elt->index = build_int_cst (sizetype, i);
2206           elt->value = build_int_cstu (byte_type_node, data[i]);
2207         }
2208
2209       DECL_INITIAL (array) = build_constructor (type, v);
2210       TREE_STATIC (array) = 1;
2211       DECL_ARTIFICIAL (array) = 1;
2212       DECL_IGNORED_P (array) = 1;
2213       TREE_READONLY (array) = 1;
2214       TREE_CONSTANT (DECL_INITIAL (array)) = 1;
2215       rest_of_decl_compilation (array, 1, 0);
2216
2217       reflection_data = build_address_of (array);
2218
2219       free (data);
2220       TYPE_REFLECTION_DATA (current_class) = NULL;
2221     }
2222   else
2223     reflection_data = null_pointer_node;
2224
2225   PUSH_FIELD_VALUE (v2, "reflection_data", reflection_data);
2226   FINISH_RECORD_CONSTRUCTOR (cons, v2, class_type_node);
2227
2228   DECL_INITIAL (decl) = cons;
2229   
2230   /* Hash synchronization requires at least 64-bit alignment. */
2231   if (flag_hash_synchronization && POINTER_SIZE < 64)
2232     DECL_ALIGN (decl) = 64; 
2233   
2234   if (flag_indirect_classes)
2235     {
2236       TREE_READONLY (decl) = 1;
2237       TREE_CONSTANT (DECL_INITIAL (decl)) = 1;
2238     }
2239
2240   rest_of_decl_compilation (decl, 1, 0);
2241   
2242   {
2243     tree classdollar_field = build_classdollar_field (type);
2244     if (!flag_indirect_classes)
2245       DECL_INITIAL (classdollar_field) = build_static_class_ref (type);
2246     rest_of_decl_compilation (classdollar_field, 1, 0);
2247   }
2248
2249   TYPE_OTABLE_DECL (type) = NULL_TREE;
2250   TYPE_ATABLE_DECL (type) = NULL_TREE;
2251   TYPE_CTABLE_DECL (type) = NULL_TREE;
2252 }
2253
2254 void
2255 finish_class (void)
2256 {
2257   java_expand_catch_classes (current_class);
2258
2259   current_function_decl = NULL_TREE;
2260   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
2261   make_class_data (current_class);
2262   register_class ();
2263   rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
2264 }
2265
2266 /* Return 2 if KLASS is compiled by this compilation job;
2267    return 1 if KLASS can otherwise be assumed to be compiled;
2268    return 0 if we cannot assume that KLASS is compiled.
2269    Returns 1 for primitive and 0 for array types.  */
2270 int
2271 is_compiled_class (tree klass)
2272 {
2273   int seen_in_zip;
2274   if (TREE_CODE (klass) == POINTER_TYPE)
2275     klass = TREE_TYPE (klass);
2276   if (TREE_CODE (klass) != RECORD_TYPE)  /* Primitive types are static. */
2277     return 1;
2278   if (TYPE_ARRAY_P (klass))
2279     return 0;
2280
2281   seen_in_zip = (TYPE_JCF (klass) && JCF_SEEN_IN_ZIP (TYPE_JCF (klass)));
2282   if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2283     {
2284       /* The class was seen in the current ZIP file and will be
2285          available as a compiled class in the future but may not have
2286          been loaded already. Load it if necessary. This prevent
2287          build_class_ref () from crashing. */
2288
2289       if (seen_in_zip && !CLASS_LOADED_P (klass) && (klass != current_class))
2290         load_class (klass, 1);
2291
2292       /* We return 2 for class seen in ZIP and class from files
2293          belonging to the same compilation unit */
2294       return 2;
2295     }
2296
2297   if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (klass)))))
2298     {
2299       if (!CLASS_LOADED_P (klass))
2300         {
2301           if (klass != current_class)
2302             load_class (klass, 1);
2303         }
2304       return 1;
2305     }
2306
2307   return 0;
2308 }
2309
2310 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
2311
2312 tree
2313 build_dtable_decl (tree type)
2314 {
2315   tree dtype, decl;
2316
2317   /* We need to build a new dtable type so that its size is uniquely
2318      computed when we're dealing with the class for real and not just
2319      faking it (like java.lang.Class during the initialization of the
2320      compiler.) We know we're not faking a class when CURRENT_CLASS is
2321      TYPE. */
2322   if (current_class == type)
2323     {
2324       tree dummy = NULL_TREE;
2325       int n;
2326
2327       dtype = make_node (RECORD_TYPE);
2328
2329       PUSH_FIELD (input_location, dtype, dummy, "top_offset", ptr_type_node);
2330       PUSH_FIELD (input_location, dtype, dummy, "type_info", ptr_type_node);
2331
2332       PUSH_FIELD (input_location, dtype, dummy, "class", class_ptr_type);
2333       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2334         {
2335           tree tmp_field = build_decl (input_location,
2336                                        FIELD_DECL, NULL_TREE, ptr_type_node);
2337           TREE_CHAIN (dummy) = tmp_field;
2338           DECL_CONTEXT (tmp_field) = dtype;
2339           DECL_ARTIFICIAL (tmp_field) = 1;
2340           dummy = tmp_field;
2341         }
2342
2343       PUSH_FIELD (input_location, dtype, dummy, "gc_descr", ptr_type_node);
2344       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2345         {
2346           tree tmp_field = build_decl (input_location,
2347                                        FIELD_DECL, NULL_TREE, ptr_type_node);
2348           TREE_CHAIN (dummy) = tmp_field;
2349           DECL_CONTEXT (tmp_field) = dtype;
2350           DECL_ARTIFICIAL (tmp_field) = 1;
2351           dummy = tmp_field;
2352         }
2353
2354       n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2355       if (TARGET_VTABLE_USES_DESCRIPTORS)
2356         n *= TARGET_VTABLE_USES_DESCRIPTORS;
2357
2358       PUSH_FIELD (input_location, dtype, dummy, "methods",
2359                   build_prim_array_type (nativecode_ptr_type_node, n));
2360       layout_type (dtype);
2361     }
2362   else
2363     dtype = dtable_type;
2364
2365   decl = build_decl (input_location,
2366                      VAR_DECL, get_identifier ("vt$"), dtype);
2367   DECL_CONTEXT (decl) = type;
2368   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2369   DECL_VTABLE_P (decl) = 1;
2370
2371   return decl;
2372 }
2373
2374 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2375    fields inherited from SUPER_CLASS. */
2376
2377 void
2378 push_super_field (tree this_class, tree super_class)
2379 {
2380   tree base_decl;
2381   /* Don't insert the field if we're just re-laying the class out. */ 
2382   if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2383     return;
2384   base_decl = build_decl (input_location,
2385                           FIELD_DECL, NULL_TREE, super_class);
2386   DECL_IGNORED_P (base_decl) = 1;
2387   DECL_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2388   TYPE_FIELDS (this_class) = base_decl;
2389   DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2390   DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2391 }
2392
2393 /* Handle the different manners we may have to lay out a super class.  */
2394
2395 static tree
2396 maybe_layout_super_class (tree super_class, tree this_class ATTRIBUTE_UNUSED)
2397 {
2398   if (!super_class)
2399     return NULL_TREE;
2400   else if (TREE_CODE (super_class) == RECORD_TYPE)
2401     {
2402       if (!CLASS_LOADED_P (super_class))
2403         load_class (super_class, 1);
2404     }
2405   /* We might have to layout the class before its dependency on
2406      the super class gets resolved by java_complete_class  */
2407   else if (TREE_CODE (super_class) == POINTER_TYPE)
2408     {
2409       if (TREE_TYPE (super_class) != NULL_TREE)
2410         super_class = TREE_TYPE (super_class);
2411       else
2412         gcc_unreachable ();
2413     }
2414   if (!TYPE_SIZE (super_class))
2415     safe_layout_class (super_class);
2416
2417   return super_class;
2418 }
2419
2420 /* safe_layout_class just makes sure that we can load a class without
2421    disrupting the current_class, input_file, input_line, etc, information
2422    about the class processed currently.  */
2423
2424 void
2425 safe_layout_class (tree klass)
2426 {
2427   tree save_current_class = current_class;
2428   location_t save_location = input_location;
2429
2430   layout_class (klass);
2431
2432   current_class = save_current_class;
2433   input_location = save_location;
2434 }
2435
2436 void
2437 layout_class (tree this_class)
2438 {
2439   int i;
2440   tree super_class = CLASSTYPE_SUPER (this_class);
2441
2442   class_list = tree_cons (this_class, NULL_TREE, class_list);
2443   if (CLASS_BEING_LAIDOUT (this_class))
2444     {
2445       char buffer [1024];
2446       char *report;
2447       tree current;
2448
2449       sprintf (buffer, " with '%s'",
2450                IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2451       obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2452
2453       for (current = TREE_CHAIN (class_list); current; 
2454            current = TREE_CHAIN (current))
2455         {
2456           tree decl = TYPE_NAME (TREE_PURPOSE (current));
2457           sprintf (buffer, "\n  which inherits from '%s' (%s:%d)",
2458                    IDENTIFIER_POINTER (DECL_NAME (decl)),
2459                    DECL_SOURCE_FILE (decl),
2460                    DECL_SOURCE_LINE (decl));
2461           obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2462         }
2463       obstack_1grow (&temporary_obstack, '\0');
2464       report = XOBFINISH (&temporary_obstack, char *);
2465       cyclic_inheritance_report = ggc_strdup (report);
2466       obstack_free (&temporary_obstack, report);
2467       TYPE_SIZE (this_class) = error_mark_node;
2468       return;
2469     }
2470   CLASS_BEING_LAIDOUT (this_class) = 1;
2471
2472   if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2473     {
2474       tree maybe_super_class 
2475         = maybe_layout_super_class (super_class, this_class);
2476       if (maybe_super_class == NULL
2477           || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2478         {
2479           TYPE_SIZE (this_class) = error_mark_node;
2480           CLASS_BEING_LAIDOUT (this_class) = 0;
2481           class_list = TREE_CHAIN (class_list);
2482           return;
2483         }
2484       if (TYPE_SIZE (this_class) == NULL_TREE)
2485         push_super_field (this_class, maybe_super_class);
2486     }
2487
2488   layout_type (this_class);
2489
2490   /* Also recursively load/layout any superinterfaces.  */
2491   if (TYPE_BINFO (this_class))
2492     {
2493       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2494         {
2495           tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2496           tree super_interface = BINFO_TYPE (binfo);
2497           tree maybe_super_interface 
2498             = maybe_layout_super_class (super_interface, NULL_TREE);
2499           if (maybe_super_interface == NULL
2500               || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2501             {
2502               TYPE_SIZE (this_class) = error_mark_node;
2503               CLASS_BEING_LAIDOUT (this_class) = 0;
2504               class_list = TREE_CHAIN (class_list);
2505               return;
2506             }
2507         }
2508     }
2509
2510   /* Convert the size back to an SI integer value.  */
2511   TYPE_SIZE_UNIT (this_class) =
2512     fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2513
2514   CLASS_BEING_LAIDOUT (this_class) = 0;
2515   class_list = TREE_CHAIN (class_list);
2516 }
2517
2518 static void
2519 add_miranda_methods (tree base_class, tree search_class)
2520 {
2521   int i;
2522   tree binfo, base_binfo;
2523
2524   if (!CLASS_PARSED_P (search_class))
2525     load_class (search_class, 1);
2526   
2527   for (binfo = TYPE_BINFO (search_class), i = 1;
2528        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2529     {
2530       tree method_decl;
2531       tree elt = BINFO_TYPE (base_binfo);
2532
2533       /* FIXME: This is totally bogus.  We should not be handling
2534          Miranda methods at all if we're using the BC ABI.  */
2535       if (TYPE_DUMMY (elt))
2536         continue;
2537
2538       /* Ensure that interface methods are seen in declared order.  */
2539       if (!CLASS_LOADED_P (elt))
2540         load_class (elt, 1);
2541       layout_class_methods (elt);
2542
2543       /* All base classes will have been laid out at this point, so the order 
2544          will be correct.  This code must match similar layout code in the 
2545          runtime.  */
2546       for (method_decl = TYPE_METHODS (elt);
2547            method_decl; method_decl = DECL_CHAIN (method_decl))
2548         {
2549           tree sig, override;
2550
2551           /* An interface can have <clinit>.  */
2552           if (ID_CLINIT_P (DECL_NAME (method_decl)))
2553             continue;
2554
2555           sig = build_java_argument_signature (TREE_TYPE (method_decl));
2556           override = lookup_argument_method (base_class,
2557                                              DECL_NAME (method_decl), sig);
2558           if (override == NULL_TREE)
2559             {
2560               /* Found a Miranda method.  Add it.  */
2561               tree new_method;
2562               sig = build_java_signature (TREE_TYPE (method_decl));
2563               new_method
2564                 = add_method (base_class,
2565                               get_access_flags_from_decl (method_decl),
2566                               DECL_NAME (method_decl), sig);
2567               METHOD_INVISIBLE (new_method) = 1;
2568             }
2569         }
2570
2571       /* Try superinterfaces.  */
2572       add_miranda_methods (base_class, elt);
2573     }
2574 }
2575
2576 void
2577 layout_class_methods (tree this_class)
2578 {
2579   tree method_decl, dtable_count;
2580   tree super_class, type_name;
2581
2582   if (TYPE_NVIRTUALS (this_class))
2583     return;
2584   
2585   super_class = CLASSTYPE_SUPER (this_class);
2586
2587   if (super_class)
2588     {
2589       super_class = maybe_layout_super_class (super_class, this_class);
2590       if (!TYPE_NVIRTUALS (super_class))
2591         layout_class_methods (super_class);
2592       dtable_count = TYPE_NVIRTUALS (super_class);
2593     }
2594   else
2595     dtable_count = integer_zero_node;
2596
2597   type_name = TYPE_NAME (this_class);
2598   if (!flag_indirect_dispatch
2599       && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2600     {
2601       /* An abstract class can have methods which are declared only in
2602          an implemented interface.  These are called "Miranda
2603          methods".  We make a dummy method entry for such methods
2604          here.  */
2605       add_miranda_methods (this_class, this_class);
2606     }
2607
2608   TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2609
2610   for (method_decl = TYPE_METHODS (this_class);
2611        method_decl; method_decl = DECL_CHAIN (method_decl))
2612     dtable_count = layout_class_method (this_class, super_class,
2613                                         method_decl, dtable_count);
2614
2615   TYPE_NVIRTUALS (this_class) = dtable_count;
2616 }
2617
2618 /* Return the index of METHOD in INTERFACE.  This index begins at 1
2619    and is used as an argument for _Jv_LookupInterfaceMethodIdx(). */
2620 int
2621 get_interface_method_index (tree method, tree interface)
2622 {
2623   tree meth;
2624   int i = 1;
2625
2626   for (meth = TYPE_METHODS (interface); ; meth = DECL_CHAIN (meth))
2627     {
2628       if (meth == method)
2629         return i;
2630       /* We don't want to put <clinit> into the interface table.  */
2631       if (! ID_CLINIT_P (DECL_NAME (meth)))
2632         ++i;
2633       gcc_assert (meth != NULL_TREE);
2634     }
2635 }
2636
2637 /* Lay METHOD_DECL out, returning a possibly new value of
2638    DTABLE_COUNT. Also mangle the method's name. */
2639
2640 tree
2641 layout_class_method (tree this_class, tree super_class,
2642                      tree method_decl, tree dtable_count)
2643 {
2644   tree method_name = DECL_NAME (method_decl);
2645
2646   TREE_PUBLIC (method_decl) = 1;
2647
2648   if (flag_indirect_classes
2649       || (METHOD_PRIVATE (method_decl) && METHOD_STATIC (method_decl)
2650           && ! METHOD_NATIVE (method_decl)
2651           && ! special_method_p (method_decl)))
2652     java_hide_decl (method_decl);
2653
2654   /* Considered external unless it is being compiled into this object
2655      file, or it was already flagged as external.  */
2656   if (!DECL_EXTERNAL (method_decl))
2657     DECL_EXTERNAL (method_decl) = ((is_compiled_class (this_class) != 2)
2658                                    || METHOD_NATIVE (method_decl));
2659
2660   if (ID_INIT_P (method_name))
2661     {
2662       const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2663       const char *ptr;
2664       for (ptr = p; *ptr; )
2665         {
2666           if (*ptr++ == '.')
2667             p = ptr;
2668         }
2669       DECL_CONSTRUCTOR_P (method_decl) = 1;
2670       build_java_signature (TREE_TYPE (method_decl));
2671     }
2672   else if (! METHOD_STATIC (method_decl))
2673     {
2674       tree method_sig =
2675         build_java_signature (TREE_TYPE (method_decl));
2676       bool method_override = false;
2677       tree super_method = lookup_java_method (super_class, method_name,
2678                                                   method_sig);
2679       if (super_method != NULL_TREE
2680           && ! METHOD_DUMMY (super_method))
2681         {
2682           method_override = true;
2683           if (! METHOD_PUBLIC (super_method) && 
2684               ! METHOD_PROTECTED (super_method))
2685             {
2686               /* Don't override private method, or default-access method in 
2687                  another package.  */
2688               if (METHOD_PRIVATE (super_method) ||
2689                   ! in_same_package (TYPE_NAME (this_class), 
2690                                      TYPE_NAME (super_class)))
2691                 method_override = false;
2692            }
2693         }
2694       if (method_override)
2695         {
2696           tree method_index = get_method_index (super_method);
2697           set_method_index (method_decl, method_index);
2698           if (method_index == NULL_TREE 
2699               && ! flag_indirect_dispatch
2700               && ! DECL_ARTIFICIAL (super_method))
2701             error ("non-static method %q+D overrides static method",
2702                    method_decl);
2703         }
2704       else if (this_class == object_type_node
2705                && (METHOD_FINAL (method_decl)
2706                    || METHOD_PRIVATE (method_decl)))
2707         {
2708           /* We don't generate vtable entries for final Object
2709              methods.  This is simply to save space, since every
2710              object would otherwise have to define them.  */
2711         }
2712       else if (! METHOD_PRIVATE (method_decl)
2713                && dtable_count)
2714         {
2715           /* We generate vtable entries for final methods because they
2716              may one day be changed to non-final.  */
2717           set_method_index (method_decl, dtable_count);
2718           dtable_count = fold_build2 (PLUS_EXPR, integer_type_node,
2719                                       dtable_count, integer_one_node);
2720         }
2721     }
2722
2723   return dtable_count;
2724 }
2725
2726 static void
2727 register_class (void)
2728 {
2729   tree node;
2730
2731   if (!registered_class)
2732     registered_class = VEC_alloc (tree, gc, 8);
2733
2734   if (flag_indirect_classes)
2735     node = current_class;
2736   else
2737     node = TREE_OPERAND (build_class_ref (current_class), 0);
2738   VEC_safe_push (tree, gc, registered_class, node);
2739 }
2740
2741 /* Emit a function that calls _Jv_RegisterNewClasses with a list of
2742    all the classes we have emitted.  */
2743
2744 static void
2745 emit_indirect_register_classes (tree *list_p)
2746 {
2747   tree klass, t, register_class_fn;
2748   int i;
2749
2750   int size = VEC_length (tree, registered_class) * 2 + 1;
2751   VEC(constructor_elt,gc) *init = VEC_alloc (constructor_elt, gc, size);
2752   tree class_array_type
2753     = build_prim_array_type (ptr_type_node, size);
2754   tree cdecl = build_decl (input_location,
2755                            VAR_DECL, get_identifier ("_Jv_CLS"),
2756                            class_array_type);
2757   tree reg_class_list;
2758   FOR_EACH_VEC_ELT (tree, registered_class, i, klass)
2759     {
2760       t = fold_convert (ptr_type_node, build_static_class_ref (klass));
2761       CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
2762       t = fold_convert (ptr_type_node,
2763                         build_address_of (build_classdollar_field (klass)));
2764       CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
2765     }
2766   CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, integer_zero_node);
2767   DECL_INITIAL (cdecl) = build_constructor (class_array_type, init);
2768   TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
2769   TREE_STATIC (cdecl) = 1;
2770   DECL_ARTIFICIAL (cdecl) = 1;
2771   DECL_IGNORED_P (cdecl) = 1;
2772   TREE_READONLY (cdecl) = 1;
2773   TREE_CONSTANT (cdecl) = 1;
2774   rest_of_decl_compilation (cdecl, 1, 0);
2775   reg_class_list = fold_convert (ptr_type_node, build_address_of (cdecl));
2776
2777   t = build_function_type_list (void_type_node, 
2778                                 build_pointer_type (ptr_type_node), NULL);
2779   t = build_decl (input_location,
2780                   FUNCTION_DECL, 
2781                   get_identifier ("_Jv_RegisterNewClasses"), t);
2782   TREE_PUBLIC (t) = 1;
2783   DECL_EXTERNAL (t) = 1;
2784   register_class_fn = t;
2785   t = build_call_expr (register_class_fn, 1, reg_class_list);
2786   append_to_statement_list (t, list_p);
2787 }
2788
2789
2790 /* Emit something to register classes at start-up time.
2791
2792    The preferred mechanism is through the .jcr section, which contain
2793    a list of pointers to classes which get registered during constructor
2794    invocation time.
2795
2796    The fallback mechanism is to add statements to *LIST_P to call
2797    _Jv_RegisterClass for each class in this file.  These statements will
2798    be added to a static constructor function for this translation unit.  */
2799
2800 void
2801 emit_register_classes (tree *list_p)
2802 {
2803   if (registered_class == NULL)
2804     return;
2805
2806   if (flag_indirect_classes)
2807     {
2808       emit_indirect_register_classes (list_p);
2809       return;
2810     }
2811
2812   /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2813      TARGET_ASM_NAMED_SECTION, else 0.  Some targets meet those conditions
2814      but lack suitable crtbegin/end objects or linker support.  These
2815      targets can override the default in tm.h to use the fallback mechanism.  */
2816   if (TARGET_USE_JCR_SECTION)
2817     {
2818       tree klass, t;
2819       int i;
2820
2821 #ifdef JCR_SECTION_NAME
2822       switch_to_section (get_section (JCR_SECTION_NAME, SECTION_WRITE, NULL));
2823 #else
2824       /* A target has defined TARGET_USE_JCR_SECTION,
2825          but doesn't have a JCR_SECTION_NAME.  */
2826       gcc_unreachable ();
2827 #endif
2828       assemble_align (POINTER_SIZE);
2829
2830       FOR_EACH_VEC_ELT (tree, registered_class, i, klass)
2831         {
2832           t = build_fold_addr_expr (klass);
2833           output_constant (t, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE);
2834         }
2835     }
2836   else
2837     {
2838       tree klass, t, register_class_fn;
2839       int i;
2840
2841       t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2842       t = build_decl (input_location,
2843                       FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2844       TREE_PUBLIC (t) = 1;
2845       DECL_EXTERNAL (t) = 1;
2846       register_class_fn = t;
2847
2848       FOR_EACH_VEC_ELT (tree, registered_class, i, klass)
2849         {
2850           t = build_fold_addr_expr (klass);
2851           t = build_call_expr (register_class_fn, 1, t);
2852           append_to_statement_list (t, list_p);
2853         }
2854     }
2855 }
2856
2857 /* Build a constructor for an entry in the symbol table.  */
2858
2859 static tree
2860 build_symbol_table_entry (tree clname, tree name, tree signature)
2861 {
2862   tree symbol;
2863   VEC(constructor_elt,gc) *v = NULL;
2864
2865   START_RECORD_CONSTRUCTOR (v, symbol_type);
2866   PUSH_FIELD_VALUE (v, "clname", clname);
2867   PUSH_FIELD_VALUE (v, "name", name);
2868   PUSH_FIELD_VALUE (v, "signature", signature);
2869   FINISH_RECORD_CONSTRUCTOR (symbol, v, symbol_type);
2870   TREE_CONSTANT (symbol) = 1;
2871
2872   return symbol;
2873 }
2874
2875 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2876
2877 static tree
2878 build_symbol_entry (tree decl, tree special)
2879 {
2880   tree clname, name, signature;
2881   clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2882   /* ???  Constructors are given the name foo.foo all the way through
2883      the compiler, but in the method table they're all renamed
2884      foo.<init>.  So, we have to do the same here unless we want an
2885      unresolved reference at runtime.  */
2886   name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL 
2887                           && DECL_CONSTRUCTOR_P (decl))
2888                          ? init_identifier_node
2889                          : DECL_NAME (decl));
2890   signature = build_java_signature (TREE_TYPE (decl));
2891   signature = build_utf8_ref (unmangle_classname 
2892                               (IDENTIFIER_POINTER (signature),
2893                                IDENTIFIER_LENGTH (signature)));
2894   /* SPECIAL is either NULL_TREE or integer_one_node.  We emit
2895      signature addr+1 if SPECIAL, and this indicates to the runtime
2896      system that this is a "special" symbol, i.e. one that should
2897      bypass access controls.  */
2898   if (special != NULL_TREE)
2899     signature = build2 (POINTER_PLUS_EXPR, TREE_TYPE (signature), signature,
2900                         fold_convert (sizetype, special));
2901
2902   return build_symbol_table_entry (clname, name, signature);
2903
2904
2905 /* Emit a symbol table: used by -findirect-dispatch.  */
2906
2907 tree
2908 emit_symbol_table (tree name, tree the_table,
2909                    VEC(method_entry,gc) *decl_table,
2910                    tree the_syms_decl, tree the_array_element_type,
2911                    int element_size)
2912 {
2913   tree table, null_symbol, table_size, the_array_type;
2914   unsigned index;
2915   method_entry *e;
2916   VEC(constructor_elt,gc) *v = NULL;
2917   
2918   /* Only emit a table if this translation unit actually made any
2919      references via it. */
2920   if (decl_table == NULL)
2921     return the_table;
2922
2923   /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2924   FOR_EACH_VEC_ELT (method_entry, decl_table, index, e)
2925     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2926                             build_symbol_entry (e->method, e->special));
2927
2928   /* Terminate the list with a "null" entry. */
2929   null_symbol = build_symbol_table_entry (null_pointer_node,
2930                                           null_pointer_node,
2931                                           null_pointer_node);
2932   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_symbol);
2933
2934   table = build_constructor (symbols_array_type, v);
2935
2936   /* Make it the initial value for otable_syms and emit the decl. */
2937   DECL_INITIAL (the_syms_decl) = table;
2938   DECL_ARTIFICIAL (the_syms_decl) = 1;
2939   DECL_IGNORED_P (the_syms_decl) = 1;
2940   rest_of_decl_compilation (the_syms_decl, 1, 0);
2941   
2942   /* Now that its size is known, redefine the table as an
2943      uninitialized static array of INDEX + 1 elements. The extra entry
2944      is used by the runtime to track whether the table has been
2945      initialized. */
2946   table_size 
2947     = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2948   the_array_type = build_array_type (the_array_element_type, table_size);
2949   the_table = build_decl (input_location,
2950                           VAR_DECL, name, the_array_type);
2951   TREE_STATIC (the_table) = 1;
2952   TREE_READONLY (the_table) = 1;  
2953   rest_of_decl_compilation (the_table, 1, 0);
2954
2955   return the_table;
2956 }
2957
2958 /* Make an entry for the catch_classes list.  */
2959 tree
2960 make_catch_class_record (tree catch_class, tree classname)
2961 {
2962   tree entry;
2963   tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2964   VEC(constructor_elt,gc) *v = NULL;
2965   START_RECORD_CONSTRUCTOR (v, type);
2966   PUSH_FIELD_VALUE (v, "address", catch_class);
2967   PUSH_FIELD_VALUE (v, "classname", classname);
2968   FINISH_RECORD_CONSTRUCTOR (entry, v, type);
2969   return entry;
2970 }
2971
2972
2973 /* Generate the list of Throwable classes that are caught by exception
2974    handlers in this class.  */
2975 tree 
2976 emit_catch_table (tree this_class)
2977 {
2978   tree table, table_size, array_type;
2979   int n_catch_classes;
2980   constructor_elt *e;
2981   /* Fill in the dummy entry that make_class created.  */
2982   e = VEC_index (constructor_elt, TYPE_CATCH_CLASSES (this_class), 0);
2983   e->value = make_catch_class_record (null_pointer_node, null_pointer_node);
2984   CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (this_class), NULL_TREE,
2985                           make_catch_class_record (null_pointer_node,
2986                                                    null_pointer_node));
2987   n_catch_classes = VEC_length (constructor_elt,
2988                                 TYPE_CATCH_CLASSES (this_class));
2989   table_size = build_index_type (build_int_cst (NULL_TREE, n_catch_classes));
2990   array_type 
2991     = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2992                         table_size);
2993   table = 
2994     build_decl (input_location,
2995                 VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2996   DECL_INITIAL (table) = 
2997     build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2998   TREE_STATIC (table) = 1;
2999   TREE_READONLY (table) = 1;  
3000   DECL_IGNORED_P (table) = 1;
3001   rest_of_decl_compilation (table, 1, 0);
3002   return table;
3003 }
3004
3005 /* Given a type, return the signature used by
3006    _Jv_FindClassFromSignature() in libgcj.  This isn't exactly the
3007    same as build_java_signature() because we want the canonical array
3008    type.  */
3009
3010 static tree
3011 build_signature_for_libgcj (tree type)
3012 {
3013   tree sig, ref;
3014
3015   sig = build_java_signature (type);
3016   ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
3017                                             IDENTIFIER_LENGTH (sig)));
3018   return ref;
3019 }
3020
3021 /* Build an entry in the type assertion table.  */
3022
3023 static tree
3024 build_assertion_table_entry (tree code, tree op1, tree op2)
3025 {
3026   VEC(constructor_elt,gc) *v = NULL;
3027   tree entry;
3028
3029   START_RECORD_CONSTRUCTOR (v, assertion_entry_type);
3030   PUSH_FIELD_VALUE (v, "assertion_code", code);
3031   PUSH_FIELD_VALUE (v, "op1", op1);
3032   PUSH_FIELD_VALUE (v, "op2", op2);
3033   FINISH_RECORD_CONSTRUCTOR (entry, v, assertion_entry_type);
3034
3035   return entry;
3036 }
3037
3038 /* Add an entry to the type assertion table. Callback used during hashtable
3039    traversal.  */
3040
3041 static int
3042 add_assertion_table_entry (void **htab_entry, void *ptr)
3043 {
3044   tree entry;
3045   tree code_val, op1_utf8, op2_utf8;
3046   VEC(constructor_elt,gc) **v = (VEC(constructor_elt,gc) **) ptr;
3047   type_assertion *as = (type_assertion *) *htab_entry;
3048
3049   code_val = build_int_cst (NULL_TREE, as->assertion_code);
3050
3051   if (as->op1 == NULL_TREE)
3052     op1_utf8 = null_pointer_node;
3053   else
3054     op1_utf8 = build_signature_for_libgcj (as->op1);
3055
3056   if (as->op2 == NULL_TREE)
3057     op2_utf8 = null_pointer_node;
3058   else
3059     op2_utf8 = build_signature_for_libgcj (as->op2);
3060
3061   entry = build_assertion_table_entry (code_val, op1_utf8, op2_utf8);
3062   
3063   CONSTRUCTOR_APPEND_ELT (*v, NULL_TREE, entry);
3064   return true;
3065 }
3066
3067 /* Generate the type assertion table for KLASS, and return its DECL.  */
3068
3069 static tree
3070 emit_assertion_table (tree klass)
3071 {
3072   tree null_entry, ctor, table_decl;
3073   htab_t assertions_htab = TYPE_ASSERTIONS (klass);
3074   VEC(constructor_elt,gc) *v = NULL;
3075
3076   /* Iterate through the hash table.  */
3077   htab_traverse (assertions_htab, add_assertion_table_entry, &v);
3078
3079   /* Finish with a null entry.  */
3080   null_entry = build_assertion_table_entry (integer_zero_node,
3081                                             null_pointer_node,
3082                                             null_pointer_node);
3083   
3084   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_entry);
3085   
3086   ctor = build_constructor (assertion_table_type, v);
3087
3088   table_decl = build_decl (input_location,
3089                            VAR_DECL, mangled_classname ("_type_assert_", klass),
3090                            assertion_table_type);
3091
3092   TREE_STATIC (table_decl) = 1;
3093   TREE_READONLY (table_decl) = 1;
3094   TREE_CONSTANT (table_decl) = 1;
3095   DECL_IGNORED_P (table_decl) = 1;
3096
3097   DECL_INITIAL (table_decl) = ctor;
3098   DECL_ARTIFICIAL (table_decl) = 1;
3099   rest_of_decl_compilation (table_decl, 1, 0);
3100
3101   return table_decl;
3102 }
3103
3104 void
3105 init_class_processing (void)
3106 {
3107   fields_ident = get_identifier ("fields");
3108   info_ident = get_identifier ("info");
3109
3110   gcc_obstack_init (&temporary_obstack);
3111 }
3112 \f
3113 static hashval_t java_treetreehash_hash (const void *);
3114 static int java_treetreehash_compare (const void *, const void *);
3115
3116 /* A hash table mapping trees to trees.  Used generally.  */
3117
3118 #define JAVA_TREEHASHHASH_H(t) ((hashval_t)TYPE_UID (t))
3119
3120 static hashval_t
3121 java_treetreehash_hash (const void *k_p)
3122 {
3123   const struct treetreehash_entry *const k
3124     = (const struct treetreehash_entry *) k_p;
3125   return JAVA_TREEHASHHASH_H (k->key);
3126 }
3127
3128 static int
3129 java_treetreehash_compare (const void * k1_p, const void * k2_p)
3130 {
3131   const struct treetreehash_entry *const k1
3132     = (const struct treetreehash_entry *) k1_p;
3133   const_tree const k2 = (const_tree) k2_p;
3134   return (k1->key == k2);
3135 }
3136
3137 tree 
3138 java_treetreehash_find (htab_t ht, tree t)
3139 {
3140   struct treetreehash_entry *e;
3141   hashval_t hv = JAVA_TREEHASHHASH_H (t);
3142   e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
3143   if (e == NULL)
3144     return NULL;
3145   else
3146     return e->value;
3147 }
3148
3149 tree *
3150 java_treetreehash_new (htab_t ht, tree t)
3151 {
3152   void **e;
3153   struct treetreehash_entry *tthe;
3154   hashval_t hv = JAVA_TREEHASHHASH_H (t);
3155
3156   e = htab_find_slot_with_hash (ht, t, hv, INSERT);
3157   if (*e == NULL)
3158     {
3159       tthe = ggc_alloc_cleared_treetreehash_entry ();
3160       tthe->key = t;
3161       *e = tthe;
3162     }
3163   else
3164     tthe = (struct treetreehash_entry *) *e;
3165   return &tthe->value;
3166 }
3167
3168 htab_t
3169 java_treetreehash_create (size_t size)
3170 {
3171   return htab_create_ggc (size, java_treetreehash_hash,
3172                           java_treetreehash_compare, NULL);
3173 }
3174
3175 /* Break down qualified IDENTIFIER into package and class-name components.
3176    For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
3177    "pkg.foo", and RIGHT to "Bar". */
3178
3179 int
3180 split_qualified_name (tree *left, tree *right, tree source)
3181 {
3182   char *p, *base;
3183   int l = IDENTIFIER_LENGTH (source);
3184
3185   base = (char *) alloca (l + 1);
3186   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
3187
3188   /* Breakdown NAME into REMAINDER . IDENTIFIER.  */
3189   p = base + l - 1;
3190   while (*p != '.' && p != base)
3191     p--;
3192
3193   /* We didn't find a '.'. Return an error.  */
3194   if (p == base)
3195     return 1;
3196
3197   *p = '\0';
3198   if (right)
3199     *right = get_identifier (p+1);
3200   *left = get_identifier (base);
3201
3202   return 0;
3203 }
3204
3205 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE 
3206    if the classes are from the same package. */
3207
3208 int
3209 in_same_package (tree name1, tree name2)
3210 {
3211   tree tmp;
3212   tree pkg1;
3213   tree pkg2;
3214
3215   if (TREE_CODE (name1) == TYPE_DECL)
3216     name1 = DECL_NAME (name1);
3217   if (TREE_CODE (name2) == TYPE_DECL)
3218     name2 = DECL_NAME (name2);
3219
3220   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
3221     /* One in empty package. */
3222     return 0;
3223
3224   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
3225     /* Both in empty package. */
3226     return 1;
3227
3228   split_qualified_name (&pkg1, &tmp, name1);
3229   split_qualified_name (&pkg2, &tmp, name2);
3230
3231   return (pkg1 == pkg2);
3232 }
3233
3234 /* lang_hooks.decls.final_write_globals: perform final processing on
3235    global variables.  */
3236
3237 void
3238 java_write_globals (void)
3239 {
3240   tree *vec = VEC_address (tree, pending_static_fields);
3241   int len = VEC_length (tree, pending_static_fields);
3242   write_global_declarations ();
3243   emit_debug_global_declarations (vec, len);
3244   VEC_free (tree, gc, pending_static_fields);
3245 }
3246
3247 #include "gt-java-class.h"