1 /* RunTime Type Identification
2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
3 Mostly written by Jason Merrill (jason@cygnus.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
34 /* C++ returns type information to the user in struct type_info
35 objects. We also use type information to implement dynamic_cast and
36 exception handlers. Type information for a particular type is
37 indicated with an ABI defined structure derived from type_info.
38 This would all be very straight forward, but for the fact that the
39 runtime library provides the definitions of the type_info structure
40 and the ABI defined derived classes. We cannot build declarations
41 of them directly in the compiler, but we need to layout objects of
42 their type. Somewhere we have to lie.
44 We define layout compatible POD-structs with compiler-defined names
45 and generate the appropriate initializations for them (complete
46 with explicit mention of their vtable). When we have to provide a
47 type_info to the user we reinterpret_cast the internal compiler
48 type to type_info. A well formed program can only explicitly refer
49 to the type_infos of complete types (& cv void). However, we chain
50 pointer type_infos to the pointed-to-type, and that can be
51 incomplete. We only need the addresses of such incomplete
52 type_info objects for static initialization.
54 The type information VAR_DECL of a type is held on the
55 get_global_binding of the type's mangled name. That VAR_DECL
56 will be the internal type. It will usually have the correct
57 internal type reflecting the kind of type it represents (pointer,
58 array, function, class, inherited class, etc). When the type it
59 represents is incomplete, it will have the internal type
60 corresponding to type_info. That will only happen at the end of
61 translation, when we are emitting the type info objects. */
63 /* Auxiliary data we hold for each type_info derived object we need. */
64 struct GTY (()) tinfo_s
{
65 tree type
; /* The (const-qualified) RECORD_TYPE for this type_info object */
67 tree vtable
; /* The VAR_DECL of the vtable. Only filled at end of
70 tree name
; /* IDENTIFIER_NODE for the ABI specified name of
71 the type_info derived type. */
77 TK_TYPE_INFO_TYPE
, /* abi::__type_info_pseudo */
78 TK_BASE_TYPE
, /* abi::__base_class_type_info */
79 TK_DERIVED_TYPES
, /* Start of types derived from abi::__type_info */
80 TK_BUILTIN_TYPE
= TK_DERIVED_TYPES
, /* abi::__fundamental_type_info */
81 TK_ARRAY_TYPE
, /* abi::__array_type_info */
82 TK_FUNCTION_TYPE
, /* abi::__function_type_info */
83 TK_ENUMERAL_TYPE
, /* abi::__enum_type_info */
84 TK_POINTER_TYPE
, /* abi::__pointer_type_info */
85 TK_POINTER_MEMBER_TYPE
, /* abi::__pointer_to_member_type_info */
86 TK_CLASS_TYPE
, /* abi::__class_type_info */
87 TK_SI_CLASS_TYPE
, /* abi::__si_class_type_info */
88 TK_VMI_CLASS_TYPES
, /* abi::__vmi_class_type_info<int> */
92 /* Names of the tinfo types. Must be same order as TK enumeration
95 static const char *const tinfo_names
[TK_MAX
] =
98 "__base_class_type_info",
99 "__fundamental_type_info",
101 "__function_type_info",
103 "__pointer_type_info",
104 "__pointer_to_member_type_info",
106 "__si_class_type_info",
107 "__vmi_class_type_info"
110 /* Helper macro to get maximum scalar-width of pointer or of the 'long'-type.
111 This of interest for llp64 targets. */
113 integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \
114 ? itk_long : itk_long_long)]
116 /* A vector of all tinfo decls that haven't yet been emitted. */
117 vec
<tree
, va_gc
> *unemitted_tinfo_decls
;
119 /* A vector of all type_info derived types we need. The first few are
120 fixed and created early. The remainder are for multiple inheritance
121 and are generated as needed. */
122 static GTY (()) vec
<tinfo_s
, va_gc
> *tinfo_descs
;
124 static tree
tinfo_name (tree
, bool);
125 static tree
build_dynamic_cast_1 (location_t
, tree
, tree
, tsubst_flags_t
);
126 static tree
throw_bad_cast (void);
127 static tree
throw_bad_typeid (void);
128 static bool typeid_ok_p (void);
129 static int qualifier_flags (tree
);
130 static bool target_incomplete_p (tree
);
131 static tree
tinfo_base_init (tinfo_s
*, tree
);
132 static tree
generic_initializer (tinfo_s
*, tree
);
133 static tree
ptr_initializer (tinfo_s
*, tree
);
134 static tree
ptm_initializer (tinfo_s
*, tree
);
135 static tree
class_initializer (tinfo_s
*, tree
, unsigned, ...);
136 static tree
get_pseudo_ti_init (tree
, unsigned);
137 static unsigned get_pseudo_ti_index (tree
);
138 static tinfo_s
*get_tinfo_desc (unsigned);
139 static void create_tinfo_types (void);
140 static bool typeinfo_in_lib_p (tree
);
142 static int doing_runtime
= 0;
144 /* Create the internal versions of the ABI types. */
147 init_rtti_processing (void)
149 vec_alloc (unemitted_tinfo_decls
, 124);
151 create_tinfo_types ();
154 /* Given the expression EXP of type `class *', return the head of the
155 object pointed to by EXP with type cv void*, if the class has any
156 virtual functions (TYPE_POLYMORPHIC_P), else just return the
160 build_headof (tree exp
)
162 tree type
= TREE_TYPE (exp
);
166 gcc_assert (TYPE_PTR_P (type
));
167 type
= TREE_TYPE (type
);
169 if (!TYPE_POLYMORPHIC_P (type
))
172 /* We use this a couple of times below, protect it. */
173 exp
= save_expr (exp
);
175 /* The offset-to-top field is at index -2 from the vptr. */
176 index
= build_int_cst (NULL_TREE
,
177 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
179 offset
= build_vtbl_ref (cp_build_fold_indirect_ref (exp
),
182 cp_build_qualified_type (ptr_type_node
,
183 cp_type_quals (TREE_TYPE (exp
)));
184 return fold_build_pointer_plus (exp
, offset
);
187 /* Get a bad_cast node for the program to throw...
189 See libstdc++/exception.cc for __throw_bad_cast */
192 throw_bad_cast (void)
197 tree name
= get_identifier ("__cxa_bad_cast");
198 fn
= get_global_binding (name
);
200 fn
= push_throw_library_fn
201 (name
, build_function_type_list (ptr_type_node
, NULL_TREE
));
204 return build_cxx_call (fn
, 0, NULL
, tf_warning_or_error
);
207 /* Return an expression for "__cxa_bad_typeid()". The expression
208 returned is an lvalue of type "const std::type_info". */
211 throw_bad_typeid (void)
216 tree name
= get_identifier ("__cxa_bad_typeid");
217 fn
= get_global_binding (name
);
220 tree t
= build_reference_type (const_type_info_type_node
);
221 t
= build_function_type_list (t
, NULL_TREE
);
222 fn
= push_throw_library_fn (name
, t
);
226 return build_cxx_call (fn
, 0, NULL
, tf_warning_or_error
);
229 /* const type_info*. */
232 type_info_ptr_type ()
234 return build_pointer_type (const_type_info_type_node
);
237 /* Return a pointer to a type_info object describing TYPE, suitably
238 cast to the language defined type (for typeid) or void (for building
239 up the descriptors). */
242 get_tinfo_ptr (tree type
, bool voidp
= false)
244 tree decl
= get_tinfo_decl (type
);
247 tree ptype
= voidp
? const_ptr_type_node
: type_info_ptr_type ();
248 return build_nop (ptype
, build_address (decl
));
251 get_void_tinfo_ptr (tree type
)
253 return get_tinfo_ptr (type
, true);
256 /* Return an lvalue expression whose type is "const std::type_info"
257 and whose value indicates the type of the expression EXP. If EXP
258 is a reference to a polymorphic class, return the dynamic type;
259 otherwise return the static type of the expression. */
262 get_tinfo_decl_dynamic (tree exp
, tsubst_flags_t complain
)
267 if (error_operand_p (exp
))
268 return error_mark_node
;
270 exp
= resolve_nondeduced_context (exp
, complain
);
272 /* Peel back references, so they match. */
273 type
= non_reference (unlowered_expr_type (exp
));
275 /* Peel off cv qualifiers. */
276 type
= cv_unqualified (type
);
278 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
279 if (CLASS_TYPE_P (type
) || type
== unknown_type_node
280 || type
== init_list_type_node
)
281 type
= complete_type_or_maybe_complain (type
, exp
, complain
);
284 return error_mark_node
;
286 /* If exp is a reference to polymorphic type, get the real type_info. */
287 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
289 /* build reference to type_info from vtable. */
292 /* The RTTI information is at index -1. */
293 index
= build_int_cst (NULL_TREE
,
294 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
295 t
= build_vtbl_ref (exp
, index
);
296 t
= convert (type_info_ptr_type (), t
);
299 /* Otherwise return the type_info for the static type of the expr. */
300 t
= get_tinfo_ptr (type
);
302 return cp_build_fold_indirect_ref (t
);
310 error ("cannot use %<typeid%> with %<-fno-rtti%>");
314 if (!const_type_info_type_node
)
316 tree name
= get_identifier ("type_info");
317 tree decl
= lookup_qualified_name (std_node
, name
);
318 if (TREE_CODE (decl
) != TYPE_DECL
)
320 gcc_rich_location
richloc (input_location
);
321 maybe_add_include_fixit (&richloc
, "<typeinfo>", false);
323 "must %<#include <typeinfo>%> before using"
328 const_type_info_type_node
329 = cp_build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_CONST
);
332 tree pseudo
= TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE
)->type
);
333 tree real
= TYPE_MAIN_VARIANT (const_type_info_type_node
);
335 /* Make sure abi::__type_info_pseudo has the same alias set
336 as std::type_info. */
337 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo
))
338 TYPE_ALIAS_SET (pseudo
) = get_alias_set (real
);
340 gcc_assert (TYPE_ALIAS_SET (pseudo
) == get_alias_set (real
));
345 /* Return an expression for "typeid(EXP)". The expression returned is
346 an lvalue of type "const std::type_info". */
349 build_typeid (tree exp
, tsubst_flags_t complain
)
351 tree cond
= NULL_TREE
, initial_expr
= exp
;
354 if (exp
== error_mark_node
|| !typeid_ok_p ())
355 return error_mark_node
;
357 if (processing_template_decl
)
358 return build_min (TYPEID_EXPR
, const_type_info_type_node
, exp
);
360 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
361 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
364 /* So we need to look into the vtable of the type of exp.
365 Make sure it isn't a null lvalue. */
366 exp
= cp_build_addr_expr (exp
, complain
);
367 exp
= save_expr (exp
);
368 cond
= cp_convert (boolean_type_node
, exp
, complain
);
369 exp
= cp_build_fold_indirect_ref (exp
);
372 exp
= get_tinfo_decl_dynamic (exp
, complain
);
374 if (exp
== error_mark_node
)
375 return error_mark_node
;
379 tree bad
= throw_bad_typeid ();
381 exp
= build3 (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
384 mark_type_use (initial_expr
);
389 /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that
390 comparisons will be done by pointer rather than string comparison. */
392 tinfo_name (tree type
, bool mark_private
)
398 name
= mangle_type_string (type
);
399 length
= strlen (name
);
403 /* Inject '*' at beginning of name to force pointer comparison. */
404 char* buf
= (char*) XALLOCAVEC (char, length
+ 2);
406 memcpy (buf
+ 1, name
, length
+ 1);
407 name_string
= build_string (length
+ 2, buf
);
410 name_string
= build_string (length
+ 1, name
);
412 return fix_string_type (name_string
);
415 /* Return a VAR_DECL for the internal ABI defined type_info object for
416 TYPE. You must arrange that the decl is mark_used, if actually use
417 it --- decls in vtables are only used if the vtable is output. */
420 get_tinfo_decl (tree type
)
422 if (variably_modified_type_p (type
, /*fn=*/NULL_TREE
))
424 error ("cannot create type information for type %qT because "
425 "it involves types of variable size",
427 return error_mark_node
;
430 if (TREE_CODE (type
) == METHOD_TYPE
)
431 type
= build_function_type (TREE_TYPE (type
),
432 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
434 return get_tinfo_decl_direct (type
, NULL
, -1);
437 /* Get or create a tinfo VAR_DECL directly from the provided information.
438 The caller must have already checked it is valid to do so. */
441 get_tinfo_decl_direct (tree type
, tree name
, int pseudo_ix
)
443 /* For a class type, the variable is cached in the type node
447 gcc_checking_assert (TREE_CODE (type
) != METHOD_TYPE
);
449 if (CLASS_TYPE_P (type
))
450 d
= CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
));
453 name
= mangle_typeinfo_for_type (type
);
455 if (!CLASS_TYPE_P (type
) || TYPE_TRANSPARENT_AGGR (type
))
456 d
= get_global_binding (name
);
462 pseudo_ix
= get_pseudo_ti_index (type
);
464 const tinfo_s
*ti
= get_tinfo_desc (pseudo_ix
);
466 d
= build_lang_decl (VAR_DECL
, name
, ti
->type
);
467 SET_DECL_ASSEMBLER_NAME (d
, name
);
468 /* Remember the type it is for. */
469 TREE_TYPE (name
) = type
;
470 DECL_TINFO_P (d
) = 1;
471 DECL_ARTIFICIAL (d
) = 1;
472 DECL_IGNORED_P (d
) = 1;
473 TREE_READONLY (d
) = 1;
475 /* Tell equal_address_to that different tinfo decls never
477 if (vec_safe_is_empty (unemitted_tinfo_decls
))
479 = build_tree_list (get_identifier ("non overlapping"),
483 = DECL_ATTRIBUTES ((*unemitted_tinfo_decls
)[0]);
485 /* Mark the variable as undefined -- but remember that we can
486 define it later if we need to do so. */
487 DECL_EXTERNAL (d
) = 1;
488 DECL_NOT_REALLY_EXTERN (d
) = 1;
489 set_linkage_according_to_type (type
, d
);
491 d
= pushdecl_top_level_and_finish (d
, NULL_TREE
);
492 if (CLASS_TYPE_P (type
))
493 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
)) = d
;
495 /* Add decl to the global array of tinfo decls. */
496 vec_safe_push (unemitted_tinfo_decls
, d
);
502 /* Return the type_info object for TYPE. */
505 get_typeid (tree type
, tsubst_flags_t complain
)
507 if (type
== error_mark_node
|| !typeid_ok_p ())
508 return error_mark_node
;
510 if (processing_template_decl
)
511 return build_min (TYPEID_EXPR
, const_type_info_type_node
, type
);
513 /* If the type of the type-id is a reference type, the result of the
514 typeid expression refers to a type_info object representing the
516 type
= non_reference (type
);
518 /* This is not one of the uses of a qualified function type in 8.3.5. */
519 if (TREE_CODE (type
) == FUNCTION_TYPE
520 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
521 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
523 if (complain
& tf_error
)
524 error ("%<typeid%> of qualified function type %qT", type
);
525 return error_mark_node
;
528 /* The top-level cv-qualifiers of the lvalue expression or the type-id
529 that is the operand of typeid are always ignored. */
530 type
= cv_unqualified (type
);
532 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
533 if (CLASS_TYPE_P (type
) || type
== unknown_type_node
534 || type
== init_list_type_node
)
535 type
= complete_type_or_maybe_complain (type
, NULL_TREE
, complain
);
538 return error_mark_node
;
540 return cp_build_fold_indirect_ref (get_tinfo_ptr (type
));
543 /* Check whether TEST is null before returning RESULT. If TEST is used in
544 RESULT, it must have previously had a save_expr applied to it. */
547 build_if_nonnull (tree test
, tree result
, tsubst_flags_t complain
)
549 tree null_ptr
= cp_convert (TREE_TYPE (test
), nullptr_node
, complain
);
550 tree cond
= build2 (NE_EXPR
, boolean_type_node
, test
, null_ptr
);
552 /* This is a compiler generated comparison, don't emit
553 e.g. -Wnonnull-compare warning for it. */
554 suppress_warning (cond
, OPT_Wnonnull
);
556 null_ptr
= cp_convert (TREE_TYPE (result
), nullptr_node
, complain
);
557 cond
= build3 (COND_EXPR
, TREE_TYPE (result
), cond
, result
, null_ptr
);
559 /* Likewise, don't emit -Wnonnull for using the result to call
560 a member function. */
561 suppress_warning (cond
, OPT_Wnonnull
);
565 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
569 build_dynamic_cast_1 (location_t loc
, tree type
, tree expr
,
570 tsubst_flags_t complain
)
572 enum tree_code tc
= TREE_CODE (type
);
575 tree old_expr
= expr
;
576 const char *errstr
= NULL
;
578 /* Save casted types in the function's used types hash table. */
579 used_types_insert (type
);
581 /* T shall be a pointer or reference to a complete class type, or
582 `pointer to cv void''. */
586 if (VOID_TYPE_P (TREE_TYPE (type
)))
590 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type
)))
592 errstr
= _("target is not pointer or reference to class");
595 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
597 errstr
= _("target is not pointer or reference to complete type");
603 errstr
= _("target is not pointer or reference");
607 if (tc
== POINTER_TYPE
)
609 expr
= decay_conversion (expr
, complain
);
610 exprtype
= TREE_TYPE (expr
);
612 /* If T is a pointer type, v shall be an rvalue of a pointer to
613 complete class type, and the result is an rvalue of type T. */
615 expr
= mark_rvalue_use (expr
);
617 if (!TYPE_PTR_P (exprtype
))
619 errstr
= _("source is not a pointer");
622 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype
)))
624 errstr
= _("source is not a pointer to class");
627 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
629 errstr
= _("source is a pointer to incomplete type");
635 expr
= mark_lvalue_use (expr
);
636 exprtype
= TREE_TYPE (expr
);
638 /* T is a reference type, v shall be an lvalue of a complete class
639 type, and the result is an lvalue of the type referred to by T. */
640 if (! MAYBE_CLASS_TYPE_P (exprtype
))
642 errstr
= _("source is not of class type");
645 if (!COMPLETE_TYPE_P (complete_type (exprtype
)))
647 errstr
= _("source is of incomplete class type");
651 exprtype
= cp_build_reference_type (exprtype
, !lvalue_p (expr
));
654 /* The dynamic_cast operator shall not cast away constness. */
655 if (!at_least_as_qualified_p (TREE_TYPE (type
),
656 TREE_TYPE (exprtype
)))
658 errstr
= _("conversion casts away constness");
662 /* If *type is an unambiguous accessible base class of *exprtype,
663 convert statically. */
665 tree binfo
= lookup_base (TREE_TYPE (exprtype
), TREE_TYPE (type
),
666 ba_check
, NULL
, complain
);
668 return build_static_cast (loc
, type
, expr
, complain
);
671 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
672 if (tc
== REFERENCE_TYPE
)
673 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
674 LOOKUP_NORMAL
, NULL_TREE
, complain
);
676 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
677 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
680 /* if TYPE is `void *', return pointer to complete object. */
681 if (tc
== POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (type
)))
683 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
684 if (TREE_CODE (expr
) == ADDR_EXPR
685 && VAR_P (TREE_OPERAND (expr
, 0))
686 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
687 return build1 (NOP_EXPR
, type
, expr
);
689 /* Since expr is used twice below, save it. */
690 expr
= save_expr (expr
);
692 expr1
= build_headof (expr
);
693 if (TREE_TYPE (expr1
) != type
)
694 expr1
= build1 (NOP_EXPR
, type
, expr1
);
695 return build_if_nonnull (expr
, expr1
, complain
);
700 tree result
, td2
, td3
;
702 tree static_type
, target_type
, boff
;
704 /* If we got here, we can't convert statically. Therefore,
705 dynamic_cast<D&>(b) (b an object) cannot succeed. */
706 if (tc
== REFERENCE_TYPE
)
709 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
711 tree expr
= throw_bad_cast ();
712 if (complain
& tf_warning
)
714 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
716 /* Bash it to the expected type. */
717 TREE_TYPE (expr
) = type
;
721 /* Ditto for dynamic_cast<D*>(&b). */
722 else if (TREE_CODE (expr
) == ADDR_EXPR
)
724 tree op
= TREE_OPERAND (expr
, 0);
726 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
728 if (complain
& tf_warning
)
730 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
732 retval
= build_int_cst (type
, 0);
737 /* Use of dynamic_cast when -fno-rtti is prohibited. */
740 if (complain
& tf_error
)
742 "%<dynamic_cast%> not permitted with %<-fno-rtti%>");
743 return error_mark_node
;
746 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
747 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
748 td2
= get_tinfo_decl (target_type
);
749 if (!mark_used (td2
, complain
) && !(complain
& tf_error
))
750 return error_mark_node
;
751 td2
= cp_build_addr_expr (td2
, complain
);
752 td3
= get_tinfo_decl (static_type
);
753 if (!mark_used (td3
, complain
) && !(complain
& tf_error
))
754 return error_mark_node
;
755 td3
= cp_build_addr_expr (td3
, complain
);
757 /* Determine how T and V are related. */
758 boff
= dcast_base_hint (static_type
, target_type
);
760 /* Since expr is used twice below, save it. */
761 expr
= save_expr (expr
);
764 if (tc
== REFERENCE_TYPE
)
765 expr1
= cp_build_addr_expr (expr1
, complain
);
772 dcast_fn
= dynamic_cast_node
;
775 unsigned flags
= push_abi_namespace ();
776 tree tinfo_ptr
= xref_tag (class_type
,
777 get_identifier ("__class_type_info"));
778 tinfo_ptr
= cp_build_qualified_type (tinfo_ptr
, TYPE_QUAL_CONST
);
779 tinfo_ptr
= build_pointer_type (tinfo_ptr
);
781 const char *fn_name
= "__dynamic_cast";
782 /* void *() (void const *, __class_type_info const *,
783 __class_type_info const *, ptrdiff_t) */
784 tree fn_type
= (build_function_type_list
785 (ptr_type_node
, const_ptr_type_node
,
786 tinfo_ptr
, tinfo_ptr
, ptrdiff_type_node
,
788 dcast_fn
= (build_library_fn_ptr
789 (fn_name
, fn_type
, ECF_LEAF
| ECF_PURE
| ECF_NOTHROW
));
790 /* As with __cxa_atexit in get_atexit_node. */
791 DECL_CONTEXT (dcast_fn
) = FROB_CONTEXT (current_namespace
);
792 DECL_SOURCE_LOCATION (dcast_fn
) = BUILTINS_LOCATION
;
793 dcast_fn
= pushdecl (dcast_fn
, /*hiding=*/true);
794 pop_abi_namespace (flags
);
795 dynamic_cast_node
= dcast_fn
;
797 if (dcast_fn
== error_mark_node
)
798 return error_mark_node
;
799 result
= build_cxx_call (dcast_fn
, 4, elems
, complain
);
800 SET_EXPR_LOCATION (result
, loc
);
802 if (tc
== REFERENCE_TYPE
)
804 tree bad
= throw_bad_cast ();
807 result
= save_expr (result
);
808 neq
= cp_truthvalue_conversion (result
, complain
);
809 return cp_convert (type
,
810 build3 (COND_EXPR
, TREE_TYPE (result
),
811 neq
, result
, bad
), complain
);
814 /* Now back to the type we want from a void*. */
815 result
= cp_convert (type
, result
, complain
);
816 return build_if_nonnull (expr
, result
, complain
);
820 errstr
= _("source type is not polymorphic");
823 if (complain
& tf_error
)
824 error_at (loc
, "cannot %<dynamic_cast%> %qE (of type %q#T) "
826 old_expr
, TREE_TYPE (old_expr
), type
, errstr
);
827 return error_mark_node
;
831 build_dynamic_cast (location_t loc
, tree type
, tree expr
,
832 tsubst_flags_t complain
)
836 if (type
== error_mark_node
|| expr
== error_mark_node
)
837 return error_mark_node
;
839 if (processing_template_decl
)
841 expr
= build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
842 TREE_SIDE_EFFECTS (expr
) = 1;
843 r
= convert_from_reference (expr
);
844 protected_set_expr_location (r
, loc
);
848 r
= convert_from_reference (build_dynamic_cast_1 (loc
, type
, expr
,
850 if (r
!= error_mark_node
)
851 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
852 protected_set_expr_location (r
, loc
);
856 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
859 qualifier_flags (tree type
)
862 int quals
= cp_type_quals (type
);
864 if (quals
& TYPE_QUAL_CONST
)
866 if (quals
& TYPE_QUAL_VOLATILE
)
868 if (quals
& TYPE_QUAL_RESTRICT
)
873 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
874 contains a pointer to member of an incomplete class. */
877 target_incomplete_p (tree type
)
880 if (TYPE_PTRDATAMEM_P (type
))
882 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)))
884 type
= TYPE_PTRMEM_POINTED_TO_TYPE (type
);
886 else if (TYPE_PTR_P (type
))
887 type
= TREE_TYPE (type
);
889 return !COMPLETE_OR_VOID_TYPE_P (type
);
892 /* Returns true if TYPE involves an incomplete class type; in that
893 case, typeinfo variables for TYPE should be emitted with internal
897 involves_incomplete_p (tree type
)
899 switch (TREE_CODE (type
))
902 return target_incomplete_p (TREE_TYPE (type
));
907 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type
))
908 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)));
911 if (TYPE_PTRMEMFUNC_P (type
))
915 if (!COMPLETE_TYPE_P (type
))
919 /* All other types do not involve incomplete class types. */
924 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
925 is the vtable pointer and NTBS name. The NTBS name is emitted as a
926 comdat const char array, so it becomes a unique key for the type. Generate
927 and emit that VAR_DECL here. (We can't always emit the type_info itself
928 as comdat, because of pointers to incomplete.) */
931 tinfo_base_init (tinfo_s
*ti
, tree target
)
936 vec
<constructor_elt
, va_gc
> *v
;
939 tree name_name
, name_string
;
941 /* Generate the NTBS array variable. */
942 tree name_type
= build_cplus_array_type
943 (cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
946 /* Determine the name of the variable -- and remember with which
947 type it is associated. */
948 name_name
= mangle_typeinfo_string_for_type (target
);
949 TREE_TYPE (name_name
) = target
;
951 name_decl
= build_lang_decl (VAR_DECL
, name_name
, name_type
);
952 SET_DECL_ASSEMBLER_NAME (name_decl
, name_name
);
953 DECL_ARTIFICIAL (name_decl
) = 1;
954 DECL_IGNORED_P (name_decl
) = 1;
955 TREE_READONLY (name_decl
) = 1;
956 TREE_STATIC (name_decl
) = 1;
957 DECL_EXTERNAL (name_decl
) = 0;
958 DECL_TINFO_P (name_decl
) = 1;
959 set_linkage_according_to_type (target
, name_decl
);
960 import_export_decl (name_decl
);
961 name_string
= tinfo_name (target
, !TREE_PUBLIC (name_decl
));
962 DECL_INITIAL (name_decl
) = name_string
;
963 mark_used (name_decl
);
964 pushdecl_top_level_and_finish (name_decl
, name_string
);
967 vtable_ptr
= ti
->vtable
;
970 int flags
= push_abi_namespace ();
971 tree real_type
= xref_tag (class_type
, ti
->name
);
972 tree real_decl
= TYPE_NAME (real_type
);
973 DECL_SOURCE_LOCATION (real_decl
) = BUILTINS_LOCATION
;
974 pop_abi_namespace (flags
);
976 if (!COMPLETE_TYPE_P (real_type
))
978 /* We never saw a definition of this type, so we need to
979 tell the compiler that this is an exported class, as
980 indeed all of the __*_type_info classes are. */
981 SET_CLASSTYPE_INTERFACE_KNOWN (real_type
);
982 CLASSTYPE_INTERFACE_ONLY (real_type
) = 1;
985 vtable_ptr
= get_vtable_decl (real_type
, /*complete=*/1);
986 vtable_ptr
= cp_build_addr_expr (vtable_ptr
, tf_warning_or_error
);
988 /* We need to point into the middle of the vtable. */
989 vtable_ptr
= fold_build_pointer_plus
991 size_binop (MULT_EXPR
,
992 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
),
993 TYPE_SIZE_UNIT (vtable_entry_type
)));
995 ti
->vtable
= vtable_ptr
;
999 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, vtable_ptr
);
1000 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1001 decay_conversion (name_decl
, tf_warning_or_error
));
1003 init
= build_constructor (init_list_type_node
, v
);
1004 TREE_CONSTANT (init
) = 1;
1005 TREE_STATIC (init
) = 1;
1010 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
1011 information about the particular type_info derivation, which adds no
1012 additional fields to the type_info base. */
1015 generic_initializer (tinfo_s
*ti
, tree target
)
1017 tree init
= tinfo_base_init (ti
, target
);
1019 init
= build_constructor_single (init_list_type_node
, NULL_TREE
, init
);
1020 TREE_CONSTANT (init
) = 1;
1021 TREE_STATIC (init
) = 1;
1025 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1026 TI provides information about the particular type_info derivation,
1027 which adds target type and qualifier flags members to the type_info base. */
1030 ptr_initializer (tinfo_s
*ti
, tree target
)
1032 tree init
= tinfo_base_init (ti
, target
);
1033 tree to
= TREE_TYPE (target
);
1034 int flags
= qualifier_flags (to
);
1035 bool incomplete
= target_incomplete_p (to
);
1036 vec
<constructor_elt
, va_gc
> *v
;
1041 if (tx_safe_fn_type_p (to
))
1044 to
= tx_unsafe_fn_variant (to
);
1046 if (flag_noexcept_type
1047 && FUNC_OR_METHOD_TYPE_P (to
)
1048 && TYPE_NOTHROW_P (to
))
1051 to
= build_exception_variant (to
, NULL_TREE
);
1053 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1054 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, flags
));
1055 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1056 get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to
)));
1058 init
= build_constructor (init_list_type_node
, v
);
1059 TREE_CONSTANT (init
) = 1;
1060 TREE_STATIC (init
) = 1;
1064 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1065 TI provides information about the particular type_info derivation,
1066 which adds class, target type and qualifier flags members to the type_info
1070 ptm_initializer (tinfo_s
*ti
, tree target
)
1072 tree init
= tinfo_base_init (ti
, target
);
1073 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
1074 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
1075 int flags
= qualifier_flags (to
);
1076 bool incomplete
= target_incomplete_p (to
);
1077 vec
<constructor_elt
, va_gc
> *v
;
1082 if (!COMPLETE_TYPE_P (klass
))
1084 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1085 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, flags
));
1086 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1087 get_void_tinfo_ptr (TYPE_MAIN_VARIANT (to
)));
1088 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, get_void_tinfo_ptr (klass
));
1090 init
= build_constructor (init_list_type_node
, v
);
1091 TREE_CONSTANT (init
) = 1;
1092 TREE_STATIC (init
) = 1;
1096 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1097 TI provides information about the particular __class_type_info derivation,
1098 which adds hint flags and N extra initializers to the type_info base. */
1101 class_initializer (tinfo_s
*ti
, tree target
, unsigned n
, ...)
1103 tree init
= tinfo_base_init (ti
, target
);
1104 va_list extra_inits
;
1106 vec
<constructor_elt
, va_gc
> *v
;
1109 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1110 va_start (extra_inits
, n
);
1111 for (i
= 0; i
< n
; i
++)
1112 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, va_arg (extra_inits
, tree
));
1113 va_end (extra_inits
);
1115 init
= build_constructor (init_list_type_node
, v
);
1116 TREE_CONSTANT (init
) = 1;
1117 TREE_STATIC (init
) = 1;
1121 /* Returns true if the typeinfo for type should be placed in
1122 the runtime library. */
1125 typeinfo_in_lib_p (tree type
)
1127 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1128 library for simple types T. */
1129 if (TYPE_PTR_P (type
)
1130 && (cp_type_quals (TREE_TYPE (type
)) == TYPE_QUAL_CONST
1131 || cp_type_quals (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
))
1132 type
= TREE_TYPE (type
);
1134 switch (TREE_CODE (type
))
1151 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1152 the index of the descriptor in the tinfo_desc vector. */
1155 get_pseudo_ti_init (tree type
, unsigned tk_index
)
1157 tinfo_s
*ti
= get_tinfo_desc (tk_index
);
1159 gcc_assert (at_eof
);
1162 case TK_POINTER_MEMBER_TYPE
:
1163 return ptm_initializer (ti
, type
);
1165 case TK_POINTER_TYPE
:
1166 return ptr_initializer (ti
, type
);
1168 case TK_BUILTIN_TYPE
:
1169 case TK_ENUMERAL_TYPE
:
1170 case TK_FUNCTION_TYPE
:
1172 return generic_initializer (ti
, type
);
1175 return class_initializer (ti
, type
, 0);
1177 case TK_SI_CLASS_TYPE
:
1179 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), 0);
1180 tree tinfo
= get_void_tinfo_ptr (BINFO_TYPE (base_binfo
));
1182 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1183 ti
= &(*tinfo_descs
)[tk_index
];
1184 return class_initializer (ti
, type
, 1, tinfo
);
1189 int hint
= ((CLASSTYPE_REPEATED_BASE_P (type
) << 0)
1190 | (CLASSTYPE_DIAMOND_SHAPED_P (type
) << 1));
1191 tree binfo
= TYPE_BINFO (type
);
1192 unsigned nbases
= BINFO_N_BASE_BINFOS (binfo
);
1193 vec
<tree
, va_gc
> *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1194 tree offset_type
= LONGPTR_T
;
1195 vec
<constructor_elt
, va_gc
> *init_vec
= NULL
;
1197 gcc_assert (tk_index
- TK_VMI_CLASS_TYPES
+ 1 == nbases
);
1199 vec_safe_grow (init_vec
, nbases
, true);
1200 /* Generate the base information initializer. */
1201 for (unsigned ix
= nbases
; ix
--;)
1203 tree base_binfo
= BINFO_BASE_BINFO (binfo
, ix
);
1207 vec
<constructor_elt
, va_gc
> *v
;
1209 if ((*base_accesses
)[ix
] == access_public_node
)
1211 tinfo
= get_void_tinfo_ptr (BINFO_TYPE (base_binfo
));
1212 if (BINFO_VIRTUAL_P (base_binfo
))
1214 /* We store the vtable offset at which the virtual
1215 base offset can be found. */
1216 offset
= BINFO_VPTR_FIELD (base_binfo
);
1220 offset
= BINFO_OFFSET (base_binfo
);
1222 /* Combine offset and flags into one field. */
1223 offset
= fold_convert (offset_type
, offset
);
1224 offset
= fold_build2_loc (input_location
,
1225 LSHIFT_EXPR
, offset_type
, offset
,
1226 build_int_cst (offset_type
, 8));
1227 offset
= fold_build2_loc (input_location
,
1228 BIT_IOR_EXPR
, offset_type
, offset
,
1229 build_int_cst (offset_type
, flags
));
1231 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, tinfo
);
1232 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, offset
);
1233 tree base_init
= build_constructor (init_list_type_node
, v
);
1234 constructor_elt
*e
= &(*init_vec
)[ix
];
1235 e
->index
= NULL_TREE
;
1236 e
->value
= base_init
;
1238 tree base_inits
= build_constructor (init_list_type_node
, init_vec
);
1240 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1241 ti
= &(*tinfo_descs
)[tk_index
];
1242 return class_initializer (ti
, type
, 3,
1243 build_int_cst (NULL_TREE
, hint
),
1244 build_int_cst (NULL_TREE
, nbases
),
1250 /* Return the index of a pseudo type info type node used to describe
1251 TYPE. TYPE must be a complete type (or cv void), except at the end
1252 of the translation unit. */
1255 get_pseudo_ti_index (tree type
)
1259 switch (TREE_CODE (type
))
1262 ix
= TK_POINTER_MEMBER_TYPE
;
1266 ix
= TK_POINTER_TYPE
;
1270 ix
= TK_ENUMERAL_TYPE
;
1274 ix
= TK_FUNCTION_TYPE
;
1283 if (TYPE_PTRMEMFUNC_P (type
))
1284 ix
= TK_POINTER_MEMBER_TYPE
;
1285 else if (!COMPLETE_TYPE_P (type
))
1288 cxx_incomplete_type_error (NULL_TREE
, type
);
1291 else if (!TYPE_BINFO (type
)
1292 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)))
1296 tree binfo
= TYPE_BINFO (type
);
1297 vec
<tree
, va_gc
> *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1298 tree base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
1299 int num_bases
= BINFO_N_BASE_BINFOS (binfo
);
1302 && (*base_accesses
)[0] == access_public_node
1303 && !BINFO_VIRTUAL_P (base_binfo
)
1304 && integer_zerop (BINFO_OFFSET (base_binfo
)))
1305 /* single non-virtual public. */
1306 ix
= TK_SI_CLASS_TYPE
;
1308 ix
= TK_VMI_CLASS_TYPES
+ num_bases
- 1;
1313 ix
= TK_BUILTIN_TYPE
;
1319 /* Return pointer to tinfo descriptor. Possibly creating the tinfo
1320 descriptor in the first place. */
1323 get_tinfo_desc (unsigned ix
)
1325 unsigned len
= tinfo_descs
->length ();
1329 /* too short, extend. */
1331 vec_safe_reserve (tinfo_descs
, len
);
1333 elt
.type
= elt
.vtable
= elt
.name
= NULL_TREE
;
1335 tinfo_descs
->quick_push (elt
);
1338 tinfo_s
*res
= &(*tinfo_descs
)[ix
];
1343 /* Ok, we have to create it. This layout must be consistent with
1344 that defined in the runtime support. We explicitly manage the
1345 vtable member, and name it for real type as used in the runtime.
1346 The RECORD type has a different name, to avoid collisions. We
1347 have to delay generating the VAR_DECL of the vtable until the end
1348 of the translation, when we'll have seen the library definition,
1349 if there was one. */
1351 /* Fields to add, chained in reverse order. */
1352 tree fields
= NULL_TREE
;
1354 if (ix
>= TK_DERIVED_TYPES
)
1356 /* First field is the pseudo type_info base class. */
1357 tree fld_base
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1358 get_tinfo_desc (TK_TYPE_INFO_TYPE
)->type
);
1360 DECL_CHAIN (fld_base
) = fields
;
1366 case TK_TYPE_INFO_TYPE
:
1368 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1369 NULL_TREE
, const_ptr_type_node
);
1372 tree fld_str
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1373 NULL_TREE
, const_string_type_node
);
1374 DECL_CHAIN (fld_str
) = fields
;
1381 /* Base class internal helper. Pointer to base type, offset to
1383 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1384 NULL_TREE
, const_ptr_type_node
);
1385 DECL_CHAIN (fld_ptr
) = fields
;
1388 tree fld_flag
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1389 NULL_TREE
, LONGPTR_T
);
1390 DECL_CHAIN (fld_flag
) = fields
;
1395 case TK_BUILTIN_TYPE
:
1396 /* Fundamental type_info */
1402 case TK_FUNCTION_TYPE
:
1405 case TK_ENUMERAL_TYPE
:
1408 case TK_POINTER_TYPE
:
1409 case TK_POINTER_MEMBER_TYPE
:
1411 /* Pointer type_info. Adds two fields, qualification mask and
1412 pointer to the pointed to type. This is really a
1413 descendant of __pbase_type_info. */
1414 tree fld_mask
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1415 NULL_TREE
, integer_type_node
);
1416 DECL_CHAIN (fld_mask
) = fields
;
1419 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1420 NULL_TREE
, const_ptr_type_node
);
1421 DECL_CHAIN (fld_ptr
) = fields
;
1424 if (ix
== TK_POINTER_MEMBER_TYPE
)
1426 /* Add a pointer to the class too. */
1427 tree fld_cls
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1428 NULL_TREE
, const_ptr_type_node
);
1429 DECL_CHAIN (fld_cls
) = fields
;
1436 /* Class type_info. No additional fields. */
1439 case TK_SI_CLASS_TYPE
:
1441 /* Single public non-virtual base class. Add pointer to base
1442 class. This is really a descendant of
1443 __class_type_info. */
1444 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1445 NULL_TREE
, const_ptr_type_node
);
1446 DECL_CHAIN (fld_ptr
) = fields
;
1451 default: /* Multiple inheritance. */
1453 unsigned num_bases
= ix
- TK_VMI_CLASS_TYPES
+ 1;
1455 tree fld_flg
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1456 NULL_TREE
, integer_type_node
);
1457 DECL_CHAIN (fld_flg
) = fields
;
1460 tree fld_cnt
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1461 NULL_TREE
, integer_type_node
);
1462 DECL_CHAIN (fld_cnt
) = fields
;
1465 /* Create the array of __base_class_type_info entries. */
1466 tree domain
= build_index_type (size_int (num_bases
- 1));
1467 tree array
= build_array_type (get_tinfo_desc (TK_BASE_TYPE
)->type
,
1469 tree fld_ary
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1471 DECL_CHAIN (fld_ary
) = fields
;
1477 /* Generate the pseudo type name. */
1478 const char *real_name
= tinfo_names
[ix
< TK_VMI_CLASS_TYPES
1479 ? ix
: unsigned (TK_VMI_CLASS_TYPES
)];
1480 size_t name_len
= strlen (real_name
);
1481 char *pseudo_name
= (char *) alloca (name_len
+ 30);
1482 memcpy (pseudo_name
, real_name
, name_len
);
1483 /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
1484 apply it to all. See get_peudo_tinfo_index where we make use of
1486 sprintf (pseudo_name
+ name_len
, "_pseudo_%d", ix
);
1488 /* Create the pseudo type. */
1489 tree pseudo_type
= make_class_type (RECORD_TYPE
);
1490 /* Pass the fields chained in reverse. */
1491 finish_builtin_struct (pseudo_type
, pseudo_name
, fields
, NULL_TREE
);
1492 CLASSTYPE_AS_BASE (pseudo_type
) = pseudo_type
;
1493 DECL_CONTEXT (TYPE_NAME (pseudo_type
)) = FROB_CONTEXT (global_namespace
);
1494 DECL_TINFO_P (TYPE_NAME (pseudo_type
)) = true;
1495 xref_basetypes (pseudo_type
, /*bases=*/NULL_TREE
);
1497 res
->type
= cp_build_qualified_type (pseudo_type
, TYPE_QUAL_CONST
);
1498 res
->name
= get_identifier (real_name
);
1500 /* Pretend this is public so determine_visibility doesn't give vtables
1501 internal linkage. */
1502 TREE_PUBLIC (TYPE_MAIN_DECL (res
->type
)) = 1;
1507 /* Return an identifying index for the pseudo type_info TYPE.
1508 We wrote the index at the end of the name, so just scan it from
1509 there. This isn't critical, as it's only on the first use of this
1510 type during module stream out. */
1513 get_pseudo_tinfo_index (tree type
)
1515 tree name
= DECL_NAME (TYPE_NAME (type
));
1516 unsigned ix
= 0, scale
= 1;
1517 size_t len
= IDENTIFIER_LENGTH (name
);
1518 const char *ptr
= IDENTIFIER_POINTER (name
) + len
;
1520 for (; *--ptr
!= '_'; scale
*= 10)
1523 gcc_checking_assert (len
&& ISDIGIT (*ptr
));
1524 ix
+= (*ptr
- '0') * scale
;
1527 gcc_assert (len
!= IDENTIFIER_LENGTH (name
));
1532 get_pseudo_tinfo_type (unsigned ix
)
1534 return get_tinfo_desc (ix
)->type
;
1537 /* We lazily create the type info types. */
1540 create_tinfo_types (void)
1542 gcc_assert (!tinfo_descs
);
1544 vec_alloc (tinfo_descs
, TK_MAX
+ 20);
1547 /* Helper for emit_support_tinfos. Emits the type_info descriptor of
1551 emit_support_tinfo_1 (tree bltn
)
1555 if (bltn
== NULL_TREE
)
1558 types
[1] = build_pointer_type (bltn
);
1559 types
[2] = build_pointer_type (cp_build_qualified_type (bltn
,
1562 for (int i
= 0; i
< 3; ++i
)
1564 tree tinfo
= get_tinfo_decl (types
[i
]);
1565 TREE_USED (tinfo
) = 1;
1566 mark_needed (tinfo
);
1567 /* The C++ ABI requires that these objects be COMDAT. But,
1568 On systems without weak symbols, initialized COMDAT
1569 objects are emitted with internal linkage. (See
1570 comdat_linkage for details.) Since we want these objects
1571 to have external linkage so that copies do not have to be
1572 emitted in code outside the runtime library, we make them
1575 It might also not be necessary to follow this detail of the
1577 if (!flag_weak
|| ! targetm
.cxx
.library_rtti_comdat ())
1579 gcc_assert (TREE_PUBLIC (tinfo
) && !DECL_COMDAT (tinfo
));
1580 DECL_INTERFACE_KNOWN (tinfo
) = 1;
1583 /* Emit it right away if not emitted already. */
1584 if (DECL_INITIAL (tinfo
) == NULL_TREE
)
1586 bool ok
= emit_tinfo_decl (tinfo
);
1588 /* When compiling libsupc++.a (fundamental_type_info.o),
1589 unemitted_tinfo_decls->last () will be tinfo, so pop it
1590 from the vector as it is emitted now. If one uses typeid
1591 etc. in the same TU as the definition of
1592 ~fundamental_type_info (), the tinfo might be emitted
1593 already earlier, in such case keep it in the vector
1594 (as otherwise we'd need to walk the whole vector) and
1595 let c_parse_final_cleanups ignore it when it will have
1596 non-NULL DECL_INITIAL. */
1597 if (unemitted_tinfo_decls
->last () == tinfo
)
1598 unemitted_tinfo_decls
->pop ();
1603 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1604 support. Generating them here guarantees consistency with the other
1605 structures. We use the following heuristic to determine when the runtime
1606 is being generated. If std::__fundamental_type_info is defined, and its
1607 destructor is defined, then the runtime is being built. */
1610 emit_support_tinfos (void)
1612 /* Dummy static variable so we can put nullptr in the array; it will be
1613 set before we actually start to walk the array. */
1614 static tree
*const fundamentals
[] =
1618 &wchar_type_node
, &char8_type_node
, &char16_type_node
, &char32_type_node
,
1619 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1620 &short_integer_type_node
, &short_unsigned_type_node
,
1621 &integer_type_node
, &unsigned_type_node
,
1622 &long_integer_type_node
, &long_unsigned_type_node
,
1623 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1624 &float_type_node
, &double_type_node
, &long_double_type_node
,
1625 &bfloat16_type_node
, &float16_type_node
, &float32_type_node
,
1626 &float64_type_node
, &float128_type_node
, &float32x_type_node
,
1627 &float64x_type_node
, &float128x_type_node
, &nullptr_type_node
,
1630 /* Similar, but for floating point types only which should get type info
1631 regardless whether they are non-NULL or NULL. */
1632 static tree
*const fundamentals_with_fallback
[] =
1634 &dfloat32_type_node
, &dfloat64_type_node
, &dfloat128_type_node
,
1639 /* Look for a defined class. */
1640 tree bltn_type
= lookup_qualified_name
1641 (abi_node
, "__fundamental_type_info", LOOK_want::TYPE
, false);
1642 if (TREE_CODE (bltn_type
) != TYPE_DECL
)
1645 bltn_type
= TREE_TYPE (bltn_type
);
1646 if (!COMPLETE_TYPE_P (bltn_type
))
1648 tree dtor
= CLASSTYPE_DESTRUCTOR (bltn_type
);
1649 if (!dtor
|| DECL_EXTERNAL (dtor
))
1652 /* All these are really builtins. So set the location. */
1653 location_t saved_loc
= input_location
;
1654 input_location
= BUILTINS_LOCATION
;
1656 tree fallback
= NULL_TREE
;
1657 for (ix
= 0; fundamentals
[ix
]; ix
++)
1658 emit_support_tinfo_1 (*fundamentals
[ix
]);
1659 for (ix
= 0; fundamentals_with_fallback
[ix
]; ix
++)
1660 if (*fundamentals_with_fallback
[ix
])
1661 emit_support_tinfo_1 (*fundamentals_with_fallback
[ix
]);
1664 if (fallback
== NULL_TREE
)
1665 fallback
= make_node (REAL_TYPE
);
1666 *fundamentals_with_fallback
[ix
] = fallback
;
1667 emit_support_tinfo_1 (fallback
);
1668 *fundamentals_with_fallback
[ix
] = NULL_TREE
;
1670 for (ix
= 0; ix
< NUM_INT_N_ENTS
; ix
++)
1671 if (int_n_enabled_p
[ix
])
1673 emit_support_tinfo_1 (int_n_trees
[ix
].signed_type
);
1674 emit_support_tinfo_1 (int_n_trees
[ix
].unsigned_type
);
1676 for (tree t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
1677 emit_support_tinfo_1 (TREE_VALUE (t
));
1679 /* Emit additional typeinfos as requested by target. */
1680 targetm
.emit_support_tinfos (emit_support_tinfo_1
);
1682 input_location
= saved_loc
;
1685 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1686 tinfo decl. Determine whether it needs emitting, and if so
1687 generate the initializer. */
1690 emit_tinfo_decl (tree decl
)
1692 gcc_assert (DECL_TINFO_P (decl
));
1694 tree type
= TREE_TYPE (DECL_NAME (decl
));
1695 if (typeinfo_in_lib_p (type
))
1698 DECL_EXTERNAL (decl
) = 0;
1701 /* If we're not in the runtime, then DECL (which is already
1702 DECL_EXTERNAL) will not be defined here. */
1703 DECL_INTERFACE_KNOWN (decl
) = 1;
1707 else if (involves_incomplete_p (type
))
1709 if (!decl_needed_p (decl
))
1711 /* If TYPE involves an incomplete class type, then the typeinfo
1712 object will be emitted with internal linkage. There is no
1713 way to know whether or not types are incomplete until the end
1714 of the compilation, so this determination must be deferred
1715 until this point. */
1716 TREE_PUBLIC (decl
) = 0;
1717 DECL_EXTERNAL (decl
) = 0;
1718 DECL_INTERFACE_KNOWN (decl
) = 1;
1721 import_export_decl (decl
);
1722 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
1726 DECL_EXTERNAL (decl
) = 0;
1727 int pseudo_ix
= get_pseudo_ti_index (type
);
1728 const tinfo_s
*ti
= get_tinfo_desc (pseudo_ix
);
1729 if (TREE_TYPE (decl
) != ti
->type
)
1731 /* If the class became complete since we first called get_tinfo_decl,
1732 its type_info descriptor may have switched from __class_type_info
1733 to e.g. __si_class_type_info. */
1734 TREE_TYPE (decl
) = ti
->type
;
1735 relayout_decl (decl
);
1737 init
= get_pseudo_ti_init (type
, pseudo_ix
);
1738 DECL_INITIAL (decl
) = init
;
1740 cp_finish_decl (decl
, init
, false, NULL_TREE
, 0);
1741 /* Avoid targets optionally bumping up the alignment to improve
1742 vector instruction accesses, tinfo are never accessed this way. */
1743 #ifdef DATA_ABI_ALIGNMENT
1744 SET_DECL_ALIGN (decl
, DATA_ABI_ALIGNMENT (decl
, TYPE_ALIGN (TREE_TYPE (decl
))));
1745 DECL_USER_ALIGN (decl
) = true;
1753 #include "gt-cp-rtti.h"