[gcc]
[official-gcc.git] / gcc / cp / rtti.c
blobd73b1bcfa973de1b2d42206005eb1e5b74623dce
1 /* RunTime Type Identification
2 Copyright (C) 1995-2017 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "cp-tree.h"
26 #include "memmodel.h"
27 #include "tm_p.h"
28 #include "stringpool.h"
29 #include "intl.h"
30 #include "stor-layout.h"
31 #include "c-family/c-pragma.h"
33 /* C++ returns type information to the user in struct type_info
34 objects. We also use type information to implement dynamic_cast and
35 exception handlers. Type information for a particular type is
36 indicated with an ABI defined structure derived from type_info.
37 This would all be very straight forward, but for the fact that the
38 runtime library provides the definitions of the type_info structure
39 and the ABI defined derived classes. We cannot build declarations
40 of them directly in the compiler, but we need to layout objects of
41 their type. Somewhere we have to lie.
43 We define layout compatible POD-structs with compiler-defined names
44 and generate the appropriate initializations for them (complete
45 with explicit mention of their vtable). When we have to provide a
46 type_info to the user we reinterpret_cast the internal compiler
47 type to type_info. A well formed program can only explicitly refer
48 to the type_infos of complete types (& cv void). However, we chain
49 pointer type_infos to the pointed-to-type, and that can be
50 incomplete. We only need the addresses of such incomplete
51 type_info objects for static initialization.
53 The type information VAR_DECL of a type is held on the
54 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
55 will be the internal type. It will usually have the correct
56 internal type reflecting the kind of type it represents (pointer,
57 array, function, class, inherited class, etc). When the type it
58 represents is incomplete, it will have the internal type
59 corresponding to type_info. That will only happen at the end of
60 translation, when we are emitting the type info objects. */
62 /* Auxiliary data we hold for each type_info derived object we need. */
63 struct GTY (()) tinfo_s {
64 tree type; /* The RECORD_TYPE for this type_info object */
66 tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
67 translation. */
69 tree name; /* IDENTIFIER_NODE for the ABI specified name of
70 the type_info derived type. */
74 enum tinfo_kind
76 TK_TYPE_INFO_TYPE, /* abi::__type_info_pseudo */
77 TK_BASE_TYPE, /* abi::__base_class_type_info */
78 TK_DERIVED_TYPES, /* Start of types derived from abi::__type_info */
79 TK_BUILTIN_TYPE = TK_DERIVED_TYPES, /* abi::__fundamental_type_info */
80 TK_ARRAY_TYPE, /* abi::__array_type_info */
81 TK_FUNCTION_TYPE, /* abi::__function_type_info */
82 TK_ENUMERAL_TYPE, /* abi::__enum_type_info */
83 TK_POINTER_TYPE, /* abi::__pointer_type_info */
84 TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
85 TK_CLASS_TYPE, /* abi::__class_type_info */
86 TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */
87 TK_VMI_CLASS_TYPES, /* abi::__vmi_class_type_info<int> */
88 TK_MAX
91 /* Names of the tinfo types. Must be same order as TK enumeration
92 above. */
94 static const char *const tinfo_names[TK_MAX] =
96 "__type_info",
97 "__base_class_type_info",
98 "__fundamental_type_info",
99 "__array_type_info",
100 "__function_type_info",
101 "__enum_type_info",
102 "__pointer_type_info",
103 "__pointer_to_member_type_info",
104 "__class_type_info",
105 "__si_class_type_info",
106 "__vmi_class_type_info"
109 /* Helper macro to get maximum scalar-width of pointer or of the 'long'-type.
110 This of interest for llp64 targets. */
111 #define LONGPTR_T \
112 integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \
113 ? itk_long : itk_long_long)]
115 /* A vector of all tinfo decls that haven't yet been emitted. */
116 vec<tree, va_gc> *unemitted_tinfo_decls;
118 /* A vector of all type_info derived types we need. The first few are
119 fixed and created early. The remainder are for multiple inheritance
120 and are generated as needed. */
121 static GTY (()) vec<tinfo_s, va_gc> *tinfo_descs;
123 static tree ifnonnull (tree, tree, tsubst_flags_t);
124 static tree tinfo_name (tree, bool);
125 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
126 static tree throw_bad_cast (void);
127 static tree throw_bad_typeid (void);
128 static tree get_tinfo_ptr (tree);
129 static bool typeid_ok_p (void);
130 static int qualifier_flags (tree);
131 static bool target_incomplete_p (tree);
132 static tree tinfo_base_init (tinfo_s *, tree);
133 static tree generic_initializer (tinfo_s *, tree);
134 static tree ptr_initializer (tinfo_s *, tree);
135 static tree ptm_initializer (tinfo_s *, tree);
136 static tree class_initializer (tinfo_s *, tree, unsigned, ...);
137 static tree get_pseudo_ti_init (tree, unsigned);
138 static unsigned get_pseudo_ti_index (tree);
139 static tinfo_s *get_tinfo_desc (unsigned);
140 static void create_tinfo_types (void);
141 static bool typeinfo_in_lib_p (tree);
143 static int doing_runtime = 0;
145 static void
146 push_abi_namespace (void)
148 push_nested_namespace (abi_node);
149 push_visibility ("default", 2);
152 static void
153 pop_abi_namespace (void)
155 pop_visibility (2);
156 pop_nested_namespace (abi_node);
159 /* Declare language defined type_info type and a pointer to const
160 type_info. This is incomplete here, and will be completed when
161 the user #includes <typeinfo>. There are language defined
162 restrictions on what can be done until that is included. Create
163 the internal versions of the ABI types. */
165 void
166 init_rtti_processing (void)
168 tree type_info_type;
170 push_namespace (std_identifier);
171 type_info_type = xref_tag (class_type, get_identifier ("type_info"),
172 /*tag_scope=*/ts_current, false);
173 pop_namespace ();
174 const_type_info_type_node
175 = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST);
176 type_info_ptr_type = build_pointer_type (const_type_info_type_node);
178 vec_alloc (unemitted_tinfo_decls, 124);
180 create_tinfo_types ();
183 /* Given the expression EXP of type `class *', return the head of the
184 object pointed to by EXP with type cv void*, if the class has any
185 virtual functions (TYPE_POLYMORPHIC_P), else just return the
186 expression. */
188 tree
189 build_headof (tree exp)
191 tree type = TREE_TYPE (exp);
192 tree offset;
193 tree index;
195 gcc_assert (TYPE_PTR_P (type));
196 type = TREE_TYPE (type);
198 if (!TYPE_POLYMORPHIC_P (type))
199 return exp;
201 /* We use this a couple of times below, protect it. */
202 exp = save_expr (exp);
204 /* The offset-to-top field is at index -2 from the vptr. */
205 index = build_int_cst (NULL_TREE,
206 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
208 offset = build_vtbl_ref (cp_build_indirect_ref (exp, RO_NULL,
209 tf_warning_or_error),
210 index);
212 type = 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 */
221 static tree
222 throw_bad_cast (void)
224 static tree fn;
225 if (!fn)
227 tree name = get_identifier ("__cxa_bad_cast");
228 fn = IDENTIFIER_GLOBAL_VALUE (name);
229 if (!fn)
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". */
240 static tree
241 throw_bad_typeid (void)
243 static tree fn;
244 if (!fn)
246 tree name = get_identifier ("__cxa_bad_typeid");
247 fn = IDENTIFIER_GLOBAL_VALUE (name);
248 if (!fn)
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. */
264 static tree
265 get_tinfo_decl_dynamic (tree exp, tsubst_flags_t complain)
267 tree type;
268 tree t;
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 (TREE_TYPE (exp));
278 /* Peel off cv qualifiers. */
279 type = TYPE_MAIN_VARIANT (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);
286 if (!type)
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. */
293 tree index;
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);
301 else
302 /* Otherwise return the type_info for the static type of the expr. */
303 t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
305 return cp_build_indirect_ref (t, RO_NULL, complain);
308 static bool
309 typeid_ok_p (void)
311 if (! flag_rtti)
313 error ("cannot use %<typeid%> with -fno-rtti");
314 return false;
317 if (!COMPLETE_TYPE_P (const_type_info_type_node))
319 error ("must %<#include <typeinfo>%> before using %<typeid%>");
320 return false;
323 tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
324 tree real = TYPE_MAIN_VARIANT (const_type_info_type_node);
326 /* Make sure abi::__type_info_pseudo has the same alias set
327 as std::type_info. */
328 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo))
329 TYPE_ALIAS_SET (pseudo) = get_alias_set (real);
330 else
331 gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real));
333 return true;
336 /* Return an expression for "typeid(EXP)". The expression returned is
337 an lvalue of type "const std::type_info". */
339 tree
340 build_typeid (tree exp, tsubst_flags_t complain)
342 tree cond = NULL_TREE, initial_expr = exp;
343 int nonnull = 0;
345 if (exp == error_mark_node || !typeid_ok_p ())
346 return error_mark_node;
348 if (processing_template_decl)
349 return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
351 /* FIXME when integrating with c_fully_fold, mark
352 resolves_to_fixed_type_p case as a non-constant expression. */
353 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
354 && ! resolves_to_fixed_type_p (exp, &nonnull)
355 && ! nonnull)
357 /* So we need to look into the vtable of the type of exp.
358 Make sure it isn't a null lvalue. */
359 exp = cp_build_addr_expr (exp, complain);
360 exp = save_expr (exp);
361 cond = cp_convert (boolean_type_node, exp, complain);
362 exp = cp_build_indirect_ref (exp, RO_NULL, complain);
365 exp = get_tinfo_decl_dynamic (exp, complain);
367 if (exp == error_mark_node)
368 return error_mark_node;
370 if (cond)
372 tree bad = throw_bad_typeid ();
374 exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
376 else
377 mark_type_use (initial_expr);
379 return exp;
382 /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that
383 comparisons will be done by pointer rather than string comparison. */
384 static tree
385 tinfo_name (tree type, bool mark_private)
387 const char *name;
388 int length;
389 tree name_string;
391 name = mangle_type_string (type);
392 length = strlen (name);
394 if (mark_private)
396 /* Inject '*' at beginning of name to force pointer comparison. */
397 char* buf = (char*) XALLOCAVEC (char, length + 2);
398 buf[0] = '*';
399 memcpy (buf + 1, name, length + 1);
400 name_string = build_string (length + 2, buf);
402 else
403 name_string = build_string (length + 1, name);
405 return fix_string_type (name_string);
408 /* Return a VAR_DECL for the internal ABI defined type_info object for
409 TYPE. You must arrange that the decl is mark_used, if actually use
410 it --- decls in vtables are only used if the vtable is output. */
412 tree
413 get_tinfo_decl (tree type)
415 tree name;
416 tree d;
418 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
420 error ("cannot create type information for type %qT because "
421 "it involves types of variable size",
422 type);
423 return error_mark_node;
426 if (TREE_CODE (type) == METHOD_TYPE)
427 type = build_function_type (TREE_TYPE (type),
428 TREE_CHAIN (TYPE_ARG_TYPES (type)));
430 type = complete_type (type);
432 /* For a class type, the variable is cached in the type node
433 itself. */
434 if (CLASS_TYPE_P (type))
436 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
437 if (d)
438 return d;
441 name = mangle_typeinfo_for_type (type);
443 d = IDENTIFIER_GLOBAL_VALUE (name);
444 if (!d)
446 int ix = get_pseudo_ti_index (type);
447 const tinfo_s *ti = get_tinfo_desc (ix);
449 d = build_lang_decl (VAR_DECL, name, ti->type);
450 SET_DECL_ASSEMBLER_NAME (d, name);
451 /* Remember the type it is for. */
452 TREE_TYPE (name) = type;
453 DECL_TINFO_P (d) = 1;
454 DECL_ARTIFICIAL (d) = 1;
455 DECL_IGNORED_P (d) = 1;
456 TREE_READONLY (d) = 1;
457 TREE_STATIC (d) = 1;
458 /* Mark the variable as undefined -- but remember that we can
459 define it later if we need to do so. */
460 DECL_EXTERNAL (d) = 1;
461 DECL_NOT_REALLY_EXTERN (d) = 1;
462 set_linkage_according_to_type (type, d);
464 d = pushdecl_top_level_and_finish (d, NULL_TREE);
465 if (CLASS_TYPE_P (type))
466 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
468 /* Add decl to the global array of tinfo decls. */
469 vec_safe_push (unemitted_tinfo_decls, d);
472 return d;
475 /* Return a pointer to a type_info object describing TYPE, suitably
476 cast to the language defined type. */
478 static tree
479 get_tinfo_ptr (tree type)
481 tree decl = get_tinfo_decl (type);
483 mark_used (decl);
484 return build_nop (type_info_ptr_type,
485 build_address (decl));
488 /* Return the type_info object for TYPE. */
490 tree
491 get_typeid (tree type, tsubst_flags_t complain)
493 if (type == error_mark_node || !typeid_ok_p ())
494 return error_mark_node;
496 if (processing_template_decl)
497 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
499 /* If the type of the type-id is a reference type, the result of the
500 typeid expression refers to a type_info object representing the
501 referenced type. */
502 type = non_reference (type);
504 /* This is not one of the uses of a qualified function type in 8.3.5. */
505 if (TREE_CODE (type) == FUNCTION_TYPE
506 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
507 || type_memfn_rqual (type) != REF_QUAL_NONE))
509 if (complain & tf_error)
510 error ("typeid of qualified function type %qT", type);
511 return error_mark_node;
514 /* The top-level cv-qualifiers of the lvalue expression or the type-id
515 that is the operand of typeid are always ignored. */
516 type = TYPE_MAIN_VARIANT (type);
518 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
519 if (CLASS_TYPE_P (type) || type == unknown_type_node
520 || type == init_list_type_node)
521 type = complete_type_or_maybe_complain (type, NULL_TREE, complain);
523 if (!type)
524 return error_mark_node;
526 return cp_build_indirect_ref (get_tinfo_ptr (type), RO_NULL, complain);
529 /* Check whether TEST is null before returning RESULT. If TEST is used in
530 RESULT, it must have previously had a save_expr applied to it. */
532 static tree
533 ifnonnull (tree test, tree result, tsubst_flags_t complain)
535 tree cond = build2 (NE_EXPR, boolean_type_node, test,
536 cp_convert (TREE_TYPE (test), nullptr_node, complain));
537 /* This is a compiler generated comparison, don't emit
538 e.g. -Wnonnull-compare warning for it. */
539 TREE_NO_WARNING (cond) = 1;
540 return build3 (COND_EXPR, TREE_TYPE (result), cond, result,
541 cp_convert (TREE_TYPE (result), nullptr_node, complain));
544 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
545 paper. */
547 static tree
548 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
550 enum tree_code tc = TREE_CODE (type);
551 tree exprtype;
552 tree dcast_fn;
553 tree old_expr = expr;
554 const char *errstr = NULL;
556 /* Save casted types in the function's used types hash table. */
557 used_types_insert (type);
559 /* T shall be a pointer or reference to a complete class type, or
560 `pointer to cv void''. */
561 switch (tc)
563 case POINTER_TYPE:
564 if (VOID_TYPE_P (TREE_TYPE (type)))
565 break;
566 /* Fall through. */
567 case REFERENCE_TYPE:
568 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
570 errstr = _("target is not pointer or reference to class");
571 goto fail;
573 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
575 errstr = _("target is not pointer or reference to complete type");
576 goto fail;
578 break;
580 default:
581 errstr = _("target is not pointer or reference");
582 goto fail;
585 if (tc == POINTER_TYPE)
587 expr = decay_conversion (expr, complain);
588 exprtype = TREE_TYPE (expr);
590 /* If T is a pointer type, v shall be an rvalue of a pointer to
591 complete class type, and the result is an rvalue of type T. */
593 expr = mark_rvalue_use (expr);
595 if (!TYPE_PTR_P (exprtype))
597 errstr = _("source is not a pointer");
598 goto fail;
600 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
602 errstr = _("source is not a pointer to class");
603 goto fail;
605 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
607 errstr = _("source is a pointer to incomplete type");
608 goto fail;
611 else
613 expr = mark_lvalue_use (expr);
615 exprtype = build_reference_type (TREE_TYPE (expr));
617 /* T is a reference type, v shall be an lvalue of a complete class
618 type, and the result is an lvalue of the type referred to by T. */
620 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
622 errstr = _("source is not of class type");
623 goto fail;
625 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
627 errstr = _("source is of incomplete class type");
628 goto fail;
632 /* The dynamic_cast operator shall not cast away constness. */
633 if (!at_least_as_qualified_p (TREE_TYPE (type),
634 TREE_TYPE (exprtype)))
636 errstr = _("conversion casts away constness");
637 goto fail;
640 /* If *type is an unambiguous accessible base class of *exprtype,
641 convert statically. */
643 tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
644 ba_check, NULL, complain);
645 if (binfo)
646 return build_static_cast (type, expr, complain);
649 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
650 if (tc == REFERENCE_TYPE)
651 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
652 LOOKUP_NORMAL, NULL_TREE, complain);
654 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
655 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
657 tree expr1;
658 /* if TYPE is `void *', return pointer to complete object. */
659 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
661 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
662 if (TREE_CODE (expr) == ADDR_EXPR
663 && VAR_P (TREE_OPERAND (expr, 0))
664 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
665 return build1 (NOP_EXPR, type, expr);
667 /* Since expr is used twice below, save it. */
668 expr = save_expr (expr);
670 expr1 = build_headof (expr);
671 if (TREE_TYPE (expr1) != type)
672 expr1 = build1 (NOP_EXPR, type, expr1);
673 return ifnonnull (expr, expr1, complain);
675 else
677 tree retval;
678 tree result, td2, td3;
679 tree elems[4];
680 tree static_type, target_type, boff;
682 /* If we got here, we can't convert statically. Therefore,
683 dynamic_cast<D&>(b) (b an object) cannot succeed. */
684 if (tc == REFERENCE_TYPE)
686 if (VAR_P (old_expr)
687 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
689 tree expr = throw_bad_cast ();
690 if (complain & tf_warning)
691 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
692 old_expr, type);
693 /* Bash it to the expected type. */
694 TREE_TYPE (expr) = type;
695 return expr;
698 /* Ditto for dynamic_cast<D*>(&b). */
699 else if (TREE_CODE (expr) == ADDR_EXPR)
701 tree op = TREE_OPERAND (expr, 0);
702 if (VAR_P (op)
703 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
705 if (complain & tf_warning)
706 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
707 op, type);
708 retval = build_int_cst (type, 0);
709 return retval;
713 /* Use of dynamic_cast when -fno-rtti is prohibited. */
714 if (!flag_rtti)
716 if (complain & tf_error)
717 error ("%<dynamic_cast%> not permitted with -fno-rtti");
718 return error_mark_node;
721 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
722 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
723 td2 = get_tinfo_decl (target_type);
724 if (!mark_used (td2, complain) && !(complain & tf_error))
725 return error_mark_node;
726 td2 = cp_build_addr_expr (td2, complain);
727 td3 = get_tinfo_decl (static_type);
728 if (!mark_used (td3, complain) && !(complain & tf_error))
729 return error_mark_node;
730 td3 = cp_build_addr_expr (td3, complain);
732 /* Determine how T and V are related. */
733 boff = dcast_base_hint (static_type, target_type);
735 /* Since expr is used twice below, save it. */
736 expr = save_expr (expr);
738 expr1 = expr;
739 if (tc == REFERENCE_TYPE)
740 expr1 = cp_build_addr_expr (expr1, complain);
742 elems[0] = expr1;
743 elems[1] = td3;
744 elems[2] = td2;
745 elems[3] = boff;
747 dcast_fn = dynamic_cast_node;
748 if (!dcast_fn)
750 tree tmp;
751 tree tinfo_ptr;
752 const char *name;
754 push_abi_namespace ();
755 tinfo_ptr = xref_tag (class_type,
756 get_identifier ("__class_type_info"),
757 /*tag_scope=*/ts_current, false);
759 tinfo_ptr = build_pointer_type
760 (cp_build_qualified_type
761 (tinfo_ptr, TYPE_QUAL_CONST));
762 name = "__dynamic_cast";
763 tmp = build_function_type_list (ptr_type_node,
764 const_ptr_type_node,
765 tinfo_ptr, tinfo_ptr,
766 ptrdiff_type_node, NULL_TREE);
767 dcast_fn = build_library_fn_ptr (name, tmp,
768 ECF_LEAF | ECF_PURE | ECF_NOTHROW);
769 pop_abi_namespace ();
770 dynamic_cast_node = dcast_fn;
772 result = build_cxx_call (dcast_fn, 4, elems, complain);
774 if (tc == REFERENCE_TYPE)
776 tree bad = throw_bad_cast ();
777 tree neq;
779 result = save_expr (result);
780 neq = cp_truthvalue_conversion (result);
781 return cp_convert (type,
782 build3 (COND_EXPR, TREE_TYPE (result),
783 neq, result, bad), complain);
786 /* Now back to the type we want from a void*. */
787 result = cp_convert (type, result, complain);
788 return ifnonnull (expr, result, complain);
791 else
792 errstr = _("source type is not polymorphic");
794 fail:
795 if (complain & tf_error)
796 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
797 old_expr, TREE_TYPE (old_expr), type, errstr);
798 return error_mark_node;
801 tree
802 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
804 tree r;
806 if (type == error_mark_node || expr == error_mark_node)
807 return error_mark_node;
809 if (processing_template_decl)
811 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
812 TREE_SIDE_EFFECTS (expr) = 1;
813 return convert_from_reference (expr);
816 r = convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
817 if (r != error_mark_node)
818 maybe_warn_about_useless_cast (type, expr, complain);
819 return r;
822 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
824 static int
825 qualifier_flags (tree type)
827 int flags = 0;
828 int quals = cp_type_quals (type);
830 if (quals & TYPE_QUAL_CONST)
831 flags |= 1;
832 if (quals & TYPE_QUAL_VOLATILE)
833 flags |= 2;
834 if (quals & TYPE_QUAL_RESTRICT)
835 flags |= 4;
836 return flags;
839 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
840 contains a pointer to member of an incomplete class. */
842 static bool
843 target_incomplete_p (tree type)
845 while (true)
846 if (TYPE_PTRDATAMEM_P (type))
848 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
849 return true;
850 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
852 else if (TYPE_PTR_P (type))
853 type = TREE_TYPE (type);
854 else
855 return !COMPLETE_OR_VOID_TYPE_P (type);
858 /* Returns true if TYPE involves an incomplete class type; in that
859 case, typeinfo variables for TYPE should be emitted with internal
860 linkage. */
862 static bool
863 involves_incomplete_p (tree type)
865 switch (TREE_CODE (type))
867 case POINTER_TYPE:
868 return target_incomplete_p (TREE_TYPE (type));
870 case OFFSET_TYPE:
871 ptrmem:
872 return
873 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
874 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
876 case RECORD_TYPE:
877 if (TYPE_PTRMEMFUNC_P (type))
878 goto ptrmem;
879 /* Fall through. */
880 case UNION_TYPE:
881 if (!COMPLETE_TYPE_P (type))
882 return true;
883 /* Fall through. */
884 default:
885 /* All other types do not involve incomplete class types. */
886 return false;
890 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
891 is the vtable pointer and NTBS name. The NTBS name is emitted as a
892 comdat const char array, so it becomes a unique key for the type. Generate
893 and emit that VAR_DECL here. (We can't always emit the type_info itself
894 as comdat, because of pointers to incomplete.) */
896 static tree
897 tinfo_base_init (tinfo_s *ti, tree target)
899 tree init;
900 tree name_decl;
901 tree vtable_ptr;
902 vec<constructor_elt, va_gc> *v;
905 tree name_name, name_string;
907 /* Generate the NTBS array variable. */
908 tree name_type = build_cplus_array_type
909 (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
910 NULL_TREE);
912 /* Determine the name of the variable -- and remember with which
913 type it is associated. */
914 name_name = mangle_typeinfo_string_for_type (target);
915 TREE_TYPE (name_name) = target;
917 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
918 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
919 DECL_ARTIFICIAL (name_decl) = 1;
920 DECL_IGNORED_P (name_decl) = 1;
921 TREE_READONLY (name_decl) = 1;
922 TREE_STATIC (name_decl) = 1;
923 DECL_EXTERNAL (name_decl) = 0;
924 DECL_TINFO_P (name_decl) = 1;
925 set_linkage_according_to_type (target, name_decl);
926 import_export_decl (name_decl);
927 name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
928 DECL_INITIAL (name_decl) = name_string;
929 mark_used (name_decl);
930 pushdecl_top_level_and_finish (name_decl, name_string);
933 vtable_ptr = ti->vtable;
934 if (!vtable_ptr)
936 tree real_type;
937 push_abi_namespace ();
938 real_type = xref_tag (class_type, ti->name,
939 /*tag_scope=*/ts_current, false);
940 pop_abi_namespace ();
942 if (!COMPLETE_TYPE_P (real_type))
944 /* We never saw a definition of this type, so we need to
945 tell the compiler that this is an exported class, as
946 indeed all of the __*_type_info classes are. */
947 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
948 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
951 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
952 vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
954 /* We need to point into the middle of the vtable. */
955 vtable_ptr = fold_build_pointer_plus
956 (vtable_ptr,
957 size_binop (MULT_EXPR,
958 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
959 TYPE_SIZE_UNIT (vtable_entry_type)));
961 ti->vtable = vtable_ptr;
964 vec_alloc (v, 2);
965 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
966 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
967 decay_conversion (name_decl, tf_warning_or_error));
969 init = build_constructor (init_list_type_node, v);
970 TREE_CONSTANT (init) = 1;
971 TREE_STATIC (init) = 1;
973 return init;
976 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
977 information about the particular type_info derivation, which adds no
978 additional fields to the type_info base. */
980 static tree
981 generic_initializer (tinfo_s *ti, tree target)
983 tree init = tinfo_base_init (ti, target);
985 init = build_constructor_single (init_list_type_node, NULL_TREE, init);
986 TREE_CONSTANT (init) = 1;
987 TREE_STATIC (init) = 1;
988 return init;
991 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
992 TI provides information about the particular type_info derivation,
993 which adds target type and qualifier flags members to the type_info base. */
995 static tree
996 ptr_initializer (tinfo_s *ti, tree target)
998 tree init = tinfo_base_init (ti, target);
999 tree to = TREE_TYPE (target);
1000 int flags = qualifier_flags (to);
1001 bool incomplete = target_incomplete_p (to);
1002 vec<constructor_elt, va_gc> *v;
1003 vec_alloc (v, 3);
1005 if (incomplete)
1006 flags |= 8;
1007 if (tx_safe_fn_type_p (to))
1009 flags |= 0x20;
1010 to = tx_unsafe_fn_variant (to);
1012 if (flag_noexcept_type
1013 && (TREE_CODE (to) == FUNCTION_TYPE
1014 || TREE_CODE (to) == METHOD_TYPE)
1015 && TYPE_NOTHROW_P (to))
1017 flags |= 0x40;
1018 to = build_exception_variant (to, NULL_TREE);
1020 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1021 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1022 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1023 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1025 init = build_constructor (init_list_type_node, v);
1026 TREE_CONSTANT (init) = 1;
1027 TREE_STATIC (init) = 1;
1028 return init;
1031 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1032 TI provides information about the particular type_info derivation,
1033 which adds class, target type and qualifier flags members to the type_info
1034 base. */
1036 static tree
1037 ptm_initializer (tinfo_s *ti, tree target)
1039 tree init = tinfo_base_init (ti, target);
1040 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1041 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1042 int flags = qualifier_flags (to);
1043 bool incomplete = target_incomplete_p (to);
1044 vec<constructor_elt, va_gc> *v;
1045 vec_alloc (v, 4);
1047 if (incomplete)
1048 flags |= 0x8;
1049 if (!COMPLETE_TYPE_P (klass))
1050 flags |= 0x10;
1051 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1052 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1053 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1054 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1055 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_tinfo_ptr (klass));
1057 init = build_constructor (init_list_type_node, v);
1058 TREE_CONSTANT (init) = 1;
1059 TREE_STATIC (init) = 1;
1060 return init;
1063 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1064 TI provides information about the particular __class_type_info derivation,
1065 which adds hint flags and N extra initializers to the type_info base. */
1067 static tree
1068 class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
1070 tree init = tinfo_base_init (ti, target);
1071 va_list extra_inits;
1072 unsigned i;
1073 vec<constructor_elt, va_gc> *v;
1074 vec_alloc (v, n+1);
1076 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1077 va_start (extra_inits, n);
1078 for (i = 0; i < n; i++)
1079 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
1080 va_end (extra_inits);
1082 init = build_constructor (init_list_type_node, v);
1083 TREE_CONSTANT (init) = 1;
1084 TREE_STATIC (init) = 1;
1085 return init;
1088 /* Returns true if the typeinfo for type should be placed in
1089 the runtime library. */
1091 static bool
1092 typeinfo_in_lib_p (tree type)
1094 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1095 library for simple types T. */
1096 if (TYPE_PTR_P (type)
1097 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1098 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1099 type = TREE_TYPE (type);
1101 switch (TREE_CODE (type))
1103 case INTEGER_TYPE:
1104 case BOOLEAN_TYPE:
1105 case REAL_TYPE:
1106 case VOID_TYPE:
1107 case NULLPTR_TYPE:
1108 return true;
1110 case LANG_TYPE:
1111 /* fall through. */
1113 default:
1114 return false;
1118 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1119 the index of the descriptor in the tinfo_desc vector. */
1121 static tree
1122 get_pseudo_ti_init (tree type, unsigned tk_index)
1124 tinfo_s *ti = get_tinfo_desc (tk_index);
1126 gcc_assert (at_eof);
1127 switch (tk_index)
1129 case TK_POINTER_MEMBER_TYPE:
1130 return ptm_initializer (ti, type);
1132 case TK_POINTER_TYPE:
1133 return ptr_initializer (ti, type);
1135 case TK_BUILTIN_TYPE:
1136 case TK_ENUMERAL_TYPE:
1137 case TK_FUNCTION_TYPE:
1138 case TK_ARRAY_TYPE:
1139 return generic_initializer (ti, type);
1141 case TK_CLASS_TYPE:
1142 return class_initializer (ti, type, 0);
1144 case TK_SI_CLASS_TYPE:
1146 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1147 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1149 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1150 ti = &(*tinfo_descs)[tk_index];
1151 return class_initializer (ti, type, 1, tinfo);
1154 default:
1156 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1157 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1158 tree binfo = TYPE_BINFO (type);
1159 unsigned nbases = BINFO_N_BASE_BINFOS (binfo);
1160 vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1161 tree offset_type = LONGPTR_T;
1162 vec<constructor_elt, va_gc> *init_vec = NULL;
1164 gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases);
1166 vec_safe_grow (init_vec, nbases);
1167 /* Generate the base information initializer. */
1168 for (unsigned ix = nbases; ix--;)
1170 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1171 int flags = 0;
1172 tree tinfo;
1173 tree offset;
1174 vec<constructor_elt, va_gc> *v;
1176 if ((*base_accesses)[ix] == access_public_node)
1177 flags |= 2;
1178 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1179 if (BINFO_VIRTUAL_P (base_binfo))
1181 /* We store the vtable offset at which the virtual
1182 base offset can be found. */
1183 offset = BINFO_VPTR_FIELD (base_binfo);
1184 flags |= 1;
1186 else
1187 offset = BINFO_OFFSET (base_binfo);
1189 /* Combine offset and flags into one field. */
1190 offset = fold_convert (offset_type, offset);
1191 offset = fold_build2_loc (input_location,
1192 LSHIFT_EXPR, offset_type, offset,
1193 build_int_cst (offset_type, 8));
1194 offset = fold_build2_loc (input_location,
1195 BIT_IOR_EXPR, offset_type, offset,
1196 build_int_cst (offset_type, flags));
1197 vec_alloc (v, 2);
1198 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
1199 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
1200 tree base_init = build_constructor (init_list_type_node, v);
1201 constructor_elt *e = &(*init_vec)[ix];
1202 e->index = NULL_TREE;
1203 e->value = base_init;
1205 tree base_inits = build_constructor (init_list_type_node, init_vec);
1207 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1208 ti = &(*tinfo_descs)[tk_index];
1209 return class_initializer (ti, type, 3,
1210 build_int_cst (NULL_TREE, hint),
1211 build_int_cst (NULL_TREE, nbases),
1212 base_inits);
1217 /* Return the index of a pseudo type info type node used to describe
1218 TYPE. TYPE must be a complete type (or cv void), except at the end
1219 of the translation unit. */
1221 static unsigned
1222 get_pseudo_ti_index (tree type)
1224 unsigned ix;
1226 switch (TREE_CODE (type))
1228 case OFFSET_TYPE:
1229 ix = TK_POINTER_MEMBER_TYPE;
1230 break;
1232 case POINTER_TYPE:
1233 ix = TK_POINTER_TYPE;
1234 break;
1236 case ENUMERAL_TYPE:
1237 ix = TK_ENUMERAL_TYPE;
1238 break;
1240 case FUNCTION_TYPE:
1241 ix = TK_FUNCTION_TYPE;
1242 break;
1244 case ARRAY_TYPE:
1245 ix = TK_ARRAY_TYPE;
1246 break;
1248 case UNION_TYPE:
1249 case RECORD_TYPE:
1250 if (TYPE_PTRMEMFUNC_P (type))
1251 ix = TK_POINTER_MEMBER_TYPE;
1252 else if (!COMPLETE_TYPE_P (type))
1254 if (!at_eof)
1255 cxx_incomplete_type_error (NULL_TREE, type);
1256 ix = TK_CLASS_TYPE;
1258 else if (!TYPE_BINFO (type)
1259 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1260 ix = TK_CLASS_TYPE;
1261 else
1263 tree binfo = TYPE_BINFO (type);
1264 vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1265 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1266 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1268 if (num_bases == 1
1269 && (*base_accesses)[0] == access_public_node
1270 && !BINFO_VIRTUAL_P (base_binfo)
1271 && integer_zerop (BINFO_OFFSET (base_binfo)))
1272 /* single non-virtual public. */
1273 ix = TK_SI_CLASS_TYPE;
1274 else
1275 ix = TK_VMI_CLASS_TYPES + num_bases - 1;
1277 break;
1279 default:
1280 ix = TK_BUILTIN_TYPE;
1281 break;
1283 return ix;
1286 /* Return pointer to tinfo descriptor. Possibly creating the tinfo
1287 descriptor in the first place. */
1289 static tinfo_s *
1290 get_tinfo_desc (unsigned ix)
1292 unsigned len = tinfo_descs->length ();
1294 if (len <= ix)
1296 /* too short, extend. */
1297 len = ix + 1 - len;
1298 vec_safe_reserve (tinfo_descs, len);
1299 tinfo_s elt;
1300 elt.type = elt.vtable = elt.name = NULL_TREE;
1301 while (len--)
1302 tinfo_descs->quick_push (elt);
1305 tinfo_s *res = &(*tinfo_descs)[ix];
1307 if (res->type)
1308 return res;
1310 /* Ok, we have to create it. This layout must be consistent with
1311 that defined in the runtime support. We explicitly manage the
1312 vtable member, and name it for real type as used in the runtime.
1313 The RECORD type has a different name, to avoid collisions. We
1314 have to delay generating the VAR_DECL of the vtable until the end
1315 of the translation, when we'll have seen the library definition,
1316 if there was one. */
1318 /* Fields to add, chained in reverse order. */
1319 tree fields = NULL_TREE;
1321 if (ix >= TK_DERIVED_TYPES)
1323 /* First field is the pseudo type_info base class. */
1324 tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1325 get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
1327 DECL_CHAIN (fld_base) = fields;
1328 fields = fld_base;
1331 switch (ix)
1333 case TK_TYPE_INFO_TYPE:
1335 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1336 NULL_TREE, const_ptr_type_node);
1337 fields = fld_ptr;
1339 tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1340 NULL_TREE, const_string_type_node);
1341 DECL_CHAIN (fld_str) = fields;
1342 fields = fld_str;
1343 break;
1346 case TK_BASE_TYPE:
1348 /* Base class internal helper. Pointer to base type, offset to
1349 base, flags. */
1350 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1351 NULL_TREE, type_info_ptr_type);
1352 DECL_CHAIN (fld_ptr) = fields;
1353 fields = fld_ptr;
1355 tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1356 NULL_TREE, LONGPTR_T);
1357 DECL_CHAIN (fld_flag) = fields;
1358 fields = fld_flag;
1359 break;
1362 case TK_BUILTIN_TYPE:
1363 /* Fundamental type_info */
1364 break;
1366 case TK_ARRAY_TYPE:
1367 break;
1369 case TK_FUNCTION_TYPE:
1370 break;
1372 case TK_ENUMERAL_TYPE:
1373 break;
1375 case TK_POINTER_TYPE:
1376 case TK_POINTER_MEMBER_TYPE:
1378 /* Pointer type_info. Adds two fields, qualification mask and
1379 pointer to the pointed to type. This is really a
1380 descendant of __pbase_type_info. */
1381 tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1382 NULL_TREE, integer_type_node);
1383 DECL_CHAIN (fld_mask) = fields;
1384 fields = fld_mask;
1386 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1387 NULL_TREE, type_info_ptr_type);
1388 DECL_CHAIN (fld_ptr) = fields;
1389 fields = fld_ptr;
1391 if (ix == TK_POINTER_MEMBER_TYPE)
1393 /* Add a pointer to the class too. */
1394 tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1395 NULL_TREE, type_info_ptr_type);
1396 DECL_CHAIN (fld_cls) = fields;
1397 fields = fld_cls;
1399 break;
1402 case TK_CLASS_TYPE:
1403 /* Class type_info. No additional fields. */
1404 break;
1406 case TK_SI_CLASS_TYPE:
1408 /* Single public non-virtual base class. Add pointer to base
1409 class. This is really a descendant of
1410 __class_type_info. */
1411 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1412 NULL_TREE, type_info_ptr_type);
1413 DECL_CHAIN (fld_ptr) = fields;
1414 fields = fld_ptr;
1415 break;
1418 default: /* Multiple inheritance. */
1420 unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1;
1422 tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1423 NULL_TREE, integer_type_node);
1424 DECL_CHAIN (fld_flg) = fields;
1425 fields = fld_flg;
1427 tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1428 NULL_TREE, integer_type_node);
1429 DECL_CHAIN (fld_cnt) = fields;
1430 fields = fld_cnt;
1432 /* Create the array of __base_class_type_info entries. */
1433 tree domain = build_index_type (size_int (num_bases - 1));
1434 tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type,
1435 domain);
1436 tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1437 NULL_TREE, array);
1438 DECL_CHAIN (fld_ary) = fields;
1439 fields = fld_ary;
1440 break;
1444 push_abi_namespace ();
1446 /* Generate the pseudo type name. */
1447 const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES
1448 ? ix : unsigned (TK_VMI_CLASS_TYPES)];
1449 size_t name_len = strlen (real_name);
1450 char *pseudo_name = (char *) alloca (name_len + 30);
1451 memcpy (pseudo_name, real_name, name_len);
1452 /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
1453 apply it to all. See get_peudo_tinfo_index where we make use of
1454 this. */
1455 sprintf (pseudo_name + name_len, "_pseudo_%d", ix);
1457 /* Create the pseudo type. */
1458 tree pseudo_type = make_class_type (RECORD_TYPE);
1459 /* Pass the fields chained in reverse. */
1460 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1461 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1463 res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1464 res->name = get_identifier (real_name);
1466 /* Pretend this is public so determine_visibility doesn't give vtables
1467 internal linkage. */
1468 TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1;
1470 pop_abi_namespace ();
1471 return res;
1474 /* We lazily create the type info types. */
1476 static void
1477 create_tinfo_types (void)
1479 gcc_assert (!tinfo_descs);
1481 vec_alloc (tinfo_descs, TK_MAX + 20);
1484 /* Helper for emit_support_tinfos. Emits the type_info descriptor of
1485 a single type. */
1487 void
1488 emit_support_tinfo_1 (tree bltn)
1490 tree types[3];
1492 if (bltn == NULL_TREE)
1493 return;
1494 types[0] = bltn;
1495 types[1] = build_pointer_type (bltn);
1496 types[2] = build_pointer_type (cp_build_qualified_type (bltn,
1497 TYPE_QUAL_CONST));
1499 for (int i = 0; i < 3; ++i)
1501 tree tinfo = get_tinfo_decl (types[i]);
1502 TREE_USED (tinfo) = 1;
1503 mark_needed (tinfo);
1504 /* The C++ ABI requires that these objects be COMDAT. But,
1505 On systems without weak symbols, initialized COMDAT
1506 objects are emitted with internal linkage. (See
1507 comdat_linkage for details.) Since we want these objects
1508 to have external linkage so that copies do not have to be
1509 emitted in code outside the runtime library, we make them
1510 non-COMDAT here.
1512 It might also not be necessary to follow this detail of the
1513 ABI. */
1514 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1516 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1517 DECL_INTERFACE_KNOWN (tinfo) = 1;
1522 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1523 support. Generating them here guarantees consistency with the other
1524 structures. We use the following heuristic to determine when the runtime
1525 is being generated. If std::__fundamental_type_info is defined, and its
1526 destructor is defined, then the runtime is being built. */
1528 void
1529 emit_support_tinfos (void)
1531 /* Dummy static variable so we can put nullptr in the array; it will be
1532 set before we actually start to walk the array. */
1533 static tree *const fundamentals[] =
1535 &void_type_node,
1536 &boolean_type_node,
1537 &wchar_type_node, &char16_type_node, &char32_type_node,
1538 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1539 &short_integer_type_node, &short_unsigned_type_node,
1540 &integer_type_node, &unsigned_type_node,
1541 &long_integer_type_node, &long_unsigned_type_node,
1542 &long_long_integer_type_node, &long_long_unsigned_type_node,
1543 &float_type_node, &double_type_node, &long_double_type_node,
1544 &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
1545 &nullptr_type_node,
1548 int ix;
1550 /* Look for a defined class. */
1551 tree bltn_type = lookup_qualified_name
1552 (abi_node, get_identifier ("__fundamental_type_info"), true, false, false);
1553 if (TREE_CODE (bltn_type) != TYPE_DECL)
1554 return;
1556 bltn_type = TREE_TYPE (bltn_type);
1557 if (!COMPLETE_TYPE_P (bltn_type))
1558 return;
1559 tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
1560 if (!dtor || DECL_EXTERNAL (dtor))
1561 return;
1563 /* All these are really builtins. So set the location. */
1564 location_t saved_loc = input_location;
1565 input_location = BUILTINS_LOCATION;
1566 doing_runtime = 1;
1567 for (ix = 0; fundamentals[ix]; ix++)
1568 emit_support_tinfo_1 (*fundamentals[ix]);
1569 for (ix = 0; ix < NUM_INT_N_ENTS; ix ++)
1570 if (int_n_enabled_p[ix])
1572 emit_support_tinfo_1 (int_n_trees[ix].signed_type);
1573 emit_support_tinfo_1 (int_n_trees[ix].unsigned_type);
1575 for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
1576 emit_support_tinfo_1 (TREE_VALUE (t));
1577 input_location = saved_loc;
1580 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1581 tinfo decl. Determine whether it needs emitting, and if so
1582 generate the initializer. */
1584 bool
1585 emit_tinfo_decl (tree decl)
1587 tree type = TREE_TYPE (DECL_NAME (decl));
1588 int in_library = typeinfo_in_lib_p (type);
1590 gcc_assert (DECL_TINFO_P (decl));
1592 if (in_library)
1594 if (doing_runtime)
1595 DECL_EXTERNAL (decl) = 0;
1596 else
1598 /* If we're not in the runtime, then DECL (which is already
1599 DECL_EXTERNAL) will not be defined here. */
1600 DECL_INTERFACE_KNOWN (decl) = 1;
1601 return false;
1604 else if (involves_incomplete_p (type))
1606 if (!decl_needed_p (decl))
1607 return false;
1608 /* If TYPE involves an incomplete class type, then the typeinfo
1609 object will be emitted with internal linkage. There is no
1610 way to know whether or not types are incomplete until the end
1611 of the compilation, so this determination must be deferred
1612 until this point. */
1613 TREE_PUBLIC (decl) = 0;
1614 DECL_EXTERNAL (decl) = 0;
1615 DECL_INTERFACE_KNOWN (decl) = 1;
1618 import_export_decl (decl);
1619 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1621 tree init;
1623 DECL_EXTERNAL (decl) = 0;
1624 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1625 DECL_INITIAL (decl) = init;
1626 mark_used (decl);
1627 cp_finish_decl (decl, init, false, NULL_TREE, 0);
1628 /* Avoid targets optionally bumping up the alignment to improve
1629 vector instruction accesses, tinfo are never accessed this way. */
1630 #ifdef DATA_ABI_ALIGNMENT
1631 SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (decl, TYPE_ALIGN (TREE_TYPE (decl))));
1632 DECL_USER_ALIGN (decl) = true;
1633 #endif
1634 return true;
1636 else
1637 return false;
1640 #include "gt-cp-rtti.h"