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