1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 97-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
32 #include "java-tree.h"
39 static tree mangle_class_field PROTO ((tree class));
40 static tree make_method_value PROTO ((tree));
41 static tree build_java_method_type PROTO ((tree, tree, int));
42 static int32 hashUtf8String PROTO ((const char *, int));
43 static tree make_field_value PROTO ((tree));
44 static tree get_dispatch_vector PROTO ((tree));
45 static tree get_dispatch_table PROTO ((tree, tree));
46 static void append_gpp_mangled_type PROTO ((struct obstack *, tree));
47 static tree mangle_static_field PROTO ((tree));
49 static rtx registerClass_libfunc;
51 extern struct obstack permanent_obstack;
52 extern struct obstack temporary_obstack;
54 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
55 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
56 Also, PREFIX is prepended, and SUFFIX is appended. */
59 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
67 int prefix_len = strlen (prefix);
68 int suffix_len = strlen (suffix);
69 int i = prefix_len + old_length + suffix_len + 1;
73 char *buffer = (char *)alloca (i);
75 strcpy (buffer, prefix);
76 for (i = 0; i < old_length; i++)
78 char ch = old_name[i];
81 buffer[prefix_len + i] = ch;
83 strcpy (buffer + prefix_len + old_length, suffix);
84 return get_identifier (buffer);
87 /* Return an IDENTIFIER_NODE the same as OLD_ID,
88 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
89 Also, PREFIX is prepended, and SUFFIX is appended. */
92 identifier_subst (old_id, prefix, old_char, new_char, suffix)
99 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
100 prefix, old_char, new_char, suffix);
103 /* Generate a valid C identifier from the name of the class TYPE,
104 prefixed by PREFIX. */
107 mangled_classname (prefix, type)
111 tree ident = TYPE_NAME (type);
112 if (TREE_CODE (ident) != IDENTIFIER_NODE)
113 ident = DECL_NAME (ident);
114 return identifier_subst (ident, prefix, '.', '_', "");
121 push_obstacks (&permanent_obstack, &permanent_obstack);
122 type = make_node (RECORD_TYPE);
123 #ifdef JAVA_USE_HANDLES
124 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
125 build_pointer_type (type));
126 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
127 methodtable_ptr_type);
128 tree handle_type = make_node (RECORD_TYPE);
129 TREE_CHAIN (field1) = field2;
130 TYPE_FIELDS (handle_type) = field1;
131 TYPE_BINFO (type) = make_tree_vec (7);
132 TYPE_BINFO (handle_type) = make_tree_vec (7);
133 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
134 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
136 TYPE_BINFO (type) = make_tree_vec (6);
143 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
144 and where each of the constituents is separated by '/',
145 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
148 unmangle_classname (name, name_length)
149 const char *name; int name_length;
151 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
152 /* It's not sufficient to compare to_return and get_identifier
153 (name) to determine whether to_return is qualified. There are
154 cases in signature analysis where name will be stripped of a
156 name = IDENTIFIER_POINTER (to_return);
160 QUALIFIED_P (to_return) = 1;
168 push_class (class_type, class_name)
169 tree class_type, class_name;
171 tree decl, signature;
172 char *save_input_filename = input_filename;
173 int save_lineno = lineno;
174 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
175 push_obstacks (&permanent_obstack, &permanent_obstack);
176 CLASS_P (class_type) = 1;
177 input_filename = IDENTIFIER_POINTER (source_name);
179 decl = build_decl (TYPE_DECL, class_name, class_type);
180 input_filename = save_input_filename;
181 lineno = save_lineno;
182 signature = identifier_subst (class_name, "L", '.', '/', ";");
183 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
185 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
186 both a typedef and in the struct name-space. We may want to re-visit
187 this later, but for now it reduces the changes needed for gdb. */
188 DECL_ARTIFICIAL (decl) = 1;
190 pushdecl_top_level (decl);
191 #ifdef JAVA_USE_HANDLES
193 tree handle_name = identifier_subst (class_name,
194 "Handle$", '.', '.', "");
195 tree handle_decl = build_decl (TYPE_DECL, handle_name,
196 CLASS_TO_HANDLE_TYPE (class_type));
197 pushdecl (handle_decl);
205 /* Finds the (global) class named NAME. Creates the class if not found.
206 Also creates associated TYPE_DECL.
207 Does not check if the class actually exists, load the class,
208 fill in field or methods, or do layout_type. */
214 tree decl = IDENTIFIER_CLASS_VALUE (name);
215 if (decl == NULL_TREE)
216 decl = push_class (make_class (), name);
217 return TREE_TYPE (decl);
221 set_super_info (access_flags, this_class, super_class, interfaces_count)
225 int interfaces_count;
227 int total_supers = interfaces_count;
228 tree class_decl = TYPE_NAME (this_class);
232 push_obstacks (&permanent_obstack, &permanent_obstack);
233 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
236 tree super_binfo = make_tree_vec (6);
237 BINFO_TYPE (super_binfo) = super_class;
238 BINFO_OFFSET (super_binfo) = integer_zero_node;
239 TREE_VIA_PUBLIC (super_binfo) = 1;
240 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
242 CLASS_HAS_SUPER (this_class) = 1;
246 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
247 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
248 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
249 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
250 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
253 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
254 direct sub-classes of Object are 1, and so on. */
261 if (! CLASS_LOADED_P (clas))
262 load_class (clas, 1);
263 while (clas != object_type_node)
266 clas = TYPE_BINFO_BASETYPE (clas, 0);
271 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
274 interface_of_p (type1, type2)
280 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
282 n = TREE_VEC_LENGTH (basetype_vec);
283 for (i = 0; i < n; i++)
285 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
286 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
289 for (i = 0; i < n; i++)
291 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
292 if (vec_elt && BINFO_TYPE (vec_elt)
293 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
299 /* Return true iff TYPE1 inherits from TYPE2. */
302 inherits_from_p (type1, type2)
305 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
309 type1 = CLASSTYPE_SUPER (type1);
315 add_interface_do (basetype_vec, interface_class, i)
316 tree basetype_vec, interface_class;
319 tree interface_binfo = make_tree_vec (6);
320 BINFO_TYPE (interface_binfo) = interface_class;
321 BINFO_OFFSET (interface_binfo) = integer_zero_node;
322 TREE_VIA_VIRTUAL (interface_binfo) = 1;
323 TREE_VIA_PUBLIC (interface_binfo) = 1;
324 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
327 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
328 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
329 if attempt is made to add it twice. */
332 maybe_add_interface (this_class, interface_class)
333 tree this_class, interface_class;
335 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
337 int n = TREE_VEC_LENGTH (basetype_vec);
342 error ("internal error - too many interface type");
345 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
347 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
348 return interface_class;
350 add_interface_do (basetype_vec, interface_class, i);
354 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
357 add_interface (this_class, interface_class)
358 tree this_class, interface_class;
360 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
362 int n = TREE_VEC_LENGTH (basetype_vec);
367 error ("internal error - too many interface type");
370 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
373 add_interface_do (basetype_vec, interface_class, i);
377 /* Return the address of a pointer to the first FUNCTION_DECL
378 in the list (*LIST) whose DECL_NAME is NAME. */
381 find_named_method (list, name)
385 while (*list && DECL_NAME (*list) != name)
386 list = &TREE_CHAIN (*list);
392 build_java_method_type (fntype, this_class, access_flags)
397 if (access_flags & ACC_STATIC)
399 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
403 add_method_1 (handle_class, access_flags, name, function_type)
409 tree method_type, fndecl;
410 push_obstacks (&permanent_obstack, &permanent_obstack);
412 method_type = build_java_method_type (function_type,
413 handle_class, access_flags);
415 fndecl = build_decl (FUNCTION_DECL, name, method_type);
416 DECL_CONTEXT (fndecl) = handle_class;
418 DECL_LANG_SPECIFIC (fndecl)
419 = (struct lang_decl *) permalloc (sizeof (struct lang_decl));
420 bzero ((PTR) DECL_LANG_SPECIFIC (fndecl), sizeof (struct lang_decl));
422 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
423 TYPE_METHODS (handle_class) = fndecl;
426 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
427 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
428 if (access_flags & ACC_PRIVATE) METHOD_PRIVATE (fndecl) = 1;
429 if (access_flags & ACC_NATIVE) METHOD_NATIVE (fndecl) = 1;
430 if (access_flags & ACC_STATIC) METHOD_STATIC (fndecl) = 1;
431 if (access_flags & ACC_FINAL) METHOD_FINAL (fndecl) = 1;
432 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
433 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
434 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
438 /* Add a method to THIS_CLASS.
439 The method's name is NAME.
440 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
443 add_method (this_class, access_flags, name, method_sig)
449 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
450 tree function_type, fndecl;
451 unsigned char *sig = (unsigned char*)IDENTIFIER_POINTER (method_sig);
452 push_obstacks (&permanent_obstack, &permanent_obstack);
454 fatal ("bad method signature");
455 function_type = get_type_from_signature (method_sig);
456 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
457 set_java_signature (TREE_TYPE (fndecl), method_sig);
463 add_field (class, name, field_type, flags)
469 int is_static = (flags & ACC_STATIC) != 0;
471 /* Push the obstack of field_type ? FIXME */
472 push_obstacks (&permanent_obstack, &permanent_obstack);
473 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
475 TREE_CHAIN (field) = TYPE_FIELDS (class);
476 TYPE_FIELDS (class) = field;
477 DECL_CONTEXT (field) = class;
479 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
480 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
481 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
482 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
483 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
484 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
487 FIELD_STATIC (field) = 1;
488 /* Always make field externally visible. This is required so
489 that native methods can always access the field. */
490 TREE_PUBLIC (field) = 1;
495 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
498 set_constant_value (field, constant)
499 tree field, constant;
501 if (field == NULL_TREE)
502 warning ("misplaced ConstantValue attribute (not in any field)");
503 else if (DECL_INITIAL (field) != NULL_TREE)
504 warning ("duplicate ConstanValue atribute for field '%s'",
505 IDENTIFIER_POINTER (DECL_NAME (field)));
507 DECL_INITIAL (field) = constant;
510 /* Count the number of Unicode chars encoded in a given Ut8 string. */
514 strLengthUtf8 (str, len)
518 register unsigned char* ptr = (unsigned char*) str;
519 register unsigned char *limit = ptr + len;
521 for (; ptr < limit; str_length++) {
522 if (UTF8_GET (ptr, limit) < 0)
530 /* Calculate a hash value for a string encoded in Utf8 format.
531 * This returns the same hash value as specified for java.lang.String.hashCode.
535 hashUtf8String (str, len)
539 register const unsigned char* ptr = (const unsigned char*) str;
540 register const unsigned char *limit = ptr + len;
544 int ch = UTF8_GET (ptr, limit);
545 /* Updated specification from
546 http://www.javasoft.com/docs/books/jls/clarify.html. */
547 hash = (31 * hash) + ch;
552 tree utf8_decl_list = NULL_TREE;
555 build_utf8_ref (name)
558 char* name_ptr = IDENTIFIER_POINTER(name);
559 int name_len = IDENTIFIER_LENGTH(name);
562 tree ctype, field, str_type, cinit, string;
563 static int utf8_count = 0;
565 tree ref = IDENTIFIER_UTF8_REF (name);
567 if (ref != NULL_TREE)
570 push_obstacks (&permanent_obstack, &permanent_obstack);
571 ctype = make_node (RECORD_TYPE);
572 str_type = build_prim_array_type (unsigned_byte_type_node,
573 name_len + 1); /* Allow for final '\0'. */
574 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
575 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
576 PUSH_FIELD (ctype, field, "data", str_type);
577 FINISH_RECORD (ctype);
578 START_RECORD_CONSTRUCTOR (cinit, ctype);
579 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
580 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
581 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
582 string = build_string (name_len, name_ptr);
583 TREE_TYPE (string) = str_type;
584 PUSH_FIELD_VALUE (cinit, "data", string);
585 FINISH_RECORD_CONSTRUCTOR (cinit);
587 /* Build a unique identifier based on buf. */
588 sprintf(buf, "_Utf%d", ++utf8_count);
589 buf_ptr = &buf[strlen (buf)];
590 if (name_len > 0 && name_ptr[0] >= '0' && name_ptr[0] <= '9')
592 while (--name_len >= 0)
594 unsigned char c = *name_ptr++;
597 if (!ISALPHA(c) && !ISDIGIT(c))
600 if (buf_ptr >= buf + 50)
605 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
606 /* FIXME get some way to force this into .text, not .data. */
607 TREE_STATIC (decl) = 1;
608 DECL_ARTIFICIAL (decl) = 1;
609 DECL_IGNORED_P (decl) = 1;
610 TREE_READONLY (decl) = 1;
611 DECL_INITIAL (decl) = cinit;
612 TREE_CHAIN (decl) = utf8_decl_list;
613 layout_decl (decl, 0);
615 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
616 utf8_decl_list = decl;
617 make_decl_rtl (decl, (char*) 0, 1);
618 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
619 IDENTIFIER_UTF8_REF (name) = ref;
624 /* Build a reference to the class TYPE.
625 Also handles primitive types and array types. */
628 build_class_ref (type)
631 int is_compiled = is_compiled_class (type);
634 tree ref, decl_name, decl;
635 if (TREE_CODE (type) == POINTER_TYPE)
636 type = TREE_TYPE (type);
637 if (TREE_CODE (type) == RECORD_TYPE)
639 if (TYPE_SIZE (type) == error_mark_node)
640 return null_pointer_node;
641 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
642 "", '/', '/', ".class");
643 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
644 if (decl == NULL_TREE)
646 push_obstacks (&permanent_obstack, &permanent_obstack);
647 decl = build_decl (VAR_DECL, decl_name, class_type_node);
648 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
649 TREE_STATIC (decl) = 1;
650 TREE_PUBLIC (decl) = 1;
651 DECL_IGNORED_P (decl) = 1;
652 DECL_ARTIFICIAL (decl) = 1;
653 DECL_ASSEMBLER_NAME (decl) = mangle_class_field (type);
654 make_decl_rtl (decl, NULL, 1);
655 pushdecl_top_level (decl);
656 if (is_compiled == 1)
657 DECL_EXTERNAL (decl) = 1;
665 if (flag_emit_class_files)
667 const char *prim_class_name;
669 if (type == char_type_node)
670 prim_class_name = "java.lang.Character";
671 else if (type == boolean_type_node)
672 prim_class_name = "java.lang.Boolean";
673 else if (type == byte_type_node)
674 prim_class_name = "java.lang.Byte";
675 else if (type == short_type_node)
676 prim_class_name = "java.lang.Short";
677 else if (type == int_type_node)
678 prim_class_name = "java.lang.Integer";
679 else if (type == long_type_node)
680 prim_class_name = "java.lang.Long";
681 else if (type == float_type_node)
682 prim_class_name = "java.lang.Float";
683 else if (type == double_type_node)
684 prim_class_name = "java.lang.Double";
685 else if (type == void_type_node)
686 prim_class_name = "java.lang.Void";
688 fatal ("internal error - bad type to build_class_ref");
689 prim_class = lookup_class (get_identifier (prim_class_name));
690 return build (COMPONENT_REF, NULL_TREE,
691 prim_class, TYPE_identifier_node);
693 decl_name = TYPE_NAME (type);
694 if (TREE_CODE (decl_name) == TYPE_DECL)
695 decl_name = DECL_NAME (decl_name);
696 name = IDENTIFIER_POINTER (decl_name);
697 if (strncmp (name, "promoted_", 9) == 0)
699 sprintf (buffer, "_Jv_%sClass", name);
700 decl_name = get_identifier (buffer);
701 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
702 if (decl == NULL_TREE)
704 push_obstacks (&permanent_obstack, &permanent_obstack);
705 decl = build_decl (VAR_DECL, decl_name, class_type_node);
706 TREE_STATIC (decl) = 1;
707 TREE_PUBLIC (decl) = 1;
708 make_decl_rtl (decl, NULL, 1);
709 pushdecl_top_level (decl);
710 if (is_compiled == 1)
711 DECL_EXTERNAL (decl) = 1;
716 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
723 push_obstacks (&permanent_obstack, &permanent_obstack);
724 index = alloc_class_constant (type);
725 cl = build_ref_from_constant_pool (index);
726 TREE_TYPE (cl) = promote_type (class_ptr_type);
733 build_static_field_ref (fdecl)
736 tree fclass = DECL_CONTEXT (fdecl);
737 int is_compiled = is_compiled_class (fclass);
740 if (DECL_RTL (fdecl) == 0)
742 push_obstacks (&permanent_obstack, &permanent_obstack);
743 make_decl_rtl (fdecl, NULL, 1);
745 if (is_compiled == 1)
746 DECL_EXTERNAL (fdecl) = 1;
753 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr
755 static tree fields_ident = NULL_TREE;
756 static tree info_ident = NULL_TREE;
757 tree ref = build_class_ref (fclass);
760 ref = build1 (INDIRECT_REF, class_type_node, ref);
761 if (fields_ident == NULL_TREE)
762 fields_ident = get_identifier ("fields");
763 if (info_ident == NULL_TREE)
764 info_ident = get_identifier ("info");
765 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
766 lookup_field (&class_type_node, fields_ident));
768 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
772 if (fld == NULL_TREE)
773 fatal ("field '%s' not found in class",
774 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
775 if (FIELD_STATIC (fld))
778 field_index *= int_size_in_bytes (field_type_node);
779 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
780 ref, build_int_2 (field_index, 0)));
781 ref = build1 (INDIRECT_REF, field_type_node, ref);
782 ref = build (COMPONENT_REF, field_info_union_node,
783 ref, lookup_field (&field_type_node, info_ident));
784 ref = build (COMPONENT_REF, ptr_type_node,
785 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
786 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
791 get_access_flags_from_decl (decl)
794 int access_flags = 0;
795 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
797 if (FIELD_STATIC (decl))
798 access_flags |= ACC_STATIC;
799 if (FIELD_PUBLIC (decl))
800 access_flags |= ACC_PUBLIC;
801 if (FIELD_PROTECTED (decl))
802 access_flags |= ACC_PROTECTED;
803 if (FIELD_PRIVATE (decl))
804 access_flags |= ACC_PRIVATE;
805 if (FIELD_FINAL (decl))
806 access_flags |= ACC_FINAL;
807 if (FIELD_VOLATILE (decl))
808 access_flags |= ACC_VOLATILE;
809 if (FIELD_TRANSIENT (decl))
810 access_flags |= ACC_TRANSIENT;
813 if (TREE_CODE (decl) == TYPE_DECL)
815 if (CLASS_PUBLIC (decl))
816 access_flags |= ACC_PUBLIC;
817 if (CLASS_FINAL (decl))
818 access_flags |= ACC_FINAL;
819 if (CLASS_SUPER (decl))
820 access_flags |= ACC_SUPER;
821 if (CLASS_INTERFACE (decl))
822 access_flags |= ACC_INTERFACE;
823 if (CLASS_ABSTRACT (decl))
824 access_flags |= ACC_ABSTRACT;
827 if (TREE_CODE (decl) == FUNCTION_DECL)
829 if (METHOD_PUBLIC (decl))
830 access_flags |= ACC_PUBLIC;
831 if (METHOD_PRIVATE (decl))
832 access_flags |= ACC_PRIVATE;
833 if (METHOD_PROTECTED (decl))
834 access_flags |= ACC_PROTECTED;
835 if (METHOD_STATIC (decl))
836 access_flags |= ACC_STATIC;
837 if (METHOD_FINAL (decl))
838 access_flags |= ACC_FINAL;
839 if (METHOD_SYNCHRONIZED (decl))
840 access_flags |= ACC_SYNCHRONIZED;
841 if (METHOD_NATIVE (decl))
842 access_flags |= ACC_NATIVE;
843 if (METHOD_ABSTRACT (decl))
844 access_flags |= ACC_ABSTRACT;
845 if (METHOD_TRANSIENT (decl))
846 access_flags |= ACC_TRANSIENT;
853 make_field_value (fdecl)
858 tree type = TREE_TYPE (fdecl);
859 int resolved = is_compiled_class (type);
860 START_RECORD_CONSTRUCTOR (finit, field_type_node);
861 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
863 type = build_class_ref (type);
866 tree signature = build_java_signature (type);
867 type = build_utf8_ref (unmangle_classname
868 (IDENTIFIER_POINTER(signature),
869 IDENTIFIER_LENGTH(signature)));
871 PUSH_FIELD_VALUE (finit, "type", type);
872 flags = get_access_flags_from_decl (fdecl);
874 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
875 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
876 bsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (fdecl))) / BITS_PER_UNIT;
877 PUSH_FIELD_VALUE (finit, "bsize", build_int_2 (bsize, 0));
878 if (FIELD_STATIC (fdecl))
880 tree cfield = TREE_CHAIN (TYPE_FIELDS(field_info_union_node));
881 tree faddr = build_address_of (build_static_field_ref (fdecl));
882 info = build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
883 build_tree_list (cfield, faddr));
888 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (fdecl)) / BITS_PER_UNIT;
889 info = build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
890 build_tree_list (TYPE_FIELDS(field_info_union_node),
891 build_int_2 (boffset, 0)));
893 PUSH_FIELD_VALUE (finit, "info", info);
895 FINISH_RECORD_CONSTRUCTOR (finit);
900 make_method_value (mdecl)
905 #define ACC_TRANSLATED 0x4000
906 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
907 code = null_pointer_node;
908 if (DECL_RTL (mdecl))
909 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
910 START_RECORD_CONSTRUCTOR (minit, method_type_node);
911 PUSH_FIELD_VALUE (minit, "name",
912 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
914 : DECL_NAME (mdecl)));
916 tree signature = build_java_signature (TREE_TYPE (mdecl));
917 PUSH_FIELD_VALUE (minit, "signature",
920 (IDENTIFIER_POINTER(signature),
921 IDENTIFIER_LENGTH(signature)))));
923 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
924 PUSH_FIELD_VALUE (minit, "ncode", code);
925 FINISH_RECORD_CONSTRUCTOR (minit);
930 get_dispatch_vector (type)
933 tree vtable = TYPE_VTABLE (type);
938 tree super = CLASSTYPE_SUPER (type);
939 int nvirtuals = TREE_INT_CST_LOW (TYPE_NVIRTUALS (type));
940 vtable = make_tree_vec (nvirtuals);
941 TYPE_VTABLE (type) = vtable;
942 if (super != NULL_TREE)
944 tree super_vtable = get_dispatch_vector (super);
945 for ( i = TREE_INT_CST_LOW (TYPE_NVIRTUALS (super)); --i >= 0; )
946 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
948 for (method = TYPE_METHODS (type); method != NULL_TREE;
949 method = TREE_CHAIN (method))
951 if (DECL_VINDEX (method) != NULL_TREE
952 && TREE_CODE (DECL_VINDEX (method)) == INTEGER_CST)
954 TREE_VEC_ELT (vtable, TREE_INT_CST_LOW (DECL_VINDEX (method)))
963 get_dispatch_table (type, this_class_addr)
964 tree type, this_class_addr;
966 tree vtable = get_dispatch_vector (type);
968 tree list = NULL_TREE;
969 int nvirtuals = TREE_VEC_LENGTH (vtable);
970 for (i = nvirtuals; --i >= 0; )
972 tree method = TREE_VEC_ELT (vtable, i);
973 if (METHOD_ABSTRACT (method))
974 warning_with_decl (method, "abstract method in non-abstract class");
975 if (DECL_RTL (method) == 0)
976 make_decl_rtl (method, NULL, 1);
977 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
978 build1 (ADDR_EXPR, nativecode_ptr_type_node, method),
981 /* Dummy entry for compatibility with G++ -fvtable-thunks. */
982 list = tree_cons (integer_zero_node, null_pointer_node, list);
983 list = tree_cons (integer_zero_node, this_class_addr, list);
984 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
990 make_class_data (type)
993 tree decl, cons, temp;
994 tree field, fields_decl;
995 tree static_fields = NULL_TREE;
996 tree instance_fields = NULL_TREE;
997 HOST_WIDE_INT static_field_count = 0;
998 HOST_WIDE_INT instance_field_count = 0;
999 HOST_WIDE_INT field_count;
1000 tree field_array_type;
1002 tree methods = NULL_TREE;
1003 tree dtable_decl = NULL_TREE;
1004 HOST_WIDE_INT method_count = 0;
1005 tree method_array_type;
1008 tree this_class_addr;
1009 tree constant_pool_constructor;
1010 tree interfaces = null_pointer_node;
1011 int interface_len = 0;
1012 tree type_decl = TYPE_NAME (type);
1014 this_class_addr = build_class_ref (type);
1015 decl = TREE_OPERAND (this_class_addr, 0);
1017 /* Build Field array. */
1018 field = TYPE_FIELDS (type);
1019 if (DECL_NAME (field) == NULL_TREE)
1020 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1021 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1023 if (! DECL_ARTIFICIAL (field))
1025 tree init = make_field_value (field);
1026 if (FIELD_STATIC (field))
1028 tree initial = DECL_INITIAL (field);
1029 static_field_count++;
1030 static_fields = tree_cons (NULL_TREE, init, static_fields);
1031 /* If the initial value is a string constant,
1032 prevent output_constant from trying to assemble the value. */
1033 if (initial != NULL_TREE
1034 && TREE_TYPE (initial) == string_ptr_type_node)
1035 DECL_INITIAL (field) = NULL_TREE;
1036 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1037 DECL_INITIAL (field) = initial;
1041 instance_field_count++;
1042 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1046 field_count = static_field_count + instance_field_count;
1047 if (field_count > 0)
1049 static_fields = nreverse (static_fields);
1050 instance_fields = nreverse (instance_fields);
1051 static_fields = chainon (static_fields, instance_fields);
1052 field_array_type = build_prim_array_type (field_type_node, field_count);
1053 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1055 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1056 NULL_TREE, static_fields);
1057 TREE_STATIC (fields_decl) = 1;
1058 DECL_ARTIFICIAL (fields_decl) = 1;
1059 DECL_IGNORED_P (fields_decl) = 1;
1060 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1063 fields_decl = NULL_TREE;
1065 /* Build Method array. */
1066 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1067 method != NULL_TREE; method = TREE_CHAIN (method))
1070 if (METHOD_PRIVATE (method)
1071 && (flag_inline_functions || optimize))
1073 init = make_method_value (method);
1075 methods = tree_cons (NULL_TREE, init, methods);
1077 method_array_type = build_prim_array_type (method_type_node, method_count);
1078 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1080 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1081 NULL_TREE, nreverse (methods));
1082 TREE_STATIC (methods_decl) = 1;
1083 DECL_ARTIFICIAL (methods_decl) = 1;
1084 DECL_IGNORED_P (methods_decl) = 1;
1085 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1087 if (flag_assume_compiled
1088 && ! CLASS_ABSTRACT (type_decl) && ! CLASS_INTERFACE (type_decl))
1090 tree dtable = get_dispatch_table (type, this_class_addr);
1091 dtable_decl = build_dtable_decl (type);
1092 DECL_INITIAL (dtable_decl) = dtable;
1093 TREE_STATIC (dtable_decl) = 1;
1094 DECL_ARTIFICIAL (dtable_decl) = 1;
1095 DECL_IGNORED_P (dtable_decl) = 1;
1096 TREE_PUBLIC (dtable_decl) = 1;
1097 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1100 super = CLASSTYPE_SUPER (type);
1101 if (super == NULL_TREE)
1102 super = null_pointer_node;
1103 else if (flag_assume_compiled)
1104 super = build_class_ref (super);
1107 int super_index = alloc_class_constant (super);
1108 super = build_int_2 (super_index, 0);
1109 TREE_TYPE (super) = ptr_type_node;
1112 /* Build and emit the array of implemented interfaces. */
1113 if (type != object_type_node)
1114 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1115 if (interface_len > 0)
1117 tree init = NULL_TREE;
1119 tree interface_array_type, idecl;
1120 interface_array_type
1121 = build_prim_array_type (class_ptr_type, interface_len);
1122 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1123 interface_array_type);
1124 for (i = interface_len; i > 0; i--)
1126 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1127 tree iclass = BINFO_TYPE (child);
1129 if (flag_assume_compiled)
1130 index = build_class_ref (iclass);
1133 int int_index = alloc_class_constant (iclass);
1134 index = build_int_2 (int_index, 0);
1135 TREE_TYPE (index) = ptr_type_node;
1137 init = tree_cons (NULL_TREE, index, init);
1139 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1141 TREE_STATIC (idecl) = 1;
1142 DECL_ARTIFICIAL (idecl) = 1;
1143 DECL_IGNORED_P (idecl) = 1;
1144 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1145 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1148 constant_pool_constructor = build_constants_constructor ();
1150 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1152 PUSH_FIELD_VALUE (temp, "vtable", NULL_TREE);
1154 PUSH_FIELD_VALUE (temp, "vtable",
1155 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1157 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1158 FINISH_RECORD_CONSTRUCTOR (temp);
1159 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1160 PUSH_SUPER_VALUE (cons, temp);
1161 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1162 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1163 PUSH_FIELD_VALUE (cons, "accflags",
1164 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1166 PUSH_FIELD_VALUE (cons, "superclass",
1167 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1168 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1169 PUSH_FIELD_VALUE (cons, "methods",
1170 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1171 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1172 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1173 PUSH_FIELD_VALUE (cons, "fields",
1174 fields_decl == NULL_TREE ? null_pointer_node
1175 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1176 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1177 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1178 PUSH_FIELD_VALUE (cons, "static_field_count",
1179 build_int_2 (static_field_count, 0));
1180 PUSH_FIELD_VALUE (cons, "vtable",
1181 dtable_decl == NULL_TREE ? null_pointer_node
1182 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1183 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1184 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1185 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1186 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1188 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1190 FINISH_RECORD_CONSTRUCTOR (cons);
1192 DECL_INITIAL (decl) = cons;
1193 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1202 /* Emit deferred inline methods. */
1203 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1204 method != NULL_TREE; method = TREE_CHAIN (method))
1206 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1208 /* It's a deferred inline method. Decide if we need to emit it. */
1209 if (flag_keep_inline_functions
1210 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (method))
1211 || ! METHOD_PRIVATE (method))
1213 temporary_allocation ();
1214 output_inline_function (method);
1215 permanent_allocation (1);
1220 make_class_data (current_class);
1222 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1225 /* Return 2 if CLASS is compiled by this compilation job;
1226 return 1 if CLASS can otherwise be assumed to be compiled;
1227 return 0 if we cannot assume that CLASS is compiled.
1228 Returns 1 for primitive and 0 for array types. */
1230 is_compiled_class (class)
1234 if (TREE_CODE (class) == POINTER_TYPE)
1235 class = TREE_TYPE (class);
1236 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1238 if (TYPE_ARRAY_P (class))
1240 if (class == current_class)
1243 seen_in_zip = (TYPE_LANG_SPECIFIC (class) && TYPE_LANG_SPECIFIC (class)->jcf
1244 && TYPE_LANG_SPECIFIC (class)->jcf->seen_in_zip);
1245 if (CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (class) || seen_in_zip)
1247 /* The class was seen in the current ZIP file and will be
1248 available as a compiled class in the future but may not have
1249 been loaded already. Load it if necessary. This prevent
1250 build_class_ref () from crashing. */
1252 if (seen_in_zip && !CLASS_LOADED_P (class))
1253 load_class (class, 1);
1255 /* We return 2 for class seen in ZIP and class from files
1256 belonging to the same compilation unit */
1260 if (flag_assume_compiled)
1262 if (!CLASS_LOADED_P (class))
1264 if (CLASS_FROM_SOURCE_P (class))
1265 safe_layout_class (class);
1267 load_class (class, 1);
1275 /* Append the mangled name of TYPE onto OBSTACK. */
1278 append_gpp_mangled_type (obstack, type)
1279 struct obstack *obstack;
1282 switch (TREE_CODE (type))
1285 case BOOLEAN_TYPE: code = 'b'; goto primitive;
1286 case CHAR_TYPE: code = 'w'; goto primitive;
1287 case VOID_TYPE: code = 'v'; goto primitive;
1289 /* Get the original type instead of the arguments promoted type.
1290 Avoid symbol name clashes. Should call a function to do that.
1292 if (type == promoted_short_type_node)
1293 type = short_type_node;
1294 if (type == promoted_byte_type_node)
1295 type = byte_type_node;
1296 switch (TYPE_PRECISION (type))
1298 case 8: code = 'c'; goto primitive;
1299 case 16: code = 's'; goto primitive;
1300 case 32: code = 'i'; goto primitive;
1301 case 64: code = 'x'; goto primitive;
1302 default: goto bad_type;
1305 obstack_1grow (obstack, code);
1308 switch (TYPE_PRECISION (type))
1310 case 32: code = 'f'; goto primitive;
1311 case 64: code = 'd'; goto primitive;
1312 default: goto bad_type;
1315 type = TREE_TYPE (type);
1316 obstack_1grow (obstack, 'P');
1318 if (TYPE_ARRAY_P (type))
1320 obstack_grow (obstack, "t6JArray1Z", sizeof("t6JArray1Z")-1);
1321 append_gpp_mangled_type (obstack, TYPE_ARRAY_ELEMENT (type));
1325 char *class_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1326 append_gpp_mangled_classtype (obstack, class_name);
1331 fatal ("internal error - trying to mangle unknown type");
1335 /* Build the mangled name of the `class' field. */
1338 mangle_class_field (class)
1342 obstack_grow (&temporary_obstack, "_CL_", 4);
1343 append_gpp_mangled_type (&temporary_obstack, class);
1344 obstack_1grow (&temporary_obstack, '\0');
1345 name = get_identifier (obstack_base (&temporary_obstack));
1346 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1350 /* Build the mangled (assembly-level) name of the static field FIELD. */
1353 mangle_static_field (field)
1356 tree class = DECL_CONTEXT (field);
1357 tree name = DECL_NAME (field);
1359 #if ! defined (NO_DOLLAR_IN_LABEL) || ! defined (NO_DOT_IN_LABEL)
1360 obstack_1grow (&temporary_obstack, '_');
1362 obstack_grow (&temporary_obstack, "__static_", 9);
1364 append_gpp_mangled_type (&temporary_obstack, class);
1365 encoded_len = unicode_mangling_length (IDENTIFIER_POINTER (name),
1366 IDENTIFIER_LENGTH (name));
1367 if (encoded_len > 0)
1369 obstack_1grow (&temporary_obstack, 'U');
1371 #ifndef NO_DOLLAR_IN_LABEL
1372 obstack_1grow (&temporary_obstack, '$');
1373 #else /* NO_DOLLAR_IN_LABEL */
1374 #ifndef NO_DOT_IN_LABEL
1375 obstack_1grow (&temporary_obstack, '.');
1376 #else /* NO_DOT_IN_LABEL */
1377 obstack_1grow (&temporary_obstack, '_');
1378 #endif /* NO_DOT_IN_LABEL */
1379 #endif /* NO_DOLLAR_IN_LABEL */
1380 if (encoded_len > 0)
1382 emit_unicode_mangled_name (&temporary_obstack,
1383 IDENTIFIER_POINTER (name),
1384 IDENTIFIER_LENGTH (name));
1388 obstack_grow (&temporary_obstack,
1389 IDENTIFIER_POINTER (name),
1390 IDENTIFIER_LENGTH (name));
1392 obstack_1grow (&temporary_obstack, '\0');
1393 name = get_identifier (obstack_base (&temporary_obstack));
1394 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1398 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1401 build_dtable_decl (type)
1405 obstack_grow (&temporary_obstack, "__vt_", 5);
1406 append_gpp_mangled_type (&temporary_obstack, type);
1407 obstack_1grow (&temporary_obstack, '\0');
1408 name = get_identifier (obstack_base (&temporary_obstack));
1409 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1410 return build_decl (VAR_DECL, name, dtable_type);
1413 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1414 fields inherited from SUPER_CLASS. */
1417 push_super_field (this_class, super_class)
1418 tree this_class, super_class;
1421 push_obstacks (&permanent_obstack, &permanent_obstack);
1422 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1424 DECL_IGNORED_P (base_decl) = 1;
1425 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1426 TYPE_FIELDS (this_class) = base_decl;
1427 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1430 /* Handle the different manners we may have to lay out a super class. */
1433 maybe_layout_super_class (super_class, this_class)
1437 if (TREE_CODE (super_class) == RECORD_TYPE)
1439 if (!CLASS_LOADED_P (super_class)
1440 && CLASS_FROM_SOURCE_P (super_class))
1441 safe_layout_class (super_class);
1442 if (!CLASS_LOADED_P (super_class))
1443 load_class (super_class, 1);
1445 /* We might have to layout the class before its dependency on
1446 the super class gets resolved by java_complete_class */
1447 else if (TREE_CODE (super_class) == POINTER_TYPE)
1449 if (TREE_TYPE (super_class) != NULL_TREE)
1450 super_class = TREE_TYPE (super_class);
1453 super_class = do_resolve_class (super_class, NULL_TREE, this_class);
1455 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1456 super_class = TREE_TYPE (super_class);
1459 if (!TYPE_SIZE (super_class))
1460 safe_layout_class (super_class);
1466 layout_class (this_class)
1469 tree super_class = CLASSTYPE_SUPER (this_class);
1474 super_class = maybe_layout_super_class (super_class, this_class);
1475 if (TREE_CODE (TYPE_SIZE (super_class)) == ERROR_MARK)
1477 TYPE_SIZE (this_class) = error_mark_node;
1480 if (TYPE_SIZE (this_class) == NULL_TREE)
1481 push_super_field (this_class, super_class);
1484 for (field = TYPE_FIELDS (this_class);
1485 field != NULL_TREE; field = TREE_CHAIN (field))
1487 if (FIELD_STATIC (field))
1489 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1490 DECL_ASSEMBLER_NAME (field) = mangle_static_field (field);
1494 layout_type (this_class);
1498 layout_class_methods (this_class)
1501 tree method_decl, dtable_count;
1502 tree super_class, handle_type;
1504 if (TYPE_NVIRTUALS (this_class))
1507 push_obstacks (&permanent_obstack, &permanent_obstack);
1508 super_class = CLASSTYPE_SUPER (this_class);
1509 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1513 super_class = maybe_layout_super_class (super_class, this_class);
1514 if (!TYPE_NVIRTUALS (super_class))
1515 layout_class_methods (super_class);
1516 dtable_count = TYPE_NVIRTUALS (super_class);
1519 dtable_count = integer_zero_node;
1521 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1523 for (method_decl = TYPE_METHODS (handle_type);
1524 method_decl; method_decl = TREE_CHAIN (method_decl))
1525 dtable_count = layout_class_method (this_class, super_class,
1526 method_decl, dtable_count);
1528 TYPE_NVIRTUALS (this_class) = dtable_count;
1530 #ifdef JAVA_USE_HANDLES
1531 layout_type (handle_type);
1536 /* Lay METHOD_DECL out, returning a possibly new value of
1540 layout_class_method (this_class, super_class, method_decl, dtable_count)
1541 tree this_class, super_class, method_decl, dtable_count;
1545 tree arg, arglist, t;
1546 int method_name_needs_escapes = 0;
1547 tree method_name = DECL_NAME (method_decl);
1548 int method_name_is_wfl =
1549 (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION);
1550 if (method_name_is_wfl)
1551 method_name = java_get_real_method_name (method_decl);
1553 if (method_name != init_identifier_node
1554 && method_name != finit_identifier_node)
1557 = unicode_mangling_length (IDENTIFIER_POINTER (method_name),
1558 IDENTIFIER_LENGTH (method_name));
1559 if (encoded_len > 0)
1561 method_name_needs_escapes = 1;
1562 emit_unicode_mangled_name (&temporary_obstack,
1563 IDENTIFIER_POINTER (method_name),
1564 IDENTIFIER_LENGTH (method_name));
1568 obstack_grow (&temporary_obstack,
1569 IDENTIFIER_POINTER (method_name),
1570 IDENTIFIER_LENGTH (method_name));
1574 obstack_grow (&temporary_obstack, "__", 2);
1575 if (method_name == finit_identifier_node)
1576 obstack_grow (&temporary_obstack, "finit", 5);
1577 append_gpp_mangled_type (&temporary_obstack, this_class);
1578 TREE_PUBLIC (method_decl) = 1;
1580 t = TREE_TYPE (method_decl);
1581 arglist = TYPE_ARG_TYPES (t);
1582 if (TREE_CODE (t) == METHOD_TYPE)
1583 arglist = TREE_CHAIN (arglist);
1584 for (arg = arglist; arg != end_params_node; )
1587 tree argtype = TREE_VALUE (arg);
1589 if (TREE_CODE (argtype) == POINTER_TYPE)
1591 /* This is O(N**2). Do we care? Cfr gcc/cp/method.c. */
1592 while (a != arg && argtype != TREE_VALUE (a))
1593 a = TREE_CHAIN (a), tindex++;
1603 arg = TREE_CHAIN (arg); nrepeats++;
1605 while (arg != end_params_node && argtype == TREE_VALUE (arg));
1608 obstack_1grow (&temporary_obstack, 'N');
1609 sprintf (buf, "%d", nrepeats);
1610 obstack_grow (&temporary_obstack, buf, strlen (buf));
1612 obstack_1grow (&temporary_obstack, '_');
1615 obstack_1grow (&temporary_obstack, 'T');
1616 sprintf (buf, "%d", tindex);
1617 obstack_grow (&temporary_obstack, buf, strlen (buf));
1619 obstack_1grow (&temporary_obstack, '_');
1623 append_gpp_mangled_type (&temporary_obstack, argtype);
1624 arg = TREE_CHAIN (arg);
1627 if (method_name_needs_escapes)
1628 obstack_1grow (&temporary_obstack, 'U');
1630 obstack_1grow (&temporary_obstack, '\0');
1631 asm_name = obstack_finish (&temporary_obstack);
1632 DECL_ASSEMBLER_NAME (method_decl) = get_identifier (asm_name);
1633 /* We don't generate a RTL for the method if it's abstract, or if
1634 it's an interface method that isn't clinit. */
1635 if (! METHOD_ABSTRACT (method_decl)
1636 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1637 && (IS_CLINIT (method_decl))))
1638 make_function_rtl (method_decl);
1639 obstack_free (&temporary_obstack, asm_name);
1641 if (method_name == init_identifier_node)
1643 char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1644 for (ptr = p; *ptr; )
1649 if (method_name_is_wfl)
1650 EXPR_WFL_NODE (DECL_NAME (method_decl)) = get_identifier (p);
1652 DECL_NAME (method_decl) = get_identifier (p);
1653 DECL_CONSTRUCTOR_P (method_decl) = 1;
1654 build_java_argument_signature (TREE_TYPE (method_decl));
1656 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1659 build_java_argument_signature (TREE_TYPE (method_decl));
1660 tree super_method = lookup_argument_method (super_class, method_name,
1662 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1664 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1665 if (DECL_VINDEX (method_decl) == NULL_TREE
1666 && !CLASS_FROM_SOURCE_P (this_class))
1667 error_with_decl (method_decl,
1668 "non-static method '%s' overrides static method");
1670 else if (TREE_TYPE (TREE_TYPE (method_decl))
1671 != TREE_TYPE (TREE_TYPE (super_method)))
1673 error_with_decl (method_decl,
1674 "Method `%s' redefined with different return type");
1675 error_with_decl (super_method,
1676 "Overridden decl is here");
1680 else if (! METHOD_FINAL (method_decl)
1681 && ! METHOD_PRIVATE (method_decl)
1682 && ! CLASS_FINAL (TYPE_NAME (this_class))
1685 DECL_VINDEX (method_decl) = dtable_count;
1686 dtable_count = build_int_2 (1+TREE_INT_CST_LOW (dtable_count), 0);
1689 return dtable_count;
1692 static tree registered_class = NULL_TREE;
1698 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1699 tree current = copy_node (node);
1701 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1702 if (!registered_class)
1703 registered_class = current;
1705 TREE_CHAIN (end) = current;
1710 /* Generate a function that gets called at start-up (static contructor) time,
1711 which calls registerClass for all the compiled classes. */
1714 emit_register_classes ()
1716 extern tree get_file_function_name PROTO((int));
1717 tree init_name = get_file_function_name ('I');
1718 tree init_type = build_function_type (void_type_node, end_params_node);
1723 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1724 DECL_ASSEMBLER_NAME (init_decl) = init_name;
1725 TREE_STATIC (init_decl) = 1;
1726 current_function_decl = init_decl;
1727 DECL_RESULT (init_decl) = build_decl(RESULT_DECL, NULL_TREE, void_type_node);
1728 /* DECL_EXTERNAL (init_decl) = 1;*/
1729 TREE_PUBLIC (init_decl) = 1;
1731 make_function_rtl (init_decl);
1732 init_function_start (init_decl, input_filename, 0);
1733 expand_function_start (init_decl, 0);
1735 for ( t = registered_class; t; t = TREE_CHAIN (t))
1736 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1737 XEXP (DECL_RTL (t), 0), Pmode);
1739 expand_function_end (input_filename, 0, 0);
1742 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1743 int saved_flag = flag_inline_functions;
1744 flag_inline_functions = 0;
1745 rest_of_compilation (init_decl);
1746 flag_inline_functions = saved_flag;
1748 current_function_decl = NULL_TREE;
1749 assemble_constructor (IDENTIFIER_POINTER (init_name));
1753 init_class_processing ()
1755 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");