1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "coretypes.h"
35 #include "java-tree.h"
47 #include "tree-iterator.h"
50 /* DOS brain-damage */
52 #define O_BINARY 0 /* MS-DOS brain-damage */
55 static tree make_method_value (tree);
56 static tree build_java_method_type (tree, tree, int);
57 static int32 hashUtf8String (const char *, int);
58 static tree make_field_value (tree);
59 static tree get_dispatch_vector (tree);
60 static tree get_dispatch_table (tree, tree);
61 static int supers_all_compiled (tree type);
62 static tree maybe_layout_super_class (tree, tree);
63 static void add_miranda_methods (tree, tree);
64 static int assume_compiled (const char *);
65 static tree build_symbol_entry (tree);
66 static tree emit_assertion_table (tree);
68 struct obstack temporary_obstack;
70 /* The compiler generates different code depending on whether or not
71 it can assume certain classes have been compiled down to native
72 code or not. The compiler options -fassume-compiled= and
73 -fno-assume-compiled= are used to create a tree of
74 class_flag_node objects. This tree is queried to determine if
75 a class is assume to be compiled or not. Each node in the tree
76 represents either a package or a specific class. */
78 typedef struct class_flag_node_struct
80 /* The class or package name. */
83 /* Nonzero if this represents an exclusion. */
86 /* Pointers to other nodes in the tree. */
87 struct class_flag_node_struct *parent;
88 struct class_flag_node_struct *sibling;
89 struct class_flag_node_struct *child;
92 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
93 static void add_class_flag (class_flag_node **, const char *, int);
95 /* This is the root of the include/exclude tree. */
97 static class_flag_node *assume_compiled_tree;
99 static class_flag_node *enable_assert_tree;
101 static GTY(()) tree class_roots[5];
102 #define registered_class class_roots[0]
103 #define fields_ident class_roots[1] /* get_identifier ("fields") */
104 #define info_ident class_roots[2] /* get_identifier ("info") */
105 #define class_list class_roots[3]
106 #define class_dtable_decl class_roots[4]
108 /* Return the node that most closely represents the class whose name
109 is IDENT. Start the search from NODE (followed by its siblings).
110 Return NULL if an appropriate node does not exist. */
112 static class_flag_node *
113 find_class_flag_node (class_flag_node *node, const char *ident)
117 size_t node_ident_length = strlen (node->ident);
119 /* node_ident_length is zero at the root of the tree. If the
120 identifiers are the same length, then we have matching
121 classes. Otherwise check if we've matched an enclosing
124 if (node_ident_length == 0
125 || (strncmp (ident, node->ident, node_ident_length) == 0
126 && (ident[node_ident_length] == '\0'
127 || ident[node_ident_length] == '.')))
129 /* We've found a match, however, there might be a more
132 class_flag_node *found = find_class_flag_node (node->child, ident);
139 /* No match yet. Continue through the sibling list. */
140 node = node->sibling;
143 /* No match at all in this tree. */
148 add_class_flag (class_flag_node **rootp, const char *ident, int value)
150 class_flag_node *root = *rootp;
151 class_flag_node *parent, *node;
153 /* Create the root of the tree if it doesn't exist yet. */
157 root = xmalloc (sizeof (class_flag_node));
160 root->sibling = NULL;
166 /* Calling the function with the empty string means we're setting
167 value for the root of the hierarchy. */
175 /* Find the parent node for this new node. PARENT will either be a
176 class or a package name. Adjust PARENT accordingly. */
178 parent = find_class_flag_node (root, ident);
179 if (strcmp (ident, parent->ident) == 0)
180 parent->value = value;
183 /* Insert new node into the tree. */
184 node = xmalloc (sizeof (class_flag_node));
186 node->ident = xstrdup (ident);
190 node->parent = parent;
191 node->sibling = parent->child;
192 parent->child = node;
196 /* Add a new IDENT to the include/exclude tree. It's an exclusion
197 if EXCLUDEP is nonzero. */
200 add_assume_compiled (const char *ident, int excludep)
202 add_class_flag (&assume_compiled_tree, ident, excludep);
205 /* The default value returned by enable_assertions. */
207 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
209 /* Enter IDENT (a class or package name) into the enable-assertions table.
210 VALUE is true to enable and false to disable. */
213 add_enable_assert (const char *ident, int value)
215 if (enable_assert_tree == NULL)
216 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
217 add_class_flag (&enable_assert_tree, ident, value);
220 /* Returns nonzero if IDENT is the name of a class that the compiler
221 should assume has been compiled to object code. */
224 assume_compiled (const char *ident)
229 if (NULL == assume_compiled_tree)
232 i = find_class_flag_node (assume_compiled_tree, ident);
239 /* Return true if we should generate code to check assertions within KLASS. */
242 enable_assertions (tree klass)
244 /* Check if command-line specifies whether we should check assertions. */
246 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
248 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
249 class_flag_node *node
250 = find_class_flag_node (enable_assert_tree, ident);
254 /* The default is to enable assertions if generating class files,
255 or not optimizing. */
256 return DEFAULT_ENABLE_ASSERT;
259 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
260 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
261 Also, PREFIX is prepended, and SUFFIX is appended. */
264 ident_subst (const char* old_name,
271 int prefix_len = strlen (prefix);
272 int suffix_len = strlen (suffix);
273 int i = prefix_len + old_length + suffix_len + 1;
274 char *buffer = alloca (i);
276 strcpy (buffer, prefix);
277 for (i = 0; i < old_length; i++)
279 char ch = old_name[i];
282 buffer[prefix_len + i] = ch;
284 strcpy (buffer + prefix_len + old_length, suffix);
285 return get_identifier (buffer);
288 /* Return an IDENTIFIER_NODE the same as OLD_ID,
289 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
290 Also, PREFIX is prepended, and SUFFIX is appended. */
293 identifier_subst (const tree old_id,
299 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
300 prefix, old_char, new_char, suffix);
303 /* Generate a valid C identifier from the name of the class TYPE,
304 prefixed by PREFIX. */
307 mangled_classname (const char *prefix, tree type)
309 tree ident = TYPE_NAME (type);
310 if (TREE_CODE (ident) != IDENTIFIER_NODE)
311 ident = DECL_NAME (ident);
312 return identifier_subst (ident, prefix, '.', '_', "");
319 type = make_node (RECORD_TYPE);
320 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
325 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
326 and where each of the constituents is separated by '/',
327 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
330 unmangle_classname (const char *name, int name_length)
332 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
333 /* It's not sufficient to compare to_return and get_identifier
334 (name) to determine whether to_return is qualified. There are
335 cases in signature analysis where name will be stripped of a
337 name = IDENTIFIER_POINTER (to_return);
341 QUALIFIED_P (to_return) = 1;
348 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
351 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
352 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
355 sprintf (buf, #NAME "_%s", typename); \
356 TYPE_## TABLE ##_DECL (type) = decl = \
357 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
358 DECL_EXTERNAL (decl) = 1; \
359 TREE_STATIC (decl) = 1; \
360 TREE_READONLY (decl) = 1; \
361 TREE_CONSTANT (decl) = 1; \
362 DECL_IGNORED_P (decl) = 1; \
363 /* Mark the table as belonging to this class. */ \
365 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
366 DECL_OWNER (decl) = TYPE; \
367 sprintf (buf, #NAME "_syms_%s", typename); \
368 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
369 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
370 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
371 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
372 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
373 pushdecl (TYPE_## TABLE ##_SYMS_DECL (TYPE)); \
377 /* Given a class, create the DECLs for all its associated indirect
380 gen_indirect_dispatch_tables (tree type)
382 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
385 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
386 tree catch_class_type = make_node (RECORD_TYPE);
388 sprintf (buf, "_catch_classes_%s", typename);
389 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
390 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
391 FINISH_RECORD (catch_class_type);
393 TYPE_CTABLE_DECL (type)
394 = build_decl (VAR_DECL, get_identifier (buf),
395 build_array_type (catch_class_type, 0));
396 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
397 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
398 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
399 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
400 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
401 pushdecl (TYPE_CTABLE_DECL (type));
404 if (flag_indirect_dispatch)
406 GEN_TABLE (ATABLE, _atable, atable_type, type);
407 GEN_TABLE (OTABLE, _otable, otable_type, type);
408 GEN_TABLE (ITABLE, _itable, itable_type, type);
415 push_class (tree class_type, tree class_name)
417 tree decl, signature;
418 location_t saved_loc = input_location;
419 #ifndef USE_MAPPED_LOCATION
420 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
421 input_filename = IDENTIFIER_POINTER (source_name);
424 CLASS_P (class_type) = 1;
425 decl = build_decl (TYPE_DECL, class_name, class_type);
426 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
428 /* dbxout needs a DECL_SIZE if in gstabs mode */
429 DECL_SIZE (decl) = integer_zero_node;
431 input_location = saved_loc;
432 signature = identifier_subst (class_name, "L", '.', '/', ";");
433 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
435 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
436 both a typedef and in the struct name-space. We may want to re-visit
437 this later, but for now it reduces the changes needed for gdb. */
438 DECL_ARTIFICIAL (decl) = 1;
440 pushdecl_top_level (decl);
445 /* Finds the (global) class named NAME. Creates the class if not found.
446 Also creates associated TYPE_DECL.
447 Does not check if the class actually exists, load the class,
448 fill in field or methods, or do layout_type. */
451 lookup_class (tree name)
453 tree decl = IDENTIFIER_CLASS_VALUE (name);
454 if (decl == NULL_TREE)
455 decl = push_class (make_class (), name);
456 return TREE_TYPE (decl);
460 set_super_info (int access_flags, tree this_class,
461 tree super_class, int interfaces_count)
463 int total_supers = interfaces_count;
464 tree class_decl = TYPE_NAME (this_class);
469 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
470 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
473 tree super_binfo = make_tree_binfo (0);
474 BINFO_TYPE (super_binfo) = super_class;
475 BINFO_OFFSET (super_binfo) = integer_zero_node;
476 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
477 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
480 set_class_decl_access_flags (access_flags, class_decl);
484 set_class_decl_access_flags (int access_flags, tree class_decl)
486 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
487 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
488 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
489 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
490 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
491 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
492 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
493 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
494 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
497 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
498 direct sub-classes of Object are 1, and so on. */
501 class_depth (tree clas)
504 if (! CLASS_LOADED_P (clas))
505 load_class (clas, 1);
506 if (TYPE_SIZE (clas) == error_mark_node)
508 while (clas != object_type_node)
511 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
516 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
519 interface_of_p (tree type1, tree type2)
522 tree binfo, base_binfo;
524 if (! TYPE_BINFO (type2))
527 for (binfo = TYPE_BINFO (type2), i = 0;
528 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
529 if (BINFO_TYPE (base_binfo) == type1)
532 for (binfo = TYPE_BINFO (type2), i = 0;
533 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
534 if (BINFO_TYPE (base_binfo)
535 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
541 /* Return true iff TYPE1 inherits from TYPE2. */
544 inherits_from_p (tree type1, tree type2)
546 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
550 type1 = CLASSTYPE_SUPER (type1);
555 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
558 enclosing_context_p (tree type1, tree type2)
560 if (!INNER_CLASS_TYPE_P (type2))
563 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
565 type2 = (INNER_CLASS_TYPE_P (type2) ?
566 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
576 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
580 common_enclosing_context_p (tree type1, tree type2)
585 for (current = type2; current;
586 current = (INNER_CLASS_TYPE_P (current) ?
587 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
589 if (type1 == current)
592 if (INNER_CLASS_TYPE_P (type1))
593 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
600 /* Return 1 iff there exists a common enclosing "this" between TYPE1
601 and TYPE2, without crossing any static context. */
604 common_enclosing_instance_p (tree type1, tree type2)
606 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
609 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
610 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
611 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
614 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
615 current = (PURE_INNER_CLASS_TYPE_P (current) ?
616 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
618 if (type1 == current)
624 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
625 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
626 if attempt is made to add it twice. */
629 maybe_add_interface (tree this_class, tree interface_class)
631 tree binfo, base_binfo;
634 for (binfo = TYPE_BINFO (this_class), i = 0;
635 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
636 if (BINFO_TYPE (base_binfo) == interface_class)
637 return interface_class;
638 add_interface (this_class, interface_class);
642 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
645 add_interface (tree this_class, tree interface_class)
647 tree interface_binfo = make_tree_binfo (0);
649 BINFO_TYPE (interface_binfo) = interface_class;
650 BINFO_OFFSET (interface_binfo) = integer_zero_node;
651 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
652 BINFO_VIRTUAL_P (interface_binfo) = 1;
654 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
658 /* Return the address of a pointer to the first FUNCTION_DECL
659 in the list (*LIST) whose DECL_NAME is NAME. */
662 find_named_method (tree *list, tree name)
664 while (*list && DECL_NAME (*list) != name)
665 list = &TREE_CHAIN (*list);
671 build_java_method_type (tree fntype, tree this_class, int access_flags)
673 if (access_flags & ACC_STATIC)
675 return build_method_type (this_class, fntype);
679 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
681 tree method_type, fndecl;
683 method_type = build_java_method_type (function_type,
684 this_class, access_flags);
686 fndecl = build_decl (FUNCTION_DECL, name, method_type);
687 DECL_CONTEXT (fndecl) = this_class;
689 DECL_LANG_SPECIFIC (fndecl)
690 = ggc_alloc_cleared (sizeof (struct lang_decl));
691 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
693 /* Initialize the static initializer test table. */
695 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
696 java_treetreehash_create (10, 1);
698 /* Initialize the initialized (static) class table. */
699 if (access_flags & ACC_STATIC)
700 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
701 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
703 /* Initialize the static method invocation compound list */
704 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
706 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
707 TYPE_METHODS (this_class) = fndecl;
709 /* Notice that this is a finalizer and update the class type
710 accordingly. This is used to optimize instance allocation. */
711 if (name == finalize_identifier_node
712 && TREE_TYPE (function_type) == void_type_node
713 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
714 HAS_FINALIZER_P (this_class) = 1;
716 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
717 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
718 if (access_flags & ACC_PRIVATE)
719 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
720 if (access_flags & ACC_NATIVE)
722 METHOD_NATIVE (fndecl) = 1;
723 DECL_EXTERNAL (fndecl) = 1;
725 if (access_flags & ACC_STATIC)
726 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
727 if (access_flags & ACC_FINAL)
728 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
729 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
730 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
731 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
735 /* Add a method to THIS_CLASS.
736 The method's name is NAME.
737 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
740 add_method (tree this_class, int access_flags, tree name, tree method_sig)
742 tree function_type, fndecl;
743 const unsigned char *sig
744 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
747 fatal_error ("bad method signature");
749 function_type = get_type_from_signature (method_sig);
750 fndecl = add_method_1 (this_class, access_flags, name, function_type);
751 set_java_signature (TREE_TYPE (fndecl), method_sig);
756 add_field (tree class, tree name, tree field_type, int flags)
758 int is_static = (flags & ACC_STATIC) != 0;
760 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
761 TREE_CHAIN (field) = TYPE_FIELDS (class);
762 TYPE_FIELDS (class) = field;
763 DECL_CONTEXT (field) = class;
765 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
766 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
767 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
768 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
769 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
770 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
773 FIELD_STATIC (field) = 1;
774 /* Always make field externally visible. This is required so
775 that native methods can always access the field. */
776 TREE_PUBLIC (field) = 1;
777 /* Considered external until we know what classes are being
778 compiled into this object file. */
779 DECL_EXTERNAL (field) = 1;
785 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
788 set_constant_value (tree field, tree constant)
790 if (field == NULL_TREE)
791 warning (0, "misplaced ConstantValue attribute (not in any field)");
792 else if (DECL_INITIAL (field) != NULL_TREE)
793 warning (0, "duplicate ConstantValue attribute for field '%s'",
794 IDENTIFIER_POINTER (DECL_NAME (field)));
797 DECL_INITIAL (field) = constant;
798 if (TREE_TYPE (constant) != TREE_TYPE (field)
799 && ! (TREE_TYPE (constant) == int_type_node
800 && INTEGRAL_TYPE_P (TREE_TYPE (field))
801 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
802 && ! (TREE_TYPE (constant) == utf8const_ptr_type
803 && TREE_TYPE (field) == string_ptr_type_node))
804 error ("ConstantValue attribute of field '%s' has wrong type",
805 IDENTIFIER_POINTER (DECL_NAME (field)));
806 if (FIELD_FINAL (field))
807 DECL_FIELD_FINAL_IUD (field) = 1;
811 /* Count the number of Unicode chars encoded in a given Ut8 string. */
815 strLengthUtf8 (char *str, int len)
817 register unsigned char* ptr = (unsigned char*) str;
818 register unsigned char *limit = ptr + len;
820 for (; ptr < limit; str_length++) {
821 if (UTF8_GET (ptr, limit) < 0)
829 /* Calculate a hash value for a string encoded in Utf8 format.
830 * This returns the same hash value as specified for java.lang.String.hashCode.
834 hashUtf8String (const char *str, int len)
836 const unsigned char* ptr = (const unsigned char*) str;
837 const unsigned char *limit = ptr + len;
841 int ch = UTF8_GET (ptr, limit);
842 /* Updated specification from
843 http://www.javasoft.com/docs/books/jls/clarify.html. */
844 hash = (31 * hash) + ch;
849 static GTY(()) tree utf8_decl_list = NULL_TREE;
852 build_utf8_ref (tree name)
854 const char * name_ptr = IDENTIFIER_POINTER(name);
855 int name_len = IDENTIFIER_LENGTH(name);
857 tree ctype, field = NULL_TREE, str_type, cinit, string;
858 static int utf8_count = 0;
860 tree ref = IDENTIFIER_UTF8_REF (name);
862 if (ref != NULL_TREE)
865 ctype = make_node (RECORD_TYPE);
866 str_type = build_prim_array_type (unsigned_byte_type_node,
867 name_len + 1); /* Allow for final '\0'. */
868 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
869 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
870 PUSH_FIELD (ctype, field, "data", str_type);
871 FINISH_RECORD (ctype);
872 START_RECORD_CONSTRUCTOR (cinit, ctype);
873 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
874 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
875 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
876 string = build_string (name_len, name_ptr);
877 TREE_TYPE (string) = str_type;
878 PUSH_FIELD_VALUE (cinit, "data", string);
879 FINISH_RECORD_CONSTRUCTOR (cinit);
880 TREE_CONSTANT (cinit) = 1;
881 TREE_INVARIANT (cinit) = 1;
883 /* Generate a unique-enough identifier. */
884 sprintf(buf, "_Utf%d", ++utf8_count);
886 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
887 TREE_STATIC (decl) = 1;
888 DECL_ARTIFICIAL (decl) = 1;
889 DECL_IGNORED_P (decl) = 1;
890 TREE_READONLY (decl) = 1;
891 TREE_THIS_VOLATILE (decl) = 0;
892 DECL_INITIAL (decl) = cinit;
894 if (HAVE_GAS_SHF_MERGE)
897 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
898 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
899 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
900 if (flag_merge_constants && decl_size < 256)
903 int flags = (SECTION_OVERRIDE
904 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
905 sprintf (buf, ".rodata.jutf8.%d", decl_size);
906 named_section_flags (buf, flags);
907 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
911 TREE_CHAIN (decl) = utf8_decl_list;
912 layout_decl (decl, 0);
914 rest_of_decl_compilation (decl, global_bindings_p (), 0);
915 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
916 utf8_decl_list = decl;
917 make_decl_rtl (decl);
918 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
919 IDENTIFIER_UTF8_REF (name) = ref;
923 /* Like build_class_ref, but instead of a direct reference generate a
924 pointer into the constant pool. */
927 build_indirect_class_ref (tree type)
931 index = alloc_class_constant (type);
932 cl = build_ref_from_constant_pool (index);
933 return convert (promote_type (class_ptr_type), cl);
936 /* Build a reference to the class TYPE.
937 Also handles primitive types and array types. */
940 build_class_ref (tree type)
942 int is_compiled = is_compiled_class (type);
945 tree ref, decl_name, decl;
946 if (TREE_CODE (type) == POINTER_TYPE)
947 type = TREE_TYPE (type);
949 if (flag_indirect_dispatch
950 && type != output_class
951 && TREE_CODE (type) == RECORD_TYPE)
952 return build_indirect_class_ref (type);
954 if (TREE_CODE (type) == RECORD_TYPE)
956 if (TYPE_SIZE (type) == error_mark_node)
957 return null_pointer_node;
958 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
959 "", '/', '/', ".class");
960 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
961 if (decl == NULL_TREE)
963 decl = build_decl (VAR_DECL, decl_name, class_type_node);
964 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
965 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
966 TREE_STATIC (decl) = 1;
967 TREE_PUBLIC (decl) = 1;
968 DECL_IGNORED_P (decl) = 1;
969 DECL_ARTIFICIAL (decl) = 1;
970 if (is_compiled == 1)
971 DECL_EXTERNAL (decl) = 1;
972 SET_DECL_ASSEMBLER_NAME (decl,
973 java_mangle_class_field
974 (&temporary_obstack, type));
975 make_decl_rtl (decl);
976 pushdecl_top_level (decl);
983 if (flag_emit_class_files)
985 const char *prim_class_name;
987 if (type == char_type_node)
988 prim_class_name = "java.lang.Character";
989 else if (type == boolean_type_node)
990 prim_class_name = "java.lang.Boolean";
991 else if (type == byte_type_node)
992 prim_class_name = "java.lang.Byte";
993 else if (type == short_type_node)
994 prim_class_name = "java.lang.Short";
995 else if (type == int_type_node)
996 prim_class_name = "java.lang.Integer";
997 else if (type == long_type_node)
998 prim_class_name = "java.lang.Long";
999 else if (type == float_type_node)
1000 prim_class_name = "java.lang.Float";
1001 else if (type == double_type_node)
1002 prim_class_name = "java.lang.Double";
1003 else if (type == void_type_node)
1004 prim_class_name = "java.lang.Void";
1008 prim_class = lookup_class (get_identifier (prim_class_name));
1009 return build3 (COMPONENT_REF, NULL_TREE,
1010 prim_class, TYPE_identifier_node, NULL_TREE);
1012 decl_name = TYPE_NAME (type);
1013 if (TREE_CODE (decl_name) == TYPE_DECL)
1014 decl_name = DECL_NAME (decl_name);
1015 name = IDENTIFIER_POINTER (decl_name);
1016 if (strncmp (name, "promoted_", 9) == 0)
1018 sprintf (buffer, "_Jv_%sClass", name);
1019 decl_name = get_identifier (buffer);
1020 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1021 if (decl == NULL_TREE)
1023 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1024 TREE_STATIC (decl) = 1;
1025 TREE_PUBLIC (decl) = 1;
1026 DECL_EXTERNAL (decl) = 1;
1027 DECL_ARTIFICIAL (decl) = 1;
1028 make_decl_rtl (decl);
1029 pushdecl_top_level (decl);
1033 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1037 return build_indirect_class_ref (type);
1040 /* Create a local statically allocated variable that will hold a
1041 pointer to a static field. */
1044 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1046 tree decl, decl_name;
1047 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1048 char *buf = alloca (strlen (name) + 20);
1049 sprintf (buf, "%s_%d_ref", name, index);
1050 decl_name = get_identifier (buf);
1051 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1052 if (decl == NULL_TREE)
1054 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1055 TREE_STATIC (decl) = 1;
1056 TREE_PUBLIC (decl) = 0;
1057 DECL_EXTERNAL (decl) = 0;
1058 DECL_ARTIFICIAL (decl) = 1;
1059 make_decl_rtl (decl);
1060 pushdecl_top_level (decl);
1066 build_static_field_ref (tree fdecl)
1068 tree fclass = DECL_CONTEXT (fdecl);
1069 int is_compiled = is_compiled_class (fclass);
1071 /* Allow static final fields to fold to a constant. When using
1072 -fno-assume-compiled, gcj will sometimes try to fold a field from
1073 an uncompiled class. This is required when the field in question
1074 meets the appropriate criteria for a compile-time constant.
1075 However, currently sometimes gcj is too eager and will end up
1076 returning the field itself, leading to an incorrect external
1077 reference being generated. */
1078 if ((is_compiled && !flag_indirect_dispatch)
1079 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1080 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1081 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1082 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1084 if (!DECL_RTL_SET_P (fdecl))
1086 if (is_compiled == 1)
1087 DECL_EXTERNAL (fdecl) = 1;
1088 make_decl_rtl (fdecl);
1093 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1094 and a class local static variable CACHE_ENTRY, then
1096 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1097 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1100 This can mostly be optimized away, so that the usual path is a
1101 load followed by a test and branch. _Jv_ResolvePoolEntry is
1102 only called once for each constant pool entry.
1104 There is an optimization that we don't do: at the start of a
1105 method, create a local copy of CACHE_ENTRY and use that instead.
1109 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1110 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1112 = build3 (CALL_EXPR, boolean_type_node,
1113 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1114 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1115 cache_entry, null_pointer_node),
1116 build_tree_list (NULL_TREE, boolean_false_node)),
1118 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1120 = build3 (CALL_EXPR, ptr_type_node,
1121 build_address_of (soft_resolvepoolentry_node),
1122 tree_cons (NULL_TREE, build_class_ref (output_class),
1123 build_tree_list (NULL_TREE, cpool_index_cst)),
1125 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1126 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1127 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1128 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1134 get_access_flags_from_decl (tree decl)
1136 int access_flags = 0;
1137 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1139 if (FIELD_STATIC (decl))
1140 access_flags |= ACC_STATIC;
1141 if (FIELD_PUBLIC (decl))
1142 access_flags |= ACC_PUBLIC;
1143 if (FIELD_PROTECTED (decl))
1144 access_flags |= ACC_PROTECTED;
1145 if (FIELD_PRIVATE (decl))
1146 access_flags |= ACC_PRIVATE;
1147 if (FIELD_FINAL (decl))
1148 access_flags |= ACC_FINAL;
1149 if (FIELD_VOLATILE (decl))
1150 access_flags |= ACC_VOLATILE;
1151 if (FIELD_TRANSIENT (decl))
1152 access_flags |= ACC_TRANSIENT;
1153 return access_flags;
1155 if (TREE_CODE (decl) == TYPE_DECL)
1157 if (CLASS_PUBLIC (decl))
1158 access_flags |= ACC_PUBLIC;
1159 if (CLASS_FINAL (decl))
1160 access_flags |= ACC_FINAL;
1161 if (CLASS_SUPER (decl))
1162 access_flags |= ACC_SUPER;
1163 if (CLASS_INTERFACE (decl))
1164 access_flags |= ACC_INTERFACE;
1165 if (CLASS_ABSTRACT (decl))
1166 access_flags |= ACC_ABSTRACT;
1167 if (CLASS_STATIC (decl))
1168 access_flags |= ACC_STATIC;
1169 if (CLASS_PRIVATE (decl))
1170 access_flags |= ACC_PRIVATE;
1171 if (CLASS_PROTECTED (decl))
1172 access_flags |= ACC_PROTECTED;
1173 if (CLASS_STRICTFP (decl))
1174 access_flags |= ACC_STRICT;
1175 return access_flags;
1177 if (TREE_CODE (decl) == FUNCTION_DECL)
1179 if (METHOD_PUBLIC (decl))
1180 access_flags |= ACC_PUBLIC;
1181 if (METHOD_PRIVATE (decl))
1182 access_flags |= ACC_PRIVATE;
1183 if (METHOD_PROTECTED (decl))
1184 access_flags |= ACC_PROTECTED;
1185 if (METHOD_STATIC (decl))
1186 access_flags |= ACC_STATIC;
1187 if (METHOD_FINAL (decl))
1188 access_flags |= ACC_FINAL;
1189 if (METHOD_SYNCHRONIZED (decl))
1190 access_flags |= ACC_SYNCHRONIZED;
1191 if (METHOD_NATIVE (decl))
1192 access_flags |= ACC_NATIVE;
1193 if (METHOD_ABSTRACT (decl))
1194 access_flags |= ACC_ABSTRACT;
1195 if (METHOD_STRICTFP (decl))
1196 access_flags |= ACC_STRICT;
1197 if (METHOD_INVISIBLE (decl))
1198 access_flags |= ACC_INVISIBLE;
1199 return access_flags;
1204 static GTY (()) int alias_labelno = 0;
1206 /* Create a private alias for METHOD. Using this alias instead of the method
1207 decl ensures that ncode entries in the method table point to the real function
1208 at runtime, not a PLT entry. */
1211 make_local_function_alias (tree method)
1213 #ifdef ASM_OUTPUT_DEF
1216 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1217 char *name = alloca (strlen (method_name) + 2);
1218 char *buf = alloca (strlen (method_name) + 128);
1220 /* Only create aliases for local functions. */
1221 if (DECL_EXTERNAL (method))
1224 /* Prefix method_name with 'L' for the alias label. */
1226 strcpy (name + 1, method_name);
1228 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1229 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1230 TREE_TYPE (method));
1231 DECL_CONTEXT (alias) = NULL;
1232 TREE_READONLY (alias) = TREE_READONLY (method);
1233 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1234 TREE_PUBLIC (alias) = 0;
1235 DECL_EXTERNAL (alias) = 0;
1236 DECL_ARTIFICIAL (alias) = 1;
1237 DECL_INLINE (alias) = 0;
1238 DECL_INITIAL (alias) = error_mark_node;
1239 TREE_ADDRESSABLE (alias) = 1;
1240 TREE_USED (alias) = 1;
1241 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1242 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1243 if (!flag_syntax_only)
1244 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1251 /** Make reflection data (_Jv_Field) for field FDECL. */
1254 make_field_value (tree fdecl)
1258 tree type = TREE_TYPE (fdecl);
1259 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1261 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1262 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1264 type = build_class_ref (type);
1267 tree signature = build_java_signature (type);
1269 type = build_utf8_ref (unmangle_classname
1270 (IDENTIFIER_POINTER (signature),
1271 IDENTIFIER_LENGTH (signature)));
1273 PUSH_FIELD_VALUE (finit, "type", type);
1275 flags = get_access_flags_from_decl (fdecl);
1277 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1279 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1280 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1284 build_constructor (field_info_union_node,
1286 ((FIELD_STATIC (fdecl)
1287 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1288 : TYPE_FIELDS (field_info_union_node)),
1289 (FIELD_STATIC (fdecl)
1290 ? build_address_of (fdecl)
1291 : byte_position (fdecl)))));
1293 FINISH_RECORD_CONSTRUCTOR (finit);
1297 /** Make reflection data (_Jv_Method) for method MDECL. */
1300 make_method_value (tree mdecl)
1302 static int method_name_count = 0;
1307 #define ACC_TRANSLATED 0x4000
1308 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1310 class_decl = DECL_CONTEXT (mdecl);
1311 /* For interfaces, the index field contains the dispatch index. */
1312 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1313 index = build_int_cst (NULL_TREE,
1314 get_interface_method_index (mdecl, class_decl));
1315 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1316 index = get_method_index (mdecl);
1318 index = integer_minus_one_node;
1320 code = null_pointer_node;
1321 if (DECL_RTL_SET_P (mdecl))
1322 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1323 make_local_function_alias (mdecl));
1324 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1325 PUSH_FIELD_VALUE (minit, "name",
1326 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1327 init_identifier_node
1328 : DECL_NAME (mdecl)));
1330 tree signature = build_java_signature (TREE_TYPE (mdecl));
1331 PUSH_FIELD_VALUE (minit, "signature",
1334 (IDENTIFIER_POINTER(signature),
1335 IDENTIFIER_LENGTH(signature)))));
1337 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1338 PUSH_FIELD_VALUE (minit, "index", index);
1339 PUSH_FIELD_VALUE (minit, "ncode", code);
1342 /* Compute the `throws' information for the method. */
1343 tree table = null_pointer_node;
1344 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1346 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1347 tree iter, type, array;
1350 table = tree_cons (NULL_TREE, table, NULL_TREE);
1351 for (iter = DECL_FUNCTION_THROWS (mdecl);
1353 iter = TREE_CHAIN (iter))
1355 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1357 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1358 IDENTIFIER_LENGTH (sig)));
1359 table = tree_cons (NULL_TREE, utf8, table);
1361 type = build_prim_array_type (ptr_type_node, length);
1362 table = build_constructor (type, table);
1363 /* Compute something unique enough. */
1364 sprintf (buf, "_methods%d", method_name_count++);
1365 array = build_decl (VAR_DECL, get_identifier (buf), type);
1366 DECL_INITIAL (array) = table;
1367 TREE_STATIC (array) = 1;
1368 DECL_ARTIFICIAL (array) = 1;
1369 DECL_IGNORED_P (array) = 1;
1370 rest_of_decl_compilation (array, 1, 0);
1372 table = build1 (ADDR_EXPR, ptr_type_node, array);
1375 PUSH_FIELD_VALUE (minit, "throws", table);
1378 FINISH_RECORD_CONSTRUCTOR (minit);
1383 get_dispatch_vector (tree type)
1385 tree vtable = TYPE_VTABLE (type);
1387 if (vtable == NULL_TREE)
1391 tree super = CLASSTYPE_SUPER (type);
1392 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1393 vtable = make_tree_vec (nvirtuals);
1394 TYPE_VTABLE (type) = vtable;
1395 if (super != NULL_TREE)
1397 tree super_vtable = get_dispatch_vector (super);
1399 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1400 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1403 for (method = TYPE_METHODS (type); method != NULL_TREE;
1404 method = TREE_CHAIN (method))
1406 tree method_index = get_method_index (method);
1407 if (method_index != NULL_TREE
1408 && host_integerp (method_index, 0))
1409 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1417 get_dispatch_table (tree type, tree this_class_addr)
1419 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1420 tree vtable = get_dispatch_vector (type);
1422 tree list = NULL_TREE;
1423 int nvirtuals = TREE_VEC_LENGTH (vtable);
1427 for (i = nvirtuals; --i >= 0; )
1429 tree method = TREE_VEC_ELT (vtable, i);
1430 if (METHOD_ABSTRACT (method))
1433 warning (0, "%Jabstract method in non-abstract class", method);
1435 if (TARGET_VTABLE_USES_DESCRIPTORS)
1436 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1437 list = tree_cons (NULL_TREE, null_pointer_node, list);
1439 list = tree_cons (NULL_TREE, null_pointer_node, list);
1443 if (!DECL_RTL_SET_P (method))
1444 make_decl_rtl (method);
1446 if (TARGET_VTABLE_USES_DESCRIPTORS)
1447 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1449 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1450 method, build_int_cst (NULL_TREE, j));
1451 TREE_CONSTANT (fdesc) = 1;
1452 TREE_INVARIANT (fdesc) = 1;
1453 list = tree_cons (NULL_TREE, fdesc, list);
1456 list = tree_cons (NULL_TREE,
1457 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1463 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1464 using the Boehm GC we sometimes stash a GC type descriptor
1465 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1466 the emitted byte count during the output to the assembly file. */
1467 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1468 fake "function descriptor". It's first word is the is the class
1469 pointer, and subsequent words (usually one) contain the GC descriptor.
1470 In all other cases, we reserve two extra vtable slots. */
1471 gc_descr = get_boehm_type_descriptor (type);
1472 list = tree_cons (NULL_TREE, gc_descr, list);
1473 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1474 list = tree_cons (NULL_TREE, gc_descr, list);
1475 list = tree_cons (NULL_TREE, this_class_addr, list);
1477 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1478 list = tree_cons (NULL_TREE, null_pointer_node, list);
1479 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1480 list = tree_cons (integer_zero_node, null_pointer_node, list);
1482 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1483 if (TARGET_VTABLE_USES_DESCRIPTORS)
1484 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1486 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1491 /* Set the method_index for a method decl. */
1493 set_method_index (tree decl, tree method_index)
1495 if (method_index != NULL_TREE)
1497 /* method_index is null if we're using indirect dispatch. */
1498 method_index = fold (convert (sizetype, method_index));
1500 if (TARGET_VTABLE_USES_DESCRIPTORS)
1501 /* Add one to skip bogus descriptor for class and GC descriptor. */
1502 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1504 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1506 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1509 DECL_VINDEX (decl) = method_index;
1512 /* Get the method_index for a method decl. */
1514 get_method_index (tree decl)
1516 tree method_index = DECL_VINDEX (decl);
1521 if (TARGET_VTABLE_USES_DESCRIPTORS)
1522 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1523 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1525 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1526 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1528 return method_index;
1532 supers_all_compiled (tree type)
1534 while (type != NULL_TREE)
1536 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1538 type = CLASSTYPE_SUPER (type);
1544 make_class_data (tree type)
1546 tree decl, cons, temp;
1547 tree field, fields_decl;
1548 tree static_fields = NULL_TREE;
1549 tree instance_fields = NULL_TREE;
1550 HOST_WIDE_INT static_field_count = 0;
1551 HOST_WIDE_INT instance_field_count = 0;
1552 HOST_WIDE_INT field_count;
1553 tree field_array_type;
1555 tree methods = NULL_TREE;
1556 tree dtable_decl = NULL_TREE;
1557 HOST_WIDE_INT method_count = 0;
1558 tree method_array_type;
1561 tree this_class_addr;
1562 tree constant_pool_constructor;
1563 tree interfaces = null_pointer_node;
1564 int interface_len = 0;
1565 tree type_decl = TYPE_NAME (type);
1566 /** Offset from start of virtual function table declaration
1567 to where objects actually point at, following new g++ ABI. */
1568 tree dtable_start_offset = build_int_cst (NULL_TREE,
1569 2 * POINTER_SIZE / BITS_PER_UNIT);
1571 this_class_addr = build_class_ref (type);
1572 decl = TREE_OPERAND (this_class_addr, 0);
1574 /* Build Field array. */
1575 field = TYPE_FIELDS (type);
1576 while (field && DECL_ARTIFICIAL (field))
1577 field = TREE_CHAIN (field); /* Skip dummy fields. */
1578 if (field && DECL_NAME (field) == NULL_TREE)
1579 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1580 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1582 if (! DECL_ARTIFICIAL (field))
1584 tree init = make_field_value (field);
1585 if (FIELD_STATIC (field))
1587 tree initial = DECL_INITIAL (field);
1588 static_field_count++;
1589 static_fields = tree_cons (NULL_TREE, init, static_fields);
1590 /* If the initial value is a string constant,
1591 prevent output_constant from trying to assemble the value. */
1592 if (initial != NULL_TREE
1593 && TREE_TYPE (initial) == string_ptr_type_node)
1594 DECL_INITIAL (field) = NULL_TREE;
1595 rest_of_decl_compilation (field, 1, 1);
1596 DECL_INITIAL (field) = initial;
1600 instance_field_count++;
1601 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1605 field_count = static_field_count + instance_field_count;
1606 if (field_count > 0)
1608 static_fields = nreverse (static_fields);
1609 instance_fields = nreverse (instance_fields);
1610 static_fields = chainon (static_fields, instance_fields);
1611 field_array_type = build_prim_array_type (field_type_node, field_count);
1612 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1614 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1616 TREE_STATIC (fields_decl) = 1;
1617 DECL_ARTIFICIAL (fields_decl) = 1;
1618 DECL_IGNORED_P (fields_decl) = 1;
1619 rest_of_decl_compilation (fields_decl, 1, 0);
1622 fields_decl = NULL_TREE;
1624 /* Build Method array. */
1625 for (method = TYPE_METHODS (type);
1626 method != NULL_TREE; method = TREE_CHAIN (method))
1629 if (METHOD_PRIVATE (method)
1630 && ! flag_keep_inline_functions
1633 /* Even if we have a decl, we don't necessarily have the code.
1634 This can happen if we inherit a method from a superclass for
1635 which we don't have a .class file. */
1636 if (METHOD_DUMMY (method))
1638 init = make_method_value (method);
1640 methods = tree_cons (NULL_TREE, init, methods);
1642 method_array_type = build_prim_array_type (method_type_node, method_count);
1643 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1645 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1646 nreverse (methods));
1647 TREE_STATIC (methods_decl) = 1;
1648 DECL_ARTIFICIAL (methods_decl) = 1;
1649 DECL_IGNORED_P (methods_decl) = 1;
1650 rest_of_decl_compilation (methods_decl, 1, 0);
1652 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1653 && !flag_indirect_dispatch)
1655 tree dtable = get_dispatch_table (type, this_class_addr);
1656 dtable_decl = build_dtable_decl (type);
1657 DECL_INITIAL (dtable_decl) = dtable;
1658 TREE_STATIC (dtable_decl) = 1;
1659 DECL_ARTIFICIAL (dtable_decl) = 1;
1660 DECL_IGNORED_P (dtable_decl) = 1;
1661 TREE_PUBLIC (dtable_decl) = 1;
1662 rest_of_decl_compilation (dtable_decl, 1, 0);
1663 if (type == class_type_node)
1664 class_dtable_decl = dtable_decl;
1667 if (class_dtable_decl == NULL_TREE)
1669 class_dtable_decl = build_dtable_decl (class_type_node);
1670 TREE_STATIC (class_dtable_decl) = 1;
1671 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1672 DECL_IGNORED_P (class_dtable_decl) = 1;
1673 if (is_compiled_class (class_type_node) != 2)
1674 DECL_EXTERNAL (class_dtable_decl) = 1;
1675 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1678 super = CLASSTYPE_SUPER (type);
1679 if (super == NULL_TREE)
1680 super = null_pointer_node;
1681 else if (! flag_indirect_dispatch
1682 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1683 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1684 super = build_class_ref (super);
1687 int super_index = alloc_class_constant (super);
1688 super = build_int_cst (ptr_type_node, super_index);
1691 /* Build and emit the array of implemented interfaces. */
1692 if (type != object_type_node)
1693 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1695 if (interface_len > 0)
1697 tree init = NULL_TREE;
1699 tree interface_array_type, idecl;
1700 interface_array_type
1701 = build_prim_array_type (class_ptr_type, interface_len);
1702 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1703 interface_array_type);
1705 for (i = interface_len; i > 0; i--)
1707 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1708 tree iclass = BINFO_TYPE (child);
1710 if (! flag_indirect_dispatch
1712 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1713 index = build_class_ref (iclass);
1716 int int_index = alloc_class_constant (iclass);
1717 index = build_int_cst (ptr_type_node, int_index);
1719 init = tree_cons (NULL_TREE, index, init);
1721 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1722 TREE_STATIC (idecl) = 1;
1723 DECL_ARTIFICIAL (idecl) = 1;
1724 DECL_IGNORED_P (idecl) = 1;
1725 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1726 rest_of_decl_compilation (idecl, 1, 0);
1729 constant_pool_constructor = build_constants_constructor ();
1731 if (flag_indirect_dispatch)
1733 TYPE_OTABLE_DECL (type)
1735 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1736 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1737 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1739 TYPE_ATABLE_DECL (type)
1741 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1742 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1743 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1745 TYPE_ITABLE_DECL (type)
1747 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1748 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1749 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1752 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1754 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1755 PUSH_FIELD_VALUE (temp, "vtable",
1756 build2 (PLUS_EXPR, dtable_ptr_type,
1757 build1 (ADDR_EXPR, dtable_ptr_type,
1759 dtable_start_offset));
1760 if (! flag_hash_synchronization)
1761 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1762 FINISH_RECORD_CONSTRUCTOR (temp);
1763 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1764 PUSH_SUPER_VALUE (cons, temp);
1765 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1766 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1767 PUSH_FIELD_VALUE (cons, "accflags",
1768 build_int_cst (NULL_TREE,
1769 get_access_flags_from_decl (type_decl)));
1771 PUSH_FIELD_VALUE (cons, "superclass",
1772 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1773 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1774 PUSH_FIELD_VALUE (cons, "methods",
1775 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1776 PUSH_FIELD_VALUE (cons, "method_count",
1777 build_int_cst (NULL_TREE, method_count));
1779 if (flag_indirect_dispatch)
1780 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1782 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1784 PUSH_FIELD_VALUE (cons, "fields",
1785 fields_decl == NULL_TREE ? null_pointer_node
1786 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1787 /* If we're using the binary compatibility ABI we don't know the
1788 size until load time. */
1789 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1790 (flag_indirect_dispatch
1791 ? integer_minus_one_node
1792 : size_in_bytes (type)));
1793 PUSH_FIELD_VALUE (cons, "field_count",
1794 build_int_cst (NULL_TREE, field_count));
1795 PUSH_FIELD_VALUE (cons, "static_field_count",
1796 build_int_cst (NULL_TREE, static_field_count));
1798 if (flag_indirect_dispatch)
1799 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1801 PUSH_FIELD_VALUE (cons, "vtable",
1802 dtable_decl == NULL_TREE ? null_pointer_node
1803 : build2 (PLUS_EXPR, dtable_ptr_type,
1804 build1 (ADDR_EXPR, dtable_ptr_type,
1806 dtable_start_offset));
1807 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1809 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1810 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1814 PUSH_FIELD_VALUE (cons, "otable",
1815 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1816 PUSH_FIELD_VALUE (cons, "otable_syms",
1817 build1 (ADDR_EXPR, symbols_array_ptr_type,
1818 TYPE_OTABLE_SYMS_DECL (type)));
1819 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1820 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1822 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1824 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1825 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1829 PUSH_FIELD_VALUE (cons, "atable",
1830 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1831 PUSH_FIELD_VALUE (cons, "atable_syms",
1832 build1 (ADDR_EXPR, symbols_array_ptr_type,
1833 TYPE_ATABLE_SYMS_DECL (type)));
1834 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1835 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1837 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1839 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1840 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1844 PUSH_FIELD_VALUE (cons, "itable",
1845 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1846 PUSH_FIELD_VALUE (cons, "itable_syms",
1847 build1 (ADDR_EXPR, symbols_array_ptr_type,
1848 TYPE_ITABLE_SYMS_DECL (type)));
1849 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1850 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1853 PUSH_FIELD_VALUE (cons, "catch_classes",
1854 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1855 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1856 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1857 PUSH_FIELD_VALUE (cons, "interface_count",
1858 build_int_cst (NULL_TREE, interface_len));
1861 convert (byte_type_node,
1862 build_int_cst (NULL_TREE,
1863 flag_indirect_dispatch
1864 ? JV_STATE_PRELOADING
1865 : JV_STATE_COMPILED)));
1867 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1868 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1869 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1870 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1871 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1872 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1875 tree assertion_table_ref;
1876 if (TYPE_ASSERTIONS (type) == NULL)
1877 assertion_table_ref = null_pointer_node;
1879 assertion_table_ref = build1 (ADDR_EXPR,
1880 build_pointer_type (assertion_table_type),
1881 emit_assertion_table (type));
1883 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1886 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1887 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1888 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1889 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1891 FINISH_RECORD_CONSTRUCTOR (cons);
1893 DECL_INITIAL (decl) = cons;
1895 /* Hash synchronization requires at least 64-bit alignment. */
1896 if (flag_hash_synchronization && POINTER_SIZE < 64)
1897 DECL_ALIGN (decl) = 64;
1899 rest_of_decl_compilation (decl, 1, 0);
1901 TYPE_OTABLE_DECL (type) = NULL_TREE;
1902 TYPE_ATABLE_DECL (type) = NULL_TREE;
1903 TYPE_CTABLE_DECL (type) = NULL_TREE;
1909 if (TYPE_VERIFY_METHOD (output_class))
1911 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1912 DECL_SAVED_TREE (verify_method)
1913 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1914 build (RETURN_EXPR, void_type_node, NULL));
1915 java_genericize (verify_method);
1916 cgraph_finalize_function (verify_method, false);
1917 TYPE_ASSERTIONS (current_class) = NULL;
1920 java_expand_catch_classes (current_class);
1922 current_function_decl = NULL_TREE;
1923 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
1924 make_class_data (current_class);
1926 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1929 /* Return 2 if CLASS is compiled by this compilation job;
1930 return 1 if CLASS can otherwise be assumed to be compiled;
1931 return 0 if we cannot assume that CLASS is compiled.
1932 Returns 1 for primitive and 0 for array types. */
1934 is_compiled_class (tree class)
1937 if (TREE_CODE (class) == POINTER_TYPE)
1938 class = TREE_TYPE (class);
1939 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1941 if (TYPE_ARRAY_P (class))
1943 if (class == current_class)
1946 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1947 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1949 /* The class was seen in the current ZIP file and will be
1950 available as a compiled class in the future but may not have
1951 been loaded already. Load it if necessary. This prevent
1952 build_class_ref () from crashing. */
1954 if (seen_in_zip && !CLASS_LOADED_P (class))
1955 load_class (class, 1);
1957 /* We return 2 for class seen in ZIP and class from files
1958 belonging to the same compilation unit */
1962 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1964 if (!CLASS_LOADED_P (class))
1966 if (CLASS_FROM_SOURCE_P (class))
1967 safe_layout_class (class);
1969 load_class (class, 1);
1977 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1980 build_dtable_decl (tree type)
1984 /* We need to build a new dtable type so that its size is uniquely
1985 computed when we're dealing with the class for real and not just
1986 faking it (like java.lang.Class during the initialization of the
1987 compiler.) We know we're not faking a class when CURRENT_CLASS is
1989 if (current_class == type)
1991 tree dummy = NULL_TREE;
1994 dtype = make_node (RECORD_TYPE);
1996 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1997 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1999 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
2000 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2002 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2003 TREE_CHAIN (dummy) = tmp_field;
2004 DECL_CONTEXT (tmp_field) = dtype;
2005 DECL_ARTIFICIAL (tmp_field) = 1;
2009 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2010 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2012 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2013 TREE_CHAIN (dummy) = tmp_field;
2014 DECL_CONTEXT (tmp_field) = dtype;
2015 DECL_ARTIFICIAL (tmp_field) = 1;
2019 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2020 if (TARGET_VTABLE_USES_DESCRIPTORS)
2021 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2023 PUSH_FIELD (dtype, dummy, "methods",
2024 build_prim_array_type (nativecode_ptr_type_node, n));
2025 layout_type (dtype);
2028 dtype = dtable_type;
2030 return build_decl (VAR_DECL,
2031 java_mangle_vtable (&temporary_obstack, type), dtype);
2034 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2035 fields inherited from SUPER_CLASS. */
2038 push_super_field (tree this_class, tree super_class)
2041 /* Don't insert the field if we're just re-laying the class out. */
2042 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2044 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2045 DECL_IGNORED_P (base_decl) = 1;
2046 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2047 TYPE_FIELDS (this_class) = base_decl;
2048 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2049 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2052 /* Handle the different manners we may have to lay out a super class. */
2055 maybe_layout_super_class (tree super_class, tree this_class)
2057 if (TREE_CODE (super_class) == RECORD_TYPE)
2059 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2060 safe_layout_class (super_class);
2061 if (!CLASS_LOADED_P (super_class))
2062 load_class (super_class, 1);
2064 /* We might have to layout the class before its dependency on
2065 the super class gets resolved by java_complete_class */
2066 else if (TREE_CODE (super_class) == POINTER_TYPE)
2068 if (TREE_TYPE (super_class) != NULL_TREE)
2069 super_class = TREE_TYPE (super_class);
2072 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2074 tree this_wrap = NULL_TREE;
2078 tree this_decl = TYPE_NAME (this_class);
2079 #ifdef USE_MAPPED_LOCATION
2080 this_wrap = build_expr_wfl (this_class,
2081 DECL_SOURCE_LOCATION (this_decl));
2083 this_wrap = build_expr_wfl (this_class,
2084 DECL_SOURCE_FILE (this_decl),
2085 DECL_SOURCE_LINE (this_decl), 0);
2088 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
2089 super_class, NULL_TREE, this_wrap);
2091 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2092 super_class = TREE_TYPE (super_class);
2095 if (!TYPE_SIZE (super_class))
2096 safe_layout_class (super_class);
2102 layout_class (tree this_class)
2104 tree super_class = CLASSTYPE_SUPER (this_class);
2107 class_list = tree_cons (this_class, NULL_TREE, class_list);
2108 if (CLASS_BEING_LAIDOUT (this_class))
2114 sprintf (buffer, " with '%s'",
2115 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2116 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2118 for (current = TREE_CHAIN (class_list); current;
2119 current = TREE_CHAIN (current))
2121 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2122 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2123 IDENTIFIER_POINTER (DECL_NAME (decl)),
2124 DECL_SOURCE_FILE (decl),
2125 DECL_SOURCE_LINE (decl));
2126 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2128 obstack_1grow (&temporary_obstack, '\0');
2129 report = obstack_finish (&temporary_obstack);
2130 cyclic_inheritance_report = ggc_strdup (report);
2131 obstack_free (&temporary_obstack, report);
2132 TYPE_SIZE (this_class) = error_mark_node;
2135 CLASS_BEING_LAIDOUT (this_class) = 1;
2137 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2139 tree maybe_super_class
2140 = maybe_layout_super_class (super_class, this_class);
2141 if (maybe_super_class == NULL
2142 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2144 TYPE_SIZE (this_class) = error_mark_node;
2145 CLASS_BEING_LAIDOUT (this_class) = 0;
2146 class_list = TREE_CHAIN (class_list);
2149 if (TYPE_SIZE (this_class) == NULL_TREE)
2150 push_super_field (this_class, maybe_super_class);
2153 for (field = TYPE_FIELDS (this_class);
2154 field != NULL_TREE; field = TREE_CHAIN (field))
2156 if (FIELD_STATIC (field))
2158 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2159 SET_DECL_ASSEMBLER_NAME (field,
2161 (&temporary_obstack, field));
2165 layout_type (this_class);
2167 /* Also recursively load/layout any superinterfaces, but only if
2168 class was loaded from bytecode. The source parser will take care
2170 if (!CLASS_FROM_SOURCE_P (this_class))
2173 if (TYPE_BINFO (this_class))
2175 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2177 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2178 tree super_interface = BINFO_TYPE (binfo);
2179 tree maybe_super_interface
2180 = maybe_layout_super_class (super_interface, NULL_TREE);
2181 if (maybe_super_interface == NULL
2182 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2184 TYPE_SIZE (this_class) = error_mark_node;
2185 CLASS_BEING_LAIDOUT (this_class) = 0;
2186 class_list = TREE_CHAIN (class_list);
2193 /* Convert the size back to an SI integer value. */
2194 TYPE_SIZE_UNIT (this_class) =
2195 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2197 CLASS_BEING_LAIDOUT (this_class) = 0;
2198 class_list = TREE_CHAIN (class_list);
2202 add_miranda_methods (tree base_class, tree search_class)
2205 tree binfo, base_binfo;
2207 if (!CLASS_PARSED_P (search_class))
2208 load_class (search_class, 1);
2210 for (binfo = TYPE_BINFO (search_class), i = 1;
2211 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2214 tree elt = BINFO_TYPE (base_binfo);
2216 /* FIXME: This is totally bogus. We should not be handling
2217 Miranda methods at all if we're using the BC ABI. */
2218 if (TYPE_DUMMY (elt))
2221 /* Ensure that interface methods are seen in declared order. */
2222 if (!CLASS_LOADED_P (elt))
2223 load_class (elt, 1);
2224 layout_class_methods (elt);
2226 /* All base classes will have been laid out at this point, so the order
2227 will be correct. This code must match similar layout code in the
2229 for (method_decl = TYPE_METHODS (elt);
2230 method_decl; method_decl = TREE_CHAIN (method_decl))
2234 /* An interface can have <clinit>. */
2235 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2238 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2239 override = lookup_argument_method (base_class,
2240 DECL_NAME (method_decl), sig);
2241 if (override == NULL_TREE)
2243 /* Found a Miranda method. Add it. */
2245 sig = build_java_signature (TREE_TYPE (method_decl));
2247 = add_method (base_class,
2248 get_access_flags_from_decl (method_decl),
2249 DECL_NAME (method_decl), sig);
2250 METHOD_INVISIBLE (new_method) = 1;
2254 /* Try superinterfaces. */
2255 add_miranda_methods (base_class, elt);
2260 layout_class_methods (tree this_class)
2262 tree method_decl, dtable_count;
2263 tree super_class, type_name;
2265 if (TYPE_NVIRTUALS (this_class))
2268 super_class = CLASSTYPE_SUPER (this_class);
2272 super_class = maybe_layout_super_class (super_class, this_class);
2273 if (!TYPE_NVIRTUALS (super_class))
2274 layout_class_methods (super_class);
2275 dtable_count = TYPE_NVIRTUALS (super_class);
2278 dtable_count = integer_zero_node;
2280 type_name = TYPE_NAME (this_class);
2281 if (!flag_indirect_dispatch
2282 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2284 /* An abstract class can have methods which are declared only in
2285 an implemented interface. These are called "Miranda
2286 methods". We make a dummy method entry for such methods
2288 add_miranda_methods (this_class, this_class);
2291 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2293 for (method_decl = TYPE_METHODS (this_class);
2294 method_decl; method_decl = TREE_CHAIN (method_decl))
2295 dtable_count = layout_class_method (this_class, super_class,
2296 method_decl, dtable_count);
2298 TYPE_NVIRTUALS (this_class) = dtable_count;
2301 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2302 argument for _Jv_LookupInterfaceMethodIdx(). */
2304 get_interface_method_index (tree method, tree interface)
2309 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2313 if (meth == NULL_TREE)
2318 /* Lay METHOD_DECL out, returning a possibly new value of
2319 DTABLE_COUNT. Also mangle the method's name. */
2322 layout_class_method (tree this_class, tree super_class,
2323 tree method_decl, tree dtable_count)
2325 tree method_name = DECL_NAME (method_decl);
2327 TREE_PUBLIC (method_decl) = 1;
2328 /* Considered external until we know what classes are being
2329 compiled into this object file. */
2330 DECL_EXTERNAL (method_decl) = 1;
2332 /* This is a good occasion to mangle the method's name */
2333 SET_DECL_ASSEMBLER_NAME (method_decl,
2334 java_mangle_decl (&temporary_obstack,
2336 /* We don't generate a RTL for the method if it's abstract, or if
2337 it's an interface method that isn't clinit. */
2338 if (! METHOD_ABSTRACT (method_decl)
2339 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2340 && (DECL_CLINIT_P (method_decl))))
2341 make_decl_rtl (method_decl);
2343 if (ID_INIT_P (method_name))
2345 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2347 for (ptr = p; *ptr; )
2352 DECL_CONSTRUCTOR_P (method_decl) = 1;
2353 build_java_argument_signature (TREE_TYPE (method_decl));
2355 else if (! METHOD_STATIC (method_decl))
2358 build_java_argument_signature (TREE_TYPE (method_decl));
2359 bool method_override = false;
2360 tree super_method = lookup_argument_method (super_class, method_name,
2362 if (super_method != NULL_TREE
2363 && ! METHOD_DUMMY (super_method))
2365 method_override = true;
2366 if (! METHOD_PUBLIC (super_method) &&
2367 ! METHOD_PROTECTED (super_method))
2369 /* Don't override private method, or default-access method in
2371 if (METHOD_PRIVATE (super_method) ||
2372 ! in_same_package (TYPE_NAME (this_class),
2373 TYPE_NAME (super_class)))
2374 method_override = false;
2377 if (method_override)
2379 tree method_index = get_method_index (super_method);
2380 set_method_index (method_decl, method_index);
2381 if (method_index == NULL_TREE
2382 && ! flag_indirect_dispatch
2383 && !CLASS_FROM_SOURCE_P (this_class)
2384 && ! DECL_ARTIFICIAL (super_method))
2385 error ("%Jnon-static method '%D' overrides static method",
2386 method_decl, method_decl);
2388 else if (this_class == object_type_node
2389 && (METHOD_FINAL (method_decl)
2390 || METHOD_PRIVATE (method_decl)))
2392 /* We don't generate vtable entries for final Object
2393 methods. This is simply to save space, since every
2394 object would otherwise have to define them. */
2396 else if (! METHOD_PRIVATE (method_decl)
2399 /* We generate vtable entries for final methods because they
2400 may one day be changed to non-final. */
2401 set_method_index (method_decl, dtable_count);
2402 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2403 dtable_count, integer_one_node));
2407 return dtable_count;
2411 register_class (void)
2413 /* END does not need to be registered with the garbage collector
2414 because it always points into the list given by REGISTERED_CLASS,
2415 and that variable is registered with the collector. */
2417 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2418 tree current = copy_node (node);
2420 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2421 if (!registered_class)
2422 registered_class = current;
2424 TREE_CHAIN (end) = current;
2429 /* Emit something to register classes at start-up time.
2431 The preferred mechanism is through the .jcr section, which contain
2432 a list of pointers to classes which get registered during constructor
2435 The fallback mechanism is to add statements to *LIST_P to call
2436 _Jv_RegisterClass for each class in this file. These statements will
2437 be added to a static constructor function for this translation unit. */
2440 emit_register_classes (tree *list_p)
2442 if (registered_class == NULL)
2445 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2446 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2447 but lack suitable crtbegin/end objects or linker support. These
2448 targets can overide the default in tm.h to use the fallback mechanism. */
2449 if (TARGET_USE_JCR_SECTION)
2451 #ifdef JCR_SECTION_NAME
2453 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2454 assemble_align (POINTER_SIZE);
2455 for (t = registered_class; t; t = TREE_CHAIN (t))
2457 mark_decl_referenced (t);
2458 assemble_integer (XEXP (DECL_RTL (t), 0),
2459 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2462 /* A target has defined TARGET_USE_JCR_SECTION, but doesn't have a
2463 JCR_SECTION_NAME. */
2469 tree klass, t, register_class_fn;
2471 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2472 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2473 TREE_PUBLIC (t) = 1;
2474 DECL_EXTERNAL (t) = 1;
2475 register_class_fn = t;
2477 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2479 t = build_fold_addr_expr (klass);
2480 t = tree_cons (NULL, t, NULL);
2481 t = build_function_call_expr (register_class_fn, t);
2482 append_to_statement_list (t, list_p);
2487 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2490 build_symbol_entry (tree decl)
2492 tree clname, name, signature, sym;
2493 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2494 /* ??? Constructors are given the name foo.foo all the way through
2495 the compiler, but in the method table they're all renamed
2496 foo.<init>. So, we have to do the same here unless we want an
2497 unresolved reference at runtime. */
2498 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2499 && DECL_CONSTRUCTOR_P (decl))
2500 ? init_identifier_node
2501 : DECL_NAME (decl));
2502 signature = build_java_signature (TREE_TYPE (decl));
2503 signature = build_utf8_ref (unmangle_classname
2504 (IDENTIFIER_POINTER (signature),
2505 IDENTIFIER_LENGTH (signature)));
2507 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2508 PUSH_FIELD_VALUE (sym, "clname", clname);
2509 PUSH_FIELD_VALUE (sym, "name", name);
2510 PUSH_FIELD_VALUE (sym, "signature", signature);
2511 FINISH_RECORD_CONSTRUCTOR (sym);
2512 TREE_CONSTANT (sym) = 1;
2513 TREE_INVARIANT (sym) = 1;
2518 /* Emit a symbol table: used by -findirect-dispatch. */
2521 emit_symbol_table (tree name, tree the_table, tree decl_list,
2522 tree the_syms_decl, tree the_array_element_type,
2525 tree method_list, method, table, list, null_symbol;
2526 tree table_size, the_array_type;
2529 /* Only emit a table if this translation unit actually made any
2530 references via it. */
2531 if (decl_list == NULL_TREE)
2534 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2536 method_list = decl_list;
2538 while (method_list != NULL_TREE)
2540 method = TREE_VALUE (method_list);
2541 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2542 method_list = TREE_CHAIN (method_list);
2546 /* Terminate the list with a "null" entry. */
2547 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2548 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2549 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2550 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2551 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2552 TREE_CONSTANT (null_symbol) = 1;
2553 TREE_INVARIANT (null_symbol) = 1;
2554 list = tree_cons (NULL_TREE, null_symbol, list);
2556 /* Put the list in the right order and make it a constructor. */
2557 list = nreverse (list);
2558 table = build_constructor (symbols_array_type, list);
2560 /* Make it the initial value for otable_syms and emit the decl. */
2561 DECL_INITIAL (the_syms_decl) = table;
2562 DECL_ARTIFICIAL (the_syms_decl) = 1;
2563 DECL_IGNORED_P (the_syms_decl) = 1;
2564 rest_of_decl_compilation (the_syms_decl, 1, 0);
2566 /* Now that its size is known, redefine the table as an
2567 uninitialized static array of INDEX + 1 elements. The extra entry
2568 is used by the runtime to track whether the table has been
2571 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2572 the_array_type = build_array_type (the_array_element_type, table_size);
2573 the_table = build_decl (VAR_DECL, name, the_array_type);
2574 TREE_STATIC (the_table) = 1;
2575 TREE_READONLY (the_table) = 1;
2576 rest_of_decl_compilation (the_table, 1, 0);
2581 /* Make an entry for the catch_classes list. */
2583 make_catch_class_record (tree catch_class, tree classname)
2586 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2587 START_RECORD_CONSTRUCTOR (entry, type);
2588 PUSH_FIELD_VALUE (entry, "address", catch_class);
2589 PUSH_FIELD_VALUE (entry, "classname", classname);
2590 FINISH_RECORD_CONSTRUCTOR (entry);
2595 /* Generate the list of Throwable classes that are caught by exception
2596 handlers in this class. */
2598 emit_catch_table (tree this_class)
2600 tree table, table_size, array_type;
2601 TYPE_CATCH_CLASSES (this_class) =
2603 make_catch_class_record (null_pointer_node, null_pointer_node),
2604 TYPE_CATCH_CLASSES (this_class));
2605 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2606 TYPE_CATCH_CLASSES (this_class) =
2608 make_catch_class_record (null_pointer_node, null_pointer_node),
2609 TYPE_CATCH_CLASSES (this_class));
2610 table_size = build_index_type
2611 (build_int_cst (NULL_TREE,
2612 list_length (TYPE_CATCH_CLASSES (this_class))));
2614 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2617 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2618 DECL_INITIAL (table) =
2619 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2620 TREE_STATIC (table) = 1;
2621 TREE_READONLY (table) = 1;
2622 DECL_IGNORED_P (table) = 1;
2623 rest_of_decl_compilation (table, 1, 0);
2627 /* Given a type, return the signature used by
2628 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2629 same as build_java_signature() because we want the canonical array
2633 build_signature_for_libgcj (tree type)
2637 sig = build_java_signature (type);
2638 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2639 IDENTIFIER_LENGTH (sig)));
2643 /* Add an entry to the type assertion table. Callback used during hashtable
2647 add_assertion_table_entry (void **htab_entry, void *ptr)
2650 tree code_val, op1_utf8, op2_utf8;
2651 tree *list = (tree *) ptr;
2652 type_assertion *as = (type_assertion *) *htab_entry;
2654 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2656 if (as->op1 == NULL_TREE)
2657 op1_utf8 = null_pointer_node;
2659 op1_utf8 = build_signature_for_libgcj (as->op1);
2661 if (as->op2 == NULL_TREE)
2662 op2_utf8 = null_pointer_node;
2664 op2_utf8 = build_signature_for_libgcj (as->op2);
2666 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2667 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2668 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2669 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2670 FINISH_RECORD_CONSTRUCTOR (entry);
2672 *list = tree_cons (NULL_TREE, entry, *list);
2676 /* Generate the type assertion table for CLASS, and return its DECL. */
2679 emit_assertion_table (tree class)
2681 tree null_entry, ctor, table_decl;
2682 tree list = NULL_TREE;
2683 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2685 /* Iterate through the hash table. */
2686 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2688 /* Finish with a null entry. */
2689 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2690 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2691 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2692 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2693 FINISH_RECORD_CONSTRUCTOR (null_entry);
2695 list = tree_cons (NULL_TREE, null_entry, list);
2697 /* Put the list in the right order and make it a constructor. */
2698 list = nreverse (list);
2699 ctor = build_constructor (assertion_table_type, list);
2701 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2702 assertion_table_type);
2704 TREE_STATIC (table_decl) = 1;
2705 TREE_READONLY (table_decl) = 1;
2706 TREE_CONSTANT (table_decl) = 1;
2707 DECL_IGNORED_P (table_decl) = 1;
2709 DECL_INITIAL (table_decl) = ctor;
2710 DECL_ARTIFICIAL (table_decl) = 1;
2711 rest_of_decl_compilation (table_decl, 1, 0);
2717 init_class_processing (void)
2719 fields_ident = get_identifier ("fields");
2720 info_ident = get_identifier ("info");
2722 gcc_obstack_init (&temporary_obstack);
2725 static hashval_t java_treetreehash_hash (const void *);
2726 static int java_treetreehash_compare (const void *, const void *);
2728 /* A hash table mapping trees to trees. Used generally. */
2730 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2733 java_treetreehash_hash (const void *k_p)
2735 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2736 return JAVA_TREEHASHHASH_H (k->key);
2740 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2742 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2743 tree k2 = (tree) k2_p;
2744 return (k1->key == k2);
2748 java_treetreehash_find (htab_t ht, tree t)
2750 struct treetreehash_entry *e;
2751 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2752 e = htab_find_with_hash (ht, t, hv);
2760 java_treetreehash_new (htab_t ht, tree t)
2763 struct treetreehash_entry *tthe;
2764 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2766 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2769 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2774 tthe = (struct treetreehash_entry *) *e;
2775 return &tthe->value;
2779 java_treetreehash_create (size_t size, int gc)
2782 return htab_create_ggc (size, java_treetreehash_hash,
2783 java_treetreehash_compare, NULL);
2785 return htab_create_alloc (size, java_treetreehash_hash,
2786 java_treetreehash_compare, free, xcalloc, free);
2789 /* Break down qualified IDENTIFIER into package and class-name components.
2790 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2791 "pkg.foo", and RIGHT to "Bar". */
2794 split_qualified_name (tree *left, tree *right, tree source)
2797 int l = IDENTIFIER_LENGTH (source);
2799 base = alloca (l + 1);
2800 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2802 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2804 while (*p != '.' && p != base)
2807 /* We didn't find a '.'. Return an error. */
2813 *right = get_identifier (p+1);
2814 *left = get_identifier (base);
2819 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2820 if the classes are from the same package. */
2823 in_same_package (tree name1, tree name2)
2829 if (TREE_CODE (name1) == TYPE_DECL)
2830 name1 = DECL_NAME (name1);
2831 if (TREE_CODE (name2) == TYPE_DECL)
2832 name2 = DECL_NAME (name2);
2834 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2835 /* One in empty package. */
2838 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2839 /* Both in empty package. */
2842 split_qualified_name (&pkg1, &tmp, name1);
2843 split_qualified_name (&pkg2, &tmp, name2);
2845 return (pkg1 == pkg2);
2848 #include "gt-java-class.h"