OSDN Git Service

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