1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.
19 
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
23 
24 /* Written by Per Bothner <bothner@cygnus.com> */
25 
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "java-tree.h"
32 #include "jcf.h"
33 #include "obstack.h"
34 #include "diagnostic-core.h"
35 #include "toplev.h"
36 #include "output.h" /* for switch_to_section and get_section */
37 #include "parse.h"
38 #include "function.h"
39 #include "ggc.h"
40 #include "cgraph.h"
41 #include "tree-iterator.h"
42 #include "target.h"
43 
44 static tree make_method_value (tree);
45 static tree build_java_method_type (tree, tree, int);
46 static int32 hashUtf8String (const char *, int);
47 static tree make_field_value (tree);
48 static tree get_dispatch_vector (tree);
49 static tree get_dispatch_table (tree, tree);
50 static int supers_all_compiled (tree type);
51 static tree maybe_layout_super_class (tree, tree);
52 static void add_miranda_methods (tree, tree);
53 static int assume_compiled (const char *);
54 static tree build_symbol_entry (tree, tree);
55 static tree emit_assertion_table (tree);
56 static void register_class (void);
57 
58 struct obstack temporary_obstack;
59 
60 static const char *cyclic_inheritance_report;
61 
62 /* The compiler generates different code depending on whether or not
63    it can assume certain classes have been compiled down to native
64    code or not.  The compiler options -fassume-compiled= and
65    -fno-assume-compiled= are used to create a tree of
66    class_flag_node objects.  This tree is queried to determine if
67    a class is assume to be compiled or not.  Each node in the tree
68    represents either a package or a specific class.  */
69 
70 typedef struct class_flag_node_struct
71 {
72   /* The class or package name.  */
73   const char *ident;
74 
75   /* Nonzero if this represents an exclusion.  */
76   int value;
77 
78   /* Pointers to other nodes in the tree.  */
79   struct class_flag_node_struct *parent;
80   struct class_flag_node_struct *sibling;
81   struct class_flag_node_struct *child;
82 } class_flag_node;
83 
84 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
85 static void add_class_flag (class_flag_node **, const char *, int);
86 
87 /* This is the root of the include/exclude tree.  */
88 
89 static class_flag_node *assume_compiled_tree;
90 
91 static class_flag_node *enable_assert_tree;
92 
93 static GTY(()) tree class_roots[4];
94 #define fields_ident class_roots[0]  /* get_identifier ("fields") */
95 #define info_ident class_roots[1]  /* get_identifier ("info") */
96 #define class_list class_roots[2]
97 #define class_dtable_decl class_roots[3]
98 
99 static GTY(()) vec<tree, va_gc> *registered_class;
100 
101 /* A tree that returns the address of the class$ of the class
102    currently being compiled.  */
103 static GTY(()) tree this_classdollar;
104 
105 /* A list of static class fields.  This is to emit proper debug
106    info for them.  */
107 vec<tree, va_gc> *pending_static_fields;
108 
109 /* Return the node that most closely represents the class whose name
110    is IDENT.  Start the search from NODE (followed by its siblings).
111    Return NULL if an appropriate node does not exist.  */
112 
113 static class_flag_node *
find_class_flag_node(class_flag_node * node,const char * ident)114 find_class_flag_node (class_flag_node *node, const char *ident)
115 {
116   while (node)
117     {
118       size_t node_ident_length = strlen (node->ident);
119 
120       /* node_ident_length is zero at the root of the tree.  If the
121 	 identifiers are the same length, then we have matching
122 	 classes.  Otherwise check if we've matched an enclosing
123 	 package name.  */
124 
125       if (node_ident_length == 0
126 	  || (strncmp (ident, node->ident, node_ident_length) == 0
127 	      && (ident[node_ident_length] == '\0'
128 		  || ident[node_ident_length] == '.')))
129 	{
130 	  /* We've found a match, however, there might be a more
131              specific match.  */
132 
133 	  class_flag_node *found = find_class_flag_node (node->child, ident);
134 	  if (found)
135 	    return found;
136 	  else
137 	    return node;
138 	}
139 
140       /* No match yet.  Continue through the sibling list.  */
141       node = node->sibling;
142     }
143 
144   /* No match at all in this tree.  */
145   return NULL;
146 }
147 
148 void
add_class_flag(class_flag_node ** rootp,const char * ident,int value)149 add_class_flag (class_flag_node **rootp, const char *ident, int value)
150 {
151   class_flag_node *root = *rootp;
152   class_flag_node *parent, *node;
153 
154   /* Create the root of the tree if it doesn't exist yet.  */
155 
156   if (NULL == root)
157     {
158       root = XNEW (class_flag_node);
159       root->ident = "";
160       root->value = 0;
161       root->sibling = NULL;
162       root->child = NULL;
163       root->parent = NULL;
164       *rootp = root;
165     }
166 
167   /* Calling the function with the empty string means we're setting
168      value for the root of the hierarchy.  */
169 
170   if (0 == ident[0])
171     {
172       root->value = value;
173       return;
174     }
175 
176   /* Find the parent node for this new node.  PARENT will either be a
177      class or a package name.  Adjust PARENT accordingly.  */
178 
179   parent = find_class_flag_node (root, ident);
180   if (strcmp (ident, parent->ident) == 0)
181     parent->value = value;
182   else
183     {
184       /* Insert new node into the tree.  */
185       node = XNEW (class_flag_node);
186 
187       node->ident = xstrdup (ident);
188       node->value = value;
189       node->child = NULL;
190 
191       node->parent = parent;
192       node->sibling = parent->child;
193       parent->child = node;
194     }
195 }
196 
197 /* Add a new IDENT to the include/exclude tree.  It's an exclusion
198    if EXCLUDEP is nonzero.  */
199 
200 void
add_assume_compiled(const char * ident,int excludep)201 add_assume_compiled (const char *ident, int excludep)
202 {
203   add_class_flag (&assume_compiled_tree, ident, excludep);
204 }
205 
206 /* The default value returned by enable_assertions. */
207 
208 #define DEFAULT_ENABLE_ASSERT (optimize == 0)
209 
210 /* Enter IDENT (a class or package name) into the enable-assertions table.
211    VALUE is true to enable and false to disable. */
212 
213 void
add_enable_assert(const char * ident,int value)214 add_enable_assert (const char *ident, int value)
215 {
216   if (enable_assert_tree == NULL)
217     add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
218   add_class_flag (&enable_assert_tree, ident, value);
219 }
220 
221 /* Returns nonzero if IDENT is the name of a class that the compiler
222    should assume has been compiled to object code.  */
223 
224 static int
assume_compiled(const char * ident)225 assume_compiled (const char *ident)
226 {
227   class_flag_node *i;
228   int result;
229 
230   if (NULL == assume_compiled_tree)
231     return 1;
232 
233   i = find_class_flag_node (assume_compiled_tree, ident);
234 
235   result = ! i->value;
236 
237   return (result);
238 }
239 
240 /* Return true if we should generate code to check assertions within KLASS. */
241 
242 bool
enable_assertions(tree klass)243 enable_assertions (tree klass)
244 {
245   /* Check if command-line specifies whether we should check assertions. */
246 
247   if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
248     {
249       const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
250       class_flag_node *node
251 	= find_class_flag_node (enable_assert_tree, ident);
252       return node->value;
253     }
254 
255   /* The default is to enable assertions if generating class files,
256      or not optimizing. */
257   return DEFAULT_ENABLE_ASSERT;
258 }
259 
260 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
261    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
262    Also, PREFIX is prepended, and SUFFIX is appended. */
263 
264 tree
ident_subst(const char * old_name,int old_length,const char * prefix,int old_char,int new_char,const char * suffix)265 ident_subst (const char* old_name,
266 	     int old_length,
267 	     const char *prefix,
268 	     int old_char,
269 	     int new_char,
270 	     const char *suffix)
271 {
272   int prefix_len = strlen (prefix);
273   int suffix_len = strlen (suffix);
274   int i = prefix_len + old_length + suffix_len + 1;
275   char *buffer = (char *) alloca (i);
276 
277   strcpy (buffer, prefix);
278   for (i = 0; i < old_length; i++)
279     {
280       char ch = old_name[i];
281       if (ch == old_char)
282 	ch = new_char;
283       buffer[prefix_len + i] = ch;
284     }
285   strcpy (buffer + prefix_len + old_length, suffix);
286   return get_identifier (buffer);
287 }
288 
289 /* Return an IDENTIFIER_NODE the same as OLD_ID,
290    except that characters matching OLD_CHAR are substituted by NEW_CHAR.
291    Also, PREFIX is prepended, and SUFFIX is appended. */
292 
293 tree
identifier_subst(const tree old_id,const char * prefix,int old_char,int new_char,const char * suffix)294 identifier_subst (const tree old_id,
295 		  const char *prefix,
296 		  int old_char,
297 		  int new_char,
298 		  const char *suffix)
299 {
300   return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
301 		      prefix, old_char, new_char, suffix);
302 }
303 
304 /* Generate a valid C identifier from the name of the class TYPE,
305    prefixed by PREFIX. */
306 
307 tree
mangled_classname(const char * prefix,tree type)308 mangled_classname (const char *prefix, tree type)
309 {
310   tree result;
311   tree ident = TYPE_NAME (type);
312   if (TREE_CODE (ident) != IDENTIFIER_NODE)
313     ident = DECL_NAME (ident);
314   result = identifier_subst (ident, prefix, '.', '_', "");
315 
316   /* Replace any characters that aren't in the set [0-9a-zA-Z_$] with
317      "_0xXX".  Class names containing such chracters are uncommon, but
318      they do sometimes occur in class files.  Without this check,
319      these names cause assembly errors.
320 
321      There is a possibility that a real class name could conflict with
322      the identifier we generate, but it is unlikely and will
323      immediately be detected as an assembler error.  At some point we
324      should do something more elaborate (perhaps using the full
325      unicode mangling scheme) in order to prevent such a conflict.  */
326   {
327     int i;
328     const int len = IDENTIFIER_LENGTH (result);
329     const char *p = IDENTIFIER_POINTER (result);
330     int illegal_chars = 0;
331 
332     /* Make two passes over the identifier.  The first pass is merely
333        to count illegal characters; we need to do this in order to
334        allocate a buffer.  */
335     for (i = 0; i < len; i++)
336       {
337 	char c = p[i];
338 	illegal_chars += (! ISALNUM (c) && c != '_' && c != '$');
339       }
340 
341     /* And the second pass, which is rarely executed, does the
342        rewriting.  */
343     if (illegal_chars != 0)
344       {
345 	char *buffer = (char *) alloca (illegal_chars * 4 + len + 1);
346 	int j;
347 
348 	for (i = 0, j = 0; i < len; i++)
349 	  {
350 	    char c = p[i];
351 	    if (! ISALNUM (c) && c != '_' && c != '$')
352 	      {
353 		buffer[j++] = '_';
354 		sprintf (&buffer[j], "0x%02x", c);
355 		j += 4;
356 	      }
357 	    else
358 	      buffer[j++] = c;
359 	  }
360 
361 	buffer[j] = 0;
362 	result = get_identifier (buffer);
363       }
364   }
365 
366   return result;
367 }
368 
369 tree
make_class(void)370 make_class (void)
371 {
372   tree type;
373   type = make_node (RECORD_TYPE);
374   /* Unfortunately we must create the binfo here, so that class
375      loading works.  */
376   TYPE_BINFO (type) = make_tree_binfo (0);
377   MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
378   TYPE_CATCH_CLASSES (type) = NULL;
379   /* Push a dummy entry; we can't call make_catch_class_record here
380      because other infrastructure may not be set up yet.  We'll come
381      back and fill it in later once said infrastructure is
382      initialized.  */
383   CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (type), NULL_TREE, NULL_TREE);
384 
385   return type;
386 }
387 
388 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
389    and where each of the constituents is separated by '/',
390    return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
391 
392 tree
unmangle_classname(const char * name,int name_length)393 unmangle_classname (const char *name, int name_length)
394 {
395   tree to_return = ident_subst (name, name_length, "", '/', '.', "");
396   /* It's not sufficient to compare to_return and get_identifier
397      (name) to determine whether to_return is qualified. There are
398      cases in signature analysis where name will be stripped of a
399      trailing ';'. */
400   name = IDENTIFIER_POINTER (to_return);
401   while (*name)
402     if (*name++ == '.')
403       {
404 	QUALIFIED_P (to_return) = 1;
405 	break;
406       }
407 
408   return to_return;
409 }
410 
411 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE)			\
412 do									\
413 {									\
414   const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
415   char *buf = (char *) alloca (strlen (type_name)			\
416                                + strlen (#NAME "_syms_") + 1);		\
417   tree decl;								\
418 									\
419   sprintf (buf, #NAME "_%s", type_name);				\
420   TYPE_## TABLE ##_DECL (type) = decl =					\
421     build_decl (input_location, VAR_DECL, get_identifier (buf), TABLE_TYPE); \
422   DECL_EXTERNAL (decl) = 1;						\
423   TREE_STATIC (decl) = 1;						\
424   TREE_READONLY (decl) = 1;						\
425   TREE_CONSTANT (decl) = 1;						\
426   DECL_IGNORED_P (decl) = 1;						\
427   /* Mark the table as belonging to this class.  */			\
428   pushdecl (decl);							\
429   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);				\
430   DECL_OWNER (decl) = TYPE;						\
431   sprintf (buf, #NAME "_syms_%s", type_name);				\
432   TYPE_## TABLE ##_SYMS_DECL (TYPE) =					\
433     build_decl (input_location, VAR_DECL, get_identifier (buf), symbols_array_type); \
434   TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;			\
435   TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;		\
436   DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1;		\
437 }									\
438 while (0)
439 
440 /* Given a class, create the DECLs for all its associated indirect
441    dispatch tables.  */
442 void
gen_indirect_dispatch_tables(tree type)443 gen_indirect_dispatch_tables (tree type)
444 {
445   const char *type_name = IDENTIFIER_POINTER (mangled_classname ("", type));
446   {
447     tree field = NULL;
448     char *buf = (char *) alloca (strlen (type_name)
449 				 + strlen ("_catch_classes_") + 1);
450     tree catch_class_type = make_node (RECORD_TYPE);
451 
452     sprintf (buf, "_catch_classes_%s", type_name);
453     PUSH_FIELD (input_location,
454 		catch_class_type, field, "address", utf8const_ptr_type);
455     PUSH_FIELD (input_location,
456 		catch_class_type, field, "classname", ptr_type_node);
457     FINISH_RECORD (catch_class_type);
458 
459     TYPE_CTABLE_DECL (type)
460       = build_decl (input_location, VAR_DECL, get_identifier (buf),
461 		    build_array_type (catch_class_type, 0));
462     DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
463     TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
464     TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
465     TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
466     DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
467     pushdecl (TYPE_CTABLE_DECL (type));
468   }
469 
470   if (flag_indirect_dispatch)
471     {
472       GEN_TABLE (ATABLE, _atable, atable_type, type);
473       GEN_TABLE (OTABLE, _otable, otable_type, type);
474       GEN_TABLE (ITABLE, _itable, itable_type, type);
475     }
476 }
477 
478 #undef GEN_TABLE
479 
480 tree
push_class(tree class_type,tree class_name)481 push_class (tree class_type, tree class_name)
482 {
483   tree decl, signature;
484   location_t saved_loc = input_location;
485   CLASS_P (class_type) = 1;
486   decl = build_decl (input_location, TYPE_DECL, class_name, class_type);
487   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
488 
489   /* dbxout needs a DECL_SIZE if in gstabs mode */
490   DECL_SIZE (decl) = integer_zero_node;
491 
492   input_location = saved_loc;
493   signature = identifier_subst (class_name, "L", '.', '/', ";");
494   IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
495 
496   /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
497      both a typedef and in the struct name-space.  We may want to re-visit
498      this later, but for now it reduces the changes needed for gdb. */
499   DECL_ARTIFICIAL (decl) = 1;
500 
501   pushdecl_top_level (decl);
502 
503   return decl;
504 }
505 
506 /* Finds the (global) class named NAME.  Creates the class if not found.
507    Also creates associated TYPE_DECL.
508    Does not check if the class actually exists, load the class,
509    fill in field or methods, or do layout_type. */
510 
511 tree
lookup_class(tree name)512 lookup_class (tree name)
513 {
514   tree decl = IDENTIFIER_CLASS_VALUE (name);
515   if (decl == NULL_TREE)
516     decl = push_class (make_class (), name);
517   return TREE_TYPE (decl);
518 }
519 
520 void
set_super_info(int access_flags,tree this_class,tree super_class,int interfaces_count)521 set_super_info (int access_flags, tree this_class,
522 		tree super_class, int interfaces_count)
523 {
524   int total_supers = interfaces_count;
525   tree class_decl = TYPE_NAME (this_class);
526 
527   if (super_class)
528     total_supers++;
529 
530   if (total_supers)
531     TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
532   TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
533   if (super_class)
534     {
535       tree super_binfo = make_tree_binfo (0);
536       BINFO_TYPE (super_binfo) = super_class;
537       BINFO_OFFSET (super_binfo) = integer_zero_node;
538       BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
539       CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
540     }
541 
542   set_class_decl_access_flags (access_flags, class_decl);
543 }
544 
545 void
set_class_decl_access_flags(int access_flags,tree class_decl)546 set_class_decl_access_flags (int access_flags, tree class_decl)
547 {
548   if (access_flags & ACC_PUBLIC)    CLASS_PUBLIC (class_decl) = 1;
549   if (access_flags & ACC_FINAL)     CLASS_FINAL (class_decl) = 1;
550   if (access_flags & ACC_SUPER)     CLASS_SUPER (class_decl) = 1;
551   if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
552   if (access_flags & ACC_ABSTRACT)  CLASS_ABSTRACT (class_decl) = 1;
553   if (access_flags & ACC_STATIC)    CLASS_STATIC (class_decl) = 1;
554   if (access_flags & ACC_PRIVATE)   CLASS_PRIVATE (class_decl) = 1;
555   if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
556   if (access_flags & ACC_STRICT)    CLASS_STRICTFP (class_decl) = 1;
557   if (access_flags & ACC_ENUM)      CLASS_ENUM (class_decl) = 1;
558   if (access_flags & ACC_SYNTHETIC) CLASS_SYNTHETIC (class_decl) = 1;
559   if (access_flags & ACC_ANNOTATION) CLASS_ANNOTATION (class_decl) = 1;
560 }
561 
562 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
563    direct sub-classes of Object are 1, and so on. */
564 
565 int
class_depth(tree clas)566 class_depth (tree clas)
567 {
568   int depth = 0;
569   if (! CLASS_LOADED_P (clas))
570     load_class (clas, 1);
571   if (TYPE_SIZE (clas) == error_mark_node)
572     return -1;
573   while (clas != object_type_node)
574     {
575       depth++;
576       clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
577     }
578   return depth;
579 }
580 
581 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
582 
583 int
interface_of_p(tree type1,tree type2)584 interface_of_p (tree type1, tree type2)
585 {
586   int i;
587   tree binfo, base_binfo;
588 
589   if (! TYPE_BINFO (type2))
590     return 0;
591 
592   for (binfo = TYPE_BINFO (type2), i = 0;
593        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
594     if (BINFO_TYPE (base_binfo) == type1)
595       return 1;
596 
597   for (binfo = TYPE_BINFO (type2), i = 0;
598        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /*  */
599     if (BINFO_TYPE (base_binfo)
600 	&& interface_of_p (type1, BINFO_TYPE (base_binfo)))
601       return 1;
602 
603   return 0;
604 }
605 
606 /* Return true iff TYPE1 inherits from TYPE2. */
607 
608 int
inherits_from_p(tree type1,tree type2)609 inherits_from_p (tree type1, tree type2)
610 {
611   while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
612     {
613       if (type1 == type2)
614 	return 1;
615 
616       if (! CLASS_LOADED_P (type1))
617 	load_class (type1, 1);
618 
619       type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
620     }
621   return 0;
622 }
623 
624 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
625 
626 int
enclosing_context_p(tree type1,tree type2)627 enclosing_context_p (tree type1, tree type2)
628 {
629   if (!INNER_CLASS_TYPE_P (type2))
630     return 0;
631 
632   for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
633        type2;
634        type2 = (INNER_CLASS_TYPE_P (type2) ?
635 		TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
636     {
637       if (type2 == type1)
638 	return 1;
639     }
640 
641   return 0;
642 }
643 
644 
645 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
646    nesting level.  */
647 
648 int
common_enclosing_context_p(tree type1,tree type2)649 common_enclosing_context_p (tree type1, tree type2)
650 {
651   while (type1)
652     {
653       tree current;
654       for (current = type2; current;
655 	   current = (INNER_CLASS_TYPE_P (current) ?
656 		      TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
657 		      NULL_TREE))
658 	if (type1 == current)
659 	  return 1;
660 
661       if (INNER_CLASS_TYPE_P (type1))
662         type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
663       else
664         break;
665     }
666   return 0;
667 }
668 
669 /* Return 1 iff there exists a common enclosing "this" between TYPE1
670    and TYPE2, without crossing any static context.  */
671 
672 int
common_enclosing_instance_p(tree type1,tree type2)673 common_enclosing_instance_p (tree type1, tree type2)
674 {
675   if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
676     return 0;
677 
678   for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
679        type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
680 		TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
681     {
682       tree current;
683       for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
684 	   current = (PURE_INNER_CLASS_TYPE_P (current) ?
685 		      TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
686 		      NULL_TREE))
687 	if (type1 == current)
688 	  return 1;
689     }
690   return 0;
691 }
692 
693 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
694    found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
695    if attempt is made to add it twice. */
696 
697 tree
maybe_add_interface(tree this_class,tree interface_class)698 maybe_add_interface (tree this_class, tree interface_class)
699 {
700   tree binfo, base_binfo;
701   int i;
702 
703   for (binfo = TYPE_BINFO (this_class), i = 0;
704        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
705     if (BINFO_TYPE (base_binfo) == interface_class)
706       return interface_class;
707   add_interface (this_class, interface_class);
708   return NULL_TREE;
709 }
710 
711 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
712 
713 void
add_interface(tree this_class,tree interface_class)714 add_interface (tree this_class, tree interface_class)
715 {
716   tree interface_binfo = make_tree_binfo (0);
717 
718   BINFO_TYPE (interface_binfo) = interface_class;
719   BINFO_OFFSET (interface_binfo) = integer_zero_node;
720   BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
721   BINFO_VIRTUAL_P (interface_binfo) = 1;
722 
723   BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
724 }
725 
726 static tree
build_java_method_type(tree fntype,tree this_class,int access_flags)727 build_java_method_type (tree fntype, tree this_class, int access_flags)
728 {
729   if (access_flags & ACC_STATIC)
730     return fntype;
731   fntype = build_method_type (this_class, fntype);
732 
733   /* We know that arg 1 of every nonstatic method is non-null; tell
734      the back-end so.  */
735   TYPE_ATTRIBUTES (fntype) = (tree_cons
736 			      (get_identifier ("nonnull"),
737 			       tree_cons (NULL_TREE,
738 					  build_int_cst (NULL_TREE, 1),
739 					  NULL_TREE),
740 			       TYPE_ATTRIBUTES (fntype)));
741   return fntype;
742 }
743 
744 void
java_hide_decl(tree decl ATTRIBUTE_UNUSED)745 java_hide_decl (tree decl ATTRIBUTE_UNUSED)
746 {
747 #ifdef HAVE_GAS_HIDDEN
748   DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
749   DECL_VISIBILITY_SPECIFIED (decl) = 1;
750 #endif
751 }
752 
753 tree
add_method_1(tree this_class,int access_flags,tree name,tree function_type)754 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
755 {
756   tree method_type, fndecl;
757 
758   method_type = build_java_method_type (function_type,
759 					this_class, access_flags);
760 
761   fndecl = build_decl (input_location, FUNCTION_DECL, name, method_type);
762   DECL_CONTEXT (fndecl) = this_class;
763 
764   DECL_LANG_SPECIFIC (fndecl)
765     = ggc_alloc_cleared_lang_decl(sizeof (struct lang_decl));
766   DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
767 
768   /* Initialize the static initializer test table.  */
769 
770   DECL_FUNCTION_INIT_TEST_TABLE (fndecl) = java_treetreehash_create (10);
771 
772   /* Initialize the initialized (static) class table. */
773   if (access_flags & ACC_STATIC)
774     DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
775       htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
776 
777   DECL_CHAIN (fndecl) = TYPE_METHODS (this_class);
778   TYPE_METHODS (this_class) = fndecl;
779 
780   /* If pointers to member functions use the least significant bit to
781      indicate whether a function is virtual, ensure a pointer
782      to this function will have that bit clear.  */
783   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
784       && !(access_flags & ACC_STATIC)
785       && DECL_ALIGN (fndecl) < 2 * BITS_PER_UNIT)
786     DECL_ALIGN (fndecl) = 2 * BITS_PER_UNIT;
787 
788   /* Notice that this is a finalizer and update the class type
789      accordingly. This is used to optimize instance allocation. */
790   if (name == finalize_identifier_node
791       && TREE_TYPE (function_type) == void_type_node
792       && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
793     HAS_FINALIZER_P (this_class) = 1;
794 
795   if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
796   if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
797   if (access_flags & ACC_PRIVATE)
798     METHOD_PRIVATE (fndecl) = 1;
799   if (access_flags & ACC_NATIVE)
800     {
801       METHOD_NATIVE (fndecl) = 1;
802       DECL_EXTERNAL (fndecl) = 1;
803     }
804   else
805     /* FNDECL is external unless we are compiling it into this object
806        file.  */
807     DECL_EXTERNAL (fndecl) = CLASS_FROM_CURRENTLY_COMPILED_P (this_class) == 0;
808   if (access_flags & ACC_STATIC)
809     METHOD_STATIC (fndecl) = 1;
810   if (access_flags & ACC_FINAL)
811     METHOD_FINAL (fndecl) = 1;
812   if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
813   if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
814   if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
815   if (access_flags & ACC_SYNTHETIC) DECL_ARTIFICIAL (fndecl) = 1;
816   if (access_flags & ACC_BRIDGE) METHOD_BRIDGE (fndecl) = 1;
817   if (access_flags & ACC_VARARGS) METHOD_VARARGS (fndecl) = 1;
818   return fndecl;
819 }
820 
821 /* Add a method to THIS_CLASS.
822    The method's name is NAME.
823    Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
824 
825 tree
add_method(tree this_class,int access_flags,tree name,tree method_sig)826 add_method (tree this_class, int access_flags, tree name, tree method_sig)
827 {
828   tree function_type, fndecl;
829   const unsigned char *sig
830     = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
831 
832   if (sig[0] != '(')
833     fatal_error ("bad method signature");
834 
835   function_type = get_type_from_signature (method_sig);
836   fndecl = add_method_1 (this_class, access_flags, name, function_type);
837   set_java_signature (TREE_TYPE (fndecl), method_sig);
838   return fndecl;
839 }
840 
841 tree
add_field(tree klass,tree name,tree field_type,int flags)842 add_field (tree klass, tree name, tree field_type, int flags)
843 {
844   int is_static = (flags & ACC_STATIC) != 0;
845   tree field;
846   field = build_decl (input_location,
847 		      is_static ? VAR_DECL : FIELD_DECL, name, field_type);
848   DECL_CHAIN (field) = TYPE_FIELDS (klass);
849   TYPE_FIELDS (klass) = field;
850   DECL_CONTEXT (field) = klass;
851   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
852 
853   if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
854   if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
855   if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
856   if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
857   if (flags & ACC_VOLATILE)
858     {
859       FIELD_VOLATILE (field) = 1;
860       TREE_THIS_VOLATILE (field) = 1;
861     }
862   if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
863   if (flags & ACC_ENUM) FIELD_ENUM (field) = 1;
864   if (flags & ACC_SYNTHETIC) FIELD_SYNTHETIC (field) = 1;
865   if (is_static)
866     {
867       FIELD_STATIC (field) = 1;
868       /* Always make field externally visible.  This is required so
869 	 that native methods can always access the field.  */
870       TREE_PUBLIC (field) = 1;
871       /* Hide everything that shouldn't be visible outside a DSO.  */
872       if (flag_indirect_classes
873 	  || (FIELD_PRIVATE (field)))
874 	java_hide_decl (field);
875       /* Considered external unless we are compiling it into this
876 	 object file.  */
877       DECL_EXTERNAL (field) = (is_compiled_class (klass) != 2);
878       if (!DECL_EXTERNAL (field))
879 	vec_safe_push (pending_static_fields, field);
880     }
881 
882   return field;
883 }
884 
885 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
886 
887 void
set_constant_value(tree field,tree constant)888 set_constant_value (tree field, tree constant)
889 {
890   if (field == NULL_TREE)
891     warning (OPT_Wattributes,
892 	     "misplaced ConstantValue attribute (not in any field)");
893   else if (DECL_INITIAL (field) != NULL_TREE)
894     warning (OPT_Wattributes,
895 	     "duplicate ConstantValue attribute for field '%s'",
896 	     IDENTIFIER_POINTER (DECL_NAME (field)));
897   else
898     {
899       DECL_INITIAL (field) = constant;
900       if (TREE_TYPE (constant) != TREE_TYPE (field)
901 	  && ! (TREE_TYPE (constant) == int_type_node
902 		&& INTEGRAL_TYPE_P (TREE_TYPE (field))
903 		&& TYPE_PRECISION (TREE_TYPE (field)) <= 32)
904 	  && ! (TREE_TYPE (constant) == utf8const_ptr_type
905 		&& TREE_TYPE (field) == string_ptr_type_node))
906 	error ("ConstantValue attribute of field '%s' has wrong type",
907 	       IDENTIFIER_POINTER (DECL_NAME (field)));
908     }
909 }
910 
911 /* Calculate a hash value for a string encoded in Utf8 format.
912  * This returns the same hash value as specified for java.lang.String.hashCode.
913  */
914 
915 static int32
hashUtf8String(const char * str,int len)916 hashUtf8String (const char *str, int len)
917 {
918   const unsigned char* ptr = (const unsigned char*) str;
919   const unsigned char *limit = ptr + len;
920   int32 hash = 0;
921   for (; ptr < limit;)
922     {
923       int ch = UTF8_GET (ptr, limit);
924       /* Updated specification from
925 	 http://www.javasoft.com/docs/books/jls/clarify.html. */
926       hash = (31 * hash) + ch;
927     }
928   return hash;
929 }
930 
931 tree
build_utf8_ref(tree name)932 build_utf8_ref (tree name)
933 {
934   const char * name_ptr = IDENTIFIER_POINTER (name);
935   int name_len = IDENTIFIER_LENGTH (name), name_pad;
936   char buf[60];
937   tree ctype, field = NULL_TREE, str_type, cinit, string;
938   static int utf8_count = 0;
939   int name_hash;
940   tree ref = IDENTIFIER_UTF8_REF (name);
941   tree decl;
942   vec<constructor_elt, va_gc> *v = NULL;
943   if (ref != NULL_TREE)
944     return ref;
945 
946   ctype = make_node (RECORD_TYPE);
947   /* '\0' byte plus padding to utf8const_type's alignment.  */
948   name_pad = TYPE_ALIGN_UNIT (utf8const_type)
949 	     - (name_len & (TYPE_ALIGN_UNIT (utf8const_type) - 1));
950   str_type = build_prim_array_type (unsigned_byte_type_node,
951 				    name_len + name_pad);
952   PUSH_FIELD (input_location, ctype, field, "hash", unsigned_short_type_node);
953   PUSH_FIELD (input_location,
954 	      ctype, field, "length", unsigned_short_type_node);
955   PUSH_FIELD (input_location, ctype, field, "data", str_type);
956   FINISH_RECORD (ctype);
957   START_RECORD_CONSTRUCTOR (v, ctype);
958   name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
959   PUSH_FIELD_VALUE (v, "hash", build_int_cst (NULL_TREE, name_hash));
960   PUSH_FIELD_VALUE (v, "length", build_int_cst (NULL_TREE, name_len));
961   string = build_string (name_len, name_ptr);
962   TREE_TYPE (string) = str_type;
963   PUSH_FIELD_VALUE (v, "data", string);
964   FINISH_RECORD_CONSTRUCTOR (cinit, v, ctype);
965   TREE_CONSTANT (cinit) = 1;
966 
967   /* Generate a unique-enough identifier.  */
968   sprintf(buf, "_Utf%d", ++utf8_count);
969 
970   decl = build_decl (input_location,
971 		     VAR_DECL, get_identifier (buf), utf8const_type);
972   TREE_STATIC (decl) = 1;
973   DECL_ARTIFICIAL (decl) = 1;
974   DECL_IGNORED_P (decl) = 1;
975   TREE_READONLY (decl) = 1;
976   TREE_THIS_VOLATILE (decl) = 0;
977   DECL_INITIAL (decl) = cinit;
978   DECL_USER_ALIGN (decl) = 1;
979 
980   if (HAVE_GAS_SHF_MERGE)
981     {
982       int decl_size;
983       /* Ensure decl_size is a multiple of utf8const_type's alignment. */
984       decl_size = name_len + 4 + name_pad;
985       if (flag_merge_constants && decl_size < 256)
986 	{
987 	  char buf[32];
988 	  int flags = (SECTION_OVERRIDE
989 		       | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
990 	  sprintf (buf, ".rodata.jutf8.%d", decl_size);
991 	  switch_to_section (get_section (buf, flags, NULL));
992 	  DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
993 	}
994     }
995 
996   layout_decl (decl, 0);
997   DECL_SIZE (decl) = TYPE_SIZE (ctype);
998   DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (ctype);
999   pushdecl (decl);
1000   rest_of_decl_compilation (decl, global_bindings_p (), 0);
1001   ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
1002   IDENTIFIER_UTF8_REF (name) = ref;
1003   return ref;
1004 }
1005 
1006 /* Like build_class_ref, but instead of a direct reference generate a
1007    pointer into the constant pool.  */
1008 
1009 static tree
build_indirect_class_ref(tree type)1010 build_indirect_class_ref (tree type)
1011 {
1012   int index;
1013   tree cl;
1014   index = alloc_class_constant (type);
1015   cl = build_ref_from_constant_pool (index);
1016   return convert (promote_type (class_ptr_type), cl);
1017 }
1018 
1019 static tree
build_static_class_ref(tree type)1020 build_static_class_ref (tree type)
1021 {
1022   tree decl_name, decl, ref;
1023 
1024   if (TYPE_SIZE (type) == error_mark_node)
1025     return null_pointer_node;
1026   decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1027 				"", '/', '/', ".class$$");
1028   decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1029   if (decl == NULL_TREE)
1030     {
1031       decl = build_decl (input_location, VAR_DECL, decl_name, class_type_node);
1032       TREE_STATIC (decl) = 1;
1033       if (! flag_indirect_classes)
1034 	{
1035 	  TREE_PUBLIC (decl) = 1;
1036 	  if (CLASS_PRIVATE (TYPE_NAME (type)))
1037 	    java_hide_decl (decl);
1038 	}
1039       DECL_IGNORED_P (decl) = 1;
1040       DECL_ARTIFICIAL (decl) = 1;
1041       if (is_compiled_class (type) == 1)
1042 	DECL_EXTERNAL (decl) = 1;
1043       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1044       DECL_CLASS_FIELD_P (decl) = 1;
1045       DECL_CONTEXT (decl) = type;
1046 
1047       /* ??? We want to preserve the DECL_CONTEXT we set just above,
1048 	 that that means not calling pushdecl_top_level.  */
1049       IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
1050     }
1051 
1052   ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1053   return ref;
1054 }
1055 
1056 static tree
build_classdollar_field(tree type)1057 build_classdollar_field (tree type)
1058 {
1059   tree decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1060 				     "", '/', '/', ".class$");
1061   tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1062 
1063   if (decl == NULL_TREE)
1064     {
1065       decl
1066 	= build_decl (input_location,
1067 		      VAR_DECL, decl_name,
1068 		      (build_type_variant
1069 		       (build_pointer_type
1070 			(build_type_variant (class_type_node,
1071 					     /* const */ 1, 0)),
1072 			/* const */ 1, 0)));
1073       TREE_STATIC (decl) = 1;
1074       TREE_CONSTANT (decl) = 1;
1075       TREE_READONLY (decl) = 1;
1076       TREE_PUBLIC (decl) = 1;
1077       java_hide_decl (decl);
1078       DECL_IGNORED_P (decl) = 1;
1079       DECL_ARTIFICIAL (decl) = 1;
1080       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1081       IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
1082       DECL_CLASS_FIELD_P (decl) = 1;
1083       DECL_CONTEXT (decl) = type;
1084     }
1085 
1086   return decl;
1087 }
1088 
1089 /* Create a local variable that holds the current class$.  */
1090 
1091 void
cache_this_class_ref(tree fndecl)1092 cache_this_class_ref (tree fndecl)
1093 {
1094   if (optimize)
1095     {
1096       tree classdollar_field;
1097       if (flag_indirect_classes)
1098 	classdollar_field = build_classdollar_field (output_class);
1099       else
1100 	classdollar_field = build_static_class_ref (output_class);
1101 
1102       this_classdollar = build_decl (input_location,
1103 				     VAR_DECL, NULL_TREE,
1104 				     TREE_TYPE (classdollar_field));
1105 
1106       java_add_local_var (this_classdollar);
1107       java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (this_classdollar),
1108 			     this_classdollar, classdollar_field));
1109     }
1110   else
1111     this_classdollar = build_classdollar_field (output_class);
1112 
1113   /* Prepend class initialization for static methods reachable from
1114      other classes.  */
1115   if (METHOD_STATIC (fndecl)
1116       && (! METHOD_PRIVATE (fndecl)
1117           || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
1118       && ! DECL_CLINIT_P (fndecl)
1119       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
1120     {
1121       tree init = build_call_expr (soft_initclass_node, 1,
1122 				   this_classdollar);
1123       java_add_stmt (init);
1124     }
1125 }
1126 
1127 /* Remove the reference to the local variable that holds the current
1128    class$.  */
1129 
1130 void
uncache_this_class_ref(tree fndecl ATTRIBUTE_UNUSED)1131 uncache_this_class_ref (tree fndecl ATTRIBUTE_UNUSED)
1132 {
1133   this_classdollar = build_classdollar_field (output_class);
1134 }
1135 
1136 /* Build a reference to the class TYPE.
1137    Also handles primitive types and array types. */
1138 
1139 tree
build_class_ref(tree type)1140 build_class_ref (tree type)
1141 {
1142   int is_compiled = is_compiled_class (type);
1143   if (is_compiled)
1144     {
1145       tree ref, decl;
1146       if (TREE_CODE (type) == POINTER_TYPE)
1147 	type = TREE_TYPE (type);
1148 
1149       if (flag_indirect_dispatch
1150 	  && type != output_class
1151 	  && TREE_CODE (type) == RECORD_TYPE)
1152 	return build_indirect_class_ref (type);
1153 
1154       if (type == output_class && flag_indirect_classes)
1155 	{
1156 	  /* This can be NULL if we see a JNI stub before we see any
1157 	     other method.  */
1158 	  if (! this_classdollar)
1159 	    this_classdollar = build_classdollar_field (output_class);
1160 	  return this_classdollar;
1161 	}
1162 
1163       if (TREE_CODE (type) == RECORD_TYPE)
1164 	return build_static_class_ref (type);
1165       else
1166 	{
1167 	  const char *name;
1168 	  tree decl_name;
1169 	  char buffer[25];
1170 	  decl_name = TYPE_NAME (type);
1171 	  if (TREE_CODE (decl_name) == TYPE_DECL)
1172 	    decl_name = DECL_NAME (decl_name);
1173 	  name = IDENTIFIER_POINTER (decl_name);
1174 	  if (strncmp (name, "promoted_", 9) == 0)
1175 	    name += 9;
1176 	  sprintf (buffer, "_Jv_%sClass", name);
1177 	  decl_name = get_identifier (buffer);
1178 	  decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1179 	  if (decl == NULL_TREE)
1180 	    {
1181 	      decl = build_decl (input_location,
1182 				 VAR_DECL, decl_name, class_type_node);
1183 	      TREE_STATIC (decl) = 1;
1184 	      TREE_PUBLIC (decl) = 1;
1185 	      DECL_EXTERNAL (decl) = 1;
1186 	      DECL_ARTIFICIAL (decl) = 1;
1187 	      pushdecl_top_level (decl);
1188 	    }
1189 	}
1190 
1191       ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1192       return ref;
1193     }
1194   else
1195     return build_indirect_class_ref (type);
1196 }
1197 
1198 /* Create a local statically allocated variable that will hold a
1199    pointer to a static field.  */
1200 
1201 static tree
build_fieldref_cache_entry(int index,tree fdecl ATTRIBUTE_UNUSED)1202 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1203 {
1204   tree decl, decl_name;
1205   const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1206   char *buf = (char *) alloca (strlen (name) + 20);
1207   sprintf (buf, "%s_%d_ref", name, index);
1208   decl_name = get_identifier (buf);
1209   decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1210   if (decl == NULL_TREE)
1211     {
1212       decl = build_decl (input_location,
1213 			 VAR_DECL, decl_name, ptr_type_node);
1214       TREE_STATIC (decl) = 1;
1215       TREE_PUBLIC (decl) = 0;
1216       DECL_EXTERNAL (decl) = 0;
1217       DECL_ARTIFICIAL (decl) = 1;
1218       DECL_IGNORED_P (decl) = 1;
1219       pushdecl_top_level (decl);
1220     }
1221   return decl;
1222 }
1223 
1224 tree
build_static_field_ref(tree fdecl)1225 build_static_field_ref (tree fdecl)
1226 {
1227   tree fclass = DECL_CONTEXT (fdecl);
1228   int is_compiled = is_compiled_class (fclass);
1229 
1230   /* Allow static final fields to fold to a constant.  When using
1231      -findirect-dispatch, we simply never do this folding if compiling
1232      from .class; in the .class file constants will be referred to via
1233      the constant pool.  */
1234   if (!flag_indirect_dispatch
1235       && (is_compiled
1236 	  || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1237 	      && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1238 		  || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1239 	      && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
1240     {
1241       if (is_compiled == 1)
1242 	DECL_EXTERNAL (fdecl) = 1;
1243     }
1244   else
1245     {
1246       /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1247 	 and a class local static variable CACHE_ENTRY, then
1248 
1249       *(fdecl **)((__builtin_expect (cache_entry == null, false))
1250 		  ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1251 		  : cache_entry)
1252 
1253       This can mostly be optimized away, so that the usual path is a
1254       load followed by a test and branch.  _Jv_ResolvePoolEntry is
1255       only called once for each constant pool entry.
1256 
1257       There is an optimization that we don't do: at the start of a
1258       method, create a local copy of CACHE_ENTRY and use that instead.
1259 
1260       */
1261 
1262       int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1263       tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1264       tree test
1265         = build_call_expr (builtin_decl_implicit (BUILT_IN_EXPECT), 2,
1266 			   build2 (EQ_EXPR, boolean_type_node,
1267 				   cache_entry, null_pointer_node),
1268 			   boolean_false_node);
1269       tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1270       tree init
1271 	= build_call_expr (soft_resolvepoolentry_node, 2,
1272 			   build_class_ref (output_class),
1273 			   cpool_index_cst);
1274       init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1275       init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1276       init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1277       fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1278     }
1279   return fdecl;
1280 }
1281 
1282 int
get_access_flags_from_decl(tree decl)1283 get_access_flags_from_decl (tree decl)
1284 {
1285   int access_flags = 0;
1286   if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1287     {
1288       if (FIELD_STATIC (decl))
1289 	access_flags |= ACC_STATIC;
1290       if (FIELD_PUBLIC (decl))
1291 	access_flags |= ACC_PUBLIC;
1292       if (FIELD_PROTECTED (decl))
1293 	access_flags |= ACC_PROTECTED;
1294       if (FIELD_PRIVATE (decl))
1295 	access_flags |= ACC_PRIVATE;
1296       if (FIELD_FINAL (decl))
1297 	access_flags |= ACC_FINAL;
1298       if (FIELD_VOLATILE (decl))
1299 	access_flags |= ACC_VOLATILE;
1300       if (FIELD_TRANSIENT (decl))
1301 	access_flags |= ACC_TRANSIENT;
1302       if (FIELD_ENUM (decl))
1303 	access_flags |= ACC_ENUM;
1304       if (FIELD_SYNTHETIC (decl))
1305 	access_flags |= ACC_SYNTHETIC;
1306       return access_flags;
1307     }
1308   if (TREE_CODE (decl) == TYPE_DECL)
1309     {
1310       if (CLASS_PUBLIC (decl))
1311 	access_flags |= ACC_PUBLIC;
1312       if (CLASS_FINAL (decl))
1313 	access_flags |= ACC_FINAL;
1314       if (CLASS_SUPER (decl))
1315 	access_flags |= ACC_SUPER;
1316       if (CLASS_INTERFACE (decl))
1317 	access_flags |= ACC_INTERFACE;
1318       if (CLASS_ABSTRACT (decl))
1319 	access_flags |= ACC_ABSTRACT;
1320       if (CLASS_STATIC (decl))
1321 	access_flags |= ACC_STATIC;
1322       if (CLASS_PRIVATE (decl))
1323 	access_flags |= ACC_PRIVATE;
1324       if (CLASS_PROTECTED (decl))
1325 	access_flags |= ACC_PROTECTED;
1326       if (CLASS_STRICTFP (decl))
1327 	access_flags |= ACC_STRICT;
1328       if (CLASS_ENUM (decl))
1329 	access_flags |= ACC_ENUM;
1330       if (CLASS_SYNTHETIC (decl))
1331 	access_flags |= ACC_SYNTHETIC;
1332       if (CLASS_ANNOTATION (decl))
1333 	access_flags |= ACC_ANNOTATION;
1334       return access_flags;
1335     }
1336   if (TREE_CODE (decl) == FUNCTION_DECL)
1337     {
1338       if (METHOD_PUBLIC (decl))
1339 	access_flags |= ACC_PUBLIC;
1340       if (METHOD_PRIVATE (decl))
1341 	access_flags |= ACC_PRIVATE;
1342       if (METHOD_PROTECTED (decl))
1343 	access_flags |= ACC_PROTECTED;
1344       if (METHOD_STATIC (decl))
1345 	access_flags |= ACC_STATIC;
1346       if (METHOD_FINAL (decl))
1347 	access_flags |= ACC_FINAL;
1348       if (METHOD_SYNCHRONIZED (decl))
1349 	access_flags |= ACC_SYNCHRONIZED;
1350       if (METHOD_NATIVE (decl))
1351 	access_flags |= ACC_NATIVE;
1352       if (METHOD_ABSTRACT (decl))
1353 	access_flags |= ACC_ABSTRACT;
1354       if (METHOD_STRICTFP (decl))
1355 	access_flags |= ACC_STRICT;
1356       if (METHOD_INVISIBLE (decl))
1357 	access_flags |= ACC_INVISIBLE;
1358       if (DECL_ARTIFICIAL (decl))
1359 	access_flags |= ACC_SYNTHETIC;
1360       if (METHOD_BRIDGE (decl))
1361 	access_flags |= ACC_BRIDGE;
1362       if (METHOD_VARARGS (decl))
1363 	access_flags |= ACC_VARARGS;
1364       return access_flags;
1365     }
1366   gcc_unreachable ();
1367 }
1368 
1369 static GTY (()) int alias_labelno = 0;
1370 
1371 /* Create a private alias for METHOD. Using this alias instead of the method
1372    decl ensures that ncode entries in the method table point to the real function
1373    at runtime, not a PLT entry.  */
1374 
1375 static tree
make_local_function_alias(tree method)1376 make_local_function_alias (tree method)
1377 {
1378 #ifdef ASM_OUTPUT_DEF
1379   tree alias;
1380 
1381   const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1382   char *name = (char *) alloca (strlen (method_name) + 2);
1383   char *buf = (char *) alloca (strlen (method_name) + 128);
1384 
1385   /* Only create aliases for local functions.  */
1386   if (DECL_EXTERNAL (method))
1387     return method;
1388 
1389   /* Prefix method_name with 'L' for the alias label.  */
1390   *name = 'L';
1391   strcpy (name + 1, method_name);
1392 
1393   targetm.asm_out.generate_internal_label (buf, name, alias_labelno++);
1394   alias = build_decl (input_location,
1395 		      FUNCTION_DECL, get_identifier (buf),
1396 		      TREE_TYPE (method));
1397   DECL_CONTEXT (alias) = NULL;
1398   TREE_READONLY (alias) = TREE_READONLY (method);
1399   TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1400   TREE_PUBLIC (alias) = 0;
1401   DECL_EXTERNAL (alias) = 0;
1402   DECL_ARTIFICIAL (alias) = 1;
1403   DECL_INITIAL (alias) = error_mark_node;
1404   TREE_ADDRESSABLE (alias) = 1;
1405   TREE_USED (alias) = 1;
1406   if (!flag_syntax_only)
1407     assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1408   return alias;
1409 #else
1410   return method;
1411 #endif
1412 }
1413 
1414 /** Make reflection data (_Jv_Field) for field FDECL. */
1415 
1416 static tree
make_field_value(tree fdecl)1417 make_field_value (tree fdecl)
1418 {
1419   tree finit;
1420   int flags;
1421   tree type = TREE_TYPE (fdecl);
1422   int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1423   vec<constructor_elt, va_gc> *v = NULL;
1424 
1425   START_RECORD_CONSTRUCTOR (v, field_type_node);
1426   PUSH_FIELD_VALUE (v, "name", build_utf8_ref (DECL_NAME (fdecl)));
1427   if (resolved)
1428     type = build_class_ref (type);
1429   else
1430     {
1431       tree signature = build_java_signature (type);
1432 
1433       type = build_utf8_ref (unmangle_classname
1434 			     (IDENTIFIER_POINTER (signature),
1435 			      IDENTIFIER_LENGTH (signature)));
1436     }
1437   PUSH_FIELD_VALUE (v, "type", type);
1438 
1439   flags = get_access_flags_from_decl (fdecl);
1440   if (! resolved)
1441     flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1442 
1443   PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, flags));
1444   PUSH_FIELD_VALUE (v, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1445 
1446   {
1447     tree field_address = integer_zero_node;
1448     tree index, value;
1449     if ((DECL_INITIAL (fdecl) || ! flag_indirect_classes)
1450 	&& FIELD_STATIC (fdecl))
1451       field_address = build_address_of (fdecl);
1452 
1453     index = (FIELD_STATIC (fdecl)
1454 	     ? DECL_CHAIN (TYPE_FIELDS (field_info_union_node))
1455 	     : TYPE_FIELDS (field_info_union_node));
1456     value = (FIELD_STATIC (fdecl)
1457 	     ? field_address
1458 	     : byte_position (fdecl));
1459 
1460     PUSH_FIELD_VALUE
1461       (v, "info",
1462        build_constructor_single (field_info_union_node, index, value));
1463   }
1464 
1465   FINISH_RECORD_CONSTRUCTOR (finit, v, field_type_node);
1466   return finit;
1467 }
1468 
1469 /** Make reflection data (_Jv_Method) for method MDECL. */
1470 
1471 static tree
make_method_value(tree mdecl)1472 make_method_value (tree mdecl)
1473 {
1474   static int method_name_count = 0;
1475   tree minit;
1476   tree index;
1477   tree code;
1478   tree class_decl;
1479 #define ACC_TRANSLATED          0x4000
1480   int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1481   vec<constructor_elt, va_gc> *v = NULL;
1482 
1483   class_decl = DECL_CONTEXT (mdecl);
1484   /* For interfaces, the index field contains the dispatch index. */
1485   if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1486     index = build_int_cst (NULL_TREE,
1487 			   get_interface_method_index (mdecl, class_decl));
1488   else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1489     index = get_method_index (mdecl);
1490   else
1491     index = integer_minus_one_node;
1492 
1493   code = null_pointer_node;
1494   if (METHOD_ABSTRACT (mdecl))
1495     code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1496 		   soft_abstractmethod_node);
1497   else
1498     code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1499 		   make_local_function_alias (mdecl));
1500   START_RECORD_CONSTRUCTOR (v, method_type_node);
1501   PUSH_FIELD_VALUE (v, "name",
1502 		    build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1503 				    init_identifier_node
1504 				    : DECL_NAME (mdecl)));
1505   {
1506     tree signature = build_java_signature (TREE_TYPE (mdecl));
1507     PUSH_FIELD_VALUE (v, "signature",
1508 		      (build_utf8_ref
1509 		       (unmangle_classname
1510 			(IDENTIFIER_POINTER(signature),
1511 			 IDENTIFIER_LENGTH(signature)))));
1512   }
1513   PUSH_FIELD_VALUE (v, "accflags", build_int_cst (NULL_TREE, accflags));
1514   PUSH_FIELD_VALUE (v, "index", index);
1515   PUSH_FIELD_VALUE (v, "ncode", code);
1516 
1517   {
1518     /* Compute the `throws' information for the method.  */
1519     tree table = null_pointer_node;
1520 
1521     if (!vec_safe_is_empty (DECL_FUNCTION_THROWS (mdecl)))
1522       {
1523 	int length = 1 + DECL_FUNCTION_THROWS (mdecl)->length ();
1524 	tree t, type, array;
1525 	char buf[60];
1526 	vec<constructor_elt, va_gc> *v = NULL;
1527 	int idx = length - 1;
1528 	unsigned ix;
1529 	constructor_elt *e;
1530 
1531 	vec_alloc (v, length);
1532 	v->quick_grow_cleared (length);
1533 
1534 	e = &(*v)[idx--];
1535 	e->value = null_pointer_node;
1536 
1537 	FOR_EACH_VEC_SAFE_ELT (DECL_FUNCTION_THROWS (mdecl), ix, t)
1538 	  {
1539 	    tree sig = DECL_NAME (TYPE_NAME (t));
1540 	    tree utf8
1541 	      = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1542 						    IDENTIFIER_LENGTH (sig)));
1543 	    e = &(*v)[idx--];
1544 	    e->value = utf8;
1545 	  }
1546 	gcc_assert (idx == -1);
1547 	type = build_prim_array_type (ptr_type_node, length);
1548 	table = build_constructor (type, v);
1549 	/* Compute something unique enough.  */
1550 	sprintf (buf, "_methods%d", method_name_count++);
1551 	array = build_decl (input_location,
1552 			    VAR_DECL, get_identifier (buf), type);
1553 	DECL_INITIAL (array) = table;
1554 	TREE_STATIC (array) = 1;
1555 	DECL_ARTIFICIAL (array) = 1;
1556 	DECL_IGNORED_P (array) = 1;
1557 	rest_of_decl_compilation (array, 1, 0);
1558 
1559 	table = build1 (ADDR_EXPR, ptr_type_node, array);
1560       }
1561 
1562     PUSH_FIELD_VALUE (v, "throws", table);
1563   }
1564 
1565   FINISH_RECORD_CONSTRUCTOR (minit, v, method_type_node);
1566   return minit;
1567 }
1568 
1569 static tree
get_dispatch_vector(tree type)1570 get_dispatch_vector (tree type)
1571 {
1572   tree vtable = TYPE_VTABLE (type);
1573 
1574   if (vtable == NULL_TREE)
1575     {
1576       HOST_WIDE_INT i;
1577       tree method;
1578       tree super = CLASSTYPE_SUPER (type);
1579       HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1580       vtable = make_tree_vec (nvirtuals);
1581       TYPE_VTABLE (type) = vtable;
1582       if (super != NULL_TREE)
1583 	{
1584 	  tree super_vtable = get_dispatch_vector (super);
1585 
1586 	  for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1587 	    TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1588 	}
1589 
1590       for (method = TYPE_METHODS (type);  method != NULL_TREE;
1591 	   method = DECL_CHAIN (method))
1592 	{
1593 	  tree method_index = get_method_index (method);
1594 	  if (method_index != NULL_TREE
1595 	      && host_integerp (method_index, 0))
1596 	    TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1597 	}
1598     }
1599 
1600   return vtable;
1601 }
1602 
1603 static tree
get_dispatch_table(tree type,tree this_class_addr)1604 get_dispatch_table (tree type, tree this_class_addr)
1605 {
1606   int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1607   tree vtable = get_dispatch_vector (type);
1608   int i, j;
1609   int nvirtuals = TREE_VEC_LENGTH (vtable);
1610   int arraysize;
1611   tree gc_descr;
1612   vec<constructor_elt, va_gc> *v = NULL;
1613   constructor_elt *e;
1614   tree arraytype;
1615 
1616   arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1617   if (TARGET_VTABLE_USES_DESCRIPTORS)
1618     arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1619   arraysize += 2;
1620 
1621   vec_safe_grow_cleared (v, arraysize);
1622   e = &(*v)[arraysize - 1];
1623 
1624 #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
1625   for (i = nvirtuals;  --i >= 0; )
1626     {
1627       tree method = TREE_VEC_ELT (vtable, i);
1628       if (METHOD_ABSTRACT (method))
1629 	{
1630 	  if (! abstract_p)
1631 	    warning_at (DECL_SOURCE_LOCATION (method), 0,
1632 			"abstract method in non-abstract class");
1633 
1634 	  if (TARGET_VTABLE_USES_DESCRIPTORS)
1635 	    for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1636 	      CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1637 	  else
1638 	    CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1639 	}
1640       else
1641 	{
1642 	  if (TARGET_VTABLE_USES_DESCRIPTORS)
1643 	    for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1644 	      {
1645 		tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1646 				     method, build_int_cst (NULL_TREE, j));
1647 		TREE_CONSTANT (fdesc) = 1;
1648 		CONSTRUCTOR_PREPEND_VALUE (e, fdesc);
1649 	      }
1650 	  else
1651 	    CONSTRUCTOR_PREPEND_VALUE (e,
1652 				       build1 (ADDR_EXPR,
1653 					       nativecode_ptr_type_node,
1654 					       method));
1655 	}
1656     }
1657 
1658   /* Dummy entry for compatibility with G++ -fvtable-thunks.  When
1659      using the Boehm GC we sometimes stash a GC type descriptor
1660      there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1661      the emitted byte count during the output to the assembly file. */
1662   /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1663      fake "function descriptor".  It's first word is the is the class
1664      pointer, and subsequent words (usually one) contain the GC descriptor.
1665      In all other cases, we reserve two extra vtable slots. */
1666   gc_descr =  get_boehm_type_descriptor (type);
1667   CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
1668   for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1669     CONSTRUCTOR_PREPEND_VALUE (e, gc_descr);
1670   CONSTRUCTOR_PREPEND_VALUE (e, this_class_addr);
1671 
1672   /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1673   CONSTRUCTOR_PREPEND_VALUE (e, null_pointer_node);
1674   /** Offset to start of whole object.  Always (ptrdiff_t)0 for Java. */
1675   gcc_assert (e == v->address ());
1676   e->index = integer_zero_node;
1677   e->value = null_pointer_node;
1678 #undef CONSTRUCTOR_PREPEND_VALUE
1679 
1680   arraytype = build_prim_array_type (nativecode_ptr_type_node, arraysize);
1681   return build_constructor (arraytype, v);
1682 }
1683 
1684 
1685 /* Set the method_index for a method decl.  */
1686 void
set_method_index(tree decl,tree method_index)1687 set_method_index (tree decl, tree method_index)
1688 {
1689   if (method_index != NULL_TREE)
1690     {
1691       /* method_index is null if we're using indirect dispatch.  */
1692       method_index = fold (convert (sizetype, method_index));
1693 
1694       if (TARGET_VTABLE_USES_DESCRIPTORS)
1695 	/* Add one to skip bogus descriptor for class and GC descriptor. */
1696 	method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1697       else
1698 	/* Add 1 to skip "class" field of dtable, and 1 to skip GC
1699 	   descriptor.  */
1700 	method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1701     }
1702 
1703   DECL_VINDEX (decl) = method_index;
1704 }
1705 
1706 /* Get the method_index for a method decl.  */
1707 tree
get_method_index(tree decl)1708 get_method_index (tree decl)
1709 {
1710   tree method_index = DECL_VINDEX (decl);
1711 
1712   if (! method_index)
1713     return NULL;
1714 
1715   if (TARGET_VTABLE_USES_DESCRIPTORS)
1716     /* Sub one to skip bogus descriptor for class and GC descriptor. */
1717     method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1718   else
1719     /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor.  */
1720     method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1721 
1722   return method_index;
1723 }
1724 
1725 static int
supers_all_compiled(tree type)1726 supers_all_compiled (tree type)
1727 {
1728   while (type != NULL_TREE)
1729     {
1730       if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1731 	return 0;
1732       type = CLASSTYPE_SUPER (type);
1733     }
1734   return 1;
1735 }
1736 
1737 static void
add_table_and_syms(vec<constructor_elt,va_gc> ** v,vec<method_entry,va_gc> * methods,const char * table_name,tree table_slot,tree table_type,const char * syms_name,tree syms_slot)1738 add_table_and_syms (vec<constructor_elt, va_gc> **v,
1739                     vec<method_entry, va_gc> *methods,
1740                     const char *table_name, tree table_slot, tree table_type,
1741                     const char *syms_name, tree syms_slot)
1742 {
1743   if (methods == NULL)
1744     {
1745       PUSH_FIELD_VALUE (*v, table_name, null_pointer_node);
1746       PUSH_FIELD_VALUE (*v, syms_name, null_pointer_node);
1747     }
1748   else
1749     {
1750       pushdecl_top_level (syms_slot);
1751       PUSH_FIELD_VALUE (*v, table_name,
1752                         build1 (ADDR_EXPR, table_type, table_slot));
1753       PUSH_FIELD_VALUE (*v, syms_name,
1754                         build1 (ADDR_EXPR, symbols_array_ptr_type,
1755                                 syms_slot));
1756       TREE_CONSTANT (table_slot) = 1;
1757     }
1758 }
1759 
1760 void
make_class_data(tree type)1761 make_class_data (tree type)
1762 {
1763   tree decl, cons, temp;
1764   tree field, fields_decl;
1765   HOST_WIDE_INT static_field_count = 0;
1766   HOST_WIDE_INT instance_field_count = 0;
1767   HOST_WIDE_INT field_count;
1768   tree field_array_type;
1769   tree method;
1770   tree dtable_decl = NULL_TREE;
1771   HOST_WIDE_INT method_count = 0;
1772   tree method_array_type;
1773   tree methods_decl;
1774   tree super;
1775   tree this_class_addr;
1776   tree constant_pool_constructor;
1777   tree interfaces = null_pointer_node;
1778   int interface_len = 0;
1779   int uses_jv_markobj = 0;
1780   tree type_decl = TYPE_NAME (type);
1781   tree id_main = get_identifier("main");
1782   tree id_class = get_identifier("java.lang.Class");
1783   /** Offset from start of virtual function table declaration
1784       to where objects actually point at, following new g++ ABI. */
1785   tree dtable_start_offset = size_int (2 * POINTER_SIZE / BITS_PER_UNIT);
1786   vec<int> field_indexes;
1787   tree first_real_field;
1788   vec<constructor_elt, va_gc> *v1 = NULL, *v2 = NULL;
1789   tree reflection_data;
1790   vec<constructor_elt, va_gc> *static_fields = NULL;
1791   vec<constructor_elt, va_gc> *instance_fields = NULL;
1792   vec<constructor_elt, va_gc> *methods = NULL;
1793 
1794   this_class_addr = build_static_class_ref (type);
1795   decl = TREE_OPERAND (this_class_addr, 0);
1796 
1797   if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1798       && !flag_indirect_dispatch)
1799     {
1800       tree dtable = get_dispatch_table (type, this_class_addr);
1801       uses_jv_markobj = uses_jv_markobj_p (dtable);
1802       if (type == class_type_node && class_dtable_decl != NULL_TREE)
1803 	{
1804 	  /* We've already created some other class, and consequently
1805 	     we made class_dtable_decl.  Now we just want to fill it
1806 	     in.  */
1807 	  dtable_decl = class_dtable_decl;
1808 	}
1809       else
1810 	{
1811 	  dtable_decl = build_dtable_decl (type);
1812 	  TREE_STATIC (dtable_decl) = 1;
1813 	  DECL_ARTIFICIAL (dtable_decl) = 1;
1814 	  DECL_IGNORED_P (dtable_decl) = 1;
1815 	}
1816 
1817       TREE_PUBLIC (dtable_decl) = 1;
1818       DECL_INITIAL (dtable_decl) = dtable;
1819       /* The only dispatch table exported from a DSO is the dispatch
1820 	 table for java.lang.Class.  */
1821       if (DECL_NAME (type_decl) != id_class)
1822 	java_hide_decl (dtable_decl);
1823       if (! flag_indirect_classes)
1824 	rest_of_decl_compilation (dtable_decl, 1, 0);
1825       /* Maybe we're compiling Class as the first class.  If so, set
1826 	 class_dtable_decl to the decl we just made.  */
1827       if (type == class_type_node && class_dtable_decl == NULL_TREE)
1828 	class_dtable_decl = dtable_decl;
1829     }
1830 
1831   /* Build Field array. */
1832   field = TYPE_FIELDS (type);
1833   while (field && DECL_ARTIFICIAL (field))
1834     field = DECL_CHAIN (field);  /* Skip dummy fields.  */
1835   if (field && DECL_NAME (field) == NULL_TREE)
1836     field = DECL_CHAIN (field);  /* Skip dummy field for inherited data. */
1837   first_real_field = field;
1838 
1839   /* First count static and instance fields.  */
1840   for ( ; field != NULL_TREE; field = DECL_CHAIN (field))
1841     {
1842       if (! DECL_ARTIFICIAL (field))
1843 	{
1844 	  if (FIELD_STATIC (field))
1845 	    static_field_count++;
1846 	  else if (uses_jv_markobj || !flag_reduced_reflection)
1847 	    instance_field_count++;
1848 	}
1849     }
1850   field_count = static_field_count + instance_field_count;
1851   field_indexes.create (field_count);
1852 
1853   /* gcj sorts fields so that static fields come first, followed by
1854      instance fields.  Unfortunately, by the time this takes place we
1855      have already generated the reflection_data for this class, and
1856      that data contains indexes into the fields.  So, we generate a
1857      permutation that maps each original field index to its final
1858      position.  Then we pass this permutation to
1859      rewrite_reflection_indexes(), which fixes up the reflection
1860      data.  */
1861   {
1862     int i;
1863     int static_count = 0;
1864     int instance_count = static_field_count;
1865     int field_index;
1866 
1867     for (i = 0, field = first_real_field;
1868 	 field != NULL_TREE;
1869 	 field = DECL_CHAIN (field), i++)
1870     {
1871       if (! DECL_ARTIFICIAL (field))
1872 	{
1873 	  field_index = 0;
1874 	  if (FIELD_STATIC (field))
1875 	    field_index = static_count++;
1876 	  else if (uses_jv_markobj || !flag_reduced_reflection)
1877 	    field_index = instance_count++;
1878 	  else
1879 	    continue;
1880 	  field_indexes.quick_push (field_index);
1881 	}
1882     }
1883   }
1884 
1885   for (field = first_real_field; field != NULL_TREE;
1886        field = DECL_CHAIN (field))
1887     {
1888       if (! DECL_ARTIFICIAL (field))
1889 	{
1890 	  if (FIELD_STATIC (field))
1891 	    {
1892               /* We must always create reflection data for static fields
1893                  as it is used in the creation of the field itself. */
1894               tree init = make_field_value (field);
1895 	      tree initial = DECL_INITIAL (field);
1896               CONSTRUCTOR_APPEND_ELT (static_fields, NULL_TREE, init);
1897 	      /* If the initial value is a string constant,
1898 		 prevent output_constant from trying to assemble the value. */
1899 	      if (initial != NULL_TREE
1900 		  && TREE_TYPE (initial) == string_ptr_type_node)
1901 		DECL_INITIAL (field) = NULL_TREE;
1902 	      rest_of_decl_compilation (field, 1, 1);
1903 	      DECL_INITIAL (field) = initial;
1904 	    }
1905 	  else if (uses_jv_markobj || !flag_reduced_reflection)
1906 	    {
1907               tree init = make_field_value (field);
1908               CONSTRUCTOR_APPEND_ELT (instance_fields, NULL_TREE, init);
1909 	    }
1910 	}
1911     }
1912 
1913   gcc_assert (static_field_count == (int) vec_safe_length (static_fields));
1914   gcc_assert (instance_field_count == (int) vec_safe_length (instance_fields));
1915 
1916   if (field_count > 0)
1917     {
1918       vec_safe_splice (static_fields, instance_fields);
1919       field_array_type = build_prim_array_type (field_type_node, field_count);
1920       fields_decl = build_decl (input_location,
1921 				VAR_DECL, mangled_classname ("_FL_", type),
1922 				field_array_type);
1923       DECL_INITIAL (fields_decl)
1924         = build_constructor (field_array_type, static_fields);
1925       TREE_STATIC (fields_decl) = 1;
1926       DECL_ARTIFICIAL (fields_decl) = 1;
1927       DECL_IGNORED_P (fields_decl) = 1;
1928       rest_of_decl_compilation (fields_decl, 1, 0);
1929     }
1930   else
1931     fields_decl = NULL_TREE;
1932 
1933   /* Build Method array. */
1934   for (method = TYPE_METHODS (type);
1935        method != NULL_TREE; method = DECL_CHAIN (method))
1936     {
1937       tree init;
1938       if (METHOD_PRIVATE (method)
1939 	  && ! flag_keep_inline_functions
1940 	  && optimize)
1941 	continue;
1942       /* Even if we have a decl, we don't necessarily have the code.
1943 	 This can happen if we inherit a method from a superclass for
1944 	 which we don't have a .class file.  */
1945       if (METHOD_DUMMY (method))
1946 	continue;
1947 
1948       /* Generate method reflection data if:
1949 
1950           - !flag_reduced_reflection.
1951 
1952           - <clinit> -- The runtime uses reflection to initialize the
1953             class.
1954 
1955           - Any method in class java.lang.Class -- Class.forName() and
1956             perhaps other things require it.
1957 
1958           - class$ -- It does not work if reflection data missing.
1959 
1960           - main -- Reflection is used to find main(String[]) methods.
1961 
1962           - public not static -- It is potentially part of an
1963             interface.  The runtime uses reflection data to build
1964             interface dispatch tables.  */
1965       if (!flag_reduced_reflection
1966           || DECL_CLINIT_P (method)
1967           || DECL_NAME (type_decl) == id_class
1968           || DECL_NAME (method) == id_main
1969           || (METHOD_PUBLIC (method) && !METHOD_STATIC (method)))
1970         {
1971           init = make_method_value (method);
1972           method_count++;
1973           CONSTRUCTOR_APPEND_ELT (methods, NULL_TREE, init);
1974         }
1975     }
1976   method_array_type = build_prim_array_type (method_type_node, method_count);
1977   methods_decl = build_decl (input_location,
1978 			     VAR_DECL, mangled_classname ("_MT_", type),
1979 			     method_array_type);
1980   DECL_INITIAL (methods_decl) = build_constructor (method_array_type, methods);
1981   TREE_STATIC (methods_decl) = 1;
1982   DECL_ARTIFICIAL (methods_decl) = 1;
1983   DECL_IGNORED_P (methods_decl) = 1;
1984   rest_of_decl_compilation (methods_decl, 1, 0);
1985 
1986   if (class_dtable_decl == NULL_TREE)
1987     {
1988       class_dtable_decl = build_dtable_decl (class_type_node);
1989       TREE_STATIC (class_dtable_decl) = 1;
1990       DECL_ARTIFICIAL (class_dtable_decl) = 1;
1991       DECL_IGNORED_P (class_dtable_decl) = 1;
1992       if (is_compiled_class (class_type_node) != 2)
1993 	{
1994 	  DECL_EXTERNAL (class_dtable_decl) = 1;
1995 	  rest_of_decl_compilation (class_dtable_decl, 1, 0);
1996 	}
1997     }
1998 
1999   super = CLASSTYPE_SUPER (type);
2000   if (super == NULL_TREE)
2001     super = null_pointer_node;
2002   else if (! flag_indirect_dispatch
2003 	   && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
2004 	   && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
2005     super = build_class_ref (super);
2006   else
2007     {
2008       int super_index = alloc_class_constant (super);
2009       super = build_int_cst (ptr_type_node, super_index);
2010     }
2011 
2012   /* Build and emit the array of implemented interfaces. */
2013   if (type != object_type_node)
2014     interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
2015 
2016   if (interface_len > 0)
2017     {
2018       int i;
2019       tree interface_array_type, idecl;
2020       vec<constructor_elt, va_gc> *init;
2021       vec_alloc (init, interface_len);
2022       interface_array_type
2023 	= build_prim_array_type (class_ptr_type, interface_len);
2024       idecl = build_decl (input_location,
2025 			  VAR_DECL, mangled_classname ("_IF_", type),
2026 			  interface_array_type);
2027 
2028       for (i = 1; i <= interface_len; i++)
2029 	{
2030 	  tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
2031 	  tree iclass = BINFO_TYPE (child);
2032 	  tree index;
2033 	  if (! flag_indirect_dispatch
2034 	      && (assume_compiled
2035 		  (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
2036 	    index = build_class_ref (iclass);
2037 	  else
2038 	    {
2039 	      int int_index = alloc_class_constant (iclass);
2040 	      index = build_int_cst (ptr_type_node, int_index);
2041 	    }
2042 	  CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, index);
2043 	}
2044       DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
2045       TREE_STATIC (idecl) = 1;
2046       DECL_ARTIFICIAL (idecl) = 1;
2047       DECL_IGNORED_P (idecl) = 1;
2048       interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
2049       rest_of_decl_compilation (idecl, 1, 0);
2050     }
2051 
2052   constant_pool_constructor = build_constants_constructor ();
2053 
2054   if (flag_indirect_dispatch)
2055     {
2056       TYPE_OTABLE_DECL (type)
2057 	= emit_symbol_table
2058 	(DECL_NAME (TYPE_OTABLE_DECL (type)),
2059 	 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
2060 	 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
2061 
2062       TYPE_ATABLE_DECL (type)
2063 	= emit_symbol_table
2064 	(DECL_NAME (TYPE_ATABLE_DECL (type)),
2065 	 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
2066 	 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
2067 
2068       TYPE_ITABLE_DECL (type)
2069 	= emit_symbol_table
2070 	(DECL_NAME (TYPE_ITABLE_DECL (type)),
2071 	 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
2072 	 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
2073     }
2074 
2075   TYPE_CTABLE_DECL (type) = emit_catch_table (type);
2076 
2077   START_RECORD_CONSTRUCTOR (v1, object_type_node);
2078   PUSH_FIELD_VALUE (v1, "vtable",
2079 		    (flag_indirect_classes
2080 		     ? null_pointer_node
2081 		     : fold_build_pointer_plus
2082 			 (build1 (ADDR_EXPR, dtable_ptr_type,
2083 				  class_dtable_decl),
2084 			  dtable_start_offset)));
2085   if (! flag_hash_synchronization)
2086     PUSH_FIELD_VALUE (v1, "sync_info", null_pointer_node);
2087   FINISH_RECORD_CONSTRUCTOR (temp, v1, object_type_node);
2088   START_RECORD_CONSTRUCTOR (v2, class_type_node);
2089   PUSH_SUPER_VALUE (v2, temp);
2090   PUSH_FIELD_VALUE (v2, "next_or_version", gcj_abi_version);
2091   PUSH_FIELD_VALUE (v2, "name", build_utf8_ref (DECL_NAME (type_decl)));
2092   PUSH_FIELD_VALUE (v2, "accflags",
2093 		    build_int_cst (NULL_TREE,
2094 				   get_access_flags_from_decl (type_decl)));
2095 
2096   PUSH_FIELD_VALUE (v2, "superclass",
2097 		    CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
2098   PUSH_FIELD_VALUE (v2, "constants", constant_pool_constructor);
2099   PUSH_FIELD_VALUE (v2, "methods",
2100                     methods_decl == NULL_TREE ? null_pointer_node
2101 		    : build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
2102   PUSH_FIELD_VALUE (v2, "method_count",
2103 		    build_int_cst (NULL_TREE, method_count));
2104 
2105   PUSH_FIELD_VALUE (v2, "vtable_method_count",
2106                     (flag_indirect_dispatch
2107                      ? integer_minus_one_node
2108                      : TYPE_NVIRTUALS (type)));
2109 
2110   PUSH_FIELD_VALUE (v2, "fields",
2111 		    fields_decl == NULL_TREE ? null_pointer_node
2112 		    : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
2113   /* If we're using the binary compatibility ABI we don't know the
2114      size until load time.  */
2115   PUSH_FIELD_VALUE (v2, "size_in_bytes",
2116 		    (flag_indirect_dispatch
2117 		     ? integer_minus_one_node
2118 		     : size_in_bytes (type)));
2119   PUSH_FIELD_VALUE (v2, "field_count",
2120 		    build_int_cst (NULL_TREE, field_count));
2121   PUSH_FIELD_VALUE (v2, "static_field_count",
2122 		    build_int_cst (NULL_TREE, static_field_count));
2123 
2124   PUSH_FIELD_VALUE (v2, "vtable",
2125                     (flag_indirect_dispatch || dtable_decl == NULL_TREE
2126                      ? null_pointer_node
2127                      : fold_build_pointer_plus
2128 			 (build1 (ADDR_EXPR, dtable_ptr_type,
2129 				  dtable_decl),
2130 			  dtable_start_offset)));
2131   add_table_and_syms (&v2, TYPE_OTABLE_METHODS (type),
2132                       "otable", TYPE_OTABLE_DECL (type), otable_ptr_type,
2133                       "otable_syms", TYPE_OTABLE_SYMS_DECL (type));
2134   add_table_and_syms (&v2, TYPE_ATABLE_METHODS (type),
2135                       "atable", TYPE_ATABLE_DECL (type), atable_ptr_type,
2136                       "atable_syms", TYPE_ATABLE_SYMS_DECL (type));
2137   add_table_and_syms (&v2, TYPE_ITABLE_METHODS (type),
2138                       "itable", TYPE_ITABLE_DECL (type), itable_ptr_type,
2139                       "itable_syms", TYPE_ITABLE_SYMS_DECL (type));
2140 
2141   PUSH_FIELD_VALUE (v2, "catch_classes",
2142 		    build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
2143   PUSH_FIELD_VALUE (v2, "interfaces", interfaces);
2144   PUSH_FIELD_VALUE (v2, "loader", null_pointer_node);
2145   PUSH_FIELD_VALUE (v2, "interface_count",
2146 		    build_int_cst (NULL_TREE, interface_len));
2147   PUSH_FIELD_VALUE (v2, "state",
2148 		    convert (byte_type_node,
2149 			     build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
2150 
2151   PUSH_FIELD_VALUE (v2, "thread", null_pointer_node);
2152   PUSH_FIELD_VALUE (v2, "depth", integer_zero_node);
2153   PUSH_FIELD_VALUE (v2, "ancestors", null_pointer_node);
2154   PUSH_FIELD_VALUE (v2, "idt", null_pointer_node);
2155   PUSH_FIELD_VALUE (v2, "arrayclass", null_pointer_node);
2156   PUSH_FIELD_VALUE (v2, "protectionDomain", null_pointer_node);
2157 
2158   {
2159     tree assertion_table_ref;
2160     if (TYPE_ASSERTIONS (type) == NULL)
2161       assertion_table_ref = null_pointer_node;
2162     else
2163       assertion_table_ref = build1 (ADDR_EXPR,
2164 				    build_pointer_type (assertion_table_type),
2165 				    emit_assertion_table (type));
2166 
2167     PUSH_FIELD_VALUE (v2, "assertion_table", assertion_table_ref);
2168   }
2169 
2170   PUSH_FIELD_VALUE (v2, "hack_signers", null_pointer_node);
2171   PUSH_FIELD_VALUE (v2, "chain", null_pointer_node);
2172   PUSH_FIELD_VALUE (v2, "aux_info", null_pointer_node);
2173   PUSH_FIELD_VALUE (v2, "engine", null_pointer_node);
2174 
2175   if (TYPE_REFLECTION_DATA (current_class))
2176     {
2177       int i;
2178       int count = TYPE_REFLECTION_DATASIZE (current_class);
2179       vec<constructor_elt, va_gc> *v;
2180       vec_alloc (v, count);
2181       unsigned char *data = TYPE_REFLECTION_DATA (current_class);
2182       tree max_index = build_int_cst (sizetype, count);
2183       tree index = build_index_type (max_index);
2184       tree type = build_array_type (unsigned_byte_type_node, index);
2185       char buf[64];
2186       tree array;
2187       static int reflection_data_count;
2188 
2189       sprintf (buf, "_reflection_data_%d", reflection_data_count++);
2190       array = build_decl (input_location,
2191 			  VAR_DECL, get_identifier (buf), type);
2192 
2193       rewrite_reflection_indexes (&field_indexes);
2194 
2195       for (i = 0; i < count; i++)
2196 	{
2197 	  constructor_elt elt;
2198  	  elt.index = build_int_cst (sizetype, i);
2199 	  elt.value = build_int_cstu (byte_type_node, data[i]);
2200 	  v->quick_push (elt);
2201 	}
2202 
2203       DECL_INITIAL (array) = build_constructor (type, v);
2204       TREE_STATIC (array) = 1;
2205       DECL_ARTIFICIAL (array) = 1;
2206       DECL_IGNORED_P (array) = 1;
2207       TREE_READONLY (array) = 1;
2208       TREE_CONSTANT (DECL_INITIAL (array)) = 1;
2209       rest_of_decl_compilation (array, 1, 0);
2210 
2211       reflection_data = build_address_of (array);
2212 
2213       free (data);
2214       TYPE_REFLECTION_DATA (current_class) = NULL;
2215     }
2216   else
2217     reflection_data = null_pointer_node;
2218 
2219   PUSH_FIELD_VALUE (v2, "reflection_data", reflection_data);
2220   FINISH_RECORD_CONSTRUCTOR (cons, v2, class_type_node);
2221 
2222   DECL_INITIAL (decl) = cons;
2223 
2224   /* Hash synchronization requires at least 64-bit alignment. */
2225   if (flag_hash_synchronization && POINTER_SIZE < 64)
2226     DECL_ALIGN (decl) = 64;
2227 
2228   if (flag_indirect_classes)
2229     {
2230       TREE_READONLY (decl) = 1;
2231       TREE_CONSTANT (DECL_INITIAL (decl)) = 1;
2232     }
2233 
2234   rest_of_decl_compilation (decl, 1, 0);
2235 
2236   {
2237     tree classdollar_field = build_classdollar_field (type);
2238     if (!flag_indirect_classes)
2239       DECL_INITIAL (classdollar_field) = build_static_class_ref (type);
2240     rest_of_decl_compilation (classdollar_field, 1, 0);
2241   }
2242 
2243   TYPE_OTABLE_DECL (type) = NULL_TREE;
2244   TYPE_ATABLE_DECL (type) = NULL_TREE;
2245   TYPE_CTABLE_DECL (type) = NULL_TREE;
2246 }
2247 
2248 void
finish_class(void)2249 finish_class (void)
2250 {
2251   java_expand_catch_classes (current_class);
2252 
2253   current_function_decl = NULL_TREE;
2254   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
2255   make_class_data (current_class);
2256   register_class ();
2257   rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
2258 }
2259 
2260 /* Return 2 if KLASS is compiled by this compilation job;
2261    return 1 if KLASS can otherwise be assumed to be compiled;
2262    return 0 if we cannot assume that KLASS is compiled.
2263    Returns 1 for primitive and 0 for array types.  */
2264 int
is_compiled_class(tree klass)2265 is_compiled_class (tree klass)
2266 {
2267   int seen_in_zip;
2268   if (TREE_CODE (klass) == POINTER_TYPE)
2269     klass = TREE_TYPE (klass);
2270   if (TREE_CODE (klass) != RECORD_TYPE)  /* Primitive types are static. */
2271     return 1;
2272   if (TYPE_ARRAY_P (klass))
2273     return 0;
2274 
2275   seen_in_zip = (TYPE_JCF (klass) && JCF_SEEN_IN_ZIP (TYPE_JCF (klass)));
2276   if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2277     {
2278       /* The class was seen in the current ZIP file and will be
2279 	 available as a compiled class in the future but may not have
2280 	 been loaded already. Load it if necessary. This prevent
2281 	 build_class_ref () from crashing. */
2282 
2283       if (seen_in_zip && !CLASS_LOADED_P (klass) && (klass != current_class))
2284         load_class (klass, 1);
2285 
2286       /* We return 2 for class seen in ZIP and class from files
2287          belonging to the same compilation unit */
2288       return 2;
2289     }
2290 
2291   if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (klass)))))
2292     {
2293       if (!CLASS_LOADED_P (klass))
2294 	{
2295 	  if (klass != current_class)
2296 	    load_class (klass, 1);
2297 	}
2298       return 1;
2299     }
2300 
2301   return 0;
2302 }
2303 
2304 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
2305 
2306 tree
build_dtable_decl(tree type)2307 build_dtable_decl (tree type)
2308 {
2309   tree dtype, decl;
2310 
2311   /* We need to build a new dtable type so that its size is uniquely
2312      computed when we're dealing with the class for real and not just
2313      faking it (like java.lang.Class during the initialization of the
2314      compiler.) We know we're not faking a class when CURRENT_CLASS is
2315      TYPE. */
2316   if (current_class == type)
2317     {
2318       tree dummy = NULL_TREE;
2319       int n;
2320 
2321       dtype = make_node (RECORD_TYPE);
2322 
2323       PUSH_FIELD (input_location, dtype, dummy, "top_offset", ptr_type_node);
2324       PUSH_FIELD (input_location, dtype, dummy, "type_info", ptr_type_node);
2325 
2326       PUSH_FIELD (input_location, dtype, dummy, "class", class_ptr_type);
2327       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2328 	{
2329 	  tree tmp_field = build_decl (input_location,
2330 				       FIELD_DECL, NULL_TREE, ptr_type_node);
2331 	  TREE_CHAIN (dummy) = tmp_field;
2332 	  DECL_CONTEXT (tmp_field) = dtype;
2333 	  DECL_ARTIFICIAL (tmp_field) = 1;
2334 	  dummy = tmp_field;
2335 	}
2336 
2337       PUSH_FIELD (input_location, dtype, dummy, "gc_descr", ptr_type_node);
2338       for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2339 	{
2340 	  tree tmp_field = build_decl (input_location,
2341 				       FIELD_DECL, NULL_TREE, ptr_type_node);
2342 	  TREE_CHAIN (dummy) = tmp_field;
2343 	  DECL_CONTEXT (tmp_field) = dtype;
2344 	  DECL_ARTIFICIAL (tmp_field) = 1;
2345 	  dummy = tmp_field;
2346 	}
2347 
2348       n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2349       if (TARGET_VTABLE_USES_DESCRIPTORS)
2350 	n *= TARGET_VTABLE_USES_DESCRIPTORS;
2351 
2352       PUSH_FIELD (input_location, dtype, dummy, "methods",
2353 		  build_prim_array_type (nativecode_ptr_type_node, n));
2354       layout_type (dtype);
2355     }
2356   else
2357     dtype = dtable_type;
2358 
2359   decl = build_decl (input_location,
2360 		     VAR_DECL, get_identifier ("vt$"), dtype);
2361   DECL_CONTEXT (decl) = type;
2362   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2363   DECL_VTABLE_P (decl) = 1;
2364 
2365   return decl;
2366 }
2367 
2368 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2369    fields inherited from SUPER_CLASS. */
2370 
2371 void
push_super_field(tree this_class,tree super_class)2372 push_super_field (tree this_class, tree super_class)
2373 {
2374   tree base_decl;
2375   /* Don't insert the field if we're just re-laying the class out. */
2376   if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2377     return;
2378   base_decl = build_decl (input_location,
2379 			  FIELD_DECL, NULL_TREE, super_class);
2380   DECL_IGNORED_P (base_decl) = 1;
2381   DECL_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2382   TYPE_FIELDS (this_class) = base_decl;
2383   DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2384   DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2385 }
2386 
2387 /* Handle the different manners we may have to lay out a super class.  */
2388 
2389 static tree
maybe_layout_super_class(tree super_class,tree this_class ATTRIBUTE_UNUSED)2390 maybe_layout_super_class (tree super_class, tree this_class ATTRIBUTE_UNUSED)
2391 {
2392   if (!super_class)
2393     return NULL_TREE;
2394   else if (TREE_CODE (super_class) == RECORD_TYPE)
2395     {
2396       if (!CLASS_LOADED_P (super_class))
2397 	load_class (super_class, 1);
2398     }
2399   /* We might have to layout the class before its dependency on
2400      the super class gets resolved by java_complete_class  */
2401   else if (TREE_CODE (super_class) == POINTER_TYPE)
2402     {
2403       if (TREE_TYPE (super_class) != NULL_TREE)
2404 	super_class = TREE_TYPE (super_class);
2405       else
2406 	gcc_unreachable ();
2407     }
2408   if (!TYPE_SIZE (super_class))
2409     safe_layout_class (super_class);
2410 
2411   return super_class;
2412 }
2413 
2414 /* safe_layout_class just makes sure that we can load a class without
2415    disrupting the current_class, input_file, input_line, etc, information
2416    about the class processed currently.  */
2417 
2418 void
safe_layout_class(tree klass)2419 safe_layout_class (tree klass)
2420 {
2421   tree save_current_class = current_class;
2422   location_t save_location = input_location;
2423 
2424   layout_class (klass);
2425 
2426   current_class = save_current_class;
2427   input_location = save_location;
2428 }
2429 
2430 void
layout_class(tree this_class)2431 layout_class (tree this_class)
2432 {
2433   int i;
2434   tree super_class = CLASSTYPE_SUPER (this_class);
2435 
2436   class_list = tree_cons (this_class, NULL_TREE, class_list);
2437   if (CLASS_BEING_LAIDOUT (this_class))
2438     {
2439       char buffer [1024];
2440       char *report;
2441       tree current;
2442 
2443       sprintf (buffer, " with '%s'",
2444 	       IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2445       obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2446 
2447       for (current = TREE_CHAIN (class_list); current;
2448 	   current = TREE_CHAIN (current))
2449 	{
2450 	  tree decl = TYPE_NAME (TREE_PURPOSE (current));
2451 	  sprintf (buffer, "\n  which inherits from '%s' (%s:%d)",
2452 		   IDENTIFIER_POINTER (DECL_NAME (decl)),
2453 		   DECL_SOURCE_FILE (decl),
2454 		   DECL_SOURCE_LINE (decl));
2455 	  obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2456 	}
2457       obstack_1grow (&temporary_obstack, '\0');
2458       report = XOBFINISH (&temporary_obstack, char *);
2459       cyclic_inheritance_report = ggc_strdup (report);
2460       obstack_free (&temporary_obstack, report);
2461       TYPE_SIZE (this_class) = error_mark_node;
2462       return;
2463     }
2464   CLASS_BEING_LAIDOUT (this_class) = 1;
2465 
2466   if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2467     {
2468       tree maybe_super_class
2469 	= maybe_layout_super_class (super_class, this_class);
2470       if (maybe_super_class == NULL
2471 	  || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2472 	{
2473 	  TYPE_SIZE (this_class) = error_mark_node;
2474 	  CLASS_BEING_LAIDOUT (this_class) = 0;
2475 	  class_list = TREE_CHAIN (class_list);
2476 	  return;
2477 	}
2478       if (TYPE_SIZE (this_class) == NULL_TREE)
2479 	push_super_field (this_class, maybe_super_class);
2480     }
2481 
2482   layout_type (this_class);
2483 
2484   /* Also recursively load/layout any superinterfaces.  */
2485   if (TYPE_BINFO (this_class))
2486     {
2487       for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2488 	{
2489 	  tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2490 	  tree super_interface = BINFO_TYPE (binfo);
2491 	  tree maybe_super_interface
2492 	    = maybe_layout_super_class (super_interface, NULL_TREE);
2493 	  if (maybe_super_interface == NULL
2494 	      || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2495 	    {
2496 	      TYPE_SIZE (this_class) = error_mark_node;
2497 	      CLASS_BEING_LAIDOUT (this_class) = 0;
2498 	      class_list = TREE_CHAIN (class_list);
2499 	      return;
2500 	    }
2501 	}
2502     }
2503 
2504   /* Convert the size back to an SI integer value.  */
2505   TYPE_SIZE_UNIT (this_class) =
2506     fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2507 
2508   CLASS_BEING_LAIDOUT (this_class) = 0;
2509   class_list = TREE_CHAIN (class_list);
2510 }
2511 
2512 static void
add_miranda_methods(tree base_class,tree search_class)2513 add_miranda_methods (tree base_class, tree search_class)
2514 {
2515   int i;
2516   tree binfo, base_binfo;
2517 
2518   if (!CLASS_PARSED_P (search_class))
2519     load_class (search_class, 1);
2520 
2521   for (binfo = TYPE_BINFO (search_class), i = 1;
2522        BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2523     {
2524       tree method_decl;
2525       tree elt = BINFO_TYPE (base_binfo);
2526 
2527       /* FIXME: This is totally bogus.  We should not be handling
2528 	 Miranda methods at all if we're using the BC ABI.  */
2529       if (TYPE_DUMMY (elt))
2530 	continue;
2531 
2532       /* Ensure that interface methods are seen in declared order.  */
2533       if (!CLASS_LOADED_P (elt))
2534 	load_class (elt, 1);
2535       layout_class_methods (elt);
2536 
2537       /* All base classes will have been laid out at this point, so the order
2538          will be correct.  This code must match similar layout code in the
2539          runtime.  */
2540       for (method_decl = TYPE_METHODS (elt);
2541 	   method_decl; method_decl = DECL_CHAIN (method_decl))
2542 	{
2543 	  tree sig, override;
2544 
2545 	  /* An interface can have <clinit>.  */
2546 	  if (ID_CLINIT_P (DECL_NAME (method_decl)))
2547 	    continue;
2548 
2549 	  sig = build_java_argument_signature (TREE_TYPE (method_decl));
2550 	  override = lookup_argument_method (base_class,
2551 					     DECL_NAME (method_decl), sig);
2552 	  if (override == NULL_TREE)
2553 	    {
2554 	      /* Found a Miranda method.  Add it.  */
2555 	      tree new_method;
2556 	      sig = build_java_signature (TREE_TYPE (method_decl));
2557 	      new_method
2558 		= add_method (base_class,
2559 			      get_access_flags_from_decl (method_decl),
2560 			      DECL_NAME (method_decl), sig);
2561 	      METHOD_INVISIBLE (new_method) = 1;
2562 	    }
2563 	}
2564 
2565       /* Try superinterfaces.  */
2566       add_miranda_methods (base_class, elt);
2567     }
2568 }
2569 
2570 void
layout_class_methods(tree this_class)2571 layout_class_methods (tree this_class)
2572 {
2573   tree method_decl, dtable_count;
2574   tree super_class, type_name;
2575 
2576   if (TYPE_NVIRTUALS (this_class))
2577     return;
2578 
2579   super_class = CLASSTYPE_SUPER (this_class);
2580 
2581   if (super_class)
2582     {
2583       super_class = maybe_layout_super_class (super_class, this_class);
2584       if (!TYPE_NVIRTUALS (super_class))
2585 	layout_class_methods (super_class);
2586       dtable_count = TYPE_NVIRTUALS (super_class);
2587     }
2588   else
2589     dtable_count = integer_zero_node;
2590 
2591   type_name = TYPE_NAME (this_class);
2592   if (!flag_indirect_dispatch
2593       && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2594     {
2595       /* An abstract class can have methods which are declared only in
2596 	 an implemented interface.  These are called "Miranda
2597 	 methods".  We make a dummy method entry for such methods
2598 	 here.  */
2599       add_miranda_methods (this_class, this_class);
2600     }
2601 
2602   TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2603 
2604   for (method_decl = TYPE_METHODS (this_class);
2605        method_decl; method_decl = DECL_CHAIN (method_decl))
2606     dtable_count = layout_class_method (this_class, super_class,
2607 					method_decl, dtable_count);
2608 
2609   TYPE_NVIRTUALS (this_class) = dtable_count;
2610 }
2611 
2612 /* Return the index of METHOD in INTERFACE.  This index begins at 1
2613    and is used as an argument for _Jv_LookupInterfaceMethodIdx(). */
2614 int
get_interface_method_index(tree method,tree interface)2615 get_interface_method_index (tree method, tree interface)
2616 {
2617   tree meth;
2618   int i = 1;
2619 
2620   for (meth = TYPE_METHODS (interface); ; meth = DECL_CHAIN (meth))
2621     {
2622       if (meth == method)
2623 	return i;
2624       /* We don't want to put <clinit> into the interface table.  */
2625       if (! ID_CLINIT_P (DECL_NAME (meth)))
2626 	++i;
2627       gcc_assert (meth != NULL_TREE);
2628     }
2629 }
2630 
2631 /* Lay METHOD_DECL out, returning a possibly new value of
2632    DTABLE_COUNT. Also mangle the method's name. */
2633 
2634 tree
layout_class_method(tree this_class,tree super_class,tree method_decl,tree dtable_count)2635 layout_class_method (tree this_class, tree super_class,
2636 		     tree method_decl, tree dtable_count)
2637 {
2638   tree method_name = DECL_NAME (method_decl);
2639 
2640   TREE_PUBLIC (method_decl) = 1;
2641 
2642   if (flag_indirect_classes
2643       || (METHOD_PRIVATE (method_decl) && METHOD_STATIC (method_decl)
2644 	  && ! METHOD_NATIVE (method_decl)
2645 	  && ! special_method_p (method_decl)))
2646     java_hide_decl (method_decl);
2647 
2648   /* Considered external unless it is being compiled into this object
2649      file, or it was already flagged as external.  */
2650   if (!DECL_EXTERNAL (method_decl))
2651     DECL_EXTERNAL (method_decl) = ((is_compiled_class (this_class) != 2)
2652                                    || METHOD_NATIVE (method_decl));
2653 
2654   if (ID_INIT_P (method_name))
2655     {
2656       const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2657       const char *ptr;
2658       for (ptr = p; *ptr; )
2659 	{
2660 	  if (*ptr++ == '.')
2661 	    p = ptr;
2662 	}
2663       DECL_CONSTRUCTOR_P (method_decl) = 1;
2664       build_java_signature (TREE_TYPE (method_decl));
2665     }
2666   else if (! METHOD_STATIC (method_decl))
2667     {
2668       tree method_sig =
2669 	build_java_signature (TREE_TYPE (method_decl));
2670       bool method_override = false;
2671       tree super_method = lookup_java_method (super_class, method_name,
2672 						  method_sig);
2673       if (super_method != NULL_TREE
2674 	  && ! METHOD_DUMMY (super_method))
2675         {
2676 	  method_override = true;
2677 	  if (! METHOD_PUBLIC (super_method) &&
2678 	      ! METHOD_PROTECTED (super_method))
2679 	    {
2680 	      /* Don't override private method, or default-access method in
2681 		 another package.  */
2682 	      if (METHOD_PRIVATE (super_method) ||
2683 		  ! in_same_package (TYPE_NAME (this_class),
2684 				     TYPE_NAME (super_class)))
2685 		method_override = false;
2686 	   }
2687 	}
2688       if (method_override)
2689 	{
2690 	  tree method_index = get_method_index (super_method);
2691 	  set_method_index (method_decl, method_index);
2692 	  if (method_index == NULL_TREE
2693 	      && ! flag_indirect_dispatch
2694 	      && ! DECL_ARTIFICIAL (super_method))
2695 	    error ("non-static method %q+D overrides static method",
2696                    method_decl);
2697 	}
2698       else if (this_class == object_type_node
2699 	       && (METHOD_FINAL (method_decl)
2700 		   || METHOD_PRIVATE (method_decl)))
2701 	{
2702 	  /* We don't generate vtable entries for final Object
2703 	     methods.  This is simply to save space, since every
2704 	     object would otherwise have to define them.  */
2705 	}
2706       else if (! METHOD_PRIVATE (method_decl)
2707 	       && dtable_count)
2708 	{
2709 	  /* We generate vtable entries for final methods because they
2710 	     may one day be changed to non-final.  */
2711 	  set_method_index (method_decl, dtable_count);
2712 	  dtable_count = fold_build2 (PLUS_EXPR, integer_type_node,
2713 				      dtable_count, integer_one_node);
2714 	}
2715     }
2716 
2717   return dtable_count;
2718 }
2719 
2720 static void
register_class(void)2721 register_class (void)
2722 {
2723   tree node;
2724 
2725   if (!registered_class)
2726     vec_alloc (registered_class, 8);
2727 
2728   if (flag_indirect_classes)
2729     node = current_class;
2730   else
2731     node = TREE_OPERAND (build_class_ref (current_class), 0);
2732   vec_safe_push (registered_class, node);
2733 }
2734 
2735 /* Emit a function that calls _Jv_RegisterNewClasses with a list of
2736    all the classes we have emitted.  */
2737 
2738 static void
emit_indirect_register_classes(tree * list_p)2739 emit_indirect_register_classes (tree *list_p)
2740 {
2741   tree klass, t, register_class_fn;
2742   int i;
2743 
2744   int size = vec_safe_length (registered_class) * 2 + 1;
2745   vec<constructor_elt, va_gc> *init;
2746   vec_alloc (init, size);
2747   tree class_array_type
2748     = build_prim_array_type (ptr_type_node, size);
2749   tree cdecl = build_decl (input_location,
2750 			   VAR_DECL, get_identifier ("_Jv_CLS"),
2751 			   class_array_type);
2752   tree reg_class_list;
2753   FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
2754     {
2755       t = fold_convert (ptr_type_node, build_static_class_ref (klass));
2756       CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
2757       t = fold_convert (ptr_type_node,
2758                         build_address_of (build_classdollar_field (klass)));
2759       CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, t);
2760     }
2761   CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, integer_zero_node);
2762   DECL_INITIAL (cdecl) = build_constructor (class_array_type, init);
2763   TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
2764   TREE_STATIC (cdecl) = 1;
2765   DECL_ARTIFICIAL (cdecl) = 1;
2766   DECL_IGNORED_P (cdecl) = 1;
2767   TREE_READONLY (cdecl) = 1;
2768   TREE_CONSTANT (cdecl) = 1;
2769   rest_of_decl_compilation (cdecl, 1, 0);
2770   reg_class_list = fold_convert (ptr_type_node, build_address_of (cdecl));
2771 
2772   t = build_function_type_list (void_type_node,
2773 				build_pointer_type (ptr_type_node), NULL);
2774   t = build_decl (input_location,
2775 		  FUNCTION_DECL,
2776 		  get_identifier ("_Jv_RegisterNewClasses"), t);
2777   TREE_PUBLIC (t) = 1;
2778   DECL_EXTERNAL (t) = 1;
2779   register_class_fn = t;
2780   t = build_call_expr (register_class_fn, 1, reg_class_list);
2781   append_to_statement_list (t, list_p);
2782 }
2783 
2784 /* Emit a list of pointers to all classes we have emitted to JCR_SECTION.  */
2785 
2786 static void
emit_register_classes_in_jcr_section(void)2787 emit_register_classes_in_jcr_section (void)
2788 {
2789 #ifdef JCR_SECTION_NAME
2790   tree klass, cdecl, class_array_type;
2791   int i;
2792   int size = vec_safe_length (registered_class);
2793   vec<constructor_elt, va_gc> *init;
2794   vec_alloc (init, size);
2795 
2796   FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
2797     CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, build_fold_addr_expr (klass));
2798 
2799   /* ??? I would like to use tree_output_constant_def() but there is no way
2800 	 to put the data in a named section name, or to set the alignment,
2801 	 via that function.  So do everything manually here.  */
2802   class_array_type = build_prim_array_type (ptr_type_node, size);
2803   cdecl = build_decl (UNKNOWN_LOCATION,
2804 		      VAR_DECL, get_identifier ("_Jv_JCR_SECTION_data"),
2805 		      class_array_type);
2806   DECL_SECTION_NAME (cdecl) = build_string (strlen (JCR_SECTION_NAME),
2807 					    JCR_SECTION_NAME);
2808   DECL_ALIGN (cdecl) = POINTER_SIZE;
2809   DECL_USER_ALIGN (cdecl) = 1;
2810   DECL_INITIAL (cdecl) = build_constructor (class_array_type, init);
2811   TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
2812   TREE_STATIC (cdecl) = 1;
2813   TREE_READONLY (cdecl) = 0;
2814   TREE_CONSTANT (cdecl) = 1;
2815   DECL_ARTIFICIAL (cdecl) = 1;
2816   DECL_IGNORED_P (cdecl) = 1;
2817   pushdecl_top_level (cdecl);
2818   relayout_decl (cdecl);
2819   rest_of_decl_compilation (cdecl, 1, 0);
2820   mark_decl_referenced (cdecl);
2821 #else
2822   /* A target has defined TARGET_USE_JCR_SECTION,
2823      but doesn't have a JCR_SECTION_NAME.  */
2824   gcc_unreachable ();
2825 #endif
2826 }
2827 
2828 
2829 /* Emit a series of calls to _Jv_RegisterClass for every class we emitted.
2830    A series of calls is added to LIST_P.  */
2831 
2832 static void
emit_Jv_RegisterClass_calls(tree * list_p)2833 emit_Jv_RegisterClass_calls (tree *list_p)
2834 {
2835   tree klass, t, register_class_fn;
2836   int i;
2837 
2838   t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2839   t = build_decl (input_location,
2840 		  FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2841   TREE_PUBLIC (t) = 1;
2842   DECL_EXTERNAL (t) = 1;
2843   register_class_fn = t;
2844 
2845   FOR_EACH_VEC_SAFE_ELT (registered_class, i, klass)
2846     {
2847       t = build_fold_addr_expr (klass);
2848       t = build_call_expr (register_class_fn, 1, t);
2849       append_to_statement_list (t, list_p);
2850     }
2851 }
2852 
2853 /* Emit something to register classes at start-up time.
2854 
2855    The default mechanism is to generate instances at run-time.
2856 
2857    An alternative mechanism is through the .jcr section, which contain
2858    a list of pointers to classes which get registered during constructor
2859    invocation time.
2860 
2861    The fallback mechanism is to add statements to *LIST_P to call
2862    _Jv_RegisterClass for each class in this file.  These statements will
2863    be added to a static constructor function for this translation unit.  */
2864 
2865 void
emit_register_classes(tree * list_p)2866 emit_register_classes (tree *list_p)
2867 {
2868   if (registered_class == NULL)
2869     return;
2870 
2871   /* By default, generate instances of Class at runtime.  */
2872   if (flag_indirect_classes)
2873     emit_indirect_register_classes (list_p);
2874   /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2875      TARGET_ASM_NAMED_SECTION, else 0.  Some targets meet those conditions
2876      but lack suitable crtbegin/end objects or linker support.  These
2877      targets can override the default in tm.h to use the fallback mechanism.  */
2878   else if (TARGET_USE_JCR_SECTION)
2879     emit_register_classes_in_jcr_section ();
2880   /* Use the fallback mechanism.  */
2881   else
2882     emit_Jv_RegisterClass_calls (list_p);
2883 }
2884 
2885 /* Build a constructor for an entry in the symbol table.  */
2886 
2887 static tree
build_symbol_table_entry(tree clname,tree name,tree signature)2888 build_symbol_table_entry (tree clname, tree name, tree signature)
2889 {
2890   tree symbol;
2891   vec<constructor_elt, va_gc> *v = NULL;
2892 
2893   START_RECORD_CONSTRUCTOR (v, symbol_type);
2894   PUSH_FIELD_VALUE (v, "clname", clname);
2895   PUSH_FIELD_VALUE (v, "name", name);
2896   PUSH_FIELD_VALUE (v, "signature", signature);
2897   FINISH_RECORD_CONSTRUCTOR (symbol, v, symbol_type);
2898   TREE_CONSTANT (symbol) = 1;
2899 
2900   return symbol;
2901 }
2902 
2903 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2904 
2905 static tree
build_symbol_entry(tree decl,tree special)2906 build_symbol_entry (tree decl, tree special)
2907 {
2908   tree clname, name, signature;
2909   clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2910   /* ???  Constructors are given the name foo.foo all the way through
2911      the compiler, but in the method table they're all renamed
2912      foo.<init>.  So, we have to do the same here unless we want an
2913      unresolved reference at runtime.  */
2914   name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2915 			  && DECL_CONSTRUCTOR_P (decl))
2916 			 ? init_identifier_node
2917 			 : DECL_NAME (decl));
2918   signature = build_java_signature (TREE_TYPE (decl));
2919   signature = build_utf8_ref (unmangle_classname
2920 			      (IDENTIFIER_POINTER (signature),
2921 			       IDENTIFIER_LENGTH (signature)));
2922   /* SPECIAL is either NULL_TREE or integer_one_node.  We emit
2923      signature addr+1 if SPECIAL, and this indicates to the runtime
2924      system that this is a "special" symbol, i.e. one that should
2925      bypass access controls.  */
2926   if (special != NULL_TREE)
2927     signature = fold_build_pointer_plus (signature, special);
2928 
2929   return build_symbol_table_entry (clname, name, signature);
2930 }
2931 
2932 /* Emit a symbol table: used by -findirect-dispatch.  */
2933 
2934 tree
emit_symbol_table(tree name,tree the_table,vec<method_entry,va_gc> * decl_table,tree the_syms_decl,tree the_array_element_type,int element_size)2935 emit_symbol_table (tree name, tree the_table,
2936 		   vec<method_entry, va_gc> *decl_table,
2937                    tree the_syms_decl, tree the_array_element_type,
2938 		   int element_size)
2939 {
2940   tree table, null_symbol, table_size, the_array_type;
2941   unsigned index;
2942   method_entry *e;
2943   vec<constructor_elt, va_gc> *v = NULL;
2944 
2945   /* Only emit a table if this translation unit actually made any
2946      references via it. */
2947   if (!decl_table)
2948     return the_table;
2949 
2950   /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2951   FOR_EACH_VEC_ELT (*decl_table, index, e)
2952     CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
2953 			    build_symbol_entry (e->method, e->special));
2954 
2955   /* Terminate the list with a "null" entry. */
2956   null_symbol = build_symbol_table_entry (null_pointer_node,
2957                                           null_pointer_node,
2958                                           null_pointer_node);
2959   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_symbol);
2960 
2961   tree symbols_arr_type
2962     = build_prim_array_type (symbol_type, vec_safe_length (v));
2963 
2964   table = build_constructor (symbols_arr_type, v);
2965 
2966   /* Make it the initial value for otable_syms and emit the decl. */
2967   TREE_TYPE (the_syms_decl) = symbols_arr_type;
2968   relayout_decl (the_syms_decl);
2969   DECL_INITIAL (the_syms_decl) = table;
2970   DECL_ARTIFICIAL (the_syms_decl) = 1;
2971   DECL_IGNORED_P (the_syms_decl) = 1;
2972   rest_of_decl_compilation (the_syms_decl, 1, 0);
2973 
2974   /* Now that its size is known, redefine the table as an
2975      uninitialized static array of INDEX + 1 elements. The extra entry
2976      is used by the runtime to track whether the table has been
2977      initialized. */
2978   table_size
2979     = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2980   the_array_type = build_array_type (the_array_element_type, table_size);
2981   the_table = build_decl (input_location,
2982 			  VAR_DECL, name, the_array_type);
2983   TREE_STATIC (the_table) = 1;
2984   TREE_READONLY (the_table) = 1;
2985   rest_of_decl_compilation (the_table, 1, 0);
2986 
2987   return the_table;
2988 }
2989 
2990 /* Make an entry for the catch_classes list.  */
2991 tree
make_catch_class_record(tree catch_class,tree classname)2992 make_catch_class_record (tree catch_class, tree classname)
2993 {
2994   tree entry;
2995   tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2996   vec<constructor_elt, va_gc> *v = NULL;
2997   START_RECORD_CONSTRUCTOR (v, type);
2998   PUSH_FIELD_VALUE (v, "address", catch_class);
2999   PUSH_FIELD_VALUE (v, "classname", classname);
3000   FINISH_RECORD_CONSTRUCTOR (entry, v, type);
3001   return entry;
3002 }
3003 
3004 
3005 /* Generate the list of Throwable classes that are caught by exception
3006    handlers in this class.  */
3007 tree
emit_catch_table(tree this_class)3008 emit_catch_table (tree this_class)
3009 {
3010   tree table, table_size, array_type;
3011   int n_catch_classes;
3012   constructor_elt *e;
3013   /* Fill in the dummy entry that make_class created.  */
3014   e = &(*TYPE_CATCH_CLASSES (this_class))[0];
3015   e->value = make_catch_class_record (null_pointer_node, null_pointer_node);
3016   CONSTRUCTOR_APPEND_ELT (TYPE_CATCH_CLASSES (this_class), NULL_TREE,
3017 			  make_catch_class_record (null_pointer_node,
3018 						   null_pointer_node));
3019   n_catch_classes = TYPE_CATCH_CLASSES (this_class)->length ();
3020   table_size = build_index_type (build_int_cst (NULL_TREE, n_catch_classes));
3021   array_type
3022     = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
3023 			table_size);
3024   table =
3025     build_decl (input_location,
3026 		VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
3027   DECL_INITIAL (table) =
3028     build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
3029   TREE_STATIC (table) = 1;
3030   TREE_READONLY (table) = 1;
3031   DECL_IGNORED_P (table) = 1;
3032   rest_of_decl_compilation (table, 1, 0);
3033   return table;
3034 }
3035 
3036 /* Given a type, return the signature used by
3037    _Jv_FindClassFromSignature() in libgcj.  This isn't exactly the
3038    same as build_java_signature() because we want the canonical array
3039    type.  */
3040 
3041 static tree
build_signature_for_libgcj(tree type)3042 build_signature_for_libgcj (tree type)
3043 {
3044   tree sig, ref;
3045 
3046   sig = build_java_signature (type);
3047   ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
3048 					    IDENTIFIER_LENGTH (sig)));
3049   return ref;
3050 }
3051 
3052 /* Build an entry in the type assertion table.  */
3053 
3054 static tree
build_assertion_table_entry(tree code,tree op1,tree op2)3055 build_assertion_table_entry (tree code, tree op1, tree op2)
3056 {
3057   vec<constructor_elt, va_gc> *v = NULL;
3058   tree entry;
3059 
3060   START_RECORD_CONSTRUCTOR (v, assertion_entry_type);
3061   PUSH_FIELD_VALUE (v, "assertion_code", code);
3062   PUSH_FIELD_VALUE (v, "op1", op1);
3063   PUSH_FIELD_VALUE (v, "op2", op2);
3064   FINISH_RECORD_CONSTRUCTOR (entry, v, assertion_entry_type);
3065 
3066   return entry;
3067 }
3068 
3069 /* Add an entry to the type assertion table. Callback used during hashtable
3070    traversal.  */
3071 
3072 static int
add_assertion_table_entry(void ** htab_entry,void * ptr)3073 add_assertion_table_entry (void **htab_entry, void *ptr)
3074 {
3075   tree entry;
3076   tree code_val, op1_utf8, op2_utf8;
3077   vec<constructor_elt, va_gc> **v
3078       = ((vec<constructor_elt, va_gc> **) ptr);
3079   type_assertion *as = (type_assertion *) *htab_entry;
3080 
3081   code_val = build_int_cst (NULL_TREE, as->assertion_code);
3082 
3083   if (as->op1 == NULL_TREE)
3084     op1_utf8 = null_pointer_node;
3085   else
3086     op1_utf8 = build_signature_for_libgcj (as->op1);
3087 
3088   if (as->op2 == NULL_TREE)
3089     op2_utf8 = null_pointer_node;
3090   else
3091     op2_utf8 = build_signature_for_libgcj (as->op2);
3092 
3093   entry = build_assertion_table_entry (code_val, op1_utf8, op2_utf8);
3094 
3095   CONSTRUCTOR_APPEND_ELT (*v, NULL_TREE, entry);
3096   return true;
3097 }
3098 
3099 /* Generate the type assertion table for KLASS, and return its DECL.  */
3100 
3101 static tree
emit_assertion_table(tree klass)3102 emit_assertion_table (tree klass)
3103 {
3104   tree null_entry, ctor, table_decl;
3105   htab_t assertions_htab = TYPE_ASSERTIONS (klass);
3106   vec<constructor_elt, va_gc> *v = NULL;
3107 
3108   /* Iterate through the hash table.  */
3109   htab_traverse (assertions_htab, add_assertion_table_entry, &v);
3110 
3111   /* Finish with a null entry.  */
3112   null_entry = build_assertion_table_entry (integer_zero_node,
3113                                             null_pointer_node,
3114                                             null_pointer_node);
3115 
3116   CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, null_entry);
3117 
3118   tree type
3119     = build_prim_array_type (assertion_entry_type, vec_safe_length (v));
3120 
3121   ctor = build_constructor (type, v);
3122 
3123   table_decl = build_decl (input_location,
3124 			   VAR_DECL, mangled_classname ("_type_assert_", klass),
3125 			   type);
3126 
3127   TREE_STATIC (table_decl) = 1;
3128   TREE_READONLY (table_decl) = 1;
3129   TREE_CONSTANT (table_decl) = 1;
3130   DECL_IGNORED_P (table_decl) = 1;
3131 
3132   DECL_INITIAL (table_decl) = ctor;
3133   DECL_ARTIFICIAL (table_decl) = 1;
3134   rest_of_decl_compilation (table_decl, 1, 0);
3135 
3136   return table_decl;
3137 }
3138 
3139 void
init_class_processing(void)3140 init_class_processing (void)
3141 {
3142   fields_ident = get_identifier ("fields");
3143   info_ident = get_identifier ("info");
3144 
3145   gcc_obstack_init (&temporary_obstack);
3146 }
3147 
3148 static hashval_t java_treetreehash_hash (const void *);
3149 static int java_treetreehash_compare (const void *, const void *);
3150 
3151 /* A hash table mapping trees to trees.  Used generally.  */
3152 
3153 #define JAVA_TREEHASHHASH_H(t) ((hashval_t)TYPE_UID (t))
3154 
3155 static hashval_t
java_treetreehash_hash(const void * k_p)3156 java_treetreehash_hash (const void *k_p)
3157 {
3158   const struct treetreehash_entry *const k
3159     = (const struct treetreehash_entry *) k_p;
3160   return JAVA_TREEHASHHASH_H (k->key);
3161 }
3162 
3163 static int
java_treetreehash_compare(const void * k1_p,const void * k2_p)3164 java_treetreehash_compare (const void * k1_p, const void * k2_p)
3165 {
3166   const struct treetreehash_entry *const k1
3167     = (const struct treetreehash_entry *) k1_p;
3168   const_tree const k2 = (const_tree) k2_p;
3169   return (k1->key == k2);
3170 }
3171 
3172 tree
java_treetreehash_find(htab_t ht,tree t)3173 java_treetreehash_find (htab_t ht, tree t)
3174 {
3175   struct treetreehash_entry *e;
3176   hashval_t hv = JAVA_TREEHASHHASH_H (t);
3177   e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
3178   if (e == NULL)
3179     return NULL;
3180   else
3181     return e->value;
3182 }
3183 
3184 tree *
java_treetreehash_new(htab_t ht,tree t)3185 java_treetreehash_new (htab_t ht, tree t)
3186 {
3187   void **e;
3188   struct treetreehash_entry *tthe;
3189   hashval_t hv = JAVA_TREEHASHHASH_H (t);
3190 
3191   e = htab_find_slot_with_hash (ht, t, hv, INSERT);
3192   if (*e == NULL)
3193     {
3194       tthe = ggc_alloc_cleared_treetreehash_entry ();
3195       tthe->key = t;
3196       *e = tthe;
3197     }
3198   else
3199     tthe = (struct treetreehash_entry *) *e;
3200   return &tthe->value;
3201 }
3202 
3203 htab_t
java_treetreehash_create(size_t size)3204 java_treetreehash_create (size_t size)
3205 {
3206   return htab_create_ggc (size, java_treetreehash_hash,
3207 			  java_treetreehash_compare, NULL);
3208 }
3209 
3210 /* Break down qualified IDENTIFIER into package and class-name components.
3211    For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
3212    "pkg.foo", and RIGHT to "Bar". */
3213 
3214 int
split_qualified_name(tree * left,tree * right,tree source)3215 split_qualified_name (tree *left, tree *right, tree source)
3216 {
3217   char *p, *base;
3218   int l = IDENTIFIER_LENGTH (source);
3219 
3220   base = (char *) alloca (l + 1);
3221   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
3222 
3223   /* Breakdown NAME into REMAINDER . IDENTIFIER.  */
3224   p = base + l - 1;
3225   while (*p != '.' && p != base)
3226     p--;
3227 
3228   /* We didn't find a '.'. Return an error.  */
3229   if (p == base)
3230     return 1;
3231 
3232   *p = '\0';
3233   if (right)
3234     *right = get_identifier (p+1);
3235   *left = get_identifier (base);
3236 
3237   return 0;
3238 }
3239 
3240 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
3241    if the classes are from the same package. */
3242 
3243 int
in_same_package(tree name1,tree name2)3244 in_same_package (tree name1, tree name2)
3245 {
3246   tree tmp;
3247   tree pkg1;
3248   tree pkg2;
3249 
3250   if (TREE_CODE (name1) == TYPE_DECL)
3251     name1 = DECL_NAME (name1);
3252   if (TREE_CODE (name2) == TYPE_DECL)
3253     name2 = DECL_NAME (name2);
3254 
3255   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
3256     /* One in empty package. */
3257     return 0;
3258 
3259   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
3260     /* Both in empty package. */
3261     return 1;
3262 
3263   split_qualified_name (&pkg1, &tmp, name1);
3264   split_qualified_name (&pkg2, &tmp, name2);
3265 
3266   return (pkg1 == pkg2);
3267 }
3268 
3269 /* lang_hooks.decls.final_write_globals: perform final processing on
3270    global variables.  */
3271 
3272 void
java_write_globals(void)3273 java_write_globals (void)
3274 {
3275   tree *vec = vec_safe_address (pending_static_fields);
3276   int len = vec_safe_length (pending_static_fields);
3277   write_global_declarations ();
3278   emit_debug_global_declarations (vec, len);
3279   vec_free (pending_static_fields);
3280 }
3281 
3282 #include "gt-java-class.h"
3283