OSDN Git Service

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