1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Mostly written by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
33 /* C++ returns type information to the user in struct type_info
34 objects. We also use type information to implement dynamic_cast and
35 exception handlers. Type information for a particular type is
36 indicated with an ABI defined structure derived from type_info.
37 This would all be very straight forward, but for the fact that the
38 runtime library provides the definitions of the type_info structure
39 and the ABI defined derived classes. We cannot build declarations
40 of them directly in the compiler, but we need to layout objects of
41 their type. Somewhere we have to lie.
43 We define layout compatible POD-structs with compiler-defined names
44 and generate the appropriate initializations for them (complete
45 with explicit mention of their vtable). When we have to provide a
46 type_info to the user we reinterpret_cast the internal compiler
47 type to type_info. A well formed program can only explicitly refer
48 to the type_infos of complete types (& cv void). However, we chain
49 pointer type_infos to the pointed-to-type, and that can be
50 incomplete. We only need the addresses of such incomplete
51 type_info objects for static initialization.
53 The type information VAR_DECL of a type is held on the
54 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
55 will be the internal type. It will usually have the correct
56 internal type reflecting the kind of type it represents (pointer,
57 array, function, class, inherited class, etc). When the type it
58 represents is incomplete, it will have the internal type
59 corresponding to type_info. That will only happen at the end of
60 translation, when we are emitting the type info objects. */
62 /* Accessors for the type_info objects. We need to remember several things
63 about each of the type_info types. The global tree nodes such as
64 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
65 the required information. */
66 /* The RECORD_TYPE of a type_info derived class. */
67 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
68 /* The VAR_DECL of the vtable for the type_info derived class.
69 This is only filled in at the end of the translation. */
70 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
71 /* The IDENTIFIER_NODE naming the real class. */
72 #define TINFO_REAL_NAME(NODE) TREE_PURPOSE (NODE)
74 static tree build_headof
PARAMS((tree
));
75 static tree ifnonnull
PARAMS((tree
, tree
));
76 static tree tinfo_name
PARAMS((tree
));
77 static tree build_dynamic_cast_1
PARAMS((tree
, tree
));
78 static tree throw_bad_cast
PARAMS((void));
79 static tree throw_bad_typeid
PARAMS((void));
80 static tree get_tinfo_decl_dynamic
PARAMS((tree
));
81 static tree get_tinfo_ptr
PARAMS((tree
));
82 static bool typeid_ok_p
PARAMS((void));
83 static int qualifier_flags
PARAMS((tree
));
84 static int target_incomplete_p
PARAMS((tree
));
85 static tree tinfo_base_init
PARAMS((tree
, tree
));
86 static tree generic_initializer
PARAMS((tree
, tree
));
87 static tree ptr_initializer
PARAMS((tree
, tree
, int *));
88 static tree ptm_initializer
PARAMS((tree
, tree
, int *));
89 static tree dfs_class_hint_mark
PARAMS ((tree
, void *));
90 static tree dfs_class_hint_unmark
PARAMS ((tree
, void *));
91 static int class_hint_flags
PARAMS((tree
));
92 static tree class_initializer
PARAMS((tree
, tree
, tree
));
93 static tree create_pseudo_type_info
PARAMS((const char *, int, ...));
94 static tree get_pseudo_ti_init
PARAMS ((tree
, tree
, int *));
95 static tree get_pseudo_ti_desc
PARAMS((tree
));
96 static void create_tinfo_types
PARAMS((void));
97 static int typeinfo_in_lib_p
PARAMS((tree
));
99 static int doing_runtime
= 0;
102 /* Declare language defined type_info type and a pointer to const
103 type_info. This is incomplete here, and will be completed when
104 the user #includes <typeinfo>. There are language defined
105 restrictions on what can be done until that is included. Create
106 the internal versions of the ABI types. */
109 init_rtti_processing ()
111 push_namespace (std_identifier
);
113 = xref_tag (class_type
, get_identifier ("type_info"),
114 /*attributes=*/NULL_TREE
, 1);
118 (build_qualified_type (type_info_type_node
, TYPE_QUAL_CONST
));
120 create_tinfo_types ();
123 /* Given the expression EXP of type `class *', return the head of the
124 object pointed to by EXP with type cv void*, if the class has any
125 virtual functions (TYPE_POLYMORPHIC_P), else just return the
132 tree type
= TREE_TYPE (exp
);
136 my_friendly_assert (TREE_CODE (type
) == POINTER_TYPE
, 20000112);
137 type
= TREE_TYPE (type
);
139 if (!TYPE_POLYMORPHIC_P (type
))
142 /* We use this a couple of times below, protect it. */
143 exp
= save_expr (exp
);
145 /* The offset-to-top field is at index -2 from the vptr. */
146 index
= build_int_2 (-2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
, -1);
148 offset
= build_vtbl_ref (build_indirect_ref (exp
, NULL
), index
);
150 type
= build_qualified_type (ptr_type_node
,
151 cp_type_quals (TREE_TYPE (exp
)));
152 return build (PLUS_EXPR
, type
, exp
,
153 cp_convert (ptrdiff_type_node
, offset
));
156 /* Get a bad_cast node for the program to throw...
158 See libstdc++/exception.cc for __throw_bad_cast */
163 tree fn
= get_identifier ("__cxa_bad_cast");
164 if (IDENTIFIER_GLOBAL_VALUE (fn
))
165 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
167 fn
= push_throw_library_fn (fn
, build_function_type (ptr_type_node
,
170 return build_call (fn
, NULL_TREE
);
176 tree fn
= get_identifier ("__cxa_bad_typeid");
177 if (IDENTIFIER_GLOBAL_VALUE (fn
))
178 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
181 tree t
= build_qualified_type (type_info_type_node
, TYPE_QUAL_CONST
);
182 t
= build_function_type (build_reference_type (t
), void_list_node
);
183 fn
= push_throw_library_fn (fn
, t
);
186 return build_call (fn
, NULL_TREE
);
189 /* Return a pointer to type_info function associated with the expression EXP.
190 If EXP is a reference to a polymorphic class, return the dynamic type;
191 otherwise return the static type of the expression. */
194 get_tinfo_decl_dynamic (exp
)
199 if (exp
== error_mark_node
)
200 return error_mark_node
;
202 type
= TREE_TYPE (exp
);
204 /* peel back references, so they match. */
205 if (TREE_CODE (type
) == REFERENCE_TYPE
)
206 type
= TREE_TYPE (type
);
208 /* Peel off cv qualifiers. */
209 type
= TYPE_MAIN_VARIANT (type
);
211 if (!VOID_TYPE_P (type
))
212 type
= complete_type_or_else (type
, exp
);
215 return error_mark_node
;
217 /* If exp is a reference to polymorphic type, get the real type_info. */
218 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
220 /* build reference to type_info from vtable. */
224 /* The RTTI information is at index -1. */
225 index
= build_int_2 (-1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
, -1);
226 t
= build_vtbl_ref (exp
, index
);
227 TREE_TYPE (t
) = type_info_ptr_type
;
231 /* Otherwise return the type_info for the static type of the expr. */
232 return get_tinfo_ptr (TYPE_MAIN_VARIANT (type
));
240 error ("cannot use typeid with -fno-rtti");
244 if (!COMPLETE_TYPE_P (type_info_type_node
))
246 error ("must #include <typeinfo> before using typeid");
257 tree cond
= NULL_TREE
;
260 if (exp
== error_mark_node
|| !typeid_ok_p ())
261 return error_mark_node
;
263 if (processing_template_decl
)
264 return build_min_nt (TYPEID_EXPR
, exp
);
266 if (TREE_CODE (exp
) == INDIRECT_REF
267 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == POINTER_TYPE
268 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
269 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
272 exp
= stabilize_reference (exp
);
273 cond
= cp_convert (boolean_type_node
, TREE_OPERAND (exp
, 0));
276 exp
= get_tinfo_decl_dynamic (exp
);
278 if (exp
== error_mark_node
)
279 return error_mark_node
;
281 exp
= build_indirect_ref (exp
, NULL
);
285 tree bad
= throw_bad_typeid ();
287 exp
= build (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
290 return convert_from_reference (exp
);
293 /* Generate the NTBS name of a type. */
301 name
= mangle_type_string (type
);
302 name_string
= fix_string_type (build_string (strlen (name
) + 1, name
));
306 /* Return a VAR_DECL for the internal ABI defined type_info object for
307 TYPE. You must arrange that the decl is mark_used, if actually use
308 it --- decls in vtables are only used if the vtable is output. */
311 get_tinfo_decl (type
)
317 if (COMPLETE_TYPE_P (type
)
318 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
320 error ("cannot create type information for type `%T' because its size is variable",
322 return error_mark_node
;
325 if (TREE_CODE (type
) == OFFSET_TYPE
)
326 type
= TREE_TYPE (type
);
327 if (TREE_CODE (type
) == METHOD_TYPE
)
328 type
= build_function_type (TREE_TYPE (type
),
329 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
331 name
= mangle_typeinfo_for_type (type
);
333 d
= IDENTIFIER_GLOBAL_VALUE (name
);
336 tree var_desc
= get_pseudo_ti_desc (type
);
338 d
= build_lang_decl (VAR_DECL
, name
, TINFO_PSEUDO_TYPE (var_desc
));
340 DECL_ARTIFICIAL (d
) = 1;
341 TREE_READONLY (d
) = 1;
343 DECL_EXTERNAL (d
) = 1;
344 SET_DECL_ASSEMBLER_NAME (d
, name
);
346 cp_finish_decl (d
, NULL_TREE
, NULL_TREE
, 0);
348 pushdecl_top_level (d
);
350 /* Remember the type it is for. */
351 TREE_TYPE (name
) = type
;
357 /* Return a pointer to a type_info object describing TYPE, suitably
358 cast to the language defined type. */
364 tree exp
= get_tinfo_decl (type
);
366 /* Convert to type_info type. */
367 exp
= build_unary_op (ADDR_EXPR
, exp
, 0);
368 exp
= ocp_convert (type_info_ptr_type
, exp
, CONV_REINTERPRET
, 0);
373 /* Return the type_info object for TYPE. */
379 if (type
== error_mark_node
|| !typeid_ok_p ())
380 return error_mark_node
;
382 if (processing_template_decl
)
383 return build_min_nt (TYPEID_EXPR
, type
);
385 /* If the type of the type-id is a reference type, the result of the
386 typeid expression refers to a type_info object representing the
388 if (TREE_CODE (type
) == REFERENCE_TYPE
)
389 type
= TREE_TYPE (type
);
391 /* The top-level cv-qualifiers of the lvalue expression or the type-id
392 that is the operand of typeid are always ignored. */
393 type
= TYPE_MAIN_VARIANT (type
);
395 if (!VOID_TYPE_P (type
))
396 type
= complete_type_or_else (type
, NULL_TREE
);
399 return error_mark_node
;
401 return build_indirect_ref (get_tinfo_ptr (type
), NULL
);
404 /* Check whether TEST is null before returning RESULT. If TEST is used in
405 RESULT, it must have previously had a save_expr applied to it. */
408 ifnonnull (test
, result
)
411 return build (COND_EXPR
, TREE_TYPE (result
),
412 build (EQ_EXPR
, boolean_type_node
, test
, integer_zero_node
),
413 cp_convert (TREE_TYPE (result
), integer_zero_node
),
417 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
421 build_dynamic_cast_1 (type
, expr
)
424 enum tree_code tc
= TREE_CODE (type
);
425 tree exprtype
= TREE_TYPE (expr
);
427 tree old_expr
= expr
;
428 const char *errstr
= NULL
;
430 /* T shall be a pointer or reference to a complete class type, or
431 `pointer to cv void''. */
435 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
438 if (! IS_AGGR_TYPE (TREE_TYPE (type
)))
440 errstr
= "target is not pointer or reference to class";
443 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
445 errstr
= "target is not pointer or reference to complete type";
451 errstr
= "target is not pointer or reference";
455 if (TREE_CODE (expr
) == OFFSET_REF
)
457 expr
= resolve_offset_ref (expr
);
458 exprtype
= TREE_TYPE (expr
);
461 if (tc
== POINTER_TYPE
)
462 expr
= convert_from_reference (expr
);
463 else if (TREE_CODE (exprtype
) != REFERENCE_TYPE
)
465 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
466 exprtype
= build_reference_type (exprtype
);
467 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
468 LOOKUP_NORMAL
, NULL_TREE
);
471 exprtype
= TREE_TYPE (expr
);
473 if (tc
== POINTER_TYPE
)
475 /* If T is a pointer type, v shall be an rvalue of a pointer to
476 complete class type, and the result is an rvalue of type T. */
478 if (TREE_CODE (exprtype
) != POINTER_TYPE
)
480 errstr
= "source is not a pointer";
483 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
485 errstr
= "source is not a pointer to class";
488 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
490 errstr
= "source is a pointer to incomplete type";
496 /* T is a reference type, v shall be an lvalue of a complete class
497 type, and the result is an lvalue of the type referred to by T. */
499 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
501 errstr
= "source is not of class type";
504 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
506 errstr
= "source is of incomplete class type";
512 /* The dynamic_cast operator shall not cast away constness. */
513 if (!at_least_as_qualified_p (TREE_TYPE (type
),
514 TREE_TYPE (exprtype
)))
516 errstr
= "conversion casts away constness";
520 /* If *type is an unambiguous accessible base class of *exprtype,
521 convert statically. */
525 binfo
= lookup_base (TREE_TYPE (exprtype
), TREE_TYPE (type
),
526 ba_not_special
, NULL
);
530 expr
= build_base_path (PLUS_EXPR
, convert_from_reference (expr
),
532 if (TREE_CODE (exprtype
) == POINTER_TYPE
)
533 expr
= non_lvalue (expr
);
538 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
539 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
542 /* if TYPE is `void *', return pointer to complete object. */
543 if (tc
== POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (type
)))
545 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
546 if (TREE_CODE (expr
) == ADDR_EXPR
547 && TREE_CODE (TREE_OPERAND (expr
, 0)) == VAR_DECL
548 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
549 return build1 (NOP_EXPR
, type
, expr
);
551 /* Since expr is used twice below, save it. */
552 expr
= save_expr (expr
);
554 expr1
= build_headof (expr
);
555 if (TREE_TYPE (expr1
) != type
)
556 expr1
= build1 (NOP_EXPR
, type
, expr1
);
557 return ifnonnull (expr
, expr1
);
562 tree result
, td2
, td3
, elems
;
563 tree static_type
, target_type
, boff
;
565 /* If we got here, we can't convert statically. Therefore,
566 dynamic_cast<D&>(b) (b an object) cannot succeed. */
567 if (tc
== REFERENCE_TYPE
)
569 if (TREE_CODE (old_expr
) == VAR_DECL
570 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
572 tree expr
= throw_bad_cast ();
573 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
575 /* Bash it to the expected type. */
576 TREE_TYPE (expr
) = type
;
580 /* Ditto for dynamic_cast<D*>(&b). */
581 else if (TREE_CODE (expr
) == ADDR_EXPR
)
583 tree op
= TREE_OPERAND (expr
, 0);
584 if (TREE_CODE (op
) == VAR_DECL
585 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
587 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
589 retval
= build_int_2 (0, 0);
590 TREE_TYPE (retval
) = type
;
595 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
596 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
597 td2
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (target_type
), 0);
598 td3
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (static_type
), 0);
600 /* Determine how T and V are related. */
601 boff
= get_dynamic_cast_base_type (static_type
, target_type
);
603 /* Since expr is used twice below, save it. */
604 expr
= save_expr (expr
);
607 if (tc
== REFERENCE_TYPE
)
608 expr1
= build_unary_op (ADDR_EXPR
, expr1
, 0);
611 (NULL_TREE
, expr1
, tree_cons
612 (NULL_TREE
, td3
, tree_cons
613 (NULL_TREE
, td2
, tree_cons
614 (NULL_TREE
, boff
, NULL_TREE
))));
616 dcast_fn
= dynamic_cast_node
;
624 push_nested_namespace (ns
);
625 tinfo_ptr
= xref_tag (class_type
,
626 get_identifier ("__class_type_info"),
627 /*attributes=*/NULL_TREE
,
630 tinfo_ptr
= build_pointer_type
631 (build_qualified_type
632 (tinfo_ptr
, TYPE_QUAL_CONST
));
633 name
= "__dynamic_cast";
635 (NULL_TREE
, const_ptr_type_node
, tree_cons
636 (NULL_TREE
, tinfo_ptr
, tree_cons
637 (NULL_TREE
, tinfo_ptr
, tree_cons
638 (NULL_TREE
, ptrdiff_type_node
, void_list_node
))));
639 tmp
= build_function_type (ptr_type_node
, tmp
);
640 dcast_fn
= build_library_fn_ptr (name
, tmp
);
641 pop_nested_namespace (ns
);
642 dynamic_cast_node
= dcast_fn
;
644 result
= build_call (dcast_fn
, elems
);
646 if (tc
== REFERENCE_TYPE
)
648 tree bad
= throw_bad_cast ();
650 result
= save_expr (result
);
651 return build (COND_EXPR
, type
, result
, result
, bad
);
654 /* Now back to the type we want from a void*. */
655 result
= cp_convert (type
, result
);
656 return ifnonnull (expr
, result
);
660 errstr
= "source type is not polymorphic";
663 error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
664 expr
, exprtype
, type
, errstr
);
665 return error_mark_node
;
669 build_dynamic_cast (type
, expr
)
672 if (type
== error_mark_node
|| expr
== error_mark_node
)
673 return error_mark_node
;
675 if (processing_template_decl
)
676 return build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
678 return convert_from_reference (build_dynamic_cast_1 (type
, expr
));
681 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
684 qualifier_flags (type
)
688 /* we want the qualifiers on this type, not any array core, it might have */
689 int quals
= TYPE_QUALS (type
);
691 if (quals
& TYPE_QUAL_CONST
)
693 if (quals
& TYPE_QUAL_VOLATILE
)
695 if (quals
& TYPE_QUAL_RESTRICT
)
700 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
701 contains a pointer to member of an incomplete class. */
704 target_incomplete_p (type
)
707 while (TREE_CODE (type
) == POINTER_TYPE
)
708 if (TYPE_PTRMEM_P (type
))
710 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)))
712 type
= TYPE_PTRMEM_POINTED_TO_TYPE (type
);
715 type
= TREE_TYPE (type
);
716 if (!COMPLETE_OR_VOID_TYPE_P (type
))
722 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
723 is the vtable pointer and NTBS name. The NTBS name is emitted as a
724 comdat const char array, so it becomes a unique key for the type. Generate
725 and emit that VAR_DECL here. (We can't always emit the type_info itself
726 as comdat, because of pointers to incomplete.) */
729 tinfo_base_init (desc
, target
)
733 tree init
= NULL_TREE
;
740 /* Generate the NTBS array variable. */
741 tree name_type
= build_cplus_array_type
742 (build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
744 tree name_string
= tinfo_name (target
);
746 name_name
= mangle_typeinfo_string_for_type (target
);
747 name_decl
= build_lang_decl (VAR_DECL
, name_name
, name_type
);
749 DECL_ARTIFICIAL (name_decl
) = 1;
750 TREE_READONLY (name_decl
) = 1;
751 TREE_STATIC (name_decl
) = 1;
752 DECL_EXTERNAL (name_decl
) = 0;
753 TREE_PUBLIC (name_decl
) = 1;
754 comdat_linkage (name_decl
);
755 /* External name of the string containing the type's name has a
757 SET_DECL_ASSEMBLER_NAME (name_decl
,
758 mangle_typeinfo_string_for_type (target
));
759 DECL_INITIAL (name_decl
) = name_string
;
760 cp_finish_decl (name_decl
, name_string
, NULL_TREE
, 0);
761 pushdecl_top_level (name_decl
);
764 vtable_ptr
= TINFO_VTABLE_DECL (desc
);
769 push_nested_namespace (abi_node
);
770 real_type
= xref_tag (class_type
, TINFO_REAL_NAME (desc
),
771 /*attributes=*/NULL_TREE
, 1);
772 pop_nested_namespace (abi_node
);
774 if (!COMPLETE_TYPE_P (real_type
))
776 /* We never saw a definition of this type, so we need to
777 tell the compiler that this is an exported class, as
778 indeed all of the __*_type_info classes are. */
779 SET_CLASSTYPE_INTERFACE_KNOWN (real_type
);
780 CLASSTYPE_INTERFACE_ONLY (real_type
) = 1;
783 vtable_ptr
= get_vtable_decl (real_type
, /*complete=*/1);
784 vtable_ptr
= build_unary_op (ADDR_EXPR
, vtable_ptr
, 0);
786 /* We need to point into the middle of the vtable. */
788 (PLUS_EXPR
, TREE_TYPE (vtable_ptr
), vtable_ptr
,
789 size_binop (MULT_EXPR
,
790 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
),
791 TYPE_SIZE_UNIT (vtable_entry_type
)));
792 TREE_CONSTANT (vtable_ptr
) = 1;
794 TINFO_VTABLE_DECL (desc
) = vtable_ptr
;
797 init
= tree_cons (NULL_TREE
, vtable_ptr
, init
);
799 init
= tree_cons (NULL_TREE
, decay_conversion (name_decl
), init
);
801 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
802 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
803 init
= tree_cons (NULL_TREE
, init
, NULL_TREE
);
808 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
809 information about the particular type_info derivation, which adds no
810 additional fields to the type_info base. */
813 generic_initializer (desc
, target
)
817 tree init
= tinfo_base_init (desc
, target
);
819 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
820 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
824 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
825 DESC provides information about the particular type_info derivation,
826 which adds target type and qualifier flags members to the type_info base. */
829 ptr_initializer (desc
, target
, non_public_ptr
)
834 tree init
= tinfo_base_init (desc
, target
);
835 tree to
= TREE_TYPE (target
);
836 int flags
= qualifier_flags (to
);
837 int incomplete
= target_incomplete_p (to
);
844 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
845 init
= tree_cons (NULL_TREE
,
846 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)),
849 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
850 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
854 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
855 DESC provides information about the particular type_info derivation,
856 which adds class, target type and qualifier flags members to the type_info
860 ptm_initializer (desc
, target
, non_public_ptr
)
865 tree init
= tinfo_base_init (desc
, target
);
866 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
867 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
868 int flags
= qualifier_flags (to
);
869 int incomplete
= target_incomplete_p (to
);
876 if (!COMPLETE_TYPE_P (klass
))
881 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
882 init
= tree_cons (NULL_TREE
,
883 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)),
885 init
= tree_cons (NULL_TREE
,
886 get_tinfo_ptr (klass
),
889 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
890 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
894 /* Check base BINFO to set hint flags in *DATA, which is really an int.
895 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
896 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
897 possible for a type to be both a virtual and non-virtual base. */
900 dfs_class_hint_mark (binfo
, data
)
904 tree basetype
= BINFO_TYPE (binfo
);
905 int *hint
= (int *) data
;
907 if (TREE_VIA_VIRTUAL (binfo
))
909 if (CLASSTYPE_MARKED (basetype
))
911 if (CLASSTYPE_MARKED2 (basetype
))
913 SET_CLASSTYPE_MARKED2 (basetype
);
917 if (CLASSTYPE_MARKED (basetype
) || CLASSTYPE_MARKED2 (basetype
))
919 SET_CLASSTYPE_MARKED (basetype
);
921 if (!TREE_VIA_PUBLIC (binfo
) && TYPE_BINFO (basetype
) != binfo
)
926 /* Clear the base's dfs marks, after searching for duplicate bases. */
929 dfs_class_hint_unmark (binfo
, data
)
931 void *data ATTRIBUTE_UNUSED
;
933 tree basetype
= BINFO_TYPE (binfo
);
935 CLEAR_CLASSTYPE_MARKED (basetype
);
936 CLEAR_CLASSTYPE_MARKED2 (basetype
);
940 /* Determine the hint flags describing the features of a class's hierarchy. */
943 class_hint_flags (type
)
949 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_mark
, NULL
, &hint_flags
);
950 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_unmark
, NULL
, NULL
);
952 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); ++i
)
954 tree base_binfo
= BINFO_BASETYPE (TYPE_BINFO (type
), i
);
956 if (TREE_VIA_PUBLIC (base_binfo
))
962 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
963 DESC provides information about the particular __class_type_info derivation,
964 which adds hint flags and TRAIL initializers to the type_info base. */
967 class_initializer (desc
, target
, trail
)
972 tree init
= tinfo_base_init (desc
, target
);
974 TREE_CHAIN (init
) = trail
;
975 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
976 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
980 /* Returns non-zero if the typeinfo for type should be placed in
981 the runtime library. */
984 typeinfo_in_lib_p (type
)
987 /* The typeinfo objects for `T*' and `const T*' are in the runtime
988 library for simple types T. */
989 if (TREE_CODE (type
) == POINTER_TYPE
990 && (cp_type_quals (TREE_TYPE (type
)) == TYPE_QUAL_CONST
991 || cp_type_quals (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
))
992 type
= TREE_TYPE (type
);
994 switch (TREE_CODE (type
))
1008 /* Generate the initializer for the type info describing
1009 TYPE. VAR_DESC is a . NON_PUBLIC_P is set non-zero, if the VAR_DECL
1010 should not be exported from this object file. This should only be
1011 called at the end of translation, when we know that no further
1012 types will be completed. */
1015 get_pseudo_ti_init (type
, var_desc
, non_public_p
)
1020 my_friendly_assert (at_eof
, 20021120);
1021 switch (TREE_CODE (type
))
1024 if (TYPE_PTRMEM_P (type
))
1025 return ptm_initializer (var_desc
, type
, non_public_p
);
1027 return ptr_initializer (var_desc
, type
, non_public_p
);
1030 return generic_initializer (var_desc
, type
);
1033 return generic_initializer (var_desc
, type
);
1036 return generic_initializer (var_desc
, type
);
1040 if (TYPE_PTRMEMFUNC_P (type
))
1041 return ptm_initializer (var_desc
, type
, non_public_p
);
1042 else if (var_desc
== class_desc_type_node
)
1044 if (!COMPLETE_TYPE_P (type
))
1045 /* Emit a non-public class_type_info. */
1047 return class_initializer (var_desc
, type
, NULL_TREE
);
1049 else if (var_desc
== si_class_desc_type_node
)
1051 tree base_binfos
= BINFO_BASETYPES (TYPE_BINFO (type
));
1052 tree base_binfo
= TREE_VEC_ELT (base_binfos
, 0);
1053 tree tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
1054 tree base_inits
= tree_cons (NULL_TREE
, tinfo
, NULL_TREE
);
1056 return class_initializer (var_desc
, type
, base_inits
);
1060 int hint
= class_hint_flags (type
);
1061 tree binfo
= TYPE_BINFO (type
);
1062 int nbases
= BINFO_N_BASETYPES (binfo
);
1063 tree base_binfos
= BINFO_BASETYPES (binfo
);
1064 tree base_inits
= NULL_TREE
;
1067 /* Generate the base information initializer. */
1068 for (ix
= nbases
; ix
--;)
1070 tree base_binfo
= TREE_VEC_ELT (base_binfos
, ix
);
1071 tree base_init
= NULL_TREE
;
1076 if (TREE_PUBLIC (base_binfo
))
1078 tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
1079 if (TREE_VIA_VIRTUAL (base_binfo
))
1081 /* We store the vtable offset at which the virtual
1082 base offset can be found. */
1083 offset
= BINFO_VPTR_FIELD
1084 (binfo_for_vbase (BINFO_TYPE (base_binfo
), type
));
1085 offset
= convert (sizetype
, offset
);
1089 offset
= BINFO_OFFSET (base_binfo
);
1091 /* combine offset and flags into one field */
1092 offset
= cp_build_binary_op (LSHIFT_EXPR
, offset
,
1093 build_int_2 (8, 0));
1094 offset
= cp_build_binary_op (BIT_IOR_EXPR
, offset
,
1095 build_int_2 (flags
, 0));
1096 base_init
= tree_cons (NULL_TREE
, offset
, base_init
);
1097 base_init
= tree_cons (NULL_TREE
, tinfo
, base_init
);
1098 base_init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_init
);
1099 base_inits
= tree_cons (NULL_TREE
, base_init
, base_inits
);
1101 base_inits
= build (CONSTRUCTOR
,
1102 NULL_TREE
, NULL_TREE
, base_inits
);
1103 base_inits
= tree_cons (NULL_TREE
, base_inits
, NULL_TREE
);
1104 /* Prepend the number of bases. */
1105 base_inits
= tree_cons (NULL_TREE
,
1106 build_int_2 (nbases
, 0), base_inits
);
1107 /* Prepend the hint flags. */
1108 base_inits
= tree_cons (NULL_TREE
,
1109 build_int_2 (hint
, 0), base_inits
);
1111 return class_initializer (var_desc
, type
, base_inits
);
1116 return generic_initializer (var_desc
, type
);
1120 /* Generate the RECORD_TYPE containing the data layout of a type_info
1121 derivative as used by the runtime. This layout must be consistent with
1122 that defined in the runtime support. Also generate the VAR_DECL for the
1123 type's vtable. We explicitly manage the vtable member, and name it for
1124 real type as used in the runtime. The RECORD type has a different name,
1125 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1126 is the generated type and TINFO_VTABLE_NAME is the name of the
1127 vtable. We have to delay generating the VAR_DECL of the vtable
1128 until the end of the translation, when we'll have seen the library
1129 definition, if there was one.
1131 REAL_NAME is the runtime's name of the type. Trailing arguments are
1132 additional FIELD_DECL's for the structure. The final argument must be
1136 create_pseudo_type_info
VPARAMS((const char *real_name
, int ident
, ...))
1145 VA_OPEN (ap
, ident
);
1146 VA_FIXEDARG (ap
, const char *, real_name
);
1147 VA_FIXEDARG (ap
, int, ident
);
1149 /* Generate the pseudo type name. */
1150 pseudo_name
= (char *)alloca (strlen (real_name
) + 30);
1151 strcpy (pseudo_name
, real_name
);
1152 strcat (pseudo_name
, "_pseudo");
1154 sprintf (pseudo_name
+ strlen (pseudo_name
), "%d", ident
);
1156 /* First field is the pseudo type_info base class. */
1157 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, ti_desc_type_node
);
1159 /* Now add the derived fields. */
1160 for (ix
= 0; (field_decl
= va_arg (ap
, tree
));)
1161 fields
[++ix
] = field_decl
;
1163 /* Create the pseudo type. */
1164 pseudo_type
= make_aggr_type (RECORD_TYPE
);
1165 finish_builtin_type (pseudo_type
, pseudo_name
, fields
, ix
, ptr_type_node
);
1166 TYPE_HAS_CONSTRUCTOR (pseudo_type
) = 1;
1168 result
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
1169 TINFO_REAL_NAME (result
) = get_identifier (real_name
);
1170 TINFO_PSEUDO_TYPE (result
) =
1171 cp_build_qualified_type (pseudo_type
, TYPE_QUAL_CONST
);
1177 /* Return a pseudo type info type node used to describe TYPE. TYPE
1178 must be a complete type (or cv void), except at the end of the
1179 translation unit. */
1182 get_pseudo_ti_desc (type
)
1185 switch (TREE_CODE (type
))
1188 return TYPE_PTRMEM_P (type
) ? ptm_desc_type_node
: ptr_desc_type_node
;
1190 return enum_desc_type_node
;
1192 return func_desc_type_node
;
1194 return ary_desc_type_node
;
1197 if (TYPE_PTRMEMFUNC_P (type
))
1198 return ptm_desc_type_node
;
1199 else if (!COMPLETE_TYPE_P (type
))
1202 cxx_incomplete_type_error (NULL_TREE
, type
);
1203 return class_desc_type_node
;
1205 else if (!CLASSTYPE_N_BASECLASSES (type
))
1206 return class_desc_type_node
;
1210 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (type
)), 0);
1211 int num_bases
= BINFO_N_BASETYPES (TYPE_BINFO (type
));
1214 && TREE_PUBLIC (base_binfo
)
1215 && !TREE_VIA_VIRTUAL (base_binfo
)
1216 && integer_zerop (BINFO_OFFSET (base_binfo
)))
1217 /* single non-virtual public. */
1218 return si_class_desc_type_node
;
1222 tree array_domain
, base_array
;
1224 if (TREE_VEC_LENGTH (vmi_class_desc_type_node
) <= num_bases
)
1227 tree extend
= make_tree_vec (num_bases
+ 5);
1229 for (ix
= TREE_VEC_LENGTH (vmi_class_desc_type_node
); ix
--;)
1230 TREE_VEC_ELT (extend
, ix
)
1231 = TREE_VEC_ELT (vmi_class_desc_type_node
, ix
);
1232 vmi_class_desc_type_node
= extend
;
1234 var_desc
= TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
);
1238 /* Add number of bases and trailing array of
1239 base_class_type_info. */
1240 array_domain
= build_index_type (size_int (num_bases
));
1242 build_array_type (base_desc_type_node
, array_domain
);
1244 push_nested_namespace (abi_node
);
1245 var_desc
= create_pseudo_type_info
1246 ("__vmi_class_type_info", num_bases
,
1247 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1248 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1249 build_decl (FIELD_DECL
, NULL_TREE
, base_array
),
1251 pop_nested_namespace (abi_node
);
1253 TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
) = var_desc
;
1258 return bltn_desc_type_node
;
1262 /* Make sure the required builtin types exist for generating the type_info
1263 varable definitions. */
1266 create_tinfo_types ()
1268 my_friendly_assert (!ti_desc_type_node
, 20020609);
1270 push_nested_namespace (abi_node
);
1272 /* Create the internal type_info structure. This is used as a base for
1273 the other structures. */
1277 ti_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1278 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, const_ptr_type_node
);
1279 fields
[1] = build_decl (FIELD_DECL
, NULL_TREE
, const_string_type_node
);
1280 finish_builtin_type (ti_desc_type_node
, "__type_info_pseudo",
1281 fields
, 1, ptr_type_node
);
1282 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node
) = 1;
1285 /* Fundamental type_info */
1286 bltn_desc_type_node
= create_pseudo_type_info
1287 ("__fundamental_type_info", 0,
1290 /* Array, function and enum type_info. No additional fields. */
1291 ary_desc_type_node
= create_pseudo_type_info
1292 ("__array_type_info", 0,
1294 func_desc_type_node
= create_pseudo_type_info
1295 ("__function_type_info", 0,
1297 enum_desc_type_node
= create_pseudo_type_info
1298 ("__enum_type_info", 0,
1301 /* Class type_info. Add a flags field. */
1302 class_desc_type_node
= create_pseudo_type_info
1303 ("__class_type_info", 0,
1306 /* Single public non-virtual base class. Add pointer to base class.
1307 This is really a descendant of __class_type_info. */
1308 si_class_desc_type_node
= create_pseudo_type_info
1309 ("__si_class_type_info", 0,
1310 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1313 /* Base class internal helper. Pointer to base type, offset to base,
1318 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
);
1319 fields
[1] = build_decl (FIELD_DECL
, NULL_TREE
, integer_types
[itk_long
]);
1320 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1321 finish_builtin_type (base_desc_type_node
, "__base_class_type_info_pseudo",
1322 fields
, 1, ptr_type_node
);
1323 TYPE_HAS_CONSTRUCTOR (base_desc_type_node
) = 1;
1326 /* General hierarchy is created as necessary in this vector. */
1327 vmi_class_desc_type_node
= make_tree_vec (10);
1329 /* Pointer type_info. Adds two fields, qualification mask
1330 and pointer to the pointed to type. This is really a descendant of
1331 __pbase_type_info. */
1332 ptr_desc_type_node
= create_pseudo_type_info
1333 ("__pointer_type_info", 0,
1334 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1335 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1338 /* Pointer to member data type_info. Add qualifications flags,
1339 pointer to the member's type info and pointer to the class.
1340 This is really a descendant of __pbase_type_info. */
1341 ptm_desc_type_node
= create_pseudo_type_info
1342 ("__pointer_to_member_type_info", 0,
1343 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1344 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1345 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1348 pop_nested_namespace (abi_node
);
1351 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1352 support. Generating them here guarantees consistency with the other
1353 structures. We use the following heuristic to determine when the runtime
1354 is being generated. If std::__fundamental_type_info is defined, and its
1355 destructor is defined, then the runtime is being built. */
1358 emit_support_tinfos ()
1360 static tree
*const fundamentals
[] =
1365 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1366 &short_integer_type_node
, &short_unsigned_type_node
,
1367 &integer_type_node
, &unsigned_type_node
,
1368 &long_integer_type_node
, &long_unsigned_type_node
,
1369 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1370 &float_type_node
, &double_type_node
, &long_double_type_node
,
1374 tree bltn_type
, dtor
;
1376 push_nested_namespace (abi_node
);
1377 bltn_type
= xref_tag (class_type
,
1378 get_identifier ("__fundamental_type_info"),
1379 /*attributes=*/NULL_TREE
,
1381 pop_nested_namespace (abi_node
);
1382 if (!COMPLETE_TYPE_P (bltn_type
))
1384 dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type
), 1);
1385 if (DECL_EXTERNAL (dtor
))
1388 for (ix
= 0; fundamentals
[ix
]; ix
++)
1390 tree bltn
= *fundamentals
[ix
];
1391 tree bltn_ptr
= build_pointer_type (bltn
);
1392 tree bltn_const_ptr
= build_pointer_type
1393 (build_qualified_type (bltn
, TYPE_QUAL_CONST
));
1396 tinfo
= get_tinfo_decl (bltn
);
1397 TREE_USED (tinfo
) = 1;
1398 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1400 tinfo
= get_tinfo_decl (bltn_ptr
);
1401 TREE_USED (tinfo
) = 1;
1402 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1404 tinfo
= get_tinfo_decl (bltn_const_ptr
);
1405 TREE_USED (tinfo
) = 1;
1406 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1410 /* Return non-zero, iff T is a type_info variable which has not had a
1411 definition emitted for it. */
1414 unemitted_tinfo_decl_p (t
, data
)
1416 void *data ATTRIBUTE_UNUSED
;
1418 if (/* It's a var decl */
1419 TREE_CODE (t
) == VAR_DECL
1420 /* whos name points back to itself */
1421 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t
)) == t
1422 /* whos name's type is non-null */
1423 && TREE_TYPE (DECL_NAME (t
))
1424 /* and whos type is a struct */
1425 && TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
1426 /* with a first field of our pseudo type info */
1427 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (t
))) == ti_desc_type_node
)
1432 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1433 tinfo decl. Determine whether it needs emitting, and if so
1434 generate the initializer. */
1437 emit_tinfo_decl (decl_ptr
, data
)
1439 void *data ATTRIBUTE_UNUSED
;
1441 tree decl
= *decl_ptr
;
1442 tree type
= TREE_TYPE (DECL_NAME (decl
));
1444 int in_library
= typeinfo_in_lib_p (type
);
1445 tree var_desc
, var_init
;
1447 import_export_tinfo (decl
, type
, in_library
);
1448 if (DECL_REALLY_EXTERN (decl
) || !DECL_NEEDED_P (decl
))
1451 if (!doing_runtime
&& in_library
)
1455 var_desc
= get_pseudo_ti_desc (type
);
1456 var_init
= get_pseudo_ti_init (type
, var_desc
, &non_public
);
1458 DECL_EXTERNAL (decl
) = 0;
1459 TREE_PUBLIC (decl
) = !non_public
;
1461 DECL_COMDAT (decl
) = 0;
1463 DECL_INITIAL (decl
) = var_init
;
1464 cp_finish_decl (decl
, var_init
, NULL_TREE
, 0);
1465 /* cp_finish_decl will have dealt with linkage. */
1467 /* Say we've dealt with it. */
1468 TREE_TYPE (DECL_NAME (decl
)) = NULL_TREE
;