OSDN Git Service

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