1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 Free Software Foundation, Inc.
5 Mostly written by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
26 #include "coretypes.h"
36 /* C++ returns type information to the user in struct type_info
37 objects. We also use type information to implement dynamic_cast and
38 exception handlers. Type information for a particular type is
39 indicated with an ABI defined structure derived from type_info.
40 This would all be very straight forward, but for the fact that the
41 runtime library provides the definitions of the type_info structure
42 and the ABI defined derived classes. We cannot build declarations
43 of them directly in the compiler, but we need to layout objects of
44 their type. Somewhere we have to lie.
46 We define layout compatible POD-structs with compiler-defined names
47 and generate the appropriate initializations for them (complete
48 with explicit mention of their vtable). When we have to provide a
49 type_info to the user we reinterpret_cast the internal compiler
50 type to type_info. A well formed program can only explicitly refer
51 to the type_infos of complete types (& cv void). However, we chain
52 pointer type_infos to the pointed-to-type, and that can be
53 incomplete. We only need the addresses of such incomplete
54 type_info objects for static initialization.
56 The type information VAR_DECL of a type is held on the
57 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
58 will be the internal type. It will usually have the correct
59 internal type reflecting the kind of type it represents (pointer,
60 array, function, class, inherited class, etc). When the type it
61 represents is incomplete, it will have the internal type
62 corresponding to type_info. That will only happen at the end of
63 translation, when we are emitting the type info objects. */
65 /* Accessors for the type_info objects. We need to remember several things
66 about each of the type_info types. The global tree nodes such as
67 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
68 the required information. */
69 /* The RECORD_TYPE of a type_info derived class. */
70 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
71 /* The VAR_DECL of the vtable for the type_info derived class.
72 This is only filled in at the end of the translation. */
73 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
74 /* The IDENTIFIER_NODE naming the real class. */
75 #define TINFO_REAL_NAME(NODE) TREE_PURPOSE (NODE)
77 /* A vector of all tinfo decls that haven't yet been emitted. */
78 VEC(tree
,gc
) *unemitted_tinfo_decls
;
80 static tree
build_headof (tree
);
81 static tree
ifnonnull (tree
, tree
);
82 static tree
tinfo_name (tree
);
83 static tree
build_dynamic_cast_1 (tree
, tree
);
84 static tree
throw_bad_cast (void);
85 static tree
throw_bad_typeid (void);
86 static tree
get_tinfo_decl_dynamic (tree
);
87 static tree
get_tinfo_ptr (tree
);
88 static bool typeid_ok_p (void);
89 static int qualifier_flags (tree
);
90 static bool target_incomplete_p (tree
);
91 static tree
tinfo_base_init (tree
, tree
);
92 static tree
generic_initializer (tree
, tree
);
93 static tree
class_initializer (tree
, tree
, tree
);
94 static tree
create_pseudo_type_info (const char *, int, ...);
95 static tree
get_pseudo_ti_init (tree
, tree
);
96 static tree
get_pseudo_ti_desc (tree
);
97 static void create_tinfo_types (void);
98 static bool typeinfo_in_lib_p (tree
);
100 static int doing_runtime
= 0;
103 /* Declare language defined type_info type and a pointer to const
104 type_info. This is incomplete here, and will be completed when
105 the user #includes <typeinfo>. There are language defined
106 restrictions on what can be done until that is included. Create
107 the internal versions of the ABI types. */
110 init_rtti_processing (void)
114 push_namespace (std_identifier
);
115 type_info_type
= xref_tag (class_type
, get_identifier ("type_info"),
116 /*tag_scope=*/ts_current
, false);
118 const_type_info_type_node
119 = build_qualified_type (type_info_type
, TYPE_QUAL_CONST
);
120 type_info_ptr_type
= build_pointer_type (const_type_info_type_node
);
122 unemitted_tinfo_decls
= VEC_alloc (tree
, gc
, 124);
124 create_tinfo_types ();
127 /* Given the expression EXP of type `class *', return the head of the
128 object pointed to by EXP with type cv void*, if the class has any
129 virtual functions (TYPE_POLYMORPHIC_P), else just return the
133 build_headof (tree exp
)
135 tree type
= TREE_TYPE (exp
);
139 gcc_assert (TREE_CODE (type
) == POINTER_TYPE
);
140 type
= TREE_TYPE (type
);
142 if (!TYPE_POLYMORPHIC_P (type
))
145 /* We use this a couple of times below, protect it. */
146 exp
= save_expr (exp
);
148 /* The offset-to-top field is at index -2 from the vptr. */
149 index
= build_int_cst (NULL_TREE
,
150 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
152 offset
= build_vtbl_ref (build_indirect_ref (exp
, NULL
), index
);
154 type
= build_qualified_type (ptr_type_node
,
155 cp_type_quals (TREE_TYPE (exp
)));
156 return build2 (PLUS_EXPR
, type
, exp
,
157 convert_to_integer (ptrdiff_type_node
, offset
));
160 /* Get a bad_cast node for the program to throw...
162 See libstdc++/exception.cc for __throw_bad_cast */
165 throw_bad_cast (void)
167 tree fn
= get_identifier ("__cxa_bad_cast");
168 if (!get_global_value_if_present (fn
, &fn
))
169 fn
= push_throw_library_fn (fn
, build_function_type (ptr_type_node
,
172 return build_cxx_call (fn
, NULL_TREE
);
175 /* Return an expression for "__cxa_bad_typeid()". The expression
176 returned is an lvalue of type "const std::type_info". */
179 throw_bad_typeid (void)
181 tree fn
= get_identifier ("__cxa_bad_typeid");
182 if (!get_global_value_if_present (fn
, &fn
))
186 t
= build_reference_type (const_type_info_type_node
);
187 t
= build_function_type (t
, void_list_node
);
188 fn
= push_throw_library_fn (fn
, t
);
191 return build_cxx_call (fn
, NULL_TREE
);
194 /* Return an lvalue expression whose type is "const std::type_info"
195 and whose value indicates the type of the expression EXP. If EXP
196 is a reference to a polymorphic class, return the dynamic type;
197 otherwise return the static type of the expression. */
200 get_tinfo_decl_dynamic (tree exp
)
205 if (exp
== error_mark_node
)
206 return error_mark_node
;
208 /* peel back references, so they match. */
209 type
= non_reference (TREE_TYPE (exp
));
211 /* Peel off cv qualifiers. */
212 type
= TYPE_MAIN_VARIANT (type
);
214 if (!VOID_TYPE_P (type
))
215 type
= complete_type_or_else (type
, exp
);
218 return error_mark_node
;
220 /* If exp is a reference to polymorphic type, get the real type_info. */
221 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
223 /* build reference to type_info from vtable. */
226 /* The RTTI information is at index -1. */
227 index
= build_int_cst (NULL_TREE
,
228 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
229 t
= build_vtbl_ref (exp
, index
);
230 t
= convert (type_info_ptr_type
, t
);
233 /* Otherwise return the type_info for the static type of the expr. */
234 t
= get_tinfo_ptr (TYPE_MAIN_VARIANT (type
));
236 return build_indirect_ref (t
, NULL
);
244 error ("cannot use typeid with -fno-rtti");
248 if (!COMPLETE_TYPE_P (const_type_info_type_node
))
250 error ("must #include <typeinfo> before using typeid");
257 /* Return an expression for "typeid(EXP)". The expression returned is
258 an lvalue of type "const std::type_info". */
261 build_typeid (tree exp
)
263 tree cond
= NULL_TREE
;
266 if (exp
== error_mark_node
|| !typeid_ok_p ())
267 return error_mark_node
;
269 if (processing_template_decl
)
270 return build_min (TYPEID_EXPR
, const_type_info_type_node
, exp
);
272 if (TREE_CODE (exp
) == INDIRECT_REF
273 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == POINTER_TYPE
274 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
275 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
278 exp
= stabilize_reference (exp
);
279 cond
= cp_convert (boolean_type_node
, TREE_OPERAND (exp
, 0));
282 exp
= get_tinfo_decl_dynamic (exp
);
284 if (exp
== error_mark_node
)
285 return error_mark_node
;
289 tree bad
= throw_bad_typeid ();
291 exp
= build3 (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
297 /* Generate the NTBS name of a type. */
299 tinfo_name (tree type
)
304 name
= mangle_type_string (type
);
305 name_string
= fix_string_type (build_string (strlen (name
) + 1, name
));
309 /* Return a VAR_DECL for the internal ABI defined type_info object for
310 TYPE. You must arrange that the decl is mark_used, if actually use
311 it --- decls in vtables are only used if the vtable is output. */
314 get_tinfo_decl (tree type
)
319 if (COMPLETE_TYPE_P (type
)
320 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
322 error ("cannot create type information for type %qT because "
323 "its size is variable",
325 return error_mark_node
;
328 if (TREE_CODE (type
) == METHOD_TYPE
)
329 type
= build_function_type (TREE_TYPE (type
),
330 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
332 /* For a class type, the variable is cached in the type node
334 if (CLASS_TYPE_P (type
))
336 d
= CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
));
341 name
= mangle_typeinfo_for_type (type
);
343 d
= IDENTIFIER_GLOBAL_VALUE (name
);
346 tree var_desc
= get_pseudo_ti_desc (type
);
348 d
= build_lang_decl (VAR_DECL
, name
, TINFO_PSEUDO_TYPE (var_desc
));
349 SET_DECL_ASSEMBLER_NAME (d
, name
);
350 /* Remember the type it is for. */
351 TREE_TYPE (name
) = type
;
352 DECL_TINFO_P (d
) = 1;
353 DECL_ARTIFICIAL (d
) = 1;
354 DECL_IGNORED_P (d
) = 1;
355 TREE_READONLY (d
) = 1;
357 /* Mark the variable as undefined -- but remember that we can
358 define it later if we need to do so. */
359 DECL_EXTERNAL (d
) = 1;
360 DECL_NOT_REALLY_EXTERN (d
) = 1;
361 if (CLASS_TYPE_P (type
))
362 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
)) = d
;
363 set_linkage_according_to_type (type
, d
);
364 pushdecl_top_level_and_finish (d
, NULL_TREE
);
366 /* Add decl to the global array of tinfo decls. */
367 VEC_safe_push (tree
, gc
, unemitted_tinfo_decls
, d
);
373 /* Return a pointer to a type_info object describing TYPE, suitably
374 cast to the language defined type. */
377 get_tinfo_ptr (tree type
)
379 tree decl
= get_tinfo_decl (type
);
382 return build_nop (type_info_ptr_type
,
383 build_address (decl
));
386 /* Return the type_info object for TYPE. */
389 get_typeid (tree type
)
391 if (type
== error_mark_node
|| !typeid_ok_p ())
392 return error_mark_node
;
394 if (processing_template_decl
)
395 return build_min (TYPEID_EXPR
, const_type_info_type_node
, type
);
397 /* If the type of the type-id is a reference type, the result of the
398 typeid expression refers to a type_info object representing the
400 type
= non_reference (type
);
402 /* The top-level cv-qualifiers of the lvalue expression or the type-id
403 that is the operand of typeid are always ignored. */
404 type
= TYPE_MAIN_VARIANT (type
);
406 if (!VOID_TYPE_P (type
))
407 type
= complete_type_or_else (type
, NULL_TREE
);
410 return error_mark_node
;
412 return build_indirect_ref (get_tinfo_ptr (type
), NULL
);
415 /* Check whether TEST is null before returning RESULT. If TEST is used in
416 RESULT, it must have previously had a save_expr applied to it. */
419 ifnonnull (tree test
, tree result
)
421 return build3 (COND_EXPR
, TREE_TYPE (result
),
422 build2 (EQ_EXPR
, boolean_type_node
, test
,
423 cp_convert (TREE_TYPE (test
), integer_zero_node
)),
424 cp_convert (TREE_TYPE (result
), integer_zero_node
),
428 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
432 build_dynamic_cast_1 (tree type
, tree expr
)
434 enum tree_code tc
= TREE_CODE (type
);
435 tree exprtype
= TREE_TYPE (expr
);
437 tree old_expr
= expr
;
438 const char *errstr
= NULL
;
440 /* T shall be a pointer or reference to a complete class type, or
441 `pointer to cv void''. */
445 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
449 if (! IS_AGGR_TYPE (TREE_TYPE (type
)))
451 errstr
= "target is not pointer or reference to class";
454 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
456 errstr
= "target is not pointer or reference to complete type";
462 errstr
= "target is not pointer or reference";
466 if (tc
== POINTER_TYPE
)
468 /* If T is a pointer type, v shall be an rvalue of a pointer to
469 complete class type, and the result is an rvalue of type T. */
471 if (TREE_CODE (exprtype
) != POINTER_TYPE
)
473 errstr
= "source is not a pointer";
476 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
478 errstr
= "source is not a pointer to class";
481 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
483 errstr
= "source is a pointer to incomplete type";
489 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
490 exprtype
= build_reference_type (exprtype
);
491 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
492 LOOKUP_NORMAL
, NULL_TREE
);
494 /* T is a reference type, v shall be an lvalue of a complete class
495 type, and the result is an lvalue of the type referred to by T. */
497 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
499 errstr
= "source is not of class type";
502 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
504 errstr
= "source is of incomplete class type";
510 /* The dynamic_cast operator shall not cast away constness. */
511 if (!at_least_as_qualified_p (TREE_TYPE (type
),
512 TREE_TYPE (exprtype
)))
514 errstr
= "conversion casts away constness";
518 /* If *type is an unambiguous accessible base class of *exprtype,
519 convert statically. */
523 binfo
= lookup_base (TREE_TYPE (exprtype
), TREE_TYPE (type
),
528 expr
= build_base_path (PLUS_EXPR
, convert_from_reference (expr
),
530 if (TREE_CODE (exprtype
) == POINTER_TYPE
)
531 expr
= non_lvalue (expr
);
536 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
537 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
540 /* if TYPE is `void *', return pointer to complete object. */
541 if (tc
== POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (type
)))
543 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
544 if (TREE_CODE (expr
) == ADDR_EXPR
545 && TREE_CODE (TREE_OPERAND (expr
, 0)) == VAR_DECL
546 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
547 return build1 (NOP_EXPR
, type
, expr
);
549 /* Since expr is used twice below, save it. */
550 expr
= save_expr (expr
);
552 expr1
= build_headof (expr
);
553 if (TREE_TYPE (expr1
) != type
)
554 expr1
= build1 (NOP_EXPR
, type
, expr1
);
555 return ifnonnull (expr
, expr1
);
560 tree result
, td2
, td3
, elems
;
561 tree static_type
, target_type
, boff
;
563 /* If we got here, we can't convert statically. Therefore,
564 dynamic_cast<D&>(b) (b an object) cannot succeed. */
565 if (tc
== REFERENCE_TYPE
)
567 if (TREE_CODE (old_expr
) == VAR_DECL
568 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
570 tree expr
= throw_bad_cast ();
571 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
573 /* Bash it to the expected type. */
574 TREE_TYPE (expr
) = type
;
578 /* Ditto for dynamic_cast<D*>(&b). */
579 else if (TREE_CODE (expr
) == ADDR_EXPR
)
581 tree op
= TREE_OPERAND (expr
, 0);
582 if (TREE_CODE (op
) == VAR_DECL
583 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
585 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
587 retval
= build_int_cst (type
, 0);
592 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
593 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
594 td2
= get_tinfo_decl (target_type
);
596 td2
= build_unary_op (ADDR_EXPR
, td2
, 0);
597 td3
= get_tinfo_decl (static_type
);
599 td3
= build_unary_op (ADDR_EXPR
, td3
, 0);
601 /* Determine how T and V are related. */
602 boff
= dcast_base_hint (static_type
, target_type
);
604 /* Since expr is used twice below, save it. */
605 expr
= save_expr (expr
);
608 if (tc
== REFERENCE_TYPE
)
609 expr1
= build_unary_op (ADDR_EXPR
, expr1
, 0);
612 (NULL_TREE
, expr1
, tree_cons
613 (NULL_TREE
, td3
, tree_cons
614 (NULL_TREE
, td2
, tree_cons
615 (NULL_TREE
, boff
, NULL_TREE
))));
617 dcast_fn
= dynamic_cast_node
;
625 push_nested_namespace (ns
);
626 tinfo_ptr
= xref_tag (class_type
,
627 get_identifier ("__class_type_info"),
628 /*tag_scope=*/ts_current
, false);
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 DECL_IS_PURE (dcast_fn
) = 1;
642 pop_nested_namespace (ns
);
643 dynamic_cast_node
= dcast_fn
;
645 result
= build_cxx_call (dcast_fn
, elems
);
647 if (tc
== REFERENCE_TYPE
)
649 tree bad
= throw_bad_cast ();
651 result
= save_expr (result
);
652 return build3 (COND_EXPR
, type
, result
, result
, bad
);
655 /* Now back to the type we want from a void*. */
656 result
= cp_convert (type
, result
);
657 return ifnonnull (expr
, result
);
661 errstr
= "source type is not polymorphic";
664 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
665 expr
, exprtype
, type
, errstr
);
666 return error_mark_node
;
670 build_dynamic_cast (tree type
, tree expr
)
672 if (type
== error_mark_node
|| expr
== error_mark_node
)
673 return error_mark_node
;
675 if (processing_template_decl
)
677 expr
= build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
678 TREE_SIDE_EFFECTS (expr
) = 1;
683 return convert_from_reference (build_dynamic_cast_1 (type
, expr
));
686 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
689 qualifier_flags (tree type
)
692 int quals
= cp_type_quals (type
);
694 if (quals
& TYPE_QUAL_CONST
)
696 if (quals
& TYPE_QUAL_VOLATILE
)
698 if (quals
& TYPE_QUAL_RESTRICT
)
703 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
704 contains a pointer to member of an incomplete class. */
707 target_incomplete_p (tree type
)
710 if (TYPE_PTRMEM_P (type
))
712 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)))
714 type
= TYPE_PTRMEM_POINTED_TO_TYPE (type
);
716 else if (TREE_CODE (type
) == POINTER_TYPE
)
717 type
= TREE_TYPE (type
);
719 return !COMPLETE_OR_VOID_TYPE_P (type
);
722 /* Returns true if TYPE involves an incomplete class type; in that
723 case, typeinfo variables for TYPE should be emitted with internal
727 involves_incomplete_p (tree type
)
729 switch (TREE_CODE (type
))
732 return target_incomplete_p (TREE_TYPE (type
));
737 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type
))
738 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)));
741 if (TYPE_PTRMEMFUNC_P (type
))
745 if (!COMPLETE_TYPE_P (type
))
749 /* All other types do not involve incomplete class types. */
754 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
755 is the vtable pointer and NTBS name. The NTBS name is emitted as a
756 comdat const char array, so it becomes a unique key for the type. Generate
757 and emit that VAR_DECL here. (We can't always emit the type_info itself
758 as comdat, because of pointers to incomplete.) */
761 tinfo_base_init (tree desc
, tree target
)
763 tree init
= NULL_TREE
;
770 /* Generate the NTBS array variable. */
771 tree name_type
= build_cplus_array_type
772 (build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
774 tree name_string
= tinfo_name (target
);
776 /* Determine the name of the variable -- and remember with which
777 type it is associated. */
778 name_name
= mangle_typeinfo_string_for_type (target
);
779 TREE_TYPE (name_name
) = target
;
781 name_decl
= build_lang_decl (VAR_DECL
, name_name
, name_type
);
782 SET_DECL_ASSEMBLER_NAME (name_decl
, name_name
);
783 DECL_ARTIFICIAL (name_decl
) = 1;
784 DECL_IGNORED_P (name_decl
) = 1;
785 TREE_READONLY (name_decl
) = 1;
786 TREE_STATIC (name_decl
) = 1;
787 DECL_EXTERNAL (name_decl
) = 0;
788 DECL_TINFO_P (name_decl
) = 1;
789 if (involves_incomplete_p (target
))
791 TREE_PUBLIC (name_decl
) = 0;
792 DECL_INTERFACE_KNOWN (name_decl
) = 1;
795 set_linkage_according_to_type (target
, name_decl
);
796 import_export_decl (name_decl
);
797 DECL_INITIAL (name_decl
) = name_string
;
798 mark_used (name_decl
);
799 pushdecl_top_level_and_finish (name_decl
, name_string
);
802 vtable_ptr
= TINFO_VTABLE_DECL (desc
);
807 push_nested_namespace (abi_node
);
808 real_type
= xref_tag (class_type
, TINFO_REAL_NAME (desc
),
809 /*tag_scope=*/ts_current
, false);
810 pop_nested_namespace (abi_node
);
812 if (!COMPLETE_TYPE_P (real_type
))
814 /* We never saw a definition of this type, so we need to
815 tell the compiler that this is an exported class, as
816 indeed all of the __*_type_info classes are. */
817 SET_CLASSTYPE_INTERFACE_KNOWN (real_type
);
818 CLASSTYPE_INTERFACE_ONLY (real_type
) = 1;
821 vtable_ptr
= get_vtable_decl (real_type
, /*complete=*/1);
822 vtable_ptr
= build_unary_op (ADDR_EXPR
, vtable_ptr
, 0);
824 /* We need to point into the middle of the vtable. */
826 (PLUS_EXPR
, TREE_TYPE (vtable_ptr
), vtable_ptr
,
827 size_binop (MULT_EXPR
,
828 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
),
829 TYPE_SIZE_UNIT (vtable_entry_type
)));
831 TINFO_VTABLE_DECL (desc
) = vtable_ptr
;
834 init
= tree_cons (NULL_TREE
, vtable_ptr
, init
);
836 init
= tree_cons (NULL_TREE
, decay_conversion (name_decl
), init
);
838 init
= build_constructor (NULL_TREE
, nreverse (init
));
839 TREE_CONSTANT (init
) = 1;
840 TREE_INVARIANT (init
) = 1;
841 TREE_STATIC (init
) = 1;
842 init
= tree_cons (NULL_TREE
, init
, NULL_TREE
);
847 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
848 information about the particular type_info derivation, which adds no
849 additional fields to the type_info base. */
852 generic_initializer (tree desc
, tree target
)
854 tree init
= tinfo_base_init (desc
, target
);
856 init
= build_constructor (NULL_TREE
, init
);
857 TREE_CONSTANT (init
) = 1;
858 TREE_INVARIANT (init
) = 1;
859 TREE_STATIC (init
) = 1;
863 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
864 DESC provides information about the particular type_info derivation,
865 which adds target type and qualifier flags members to the type_info base. */
868 ptr_initializer (tree desc
, tree target
)
870 tree init
= tinfo_base_init (desc
, target
);
871 tree to
= TREE_TYPE (target
);
872 int flags
= qualifier_flags (to
);
873 bool incomplete
= target_incomplete_p (to
);
877 init
= tree_cons (NULL_TREE
, build_int_cst (NULL_TREE
, flags
), init
);
878 init
= tree_cons (NULL_TREE
,
879 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)),
882 init
= build_constructor (NULL_TREE
, nreverse (init
));
883 TREE_CONSTANT (init
) = 1;
884 TREE_INVARIANT (init
) = 1;
885 TREE_STATIC (init
) = 1;
889 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
890 DESC provides information about the particular type_info derivation,
891 which adds class, target type and qualifier flags members to the type_info
895 ptm_initializer (tree desc
, tree target
)
897 tree init
= tinfo_base_init (desc
, target
);
898 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
899 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
900 int flags
= qualifier_flags (to
);
901 bool incomplete
= target_incomplete_p (to
);
905 if (!COMPLETE_TYPE_P (klass
))
907 init
= tree_cons (NULL_TREE
, build_int_cst (NULL_TREE
, flags
), init
);
908 init
= tree_cons (NULL_TREE
,
909 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)),
911 init
= tree_cons (NULL_TREE
,
912 get_tinfo_ptr (klass
),
915 init
= build_constructor (NULL_TREE
, nreverse (init
));
916 TREE_CONSTANT (init
) = 1;
917 TREE_INVARIANT (init
) = 1;
918 TREE_STATIC (init
) = 1;
922 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
923 DESC provides information about the particular __class_type_info derivation,
924 which adds hint flags and TRAIL initializers to the type_info base. */
927 class_initializer (tree desc
, tree target
, tree trail
)
929 tree init
= tinfo_base_init (desc
, target
);
931 TREE_CHAIN (init
) = trail
;
932 init
= build_constructor (NULL_TREE
, init
);
933 TREE_CONSTANT (init
) = 1;
934 TREE_INVARIANT (init
) = 1;
935 TREE_STATIC (init
) = 1;
939 /* Returns true if the typeinfo for type should be placed in
940 the runtime library. */
943 typeinfo_in_lib_p (tree type
)
945 /* The typeinfo objects for `T*' and `const T*' are in the runtime
946 library for simple types T. */
947 if (TREE_CODE (type
) == POINTER_TYPE
948 && (cp_type_quals (TREE_TYPE (type
)) == TYPE_QUAL_CONST
949 || cp_type_quals (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
))
950 type
= TREE_TYPE (type
);
952 switch (TREE_CODE (type
))
966 /* Generate the initializer for the type info describing TYPE. */
969 get_pseudo_ti_init (tree type
, tree var_desc
)
972 switch (TREE_CODE (type
))
975 return ptm_initializer (var_desc
, type
);
977 return ptr_initializer (var_desc
, type
);
979 return generic_initializer (var_desc
, type
);
982 return generic_initializer (var_desc
, type
);
985 return generic_initializer (var_desc
, type
);
989 if (TYPE_PTRMEMFUNC_P (type
))
990 return ptm_initializer (var_desc
, type
);
991 else if (var_desc
== class_desc_type_node
)
992 return class_initializer (var_desc
, type
, NULL_TREE
);
993 else if (var_desc
== si_class_desc_type_node
)
995 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), 0);
996 tree tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
997 tree base_inits
= tree_cons (NULL_TREE
, tinfo
, NULL_TREE
);
999 return class_initializer (var_desc
, type
, base_inits
);
1003 int hint
= ((CLASSTYPE_REPEATED_BASE_P (type
) << 0)
1004 | (CLASSTYPE_DIAMOND_SHAPED_P (type
) << 1));
1005 tree binfo
= TYPE_BINFO (type
);
1006 int nbases
= BINFO_N_BASE_BINFOS (binfo
);
1007 VEC(tree
,gc
) *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1008 tree base_inits
= NULL_TREE
;
1011 /* Generate the base information initializer. */
1012 for (ix
= nbases
; ix
--;)
1014 tree base_binfo
= BINFO_BASE_BINFO (binfo
, ix
);
1015 tree base_init
= NULL_TREE
;
1020 if (VEC_index (tree
, base_accesses
, ix
) == access_public_node
)
1022 tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
1023 if (BINFO_VIRTUAL_P (base_binfo
))
1025 /* We store the vtable offset at which the virtual
1026 base offset can be found. */
1027 offset
= BINFO_VPTR_FIELD (base_binfo
);
1028 offset
= convert (sizetype
, offset
);
1032 offset
= BINFO_OFFSET (base_binfo
);
1034 /* Combine offset and flags into one field. */
1035 offset
= cp_build_binary_op (LSHIFT_EXPR
, offset
,
1036 build_int_cst (NULL_TREE
, 8));
1037 offset
= cp_build_binary_op (BIT_IOR_EXPR
, offset
,
1038 build_int_cst (NULL_TREE
, flags
));
1039 base_init
= tree_cons (NULL_TREE
, offset
, base_init
);
1040 base_init
= tree_cons (NULL_TREE
, tinfo
, base_init
);
1041 base_init
= build_constructor (NULL_TREE
, base_init
);
1042 base_inits
= tree_cons (NULL_TREE
, base_init
, base_inits
);
1044 base_inits
= build_constructor (NULL_TREE
, base_inits
);
1045 base_inits
= tree_cons (NULL_TREE
, base_inits
, NULL_TREE
);
1046 /* Prepend the number of bases. */
1047 base_inits
= tree_cons (NULL_TREE
,
1048 build_int_cst (NULL_TREE
, nbases
),
1050 /* Prepend the hint flags. */
1051 base_inits
= tree_cons (NULL_TREE
,
1052 build_int_cst (NULL_TREE
, hint
),
1055 return class_initializer (var_desc
, type
, base_inits
);
1060 return generic_initializer (var_desc
, type
);
1064 /* Generate the RECORD_TYPE containing the data layout of a type_info
1065 derivative as used by the runtime. This layout must be consistent with
1066 that defined in the runtime support. Also generate the VAR_DECL for the
1067 type's vtable. We explicitly manage the vtable member, and name it for
1068 real type as used in the runtime. The RECORD type has a different name,
1069 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1070 is the generated type and TINFO_VTABLE_NAME is the name of the
1071 vtable. We have to delay generating the VAR_DECL of the vtable
1072 until the end of the translation, when we'll have seen the library
1073 definition, if there was one.
1075 REAL_NAME is the runtime's name of the type. Trailing arguments are
1076 additional FIELD_DECL's for the structure. The final argument must be
1080 create_pseudo_type_info (const char *real_name
, int ident
, ...)
1089 va_start (ap
, ident
);
1091 /* Generate the pseudo type name. */
1092 pseudo_name
= alloca (strlen (real_name
) + 30);
1093 strcpy (pseudo_name
, real_name
);
1094 strcat (pseudo_name
, "_pseudo");
1096 sprintf (pseudo_name
+ strlen (pseudo_name
), "%d", ident
);
1098 /* First field is the pseudo type_info base class. */
1099 fields
= build_decl (FIELD_DECL
, NULL_TREE
, ti_desc_type_node
);
1101 /* Now add the derived fields. */
1102 while ((field_decl
= va_arg (ap
, tree
)))
1104 TREE_CHAIN (field_decl
) = fields
;
1105 fields
= field_decl
;
1108 /* Create the pseudo type. */
1109 pseudo_type
= make_aggr_type (RECORD_TYPE
);
1110 finish_builtin_struct (pseudo_type
, pseudo_name
, fields
, NULL_TREE
);
1111 CLASSTYPE_AS_BASE (pseudo_type
) = pseudo_type
;
1113 result
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
1114 TINFO_REAL_NAME (result
) = get_identifier (real_name
);
1115 TINFO_PSEUDO_TYPE (result
) =
1116 cp_build_qualified_type (pseudo_type
, TYPE_QUAL_CONST
);
1122 /* Return a pseudo type info type node used to describe TYPE. TYPE
1123 must be a complete type (or cv void), except at the end of the
1124 translation unit. */
1127 get_pseudo_ti_desc (tree type
)
1129 switch (TREE_CODE (type
))
1132 return ptm_desc_type_node
;
1134 return ptr_desc_type_node
;
1136 return enum_desc_type_node
;
1138 return func_desc_type_node
;
1140 return ary_desc_type_node
;
1143 if (TYPE_PTRMEMFUNC_P (type
))
1144 return ptm_desc_type_node
;
1145 else if (!COMPLETE_TYPE_P (type
))
1148 cxx_incomplete_type_error (NULL_TREE
, type
);
1149 return class_desc_type_node
;
1151 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)))
1152 return class_desc_type_node
;
1155 tree binfo
= TYPE_BINFO (type
);
1156 VEC(tree
,gc
) *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1157 tree base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
1158 int num_bases
= BINFO_N_BASE_BINFOS (binfo
);
1161 && VEC_index (tree
, base_accesses
, 0) == access_public_node
1162 && !BINFO_VIRTUAL_P (base_binfo
)
1163 && integer_zerop (BINFO_OFFSET (base_binfo
)))
1164 /* single non-virtual public. */
1165 return si_class_desc_type_node
;
1169 tree array_domain
, base_array
;
1171 if (TREE_VEC_LENGTH (vmi_class_desc_type_node
) <= num_bases
)
1174 tree extend
= make_tree_vec (num_bases
+ 5);
1176 for (ix
= TREE_VEC_LENGTH (vmi_class_desc_type_node
); ix
--;)
1177 TREE_VEC_ELT (extend
, ix
)
1178 = TREE_VEC_ELT (vmi_class_desc_type_node
, ix
);
1179 vmi_class_desc_type_node
= extend
;
1181 var_desc
= TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
);
1185 /* Create the array of __base_class_type_info entries.
1186 G++ 3.2 allocated an array that had one too many
1187 entries, and then filled that extra entries with
1189 if (abi_version_at_least (2))
1190 array_domain
= build_index_type (size_int (num_bases
- 1));
1192 array_domain
= build_index_type (size_int (num_bases
));
1194 build_array_type (base_desc_type_node
, array_domain
);
1196 push_nested_namespace (abi_node
);
1197 var_desc
= create_pseudo_type_info
1198 ("__vmi_class_type_info", num_bases
,
1199 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1200 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1201 build_decl (FIELD_DECL
, NULL_TREE
, base_array
),
1203 pop_nested_namespace (abi_node
);
1205 TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
) = var_desc
;
1210 return bltn_desc_type_node
;
1214 /* Make sure the required builtin types exist for generating the type_info
1215 variable definitions. */
1218 create_tinfo_types (void)
1220 gcc_assert (!ti_desc_type_node
);
1222 push_nested_namespace (abi_node
);
1224 /* Create the internal type_info structure. This is used as a base for
1225 the other structures. */
1229 ti_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1230 field
= build_decl (FIELD_DECL
, NULL_TREE
, const_ptr_type_node
);
1233 field
= build_decl (FIELD_DECL
, NULL_TREE
, const_string_type_node
);
1234 TREE_CHAIN (field
) = fields
;
1237 finish_builtin_struct (ti_desc_type_node
, "__type_info_pseudo",
1239 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node
) = 1;
1242 /* Fundamental type_info */
1243 bltn_desc_type_node
= create_pseudo_type_info
1244 ("__fundamental_type_info", 0,
1247 /* Array, function and enum type_info. No additional fields. */
1248 ary_desc_type_node
= create_pseudo_type_info
1249 ("__array_type_info", 0,
1251 func_desc_type_node
= create_pseudo_type_info
1252 ("__function_type_info", 0,
1254 enum_desc_type_node
= create_pseudo_type_info
1255 ("__enum_type_info", 0,
1258 /* Class type_info. Add a flags field. */
1259 class_desc_type_node
= create_pseudo_type_info
1260 ("__class_type_info", 0,
1263 /* Single public non-virtual base class. Add pointer to base class.
1264 This is really a descendant of __class_type_info. */
1265 si_class_desc_type_node
= create_pseudo_type_info
1266 ("__si_class_type_info", 0,
1267 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1270 /* Base class internal helper. Pointer to base type, offset to base,
1275 field
= build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
);
1278 field
= build_decl (FIELD_DECL
, NULL_TREE
, integer_types
[itk_long
]);
1279 TREE_CHAIN (field
) = fields
;
1282 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1283 finish_builtin_struct (base_desc_type_node
, "__base_class_type_info_pseudo",
1285 TYPE_HAS_CONSTRUCTOR (base_desc_type_node
) = 1;
1288 /* General hierarchy is created as necessary in this vector. */
1289 vmi_class_desc_type_node
= make_tree_vec (10);
1291 /* Pointer type_info. Adds two fields, qualification mask
1292 and pointer to the pointed to type. This is really a descendant of
1293 __pbase_type_info. */
1294 ptr_desc_type_node
= create_pseudo_type_info
1295 ("__pointer_type_info", 0,
1296 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1297 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1300 /* Pointer to member data type_info. Add qualifications flags,
1301 pointer to the member's type info and pointer to the class.
1302 This is really a descendant of __pbase_type_info. */
1303 ptm_desc_type_node
= create_pseudo_type_info
1304 ("__pointer_to_member_type_info", 0,
1305 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1306 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1307 build_decl (FIELD_DECL
, NULL_TREE
, type_info_ptr_type
),
1310 pop_nested_namespace (abi_node
);
1313 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1314 support. Generating them here guarantees consistency with the other
1315 structures. We use the following heuristic to determine when the runtime
1316 is being generated. If std::__fundamental_type_info is defined, and its
1317 destructor is defined, then the runtime is being built. */
1320 emit_support_tinfos (void)
1322 static tree
*const fundamentals
[] =
1327 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1328 &short_integer_type_node
, &short_unsigned_type_node
,
1329 &integer_type_node
, &unsigned_type_node
,
1330 &long_integer_type_node
, &long_unsigned_type_node
,
1331 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1332 &float_type_node
, &double_type_node
, &long_double_type_node
,
1336 tree bltn_type
, dtor
;
1338 push_nested_namespace (abi_node
);
1339 bltn_type
= xref_tag (class_type
,
1340 get_identifier ("__fundamental_type_info"),
1341 /*tag_scope=*/ts_current
, false);
1342 pop_nested_namespace (abi_node
);
1343 if (!COMPLETE_TYPE_P (bltn_type
))
1345 dtor
= CLASSTYPE_DESTRUCTORS (bltn_type
);
1346 if (!dtor
|| DECL_EXTERNAL (dtor
))
1349 for (ix
= 0; fundamentals
[ix
]; ix
++)
1351 tree bltn
= *fundamentals
[ix
];
1356 types
[1] = build_pointer_type (bltn
);
1357 types
[2] = build_pointer_type (build_qualified_type (bltn
,
1360 for (i
= 0; i
< 3; ++i
)
1364 tinfo
= get_tinfo_decl (types
[i
]);
1365 TREE_USED (tinfo
) = 1;
1366 mark_needed (tinfo
);
1367 /* The C++ ABI requires that these objects be COMDAT. But,
1368 On systems without weak symbols, initialized COMDAT
1369 objects are emitted with internal linkage. (See
1370 comdat_linkage for details.) Since we want these objects
1371 to have external linkage so that copies do not have to be
1372 emitted in code outside the runtime library, we make them
1376 gcc_assert (TREE_PUBLIC (tinfo
) && !DECL_COMDAT (tinfo
));
1377 DECL_INTERFACE_KNOWN (tinfo
) = 1;
1383 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1384 tinfo decl. Determine whether it needs emitting, and if so
1385 generate the initializer. */
1388 emit_tinfo_decl (tree decl
)
1390 tree type
= TREE_TYPE (DECL_NAME (decl
));
1391 int in_library
= typeinfo_in_lib_p (type
);
1392 tree var_desc
, var_init
;
1394 gcc_assert (DECL_TINFO_P (decl
));
1399 DECL_EXTERNAL (decl
) = 0;
1402 /* If we're not in the runtime, then DECL (which is already
1403 DECL_EXTERNAL) will not be defined here. */
1404 DECL_INTERFACE_KNOWN (decl
) = 1;
1408 else if (involves_incomplete_p (type
))
1410 if (!decl_needed_p (decl
))
1412 /* If TYPE involves an incomplete class type, then the typeinfo
1413 object will be emitted with internal linkage. There is no
1414 way to know whether or not types are incomplete until the end
1415 of the compilation, so this determination must be deferred
1416 until this point. */
1417 TREE_PUBLIC (decl
) = 0;
1418 DECL_EXTERNAL (decl
) = 0;
1419 DECL_INTERFACE_KNOWN (decl
) = 1;
1422 import_export_decl (decl
);
1423 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
1425 DECL_EXTERNAL (decl
) = 0;
1426 var_desc
= get_pseudo_ti_desc (type
);
1427 var_init
= get_pseudo_ti_init (type
, var_desc
);
1428 DECL_INITIAL (decl
) = var_init
;
1430 cp_finish_decl (decl
, var_init
, NULL_TREE
, 0);