OSDN Git Service

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