1 /* RunTime Type Identification
2 Copyright (C) 1995-2020 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
ifnonnull (tree
, tree
, tsubst_flags_t
);
125 static tree
tinfo_name (tree
, bool);
126 static tree
build_dynamic_cast_1 (location_t
, tree
, tree
, tsubst_flags_t
);
127 static tree
throw_bad_cast (void);
128 static tree
throw_bad_typeid (void);
129 static tree
get_tinfo_ptr (tree
);
130 static bool typeid_ok_p (void);
131 static int qualifier_flags (tree
);
132 static bool target_incomplete_p (tree
);
133 static tree
tinfo_base_init (tinfo_s
*, tree
);
134 static tree
generic_initializer (tinfo_s
*, tree
);
135 static tree
ptr_initializer (tinfo_s
*, tree
);
136 static tree
ptm_initializer (tinfo_s
*, tree
);
137 static tree
class_initializer (tinfo_s
*, tree
, unsigned, ...);
138 static tree
get_pseudo_ti_init (tree
, unsigned);
139 static unsigned get_pseudo_ti_index (tree
);
140 static tinfo_s
*get_tinfo_desc (unsigned);
141 static void create_tinfo_types (void);
142 static bool typeinfo_in_lib_p (tree
);
144 static int doing_runtime
= 0;
147 push_abi_namespace (void)
149 push_nested_namespace (abi_node
);
150 push_visibility ("default", 2);
154 pop_abi_namespace (void)
157 pop_nested_namespace (abi_node
);
160 /* Declare language defined type_info type and a pointer to const
161 type_info. This is incomplete here, and will be completed when
162 the user #includes <typeinfo>. There are language defined
163 restrictions on what can be done until that is included. Create
164 the internal versions of the ABI types. */
167 init_rtti_processing (void)
171 push_nested_namespace (std_node
);
172 type_info_type
= xref_tag (class_type
, get_identifier ("type_info"),
173 /*tag_scope=*/ts_current
, false);
174 pop_nested_namespace (std_node
);
175 const_type_info_type_node
176 = cp_build_qualified_type (type_info_type
, TYPE_QUAL_CONST
);
177 type_info_ptr_type
= build_pointer_type (const_type_info_type_node
);
179 vec_alloc (unemitted_tinfo_decls
, 124);
181 create_tinfo_types ();
184 /* Given the expression EXP of type `class *', return the head of the
185 object pointed to by EXP with type cv void*, if the class has any
186 virtual functions (TYPE_POLYMORPHIC_P), else just return the
190 build_headof (tree exp
)
192 tree type
= TREE_TYPE (exp
);
196 gcc_assert (TYPE_PTR_P (type
));
197 type
= TREE_TYPE (type
);
199 if (!TYPE_POLYMORPHIC_P (type
))
202 /* We use this a couple of times below, protect it. */
203 exp
= save_expr (exp
);
205 /* The offset-to-top field is at index -2 from the vptr. */
206 index
= build_int_cst (NULL_TREE
,
207 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
209 offset
= build_vtbl_ref (cp_build_fold_indirect_ref (exp
),
212 cp_build_qualified_type (ptr_type_node
,
213 cp_type_quals (TREE_TYPE (exp
)));
214 return fold_build_pointer_plus (exp
, offset
);
217 /* Get a bad_cast node for the program to throw...
219 See libstdc++/exception.cc for __throw_bad_cast */
222 throw_bad_cast (void)
227 tree name
= get_identifier ("__cxa_bad_cast");
228 fn
= get_global_binding (name
);
230 fn
= push_throw_library_fn
231 (name
, build_function_type_list (ptr_type_node
, NULL_TREE
));
234 return build_cxx_call (fn
, 0, NULL
, tf_warning_or_error
);
237 /* Return an expression for "__cxa_bad_typeid()". The expression
238 returned is an lvalue of type "const std::type_info". */
241 throw_bad_typeid (void)
246 tree name
= get_identifier ("__cxa_bad_typeid");
247 fn
= get_global_binding (name
);
250 tree t
= build_reference_type (const_type_info_type_node
);
251 t
= build_function_type_list (t
, NULL_TREE
);
252 fn
= push_throw_library_fn (name
, t
);
256 return build_cxx_call (fn
, 0, NULL
, tf_warning_or_error
);
259 /* Return an lvalue expression whose type is "const std::type_info"
260 and whose value indicates the type of the expression EXP. If EXP
261 is a reference to a polymorphic class, return the dynamic type;
262 otherwise return the static type of the expression. */
265 get_tinfo_decl_dynamic (tree exp
, tsubst_flags_t complain
)
270 if (error_operand_p (exp
))
271 return error_mark_node
;
273 exp
= resolve_nondeduced_context (exp
, complain
);
275 /* Peel back references, so they match. */
276 type
= non_reference (unlowered_expr_type (exp
));
278 /* Peel off cv qualifiers. */
279 type
= cv_unqualified (type
);
281 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
282 if (CLASS_TYPE_P (type
) || type
== unknown_type_node
283 || type
== init_list_type_node
)
284 type
= complete_type_or_maybe_complain (type
, exp
, complain
);
287 return error_mark_node
;
289 /* If exp is a reference to polymorphic type, get the real type_info. */
290 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
292 /* build reference to type_info from vtable. */
295 /* The RTTI information is at index -1. */
296 index
= build_int_cst (NULL_TREE
,
297 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
);
298 t
= build_vtbl_ref (exp
, index
);
299 t
= convert (type_info_ptr_type
, t
);
302 /* Otherwise return the type_info for the static type of the expr. */
303 t
= get_tinfo_ptr (type
);
305 return cp_build_fold_indirect_ref (t
);
313 error ("cannot use %<typeid%> with %<-fno-rtti%>");
317 if (!COMPLETE_TYPE_P (const_type_info_type_node
))
319 gcc_rich_location
richloc (input_location
);
320 maybe_add_include_fixit (&richloc
, "<typeinfo>", false);
322 "must %<#include <typeinfo>%> before using"
328 tree pseudo
= TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE
)->type
);
329 tree real
= TYPE_MAIN_VARIANT (const_type_info_type_node
);
331 /* Make sure abi::__type_info_pseudo has the same alias set
332 as std::type_info. */
333 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo
))
334 TYPE_ALIAS_SET (pseudo
) = get_alias_set (real
);
336 gcc_assert (TYPE_ALIAS_SET (pseudo
) == get_alias_set (real
));
341 /* Return an expression for "typeid(EXP)". The expression returned is
342 an lvalue of type "const std::type_info". */
345 build_typeid (tree exp
, tsubst_flags_t complain
)
347 tree cond
= NULL_TREE
, initial_expr
= exp
;
350 if (exp
== error_mark_node
|| !typeid_ok_p ())
351 return error_mark_node
;
353 if (processing_template_decl
)
354 return build_min (TYPEID_EXPR
, const_type_info_type_node
, exp
);
356 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
357 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
360 /* So we need to look into the vtable of the type of exp.
361 Make sure it isn't a null lvalue. */
362 exp
= cp_build_addr_expr (exp
, complain
);
363 exp
= save_expr (exp
);
364 cond
= cp_convert (boolean_type_node
, exp
, complain
);
365 exp
= cp_build_fold_indirect_ref (exp
);
368 exp
= get_tinfo_decl_dynamic (exp
, complain
);
370 if (exp
== error_mark_node
)
371 return error_mark_node
;
375 tree bad
= throw_bad_typeid ();
377 exp
= build3 (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
380 mark_type_use (initial_expr
);
385 /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that
386 comparisons will be done by pointer rather than string comparison. */
388 tinfo_name (tree type
, bool mark_private
)
394 name
= mangle_type_string (type
);
395 length
= strlen (name
);
399 /* Inject '*' at beginning of name to force pointer comparison. */
400 char* buf
= (char*) XALLOCAVEC (char, length
+ 2);
402 memcpy (buf
+ 1, name
, length
+ 1);
403 name_string
= build_string (length
+ 2, buf
);
406 name_string
= build_string (length
+ 1, name
);
408 return fix_string_type (name_string
);
411 /* Return a VAR_DECL for the internal ABI defined type_info object for
412 TYPE. You must arrange that the decl is mark_used, if actually use
413 it --- decls in vtables are only used if the vtable is output. */
416 get_tinfo_decl (tree type
)
421 if (variably_modified_type_p (type
, /*fn=*/NULL_TREE
))
423 error ("cannot create type information for type %qT because "
424 "it involves types of variable size",
426 return error_mark_node
;
429 if (TREE_CODE (type
) == METHOD_TYPE
)
430 type
= build_function_type (TREE_TYPE (type
),
431 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
433 type
= complete_type (type
);
435 /* For a class type, the variable is cached in the type node
437 if (CLASS_TYPE_P (type
))
439 d
= CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
));
444 name
= mangle_typeinfo_for_type (type
);
446 d
= get_global_binding (name
);
449 int ix
= get_pseudo_ti_index (type
);
450 const tinfo_s
*ti
= get_tinfo_desc (ix
);
452 d
= build_lang_decl (VAR_DECL
, name
, ti
->type
);
453 SET_DECL_ASSEMBLER_NAME (d
, name
);
454 /* Remember the type it is for. */
455 TREE_TYPE (name
) = type
;
456 DECL_TINFO_P (d
) = 1;
457 DECL_ARTIFICIAL (d
) = 1;
458 DECL_IGNORED_P (d
) = 1;
459 TREE_READONLY (d
) = 1;
462 /* Mark the variable as undefined -- but remember that we can
463 define it later if we need to do so. */
464 DECL_EXTERNAL (d
) = 1;
465 DECL_NOT_REALLY_EXTERN (d
) = 1;
466 set_linkage_according_to_type (type
, d
);
468 d
= pushdecl_top_level_and_finish (d
, NULL_TREE
);
469 if (CLASS_TYPE_P (type
))
470 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type
)) = d
;
472 /* Add decl to the global array of tinfo decls. */
473 vec_safe_push (unemitted_tinfo_decls
, d
);
479 /* Return a pointer to a type_info object describing TYPE, suitably
480 cast to the language defined type. */
483 get_tinfo_ptr (tree type
)
485 tree decl
= get_tinfo_decl (type
);
488 return build_nop (type_info_ptr_type
,
489 build_address (decl
));
492 /* Return the type_info object for TYPE. */
495 get_typeid (tree type
, tsubst_flags_t complain
)
497 if (type
== error_mark_node
|| !typeid_ok_p ())
498 return error_mark_node
;
500 if (processing_template_decl
)
501 return build_min (TYPEID_EXPR
, const_type_info_type_node
, type
);
503 /* If the type of the type-id is a reference type, the result of the
504 typeid expression refers to a type_info object representing the
506 type
= non_reference (type
);
508 /* This is not one of the uses of a qualified function type in 8.3.5. */
509 if (TREE_CODE (type
) == FUNCTION_TYPE
510 && (type_memfn_quals (type
) != TYPE_UNQUALIFIED
511 || type_memfn_rqual (type
) != REF_QUAL_NONE
))
513 if (complain
& tf_error
)
514 error ("%<typeid%> of qualified function type %qT", type
);
515 return error_mark_node
;
518 /* The top-level cv-qualifiers of the lvalue expression or the type-id
519 that is the operand of typeid are always ignored. */
520 type
= cv_unqualified (type
);
522 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
523 if (CLASS_TYPE_P (type
) || type
== unknown_type_node
524 || type
== init_list_type_node
)
525 type
= complete_type_or_maybe_complain (type
, NULL_TREE
, complain
);
528 return error_mark_node
;
530 return cp_build_fold_indirect_ref (get_tinfo_ptr (type
));
533 /* Check whether TEST is null before returning RESULT. If TEST is used in
534 RESULT, it must have previously had a save_expr applied to it. */
537 ifnonnull (tree test
, tree result
, tsubst_flags_t complain
)
539 tree cond
= build2 (NE_EXPR
, boolean_type_node
, test
,
540 cp_convert (TREE_TYPE (test
), nullptr_node
, complain
));
541 /* This is a compiler generated comparison, don't emit
542 e.g. -Wnonnull-compare warning for it. */
543 TREE_NO_WARNING (cond
) = 1;
544 return build3 (COND_EXPR
, TREE_TYPE (result
), cond
, result
,
545 cp_convert (TREE_TYPE (result
), nullptr_node
, complain
));
548 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
552 build_dynamic_cast_1 (location_t loc
, tree type
, tree expr
,
553 tsubst_flags_t complain
)
555 enum tree_code tc
= TREE_CODE (type
);
558 tree old_expr
= expr
;
559 const char *errstr
= NULL
;
561 /* Save casted types in the function's used types hash table. */
562 used_types_insert (type
);
564 /* T shall be a pointer or reference to a complete class type, or
565 `pointer to cv void''. */
569 if (VOID_TYPE_P (TREE_TYPE (type
)))
573 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type
)))
575 errstr
= _("target is not pointer or reference to class");
578 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
580 errstr
= _("target is not pointer or reference to complete type");
586 errstr
= _("target is not pointer or reference");
590 if (tc
== POINTER_TYPE
)
592 expr
= decay_conversion (expr
, complain
);
593 exprtype
= TREE_TYPE (expr
);
595 /* If T is a pointer type, v shall be an rvalue of a pointer to
596 complete class type, and the result is an rvalue of type T. */
598 expr
= mark_rvalue_use (expr
);
600 if (!TYPE_PTR_P (exprtype
))
602 errstr
= _("source is not a pointer");
605 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype
)))
607 errstr
= _("source is not a pointer to class");
610 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
612 errstr
= _("source is a pointer to incomplete type");
618 expr
= mark_lvalue_use (expr
);
619 exprtype
= TREE_TYPE (expr
);
621 /* T is a reference type, v shall be an lvalue of a complete class
622 type, and the result is an lvalue of the type referred to by T. */
623 if (! MAYBE_CLASS_TYPE_P (exprtype
))
625 errstr
= _("source is not of class type");
628 if (!COMPLETE_TYPE_P (complete_type (exprtype
)))
630 errstr
= _("source is of incomplete class type");
634 exprtype
= cp_build_reference_type (exprtype
, !lvalue_p (expr
));
637 /* The dynamic_cast operator shall not cast away constness. */
638 if (!at_least_as_qualified_p (TREE_TYPE (type
),
639 TREE_TYPE (exprtype
)))
641 errstr
= _("conversion casts away constness");
645 /* If *type is an unambiguous accessible base class of *exprtype,
646 convert statically. */
648 tree binfo
= lookup_base (TREE_TYPE (exprtype
), TREE_TYPE (type
),
649 ba_check
, NULL
, complain
);
651 return build_static_cast (loc
, type
, expr
, complain
);
654 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
655 if (tc
== REFERENCE_TYPE
)
656 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
657 LOOKUP_NORMAL
, NULL_TREE
, complain
);
659 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
660 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
663 /* if TYPE is `void *', return pointer to complete object. */
664 if (tc
== POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (type
)))
666 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
667 if (TREE_CODE (expr
) == ADDR_EXPR
668 && VAR_P (TREE_OPERAND (expr
, 0))
669 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
670 return build1 (NOP_EXPR
, type
, expr
);
672 /* Since expr is used twice below, save it. */
673 expr
= save_expr (expr
);
675 expr1
= build_headof (expr
);
676 if (TREE_TYPE (expr1
) != type
)
677 expr1
= build1 (NOP_EXPR
, type
, expr1
);
678 return ifnonnull (expr
, expr1
, complain
);
683 tree result
, td2
, td3
;
685 tree static_type
, target_type
, boff
;
687 /* If we got here, we can't convert statically. Therefore,
688 dynamic_cast<D&>(b) (b an object) cannot succeed. */
689 if (tc
== REFERENCE_TYPE
)
692 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
694 tree expr
= throw_bad_cast ();
695 if (complain
& tf_warning
)
697 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
699 /* Bash it to the expected type. */
700 TREE_TYPE (expr
) = type
;
704 /* Ditto for dynamic_cast<D*>(&b). */
705 else if (TREE_CODE (expr
) == ADDR_EXPR
)
707 tree op
= TREE_OPERAND (expr
, 0);
709 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
711 if (complain
& tf_warning
)
713 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
715 retval
= build_int_cst (type
, 0);
720 /* Use of dynamic_cast when -fno-rtti is prohibited. */
723 if (complain
& tf_error
)
725 "%<dynamic_cast%> not permitted with %<-fno-rtti%>");
726 return error_mark_node
;
729 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
730 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
731 td2
= get_tinfo_decl (target_type
);
732 if (!mark_used (td2
, complain
) && !(complain
& tf_error
))
733 return error_mark_node
;
734 td2
= cp_build_addr_expr (td2
, complain
);
735 td3
= get_tinfo_decl (static_type
);
736 if (!mark_used (td3
, complain
) && !(complain
& tf_error
))
737 return error_mark_node
;
738 td3
= cp_build_addr_expr (td3
, complain
);
740 /* Determine how T and V are related. */
741 boff
= dcast_base_hint (static_type
, target_type
);
743 /* Since expr is used twice below, save it. */
744 expr
= save_expr (expr
);
747 if (tc
== REFERENCE_TYPE
)
748 expr1
= cp_build_addr_expr (expr1
, complain
);
755 dcast_fn
= dynamic_cast_node
;
762 push_abi_namespace ();
763 tinfo_ptr
= xref_tag (class_type
,
764 get_identifier ("__class_type_info"),
765 /*tag_scope=*/ts_current
, false);
767 tinfo_ptr
= build_pointer_type
768 (cp_build_qualified_type
769 (tinfo_ptr
, TYPE_QUAL_CONST
));
770 name
= "__dynamic_cast";
771 tmp
= build_function_type_list (ptr_type_node
,
773 tinfo_ptr
, tinfo_ptr
,
774 ptrdiff_type_node
, NULL_TREE
);
775 dcast_fn
= build_library_fn_ptr (name
, tmp
,
776 ECF_LEAF
| ECF_PURE
| ECF_NOTHROW
);
777 pop_abi_namespace ();
778 dynamic_cast_node
= dcast_fn
;
780 result
= build_cxx_call (dcast_fn
, 4, elems
, complain
);
781 SET_EXPR_LOCATION (result
, loc
);
783 if (tc
== REFERENCE_TYPE
)
785 tree bad
= throw_bad_cast ();
788 result
= save_expr (result
);
789 neq
= cp_truthvalue_conversion (result
, complain
);
790 return cp_convert (type
,
791 build3 (COND_EXPR
, TREE_TYPE (result
),
792 neq
, result
, bad
), complain
);
795 /* Now back to the type we want from a void*. */
796 result
= cp_convert (type
, result
, complain
);
797 return ifnonnull (expr
, result
, complain
);
801 errstr
= _("source type is not polymorphic");
804 if (complain
& tf_error
)
805 error_at (loc
, "cannot %<dynamic_cast%> %qE (of type %q#T) "
807 old_expr
, TREE_TYPE (old_expr
), type
, errstr
);
808 return error_mark_node
;
812 build_dynamic_cast (location_t loc
, tree type
, tree expr
,
813 tsubst_flags_t complain
)
817 if (type
== error_mark_node
|| expr
== error_mark_node
)
818 return error_mark_node
;
820 if (processing_template_decl
)
822 expr
= build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
823 TREE_SIDE_EFFECTS (expr
) = 1;
824 r
= convert_from_reference (expr
);
825 protected_set_expr_location (r
, loc
);
829 r
= convert_from_reference (build_dynamic_cast_1 (loc
, type
, expr
,
831 if (r
!= error_mark_node
)
832 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
833 protected_set_expr_location (r
, loc
);
837 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
840 qualifier_flags (tree type
)
843 int quals
= cp_type_quals (type
);
845 if (quals
& TYPE_QUAL_CONST
)
847 if (quals
& TYPE_QUAL_VOLATILE
)
849 if (quals
& TYPE_QUAL_RESTRICT
)
854 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
855 contains a pointer to member of an incomplete class. */
858 target_incomplete_p (tree type
)
861 if (TYPE_PTRDATAMEM_P (type
))
863 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)))
865 type
= TYPE_PTRMEM_POINTED_TO_TYPE (type
);
867 else if (TYPE_PTR_P (type
))
868 type
= TREE_TYPE (type
);
870 return !COMPLETE_OR_VOID_TYPE_P (type
);
873 /* Returns true if TYPE involves an incomplete class type; in that
874 case, typeinfo variables for TYPE should be emitted with internal
878 involves_incomplete_p (tree type
)
880 switch (TREE_CODE (type
))
883 return target_incomplete_p (TREE_TYPE (type
));
888 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type
))
889 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)));
892 if (TYPE_PTRMEMFUNC_P (type
))
896 if (!COMPLETE_TYPE_P (type
))
900 /* All other types do not involve incomplete class types. */
905 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
906 is the vtable pointer and NTBS name. The NTBS name is emitted as a
907 comdat const char array, so it becomes a unique key for the type. Generate
908 and emit that VAR_DECL here. (We can't always emit the type_info itself
909 as comdat, because of pointers to incomplete.) */
912 tinfo_base_init (tinfo_s
*ti
, tree target
)
917 vec
<constructor_elt
, va_gc
> *v
;
920 tree name_name
, name_string
;
922 /* Generate the NTBS array variable. */
923 tree name_type
= build_cplus_array_type
924 (cp_build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
927 /* Determine the name of the variable -- and remember with which
928 type it is associated. */
929 name_name
= mangle_typeinfo_string_for_type (target
);
930 TREE_TYPE (name_name
) = target
;
932 name_decl
= build_lang_decl (VAR_DECL
, name_name
, name_type
);
933 SET_DECL_ASSEMBLER_NAME (name_decl
, name_name
);
934 DECL_ARTIFICIAL (name_decl
) = 1;
935 DECL_IGNORED_P (name_decl
) = 1;
936 TREE_READONLY (name_decl
) = 1;
937 TREE_STATIC (name_decl
) = 1;
938 DECL_EXTERNAL (name_decl
) = 0;
939 DECL_TINFO_P (name_decl
) = 1;
940 set_linkage_according_to_type (target
, name_decl
);
941 import_export_decl (name_decl
);
942 name_string
= tinfo_name (target
, !TREE_PUBLIC (name_decl
));
943 DECL_INITIAL (name_decl
) = name_string
;
944 mark_used (name_decl
);
945 pushdecl_top_level_and_finish (name_decl
, name_string
);
948 vtable_ptr
= ti
->vtable
;
952 push_abi_namespace ();
953 real_type
= xref_tag (class_type
, ti
->name
,
954 /*tag_scope=*/ts_current
, false);
955 pop_abi_namespace ();
957 if (!COMPLETE_TYPE_P (real_type
))
959 /* We never saw a definition of this type, so we need to
960 tell the compiler that this is an exported class, as
961 indeed all of the __*_type_info classes are. */
962 SET_CLASSTYPE_INTERFACE_KNOWN (real_type
);
963 CLASSTYPE_INTERFACE_ONLY (real_type
) = 1;
966 vtable_ptr
= get_vtable_decl (real_type
, /*complete=*/1);
967 vtable_ptr
= cp_build_addr_expr (vtable_ptr
, tf_warning_or_error
);
969 /* We need to point into the middle of the vtable. */
970 vtable_ptr
= fold_build_pointer_plus
972 size_binop (MULT_EXPR
,
973 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE
),
974 TYPE_SIZE_UNIT (vtable_entry_type
)));
976 ti
->vtable
= vtable_ptr
;
980 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, vtable_ptr
);
981 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
982 decay_conversion (name_decl
, tf_warning_or_error
));
984 init
= build_constructor (init_list_type_node
, v
);
985 TREE_CONSTANT (init
) = 1;
986 TREE_STATIC (init
) = 1;
991 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
992 information about the particular type_info derivation, which adds no
993 additional fields to the type_info base. */
996 generic_initializer (tinfo_s
*ti
, tree target
)
998 tree init
= tinfo_base_init (ti
, target
);
1000 init
= build_constructor_single (init_list_type_node
, NULL_TREE
, init
);
1001 TREE_CONSTANT (init
) = 1;
1002 TREE_STATIC (init
) = 1;
1006 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1007 TI provides information about the particular type_info derivation,
1008 which adds target type and qualifier flags members to the type_info base. */
1011 ptr_initializer (tinfo_s
*ti
, tree target
)
1013 tree init
= tinfo_base_init (ti
, target
);
1014 tree to
= TREE_TYPE (target
);
1015 int flags
= qualifier_flags (to
);
1016 bool incomplete
= target_incomplete_p (to
);
1017 vec
<constructor_elt
, va_gc
> *v
;
1022 if (tx_safe_fn_type_p (to
))
1025 to
= tx_unsafe_fn_variant (to
);
1027 if (flag_noexcept_type
1028 && FUNC_OR_METHOD_TYPE_P (to
)
1029 && TYPE_NOTHROW_P (to
))
1032 to
= build_exception_variant (to
, NULL_TREE
);
1034 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1035 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, flags
));
1036 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1037 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)));
1039 init
= build_constructor (init_list_type_node
, v
);
1040 TREE_CONSTANT (init
) = 1;
1041 TREE_STATIC (init
) = 1;
1045 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1046 TI provides information about the particular type_info derivation,
1047 which adds class, target type and qualifier flags members to the type_info
1051 ptm_initializer (tinfo_s
*ti
, tree target
)
1053 tree init
= tinfo_base_init (ti
, target
);
1054 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
1055 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
1056 int flags
= qualifier_flags (to
);
1057 bool incomplete
= target_incomplete_p (to
);
1058 vec
<constructor_elt
, va_gc
> *v
;
1063 if (!COMPLETE_TYPE_P (klass
))
1065 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1066 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, build_int_cst (NULL_TREE
, flags
));
1067 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
,
1068 get_tinfo_ptr (TYPE_MAIN_VARIANT (to
)));
1069 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, get_tinfo_ptr (klass
));
1071 init
= build_constructor (init_list_type_node
, v
);
1072 TREE_CONSTANT (init
) = 1;
1073 TREE_STATIC (init
) = 1;
1077 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1078 TI provides information about the particular __class_type_info derivation,
1079 which adds hint flags and N extra initializers to the type_info base. */
1082 class_initializer (tinfo_s
*ti
, tree target
, unsigned n
, ...)
1084 tree init
= tinfo_base_init (ti
, target
);
1085 va_list extra_inits
;
1087 vec
<constructor_elt
, va_gc
> *v
;
1090 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init
);
1091 va_start (extra_inits
, n
);
1092 for (i
= 0; i
< n
; i
++)
1093 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, va_arg (extra_inits
, tree
));
1094 va_end (extra_inits
);
1096 init
= build_constructor (init_list_type_node
, v
);
1097 TREE_CONSTANT (init
) = 1;
1098 TREE_STATIC (init
) = 1;
1102 /* Returns true if the typeinfo for type should be placed in
1103 the runtime library. */
1106 typeinfo_in_lib_p (tree type
)
1108 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1109 library for simple types T. */
1110 if (TYPE_PTR_P (type
)
1111 && (cp_type_quals (TREE_TYPE (type
)) == TYPE_QUAL_CONST
1112 || cp_type_quals (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
))
1113 type
= TREE_TYPE (type
);
1115 switch (TREE_CODE (type
))
1132 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1133 the index of the descriptor in the tinfo_desc vector. */
1136 get_pseudo_ti_init (tree type
, unsigned tk_index
)
1138 tinfo_s
*ti
= get_tinfo_desc (tk_index
);
1140 gcc_assert (at_eof
);
1143 case TK_POINTER_MEMBER_TYPE
:
1144 return ptm_initializer (ti
, type
);
1146 case TK_POINTER_TYPE
:
1147 return ptr_initializer (ti
, type
);
1149 case TK_BUILTIN_TYPE
:
1150 case TK_ENUMERAL_TYPE
:
1151 case TK_FUNCTION_TYPE
:
1153 return generic_initializer (ti
, type
);
1156 return class_initializer (ti
, type
, 0);
1158 case TK_SI_CLASS_TYPE
:
1160 tree base_binfo
= BINFO_BASE_BINFO (TYPE_BINFO (type
), 0);
1161 tree tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
1163 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1164 ti
= &(*tinfo_descs
)[tk_index
];
1165 return class_initializer (ti
, type
, 1, tinfo
);
1170 int hint
= ((CLASSTYPE_REPEATED_BASE_P (type
) << 0)
1171 | (CLASSTYPE_DIAMOND_SHAPED_P (type
) << 1));
1172 tree binfo
= TYPE_BINFO (type
);
1173 unsigned nbases
= BINFO_N_BASE_BINFOS (binfo
);
1174 vec
<tree
, va_gc
> *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1175 tree offset_type
= LONGPTR_T
;
1176 vec
<constructor_elt
, va_gc
> *init_vec
= NULL
;
1178 gcc_assert (tk_index
- TK_VMI_CLASS_TYPES
+ 1 == nbases
);
1180 vec_safe_grow (init_vec
, nbases
, true);
1181 /* Generate the base information initializer. */
1182 for (unsigned ix
= nbases
; ix
--;)
1184 tree base_binfo
= BINFO_BASE_BINFO (binfo
, ix
);
1188 vec
<constructor_elt
, va_gc
> *v
;
1190 if ((*base_accesses
)[ix
] == access_public_node
)
1192 tinfo
= get_tinfo_ptr (BINFO_TYPE (base_binfo
));
1193 if (BINFO_VIRTUAL_P (base_binfo
))
1195 /* We store the vtable offset at which the virtual
1196 base offset can be found. */
1197 offset
= BINFO_VPTR_FIELD (base_binfo
);
1201 offset
= BINFO_OFFSET (base_binfo
);
1203 /* Combine offset and flags into one field. */
1204 offset
= fold_convert (offset_type
, offset
);
1205 offset
= fold_build2_loc (input_location
,
1206 LSHIFT_EXPR
, offset_type
, offset
,
1207 build_int_cst (offset_type
, 8));
1208 offset
= fold_build2_loc (input_location
,
1209 BIT_IOR_EXPR
, offset_type
, offset
,
1210 build_int_cst (offset_type
, flags
));
1212 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, tinfo
);
1213 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, offset
);
1214 tree base_init
= build_constructor (init_list_type_node
, v
);
1215 constructor_elt
*e
= &(*init_vec
)[ix
];
1216 e
->index
= NULL_TREE
;
1217 e
->value
= base_init
;
1219 tree base_inits
= build_constructor (init_list_type_node
, init_vec
);
1221 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1222 ti
= &(*tinfo_descs
)[tk_index
];
1223 return class_initializer (ti
, type
, 3,
1224 build_int_cst (NULL_TREE
, hint
),
1225 build_int_cst (NULL_TREE
, nbases
),
1231 /* Return the index of a pseudo type info type node used to describe
1232 TYPE. TYPE must be a complete type (or cv void), except at the end
1233 of the translation unit. */
1236 get_pseudo_ti_index (tree type
)
1240 switch (TREE_CODE (type
))
1243 ix
= TK_POINTER_MEMBER_TYPE
;
1247 ix
= TK_POINTER_TYPE
;
1251 ix
= TK_ENUMERAL_TYPE
;
1255 ix
= TK_FUNCTION_TYPE
;
1264 if (TYPE_PTRMEMFUNC_P (type
))
1265 ix
= TK_POINTER_MEMBER_TYPE
;
1266 else if (!COMPLETE_TYPE_P (type
))
1269 cxx_incomplete_type_error (NULL_TREE
, type
);
1272 else if (!TYPE_BINFO (type
)
1273 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type
)))
1277 tree binfo
= TYPE_BINFO (type
);
1278 vec
<tree
, va_gc
> *base_accesses
= BINFO_BASE_ACCESSES (binfo
);
1279 tree base_binfo
= BINFO_BASE_BINFO (binfo
, 0);
1280 int num_bases
= BINFO_N_BASE_BINFOS (binfo
);
1283 && (*base_accesses
)[0] == access_public_node
1284 && !BINFO_VIRTUAL_P (base_binfo
)
1285 && integer_zerop (BINFO_OFFSET (base_binfo
)))
1286 /* single non-virtual public. */
1287 ix
= TK_SI_CLASS_TYPE
;
1289 ix
= TK_VMI_CLASS_TYPES
+ num_bases
- 1;
1294 ix
= TK_BUILTIN_TYPE
;
1300 /* Return pointer to tinfo descriptor. Possibly creating the tinfo
1301 descriptor in the first place. */
1304 get_tinfo_desc (unsigned ix
)
1306 unsigned len
= tinfo_descs
->length ();
1310 /* too short, extend. */
1312 vec_safe_reserve (tinfo_descs
, len
);
1314 elt
.type
= elt
.vtable
= elt
.name
= NULL_TREE
;
1316 tinfo_descs
->quick_push (elt
);
1319 tinfo_s
*res
= &(*tinfo_descs
)[ix
];
1324 /* Ok, we have to create it. This layout must be consistent with
1325 that defined in the runtime support. We explicitly manage the
1326 vtable member, and name it for real type as used in the runtime.
1327 The RECORD type has a different name, to avoid collisions. We
1328 have to delay generating the VAR_DECL of the vtable until the end
1329 of the translation, when we'll have seen the library definition,
1330 if there was one. */
1332 /* Fields to add, chained in reverse order. */
1333 tree fields
= NULL_TREE
;
1335 if (ix
>= TK_DERIVED_TYPES
)
1337 /* First field is the pseudo type_info base class. */
1338 tree fld_base
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1339 get_tinfo_desc (TK_TYPE_INFO_TYPE
)->type
);
1341 DECL_CHAIN (fld_base
) = fields
;
1347 case TK_TYPE_INFO_TYPE
:
1349 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1350 NULL_TREE
, const_ptr_type_node
);
1353 tree fld_str
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1354 NULL_TREE
, const_string_type_node
);
1355 DECL_CHAIN (fld_str
) = fields
;
1362 /* Base class internal helper. Pointer to base type, offset to
1364 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1365 NULL_TREE
, type_info_ptr_type
);
1366 DECL_CHAIN (fld_ptr
) = fields
;
1369 tree fld_flag
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1370 NULL_TREE
, LONGPTR_T
);
1371 DECL_CHAIN (fld_flag
) = fields
;
1376 case TK_BUILTIN_TYPE
:
1377 /* Fundamental type_info */
1383 case TK_FUNCTION_TYPE
:
1386 case TK_ENUMERAL_TYPE
:
1389 case TK_POINTER_TYPE
:
1390 case TK_POINTER_MEMBER_TYPE
:
1392 /* Pointer type_info. Adds two fields, qualification mask and
1393 pointer to the pointed to type. This is really a
1394 descendant of __pbase_type_info. */
1395 tree fld_mask
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1396 NULL_TREE
, integer_type_node
);
1397 DECL_CHAIN (fld_mask
) = fields
;
1400 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1401 NULL_TREE
, type_info_ptr_type
);
1402 DECL_CHAIN (fld_ptr
) = fields
;
1405 if (ix
== TK_POINTER_MEMBER_TYPE
)
1407 /* Add a pointer to the class too. */
1408 tree fld_cls
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1409 NULL_TREE
, type_info_ptr_type
);
1410 DECL_CHAIN (fld_cls
) = fields
;
1417 /* Class type_info. No additional fields. */
1420 case TK_SI_CLASS_TYPE
:
1422 /* Single public non-virtual base class. Add pointer to base
1423 class. This is really a descendant of
1424 __class_type_info. */
1425 tree fld_ptr
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1426 NULL_TREE
, type_info_ptr_type
);
1427 DECL_CHAIN (fld_ptr
) = fields
;
1432 default: /* Multiple inheritance. */
1434 unsigned num_bases
= ix
- TK_VMI_CLASS_TYPES
+ 1;
1436 tree fld_flg
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1437 NULL_TREE
, integer_type_node
);
1438 DECL_CHAIN (fld_flg
) = fields
;
1441 tree fld_cnt
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1442 NULL_TREE
, integer_type_node
);
1443 DECL_CHAIN (fld_cnt
) = fields
;
1446 /* Create the array of __base_class_type_info entries. */
1447 tree domain
= build_index_type (size_int (num_bases
- 1));
1448 tree array
= build_array_type (get_tinfo_desc (TK_BASE_TYPE
)->type
,
1450 tree fld_ary
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1452 DECL_CHAIN (fld_ary
) = fields
;
1458 push_abi_namespace ();
1460 /* Generate the pseudo type name. */
1461 const char *real_name
= tinfo_names
[ix
< TK_VMI_CLASS_TYPES
1462 ? ix
: unsigned (TK_VMI_CLASS_TYPES
)];
1463 size_t name_len
= strlen (real_name
);
1464 char *pseudo_name
= (char *) alloca (name_len
+ 30);
1465 memcpy (pseudo_name
, real_name
, name_len
);
1466 /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
1467 apply it to all. See get_peudo_tinfo_index where we make use of
1469 sprintf (pseudo_name
+ name_len
, "_pseudo_%d", ix
);
1471 /* Create the pseudo type. */
1472 tree pseudo_type
= make_class_type (RECORD_TYPE
);
1473 /* Pass the fields chained in reverse. */
1474 finish_builtin_struct (pseudo_type
, pseudo_name
, fields
, NULL_TREE
);
1475 CLASSTYPE_AS_BASE (pseudo_type
) = pseudo_type
;
1476 xref_basetypes (pseudo_type
, /*bases=*/NULL_TREE
);
1478 res
->type
= cp_build_qualified_type (pseudo_type
, TYPE_QUAL_CONST
);
1479 res
->name
= get_identifier (real_name
);
1481 /* Pretend this is public so determine_visibility doesn't give vtables
1482 internal linkage. */
1483 TREE_PUBLIC (TYPE_MAIN_DECL (res
->type
)) = 1;
1485 pop_abi_namespace ();
1489 /* We lazily create the type info types. */
1492 create_tinfo_types (void)
1494 gcc_assert (!tinfo_descs
);
1496 vec_alloc (tinfo_descs
, TK_MAX
+ 20);
1499 /* Helper for emit_support_tinfos. Emits the type_info descriptor of
1503 emit_support_tinfo_1 (tree bltn
)
1507 if (bltn
== NULL_TREE
)
1510 types
[1] = build_pointer_type (bltn
);
1511 types
[2] = build_pointer_type (cp_build_qualified_type (bltn
,
1514 for (int i
= 0; i
< 3; ++i
)
1516 tree tinfo
= get_tinfo_decl (types
[i
]);
1517 TREE_USED (tinfo
) = 1;
1518 mark_needed (tinfo
);
1519 /* The C++ ABI requires that these objects be COMDAT. But,
1520 On systems without weak symbols, initialized COMDAT
1521 objects are emitted with internal linkage. (See
1522 comdat_linkage for details.) Since we want these objects
1523 to have external linkage so that copies do not have to be
1524 emitted in code outside the runtime library, we make them
1527 It might also not be necessary to follow this detail of the
1529 if (!flag_weak
|| ! targetm
.cxx
.library_rtti_comdat ())
1531 gcc_assert (TREE_PUBLIC (tinfo
) && !DECL_COMDAT (tinfo
));
1532 DECL_INTERFACE_KNOWN (tinfo
) = 1;
1537 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1538 support. Generating them here guarantees consistency with the other
1539 structures. We use the following heuristic to determine when the runtime
1540 is being generated. If std::__fundamental_type_info is defined, and its
1541 destructor is defined, then the runtime is being built. */
1544 emit_support_tinfos (void)
1546 /* Dummy static variable so we can put nullptr in the array; it will be
1547 set before we actually start to walk the array. */
1548 static tree
*const fundamentals
[] =
1552 &wchar_type_node
, &char8_type_node
, &char16_type_node
, &char32_type_node
,
1553 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1554 &short_integer_type_node
, &short_unsigned_type_node
,
1555 &integer_type_node
, &unsigned_type_node
,
1556 &long_integer_type_node
, &long_unsigned_type_node
,
1557 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1558 &float_type_node
, &double_type_node
, &long_double_type_node
,
1559 &dfloat32_type_node
, &dfloat64_type_node
, &dfloat128_type_node
,
1565 /* Look for a defined class. */
1566 tree bltn_type
= lookup_qualified_name
1567 (abi_node
, "__fundamental_type_info", LOOK_want::TYPE
, false);
1568 if (TREE_CODE (bltn_type
) != TYPE_DECL
)
1571 bltn_type
= TREE_TYPE (bltn_type
);
1572 if (!COMPLETE_TYPE_P (bltn_type
))
1574 tree dtor
= CLASSTYPE_DESTRUCTOR (bltn_type
);
1575 if (!dtor
|| DECL_EXTERNAL (dtor
))
1578 /* All these are really builtins. So set the location. */
1579 location_t saved_loc
= input_location
;
1580 input_location
= BUILTINS_LOCATION
;
1582 for (ix
= 0; fundamentals
[ix
]; ix
++)
1583 emit_support_tinfo_1 (*fundamentals
[ix
]);
1584 for (ix
= 0; ix
< NUM_INT_N_ENTS
; ix
++)
1585 if (int_n_enabled_p
[ix
])
1587 emit_support_tinfo_1 (int_n_trees
[ix
].signed_type
);
1588 emit_support_tinfo_1 (int_n_trees
[ix
].unsigned_type
);
1590 for (tree t
= registered_builtin_types
; t
; t
= TREE_CHAIN (t
))
1591 emit_support_tinfo_1 (TREE_VALUE (t
));
1592 /* For compatibility, emit DFP typeinfos even when DFP isn't enabled,
1593 because we've emitted that in the past. */
1594 if (!targetm
.decimal_float_supported_p ())
1596 gcc_assert (dfloat32_type_node
== NULL_TREE
1597 && dfloat64_type_node
== NULL_TREE
1598 && dfloat128_type_node
== NULL_TREE
);
1599 fallback_dfloat32_type
= make_node (REAL_TYPE
);
1600 fallback_dfloat64_type
= make_node (REAL_TYPE
);
1601 fallback_dfloat128_type
= make_node (REAL_TYPE
);
1602 emit_support_tinfo_1 (fallback_dfloat32_type
);
1603 emit_support_tinfo_1 (fallback_dfloat64_type
);
1604 emit_support_tinfo_1 (fallback_dfloat128_type
);
1606 input_location
= saved_loc
;
1609 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1610 tinfo decl. Determine whether it needs emitting, and if so
1611 generate the initializer. */
1614 emit_tinfo_decl (tree decl
)
1616 tree type
= TREE_TYPE (DECL_NAME (decl
));
1617 int in_library
= typeinfo_in_lib_p (type
);
1619 gcc_assert (DECL_TINFO_P (decl
));
1624 DECL_EXTERNAL (decl
) = 0;
1627 /* If we're not in the runtime, then DECL (which is already
1628 DECL_EXTERNAL) will not be defined here. */
1629 DECL_INTERFACE_KNOWN (decl
) = 1;
1633 else if (involves_incomplete_p (type
))
1635 if (!decl_needed_p (decl
))
1637 /* If TYPE involves an incomplete class type, then the typeinfo
1638 object will be emitted with internal linkage. There is no
1639 way to know whether or not types are incomplete until the end
1640 of the compilation, so this determination must be deferred
1641 until this point. */
1642 TREE_PUBLIC (decl
) = 0;
1643 DECL_EXTERNAL (decl
) = 0;
1644 DECL_INTERFACE_KNOWN (decl
) = 1;
1647 import_export_decl (decl
);
1648 if (DECL_NOT_REALLY_EXTERN (decl
) && decl_needed_p (decl
))
1652 DECL_EXTERNAL (decl
) = 0;
1653 init
= get_pseudo_ti_init (type
, get_pseudo_ti_index (type
));
1654 DECL_INITIAL (decl
) = init
;
1656 cp_finish_decl (decl
, init
, false, NULL_TREE
, 0);
1657 /* Avoid targets optionally bumping up the alignment to improve
1658 vector instruction accesses, tinfo are never accessed this way. */
1659 #ifdef DATA_ABI_ALIGNMENT
1660 SET_DECL_ALIGN (decl
, DATA_ABI_ALIGNMENT (decl
, TYPE_ALIGN (TREE_TYPE (decl
))));
1661 DECL_USER_ALIGN (decl
) = true;
1669 #include "gt-cp-rtti.h"