OSDN Git Service

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