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