1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "coretypes.h"
35 #include "java-tree.h"
46 #include "tree-iterator.h"
48 /* DOS brain-damage */
50 #define O_BINARY 0 /* MS-DOS brain-damage */
53 static tree make_method_value (tree);
54 static tree build_java_method_type (tree, tree, int);
55 static int32 hashUtf8String (const char *, int);
56 static tree make_field_value (tree);
57 static tree get_dispatch_vector (tree);
58 static tree get_dispatch_table (tree, tree);
59 static int supers_all_compiled (tree type);
60 static void add_interface_do (tree, tree, int);
61 static tree maybe_layout_super_class (tree, tree);
62 static void add_miranda_methods (tree, tree);
63 static int assume_compiled (const char *);
64 static tree build_symbol_entry (tree);
66 struct obstack temporary_obstack;
68 /* The compiler generates different code depending on whether or not
69 it can assume certain classes have been compiled down to native
70 code or not. The compiler options -fassume-compiled= and
71 -fno-assume-compiled= are used to create a tree of
72 class_flag_node objects. This tree is queried to determine if
73 a class is assume to be compiled or not. Each node in the tree
74 represents either a package or a specific class. */
76 typedef struct class_flag_node_struct
78 /* The class or package name. */
81 /* Nonzero if this represents an exclusion. */
84 /* Pointers to other nodes in the tree. */
85 struct class_flag_node_struct *parent;
86 struct class_flag_node_struct *sibling;
87 struct class_flag_node_struct *child;
90 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
91 static void add_class_flag (class_flag_node **, const char *, int);
93 /* This is the root of the include/exclude tree. */
95 static class_flag_node *assume_compiled_tree;
97 static class_flag_node *enable_assert_tree;
99 static GTY(()) tree class_roots[5];
100 #define registered_class class_roots[0]
101 #define fields_ident class_roots[1] /* get_identifier ("fields") */
102 #define info_ident class_roots[2] /* get_identifier ("info") */
103 #define class_list class_roots[3]
104 #define class_dtable_decl class_roots[4]
106 /* Return the node that most closely represents the class whose name
107 is IDENT. Start the search from NODE (followed by its siblings).
108 Return NULL if an appropriate node does not exist. */
110 static class_flag_node *
111 find_class_flag_node (class_flag_node *node, const char *ident)
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 && (ident[node_ident_length] == '\0'
125 || ident[node_ident_length] == '.')))
127 /* We've found a match, however, there might be a more
130 class_flag_node *found = find_class_flag_node (node->child, ident);
137 /* No match yet. Continue through the sibling list. */
138 node = node->sibling;
141 /* No match at all in this tree. */
146 add_class_flag (class_flag_node **rootp, const char *ident, int value)
148 class_flag_node *root = *rootp;
149 class_flag_node *parent, *node;
151 /* Create the root of the tree if it doesn't exist yet. */
155 root = xmalloc (sizeof (class_flag_node));
158 root->sibling = NULL;
164 /* Calling the function with the empty string means we're setting
165 value for the root of the hierarchy. */
173 /* Find the parent node for this new node. PARENT will either be a
174 class or a package name. Adjust PARENT accordingly. */
176 parent = find_class_flag_node (root, ident);
177 if (strcmp (ident, parent->ident) == 0)
178 parent->value = value;
181 /* Insert new node into the tree. */
182 node = xmalloc (sizeof (class_flag_node));
184 node->ident = xstrdup (ident);
188 node->parent = parent;
189 node->sibling = parent->child;
190 parent->child = node;
194 /* Add a new IDENT to the include/exclude tree. It's an exclusion
195 if EXCLUDEP is nonzero. */
198 add_assume_compiled (const char *ident, int excludep)
200 add_class_flag (&assume_compiled_tree, ident, excludep);
203 /* The default value returned by enable_assertions. */
205 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
207 /* Enter IDENT (a class or package name) into the enable-assertions table.
208 VALUE is true to enable and false to disable. */
211 add_enable_assert (const char *ident, int value)
213 if (enable_assert_tree == NULL)
214 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
215 add_class_flag (&enable_assert_tree, ident, value);
218 /* Returns nonzero if IDENT is the name of a class that the compiler
219 should assume has been compiled to object code. */
222 assume_compiled (const char *ident)
227 if (NULL == assume_compiled_tree)
230 i = find_class_flag_node (assume_compiled_tree, ident);
237 /* Return true if we should generate code to check assertions within KLASS. */
240 enable_assertions (tree klass)
242 /* Check if command-line specifies whether we should check assertions. */
244 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
246 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
247 class_flag_node *node
248 = find_class_flag_node (enable_assert_tree, ident);
252 /* The default is to enable assertions if generating class files,
253 or not optimizing. */
254 return DEFAULT_ENABLE_ASSERT;
257 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
258 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
259 Also, PREFIX is prepended, and SUFFIX is appended. */
262 ident_subst (const char* old_name,
269 int prefix_len = strlen (prefix);
270 int suffix_len = strlen (suffix);
271 int i = prefix_len + old_length + suffix_len + 1;
275 char *buffer = alloca (i);
277 strcpy (buffer, prefix);
278 for (i = 0; i < old_length; i++)
280 char ch = old_name[i];
283 buffer[prefix_len + i] = ch;
285 strcpy (buffer + prefix_len + old_length, suffix);
286 return get_identifier (buffer);
289 /* Return an IDENTIFIER_NODE the same as OLD_ID,
290 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
291 Also, PREFIX is prepended, and SUFFIX is appended. */
294 identifier_subst (const tree old_id,
300 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
301 prefix, old_char, new_char, suffix);
304 /* Generate a valid C identifier from the name of the class TYPE,
305 prefixed by PREFIX. */
308 mangled_classname (const char *prefix, tree type)
310 tree ident = TYPE_NAME (type);
311 if (TREE_CODE (ident) != IDENTIFIER_NODE)
312 ident = DECL_NAME (ident);
313 return identifier_subst (ident, prefix, '.', '_', "");
320 type = make_node (RECORD_TYPE);
321 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
326 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
327 and where each of the constituents is separated by '/',
328 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
331 unmangle_classname (const char *name, int name_length)
333 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
334 /* It's not sufficient to compare to_return and get_identifier
335 (name) to determine whether to_return is qualified. There are
336 cases in signature analysis where name will be stripped of a
338 name = IDENTIFIER_POINTER (to_return);
342 QUALIFIED_P (to_return) = 1;
350 /* Given a class, create the DECLs for all its associated indirect
353 gen_indirect_dispatch_tables (tree type)
355 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
358 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
359 tree catch_class_type = make_node (RECORD_TYPE);
361 sprintf (buf, "_catch_classes_%s", typename);
362 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
363 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
364 FINISH_RECORD (catch_class_type);
366 TYPE_CTABLE_DECL (type)
367 = build_decl (VAR_DECL, get_identifier (buf),
368 build_array_type (catch_class_type, 0));
369 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
370 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
371 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
372 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
373 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
374 pushdecl (TYPE_CTABLE_DECL (type));
377 if (flag_indirect_dispatch)
380 char *buf = alloca (strlen (typename) + strlen ("_otable_syms_") + 1);
382 sprintf (buf, "_otable_%s", typename);
383 TYPE_OTABLE_DECL (type) =
384 build_decl (VAR_DECL, get_identifier (buf), otable_type);
385 DECL_EXTERNAL (TYPE_OTABLE_DECL (type)) = 1;
386 TREE_STATIC (TYPE_OTABLE_DECL (type)) = 1;
387 TREE_READONLY (TYPE_OTABLE_DECL (type)) = 1;
388 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
389 DECL_IGNORED_P (TYPE_OTABLE_DECL (type)) = 1;
390 pushdecl (TYPE_OTABLE_DECL (type));
391 sprintf (buf, "_otable_syms_%s", typename);
392 TYPE_OTABLE_SYMS_DECL (type) =
393 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type);
394 TREE_STATIC (TYPE_OTABLE_SYMS_DECL (type)) = 1;
395 TREE_CONSTANT (TYPE_OTABLE_SYMS_DECL (type)) = 1;
396 DECL_IGNORED_P(TYPE_OTABLE_SYMS_DECL (type)) = 1;
397 pushdecl (TYPE_OTABLE_SYMS_DECL (type));
401 char *buf = alloca (strlen (typename) + strlen ("_atable_syms_") + 1);
404 sprintf (buf, "_atable_%s", typename);
405 TYPE_ATABLE_DECL (type) = decl =
406 build_decl (VAR_DECL, get_identifier (buf), atable_type);
407 DECL_EXTERNAL (decl) = 1;
408 TREE_STATIC (decl) = 1;
409 TREE_READONLY (decl) = 1;
410 TREE_CONSTANT (decl) = 1;
411 DECL_IGNORED_P (decl) = 1;
412 /* Mark the atable as belonging to this class. */
414 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
415 DECL_OWNER (decl) = type;
416 sprintf (buf, "_atable_syms_%s", typename);
417 TYPE_ATABLE_SYMS_DECL (type) =
418 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type);
419 TREE_STATIC (TYPE_ATABLE_SYMS_DECL (type)) = 1;
420 TREE_CONSTANT (TYPE_ATABLE_SYMS_DECL (type)) = 1;
421 DECL_IGNORED_P (TYPE_ATABLE_SYMS_DECL (type)) = 1;
422 pushdecl (TYPE_ATABLE_SYMS_DECL (type));
428 push_class (tree class_type, tree class_name)
430 tree decl, signature;
431 location_t saved_loc = input_location;
432 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
433 CLASS_P (class_type) = 1;
434 input_filename = IDENTIFIER_POINTER (source_name);
436 decl = build_decl (TYPE_DECL, class_name, class_type);
438 /* dbxout needs a DECL_SIZE if in gstabs mode */
439 DECL_SIZE (decl) = integer_zero_node;
441 input_location = saved_loc;
442 signature = identifier_subst (class_name, "L", '.', '/', ";");
443 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
445 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
446 both a typedef and in the struct name-space. We may want to re-visit
447 this later, but for now it reduces the changes needed for gdb. */
448 DECL_ARTIFICIAL (decl) = 1;
450 pushdecl_top_level (decl);
455 /* Finds the (global) class named NAME. Creates the class if not found.
456 Also creates associated TYPE_DECL.
457 Does not check if the class actually exists, load the class,
458 fill in field or methods, or do layout_type. */
461 lookup_class (tree name)
463 tree decl = IDENTIFIER_CLASS_VALUE (name);
464 if (decl == NULL_TREE)
465 decl = push_class (make_class (), name);
466 return TREE_TYPE (decl);
470 set_super_info (int access_flags, tree this_class,
471 tree super_class, int interfaces_count)
473 int total_supers = interfaces_count;
474 tree class_decl = TYPE_NAME (this_class);
479 TYPE_BINFO (this_class) = make_tree_binfo (0);
480 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
481 BINFO_BASE_BINFOS (TYPE_BINFO (this_class)) = make_tree_vec (total_supers);
484 tree super_binfo = make_tree_binfo (0);
485 BINFO_TYPE (super_binfo) = super_class;
486 BINFO_OFFSET (super_binfo) = integer_zero_node;
487 TREE_VEC_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (this_class)), 0)
489 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
492 set_class_decl_access_flags (access_flags, class_decl);
496 set_class_decl_access_flags (int access_flags, tree class_decl)
498 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
499 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
500 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
501 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
502 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
503 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
504 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
505 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
506 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
509 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
510 direct sub-classes of Object are 1, and so on. */
513 class_depth (tree clas)
516 if (! CLASS_LOADED_P (clas))
517 load_class (clas, 1);
518 if (TYPE_SIZE (clas) == error_mark_node)
520 while (clas != object_type_node)
523 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
528 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
531 interface_of_p (tree type1, tree type2)
536 if (! TYPE_BINFO (type2))
538 basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (type2));
539 n = TREE_VEC_LENGTH (basetype_vec);
540 for (i = 0; i < n; i++)
542 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
543 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
546 for (i = 0; i < n; i++)
548 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
549 if (vec_elt && BINFO_TYPE (vec_elt)
550 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
556 /* Return true iff TYPE1 inherits from TYPE2. */
559 inherits_from_p (tree type1, tree type2)
561 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
565 type1 = CLASSTYPE_SUPER (type1);
570 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
573 enclosing_context_p (tree type1, tree type2)
575 if (!INNER_CLASS_TYPE_P (type2))
578 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
580 type2 = (INNER_CLASS_TYPE_P (type2) ?
581 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
591 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
595 common_enclosing_context_p (tree type1, tree type2)
600 for (current = type2; current;
601 current = (INNER_CLASS_TYPE_P (current) ?
602 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
604 if (type1 == current)
607 if (INNER_CLASS_TYPE_P (type1))
608 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
615 /* Return 1 iff there exists a common enclosing "this" between TYPE1
616 and TYPE2, without crossing any static context. */
619 common_enclosing_instance_p (tree type1, tree type2)
621 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
624 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
625 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
626 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
629 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
630 current = (PURE_INNER_CLASS_TYPE_P (current) ?
631 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
633 if (type1 == current)
640 add_interface_do (tree basetype_vec, tree interface_class, int i)
642 tree interface_binfo = make_tree_binfo (0);
643 BINFO_TYPE (interface_binfo) = interface_class;
644 BINFO_OFFSET (interface_binfo) = integer_zero_node;
645 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
646 BINFO_VIRTUAL_P (interface_binfo) = 1;
647 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
650 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
651 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
652 if attempt is made to add it twice. */
655 maybe_add_interface (tree this_class, tree interface_class)
657 tree basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (this_class));
659 int n = TREE_VEC_LENGTH (basetype_vec);
664 error ("internal error - too many interface type");
667 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
669 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
670 return interface_class;
672 add_interface_do (basetype_vec, interface_class, i);
676 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
679 add_interface (tree this_class, tree interface_class)
681 tree basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (this_class));
683 int n = TREE_VEC_LENGTH (basetype_vec);
688 error ("internal error - too many interface type");
691 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
694 add_interface_do (basetype_vec, interface_class, i);
698 /* Return the address of a pointer to the first FUNCTION_DECL
699 in the list (*LIST) whose DECL_NAME is NAME. */
702 find_named_method (tree *list, tree name)
704 while (*list && DECL_NAME (*list) != name)
705 list = &TREE_CHAIN (*list);
711 build_java_method_type (tree fntype, tree this_class, int access_flags)
713 if (access_flags & ACC_STATIC)
715 return build_method_type (this_class, fntype);
719 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
721 tree method_type, fndecl;
723 method_type = build_java_method_type (function_type,
724 this_class, access_flags);
726 fndecl = build_decl (FUNCTION_DECL, name, method_type);
727 DECL_CONTEXT (fndecl) = this_class;
729 DECL_LANG_SPECIFIC (fndecl)
730 = ggc_alloc_cleared (sizeof (struct lang_decl));
731 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
733 /* Initialize the static initializer test table. */
735 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
736 java_treetreehash_create (10, 1);
738 /* Initialize the initialized (static) class table. */
739 if (access_flags & ACC_STATIC)
740 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
741 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
743 /* Initialize the static method invocation compound list */
744 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
746 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
747 TYPE_METHODS (this_class) = fndecl;
749 /* Notice that this is a finalizer and update the class type
750 accordingly. This is used to optimize instance allocation. */
751 if (name == finalize_identifier_node
752 && TREE_TYPE (function_type) == void_type_node
753 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
754 HAS_FINALIZER_P (this_class) = 1;
756 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
757 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
758 if (access_flags & ACC_PRIVATE)
759 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
760 if (access_flags & ACC_NATIVE)
762 METHOD_NATIVE (fndecl) = 1;
763 DECL_EXTERNAL (fndecl) = 1;
765 if (access_flags & ACC_STATIC)
766 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
767 if (access_flags & ACC_FINAL)
768 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
769 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
770 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
771 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
775 /* Add a method to THIS_CLASS.
776 The method's name is NAME.
777 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
780 add_method (tree this_class, int access_flags, tree name, tree method_sig)
782 tree function_type, fndecl;
783 const unsigned char *sig
784 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
787 fatal_error ("bad method signature");
789 function_type = get_type_from_signature (method_sig);
790 fndecl = add_method_1 (this_class, access_flags, name, function_type);
791 set_java_signature (TREE_TYPE (fndecl), method_sig);
796 add_field (tree class, tree name, tree field_type, int flags)
798 int is_static = (flags & ACC_STATIC) != 0;
800 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
801 TREE_CHAIN (field) = TYPE_FIELDS (class);
802 TYPE_FIELDS (class) = field;
803 DECL_CONTEXT (field) = class;
805 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
806 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
807 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
808 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
809 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
810 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
813 FIELD_STATIC (field) = 1;
814 /* Always make field externally visible. This is required so
815 that native methods can always access the field. */
816 TREE_PUBLIC (field) = 1;
817 /* Considered external until we know what classes are being
818 compiled into this object file. */
819 DECL_EXTERNAL (field) = 1;
825 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
828 set_constant_value (tree field, tree constant)
830 if (field == NULL_TREE)
831 warning ("misplaced ConstantValue attribute (not in any field)");
832 else if (DECL_INITIAL (field) != NULL_TREE)
833 warning ("duplicate ConstantValue attribute for field '%s'",
834 IDENTIFIER_POINTER (DECL_NAME (field)));
837 DECL_INITIAL (field) = constant;
838 if (TREE_TYPE (constant) != TREE_TYPE (field)
839 && ! (TREE_TYPE (constant) == int_type_node
840 && INTEGRAL_TYPE_P (TREE_TYPE (field))
841 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
842 && ! (TREE_TYPE (constant) == utf8const_ptr_type
843 && TREE_TYPE (field) == string_ptr_type_node))
844 error ("ConstantValue attribute of field '%s' has wrong type",
845 IDENTIFIER_POINTER (DECL_NAME (field)));
846 if (FIELD_FINAL (field))
847 DECL_FIELD_FINAL_IUD (field) = 1;
851 /* Count the number of Unicode chars encoded in a given Ut8 string. */
855 strLengthUtf8 (char *str, int len)
857 register unsigned char* ptr = (unsigned char*) str;
858 register unsigned char *limit = ptr + len;
860 for (; ptr < limit; str_length++) {
861 if (UTF8_GET (ptr, limit) < 0)
869 /* Calculate a hash value for a string encoded in Utf8 format.
870 * This returns the same hash value as specified for java.lang.String.hashCode.
874 hashUtf8String (const char *str, int len)
876 const unsigned char* ptr = (const unsigned char*) str;
877 const unsigned char *limit = ptr + len;
881 int ch = UTF8_GET (ptr, limit);
882 /* Updated specification from
883 http://www.javasoft.com/docs/books/jls/clarify.html. */
884 hash = (31 * hash) + ch;
889 static GTY(()) tree utf8_decl_list = NULL_TREE;
892 build_utf8_ref (tree name)
894 const char * name_ptr = IDENTIFIER_POINTER(name);
895 int name_len = IDENTIFIER_LENGTH(name);
897 tree ctype, field = NULL_TREE, str_type, cinit, string;
898 static int utf8_count = 0;
900 tree ref = IDENTIFIER_UTF8_REF (name);
902 if (ref != NULL_TREE)
905 ctype = make_node (RECORD_TYPE);
906 str_type = build_prim_array_type (unsigned_byte_type_node,
907 name_len + 1); /* Allow for final '\0'. */
908 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
909 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
910 PUSH_FIELD (ctype, field, "data", str_type);
911 FINISH_RECORD (ctype);
912 START_RECORD_CONSTRUCTOR (cinit, ctype);
913 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
914 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
915 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
916 string = build_string (name_len, name_ptr);
917 TREE_TYPE (string) = str_type;
918 PUSH_FIELD_VALUE (cinit, "data", string);
919 FINISH_RECORD_CONSTRUCTOR (cinit);
920 TREE_CONSTANT (cinit) = 1;
921 TREE_INVARIANT (cinit) = 1;
923 /* Generate a unique-enough identifier. */
924 sprintf(buf, "_Utf%d", ++utf8_count);
926 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
927 TREE_STATIC (decl) = 1;
928 DECL_ARTIFICIAL (decl) = 1;
929 DECL_IGNORED_P (decl) = 1;
930 TREE_READONLY (decl) = 1;
931 TREE_THIS_VOLATILE (decl) = 0;
932 DECL_INITIAL (decl) = cinit;
934 if (HAVE_GAS_SHF_MERGE)
937 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
938 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
939 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
940 if (flag_merge_constants && decl_size < 256)
943 int flags = (SECTION_OVERRIDE
944 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
945 sprintf (buf, ".rodata.jutf8.%d", decl_size);
946 named_section_flags (buf, flags);
947 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
951 TREE_CHAIN (decl) = utf8_decl_list;
952 layout_decl (decl, 0);
954 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
955 utf8_decl_list = decl;
956 make_decl_rtl (decl, (char*) 0);
957 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
958 IDENTIFIER_UTF8_REF (name) = ref;
962 /* Like build_class_ref, but instead of a direct reference generate a
963 pointer into the constant pool. */
966 build_indirect_class_ref (tree type)
970 index = alloc_class_constant (type);
971 cl = build_ref_from_constant_pool (index);
972 return convert (promote_type (class_ptr_type), cl);
975 /* Build a reference to the class TYPE.
976 Also handles primitive types and array types. */
979 build_class_ref (tree type)
981 int is_compiled = is_compiled_class (type);
984 tree ref, decl_name, decl;
985 if (TREE_CODE (type) == POINTER_TYPE)
986 type = TREE_TYPE (type);
988 /* FIXME: we really want an indirect reference to our
989 superclass. However, libgcj assumes that a superclass
990 pointer always points directly to a class. As a workaround
991 we always emit this hard superclass reference. */
992 if (flag_indirect_dispatch
993 && type != output_class
994 && type != CLASSTYPE_SUPER (output_class)
995 && TREE_CODE (type) == RECORD_TYPE)
996 return build_indirect_class_ref (type);
998 if (TREE_CODE (type) == RECORD_TYPE)
1000 if (TYPE_SIZE (type) == error_mark_node)
1001 return null_pointer_node;
1002 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1003 "", '/', '/', ".class");
1004 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1005 if (decl == NULL_TREE)
1007 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1008 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
1009 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
1010 TREE_STATIC (decl) = 1;
1011 TREE_PUBLIC (decl) = 1;
1012 DECL_IGNORED_P (decl) = 1;
1013 DECL_ARTIFICIAL (decl) = 1;
1014 if (is_compiled == 1)
1015 DECL_EXTERNAL (decl) = 1;
1016 SET_DECL_ASSEMBLER_NAME (decl,
1017 java_mangle_class_field
1018 (&temporary_obstack, type));
1019 make_decl_rtl (decl, NULL);
1020 pushdecl_top_level (decl);
1027 if (flag_emit_class_files)
1029 const char *prim_class_name;
1031 if (type == char_type_node)
1032 prim_class_name = "java.lang.Character";
1033 else if (type == boolean_type_node)
1034 prim_class_name = "java.lang.Boolean";
1035 else if (type == byte_type_node)
1036 prim_class_name = "java.lang.Byte";
1037 else if (type == short_type_node)
1038 prim_class_name = "java.lang.Short";
1039 else if (type == int_type_node)
1040 prim_class_name = "java.lang.Integer";
1041 else if (type == long_type_node)
1042 prim_class_name = "java.lang.Long";
1043 else if (type == float_type_node)
1044 prim_class_name = "java.lang.Float";
1045 else if (type == double_type_node)
1046 prim_class_name = "java.lang.Double";
1047 else if (type == void_type_node)
1048 prim_class_name = "java.lang.Void";
1052 prim_class = lookup_class (get_identifier (prim_class_name));
1053 return build3 (COMPONENT_REF, NULL_TREE,
1054 prim_class, TYPE_identifier_node, NULL_TREE);
1056 decl_name = TYPE_NAME (type);
1057 if (TREE_CODE (decl_name) == TYPE_DECL)
1058 decl_name = DECL_NAME (decl_name);
1059 name = IDENTIFIER_POINTER (decl_name);
1060 if (strncmp (name, "promoted_", 9) == 0)
1062 sprintf (buffer, "_Jv_%sClass", name);
1063 decl_name = get_identifier (buffer);
1064 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1065 if (decl == NULL_TREE)
1067 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1068 TREE_STATIC (decl) = 1;
1069 TREE_PUBLIC (decl) = 1;
1070 DECL_EXTERNAL (decl) = 1;
1071 make_decl_rtl (decl, NULL);
1072 pushdecl_top_level (decl);
1076 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1080 return build_indirect_class_ref (type);
1084 build_static_field_ref (tree fdecl)
1086 tree fclass = DECL_CONTEXT (fdecl);
1087 int is_compiled = is_compiled_class (fclass);
1089 /* Allow static final fields to fold to a constant. When using
1090 -fno-assume-compiled, gcj will sometimes try to fold a field from
1091 an uncompiled class. This is required when the field in question
1092 meets the appropriate criteria for a compile-time constant.
1093 However, currently sometimes gcj is too eager and will end up
1094 returning the field itself, leading to an incorrect external
1095 reference being generated. */
1097 && (! flag_indirect_dispatch || current_class == fclass))
1098 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1099 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1100 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1101 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1103 if (!DECL_RTL_SET_P (fdecl))
1105 if (is_compiled == 1)
1106 DECL_EXTERNAL (fdecl) = 1;
1107 make_decl_rtl (fdecl, NULL);
1112 if (flag_indirect_dispatch)
1115 = build_int_2 (get_symbol_table_index
1116 (fdecl, &TYPE_ATABLE_METHODS (output_class)), 0);
1118 = build4 (ARRAY_REF, build_pointer_type (TREE_TYPE (fdecl)),
1119 TYPE_ATABLE_DECL (output_class), table_index,
1120 NULL_TREE, NULL_TREE);
1121 return fold (build1 (INDIRECT_REF, TREE_TYPE (fdecl),
1127 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1128 tree ref = build_class_ref (fclass);
1130 int field_index = 0;
1131 ref = build1 (INDIRECT_REF, class_type_node, ref);
1132 ref = build3 (COMPONENT_REF, field_ptr_type_node, ref,
1133 lookup_field (&class_type_node, fields_ident),
1136 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1140 if (fld == NULL_TREE)
1141 fatal_error ("field '%s' not found in class",
1142 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1143 if (FIELD_STATIC (fld))
1146 field_index *= int_size_in_bytes (field_type_node);
1147 ref = fold (build2 (PLUS_EXPR, field_ptr_type_node,
1148 ref, build_int_2 (field_index, 0)));
1149 ref = build1 (INDIRECT_REF, field_type_node, ref);
1150 ref = build3 (COMPONENT_REF, field_info_union_node,
1151 ref, lookup_field (&field_type_node, info_ident),
1153 ref = build3 (COMPONENT_REF, ptr_type_node,
1154 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)),
1156 ref = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (fdecl)), ref);
1157 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1162 get_access_flags_from_decl (tree decl)
1164 int access_flags = 0;
1165 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1167 if (FIELD_STATIC (decl))
1168 access_flags |= ACC_STATIC;
1169 if (FIELD_PUBLIC (decl))
1170 access_flags |= ACC_PUBLIC;
1171 if (FIELD_PROTECTED (decl))
1172 access_flags |= ACC_PROTECTED;
1173 if (FIELD_PRIVATE (decl))
1174 access_flags |= ACC_PRIVATE;
1175 if (FIELD_FINAL (decl))
1176 access_flags |= ACC_FINAL;
1177 if (FIELD_VOLATILE (decl))
1178 access_flags |= ACC_VOLATILE;
1179 if (FIELD_TRANSIENT (decl))
1180 access_flags |= ACC_TRANSIENT;
1181 return access_flags;
1183 if (TREE_CODE (decl) == TYPE_DECL)
1185 if (CLASS_PUBLIC (decl))
1186 access_flags |= ACC_PUBLIC;
1187 if (CLASS_FINAL (decl))
1188 access_flags |= ACC_FINAL;
1189 if (CLASS_SUPER (decl))
1190 access_flags |= ACC_SUPER;
1191 if (CLASS_INTERFACE (decl))
1192 access_flags |= ACC_INTERFACE;
1193 if (CLASS_ABSTRACT (decl))
1194 access_flags |= ACC_ABSTRACT;
1195 if (CLASS_STATIC (decl))
1196 access_flags |= ACC_STATIC;
1197 if (CLASS_PRIVATE (decl))
1198 access_flags |= ACC_PRIVATE;
1199 if (CLASS_PROTECTED (decl))
1200 access_flags |= ACC_PROTECTED;
1201 if (CLASS_STRICTFP (decl))
1202 access_flags |= ACC_STRICT;
1203 return access_flags;
1205 if (TREE_CODE (decl) == FUNCTION_DECL)
1207 if (METHOD_PUBLIC (decl))
1208 access_flags |= ACC_PUBLIC;
1209 if (METHOD_PRIVATE (decl))
1210 access_flags |= ACC_PRIVATE;
1211 if (METHOD_PROTECTED (decl))
1212 access_flags |= ACC_PROTECTED;
1213 if (METHOD_STATIC (decl))
1214 access_flags |= ACC_STATIC;
1215 if (METHOD_FINAL (decl))
1216 access_flags |= ACC_FINAL;
1217 if (METHOD_SYNCHRONIZED (decl))
1218 access_flags |= ACC_SYNCHRONIZED;
1219 if (METHOD_NATIVE (decl))
1220 access_flags |= ACC_NATIVE;
1221 if (METHOD_ABSTRACT (decl))
1222 access_flags |= ACC_ABSTRACT;
1223 if (METHOD_STRICTFP (decl))
1224 access_flags |= ACC_STRICT;
1225 if (METHOD_INVISIBLE (decl))
1226 access_flags |= ACC_INVISIBLE;
1227 return access_flags;
1233 make_field_value (tree fdecl)
1237 tree type = TREE_TYPE (fdecl);
1238 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1240 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1241 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1243 type = build_class_ref (type);
1246 tree signature = build_java_signature (type);
1248 type = build_utf8_ref (unmangle_classname
1249 (IDENTIFIER_POINTER (signature),
1250 IDENTIFIER_LENGTH (signature)));
1252 PUSH_FIELD_VALUE (finit, "type", type);
1254 flags = get_access_flags_from_decl (fdecl);
1256 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1258 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1259 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1263 build_constructor (field_info_union_node,
1265 ((FIELD_STATIC (fdecl)
1266 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1267 : TYPE_FIELDS (field_info_union_node)),
1268 (FIELD_STATIC (fdecl)
1269 ? build_address_of (build_static_field_ref (fdecl))
1270 : byte_position (fdecl)))));
1272 FINISH_RECORD_CONSTRUCTOR (finit);
1277 make_method_value (tree mdecl)
1279 static int method_name_count = 0;
1284 #define ACC_TRANSLATED 0x4000
1285 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1287 class_decl = DECL_CONTEXT (mdecl);
1288 /* For interfaces, the index field contains the dispatch index. */
1289 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1290 index = build_int_2 (get_interface_method_index (mdecl, class_decl), 0);
1291 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1292 index = get_method_index (mdecl);
1294 index = integer_minus_one_node;
1296 code = null_pointer_node;
1297 if (DECL_RTL_SET_P (mdecl))
1298 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1299 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1300 PUSH_FIELD_VALUE (minit, "name",
1301 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1302 init_identifier_node
1303 : DECL_NAME (mdecl)));
1305 tree signature = build_java_signature (TREE_TYPE (mdecl));
1306 PUSH_FIELD_VALUE (minit, "signature",
1309 (IDENTIFIER_POINTER(signature),
1310 IDENTIFIER_LENGTH(signature)))));
1312 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1313 PUSH_FIELD_VALUE (minit, "index", index);
1314 PUSH_FIELD_VALUE (minit, "ncode", code);
1317 /* Compute the `throws' information for the method. */
1318 tree table = null_pointer_node;
1319 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1321 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1322 tree iter, type, array;
1325 table = tree_cons (NULL_TREE, table, NULL_TREE);
1326 for (iter = DECL_FUNCTION_THROWS (mdecl);
1328 iter = TREE_CHAIN (iter))
1330 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1332 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1333 IDENTIFIER_LENGTH (sig)));
1334 table = tree_cons (NULL_TREE, utf8, table);
1336 type = build_prim_array_type (ptr_type_node, length);
1337 table = build_constructor (type, table);
1338 /* Compute something unique enough. */
1339 sprintf (buf, "_methods%d", method_name_count++);
1340 array = build_decl (VAR_DECL, get_identifier (buf), type);
1341 DECL_INITIAL (array) = table;
1342 TREE_STATIC (array) = 1;
1343 DECL_ARTIFICIAL (array) = 1;
1344 DECL_IGNORED_P (array) = 1;
1345 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1347 table = build1 (ADDR_EXPR, ptr_type_node, array);
1350 PUSH_FIELD_VALUE (minit, "throws", table);
1353 FINISH_RECORD_CONSTRUCTOR (minit);
1358 get_dispatch_vector (tree type)
1360 tree vtable = TYPE_VTABLE (type);
1362 if (vtable == NULL_TREE)
1366 tree super = CLASSTYPE_SUPER (type);
1367 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1368 vtable = make_tree_vec (nvirtuals);
1369 TYPE_VTABLE (type) = vtable;
1370 if (super != NULL_TREE)
1372 tree super_vtable = get_dispatch_vector (super);
1374 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1375 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1378 for (method = TYPE_METHODS (type); method != NULL_TREE;
1379 method = TREE_CHAIN (method))
1381 tree method_index = get_method_index (method);
1382 if (method_index != NULL_TREE
1383 && host_integerp (method_index, 0))
1384 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1392 get_dispatch_table (tree type, tree this_class_addr)
1394 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1395 tree vtable = get_dispatch_vector (type);
1397 tree list = NULL_TREE;
1398 int nvirtuals = TREE_VEC_LENGTH (vtable);
1402 for (i = nvirtuals; --i >= 0; )
1404 tree method = TREE_VEC_ELT (vtable, i);
1405 if (METHOD_ABSTRACT (method))
1408 warning ("%Jabstract method in non-abstract class", method);
1410 if (TARGET_VTABLE_USES_DESCRIPTORS)
1411 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1412 list = tree_cons (NULL_TREE, null_pointer_node, list);
1414 list = tree_cons (NULL_TREE, null_pointer_node, list);
1418 if (!DECL_RTL_SET_P (method))
1419 make_decl_rtl (method, NULL);
1421 if (TARGET_VTABLE_USES_DESCRIPTORS)
1422 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1424 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1425 method, build_int_2 (j, 0));
1426 TREE_CONSTANT (fdesc) = 1;
1427 TREE_INVARIANT (fdesc) = 1;
1428 list = tree_cons (NULL_TREE, fdesc, list);
1431 list = tree_cons (NULL_TREE,
1432 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1438 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1439 using the Boehm GC we sometimes stash a GC type descriptor
1440 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1441 the emitted byte count during the output to the assembly file. */
1442 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1443 fake "function descriptor". It's first word is the is the class
1444 pointer, and subsequent words (usually one) contain the GC descriptor.
1445 In all other cases, we reserve two extra vtable slots. */
1446 gc_descr = get_boehm_type_descriptor (type);
1447 list = tree_cons (NULL_TREE, gc_descr, list);
1448 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1449 list = tree_cons (NULL_TREE, gc_descr, list);
1450 list = tree_cons (NULL_TREE, this_class_addr, list);
1452 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1453 list = tree_cons (NULL_TREE, null_pointer_node, list);
1454 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1455 list = tree_cons (integer_zero_node, null_pointer_node, list);
1457 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1458 if (TARGET_VTABLE_USES_DESCRIPTORS)
1459 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1461 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1466 /* Set the method_index for a method decl. */
1468 set_method_index (tree decl, tree method_index)
1470 method_index = fold (convert (sizetype, method_index));
1472 if (TARGET_VTABLE_USES_DESCRIPTORS)
1473 /* Add one to skip bogus descriptor for class and GC descriptor. */
1474 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1476 /* Add 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1477 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1479 DECL_VINDEX (decl) = method_index;
1482 /* Get the method_index for a method decl. */
1484 get_method_index (tree decl)
1486 tree method_index = DECL_VINDEX (decl);
1491 if (TARGET_VTABLE_USES_DESCRIPTORS)
1492 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1493 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1495 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1496 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1498 return method_index;
1502 supers_all_compiled (tree type)
1504 while (type != NULL_TREE)
1506 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1508 type = CLASSTYPE_SUPER (type);
1514 make_class_data (tree type)
1516 tree decl, cons, temp;
1517 tree field, fields_decl;
1518 tree static_fields = NULL_TREE;
1519 tree instance_fields = NULL_TREE;
1520 HOST_WIDE_INT static_field_count = 0;
1521 HOST_WIDE_INT instance_field_count = 0;
1522 HOST_WIDE_INT field_count;
1523 tree field_array_type;
1525 tree methods = NULL_TREE;
1526 tree dtable_decl = NULL_TREE;
1527 HOST_WIDE_INT method_count = 0;
1528 tree method_array_type;
1531 tree this_class_addr;
1532 tree constant_pool_constructor;
1533 tree interfaces = null_pointer_node;
1534 int interface_len = 0;
1535 tree type_decl = TYPE_NAME (type);
1536 /** Offset from start of virtual function table declaration
1537 to where objects actually point at, following new g++ ABI. */
1538 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1540 this_class_addr = build_class_ref (type);
1541 decl = TREE_OPERAND (this_class_addr, 0);
1543 /* Build Field array. */
1544 field = TYPE_FIELDS (type);
1545 if (DECL_NAME (field) == NULL_TREE)
1546 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1547 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1549 if (! DECL_ARTIFICIAL (field))
1551 tree init = make_field_value (field);
1552 if (FIELD_STATIC (field))
1554 tree initial = DECL_INITIAL (field);
1555 static_field_count++;
1556 static_fields = tree_cons (NULL_TREE, init, static_fields);
1557 /* If the initial value is a string constant,
1558 prevent output_constant from trying to assemble the value. */
1559 if (initial != NULL_TREE
1560 && TREE_TYPE (initial) == string_ptr_type_node)
1561 DECL_INITIAL (field) = NULL_TREE;
1562 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1563 DECL_INITIAL (field) = initial;
1567 instance_field_count++;
1568 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1572 field_count = static_field_count + instance_field_count;
1573 if (field_count > 0)
1575 static_fields = nreverse (static_fields);
1576 instance_fields = nreverse (instance_fields);
1577 static_fields = chainon (static_fields, instance_fields);
1578 field_array_type = build_prim_array_type (field_type_node, field_count);
1579 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1581 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1583 TREE_STATIC (fields_decl) = 1;
1584 DECL_ARTIFICIAL (fields_decl) = 1;
1585 DECL_IGNORED_P (fields_decl) = 1;
1586 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1589 fields_decl = NULL_TREE;
1591 /* Build Method array. */
1592 for (method = TYPE_METHODS (type);
1593 method != NULL_TREE; method = TREE_CHAIN (method))
1596 if (METHOD_PRIVATE (method)
1597 && ! flag_keep_inline_functions
1598 && (flag_inline_functions || optimize))
1600 init = make_method_value (method);
1602 methods = tree_cons (NULL_TREE, init, methods);
1604 method_array_type = build_prim_array_type (method_type_node, method_count);
1605 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1607 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1608 nreverse (methods));
1609 TREE_STATIC (methods_decl) = 1;
1610 DECL_ARTIFICIAL (methods_decl) = 1;
1611 DECL_IGNORED_P (methods_decl) = 1;
1612 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1614 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1615 && !flag_indirect_dispatch)
1617 tree dtable = get_dispatch_table (type, this_class_addr);
1618 dtable_decl = build_dtable_decl (type);
1619 DECL_INITIAL (dtable_decl) = dtable;
1620 TREE_STATIC (dtable_decl) = 1;
1621 DECL_ARTIFICIAL (dtable_decl) = 1;
1622 DECL_IGNORED_P (dtable_decl) = 1;
1623 TREE_PUBLIC (dtable_decl) = 1;
1624 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1625 if (type == class_type_node)
1626 class_dtable_decl = dtable_decl;
1629 if (class_dtable_decl == NULL_TREE)
1631 class_dtable_decl = build_dtable_decl (class_type_node);
1632 TREE_STATIC (class_dtable_decl) = 1;
1633 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1634 DECL_IGNORED_P (class_dtable_decl) = 1;
1635 if (is_compiled_class (class_type_node) != 2)
1636 DECL_EXTERNAL (class_dtable_decl) = 1;
1637 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1640 super = CLASSTYPE_SUPER (type);
1641 if (super == NULL_TREE)
1642 super = null_pointer_node;
1643 else if (/* FIXME: we should also test for (!
1644 flag_indirect_dispatch) here, but libgcj can't cope with
1645 a symbolic reference a superclass in the class data. */
1646 assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1647 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1648 super = build_class_ref (super);
1651 int super_index = alloc_class_constant (super);
1652 super = build_int_2 (super_index, 0);
1653 TREE_TYPE (super) = ptr_type_node;
1656 /* Build and emit the array of implemented interfaces. */
1657 if (type != object_type_node)
1658 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1660 if (interface_len > 0)
1662 tree init = NULL_TREE;
1664 tree interface_array_type, idecl;
1665 interface_array_type
1666 = build_prim_array_type (class_ptr_type, interface_len);
1667 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1668 interface_array_type);
1669 for (i = interface_len; i > 0; i--)
1671 tree child = TREE_VEC_ELT (BINFO_BASE_BINFOS (TYPE_BINFO (type)), i);
1672 tree iclass = BINFO_TYPE (child);
1674 if (! flag_indirect_dispatch
1676 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1677 index = build_class_ref (iclass);
1680 int int_index = alloc_class_constant (iclass);
1681 index = build_int_2 (int_index, 0);
1682 TREE_TYPE (index) = ptr_type_node;
1684 init = tree_cons (NULL_TREE, index, init);
1686 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1687 TREE_STATIC (idecl) = 1;
1688 DECL_ARTIFICIAL (idecl) = 1;
1689 DECL_IGNORED_P (idecl) = 1;
1690 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1691 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1694 constant_pool_constructor = build_constants_constructor ();
1696 if (flag_indirect_dispatch)
1698 TYPE_OTABLE_DECL (type)
1700 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1701 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1702 TYPE_OTABLE_SYMS_DECL (type), integer_type_node);
1704 TYPE_ATABLE_DECL (type)
1706 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1707 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1708 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node);
1711 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1713 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1714 PUSH_FIELD_VALUE (temp, "vtable",
1715 build2 (PLUS_EXPR, dtable_ptr_type,
1716 build1 (ADDR_EXPR, dtable_ptr_type,
1718 dtable_start_offset));
1719 if (! flag_hash_synchronization)
1720 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1721 FINISH_RECORD_CONSTRUCTOR (temp);
1722 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1723 PUSH_SUPER_VALUE (cons, temp);
1724 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1725 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1726 PUSH_FIELD_VALUE (cons, "accflags",
1727 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1729 PUSH_FIELD_VALUE (cons, "superclass",
1730 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1731 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1732 PUSH_FIELD_VALUE (cons, "methods",
1733 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1734 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1736 if (flag_indirect_dispatch)
1737 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1739 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1741 PUSH_FIELD_VALUE (cons, "fields",
1742 fields_decl == NULL_TREE ? null_pointer_node
1743 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1744 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1745 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1746 PUSH_FIELD_VALUE (cons, "static_field_count",
1747 build_int_2 (static_field_count, 0));
1749 if (flag_indirect_dispatch)
1750 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1752 PUSH_FIELD_VALUE (cons, "vtable",
1753 dtable_decl == NULL_TREE ? null_pointer_node
1754 : build2 (PLUS_EXPR, dtable_ptr_type,
1755 build1 (ADDR_EXPR, dtable_ptr_type,
1757 dtable_start_offset));
1758 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1760 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1761 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1765 PUSH_FIELD_VALUE (cons, "otable",
1766 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1767 PUSH_FIELD_VALUE (cons, "otable_syms",
1768 build1 (ADDR_EXPR, symbols_array_ptr_type,
1769 TYPE_OTABLE_SYMS_DECL (type)));
1770 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1771 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1773 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1775 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1776 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1780 PUSH_FIELD_VALUE (cons, "atable",
1781 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1782 PUSH_FIELD_VALUE (cons, "atable_syms",
1783 build1 (ADDR_EXPR, symbols_array_ptr_type,
1784 TYPE_ATABLE_SYMS_DECL (type)));
1785 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1786 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1789 PUSH_FIELD_VALUE (cons, "catch_classes",
1790 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1791 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1792 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1793 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1794 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1796 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1797 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1798 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1799 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1800 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1801 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1802 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1803 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1804 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1806 FINISH_RECORD_CONSTRUCTOR (cons);
1808 DECL_INITIAL (decl) = cons;
1810 /* Hash synchronization requires at least 64-bit alignment. */
1811 if (flag_hash_synchronization && POINTER_SIZE < 64)
1812 DECL_ALIGN (decl) = 64;
1814 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1820 java_expand_catch_classes (current_class);
1822 current_function_decl = NULL_TREE;
1823 make_class_data (current_class);
1825 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1828 /* Return 2 if CLASS is compiled by this compilation job;
1829 return 1 if CLASS can otherwise be assumed to be compiled;
1830 return 0 if we cannot assume that CLASS is compiled.
1831 Returns 1 for primitive and 0 for array types. */
1833 is_compiled_class (tree class)
1836 if (TREE_CODE (class) == POINTER_TYPE)
1837 class = TREE_TYPE (class);
1838 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1840 if (TYPE_ARRAY_P (class))
1842 if (class == current_class)
1845 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1846 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1848 /* The class was seen in the current ZIP file and will be
1849 available as a compiled class in the future but may not have
1850 been loaded already. Load it if necessary. This prevent
1851 build_class_ref () from crashing. */
1853 if (seen_in_zip && !CLASS_LOADED_P (class))
1854 load_class (class, 1);
1856 /* We return 2 for class seen in ZIP and class from files
1857 belonging to the same compilation unit */
1861 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1863 if (!CLASS_LOADED_P (class))
1865 if (CLASS_FROM_SOURCE_P (class))
1866 safe_layout_class (class);
1868 load_class (class, 1);
1876 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1879 build_dtable_decl (tree type)
1883 /* We need to build a new dtable type so that its size is uniquely
1884 computed when we're dealing with the class for real and not just
1885 faking it (like java.lang.Class during the initialization of the
1886 compiler.) We know we're not faking a class when CURRENT_CLASS is
1888 if (current_class == type)
1890 tree dummy = NULL_TREE;
1893 dtype = make_node (RECORD_TYPE);
1895 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1896 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1898 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1899 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1901 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1902 TREE_CHAIN (dummy) = tmp_field;
1903 DECL_CONTEXT (tmp_field) = dtype;
1904 DECL_ARTIFICIAL (tmp_field) = 1;
1908 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1909 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1911 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1912 TREE_CHAIN (dummy) = tmp_field;
1913 DECL_CONTEXT (tmp_field) = dtype;
1914 DECL_ARTIFICIAL (tmp_field) = 1;
1918 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1919 if (TARGET_VTABLE_USES_DESCRIPTORS)
1920 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1922 PUSH_FIELD (dtype, dummy, "methods",
1923 build_prim_array_type (nativecode_ptr_type_node, n));
1924 layout_type (dtype);
1927 dtype = dtable_type;
1929 return build_decl (VAR_DECL,
1930 java_mangle_vtable (&temporary_obstack, type), dtype);
1933 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1934 fields inherited from SUPER_CLASS. */
1937 push_super_field (tree this_class, tree super_class)
1940 /* Don't insert the field if we're just re-laying the class out. */
1941 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1943 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1944 DECL_IGNORED_P (base_decl) = 1;
1945 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1946 TYPE_FIELDS (this_class) = base_decl;
1947 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1948 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1951 /* Handle the different manners we may have to lay out a super class. */
1954 maybe_layout_super_class (tree super_class, tree this_class)
1956 if (TREE_CODE (super_class) == RECORD_TYPE)
1958 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1959 safe_layout_class (super_class);
1960 if (!CLASS_LOADED_P (super_class))
1961 load_class (super_class, 1);
1963 /* We might have to layout the class before its dependency on
1964 the super class gets resolved by java_complete_class */
1965 else if (TREE_CODE (super_class) == POINTER_TYPE)
1967 if (TREE_TYPE (super_class) != NULL_TREE)
1968 super_class = TREE_TYPE (super_class);
1971 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
1973 tree this_wrap = NULL_TREE;
1977 tree this_decl = TYPE_NAME (this_class);
1978 this_wrap = build_expr_wfl (this_class,
1979 DECL_SOURCE_FILE (this_decl),
1980 DECL_SOURCE_LINE (this_decl), 0);
1982 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1983 super_class, NULL_TREE, this_wrap);
1985 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1986 super_class = TREE_TYPE (super_class);
1989 if (!TYPE_SIZE (super_class))
1990 safe_layout_class (super_class);
1996 layout_class (tree this_class)
1998 tree super_class = CLASSTYPE_SUPER (this_class);
2001 class_list = tree_cons (this_class, NULL_TREE, class_list);
2002 if (CLASS_BEING_LAIDOUT (this_class))
2008 sprintf (buffer, " with `%s'",
2009 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2010 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2012 for (current = TREE_CHAIN (class_list); current;
2013 current = TREE_CHAIN (current))
2015 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2016 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
2017 IDENTIFIER_POINTER (DECL_NAME (decl)),
2018 DECL_SOURCE_FILE (decl),
2019 DECL_SOURCE_LINE (decl));
2020 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2022 obstack_1grow (&temporary_obstack, '\0');
2023 report = obstack_finish (&temporary_obstack);
2024 cyclic_inheritance_report = ggc_strdup (report);
2025 obstack_free (&temporary_obstack, report);
2026 TYPE_SIZE (this_class) = error_mark_node;
2029 CLASS_BEING_LAIDOUT (this_class) = 1;
2031 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2033 tree maybe_super_class
2034 = maybe_layout_super_class (super_class, this_class);
2035 if (maybe_super_class == NULL
2036 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2038 TYPE_SIZE (this_class) = error_mark_node;
2039 CLASS_BEING_LAIDOUT (this_class) = 0;
2040 class_list = TREE_CHAIN (class_list);
2043 if (TYPE_SIZE (this_class) == NULL_TREE)
2044 push_super_field (this_class, maybe_super_class);
2047 for (field = TYPE_FIELDS (this_class);
2048 field != NULL_TREE; field = TREE_CHAIN (field))
2050 if (FIELD_STATIC (field))
2052 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2053 SET_DECL_ASSEMBLER_NAME (field,
2055 (&temporary_obstack, field));
2059 layout_type (this_class);
2061 /* Also recursively load/layout any superinterfaces, but only if
2062 class was loaded from bytecode. The source parser will take care
2064 if (!CLASS_FROM_SOURCE_P (this_class))
2066 tree basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (this_class));
2070 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
2072 for (i = n; i > 0; i--)
2074 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
2075 tree super_interface = BINFO_TYPE (vec_elt);
2077 tree maybe_super_interface
2078 = maybe_layout_super_class (super_interface, NULL_TREE);
2079 if (maybe_super_interface == NULL
2080 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2082 TYPE_SIZE (this_class) = error_mark_node;
2083 CLASS_BEING_LAIDOUT (this_class) = 0;
2084 class_list = TREE_CHAIN (class_list);
2091 /* Convert the size back to an SI integer value. */
2092 TYPE_SIZE_UNIT (this_class) =
2093 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2095 CLASS_BEING_LAIDOUT (this_class) = 0;
2096 class_list = TREE_CHAIN (class_list);
2100 add_miranda_methods (tree base_class, tree search_class)
2102 tree basetype_vec = BINFO_BASE_BINFOS (TYPE_BINFO (search_class));
2103 int i, n = TREE_VEC_LENGTH (basetype_vec);
2104 for (i = 1; i < n; ++i)
2107 tree elt = TREE_VEC_ELT (basetype_vec, i);
2108 if (elt == NULL_TREE)
2110 elt = BINFO_TYPE (elt);
2112 /* Ensure that interface methods are seen in declared order. */
2113 layout_class_methods (elt);
2115 /* All base classes will have been laid out at this point, so the order
2116 will be correct. This code must match similar layout code in the
2118 for (method_decl = TYPE_METHODS (elt);
2119 method_decl; method_decl = TREE_CHAIN (method_decl))
2123 /* An interface can have <clinit>. */
2124 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2127 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2128 override = lookup_argument_method (base_class,
2129 DECL_NAME (method_decl), sig);
2130 if (override == NULL_TREE)
2132 /* Found a Miranda method. Add it. */
2134 sig = build_java_signature (TREE_TYPE (method_decl));
2136 = add_method (base_class,
2137 get_access_flags_from_decl (method_decl),
2138 DECL_NAME (method_decl), sig);
2139 METHOD_INVISIBLE (new_method) = 1;
2143 /* Try superinterfaces. */
2144 add_miranda_methods (base_class, elt);
2149 layout_class_methods (tree this_class)
2151 tree method_decl, dtable_count;
2152 tree super_class, type_name;
2154 if (TYPE_NVIRTUALS (this_class))
2157 super_class = CLASSTYPE_SUPER (this_class);
2161 super_class = maybe_layout_super_class (super_class, this_class);
2162 if (!TYPE_NVIRTUALS (super_class))
2163 layout_class_methods (super_class);
2164 dtable_count = TYPE_NVIRTUALS (super_class);
2167 dtable_count = integer_zero_node;
2169 type_name = TYPE_NAME (this_class);
2170 if (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name))
2172 /* An abstract class can have methods which are declared only in
2173 an implemented interface. These are called "Miranda
2174 methods". We make a dummy method entry for such methods
2176 add_miranda_methods (this_class, this_class);
2179 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2181 for (method_decl = TYPE_METHODS (this_class);
2182 method_decl; method_decl = TREE_CHAIN (method_decl))
2183 dtable_count = layout_class_method (this_class, super_class,
2184 method_decl, dtable_count);
2186 TYPE_NVIRTUALS (this_class) = dtable_count;
2189 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2190 argument for _Jv_LookupInterfaceMethodIdx(). */
2192 get_interface_method_index (tree method, tree interface)
2197 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2201 if (meth == NULL_TREE)
2206 /* Lay METHOD_DECL out, returning a possibly new value of
2207 DTABLE_COUNT. Also mangle the method's name. */
2210 layout_class_method (tree this_class, tree super_class,
2211 tree method_decl, tree dtable_count)
2213 tree method_name = DECL_NAME (method_decl);
2215 TREE_PUBLIC (method_decl) = 1;
2216 /* Considered external until we know what classes are being
2217 compiled into this object file. */
2218 DECL_EXTERNAL (method_decl) = 1;
2220 /* This is a good occasion to mangle the method's name */
2221 SET_DECL_ASSEMBLER_NAME (method_decl,
2222 java_mangle_decl (&temporary_obstack,
2224 /* We don't generate a RTL for the method if it's abstract, or if
2225 it's an interface method that isn't clinit. */
2226 if (! METHOD_ABSTRACT (method_decl)
2227 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2228 && (DECL_CLINIT_P (method_decl))))
2229 make_decl_rtl (method_decl, NULL);
2231 if (ID_INIT_P (method_name))
2233 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2235 for (ptr = p; *ptr; )
2240 DECL_CONSTRUCTOR_P (method_decl) = 1;
2241 build_java_argument_signature (TREE_TYPE (method_decl));
2243 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2246 build_java_argument_signature (TREE_TYPE (method_decl));
2247 bool method_override = false;
2248 tree super_method = lookup_argument_method (super_class, method_name,
2250 if (super_method != NULL_TREE)
2252 method_override = true;
2253 if (! METHOD_PUBLIC (super_method) &&
2254 ! METHOD_PROTECTED (super_method))
2256 /* Don't override private method, or default-access method in
2258 if (METHOD_PRIVATE (super_method) ||
2259 ! in_same_package (TYPE_NAME (this_class),
2260 TYPE_NAME (super_class)))
2261 method_override = false;
2264 if (method_override)
2266 tree method_index = get_method_index (super_method);
2267 set_method_index (method_decl, method_index);
2268 if (method_index == NULL_TREE
2269 && !CLASS_FROM_SOURCE_P (this_class))
2270 error ("%Jnon-static method '%D' overrides static method",
2271 method_decl, method_decl);
2273 else if (! METHOD_FINAL (method_decl)
2274 && ! METHOD_PRIVATE (method_decl)
2275 && ! CLASS_FINAL (TYPE_NAME (this_class))
2278 set_method_index (method_decl, dtable_count);
2279 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2280 dtable_count, integer_one_node));
2284 return dtable_count;
2288 register_class (void)
2290 /* END does not need to be registered with the garbage collector
2291 because it always points into the list given by REGISTERED_CLASS,
2292 and that variable is registered with the collector. */
2294 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2295 tree current = copy_node (node);
2297 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2298 if (!registered_class)
2299 registered_class = current;
2301 TREE_CHAIN (end) = current;
2306 /* Emit something to register classes at start-up time.
2308 The preferred mechanism is through the .jcr section, which contain
2309 a list of pointers to classes which get registered during constructor
2312 The fallback mechanism is to add statements to *LIST_P to call
2313 _Jv_RegisterClass for each class in this file. These statements will
2314 be added to a static constructor function for this translation unit. */
2317 emit_register_classes (tree *list_p)
2319 if (registered_class == NULL)
2322 /* ??? This isn't quite the correct test. We also have to know
2323 that the target is using gcc's crtbegin/crtend objects rather
2324 than the ones that come with the operating system. */
2325 if (SUPPORTS_WEAK && targetm.have_named_sections)
2327 #ifdef JCR_SECTION_NAME
2329 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2330 assemble_align (POINTER_SIZE);
2331 for (t = registered_class; t; t = TREE_CHAIN (t))
2332 assemble_integer (XEXP (DECL_RTL (t), 0),
2333 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2340 tree klass, t, register_class_fn;
2342 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2343 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2344 TREE_PUBLIC (t) = 1;
2345 DECL_EXTERNAL (t) = 1;
2346 register_class_fn = t;
2348 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2350 t = build_fold_addr_expr (klass);
2351 t = tree_cons (NULL, t, NULL);
2352 t = build_function_call_expr (register_class_fn, t);
2353 append_to_statement_list (t, list_p);
2358 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2361 build_symbol_entry (tree decl)
2363 tree clname, name, signature, sym;
2365 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2366 name = build_utf8_ref (DECL_NAME (decl));
2367 signature = build_java_signature (TREE_TYPE (decl));
2368 signature = build_utf8_ref (unmangle_classname
2369 (IDENTIFIER_POINTER (signature),
2370 IDENTIFIER_LENGTH (signature)));
2372 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2373 PUSH_FIELD_VALUE (sym, "clname", clname);
2374 PUSH_FIELD_VALUE (sym, "name", name);
2375 PUSH_FIELD_VALUE (sym, "signature", signature);
2376 FINISH_RECORD_CONSTRUCTOR (sym);
2377 TREE_CONSTANT (sym) = 1;
2378 TREE_INVARIANT (sym) = 1;
2383 /* Emit a symbol table: used by -findirect-dispatch. */
2386 emit_symbol_table (tree name, tree the_table, tree decl_list,
2387 tree the_syms_decl, tree the_array_element_type)
2389 tree method_list, method, table, list, null_symbol;
2390 tree table_size, the_array_type;
2393 /* Only emit a table if this translation unit actually made any
2394 references via it. */
2395 if (decl_list == NULL_TREE)
2398 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2400 method_list = decl_list;
2402 while (method_list != NULL_TREE)
2404 method = TREE_VALUE (method_list);
2405 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2406 method_list = TREE_CHAIN (method_list);
2410 /* Terminate the list with a "null" entry. */
2411 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2412 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2413 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2414 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2415 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2416 TREE_CONSTANT (null_symbol) = 1;
2417 TREE_INVARIANT (null_symbol) = 1;
2418 list = tree_cons (NULL_TREE, null_symbol, list);
2420 /* Put the list in the right order and make it a constructor. */
2421 list = nreverse (list);
2422 table = build_constructor (symbols_array_type, list);
2424 /* Make it the initial value for otable_syms and emit the decl. */
2425 DECL_INITIAL (the_syms_decl) = table;
2426 DECL_ARTIFICIAL (the_syms_decl) = 1;
2427 DECL_IGNORED_P (the_syms_decl) = 1;
2428 rest_of_decl_compilation (the_syms_decl, NULL, 1, 0);
2430 /* Now that its size is known, redefine the table as an
2431 uninitialized static array of INDEX + 1 elements. The extra entry
2432 is used by the runtime to track whether the table has been
2434 table_size = build_index_type (build_int_2 (index, 0));
2435 the_array_type = build_array_type (the_array_element_type, table_size);
2436 the_table = build_decl (VAR_DECL, name, the_array_type);
2437 TREE_STATIC (the_table) = 1;
2438 TREE_READONLY (the_table) = 1;
2439 rest_of_decl_compilation (the_table, NULL, 1, 0);
2444 /* make an entry for the catch_classes list. */
2446 make_catch_class_record (tree catch_class, tree classname)
2449 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2450 START_RECORD_CONSTRUCTOR (entry, type);
2451 PUSH_FIELD_VALUE (entry, "address", catch_class);
2452 PUSH_FIELD_VALUE (entry, "classname", classname);
2453 FINISH_RECORD_CONSTRUCTOR (entry);
2458 /* Generate the list of Throwable classes that are caught by exception
2459 handlers in this class. */
2461 emit_catch_table (tree this_class)
2463 tree table, table_size, array_type;
2464 TYPE_CATCH_CLASSES (this_class) =
2466 make_catch_class_record (null_pointer_node, null_pointer_node),
2467 TYPE_CATCH_CLASSES (this_class));
2468 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2469 TYPE_CATCH_CLASSES (this_class) =
2471 make_catch_class_record (null_pointer_node, null_pointer_node),
2472 TYPE_CATCH_CLASSES (this_class));
2474 build_index_type (build_int_2
2475 (list_length (TYPE_CATCH_CLASSES (this_class)), 0));
2477 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2480 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2481 DECL_INITIAL (table) =
2482 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2483 TREE_STATIC (table) = 1;
2484 TREE_READONLY (table) = 1;
2485 DECL_IGNORED_P (table) = 1;
2486 rest_of_decl_compilation (table, NULL, 1, 0);
2492 init_class_processing (void)
2494 fields_ident = get_identifier ("fields");
2495 info_ident = get_identifier ("info");
2497 gcc_obstack_init (&temporary_obstack);
2500 static hashval_t java_treetreehash_hash (const void *);
2501 static int java_treetreehash_compare (const void *, const void *);
2503 /* A hash table mapping trees to trees. Used generally. */
2505 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2508 java_treetreehash_hash (const void *k_p)
2510 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2511 return JAVA_TREEHASHHASH_H (k->key);
2515 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2517 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2518 tree k2 = (tree) k2_p;
2519 return (k1->key == k2);
2523 java_treetreehash_find (htab_t ht, tree t)
2525 struct treetreehash_entry *e;
2526 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2527 e = htab_find_with_hash (ht, t, hv);
2535 java_treetreehash_new (htab_t ht, tree t)
2538 struct treetreehash_entry *tthe;
2539 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2541 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2544 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2549 tthe = (struct treetreehash_entry *) *e;
2550 return &tthe->value;
2554 java_treetreehash_create (size_t size, int gc)
2557 return htab_create_ggc (size, java_treetreehash_hash,
2558 java_treetreehash_compare, NULL);
2560 return htab_create_alloc (size, java_treetreehash_hash,
2561 java_treetreehash_compare, free, xcalloc, free);
2564 /* Break down qualified IDENTIFIER into package and class-name components.
2565 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2566 "pkg.foo", and RIGHT to "Bar". */
2569 split_qualified_name (tree *left, tree *right, tree source)
2572 int l = IDENTIFIER_LENGTH (source);
2574 base = alloca (l + 1);
2575 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2577 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2579 while (*p != '.' && p != base)
2582 /* We didn't find a '.'. Return an error. */
2588 *right = get_identifier (p+1);
2589 *left = get_identifier (base);
2594 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2595 if the classes are from the same package. */
2598 in_same_package (tree name1, tree name2)
2604 if (TREE_CODE (name1) == TYPE_DECL)
2605 name1 = DECL_NAME (name1);
2606 if (TREE_CODE (name2) == TYPE_DECL)
2607 name2 = DECL_NAME (name2);
2609 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2610 /* One in empty package. */
2613 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2614 /* Both in empty package. */
2617 split_qualified_name (&pkg1, &tmp, name1);
2618 split_qualified_name (&pkg2, &tmp, name2);
2620 return (pkg1 == pkg2);
2623 #include "gt-java-class.h"