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