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"
44 /* DOS brain-damage */
46 #define O_BINARY 0 /* MS-DOS brain-damage */
49 static tree make_method_value PARAMS ((tree));
50 static tree build_java_method_type PARAMS ((tree, tree, int));
51 static int32 hashUtf8String PARAMS ((const char *, int));
52 static tree make_field_value PARAMS ((tree));
53 static tree get_dispatch_vector PARAMS ((tree));
54 static tree get_dispatch_table PARAMS ((tree, tree));
55 static void add_interface_do PARAMS ((tree, tree, int));
56 static tree maybe_layout_super_class PARAMS ((tree, tree));
57 static int assume_compiled PARAMS ((const char *));
58 static struct hash_entry *init_test_hash_newfunc PARAMS ((struct hash_entry *,
61 static rtx registerClass_libfunc;
62 static rtx registerResource_libfunc;
64 extern struct obstack permanent_obstack;
65 struct obstack temporary_obstack;
67 /* The compiler generates different code depending on whether or not
68 it can assume certain classes have been compiled down to native
69 code or not. The compiler options -fassume-compiled= and
70 -fno-assume-compiled= are used to create a tree of
71 assume_compiled_node objects. This tree is queried to determine if
72 a class is assume to be compiled or not. Each node in the tree
73 represents either a package or a specific class. */
75 typedef struct assume_compiled_node_struct
77 /* The class or package name. */
80 /* Non-zero if this represents an exclusion. */
83 /* Pointers to other nodes in the tree. */
84 struct assume_compiled_node_struct *parent;
85 struct assume_compiled_node_struct *sibling;
86 struct assume_compiled_node_struct *child;
87 } assume_compiled_node;
89 static assume_compiled_node *find_assume_compiled_node
90 PARAMS ((assume_compiled_node *, const char *));
92 /* This is the root of the include/exclude tree. */
94 static assume_compiled_node *assume_compiled_tree;
96 static tree class_roots[5]
97 = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
98 #define registered_class class_roots[0]
99 #define fields_ident class_roots[1] /* get_identifier ("fields") */
100 #define info_ident class_roots[2] /* get_identifier ("info") */
101 #define class_list class_roots[3]
102 #define class_dtable_decl class_roots[4]
104 /* Return the node that most closely represents the class whose name
105 is IDENT. Start the search from NODE. Return NULL if an
106 appropriate node does not exist. */
108 static assume_compiled_node *
109 find_assume_compiled_node (node, ident)
110 assume_compiled_node *node;
115 size_t node_ident_length = strlen (node->ident);
117 /* node_ident_length is zero at the root of the tree. If the
118 identifiers are the same length, then we have matching
119 classes. Otherwise check if we've matched an enclosing
122 if (node_ident_length == 0
123 || (strncmp (ident, node->ident, node_ident_length) == 0
124 && (strlen (ident) == node_ident_length
125 || ident[node_ident_length] == '.')))
127 /* We've found a match, however, there might be a more
130 assume_compiled_node *found = find_assume_compiled_node (node->child,
138 /* No match yet. Continue through the sibling list. */
139 node = node->sibling;
142 /* No match at all in this tree. */
146 /* Add a new IDENT to the include/exclude tree. It's an exclusion
147 if EXCLUDEP is non-zero. */
150 add_assume_compiled (ident, excludep)
154 assume_compiled_node *parent;
155 assume_compiled_node *node =
156 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
158 node->ident = xstrdup (ident);
159 node->excludep = excludep;
162 /* Create the root of the tree if it doesn't exist yet. */
164 if (NULL == assume_compiled_tree)
166 assume_compiled_tree =
167 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
168 assume_compiled_tree->ident = "";
169 assume_compiled_tree->excludep = 0;
170 assume_compiled_tree->sibling = NULL;
171 assume_compiled_tree->child = NULL;
172 assume_compiled_tree->parent = NULL;
175 /* Calling the function with the empty string means we're setting
176 excludep for the root of the hierarchy. */
180 assume_compiled_tree->excludep = excludep;
184 /* Find the parent node for this new node. PARENT will either be a
185 class or a package name. Adjust PARENT accordingly. */
187 parent = find_assume_compiled_node (assume_compiled_tree, ident);
188 if (ident[strlen (parent->ident)] != '.')
189 parent = parent->parent;
191 /* Insert NODE into the tree. */
193 node->parent = parent;
194 node->sibling = parent->child;
195 parent->child = node;
198 /* Returns non-zero if IDENT is the name of a class that the compiler
199 should assume has been compiled to FIXME */
202 assume_compiled (ident)
205 assume_compiled_node *i;
208 if (NULL == assume_compiled_tree)
211 i = find_assume_compiled_node (assume_compiled_tree,
214 result = ! i->excludep;
219 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
220 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
221 Also, PREFIX is prepended, and SUFFIX is appended. */
224 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
225 const char* old_name;
232 int prefix_len = strlen (prefix);
233 int suffix_len = strlen (suffix);
234 int i = prefix_len + old_length + suffix_len + 1;
238 char *buffer = (char *)alloca (i);
240 strcpy (buffer, prefix);
241 for (i = 0; i < old_length; i++)
243 char ch = old_name[i];
246 buffer[prefix_len + i] = ch;
248 strcpy (buffer + prefix_len + old_length, suffix);
249 return get_identifier (buffer);
252 /* Return an IDENTIFIER_NODE the same as OLD_ID,
253 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
254 Also, PREFIX is prepended, and SUFFIX is appended. */
257 identifier_subst (old_id, prefix, old_char, new_char, suffix)
264 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
265 prefix, old_char, new_char, suffix);
268 /* Generate a valid C identifier from the name of the class TYPE,
269 prefixed by PREFIX. */
272 mangled_classname (prefix, type)
276 tree ident = TYPE_NAME (type);
277 if (TREE_CODE (ident) != IDENTIFIER_NODE)
278 ident = DECL_NAME (ident);
279 return identifier_subst (ident, prefix, '.', '_', "");
286 type = make_node (RECORD_TYPE);
287 #ifdef JAVA_USE_HANDLES
288 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
289 build_pointer_type (type));
290 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
291 methodtable_ptr_type);
292 tree handle_type = make_node (RECORD_TYPE);
293 TREE_CHAIN (field1) = field2;
294 TYPE_FIELDS (handle_type) = field1;
295 TYPE_BINFO (type) = make_tree_vec (7);
296 TYPE_BINFO (handle_type) = make_tree_vec (7);
297 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
298 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
300 TYPE_BINFO (type) = make_tree_vec (6);
302 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
307 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
308 and where each of the constituents is separated by '/',
309 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
312 unmangle_classname (name, name_length)
313 const char *name; int name_length;
315 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
316 /* It's not sufficient to compare to_return and get_identifier
317 (name) to determine whether to_return is qualified. There are
318 cases in signature analysis where name will be stripped of a
320 name = IDENTIFIER_POINTER (to_return);
324 QUALIFIED_P (to_return) = 1;
332 push_class (class_type, class_name)
333 tree class_type, class_name;
335 tree decl, signature;
336 const char *save_input_filename = input_filename;
337 int save_lineno = lineno;
338 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
339 CLASS_P (class_type) = 1;
340 input_filename = IDENTIFIER_POINTER (source_name);
342 decl = build_decl (TYPE_DECL, class_name, class_type);
344 /* dbxout needs a DECL_SIZE if in gstabs mode */
345 DECL_SIZE (decl) = integer_zero_node;
347 input_filename = save_input_filename;
348 lineno = save_lineno;
349 signature = identifier_subst (class_name, "L", '.', '/', ";");
350 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
352 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
353 both a typedef and in the struct name-space. We may want to re-visit
354 this later, but for now it reduces the changes needed for gdb. */
355 DECL_ARTIFICIAL (decl) = 1;
357 pushdecl_top_level (decl);
358 #ifdef JAVA_USE_HANDLES
360 tree handle_name = identifier_subst (class_name,
361 "Handle$", '.', '.', "");
362 tree handle_decl = build_decl (TYPE_DECL, handle_name,
363 CLASS_TO_HANDLE_TYPE (class_type));
364 pushdecl (handle_decl);
371 /* Finds the (global) class named NAME. Creates the class if not found.
372 Also creates associated TYPE_DECL.
373 Does not check if the class actually exists, load the class,
374 fill in field or methods, or do layout_type. */
380 tree decl = IDENTIFIER_CLASS_VALUE (name);
381 if (decl == NULL_TREE)
382 decl = push_class (make_class (), name);
383 return TREE_TYPE (decl);
387 set_super_info (access_flags, this_class, super_class, interfaces_count)
391 int interfaces_count;
393 int total_supers = interfaces_count;
394 tree class_decl = TYPE_NAME (this_class);
398 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
401 tree super_binfo = make_tree_vec (6);
402 BINFO_TYPE (super_binfo) = super_class;
403 BINFO_OFFSET (super_binfo) = integer_zero_node;
404 TREE_VIA_PUBLIC (super_binfo) = 1;
405 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
407 CLASS_HAS_SUPER (this_class) = 1;
410 set_class_decl_access_flags (access_flags, class_decl);
414 set_class_decl_access_flags (access_flags, class_decl)
418 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
419 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
420 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
421 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
422 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
423 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
424 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
425 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
428 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
429 direct sub-classes of Object are 1, and so on. */
436 if (! CLASS_LOADED_P (clas))
437 load_class (clas, 1);
438 if (TYPE_SIZE (clas) == error_mark_node)
440 while (clas != object_type_node)
443 clas = TYPE_BINFO_BASETYPE (clas, 0);
448 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
451 interface_of_p (type1, type2)
457 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
459 n = TREE_VEC_LENGTH (basetype_vec);
460 for (i = 0; i < n; i++)
462 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
463 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
466 for (i = 0; i < n; i++)
468 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
469 if (vec_elt && BINFO_TYPE (vec_elt)
470 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
476 /* Return true iff TYPE1 inherits from TYPE2. */
479 inherits_from_p (type1, type2)
482 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
486 type1 = CLASSTYPE_SUPER (type1);
491 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
494 enclosing_context_p (type1, type2)
497 if (!INNER_CLASS_TYPE_P (type2))
500 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
502 type2 = (INNER_CLASS_TYPE_P (type2) ?
503 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
512 /* Return 1 iff there exists a common enclosing context between TYPE1
515 int common_enclosing_context_p (type1, type2)
518 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
521 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
522 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
523 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
526 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
527 current = (PURE_INNER_CLASS_TYPE_P (current) ?
528 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
530 if (type1 == current)
537 add_interface_do (basetype_vec, interface_class, i)
538 tree basetype_vec, interface_class;
541 tree interface_binfo = make_tree_vec (6);
542 BINFO_TYPE (interface_binfo) = interface_class;
543 BINFO_OFFSET (interface_binfo) = integer_zero_node;
544 TREE_VIA_VIRTUAL (interface_binfo) = 1;
545 TREE_VIA_PUBLIC (interface_binfo) = 1;
546 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
549 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
550 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
551 if attempt is made to add it twice. */
554 maybe_add_interface (this_class, interface_class)
555 tree this_class, interface_class;
557 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
559 int n = TREE_VEC_LENGTH (basetype_vec);
564 error ("internal error - too many interface type");
567 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
569 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
570 return interface_class;
572 add_interface_do (basetype_vec, interface_class, i);
576 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
579 add_interface (this_class, interface_class)
580 tree this_class, interface_class;
582 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
584 int n = TREE_VEC_LENGTH (basetype_vec);
589 error ("internal error - too many interface type");
592 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
595 add_interface_do (basetype_vec, interface_class, i);
599 /* Return the address of a pointer to the first FUNCTION_DECL
600 in the list (*LIST) whose DECL_NAME is NAME. */
603 find_named_method (list, name)
607 while (*list && DECL_NAME (*list) != name)
608 list = &TREE_CHAIN (*list);
614 build_java_method_type (fntype, this_class, access_flags)
619 if (access_flags & ACC_STATIC)
621 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
624 static struct hash_entry *
625 init_test_hash_newfunc (entry, table, string)
626 struct hash_entry *entry;
627 struct hash_table *table;
628 hash_table_key string ATTRIBUTE_UNUSED;
630 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
633 ret = ((struct init_test_hash_entry *)
634 hash_allocate (table, sizeof (struct init_test_hash_entry)));
638 ret->init_test_decl = 0;
639 return (struct hash_entry *) ret;
642 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
643 (parse.y). The hash of a tree node is its pointer value, comparison
647 java_hash_hash_tree_node (k)
654 java_hash_compare_tree_node (k1, k2)
658 return ((char*) k1 == (char*) k2);
662 add_method_1 (handle_class, access_flags, name, function_type)
668 tree method_type, fndecl;
670 method_type = build_java_method_type (function_type,
671 handle_class, access_flags);
673 fndecl = build_decl (FUNCTION_DECL, name, method_type);
674 DECL_CONTEXT (fndecl) = handle_class;
676 DECL_LANG_SPECIFIC (fndecl)
677 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
679 /* Initialize the static initializer test table. */
680 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
681 init_test_hash_newfunc, java_hash_hash_tree_node,
682 java_hash_compare_tree_node);
684 /* Initialize the initialized (static) class table. */
685 if (access_flags & ACC_STATIC)
686 hash_table_init (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
687 init_test_hash_newfunc, java_hash_hash_tree_node,
688 java_hash_compare_tree_node);
690 /* Initialize the static method invocation compound table */
691 if (STATIC_CLASS_INIT_OPT_P ())
692 hash_table_init (&DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl),
693 init_test_hash_newfunc, java_hash_hash_tree_node,
694 java_hash_compare_tree_node);
696 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
697 TYPE_METHODS (handle_class) = fndecl;
699 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
700 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
701 if (access_flags & ACC_PRIVATE)
702 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
703 if (access_flags & ACC_NATIVE)
705 METHOD_NATIVE (fndecl) = 1;
706 DECL_EXTERNAL (fndecl) = 1;
708 if (access_flags & ACC_STATIC)
709 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
710 if (access_flags & ACC_FINAL)
711 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
712 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
713 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
714 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
718 /* Add a method to THIS_CLASS.
719 The method's name is NAME.
720 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
723 add_method (this_class, access_flags, name, method_sig)
729 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
730 tree function_type, fndecl;
731 const unsigned char *sig
732 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
735 fatal_error ("bad method signature");
737 function_type = get_type_from_signature (method_sig);
738 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
739 set_java_signature (TREE_TYPE (fndecl), method_sig);
744 add_field (class, name, field_type, flags)
750 int is_static = (flags & ACC_STATIC) != 0;
752 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
753 TREE_CHAIN (field) = TYPE_FIELDS (class);
754 TYPE_FIELDS (class) = field;
755 DECL_CONTEXT (field) = class;
757 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
758 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
759 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
760 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
761 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
762 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
765 FIELD_STATIC (field) = 1;
766 /* Always make field externally visible. This is required so
767 that native methods can always access the field. */
768 TREE_PUBLIC (field) = 1;
773 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
776 set_constant_value (field, constant)
777 tree field, constant;
779 if (field == NULL_TREE)
780 warning ("misplaced ConstantValue attribute (not in any field)");
781 else if (DECL_INITIAL (field) != NULL_TREE)
782 warning ("duplicate ConstanValue atribute for field '%s'",
783 IDENTIFIER_POINTER (DECL_NAME (field)));
786 DECL_INITIAL (field) = constant;
787 if (TREE_TYPE (constant) != TREE_TYPE (field)
788 && ! (TREE_TYPE (constant) == int_type_node
789 && INTEGRAL_TYPE_P (TREE_TYPE (field))
790 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
791 && ! (TREE_TYPE (constant) == utf8const_ptr_type
792 && TREE_TYPE (field) == string_ptr_type_node))
793 error ("ConstantValue attribute of field '%s' has wrong type",
794 IDENTIFIER_POINTER (DECL_NAME (field)));
795 if (FIELD_FINAL (field))
796 DECL_FIELD_FINAL_IUD (field) = 1;
800 /* Count the number of Unicode chars encoded in a given Ut8 string. */
804 strLengthUtf8 (str, len)
808 register unsigned char* ptr = (unsigned char*) str;
809 register unsigned char *limit = ptr + len;
811 for (; ptr < limit; str_length++) {
812 if (UTF8_GET (ptr, limit) < 0)
820 /* Calculate a hash value for a string encoded in Utf8 format.
821 * This returns the same hash value as specified for java.lang.String.hashCode.
825 hashUtf8String (str, len)
829 register const unsigned char* ptr = (const unsigned char*) str;
830 register const unsigned char *limit = ptr + len;
834 int ch = UTF8_GET (ptr, limit);
835 /* Updated specification from
836 http://www.javasoft.com/docs/books/jls/clarify.html. */
837 hash = (31 * hash) + ch;
842 /* Generate a byte array representing the contents of FILENAME. The
843 array is assigned a unique local symbol. The array represents a
844 compiled Java resource, which is accessed by the runtime using
847 compile_resource_file (name, filename)
851 struct stat stat_buf;
855 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
856 static int Jr_count = 0;
858 fd = open (filename, O_RDONLY | O_BINARY);
861 perror ("Failed to read resource file");
864 if (fstat (fd, &stat_buf) != 0
865 || ! S_ISREG (stat_buf.st_mode))
867 perror ("Could not figure length of resource file");
870 buffer = xmalloc (strlen (name) + stat_buf.st_size);
871 strcpy (buffer, name);
872 read (fd, buffer + strlen (name), stat_buf.st_size);
874 data_type = build_prim_array_type (unsigned_byte_type_node,
875 strlen (name) + stat_buf.st_size);
876 rtype = make_node (RECORD_TYPE);
877 PUSH_FIELD (rtype, field, "name_length", unsigned_int_type_node);
878 PUSH_FIELD (rtype, field, "resource_length", unsigned_int_type_node);
879 PUSH_FIELD (rtype, field, "data", data_type);
880 FINISH_RECORD (rtype);
881 START_RECORD_CONSTRUCTOR (rinit, rtype);
882 PUSH_FIELD_VALUE (rinit, "name_length",
883 build_int_2 (strlen (name), 0));
884 PUSH_FIELD_VALUE (rinit, "resource_length",
885 build_int_2 (stat_buf.st_size, 0));
886 data = build_string (strlen(name) + stat_buf.st_size, buffer);
887 TREE_TYPE (data) = data_type;
888 PUSH_FIELD_VALUE (rinit, "data", data);
889 FINISH_RECORD_CONSTRUCTOR (rinit);
890 TREE_CONSTANT (rinit) = 1;
892 /* Generate a unique-enough identifier. */
893 sprintf(buf, "_Jr%d", ++Jr_count);
895 decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
896 TREE_STATIC (decl) = 1;
897 DECL_ARTIFICIAL (decl) = 1;
898 DECL_IGNORED_P (decl) = 1;
899 TREE_READONLY (decl) = 1;
900 TREE_THIS_VOLATILE (decl) = 0;
901 DECL_INITIAL (decl) = rinit;
902 layout_decl (decl, 0);
904 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
905 make_decl_rtl (decl, (char*) 0);
906 assemble_variable (decl, 1, 0, 0);
909 tree init_name = get_file_function_name ('I');
910 tree init_type = build_function_type (void_type_node, end_params_node);
913 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
914 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
915 TREE_STATIC (init_decl) = 1;
916 current_function_decl = init_decl;
917 DECL_RESULT (init_decl) = build_decl (RESULT_DECL,
918 NULL_TREE, void_type_node);
919 /* DECL_EXTERNAL (init_decl) = 1;*/
920 TREE_PUBLIC (init_decl) = 1;
922 make_decl_rtl (init_decl, NULL);
923 init_function_start (init_decl, input_filename, 0);
924 expand_function_start (init_decl, 0);
926 emit_library_call (registerResource_libfunc, 0, VOIDmode, 1,
927 gen_rtx (SYMBOL_REF, Pmode, buf),
930 expand_function_end (input_filename, 0, 0);
933 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
934 int saved_flag = flag_inline_functions;
935 flag_inline_functions = 0;
936 rest_of_compilation (init_decl);
937 flag_inline_functions = saved_flag;
939 current_function_decl = NULL_TREE;
940 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
941 DEFAULT_INIT_PRIORITY);
945 tree utf8_decl_list = NULL_TREE;
948 build_utf8_ref (name)
951 const char * name_ptr = IDENTIFIER_POINTER(name);
952 int name_len = IDENTIFIER_LENGTH(name);
954 tree ctype, field = NULL_TREE, str_type, cinit, string;
955 static int utf8_count = 0;
957 tree ref = IDENTIFIER_UTF8_REF (name);
959 if (ref != NULL_TREE)
962 ctype = make_node (RECORD_TYPE);
963 str_type = build_prim_array_type (unsigned_byte_type_node,
964 name_len + 1); /* Allow for final '\0'. */
965 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
966 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
967 PUSH_FIELD (ctype, field, "data", str_type);
968 FINISH_RECORD (ctype);
969 START_RECORD_CONSTRUCTOR (cinit, ctype);
970 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
971 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
972 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
973 string = build_string (name_len, name_ptr);
974 TREE_TYPE (string) = str_type;
975 PUSH_FIELD_VALUE (cinit, "data", string);
976 FINISH_RECORD_CONSTRUCTOR (cinit);
977 TREE_CONSTANT (cinit) = 1;
979 /* Generate a unique-enough identifier. */
980 sprintf(buf, "_Utf%d", ++utf8_count);
982 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
983 TREE_STATIC (decl) = 1;
984 DECL_ARTIFICIAL (decl) = 1;
985 DECL_IGNORED_P (decl) = 1;
986 TREE_READONLY (decl) = 1;
987 TREE_THIS_VOLATILE (decl) = 0;
988 DECL_INITIAL (decl) = cinit;
989 TREE_CHAIN (decl) = utf8_decl_list;
990 layout_decl (decl, 0);
992 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
993 utf8_decl_list = decl;
994 make_decl_rtl (decl, (char*) 0);
995 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
996 IDENTIFIER_UTF8_REF (name) = ref;
1000 /* Build a reference to the class TYPE.
1001 Also handles primitive types and array types. */
1004 build_class_ref (type)
1007 int is_compiled = is_compiled_class (type);
1010 tree ref, decl_name, decl;
1011 if (TREE_CODE (type) == POINTER_TYPE)
1012 type = TREE_TYPE (type);
1013 if (TREE_CODE (type) == RECORD_TYPE)
1015 if (TYPE_SIZE (type) == error_mark_node)
1016 return null_pointer_node;
1017 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1018 "", '/', '/', ".class");
1019 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1020 if (decl == NULL_TREE)
1022 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1023 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
1024 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
1025 TREE_STATIC (decl) = 1;
1026 TREE_PUBLIC (decl) = 1;
1027 DECL_IGNORED_P (decl) = 1;
1028 DECL_ARTIFICIAL (decl) = 1;
1029 SET_DECL_ASSEMBLER_NAME (decl,
1030 java_mangle_class_field
1031 (&temporary_obstack, type));
1032 make_decl_rtl (decl, NULL);
1033 pushdecl_top_level (decl);
1034 if (is_compiled == 1)
1035 DECL_EXTERNAL (decl) = 1;
1042 if (flag_emit_class_files)
1044 const char *prim_class_name;
1046 if (type == char_type_node)
1047 prim_class_name = "java.lang.Character";
1048 else if (type == boolean_type_node)
1049 prim_class_name = "java.lang.Boolean";
1050 else if (type == byte_type_node)
1051 prim_class_name = "java.lang.Byte";
1052 else if (type == short_type_node)
1053 prim_class_name = "java.lang.Short";
1054 else if (type == int_type_node)
1055 prim_class_name = "java.lang.Integer";
1056 else if (type == long_type_node)
1057 prim_class_name = "java.lang.Long";
1058 else if (type == float_type_node)
1059 prim_class_name = "java.lang.Float";
1060 else if (type == double_type_node)
1061 prim_class_name = "java.lang.Double";
1062 else if (type == void_type_node)
1063 prim_class_name = "java.lang.Void";
1067 prim_class = lookup_class (get_identifier (prim_class_name));
1068 return build (COMPONENT_REF, NULL_TREE,
1069 prim_class, TYPE_identifier_node);
1071 decl_name = TYPE_NAME (type);
1072 if (TREE_CODE (decl_name) == TYPE_DECL)
1073 decl_name = DECL_NAME (decl_name);
1074 name = IDENTIFIER_POINTER (decl_name);
1075 if (strncmp (name, "promoted_", 9) == 0)
1077 sprintf (buffer, "_Jv_%sClass", name);
1078 decl_name = get_identifier (buffer);
1079 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1080 if (decl == NULL_TREE)
1082 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1083 TREE_STATIC (decl) = 1;
1084 TREE_PUBLIC (decl) = 1;
1085 make_decl_rtl (decl, NULL);
1086 pushdecl_top_level (decl);
1087 if (is_compiled == 1)
1088 DECL_EXTERNAL (decl) = 1;
1092 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1099 index = alloc_class_constant (type);
1100 cl = build_ref_from_constant_pool (index);
1101 TREE_TYPE (cl) = promote_type (class_ptr_type);
1107 build_static_field_ref (fdecl)
1110 tree fclass = DECL_CONTEXT (fdecl);
1111 int is_compiled = is_compiled_class (fclass);
1114 if (!DECL_RTL_SET_P (fdecl))
1116 if (is_compiled == 1)
1117 DECL_EXTERNAL (fdecl) = 1;
1118 make_decl_rtl (fdecl, NULL);
1125 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1126 tree ref = build_class_ref (fclass);
1128 int field_index = 0;
1129 ref = build1 (INDIRECT_REF, class_type_node, ref);
1130 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1131 lookup_field (&class_type_node, fields_ident));
1133 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1137 if (fld == NULL_TREE)
1138 fatal_error ("field '%s' not found in class",
1139 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1140 if (FIELD_STATIC (fld))
1143 field_index *= int_size_in_bytes (field_type_node);
1144 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1145 ref, build_int_2 (field_index, 0)));
1146 ref = build1 (INDIRECT_REF, field_type_node, ref);
1147 ref = build (COMPONENT_REF, field_info_union_node,
1148 ref, lookup_field (&field_type_node, info_ident));
1149 ref = build (COMPONENT_REF, ptr_type_node,
1150 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1151 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1156 get_access_flags_from_decl (decl)
1159 int access_flags = 0;
1160 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1162 if (FIELD_STATIC (decl))
1163 access_flags |= ACC_STATIC;
1164 if (FIELD_PUBLIC (decl))
1165 access_flags |= ACC_PUBLIC;
1166 if (FIELD_PROTECTED (decl))
1167 access_flags |= ACC_PROTECTED;
1168 if (FIELD_PRIVATE (decl))
1169 access_flags |= ACC_PRIVATE;
1170 if (FIELD_FINAL (decl))
1171 access_flags |= ACC_FINAL;
1172 if (FIELD_VOLATILE (decl))
1173 access_flags |= ACC_VOLATILE;
1174 if (FIELD_TRANSIENT (decl))
1175 access_flags |= ACC_TRANSIENT;
1176 return access_flags;
1178 if (TREE_CODE (decl) == TYPE_DECL)
1180 if (CLASS_PUBLIC (decl))
1181 access_flags |= ACC_PUBLIC;
1182 if (CLASS_FINAL (decl))
1183 access_flags |= ACC_FINAL;
1184 if (CLASS_SUPER (decl))
1185 access_flags |= ACC_SUPER;
1186 if (CLASS_INTERFACE (decl))
1187 access_flags |= ACC_INTERFACE;
1188 if (CLASS_ABSTRACT (decl))
1189 access_flags |= ACC_ABSTRACT;
1190 if (CLASS_STATIC (decl))
1191 access_flags |= ACC_STATIC;
1192 if (CLASS_PRIVATE (decl))
1193 access_flags |= ACC_PRIVATE;
1194 if (CLASS_PROTECTED (decl))
1195 access_flags |= ACC_PROTECTED;
1196 return access_flags;
1198 if (TREE_CODE (decl) == FUNCTION_DECL)
1200 if (METHOD_PUBLIC (decl))
1201 access_flags |= ACC_PUBLIC;
1202 if (METHOD_PRIVATE (decl))
1203 access_flags |= ACC_PRIVATE;
1204 if (METHOD_PROTECTED (decl))
1205 access_flags |= ACC_PROTECTED;
1206 if (METHOD_STATIC (decl))
1207 access_flags |= ACC_STATIC;
1208 if (METHOD_FINAL (decl))
1209 access_flags |= ACC_FINAL;
1210 if (METHOD_SYNCHRONIZED (decl))
1211 access_flags |= ACC_SYNCHRONIZED;
1212 if (METHOD_NATIVE (decl))
1213 access_flags |= ACC_NATIVE;
1214 if (METHOD_ABSTRACT (decl))
1215 access_flags |= ACC_ABSTRACT;
1216 if (METHOD_TRANSIENT (decl))
1217 access_flags |= ACC_TRANSIENT;
1218 return access_flags;
1224 make_field_value (fdecl)
1229 tree type = TREE_TYPE (fdecl);
1230 int resolved = is_compiled_class (type);
1232 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1233 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1235 type = build_class_ref (type);
1238 tree signature = build_java_signature (type);
1240 type = build_utf8_ref (unmangle_classname
1241 (IDENTIFIER_POINTER (signature),
1242 IDENTIFIER_LENGTH (signature)));
1244 PUSH_FIELD_VALUE (finit, "type", type);
1246 flags = get_access_flags_from_decl (fdecl);
1248 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1250 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1251 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1255 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1257 ((FIELD_STATIC (fdecl)
1258 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1259 : TYPE_FIELDS (field_info_union_node)),
1260 (FIELD_STATIC (fdecl)
1261 ? build_address_of (build_static_field_ref (fdecl))
1262 : byte_position (fdecl)))));
1264 FINISH_RECORD_CONSTRUCTOR (finit);
1269 make_method_value (mdecl)
1272 static int method_name_count = 0;
1275 #define ACC_TRANSLATED 0x4000
1276 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1277 code = null_pointer_node;
1278 if (DECL_RTL_SET_P (mdecl))
1279 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1280 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1281 PUSH_FIELD_VALUE (minit, "name",
1282 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1283 init_identifier_node
1284 : DECL_NAME (mdecl)));
1286 tree signature = build_java_signature (TREE_TYPE (mdecl));
1287 PUSH_FIELD_VALUE (minit, "signature",
1290 (IDENTIFIER_POINTER(signature),
1291 IDENTIFIER_LENGTH(signature)))));
1293 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1294 PUSH_FIELD_VALUE (minit, "ncode", code);
1297 /* Compute the `throws' information for the method. */
1298 tree table = integer_zero_node;
1299 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1301 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1302 tree iter, type, array;
1305 table = tree_cons (NULL_TREE, table, NULL_TREE);
1306 for (iter = DECL_FUNCTION_THROWS (mdecl);
1308 iter = TREE_CHAIN (iter))
1310 tree sig = build_java_signature (TREE_VALUE (iter));
1312 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1313 IDENTIFIER_LENGTH (sig)));
1314 table = tree_cons (NULL_TREE, utf8, table);
1316 type = build_prim_array_type (ptr_type_node, length);
1317 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1318 /* Compute something unique enough. */
1319 sprintf (buf, "_methods%d", method_name_count++);
1320 array = build_decl (VAR_DECL, get_identifier (buf), type);
1321 DECL_INITIAL (array) = table;
1322 TREE_STATIC (array) = 1;
1323 DECL_ARTIFICIAL (array) = 1;
1324 DECL_IGNORED_P (array) = 1;
1325 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1327 table = build1 (ADDR_EXPR, ptr_type_node, array);
1330 PUSH_FIELD_VALUE (minit, "throws", table);
1333 FINISH_RECORD_CONSTRUCTOR (minit);
1338 get_dispatch_vector (type)
1341 tree vtable = TYPE_VTABLE (type);
1346 tree super = CLASSTYPE_SUPER (type);
1347 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1348 vtable = make_tree_vec (nvirtuals);
1349 TYPE_VTABLE (type) = vtable;
1350 if (super != NULL_TREE)
1352 tree super_vtable = get_dispatch_vector (super);
1354 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1355 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1358 for (method = TYPE_METHODS (type); method != NULL_TREE;
1359 method = TREE_CHAIN (method))
1360 if (DECL_VINDEX (method) != NULL_TREE
1361 && host_integerp (DECL_VINDEX (method), 0))
1362 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1370 get_dispatch_table (type, this_class_addr)
1371 tree type, this_class_addr;
1373 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1374 tree vtable = get_dispatch_vector (type);
1376 tree list = NULL_TREE;
1377 int nvirtuals = TREE_VEC_LENGTH (vtable);
1378 for (i = nvirtuals; --i >= 0; )
1380 tree method = TREE_VEC_ELT (vtable, i);
1381 if (METHOD_ABSTRACT (method))
1384 warning_with_decl (method,
1385 "abstract method in non-abstract class");
1386 method = null_pointer_node;
1390 if (!DECL_RTL_SET_P (method))
1391 make_decl_rtl (method, NULL);
1392 method = build1 (ADDR_EXPR, nativecode_ptr_type_node, method);
1394 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
1397 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1398 using the Boehm GC we sometimes stash a GC type descriptor
1399 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1400 the emitted byte count during the output to the assembly file. */
1401 list = tree_cons (NULL_TREE, get_boehm_type_descriptor (type),
1403 list = tree_cons (integer_zero_node, this_class_addr, list);
1404 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
1410 make_class_data (type)
1413 tree decl, cons, temp;
1414 tree field, fields_decl;
1415 tree static_fields = NULL_TREE;
1416 tree instance_fields = NULL_TREE;
1417 HOST_WIDE_INT static_field_count = 0;
1418 HOST_WIDE_INT instance_field_count = 0;
1419 HOST_WIDE_INT field_count;
1420 tree field_array_type;
1422 tree methods = NULL_TREE;
1423 tree dtable_decl = NULL_TREE;
1424 HOST_WIDE_INT method_count = 0;
1425 tree method_array_type;
1428 tree this_class_addr;
1429 tree constant_pool_constructor;
1430 tree interfaces = null_pointer_node;
1431 int interface_len = 0;
1432 tree type_decl = TYPE_NAME (type);
1434 this_class_addr = build_class_ref (type);
1435 decl = TREE_OPERAND (this_class_addr, 0);
1437 /* Build Field array. */
1438 field = TYPE_FIELDS (type);
1439 if (DECL_NAME (field) == NULL_TREE)
1440 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1441 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1443 if (! DECL_ARTIFICIAL (field))
1445 tree init = make_field_value (field);
1446 if (FIELD_STATIC (field))
1448 tree initial = DECL_INITIAL (field);
1449 static_field_count++;
1450 static_fields = tree_cons (NULL_TREE, init, static_fields);
1451 /* If the initial value is a string constant,
1452 prevent output_constant from trying to assemble the value. */
1453 if (initial != NULL_TREE
1454 && TREE_TYPE (initial) == string_ptr_type_node)
1455 DECL_INITIAL (field) = NULL_TREE;
1456 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1457 DECL_INITIAL (field) = initial;
1461 instance_field_count++;
1462 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1466 field_count = static_field_count + instance_field_count;
1467 if (field_count > 0)
1469 static_fields = nreverse (static_fields);
1470 instance_fields = nreverse (instance_fields);
1471 static_fields = chainon (static_fields, instance_fields);
1472 field_array_type = build_prim_array_type (field_type_node, field_count);
1473 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1475 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1476 NULL_TREE, static_fields);
1477 TREE_STATIC (fields_decl) = 1;
1478 DECL_ARTIFICIAL (fields_decl) = 1;
1479 DECL_IGNORED_P (fields_decl) = 1;
1480 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1483 fields_decl = NULL_TREE;
1485 /* Build Method array. */
1486 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1487 method != NULL_TREE; method = TREE_CHAIN (method))
1490 if (METHOD_PRIVATE (method)
1491 && ! flag_keep_inline_functions
1492 && (flag_inline_functions || optimize))
1494 init = make_method_value (method);
1496 methods = tree_cons (NULL_TREE, init, methods);
1498 method_array_type = build_prim_array_type (method_type_node, method_count);
1499 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1501 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1502 NULL_TREE, nreverse (methods));
1503 TREE_STATIC (methods_decl) = 1;
1504 DECL_ARTIFICIAL (methods_decl) = 1;
1505 DECL_IGNORED_P (methods_decl) = 1;
1506 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1508 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1509 && ! CLASS_INTERFACE (type_decl))
1511 tree dtable = get_dispatch_table (type, this_class_addr);
1512 dtable_decl = build_dtable_decl (type);
1513 DECL_INITIAL (dtable_decl) = dtable;
1514 TREE_STATIC (dtable_decl) = 1;
1515 DECL_ARTIFICIAL (dtable_decl) = 1;
1516 DECL_IGNORED_P (dtable_decl) = 1;
1517 TREE_PUBLIC (dtable_decl) = 1;
1518 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1519 if (type == class_type_node)
1520 class_dtable_decl = dtable_decl;
1523 if (class_dtable_decl == NULL_TREE)
1525 class_dtable_decl = build_dtable_decl (class_type_node);
1526 TREE_STATIC (class_dtable_decl) = 1;
1527 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1528 DECL_IGNORED_P (class_dtable_decl) = 1;
1529 if (is_compiled_class (class_type_node) != 2)
1530 DECL_EXTERNAL (class_dtable_decl) = 1;
1531 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1534 super = CLASSTYPE_SUPER (type);
1535 if (super == NULL_TREE)
1536 super = null_pointer_node;
1537 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1538 super = build_class_ref (super);
1541 int super_index = alloc_class_constant (super);
1542 super = build_int_2 (super_index, 0);
1543 TREE_TYPE (super) = ptr_type_node;
1546 /* Build and emit the array of implemented interfaces. */
1547 if (type != object_type_node)
1548 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1549 if (interface_len > 0)
1551 tree init = NULL_TREE;
1553 tree interface_array_type, idecl;
1554 interface_array_type
1555 = build_prim_array_type (class_ptr_type, interface_len);
1556 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1557 interface_array_type);
1558 for (i = interface_len; i > 0; i--)
1560 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1561 tree iclass = BINFO_TYPE (child);
1563 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1564 index = build_class_ref (iclass);
1567 int int_index = alloc_class_constant (iclass);
1568 index = build_int_2 (int_index, 0);
1569 TREE_TYPE (index) = ptr_type_node;
1571 init = tree_cons (NULL_TREE, index, init);
1573 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1575 TREE_STATIC (idecl) = 1;
1576 DECL_ARTIFICIAL (idecl) = 1;
1577 DECL_IGNORED_P (idecl) = 1;
1578 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1579 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1582 constant_pool_constructor = build_constants_constructor ();
1584 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1585 PUSH_FIELD_VALUE (temp, "vtable",
1586 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1587 if (! flag_hash_synchronization)
1588 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1589 FINISH_RECORD_CONSTRUCTOR (temp);
1590 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1591 PUSH_SUPER_VALUE (cons, temp);
1592 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1593 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1594 PUSH_FIELD_VALUE (cons, "accflags",
1595 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1597 PUSH_FIELD_VALUE (cons, "superclass",
1598 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1599 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1600 PUSH_FIELD_VALUE (cons, "methods",
1601 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1602 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1603 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1604 PUSH_FIELD_VALUE (cons, "fields",
1605 fields_decl == NULL_TREE ? null_pointer_node
1606 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1607 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1608 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1609 PUSH_FIELD_VALUE (cons, "static_field_count",
1610 build_int_2 (static_field_count, 0));
1611 PUSH_FIELD_VALUE (cons, "vtable",
1612 dtable_decl == NULL_TREE ? null_pointer_node
1613 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1614 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1615 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1616 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1617 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1619 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1620 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1621 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1622 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1623 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1624 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1626 FINISH_RECORD_CONSTRUCTOR (cons);
1628 DECL_INITIAL (decl) = cons;
1629 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1636 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1637 int saw_native_method = 0;
1639 /* Find out if we have any native methods. We use this information
1641 for (method = type_methods;
1642 method != NULL_TREE;
1643 method = TREE_CHAIN (method))
1645 if (METHOD_NATIVE (method))
1647 saw_native_method = 1;
1652 /* Emit deferred inline methods. */
1653 for (method = type_methods; method != NULL_TREE; )
1655 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1657 output_inline_function (method);
1658 /* Scan the list again to see if there are any earlier
1660 method = type_methods;
1663 method = TREE_CHAIN (method);
1666 current_function_decl = NULL_TREE;
1667 make_class_data (current_class);
1669 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1672 /* Return 2 if CLASS is compiled by this compilation job;
1673 return 1 if CLASS can otherwise be assumed to be compiled;
1674 return 0 if we cannot assume that CLASS is compiled.
1675 Returns 1 for primitive and 0 for array types. */
1677 is_compiled_class (class)
1681 if (TREE_CODE (class) == POINTER_TYPE)
1682 class = TREE_TYPE (class);
1683 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1685 if (TYPE_ARRAY_P (class))
1687 if (class == current_class)
1690 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1691 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1693 /* The class was seen in the current ZIP file and will be
1694 available as a compiled class in the future but may not have
1695 been loaded already. Load it if necessary. This prevent
1696 build_class_ref () from crashing. */
1698 if (seen_in_zip && !CLASS_LOADED_P (class))
1699 load_class (class, 1);
1701 /* We return 2 for class seen in ZIP and class from files
1702 belonging to the same compilation unit */
1706 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1708 if (!CLASS_LOADED_P (class))
1710 if (CLASS_FROM_SOURCE_P (class))
1711 safe_layout_class (class);
1713 load_class (class, 1);
1721 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1724 build_dtable_decl (type)
1729 /* We need to build a new dtable type so that its size is uniquely
1730 computed when we're dealing with the class for real and not just
1731 faking it (like java.lang.Class during the initialization of the
1732 compiler.) We now we're not faking a class when CURRENT_CLASS is
1734 if (current_class == type)
1736 tree dummy = NULL_TREE, aomt, n;
1738 dtype = make_node (RECORD_TYPE);
1739 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1740 n = build_int_2 (TREE_VEC_LENGTH (get_dispatch_vector (type)), 0);
1741 aomt = build_array_type (ptr_type_node, build_index_type (n));
1742 PUSH_FIELD (dtype, dummy, "methods", aomt);
1743 layout_type (dtype);
1746 dtype = dtable_type;
1748 return build_decl (VAR_DECL,
1749 java_mangle_vtable (&temporary_obstack, type), dtype);
1752 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1753 fields inherited from SUPER_CLASS. */
1756 push_super_field (this_class, super_class)
1757 tree this_class, super_class;
1760 /* Don't insert the field if we're just re-laying the class out. */
1761 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1763 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1764 DECL_IGNORED_P (base_decl) = 1;
1765 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1766 TYPE_FIELDS (this_class) = base_decl;
1767 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1768 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1771 /* Handle the different manners we may have to lay out a super class. */
1774 maybe_layout_super_class (super_class, this_class)
1778 if (TREE_CODE (super_class) == RECORD_TYPE)
1780 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1781 safe_layout_class (super_class);
1782 if (!CLASS_LOADED_P (super_class))
1783 load_class (super_class, 1);
1785 /* We might have to layout the class before its dependency on
1786 the super class gets resolved by java_complete_class */
1787 else if (TREE_CODE (super_class) == POINTER_TYPE)
1789 if (TREE_TYPE (super_class) != NULL_TREE)
1790 super_class = TREE_TYPE (super_class);
1793 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1794 super_class, NULL_TREE, this_class);
1796 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1797 super_class = TREE_TYPE (super_class);
1800 if (!TYPE_SIZE (super_class))
1801 safe_layout_class (super_class);
1807 layout_class (this_class)
1810 tree super_class = CLASSTYPE_SUPER (this_class);
1813 class_list = tree_cons (this_class, NULL_TREE, class_list);
1814 if (CLASS_BEING_LAIDOUT (this_class))
1820 sprintf (buffer, " with `%s'",
1821 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1822 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1824 for (current = TREE_CHAIN (class_list); current;
1825 current = TREE_CHAIN (current))
1827 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1828 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1829 IDENTIFIER_POINTER (DECL_NAME (decl)),
1830 DECL_SOURCE_FILE (decl),
1831 DECL_SOURCE_LINE (decl));
1832 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1834 obstack_1grow (&temporary_obstack, '\0');
1835 report = obstack_finish (&temporary_obstack);
1836 cyclic_inheritance_report = ggc_strdup (report);
1837 obstack_free (&temporary_obstack, report);
1838 TYPE_SIZE (this_class) = error_mark_node;
1841 CLASS_BEING_LAIDOUT (this_class) = 1;
1843 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1845 tree maybe_super_class
1846 = maybe_layout_super_class (super_class, this_class);
1847 if (maybe_super_class == NULL
1848 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1850 TYPE_SIZE (this_class) = error_mark_node;
1851 CLASS_BEING_LAIDOUT (this_class) = 0;
1852 class_list = TREE_CHAIN (class_list);
1855 if (TYPE_SIZE (this_class) == NULL_TREE)
1856 push_super_field (this_class, maybe_super_class);
1859 for (field = TYPE_FIELDS (this_class);
1860 field != NULL_TREE; field = TREE_CHAIN (field))
1862 if (FIELD_STATIC (field))
1864 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1865 SET_DECL_ASSEMBLER_NAME (field,
1867 (&temporary_obstack, field));
1871 layout_type (this_class);
1873 /* Also recursively load/layout any superinterfaces, but only if class was
1874 loaded from bytecode. The source parser will take care of this itself. */
1875 if (!CLASS_FROM_SOURCE_P (this_class))
1877 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1881 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1883 for (i = n; i > 0; i--)
1885 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1886 tree super_interface = BINFO_TYPE (vec_elt);
1888 tree maybe_super_interface
1889 = maybe_layout_super_class (super_interface, NULL_TREE);
1890 if (maybe_super_interface == NULL
1891 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1893 TYPE_SIZE (this_class) = error_mark_node;
1894 CLASS_BEING_LAIDOUT (this_class) = 0;
1895 class_list = TREE_CHAIN (class_list);
1902 /* Convert the size back to an SI integer value */
1903 TYPE_SIZE_UNIT (this_class) =
1904 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1906 CLASS_BEING_LAIDOUT (this_class) = 0;
1907 class_list = TREE_CHAIN (class_list);
1911 layout_class_methods (this_class)
1914 tree method_decl, dtable_count;
1915 tree super_class, handle_type;
1917 if (TYPE_NVIRTUALS (this_class))
1920 super_class = CLASSTYPE_SUPER (this_class);
1921 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1925 super_class = maybe_layout_super_class (super_class, this_class);
1926 if (!TYPE_NVIRTUALS (super_class))
1927 layout_class_methods (super_class);
1928 dtable_count = TYPE_NVIRTUALS (super_class);
1931 dtable_count = integer_zero_node;
1933 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1935 for (method_decl = TYPE_METHODS (handle_type);
1936 method_decl; method_decl = TREE_CHAIN (method_decl))
1937 dtable_count = layout_class_method (this_class, super_class,
1938 method_decl, dtable_count);
1940 TYPE_NVIRTUALS (this_class) = dtable_count;
1942 #ifdef JAVA_USE_HANDLES
1943 layout_type (handle_type);
1947 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
1948 and 1 if STR is "greater" than NAME. */
1950 /* Lay METHOD_DECL out, returning a possibly new value of
1951 DTABLE_COUNT. Also mangle the method's name. */
1954 layout_class_method (this_class, super_class, method_decl, dtable_count)
1955 tree this_class, super_class, method_decl, dtable_count;
1957 tree method_name = DECL_NAME (method_decl);
1959 TREE_PUBLIC (method_decl) = 1;
1961 /* This is a good occasion to mangle the method's name */
1962 SET_DECL_ASSEMBLER_NAME (method_decl,
1963 java_mangle_decl (&temporary_obstack,
1965 /* We don't generate a RTL for the method if it's abstract, or if
1966 it's an interface method that isn't clinit. */
1967 if (! METHOD_ABSTRACT (method_decl)
1968 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1969 && (DECL_CLINIT_P (method_decl))))
1970 make_decl_rtl (method_decl, NULL);
1972 if (ID_INIT_P (method_name))
1974 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1976 for (ptr = p; *ptr; )
1981 DECL_CONSTRUCTOR_P (method_decl) = 1;
1982 build_java_argument_signature (TREE_TYPE (method_decl));
1984 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1987 build_java_argument_signature (TREE_TYPE (method_decl));
1988 tree super_method = lookup_argument_method (super_class, method_name,
1990 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1992 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1993 if (DECL_VINDEX (method_decl) == NULL_TREE
1994 && !CLASS_FROM_SOURCE_P (this_class))
1995 error_with_decl (method_decl,
1996 "non-static method '%s' overrides static method");
1998 else if (! METHOD_FINAL (method_decl)
1999 && ! METHOD_PRIVATE (method_decl)
2000 && ! CLASS_FINAL (TYPE_NAME (this_class))
2003 DECL_VINDEX (method_decl) = dtable_count;
2004 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2005 dtable_count, integer_one_node));
2009 return dtable_count;
2015 /* END does not need to be registered with the garbage collector
2016 because it always points into the list given by REGISTERED_CLASS,
2017 and that variable is registered with the collector. */
2019 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2020 tree current = copy_node (node);
2022 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2023 if (!registered_class)
2024 registered_class = current;
2026 TREE_CHAIN (end) = current;
2031 /* Emit something to register classes at start-up time.
2033 The preferred mechanism is through the .jcr section, which contain
2034 a list of pointers to classes which get registered during
2035 constructor invoction time. The fallback mechanism is to generate
2036 a `constructor' function which calls _Jv_RegisterClass for each
2037 class in this file. */
2040 emit_register_classes ()
2042 /* ??? This isn't quite the correct test. We also have to know
2043 that the target is using gcc's crtbegin/crtend objects rather
2044 than the ones that come with the operating system. */
2045 if (SUPPORTS_WEAK && targetm.have_named_sections)
2047 #ifdef JCR_SECTION_NAME
2049 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2050 assemble_align (POINTER_SIZE);
2051 for (t = registered_class; t; t = TREE_CHAIN (t))
2052 assemble_integer (XEXP (DECL_RTL (t), 0),
2053 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2060 extern tree get_file_function_name PARAMS ((int));
2061 tree init_name = get_file_function_name ('I');
2062 tree init_type = build_function_type (void_type_node, end_params_node);
2066 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2067 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2068 TREE_STATIC (init_decl) = 1;
2069 current_function_decl = init_decl;
2070 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2073 /* It can be a static function as long as collect2 does not have
2074 to scan the object file to find its ctor/dtor routine. */
2075 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2077 /* Suppress spurious warnings. */
2078 TREE_USED (init_decl) = 1;
2081 make_decl_rtl (init_decl, NULL);
2082 init_function_start (init_decl, input_filename, 0);
2083 expand_function_start (init_decl, 0);
2085 /* Do not allow the function to be deferred. */
2086 current_function_cannot_inline
2087 = "static constructors and destructors cannot be inlined";
2089 for ( t = registered_class; t; t = TREE_CHAIN (t))
2090 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2091 XEXP (DECL_RTL (t), 0), Pmode);
2093 expand_function_end (input_filename, 0, 0);
2095 rest_of_compilation (init_decl);
2096 current_function_decl = NULL_TREE;
2098 if (targetm.have_ctors_dtors)
2099 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2100 DEFAULT_INIT_PRIORITY);
2105 init_class_processing ()
2107 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
2108 registerResource_libfunc =
2109 gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterResource");
2110 ggc_add_tree_root (class_roots, sizeof (class_roots) / sizeof (tree));
2111 fields_ident = get_identifier ("fields");
2112 info_ident = get_identifier ("info");
2113 ggc_add_rtx_root (®isterClass_libfunc, 1);
2114 gcc_obstack_init (&temporary_obstack);