OSDN Git Service

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