* semantics.c (finish_qualified_id_expr): Handle building up a
[official-gcc.git] / gcc / cp / rtti.c
blob51cb5ee9c62cd523b530e13d33e78329086d0bd8
1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Mostly written by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "intl.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "convert.h"
32 #include "target.h"
33 #include "c-family/c-pragma.h"
35 /* C++ returns type information to the user in struct type_info
36 objects. We also use type information to implement dynamic_cast and
37 exception handlers. Type information for a particular type is
38 indicated with an ABI defined structure derived from type_info.
39 This would all be very straight forward, but for the fact that the
40 runtime library provides the definitions of the type_info structure
41 and the ABI defined derived classes. We cannot build declarations
42 of them directly in the compiler, but we need to layout objects of
43 their type. Somewhere we have to lie.
45 We define layout compatible POD-structs with compiler-defined names
46 and generate the appropriate initializations for them (complete
47 with explicit mention of their vtable). When we have to provide a
48 type_info to the user we reinterpret_cast the internal compiler
49 type to type_info. A well formed program can only explicitly refer
50 to the type_infos of complete types (& cv void). However, we chain
51 pointer type_infos to the pointed-to-type, and that can be
52 incomplete. We only need the addresses of such incomplete
53 type_info objects for static initialization.
55 The type information VAR_DECL of a type is held on the
56 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
57 will be the internal type. It will usually have the correct
58 internal type reflecting the kind of type it represents (pointer,
59 array, function, class, inherited class, etc). When the type it
60 represents is incomplete, it will have the internal type
61 corresponding to type_info. That will only happen at the end of
62 translation, when we are emitting the type info objects. */
64 /* Auxiliary data we hold for each type_info derived object we need. */
65 typedef struct GTY (()) tinfo_s {
66 tree type; /* The RECORD_TYPE for this type_info object */
68 tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
69 translation. */
71 tree name; /* IDENTIFIER_NODE for the ABI specified name of
72 the type_info derived type. */
73 } tinfo_s;
75 DEF_VEC_O(tinfo_s);
76 DEF_VEC_ALLOC_O(tinfo_s,gc);
78 typedef enum tinfo_kind
80 TK_TYPE_INFO_TYPE, /* abi::__type_info_pseudo */
81 TK_BASE_TYPE, /* abi::__base_class_type_info */
82 TK_BUILTIN_TYPE, /* abi::__fundamental_type_info */
83 TK_ARRAY_TYPE, /* abi::__array_type_info */
84 TK_FUNCTION_TYPE, /* abi::__function_type_info */
85 TK_ENUMERAL_TYPE, /* abi::__enum_type_info */
86 TK_POINTER_TYPE, /* abi::__pointer_type_info */
87 TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
88 TK_CLASS_TYPE, /* abi::__class_type_info */
89 TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */
90 TK_FIXED /* end of fixed descriptors. */
91 /* ... abi::__vmi_type_info<I> */
92 } tinfo_kind;
94 /* A vector of all tinfo decls that haven't yet been emitted. */
95 VEC(tree,gc) *unemitted_tinfo_decls;
97 /* A vector of all type_info derived types we need. The first few are
98 fixed and created early. The remainder are for multiple inheritance
99 and are generated as needed. */
100 static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
102 static tree ifnonnull (tree, tree, tsubst_flags_t);
103 static tree tinfo_name (tree, bool);
104 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
105 static tree throw_bad_cast (void);
106 static tree throw_bad_typeid (void);
107 static tree get_tinfo_decl_dynamic (tree);
108 static tree get_tinfo_ptr (tree);
109 static bool typeid_ok_p (void);
110 static int qualifier_flags (tree);
111 static bool target_incomplete_p (tree);
112 static tree tinfo_base_init (tinfo_s *, tree);
113 static tree generic_initializer (tinfo_s *, tree);
114 static tree ptr_initializer (tinfo_s *, tree);
115 static tree ptm_initializer (tinfo_s *, tree);
116 static tree class_initializer (tinfo_s *, tree, unsigned, ...);
117 static void create_pseudo_type_info (int, const char *, ...);
118 static tree get_pseudo_ti_init (tree, unsigned);
119 static unsigned get_pseudo_ti_index (tree);
120 static void create_tinfo_types (void);
121 static bool typeinfo_in_lib_p (tree);
123 static int doing_runtime = 0;
125 static void
126 push_abi_namespace (void)
128 push_nested_namespace (abi_node);
129 push_visibility ("default", 2);
132 static void
133 pop_abi_namespace (void)
135 pop_visibility (2);
136 pop_nested_namespace (abi_node);
139 /* Declare language defined type_info type and a pointer to const
140 type_info. This is incomplete here, and will be completed when
141 the user #includes <typeinfo>. There are language defined
142 restrictions on what can be done until that is included. Create
143 the internal versions of the ABI types. */
145 void
146 init_rtti_processing (void)
148 tree type_info_type;
150 push_namespace (std_identifier);
151 type_info_type = xref_tag (class_type, get_identifier ("type_info"),
152 /*tag_scope=*/ts_current, false);
153 pop_namespace ();
154 const_type_info_type_node
155 = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST);
156 type_info_ptr_type = build_pointer_type (const_type_info_type_node);
158 unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
160 create_tinfo_types ();
163 /* Given the expression EXP of type `class *', return the head of the
164 object pointed to by EXP with type cv void*, if the class has any
165 virtual functions (TYPE_POLYMORPHIC_P), else just return the
166 expression. */
168 tree
169 build_headof (tree exp)
171 tree type = TREE_TYPE (exp);
172 tree offset;
173 tree index;
175 gcc_assert (TREE_CODE (type) == POINTER_TYPE);
176 type = TREE_TYPE (type);
178 if (!TYPE_POLYMORPHIC_P (type))
179 return exp;
181 /* We use this a couple of times below, protect it. */
182 exp = save_expr (exp);
184 /* The offset-to-top field is at index -2 from the vptr. */
185 index = build_int_cst (NULL_TREE,
186 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
188 offset = build_vtbl_ref (cp_build_indirect_ref (exp, RO_NULL,
189 tf_warning_or_error),
190 index);
192 type = cp_build_qualified_type (ptr_type_node,
193 cp_type_quals (TREE_TYPE (exp)));
194 return fold_build_pointer_plus (exp, offset);
197 /* Get a bad_cast node for the program to throw...
199 See libstdc++/exception.cc for __throw_bad_cast */
201 static tree
202 throw_bad_cast (void)
204 tree fn = get_identifier ("__cxa_bad_cast");
205 if (!get_global_value_if_present (fn, &fn))
206 fn = push_throw_library_fn (fn, build_function_type_list (ptr_type_node,
207 NULL_TREE));
209 return build_cxx_call (fn, 0, NULL);
212 /* Return an expression for "__cxa_bad_typeid()". The expression
213 returned is an lvalue of type "const std::type_info". */
215 static tree
216 throw_bad_typeid (void)
218 tree fn = get_identifier ("__cxa_bad_typeid");
219 if (!get_global_value_if_present (fn, &fn))
221 tree t;
223 t = build_reference_type (const_type_info_type_node);
224 t = build_function_type_list (t, NULL_TREE);
225 fn = push_throw_library_fn (fn, t);
228 return build_cxx_call (fn, 0, NULL);
231 /* Return an lvalue expression whose type is "const std::type_info"
232 and whose value indicates the type of the expression EXP. If EXP
233 is a reference to a polymorphic class, return the dynamic type;
234 otherwise return the static type of the expression. */
236 static tree
237 get_tinfo_decl_dynamic (tree exp)
239 tree type;
240 tree t;
242 if (error_operand_p (exp))
243 return error_mark_node;
245 exp = resolve_nondeduced_context (exp);
247 /* peel back references, so they match. */
248 type = non_reference (TREE_TYPE (exp));
250 /* Peel off cv qualifiers. */
251 type = TYPE_MAIN_VARIANT (type);
253 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
254 if (CLASS_TYPE_P (type) || type == unknown_type_node
255 || type == init_list_type_node)
256 type = complete_type_or_else (type, exp);
258 if (!type)
259 return error_mark_node;
261 /* If exp is a reference to polymorphic type, get the real type_info. */
262 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
264 /* build reference to type_info from vtable. */
265 tree index;
267 /* The RTTI information is at index -1. */
268 index = build_int_cst (NULL_TREE,
269 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
270 t = build_vtbl_ref (exp, index);
271 t = convert (type_info_ptr_type, t);
273 else
274 /* Otherwise return the type_info for the static type of the expr. */
275 t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
277 return cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
280 static bool
281 typeid_ok_p (void)
283 tree pseudo_type_info, type_info_type;
285 if (! flag_rtti)
287 error ("cannot use typeid with -fno-rtti");
288 return false;
291 if (!COMPLETE_TYPE_P (const_type_info_type_node))
293 error ("must #include <typeinfo> before using typeid");
294 return false;
297 pseudo_type_info
298 = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE).type;
299 type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
301 /* Make sure abi::__type_info_pseudo has the same alias set
302 as std::type_info. */
303 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
304 TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
305 else
306 gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
307 == get_alias_set (type_info_type));
309 return true;
312 /* Return an expression for "typeid(EXP)". The expression returned is
313 an lvalue of type "const std::type_info". */
315 tree
316 build_typeid (tree exp)
318 tree cond = NULL_TREE, initial_expr = exp;
319 int nonnull = 0;
321 if (exp == error_mark_node || !typeid_ok_p ())
322 return error_mark_node;
324 if (processing_template_decl)
325 return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
327 /* FIXME when integrating with c_fully_fold, mark
328 resolves_to_fixed_type_p case as a non-constant expression. */
329 if (TREE_CODE (exp) == INDIRECT_REF
330 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
331 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
332 && ! resolves_to_fixed_type_p (exp, &nonnull)
333 && ! nonnull)
335 /* So we need to look into the vtable of the type of exp.
336 This is an lvalue use of expr then. */
337 exp = mark_lvalue_use (exp);
338 exp = stabilize_reference (exp);
339 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0),
340 tf_warning_or_error);
343 exp = get_tinfo_decl_dynamic (exp);
345 if (exp == error_mark_node)
346 return error_mark_node;
348 if (cond)
350 tree bad = throw_bad_typeid ();
352 exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
354 else
355 mark_type_use (initial_expr);
357 return exp;
360 /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that
361 comparisons will be done by pointer rather than string comparison. */
362 static tree
363 tinfo_name (tree type, bool mark_private)
365 const char *name;
366 int length;
367 tree name_string;
369 name = mangle_type_string (type);
370 length = strlen (name);
372 if (mark_private)
374 /* Inject '*' at beginning of name to force pointer comparison. */
375 char* buf = (char*) XALLOCAVEC (char, length + 2);
376 buf[0] = '*';
377 memcpy (buf + 1, name, length + 1);
378 name_string = build_string (length + 2, buf);
380 else
381 name_string = build_string (length + 1, name);
383 return fix_string_type (name_string);
386 /* Return a VAR_DECL for the internal ABI defined type_info object for
387 TYPE. You must arrange that the decl is mark_used, if actually use
388 it --- decls in vtables are only used if the vtable is output. */
390 tree
391 get_tinfo_decl (tree type)
393 tree name;
394 tree d;
396 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
398 error ("cannot create type information for type %qT because "
399 "it involves types of variable size",
400 type);
401 return error_mark_node;
404 if (TREE_CODE (type) == METHOD_TYPE)
405 type = build_function_type (TREE_TYPE (type),
406 TREE_CHAIN (TYPE_ARG_TYPES (type)));
408 type = complete_type (type);
410 /* For a class type, the variable is cached in the type node
411 itself. */
412 if (CLASS_TYPE_P (type))
414 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
415 if (d)
416 return d;
419 name = mangle_typeinfo_for_type (type);
421 d = IDENTIFIER_GLOBAL_VALUE (name);
422 if (!d)
424 int ix = get_pseudo_ti_index (type);
425 tinfo_s *ti = &VEC_index (tinfo_s, tinfo_descs, ix);
427 d = build_lang_decl (VAR_DECL, name, ti->type);
428 SET_DECL_ASSEMBLER_NAME (d, name);
429 /* Remember the type it is for. */
430 TREE_TYPE (name) = type;
431 DECL_TINFO_P (d) = 1;
432 DECL_ARTIFICIAL (d) = 1;
433 DECL_IGNORED_P (d) = 1;
434 TREE_READONLY (d) = 1;
435 TREE_STATIC (d) = 1;
436 /* Mark the variable as undefined -- but remember that we can
437 define it later if we need to do so. */
438 DECL_EXTERNAL (d) = 1;
439 DECL_NOT_REALLY_EXTERN (d) = 1;
440 set_linkage_according_to_type (type, d);
442 d = pushdecl_top_level_and_finish (d, NULL_TREE);
443 if (CLASS_TYPE_P (type))
444 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
446 /* Add decl to the global array of tinfo decls. */
447 VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
450 return d;
453 /* Return a pointer to a type_info object describing TYPE, suitably
454 cast to the language defined type. */
456 static tree
457 get_tinfo_ptr (tree type)
459 tree decl = get_tinfo_decl (type);
461 mark_used (decl);
462 return build_nop (type_info_ptr_type,
463 build_address (decl));
466 /* Return the type_info object for TYPE. */
468 tree
469 get_typeid (tree type)
471 if (type == error_mark_node || !typeid_ok_p ())
472 return error_mark_node;
474 if (processing_template_decl)
475 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
477 /* If the type of the type-id is a reference type, the result of the
478 typeid expression refers to a type_info object representing the
479 referenced type. */
480 type = non_reference (type);
482 /* The top-level cv-qualifiers of the lvalue expression or the type-id
483 that is the operand of typeid are always ignored. */
484 type = TYPE_MAIN_VARIANT (type);
486 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
487 if (CLASS_TYPE_P (type) || type == unknown_type_node
488 || type == init_list_type_node)
489 type = complete_type_or_else (type, NULL_TREE);
491 if (!type)
492 return error_mark_node;
494 return cp_build_indirect_ref (get_tinfo_ptr (type), RO_NULL,
495 tf_warning_or_error);
498 /* Check whether TEST is null before returning RESULT. If TEST is used in
499 RESULT, it must have previously had a save_expr applied to it. */
501 static tree
502 ifnonnull (tree test, tree result, tsubst_flags_t complain)
504 return build3 (COND_EXPR, TREE_TYPE (result),
505 build2 (EQ_EXPR, boolean_type_node, test,
506 cp_convert (TREE_TYPE (test), nullptr_node,
507 complain)),
508 cp_convert (TREE_TYPE (result), nullptr_node, complain),
509 result);
512 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
513 paper. */
515 static tree
516 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
518 enum tree_code tc = TREE_CODE (type);
519 tree exprtype;
520 tree dcast_fn;
521 tree old_expr = expr;
522 const char *errstr = NULL;
524 /* Save casted types in the function's used types hash table. */
525 used_types_insert (type);
527 /* T shall be a pointer or reference to a complete class type, or
528 `pointer to cv void''. */
529 switch (tc)
531 case POINTER_TYPE:
532 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
533 break;
534 /* Fall through. */
535 case REFERENCE_TYPE:
536 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
538 errstr = _("target is not pointer or reference to class");
539 goto fail;
541 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
543 errstr = _("target is not pointer or reference to complete type");
544 goto fail;
546 break;
548 default:
549 errstr = _("target is not pointer or reference");
550 goto fail;
553 if (tc == POINTER_TYPE)
555 expr = decay_conversion (expr, complain);
556 exprtype = TREE_TYPE (expr);
558 /* If T is a pointer type, v shall be an rvalue of a pointer to
559 complete class type, and the result is an rvalue of type T. */
561 expr = mark_rvalue_use (expr);
563 if (TREE_CODE (exprtype) != POINTER_TYPE)
565 errstr = _("source is not a pointer");
566 goto fail;
568 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
570 errstr = _("source is not a pointer to class");
571 goto fail;
573 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
575 errstr = _("source is a pointer to incomplete type");
576 goto fail;
579 else
581 expr = mark_lvalue_use (expr);
583 exprtype = build_reference_type (TREE_TYPE (expr));
585 /* T is a reference type, v shall be an lvalue of a complete class
586 type, and the result is an lvalue of the type referred to by T. */
588 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
590 errstr = _("source is not of class type");
591 goto fail;
593 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
595 errstr = _("source is of incomplete class type");
596 goto fail;
599 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
600 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
601 LOOKUP_NORMAL, NULL_TREE, complain);
604 /* The dynamic_cast operator shall not cast away constness. */
605 if (!at_least_as_qualified_p (TREE_TYPE (type),
606 TREE_TYPE (exprtype)))
608 errstr = _("conversion casts away constness");
609 goto fail;
612 /* If *type is an unambiguous accessible base class of *exprtype,
613 convert statically. */
615 tree binfo;
617 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
618 ba_check, NULL);
620 if (binfo)
622 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
623 binfo, 0, complain);
624 if (TREE_CODE (exprtype) == POINTER_TYPE)
625 expr = rvalue (expr);
626 return expr;
630 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
631 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
633 tree expr1;
634 /* if TYPE is `void *', return pointer to complete object. */
635 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
637 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
638 if (TREE_CODE (expr) == ADDR_EXPR
639 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
640 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
641 return build1 (NOP_EXPR, type, expr);
643 /* Since expr is used twice below, save it. */
644 expr = save_expr (expr);
646 expr1 = build_headof (expr);
647 if (TREE_TYPE (expr1) != type)
648 expr1 = build1 (NOP_EXPR, type, expr1);
649 return ifnonnull (expr, expr1, complain);
651 else
653 tree retval;
654 tree result, td2, td3;
655 tree elems[4];
656 tree static_type, target_type, boff;
658 /* If we got here, we can't convert statically. Therefore,
659 dynamic_cast<D&>(b) (b an object) cannot succeed. */
660 if (tc == REFERENCE_TYPE)
662 if (TREE_CODE (old_expr) == VAR_DECL
663 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
665 tree expr = throw_bad_cast ();
666 if (complain & tf_warning)
667 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
668 old_expr, type);
669 /* Bash it to the expected type. */
670 TREE_TYPE (expr) = type;
671 return expr;
674 /* Ditto for dynamic_cast<D*>(&b). */
675 else if (TREE_CODE (expr) == ADDR_EXPR)
677 tree op = TREE_OPERAND (expr, 0);
678 if (TREE_CODE (op) == VAR_DECL
679 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
681 if (complain & tf_warning)
682 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
683 op, type);
684 retval = build_int_cst (type, 0);
685 return retval;
689 /* Use of dynamic_cast when -fno-rtti is prohibited. */
690 if (!flag_rtti)
692 if (complain & tf_error)
693 error ("%<dynamic_cast%> not permitted with -fno-rtti");
694 return error_mark_node;
697 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
698 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
699 td2 = get_tinfo_decl (target_type);
700 mark_used (td2);
701 td2 = cp_build_addr_expr (td2, complain);
702 td3 = get_tinfo_decl (static_type);
703 mark_used (td3);
704 td3 = cp_build_addr_expr (td3, complain);
706 /* Determine how T and V are related. */
707 boff = dcast_base_hint (static_type, target_type);
709 /* Since expr is used twice below, save it. */
710 expr = save_expr (expr);
712 expr1 = expr;
713 if (tc == REFERENCE_TYPE)
714 expr1 = cp_build_addr_expr (expr1, complain);
716 elems[0] = expr1;
717 elems[1] = td3;
718 elems[2] = td2;
719 elems[3] = boff;
721 dcast_fn = dynamic_cast_node;
722 if (!dcast_fn)
724 tree tmp;
725 tree tinfo_ptr;
726 const char *name;
728 push_abi_namespace ();
729 tinfo_ptr = xref_tag (class_type,
730 get_identifier ("__class_type_info"),
731 /*tag_scope=*/ts_current, false);
733 tinfo_ptr = build_pointer_type
734 (cp_build_qualified_type
735 (tinfo_ptr, TYPE_QUAL_CONST));
736 name = "__dynamic_cast";
737 tmp = build_function_type_list (ptr_type_node,
738 const_ptr_type_node,
739 tinfo_ptr, tinfo_ptr,
740 ptrdiff_type_node, NULL_TREE);
741 dcast_fn = build_library_fn_ptr (name, tmp);
742 DECL_PURE_P (dcast_fn) = 1;
743 pop_abi_namespace ();
744 dynamic_cast_node = dcast_fn;
746 result = build_cxx_call (dcast_fn, 4, elems);
748 if (tc == REFERENCE_TYPE)
750 tree bad = throw_bad_cast ();
751 tree neq;
753 result = save_expr (result);
754 neq = cp_truthvalue_conversion (result);
755 return cp_convert (type,
756 build3 (COND_EXPR, TREE_TYPE (result),
757 neq, result, bad), complain);
760 /* Now back to the type we want from a void*. */
761 result = cp_convert (type, result, complain);
762 return ifnonnull (expr, result, complain);
765 else
766 errstr = _("source type is not polymorphic");
768 fail:
769 if (complain & tf_error)
770 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
771 old_expr, TREE_TYPE (old_expr), type, errstr);
772 return error_mark_node;
775 tree
776 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
778 tree r;
780 if (type == error_mark_node || expr == error_mark_node)
781 return error_mark_node;
783 if (processing_template_decl)
785 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
786 TREE_SIDE_EFFECTS (expr) = 1;
787 return convert_from_reference (expr);
790 r = convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
791 if (r != error_mark_node)
792 maybe_warn_about_useless_cast (type, expr, complain);
793 return r;
796 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
798 static int
799 qualifier_flags (tree type)
801 int flags = 0;
802 int quals = cp_type_quals (type);
804 if (quals & TYPE_QUAL_CONST)
805 flags |= 1;
806 if (quals & TYPE_QUAL_VOLATILE)
807 flags |= 2;
808 if (quals & TYPE_QUAL_RESTRICT)
809 flags |= 4;
810 return flags;
813 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
814 contains a pointer to member of an incomplete class. */
816 static bool
817 target_incomplete_p (tree type)
819 while (true)
820 if (TYPE_PTRDATAMEM_P (type))
822 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
823 return true;
824 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
826 else if (TREE_CODE (type) == POINTER_TYPE)
827 type = TREE_TYPE (type);
828 else
829 return !COMPLETE_OR_VOID_TYPE_P (type);
832 /* Returns true if TYPE involves an incomplete class type; in that
833 case, typeinfo variables for TYPE should be emitted with internal
834 linkage. */
836 static bool
837 involves_incomplete_p (tree type)
839 switch (TREE_CODE (type))
841 case POINTER_TYPE:
842 return target_incomplete_p (TREE_TYPE (type));
844 case OFFSET_TYPE:
845 ptrmem:
846 return
847 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
848 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
850 case RECORD_TYPE:
851 if (TYPE_PTRMEMFUNC_P (type))
852 goto ptrmem;
853 /* Fall through. */
854 case UNION_TYPE:
855 if (!COMPLETE_TYPE_P (type))
856 return true;
858 default:
859 /* All other types do not involve incomplete class types. */
860 return false;
864 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
865 is the vtable pointer and NTBS name. The NTBS name is emitted as a
866 comdat const char array, so it becomes a unique key for the type. Generate
867 and emit that VAR_DECL here. (We can't always emit the type_info itself
868 as comdat, because of pointers to incomplete.) */
870 static tree
871 tinfo_base_init (tinfo_s *ti, tree target)
873 tree init;
874 tree name_decl;
875 tree vtable_ptr;
876 VEC(constructor_elt,gc) *v;
879 tree name_name, name_string;
881 /* Generate the NTBS array variable. */
882 tree name_type = build_cplus_array_type
883 (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST),
884 NULL_TREE);
886 /* Determine the name of the variable -- and remember with which
887 type it is associated. */
888 name_name = mangle_typeinfo_string_for_type (target);
889 TREE_TYPE (name_name) = target;
891 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
892 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
893 DECL_ARTIFICIAL (name_decl) = 1;
894 DECL_IGNORED_P (name_decl) = 1;
895 TREE_READONLY (name_decl) = 1;
896 TREE_STATIC (name_decl) = 1;
897 DECL_EXTERNAL (name_decl) = 0;
898 DECL_TINFO_P (name_decl) = 1;
899 set_linkage_according_to_type (target, name_decl);
900 import_export_decl (name_decl);
901 name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
902 DECL_INITIAL (name_decl) = name_string;
903 mark_used (name_decl);
904 pushdecl_top_level_and_finish (name_decl, name_string);
907 vtable_ptr = ti->vtable;
908 if (!vtable_ptr)
910 tree real_type;
911 push_abi_namespace ();
912 real_type = xref_tag (class_type, ti->name,
913 /*tag_scope=*/ts_current, false);
914 pop_abi_namespace ();
916 if (!COMPLETE_TYPE_P (real_type))
918 /* We never saw a definition of this type, so we need to
919 tell the compiler that this is an exported class, as
920 indeed all of the __*_type_info classes are. */
921 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
922 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
925 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
926 vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error);
928 /* We need to point into the middle of the vtable. */
929 vtable_ptr = fold_build_pointer_plus
930 (vtable_ptr,
931 size_binop (MULT_EXPR,
932 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
933 TYPE_SIZE_UNIT (vtable_entry_type)));
935 ti->vtable = vtable_ptr;
938 v = VEC_alloc (constructor_elt, gc, 2);
939 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr);
940 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
941 decay_conversion (name_decl, tf_warning_or_error));
943 init = build_constructor (init_list_type_node, v);
944 TREE_CONSTANT (init) = 1;
945 TREE_STATIC (init) = 1;
947 return init;
950 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
951 information about the particular type_info derivation, which adds no
952 additional fields to the type_info base. */
954 static tree
955 generic_initializer (tinfo_s *ti, tree target)
957 tree init = tinfo_base_init (ti, target);
959 init = build_constructor_single (init_list_type_node, NULL_TREE, init);
960 TREE_CONSTANT (init) = 1;
961 TREE_STATIC (init) = 1;
962 return init;
965 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
966 TI provides information about the particular type_info derivation,
967 which adds target type and qualifier flags members to the type_info base. */
969 static tree
970 ptr_initializer (tinfo_s *ti, tree target)
972 tree init = tinfo_base_init (ti, target);
973 tree to = TREE_TYPE (target);
974 int flags = qualifier_flags (to);
975 bool incomplete = target_incomplete_p (to);
976 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 3);
978 if (incomplete)
979 flags |= 8;
980 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
981 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
982 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
983 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
985 init = build_constructor (init_list_type_node, v);
986 TREE_CONSTANT (init) = 1;
987 TREE_STATIC (init) = 1;
988 return init;
991 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
992 TI provides information about the particular type_info derivation,
993 which adds class, target type and qualifier flags members to the type_info
994 base. */
996 static tree
997 ptm_initializer (tinfo_s *ti, tree target)
999 tree init = tinfo_base_init (ti, target);
1000 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
1001 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
1002 int flags = qualifier_flags (to);
1003 bool incomplete = target_incomplete_p (to);
1004 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, 4);
1006 if (incomplete)
1007 flags |= 0x8;
1008 if (!COMPLETE_TYPE_P (klass))
1009 flags |= 0x10;
1010 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1011 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags));
1012 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE,
1013 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)));
1014 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_tinfo_ptr (klass));
1016 init = build_constructor (init_list_type_node, v);
1017 TREE_CONSTANT (init) = 1;
1018 TREE_STATIC (init) = 1;
1019 return init;
1022 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1023 TI provides information about the particular __class_type_info derivation,
1024 which adds hint flags and N extra initializers to the type_info base. */
1026 static tree
1027 class_initializer (tinfo_s *ti, tree target, unsigned n, ...)
1029 tree init = tinfo_base_init (ti, target);
1030 va_list extra_inits;
1031 unsigned i;
1032 VEC(constructor_elt,gc) *v = VEC_alloc (constructor_elt, gc, n+1);
1034 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
1035 va_start (extra_inits, n);
1036 for (i = 0; i < n; i++)
1037 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree));
1038 va_end (extra_inits);
1040 init = build_constructor (init_list_type_node, v);
1041 TREE_CONSTANT (init) = 1;
1042 TREE_STATIC (init) = 1;
1043 return init;
1046 /* Returns true if the typeinfo for type should be placed in
1047 the runtime library. */
1049 static bool
1050 typeinfo_in_lib_p (tree type)
1052 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1053 library for simple types T. */
1054 if (TREE_CODE (type) == POINTER_TYPE
1055 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1056 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1057 type = TREE_TYPE (type);
1059 switch (TREE_CODE (type))
1061 case INTEGER_TYPE:
1062 case BOOLEAN_TYPE:
1063 case REAL_TYPE:
1064 case VOID_TYPE:
1065 case NULLPTR_TYPE:
1066 return true;
1068 case LANG_TYPE:
1069 /* fall through. */
1071 default:
1072 return false;
1076 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1077 the index of the descriptor in the tinfo_desc vector. */
1079 static tree
1080 get_pseudo_ti_init (tree type, unsigned tk_index)
1082 tinfo_s *ti = &VEC_index (tinfo_s, tinfo_descs, tk_index);
1084 gcc_assert (at_eof);
1085 switch (tk_index)
1087 case TK_POINTER_MEMBER_TYPE:
1088 return ptm_initializer (ti, type);
1090 case TK_POINTER_TYPE:
1091 return ptr_initializer (ti, type);
1093 case TK_BUILTIN_TYPE:
1094 case TK_ENUMERAL_TYPE:
1095 case TK_FUNCTION_TYPE:
1096 case TK_ARRAY_TYPE:
1097 return generic_initializer (ti, type);
1099 case TK_CLASS_TYPE:
1100 return class_initializer (ti, type, 0);
1102 case TK_SI_CLASS_TYPE:
1104 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1105 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1107 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1108 ti = &VEC_index (tinfo_s, tinfo_descs, tk_index);
1109 return class_initializer (ti, type, 1, tinfo);
1112 default:
1114 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1115 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1116 tree binfo = TYPE_BINFO (type);
1117 int nbases = BINFO_N_BASE_BINFOS (binfo);
1118 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1119 tree offset_type = integer_types[itk_long];
1120 tree base_inits = NULL_TREE;
1121 int ix;
1122 VEC(constructor_elt,gc) *init_vec = NULL;
1123 constructor_elt *e;
1125 gcc_assert (tk_index >= TK_FIXED);
1127 VEC_safe_grow (constructor_elt, gc, init_vec, nbases);
1128 /* Generate the base information initializer. */
1129 for (ix = nbases; ix--;)
1131 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1132 tree base_init;
1133 int flags = 0;
1134 tree tinfo;
1135 tree offset;
1136 VEC(constructor_elt,gc) *v;
1138 if (VEC_index (tree, base_accesses, ix) == access_public_node)
1139 flags |= 2;
1140 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1141 if (BINFO_VIRTUAL_P (base_binfo))
1143 /* We store the vtable offset at which the virtual
1144 base offset can be found. */
1145 offset = BINFO_VPTR_FIELD (base_binfo);
1146 flags |= 1;
1148 else
1149 offset = BINFO_OFFSET (base_binfo);
1151 /* Combine offset and flags into one field. */
1152 offset = fold_convert (offset_type, offset);
1153 offset = fold_build2_loc (input_location,
1154 LSHIFT_EXPR, offset_type, offset,
1155 build_int_cst (offset_type, 8));
1156 offset = fold_build2_loc (input_location,
1157 BIT_IOR_EXPR, offset_type, offset,
1158 build_int_cst (offset_type, flags));
1159 v = VEC_alloc (constructor_elt, gc, 2);
1160 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo);
1161 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset);
1162 base_init = build_constructor (init_list_type_node, v);
1163 e = &VEC_index (constructor_elt, init_vec, ix);
1164 e->index = NULL_TREE;
1165 e->value = base_init;
1167 base_inits = build_constructor (init_list_type_node, init_vec);
1169 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1170 ti = &VEC_index (tinfo_s, tinfo_descs, tk_index);
1171 return class_initializer (ti, type, 3,
1172 build_int_cst (NULL_TREE, hint),
1173 build_int_cst (NULL_TREE, nbases),
1174 base_inits);
1179 /* Generate the RECORD_TYPE containing the data layout of a type_info
1180 derivative as used by the runtime. This layout must be consistent with
1181 that defined in the runtime support. Also generate the VAR_DECL for the
1182 type's vtable. We explicitly manage the vtable member, and name it for
1183 real type as used in the runtime. The RECORD type has a different name,
1184 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1185 is the generated type and TINFO_VTABLE_NAME is the name of the
1186 vtable. We have to delay generating the VAR_DECL of the vtable
1187 until the end of the translation, when we'll have seen the library
1188 definition, if there was one.
1190 REAL_NAME is the runtime's name of the type. Trailing arguments are
1191 additional FIELD_DECL's for the structure. The final argument must be
1192 NULL. */
1194 static void
1195 create_pseudo_type_info (int tk, const char *real_name, ...)
1197 tinfo_s *ti;
1198 tree pseudo_type;
1199 char *pseudo_name;
1200 tree fields;
1201 tree field_decl;
1202 va_list ap;
1204 va_start (ap, real_name);
1206 /* Generate the pseudo type name. */
1207 pseudo_name = (char *) alloca (strlen (real_name) + 30);
1208 strcpy (pseudo_name, real_name);
1209 strcat (pseudo_name, "_pseudo");
1210 if (tk >= TK_FIXED)
1211 sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1213 /* First field is the pseudo type_info base class. */
1214 fields = build_decl (input_location,
1215 FIELD_DECL, NULL_TREE,
1216 VEC_index (tinfo_s, tinfo_descs,
1217 TK_TYPE_INFO_TYPE).type);
1219 /* Now add the derived fields. */
1220 while ((field_decl = va_arg (ap, tree)))
1222 DECL_CHAIN (field_decl) = fields;
1223 fields = field_decl;
1226 /* Create the pseudo type. */
1227 pseudo_type = make_class_type (RECORD_TYPE);
1228 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1229 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1231 ti = &VEC_index (tinfo_s, tinfo_descs, tk);
1232 ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1233 ti->name = get_identifier (real_name);
1234 ti->vtable = NULL_TREE;
1236 /* Pretend this is public so determine_visibility doesn't give vtables
1237 internal linkage. */
1238 TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1240 va_end (ap);
1243 /* Return the index of a pseudo type info type node used to describe
1244 TYPE. TYPE must be a complete type (or cv void), except at the end
1245 of the translation unit. */
1247 static unsigned
1248 get_pseudo_ti_index (tree type)
1250 unsigned ix;
1252 switch (TREE_CODE (type))
1254 case OFFSET_TYPE:
1255 ix = TK_POINTER_MEMBER_TYPE;
1256 break;
1258 case POINTER_TYPE:
1259 ix = TK_POINTER_TYPE;
1260 break;
1262 case ENUMERAL_TYPE:
1263 ix = TK_ENUMERAL_TYPE;
1264 break;
1266 case FUNCTION_TYPE:
1267 ix = TK_FUNCTION_TYPE;
1268 break;
1270 case ARRAY_TYPE:
1271 ix = TK_ARRAY_TYPE;
1272 break;
1274 case UNION_TYPE:
1275 case RECORD_TYPE:
1276 if (TYPE_PTRMEMFUNC_P (type))
1278 ix = TK_POINTER_MEMBER_TYPE;
1279 break;
1281 else if (!COMPLETE_TYPE_P (type))
1283 if (!at_eof)
1284 cxx_incomplete_type_error (NULL_TREE, type);
1285 ix = TK_CLASS_TYPE;
1286 break;
1288 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1290 ix = TK_CLASS_TYPE;
1291 break;
1293 else
1295 tree binfo = TYPE_BINFO (type);
1296 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1297 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1298 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1300 if (num_bases == 1
1301 && VEC_index (tree, base_accesses, 0) == access_public_node
1302 && !BINFO_VIRTUAL_P (base_binfo)
1303 && integer_zerop (BINFO_OFFSET (base_binfo)))
1305 /* single non-virtual public. */
1306 ix = TK_SI_CLASS_TYPE;
1307 break;
1309 else
1311 tinfo_s *ti;
1312 tree array_domain, base_array;
1314 ix = TK_FIXED + num_bases;
1315 if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1317 /* too short, extend. */
1318 unsigned len = VEC_length (tinfo_s, tinfo_descs);
1320 VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1321 while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1322 ti->type = ti->vtable = ti->name = NULL_TREE;
1324 else if (VEC_index (tinfo_s, tinfo_descs, ix).type)
1325 /* already created. */
1326 break;
1328 /* Create the array of __base_class_type_info entries.
1329 G++ 3.2 allocated an array that had one too many
1330 entries, and then filled that extra entries with
1331 zeros. */
1332 if (abi_version_at_least (2))
1333 array_domain = build_index_type (size_int (num_bases - 1));
1334 else
1335 array_domain = build_index_type (size_int (num_bases));
1336 base_array =
1337 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1338 TK_BASE_TYPE).type,
1339 array_domain);
1341 push_abi_namespace ();
1342 create_pseudo_type_info
1343 (ix, "__vmi_class_type_info",
1344 build_decl (input_location,
1345 FIELD_DECL, NULL_TREE, integer_type_node),
1346 build_decl (input_location,
1347 FIELD_DECL, NULL_TREE, integer_type_node),
1348 build_decl (input_location,
1349 FIELD_DECL, NULL_TREE, base_array),
1350 NULL);
1351 pop_abi_namespace ();
1352 break;
1355 default:
1356 ix = TK_BUILTIN_TYPE;
1357 break;
1359 return ix;
1362 /* Make sure the required builtin types exist for generating the type_info
1363 variable definitions. */
1365 static void
1366 create_tinfo_types (void)
1368 tinfo_s *ti;
1370 gcc_assert (!tinfo_descs);
1372 VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1374 push_abi_namespace ();
1376 /* Create the internal type_info structure. This is used as a base for
1377 the other structures. */
1379 tree field, fields;
1381 field = build_decl (BUILTINS_LOCATION,
1382 FIELD_DECL, NULL_TREE, const_ptr_type_node);
1383 fields = field;
1385 field = build_decl (BUILTINS_LOCATION,
1386 FIELD_DECL, NULL_TREE, const_string_type_node);
1387 DECL_CHAIN (field) = fields;
1388 fields = field;
1390 ti = &VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1391 ti->type = make_class_type (RECORD_TYPE);
1392 ti->vtable = NULL_TREE;
1393 ti->name = NULL_TREE;
1394 finish_builtin_struct (ti->type, "__type_info_pseudo",
1395 fields, NULL_TREE);
1398 /* Fundamental type_info */
1399 create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1401 /* Array, function and enum type_info. No additional fields. */
1402 create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1403 create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1404 create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1406 /* Class type_info. No additional fields. */
1407 create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1409 /* Single public non-virtual base class. Add pointer to base class.
1410 This is really a descendant of __class_type_info. */
1411 create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1412 build_decl (BUILTINS_LOCATION,
1413 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1414 NULL);
1416 /* Base class internal helper. Pointer to base type, offset to base,
1417 flags. */
1419 tree field, fields;
1421 field = build_decl (BUILTINS_LOCATION,
1422 FIELD_DECL, NULL_TREE, type_info_ptr_type);
1423 fields = field;
1425 field = build_decl (BUILTINS_LOCATION,
1426 FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1427 DECL_CHAIN (field) = fields;
1428 fields = field;
1430 ti = &VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1432 ti->type = make_class_type (RECORD_TYPE);
1433 ti->vtable = NULL_TREE;
1434 ti->name = NULL_TREE;
1435 finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1436 fields, NULL_TREE);
1439 /* Pointer type_info. Adds two fields, qualification mask
1440 and pointer to the pointed to type. This is really a descendant of
1441 __pbase_type_info. */
1442 create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1443 build_decl (BUILTINS_LOCATION,
1444 FIELD_DECL, NULL_TREE, integer_type_node),
1445 build_decl (BUILTINS_LOCATION,
1446 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1447 NULL);
1449 /* Pointer to member data type_info. Add qualifications flags,
1450 pointer to the member's type info and pointer to the class.
1451 This is really a descendant of __pbase_type_info. */
1452 create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1453 "__pointer_to_member_type_info",
1454 build_decl (BUILTINS_LOCATION,
1455 FIELD_DECL, NULL_TREE, integer_type_node),
1456 build_decl (BUILTINS_LOCATION,
1457 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1458 build_decl (BUILTINS_LOCATION,
1459 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1460 NULL);
1462 pop_abi_namespace ();
1465 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1466 support. Generating them here guarantees consistency with the other
1467 structures. We use the following heuristic to determine when the runtime
1468 is being generated. If std::__fundamental_type_info is defined, and its
1469 destructor is defined, then the runtime is being built. */
1471 void
1472 emit_support_tinfos (void)
1474 /* Dummy static variable so we can put nullptr in the array; it will be
1475 set before we actually start to walk the array. */
1476 static tree *const fundamentals[] =
1478 &void_type_node,
1479 &boolean_type_node,
1480 &wchar_type_node, &char16_type_node, &char32_type_node,
1481 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1482 &short_integer_type_node, &short_unsigned_type_node,
1483 &integer_type_node, &unsigned_type_node,
1484 &long_integer_type_node, &long_unsigned_type_node,
1485 &long_long_integer_type_node, &long_long_unsigned_type_node,
1486 &int128_integer_type_node, &int128_unsigned_type_node,
1487 &float_type_node, &double_type_node, &long_double_type_node,
1488 &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node,
1489 &nullptr_type_node,
1492 int ix;
1493 tree bltn_type, dtor;
1495 push_abi_namespace ();
1496 bltn_type = xref_tag (class_type,
1497 get_identifier ("__fundamental_type_info"),
1498 /*tag_scope=*/ts_current, false);
1499 pop_abi_namespace ();
1500 if (!COMPLETE_TYPE_P (bltn_type))
1501 return;
1502 dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1503 if (!dtor || DECL_EXTERNAL (dtor))
1504 return;
1505 doing_runtime = 1;
1506 for (ix = 0; fundamentals[ix]; ix++)
1508 tree bltn = *fundamentals[ix];
1509 tree types[3];
1510 int i;
1512 if (bltn == NULL_TREE)
1513 continue;
1514 types[0] = bltn;
1515 types[1] = build_pointer_type (bltn);
1516 types[2] = build_pointer_type (cp_build_qualified_type (bltn,
1517 TYPE_QUAL_CONST));
1519 for (i = 0; i < 3; ++i)
1521 tree tinfo;
1523 tinfo = get_tinfo_decl (types[i]);
1524 TREE_USED (tinfo) = 1;
1525 mark_needed (tinfo);
1526 /* The C++ ABI requires that these objects be COMDAT. But,
1527 On systems without weak symbols, initialized COMDAT
1528 objects are emitted with internal linkage. (See
1529 comdat_linkage for details.) Since we want these objects
1530 to have external linkage so that copies do not have to be
1531 emitted in code outside the runtime library, we make them
1532 non-COMDAT here.
1534 It might also not be necessary to follow this detail of the
1535 ABI. */
1536 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1538 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1539 DECL_INTERFACE_KNOWN (tinfo) = 1;
1545 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1546 tinfo decl. Determine whether it needs emitting, and if so
1547 generate the initializer. */
1549 bool
1550 emit_tinfo_decl (tree decl)
1552 tree type = TREE_TYPE (DECL_NAME (decl));
1553 int in_library = typeinfo_in_lib_p (type);
1555 gcc_assert (DECL_TINFO_P (decl));
1557 if (in_library)
1559 if (doing_runtime)
1560 DECL_EXTERNAL (decl) = 0;
1561 else
1563 /* If we're not in the runtime, then DECL (which is already
1564 DECL_EXTERNAL) will not be defined here. */
1565 DECL_INTERFACE_KNOWN (decl) = 1;
1566 return false;
1569 else if (involves_incomplete_p (type))
1571 if (!decl_needed_p (decl))
1572 return false;
1573 /* If TYPE involves an incomplete class type, then the typeinfo
1574 object will be emitted with internal linkage. There is no
1575 way to know whether or not types are incomplete until the end
1576 of the compilation, so this determination must be deferred
1577 until this point. */
1578 TREE_PUBLIC (decl) = 0;
1579 DECL_EXTERNAL (decl) = 0;
1580 DECL_INTERFACE_KNOWN (decl) = 1;
1583 import_export_decl (decl);
1584 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1586 tree init;
1588 DECL_EXTERNAL (decl) = 0;
1589 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1590 DECL_INITIAL (decl) = init;
1591 mark_used (decl);
1592 cp_finish_decl (decl, init, false, NULL_TREE, 0);
1593 return true;
1595 else
1596 return false;
1599 #include "gt-cp-rtti.h"