OSDN Git Service

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