1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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> */
33 #include "java-tree.h"
43 static tree make_method_value PARAMS ((tree));
44 static tree build_java_method_type PARAMS ((tree, tree, int));
45 static int32 hashUtf8String PARAMS ((const char *, int));
46 static tree make_field_value PARAMS ((tree));
47 static tree get_dispatch_vector PARAMS ((tree));
48 static tree get_dispatch_table PARAMS ((tree, tree));
49 static void add_interface_do PARAMS ((tree, tree, int));
50 static tree maybe_layout_super_class PARAMS ((tree, tree));
51 static int assume_compiled PARAMS ((const char *));
52 static struct hash_entry *init_test_hash_newfunc PARAMS ((struct hash_entry *,
55 static rtx registerClass_libfunc;
57 extern struct obstack permanent_obstack;
58 struct obstack temporary_obstack;
60 /* The compiler generates different code depending on whether or not
61 it can assume certain classes have been compiled down to native
62 code or not. The compiler options -fassume-compiled= and
63 -fno-assume-compiled= are used to create a tree of
64 assume_compiled_node objects. This tree is queried to determine if
65 a class is assume to be compiled or not. Each node in the tree
66 represents either a package or a specific class. */
68 typedef struct assume_compiled_node_struct
70 /* The class or package name. */
73 /* Non-zero if this represents an exclusion. */
76 /* Pointers to other nodes in the tree. */
77 struct assume_compiled_node_struct *parent;
78 struct assume_compiled_node_struct *sibling;
79 struct assume_compiled_node_struct *child;
80 } assume_compiled_node;
82 static assume_compiled_node *find_assume_compiled_node
83 PARAMS ((assume_compiled_node *, const char *));
85 /* This is the root of the include/exclude tree. */
87 static assume_compiled_node *assume_compiled_tree;
89 static tree class_roots[5]
90 = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
91 #define registered_class class_roots[0]
92 #define fields_ident class_roots[1] /* get_identifier ("fields") */
93 #define info_ident class_roots[2] /* get_identifier ("info") */
94 #define class_list class_roots[3]
95 #define class_dtable_decl class_roots[4]
97 /* Return the node that most closely represents the class whose name
98 is IDENT. Start the search from NODE. Return NULL if an
99 appropriate node does not exist. */
101 static assume_compiled_node *
102 find_assume_compiled_node (node, ident)
103 assume_compiled_node *node;
108 size_t node_ident_length = strlen (node->ident);
110 /* node_ident_length is zero at the root of the tree. If the
111 identifiers are the same length, then we have matching
112 classes. Otherwise check if we've matched an enclosing
115 if (node_ident_length == 0
116 || (strncmp (ident, node->ident, node_ident_length) == 0
117 && (strlen (ident) == node_ident_length
118 || ident[node_ident_length] == '.')))
120 /* We've found a match, however, there might be a more
123 assume_compiled_node *found = find_assume_compiled_node (node->child,
131 /* No match yet. Continue through the sibling list. */
132 node = node->sibling;
135 /* No match at all in this tree. */
139 /* Add a new IDENT to the include/exclude tree. It's an exclusion
140 if EXCLUDEP is non-zero. */
143 add_assume_compiled (ident, excludep)
147 assume_compiled_node *parent;
148 assume_compiled_node *node =
149 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
151 node->ident = xstrdup (ident);
152 node->excludep = excludep;
155 /* Create the root of the tree if it doesn't exist yet. */
157 if (NULL == assume_compiled_tree)
159 assume_compiled_tree =
160 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
161 assume_compiled_tree->ident = "";
162 assume_compiled_tree->excludep = 0;
163 assume_compiled_tree->sibling = NULL;
164 assume_compiled_tree->child = NULL;
165 assume_compiled_tree->parent = NULL;
168 /* Calling the function with the empty string means we're setting
169 excludep for the root of the hierarchy. */
173 assume_compiled_tree->excludep = excludep;
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_assume_compiled_node (assume_compiled_tree, ident);
181 if (ident[strlen (parent->ident)] != '.')
182 parent = parent->parent;
184 /* Insert NODE into the tree. */
186 node->parent = parent;
187 node->sibling = parent->child;
188 parent->child = node;
191 /* Returns non-zero if IDENT is the name of a class that the compiler
192 should assume has been compiled to FIXME */
195 assume_compiled (ident)
198 assume_compiled_node *i;
201 if (NULL == assume_compiled_tree)
204 i = find_assume_compiled_node (assume_compiled_tree,
207 result = ! i->excludep;
212 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
213 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
214 Also, PREFIX is prepended, and SUFFIX is appended. */
217 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
218 const char* old_name;
225 int prefix_len = strlen (prefix);
226 int suffix_len = strlen (suffix);
227 int i = prefix_len + old_length + suffix_len + 1;
231 char *buffer = (char *)alloca (i);
233 strcpy (buffer, prefix);
234 for (i = 0; i < old_length; i++)
236 char ch = old_name[i];
239 buffer[prefix_len + i] = ch;
241 strcpy (buffer + prefix_len + old_length, suffix);
242 return get_identifier (buffer);
245 /* Return an IDENTIFIER_NODE the same as OLD_ID,
246 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
247 Also, PREFIX is prepended, and SUFFIX is appended. */
250 identifier_subst (old_id, prefix, old_char, new_char, suffix)
257 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
258 prefix, old_char, new_char, suffix);
261 /* Generate a valid C identifier from the name of the class TYPE,
262 prefixed by PREFIX. */
265 mangled_classname (prefix, type)
269 tree ident = TYPE_NAME (type);
270 if (TREE_CODE (ident) != IDENTIFIER_NODE)
271 ident = DECL_NAME (ident);
272 return identifier_subst (ident, prefix, '.', '_', "");
279 type = make_node (RECORD_TYPE);
280 #ifdef JAVA_USE_HANDLES
281 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
282 build_pointer_type (type));
283 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
284 methodtable_ptr_type);
285 tree handle_type = make_node (RECORD_TYPE);
286 TREE_CHAIN (field1) = field2;
287 TYPE_FIELDS (handle_type) = field1;
288 TYPE_BINFO (type) = make_tree_vec (7);
289 TYPE_BINFO (handle_type) = make_tree_vec (7);
290 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
291 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
293 TYPE_BINFO (type) = make_tree_vec (6);
295 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
300 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
301 and where each of the constituents is separated by '/',
302 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
305 unmangle_classname (name, name_length)
306 const char *name; int name_length;
308 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
309 /* It's not sufficient to compare to_return and get_identifier
310 (name) to determine whether to_return is qualified. There are
311 cases in signature analysis where name will be stripped of a
313 name = IDENTIFIER_POINTER (to_return);
317 QUALIFIED_P (to_return) = 1;
325 push_class (class_type, class_name)
326 tree class_type, class_name;
328 tree decl, signature;
329 const char *save_input_filename = input_filename;
330 int save_lineno = lineno;
331 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
332 CLASS_P (class_type) = 1;
333 input_filename = IDENTIFIER_POINTER (source_name);
335 decl = build_decl (TYPE_DECL, class_name, class_type);
337 /* dbxout needs a DECL_SIZE if in gstabs mode */
338 DECL_SIZE (decl) = integer_zero_node;
340 input_filename = save_input_filename;
341 lineno = save_lineno;
342 signature = identifier_subst (class_name, "L", '.', '/', ";");
343 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
345 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
346 both a typedef and in the struct name-space. We may want to re-visit
347 this later, but for now it reduces the changes needed for gdb. */
348 DECL_ARTIFICIAL (decl) = 1;
350 pushdecl_top_level (decl);
351 #ifdef JAVA_USE_HANDLES
353 tree handle_name = identifier_subst (class_name,
354 "Handle$", '.', '.', "");
355 tree handle_decl = build_decl (TYPE_DECL, handle_name,
356 CLASS_TO_HANDLE_TYPE (class_type));
357 pushdecl (handle_decl);
364 /* Finds the (global) class named NAME. Creates the class if not found.
365 Also creates associated TYPE_DECL.
366 Does not check if the class actually exists, load the class,
367 fill in field or methods, or do layout_type. */
373 tree decl = IDENTIFIER_CLASS_VALUE (name);
374 if (decl == NULL_TREE)
375 decl = push_class (make_class (), name);
376 return TREE_TYPE (decl);
380 set_super_info (access_flags, this_class, super_class, interfaces_count)
384 int interfaces_count;
386 int total_supers = interfaces_count;
387 tree class_decl = TYPE_NAME (this_class);
391 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
394 tree super_binfo = make_tree_vec (6);
395 BINFO_TYPE (super_binfo) = super_class;
396 BINFO_OFFSET (super_binfo) = integer_zero_node;
397 TREE_VIA_PUBLIC (super_binfo) = 1;
398 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
400 CLASS_HAS_SUPER (this_class) = 1;
403 set_class_decl_access_flags (access_flags, class_decl);
407 set_class_decl_access_flags (access_flags, class_decl)
411 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
412 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
413 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
414 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
415 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
416 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
417 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
418 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
421 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
422 direct sub-classes of Object are 1, and so on. */
429 if (! CLASS_LOADED_P (clas))
430 load_class (clas, 1);
431 if (TYPE_SIZE (clas) == error_mark_node)
433 while (clas != object_type_node)
436 clas = TYPE_BINFO_BASETYPE (clas, 0);
441 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
444 interface_of_p (type1, type2)
450 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
452 n = TREE_VEC_LENGTH (basetype_vec);
453 for (i = 0; i < n; i++)
455 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
456 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
459 for (i = 0; i < n; i++)
461 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
462 if (vec_elt && BINFO_TYPE (vec_elt)
463 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
469 /* Return true iff TYPE1 inherits from TYPE2. */
472 inherits_from_p (type1, type2)
475 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
479 type1 = CLASSTYPE_SUPER (type1);
484 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
487 enclosing_context_p (type1, type2)
490 if (!INNER_CLASS_TYPE_P (type2))
493 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
495 type2 = (INNER_CLASS_TYPE_P (type2) ?
496 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
505 /* Return 1 iff there exists a common enclosing context between TYPE1
508 int common_enclosing_context_p (type1, type2)
511 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
514 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
515 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
516 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
519 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
520 current = (PURE_INNER_CLASS_TYPE_P (current) ?
521 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
523 if (type1 == current)
530 add_interface_do (basetype_vec, interface_class, i)
531 tree basetype_vec, interface_class;
534 tree interface_binfo = make_tree_vec (6);
535 BINFO_TYPE (interface_binfo) = interface_class;
536 BINFO_OFFSET (interface_binfo) = integer_zero_node;
537 TREE_VIA_VIRTUAL (interface_binfo) = 1;
538 TREE_VIA_PUBLIC (interface_binfo) = 1;
539 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
542 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
543 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
544 if attempt is made to add it twice. */
547 maybe_add_interface (this_class, interface_class)
548 tree this_class, interface_class;
550 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
552 int n = TREE_VEC_LENGTH (basetype_vec);
557 error ("internal error - too many interface type");
560 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
562 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
563 return interface_class;
565 add_interface_do (basetype_vec, interface_class, i);
569 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
572 add_interface (this_class, interface_class)
573 tree this_class, interface_class;
575 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
577 int n = TREE_VEC_LENGTH (basetype_vec);
582 error ("internal error - too many interface type");
585 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
588 add_interface_do (basetype_vec, interface_class, i);
592 /* Return the address of a pointer to the first FUNCTION_DECL
593 in the list (*LIST) whose DECL_NAME is NAME. */
596 find_named_method (list, name)
600 while (*list && DECL_NAME (*list) != name)
601 list = &TREE_CHAIN (*list);
607 build_java_method_type (fntype, this_class, access_flags)
612 if (access_flags & ACC_STATIC)
614 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
617 static struct hash_entry *
618 init_test_hash_newfunc (entry, table, string)
619 struct hash_entry *entry;
620 struct hash_table *table;
621 hash_table_key string ATTRIBUTE_UNUSED;
623 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
626 ret = ((struct init_test_hash_entry *)
627 hash_allocate (table, sizeof (struct init_test_hash_entry)));
631 ret->init_test_decl = 0;
632 return (struct hash_entry *) ret;
635 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
636 (parse.y). The hash of a tree node is its pointer value, comparison
640 java_hash_hash_tree_node (k)
647 java_hash_compare_tree_node (k1, k2)
651 return ((char*) k1 == (char*) k2);
655 add_method_1 (handle_class, access_flags, name, function_type)
661 tree method_type, fndecl;
663 method_type = build_java_method_type (function_type,
664 handle_class, access_flags);
666 fndecl = build_decl (FUNCTION_DECL, name, method_type);
667 DECL_CONTEXT (fndecl) = handle_class;
669 DECL_LANG_SPECIFIC (fndecl)
670 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
672 /* Initialize the static initializer test table. */
673 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
674 init_test_hash_newfunc, java_hash_hash_tree_node,
675 java_hash_compare_tree_node);
677 /* Initialize the initialized (static) class table. */
678 if (access_flags & ACC_STATIC)
679 hash_table_init (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
680 init_test_hash_newfunc, java_hash_hash_tree_node,
681 java_hash_compare_tree_node);
683 /* Initialize the static method invocation compound table */
684 if (STATIC_CLASS_INIT_OPT_P ())
685 hash_table_init (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl),
686 init_test_hash_newfunc, java_hash_hash_tree_node,
687 java_hash_compare_tree_node);
689 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
690 TYPE_METHODS (handle_class) = fndecl;
692 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
693 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
694 if (access_flags & ACC_PRIVATE)
695 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
696 if (access_flags & ACC_NATIVE)
698 METHOD_NATIVE (fndecl) = 1;
699 DECL_EXTERNAL (fndecl) = 1;
701 if (access_flags & ACC_STATIC)
702 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
703 if (access_flags & ACC_FINAL)
704 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
705 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
706 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
707 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
711 /* Add a method to THIS_CLASS.
712 The method's name is NAME.
713 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
716 add_method (this_class, access_flags, name, method_sig)
722 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
723 tree function_type, fndecl;
724 const unsigned char *sig
725 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
728 fatal_error ("bad method signature");
730 function_type = get_type_from_signature (method_sig);
731 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
732 set_java_signature (TREE_TYPE (fndecl), method_sig);
737 add_field (class, name, field_type, flags)
743 int is_static = (flags & ACC_STATIC) != 0;
745 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
746 TREE_CHAIN (field) = TYPE_FIELDS (class);
747 TYPE_FIELDS (class) = field;
748 DECL_CONTEXT (field) = class;
750 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
751 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
752 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
753 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
754 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
755 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
758 FIELD_STATIC (field) = 1;
759 /* Always make field externally visible. This is required so
760 that native methods can always access the field. */
761 TREE_PUBLIC (field) = 1;
766 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
769 set_constant_value (field, constant)
770 tree field, constant;
772 if (field == NULL_TREE)
773 warning ("misplaced ConstantValue attribute (not in any field)");
774 else if (DECL_INITIAL (field) != NULL_TREE)
775 warning ("duplicate ConstanValue atribute for field '%s'",
776 IDENTIFIER_POINTER (DECL_NAME (field)));
779 DECL_INITIAL (field) = constant;
780 if (TREE_TYPE (constant) != TREE_TYPE (field)
781 && ! (TREE_TYPE (constant) == int_type_node
782 && INTEGRAL_TYPE_P (TREE_TYPE (field))
783 && TYPE_PRECISION (TREE_TYPE (field)) <= 32))
784 warning ("ConstantValue attribute of field '%s' has wrong type",
785 IDENTIFIER_POINTER (DECL_NAME (field)));
786 if (FIELD_FINAL (field))
787 DECL_FIELD_FINAL_IUD (field) = 1;
791 /* Count the number of Unicode chars encoded in a given Ut8 string. */
795 strLengthUtf8 (str, len)
799 register unsigned char* ptr = (unsigned char*) str;
800 register unsigned char *limit = ptr + len;
802 for (; ptr < limit; str_length++) {
803 if (UTF8_GET (ptr, limit) < 0)
811 /* Calculate a hash value for a string encoded in Utf8 format.
812 * This returns the same hash value as specified for java.lang.String.hashCode.
816 hashUtf8String (str, len)
820 register const unsigned char* ptr = (const unsigned char*) str;
821 register const unsigned char *limit = ptr + len;
825 int ch = UTF8_GET (ptr, limit);
826 /* Updated specification from
827 http://www.javasoft.com/docs/books/jls/clarify.html. */
828 hash = (31 * hash) + ch;
833 tree utf8_decl_list = NULL_TREE;
836 build_utf8_ref (name)
839 const char * name_ptr = IDENTIFIER_POINTER(name);
840 int name_len = IDENTIFIER_LENGTH(name);
842 tree ctype, field = NULL_TREE, str_type, cinit, string;
843 static int utf8_count = 0;
845 tree ref = IDENTIFIER_UTF8_REF (name);
847 if (ref != NULL_TREE)
850 ctype = make_node (RECORD_TYPE);
851 str_type = build_prim_array_type (unsigned_byte_type_node,
852 name_len + 1); /* Allow for final '\0'. */
853 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
854 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
855 PUSH_FIELD (ctype, field, "data", str_type);
856 FINISH_RECORD (ctype);
857 START_RECORD_CONSTRUCTOR (cinit, ctype);
858 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
859 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
860 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
861 string = build_string (name_len, name_ptr);
862 TREE_TYPE (string) = str_type;
863 PUSH_FIELD_VALUE (cinit, "data", string);
864 FINISH_RECORD_CONSTRUCTOR (cinit);
865 TREE_CONSTANT (cinit) = 1;
867 /* Generate a unique-enough identifier. */
868 sprintf(buf, "_Utf%d", ++utf8_count);
870 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
871 TREE_STATIC (decl) = 1;
872 DECL_ARTIFICIAL (decl) = 1;
873 DECL_IGNORED_P (decl) = 1;
874 TREE_READONLY (decl) = 1;
875 TREE_THIS_VOLATILE (decl) = 0;
876 DECL_INITIAL (decl) = cinit;
877 TREE_CHAIN (decl) = utf8_decl_list;
878 layout_decl (decl, 0);
880 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
881 utf8_decl_list = decl;
882 make_decl_rtl (decl, (char*) 0);
883 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
884 IDENTIFIER_UTF8_REF (name) = ref;
888 /* Build a reference to the class TYPE.
889 Also handles primitive types and array types. */
892 build_class_ref (type)
895 int is_compiled = is_compiled_class (type);
898 tree ref, decl_name, decl;
899 if (TREE_CODE (type) == POINTER_TYPE)
900 type = TREE_TYPE (type);
901 if (TREE_CODE (type) == RECORD_TYPE)
903 if (TYPE_SIZE (type) == error_mark_node)
904 return null_pointer_node;
905 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
906 "", '/', '/', ".class");
907 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
908 if (decl == NULL_TREE)
910 decl = build_decl (VAR_DECL, decl_name, class_type_node);
911 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
912 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
913 TREE_STATIC (decl) = 1;
914 TREE_PUBLIC (decl) = 1;
915 DECL_IGNORED_P (decl) = 1;
916 DECL_ARTIFICIAL (decl) = 1;
917 SET_DECL_ASSEMBLER_NAME (decl,
918 java_mangle_class_field
919 (&temporary_obstack, type));
920 make_decl_rtl (decl, NULL);
921 pushdecl_top_level (decl);
922 if (is_compiled == 1)
923 DECL_EXTERNAL (decl) = 1;
930 if (flag_emit_class_files)
932 const char *prim_class_name;
934 if (type == char_type_node)
935 prim_class_name = "java.lang.Character";
936 else if (type == boolean_type_node)
937 prim_class_name = "java.lang.Boolean";
938 else if (type == byte_type_node)
939 prim_class_name = "java.lang.Byte";
940 else if (type == short_type_node)
941 prim_class_name = "java.lang.Short";
942 else if (type == int_type_node)
943 prim_class_name = "java.lang.Integer";
944 else if (type == long_type_node)
945 prim_class_name = "java.lang.Long";
946 else if (type == float_type_node)
947 prim_class_name = "java.lang.Float";
948 else if (type == double_type_node)
949 prim_class_name = "java.lang.Double";
950 else if (type == void_type_node)
951 prim_class_name = "java.lang.Void";
955 prim_class = lookup_class (get_identifier (prim_class_name));
956 return build (COMPONENT_REF, NULL_TREE,
957 prim_class, TYPE_identifier_node);
959 decl_name = TYPE_NAME (type);
960 if (TREE_CODE (decl_name) == TYPE_DECL)
961 decl_name = DECL_NAME (decl_name);
962 name = IDENTIFIER_POINTER (decl_name);
963 if (strncmp (name, "promoted_", 9) == 0)
965 sprintf (buffer, "_Jv_%sClass", name);
966 decl_name = get_identifier (buffer);
967 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
968 if (decl == NULL_TREE)
970 decl = build_decl (VAR_DECL, decl_name, class_type_node);
971 TREE_STATIC (decl) = 1;
972 TREE_PUBLIC (decl) = 1;
973 make_decl_rtl (decl, NULL);
974 pushdecl_top_level (decl);
975 if (is_compiled == 1)
976 DECL_EXTERNAL (decl) = 1;
980 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
987 index = alloc_class_constant (type);
988 cl = build_ref_from_constant_pool (index);
989 TREE_TYPE (cl) = promote_type (class_ptr_type);
995 build_static_field_ref (fdecl)
998 tree fclass = DECL_CONTEXT (fdecl);
999 int is_compiled = is_compiled_class (fclass);
1002 if (!DECL_RTL_SET_P (fdecl))
1004 if (is_compiled == 1)
1005 DECL_EXTERNAL (fdecl) = 1;
1006 make_decl_rtl (fdecl, NULL);
1013 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1014 tree ref = build_class_ref (fclass);
1016 int field_index = 0;
1017 ref = build1 (INDIRECT_REF, class_type_node, ref);
1018 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1019 lookup_field (&class_type_node, fields_ident));
1021 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1025 if (fld == NULL_TREE)
1026 fatal_error ("field '%s' not found in class",
1027 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1028 if (FIELD_STATIC (fld))
1031 field_index *= int_size_in_bytes (field_type_node);
1032 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1033 ref, build_int_2 (field_index, 0)));
1034 ref = build1 (INDIRECT_REF, field_type_node, ref);
1035 ref = build (COMPONENT_REF, field_info_union_node,
1036 ref, lookup_field (&field_type_node, info_ident));
1037 ref = build (COMPONENT_REF, ptr_type_node,
1038 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1039 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1044 get_access_flags_from_decl (decl)
1047 int access_flags = 0;
1048 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1050 if (FIELD_STATIC (decl))
1051 access_flags |= ACC_STATIC;
1052 if (FIELD_PUBLIC (decl))
1053 access_flags |= ACC_PUBLIC;
1054 if (FIELD_PROTECTED (decl))
1055 access_flags |= ACC_PROTECTED;
1056 if (FIELD_PRIVATE (decl))
1057 access_flags |= ACC_PRIVATE;
1058 if (FIELD_FINAL (decl))
1059 access_flags |= ACC_FINAL;
1060 if (FIELD_VOLATILE (decl))
1061 access_flags |= ACC_VOLATILE;
1062 if (FIELD_TRANSIENT (decl))
1063 access_flags |= ACC_TRANSIENT;
1064 return access_flags;
1066 if (TREE_CODE (decl) == TYPE_DECL)
1068 if (CLASS_PUBLIC (decl))
1069 access_flags |= ACC_PUBLIC;
1070 if (CLASS_FINAL (decl))
1071 access_flags |= ACC_FINAL;
1072 if (CLASS_SUPER (decl))
1073 access_flags |= ACC_SUPER;
1074 if (CLASS_INTERFACE (decl))
1075 access_flags |= ACC_INTERFACE;
1076 if (CLASS_ABSTRACT (decl))
1077 access_flags |= ACC_ABSTRACT;
1078 if (CLASS_STATIC (decl))
1079 access_flags |= ACC_STATIC;
1080 if (CLASS_PRIVATE (decl))
1081 access_flags |= ACC_PRIVATE;
1082 if (CLASS_PROTECTED (decl))
1083 access_flags |= ACC_PROTECTED;
1084 return access_flags;
1086 if (TREE_CODE (decl) == FUNCTION_DECL)
1088 if (METHOD_PUBLIC (decl))
1089 access_flags |= ACC_PUBLIC;
1090 if (METHOD_PRIVATE (decl))
1091 access_flags |= ACC_PRIVATE;
1092 if (METHOD_PROTECTED (decl))
1093 access_flags |= ACC_PROTECTED;
1094 if (METHOD_STATIC (decl))
1095 access_flags |= ACC_STATIC;
1096 if (METHOD_FINAL (decl))
1097 access_flags |= ACC_FINAL;
1098 if (METHOD_SYNCHRONIZED (decl))
1099 access_flags |= ACC_SYNCHRONIZED;
1100 if (METHOD_NATIVE (decl))
1101 access_flags |= ACC_NATIVE;
1102 if (METHOD_ABSTRACT (decl))
1103 access_flags |= ACC_ABSTRACT;
1104 if (METHOD_TRANSIENT (decl))
1105 access_flags |= ACC_TRANSIENT;
1106 return access_flags;
1112 make_field_value (fdecl)
1117 tree type = TREE_TYPE (fdecl);
1118 int resolved = is_compiled_class (type);
1120 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1121 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1123 type = build_class_ref (type);
1126 tree signature = build_java_signature (type);
1128 type = build_utf8_ref (unmangle_classname
1129 (IDENTIFIER_POINTER (signature),
1130 IDENTIFIER_LENGTH (signature)));
1132 PUSH_FIELD_VALUE (finit, "type", type);
1134 flags = get_access_flags_from_decl (fdecl);
1136 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1138 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1139 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1143 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1145 ((FIELD_STATIC (fdecl)
1146 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1147 : TYPE_FIELDS (field_info_union_node)),
1148 (FIELD_STATIC (fdecl)
1149 ? build_address_of (build_static_field_ref (fdecl))
1150 : byte_position (fdecl)))));
1152 FINISH_RECORD_CONSTRUCTOR (finit);
1157 make_method_value (mdecl)
1160 static int method_name_count = 0;
1163 #define ACC_TRANSLATED 0x4000
1164 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1165 code = null_pointer_node;
1166 if (DECL_RTL_SET_P (mdecl))
1167 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1168 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1169 PUSH_FIELD_VALUE (minit, "name",
1170 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1171 init_identifier_node
1172 : DECL_NAME (mdecl)));
1174 tree signature = build_java_signature (TREE_TYPE (mdecl));
1175 PUSH_FIELD_VALUE (minit, "signature",
1178 (IDENTIFIER_POINTER(signature),
1179 IDENTIFIER_LENGTH(signature)))));
1181 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1182 PUSH_FIELD_VALUE (minit, "ncode", code);
1185 /* Compute the `throws' information for the method. */
1186 tree table = integer_zero_node;
1187 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1189 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1190 tree iter, type, array;
1193 table = tree_cons (NULL_TREE, table, NULL_TREE);
1194 for (iter = DECL_FUNCTION_THROWS (mdecl);
1196 iter = TREE_CHAIN (iter))
1198 tree sig = build_java_signature (TREE_VALUE (iter));
1200 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1201 IDENTIFIER_LENGTH (sig)));
1202 table = tree_cons (NULL_TREE, utf8, table);
1204 type = build_prim_array_type (ptr_type_node, length);
1205 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1206 /* Compute something unique enough. */
1207 sprintf (buf, "_methods%d", method_name_count++);
1208 array = build_decl (VAR_DECL, get_identifier (buf), type);
1209 DECL_INITIAL (array) = table;
1210 TREE_STATIC (array) = 1;
1211 DECL_ARTIFICIAL (array) = 1;
1212 DECL_IGNORED_P (array) = 1;
1213 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1215 table = build1 (ADDR_EXPR, ptr_type_node, array);
1218 PUSH_FIELD_VALUE (minit, "throws", table);
1221 FINISH_RECORD_CONSTRUCTOR (minit);
1226 get_dispatch_vector (type)
1229 tree vtable = TYPE_VTABLE (type);
1234 tree super = CLASSTYPE_SUPER (type);
1235 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1236 vtable = make_tree_vec (nvirtuals);
1237 TYPE_VTABLE (type) = vtable;
1238 if (super != NULL_TREE)
1240 tree super_vtable = get_dispatch_vector (super);
1242 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1243 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1246 for (method = TYPE_METHODS (type); method != NULL_TREE;
1247 method = TREE_CHAIN (method))
1248 if (DECL_VINDEX (method) != NULL_TREE
1249 && host_integerp (DECL_VINDEX (method), 0))
1250 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1258 get_dispatch_table (type, this_class_addr)
1259 tree type, this_class_addr;
1261 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1262 tree vtable = get_dispatch_vector (type);
1264 tree list = NULL_TREE;
1265 int nvirtuals = TREE_VEC_LENGTH (vtable);
1266 for (i = nvirtuals; --i >= 0; )
1268 tree method = TREE_VEC_ELT (vtable, i);
1269 if (METHOD_ABSTRACT (method))
1272 warning_with_decl (method,
1273 "abstract method in non-abstract class");
1274 method = null_pointer_node;
1278 if (!DECL_RTL_SET_P (method))
1279 make_decl_rtl (method, NULL);
1280 method = build1 (ADDR_EXPR, nativecode_ptr_type_node, method);
1282 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
1285 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1286 using the Boehm GC we sometimes stash a GC type descriptor
1287 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1288 the emitted byte count during the output to the assembly file. */
1289 list = tree_cons (NULL_TREE, get_boehm_type_descriptor (type),
1291 list = tree_cons (integer_zero_node, this_class_addr, list);
1292 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
1298 make_class_data (type)
1301 tree decl, cons, temp;
1302 tree field, fields_decl;
1303 tree static_fields = NULL_TREE;
1304 tree instance_fields = NULL_TREE;
1305 HOST_WIDE_INT static_field_count = 0;
1306 HOST_WIDE_INT instance_field_count = 0;
1307 HOST_WIDE_INT field_count;
1308 tree field_array_type;
1310 tree methods = NULL_TREE;
1311 tree dtable_decl = NULL_TREE;
1312 HOST_WIDE_INT method_count = 0;
1313 tree method_array_type;
1316 tree this_class_addr;
1317 tree constant_pool_constructor;
1318 tree interfaces = null_pointer_node;
1319 int interface_len = 0;
1320 tree type_decl = TYPE_NAME (type);
1322 this_class_addr = build_class_ref (type);
1323 decl = TREE_OPERAND (this_class_addr, 0);
1325 /* Build Field array. */
1326 field = TYPE_FIELDS (type);
1327 if (DECL_NAME (field) == NULL_TREE)
1328 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1329 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1331 if (! DECL_ARTIFICIAL (field))
1333 tree init = make_field_value (field);
1334 if (FIELD_STATIC (field))
1336 tree initial = DECL_INITIAL (field);
1337 static_field_count++;
1338 static_fields = tree_cons (NULL_TREE, init, static_fields);
1339 /* If the initial value is a string constant,
1340 prevent output_constant from trying to assemble the value. */
1341 if (initial != NULL_TREE
1342 && TREE_TYPE (initial) == string_ptr_type_node)
1343 DECL_INITIAL (field) = NULL_TREE;
1344 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1345 DECL_INITIAL (field) = initial;
1349 instance_field_count++;
1350 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1354 field_count = static_field_count + instance_field_count;
1355 if (field_count > 0)
1357 static_fields = nreverse (static_fields);
1358 instance_fields = nreverse (instance_fields);
1359 static_fields = chainon (static_fields, instance_fields);
1360 field_array_type = build_prim_array_type (field_type_node, field_count);
1361 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1363 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1364 NULL_TREE, static_fields);
1365 TREE_STATIC (fields_decl) = 1;
1366 DECL_ARTIFICIAL (fields_decl) = 1;
1367 DECL_IGNORED_P (fields_decl) = 1;
1368 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1371 fields_decl = NULL_TREE;
1373 /* Build Method array. */
1374 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1375 method != NULL_TREE; method = TREE_CHAIN (method))
1378 if (METHOD_PRIVATE (method)
1379 && ! flag_keep_inline_functions
1380 && (flag_inline_functions || optimize))
1382 init = make_method_value (method);
1384 methods = tree_cons (NULL_TREE, init, methods);
1386 method_array_type = build_prim_array_type (method_type_node, method_count);
1387 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1389 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1390 NULL_TREE, nreverse (methods));
1391 TREE_STATIC (methods_decl) = 1;
1392 DECL_ARTIFICIAL (methods_decl) = 1;
1393 DECL_IGNORED_P (methods_decl) = 1;
1394 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1396 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1397 && ! CLASS_INTERFACE (type_decl))
1399 tree dtable = get_dispatch_table (type, this_class_addr);
1400 dtable_decl = build_dtable_decl (type);
1401 DECL_INITIAL (dtable_decl) = dtable;
1402 TREE_STATIC (dtable_decl) = 1;
1403 DECL_ARTIFICIAL (dtable_decl) = 1;
1404 DECL_IGNORED_P (dtable_decl) = 1;
1405 TREE_PUBLIC (dtable_decl) = 1;
1406 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1407 if (type == class_type_node)
1408 class_dtable_decl = dtable_decl;
1411 if (class_dtable_decl == NULL_TREE)
1413 class_dtable_decl = build_dtable_decl (class_type_node);
1414 TREE_STATIC (class_dtable_decl) = 1;
1415 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1416 DECL_IGNORED_P (class_dtable_decl) = 1;
1417 if (is_compiled_class (class_type_node) != 2)
1418 DECL_EXTERNAL (class_dtable_decl) = 1;
1419 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1422 super = CLASSTYPE_SUPER (type);
1423 if (super == NULL_TREE)
1424 super = null_pointer_node;
1425 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1426 super = build_class_ref (super);
1429 int super_index = alloc_class_constant (super);
1430 super = build_int_2 (super_index, 0);
1431 TREE_TYPE (super) = ptr_type_node;
1434 /* Build and emit the array of implemented interfaces. */
1435 if (type != object_type_node)
1436 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1437 if (interface_len > 0)
1439 tree init = NULL_TREE;
1441 tree interface_array_type, idecl;
1442 interface_array_type
1443 = build_prim_array_type (class_ptr_type, interface_len);
1444 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1445 interface_array_type);
1446 for (i = interface_len; i > 0; i--)
1448 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1449 tree iclass = BINFO_TYPE (child);
1451 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1452 index = build_class_ref (iclass);
1455 int int_index = alloc_class_constant (iclass);
1456 index = build_int_2 (int_index, 0);
1457 TREE_TYPE (index) = ptr_type_node;
1459 init = tree_cons (NULL_TREE, index, init);
1461 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1463 TREE_STATIC (idecl) = 1;
1464 DECL_ARTIFICIAL (idecl) = 1;
1465 DECL_IGNORED_P (idecl) = 1;
1466 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1467 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1470 constant_pool_constructor = build_constants_constructor ();
1472 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1473 PUSH_FIELD_VALUE (temp, "vtable",
1474 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1475 if (! flag_hash_synchronization)
1476 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1477 FINISH_RECORD_CONSTRUCTOR (temp);
1478 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1479 PUSH_SUPER_VALUE (cons, temp);
1480 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1481 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1482 PUSH_FIELD_VALUE (cons, "accflags",
1483 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1485 PUSH_FIELD_VALUE (cons, "superclass",
1486 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1487 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1488 PUSH_FIELD_VALUE (cons, "methods",
1489 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1490 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1491 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1492 PUSH_FIELD_VALUE (cons, "fields",
1493 fields_decl == NULL_TREE ? null_pointer_node
1494 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1495 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1496 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1497 PUSH_FIELD_VALUE (cons, "static_field_count",
1498 build_int_2 (static_field_count, 0));
1499 PUSH_FIELD_VALUE (cons, "vtable",
1500 dtable_decl == NULL_TREE ? null_pointer_node
1501 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1502 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1503 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1504 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1505 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1507 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1508 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1509 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1510 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1511 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1512 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1514 FINISH_RECORD_CONSTRUCTOR (cons);
1516 DECL_INITIAL (decl) = cons;
1517 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1524 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1525 int saw_native_method = 0;
1527 /* Find out if we have any native methods. We use this information
1529 for (method = type_methods;
1530 method != NULL_TREE;
1531 method = TREE_CHAIN (method))
1533 if (METHOD_NATIVE (method))
1535 saw_native_method = 1;
1540 /* Emit deferred inline methods. */
1541 for (method = type_methods; method != NULL_TREE; )
1543 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1545 output_inline_function (method);
1546 /* Scan the list again to see if there are any earlier
1548 method = type_methods;
1551 method = TREE_CHAIN (method);
1554 current_function_decl = NULL_TREE;
1555 make_class_data (current_class);
1557 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1560 /* Return 2 if CLASS is compiled by this compilation job;
1561 return 1 if CLASS can otherwise be assumed to be compiled;
1562 return 0 if we cannot assume that CLASS is compiled.
1563 Returns 1 for primitive and 0 for array types. */
1565 is_compiled_class (class)
1569 if (TREE_CODE (class) == POINTER_TYPE)
1570 class = TREE_TYPE (class);
1571 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1573 if (TYPE_ARRAY_P (class))
1575 if (class == current_class)
1578 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1579 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1581 /* The class was seen in the current ZIP file and will be
1582 available as a compiled class in the future but may not have
1583 been loaded already. Load it if necessary. This prevent
1584 build_class_ref () from crashing. */
1586 if (seen_in_zip && !CLASS_LOADED_P (class))
1587 load_class (class, 1);
1589 /* We return 2 for class seen in ZIP and class from files
1590 belonging to the same compilation unit */
1594 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1596 if (!CLASS_LOADED_P (class))
1598 if (CLASS_FROM_SOURCE_P (class))
1599 safe_layout_class (class);
1601 load_class (class, 1);
1609 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1612 build_dtable_decl (type)
1617 /* We need to build a new dtable type so that its size is uniquely
1618 computed when we're dealing with the class for real and not just
1619 faking it (like java.lang.Class during the initialization of the
1620 compiler.) We now we're not faking a class when CURRENT_CLASS is
1622 if (current_class == type)
1624 tree dummy = NULL_TREE, aomt, n;
1626 dtype = make_node (RECORD_TYPE);
1627 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1628 n = build_int_2 (TREE_VEC_LENGTH (get_dispatch_vector (type)), 0);
1629 aomt = build_array_type (ptr_type_node, build_index_type (n));
1630 PUSH_FIELD (dtype, dummy, "methods", aomt);
1631 layout_type (dtype);
1634 dtype = dtable_type;
1636 return build_decl (VAR_DECL,
1637 java_mangle_vtable (&temporary_obstack, type), dtype);
1640 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1641 fields inherited from SUPER_CLASS. */
1644 push_super_field (this_class, super_class)
1645 tree this_class, super_class;
1648 /* Don't insert the field if we're just re-laying the class out. */
1649 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1651 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1652 DECL_IGNORED_P (base_decl) = 1;
1653 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1654 TYPE_FIELDS (this_class) = base_decl;
1655 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1656 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1659 /* Handle the different manners we may have to lay out a super class. */
1662 maybe_layout_super_class (super_class, this_class)
1666 if (TREE_CODE (super_class) == RECORD_TYPE)
1668 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1669 safe_layout_class (super_class);
1670 if (!CLASS_LOADED_P (super_class))
1671 load_class (super_class, 1);
1673 /* We might have to layout the class before its dependency on
1674 the super class gets resolved by java_complete_class */
1675 else if (TREE_CODE (super_class) == POINTER_TYPE)
1677 if (TREE_TYPE (super_class) != NULL_TREE)
1678 super_class = TREE_TYPE (super_class);
1681 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1682 super_class, NULL_TREE, this_class);
1684 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1685 super_class = TREE_TYPE (super_class);
1688 if (!TYPE_SIZE (super_class))
1689 safe_layout_class (super_class);
1695 layout_class (this_class)
1698 tree super_class = CLASSTYPE_SUPER (this_class);
1701 class_list = tree_cons (this_class, NULL_TREE, class_list);
1702 if (CLASS_BEING_LAIDOUT (this_class))
1708 sprintf (buffer, " with `%s'",
1709 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1710 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1712 for (current = TREE_CHAIN (class_list); current;
1713 current = TREE_CHAIN (current))
1715 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1716 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1717 IDENTIFIER_POINTER (DECL_NAME (decl)),
1718 DECL_SOURCE_FILE (decl),
1719 DECL_SOURCE_LINE (decl));
1720 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1722 obstack_1grow (&temporary_obstack, '\0');
1723 report = obstack_finish (&temporary_obstack);
1724 cyclic_inheritance_report = ggc_strdup (report);
1725 obstack_free (&temporary_obstack, report);
1726 TYPE_SIZE (this_class) = error_mark_node;
1729 CLASS_BEING_LAIDOUT (this_class) = 1;
1731 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1733 tree maybe_super_class
1734 = maybe_layout_super_class (super_class, this_class);
1735 if (maybe_super_class == NULL
1736 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1738 TYPE_SIZE (this_class) = error_mark_node;
1739 CLASS_BEING_LAIDOUT (this_class) = 0;
1740 class_list = TREE_CHAIN (class_list);
1743 if (TYPE_SIZE (this_class) == NULL_TREE)
1744 push_super_field (this_class, maybe_super_class);
1747 for (field = TYPE_FIELDS (this_class);
1748 field != NULL_TREE; field = TREE_CHAIN (field))
1750 if (FIELD_STATIC (field))
1752 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1753 SET_DECL_ASSEMBLER_NAME (field,
1755 (&temporary_obstack, field));
1759 layout_type (this_class);
1761 /* Also recursively load/layout any superinterfaces, but only if class was
1762 loaded from bytecode. The source parser will take care of this itself. */
1763 if (!CLASS_FROM_SOURCE_P (this_class))
1765 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1769 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1771 for (i = n; i > 0; i--)
1773 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1774 tree super_interface = BINFO_TYPE (vec_elt);
1776 tree maybe_super_interface
1777 = maybe_layout_super_class (super_interface, NULL_TREE);
1778 if (maybe_super_interface == NULL
1779 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1781 TYPE_SIZE (this_class) = error_mark_node;
1782 CLASS_BEING_LAIDOUT (this_class) = 0;
1783 class_list = TREE_CHAIN (class_list);
1790 /* Convert the size back to an SI integer value */
1791 TYPE_SIZE_UNIT (this_class) =
1792 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1794 CLASS_BEING_LAIDOUT (this_class) = 0;
1795 class_list = TREE_CHAIN (class_list);
1799 layout_class_methods (this_class)
1802 tree method_decl, dtable_count;
1803 tree super_class, handle_type;
1805 if (TYPE_NVIRTUALS (this_class))
1808 super_class = CLASSTYPE_SUPER (this_class);
1809 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1813 super_class = maybe_layout_super_class (super_class, this_class);
1814 if (!TYPE_NVIRTUALS (super_class))
1815 layout_class_methods (super_class);
1816 dtable_count = TYPE_NVIRTUALS (super_class);
1819 dtable_count = integer_zero_node;
1821 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1823 for (method_decl = TYPE_METHODS (handle_type);
1824 method_decl; method_decl = TREE_CHAIN (method_decl))
1825 dtable_count = layout_class_method (this_class, super_class,
1826 method_decl, dtable_count);
1828 TYPE_NVIRTUALS (this_class) = dtable_count;
1830 #ifdef JAVA_USE_HANDLES
1831 layout_type (handle_type);
1835 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
1836 and 1 if STR is "greater" than NAME. */
1838 /* Lay METHOD_DECL out, returning a possibly new value of
1839 DTABLE_COUNT. Also mangle the method's name. */
1842 layout_class_method (this_class, super_class, method_decl, dtable_count)
1843 tree this_class, super_class, method_decl, dtable_count;
1845 tree method_name = DECL_NAME (method_decl);
1847 TREE_PUBLIC (method_decl) = 1;
1849 /* This is a good occasion to mangle the method's name */
1850 SET_DECL_ASSEMBLER_NAME (method_decl,
1851 java_mangle_decl (&temporary_obstack,
1853 /* We don't generate a RTL for the method if it's abstract, or if
1854 it's an interface method that isn't clinit. */
1855 if (! METHOD_ABSTRACT (method_decl)
1856 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1857 && (DECL_CLINIT_P (method_decl))))
1858 make_decl_rtl (method_decl, NULL);
1860 if (ID_INIT_P (method_name))
1862 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1864 for (ptr = p; *ptr; )
1869 DECL_CONSTRUCTOR_P (method_decl) = 1;
1870 build_java_argument_signature (TREE_TYPE (method_decl));
1872 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1875 build_java_argument_signature (TREE_TYPE (method_decl));
1876 tree super_method = lookup_argument_method (super_class, method_name,
1878 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1880 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1881 if (DECL_VINDEX (method_decl) == NULL_TREE
1882 && !CLASS_FROM_SOURCE_P (this_class))
1883 error_with_decl (method_decl,
1884 "non-static method '%s' overrides static method");
1886 else if (! METHOD_FINAL (method_decl)
1887 && ! METHOD_PRIVATE (method_decl)
1888 && ! CLASS_FINAL (TYPE_NAME (this_class))
1891 DECL_VINDEX (method_decl) = dtable_count;
1892 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
1893 dtable_count, integer_one_node));
1897 return dtable_count;
1903 /* END does not need to be registered with the garbage collector
1904 because it always points into the list given by REGISTERED_CLASS,
1905 and that variable is registered with the collector. */
1907 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1908 tree current = copy_node (node);
1910 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1911 if (!registered_class)
1912 registered_class = current;
1914 TREE_CHAIN (end) = current;
1919 /* Emit something to register classes at start-up time.
1921 The preferred mechanism is through the .jcr section, which contain
1922 a list of pointers to classes which get registered during
1923 constructor invoction time. The fallback mechanism is to generate
1924 a `constructor' function which calls _Jv_RegisterClass for each
1925 class in this file. */
1928 emit_register_classes ()
1930 /* ??? This isn't quite the correct test. We also have to know
1931 that the target is using gcc's crtbegin/crtend objects rather
1932 than the ones that come with the operating system. */
1933 if (SUPPORTS_WEAK && targetm.have_named_sections)
1935 #ifdef JCR_SECTION_NAME
1937 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
1938 assemble_align (POINTER_SIZE);
1939 for (t = registered_class; t; t = TREE_CHAIN (t))
1940 assemble_integer (XEXP (DECL_RTL (t), 0),
1941 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1948 extern tree get_file_function_name PARAMS ((int));
1949 tree init_name = get_file_function_name ('I');
1950 tree init_type = build_function_type (void_type_node, end_params_node);
1954 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1955 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
1956 TREE_STATIC (init_decl) = 1;
1957 current_function_decl = init_decl;
1958 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
1961 /* It can be a static function as long as collect2 does not have
1962 to scan the object file to find its ctor/dtor routine. */
1963 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
1965 /* Suppress spurious warnings. */
1966 TREE_USED (init_decl) = 1;
1969 make_decl_rtl (init_decl, NULL);
1970 init_function_start (init_decl, input_filename, 0);
1971 expand_function_start (init_decl, 0);
1973 /* Do not allow the function to be deferred. */
1974 current_function_cannot_inline
1975 = "static constructors and destructors cannot be inlined";
1977 for ( t = registered_class; t; t = TREE_CHAIN (t))
1978 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1979 XEXP (DECL_RTL (t), 0), Pmode);
1981 expand_function_end (input_filename, 0, 0);
1983 rest_of_compilation (init_decl);
1984 current_function_decl = NULL_TREE;
1986 if (targetm.have_ctors_dtors)
1987 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
1988 DEFAULT_INIT_PRIORITY);
1993 init_class_processing ()
1995 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
1996 ggc_add_tree_root (class_roots, sizeof (class_roots) / sizeof (tree));
1997 fields_ident = get_identifier ("fields");
1998 info_ident = get_identifier ("info");
1999 ggc_add_rtx_root (®isterClass_libfunc, 1);
2000 gcc_obstack_init (&temporary_obstack);