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"
41 static tree make_method_value PARAMS ((tree));
42 static tree build_java_method_type PARAMS ((tree, tree, int));
43 static int32 hashUtf8String PARAMS ((const char *, int));
44 static tree make_field_value PARAMS ((tree));
45 static tree get_dispatch_vector PARAMS ((tree));
46 static tree get_dispatch_table PARAMS ((tree, tree));
47 static void add_interface_do PARAMS ((tree, tree, int));
48 static tree maybe_layout_super_class PARAMS ((tree, tree));
49 static int assume_compiled PARAMS ((const char *));
50 static struct hash_entry *init_test_hash_newfunc PARAMS ((struct hash_entry *,
53 static rtx registerClass_libfunc;
55 extern struct obstack permanent_obstack;
56 struct obstack temporary_obstack;
58 /* The compiler generates different code depending on whether or not
59 it can assume certain classes have been compiled down to native
60 code or not. The compiler options -fassume-compiled= and
61 -fno-assume-compiled= are used to create a tree of
62 assume_compiled_node objects. This tree is queried to determine if
63 a class is assume to be compiled or not. Each node in the tree
64 represents either a package or a specific class. */
66 typedef struct assume_compiled_node_struct
68 /* The class or package name. */
71 /* Non-zero if this represents an exclusion. */
74 /* Pointers to other nodes in the tree. */
75 struct assume_compiled_node_struct *parent;
76 struct assume_compiled_node_struct *sibling;
77 struct assume_compiled_node_struct *child;
78 } assume_compiled_node;
80 static assume_compiled_node *find_assume_compiled_node
81 PARAMS ((assume_compiled_node *, const char *));
83 /* This is the root of the include/exclude tree. */
85 static assume_compiled_node *assume_compiled_tree;
87 static tree class_roots[5]
88 = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
89 #define registered_class class_roots[0]
90 #define fields_ident class_roots[1] /* get_identifier ("fields") */
91 #define info_ident class_roots[2] /* get_identifier ("info") */
92 #define class_list class_roots[3]
93 #define class_dtable_decl class_roots[4]
95 /* Return the node that most closely represents the class whose name
96 is IDENT. Start the search from NODE. Return NULL if an
97 appropriate node does not exist. */
99 static assume_compiled_node *
100 find_assume_compiled_node (node, ident)
101 assume_compiled_node *node;
106 size_t node_ident_length = strlen (node->ident);
108 /* node_ident_length is zero at the root of the tree. If the
109 identifiers are the same length, then we have matching
110 classes. Otherwise check if we've matched an enclosing
113 if (node_ident_length == 0
114 || (strncmp (ident, node->ident, node_ident_length) == 0
115 && (strlen (ident) == node_ident_length
116 || ident[node_ident_length] == '.')))
118 /* We've found a match, however, there might be a more
121 assume_compiled_node *found = find_assume_compiled_node (node->child,
129 /* No match yet. Continue through the sibling list. */
130 node = node->sibling;
133 /* No match at all in this tree. */
137 /* Add a new IDENT to the include/exclude tree. It's an exclusion
138 if EXCLUDEP is non-zero. */
141 add_assume_compiled (ident, excludep)
145 assume_compiled_node *parent;
146 assume_compiled_node *node =
147 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
149 node->ident = xstrdup (ident);
150 node->excludep = excludep;
153 /* Create the root of the tree if it doesn't exist yet. */
155 if (NULL == assume_compiled_tree)
157 assume_compiled_tree =
158 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
159 assume_compiled_tree->ident = "";
160 assume_compiled_tree->excludep = 0;
161 assume_compiled_tree->sibling = NULL;
162 assume_compiled_tree->child = NULL;
163 assume_compiled_tree->parent = NULL;
166 /* Calling the function with the empty string means we're setting
167 excludep for the root of the hierarchy. */
171 assume_compiled_tree->excludep = excludep;
175 /* Find the parent node for this new node. PARENT will either be a
176 class or a package name. Adjust PARENT accordingly. */
178 parent = find_assume_compiled_node (assume_compiled_tree, ident);
179 if (ident[strlen (parent->ident)] != '.')
180 parent = parent->parent;
182 /* Insert NODE into the tree. */
184 node->parent = parent;
185 node->sibling = parent->child;
186 parent->child = node;
189 /* Returns non-zero if IDENT is the name of a class that the compiler
190 should assume has been compiled to FIXME */
193 assume_compiled (ident)
196 assume_compiled_node *i;
199 if (NULL == assume_compiled_tree)
202 i = find_assume_compiled_node (assume_compiled_tree,
205 result = ! i->excludep;
210 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
211 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
212 Also, PREFIX is prepended, and SUFFIX is appended. */
215 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
216 const char* old_name;
223 int prefix_len = strlen (prefix);
224 int suffix_len = strlen (suffix);
225 int i = prefix_len + old_length + suffix_len + 1;
229 char *buffer = (char *)alloca (i);
231 strcpy (buffer, prefix);
232 for (i = 0; i < old_length; i++)
234 char ch = old_name[i];
237 buffer[prefix_len + i] = ch;
239 strcpy (buffer + prefix_len + old_length, suffix);
240 return get_identifier (buffer);
243 /* Return an IDENTIFIER_NODE the same as OLD_ID,
244 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
245 Also, PREFIX is prepended, and SUFFIX is appended. */
248 identifier_subst (old_id, prefix, old_char, new_char, suffix)
255 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
256 prefix, old_char, new_char, suffix);
259 /* Generate a valid C identifier from the name of the class TYPE,
260 prefixed by PREFIX. */
263 mangled_classname (prefix, type)
267 tree ident = TYPE_NAME (type);
268 if (TREE_CODE (ident) != IDENTIFIER_NODE)
269 ident = DECL_NAME (ident);
270 return identifier_subst (ident, prefix, '.', '_', "");
277 type = make_node (RECORD_TYPE);
278 #ifdef JAVA_USE_HANDLES
279 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
280 build_pointer_type (type));
281 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
282 methodtable_ptr_type);
283 tree handle_type = make_node (RECORD_TYPE);
284 TREE_CHAIN (field1) = field2;
285 TYPE_FIELDS (handle_type) = field1;
286 TYPE_BINFO (type) = make_tree_vec (7);
287 TYPE_BINFO (handle_type) = make_tree_vec (7);
288 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
289 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
291 TYPE_BINFO (type) = make_tree_vec (6);
293 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
298 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
299 and where each of the constituents is separated by '/',
300 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
303 unmangle_classname (name, name_length)
304 const char *name; int name_length;
306 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
307 /* It's not sufficient to compare to_return and get_identifier
308 (name) to determine whether to_return is qualified. There are
309 cases in signature analysis where name will be stripped of a
311 name = IDENTIFIER_POINTER (to_return);
315 QUALIFIED_P (to_return) = 1;
323 push_class (class_type, class_name)
324 tree class_type, class_name;
326 tree decl, signature;
327 const char *save_input_filename = input_filename;
328 int save_lineno = lineno;
329 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
330 CLASS_P (class_type) = 1;
331 input_filename = IDENTIFIER_POINTER (source_name);
333 decl = build_decl (TYPE_DECL, class_name, class_type);
334 input_filename = save_input_filename;
335 lineno = save_lineno;
336 signature = identifier_subst (class_name, "L", '.', '/', ";");
337 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
339 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
340 both a typedef and in the struct name-space. We may want to re-visit
341 this later, but for now it reduces the changes needed for gdb. */
342 DECL_ARTIFICIAL (decl) = 1;
344 pushdecl_top_level (decl);
345 #ifdef JAVA_USE_HANDLES
347 tree handle_name = identifier_subst (class_name,
348 "Handle$", '.', '.', "");
349 tree handle_decl = build_decl (TYPE_DECL, handle_name,
350 CLASS_TO_HANDLE_TYPE (class_type));
351 pushdecl (handle_decl);
358 /* Finds the (global) class named NAME. Creates the class if not found.
359 Also creates associated TYPE_DECL.
360 Does not check if the class actually exists, load the class,
361 fill in field or methods, or do layout_type. */
367 tree decl = IDENTIFIER_CLASS_VALUE (name);
368 if (decl == NULL_TREE)
369 decl = push_class (make_class (), name);
370 return TREE_TYPE (decl);
374 set_super_info (access_flags, this_class, super_class, interfaces_count)
378 int interfaces_count;
380 int total_supers = interfaces_count;
381 tree class_decl = TYPE_NAME (this_class);
385 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
388 tree super_binfo = make_tree_vec (6);
389 BINFO_TYPE (super_binfo) = super_class;
390 BINFO_OFFSET (super_binfo) = integer_zero_node;
391 TREE_VIA_PUBLIC (super_binfo) = 1;
392 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
394 CLASS_HAS_SUPER (this_class) = 1;
397 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
398 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
399 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
400 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
401 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
402 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
403 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
404 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
407 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
408 direct sub-classes of Object are 1, and so on. */
415 if (! CLASS_LOADED_P (clas))
416 load_class (clas, 1);
417 if (TYPE_SIZE (clas) == error_mark_node)
419 while (clas != object_type_node)
422 clas = TYPE_BINFO_BASETYPE (clas, 0);
427 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
430 interface_of_p (type1, type2)
436 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
438 n = TREE_VEC_LENGTH (basetype_vec);
439 for (i = 0; i < n; i++)
441 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
442 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
445 for (i = 0; i < n; i++)
447 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
448 if (vec_elt && BINFO_TYPE (vec_elt)
449 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
455 /* Return true iff TYPE1 inherits from TYPE2. */
458 inherits_from_p (type1, type2)
461 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
465 type1 = CLASSTYPE_SUPER (type1);
470 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
473 enclosing_context_p (type1, type2)
476 if (!INNER_CLASS_TYPE_P (type2))
479 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
481 type2 = (INNER_CLASS_TYPE_P (type2) ?
482 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
491 /* Return 1 iff there exists a common enclosing context between TYPE1
494 int common_enclosing_context_p (type1, type2)
497 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
500 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
501 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
502 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
505 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
506 current = (PURE_INNER_CLASS_TYPE_P (current) ?
507 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
509 if (type1 == current)
516 add_interface_do (basetype_vec, interface_class, i)
517 tree basetype_vec, interface_class;
520 tree interface_binfo = make_tree_vec (6);
521 BINFO_TYPE (interface_binfo) = interface_class;
522 BINFO_OFFSET (interface_binfo) = integer_zero_node;
523 TREE_VIA_VIRTUAL (interface_binfo) = 1;
524 TREE_VIA_PUBLIC (interface_binfo) = 1;
525 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
528 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
529 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
530 if attempt is made to add it twice. */
533 maybe_add_interface (this_class, interface_class)
534 tree this_class, interface_class;
536 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
538 int n = TREE_VEC_LENGTH (basetype_vec);
543 error ("internal error - too many interface type");
546 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
548 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
549 return interface_class;
551 add_interface_do (basetype_vec, interface_class, i);
555 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
558 add_interface (this_class, interface_class)
559 tree this_class, interface_class;
561 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
563 int n = TREE_VEC_LENGTH (basetype_vec);
568 error ("internal error - too many interface type");
571 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
574 add_interface_do (basetype_vec, interface_class, i);
578 /* Return the address of a pointer to the first FUNCTION_DECL
579 in the list (*LIST) whose DECL_NAME is NAME. */
582 find_named_method (list, name)
586 while (*list && DECL_NAME (*list) != name)
587 list = &TREE_CHAIN (*list);
593 build_java_method_type (fntype, this_class, access_flags)
598 if (access_flags & ACC_STATIC)
600 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
603 static struct hash_entry *
604 init_test_hash_newfunc (entry, table, string)
605 struct hash_entry *entry;
606 struct hash_table *table;
607 hash_table_key string ATTRIBUTE_UNUSED;
609 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
612 ret = ((struct init_test_hash_entry *)
613 hash_allocate (table, sizeof (struct init_test_hash_entry)));
617 ret->init_test_decl = 0;
618 return (struct hash_entry *) ret;
621 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
622 (parse.y). The hash of a tree node is it's pointer value,
623 comparison is direct. */
626 java_hash_hash_tree_node (k)
633 java_hash_compare_tree_node (k1, k2)
637 return ((char*) k1 == (char*) k2);
641 add_method_1 (handle_class, access_flags, name, function_type)
647 tree method_type, fndecl;
649 method_type = build_java_method_type (function_type,
650 handle_class, access_flags);
652 fndecl = build_decl (FUNCTION_DECL, name, method_type);
653 DECL_CONTEXT (fndecl) = handle_class;
655 DECL_LANG_SPECIFIC (fndecl)
656 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
658 /* Initialize the static initializer test table. */
659 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
660 init_test_hash_newfunc, java_hash_hash_tree_node,
661 java_hash_compare_tree_node);
663 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
664 TYPE_METHODS (handle_class) = fndecl;
666 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
667 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
668 if (access_flags & ACC_PRIVATE)
669 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
670 if (access_flags & ACC_NATIVE)
672 METHOD_NATIVE (fndecl) = 1;
673 DECL_EXTERNAL (fndecl) = 1;
675 if (access_flags & ACC_STATIC)
676 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
677 if (access_flags & ACC_FINAL)
678 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
679 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
680 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
681 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
685 /* Add a method to THIS_CLASS.
686 The method's name is NAME.
687 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
690 add_method (this_class, access_flags, name, method_sig)
696 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
697 tree function_type, fndecl;
698 const unsigned char *sig
699 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
702 fatal_error ("bad method signature");
704 function_type = get_type_from_signature (method_sig);
705 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
706 set_java_signature (TREE_TYPE (fndecl), method_sig);
711 add_field (class, name, field_type, flags)
717 int is_static = (flags & ACC_STATIC) != 0;
719 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
720 TREE_CHAIN (field) = TYPE_FIELDS (class);
721 TYPE_FIELDS (class) = field;
722 DECL_CONTEXT (field) = class;
724 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
725 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
726 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
727 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
728 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
729 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
732 FIELD_STATIC (field) = 1;
733 /* Always make field externally visible. This is required so
734 that native methods can always access the field. */
735 TREE_PUBLIC (field) = 1;
740 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
743 set_constant_value (field, constant)
744 tree field, constant;
746 if (field == NULL_TREE)
747 warning ("misplaced ConstantValue attribute (not in any field)");
748 else if (DECL_INITIAL (field) != NULL_TREE)
749 warning ("duplicate ConstanValue atribute for field '%s'",
750 IDENTIFIER_POINTER (DECL_NAME (field)));
753 DECL_INITIAL (field) = constant;
754 if (FIELD_FINAL (field))
755 DECL_FIELD_FINAL_IUD (field) = 1;
759 /* Count the number of Unicode chars encoded in a given Ut8 string. */
763 strLengthUtf8 (str, len)
767 register unsigned char* ptr = (unsigned char*) str;
768 register unsigned char *limit = ptr + len;
770 for (; ptr < limit; str_length++) {
771 if (UTF8_GET (ptr, limit) < 0)
779 /* Calculate a hash value for a string encoded in Utf8 format.
780 * This returns the same hash value as specified for java.lang.String.hashCode.
784 hashUtf8String (str, len)
788 register const unsigned char* ptr = (const unsigned char*) str;
789 register const unsigned char *limit = ptr + len;
793 int ch = UTF8_GET (ptr, limit);
794 /* Updated specification from
795 http://www.javasoft.com/docs/books/jls/clarify.html. */
796 hash = (31 * hash) + ch;
801 tree utf8_decl_list = NULL_TREE;
804 build_utf8_ref (name)
807 const char * name_ptr = IDENTIFIER_POINTER(name);
808 int name_len = IDENTIFIER_LENGTH(name);
811 tree ctype, field = NULL_TREE, str_type, cinit, string;
812 static int utf8_count = 0;
814 tree ref = IDENTIFIER_UTF8_REF (name);
816 if (ref != NULL_TREE)
819 ctype = make_node (RECORD_TYPE);
820 str_type = build_prim_array_type (unsigned_byte_type_node,
821 name_len + 1); /* Allow for final '\0'. */
822 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
823 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
824 PUSH_FIELD (ctype, field, "data", str_type);
825 FINISH_RECORD (ctype);
826 START_RECORD_CONSTRUCTOR (cinit, ctype);
827 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
828 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
829 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
830 string = build_string (name_len, name_ptr);
831 TREE_TYPE (string) = str_type;
832 PUSH_FIELD_VALUE (cinit, "data", string);
833 FINISH_RECORD_CONSTRUCTOR (cinit);
834 TREE_CONSTANT (cinit) = 1;
836 /* Build a unique identifier based on buf. */
837 sprintf(buf, "_Utf%d", ++utf8_count);
838 buf_ptr = &buf[strlen (buf)];
839 if (name_len > 0 && name_ptr[0] >= '0' && name_ptr[0] <= '9')
841 while (--name_len >= 0)
843 unsigned char c = *name_ptr++;
846 if (!ISALPHA(c) && !ISDIGIT(c))
849 if (buf_ptr >= buf + 50)
854 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
855 /* FIXME get some way to force this into .text, not .data. */
856 TREE_STATIC (decl) = 1;
857 DECL_ARTIFICIAL (decl) = 1;
858 DECL_IGNORED_P (decl) = 1;
859 TREE_READONLY (decl) = 1;
860 TREE_THIS_VOLATILE (decl) = 0;
861 DECL_INITIAL (decl) = cinit;
862 TREE_CHAIN (decl) = utf8_decl_list;
863 layout_decl (decl, 0);
865 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
866 utf8_decl_list = decl;
867 make_decl_rtl (decl, (char*) 0);
868 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
869 IDENTIFIER_UTF8_REF (name) = ref;
873 /* Build a reference to the class TYPE.
874 Also handles primitive types and array types. */
877 build_class_ref (type)
880 int is_compiled = is_compiled_class (type);
883 tree ref, decl_name, decl;
884 if (TREE_CODE (type) == POINTER_TYPE)
885 type = TREE_TYPE (type);
886 if (TREE_CODE (type) == RECORD_TYPE)
888 if (TYPE_SIZE (type) == error_mark_node)
889 return null_pointer_node;
890 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
891 "", '/', '/', ".class");
892 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
893 if (decl == NULL_TREE)
895 decl = build_decl (VAR_DECL, decl_name, class_type_node);
896 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
897 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
898 TREE_STATIC (decl) = 1;
899 TREE_PUBLIC (decl) = 1;
900 DECL_IGNORED_P (decl) = 1;
901 DECL_ARTIFICIAL (decl) = 1;
902 SET_DECL_ASSEMBLER_NAME (decl,
903 java_mangle_class_field
904 (&temporary_obstack, type));
905 make_decl_rtl (decl, NULL);
906 pushdecl_top_level (decl);
907 if (is_compiled == 1)
908 DECL_EXTERNAL (decl) = 1;
915 if (flag_emit_class_files)
917 const char *prim_class_name;
919 if (type == char_type_node)
920 prim_class_name = "java.lang.Character";
921 else if (type == boolean_type_node)
922 prim_class_name = "java.lang.Boolean";
923 else if (type == byte_type_node)
924 prim_class_name = "java.lang.Byte";
925 else if (type == short_type_node)
926 prim_class_name = "java.lang.Short";
927 else if (type == int_type_node)
928 prim_class_name = "java.lang.Integer";
929 else if (type == long_type_node)
930 prim_class_name = "java.lang.Long";
931 else if (type == float_type_node)
932 prim_class_name = "java.lang.Float";
933 else if (type == double_type_node)
934 prim_class_name = "java.lang.Double";
935 else if (type == void_type_node)
936 prim_class_name = "java.lang.Void";
940 prim_class = lookup_class (get_identifier (prim_class_name));
941 return build (COMPONENT_REF, NULL_TREE,
942 prim_class, TYPE_identifier_node);
944 decl_name = TYPE_NAME (type);
945 if (TREE_CODE (decl_name) == TYPE_DECL)
946 decl_name = DECL_NAME (decl_name);
947 name = IDENTIFIER_POINTER (decl_name);
948 if (strncmp (name, "promoted_", 9) == 0)
950 sprintf (buffer, "_Jv_%sClass", name);
951 decl_name = get_identifier (buffer);
952 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
953 if (decl == NULL_TREE)
955 decl = build_decl (VAR_DECL, decl_name, class_type_node);
956 TREE_STATIC (decl) = 1;
957 TREE_PUBLIC (decl) = 1;
958 make_decl_rtl (decl, NULL);
959 pushdecl_top_level (decl);
960 if (is_compiled == 1)
961 DECL_EXTERNAL (decl) = 1;
965 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
972 index = alloc_class_constant (type);
973 cl = build_ref_from_constant_pool (index);
974 TREE_TYPE (cl) = promote_type (class_ptr_type);
980 build_static_field_ref (fdecl)
983 tree fclass = DECL_CONTEXT (fdecl);
984 int is_compiled = is_compiled_class (fclass);
987 if (!DECL_RTL_SET_P (fdecl))
989 if (is_compiled == 1)
990 DECL_EXTERNAL (fdecl) = 1;
991 make_decl_rtl (fdecl, NULL);
998 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
999 tree ref = build_class_ref (fclass);
1001 int field_index = 0;
1002 ref = build1 (INDIRECT_REF, class_type_node, ref);
1003 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1004 lookup_field (&class_type_node, fields_ident));
1006 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1010 if (fld == NULL_TREE)
1011 fatal_error ("field '%s' not found in class",
1012 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1013 if (FIELD_STATIC (fld))
1016 field_index *= int_size_in_bytes (field_type_node);
1017 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1018 ref, build_int_2 (field_index, 0)));
1019 ref = build1 (INDIRECT_REF, field_type_node, ref);
1020 ref = build (COMPONENT_REF, field_info_union_node,
1021 ref, lookup_field (&field_type_node, info_ident));
1022 ref = build (COMPONENT_REF, ptr_type_node,
1023 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1024 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1029 get_access_flags_from_decl (decl)
1032 int access_flags = 0;
1033 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1035 if (FIELD_STATIC (decl))
1036 access_flags |= ACC_STATIC;
1037 if (FIELD_PUBLIC (decl))
1038 access_flags |= ACC_PUBLIC;
1039 if (FIELD_PROTECTED (decl))
1040 access_flags |= ACC_PROTECTED;
1041 if (FIELD_PRIVATE (decl))
1042 access_flags |= ACC_PRIVATE;
1043 if (FIELD_FINAL (decl))
1044 access_flags |= ACC_FINAL;
1045 if (FIELD_VOLATILE (decl))
1046 access_flags |= ACC_VOLATILE;
1047 if (FIELD_TRANSIENT (decl))
1048 access_flags |= ACC_TRANSIENT;
1049 return access_flags;
1051 if (TREE_CODE (decl) == TYPE_DECL)
1053 if (CLASS_PUBLIC (decl))
1054 access_flags |= ACC_PUBLIC;
1055 if (CLASS_FINAL (decl))
1056 access_flags |= ACC_FINAL;
1057 if (CLASS_SUPER (decl))
1058 access_flags |= ACC_SUPER;
1059 if (CLASS_INTERFACE (decl))
1060 access_flags |= ACC_INTERFACE;
1061 if (CLASS_ABSTRACT (decl))
1062 access_flags |= ACC_ABSTRACT;
1063 if (CLASS_STATIC (decl))
1064 access_flags |= ACC_STATIC;
1065 if (CLASS_PRIVATE (decl))
1066 access_flags |= ACC_PRIVATE;
1067 if (CLASS_PROTECTED (decl))
1068 access_flags |= ACC_PROTECTED;
1069 return access_flags;
1071 if (TREE_CODE (decl) == FUNCTION_DECL)
1073 if (METHOD_PUBLIC (decl))
1074 access_flags |= ACC_PUBLIC;
1075 if (METHOD_PRIVATE (decl))
1076 access_flags |= ACC_PRIVATE;
1077 if (METHOD_PROTECTED (decl))
1078 access_flags |= ACC_PROTECTED;
1079 if (METHOD_STATIC (decl))
1080 access_flags |= ACC_STATIC;
1081 if (METHOD_FINAL (decl))
1082 access_flags |= ACC_FINAL;
1083 if (METHOD_SYNCHRONIZED (decl))
1084 access_flags |= ACC_SYNCHRONIZED;
1085 if (METHOD_NATIVE (decl))
1086 access_flags |= ACC_NATIVE;
1087 if (METHOD_ABSTRACT (decl))
1088 access_flags |= ACC_ABSTRACT;
1089 if (METHOD_TRANSIENT (decl))
1090 access_flags |= ACC_TRANSIENT;
1091 return access_flags;
1097 make_field_value (fdecl)
1102 tree type = TREE_TYPE (fdecl);
1103 int resolved = is_compiled_class (type);
1105 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1106 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1108 type = build_class_ref (type);
1111 tree signature = build_java_signature (type);
1113 type = build_utf8_ref (unmangle_classname
1114 (IDENTIFIER_POINTER (signature),
1115 IDENTIFIER_LENGTH (signature)));
1117 PUSH_FIELD_VALUE (finit, "type", type);
1119 flags = get_access_flags_from_decl (fdecl);
1121 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1123 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1124 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1128 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1130 ((FIELD_STATIC (fdecl)
1131 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1132 : TYPE_FIELDS (field_info_union_node)),
1133 (FIELD_STATIC (fdecl)
1134 ? build_address_of (build_static_field_ref (fdecl))
1135 : byte_position (fdecl)))));
1137 FINISH_RECORD_CONSTRUCTOR (finit);
1142 make_method_value (mdecl)
1147 #define ACC_TRANSLATED 0x4000
1148 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1149 code = null_pointer_node;
1150 if (DECL_RTL_SET_P (mdecl))
1151 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1152 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1153 PUSH_FIELD_VALUE (minit, "name",
1154 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1155 init_identifier_node
1156 : DECL_NAME (mdecl)));
1158 tree signature = build_java_signature (TREE_TYPE (mdecl));
1159 PUSH_FIELD_VALUE (minit, "signature",
1162 (IDENTIFIER_POINTER(signature),
1163 IDENTIFIER_LENGTH(signature)))));
1165 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1166 PUSH_FIELD_VALUE (minit, "ncode", code);
1167 FINISH_RECORD_CONSTRUCTOR (minit);
1172 get_dispatch_vector (type)
1175 tree vtable = TYPE_VTABLE (type);
1180 tree super = CLASSTYPE_SUPER (type);
1181 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1182 vtable = make_tree_vec (nvirtuals);
1183 TYPE_VTABLE (type) = vtable;
1184 if (super != NULL_TREE)
1186 tree super_vtable = get_dispatch_vector (super);
1188 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1189 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1192 for (method = TYPE_METHODS (type); method != NULL_TREE;
1193 method = TREE_CHAIN (method))
1194 if (DECL_VINDEX (method) != NULL_TREE
1195 && host_integerp (DECL_VINDEX (method), 0))
1196 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1204 get_dispatch_table (type, this_class_addr)
1205 tree type, this_class_addr;
1207 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1208 tree vtable = get_dispatch_vector (type);
1210 tree list = NULL_TREE;
1211 int nvirtuals = TREE_VEC_LENGTH (vtable);
1212 for (i = nvirtuals; --i >= 0; )
1214 tree method = TREE_VEC_ELT (vtable, i);
1215 if (METHOD_ABSTRACT (method))
1218 warning_with_decl (method,
1219 "abstract method in non-abstract class");
1220 method = null_pointer_node;
1224 if (!DECL_RTL_SET_P (method))
1225 make_decl_rtl (method, NULL);
1226 method = build1 (ADDR_EXPR, nativecode_ptr_type_node, method);
1228 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
1231 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1232 using the Boehm GC we sometimes stash a GC type descriptor
1233 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1234 the emitted byte count during the output to the assembly file. */
1235 list = tree_cons (NULL_TREE, get_boehm_type_descriptor (type),
1237 list = tree_cons (integer_zero_node, this_class_addr, list);
1238 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
1244 make_class_data (type)
1247 tree decl, cons, temp;
1248 tree field, fields_decl;
1249 tree static_fields = NULL_TREE;
1250 tree instance_fields = NULL_TREE;
1251 HOST_WIDE_INT static_field_count = 0;
1252 HOST_WIDE_INT instance_field_count = 0;
1253 HOST_WIDE_INT field_count;
1254 tree field_array_type;
1256 tree methods = NULL_TREE;
1257 tree dtable_decl = NULL_TREE;
1258 HOST_WIDE_INT method_count = 0;
1259 tree method_array_type;
1262 tree this_class_addr;
1263 tree constant_pool_constructor;
1264 tree interfaces = null_pointer_node;
1265 int interface_len = 0;
1266 tree type_decl = TYPE_NAME (type);
1268 this_class_addr = build_class_ref (type);
1269 decl = TREE_OPERAND (this_class_addr, 0);
1271 /* Build Field array. */
1272 field = TYPE_FIELDS (type);
1273 if (DECL_NAME (field) == NULL_TREE)
1274 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1275 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1277 if (! DECL_ARTIFICIAL (field))
1279 tree init = make_field_value (field);
1280 if (FIELD_STATIC (field))
1282 tree initial = DECL_INITIAL (field);
1283 static_field_count++;
1284 static_fields = tree_cons (NULL_TREE, init, static_fields);
1285 /* If the initial value is a string constant,
1286 prevent output_constant from trying to assemble the value. */
1287 if (initial != NULL_TREE
1288 && TREE_TYPE (initial) == string_ptr_type_node)
1289 DECL_INITIAL (field) = NULL_TREE;
1290 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1291 DECL_INITIAL (field) = initial;
1295 instance_field_count++;
1296 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1300 field_count = static_field_count + instance_field_count;
1301 if (field_count > 0)
1303 static_fields = nreverse (static_fields);
1304 instance_fields = nreverse (instance_fields);
1305 static_fields = chainon (static_fields, instance_fields);
1306 field_array_type = build_prim_array_type (field_type_node, field_count);
1307 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1309 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1310 NULL_TREE, static_fields);
1311 TREE_STATIC (fields_decl) = 1;
1312 DECL_ARTIFICIAL (fields_decl) = 1;
1313 DECL_IGNORED_P (fields_decl) = 1;
1314 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1317 fields_decl = NULL_TREE;
1319 /* Build Method array. */
1320 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1321 method != NULL_TREE; method = TREE_CHAIN (method))
1324 if (METHOD_PRIVATE (method)
1325 && ! flag_keep_inline_functions
1326 && (flag_inline_functions || optimize))
1328 init = make_method_value (method);
1330 methods = tree_cons (NULL_TREE, init, methods);
1332 method_array_type = build_prim_array_type (method_type_node, method_count);
1333 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1335 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1336 NULL_TREE, nreverse (methods));
1337 TREE_STATIC (methods_decl) = 1;
1338 DECL_ARTIFICIAL (methods_decl) = 1;
1339 DECL_IGNORED_P (methods_decl) = 1;
1340 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1342 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1343 && ! CLASS_INTERFACE (type_decl))
1345 tree dtable = get_dispatch_table (type, this_class_addr);
1346 dtable_decl = build_dtable_decl (type);
1347 DECL_INITIAL (dtable_decl) = dtable;
1348 TREE_STATIC (dtable_decl) = 1;
1349 DECL_ARTIFICIAL (dtable_decl) = 1;
1350 DECL_IGNORED_P (dtable_decl) = 1;
1351 TREE_PUBLIC (dtable_decl) = 1;
1352 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1353 if (type == class_type_node)
1354 class_dtable_decl = dtable_decl;
1357 if (class_dtable_decl == NULL_TREE)
1359 class_dtable_decl = build_dtable_decl (class_type_node);
1360 TREE_STATIC (class_dtable_decl) = 1;
1361 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1362 DECL_IGNORED_P (class_dtable_decl) = 1;
1363 if (is_compiled_class (class_type_node) != 2)
1364 DECL_EXTERNAL (class_dtable_decl) = 1;
1365 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1368 super = CLASSTYPE_SUPER (type);
1369 if (super == NULL_TREE)
1370 super = null_pointer_node;
1371 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1372 super = build_class_ref (super);
1375 int super_index = alloc_class_constant (super);
1376 super = build_int_2 (super_index, 0);
1377 TREE_TYPE (super) = ptr_type_node;
1380 /* Build and emit the array of implemented interfaces. */
1381 if (type != object_type_node)
1382 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1383 if (interface_len > 0)
1385 tree init = NULL_TREE;
1387 tree interface_array_type, idecl;
1388 interface_array_type
1389 = build_prim_array_type (class_ptr_type, interface_len);
1390 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1391 interface_array_type);
1392 for (i = interface_len; i > 0; i--)
1394 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1395 tree iclass = BINFO_TYPE (child);
1397 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1398 index = build_class_ref (iclass);
1401 int int_index = alloc_class_constant (iclass);
1402 index = build_int_2 (int_index, 0);
1403 TREE_TYPE (index) = ptr_type_node;
1405 init = tree_cons (NULL_TREE, index, init);
1407 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1409 TREE_STATIC (idecl) = 1;
1410 DECL_ARTIFICIAL (idecl) = 1;
1411 DECL_IGNORED_P (idecl) = 1;
1412 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1413 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1416 constant_pool_constructor = build_constants_constructor ();
1418 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1419 PUSH_FIELD_VALUE (temp, "vtable",
1420 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1421 if (! flag_hash_synchronization)
1422 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1423 FINISH_RECORD_CONSTRUCTOR (temp);
1424 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1425 PUSH_SUPER_VALUE (cons, temp);
1426 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1427 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1428 PUSH_FIELD_VALUE (cons, "accflags",
1429 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1431 PUSH_FIELD_VALUE (cons, "superclass",
1432 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1433 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1434 PUSH_FIELD_VALUE (cons, "methods",
1435 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1436 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1437 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1438 PUSH_FIELD_VALUE (cons, "fields",
1439 fields_decl == NULL_TREE ? null_pointer_node
1440 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1441 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1442 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1443 PUSH_FIELD_VALUE (cons, "static_field_count",
1444 build_int_2 (static_field_count, 0));
1445 PUSH_FIELD_VALUE (cons, "vtable",
1446 dtable_decl == NULL_TREE ? null_pointer_node
1447 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1448 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1449 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1450 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1451 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1453 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1454 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1455 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1456 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1457 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1459 FINISH_RECORD_CONSTRUCTOR (cons);
1461 DECL_INITIAL (decl) = cons;
1462 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1469 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1470 int saw_native_method = 0;
1472 /* Find out if we have any native methods. We use this information
1474 for (method = type_methods;
1475 method != NULL_TREE;
1476 method = TREE_CHAIN (method))
1478 if (METHOD_NATIVE (method))
1480 saw_native_method = 1;
1485 /* Emit deferred inline methods. */
1486 for (method = type_methods; method != NULL_TREE; )
1488 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1490 /* It's a deferred inline method. Decide if we need to emit it. */
1491 if (flag_keep_inline_functions
1492 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (method))
1493 || ! METHOD_PRIVATE (method)
1494 || saw_native_method)
1496 output_inline_function (method);
1497 /* Scan the list again to see if there are any earlier
1499 method = type_methods;
1503 method = TREE_CHAIN (method);
1506 current_function_decl = NULL_TREE;
1507 make_class_data (current_class);
1509 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1512 /* Return 2 if CLASS is compiled by this compilation job;
1513 return 1 if CLASS can otherwise be assumed to be compiled;
1514 return 0 if we cannot assume that CLASS is compiled.
1515 Returns 1 for primitive and 0 for array types. */
1517 is_compiled_class (class)
1521 if (TREE_CODE (class) == POINTER_TYPE)
1522 class = TREE_TYPE (class);
1523 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1525 if (TYPE_ARRAY_P (class))
1527 if (class == current_class)
1530 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1531 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1533 /* The class was seen in the current ZIP file and will be
1534 available as a compiled class in the future but may not have
1535 been loaded already. Load it if necessary. This prevent
1536 build_class_ref () from crashing. */
1538 if (seen_in_zip && !CLASS_LOADED_P (class))
1539 load_class (class, 1);
1541 /* We return 2 for class seen in ZIP and class from files
1542 belonging to the same compilation unit */
1546 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1548 if (!CLASS_LOADED_P (class))
1550 if (CLASS_FROM_SOURCE_P (class))
1551 safe_layout_class (class);
1553 load_class (class, 1);
1561 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1564 build_dtable_decl (type)
1569 /* We need to build a new dtable type so that its size is uniquely
1570 computed when we're dealing with the class for real and not just
1571 faking it (like java.lang.Class during the initialization of the
1572 compiler.) We now we're not faking a class when CURRENT_CLASS is
1574 if (current_class == type)
1576 tree dummy = NULL_TREE, aomt, n;
1578 dtype = make_node (RECORD_TYPE);
1579 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1580 n = build_int_2 (TREE_VEC_LENGTH (get_dispatch_vector (type)), 0);
1581 aomt = build_array_type (ptr_type_node, build_index_type (n));
1582 PUSH_FIELD (dtype, dummy, "methods", aomt);
1583 layout_type (dtype);
1586 dtype = dtable_type;
1588 return build_decl (VAR_DECL,
1589 java_mangle_vtable (&temporary_obstack, type), dtype);
1592 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1593 fields inherited from SUPER_CLASS. */
1596 push_super_field (this_class, super_class)
1597 tree this_class, super_class;
1600 /* Don't insert the field if we're just re-laying the class out. */
1601 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1603 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1604 DECL_IGNORED_P (base_decl) = 1;
1605 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1606 TYPE_FIELDS (this_class) = base_decl;
1607 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1608 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1611 /* Handle the different manners we may have to lay out a super class. */
1614 maybe_layout_super_class (super_class, this_class)
1618 if (TREE_CODE (super_class) == RECORD_TYPE)
1620 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1621 safe_layout_class (super_class);
1622 if (!CLASS_LOADED_P (super_class))
1623 load_class (super_class, 1);
1625 /* We might have to layout the class before its dependency on
1626 the super class gets resolved by java_complete_class */
1627 else if (TREE_CODE (super_class) == POINTER_TYPE)
1629 if (TREE_TYPE (super_class) != NULL_TREE)
1630 super_class = TREE_TYPE (super_class);
1633 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1634 super_class, NULL_TREE, this_class);
1636 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1637 super_class = TREE_TYPE (super_class);
1640 if (!TYPE_SIZE (super_class))
1641 safe_layout_class (super_class);
1647 layout_class (this_class)
1650 tree super_class = CLASSTYPE_SUPER (this_class);
1653 class_list = tree_cons (this_class, NULL_TREE, class_list);
1654 if (CLASS_BEING_LAIDOUT (this_class))
1660 sprintf (buffer, " with `%s'",
1661 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1662 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1664 for (current = TREE_CHAIN (class_list); current;
1665 current = TREE_CHAIN (current))
1667 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1668 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1669 IDENTIFIER_POINTER (DECL_NAME (decl)),
1670 DECL_SOURCE_FILE (decl),
1671 DECL_SOURCE_LINE (decl));
1672 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1674 obstack_1grow (&temporary_obstack, '\0');
1675 report = obstack_finish (&temporary_obstack);
1676 cyclic_inheritance_report = ggc_strdup (report);
1677 obstack_free (&temporary_obstack, report);
1678 TYPE_SIZE (this_class) = error_mark_node;
1681 CLASS_BEING_LAIDOUT (this_class) = 1;
1683 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1685 tree maybe_super_class
1686 = maybe_layout_super_class (super_class, this_class);
1687 if (maybe_super_class == NULL
1688 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1690 TYPE_SIZE (this_class) = error_mark_node;
1691 CLASS_BEING_LAIDOUT (this_class) = 0;
1692 class_list = TREE_CHAIN (class_list);
1695 if (TYPE_SIZE (this_class) == NULL_TREE)
1696 push_super_field (this_class, super_class);
1699 for (field = TYPE_FIELDS (this_class);
1700 field != NULL_TREE; field = TREE_CHAIN (field))
1702 if (FIELD_STATIC (field))
1704 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1705 SET_DECL_ASSEMBLER_NAME (field,
1707 (&temporary_obstack, field));
1711 layout_type (this_class);
1713 /* Also recursively load/layout any superinterfaces, but only if class was
1714 loaded from bytecode. The source parser will take care of this itself. */
1715 if (!CLASS_FROM_SOURCE_P (this_class))
1717 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1721 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1723 for (i = n; i > 0; i--)
1725 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1726 tree super_interface = BINFO_TYPE (vec_elt);
1728 tree maybe_super_interface
1729 = maybe_layout_super_class (super_interface, NULL_TREE);
1730 if (maybe_super_interface == NULL
1731 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1733 TYPE_SIZE (this_class) = error_mark_node;
1734 CLASS_BEING_LAIDOUT (this_class) = 0;
1735 class_list = TREE_CHAIN (class_list);
1742 /* Convert the size back to an SI integer value */
1743 TYPE_SIZE_UNIT (this_class) =
1744 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1746 CLASS_BEING_LAIDOUT (this_class) = 0;
1747 class_list = TREE_CHAIN (class_list);
1751 layout_class_methods (this_class)
1754 tree method_decl, dtable_count;
1755 tree super_class, handle_type;
1757 if (TYPE_NVIRTUALS (this_class))
1760 super_class = CLASSTYPE_SUPER (this_class);
1761 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1765 super_class = maybe_layout_super_class (super_class, this_class);
1766 if (!TYPE_NVIRTUALS (super_class))
1767 layout_class_methods (super_class);
1768 dtable_count = TYPE_NVIRTUALS (super_class);
1771 dtable_count = integer_zero_node;
1773 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1775 for (method_decl = TYPE_METHODS (handle_type);
1776 method_decl; method_decl = TREE_CHAIN (method_decl))
1777 dtable_count = layout_class_method (this_class, super_class,
1778 method_decl, dtable_count);
1780 TYPE_NVIRTUALS (this_class) = dtable_count;
1782 #ifdef JAVA_USE_HANDLES
1783 layout_type (handle_type);
1787 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
1788 and 1 if STR is "greater" than NAME. */
1790 /* Lay METHOD_DECL out, returning a possibly new value of
1791 DTABLE_COUNT. Also mangle the method's name. */
1794 layout_class_method (this_class, super_class, method_decl, dtable_count)
1795 tree this_class, super_class, method_decl, dtable_count;
1797 tree method_name = DECL_NAME (method_decl);
1799 TREE_PUBLIC (method_decl) = 1;
1801 /* This is a good occasion to mangle the method's name */
1802 SET_DECL_ASSEMBLER_NAME (method_decl,
1803 java_mangle_decl (&temporary_obstack,
1805 /* We don't generate a RTL for the method if it's abstract, or if
1806 it's an interface method that isn't clinit. */
1807 if (! METHOD_ABSTRACT (method_decl)
1808 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1809 && (DECL_CLINIT_P (method_decl))))
1810 make_decl_rtl (method_decl, NULL);
1812 if (ID_INIT_P (method_name))
1814 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1816 for (ptr = p; *ptr; )
1821 DECL_CONSTRUCTOR_P (method_decl) = 1;
1822 build_java_argument_signature (TREE_TYPE (method_decl));
1824 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1827 build_java_argument_signature (TREE_TYPE (method_decl));
1828 tree super_method = lookup_argument_method (super_class, method_name,
1830 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1832 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1833 if (DECL_VINDEX (method_decl) == NULL_TREE
1834 && !CLASS_FROM_SOURCE_P (this_class))
1835 error_with_decl (method_decl,
1836 "non-static method '%s' overrides static method");
1838 else if (! METHOD_FINAL (method_decl)
1839 && ! METHOD_PRIVATE (method_decl)
1840 && ! CLASS_FINAL (TYPE_NAME (this_class))
1843 DECL_VINDEX (method_decl) = dtable_count;
1844 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
1845 dtable_count, integer_one_node));
1849 return dtable_count;
1855 /* END does not need to be registered with the garbage collector
1856 because it always points into the list given by REGISTERED_CLASS,
1857 and that variable is registered with the collector. */
1859 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1860 tree current = copy_node (node);
1862 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1863 if (!registered_class)
1864 registered_class = current;
1866 TREE_CHAIN (end) = current;
1871 /* Generate a function that gets called at start-up (static contructor) time,
1872 which calls registerClass for all the compiled classes. */
1875 emit_register_classes ()
1877 extern tree get_file_function_name PARAMS ((int));
1878 tree init_name = get_file_function_name ('I');
1879 tree init_type = build_function_type (void_type_node, end_params_node);
1883 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1884 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
1885 TREE_STATIC (init_decl) = 1;
1886 current_function_decl = init_decl;
1887 DECL_RESULT (init_decl) = build_decl(RESULT_DECL, NULL_TREE, void_type_node);
1888 /* DECL_EXTERNAL (init_decl) = 1;*/
1889 TREE_PUBLIC (init_decl) = 1;
1891 make_decl_rtl (init_decl, NULL);
1892 init_function_start (init_decl, input_filename, 0);
1893 expand_function_start (init_decl, 0);
1895 for ( t = registered_class; t; t = TREE_CHAIN (t))
1896 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1897 XEXP (DECL_RTL (t), 0), Pmode);
1899 expand_function_end (input_filename, 0, 0);
1902 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1903 int saved_flag = flag_inline_functions;
1904 flag_inline_functions = 0;
1905 rest_of_compilation (init_decl);
1906 flag_inline_functions = saved_flag;
1908 current_function_decl = NULL_TREE;
1909 assemble_constructor (IDENTIFIER_POINTER (init_name));
1913 init_class_processing ()
1915 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
1916 ggc_add_tree_root (class_roots, sizeof (class_roots) / sizeof (tree));
1917 fields_ident = get_identifier ("fields");
1918 info_ident = get_identifier ("info");
1919 ggc_add_rtx_root (®isterClass_libfunc, 1);
1920 gcc_obstack_init (&temporary_obstack);