OSDN Git Service

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