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