class.c 65.6 KB
Newer Older
Anthony Green committed
1
/* Functions related to building classes and their related objects.
2
   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3
   Free Software Foundation, Inc.
Anthony Green committed
4

5
This file is part of GCC.
Anthony Green committed
6

7
GCC is free software; you can redistribute it and/or modify
Anthony Green committed
8 9 10 11
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

12
GCC is distributed in the hope that it will be useful,
Anthony Green committed
13 14 15 16 17
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING.  If not, write to
Anthony Green committed
19 20 21 22 23 24 25 26 27 28
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */

/* Written by Per Bothner <bothner@cygnus.com> */

#include "config.h"
29
#include "system.h"
30 31
#include "coretypes.h"
#include "tm.h"
Anthony Green committed
32 33
#include "tree.h"
#include "rtl.h"
34
#include "flags.h"
Anthony Green committed
35 36 37
#include "java-tree.h"
#include "jcf.h"
#include "obstack.h"
38
#include "toplev.h"
Kaveh R. Ghazi committed
39 40
#include "output.h"
#include "parse.h"
41
#include "function.h"
42
#include "ggc.h"
43
#include "stdio.h"
44
#include "target.h"
Anthony Green committed
45

46 47 48 49 50
/* DOS brain-damage */
#ifndef O_BINARY
#define O_BINARY 0 /* MS-DOS brain-damage */
#endif

51 52 53 54 55 56
static tree make_method_value (tree);
static tree build_java_method_type (tree, tree, int);
static int32 hashUtf8String (const char *, int);
static tree make_field_value (tree);
static tree get_dispatch_vector (tree);
static tree get_dispatch_table (tree, tree);
57
static int supers_all_compiled (tree type);
58 59 60 61
static void add_interface_do (tree, tree, int);
static tree maybe_layout_super_class (tree, tree);
static int assume_compiled (const char *);
static tree build_method_symbols_entry (tree);
62

63
static GTY(()) rtx registerClass_libfunc;
Anthony Green committed
64

Mark Mitchell committed
65
struct obstack temporary_obstack;
Anthony Green committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79
/* The compiler generates different code depending on whether or not
   it can assume certain classes have been compiled down to native
   code or not.  The compiler options -fassume-compiled= and
   -fno-assume-compiled= are used to create a tree of
   assume_compiled_node objects.  This tree is queried to determine if
   a class is assume to be compiled or not.  Each node in the tree
   represents either a package or a specific class.  */

typedef struct assume_compiled_node_struct
{
  /* The class or package name.  */
  const char *ident;

80
  /* Nonzero if this represents an exclusion.  */
81 82 83 84 85 86 87 88
  int excludep;

  /* Pointers to other nodes in the tree.  */
  struct assume_compiled_node_struct *parent;
  struct assume_compiled_node_struct *sibling;
  struct assume_compiled_node_struct *child;
} assume_compiled_node;

89 90
static assume_compiled_node *find_assume_compiled_node (assume_compiled_node *,
							const char *);
91

92 93 94 95
/* This is the root of the include/exclude tree.  */

static assume_compiled_node *assume_compiled_tree;

96
static GTY(()) tree class_roots[5];
97 98 99 100
#define registered_class class_roots[0]
#define fields_ident class_roots[1]  /* get_identifier ("fields") */
#define info_ident class_roots[2]  /* get_identifier ("info") */
#define class_list class_roots[3]
101
#define class_dtable_decl class_roots[4]
102

103 104 105 106
/* Return the node that most closely represents the class whose name
   is IDENT.  Start the search from NODE.  Return NULL if an
   appropriate node does not exist.  */

107
static assume_compiled_node *
108
find_assume_compiled_node (assume_compiled_node *node, const char *ident)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
{
  while (node)
    {
      size_t node_ident_length = strlen (node->ident);

      /* node_ident_length is zero at the root of the tree.  If the
	 identifiers are the same length, then we have matching
	 classes.  Otherwise check if we've matched an enclosing
	 package name.  */

      if (node_ident_length == 0
	  || (strncmp (ident, node->ident, node_ident_length) == 0
	      && (strlen (ident) == node_ident_length
		  || ident[node_ident_length] == '.')))
	{
	  /* We've found a match, however, there might be a more
             specific match.  */

	  assume_compiled_node *found = find_assume_compiled_node (node->child,
								   ident);
	  if (found)
	    return found;
	  else
	    return node;
	}

      /* No match yet.  Continue through the sibling list.  */
      node = node->sibling;
    }

  /* No match at all in this tree.  */
  return NULL;
}

/* Add a new IDENT to the include/exclude tree.  It's an exclusion
144
   if EXCLUDEP is nonzero.  */
145 146

void
147
add_assume_compiled (const char *ident, int excludep)
148
{
149
  int len;
150 151
  assume_compiled_node *parent;
  assume_compiled_node *node = 
152
    xmalloc (sizeof (assume_compiled_node));
153

154
  node->ident = xstrdup (ident);
155 156 157 158 159 160 161
  node->excludep = excludep;
  node->child = NULL;

  /* Create the root of the tree if it doesn't exist yet.  */

  if (NULL == assume_compiled_tree)
    {
162
      assume_compiled_tree = xmalloc (sizeof (assume_compiled_node));
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      assume_compiled_tree->ident = "";
      assume_compiled_tree->excludep = 0;
      assume_compiled_tree->sibling = NULL;
      assume_compiled_tree->child = NULL;
      assume_compiled_tree->parent = NULL;
    }

  /* Calling the function with the empty string means we're setting
     excludep for the root of the hierarchy.  */

  if (0 == ident[0])
    {
      assume_compiled_tree->excludep = excludep;
      return;
    }

  /* Find the parent node for this new node.  PARENT will either be a
     class or a package name.  Adjust PARENT accordingly.  */

  parent = find_assume_compiled_node (assume_compiled_tree, ident);
183 184
  len = strlen (parent->ident);
  if (parent->ident[len] && parent->ident[len] != '.')
185 186 187 188 189 190 191 192 193
    parent = parent->parent;

  /* Insert NODE into the tree.  */

  node->parent = parent;
  node->sibling = parent->child;
  parent->child = node;
}

194
/* Returns nonzero if IDENT is the name of a class that the compiler
195
   should assume has been compiled to object code.  */
196

197
static int
198
assume_compiled (const char *ident)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
{
  assume_compiled_node *i;
  int result;
  
  if (NULL == assume_compiled_tree)
    return 1;

  i = find_assume_compiled_node (assume_compiled_tree,
				 ident);

  result = ! i->excludep;
  
  return (result);
}

Anthony Green committed
214 215 216 217 218
/* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
   except that characters matching OLD_CHAR are substituted by NEW_CHAR.
   Also, PREFIX is prepended, and SUFFIX is appended. */

tree
219 220 221 222 223 224
ident_subst (const char* old_name,
	     int old_length,
	     const char *prefix,
	     int old_char,
	     int new_char,
	     const char *suffix)
Anthony Green committed
225 226 227 228 229 230 231
{
  int prefix_len = strlen (prefix);
  int suffix_len = strlen (suffix);
  int i = prefix_len + old_length + suffix_len + 1;
#ifdef __GNUC__
  char buffer[i];
#else
232
  char *buffer = alloca (i);
Anthony Green committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#endif
  strcpy (buffer, prefix);
  for (i = 0; i < old_length; i++)
    {
      char ch = old_name[i];
      if (ch == old_char)
	ch = new_char;
      buffer[prefix_len + i] = ch;
    }
  strcpy (buffer + prefix_len + old_length, suffix);
  return get_identifier (buffer);
}

/* Return an IDENTIFIER_NODE the same as OLD_ID,
   except that characters matching OLD_CHAR are substituted by NEW_CHAR.
   Also, PREFIX is prepended, and SUFFIX is appended. */

tree
251 252 253 254 255
identifier_subst (const tree old_id,
		  const char *prefix,
		  int old_char,
		  int new_char,
		  const char *suffix)
Anthony Green committed
256 257 258 259 260 261 262 263 264
{
  return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
		      prefix, old_char, new_char, suffix);
}

/* Generate a valid C identifier from the name of the class TYPE,
   prefixed by PREFIX. */

tree
265
mangled_classname (const char *prefix, tree type)
Anthony Green committed
266 267 268 269
{
  tree ident = TYPE_NAME (type);
  if (TREE_CODE (ident) != IDENTIFIER_NODE)
    ident = DECL_NAME (ident);
270
  return identifier_subst (ident, prefix, '.', '_', "");
Anthony Green committed
271 272 273
}

tree
274
make_class (void)
Anthony Green committed
275 276 277 278
{
  tree type;
  type = make_node (RECORD_TYPE);
  TYPE_BINFO (type) = make_tree_vec (6);
279
  MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
Anthony Green committed
280 281 282 283 284 285 286 287 288

  return type;
}

/* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
   and where each of the constituents is separated by '/',
   return a corresponding IDENTIFIER_NODE, except using '.' as separator. */

tree
289
unmangle_classname (const char *name, int name_length)
Anthony Green committed
290
{
291
  tree to_return = ident_subst (name, name_length, "", '/', '.', "");
292 293 294 295 296 297 298 299 300 301 302 303
  /* It's not sufficient to compare to_return and get_identifier
     (name) to determine whether to_return is qualified. There are
     cases in signature analysis where name will be stripped of a
     trailing ';'. */
  name = IDENTIFIER_POINTER (to_return);
  while (*name)
    if (*name++ == '.') 
      {
	QUALIFIED_P (to_return) = 1;
	break;
      }
  
304
  return to_return;
Anthony Green committed
305 306 307
}

tree
308
push_class (tree class_type, tree class_name)
Anthony Green committed
309 310
{
  tree decl, signature;
Zack Weinberg committed
311
  const char *save_input_filename = input_filename;
Anthony Green committed
312 313
  int save_lineno = lineno;
  tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
314
  CLASS_P (class_type) = 1;
Anthony Green committed
315 316 317
  input_filename = IDENTIFIER_POINTER (source_name);
  lineno = 0;
  decl = build_decl (TYPE_DECL, class_name, class_type);
318 319 320 321

  /* dbxout needs a DECL_SIZE if in gstabs mode */
  DECL_SIZE (decl) = integer_zero_node;

Anthony Green committed
322 323 324
  input_filename = save_input_filename;
  lineno = save_lineno;
  signature = identifier_subst (class_name, "L", '.', '/', ";");
Per Bothner committed
325
  IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
Anthony Green committed
326

327
  /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
Anthony Green committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
     both a typedef and in the struct name-space.  We may want to re-visit
     this later, but for now it reduces the changes needed for gdb. */
  DECL_ARTIFICIAL (decl) = 1;

  pushdecl_top_level (decl);

  return decl;
}

/* Finds the (global) class named NAME.  Creates the class if not found.
   Also creates associated TYPE_DECL.
   Does not check if the class actually exists, load the class,
   fill in field or methods, or do layout_type. */

tree
343
lookup_class (tree name)
Anthony Green committed
344 345 346 347 348 349 350 351
{
  tree decl = IDENTIFIER_CLASS_VALUE (name);
  if (decl == NULL_TREE)
    decl = push_class (make_class (), name);
  return TREE_TYPE (decl);
}

void
352 353
set_super_info (int access_flags, tree this_class,
		tree super_class, int interfaces_count)
Anthony Green committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
  int total_supers = interfaces_count;
  tree class_decl = TYPE_NAME (this_class);
  if (super_class)
    total_supers++;

  TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
  if (super_class)
    {
      tree super_binfo = make_tree_vec (6);
      BINFO_TYPE (super_binfo) = super_class;
      BINFO_OFFSET (super_binfo) = integer_zero_node;
      TREE_VIA_PUBLIC (super_binfo) = 1;
      TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
	= super_binfo;
      CLASS_HAS_SUPER (this_class) = 1;
    }
371

372 373 374 375
  set_class_decl_access_flags (access_flags, class_decl);
}

void
376
set_class_decl_access_flags (int access_flags, tree class_decl)
377
{
Anthony Green committed
378 379 380 381 382
  if (access_flags & ACC_PUBLIC)    CLASS_PUBLIC (class_decl) = 1;
  if (access_flags & ACC_FINAL)     CLASS_FINAL (class_decl) = 1;
  if (access_flags & ACC_SUPER)     CLASS_SUPER (class_decl) = 1;
  if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
  if (access_flags & ACC_ABSTRACT)  CLASS_ABSTRACT (class_decl) = 1;
383
  if (access_flags & ACC_STATIC)    CLASS_STATIC (class_decl) = 1;
384
  if (access_flags & ACC_PRIVATE)   CLASS_PRIVATE (class_decl) = 1;
385
  if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
386
  if (access_flags & ACC_STRICT)    CLASS_STRICTFP (class_decl) = 1;
Anthony Green committed
387 388 389 390 391 392
}

/* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
   direct sub-classes of Object are 1, and so on. */

int
393
class_depth (tree clas)
Anthony Green committed
394 395 396 397
{
  int depth = 0;
  if (! CLASS_LOADED_P (clas))
    load_class (clas, 1);
398 399
  if (TYPE_SIZE (clas) == error_mark_node)
    return -1;
Anthony Green committed
400 401 402 403 404 405 406 407 408 409 410
  while (clas != object_type_node)
    {
      depth++;
      clas = TYPE_BINFO_BASETYPE (clas, 0);
    }
  return depth;
}

/* Return true iff TYPE2 is an interface that extends interface TYPE1 */

int
411
interface_of_p (tree type1, tree type2)
Anthony Green committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
{
  int n, i;
  tree basetype_vec;

  if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
    return 0;
  n = TREE_VEC_LENGTH (basetype_vec);
  for (i = 0; i < n; i++)
    {
      tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
      if (vec_elt && BINFO_TYPE (vec_elt) == type1)
	return 1;
    }
  for (i = 0; i < n; i++)
    {
      tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
      if (vec_elt && BINFO_TYPE (vec_elt) 
	  && interface_of_p (type1, BINFO_TYPE (vec_elt)))
	return 1;
    }
  return 0;
}

/* Return true iff TYPE1 inherits from TYPE2. */

int
438
inherits_from_p (tree type1, tree type2)
Anthony Green committed
439 440 441 442 443 444 445 446 447 448
{
  while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
    {
      if (type1 == type2)
	return 1;
      type1 = CLASSTYPE_SUPER (type1);
    }
  return 0;
}

449 450 451
/* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */

int
452
enclosing_context_p (tree type1, tree type2)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
{
  if (!INNER_CLASS_TYPE_P (type2))
    return 0;

  for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
       type2; 
       type2 = (INNER_CLASS_TYPE_P (type2) ?
		TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
    {
      if (type2 == type1)
	return 1;
    }

  return 0;
}

469 470 471
/* Return 1 iff there exists a common enclosing context between TYPE1
   and TYPE2.  */

472
int common_enclosing_context_p (tree type1, tree type2)
473
{
474
  if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    return 0;
  
  for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1; 
       type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
		TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
    {
      tree current;
      for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
	   current = (PURE_INNER_CLASS_TYPE_P (current) ?
		      TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) : 
		      NULL_TREE))
	if (type1 == current)
	  return 1;
    }
  return 0;
}

Anthony Green committed
492
static void
493
add_interface_do (tree basetype_vec, tree interface_class, int i)
Anthony Green committed
494 495 496 497
{
  tree interface_binfo = make_tree_vec (6);
  BINFO_TYPE (interface_binfo) = interface_class;
  BINFO_OFFSET (interface_binfo) = integer_zero_node;
498
  BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
Anthony Green committed
499 500 501 502 503 504 505 506 507 508
  TREE_VIA_VIRTUAL (interface_binfo) = 1;
  TREE_VIA_PUBLIC (interface_binfo) = 1;
  TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
}

/* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
   found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
   if attempt is made to add it twice. */

tree
509
maybe_add_interface (tree this_class, tree interface_class)
Anthony Green committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
{
  tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
  int i;
  int n = TREE_VEC_LENGTH (basetype_vec);
  for (i = 0; ; i++)
    {
      if (i >= n)
	{
	  error ("internal error - too many interface type");
	  return NULL_TREE;
	}
      else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
	break;
      else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
	return interface_class;
    } 
  add_interface_do (basetype_vec, interface_class, i);
  return NULL_TREE;
}

/* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */

void
533
add_interface (tree this_class, tree interface_class)
Anthony Green committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
{
  tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
  int i;
  int n = TREE_VEC_LENGTH (basetype_vec);
  for (i = 0; ; i++)
    {
      if (i >= n)
	{
	  error ("internal error - too many interface type");
	  return;
	}
      else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
	break;
    }
  add_interface_do (basetype_vec, interface_class, i);
}

Kaveh R. Ghazi committed
551
#if 0
Anthony Green committed
552 553 554 555
/* Return the address of a pointer to the first FUNCTION_DECL
   in the list (*LIST) whose DECL_NAME is NAME. */

static tree *
556
find_named_method (tree *list, tree name)
Anthony Green committed
557 558 559 560 561
{
  while (*list && DECL_NAME (*list) != name)
    list = &TREE_CHAIN (*list);
  return list;
}
Kaveh R. Ghazi committed
562
#endif
Anthony Green committed
563

564
static tree
565
build_java_method_type (tree fntype, tree this_class, int access_flags)
Anthony Green committed
566 567 568
{
  if (access_flags & ACC_STATIC)
    return fntype;
569
  return build_method_type (this_class, fntype);
Anthony Green committed
570 571 572
}

tree
573
add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
Anthony Green committed
574 575 576 577
{
  tree method_type, fndecl;

  method_type = build_java_method_type (function_type,
578
					this_class, access_flags);
Anthony Green committed
579 580

  fndecl = build_decl (FUNCTION_DECL, name, method_type);
581
  DECL_CONTEXT (fndecl) = this_class;
Anthony Green committed
582 583

  DECL_LANG_SPECIFIC (fndecl)
584
    = ggc_alloc_cleared (sizeof (struct lang_decl));
585
  DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
Anthony Green committed
586

587
  /* Initialize the static initializer test table.  */
588 589 590
  
  DECL_FUNCTION_INIT_TEST_TABLE (fndecl) = 
    java_treetreehash_create (10, 1);
591

592 593
  /* Initialize the initialized (static) class table. */
  if (access_flags & ACC_STATIC)
594 595
    DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
      htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
596

597 598
  /* Initialize the static method invocation compound list */
  DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
599

600 601
  TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
  TYPE_METHODS (this_class) = fndecl;
Anthony Green committed
602

603 604 605 606 607
  /* Notice that this is a finalizer and update the class type
     accordingly. This is used to optimize instance allocation. */
  if (name == finalize_identifier_node
      && TREE_TYPE (function_type) == void_type_node
      && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
608
    HAS_FINALIZER_P (this_class) = 1;
609

Anthony Green committed
610 611
  if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
  if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
612 613
  if (access_flags & ACC_PRIVATE)
    METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
614 615 616 617 618
  if (access_flags & ACC_NATIVE)
    {
      METHOD_NATIVE (fndecl) = 1;
      DECL_EXTERNAL (fndecl) = 1;
    }
619 620 621 622
  if (access_flags & ACC_STATIC) 
    METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
  if (access_flags & ACC_FINAL) 
    METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
Anthony Green committed
623 624 625
  if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
  if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
  if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
626
  if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
Anthony Green committed
627 628 629 630 631 632 633 634
  return fndecl;
}

/* Add a method to THIS_CLASS.
   The method's name is NAME.
   Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */

tree
635
add_method (tree this_class, int access_flags, tree name, tree method_sig)
Anthony Green committed
636
{
637
  tree function_type, fndecl;
638 639 640
  const unsigned char *sig
    = (const unsigned char *) IDENTIFIER_POINTER (method_sig);

Anthony Green committed
641
  if (sig[0] != '(')
642 643
    fatal_error ("bad method signature");

Anthony Green committed
644
  function_type = get_type_from_signature (method_sig);
645
  fndecl = add_method_1 (this_class, access_flags, name, function_type);
Anthony Green committed
646 647 648 649 650
  set_java_signature (TREE_TYPE (fndecl), method_sig);
  return fndecl;
}

tree
651
add_field (tree class, tree name, tree field_type, int flags)
Anthony Green committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
{
  int is_static = (flags & ACC_STATIC) != 0;
  tree field;
  field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
  TREE_CHAIN (field) = TYPE_FIELDS (class);
  TYPE_FIELDS (class) = field;
  DECL_CONTEXT (field) = class;

  if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
  if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
  if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
  if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
  if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
  if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
  if (is_static)
    {
      FIELD_STATIC (field) = 1;
Per Bothner committed
669 670 671
      /* Always make field externally visible.  This is required so
	 that native methods can always access the field.  */
      TREE_PUBLIC (field) = 1;
672 673 674
      /* Considered external until we know what classes are being
	 compiled into this object file.  */
      DECL_EXTERNAL (field) = 1;
Anthony Green committed
675
    }
676

Anthony Green committed
677 678 679 680 681 682
  return field;
}

/* Associate a constant value CONSTANT with VAR_DECL FIELD. */

void
683
set_constant_value (tree field, tree constant)
Anthony Green committed
684 685 686 687
{
  if (field == NULL_TREE)
    warning ("misplaced ConstantValue attribute (not in any field)");
  else if (DECL_INITIAL (field) != NULL_TREE)
688
    warning ("duplicate ConstantValue attribute for field '%s'",
Anthony Green committed
689 690
	     IDENTIFIER_POINTER (DECL_NAME (field)));
  else
691 692
    {
      DECL_INITIAL (field) = constant;
693 694 695
      if (TREE_TYPE (constant) != TREE_TYPE (field)
	  && ! (TREE_TYPE (constant) == int_type_node
		&& INTEGRAL_TYPE_P (TREE_TYPE (field))
696 697 698 699
		&& TYPE_PRECISION (TREE_TYPE (field)) <= 32)
	  && ! (TREE_TYPE (constant) == utf8const_ptr_type
		&& TREE_TYPE (field) == string_ptr_type_node))
	error ("ConstantValue attribute of field '%s' has wrong type",
700
	       IDENTIFIER_POINTER (DECL_NAME (field)));
701 702 703
      if (FIELD_FINAL (field))
	DECL_FIELD_FINAL_IUD (field) = 1;
    }
Anthony Green committed
704 705 706 707
}

/* Count the number of Unicode chars encoded in a given Ut8 string. */

708
#if 0
Anthony Green committed
709
int
710
strLengthUtf8 (char *str, int len)
Anthony Green committed
711 712 713 714 715 716 717 718 719 720
{
  register unsigned char* ptr = (unsigned char*) str;
  register unsigned char *limit = ptr + len;
  int str_length = 0;
  for (; ptr < limit; str_length++) {
    if (UTF8_GET (ptr, limit) < 0)
      return -1;
  }
  return str_length;
}
721
#endif
Anthony Green committed
722 723 724 725 726 727


/* Calculate a hash value for a string encoded in Utf8 format.
 * This returns the same hash value as specified for java.lang.String.hashCode.
 */

728
static int32
729
hashUtf8String (const char *str, int len)
Anthony Green committed
730
{
731 732
  register const unsigned char* ptr = (const unsigned char*) str;
  register const unsigned char *limit = ptr + len;
Anthony Green committed
733 734 735 736 737 738 739 740 741 742 743
  int32 hash = 0;
  for (; ptr < limit;)
    {
      int ch = UTF8_GET (ptr, limit);
      /* Updated specification from
	 http://www.javasoft.com/docs/books/jls/clarify.html. */
      hash = (31 * hash) + ch;
    }
  return hash;
}

744
static GTY(()) tree utf8_decl_list = NULL_TREE;
Anthony Green committed
745 746

tree
747
build_utf8_ref (tree name)
Anthony Green committed
748
{
749
  const char * name_ptr = IDENTIFIER_POINTER(name);
Anthony Green committed
750 751
  int name_len = IDENTIFIER_LENGTH(name);
  char buf[60];
Kaveh R. Ghazi committed
752
  tree ctype, field = NULL_TREE, str_type, cinit, string;
Anthony Green committed
753
  static int utf8_count = 0;
754
  int name_hash;
Anthony Green committed
755 756 757 758 759 760 761 762 763 764 765 766 767
  tree ref = IDENTIFIER_UTF8_REF (name);
  tree decl;
  if (ref != NULL_TREE)
    return ref;

  ctype = make_node (RECORD_TYPE);
  str_type = build_prim_array_type (unsigned_byte_type_node,
				    name_len + 1); /* Allow for final '\0'. */
  PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
  PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
  PUSH_FIELD (ctype, field, "data", str_type);
  FINISH_RECORD (ctype);
  START_RECORD_CONSTRUCTOR (cinit, ctype);
768 769 770 771
  name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
  PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
  PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
  string = build_string (name_len, name_ptr);
Anthony Green committed
772 773 774
  TREE_TYPE (string) = str_type;
  PUSH_FIELD_VALUE (cinit, "data", string);
  FINISH_RECORD_CONSTRUCTOR (cinit);
Anthony Green committed
775
  TREE_CONSTANT (cinit) = 1;
Anthony Green committed
776

777
  /* Generate a unique-enough identifier.  */
Anthony Green committed
778 779 780 781 782 783 784
  sprintf(buf, "_Utf%d", ++utf8_count);

  decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
  TREE_STATIC (decl) = 1;
  DECL_ARTIFICIAL (decl) = 1;
  DECL_IGNORED_P (decl) = 1;
  TREE_READONLY (decl) = 1;
Anthony Green committed
785
  TREE_THIS_VOLATILE (decl) = 0;
Anthony Green committed
786
  DECL_INITIAL (decl) = cinit;
787
#ifdef HAVE_GAS_SHF_MERGE
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
  {
    int decl_size;
    /* Ensure decl_size is a multiple of utf8const_type's alignment. */
    decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
	         & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
    if (flag_merge_constants && decl_size < 256)
      {
        char buf[32];
        int flags = (SECTION_OVERRIDE
        	   | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
        sprintf (buf, ".rodata.jutf8.%d", decl_size);
        named_section_flags (buf, flags);
        DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
      }
  }
803
#endif
Anthony Green committed
804 805 806 807 808
  TREE_CHAIN (decl) = utf8_decl_list;
  layout_decl (decl, 0);
  pushdecl (decl);
  rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
  utf8_decl_list = decl;
809
  make_decl_rtl (decl, (char*) 0);
Anthony Green committed
810 811 812 813 814 815 816 817 818
  ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
  IDENTIFIER_UTF8_REF (name) = ref;
  return ref;
}

/* Build a reference to the class TYPE.
   Also handles primitive types and array types. */

tree
819
build_class_ref (tree type)
Anthony Green committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
{
  int is_compiled = is_compiled_class (type);
  if (is_compiled)
    {
      tree ref, decl_name, decl;
      if (TREE_CODE (type) == POINTER_TYPE)
	type = TREE_TYPE (type);
      if (TREE_CODE (type) == RECORD_TYPE)
	{
	  if (TYPE_SIZE (type) == error_mark_node)
	    return null_pointer_node;
	  decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
					"", '/', '/', ".class");
	  decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
	  if (decl == NULL_TREE)
	    {
	      decl = build_decl (VAR_DECL, decl_name, class_type_node);
	      DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
838
	      DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
Anthony Green committed
839 840 841 842
	      TREE_STATIC (decl) = 1;
	      TREE_PUBLIC (decl) = 1;
	      DECL_IGNORED_P (decl) = 1;
	      DECL_ARTIFICIAL (decl) = 1;
843 844
	      if (is_compiled == 1)
		DECL_EXTERNAL (decl) = 1;
845 846 847
	      SET_DECL_ASSEMBLER_NAME (decl, 
				       java_mangle_class_field
				       (&temporary_obstack, type));
848
	      make_decl_rtl (decl, NULL);
Anthony Green committed
849 850 851 852 853
	      pushdecl_top_level (decl);
	    }
	}
      else
	{
854
	  const char *name;
855
	  char buffer[25];
856 857
	  if (flag_emit_class_files)
	    {
Kaveh R. Ghazi committed
858
	      const char *prim_class_name;
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	      tree prim_class;
	      if (type == char_type_node)
		prim_class_name = "java.lang.Character";
	      else if (type == boolean_type_node)
		prim_class_name = "java.lang.Boolean";
	      else if (type == byte_type_node)
		prim_class_name = "java.lang.Byte";
	      else if (type == short_type_node)
		prim_class_name = "java.lang.Short";
	      else if (type == int_type_node)
		prim_class_name = "java.lang.Integer";
	      else if (type == long_type_node)
		prim_class_name = "java.lang.Long";
	      else if (type == float_type_node)
                prim_class_name = "java.lang.Float";
	      else if (type == double_type_node)
                prim_class_name = "java.lang.Double";
	      else if (type == void_type_node)
                prim_class_name = "java.lang.Void";
	      else
879 880
		abort ();

881 882 883 884
	      prim_class = lookup_class (get_identifier (prim_class_name));
	      return build (COMPONENT_REF, NULL_TREE,
			    prim_class, TYPE_identifier_node);
	    }
Anthony Green committed
885 886 887 888 889 890
	  decl_name = TYPE_NAME (type);
	  if (TREE_CODE (decl_name) == TYPE_DECL)
	    decl_name = DECL_NAME (decl_name);
	  name = IDENTIFIER_POINTER (decl_name);
	  if (strncmp (name, "promoted_", 9) == 0)
	    name += 9;
891
	  sprintf (buffer, "_Jv_%sClass", name);
Anthony Green committed
892 893 894 895 896 897 898
	  decl_name = get_identifier (buffer);
	  decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
	  if (decl == NULL_TREE)
	    {
	      decl = build_decl (VAR_DECL, decl_name, class_type_node);
	      TREE_STATIC (decl) = 1;
	      TREE_PUBLIC (decl) = 1;
899
	      DECL_EXTERNAL (decl) = 1;
900
	      make_decl_rtl (decl, NULL);
Anthony Green committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
	      pushdecl_top_level (decl);
	    }
	}

      ref = build1 (ADDR_EXPR, class_ptr_type, decl);
      return ref;
    }
  else
    {
      int index;
      tree cl;
      index = alloc_class_constant (type);
      cl = build_ref_from_constant_pool (index); 
      TREE_TYPE (cl) = promote_type (class_ptr_type);
      return cl;
    }
}

tree
920
build_static_field_ref (tree fdecl)
Anthony Green committed
921 922 923
{
  tree fclass = DECL_CONTEXT (fdecl);
  int is_compiled = is_compiled_class (fclass);
924 925 926

  /* Allow static final fields to fold to a constant.  */
  if (is_compiled || FIELD_FINAL (fdecl))
Anthony Green committed
927
    {
928
      if (!DECL_RTL_SET_P (fdecl))
Anthony Green committed
929 930 931
	{
	  if (is_compiled == 1)
	    DECL_EXTERNAL (fdecl) = 1;
932
	  make_decl_rtl (fdecl, NULL);
Anthony Green committed
933 934 935 936 937 938
	}
      return fdecl;
    }
  else
    {
      /* Compile as:
939
       * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
Anthony Green committed
940 941 942 943 944 945 946 947 948 949 950 951
      tree ref = build_class_ref (fclass);
      tree fld;
      int field_index = 0;
      ref = build1 (INDIRECT_REF, class_type_node, ref);
      ref = build (COMPONENT_REF, field_ptr_type_node, ref,
		   lookup_field (&class_type_node, fields_ident));

      for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
	{
	  if (fld == fdecl)
	    break;
	  if (fld == NULL_TREE)
952 953
	    fatal_error ("field '%s' not found in class",
			 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
Anthony Green committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	  if (FIELD_STATIC (fld))
	    field_index++;
	}
      field_index *= int_size_in_bytes (field_type_node);
      ref = fold (build (PLUS_EXPR, field_ptr_type_node,
			 ref, build_int_2 (field_index, 0)));
      ref = build1 (INDIRECT_REF, field_type_node, ref);
      ref = build (COMPONENT_REF, field_info_union_node,
		   ref, lookup_field (&field_type_node, info_ident));
      ref = build (COMPONENT_REF, ptr_type_node,
		   ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
      return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
    }
}

int
970
get_access_flags_from_decl (tree decl)
Anthony Green committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
{
  int access_flags = 0;
  if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
    {
      if (FIELD_STATIC (decl))
	access_flags |= ACC_STATIC;
      if (FIELD_PUBLIC (decl))
	access_flags |= ACC_PUBLIC;
      if (FIELD_PROTECTED (decl))
	access_flags |= ACC_PROTECTED;
      if (FIELD_PRIVATE (decl))
	access_flags |= ACC_PRIVATE;
      if (FIELD_FINAL (decl))
	access_flags |= ACC_FINAL;
      if (FIELD_VOLATILE (decl))
	access_flags |= ACC_VOLATILE;
      if (FIELD_TRANSIENT (decl))
	access_flags |= ACC_TRANSIENT;
      return access_flags;
    }
  if (TREE_CODE (decl) == TYPE_DECL)
    {
      if (CLASS_PUBLIC (decl))
	access_flags |= ACC_PUBLIC;
      if (CLASS_FINAL (decl))
	access_flags |= ACC_FINAL;
      if (CLASS_SUPER (decl))
	access_flags |= ACC_SUPER;
      if (CLASS_INTERFACE (decl))
	access_flags |= ACC_INTERFACE;
      if (CLASS_ABSTRACT (decl))
	access_flags |= ACC_ABSTRACT;
1003 1004
      if (CLASS_STATIC (decl))
	access_flags |= ACC_STATIC;
1005 1006 1007 1008
      if (CLASS_PRIVATE (decl))
	access_flags |= ACC_PRIVATE;
      if (CLASS_PROTECTED (decl))
	access_flags |= ACC_PROTECTED;
1009 1010
      if (CLASS_STRICTFP (decl))
	access_flags |= ACC_STRICT;
Anthony Green committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
      return access_flags;
    }
  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
      if (METHOD_PUBLIC (decl))
	access_flags |= ACC_PUBLIC;
      if (METHOD_PRIVATE (decl))
	access_flags |= ACC_PRIVATE;
      if (METHOD_PROTECTED (decl))
	access_flags |= ACC_PROTECTED;
      if (METHOD_STATIC (decl))
	access_flags |= ACC_STATIC;
      if (METHOD_FINAL (decl))
	access_flags |= ACC_FINAL;
      if (METHOD_SYNCHRONIZED (decl))
	access_flags |= ACC_SYNCHRONIZED;
      if (METHOD_NATIVE (decl))
	access_flags |= ACC_NATIVE;
      if (METHOD_ABSTRACT (decl))
	access_flags |= ACC_ABSTRACT;
      if (METHOD_TRANSIENT (decl))
	access_flags |= ACC_TRANSIENT;
1033 1034
      if (METHOD_STRICTFP (decl))
	access_flags |= ACC_STRICT;
Anthony Green committed
1035 1036 1037 1038 1039
      return access_flags;
    }
  abort ();
}

1040
static tree
1041
make_field_value (tree fdecl)
Anthony Green committed
1042
{
1043
  tree finit;
1044
  int flags;
Anthony Green committed
1045 1046
  tree type = TREE_TYPE (fdecl);
  int resolved = is_compiled_class (type);
1047

Anthony Green committed
1048 1049 1050 1051 1052
  START_RECORD_CONSTRUCTOR (finit, field_type_node);
  PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
  if (resolved)
    type = build_class_ref (type);
  else
1053 1054
    {
      tree signature = build_java_signature (type);
1055

1056
      type = build_utf8_ref (unmangle_classname 
1057 1058
			     (IDENTIFIER_POINTER (signature),
			      IDENTIFIER_LENGTH (signature)));
1059
    }
Anthony Green committed
1060
  PUSH_FIELD_VALUE (finit, "type", type);
1061

Anthony Green committed
1062 1063 1064
  flags = get_access_flags_from_decl (fdecl);
  if (! resolved)
    flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1065

Anthony Green committed
1066
  PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1067 1068
  PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  PUSH_FIELD_VALUE
    (finit, "info",
     build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
	    build_tree_list
	    ((FIELD_STATIC (fdecl)
	      ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
	      : TYPE_FIELDS (field_info_union_node)),
	     (FIELD_STATIC (fdecl)
	      ? build_address_of (build_static_field_ref (fdecl))
	      : byte_position (fdecl)))));
Anthony Green committed
1079 1080 1081 1082 1083

  FINISH_RECORD_CONSTRUCTOR (finit);
  return finit;
}

1084
static tree
1085
make_method_value (tree mdecl)
Anthony Green committed
1086
{
1087
  static int method_name_count = 0;
Anthony Green committed
1088
  tree minit;
1089
  tree index;
Anthony Green committed
1090 1091 1092
  tree code;
#define ACC_TRANSLATED          0x4000
  int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1093 1094 1095 1096 1097 1098

  if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
    index = DECL_VINDEX (mdecl);
  else
    index = integer_minus_one_node;

Anthony Green committed
1099
  code = null_pointer_node;
1100
  if (DECL_RTL_SET_P (mdecl))
Anthony Green committed
1101 1102 1103 1104 1105 1106
    code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
  START_RECORD_CONSTRUCTOR (minit, method_type_node);
  PUSH_FIELD_VALUE (minit, "name",
		    build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
				    init_identifier_node
				    : DECL_NAME (mdecl)));
1107 1108 1109 1110 1111 1112 1113 1114
  {
    tree signature = build_java_signature (TREE_TYPE (mdecl));
    PUSH_FIELD_VALUE (minit, "signature", 
		      (build_utf8_ref 
		       (unmangle_classname 
			(IDENTIFIER_POINTER(signature),
			 IDENTIFIER_LENGTH(signature)))));
  }
Anthony Green committed
1115
  PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1116
  PUSH_FIELD_VALUE (minit, "index", index);
Anthony Green committed
1117
  PUSH_FIELD_VALUE (minit, "ncode", code);
1118 1119 1120

  {
    /* Compute the `throws' information for the method.  */
1121
    tree table = null_pointer_node;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
    if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
      {
	int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
	tree iter, type, array;
	char buf[60];

	table = tree_cons (NULL_TREE, table, NULL_TREE);
	for (iter = DECL_FUNCTION_THROWS (mdecl);
	     iter != NULL_TREE;
	     iter = TREE_CHAIN (iter))
	  {
1133
	    tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	    tree utf8
	      = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
						    IDENTIFIER_LENGTH (sig)));
	    table = tree_cons (NULL_TREE, utf8, table);
	  }
	type = build_prim_array_type (ptr_type_node, length);
	table = build (CONSTRUCTOR, type, NULL_TREE, table);
	/* Compute something unique enough.  */
	sprintf (buf, "_methods%d", method_name_count++);
	array = build_decl (VAR_DECL, get_identifier (buf), type);
	DECL_INITIAL (array) = table;
	TREE_STATIC (array) = 1;
	DECL_ARTIFICIAL (array) = 1;
	DECL_IGNORED_P (array) = 1;
	rest_of_decl_compilation (array, (char*) 0, 1, 0);

	table = build1 (ADDR_EXPR, ptr_type_node, array);
      }

    PUSH_FIELD_VALUE (minit, "throws", table);
  }

Anthony Green committed
1156 1157 1158 1159
  FINISH_RECORD_CONSTRUCTOR (minit);
  return minit;
}

1160
static tree
1161
get_dispatch_vector (tree type)
Anthony Green committed
1162 1163
{
  tree vtable = TYPE_VTABLE (type);
1164 1165

  if (vtable == NULL_TREE)
Anthony Green committed
1166
    {
1167
      HOST_WIDE_INT i;
Anthony Green committed
1168 1169
      tree method;
      tree super = CLASSTYPE_SUPER (type);
1170
      HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
Anthony Green committed
1171 1172 1173 1174 1175
      vtable = make_tree_vec (nvirtuals);
      TYPE_VTABLE (type) = vtable;
      if (super != NULL_TREE)
	{
	  tree super_vtable = get_dispatch_vector (super);
1176 1177

	  for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
Anthony Green committed
1178 1179
	    TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
	}
1180

Anthony Green committed
1181 1182
      for (method = TYPE_METHODS (type);  method != NULL_TREE;
	   method = TREE_CHAIN (method))
1183 1184 1185 1186
	if (DECL_VINDEX (method) != NULL_TREE
	    && host_integerp (DECL_VINDEX (method), 0))
	  TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
	    = method;
Anthony Green committed
1187
    }
1188

Anthony Green committed
1189 1190 1191
  return vtable;
}

1192
static tree
1193
get_dispatch_table (tree type, tree this_class_addr)
Anthony Green committed
1194
{
1195
  int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
Anthony Green committed
1196
  tree vtable = get_dispatch_vector (type);
1197
  int i, j;
Anthony Green committed
1198 1199
  tree list = NULL_TREE;
  int nvirtuals = TREE_VEC_LENGTH (vtable);
1200
  int arraysize;
1201
  tree gc_descr;
1202

Anthony Green committed
1203 1204 1205 1206
  for (i = nvirtuals;  --i >= 0; )
    {
      tree method = TREE_VEC_ELT (vtable, i);
      if (METHOD_ABSTRACT (method))
1207 1208 1209 1210
	{
	  if (! abstract_p)
	    warning_with_decl (method,
			       "abstract method in non-abstract class");
1211 1212 1213 1214 1215 1216

	  if (TARGET_VTABLE_USES_DESCRIPTORS)
	    for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
	      list = tree_cons (NULL_TREE, null_pointer_node, list);
	  else
	    list = tree_cons (NULL_TREE, null_pointer_node, list);
1217 1218 1219
	}
      else
	{
1220
	  if (!DECL_RTL_SET_P (method))
1221
	    make_decl_rtl (method, NULL);
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

	  if (TARGET_VTABLE_USES_DESCRIPTORS)
	    for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
	      {
		tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node, 
				    method, build_int_2 (j, 0));
		TREE_CONSTANT (fdesc) = 1;
	        list = tree_cons (NULL_TREE, fdesc, list);
	      }
	  else
	    list = tree_cons (NULL_TREE,
			      build1 (ADDR_EXPR, nativecode_ptr_type_node,
				      method),
			      list);
1236
	}
Anthony Green committed
1237
    }
1238

1239 1240
  /* Dummy entry for compatibility with G++ -fvtable-thunks.  When
     using the Boehm GC we sometimes stash a GC type descriptor
1241 1242
     there. We set the PURPOSE to NULL_TREE not to interfere (reset)
     the emitted byte count during the output to the assembly file. */
1243 1244 1245 1246 1247 1248 1249 1250
  /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
     fake "function descriptor".  It's first word is the is the class
     pointer, and subsequent words (usually one) contain the GC descriptor.
     In all other cases, we reserve two extra vtable slots. */
  gc_descr =  get_boehm_type_descriptor (type);
  list = tree_cons (NULL_TREE, gc_descr, list);
  for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
    list = tree_cons (NULL_TREE, gc_descr, list);
1251 1252 1253 1254 1255 1256
  list = tree_cons (NULL_TREE, this_class_addr, list);

  /** Pointer to type_info object (to be implemented), according to g++ ABI. */
  list = tree_cons (NULL_TREE, null_pointer_node, list);
  /** Offset to start of whole object.  Always (ptrdiff_t)0 for Java. */
  list = tree_cons (integer_zero_node, null_pointer_node, list);
1257

1258
  arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1259 1260
  if (TARGET_VTABLE_USES_DESCRIPTORS)
    arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1261
  arraysize += 2;
1262 1263 1264
  return build (CONSTRUCTOR,
		build_prim_array_type (nativecode_ptr_type_node, arraysize),
		NULL_TREE, list);
Anthony Green committed
1265 1266
}

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
static int
supers_all_compiled (tree type)
{
  while (type != NULL_TREE)
    {
      if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
	return 0;
      type = CLASSTYPE_SUPER (type);
    }
  return 1;
}

Anthony Green committed
1279
void
1280
make_class_data (tree type)
Anthony Green committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
{
  tree decl, cons, temp;
  tree field, fields_decl;
  tree static_fields = NULL_TREE;
  tree instance_fields = NULL_TREE;
  HOST_WIDE_INT static_field_count = 0;
  HOST_WIDE_INT instance_field_count = 0;
  HOST_WIDE_INT field_count;
  tree field_array_type;
  tree method;
  tree methods = NULL_TREE;
  tree dtable_decl = NULL_TREE;
  HOST_WIDE_INT method_count = 0;
  tree method_array_type;
  tree methods_decl;
  tree super;
  tree this_class_addr;
  tree constant_pool_constructor;
  tree interfaces = null_pointer_node;
  int interface_len = 0;
  tree type_decl = TYPE_NAME (type);
1302 1303 1304
  /** Offset from start of virtual function table declaration
      to where objects actually point at, following new g++ ABI. */
  tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
Anthony Green committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

  this_class_addr = build_class_ref (type);
  decl = TREE_OPERAND (this_class_addr, 0);

  /* Build Field array. */
  field = TYPE_FIELDS (type);
  if (DECL_NAME (field) == NULL_TREE)
    field = TREE_CHAIN (field);  /* Skip dummy field for inherited data. */
  for ( ;  field != NULL_TREE;  field = TREE_CHAIN (field))
    {
      if (! DECL_ARTIFICIAL (field))
	{
	  tree init = make_field_value (field);
	  if (FIELD_STATIC (field))
	    {
1320
	      tree initial = DECL_INITIAL (field);
Anthony Green committed
1321 1322
	      static_field_count++;
	      static_fields = tree_cons (NULL_TREE, init, static_fields);
1323 1324 1325 1326 1327
	      /* If the initial value is a string constant,
		 prevent output_constant from trying to assemble the value. */
	      if (initial != NULL_TREE
		  && TREE_TYPE (initial) == string_ptr_type_node)
		DECL_INITIAL (field) = NULL_TREE;
Anthony Green committed
1328
	      rest_of_decl_compilation (field, (char*) 0, 1, 1);
1329
	      DECL_INITIAL (field) = initial;
Anthony Green committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	    }
	  else
	    {
	      instance_field_count++;
	      instance_fields = tree_cons (NULL_TREE, init, instance_fields);
	    }
	}
    }
  field_count = static_field_count + instance_field_count;
  if (field_count > 0)
    {
      static_fields = nreverse (static_fields);
      instance_fields = nreverse (instance_fields);
      static_fields = chainon (static_fields, instance_fields);
      field_array_type = build_prim_array_type (field_type_node, field_count);
      fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
				field_array_type);
      DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
					  NULL_TREE, static_fields);
      TREE_STATIC (fields_decl) = 1;
      DECL_ARTIFICIAL (fields_decl) = 1;
      DECL_IGNORED_P (fields_decl) = 1;
      rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
    }
  else
    fields_decl = NULL_TREE;

  /* Build Method array. */
1358
  for (method = TYPE_METHODS (type);
Anthony Green committed
1359 1360
       method != NULL_TREE; method = TREE_CHAIN (method))
    {
1361 1362
      tree init;
      if (METHOD_PRIVATE (method)
1363
	  && ! flag_keep_inline_functions
1364 1365
	  && (flag_inline_functions || optimize))
	continue;
1366
      init = make_method_value (method);
Anthony Green committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
      method_count++;
      methods = tree_cons (NULL_TREE, init, methods);
    }
  method_array_type = build_prim_array_type (method_type_node, method_count);
  methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
			     method_array_type);
  DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
				       NULL_TREE, nreverse (methods));
  TREE_STATIC (methods_decl) = 1;
  DECL_ARTIFICIAL (methods_decl) = 1;
  DECL_IGNORED_P (methods_decl) = 1;
  rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);

1380 1381
  if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
      && !flag_indirect_dispatch)
Anthony Green committed
1382 1383 1384 1385 1386 1387 1388 1389 1390
    {
      tree dtable = get_dispatch_table (type, this_class_addr);
      dtable_decl = build_dtable_decl (type);
      DECL_INITIAL (dtable_decl) = dtable;
      TREE_STATIC (dtable_decl) = 1;
      DECL_ARTIFICIAL (dtable_decl) = 1;
      DECL_IGNORED_P (dtable_decl) = 1;
      TREE_PUBLIC (dtable_decl) = 1;
      rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
      if (type == class_type_node)
	class_dtable_decl = dtable_decl;
    }

  if (class_dtable_decl == NULL_TREE)
    {
      class_dtable_decl = build_dtable_decl (class_type_node);
      TREE_STATIC (class_dtable_decl) = 1;
      DECL_ARTIFICIAL (class_dtable_decl) = 1;
      DECL_IGNORED_P (class_dtable_decl) = 1;
1401 1402
      if (is_compiled_class (class_type_node) != 2)
	DECL_EXTERNAL (class_dtable_decl) = 1;
1403
      rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
Anthony Green committed
1404 1405 1406 1407 1408
    }

  super = CLASSTYPE_SUPER (type);
  if (super == NULL_TREE)
    super = null_pointer_node;
1409 1410
  else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
	   && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
Anthony Green committed
1411 1412 1413 1414 1415
    super = build_class_ref (super);
  else
    {
      int super_index = alloc_class_constant (super);
      super = build_int_2 (super_index, 0);
Kaveh R. Ghazi committed
1416
      TREE_TYPE (super) = ptr_type_node;
Anthony Green committed
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    }

  /* Build and emit the array of implemented interfaces. */
  if (type != object_type_node)
      interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
  if (interface_len > 0)
    {
      tree init = NULL_TREE;
      int i;
      tree interface_array_type, idecl;
      interface_array_type
	= build_prim_array_type (class_ptr_type, interface_len);
      idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
			  interface_array_type);
      for (i = interface_len;  i > 0; i--)
	{
	  tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
	  tree iclass = BINFO_TYPE (child);
	  tree index;
1436
	  if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
Anthony Green committed
1437 1438 1439 1440 1441
	    index = build_class_ref (iclass);
	  else
	    {
		int int_index = alloc_class_constant (iclass);
		index = build_int_2 (int_index, 0);
Kaveh R. Ghazi committed
1442
		TREE_TYPE (index) = ptr_type_node;
Anthony Green committed
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	    }
	  init = tree_cons (NULL_TREE, index, init); 
	}
      DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
				    NULL_TREE, init);
      TREE_STATIC (idecl) = 1;
      DECL_ARTIFICIAL (idecl) = 1;
      DECL_IGNORED_P (idecl) = 1;
      interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
      rest_of_decl_compilation (idecl,  (char*) 0, 1, 0);
    }

  constant_pool_constructor = build_constants_constructor ();

  START_RECORD_CONSTRUCTOR (temp, object_type_node);
1458
  PUSH_FIELD_VALUE (temp, "vtable",
1459 1460 1461
		    build (PLUS_EXPR, dtable_ptr_type,
			   build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
			   dtable_start_offset));
1462 1463
  if (! flag_hash_synchronization)
    PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
Anthony Green committed
1464 1465 1466 1467
  FINISH_RECORD_CONSTRUCTOR (temp);
  START_RECORD_CONSTRUCTOR (cons, class_type_node);
  PUSH_SUPER_VALUE (cons, temp);
  PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1468
  PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
Anthony Green committed
1469 1470 1471
  PUSH_FIELD_VALUE (cons, "accflags",
		    build_int_2 (get_access_flags_from_decl (type_decl), 0));

1472 1473
  PUSH_FIELD_VALUE (cons, "superclass", 
		    CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
Anthony Green committed
1474 1475 1476
  PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
  PUSH_FIELD_VALUE (cons, "methods",
		    build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1477
  PUSH_FIELD_VALUE (cons, "method_count",  build_int_2 (method_count, 0));
1478 1479 1480 1481 1482 1483

  if (flag_indirect_dispatch)
    PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
  else
    PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
    
Anthony Green committed
1484 1485 1486
  PUSH_FIELD_VALUE (cons, "fields",
		    fields_decl == NULL_TREE ? null_pointer_node
		    : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1487 1488 1489 1490
  PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
  PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
  PUSH_FIELD_VALUE (cons, "static_field_count",
		    build_int_2 (static_field_count, 0));
1491 1492 1493 1494 1495 1496

  if (flag_indirect_dispatch)
    PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
  else
    PUSH_FIELD_VALUE (cons, "vtable",
		      dtable_decl == NULL_TREE ? null_pointer_node
1497 1498 1499
		      : build (PLUS_EXPR, dtable_ptr_type,
			       build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
			       dtable_start_offset));
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  
  if (otable_methods == NULL_TREE)
    {
      PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
      PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
    }
  else
    {
      PUSH_FIELD_VALUE (cons, "otable",
			build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
      PUSH_FIELD_VALUE (cons, "otable_syms",
			build1 (ADDR_EXPR, method_symbols_array_ptr_type,
				otable_syms_decl));
    }
Anthony Green committed
1514 1515
  PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
  PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1516
  PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1517
  PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
Anthony Green committed
1518

Per Bothner committed
1519
  PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1520 1521 1522
  PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
  PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
  PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
Bryce McKinlay committed
1523
  PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1524
  PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1525
  PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
Per Bothner committed
1526

Anthony Green committed
1527 1528 1529
  FINISH_RECORD_CONSTRUCTOR (cons);

  DECL_INITIAL (decl) = cons;
1530 1531 1532 1533 1534
  
  /* Hash synchronization requires at least 64-bit alignment. */
  if (flag_hash_synchronization && POINTER_SIZE < 64)
    DECL_ALIGN (decl) = 64; 
  
Anthony Green committed
1535 1536 1537
  rest_of_decl_compilation (decl, (char*) 0, 1, 0);
}

1538
void
1539
finish_class (void)
1540 1541
{
  tree method;
1542
  tree type_methods = TYPE_METHODS (current_class);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
  int saw_native_method = 0;

  /* Find out if we have any native methods.  We use this information
     later.  */
  for (method = type_methods;
       method != NULL_TREE;
       method = TREE_CHAIN (method))
    {
      if (METHOD_NATIVE (method))
	{
	  saw_native_method = 1;
	  break;
	}
    }

1558 1559
  /* Emit deferred inline methods. */  
  for (method = type_methods; method != NULL_TREE; )
1560 1561 1562
    {
      if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
	{
1563 1564 1565 1566 1567
	  output_inline_function (method);
	  /* Scan the list again to see if there are any earlier
	     methods to emit. */
	  method = type_methods;
	  continue;
1568
	}
1569
      method = TREE_CHAIN (method);
1570 1571
    }

1572
  current_function_decl = NULL_TREE;
1573 1574 1575 1576 1577
  make_class_data (current_class);
  register_class ();
  rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
}

Anthony Green committed
1578 1579 1580 1581 1582
/* Return 2 if CLASS is compiled by this compilation job;
   return 1 if CLASS can otherwise be assumed to be compiled;
   return 0 if we cannot assume that CLASS is compiled.
   Returns 1 for primitive and 0 for array types.  */
int
1583
is_compiled_class (tree class)
Anthony Green committed
1584
{
1585
  int seen_in_zip;
Anthony Green committed
1586 1587 1588 1589 1590 1591 1592 1593
  if (TREE_CODE (class) == POINTER_TYPE)
    class = TREE_TYPE (class);
  if (TREE_CODE (class) != RECORD_TYPE)  /* Primitive types are static. */
    return 1;
  if (TYPE_ARRAY_P (class))
    return 0;
  if (class == current_class)
    return 2;
1594

1595
  seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1596
  if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
Anthony Green committed
1597 1598 1599 1600
    {
      /* The class was seen in the current ZIP file and will be
	 available as a compiled class in the future but may not have
	 been loaded already. Load it if necessary. This prevent
1601
	 build_class_ref () from crashing. */
Anthony Green committed
1602

1603
      if (seen_in_zip && !CLASS_LOADED_P (class))
Anthony Green committed
1604
        load_class (class, 1);
1605 1606 1607

      /* We return 2 for class seen in ZIP and class from files
         belonging to the same compilation unit */
Anthony Green committed
1608 1609 1610
      return 2;
    }

1611
  if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
Anthony Green committed
1612 1613
    {
      if (!CLASS_LOADED_P (class))
1614 1615 1616 1617 1618 1619
	{
	  if (CLASS_FROM_SOURCE_P (class))
	    safe_layout_class (class);
	  else
	    load_class (class, 1);
	}
Anthony Green committed
1620 1621 1622 1623 1624 1625 1626 1627 1628
      return 1;
    }

  return 0;
}

/* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */

tree
1629
build_dtable_decl (tree type)
Anthony Green committed
1630
{
1631
  tree dtype;
1632 1633 1634 1635

  /* We need to build a new dtable type so that its size is uniquely
     computed when we're dealing with the class for real and not just
     faking it (like java.lang.Class during the initialization of the
1636
     compiler.) We know we're not faking a class when CURRENT_CLASS is
1637 1638 1639
     TYPE. */
  if (current_class == type)
    {
1640 1641
      tree dummy = NULL_TREE;
      int n;
1642 1643

      dtype = make_node (RECORD_TYPE);
1644

1645 1646 1647
      PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
      PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);

1648
      PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
      for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
	{
	  tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
	  TREE_CHAIN (dummy) = tmp_field;
	  DECL_CONTEXT (tmp_field) = dtype;
	  DECL_ARTIFICIAL (tmp_field) = 1;
	  dummy = tmp_field;
	}

      PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
      for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
	{
	  tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
	  TREE_CHAIN (dummy) = tmp_field;
	  DECL_CONTEXT (tmp_field) = dtype;
	  DECL_ARTIFICIAL (tmp_field) = 1;
	  dummy = tmp_field;
	}

      n = TREE_VEC_LENGTH (get_dispatch_vector (type));
      if (TARGET_VTABLE_USES_DESCRIPTORS)
	n *= TARGET_VTABLE_USES_DESCRIPTORS;

      PUSH_FIELD (dtype, dummy, "methods",
		  build_prim_array_type (nativecode_ptr_type_node, n));
1674 1675 1676 1677 1678
      layout_type (dtype);
    }
  else
    dtype = dtable_type;

1679 1680
  return build_decl (VAR_DECL, 
		     java_mangle_vtable (&temporary_obstack, type), dtype);
Anthony Green committed
1681 1682 1683 1684 1685 1686
}

/* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
   fields inherited from SUPER_CLASS. */

void
1687
push_super_field (tree this_class, tree super_class)
Anthony Green committed
1688 1689
{
  tree base_decl;
1690 1691 1692
  /* Don't insert the field if we're just re-laying the class out. */ 
  if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
    return;
Anthony Green committed
1693 1694 1695 1696 1697
  base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
  DECL_IGNORED_P (base_decl) = 1;
  TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
  TYPE_FIELDS (this_class) = base_decl;
  DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1698
  DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
Anthony Green committed
1699 1700
}

1701 1702 1703
/* Handle the different manners we may have to lay out a super class.  */

static tree
1704
maybe_layout_super_class (tree super_class, tree this_class)
1705 1706 1707
{
  if (TREE_CODE (super_class) == RECORD_TYPE)
    {
1708
      if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1709 1710 1711 1712 1713 1714
	safe_layout_class (super_class);
      if (!CLASS_LOADED_P (super_class))
	load_class (super_class, 1);
    }
  /* We might have to layout the class before its dependency on
     the super class gets resolved by java_complete_class  */
1715
  else if (TREE_CODE (super_class) == POINTER_TYPE)
1716
    {
1717 1718 1719 1720
      if (TREE_TYPE (super_class) != NULL_TREE)
	super_class = TREE_TYPE (super_class);
      else
	{
1721 1722
	  super_class = do_resolve_class (NULL_TREE, /* FIXME? */
					  super_class, NULL_TREE, this_class);
1723 1724 1725 1726
	  if (!super_class)
	    return NULL_TREE;	/* FIXME, NULL_TREE not checked by caller. */
	  super_class = TREE_TYPE (super_class);
	}
1727 1728 1729 1730 1731 1732 1733
    }
  if (!TYPE_SIZE (super_class))
    safe_layout_class (super_class);

  return super_class;
}

Anthony Green committed
1734
void
1735
layout_class (tree this_class)
Anthony Green committed
1736 1737
{
  tree super_class = CLASSTYPE_SUPER (this_class);
1738
  tree field;
1739
  
1740
  class_list = tree_cons (this_class, NULL_TREE, class_list);
1741 1742 1743
  if (CLASS_BEING_LAIDOUT (this_class))
    {
      char buffer [1024];
Mark Mitchell committed
1744
      char *report;
1745 1746 1747 1748 1749 1750
      tree current;
      
      sprintf (buffer, " with `%s'",
	       IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
      obstack_grow (&temporary_obstack, buffer, strlen (buffer));

1751
      for (current = TREE_CHAIN (class_list); current; 
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	   current = TREE_CHAIN (current))
	{
	  tree decl = TYPE_NAME (TREE_PURPOSE (current));
	  sprintf (buffer, "\n  which inherits from `%s' (%s:%d)",
		   IDENTIFIER_POINTER (DECL_NAME (decl)),
		   DECL_SOURCE_FILE (decl),
		   DECL_SOURCE_LINE (decl));
	  obstack_grow (&temporary_obstack, buffer, strlen (buffer));
	}
      obstack_1grow (&temporary_obstack, '\0');
Mark Mitchell committed
1762 1763 1764
      report = obstack_finish (&temporary_obstack);
      cyclic_inheritance_report = ggc_strdup (report);
      obstack_free (&temporary_obstack, report);
1765 1766 1767 1768
      TYPE_SIZE (this_class) = error_mark_node;
      return;
    }
  CLASS_BEING_LAIDOUT (this_class) = 1;
Anthony Green committed
1769

1770
  if (super_class && !CLASS_BEING_LAIDOUT (super_class))
Anthony Green committed
1771
    {
1772 1773 1774 1775
      tree maybe_super_class 
	= maybe_layout_super_class (super_class, this_class);
      if (maybe_super_class == NULL
	  || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
Anthony Green committed
1776 1777
	{
	  TYPE_SIZE (this_class) = error_mark_node;
1778
	  CLASS_BEING_LAIDOUT (this_class) = 0;
1779
	  class_list = TREE_CHAIN (class_list);
Anthony Green committed
1780 1781 1782
	  return;
	}
      if (TYPE_SIZE (this_class) == NULL_TREE)
1783
	push_super_field (this_class, maybe_super_class);
Anthony Green committed
1784 1785 1786 1787 1788 1789 1790 1791
    }

  for (field = TYPE_FIELDS (this_class);
       field != NULL_TREE;  field = TREE_CHAIN (field))
    {
      if (FIELD_STATIC (field))
	{
	  /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1792 1793 1794
	  SET_DECL_ASSEMBLER_NAME (field,
				   java_mangle_decl
				   (&temporary_obstack, field));
Anthony Green committed
1795 1796 1797 1798
	}
    }

  layout_type (this_class);
1799

1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
  /* Also recursively load/layout any superinterfaces, but only if class was
  loaded from bytecode. The source parser will take care of this itself. */
  if (!CLASS_FROM_SOURCE_P (this_class))
    {
      tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);

      if (basetype_vec)
	{
	  int n = TREE_VEC_LENGTH (basetype_vec) - 1;
	  int i;
	  for (i = n; i > 0; i--)
	    {
	      tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
	      tree super_interface = BINFO_TYPE (vec_elt);

	      tree maybe_super_interface 
		= maybe_layout_super_class (super_interface, NULL_TREE);
	      if (maybe_super_interface == NULL
		  || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
		{
		  TYPE_SIZE (this_class) = error_mark_node;
		  CLASS_BEING_LAIDOUT (this_class) = 0;
1822
		  class_list = TREE_CHAIN (class_list);
1823 1824 1825 1826 1827 1828
		  return;
		}
	    }
	}
    }

1829 1830 1831
  /* Convert the size back to an SI integer value */
  TYPE_SIZE_UNIT (this_class) = 
    fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1832 1833

  CLASS_BEING_LAIDOUT (this_class) = 0;
1834
  class_list = TREE_CHAIN (class_list);
1835
}
Anthony Green committed
1836

1837
void
1838
layout_class_methods (tree this_class)
1839 1840
{
  tree method_decl, dtable_count;
1841
  tree super_class;
1842 1843 1844 1845 1846

  if (TYPE_NVIRTUALS (this_class))
    return;

  super_class = CLASSTYPE_SUPER (this_class);
Anthony Green committed
1847

1848
  if (super_class)
Anthony Green committed
1849
    {
1850
      super_class = maybe_layout_super_class (super_class, this_class);
1851 1852 1853 1854 1855 1856
      if (!TYPE_NVIRTUALS (super_class))
	layout_class_methods (super_class);
      dtable_count = TYPE_NVIRTUALS (super_class);
    }
  else
    dtable_count = integer_zero_node;
1857

1858
  TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
1859

1860
  for (method_decl = TYPE_METHODS (this_class);
1861 1862 1863 1864 1865 1866 1867 1868
       method_decl; method_decl = TREE_CHAIN (method_decl))
    dtable_count = layout_class_method (this_class, super_class, 
					method_decl, dtable_count);

  TYPE_NVIRTUALS (this_class) = dtable_count;
}

/* Lay METHOD_DECL out, returning a possibly new value of
1869
   DTABLE_COUNT. Also mangle the method's name. */
1870 1871

tree
1872 1873
layout_class_method (tree this_class, tree super_class,
		     tree method_decl, tree dtable_count)
1874 1875
{
  tree method_name = DECL_NAME (method_decl);
1876 1877 1878

  TREE_PUBLIC (method_decl) = 1;

1879
  /* This is a good occasion to mangle the method's name */
1880 1881 1882
  SET_DECL_ASSEMBLER_NAME (method_decl,
			   java_mangle_decl (&temporary_obstack, 
					     method_decl));
1883 1884
  /* We don't generate a RTL for the method if it's abstract, or if
     it's an interface method that isn't clinit. */
1885
  if (! METHOD_ABSTRACT (method_decl) 
1886
      || (CLASS_INTERFACE (TYPE_NAME (this_class)) 
1887
	  && (DECL_CLINIT_P (method_decl))))
1888
    make_decl_rtl (method_decl, NULL);
1889

1890
  if (ID_INIT_P (method_name))
1891
    {
1892
      const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1893
      const char *ptr;
1894
      for (ptr = p; *ptr; )
Anthony Green committed
1895
	{
1896 1897 1898 1899
	  if (*ptr++ == '.')
	    p = ptr;
	}
      DECL_CONSTRUCTOR_P (method_decl) = 1;
1900
      build_java_argument_signature (TREE_TYPE (method_decl));
1901 1902 1903
    }
  else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
    {
1904
      tree method_sig =
1905 1906
	build_java_argument_signature (TREE_TYPE (method_decl));
      tree super_method = lookup_argument_method (super_class, method_name,
Anthony Green committed
1907
						  method_sig);
1908
      if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1909 1910
	{
	  DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1911 1912
	  if (DECL_VINDEX (method_decl) == NULL_TREE 
	      && !CLASS_FROM_SOURCE_P (this_class))
1913 1914 1915 1916
	    error_with_decl (method_decl,
			     "non-static method '%s' overrides static method");
	}
      else if (! METHOD_FINAL (method_decl)
1917
	       && ! METHOD_PRIVATE (method_decl)
1918 1919 1920 1921
	       && ! CLASS_FINAL (TYPE_NAME (this_class))
	       && dtable_count)
	{
	  DECL_VINDEX (method_decl) = dtable_count;
1922 1923
	  dtable_count = fold (build (PLUS_EXPR, integer_type_node,
				      dtable_count, integer_one_node));
Anthony Green committed
1924 1925
	}
    }
1926

1927
  return dtable_count;
Anthony Green committed
1928 1929 1930
}

void
1931
register_class (void)
Anthony Green committed
1932
{
1933 1934 1935
  /* END does not need to be registered with the garbage collector
     because it always points into the list given by REGISTERED_CLASS,
     and that variable is registered with the collector.  */
Anthony Green committed
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
  static tree end;
  tree node    = TREE_OPERAND (build_class_ref (current_class), 0);
  tree current = copy_node (node);

  XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
  if (!registered_class)
    registered_class = current;
  else
    TREE_CHAIN (end) = current;

  end = current;
}

1949 1950 1951 1952
/* Emit something to register classes at start-up time.

   The preferred mechanism is through the .jcr section, which contain
   a list of pointers to classes which get registered during
1953
   constructor invocation time.  The fallback mechanism is to generate
1954 1955
   a `constructor' function which calls _Jv_RegisterClass for each
   class in this file.  */
Anthony Green committed
1956 1957

void
1958
emit_register_classes (void)
Anthony Green committed
1959
{
1960 1961 1962
  /* ??? This isn't quite the correct test.  We also have to know
     that the target is using gcc's crtbegin/crtend objects rather
     than the ones that come with the operating system.  */
1963 1964
  if (SUPPORTS_WEAK && targetm.have_named_sections)
    {
1965
#ifdef JCR_SECTION_NAME
1966
      tree t;
1967 1968
      named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
      assemble_align (POINTER_SIZE);
1969 1970
      for (t = registered_class; t; t = TREE_CHAIN (t))
	assemble_integer (XEXP (DECL_RTL (t), 0),
1971
			  POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1972 1973 1974
#else
      abort ();
#endif
1975 1976 1977
    }
  else
    {
1978
      extern tree get_file_function_name (int);
1979 1980 1981 1982 1983 1984 1985 1986 1987
      tree init_name = get_file_function_name ('I');
      tree init_type = build_function_type (void_type_node, end_params_node);
      tree init_decl;
      tree t;
      
      init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
      SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
      TREE_STATIC (init_decl) = 1;
      current_function_decl = init_decl;
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
      DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
					    void_type_node);

      /* It can be a static function as long as collect2 does not have
         to scan the object file to find its ctor/dtor routine.  */
      TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;

      /* Suppress spurious warnings.  */
      TREE_USED (init_decl) = 1;

1998 1999 2000 2001
      pushlevel (0);
      make_decl_rtl (init_decl, NULL);
      init_function_start (init_decl, input_filename, 0);
      expand_function_start (init_decl, 0);
2002 2003 2004 2005 2006

      /* Do not allow the function to be deferred.  */
      current_function_cannot_inline
	= "static constructors and destructors cannot be inlined";

2007 2008 2009 2010 2011 2012
      for ( t = registered_class; t; t = TREE_CHAIN (t))
	emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
			   XEXP (DECL_RTL (t), 0), Pmode);
      
      expand_function_end (input_filename, 0, 0);
      poplevel (1, 0, 1);
2013
      rest_of_compilation (init_decl);
2014
      current_function_decl = NULL_TREE;
2015 2016 2017 2018

      if (targetm.have_ctors_dtors)
	(* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
					 DEFAULT_INIT_PRIORITY);
2019
    }
Anthony Green committed
2020 2021
}

2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
/* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */

tree
build_method_symbols_entry (tree method)
{
  tree clname, name, signature, method_symbol;
  
  clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
  name = build_utf8_ref (DECL_NAME (method));
  signature = build_java_signature (TREE_TYPE (method));
  signature = build_utf8_ref (unmangle_classname 
			      (IDENTIFIER_POINTER (signature),
			       IDENTIFIER_LENGTH (signature)));

  START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
  PUSH_FIELD_VALUE (method_symbol, "clname", clname);
  PUSH_FIELD_VALUE (method_symbol, "name", name);
  PUSH_FIELD_VALUE (method_symbol, "signature", signature);
  FINISH_RECORD_CONSTRUCTOR (method_symbol);
  TREE_CONSTANT (method_symbol) = 1;

  return method_symbol;
} 

/* Emit the offset symbols table for indirect virtual dispatch. */

void
2049
emit_offset_symbol_table (void)
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
{
  tree method_list, method, table, list, null_symbol;
  tree otable_bound, otable_array_type;
  int index;
  
  /* Only emit an offset table if this translation unit actually made virtual 
     calls. */
  if (otable_methods == NULL_TREE)
    return;

  /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
  index = 0;
  method_list = otable_methods;
  list = NULL_TREE;  
  while (method_list != NULL_TREE)
    {
      method = TREE_VALUE (method_list);
      list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
      method_list = TREE_CHAIN (method_list);
      index++;
    }

  /* Terminate the list with a "null" entry. */
  START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
  PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
  PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
  PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
  FINISH_RECORD_CONSTRUCTOR (null_symbol);
  TREE_CONSTANT (null_symbol) = 1;  
  list = tree_cons (NULL_TREE, null_symbol, list);

  /* Put the list in the right order and make it a constructor. */
  list = nreverse (list);
  table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);  

  /* Make it the initial value for otable_syms and emit the decl. */
  DECL_INITIAL (otable_syms_decl) = table;
  DECL_ARTIFICIAL (otable_syms_decl) = 1;
  DECL_IGNORED_P (otable_syms_decl) = 1;
  rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
  
  /* Now that its size is known, redefine otable as an uninitialized static 
     array of INDEX + 1 integers. The extra entry is used by the runtime 
     to track whether the otable has been initialized. */
  otable_bound = build_index_type (build_int_2 (index, 0));
  otable_array_type = build_array_type (integer_type_node, otable_bound);
  otable_decl = build_decl (VAR_DECL, get_identifier ("otable"), 
			    otable_array_type);
  TREE_STATIC (otable_decl) = 1;
  TREE_READONLY (otable_decl) = 1;  
  rest_of_decl_compilation (otable_decl, NULL, 1, 0);
}

Anthony Green committed
2103
void
2104
init_class_processing (void)
Anthony Green committed
2105
{
2106
  registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2107 2108
  fields_ident = get_identifier ("fields");
  info_ident = get_identifier ("info");
Mark Mitchell committed
2109
  gcc_obstack_init (&temporary_obstack);
Anthony Green committed
2110
}
2111

2112 2113
static hashval_t java_treetreehash_hash (const void *);
static int java_treetreehash_compare (const void *, const void *);
2114 2115 2116

/* A hash table mapping trees to trees.  Used generally.  */

2117
#define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2118 2119

static hashval_t
2120
java_treetreehash_hash (const void *k_p)
2121 2122 2123 2124 2125 2126
{
  struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
  return JAVA_TREEHASHHASH_H (k->key);
}

static int
2127
java_treetreehash_compare (const void * k1_p, const void * k2_p)
2128 2129 2130 2131 2132 2133 2134
{
  struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
  tree k2 = (tree) k2_p;
  return (k1->key == k2);
}

tree 
2135
java_treetreehash_find (htab_t ht, tree t)
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
{
  struct treetreehash_entry *e;
  hashval_t hv = JAVA_TREEHASHHASH_H (t);
  e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
  if (e == NULL)
    return NULL;
  else
    return e->value;
}

tree *
2147
java_treetreehash_new (htab_t ht, tree t)
2148
{
2149
  void **e;
2150 2151 2152 2153 2154 2155 2156 2157
  struct treetreehash_entry *tthe;
  hashval_t hv = JAVA_TREEHASHHASH_H (t);

  e = htab_find_slot_with_hash (ht, t, hv, INSERT);
  if (*e == NULL)
    {
      tthe = (*ht->alloc_f) (1, sizeof (*tthe));
      tthe->key = t;
2158
      *e = tthe;
2159 2160 2161 2162 2163 2164 2165
    }
  else
    tthe = (struct treetreehash_entry *) *e;
  return &tthe->value;
}

htab_t
2166
java_treetreehash_create (size_t size, int gc)
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
{
  if (gc)
    return htab_create_ggc (size, java_treetreehash_hash,
			    java_treetreehash_compare, NULL);
  else
    return htab_create_alloc (size, java_treetreehash_hash,
			      java_treetreehash_compare, free, xcalloc, free);
}

#include "gt-java-class.h"