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