d6288622246d68e60351ca4272c7d1af5b485db5
[gcc.git] / gcc / cp / rtti.c
1 /* RunTime Type Identification
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Mostly written by Jason Merrill (jason@cygnus.com).
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
32 #include "gcc-rich-location.h"
33
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.
43
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.
53
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. */
62
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 */
66
67 tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
68 translation. */
69
70 tree name; /* IDENTIFIER_NODE for the ABI specified name of
71 the type_info derived type. */
72 };
73
74
75 enum tinfo_kind
76 {
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> */
89 TK_MAX
90 };
91
92 /* Names of the tinfo types. Must be same order as TK enumeration
93 above. */
94
95 static const char *const tinfo_names[TK_MAX] =
96 {
97 "__type_info",
98 "__base_class_type_info",
99 "__fundamental_type_info",
100 "__array_type_info",
101 "__function_type_info",
102 "__enum_type_info",
103 "__pointer_type_info",
104 "__pointer_to_member_type_info",
105 "__class_type_info",
106 "__si_class_type_info",
107 "__vmi_class_type_info"
108 };
109
110 /* Helper macro to get maximum scalar-width of pointer or of the 'long'-type.
111 This of interest for llp64 targets. */
112 #define LONGPTR_T \
113 integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \
114 ? itk_long : itk_long_long)]
115
116 /* A vector of all tinfo decls that haven't yet been emitted. */
117 vec<tree, va_gc> *unemitted_tinfo_decls;
118
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;
123
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);
143
144 static int doing_runtime = 0;
145 \f
146 static void
147 push_abi_namespace (void)
148 {
149 push_nested_namespace (abi_node);
150 push_visibility ("default", 2);
151 }
152
153 static void
154 pop_abi_namespace (void)
155 {
156 pop_visibility (2);
157 pop_nested_namespace (abi_node);
158 }
159
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. */
165
166 void
167 init_rtti_processing (void)
168 {
169 push_nested_namespace (std_node);
170 tree type_info_type = xref_tag (class_type, get_identifier ("type_info"));
171 pop_nested_namespace (std_node);
172 const_type_info_type_node
173 = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST);
174 type_info_ptr_type = build_pointer_type (const_type_info_type_node);
175
176 vec_alloc (unemitted_tinfo_decls, 124);
177
178 create_tinfo_types ();
179 }
180
181 /* Given the expression EXP of type `class *', return the head of the
182 object pointed to by EXP with type cv void*, if the class has any
183 virtual functions (TYPE_POLYMORPHIC_P), else just return the
184 expression. */
185
186 tree
187 build_headof (tree exp)
188 {
189 tree type = TREE_TYPE (exp);
190 tree offset;
191 tree index;
192
193 gcc_assert (TYPE_PTR_P (type));
194 type = TREE_TYPE (type);
195
196 if (!TYPE_POLYMORPHIC_P (type))
197 return exp;
198
199 /* We use this a couple of times below, protect it. */
200 exp = save_expr (exp);
201
202 /* The offset-to-top field is at index -2 from the vptr. */
203 index = build_int_cst (NULL_TREE,
204 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
205
206 offset = build_vtbl_ref (cp_build_fold_indirect_ref (exp),
207 index);
208
209 cp_build_qualified_type (ptr_type_node,
210 cp_type_quals (TREE_TYPE (exp)));
211 return fold_build_pointer_plus (exp, offset);
212 }
213
214 /* Get a bad_cast node for the program to throw...
215
216 See libstdc++/exception.cc for __throw_bad_cast */
217
218 static tree
219 throw_bad_cast (void)
220 {
221 static tree fn;
222 if (!fn)
223 {
224 tree name = get_identifier ("__cxa_bad_cast");
225 fn = get_global_binding (name);
226 if (!fn)
227 fn = push_throw_library_fn
228 (name, build_function_type_list (ptr_type_node, NULL_TREE));
229 }
230
231 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
232 }
233
234 /* Return an expression for "__cxa_bad_typeid()". The expression
235 returned is an lvalue of type "const std::type_info". */
236
237 static tree
238 throw_bad_typeid (void)
239 {
240 static tree fn;
241 if (!fn)
242 {
243 tree name = get_identifier ("__cxa_bad_typeid");
244 fn = get_global_binding (name);
245 if (!fn)
246 {
247 tree t = build_reference_type (const_type_info_type_node);
248 t = build_function_type_list (t, NULL_TREE);
249 fn = push_throw_library_fn (name, t);
250 }
251 }
252
253 return build_cxx_call (fn, 0, NULL, tf_warning_or_error);
254 }
255 \f
256 /* Return an lvalue expression whose type is "const std::type_info"
257 and whose value indicates the type of the expression EXP. If EXP
258 is a reference to a polymorphic class, return the dynamic type;
259 otherwise return the static type of the expression. */
260
261 static tree
262 get_tinfo_decl_dynamic (tree exp, tsubst_flags_t complain)
263 {
264 tree type;
265 tree t;
266
267 if (error_operand_p (exp))
268 return error_mark_node;
269
270 exp = resolve_nondeduced_context (exp, complain);
271
272 /* Peel back references, so they match. */
273 type = non_reference (unlowered_expr_type (exp));
274
275 /* Peel off cv qualifiers. */
276 type = cv_unqualified (type);
277
278 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
279 if (CLASS_TYPE_P (type) || type == unknown_type_node
280 || type == init_list_type_node)
281 type = complete_type_or_maybe_complain (type, exp, complain);
282
283 if (!type)
284 return error_mark_node;
285
286 /* If exp is a reference to polymorphic type, get the real type_info. */
287 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
288 {
289 /* build reference to type_info from vtable. */
290 tree index;
291
292 /* The RTTI information is at index -1. */
293 index = build_int_cst (NULL_TREE,
294 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
295 t = build_vtbl_ref (exp, index);
296 t = convert (type_info_ptr_type, t);
297 }
298 else
299 /* Otherwise return the type_info for the static type of the expr. */
300 t = get_tinfo_ptr (type);
301
302 return cp_build_fold_indirect_ref (t);
303 }
304
305 static bool
306 typeid_ok_p (void)
307 {
308 if (! flag_rtti)
309 {
310 error ("cannot use %<typeid%> with %<-fno-rtti%>");
311 return false;
312 }
313
314 if (!COMPLETE_TYPE_P (const_type_info_type_node))
315 {
316 gcc_rich_location richloc (input_location);
317 maybe_add_include_fixit (&richloc, "<typeinfo>", false);
318 error_at (&richloc,
319 "must %<#include <typeinfo>%> before using"
320 " %<typeid%>");
321
322 return false;
323 }
324
325 tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
326 tree real = TYPE_MAIN_VARIANT (const_type_info_type_node);
327
328 /* Make sure abi::__type_info_pseudo has the same alias set
329 as std::type_info. */
330 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo))
331 TYPE_ALIAS_SET (pseudo) = get_alias_set (real);
332 else
333 gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real));
334
335 return true;
336 }
337
338 /* Return an expression for "typeid(EXP)". The expression returned is
339 an lvalue of type "const std::type_info". */
340
341 tree
342 build_typeid (tree exp, tsubst_flags_t complain)
343 {
344 tree cond = NULL_TREE, initial_expr = exp;
345 int nonnull = 0;
346
347 if (exp == error_mark_node || !typeid_ok_p ())
348 return error_mark_node;
349
350 if (processing_template_decl)
351 return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
352
353 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
354 && ! resolves_to_fixed_type_p (exp, &nonnull)
355 && ! nonnull)
356 {
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_fold_indirect_ref (exp);
363 }
364
365 exp = get_tinfo_decl_dynamic (exp, complain);
366
367 if (exp == error_mark_node)
368 return error_mark_node;
369
370 if (cond)
371 {
372 tree bad = throw_bad_typeid ();
373
374 exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
375 }
376 else
377 mark_type_use (initial_expr);
378
379 return exp;
380 }
381
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)
386 {
387 const char *name;
388 int length;
389 tree name_string;
390
391 name = mangle_type_string (type);
392 length = strlen (name);
393
394 if (mark_private)
395 {
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);
401 }
402 else
403 name_string = build_string (length + 1, name);
404
405 return fix_string_type (name_string);
406 }
407
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. */
411
412 tree
413 get_tinfo_decl (tree type)
414 {
415 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
416 {
417 error ("cannot create type information for type %qT because "
418 "it involves types of variable size",
419 type);
420 return error_mark_node;
421 }
422
423 if (TREE_CODE (type) == METHOD_TYPE)
424 type = build_function_type (TREE_TYPE (type),
425 TREE_CHAIN (TYPE_ARG_TYPES (type)));
426
427 return get_tinfo_decl_direct (type, NULL, -1);
428 }
429
430 /* Get or create a tinfo VAR_DECL directly from the provided information.
431 The caller must have already checked it is valid to do so. */
432
433 tree
434 get_tinfo_decl_direct (tree type, tree name, int pseudo_ix)
435 {
436 /* For a class type, the variable is cached in the type node
437 itself. */
438 tree d = NULL_TREE;
439
440 gcc_checking_assert (TREE_CODE (type) != METHOD_TYPE);
441
442 if (pseudo_ix < 0)
443 type = complete_type (type);
444
445 if (CLASS_TYPE_P (type))
446 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
447
448 if (!name)
449 name = mangle_typeinfo_for_type (type);
450
451 if (!CLASS_TYPE_P (type))
452 d = get_global_binding (name);
453
454 if (!d)
455 {
456 /* Create it. */
457 if (pseudo_ix < 0)
458 pseudo_ix = get_pseudo_ti_index (type);
459
460 const tinfo_s *ti = get_tinfo_desc (pseudo_ix);
461
462 d = build_lang_decl (VAR_DECL, name, ti->type);
463 SET_DECL_ASSEMBLER_NAME (d, name);
464 /* Remember the type it is for. */
465 TREE_TYPE (name) = type;
466 DECL_TINFO_P (d) = 1;
467 DECL_ARTIFICIAL (d) = 1;
468 DECL_IGNORED_P (d) = 1;
469 TREE_READONLY (d) = 1;
470 TREE_STATIC (d) = 1;
471
472 /* Mark the variable as undefined -- but remember that we can
473 define it later if we need to do so. */
474 DECL_EXTERNAL (d) = 1;
475 DECL_NOT_REALLY_EXTERN (d) = 1;
476 set_linkage_according_to_type (type, d);
477
478 d = pushdecl_top_level_and_finish (d, NULL_TREE);
479 if (CLASS_TYPE_P (type))
480 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
481
482 /* Add decl to the global array of tinfo decls. */
483 vec_safe_push (unemitted_tinfo_decls, d);
484 }
485
486 return d;
487 }
488
489 /* Return a pointer to a type_info object describing TYPE, suitably
490 cast to the language defined type. */
491
492 static tree
493 get_tinfo_ptr (tree type)
494 {
495 tree decl = get_tinfo_decl (type);
496
497 mark_used (decl);
498 return build_nop (type_info_ptr_type,
499 build_address (decl));
500 }
501
502 /* Return the type_info object for TYPE. */
503
504 tree
505 get_typeid (tree type, tsubst_flags_t complain)
506 {
507 if (type == error_mark_node || !typeid_ok_p ())
508 return error_mark_node;
509
510 if (processing_template_decl)
511 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
512
513 /* If the type of the type-id is a reference type, the result of the
514 typeid expression refers to a type_info object representing the
515 referenced type. */
516 type = non_reference (type);
517
518 /* This is not one of the uses of a qualified function type in 8.3.5. */
519 if (TREE_CODE (type) == FUNCTION_TYPE
520 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
521 || type_memfn_rqual (type) != REF_QUAL_NONE))
522 {
523 if (complain & tf_error)
524 error ("%<typeid%> of qualified function type %qT", type);
525 return error_mark_node;
526 }
527
528 /* The top-level cv-qualifiers of the lvalue expression or the type-id
529 that is the operand of typeid are always ignored. */
530 type = cv_unqualified (type);
531
532 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
533 if (CLASS_TYPE_P (type) || type == unknown_type_node
534 || type == init_list_type_node)
535 type = complete_type_or_maybe_complain (type, NULL_TREE, complain);
536
537 if (!type)
538 return error_mark_node;
539
540 return cp_build_fold_indirect_ref (get_tinfo_ptr (type));
541 }
542
543 /* Check whether TEST is null before returning RESULT. If TEST is used in
544 RESULT, it must have previously had a save_expr applied to it. */
545
546 static tree
547 ifnonnull (tree test, tree result, tsubst_flags_t complain)
548 {
549 tree cond = build2 (NE_EXPR, boolean_type_node, test,
550 cp_convert (TREE_TYPE (test), nullptr_node, complain));
551 /* This is a compiler generated comparison, don't emit
552 e.g. -Wnonnull-compare warning for it. */
553 TREE_NO_WARNING (cond) = 1;
554 return build3 (COND_EXPR, TREE_TYPE (result), cond, result,
555 cp_convert (TREE_TYPE (result), nullptr_node, complain));
556 }
557
558 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
559 paper. */
560
561 static tree
562 build_dynamic_cast_1 (location_t loc, tree type, tree expr,
563 tsubst_flags_t complain)
564 {
565 enum tree_code tc = TREE_CODE (type);
566 tree exprtype;
567 tree dcast_fn;
568 tree old_expr = expr;
569 const char *errstr = NULL;
570
571 /* Save casted types in the function's used types hash table. */
572 used_types_insert (type);
573
574 /* T shall be a pointer or reference to a complete class type, or
575 `pointer to cv void''. */
576 switch (tc)
577 {
578 case POINTER_TYPE:
579 if (VOID_TYPE_P (TREE_TYPE (type)))
580 break;
581 /* Fall through. */
582 case REFERENCE_TYPE:
583 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
584 {
585 errstr = _("target is not pointer or reference to class");
586 goto fail;
587 }
588 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
589 {
590 errstr = _("target is not pointer or reference to complete type");
591 goto fail;
592 }
593 break;
594
595 default:
596 errstr = _("target is not pointer or reference");
597 goto fail;
598 }
599
600 if (tc == POINTER_TYPE)
601 {
602 expr = decay_conversion (expr, complain);
603 exprtype = TREE_TYPE (expr);
604
605 /* If T is a pointer type, v shall be an rvalue of a pointer to
606 complete class type, and the result is an rvalue of type T. */
607
608 expr = mark_rvalue_use (expr);
609
610 if (!TYPE_PTR_P (exprtype))
611 {
612 errstr = _("source is not a pointer");
613 goto fail;
614 }
615 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
616 {
617 errstr = _("source is not a pointer to class");
618 goto fail;
619 }
620 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
621 {
622 errstr = _("source is a pointer to incomplete type");
623 goto fail;
624 }
625 }
626 else
627 {
628 expr = mark_lvalue_use (expr);
629 exprtype = TREE_TYPE (expr);
630
631 /* T is a reference type, v shall be an lvalue of a complete class
632 type, and the result is an lvalue of the type referred to by T. */
633 if (! MAYBE_CLASS_TYPE_P (exprtype))
634 {
635 errstr = _("source is not of class type");
636 goto fail;
637 }
638 if (!COMPLETE_TYPE_P (complete_type (exprtype)))
639 {
640 errstr = _("source is of incomplete class type");
641 goto fail;
642 }
643
644 exprtype = cp_build_reference_type (exprtype, !lvalue_p (expr));
645 }
646
647 /* The dynamic_cast operator shall not cast away constness. */
648 if (!at_least_as_qualified_p (TREE_TYPE (type),
649 TREE_TYPE (exprtype)))
650 {
651 errstr = _("conversion casts away constness");
652 goto fail;
653 }
654
655 /* If *type is an unambiguous accessible base class of *exprtype,
656 convert statically. */
657 {
658 tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
659 ba_check, NULL, complain);
660 if (binfo)
661 return build_static_cast (loc, type, expr, complain);
662 }
663
664 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
665 if (tc == REFERENCE_TYPE)
666 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
667 LOOKUP_NORMAL, NULL_TREE, complain);
668
669 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
670 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
671 {
672 tree expr1;
673 /* if TYPE is `void *', return pointer to complete object. */
674 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
675 {
676 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
677 if (TREE_CODE (expr) == ADDR_EXPR
678 && VAR_P (TREE_OPERAND (expr, 0))
679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
680 return build1 (NOP_EXPR, type, expr);
681
682 /* Since expr is used twice below, save it. */
683 expr = save_expr (expr);
684
685 expr1 = build_headof (expr);
686 if (TREE_TYPE (expr1) != type)
687 expr1 = build1 (NOP_EXPR, type, expr1);
688 return ifnonnull (expr, expr1, complain);
689 }
690 else
691 {
692 tree retval;
693 tree result, td2, td3;
694 tree elems[4];
695 tree static_type, target_type, boff;
696
697 /* If we got here, we can't convert statically. Therefore,
698 dynamic_cast<D&>(b) (b an object) cannot succeed. */
699 if (tc == REFERENCE_TYPE)
700 {
701 if (VAR_P (old_expr)
702 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
703 {
704 tree expr = throw_bad_cast ();
705 if (complain & tf_warning)
706 warning_at (loc, 0,
707 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
708 type, old_expr);
709 /* Bash it to the expected type. */
710 TREE_TYPE (expr) = type;
711 return expr;
712 }
713 }
714 /* Ditto for dynamic_cast<D*>(&b). */
715 else if (TREE_CODE (expr) == ADDR_EXPR)
716 {
717 tree op = TREE_OPERAND (expr, 0);
718 if (VAR_P (op)
719 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
720 {
721 if (complain & tf_warning)
722 warning_at (loc, 0,
723 "%<dynamic_cast<%#T>(%#D)%> can never succeed",
724 type, op);
725 retval = build_int_cst (type, 0);
726 return retval;
727 }
728 }
729
730 /* Use of dynamic_cast when -fno-rtti is prohibited. */
731 if (!flag_rtti)
732 {
733 if (complain & tf_error)
734 error_at (loc,
735 "%<dynamic_cast%> not permitted with %<-fno-rtti%>");
736 return error_mark_node;
737 }
738
739 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
740 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
741 td2 = get_tinfo_decl (target_type);
742 if (!mark_used (td2, complain) && !(complain & tf_error))
743 return error_mark_node;
744 td2 = cp_build_addr_expr (td2, complain);
745 td3 = get_tinfo_decl (static_type);
746 if (!mark_used (td3, complain) && !(complain & tf_error))
747 return error_mark_node;
748 td3 = cp_build_addr_expr (td3, complain);
749
750 /* Determine how T and V are related. */
751 boff = dcast_base_hint (static_type, target_type);
752
753 /* Since expr is used twice below, save it. */
754 expr = save_expr (expr);
755
756 expr1 = expr;
757 if (tc == REFERENCE_TYPE)
758 expr1 = cp_build_addr_expr (expr1, complain);
759
760 elems[0] = expr1;
761 elems[1] = td3;
762 elems[2] = td2;
763 elems[3] = boff;
764
765 dcast_fn = dynamic_cast_node;
766 if (!dcast_fn)
767 {
768 push_abi_namespace ();
769 tree tinfo_ptr = xref_tag (class_type,
770 get_identifier ("__class_type_info"));
771 tinfo_ptr = cp_build_qualified_type (tinfo_ptr, TYPE_QUAL_CONST);
772 tinfo_ptr = build_pointer_type (tinfo_ptr);
773
774 const char *fn_name = "__dynamic_cast";
775 /* void *() (void const *, __class_type_info const *,
776 __class_type_info const *, ptrdiff_t) */
777 tree fn_type = (build_function_type_list
778 (ptr_type_node, const_ptr_type_node,
779 tinfo_ptr, tinfo_ptr, ptrdiff_type_node,
780 NULL_TREE));
781 dcast_fn = (build_library_fn_ptr
782 (fn_name, fn_type, ECF_LEAF | ECF_PURE | ECF_NOTHROW));
783 pop_abi_namespace ();
784 dynamic_cast_node = dcast_fn;
785 }
786 result = build_cxx_call (dcast_fn, 4, elems, complain);
787 SET_EXPR_LOCATION (result, loc);
788
789 if (tc == REFERENCE_TYPE)
790 {
791 tree bad = throw_bad_cast ();
792 tree neq;
793
794 result = save_expr (result);
795 neq = cp_truthvalue_conversion (result, complain);
796 return cp_convert (type,
797 build3 (COND_EXPR, TREE_TYPE (result),
798 neq, result, bad), complain);
799 }
800
801 /* Now back to the type we want from a void*. */
802 result = cp_convert (type, result, complain);
803 return ifnonnull (expr, result, complain);
804 }
805 }
806 else
807 errstr = _("source type is not polymorphic");
808
809 fail:
810 if (complain & tf_error)
811 error_at (loc, "cannot %<dynamic_cast%> %qE (of type %q#T) "
812 "to type %q#T (%s)",
813 old_expr, TREE_TYPE (old_expr), type, errstr);
814 return error_mark_node;
815 }
816
817 tree
818 build_dynamic_cast (location_t loc, tree type, tree expr,
819 tsubst_flags_t complain)
820 {
821 tree r;
822
823 if (type == error_mark_node || expr == error_mark_node)
824 return error_mark_node;
825
826 if (processing_template_decl)
827 {
828 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
829 TREE_SIDE_EFFECTS (expr) = 1;
830 r = convert_from_reference (expr);
831 protected_set_expr_location (r, loc);
832 return r;
833 }
834
835 r = convert_from_reference (build_dynamic_cast_1 (loc, type, expr,
836 complain));
837 if (r != error_mark_node)
838 maybe_warn_about_useless_cast (loc, type, expr, complain);
839 protected_set_expr_location (r, loc);
840 return r;
841 }
842
843 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
844
845 static int
846 qualifier_flags (tree type)
847 {
848 int flags = 0;
849 int quals = cp_type_quals (type);
850
851 if (quals & TYPE_QUAL_CONST)
852 flags |= 1;
853 if (quals & TYPE_QUAL_VOLATILE)
854 flags |= 2;
855 if (quals & TYPE_QUAL_RESTRICT)
856 flags |= 4;
857 return flags;
858 }
859
860 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
861 contains a pointer to member of an incomplete class. */
862
863 static bool
864 target_incomplete_p (tree type)
865 {
866 while (true)
867 if (TYPE_PTRDATAMEM_P (type))
868 {
869 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
870 return true;
871 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
872 }
873 else if (TYPE_PTR_P (type))
874 type = TREE_TYPE (type);
875 else
876 return !COMPLETE_OR_VOID_TYPE_P (type);
877 }
878
879 /* Returns true if TYPE involves an incomplete class type; in that
880 case, typeinfo variables for TYPE should be emitted with internal
881 linkage. */
882
883 static bool
884 involves_incomplete_p (tree type)
885 {
886 switch (TREE_CODE (type))
887 {
888 case POINTER_TYPE:
889 return target_incomplete_p (TREE_TYPE (type));
890
891 case OFFSET_TYPE:
892 ptrmem:
893 return
894 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
895 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
896
897 case RECORD_TYPE:
898 if (TYPE_PTRMEMFUNC_P (type))
899 goto ptrmem;
900 /* Fall through. */
901 case UNION_TYPE:
902 if (!COMPLETE_TYPE_P (type))
903 return true;
904 /* Fall through. */
905 default:
906 /* All other types do not involve incomplete class types. */
907 return false;
908 }
909 }
910
911 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
912 is the vtable pointer and NTBS name. The NTBS name is emitted as a
913 comdat const char array, so it becomes a unique key for the type. Generate
914 and emit that VAR_DECL here. (We can't always emit the type_info itself
915 as comdat, because of pointers to incomplete.) */
916
917 static tree
918 tinfo_base_init (tinfo_s *ti, tree target)
919 {
920 tree init;
921 tree name_decl;
922 tree vtable_ptr;
923 vec<constructor_elt, va_gc> *v;
924
925 {
926 tree name_name, name_string;
927
928 /* Generate the NTBS array variable. */
929 tree name_type = build_cplus_array_type
930 (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
931 NULL_TREE);
932
933 /* Determine the name of the variable -- and remember with which
934 type it is associated. */
935 name_name = mangle_typeinfo_string_for_type (target);
936 TREE_TYPE (name_name) = target;
937
938 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
939 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
940 DECL_ARTIFICIAL (name_decl) = 1;
941 DECL_IGNORED_P (name_decl) = 1;
942 TREE_READONLY (name_decl) = 1;
943 TREE_STATIC (name_decl) = 1;
944 DECL_EXTERNAL (name_decl) = 0;
945 DECL_TINFO_P (name_decl) = 1;
946 set_linkage_according_to_type (target, name_decl);
947 import_export_decl (name_decl);
948 name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
949 DECL_INITIAL (name_decl) = name_string;
950 mark_used (name_decl);
951 pushdecl_top_level_and_finish (name_decl, name_string);
952 }
953
954 vtable_ptr = ti->vtable;
955 if (!vtable_ptr)
956 {
957 push_abi_namespace ();
958 tree real_type = xref_tag (class_type, ti->name);
959 tree real_decl = TYPE_NAME (real_type);
960 DECL_SOURCE_LOCATION (real_decl) = BUILTINS_LOCATION;
961 pop_abi_namespace ();
962
963 if (!COMPLETE_TYPE_P (real_type))
964 {
965 /* We never saw a definition of this type, so we need to
966 tell the compiler that this is an exported class, as
967 indeed all of the __*_type_info classes are. */
968 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
969 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
970 }
971
972 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
973 vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
974
975 /* We need to point into the middle of the vtable. */
976 vtable_ptr = fold_build_pointer_plus
977 (vtable_ptr,
978 size_binop (MULT_EXPR,
979 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
980 TYPE_SIZE_UNIT (vtable_entry_type)));
981
982 ti->vtable = vtable_ptr;
983 }
984
985 vec_alloc (v, 2);
986 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
987 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
988 decay_conversion (name_decl, tf_warning_or_error));
989
990 init = build_constructor (init_list_type_node, v);
991 TREE_CONSTANT (init) = 1;
992 TREE_STATIC (init) = 1;
993
994 return init;
995 }
996
997 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
998 information about the particular type_info derivation, which adds no
999 additional fields to the type_info base. */
1000
1001 static tree
1002 generic_initializer (tinfo_s *ti, tree target)
1003 {
1004 tree init = tinfo_base_init (ti, target);
1005
1006 init = build_constructor_single (init_list_type_node, NULL_TREE, init);
1007 TREE_CONSTANT (init) = 1;
1008 TREE_STATIC (init) = 1;
1009 return init;
1010 }
1011
1012 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1013 TI provides information about the particular type_info derivation,
1014 which adds target type and qualifier flags members to the type_info base. */
1015
1016 static tree
1017 ptr_initializer (tinfo_s *ti, tree target)
1018 {
1019 tree init = tinfo_base_init (ti, target);
1020 tree to = TREE_TYPE (target);
1021 int flags = qualifier_flags (to);
1022 bool incomplete = target_incomplete_p (to);
1023 vec<constructor_elt, va_gc> *v;
1024 vec_alloc (v, 3);
1025
1026 if (incomplete)
1027 flags |= 8;
1028 if (tx_safe_fn_type_p (to))
1029 {
1030 flags |= 0x20;
1031 to = tx_unsafe_fn_variant (to);
1032 }
1033 if (flag_noexcept_type
1034 && FUNC_OR_METHOD_TYPE_P (to)
1035 && TYPE_NOTHROW_P (to))
1036 {
1037 flags |= 0x40;
1038 to = build_exception_variant (to, NULL_TREE);
1039 }
1040 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1041 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1042 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1043 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1044
1045 init = build_constructor (init_list_type_node, v);
1046 TREE_CONSTANT (init) = 1;
1047 TREE_STATIC (init) = 1;
1048 return init;
1049 }
1050
1051 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1052 TI provides information about the particular type_info derivation,
1053 which adds class, target type and qualifier flags members to the type_info
1054 base. */
1055
1056 static tree
1057 ptm_initializer (tinfo_s *ti, tree target)
1058 {
1059 tree init = tinfo_base_init (ti, target);
1060 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1061 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1062 int flags = qualifier_flags (to);
1063 bool incomplete = target_incomplete_p (to);
1064 vec<constructor_elt, va_gc> *v;
1065 vec_alloc (v, 4);
1066
1067 if (incomplete)
1068 flags |= 0x8;
1069 if (!COMPLETE_TYPE_P (klass))
1070 flags |= 0x10;
1071 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1072 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1073 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1074 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1075 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_tinfo_ptr (klass));
1076
1077 init = build_constructor (init_list_type_node, v);
1078 TREE_CONSTANT (init) = 1;
1079 TREE_STATIC (init) = 1;
1080 return init;
1081 }
1082
1083 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1084 TI provides information about the particular __class_type_info derivation,
1085 which adds hint flags and N extra initializers to the type_info base. */
1086
1087 static tree
1088 class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
1089 {
1090 tree init = tinfo_base_init (ti, target);
1091 va_list extra_inits;
1092 unsigned i;
1093 vec<constructor_elt, va_gc> *v;
1094 vec_alloc (v, n+1);
1095
1096 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1097 va_start (extra_inits, n);
1098 for (i = 0; i < n; i++)
1099 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
1100 va_end (extra_inits);
1101
1102 init = build_constructor (init_list_type_node, v);
1103 TREE_CONSTANT (init) = 1;
1104 TREE_STATIC (init) = 1;
1105 return init;
1106 }
1107
1108 /* Returns true if the typeinfo for type should be placed in
1109 the runtime library. */
1110
1111 static bool
1112 typeinfo_in_lib_p (tree type)
1113 {
1114 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1115 library for simple types T. */
1116 if (TYPE_PTR_P (type)
1117 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1118 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1119 type = TREE_TYPE (type);
1120
1121 switch (TREE_CODE (type))
1122 {
1123 case INTEGER_TYPE:
1124 case BOOLEAN_TYPE:
1125 case REAL_TYPE:
1126 case VOID_TYPE:
1127 case NULLPTR_TYPE:
1128 return true;
1129
1130 case LANG_TYPE:
1131 /* fall through. */
1132
1133 default:
1134 return false;
1135 }
1136 }
1137
1138 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1139 the index of the descriptor in the tinfo_desc vector. */
1140
1141 static tree
1142 get_pseudo_ti_init (tree type, unsigned tk_index)
1143 {
1144 tinfo_s *ti = get_tinfo_desc (tk_index);
1145
1146 gcc_assert (at_eof);
1147 switch (tk_index)
1148 {
1149 case TK_POINTER_MEMBER_TYPE:
1150 return ptm_initializer (ti, type);
1151
1152 case TK_POINTER_TYPE:
1153 return ptr_initializer (ti, type);
1154
1155 case TK_BUILTIN_TYPE:
1156 case TK_ENUMERAL_TYPE:
1157 case TK_FUNCTION_TYPE:
1158 case TK_ARRAY_TYPE:
1159 return generic_initializer (ti, type);
1160
1161 case TK_CLASS_TYPE:
1162 return class_initializer (ti, type, 0);
1163
1164 case TK_SI_CLASS_TYPE:
1165 {
1166 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1167 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1168
1169 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1170 ti = &(*tinfo_descs)[tk_index];
1171 return class_initializer (ti, type, 1, tinfo);
1172 }
1173
1174 default:
1175 {
1176 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1177 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1178 tree binfo = TYPE_BINFO (type);
1179 unsigned nbases = BINFO_N_BASE_BINFOS (binfo);
1180 vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1181 tree offset_type = LONGPTR_T;
1182 vec<constructor_elt, va_gc> *init_vec = NULL;
1183
1184 gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases);
1185
1186 vec_safe_grow (init_vec, nbases, true);
1187 /* Generate the base information initializer. */
1188 for (unsigned ix = nbases; ix--;)
1189 {
1190 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1191 int flags = 0;
1192 tree tinfo;
1193 tree offset;
1194 vec<constructor_elt, va_gc> *v;
1195
1196 if ((*base_accesses)[ix] == access_public_node)
1197 flags |= 2;
1198 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1199 if (BINFO_VIRTUAL_P (base_binfo))
1200 {
1201 /* We store the vtable offset at which the virtual
1202 base offset can be found. */
1203 offset = BINFO_VPTR_FIELD (base_binfo);
1204 flags |= 1;
1205 }
1206 else
1207 offset = BINFO_OFFSET (base_binfo);
1208
1209 /* Combine offset and flags into one field. */
1210 offset = fold_convert (offset_type, offset);
1211 offset = fold_build2_loc (input_location,
1212 LSHIFT_EXPR, offset_type, offset,
1213 build_int_cst (offset_type, 8));
1214 offset = fold_build2_loc (input_location,
1215 BIT_IOR_EXPR, offset_type, offset,
1216 build_int_cst (offset_type, flags));
1217 vec_alloc (v, 2);
1218 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
1219 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
1220 tree base_init = build_constructor (init_list_type_node, v);
1221 constructor_elt *e = &(*init_vec)[ix];
1222 e->index = NULL_TREE;
1223 e->value = base_init;
1224 }
1225 tree base_inits = build_constructor (init_list_type_node, init_vec);
1226
1227 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1228 ti = &(*tinfo_descs)[tk_index];
1229 return class_initializer (ti, type, 3,
1230 build_int_cst (NULL_TREE, hint),
1231 build_int_cst (NULL_TREE, nbases),
1232 base_inits);
1233 }
1234 }
1235 }
1236
1237 /* Return the index of a pseudo type info type node used to describe
1238 TYPE. TYPE must be a complete type (or cv void), except at the end
1239 of the translation unit. */
1240
1241 static unsigned
1242 get_pseudo_ti_index (tree type)
1243 {
1244 unsigned ix;
1245
1246 switch (TREE_CODE (type))
1247 {
1248 case OFFSET_TYPE:
1249 ix = TK_POINTER_MEMBER_TYPE;
1250 break;
1251
1252 case POINTER_TYPE:
1253 ix = TK_POINTER_TYPE;
1254 break;
1255
1256 case ENUMERAL_TYPE:
1257 ix = TK_ENUMERAL_TYPE;
1258 break;
1259
1260 case FUNCTION_TYPE:
1261 ix = TK_FUNCTION_TYPE;
1262 break;
1263
1264 case ARRAY_TYPE:
1265 ix = TK_ARRAY_TYPE;
1266 break;
1267
1268 case UNION_TYPE:
1269 case RECORD_TYPE:
1270 if (TYPE_PTRMEMFUNC_P (type))
1271 ix = TK_POINTER_MEMBER_TYPE;
1272 else if (!COMPLETE_TYPE_P (type))
1273 {
1274 if (!at_eof)
1275 cxx_incomplete_type_error (NULL_TREE, type);
1276 ix = TK_CLASS_TYPE;
1277 }
1278 else if (!TYPE_BINFO (type)
1279 || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1280 ix = TK_CLASS_TYPE;
1281 else
1282 {
1283 tree binfo = TYPE_BINFO (type);
1284 vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo);
1285 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1286 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1287
1288 if (num_bases == 1
1289 && (*base_accesses)[0] == access_public_node
1290 && !BINFO_VIRTUAL_P (base_binfo)
1291 && integer_zerop (BINFO_OFFSET (base_binfo)))
1292 /* single non-virtual public. */
1293 ix = TK_SI_CLASS_TYPE;
1294 else
1295 ix = TK_VMI_CLASS_TYPES + num_bases - 1;
1296 }
1297 break;
1298
1299 default:
1300 ix = TK_BUILTIN_TYPE;
1301 break;
1302 }
1303 return ix;
1304 }
1305
1306 /* Return pointer to tinfo descriptor. Possibly creating the tinfo
1307 descriptor in the first place. */
1308
1309 static tinfo_s *
1310 get_tinfo_desc (unsigned ix)
1311 {
1312 unsigned len = tinfo_descs->length ();
1313
1314 if (len <= ix)
1315 {
1316 /* too short, extend. */
1317 len = ix + 1 - len;
1318 vec_safe_reserve (tinfo_descs, len);
1319 tinfo_s elt;
1320 elt.type = elt.vtable = elt.name = NULL_TREE;
1321 while (len--)
1322 tinfo_descs->quick_push (elt);
1323 }
1324
1325 tinfo_s *res = &(*tinfo_descs)[ix];
1326
1327 if (res->type)
1328 return res;
1329
1330 /* Ok, we have to create it. This layout must be consistent with
1331 that defined in the runtime support. We explicitly manage the
1332 vtable member, and name it for real type as used in the runtime.
1333 The RECORD type has a different name, to avoid collisions. We
1334 have to delay generating the VAR_DECL of the vtable until the end
1335 of the translation, when we'll have seen the library definition,
1336 if there was one. */
1337
1338 /* Fields to add, chained in reverse order. */
1339 tree fields = NULL_TREE;
1340
1341 if (ix >= TK_DERIVED_TYPES)
1342 {
1343 /* First field is the pseudo type_info base class. */
1344 tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1345 get_tinfo_desc (TK_TYPE_INFO_TYPE)->type);
1346
1347 DECL_CHAIN (fld_base) = fields;
1348 fields = fld_base;
1349 }
1350
1351 switch (ix)
1352 {
1353 case TK_TYPE_INFO_TYPE:
1354 {
1355 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1356 NULL_TREE, const_ptr_type_node);
1357 fields = fld_ptr;
1358
1359 tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1360 NULL_TREE, const_string_type_node);
1361 DECL_CHAIN (fld_str) = fields;
1362 fields = fld_str;
1363 break;
1364 }
1365
1366 case TK_BASE_TYPE:
1367 {
1368 /* Base class internal helper. Pointer to base type, offset to
1369 base, flags. */
1370 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1371 NULL_TREE, type_info_ptr_type);
1372 DECL_CHAIN (fld_ptr) = fields;
1373 fields = fld_ptr;
1374
1375 tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1376 NULL_TREE, LONGPTR_T);
1377 DECL_CHAIN (fld_flag) = fields;
1378 fields = fld_flag;
1379 break;
1380 }
1381
1382 case TK_BUILTIN_TYPE:
1383 /* Fundamental type_info */
1384 break;
1385
1386 case TK_ARRAY_TYPE:
1387 break;
1388
1389 case TK_FUNCTION_TYPE:
1390 break;
1391
1392 case TK_ENUMERAL_TYPE:
1393 break;
1394
1395 case TK_POINTER_TYPE:
1396 case TK_POINTER_MEMBER_TYPE:
1397 {
1398 /* Pointer type_info. Adds two fields, qualification mask and
1399 pointer to the pointed to type. This is really a
1400 descendant of __pbase_type_info. */
1401 tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1402 NULL_TREE, integer_type_node);
1403 DECL_CHAIN (fld_mask) = fields;
1404 fields = fld_mask;
1405
1406 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1407 NULL_TREE, type_info_ptr_type);
1408 DECL_CHAIN (fld_ptr) = fields;
1409 fields = fld_ptr;
1410
1411 if (ix == TK_POINTER_MEMBER_TYPE)
1412 {
1413 /* Add a pointer to the class too. */
1414 tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1415 NULL_TREE, type_info_ptr_type);
1416 DECL_CHAIN (fld_cls) = fields;
1417 fields = fld_cls;
1418 }
1419 break;
1420 }
1421
1422 case TK_CLASS_TYPE:
1423 /* Class type_info. No additional fields. */
1424 break;
1425
1426 case TK_SI_CLASS_TYPE:
1427 {
1428 /* Single public non-virtual base class. Add pointer to base
1429 class. This is really a descendant of
1430 __class_type_info. */
1431 tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1432 NULL_TREE, type_info_ptr_type);
1433 DECL_CHAIN (fld_ptr) = fields;
1434 fields = fld_ptr;
1435 break;
1436 }
1437
1438 default: /* Multiple inheritance. */
1439 {
1440 unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1;
1441
1442 tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1443 NULL_TREE, integer_type_node);
1444 DECL_CHAIN (fld_flg) = fields;
1445 fields = fld_flg;
1446
1447 tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1448 NULL_TREE, integer_type_node);
1449 DECL_CHAIN (fld_cnt) = fields;
1450 fields = fld_cnt;
1451
1452 /* Create the array of __base_class_type_info entries. */
1453 tree domain = build_index_type (size_int (num_bases - 1));
1454 tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type,
1455 domain);
1456 tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1457 NULL_TREE, array);
1458 DECL_CHAIN (fld_ary) = fields;
1459 fields = fld_ary;
1460 break;
1461 }
1462 }
1463
1464 /* Generate the pseudo type name. */
1465 const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES
1466 ? ix : unsigned (TK_VMI_CLASS_TYPES)];
1467 size_t name_len = strlen (real_name);
1468 char *pseudo_name = (char *) alloca (name_len + 30);
1469 memcpy (pseudo_name, real_name, name_len);
1470 /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well
1471 apply it to all. See get_peudo_tinfo_index where we make use of
1472 this. */
1473 sprintf (pseudo_name + name_len, "_pseudo_%d", ix);
1474
1475 /* Create the pseudo type. */
1476 tree pseudo_type = make_class_type (RECORD_TYPE);
1477 /* Pass the fields chained in reverse. */
1478 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1479 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1480 DECL_CONTEXT (TYPE_NAME (pseudo_type)) = FROB_CONTEXT (global_namespace);
1481 DECL_TINFO_P (TYPE_NAME (pseudo_type)) = true;
1482 xref_basetypes (pseudo_type, /*bases=*/NULL_TREE);
1483
1484 res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1485 res->name = get_identifier (real_name);
1486
1487 /* Pretend this is public so determine_visibility doesn't give vtables
1488 internal linkage. */
1489 TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1;
1490
1491 return res;
1492 }
1493
1494 /* Return an identifying index for the pseudo type_info TYPE.
1495 We wrote the index at the end of the name, so just scan it from
1496 there. This isn't critical, as it's only on the first use of this
1497 type during module stream out. */
1498
1499 unsigned
1500 get_pseudo_tinfo_index (tree type)
1501 {
1502 tree name = DECL_NAME (TYPE_NAME (type));
1503 unsigned ix = 0, scale = 1;
1504 size_t len = IDENTIFIER_LENGTH (name);
1505 const char *ptr = IDENTIFIER_POINTER (name) + len;
1506
1507 for (; *--ptr != '_'; scale *= 10)
1508 {
1509 len--;
1510 gcc_checking_assert (len && ISDIGIT (*ptr));
1511 ix += (*ptr - '0') * scale;
1512 }
1513
1514 gcc_assert (len != IDENTIFIER_LENGTH (name));
1515 return ix;
1516 }
1517
1518 tree
1519 get_pseudo_tinfo_type (unsigned ix)
1520 {
1521 return get_tinfo_desc (ix)->type;
1522 }
1523
1524 /* We lazily create the type info types. */
1525
1526 static void
1527 create_tinfo_types (void)
1528 {
1529 gcc_assert (!tinfo_descs);
1530
1531 vec_alloc (tinfo_descs, TK_MAX + 20);
1532 }
1533
1534 /* Helper for emit_support_tinfos. Emits the type_info descriptor of
1535 a single type. */
1536
1537 void
1538 emit_support_tinfo_1 (tree bltn)
1539 {
1540 tree types[3];
1541
1542 if (bltn == NULL_TREE)
1543 return;
1544 types[0] = bltn;
1545 types[1] = build_pointer_type (bltn);
1546 types[2] = build_pointer_type (cp_build_qualified_type (bltn,
1547 TYPE_QUAL_CONST));
1548
1549 for (int i = 0; i < 3; ++i)
1550 {
1551 tree tinfo = get_tinfo_decl (types[i]);
1552 TREE_USED (tinfo) = 1;
1553 mark_needed (tinfo);
1554 /* The C++ ABI requires that these objects be COMDAT. But,
1555 On systems without weak symbols, initialized COMDAT
1556 objects are emitted with internal linkage. (See
1557 comdat_linkage for details.) Since we want these objects
1558 to have external linkage so that copies do not have to be
1559 emitted in code outside the runtime library, we make them
1560 non-COMDAT here.
1561
1562 It might also not be necessary to follow this detail of the
1563 ABI. */
1564 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1565 {
1566 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1567 DECL_INTERFACE_KNOWN (tinfo) = 1;
1568 }
1569 }
1570 }
1571
1572 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1573 support. Generating them here guarantees consistency with the other
1574 structures. We use the following heuristic to determine when the runtime
1575 is being generated. If std::__fundamental_type_info is defined, and its
1576 destructor is defined, then the runtime is being built. */
1577
1578 void
1579 emit_support_tinfos (void)
1580 {
1581 /* Dummy static variable so we can put nullptr in the array; it will be
1582 set before we actually start to walk the array. */
1583 static tree *const fundamentals[] =
1584 {
1585 &void_type_node,
1586 &boolean_type_node,
1587 &wchar_type_node, &char8_type_node, &char16_type_node, &char32_type_node,
1588 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1589 &short_integer_type_node, &short_unsigned_type_node,
1590 &integer_type_node, &unsigned_type_node,
1591 &long_integer_type_node, &long_unsigned_type_node,
1592 &long_long_integer_type_node, &long_long_unsigned_type_node,
1593 &float_type_node, &double_type_node, &long_double_type_node,
1594 &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
1595 &nullptr_type_node,
1596 0
1597 };
1598 int ix;
1599
1600 /* Look for a defined class. */
1601 tree bltn_type = lookup_qualified_name
1602 (abi_node, "__fundamental_type_info", LOOK_want::TYPE, false);
1603 if (TREE_CODE (bltn_type) != TYPE_DECL)
1604 return;
1605
1606 bltn_type = TREE_TYPE (bltn_type);
1607 if (!COMPLETE_TYPE_P (bltn_type))
1608 return;
1609 tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
1610 if (!dtor || DECL_EXTERNAL (dtor))
1611 return;
1612
1613 /* All these are really builtins. So set the location. */
1614 location_t saved_loc = input_location;
1615 input_location = BUILTINS_LOCATION;
1616 doing_runtime = 1;
1617 for (ix = 0; fundamentals[ix]; ix++)
1618 emit_support_tinfo_1 (*fundamentals[ix]);
1619 for (ix = 0; ix < NUM_INT_N_ENTS; ix ++)
1620 if (int_n_enabled_p[ix])
1621 {
1622 emit_support_tinfo_1 (int_n_trees[ix].signed_type);
1623 emit_support_tinfo_1 (int_n_trees[ix].unsigned_type);
1624 }
1625 for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
1626 emit_support_tinfo_1 (TREE_VALUE (t));
1627 /* For compatibility, emit DFP typeinfos even when DFP isn't enabled,
1628 because we've emitted that in the past. */
1629 if (!targetm.decimal_float_supported_p ())
1630 {
1631 gcc_assert (dfloat32_type_node == NULL_TREE
1632 && dfloat64_type_node == NULL_TREE
1633 && dfloat128_type_node == NULL_TREE);
1634 fallback_dfloat32_type = make_node (REAL_TYPE);
1635 fallback_dfloat64_type = make_node (REAL_TYPE);
1636 fallback_dfloat128_type = make_node (REAL_TYPE);
1637 emit_support_tinfo_1 (fallback_dfloat32_type);
1638 emit_support_tinfo_1 (fallback_dfloat64_type);
1639 emit_support_tinfo_1 (fallback_dfloat128_type);
1640 }
1641 input_location = saved_loc;
1642 }
1643
1644 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1645 tinfo decl. Determine whether it needs emitting, and if so
1646 generate the initializer. */
1647
1648 bool
1649 emit_tinfo_decl (tree decl)
1650 {
1651 gcc_assert (DECL_TINFO_P (decl));
1652
1653 tree type = TREE_TYPE (DECL_NAME (decl));
1654 if (typeinfo_in_lib_p (type))
1655 {
1656 if (doing_runtime)
1657 DECL_EXTERNAL (decl) = 0;
1658 else
1659 {
1660 /* If we're not in the runtime, then DECL (which is already
1661 DECL_EXTERNAL) will not be defined here. */
1662 DECL_INTERFACE_KNOWN (decl) = 1;
1663 return false;
1664 }
1665 }
1666 else if (involves_incomplete_p (type))
1667 {
1668 if (!decl_needed_p (decl))
1669 return false;
1670 /* If TYPE involves an incomplete class type, then the typeinfo
1671 object will be emitted with internal linkage. There is no
1672 way to know whether or not types are incomplete until the end
1673 of the compilation, so this determination must be deferred
1674 until this point. */
1675 TREE_PUBLIC (decl) = 0;
1676 DECL_EXTERNAL (decl) = 0;
1677 DECL_INTERFACE_KNOWN (decl) = 1;
1678 }
1679
1680 import_export_decl (decl);
1681 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1682 {
1683 tree init;
1684
1685 DECL_EXTERNAL (decl) = 0;
1686 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1687 DECL_INITIAL (decl) = init;
1688 mark_used (decl);
1689 cp_finish_decl (decl, init, false, NULL_TREE, 0);
1690 /* Avoid targets optionally bumping up the alignment to improve
1691 vector instruction accesses, tinfo are never accessed this way. */
1692 #ifdef DATA_ABI_ALIGNMENT
1693 SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (decl, TYPE_ALIGN (TREE_TYPE (decl))));
1694 DECL_USER_ALIGN (decl) = true;
1695 #endif
1696 return true;
1697 }
1698 else
1699 return false;
1700 }
1701
1702 #include "gt-cp-rtti.h"