1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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, tree);
66 static tree emit_assertion_table (tree);
67 static void register_class (void);
69 struct obstack temporary_obstack;
71 /* The compiler generates different code depending on whether or not
72 it can assume certain classes have been compiled down to native
73 code or not. The compiler options -fassume-compiled= and
74 -fno-assume-compiled= are used to create a tree of
75 class_flag_node objects. This tree is queried to determine if
76 a class is assume to be compiled or not. Each node in the tree
77 represents either a package or a specific class. */
79 typedef struct class_flag_node_struct
81 /* The class or package name. */
84 /* Nonzero if this represents an exclusion. */
87 /* Pointers to other nodes in the tree. */
88 struct class_flag_node_struct *parent;
89 struct class_flag_node_struct *sibling;
90 struct class_flag_node_struct *child;
93 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
94 static void add_class_flag (class_flag_node **, const char *, int);
96 /* This is the root of the include/exclude tree. */
98 static class_flag_node *assume_compiled_tree;
100 static class_flag_node *enable_assert_tree;
102 static GTY(()) tree class_roots[4];
103 #define fields_ident class_roots[0] /* get_identifier ("fields") */
104 #define info_ident class_roots[1] /* get_identifier ("info") */
105 #define class_list class_roots[2]
106 #define class_dtable_decl class_roots[3]
108 static GTY(()) VEC(tree,gc) *registered_class;
110 /* Return the node that most closely represents the class whose name
111 is IDENT. Start the search from NODE (followed by its siblings).
112 Return NULL if an appropriate node does not exist. */
114 static class_flag_node *
115 find_class_flag_node (class_flag_node *node, const char *ident)
119 size_t node_ident_length = strlen (node->ident);
121 /* node_ident_length is zero at the root of the tree. If the
122 identifiers are the same length, then we have matching
123 classes. Otherwise check if we've matched an enclosing
126 if (node_ident_length == 0
127 || (strncmp (ident, node->ident, node_ident_length) == 0
128 && (ident[node_ident_length] == '\0'
129 || ident[node_ident_length] == '.')))
131 /* We've found a match, however, there might be a more
134 class_flag_node *found = find_class_flag_node (node->child, ident);
141 /* No match yet. Continue through the sibling list. */
142 node = node->sibling;
145 /* No match at all in this tree. */
150 add_class_flag (class_flag_node **rootp, const char *ident, int value)
152 class_flag_node *root = *rootp;
153 class_flag_node *parent, *node;
155 /* Create the root of the tree if it doesn't exist yet. */
159 root = XNEW (class_flag_node);
162 root->sibling = NULL;
168 /* Calling the function with the empty string means we're setting
169 value for the root of the hierarchy. */
177 /* Find the parent node for this new node. PARENT will either be a
178 class or a package name. Adjust PARENT accordingly. */
180 parent = find_class_flag_node (root, ident);
181 if (strcmp (ident, parent->ident) == 0)
182 parent->value = value;
185 /* Insert new node into the tree. */
186 node = XNEW (class_flag_node);
188 node->ident = xstrdup (ident);
192 node->parent = parent;
193 node->sibling = parent->child;
194 parent->child = node;
198 /* Add a new IDENT to the include/exclude tree. It's an exclusion
199 if EXCLUDEP is nonzero. */
202 add_assume_compiled (const char *ident, int excludep)
204 add_class_flag (&assume_compiled_tree, ident, excludep);
207 /* The default value returned by enable_assertions. */
209 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
211 /* Enter IDENT (a class or package name) into the enable-assertions table.
212 VALUE is true to enable and false to disable. */
215 add_enable_assert (const char *ident, int value)
217 if (enable_assert_tree == NULL)
218 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
219 add_class_flag (&enable_assert_tree, ident, value);
222 /* Returns nonzero if IDENT is the name of a class that the compiler
223 should assume has been compiled to object code. */
226 assume_compiled (const char *ident)
231 if (NULL == assume_compiled_tree)
234 i = find_class_flag_node (assume_compiled_tree, ident);
241 /* Return true if we should generate code to check assertions within KLASS. */
244 enable_assertions (tree klass)
246 /* Check if command-line specifies whether we should check assertions. */
248 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
250 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
251 class_flag_node *node
252 = find_class_flag_node (enable_assert_tree, ident);
256 /* The default is to enable assertions if generating class files,
257 or not optimizing. */
258 return DEFAULT_ENABLE_ASSERT;
261 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
262 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
263 Also, PREFIX is prepended, and SUFFIX is appended. */
266 ident_subst (const char* old_name,
273 int prefix_len = strlen (prefix);
274 int suffix_len = strlen (suffix);
275 int i = prefix_len + old_length + suffix_len + 1;
276 char *buffer = alloca (i);
278 strcpy (buffer, prefix);
279 for (i = 0; i < old_length; i++)
281 char ch = old_name[i];
284 buffer[prefix_len + i] = ch;
286 strcpy (buffer + prefix_len + old_length, suffix);
287 return get_identifier (buffer);
290 /* Return an IDENTIFIER_NODE the same as OLD_ID,
291 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
292 Also, PREFIX is prepended, and SUFFIX is appended. */
295 identifier_subst (const tree old_id,
301 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
302 prefix, old_char, new_char, suffix);
305 /* Generate a valid C identifier from the name of the class TYPE,
306 prefixed by PREFIX. */
309 mangled_classname (const char *prefix, tree type)
311 tree ident = TYPE_NAME (type);
312 if (TREE_CODE (ident) != IDENTIFIER_NODE)
313 ident = DECL_NAME (ident);
314 return identifier_subst (ident, prefix, '.', '_', "");
321 type = make_node (RECORD_TYPE);
322 /* Unfortunately we must create the binfo here, so that class
324 TYPE_BINFO (type) = make_tree_binfo (0);
325 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
330 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
331 and where each of the constituents is separated by '/',
332 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
335 unmangle_classname (const char *name, int name_length)
337 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
338 /* It's not sufficient to compare to_return and get_identifier
339 (name) to determine whether to_return is qualified. There are
340 cases in signature analysis where name will be stripped of a
342 name = IDENTIFIER_POINTER (to_return);
346 QUALIFIED_P (to_return) = 1;
353 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
356 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
357 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
360 sprintf (buf, #NAME "_%s", typename); \
361 TYPE_## TABLE ##_DECL (type) = decl = \
362 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
363 DECL_EXTERNAL (decl) = 1; \
364 TREE_STATIC (decl) = 1; \
365 TREE_READONLY (decl) = 1; \
366 TREE_CONSTANT (decl) = 1; \
367 DECL_IGNORED_P (decl) = 1; \
368 /* Mark the table as belonging to this class. */ \
370 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
371 DECL_OWNER (decl) = TYPE; \
372 sprintf (buf, #NAME "_syms_%s", typename); \
373 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
374 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
375 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
376 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
377 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
381 /* Given a class, create the DECLs for all its associated indirect
384 gen_indirect_dispatch_tables (tree type)
386 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
389 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
390 tree catch_class_type = make_node (RECORD_TYPE);
392 sprintf (buf, "_catch_classes_%s", typename);
393 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
394 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
395 FINISH_RECORD (catch_class_type);
397 TYPE_CTABLE_DECL (type)
398 = build_decl (VAR_DECL, get_identifier (buf),
399 build_array_type (catch_class_type, 0));
400 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
401 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
402 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
403 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
404 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
405 pushdecl (TYPE_CTABLE_DECL (type));
408 if (flag_indirect_dispatch)
410 GEN_TABLE (ATABLE, _atable, atable_type, type);
411 GEN_TABLE (OTABLE, _otable, otable_type, type);
412 GEN_TABLE (ITABLE, _itable, itable_type, type);
419 push_class (tree class_type, tree class_name)
421 tree decl, signature;
422 location_t saved_loc = input_location;
423 #ifndef USE_MAPPED_LOCATION
424 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
425 input_filename = IDENTIFIER_POINTER (source_name);
428 CLASS_P (class_type) = 1;
429 decl = build_decl (TYPE_DECL, class_name, class_type);
430 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
432 /* dbxout needs a DECL_SIZE if in gstabs mode */
433 DECL_SIZE (decl) = integer_zero_node;
435 input_location = saved_loc;
436 signature = identifier_subst (class_name, "L", '.', '/', ";");
437 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
439 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
440 both a typedef and in the struct name-space. We may want to re-visit
441 this later, but for now it reduces the changes needed for gdb. */
442 DECL_ARTIFICIAL (decl) = 1;
444 pushdecl_top_level (decl);
449 /* Finds the (global) class named NAME. Creates the class if not found.
450 Also creates associated TYPE_DECL.
451 Does not check if the class actually exists, load the class,
452 fill in field or methods, or do layout_type. */
455 lookup_class (tree name)
457 tree decl = IDENTIFIER_CLASS_VALUE (name);
458 if (decl == NULL_TREE)
459 decl = push_class (make_class (), name);
460 return TREE_TYPE (decl);
464 set_super_info (int access_flags, tree this_class,
465 tree super_class, int interfaces_count)
467 int total_supers = interfaces_count;
468 tree class_decl = TYPE_NAME (this_class);
474 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
475 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
478 tree super_binfo = make_tree_binfo (0);
479 BINFO_TYPE (super_binfo) = super_class;
480 BINFO_OFFSET (super_binfo) = integer_zero_node;
481 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
482 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
485 set_class_decl_access_flags (access_flags, class_decl);
489 set_class_decl_access_flags (int access_flags, tree class_decl)
491 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
492 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
493 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
494 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
495 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
496 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
497 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
498 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
499 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
502 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
503 direct sub-classes of Object are 1, and so on. */
506 class_depth (tree clas)
509 if (! CLASS_LOADED_P (clas))
510 load_class (clas, 1);
511 if (TYPE_SIZE (clas) == error_mark_node)
513 while (clas != object_type_node)
516 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
521 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
524 interface_of_p (tree type1, tree type2)
527 tree binfo, base_binfo;
529 if (! TYPE_BINFO (type2))
532 for (binfo = TYPE_BINFO (type2), i = 0;
533 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
534 if (BINFO_TYPE (base_binfo) == type1)
537 for (binfo = TYPE_BINFO (type2), i = 0;
538 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
539 if (BINFO_TYPE (base_binfo)
540 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
546 /* Return true iff TYPE1 inherits from TYPE2. */
549 inherits_from_p (tree type1, tree type2)
551 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
556 if (! CLASS_LOADED_P (type1))
557 load_class (type1, 1);
559 type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
564 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
567 enclosing_context_p (tree type1, tree type2)
569 if (!INNER_CLASS_TYPE_P (type2))
572 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
574 type2 = (INNER_CLASS_TYPE_P (type2) ?
575 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
585 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
589 common_enclosing_context_p (tree type1, tree type2)
594 for (current = type2; current;
595 current = (INNER_CLASS_TYPE_P (current) ?
596 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
598 if (type1 == current)
601 if (INNER_CLASS_TYPE_P (type1))
602 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
609 /* Return 1 iff there exists a common enclosing "this" between TYPE1
610 and TYPE2, without crossing any static context. */
613 common_enclosing_instance_p (tree type1, tree type2)
615 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
618 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
619 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
620 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
623 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
624 current = (PURE_INNER_CLASS_TYPE_P (current) ?
625 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
627 if (type1 == current)
633 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
634 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
635 if attempt is made to add it twice. */
638 maybe_add_interface (tree this_class, tree interface_class)
640 tree binfo, base_binfo;
643 for (binfo = TYPE_BINFO (this_class), i = 0;
644 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
645 if (BINFO_TYPE (base_binfo) == interface_class)
646 return interface_class;
647 add_interface (this_class, interface_class);
651 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
654 add_interface (tree this_class, tree interface_class)
656 tree interface_binfo = make_tree_binfo (0);
658 BINFO_TYPE (interface_binfo) = interface_class;
659 BINFO_OFFSET (interface_binfo) = integer_zero_node;
660 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
661 BINFO_VIRTUAL_P (interface_binfo) = 1;
663 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
667 /* Return the address of a pointer to the first FUNCTION_DECL
668 in the list (*LIST) whose DECL_NAME is NAME. */
671 find_named_method (tree *list, tree name)
673 while (*list && DECL_NAME (*list) != name)
674 list = &TREE_CHAIN (*list);
680 build_java_method_type (tree fntype, tree this_class, int access_flags)
682 if (access_flags & ACC_STATIC)
684 fntype = build_method_type (this_class, fntype);
686 /* We know that arg 1 of every nonstatic method is non-null; tell
688 TYPE_ATTRIBUTES (fntype) = (tree_cons
689 (get_identifier ("nonnull"),
690 tree_cons (NULL_TREE,
691 build_int_cst (NULL_TREE, 1),
693 TYPE_ATTRIBUTES (fntype)));
698 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
700 tree method_type, fndecl;
702 method_type = build_java_method_type (function_type,
703 this_class, access_flags);
705 fndecl = build_decl (FUNCTION_DECL, name, method_type);
706 DECL_CONTEXT (fndecl) = this_class;
708 DECL_LANG_SPECIFIC (fndecl)
709 = ggc_alloc_cleared (sizeof (struct lang_decl));
710 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
712 /* Initialize the static initializer test table. */
714 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
715 java_treetreehash_create (10, 1);
717 /* Initialize the initialized (static) class table. */
718 if (access_flags & ACC_STATIC)
719 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
720 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
722 /* Initialize the static method invocation compound list */
723 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
725 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
726 TYPE_METHODS (this_class) = fndecl;
728 /* Notice that this is a finalizer and update the class type
729 accordingly. This is used to optimize instance allocation. */
730 if (name == finalize_identifier_node
731 && TREE_TYPE (function_type) == void_type_node
732 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
733 HAS_FINALIZER_P (this_class) = 1;
735 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
736 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
737 if (access_flags & ACC_PRIVATE)
738 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
739 if (access_flags & ACC_NATIVE)
741 METHOD_NATIVE (fndecl) = 1;
742 DECL_EXTERNAL (fndecl) = 1;
744 if (access_flags & ACC_STATIC)
745 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
746 if (access_flags & ACC_FINAL)
747 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
748 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
749 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
750 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
754 /* Add a method to THIS_CLASS.
755 The method's name is NAME.
756 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
759 add_method (tree this_class, int access_flags, tree name, tree method_sig)
761 tree function_type, fndecl;
762 const unsigned char *sig
763 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
766 fatal_error ("bad method signature");
768 function_type = get_type_from_signature (method_sig);
769 fndecl = add_method_1 (this_class, access_flags, name, function_type);
770 set_java_signature (TREE_TYPE (fndecl), method_sig);
775 add_field (tree class, tree name, tree field_type, int flags)
777 int is_static = (flags & ACC_STATIC) != 0;
779 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
780 TREE_CHAIN (field) = TYPE_FIELDS (class);
781 TYPE_FIELDS (class) = field;
782 DECL_CONTEXT (field) = class;
784 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
785 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
786 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
787 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
788 if (flags & ACC_VOLATILE)
790 FIELD_VOLATILE (field) = 1;
791 TREE_THIS_VOLATILE (field) = 1;
793 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
796 FIELD_STATIC (field) = 1;
797 /* Always make field externally visible. This is required so
798 that native methods can always access the field. */
799 TREE_PUBLIC (field) = 1;
800 /* Considered external until we know what classes are being
801 compiled into this object file. */
802 DECL_EXTERNAL (field) = 1;
808 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
811 set_constant_value (tree field, tree constant)
813 if (field == NULL_TREE)
814 warning (OPT_Wattributes,
815 "misplaced ConstantValue attribute (not in any field)");
816 else if (DECL_INITIAL (field) != NULL_TREE)
817 warning (OPT_Wattributes,
818 "duplicate ConstantValue attribute for field '%s'",
819 IDENTIFIER_POINTER (DECL_NAME (field)));
822 DECL_INITIAL (field) = constant;
823 if (TREE_TYPE (constant) != TREE_TYPE (field)
824 && ! (TREE_TYPE (constant) == int_type_node
825 && INTEGRAL_TYPE_P (TREE_TYPE (field))
826 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
827 && ! (TREE_TYPE (constant) == utf8const_ptr_type
828 && TREE_TYPE (field) == string_ptr_type_node))
829 error ("ConstantValue attribute of field '%s' has wrong type",
830 IDENTIFIER_POINTER (DECL_NAME (field)));
831 if (FIELD_FINAL (field))
832 DECL_FIELD_FINAL_IUD (field) = 1;
836 /* Count the number of Unicode chars encoded in a given Ut8 string. */
840 strLengthUtf8 (char *str, int len)
842 register unsigned char* ptr = (unsigned char*) str;
843 register unsigned char *limit = ptr + len;
845 for (; ptr < limit; str_length++) {
846 if (UTF8_GET (ptr, limit) < 0)
854 /* Calculate a hash value for a string encoded in Utf8 format.
855 * This returns the same hash value as specified for java.lang.String.hashCode.
859 hashUtf8String (const char *str, int len)
861 const unsigned char* ptr = (const unsigned char*) str;
862 const unsigned char *limit = ptr + len;
866 int ch = UTF8_GET (ptr, limit);
867 /* Updated specification from
868 http://www.javasoft.com/docs/books/jls/clarify.html. */
869 hash = (31 * hash) + ch;
874 static GTY(()) tree utf8_decl_list = NULL_TREE;
877 build_utf8_ref (tree name)
879 const char * name_ptr = IDENTIFIER_POINTER(name);
880 int name_len = IDENTIFIER_LENGTH(name);
882 tree ctype, field = NULL_TREE, str_type, cinit, string;
883 static int utf8_count = 0;
885 tree ref = IDENTIFIER_UTF8_REF (name);
887 if (ref != NULL_TREE)
890 ctype = make_node (RECORD_TYPE);
891 str_type = build_prim_array_type (unsigned_byte_type_node,
892 name_len + 1); /* Allow for final '\0'. */
893 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
894 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
895 PUSH_FIELD (ctype, field, "data", str_type);
896 FINISH_RECORD (ctype);
897 START_RECORD_CONSTRUCTOR (cinit, ctype);
898 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
899 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
900 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
901 string = build_string (name_len, name_ptr);
902 TREE_TYPE (string) = str_type;
903 PUSH_FIELD_VALUE (cinit, "data", string);
904 FINISH_RECORD_CONSTRUCTOR (cinit);
905 TREE_CONSTANT (cinit) = 1;
906 TREE_INVARIANT (cinit) = 1;
908 /* Generate a unique-enough identifier. */
909 sprintf(buf, "_Utf%d", ++utf8_count);
911 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
912 TREE_STATIC (decl) = 1;
913 DECL_ARTIFICIAL (decl) = 1;
914 DECL_IGNORED_P (decl) = 1;
915 TREE_READONLY (decl) = 1;
916 TREE_THIS_VOLATILE (decl) = 0;
917 DECL_INITIAL (decl) = cinit;
919 if (HAVE_GAS_SHF_MERGE)
922 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
923 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
924 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
925 if (flag_merge_constants && decl_size < 256)
928 int flags = (SECTION_OVERRIDE
929 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
930 sprintf (buf, ".rodata.jutf8.%d", decl_size);
931 switch_to_section (get_section (buf, flags, NULL));
932 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
936 TREE_CHAIN (decl) = utf8_decl_list;
937 layout_decl (decl, 0);
939 rest_of_decl_compilation (decl, global_bindings_p (), 0);
940 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
941 utf8_decl_list = decl;
942 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
943 IDENTIFIER_UTF8_REF (name) = ref;
947 /* Like build_class_ref, but instead of a direct reference generate a
948 pointer into the constant pool. */
951 build_indirect_class_ref (tree type)
955 index = alloc_class_constant (type);
956 cl = build_ref_from_constant_pool (index);
957 return convert (promote_type (class_ptr_type), cl);
961 build_static_class_ref (tree type)
963 tree decl_name, decl, ref;
965 if (TYPE_SIZE (type) == error_mark_node)
966 return null_pointer_node;
967 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
968 "", '/', '/', ".class$$");
969 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
970 if (decl == NULL_TREE)
972 decl = build_decl (VAR_DECL, decl_name, class_type_node);
973 TREE_STATIC (decl) = 1;
974 if (! flag_indirect_classes)
975 TREE_PUBLIC (decl) = 1;
976 DECL_IGNORED_P (decl) = 1;
977 DECL_ARTIFICIAL (decl) = 1;
978 if (is_compiled_class (type) == 1)
979 DECL_EXTERNAL (decl) = 1;
980 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
981 DECL_CLASS_FIELD_P (decl) = 1;
982 DECL_CONTEXT (decl) = type;
984 /* ??? We want to preserve the DECL_CONTEXT we set just above,
985 that that means not calling pushdecl_top_level. */
986 IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
989 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
994 build_classdollar_field (tree type)
996 tree decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
997 "", '/', '/', ".class$");
998 tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1000 if (decl == NULL_TREE)
1003 = build_decl (VAR_DECL, decl_name,
1006 (build_type_variant (class_type_node,
1008 /* const */ 1, 0)));
1009 TREE_STATIC (decl) = 1;
1010 TREE_INVARIANT (decl) = 1;
1011 TREE_CONSTANT (decl) = 1;
1012 TREE_READONLY (decl) = 1;
1013 TREE_PUBLIC (decl) = 1;
1014 DECL_IGNORED_P (decl) = 1;
1015 DECL_ARTIFICIAL (decl) = 1;
1016 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1017 IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
1018 DECL_CLASS_FIELD_P (decl) = 1;
1019 DECL_CONTEXT (decl) = type;
1025 /* Build a reference to the class TYPE.
1026 Also handles primitive types and array types. */
1029 build_class_ref (tree type)
1031 int is_compiled = is_compiled_class (type);
1035 if (TREE_CODE (type) == POINTER_TYPE)
1036 type = TREE_TYPE (type);
1038 if (flag_indirect_dispatch
1039 && type != output_class
1040 && TREE_CODE (type) == RECORD_TYPE)
1041 return build_indirect_class_ref (type);
1043 if (type == output_class && flag_indirect_classes)
1044 return build_classdollar_field (type);
1046 if (TREE_CODE (type) == RECORD_TYPE)
1047 return build_static_class_ref (type);
1053 if (flag_emit_class_files)
1055 const char *prim_class_name;
1057 if (type == char_type_node)
1058 prim_class_name = "java.lang.Character";
1059 else if (type == boolean_type_node)
1060 prim_class_name = "java.lang.Boolean";
1061 else if (type == byte_type_node)
1062 prim_class_name = "java.lang.Byte";
1063 else if (type == short_type_node)
1064 prim_class_name = "java.lang.Short";
1065 else if (type == int_type_node)
1066 prim_class_name = "java.lang.Integer";
1067 else if (type == long_type_node)
1068 prim_class_name = "java.lang.Long";
1069 else if (type == float_type_node)
1070 prim_class_name = "java.lang.Float";
1071 else if (type == double_type_node)
1072 prim_class_name = "java.lang.Double";
1073 else if (type == void_type_node)
1074 prim_class_name = "java.lang.Void";
1078 prim_class = lookup_class (get_identifier (prim_class_name));
1079 /* We wrap the class in a NOP_EXPR, because it is a
1080 type. We can't hold it in the COMPONENT_REF itself,
1081 as that type must remain NULL. */
1082 prim_class = build1 (NOP_EXPR, prim_class, NULL_TREE);
1084 return build3 (COMPONENT_REF, NULL_TREE,
1085 prim_class, TYPE_identifier_node, NULL_TREE);
1087 decl_name = TYPE_NAME (type);
1088 if (TREE_CODE (decl_name) == TYPE_DECL)
1089 decl_name = DECL_NAME (decl_name);
1090 name = IDENTIFIER_POINTER (decl_name);
1091 if (strncmp (name, "promoted_", 9) == 0)
1093 sprintf (buffer, "_Jv_%sClass", name);
1094 decl_name = get_identifier (buffer);
1095 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1096 if (decl == NULL_TREE)
1098 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1099 TREE_STATIC (decl) = 1;
1100 TREE_PUBLIC (decl) = 1;
1101 DECL_EXTERNAL (decl) = 1;
1102 DECL_ARTIFICIAL (decl) = 1;
1103 pushdecl_top_level (decl);
1107 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1111 return build_indirect_class_ref (type);
1114 /* Create a local statically allocated variable that will hold a
1115 pointer to a static field. */
1118 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1120 tree decl, decl_name;
1121 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1122 char *buf = alloca (strlen (name) + 20);
1123 sprintf (buf, "%s_%d_ref", name, index);
1124 decl_name = get_identifier (buf);
1125 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1126 if (decl == NULL_TREE)
1128 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1129 TREE_STATIC (decl) = 1;
1130 TREE_PUBLIC (decl) = 0;
1131 DECL_EXTERNAL (decl) = 0;
1132 DECL_ARTIFICIAL (decl) = 1;
1133 pushdecl_top_level (decl);
1139 build_static_field_ref (tree fdecl)
1141 tree fclass = DECL_CONTEXT (fdecl);
1142 int is_compiled = is_compiled_class (fclass);
1143 int from_class = ! CLASS_FROM_SOURCE_P (current_class);
1145 /* Allow static final fields to fold to a constant. When using
1146 -findirect-dispatch, we simply never do this folding if compiling
1147 from .class; in the .class file constants will be referred to via
1148 the constant pool. */
1149 if ((!flag_indirect_dispatch || !from_class)
1151 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1152 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1153 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1154 && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
1156 if (is_compiled == 1)
1157 DECL_EXTERNAL (fdecl) = 1;
1161 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1162 and a class local static variable CACHE_ENTRY, then
1164 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1165 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1168 This can mostly be optimized away, so that the usual path is a
1169 load followed by a test and branch. _Jv_ResolvePoolEntry is
1170 only called once for each constant pool entry.
1172 There is an optimization that we don't do: at the start of a
1173 method, create a local copy of CACHE_ENTRY and use that instead.
1177 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1178 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1180 = build3 (CALL_EXPR, boolean_type_node,
1181 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1182 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1183 cache_entry, null_pointer_node),
1184 build_tree_list (NULL_TREE, boolean_false_node)),
1186 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1188 = build3 (CALL_EXPR, ptr_type_node,
1189 build_address_of (soft_resolvepoolentry_node),
1190 tree_cons (NULL_TREE, build_class_ref (output_class),
1191 build_tree_list (NULL_TREE, cpool_index_cst)),
1193 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1194 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1195 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1196 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1202 get_access_flags_from_decl (tree decl)
1204 int access_flags = 0;
1205 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1207 if (FIELD_STATIC (decl))
1208 access_flags |= ACC_STATIC;
1209 if (FIELD_PUBLIC (decl))
1210 access_flags |= ACC_PUBLIC;
1211 if (FIELD_PROTECTED (decl))
1212 access_flags |= ACC_PROTECTED;
1213 if (FIELD_PRIVATE (decl))
1214 access_flags |= ACC_PRIVATE;
1215 if (FIELD_FINAL (decl))
1216 access_flags |= ACC_FINAL;
1217 if (FIELD_VOLATILE (decl))
1218 access_flags |= ACC_VOLATILE;
1219 if (FIELD_TRANSIENT (decl))
1220 access_flags |= ACC_TRANSIENT;
1221 return access_flags;
1223 if (TREE_CODE (decl) == TYPE_DECL)
1225 if (CLASS_PUBLIC (decl))
1226 access_flags |= ACC_PUBLIC;
1227 if (CLASS_FINAL (decl))
1228 access_flags |= ACC_FINAL;
1229 if (CLASS_SUPER (decl))
1230 access_flags |= ACC_SUPER;
1231 if (CLASS_INTERFACE (decl))
1232 access_flags |= ACC_INTERFACE;
1233 if (CLASS_ABSTRACT (decl))
1234 access_flags |= ACC_ABSTRACT;
1235 if (CLASS_STATIC (decl))
1236 access_flags |= ACC_STATIC;
1237 if (CLASS_PRIVATE (decl))
1238 access_flags |= ACC_PRIVATE;
1239 if (CLASS_PROTECTED (decl))
1240 access_flags |= ACC_PROTECTED;
1241 if (CLASS_STRICTFP (decl))
1242 access_flags |= ACC_STRICT;
1243 return access_flags;
1245 if (TREE_CODE (decl) == FUNCTION_DECL)
1247 if (METHOD_PUBLIC (decl))
1248 access_flags |= ACC_PUBLIC;
1249 if (METHOD_PRIVATE (decl))
1250 access_flags |= ACC_PRIVATE;
1251 if (METHOD_PROTECTED (decl))
1252 access_flags |= ACC_PROTECTED;
1253 if (METHOD_STATIC (decl))
1254 access_flags |= ACC_STATIC;
1255 if (METHOD_FINAL (decl))
1256 access_flags |= ACC_FINAL;
1257 if (METHOD_SYNCHRONIZED (decl))
1258 access_flags |= ACC_SYNCHRONIZED;
1259 if (METHOD_NATIVE (decl))
1260 access_flags |= ACC_NATIVE;
1261 if (METHOD_ABSTRACT (decl))
1262 access_flags |= ACC_ABSTRACT;
1263 if (METHOD_STRICTFP (decl))
1264 access_flags |= ACC_STRICT;
1265 if (METHOD_INVISIBLE (decl))
1266 access_flags |= ACC_INVISIBLE;
1267 return access_flags;
1272 static GTY (()) int alias_labelno = 0;
1274 /* Create a private alias for METHOD. Using this alias instead of the method
1275 decl ensures that ncode entries in the method table point to the real function
1276 at runtime, not a PLT entry. */
1279 make_local_function_alias (tree method)
1281 #ifdef ASM_OUTPUT_DEF
1284 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1285 char *name = alloca (strlen (method_name) + 2);
1286 char *buf = alloca (strlen (method_name) + 128);
1288 /* Only create aliases for local functions. */
1289 if (DECL_EXTERNAL (method))
1292 /* Prefix method_name with 'L' for the alias label. */
1294 strcpy (name + 1, method_name);
1296 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1297 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1298 TREE_TYPE (method));
1299 DECL_CONTEXT (alias) = NULL;
1300 TREE_READONLY (alias) = TREE_READONLY (method);
1301 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1302 TREE_PUBLIC (alias) = 0;
1303 DECL_EXTERNAL (alias) = 0;
1304 DECL_ARTIFICIAL (alias) = 1;
1305 DECL_INLINE (alias) = 0;
1306 DECL_INITIAL (alias) = error_mark_node;
1307 TREE_ADDRESSABLE (alias) = 1;
1308 TREE_USED (alias) = 1;
1309 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1310 if (!flag_syntax_only)
1311 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1318 /** Make reflection data (_Jv_Field) for field FDECL. */
1321 make_field_value (tree fdecl)
1325 tree type = TREE_TYPE (fdecl);
1326 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1328 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1329 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1331 type = build_class_ref (type);
1334 tree signature = build_java_signature (type);
1336 type = build_utf8_ref (unmangle_classname
1337 (IDENTIFIER_POINTER (signature),
1338 IDENTIFIER_LENGTH (signature)));
1340 PUSH_FIELD_VALUE (finit, "type", type);
1342 flags = get_access_flags_from_decl (fdecl);
1344 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1346 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1347 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1350 tree field_address = integer_zero_node;
1351 if ((DECL_INITIAL (fdecl) || ! flag_indirect_classes)
1352 && FIELD_STATIC (fdecl))
1353 field_address = build_address_of (fdecl);
1357 build_constructor_from_list (field_info_union_node,
1359 ((FIELD_STATIC (fdecl)
1360 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1361 : TYPE_FIELDS (field_info_union_node)),
1362 (FIELD_STATIC (fdecl)
1364 : byte_position (fdecl)))));
1367 FINISH_RECORD_CONSTRUCTOR (finit);
1371 /** Make reflection data (_Jv_Method) for method MDECL. */
1374 make_method_value (tree mdecl)
1376 static int method_name_count = 0;
1381 #define ACC_TRANSLATED 0x4000
1382 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1384 class_decl = DECL_CONTEXT (mdecl);
1385 /* For interfaces, the index field contains the dispatch index. */
1386 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1387 index = build_int_cst (NULL_TREE,
1388 get_interface_method_index (mdecl, class_decl));
1389 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1390 index = get_method_index (mdecl);
1392 index = integer_minus_one_node;
1394 code = null_pointer_node;
1395 if (METHOD_ABSTRACT (mdecl))
1396 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1397 soft_abstractmethod_node);
1399 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1400 make_local_function_alias (mdecl));
1401 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1402 PUSH_FIELD_VALUE (minit, "name",
1403 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1404 init_identifier_node
1405 : DECL_NAME (mdecl)));
1407 tree signature = build_java_signature (TREE_TYPE (mdecl));
1408 PUSH_FIELD_VALUE (minit, "signature",
1411 (IDENTIFIER_POINTER(signature),
1412 IDENTIFIER_LENGTH(signature)))));
1414 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1415 PUSH_FIELD_VALUE (minit, "index", index);
1416 PUSH_FIELD_VALUE (minit, "ncode", code);
1419 /* Compute the `throws' information for the method. */
1420 tree table = null_pointer_node;
1421 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1423 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1424 tree iter, type, array;
1427 table = tree_cons (NULL_TREE, table, NULL_TREE);
1428 for (iter = DECL_FUNCTION_THROWS (mdecl);
1430 iter = TREE_CHAIN (iter))
1432 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1434 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1435 IDENTIFIER_LENGTH (sig)));
1436 table = tree_cons (NULL_TREE, utf8, table);
1438 type = build_prim_array_type (ptr_type_node, length);
1439 table = build_constructor_from_list (type, table);
1440 /* Compute something unique enough. */
1441 sprintf (buf, "_methods%d", method_name_count++);
1442 array = build_decl (VAR_DECL, get_identifier (buf), type);
1443 DECL_INITIAL (array) = table;
1444 TREE_STATIC (array) = 1;
1445 DECL_ARTIFICIAL (array) = 1;
1446 DECL_IGNORED_P (array) = 1;
1447 rest_of_decl_compilation (array, 1, 0);
1449 table = build1 (ADDR_EXPR, ptr_type_node, array);
1452 PUSH_FIELD_VALUE (minit, "throws", table);
1455 FINISH_RECORD_CONSTRUCTOR (minit);
1460 get_dispatch_vector (tree type)
1462 tree vtable = TYPE_VTABLE (type);
1464 if (vtable == NULL_TREE)
1468 tree super = CLASSTYPE_SUPER (type);
1469 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1470 vtable = make_tree_vec (nvirtuals);
1471 TYPE_VTABLE (type) = vtable;
1472 if (super != NULL_TREE)
1474 tree super_vtable = get_dispatch_vector (super);
1476 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1477 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1480 for (method = TYPE_METHODS (type); method != NULL_TREE;
1481 method = TREE_CHAIN (method))
1483 tree method_index = get_method_index (method);
1484 if (method_index != NULL_TREE
1485 && host_integerp (method_index, 0))
1486 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1494 get_dispatch_table (tree type, tree this_class_addr)
1496 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1497 tree vtable = get_dispatch_vector (type);
1499 tree list = NULL_TREE;
1500 int nvirtuals = TREE_VEC_LENGTH (vtable);
1504 for (i = nvirtuals; --i >= 0; )
1506 tree method = TREE_VEC_ELT (vtable, i);
1507 if (METHOD_ABSTRACT (method))
1510 warning (0, "%Jabstract method in non-abstract class", method);
1512 if (TARGET_VTABLE_USES_DESCRIPTORS)
1513 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1514 list = tree_cons (NULL_TREE, null_pointer_node, list);
1516 list = tree_cons (NULL_TREE, null_pointer_node, list);
1520 if (TARGET_VTABLE_USES_DESCRIPTORS)
1521 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1523 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1524 method, build_int_cst (NULL_TREE, j));
1525 TREE_CONSTANT (fdesc) = 1;
1526 TREE_INVARIANT (fdesc) = 1;
1527 list = tree_cons (NULL_TREE, fdesc, list);
1530 list = tree_cons (NULL_TREE,
1531 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1537 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1538 using the Boehm GC we sometimes stash a GC type descriptor
1539 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1540 the emitted byte count during the output to the assembly file. */
1541 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1542 fake "function descriptor". It's first word is the is the class
1543 pointer, and subsequent words (usually one) contain the GC descriptor.
1544 In all other cases, we reserve two extra vtable slots. */
1545 gc_descr = get_boehm_type_descriptor (type);
1546 list = tree_cons (NULL_TREE, gc_descr, list);
1547 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1548 list = tree_cons (NULL_TREE, gc_descr, list);
1549 list = tree_cons (NULL_TREE, this_class_addr, list);
1551 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1552 list = tree_cons (NULL_TREE, null_pointer_node, list);
1553 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1554 list = tree_cons (integer_zero_node, null_pointer_node, list);
1556 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1557 if (TARGET_VTABLE_USES_DESCRIPTORS)
1558 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1560 return build_constructor_from_list
1561 (build_prim_array_type (nativecode_ptr_type_node,
1566 /* Set the method_index for a method decl. */
1568 set_method_index (tree decl, tree method_index)
1570 if (method_index != NULL_TREE)
1572 /* method_index is null if we're using indirect dispatch. */
1573 method_index = fold (convert (sizetype, method_index));
1575 if (TARGET_VTABLE_USES_DESCRIPTORS)
1576 /* Add one to skip bogus descriptor for class and GC descriptor. */
1577 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1579 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1581 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1584 DECL_VINDEX (decl) = method_index;
1587 /* Get the method_index for a method decl. */
1589 get_method_index (tree decl)
1591 tree method_index = DECL_VINDEX (decl);
1596 if (TARGET_VTABLE_USES_DESCRIPTORS)
1597 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1598 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1600 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1601 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1603 return method_index;
1607 supers_all_compiled (tree type)
1609 while (type != NULL_TREE)
1611 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1613 type = CLASSTYPE_SUPER (type);
1619 make_class_data (tree type)
1621 tree decl, cons, temp;
1622 tree field, fields_decl;
1623 tree static_fields = NULL_TREE;
1624 tree instance_fields = NULL_TREE;
1625 HOST_WIDE_INT static_field_count = 0;
1626 HOST_WIDE_INT instance_field_count = 0;
1627 HOST_WIDE_INT field_count;
1628 tree field_array_type;
1630 tree methods = NULL_TREE;
1631 tree dtable_decl = NULL_TREE;
1632 HOST_WIDE_INT method_count = 0;
1633 tree method_array_type;
1636 tree this_class_addr;
1637 tree constant_pool_constructor;
1638 tree interfaces = null_pointer_node;
1639 int interface_len = 0;
1640 int uses_jv_markobj = 0;
1641 tree type_decl = TYPE_NAME (type);
1642 tree id_main = get_identifier("main");
1643 tree id_class = get_identifier("java.lang.Class");
1644 /** Offset from start of virtual function table declaration
1645 to where objects actually point at, following new g++ ABI. */
1646 tree dtable_start_offset = build_int_cst (NULL_TREE,
1647 2 * POINTER_SIZE / BITS_PER_UNIT);
1649 this_class_addr = build_static_class_ref (type);
1650 decl = TREE_OPERAND (this_class_addr, 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 uses_jv_markobj = uses_jv_markobj_p (dtable);
1657 dtable_decl = build_dtable_decl (type);
1658 DECL_INITIAL (dtable_decl) = dtable;
1659 TREE_STATIC (dtable_decl) = 1;
1660 DECL_ARTIFICIAL (dtable_decl) = 1;
1661 DECL_IGNORED_P (dtable_decl) = 1;
1662 TREE_PUBLIC (dtable_decl) = 1;
1663 if (! flag_indirect_classes)
1664 rest_of_decl_compilation (dtable_decl, 1, 0);
1665 if (type == class_type_node)
1666 class_dtable_decl = dtable_decl;
1669 /* Build Field array. */
1670 field = TYPE_FIELDS (type);
1671 while (field && DECL_ARTIFICIAL (field))
1672 field = TREE_CHAIN (field); /* Skip dummy fields. */
1673 if (field && DECL_NAME (field) == NULL_TREE)
1674 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1675 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1677 if (! DECL_ARTIFICIAL (field))
1679 if (FIELD_STATIC (field))
1681 /* We must always create reflection data for static fields
1682 as it is used in the creation of the field itself. */
1683 tree init = make_field_value (field);
1684 tree initial = DECL_INITIAL (field);
1685 static_field_count++;
1686 static_fields = tree_cons (NULL_TREE, init, static_fields);
1687 /* If the initial value is a string constant,
1688 prevent output_constant from trying to assemble the value. */
1689 if (initial != NULL_TREE
1690 && TREE_TYPE (initial) == string_ptr_type_node)
1691 DECL_INITIAL (field) = NULL_TREE;
1692 rest_of_decl_compilation (field, 1, 1);
1693 DECL_INITIAL (field) = initial;
1695 else if (uses_jv_markobj || !flag_reduced_reflection)
1697 tree init = make_field_value (field);
1698 instance_field_count++;
1699 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1703 field_count = static_field_count + instance_field_count;
1704 if (field_count > 0)
1706 static_fields = nreverse (static_fields);
1707 instance_fields = nreverse (instance_fields);
1708 static_fields = chainon (static_fields, instance_fields);
1709 field_array_type = build_prim_array_type (field_type_node, field_count);
1710 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1712 DECL_INITIAL (fields_decl) = build_constructor_from_list
1713 (field_array_type, static_fields);
1714 TREE_STATIC (fields_decl) = 1;
1715 DECL_ARTIFICIAL (fields_decl) = 1;
1716 DECL_IGNORED_P (fields_decl) = 1;
1717 rest_of_decl_compilation (fields_decl, 1, 0);
1720 fields_decl = NULL_TREE;
1722 /* Build Method array. */
1723 for (method = TYPE_METHODS (type);
1724 method != NULL_TREE; method = TREE_CHAIN (method))
1727 if (METHOD_PRIVATE (method)
1728 && ! flag_keep_inline_functions
1731 /* Even if we have a decl, we don't necessarily have the code.
1732 This can happen if we inherit a method from a superclass for
1733 which we don't have a .class file. */
1734 if (METHOD_DUMMY (method))
1737 /* Generate method reflection data if:
1739 - !flag_reduced_reflection.
1741 - <clinit> -- The runtime uses reflection to initialize the
1744 - Any method in class java.lang.Class -- Class.forName() and
1745 perhaps other things require it.
1747 - class$ -- It does not work if reflection data missing.
1749 - main -- Reflection is used to find main(String[]) methods.
1751 - public not static -- It is potentially part of an
1752 interface. The runtime uses reflection data to build
1753 interface dispatch tables. */
1754 if (!flag_reduced_reflection
1755 || DECL_CLINIT_P (method)
1756 || DECL_NAME (type_decl) == id_class
1757 || DECL_NAME (method) == id_main
1758 || (METHOD_PUBLIC (method) && !METHOD_STATIC (method))
1759 || TYPE_DOT_CLASS (type) == method)
1761 init = make_method_value (method);
1763 methods = tree_cons (NULL_TREE, init, methods);
1766 method_array_type = build_prim_array_type (method_type_node, method_count);
1767 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1769 DECL_INITIAL (methods_decl) = build_constructor_from_list
1770 (method_array_type, nreverse (methods));
1771 TREE_STATIC (methods_decl) = 1;
1772 DECL_ARTIFICIAL (methods_decl) = 1;
1773 DECL_IGNORED_P (methods_decl) = 1;
1774 rest_of_decl_compilation (methods_decl, 1, 0);
1776 if (class_dtable_decl == NULL_TREE)
1778 class_dtable_decl = build_dtable_decl (class_type_node);
1779 TREE_STATIC (class_dtable_decl) = 1;
1780 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1781 DECL_IGNORED_P (class_dtable_decl) = 1;
1782 if (is_compiled_class (class_type_node) != 2)
1783 DECL_EXTERNAL (class_dtable_decl) = 1;
1784 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1787 super = CLASSTYPE_SUPER (type);
1788 if (super == NULL_TREE)
1789 super = null_pointer_node;
1790 else if (! flag_indirect_dispatch
1791 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1792 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1793 super = build_class_ref (super);
1796 int super_index = alloc_class_constant (super);
1797 super = build_int_cst (ptr_type_node, super_index);
1800 /* Build and emit the array of implemented interfaces. */
1801 if (type != object_type_node)
1802 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1804 if (interface_len > 0)
1806 tree init = NULL_TREE;
1808 tree interface_array_type, idecl;
1809 interface_array_type
1810 = build_prim_array_type (class_ptr_type, interface_len);
1811 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1812 interface_array_type);
1814 for (i = interface_len; i > 0; i--)
1816 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1817 tree iclass = BINFO_TYPE (child);
1819 if (! flag_indirect_dispatch
1821 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1822 index = build_class_ref (iclass);
1825 int int_index = alloc_class_constant (iclass);
1826 index = build_int_cst (ptr_type_node, int_index);
1828 init = tree_cons (NULL_TREE, index, init);
1830 DECL_INITIAL (idecl) = build_constructor_from_list (interface_array_type,
1832 TREE_STATIC (idecl) = 1;
1833 DECL_ARTIFICIAL (idecl) = 1;
1834 DECL_IGNORED_P (idecl) = 1;
1835 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1836 rest_of_decl_compilation (idecl, 1, 0);
1839 constant_pool_constructor = build_constants_constructor ();
1841 if (flag_indirect_dispatch)
1843 TYPE_OTABLE_DECL (type)
1845 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1846 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1847 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1849 TYPE_ATABLE_DECL (type)
1851 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1852 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1853 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1855 TYPE_ITABLE_DECL (type)
1857 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1858 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1859 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1862 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1864 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1865 PUSH_FIELD_VALUE (temp, "vtable",
1866 build2 (PLUS_EXPR, dtable_ptr_type,
1867 build1 (ADDR_EXPR, dtable_ptr_type,
1869 dtable_start_offset));
1870 if (! flag_hash_synchronization)
1871 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1872 FINISH_RECORD_CONSTRUCTOR (temp);
1873 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1874 PUSH_SUPER_VALUE (cons, temp);
1875 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1876 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1877 PUSH_FIELD_VALUE (cons, "accflags",
1878 build_int_cst (NULL_TREE,
1879 get_access_flags_from_decl (type_decl)));
1881 PUSH_FIELD_VALUE (cons, "superclass",
1882 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1883 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1884 PUSH_FIELD_VALUE (cons, "methods",
1885 methods_decl == NULL_TREE ? null_pointer_node
1886 : build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1887 PUSH_FIELD_VALUE (cons, "method_count",
1888 build_int_cst (NULL_TREE, method_count));
1890 if (flag_indirect_dispatch)
1891 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1893 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1895 PUSH_FIELD_VALUE (cons, "fields",
1896 fields_decl == NULL_TREE ? null_pointer_node
1897 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1898 /* If we're using the binary compatibility ABI we don't know the
1899 size until load time. */
1900 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1901 (flag_indirect_dispatch
1902 ? integer_minus_one_node
1903 : size_in_bytes (type)));
1904 PUSH_FIELD_VALUE (cons, "field_count",
1905 build_int_cst (NULL_TREE, field_count));
1906 PUSH_FIELD_VALUE (cons, "static_field_count",
1907 build_int_cst (NULL_TREE, static_field_count));
1909 if (flag_indirect_dispatch)
1910 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1912 PUSH_FIELD_VALUE (cons, "vtable",
1913 dtable_decl == NULL_TREE ? null_pointer_node
1914 : build2 (PLUS_EXPR, dtable_ptr_type,
1915 build1 (ADDR_EXPR, dtable_ptr_type,
1917 dtable_start_offset));
1918 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1920 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1921 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1925 pushdecl_top_level (TYPE_OTABLE_SYMS_DECL (type));
1926 PUSH_FIELD_VALUE (cons, "otable",
1927 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1928 PUSH_FIELD_VALUE (cons, "otable_syms",
1929 build1 (ADDR_EXPR, symbols_array_ptr_type,
1930 TYPE_OTABLE_SYMS_DECL (type)));
1931 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1932 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1934 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1936 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1937 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1941 pushdecl_top_level (TYPE_ATABLE_SYMS_DECL (type));
1942 PUSH_FIELD_VALUE (cons, "atable",
1943 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1944 PUSH_FIELD_VALUE (cons, "atable_syms",
1945 build1 (ADDR_EXPR, symbols_array_ptr_type,
1946 TYPE_ATABLE_SYMS_DECL (type)));
1947 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1948 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1950 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1952 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1953 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1957 pushdecl_top_level (TYPE_ITABLE_SYMS_DECL (type));
1958 PUSH_FIELD_VALUE (cons, "itable",
1959 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1960 PUSH_FIELD_VALUE (cons, "itable_syms",
1961 build1 (ADDR_EXPR, symbols_array_ptr_type,
1962 TYPE_ITABLE_SYMS_DECL (type)));
1963 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1964 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1967 PUSH_FIELD_VALUE (cons, "catch_classes",
1968 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1969 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1970 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1971 PUSH_FIELD_VALUE (cons, "interface_count",
1972 build_int_cst (NULL_TREE, interface_len));
1973 PUSH_FIELD_VALUE (cons, "state",
1974 convert (byte_type_node,
1975 build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
1977 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1978 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1979 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1980 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1981 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1982 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1985 tree assertion_table_ref;
1986 if (TYPE_ASSERTIONS (type) == NULL)
1987 assertion_table_ref = null_pointer_node;
1989 assertion_table_ref = build1 (ADDR_EXPR,
1990 build_pointer_type (assertion_table_type),
1991 emit_assertion_table (type));
1993 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1996 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1997 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1998 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1999 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
2001 FINISH_RECORD_CONSTRUCTOR (cons);
2003 DECL_INITIAL (decl) = cons;
2005 /* Hash synchronization requires at least 64-bit alignment. */
2006 if (flag_hash_synchronization && POINTER_SIZE < 64)
2007 DECL_ALIGN (decl) = 64;
2009 if (flag_indirect_classes)
2011 TREE_READONLY (decl) = 1;
2012 TREE_CONSTANT (DECL_INITIAL (decl)) = 1;
2015 rest_of_decl_compilation (decl, 1, 0);
2018 tree classdollar_field = build_classdollar_field (type);
2019 if (!flag_indirect_classes)
2020 DECL_INITIAL (classdollar_field) = build_static_class_ref (type);
2021 rest_of_decl_compilation (classdollar_field, 1, 0);
2024 TYPE_OTABLE_DECL (type) = NULL_TREE;
2025 TYPE_ATABLE_DECL (type) = NULL_TREE;
2026 TYPE_CTABLE_DECL (type) = NULL_TREE;
2032 if (TYPE_VERIFY_METHOD (output_class))
2034 tree verify_method = TYPE_VERIFY_METHOD (output_class);
2035 DECL_SAVED_TREE (verify_method)
2036 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
2037 build1 (RETURN_EXPR, void_type_node, NULL));
2038 java_genericize (verify_method);
2039 cgraph_finalize_function (verify_method, false);
2040 TYPE_ASSERTIONS (current_class) = NULL;
2043 java_expand_catch_classes (current_class);
2045 current_function_decl = NULL_TREE;
2046 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
2047 make_class_data (current_class);
2049 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
2052 /* Return 2 if CLASS is compiled by this compilation job;
2053 return 1 if CLASS can otherwise be assumed to be compiled;
2054 return 0 if we cannot assume that CLASS is compiled.
2055 Returns 1 for primitive and 0 for array types. */
2057 is_compiled_class (tree class)
2060 if (TREE_CODE (class) == POINTER_TYPE)
2061 class = TREE_TYPE (class);
2062 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
2064 if (TYPE_ARRAY_P (class))
2066 if (class == current_class)
2069 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
2070 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
2072 /* The class was seen in the current ZIP file and will be
2073 available as a compiled class in the future but may not have
2074 been loaded already. Load it if necessary. This prevent
2075 build_class_ref () from crashing. */
2077 if (seen_in_zip && !CLASS_LOADED_P (class))
2078 load_class (class, 1);
2080 /* We return 2 for class seen in ZIP and class from files
2081 belonging to the same compilation unit */
2085 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
2087 if (!CLASS_LOADED_P (class))
2089 if (CLASS_FROM_SOURCE_P (class))
2090 safe_layout_class (class);
2092 load_class (class, 1);
2100 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
2103 build_dtable_decl (tree type)
2107 /* We need to build a new dtable type so that its size is uniquely
2108 computed when we're dealing with the class for real and not just
2109 faking it (like java.lang.Class during the initialization of the
2110 compiler.) We know we're not faking a class when CURRENT_CLASS is
2112 if (current_class == type)
2114 tree dummy = NULL_TREE;
2117 dtype = make_node (RECORD_TYPE);
2119 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
2120 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
2122 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
2123 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2125 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2126 TREE_CHAIN (dummy) = tmp_field;
2127 DECL_CONTEXT (tmp_field) = dtype;
2128 DECL_ARTIFICIAL (tmp_field) = 1;
2132 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2133 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2135 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2136 TREE_CHAIN (dummy) = tmp_field;
2137 DECL_CONTEXT (tmp_field) = dtype;
2138 DECL_ARTIFICIAL (tmp_field) = 1;
2142 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2143 if (TARGET_VTABLE_USES_DESCRIPTORS)
2144 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2146 PUSH_FIELD (dtype, dummy, "methods",
2147 build_prim_array_type (nativecode_ptr_type_node, n));
2148 layout_type (dtype);
2151 dtype = dtable_type;
2153 decl = build_decl (VAR_DECL, get_identifier ("vt$"), dtype);
2154 DECL_CONTEXT (decl) = type;
2155 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2156 DECL_VTABLE_P (decl) = 1;
2161 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2162 fields inherited from SUPER_CLASS. */
2165 push_super_field (tree this_class, tree super_class)
2168 /* Don't insert the field if we're just re-laying the class out. */
2169 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2171 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2172 DECL_IGNORED_P (base_decl) = 1;
2173 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2174 TYPE_FIELDS (this_class) = base_decl;
2175 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2176 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2179 /* Handle the different manners we may have to lay out a super class. */
2182 maybe_layout_super_class (tree super_class, tree this_class)
2186 else if (TREE_CODE (super_class) == RECORD_TYPE)
2188 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2189 safe_layout_class (super_class);
2190 if (!CLASS_LOADED_P (super_class))
2191 load_class (super_class, 1);
2193 /* We might have to layout the class before its dependency on
2194 the super class gets resolved by java_complete_class */
2195 else if (TREE_CODE (super_class) == POINTER_TYPE)
2197 if (TREE_TYPE (super_class) != NULL_TREE)
2198 super_class = TREE_TYPE (super_class);
2201 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2203 tree this_wrap = NULL_TREE;
2205 /* Set the correct context for class resolution. */
2206 current_class = this_class;
2210 tree this_decl = TYPE_NAME (this_class);
2211 #ifdef USE_MAPPED_LOCATION
2212 this_wrap = build_expr_wfl (this_class,
2213 DECL_SOURCE_LOCATION (this_decl));
2215 this_wrap = build_expr_wfl (this_class,
2216 DECL_SOURCE_FILE (this_decl),
2217 DECL_SOURCE_LINE (this_decl), 0);
2221 = do_resolve_class (DECL_CONTEXT (TYPE_NAME (this_class)),
2222 this_class, super_class, NULL_TREE, this_wrap);
2224 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2225 super_class = TREE_TYPE (super_class);
2228 if (!TYPE_SIZE (super_class))
2229 safe_layout_class (super_class);
2235 layout_class (tree this_class)
2237 tree super_class = CLASSTYPE_SUPER (this_class);
2239 class_list = tree_cons (this_class, NULL_TREE, class_list);
2240 if (CLASS_BEING_LAIDOUT (this_class))
2246 sprintf (buffer, " with '%s'",
2247 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2248 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2250 for (current = TREE_CHAIN (class_list); current;
2251 current = TREE_CHAIN (current))
2253 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2254 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2255 IDENTIFIER_POINTER (DECL_NAME (decl)),
2256 DECL_SOURCE_FILE (decl),
2257 DECL_SOURCE_LINE (decl));
2258 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2260 obstack_1grow (&temporary_obstack, '\0');
2261 report = obstack_finish (&temporary_obstack);
2262 cyclic_inheritance_report = ggc_strdup (report);
2263 obstack_free (&temporary_obstack, report);
2264 TYPE_SIZE (this_class) = error_mark_node;
2267 CLASS_BEING_LAIDOUT (this_class) = 1;
2269 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2271 tree maybe_super_class
2272 = maybe_layout_super_class (super_class, this_class);
2273 if (maybe_super_class == NULL
2274 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2276 TYPE_SIZE (this_class) = error_mark_node;
2277 CLASS_BEING_LAIDOUT (this_class) = 0;
2278 class_list = TREE_CHAIN (class_list);
2281 if (TYPE_SIZE (this_class) == NULL_TREE)
2282 push_super_field (this_class, maybe_super_class);
2285 layout_type (this_class);
2287 /* Also recursively load/layout any superinterfaces, but only if
2288 class was loaded from bytecode. The source parser will take care
2290 if (!CLASS_FROM_SOURCE_P (this_class))
2293 if (TYPE_BINFO (this_class))
2295 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2297 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2298 tree super_interface = BINFO_TYPE (binfo);
2299 tree maybe_super_interface
2300 = maybe_layout_super_class (super_interface, NULL_TREE);
2301 if (maybe_super_interface == NULL
2302 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2304 TYPE_SIZE (this_class) = error_mark_node;
2305 CLASS_BEING_LAIDOUT (this_class) = 0;
2306 class_list = TREE_CHAIN (class_list);
2313 /* Convert the size back to an SI integer value. */
2314 TYPE_SIZE_UNIT (this_class) =
2315 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2317 CLASS_BEING_LAIDOUT (this_class) = 0;
2318 class_list = TREE_CHAIN (class_list);
2322 add_miranda_methods (tree base_class, tree search_class)
2325 tree binfo, base_binfo;
2327 if (!CLASS_PARSED_P (search_class))
2328 load_class (search_class, 1);
2330 for (binfo = TYPE_BINFO (search_class), i = 1;
2331 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2334 tree elt = BINFO_TYPE (base_binfo);
2336 /* FIXME: This is totally bogus. We should not be handling
2337 Miranda methods at all if we're using the BC ABI. */
2338 if (TYPE_DUMMY (elt))
2341 /* Ensure that interface methods are seen in declared order. */
2342 if (!CLASS_LOADED_P (elt))
2343 load_class (elt, 1);
2344 layout_class_methods (elt);
2346 /* All base classes will have been laid out at this point, so the order
2347 will be correct. This code must match similar layout code in the
2349 for (method_decl = TYPE_METHODS (elt);
2350 method_decl; method_decl = TREE_CHAIN (method_decl))
2354 /* An interface can have <clinit>. */
2355 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2358 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2359 override = lookup_argument_method (base_class,
2360 DECL_NAME (method_decl), sig);
2361 if (override == NULL_TREE)
2363 /* Found a Miranda method. Add it. */
2365 sig = build_java_signature (TREE_TYPE (method_decl));
2367 = add_method (base_class,
2368 get_access_flags_from_decl (method_decl),
2369 DECL_NAME (method_decl), sig);
2370 METHOD_INVISIBLE (new_method) = 1;
2374 /* Try superinterfaces. */
2375 add_miranda_methods (base_class, elt);
2380 layout_class_methods (tree this_class)
2382 tree method_decl, dtable_count;
2383 tree super_class, type_name;
2385 if (TYPE_NVIRTUALS (this_class))
2388 super_class = CLASSTYPE_SUPER (this_class);
2392 super_class = maybe_layout_super_class (super_class, this_class);
2393 if (!TYPE_NVIRTUALS (super_class))
2394 layout_class_methods (super_class);
2395 dtable_count = TYPE_NVIRTUALS (super_class);
2398 dtable_count = integer_zero_node;
2400 type_name = TYPE_NAME (this_class);
2401 if (!flag_indirect_dispatch
2402 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2404 /* An abstract class can have methods which are declared only in
2405 an implemented interface. These are called "Miranda
2406 methods". We make a dummy method entry for such methods
2408 add_miranda_methods (this_class, this_class);
2411 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2413 for (method_decl = TYPE_METHODS (this_class);
2414 method_decl; method_decl = TREE_CHAIN (method_decl))
2415 dtable_count = layout_class_method (this_class, super_class,
2416 method_decl, dtable_count);
2418 TYPE_NVIRTUALS (this_class) = dtable_count;
2421 /* Return the index of METHOD in INTERFACE. This index begins at 1
2422 and is used as an argument for _Jv_LookupInterfaceMethodIdx(). */
2424 get_interface_method_index (tree method, tree interface)
2429 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth))
2433 /* We don't want to put <clinit> into the interface table. */
2434 if (! ID_CLINIT_P (DECL_NAME (meth)))
2436 gcc_assert (meth != NULL_TREE);
2440 /* Lay METHOD_DECL out, returning a possibly new value of
2441 DTABLE_COUNT. Also mangle the method's name. */
2444 layout_class_method (tree this_class, tree super_class,
2445 tree method_decl, tree dtable_count)
2447 tree method_name = DECL_NAME (method_decl);
2449 TREE_PUBLIC (method_decl) = 1;
2450 /* Considered external until we know what classes are being
2451 compiled into this object file. */
2452 DECL_EXTERNAL (method_decl) = 1;
2454 if (ID_INIT_P (method_name))
2456 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2458 for (ptr = p; *ptr; )
2463 DECL_CONSTRUCTOR_P (method_decl) = 1;
2464 build_java_argument_signature (TREE_TYPE (method_decl));
2466 else if (! METHOD_STATIC (method_decl))
2469 build_java_argument_signature (TREE_TYPE (method_decl));
2470 bool method_override = false;
2471 tree super_method = lookup_argument_method (super_class, method_name,
2473 if (super_method != NULL_TREE
2474 && ! METHOD_DUMMY (super_method))
2476 method_override = true;
2477 if (! METHOD_PUBLIC (super_method) &&
2478 ! METHOD_PROTECTED (super_method))
2480 /* Don't override private method, or default-access method in
2482 if (METHOD_PRIVATE (super_method) ||
2483 ! in_same_package (TYPE_NAME (this_class),
2484 TYPE_NAME (super_class)))
2485 method_override = false;
2488 if (method_override)
2490 tree method_index = get_method_index (super_method);
2491 set_method_index (method_decl, method_index);
2492 if (method_index == NULL_TREE
2493 && ! flag_indirect_dispatch
2494 && !CLASS_FROM_SOURCE_P (this_class)
2495 && ! DECL_ARTIFICIAL (super_method))
2496 error ("non-static method %q+D overrides static method",
2499 else if (this_class == object_type_node
2500 && (METHOD_FINAL (method_decl)
2501 || METHOD_PRIVATE (method_decl)))
2503 /* We don't generate vtable entries for final Object
2504 methods. This is simply to save space, since every
2505 object would otherwise have to define them. */
2507 else if (! METHOD_PRIVATE (method_decl)
2510 /* We generate vtable entries for final methods because they
2511 may one day be changed to non-final. */
2512 set_method_index (method_decl, dtable_count);
2513 dtable_count = fold_build2 (PLUS_EXPR, integer_type_node,
2514 dtable_count, integer_one_node);
2518 return dtable_count;
2522 register_class (void)
2526 if (!registered_class)
2527 registered_class = VEC_alloc (tree, gc, 8);
2529 if (flag_indirect_classes)
2530 node = current_class;
2532 node = TREE_OPERAND (build_class_ref (current_class), 0);
2533 VEC_safe_push (tree, gc, registered_class, node);
2536 /* Emit a function that calls _Jv_RegisterNewClasses with a list of
2537 all the classes we have emitted. */
2540 emit_indirect_register_classes (tree *list_p)
2542 tree klass, t, register_class_fn;
2545 tree init = NULL_TREE;
2546 int size = VEC_length (tree, registered_class) * 2 + 1;
2547 tree class_array_type
2548 = build_prim_array_type (ptr_type_node, size);
2549 tree cdecl = build_decl (VAR_DECL, get_identifier ("_Jv_CLS"),
2551 tree reg_class_list;
2552 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2554 init = tree_cons (NULL_TREE,
2555 fold_convert (ptr_type_node,
2556 build_static_class_ref (klass)), init);
2559 fold_convert (ptr_type_node,
2560 build_address_of (build_classdollar_field (klass))),
2563 init = tree_cons (NULL_TREE, integer_zero_node, init);
2564 DECL_INITIAL (cdecl) = build_constructor_from_list (class_array_type,
2566 TREE_CONSTANT (DECL_INITIAL (cdecl)) = 1;
2567 TREE_STATIC (cdecl) = 1;
2568 DECL_ARTIFICIAL (cdecl) = 1;
2569 DECL_IGNORED_P (cdecl) = 1;
2570 TREE_READONLY (cdecl) = 1;
2571 TREE_CONSTANT (cdecl) = 1;
2572 rest_of_decl_compilation (cdecl, 1, 0);
2573 reg_class_list = fold_convert (ptr_type_node, build_address_of (cdecl));
2575 t = build_function_type_list (void_type_node,
2576 build_pointer_type (ptr_type_node), NULL);
2577 t = build_decl (FUNCTION_DECL,
2578 get_identifier ("_Jv_RegisterNewClasses"), t);
2579 TREE_PUBLIC (t) = 1;
2580 DECL_EXTERNAL (t) = 1;
2581 register_class_fn = t;
2582 t = tree_cons (NULL, reg_class_list, NULL);
2583 t = build_function_call_expr (register_class_fn, t);
2584 append_to_statement_list (t, list_p);
2588 /* Emit something to register classes at start-up time.
2590 The preferred mechanism is through the .jcr section, which contain
2591 a list of pointers to classes which get registered during constructor
2594 The fallback mechanism is to add statements to *LIST_P to call
2595 _Jv_RegisterClass for each class in this file. These statements will
2596 be added to a static constructor function for this translation unit. */
2599 emit_register_classes (tree *list_p)
2601 if (registered_class == NULL)
2604 if (flag_indirect_classes)
2606 emit_indirect_register_classes (list_p);
2610 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2611 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2612 but lack suitable crtbegin/end objects or linker support. These
2613 targets can override the default in tm.h to use the fallback mechanism. */
2614 if (TARGET_USE_JCR_SECTION)
2619 #ifdef JCR_SECTION_NAME
2620 switch_to_section (get_section (JCR_SECTION_NAME, SECTION_WRITE, NULL));
2622 /* A target has defined TARGET_USE_JCR_SECTION,
2623 but doesn't have a JCR_SECTION_NAME. */
2626 assemble_align (POINTER_SIZE);
2628 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2630 t = build_fold_addr_expr (klass);
2631 output_constant (t, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE);
2636 tree klass, t, register_class_fn;
2639 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2640 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2641 TREE_PUBLIC (t) = 1;
2642 DECL_EXTERNAL (t) = 1;
2643 register_class_fn = t;
2645 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2647 t = build_fold_addr_expr (klass);
2648 t = tree_cons (NULL, t, NULL);
2649 t = build_function_call_expr (register_class_fn, t);
2650 append_to_statement_list (t, list_p);
2655 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2658 build_symbol_entry (tree decl, tree special)
2660 tree clname, name, signature, sym;
2661 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2662 /* ??? Constructors are given the name foo.foo all the way through
2663 the compiler, but in the method table they're all renamed
2664 foo.<init>. So, we have to do the same here unless we want an
2665 unresolved reference at runtime. */
2666 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2667 && DECL_CONSTRUCTOR_P (decl))
2668 ? init_identifier_node
2669 : DECL_NAME (decl));
2670 signature = build_java_signature (TREE_TYPE (decl));
2671 signature = build_utf8_ref (unmangle_classname
2672 (IDENTIFIER_POINTER (signature),
2673 IDENTIFIER_LENGTH (signature)));
2674 /* SPECIAL is either NULL_TREE or integer_one_node. We emit
2675 signature addr+1 if SPECIAL, and this indicates to the runtime
2676 system that this is a "special" symbol, i.e. one that should
2677 bypass access controls. */
2678 if (special != NULL_TREE)
2679 signature = build2 (PLUS_EXPR, TREE_TYPE (signature), signature, special);
2681 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2682 PUSH_FIELD_VALUE (sym, "clname", clname);
2683 PUSH_FIELD_VALUE (sym, "name", name);
2684 PUSH_FIELD_VALUE (sym, "signature", signature);
2685 FINISH_RECORD_CONSTRUCTOR (sym);
2686 TREE_CONSTANT (sym) = 1;
2687 TREE_INVARIANT (sym) = 1;
2692 /* Emit a symbol table: used by -findirect-dispatch. */
2695 emit_symbol_table (tree name, tree the_table, tree decl_list,
2696 tree the_syms_decl, tree the_array_element_type,
2699 tree method_list, method, table, list, null_symbol;
2700 tree table_size, the_array_type;
2703 /* Only emit a table if this translation unit actually made any
2704 references via it. */
2705 if (decl_list == NULL_TREE)
2708 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2710 method_list = decl_list;
2712 while (method_list != NULL_TREE)
2714 tree special = TREE_PURPOSE (method_list);
2715 method = TREE_VALUE (method_list);
2716 list = tree_cons (NULL_TREE, build_symbol_entry (method, special), list);
2717 method_list = TREE_CHAIN (method_list);
2721 /* Terminate the list with a "null" entry. */
2722 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2723 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2724 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2725 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2726 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2727 TREE_CONSTANT (null_symbol) = 1;
2728 TREE_INVARIANT (null_symbol) = 1;
2729 list = tree_cons (NULL_TREE, null_symbol, list);
2731 /* Put the list in the right order and make it a constructor. */
2732 list = nreverse (list);
2733 table = build_constructor_from_list (symbols_array_type, list);
2735 /* Make it the initial value for otable_syms and emit the decl. */
2736 DECL_INITIAL (the_syms_decl) = table;
2737 DECL_ARTIFICIAL (the_syms_decl) = 1;
2738 DECL_IGNORED_P (the_syms_decl) = 1;
2739 rest_of_decl_compilation (the_syms_decl, 1, 0);
2741 /* Now that its size is known, redefine the table as an
2742 uninitialized static array of INDEX + 1 elements. The extra entry
2743 is used by the runtime to track whether the table has been
2746 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2747 the_array_type = build_array_type (the_array_element_type, table_size);
2748 the_table = build_decl (VAR_DECL, name, the_array_type);
2749 TREE_STATIC (the_table) = 1;
2750 TREE_READONLY (the_table) = 1;
2751 rest_of_decl_compilation (the_table, 1, 0);
2756 /* Make an entry for the catch_classes list. */
2758 make_catch_class_record (tree catch_class, tree classname)
2761 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2762 START_RECORD_CONSTRUCTOR (entry, type);
2763 PUSH_FIELD_VALUE (entry, "address", catch_class);
2764 PUSH_FIELD_VALUE (entry, "classname", classname);
2765 FINISH_RECORD_CONSTRUCTOR (entry);
2770 /* Generate the list of Throwable classes that are caught by exception
2771 handlers in this class. */
2773 emit_catch_table (tree this_class)
2775 tree table, table_size, array_type;
2776 TYPE_CATCH_CLASSES (this_class) =
2778 make_catch_class_record (null_pointer_node, null_pointer_node),
2779 TYPE_CATCH_CLASSES (this_class));
2780 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2781 TYPE_CATCH_CLASSES (this_class) =
2783 make_catch_class_record (null_pointer_node, null_pointer_node),
2784 TYPE_CATCH_CLASSES (this_class));
2785 table_size = build_index_type
2786 (build_int_cst (NULL_TREE,
2787 list_length (TYPE_CATCH_CLASSES (this_class))));
2789 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2792 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2793 DECL_INITIAL (table) =
2794 build_constructor_from_list (array_type, TYPE_CATCH_CLASSES (this_class));
2795 TREE_STATIC (table) = 1;
2796 TREE_READONLY (table) = 1;
2797 DECL_IGNORED_P (table) = 1;
2798 rest_of_decl_compilation (table, 1, 0);
2802 /* Given a type, return the signature used by
2803 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2804 same as build_java_signature() because we want the canonical array
2808 build_signature_for_libgcj (tree type)
2812 sig = build_java_signature (type);
2813 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2814 IDENTIFIER_LENGTH (sig)));
2818 /* Add an entry to the type assertion table. Callback used during hashtable
2822 add_assertion_table_entry (void **htab_entry, void *ptr)
2825 tree code_val, op1_utf8, op2_utf8;
2826 tree *list = (tree *) ptr;
2827 type_assertion *as = (type_assertion *) *htab_entry;
2829 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2831 if (as->op1 == NULL_TREE)
2832 op1_utf8 = null_pointer_node;
2834 op1_utf8 = build_signature_for_libgcj (as->op1);
2836 if (as->op2 == NULL_TREE)
2837 op2_utf8 = null_pointer_node;
2839 op2_utf8 = build_signature_for_libgcj (as->op2);
2841 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2842 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2843 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2844 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2845 FINISH_RECORD_CONSTRUCTOR (entry);
2847 *list = tree_cons (NULL_TREE, entry, *list);
2851 /* Generate the type assertion table for CLASS, and return its DECL. */
2854 emit_assertion_table (tree class)
2856 tree null_entry, ctor, table_decl;
2857 tree list = NULL_TREE;
2858 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2860 /* Iterate through the hash table. */
2861 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2863 /* Finish with a null entry. */
2864 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2865 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2866 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2867 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2868 FINISH_RECORD_CONSTRUCTOR (null_entry);
2870 list = tree_cons (NULL_TREE, null_entry, list);
2872 /* Put the list in the right order and make it a constructor. */
2873 list = nreverse (list);
2874 ctor = build_constructor_from_list (assertion_table_type, list);
2876 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2877 assertion_table_type);
2879 TREE_STATIC (table_decl) = 1;
2880 TREE_READONLY (table_decl) = 1;
2881 TREE_CONSTANT (table_decl) = 1;
2882 DECL_IGNORED_P (table_decl) = 1;
2884 DECL_INITIAL (table_decl) = ctor;
2885 DECL_ARTIFICIAL (table_decl) = 1;
2886 rest_of_decl_compilation (table_decl, 1, 0);
2892 init_class_processing (void)
2894 fields_ident = get_identifier ("fields");
2895 info_ident = get_identifier ("info");
2897 gcc_obstack_init (&temporary_obstack);
2900 static hashval_t java_treetreehash_hash (const void *);
2901 static int java_treetreehash_compare (const void *, const void *);
2903 /* A hash table mapping trees to trees. Used generally. */
2905 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2908 java_treetreehash_hash (const void *k_p)
2910 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2911 return JAVA_TREEHASHHASH_H (k->key);
2915 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2917 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2918 tree k2 = (tree) k2_p;
2919 return (k1->key == k2);
2923 java_treetreehash_find (htab_t ht, tree t)
2925 struct treetreehash_entry *e;
2926 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2927 e = htab_find_with_hash (ht, t, hv);
2935 java_treetreehash_new (htab_t ht, tree t)
2938 struct treetreehash_entry *tthe;
2939 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2941 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2944 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2949 tthe = (struct treetreehash_entry *) *e;
2950 return &tthe->value;
2954 java_treetreehash_create (size_t size, int gc)
2957 return htab_create_ggc (size, java_treetreehash_hash,
2958 java_treetreehash_compare, NULL);
2960 return htab_create_alloc (size, java_treetreehash_hash,
2961 java_treetreehash_compare, free, xcalloc, free);
2964 /* Break down qualified IDENTIFIER into package and class-name components.
2965 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2966 "pkg.foo", and RIGHT to "Bar". */
2969 split_qualified_name (tree *left, tree *right, tree source)
2972 int l = IDENTIFIER_LENGTH (source);
2974 base = alloca (l + 1);
2975 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2977 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2979 while (*p != '.' && p != base)
2982 /* We didn't find a '.'. Return an error. */
2988 *right = get_identifier (p+1);
2989 *left = get_identifier (base);
2994 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2995 if the classes are from the same package. */
2998 in_same_package (tree name1, tree name2)
3004 if (TREE_CODE (name1) == TYPE_DECL)
3005 name1 = DECL_NAME (name1);
3006 if (TREE_CODE (name2) == TYPE_DECL)
3007 name2 = DECL_NAME (name2);
3009 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
3010 /* One in empty package. */
3013 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
3014 /* Both in empty package. */
3017 split_qualified_name (&pkg1, &tmp, name1);
3018 split_qualified_name (&pkg2, &tmp, name2);
3020 return (pkg1 == pkg2);
3023 #include "gt-java-class.h"