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
));
42 static rtx registerClass_libfunc
;
44 extern struct obstack permanent_obstack
;
45 extern struct obstack temporary_obstack
;
47 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
48 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
49 Also, PREFIX is prepended, and SUFFIX is appended. */
52 ident_subst (old_name
, old_length
, prefix
, old_char
, new_char
, suffix
)
60 int prefix_len
= strlen (prefix
);
61 int suffix_len
= strlen (suffix
);
62 int i
= prefix_len
+ old_length
+ suffix_len
+ 1;
66 char *buffer
= (char *)alloca (i
);
68 strcpy (buffer
, prefix
);
69 for (i
= 0; i
< old_length
; i
++)
71 char ch
= old_name
[i
];
74 buffer
[prefix_len
+ i
] = ch
;
76 strcpy (buffer
+ prefix_len
+ old_length
, suffix
);
77 return get_identifier (buffer
);
80 /* Return an IDENTIFIER_NODE the same as OLD_ID,
81 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
82 Also, PREFIX is prepended, and SUFFIX is appended. */
85 identifier_subst (old_id
, prefix
, old_char
, new_char
, suffix
)
92 return ident_subst (IDENTIFIER_POINTER (old_id
), IDENTIFIER_LENGTH (old_id
),
93 prefix
, old_char
, new_char
, suffix
);
96 /* Generate a valid C identifier from the name of the class TYPE,
97 prefixed by PREFIX. */
100 mangled_classname (prefix
, type
)
104 tree ident
= TYPE_NAME (type
);
105 if (TREE_CODE (ident
) != IDENTIFIER_NODE
)
106 ident
= DECL_NAME (ident
);
107 return identifier_subst (ident
, prefix
, '.', '_', "");
114 push_obstacks (&permanent_obstack
, &permanent_obstack
);
115 type
= make_node (RECORD_TYPE
);
116 #ifdef JAVA_USE_HANDLES
117 tree field1
= build_decl (FIELD_DECL
, get_identifier ("obj"),
118 build_pointer_type (type
));
119 tree field2
= build_decl (FIELD_DECL
, get_identifier ("methods"),
120 methodtable_ptr_type
);
121 tree handle_type
= make_node (RECORD_TYPE
);
122 TREE_CHAIN (field1
) = field2
;
123 TYPE_FIELDS (handle_type
) = field1
;
124 TYPE_BINFO (type
) = make_tree_vec (7);
125 TYPE_BINFO (handle_type
) = make_tree_vec (7);
126 BINFO_HANDLE (TYPE_BINFO (handle_type
)) = type
;
127 BINFO_HANDLE (TYPE_BINFO (type
)) = handle_type
;
129 TYPE_BINFO (type
) = make_tree_vec (6);
137 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
138 and where each of the constituents is separated by '/',
139 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
142 unmangle_classname (name
, name_length
)
143 const char *name
; int name_length
;
145 tree to_return
= ident_subst (name
, name_length
, "", '/', '.', "");
146 if (to_return
!= get_identifier ((char *)name
))
147 QUALIFIED_P (to_return
) = 1;
152 push_class (class_type
, class_name
)
153 tree class_type
, class_name
;
155 tree decl
, signature
;
156 char *save_input_filename
= input_filename
;
157 int save_lineno
= lineno
;
158 tree source_name
= identifier_subst (class_name
, "", '.', '/', ".java");
159 push_obstacks (&permanent_obstack
, &permanent_obstack
);
160 input_filename
= IDENTIFIER_POINTER (source_name
);
162 decl
= build_decl (TYPE_DECL
, class_name
, class_type
);
163 input_filename
= save_input_filename
;
164 lineno
= save_lineno
;
165 signature
= identifier_subst (class_name
, "L", '.', '/', ";");
166 IDENTIFIER_SIGNATURE_TYPE (signature
) = build_pointer_type (class_type
);
168 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
169 both a typedef and in the struct name-space. We may want to re-visit
170 this later, but for now it reduces the changes needed for gdb. */
171 DECL_ARTIFICIAL (decl
) = 1;
173 pushdecl_top_level (decl
);
174 #ifdef JAVA_USE_HANDLES
176 tree handle_name
= identifier_subst (class_name
,
177 "Handle$", '.', '.', "");
178 tree handle_decl
= build_decl (TYPE_DECL
, handle_name
,
179 CLASS_TO_HANDLE_TYPE (class_type
));
180 pushdecl (handle_decl
);
188 /* Finds the (global) class named NAME. Creates the class if not found.
189 Also creates associated TYPE_DECL.
190 Does not check if the class actually exists, load the class,
191 fill in field or methods, or do layout_type. */
197 tree decl
= IDENTIFIER_CLASS_VALUE (name
);
198 if (decl
== NULL_TREE
)
199 decl
= push_class (make_class (), name
);
200 return TREE_TYPE (decl
);
204 set_super_info (access_flags
, this_class
, super_class
, interfaces_count
)
208 int interfaces_count
;
210 int total_supers
= interfaces_count
;
211 tree class_decl
= TYPE_NAME (this_class
);
215 push_obstacks (&permanent_obstack
, &permanent_obstack
);
216 TYPE_BINFO_BASETYPES (this_class
) = make_tree_vec (total_supers
);
219 tree super_binfo
= make_tree_vec (6);
220 BINFO_TYPE (super_binfo
) = super_class
;
221 BINFO_OFFSET (super_binfo
) = integer_zero_node
;
222 TREE_VIA_PUBLIC (super_binfo
) = 1;
223 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class
)), 0)
225 CLASS_HAS_SUPER (this_class
) = 1;
229 if (access_flags
& ACC_PUBLIC
) CLASS_PUBLIC (class_decl
) = 1;
230 if (access_flags
& ACC_FINAL
) CLASS_FINAL (class_decl
) = 1;
231 if (access_flags
& ACC_SUPER
) CLASS_SUPER (class_decl
) = 1;
232 if (access_flags
& ACC_INTERFACE
) CLASS_INTERFACE (class_decl
) = 1;
233 if (access_flags
& ACC_ABSTRACT
) CLASS_ABSTRACT (class_decl
) = 1;
236 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
237 direct sub-classes of Object are 1, and so on. */
244 if (! CLASS_LOADED_P (clas
))
245 load_class (clas
, 1);
246 while (clas
!= object_type_node
)
249 clas
= TYPE_BINFO_BASETYPE (clas
, 0);
254 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
257 interface_of_p (type1
, type2
)
263 if (!(basetype_vec
= TYPE_BINFO_BASETYPES (type2
)))
265 n
= TREE_VEC_LENGTH (basetype_vec
);
266 for (i
= 0; i
< n
; i
++)
268 tree vec_elt
= TREE_VEC_ELT (basetype_vec
, i
);
269 if (vec_elt
&& BINFO_TYPE (vec_elt
) == type1
)
272 for (i
= 0; i
< n
; i
++)
274 tree vec_elt
= TREE_VEC_ELT (basetype_vec
, i
);
275 if (vec_elt
&& BINFO_TYPE (vec_elt
)
276 && interface_of_p (type1
, BINFO_TYPE (vec_elt
)))
282 /* Return true iff TYPE1 inherits from TYPE2. */
285 inherits_from_p (type1
, type2
)
288 while (type1
!= NULL_TREE
&& TREE_CODE (type1
) == RECORD_TYPE
)
292 type1
= CLASSTYPE_SUPER (type1
);
298 add_interface_do (basetype_vec
, interface_class
, i
)
299 tree basetype_vec
, interface_class
;
302 tree interface_binfo
= make_tree_vec (6);
303 BINFO_TYPE (interface_binfo
) = interface_class
;
304 BINFO_OFFSET (interface_binfo
) = integer_zero_node
;
305 TREE_VIA_VIRTUAL (interface_binfo
) = 1;
306 TREE_VIA_PUBLIC (interface_binfo
) = 1;
307 TREE_VEC_ELT (basetype_vec
, i
) = interface_binfo
;
310 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
311 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
312 if attempt is made to add it twice. */
315 maybe_add_interface (this_class
, interface_class
)
316 tree this_class
, interface_class
;
318 tree basetype_vec
= TYPE_BINFO_BASETYPES (this_class
);
319 tree interface_binfo
= make_tree_vec (6);
321 int n
= TREE_VEC_LENGTH (basetype_vec
);
326 error ("internal error - too many interface type");
329 else if (TREE_VEC_ELT (basetype_vec
, i
) == NULL_TREE
)
331 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec
, i
)) == interface_class
)
332 return interface_class
;
334 add_interface_do (basetype_vec
, interface_class
, i
);
338 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
341 add_interface (this_class
, interface_class
)
342 tree this_class
, interface_class
;
344 tree basetype_vec
= TYPE_BINFO_BASETYPES (this_class
);
346 int n
= TREE_VEC_LENGTH (basetype_vec
);
351 error ("internal error - too many interface type");
354 else if (TREE_VEC_ELT (basetype_vec
, i
) == NULL_TREE
)
357 add_interface_do (basetype_vec
, interface_class
, i
);
361 /* Return the address of a pointer to the first FUNCTION_DECL
362 in the list (*LIST) whose DECL_NAME is NAME. */
365 find_named_method (list
, name
)
369 while (*list
&& DECL_NAME (*list
) != name
)
370 list
= &TREE_CHAIN (*list
);
376 build_java_method_type (fntype
, this_class
, access_flags
)
381 if (access_flags
& ACC_STATIC
)
383 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class
), fntype
);
387 add_method_1 (handle_class
, access_flags
, name
, function_type
)
393 tree method_type
, fndecl
;
394 push_obstacks (&permanent_obstack
, &permanent_obstack
);
396 method_type
= build_java_method_type (function_type
,
397 handle_class
, access_flags
);
399 fndecl
= build_decl (FUNCTION_DECL
, name
, method_type
);
400 DECL_CONTEXT (fndecl
) = handle_class
;
402 DECL_LANG_SPECIFIC (fndecl
)
403 = (struct lang_decl
*) permalloc (sizeof (struct lang_decl
));
404 bzero (DECL_LANG_SPECIFIC (fndecl
), sizeof (struct lang_decl
));
406 TREE_CHAIN (fndecl
) = TYPE_METHODS (handle_class
);
407 TYPE_METHODS (handle_class
) = fndecl
;
410 if (access_flags
& ACC_PUBLIC
) METHOD_PUBLIC (fndecl
) = 1;
411 if (access_flags
& ACC_PROTECTED
) METHOD_PROTECTED (fndecl
) = 1;
412 if (access_flags
& ACC_PRIVATE
) METHOD_PRIVATE (fndecl
) = 1;
413 if (access_flags
& ACC_NATIVE
) METHOD_NATIVE (fndecl
) = 1;
414 if (access_flags
& ACC_STATIC
) METHOD_STATIC (fndecl
) = 1;
415 if (access_flags
& ACC_FINAL
) METHOD_FINAL (fndecl
) = 1;
416 if (access_flags
& ACC_SYNCHRONIZED
) METHOD_SYNCHRONIZED (fndecl
) = 1;
417 if (access_flags
& ACC_ABSTRACT
) METHOD_ABSTRACT (fndecl
) = 1;
418 if (access_flags
& ACC_TRANSIENT
) METHOD_TRANSIENT (fndecl
) = 1;
422 /* Add a method to THIS_CLASS.
423 The method's name is NAME.
424 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
427 add_method (this_class
, access_flags
, name
, method_sig
)
433 tree handle_class
= CLASS_TO_HANDLE_TYPE (this_class
);
434 tree function_type
, fndecl
;
435 unsigned char *sig
= (unsigned char*)IDENTIFIER_POINTER (method_sig
);
436 push_obstacks (&permanent_obstack
, &permanent_obstack
);
438 fatal ("bad method signature");
439 function_type
= get_type_from_signature (method_sig
);
440 fndecl
= add_method_1 (handle_class
, access_flags
, name
, function_type
);
441 set_java_signature (TREE_TYPE (fndecl
), method_sig
);
447 add_field (class, name
, field_type
, flags
)
453 int is_static
= (flags
& ACC_STATIC
) != 0;
455 /* Push the obstack of field_type ? FIXME */
456 push_obstacks (&permanent_obstack
, &permanent_obstack
);
457 field
= build_decl (is_static
? VAR_DECL
: FIELD_DECL
, name
, field_type
);
459 TREE_CHAIN (field
) = TYPE_FIELDS (class);
460 TYPE_FIELDS (class) = field
;
461 DECL_CONTEXT (field
) = class;
463 if (flags
& ACC_PUBLIC
) FIELD_PUBLIC (field
) = 1;
464 if (flags
& ACC_PROTECTED
) FIELD_PROTECTED (field
) = 1;
465 if (flags
& ACC_PRIVATE
) FIELD_PRIVATE (field
) = 1;
466 if (flags
& ACC_FINAL
) FIELD_FINAL (field
) = 1;
467 if (flags
& ACC_VOLATILE
) FIELD_VOLATILE (field
) = 1;
468 if (flags
& ACC_TRANSIENT
) FIELD_TRANSIENT (field
) = 1;
471 FIELD_STATIC (field
) = 1;
472 /* Always make field externally visible. This is required so
473 that native methods can always access the field. */
474 TREE_PUBLIC (field
) = 1;
479 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
482 set_constant_value (field
, constant
)
483 tree field
, constant
;
485 if (field
== NULL_TREE
)
486 warning ("misplaced ConstantValue attribute (not in any field)");
487 else if (DECL_INITIAL (field
) != NULL_TREE
)
488 warning ("duplicate ConstanValue atribute for field '%s'",
489 IDENTIFIER_POINTER (DECL_NAME (field
)));
491 DECL_INITIAL (field
) = constant
;
494 /* Count the number of Unicode chars encoded in a given Ut8 string. */
497 strLengthUtf8 (str
, len
)
501 register unsigned char* ptr
= (unsigned char*) str
;
502 register unsigned char *limit
= ptr
+ len
;
504 for (; ptr
< limit
; str_length
++) {
505 if (UTF8_GET (ptr
, limit
) < 0)
512 /* Calculate a hash value for a string encoded in Utf8 format.
513 * This returns the same hash value as specified for java.lang.String.hashCode.
517 hashUtf8String (str
, len
)
521 register unsigned char* ptr
= (unsigned char*) str
;
522 register unsigned char *limit
= ptr
+ len
;
526 int ch
= UTF8_GET (ptr
, limit
);
527 /* Updated specification from
528 http://www.javasoft.com/docs/books/jls/clarify.html. */
529 hash
= (31 * hash
) + ch
;
534 tree utf8_decl_list
= NULL_TREE
;
537 build_utf8_ref (name
)
540 char* name_ptr
= IDENTIFIER_POINTER(name
);
541 int name_len
= IDENTIFIER_LENGTH(name
);
544 tree ctype
, field
, str_type
, cinit
, string
;
545 static int utf8_count
= 0;
547 tree ref
= IDENTIFIER_UTF8_REF (name
);
549 if (ref
!= NULL_TREE
)
552 push_obstacks (&permanent_obstack
, &permanent_obstack
);
553 ctype
= make_node (RECORD_TYPE
);
554 str_type
= build_prim_array_type (unsigned_byte_type_node
,
555 name_len
+ 1); /* Allow for final '\0'. */
556 PUSH_FIELD (ctype
, field
, "hash", unsigned_short_type_node
);
557 PUSH_FIELD (ctype
, field
, "length", unsigned_short_type_node
);
558 PUSH_FIELD (ctype
, field
, "data", str_type
);
559 FINISH_RECORD (ctype
);
560 START_RECORD_CONSTRUCTOR (cinit
, ctype
);
561 name_hash
= hashUtf8String (name_ptr
, name_len
) & 0xFFFF;
562 PUSH_FIELD_VALUE (cinit
, "hash", build_int_2 (name_hash
, 0));
563 PUSH_FIELD_VALUE (cinit
, "length", build_int_2 (name_len
, 0));
564 string
= build_string (name_len
, name_ptr
);
565 TREE_TYPE (string
) = str_type
;
566 PUSH_FIELD_VALUE (cinit
, "data", string
);
567 FINISH_RECORD_CONSTRUCTOR (cinit
);
569 /* Build a unique identifier based on buf. */
570 sprintf(buf
, "_Utf%d", ++utf8_count
);
571 buf_ptr
= &buf
[strlen (buf
)];
572 if (name_len
> 0 && name_ptr
[0] >= '0' && name_ptr
[0] <= '9')
574 while (--name_len
>= 0)
576 unsigned char c
= *name_ptr
++;
579 if (!ISALPHA(c
) && !ISDIGIT(c
))
582 if (buf_ptr
>= buf
+ 50)
587 decl
= build_decl (VAR_DECL
, get_identifier (buf
), utf8const_type
);
588 /* FIXME get some way to force this into .text, not .data. */
589 TREE_STATIC (decl
) = 1;
590 DECL_ARTIFICIAL (decl
) = 1;
591 DECL_IGNORED_P (decl
) = 1;
592 TREE_READONLY (decl
) = 1;
593 DECL_INITIAL (decl
) = cinit
;
594 TREE_CHAIN (decl
) = utf8_decl_list
;
595 layout_decl (decl
, 0);
597 rest_of_decl_compilation (decl
, (char*) 0, global_bindings_p (), 0);
598 utf8_decl_list
= decl
;
599 make_decl_rtl (decl
, (char*) 0, 1);
600 ref
= build1 (ADDR_EXPR
, utf8const_ptr_type
, decl
);
601 IDENTIFIER_UTF8_REF (name
) = ref
;
606 /* Build a reference to the class TYPE.
607 Also handles primitive types and array types. */
610 build_class_ref (type
)
613 int is_compiled
= is_compiled_class (type
);
616 tree ref
, decl_name
, decl
;
617 if (TREE_CODE (type
) == POINTER_TYPE
)
618 type
= TREE_TYPE (type
);
619 if (TREE_CODE (type
) == RECORD_TYPE
)
621 if (TYPE_SIZE (type
) == error_mark_node
)
622 return null_pointer_node
;
623 decl_name
= identifier_subst (DECL_NAME (TYPE_NAME (type
)),
624 "", '/', '/', ".class");
625 decl
= IDENTIFIER_GLOBAL_VALUE (decl_name
);
626 if (decl
== NULL_TREE
)
628 push_obstacks (&permanent_obstack
, &permanent_obstack
);
629 decl
= build_decl (VAR_DECL
, decl_name
, class_type_node
);
630 DECL_SIZE (decl
) = TYPE_SIZE (class_type_node
);
631 TREE_STATIC (decl
) = 1;
632 TREE_PUBLIC (decl
) = 1;
633 DECL_IGNORED_P (decl
) = 1;
634 DECL_ARTIFICIAL (decl
) = 1;
635 DECL_ASSEMBLER_NAME (decl
) = mangle_class_field (type
);
636 make_decl_rtl (decl
, NULL
, 1);
637 pushdecl_top_level (decl
);
638 if (is_compiled
== 1)
639 DECL_EXTERNAL (decl
) = 1;
647 if (flag_emit_class_files
)
649 const char *prim_class_name
;
651 if (type
== char_type_node
)
652 prim_class_name
= "java.lang.Character";
653 else if (type
== boolean_type_node
)
654 prim_class_name
= "java.lang.Boolean";
655 else if (type
== byte_type_node
)
656 prim_class_name
= "java.lang.Byte";
657 else if (type
== short_type_node
)
658 prim_class_name
= "java.lang.Short";
659 else if (type
== int_type_node
)
660 prim_class_name
= "java.lang.Integer";
661 else if (type
== long_type_node
)
662 prim_class_name
= "java.lang.Long";
663 else if (type
== float_type_node
)
664 prim_class_name
= "java.lang.Float";
665 else if (type
== double_type_node
)
666 prim_class_name
= "java.lang.Double";
667 else if (type
== void_type_node
)
668 prim_class_name
= "java.lang.Void";
670 fatal ("internal error - bad type to build_class_ref");
671 prim_class
= lookup_class (get_identifier (prim_class_name
));
672 return build (COMPONENT_REF
, NULL_TREE
,
673 prim_class
, TYPE_identifier_node
);
675 decl_name
= TYPE_NAME (type
);
676 if (TREE_CODE (decl_name
) == TYPE_DECL
)
677 decl_name
= DECL_NAME (decl_name
);
678 name
= IDENTIFIER_POINTER (decl_name
);
679 if (strncmp (name
, "promoted_", 9) == 0)
681 sprintf (buffer
, "_Jv_%sClass", name
);
682 decl_name
= get_identifier (buffer
);
683 decl
= IDENTIFIER_GLOBAL_VALUE (decl_name
);
684 if (decl
== NULL_TREE
)
686 push_obstacks (&permanent_obstack
, &permanent_obstack
);
687 decl
= build_decl (VAR_DECL
, decl_name
, class_type_node
);
688 TREE_STATIC (decl
) = 1;
689 TREE_PUBLIC (decl
) = 1;
690 make_decl_rtl (decl
, NULL
, 1);
691 pushdecl_top_level (decl
);
692 if (is_compiled
== 1)
693 DECL_EXTERNAL (decl
) = 1;
698 ref
= build1 (ADDR_EXPR
, class_ptr_type
, decl
);
705 push_obstacks (&permanent_obstack
, &permanent_obstack
);
706 index
= alloc_class_constant (type
);
707 cl
= build_ref_from_constant_pool (index
);
708 TREE_TYPE (cl
) = promote_type (class_ptr_type
);
715 build_static_field_ref (fdecl
)
718 tree fclass
= DECL_CONTEXT (fdecl
);
719 int is_compiled
= is_compiled_class (fclass
);
722 if (DECL_RTL (fdecl
) == 0)
724 push_obstacks (&permanent_obstack
, &permanent_obstack
);
725 make_decl_rtl (fdecl
, NULL
, 1);
727 if (is_compiled
== 1)
728 DECL_EXTERNAL (fdecl
) = 1;
735 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr
737 static tree fields_ident
= NULL_TREE
;
738 static tree info_ident
= NULL_TREE
;
739 tree ref
= build_class_ref (fclass
);
742 ref
= build1 (INDIRECT_REF
, class_type_node
, ref
);
743 if (fields_ident
== NULL_TREE
)
744 fields_ident
= get_identifier ("fields");
745 if (info_ident
== NULL_TREE
)
746 info_ident
= get_identifier ("info");
747 ref
= build (COMPONENT_REF
, field_ptr_type_node
, ref
,
748 lookup_field (&class_type_node
, fields_ident
));
750 for (fld
= TYPE_FIELDS (fclass
); ; fld
= TREE_CHAIN (fld
))
754 if (fld
== NULL_TREE
)
755 fatal ("field '%s' not found in class",
756 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
757 if (FIELD_STATIC (fld
))
760 field_index
*= int_size_in_bytes (field_type_node
);
761 ref
= fold (build (PLUS_EXPR
, field_ptr_type_node
,
762 ref
, build_int_2 (field_index
, 0)));
763 ref
= build1 (INDIRECT_REF
, field_type_node
, ref
);
764 ref
= build (COMPONENT_REF
, field_info_union_node
,
765 ref
, lookup_field (&field_type_node
, info_ident
));
766 ref
= build (COMPONENT_REF
, ptr_type_node
,
767 ref
, TREE_CHAIN (TYPE_FIELDS (field_info_union_node
)));
768 return fold (build1 (INDIRECT_REF
, TREE_TYPE(fdecl
), ref
));
773 get_access_flags_from_decl (decl
)
776 int access_flags
= 0;
777 if (TREE_CODE (decl
) == FIELD_DECL
|| TREE_CODE (decl
) == VAR_DECL
)
779 if (FIELD_STATIC (decl
))
780 access_flags
|= ACC_STATIC
;
781 if (FIELD_PUBLIC (decl
))
782 access_flags
|= ACC_PUBLIC
;
783 if (FIELD_PROTECTED (decl
))
784 access_flags
|= ACC_PROTECTED
;
785 if (FIELD_PRIVATE (decl
))
786 access_flags
|= ACC_PRIVATE
;
787 if (FIELD_FINAL (decl
))
788 access_flags
|= ACC_FINAL
;
789 if (FIELD_VOLATILE (decl
))
790 access_flags
|= ACC_VOLATILE
;
791 if (FIELD_TRANSIENT (decl
))
792 access_flags
|= ACC_TRANSIENT
;
795 if (TREE_CODE (decl
) == TYPE_DECL
)
797 if (CLASS_PUBLIC (decl
))
798 access_flags
|= ACC_PUBLIC
;
799 if (CLASS_FINAL (decl
))
800 access_flags
|= ACC_FINAL
;
801 if (CLASS_SUPER (decl
))
802 access_flags
|= ACC_SUPER
;
803 if (CLASS_INTERFACE (decl
))
804 access_flags
|= ACC_INTERFACE
;
805 if (CLASS_ABSTRACT (decl
))
806 access_flags
|= ACC_ABSTRACT
;
809 if (TREE_CODE (decl
) == FUNCTION_DECL
)
811 if (METHOD_PUBLIC (decl
))
812 access_flags
|= ACC_PUBLIC
;
813 if (METHOD_PRIVATE (decl
))
814 access_flags
|= ACC_PRIVATE
;
815 if (METHOD_PROTECTED (decl
))
816 access_flags
|= ACC_PROTECTED
;
817 if (METHOD_STATIC (decl
))
818 access_flags
|= ACC_STATIC
;
819 if (METHOD_FINAL (decl
))
820 access_flags
|= ACC_FINAL
;
821 if (METHOD_SYNCHRONIZED (decl
))
822 access_flags
|= ACC_SYNCHRONIZED
;
823 if (METHOD_NATIVE (decl
))
824 access_flags
|= ACC_NATIVE
;
825 if (METHOD_ABSTRACT (decl
))
826 access_flags
|= ACC_ABSTRACT
;
827 if (METHOD_TRANSIENT (decl
))
828 access_flags
|= ACC_TRANSIENT
;
835 make_field_value (tree fdecl
)
839 tree type
= TREE_TYPE (fdecl
);
840 int resolved
= is_compiled_class (type
);
841 START_RECORD_CONSTRUCTOR (finit
, field_type_node
);
842 PUSH_FIELD_VALUE (finit
, "name", build_utf8_ref (DECL_NAME (fdecl
)));
844 type
= build_class_ref (type
);
847 tree signature
= build_java_signature (type
);
848 type
= build_utf8_ref (unmangle_classname
849 (IDENTIFIER_POINTER(signature
),
850 IDENTIFIER_LENGTH(signature
)));
852 PUSH_FIELD_VALUE (finit
, "type", type
);
853 flags
= get_access_flags_from_decl (fdecl
);
855 flags
|= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
856 PUSH_FIELD_VALUE (finit
, "accflags", build_int_2 (flags
, 0));
857 bsize
= TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (fdecl
))) / BITS_PER_UNIT
;
858 PUSH_FIELD_VALUE (finit
, "bsize", build_int_2 (bsize
, 0));
859 if (FIELD_STATIC (fdecl
))
861 tree cfield
= TREE_CHAIN (TYPE_FIELDS(field_info_union_node
));
862 tree faddr
= build_address_of (build_static_field_ref (fdecl
));
863 info
= build (CONSTRUCTOR
, field_info_union_node
, NULL_TREE
,
864 build_tree_list (cfield
, faddr
));
869 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (fdecl
)) / BITS_PER_UNIT
;
870 info
= build (CONSTRUCTOR
, field_info_union_node
, NULL_TREE
,
871 build_tree_list (TYPE_FIELDS(field_info_union_node
),
872 build_int_2 (boffset
, 0)));
874 PUSH_FIELD_VALUE (finit
, "info", info
);
876 FINISH_RECORD_CONSTRUCTOR (finit
);
881 make_method_value (mdecl
)
886 #define ACC_TRANSLATED 0x4000
887 int accflags
= get_access_flags_from_decl (mdecl
) | ACC_TRANSLATED
;
888 code
= null_pointer_node
;
889 if (DECL_RTL (mdecl
))
890 code
= build1 (ADDR_EXPR
, nativecode_ptr_type_node
, mdecl
);
891 START_RECORD_CONSTRUCTOR (minit
, method_type_node
);
892 PUSH_FIELD_VALUE (minit
, "name",
893 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl
) ?
895 : DECL_NAME (mdecl
)));
897 tree signature
= build_java_signature (TREE_TYPE (mdecl
));
898 PUSH_FIELD_VALUE (minit
, "signature",
901 (IDENTIFIER_POINTER(signature
),
902 IDENTIFIER_LENGTH(signature
)))));
904 PUSH_FIELD_VALUE (minit
, "accflags", build_int_2 (accflags
, 0));
905 PUSH_FIELD_VALUE (minit
, "ncode", code
);
906 FINISH_RECORD_CONSTRUCTOR (minit
);
911 get_dispatch_vector (type
)
914 tree vtable
= TYPE_VTABLE (type
);
919 tree super
= CLASSTYPE_SUPER (type
);
920 int nvirtuals
= TREE_INT_CST_LOW (TYPE_NVIRTUALS (type
));
921 vtable
= make_tree_vec (nvirtuals
);
922 TYPE_VTABLE (type
) = vtable
;
923 if (super
!= NULL_TREE
)
925 tree super_vtable
= get_dispatch_vector (super
);
926 for ( i
= TREE_INT_CST_LOW (TYPE_NVIRTUALS (super
)); --i
>= 0; )
927 TREE_VEC_ELT (vtable
, i
) = TREE_VEC_ELT (super_vtable
, i
);
929 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
930 method
= TREE_CHAIN (method
))
932 if (DECL_VINDEX (method
) != NULL_TREE
933 && TREE_CODE (DECL_VINDEX (method
)) == INTEGER_CST
)
935 TREE_VEC_ELT (vtable
, TREE_INT_CST_LOW (DECL_VINDEX (method
)))
944 get_dispatch_table (type
, this_class_addr
)
945 tree type
, this_class_addr
;
947 tree vtable
= get_dispatch_vector (type
);
949 tree list
= NULL_TREE
;
950 int nvirtuals
= TREE_VEC_LENGTH (vtable
);
951 for (i
= nvirtuals
; --i
>= 0; )
953 tree method
= TREE_VEC_ELT (vtable
, i
);
954 if (METHOD_ABSTRACT (method
))
955 warning_with_decl (method
, "abstract method in non-abstract class");
956 if (DECL_RTL (method
) == 0)
957 make_decl_rtl (method
, NULL
, 1);
958 list
= tree_cons (NULL_TREE
/*DECL_VINDEX (method) + 2*/,
959 build1 (ADDR_EXPR
, nativecode_ptr_type_node
, method
),
962 /* Dummy entry for compatibility with G++ -fvtable-thunks. */
963 list
= tree_cons (integer_zero_node
, null_pointer_node
, list
);
964 list
= tree_cons (integer_zero_node
, this_class_addr
, list
);
965 return build (CONSTRUCTOR
, build_prim_array_type (nativecode_ptr_type_node
,
971 make_class_data (type
)
974 tree decl
, cons
, temp
;
975 tree field
, fields_decl
;
976 tree static_fields
= NULL_TREE
;
977 tree instance_fields
= NULL_TREE
;
978 HOST_WIDE_INT static_field_count
= 0;
979 HOST_WIDE_INT instance_field_count
= 0;
980 HOST_WIDE_INT field_count
;
981 tree field_array_type
;
983 tree methods
= NULL_TREE
;
984 tree dtable_decl
= NULL_TREE
;
985 HOST_WIDE_INT method_count
= 0;
986 tree method_array_type
;
989 tree this_class_addr
;
990 tree constant_pool_constructor
;
991 tree interfaces
= null_pointer_node
;
992 int interface_len
= 0;
993 tree type_decl
= TYPE_NAME (type
);
995 this_class_addr
= build_class_ref (type
);
996 decl
= TREE_OPERAND (this_class_addr
, 0);
998 /* Build Field array. */
999 field
= TYPE_FIELDS (type
);
1000 if (DECL_NAME (field
) == NULL_TREE
)
1001 field
= TREE_CHAIN (field
); /* Skip dummy field for inherited data. */
1002 for ( ; field
!= NULL_TREE
; field
= TREE_CHAIN (field
))
1004 if (! DECL_ARTIFICIAL (field
))
1006 tree init
= make_field_value (field
);
1007 if (FIELD_STATIC (field
))
1009 static_field_count
++;
1010 static_fields
= tree_cons (NULL_TREE
, init
, static_fields
);
1011 rest_of_decl_compilation (field
, (char*) 0, 1, 1);
1015 instance_field_count
++;
1016 instance_fields
= tree_cons (NULL_TREE
, init
, instance_fields
);
1020 field_count
= static_field_count
+ instance_field_count
;
1021 if (field_count
> 0)
1023 static_fields
= nreverse (static_fields
);
1024 instance_fields
= nreverse (instance_fields
);
1025 static_fields
= chainon (static_fields
, instance_fields
);
1026 field_array_type
= build_prim_array_type (field_type_node
, field_count
);
1027 fields_decl
= build_decl (VAR_DECL
, mangled_classname ("_FL_", type
),
1029 DECL_INITIAL (fields_decl
) = build (CONSTRUCTOR
, field_array_type
,
1030 NULL_TREE
, static_fields
);
1031 TREE_STATIC (fields_decl
) = 1;
1032 DECL_ARTIFICIAL (fields_decl
) = 1;
1033 DECL_IGNORED_P (fields_decl
) = 1;
1034 rest_of_decl_compilation (fields_decl
, (char*) 0, 1, 0);
1037 fields_decl
= NULL_TREE
;
1039 /* Build Method array. */
1040 for (method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type
));
1041 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
1044 if (METHOD_PRIVATE (method
)
1045 && (flag_inline_functions
|| optimize
))
1047 init
= make_method_value (method
);
1049 methods
= tree_cons (NULL_TREE
, init
, methods
);
1051 method_array_type
= build_prim_array_type (method_type_node
, method_count
);
1052 methods_decl
= build_decl (VAR_DECL
, mangled_classname ("_MT_", type
),
1054 DECL_INITIAL (methods_decl
) = build (CONSTRUCTOR
, method_array_type
,
1055 NULL_TREE
, nreverse (methods
));
1056 TREE_STATIC (methods_decl
) = 1;
1057 DECL_ARTIFICIAL (methods_decl
) = 1;
1058 DECL_IGNORED_P (methods_decl
) = 1;
1059 rest_of_decl_compilation (methods_decl
, (char*) 0, 1, 0);
1061 if (flag_assume_compiled
1062 && ! CLASS_ABSTRACT (type_decl
) && ! CLASS_INTERFACE (type_decl
))
1064 tree dtable
= get_dispatch_table (type
, this_class_addr
);
1065 dtable_decl
= build_dtable_decl (type
);
1066 DECL_INITIAL (dtable_decl
) = dtable
;
1067 TREE_STATIC (dtable_decl
) = 1;
1068 DECL_ARTIFICIAL (dtable_decl
) = 1;
1069 DECL_IGNORED_P (dtable_decl
) = 1;
1070 TREE_PUBLIC (dtable_decl
) = 1;
1071 rest_of_decl_compilation (dtable_decl
, (char*) 0, 1, 0);
1074 super
= CLASSTYPE_SUPER (type
);
1075 if (super
== NULL_TREE
)
1076 super
= null_pointer_node
;
1077 else if (flag_assume_compiled
)
1078 super
= build_class_ref (super
);
1081 int super_index
= alloc_class_constant (super
);
1082 super
= build_int_2 (super_index
, 0);
1083 TREE_TYPE (super
) == ptr_type_node
;
1086 /* Build and emit the array of implemented interfaces. */
1087 if (type
!= object_type_node
)
1088 interface_len
= TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type
)) - 1;
1089 if (interface_len
> 0)
1091 tree init
= NULL_TREE
;
1093 tree interface_array_type
, idecl
;
1094 interface_array_type
1095 = build_prim_array_type (class_ptr_type
, interface_len
);
1096 idecl
= build_decl (VAR_DECL
, mangled_classname ("_IF_", type
),
1097 interface_array_type
);
1098 for (i
= interface_len
; i
> 0; i
--)
1100 tree child
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), i
);
1101 tree iclass
= BINFO_TYPE (child
);
1103 if (flag_assume_compiled
)
1104 index
= build_class_ref (iclass
);
1107 int int_index
= alloc_class_constant (iclass
);
1108 index
= build_int_2 (int_index
, 0);
1109 TREE_TYPE (index
) == ptr_type_node
;
1111 init
= tree_cons (NULL_TREE
, index
, init
);
1113 DECL_INITIAL (idecl
) = build (CONSTRUCTOR
, interface_array_type
,
1115 TREE_STATIC (idecl
) = 1;
1116 DECL_ARTIFICIAL (idecl
) = 1;
1117 DECL_IGNORED_P (idecl
) = 1;
1118 interfaces
= build1 (ADDR_EXPR
, ptr_type_node
, idecl
);
1119 rest_of_decl_compilation (idecl
, (char*) 0, 1, 0);
1122 constant_pool_constructor
= build_constants_constructor ();
1124 START_RECORD_CONSTRUCTOR (temp
, object_type_node
);
1126 PUSH_FIELD_VALUE (temp
, "vtable", NULL_TREE
);
1128 PUSH_FIELD_VALUE (temp
, "vtable",
1129 build1 (ADDR_EXPR
, dtable_ptr_type
, class_dtable_decl
));
1131 PUSH_FIELD_VALUE (temp
, "sync_info", null_pointer_node
);
1132 FINISH_RECORD_CONSTRUCTOR (temp
);
1133 START_RECORD_CONSTRUCTOR (cons
, class_type_node
);
1134 PUSH_SUPER_VALUE (cons
, temp
);
1135 PUSH_FIELD_VALUE (cons
, "next", null_pointer_node
);
1136 PUSH_FIELD_VALUE (cons
, "name", build_utf8_ref (DECL_NAME (type_decl
)));
1137 PUSH_FIELD_VALUE (cons
, "accflags",
1138 build_int_2 (get_access_flags_from_decl (type_decl
), 0));
1140 PUSH_FIELD_VALUE (cons
, "superclass",
1141 CLASS_INTERFACE (type_decl
) ? null_pointer_node
: super
);
1142 PUSH_FIELD_VALUE (cons
, "constants", constant_pool_constructor
);
1143 PUSH_FIELD_VALUE (cons
, "methods",
1144 build1 (ADDR_EXPR
, method_ptr_type_node
, methods_decl
));
1145 PUSH_FIELD_VALUE (cons
, "method_count", build_int_2 (method_count
, 0));
1146 PUSH_FIELD_VALUE (cons
, "vtable_method_count", TYPE_NVIRTUALS (type
));
1147 PUSH_FIELD_VALUE (cons
, "fields",
1148 fields_decl
== NULL_TREE
? null_pointer_node
1149 : build1 (ADDR_EXPR
, field_ptr_type_node
, fields_decl
));
1150 PUSH_FIELD_VALUE (cons
, "size_in_bytes", size_in_bytes (type
));
1151 PUSH_FIELD_VALUE (cons
, "field_count", build_int_2 (field_count
, 0));
1152 PUSH_FIELD_VALUE (cons
, "static_field_count",
1153 build_int_2 (static_field_count
, 0));
1154 PUSH_FIELD_VALUE (cons
, "vtable",
1155 dtable_decl
== NULL_TREE
? null_pointer_node
1156 : build1 (ADDR_EXPR
, dtable_ptr_type
, dtable_decl
));
1157 PUSH_FIELD_VALUE (cons
, "interfaces", interfaces
);
1158 PUSH_FIELD_VALUE (cons
, "loader", null_pointer_node
);
1159 PUSH_FIELD_VALUE (cons
, "interface_count", build_int_2 (interface_len
, 0));
1160 PUSH_FIELD_VALUE (cons
, "state", integer_zero_node
);
1162 PUSH_FIELD_VALUE (cons
, "thread", null_pointer_node
);
1164 FINISH_RECORD_CONSTRUCTOR (cons
);
1166 DECL_INITIAL (decl
) = cons
;
1167 rest_of_decl_compilation (decl
, (char*) 0, 1, 0);
1176 /* Emit deferred inline methods. */
1177 for ( method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class
));
1178 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
1180 if (! TREE_ASM_WRITTEN (method
) && DECL_SAVED_INSNS (method
) != 0)
1182 /* It's a deferred inline method. Decide if we need to emit it. */
1183 if (flag_keep_inline_functions
1184 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (method
))
1185 || ! METHOD_PRIVATE (method
))
1187 temporary_allocation ();
1188 output_inline_function (method
);
1189 permanent_allocation (1);
1194 make_class_data (current_class
);
1196 rest_of_decl_compilation (TYPE_NAME (current_class
), (char*) 0, 1, 0);
1199 /* Return 2 if CLASS is compiled by this compilation job;
1200 return 1 if CLASS can otherwise be assumed to be compiled;
1201 return 0 if we cannot assume that CLASS is compiled.
1202 Returns 1 for primitive and 0 for array types. */
1204 is_compiled_class (class)
1208 if (TREE_CODE (class) == POINTER_TYPE
)
1209 class = TREE_TYPE (class);
1210 if (TREE_CODE (class) != RECORD_TYPE
) /* Primitive types are static. */
1212 if (TYPE_ARRAY_P (class))
1214 if (class == current_class
)
1217 seen_in_zip
= (TYPE_LANG_SPECIFIC (class) && TYPE_LANG_SPECIFIC (class)->jcf
1218 && TYPE_LANG_SPECIFIC (class)->jcf
->seen_in_zip
);
1219 if (CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (class) || seen_in_zip
)
1221 /* The class was seen in the current ZIP file and will be
1222 available as a compiled class in the future but may not have
1223 been loaded already. Load it if necessary. This prevent
1224 build_class_ref () from crashing. */
1226 if (seen_in_zip
&& !CLASS_LOADED_P (class))
1227 load_class (class, 1);
1229 /* We return 2 for class seen in ZIP and class from files
1230 belonging to the same compilation unit */
1234 if (flag_assume_compiled
)
1236 if (!CLASS_LOADED_P (class))
1238 if (CLASS_FROM_SOURCE_P (class))
1239 safe_layout_class (class);
1241 load_class (class, 1);
1249 /* Append the mangled name of TYPE onto OBSTACK. */
1252 append_gpp_mangled_type (obstack
, type
)
1253 struct obstack
*obstack
;
1256 switch (TREE_CODE (type
))
1259 case BOOLEAN_TYPE
: code
= 'b'; goto primitive
;
1260 case CHAR_TYPE
: code
= 'w'; goto primitive
;
1261 case VOID_TYPE
: code
= 'v'; goto primitive
;
1263 /* Get the original type instead of the arguments promoted type.
1264 Avoid symbol name clashes. Should call a function to do that.
1266 if (type
== promoted_short_type_node
)
1267 type
= short_type_node
;
1268 if (type
== promoted_byte_type_node
)
1269 type
= byte_type_node
;
1270 switch (TYPE_PRECISION (type
))
1272 case 8: code
= 'c'; goto primitive
;
1273 case 16: code
= 's'; goto primitive
;
1274 case 32: code
= 'i'; goto primitive
;
1275 case 64: code
= 'x'; goto primitive
;
1276 default: goto bad_type
;
1279 obstack_1grow (obstack
, code
);
1282 switch (TYPE_PRECISION (type
))
1284 case 32: code
= 'f'; goto primitive
;
1285 case 64: code
= 'd'; goto primitive
;
1286 default: goto bad_type
;
1289 type
= TREE_TYPE (type
);
1290 obstack_1grow (obstack
, 'P');
1292 if (TYPE_ARRAY_P (type
))
1294 obstack_grow (obstack
, "t6JArray1Z", sizeof("t6JArray1Z")-1);
1295 append_gpp_mangled_type (obstack
, TYPE_ARRAY_ELEMENT (type
));
1299 char *class_name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
1300 append_gpp_mangled_classtype (obstack
, class_name
);
1305 fatal ("internal error - trying to mangle unknown type");
1309 /* Build the mangled name of the `class' field. */
1312 mangle_class_field (class)
1316 obstack_grow (&temporary_obstack
, "_CL_", 4);
1317 append_gpp_mangled_type (&temporary_obstack
, class);
1318 obstack_1grow (&temporary_obstack
, '\0');
1319 name
= get_identifier (obstack_base (&temporary_obstack
));
1320 obstack_free (&temporary_obstack
, obstack_base (&temporary_obstack
));
1324 /* Build the mangled (assembly-level) name of the static field FIELD. */
1327 mangle_static_field (field
)
1330 tree
class = DECL_CONTEXT (field
);
1331 tree name
= DECL_NAME (field
);
1333 #if ! defined (NO_DOLLAR_IN_LABEL) || ! defined (NO_DOT_IN_LABEL)
1334 obstack_1grow (&temporary_obstack
, '_');
1336 obstack_grow (&temporary_obstack
, "__static_", 9);
1338 append_gpp_mangled_type (&temporary_obstack
, class);
1339 encoded_len
= unicode_mangling_length (IDENTIFIER_POINTER (name
),
1340 IDENTIFIER_LENGTH (name
));
1341 if (encoded_len
> 0)
1343 obstack_1grow (&temporary_obstack
, 'U');
1345 #ifndef NO_DOLLAR_IN_LABEL
1346 obstack_1grow (&temporary_obstack
, '$');
1347 #else /* NO_DOLLAR_IN_LABEL */
1348 #ifndef NO_DOT_IN_LABEL
1349 obstack_1grow (&temporary_obstack
, '.');
1350 #else /* NO_DOT_IN_LABEL */
1351 obstack_1grow (&temporary_obstack
, '_');
1352 #endif /* NO_DOT_IN_LABEL */
1353 #endif /* NO_DOLLAR_IN_LABEL */
1354 if (encoded_len
> 0)
1356 emit_unicode_mangled_name (&temporary_obstack
,
1357 IDENTIFIER_POINTER (name
),
1358 IDENTIFIER_LENGTH (name
));
1362 obstack_grow (&temporary_obstack
,
1363 IDENTIFIER_POINTER (name
),
1364 IDENTIFIER_LENGTH (name
));
1366 obstack_1grow (&temporary_obstack
, '\0');
1367 name
= get_identifier (obstack_base (&temporary_obstack
));
1368 obstack_free (&temporary_obstack
, obstack_base (&temporary_obstack
));
1372 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1375 build_dtable_decl (type
)
1379 obstack_grow (&temporary_obstack
, "__vt_", 5);
1380 append_gpp_mangled_type (&temporary_obstack
, type
);
1381 obstack_1grow (&temporary_obstack
, '\0');
1382 name
= get_identifier (obstack_base (&temporary_obstack
));
1383 obstack_free (&temporary_obstack
, obstack_base (&temporary_obstack
));
1384 return build_decl (VAR_DECL
, name
, dtable_type
);
1387 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1388 fields inherited from SUPER_CLASS. */
1391 push_super_field (this_class
, super_class
)
1392 tree this_class
, super_class
;
1395 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1396 base_decl
= build_decl (FIELD_DECL
, NULL_TREE
, super_class
);
1398 DECL_IGNORED_P (base_decl
) = 1;
1399 TREE_CHAIN (base_decl
) = TYPE_FIELDS (this_class
);
1400 TYPE_FIELDS (this_class
) = base_decl
;
1401 DECL_SIZE (base_decl
) = TYPE_SIZE (super_class
);
1404 /* Handle the different manners we may have to lay out a super class. */
1407 maybe_layout_super_class (super_class
, this_class
)
1411 if (TREE_CODE (super_class
) == RECORD_TYPE
)
1413 if (!CLASS_LOADED_P (super_class
)
1414 && CLASS_FROM_SOURCE_P (super_class
))
1415 safe_layout_class (super_class
);
1416 if (!CLASS_LOADED_P (super_class
))
1417 load_class (super_class
, 1);
1419 /* We might have to layout the class before its dependency on
1420 the super class gets resolved by java_complete_class */
1421 else if (TREE_CODE (super_class
) == POINTER_TYPE
)
1423 if (TREE_TYPE (super_class
) != NULL_TREE
)
1424 super_class
= TREE_TYPE (super_class
);
1427 super_class
= do_resolve_class (super_class
, NULL_TREE
, this_class
);
1429 return NULL_TREE
; /* FIXME, NULL_TREE not checked by caller. */
1430 super_class
= TREE_TYPE (super_class
);
1433 if (!TYPE_SIZE (super_class
))
1434 safe_layout_class (super_class
);
1440 layout_class (this_class
)
1443 tree super_class
= CLASSTYPE_SUPER (this_class
);
1448 super_class
= maybe_layout_super_class (super_class
, this_class
);
1449 if (TREE_CODE (TYPE_SIZE (super_class
)) == ERROR_MARK
)
1451 TYPE_SIZE (this_class
) = error_mark_node
;
1454 if (TYPE_SIZE (this_class
) == NULL_TREE
)
1455 push_super_field (this_class
, super_class
);
1458 for (field
= TYPE_FIELDS (this_class
);
1459 field
!= NULL_TREE
; field
= TREE_CHAIN (field
))
1461 if (FIELD_STATIC (field
))
1463 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1464 DECL_ASSEMBLER_NAME (field
) = mangle_static_field (field
);
1468 layout_type (this_class
);
1472 layout_class_methods (this_class
)
1475 tree method_decl
, dtable_count
;
1476 tree super_class
, handle_type
;
1478 if (TYPE_NVIRTUALS (this_class
))
1481 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1482 super_class
= CLASSTYPE_SUPER (this_class
);
1483 handle_type
= CLASS_TO_HANDLE_TYPE (this_class
);
1487 super_class
= maybe_layout_super_class (super_class
, this_class
);
1488 if (!TYPE_NVIRTUALS (super_class
))
1489 layout_class_methods (super_class
);
1490 dtable_count
= TYPE_NVIRTUALS (super_class
);
1493 dtable_count
= integer_zero_node
;
1495 TYPE_METHODS (handle_type
) = nreverse (TYPE_METHODS (handle_type
));
1497 for (method_decl
= TYPE_METHODS (handle_type
);
1498 method_decl
; method_decl
= TREE_CHAIN (method_decl
))
1499 dtable_count
= layout_class_method (this_class
, super_class
,
1500 method_decl
, dtable_count
);
1502 TYPE_NVIRTUALS (this_class
) = dtable_count
;
1504 #ifdef JAVA_USE_HANDLES
1505 layout_type (handle_type
);
1510 /* Lay METHOD_DECL out, returning a possibly new value of
1514 layout_class_method (this_class
, super_class
, method_decl
, dtable_count
)
1515 tree this_class
, super_class
, method_decl
, dtable_count
;
1519 tree arg
, arglist
, t
;
1520 int method_name_needs_escapes
= 0;
1521 tree method_name
= DECL_NAME (method_decl
);
1522 int method_name_is_wfl
=
1523 (TREE_CODE (method_name
) == EXPR_WITH_FILE_LOCATION
);
1524 if (method_name_is_wfl
)
1525 method_name
= java_get_real_method_name (method_decl
);
1527 if (method_name
!= init_identifier_node
1528 && method_name
!= finit_identifier_node
)
1531 = unicode_mangling_length (IDENTIFIER_POINTER (method_name
),
1532 IDENTIFIER_LENGTH (method_name
));
1533 if (encoded_len
> 0)
1535 method_name_needs_escapes
= 1;
1536 emit_unicode_mangled_name (&temporary_obstack
,
1537 IDENTIFIER_POINTER (method_name
),
1538 IDENTIFIER_LENGTH (method_name
));
1542 obstack_grow (&temporary_obstack
,
1543 IDENTIFIER_POINTER (method_name
),
1544 IDENTIFIER_LENGTH (method_name
));
1548 obstack_grow (&temporary_obstack
, "__", 2);
1549 if (method_name
== finit_identifier_node
)
1550 obstack_grow (&temporary_obstack
, "finit", 5);
1551 append_gpp_mangled_type (&temporary_obstack
, this_class
);
1552 TREE_PUBLIC (method_decl
) = 1;
1554 t
= TREE_TYPE (method_decl
);
1555 arglist
= TYPE_ARG_TYPES (t
);
1556 if (TREE_CODE (t
) == METHOD_TYPE
)
1557 arglist
= TREE_CHAIN (arglist
);
1558 for (arg
= arglist
; arg
!= end_params_node
; )
1561 tree argtype
= TREE_VALUE (arg
);
1563 if (TREE_CODE (argtype
) == POINTER_TYPE
)
1565 /* This is O(N**2). Do we care? Cfr gcc/cp/method.c. */
1566 while (a
!= arg
&& argtype
!= TREE_VALUE (a
))
1567 a
= TREE_CHAIN (a
), tindex
++;
1577 arg
= TREE_CHAIN (arg
); nrepeats
++;
1579 while (arg
!= end_params_node
&& argtype
== TREE_VALUE (arg
));
1582 obstack_1grow (&temporary_obstack
, 'N');
1583 sprintf (buf
, "%d", nrepeats
);
1584 obstack_grow (&temporary_obstack
, buf
, strlen (buf
));
1586 obstack_1grow (&temporary_obstack
, '_');
1589 obstack_1grow (&temporary_obstack
, 'T');
1590 sprintf (buf
, "%d", tindex
);
1591 obstack_grow (&temporary_obstack
, buf
, strlen (buf
));
1593 obstack_1grow (&temporary_obstack
, '_');
1597 append_gpp_mangled_type (&temporary_obstack
, argtype
);
1598 arg
= TREE_CHAIN (arg
);
1601 if (method_name_needs_escapes
)
1602 obstack_1grow (&temporary_obstack
, 'U');
1604 obstack_1grow (&temporary_obstack
, '\0');
1605 asm_name
= obstack_finish (&temporary_obstack
);
1606 DECL_ASSEMBLER_NAME (method_decl
) = get_identifier (asm_name
);
1607 if (! METHOD_ABSTRACT (method_decl
)
1608 && ! CLASS_INTERFACE (TYPE_NAME (this_class
)))
1609 make_function_rtl (method_decl
);
1610 obstack_free (&temporary_obstack
, asm_name
);
1612 if (method_name
== init_identifier_node
)
1614 char *p
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class
)));
1615 for (ptr
= p
; *ptr
; )
1620 if (method_name_is_wfl
)
1621 EXPR_WFL_NODE (DECL_NAME (method_decl
)) = get_identifier (p
);
1623 DECL_NAME (method_decl
) = get_identifier (p
);
1624 DECL_CONSTRUCTOR_P (method_decl
) = 1;
1625 build_java_argument_signature (TREE_TYPE (method_decl
));
1627 else if (! METHOD_STATIC (method_decl
) && !DECL_ARTIFICIAL (method_decl
))
1630 build_java_argument_signature (TREE_TYPE (method_decl
));
1631 tree super_method
= lookup_argument_method (super_class
, method_name
,
1633 if (super_method
!= NULL_TREE
&& ! METHOD_PRIVATE (super_method
))
1635 DECL_VINDEX (method_decl
) = DECL_VINDEX (super_method
);
1636 if (DECL_VINDEX (method_decl
) == NULL_TREE
)
1637 error_with_decl (method_decl
,
1638 "non-static method '%s' overrides static method");
1640 else if (TREE_TYPE (TREE_TYPE (method_decl
))
1641 != TREE_TYPE (TREE_TYPE (super_method
)))
1643 error_with_decl (method_decl
,
1644 "Method `%s' redefined with different return type");
1645 error_with_decl (super_method
,
1646 "Overridden decl is here");
1650 else if (! METHOD_FINAL (method_decl
)
1651 && ! METHOD_PRIVATE (method_decl
)
1652 && ! CLASS_FINAL (TYPE_NAME (this_class
))
1655 DECL_VINDEX (method_decl
) = dtable_count
;
1656 dtable_count
= build_int_2 (1+TREE_INT_CST_LOW (dtable_count
), 0);
1659 return dtable_count
;
1662 static tree registered_class
= NULL_TREE
;
1668 tree node
= TREE_OPERAND (build_class_ref (current_class
), 0);
1669 tree current
= copy_node (node
);
1671 XEXP (DECL_RTL (current
), 0) = copy_rtx (XEXP (DECL_RTL(node
), 0));
1672 if (!registered_class
)
1673 registered_class
= current
;
1675 TREE_CHAIN (end
) = current
;
1680 /* Generate a function that gets called at start-up (static contructor) time,
1681 which calls registerClass for all the compiled classes. */
1684 emit_register_classes ()
1686 tree decl
= getdecls ();
1688 extern tree get_file_function_name
PROTO((int));
1689 tree init_name
= get_file_function_name ('I');
1690 tree init_type
= build_function_type (void_type_node
, end_params_node
);
1695 init_decl
= build_decl (FUNCTION_DECL
, init_name
, init_type
);
1696 DECL_ASSEMBLER_NAME (init_decl
) = init_name
;
1697 TREE_STATIC (init_decl
) = 1;
1698 current_function_decl
= init_decl
;
1699 DECL_RESULT (init_decl
) = build_decl(RESULT_DECL
, NULL_TREE
, void_type_node
);
1700 /* DECL_EXTERNAL (init_decl) = 1;*/
1701 TREE_PUBLIC (init_decl
) = 1;
1703 make_function_rtl (init_decl
);
1704 init_function_start (init_decl
, input_filename
, 0);
1705 expand_function_start (init_decl
, 0);
1707 for ( t
= registered_class
; t
; t
= TREE_CHAIN (t
))
1708 emit_library_call (registerClass_libfunc
, 0, VOIDmode
, 1,
1709 XEXP (DECL_RTL (t
), 0), Pmode
);
1711 expand_function_end (input_filename
, 0, 0);
1714 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1715 int saved_flag
= flag_inline_functions
;
1716 flag_inline_functions
= 0;
1717 rest_of_compilation (init_decl
);
1718 flag_inline_functions
= saved_flag
;
1720 current_function_decl
= NULL_TREE
;
1721 assemble_constructor (IDENTIFIER_POINTER (init_name
));
1725 init_class_processing ()
1727 registerClass_libfunc
= gen_rtx (SYMBOL_REF
, Pmode
, "_Jv_RegisterClass");