OSDN Git Service

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