1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009
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)
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/>. */
25 #include "coretypes.h"
37 /* C++ returns type information to the user in struct type_info
38 objects. We also use type information to implement dynamic_cast and
39 exception handlers. Type information for a particular type is
40 indicated with an ABI defined structure derived from type_info.
41 This would all be very straight forward, but for the fact that the
42 runtime library provides the definitions of the type_info structure
43 and the ABI defined derived classes. We cannot build declarations
44 of them directly in the compiler, but we need to layout objects of
45 their type. Somewhere we have to lie.
47 We define layout compatible POD-structs with compiler-defined names
48 and generate the appropriate initializations for them (complete
49 with explicit mention of their vtable). When we have to provide a
50 type_info to the user we reinterpret_cast the internal compiler
51 type to type_info. A well formed program can only explicitly refer
52 to the type_infos of complete types (& cv void). However, we chain
53 pointer type_infos to the pointed-to-type, and that can be
54 incomplete. We only need the addresses of such incomplete
55 type_info objects for static initialization.
57 The type information VAR_DECL of a type is held on the
58 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
59 will be the internal type. It will usually have the correct
60 internal type reflecting the kind of type it represents (pointer,
61 array, function, class, inherited class, etc). When the type it
62 represents is incomplete, it will have the internal type
63 corresponding to type_info. That will only happen at the end of
64 translation, when we are emitting the type info objects. */
66 /* Auxiliary data we hold for each type_info derived object we need. */
67 typedef struct GTY (()) tinfo_s {
68 tree type; /* The RECORD_TYPE for this type_info object */
70 tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
73 tree name; /* IDENTIFIER_NODE for the ABI specified name of
74 the type_info derived type. */
78 DEF_VEC_ALLOC_O(tinfo_s,gc);
80 typedef enum tinfo_kind
82 TK_TYPE_INFO_TYPE, /* abi::__type_info_pseudo */
83 TK_BASE_TYPE, /* abi::__base_class_type_info */
84 TK_BUILTIN_TYPE, /* abi::__fundamental_type_info */
85 TK_ARRAY_TYPE, /* abi::__array_type_info */
86 TK_FUNCTION_TYPE, /* abi::__function_type_info */
87 TK_ENUMERAL_TYPE, /* abi::__enum_type_info */
88 TK_POINTER_TYPE, /* abi::__pointer_type_info */
89 TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
90 TK_CLASS_TYPE, /* abi::__class_type_info */
91 TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */
92 TK_FIXED /* end of fixed descriptors. */
93 /* ... abi::__vmi_type_info<I> */
96 /* A vector of all tinfo decls that haven't yet been emitted. */
97 VEC(tree,gc) *unemitted_tinfo_decls;
99 /* A vector of all type_info derived types we need. The first few are
100 fixed and created early. The remainder are for multiple inheritance
101 and are generated as needed. */
102 static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
104 static tree ifnonnull (tree, tree);
105 static tree tinfo_name (tree);
106 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
107 static tree throw_bad_cast (void);
108 static tree throw_bad_typeid (void);
109 static tree get_tinfo_decl_dynamic (tree);
110 static tree get_tinfo_ptr (tree);
111 static bool typeid_ok_p (void);
112 static int qualifier_flags (tree);
113 static bool target_incomplete_p (tree);
114 static tree tinfo_base_init (tinfo_s *, tree);
115 static tree generic_initializer (tinfo_s *, tree);
116 static tree ptr_initializer (tinfo_s *, tree);
117 static tree ptm_initializer (tinfo_s *, tree);
118 static tree class_initializer (tinfo_s *, tree, tree);
119 static void create_pseudo_type_info (int, const char *, ...);
120 static tree get_pseudo_ti_init (tree, unsigned);
121 static unsigned get_pseudo_ti_index (tree);
122 static void create_tinfo_types (void);
123 static bool typeinfo_in_lib_p (tree);
125 static int doing_runtime = 0;
128 push_abi_namespace (void)
130 push_nested_namespace (abi_node);
131 push_visibility ("default");
135 pop_abi_namespace (void)
138 pop_nested_namespace (abi_node);
141 /* Declare language defined type_info type and a pointer to const
142 type_info. This is incomplete here, and will be completed when
143 the user #includes <typeinfo>. There are language defined
144 restrictions on what can be done until that is included. Create
145 the internal versions of the ABI types. */
148 init_rtti_processing (void)
152 push_namespace (std_identifier);
153 type_info_type = xref_tag (class_type, get_identifier ("type_info"),
154 /*tag_scope=*/ts_current, false);
156 const_type_info_type_node
157 = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
158 type_info_ptr_type = build_pointer_type (const_type_info_type_node);
160 unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
162 create_tinfo_types ();
165 /* Given the expression EXP of type `class *', return the head of the
166 object pointed to by EXP with type cv void*, if the class has any
167 virtual functions (TYPE_POLYMORPHIC_P), else just return the
171 build_headof (tree exp)
173 tree type = TREE_TYPE (exp);
177 gcc_assert (TREE_CODE (type) == POINTER_TYPE);
178 type = TREE_TYPE (type);
180 if (!TYPE_POLYMORPHIC_P (type))
183 /* We use this a couple of times below, protect it. */
184 exp = save_expr (exp);
186 /* The offset-to-top field is at index -2 from the vptr. */
187 index = build_int_cst (NULL_TREE,
188 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
190 offset = build_vtbl_ref (cp_build_indirect_ref (exp, NULL,
191 tf_warning_or_error),
194 type = build_qualified_type (ptr_type_node,
195 cp_type_quals (TREE_TYPE (exp)));
196 return build2 (POINTER_PLUS_EXPR, type, exp,
197 convert_to_integer (sizetype, offset));
200 /* Get a bad_cast node for the program to throw...
202 See libstdc++/exception.cc for __throw_bad_cast */
205 throw_bad_cast (void)
207 tree fn = get_identifier ("__cxa_bad_cast");
208 if (!get_global_value_if_present (fn, &fn))
209 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
212 return build_cxx_call (fn, 0, NULL);
215 /* Return an expression for "__cxa_bad_typeid()". The expression
216 returned is an lvalue of type "const std::type_info". */
219 throw_bad_typeid (void)
221 tree fn = get_identifier ("__cxa_bad_typeid");
222 if (!get_global_value_if_present (fn, &fn))
226 t = build_reference_type (const_type_info_type_node);
227 t = build_function_type (t, void_list_node);
228 fn = push_throw_library_fn (fn, t);
231 return build_cxx_call (fn, 0, NULL);
234 /* Return an lvalue expression whose type is "const std::type_info"
235 and whose value indicates the type of the expression EXP. If EXP
236 is a reference to a polymorphic class, return the dynamic type;
237 otherwise return the static type of the expression. */
240 get_tinfo_decl_dynamic (tree exp)
245 if (error_operand_p (exp))
246 return error_mark_node;
248 /* peel back references, so they match. */
249 type = non_reference (TREE_TYPE (exp));
251 /* Peel off cv qualifiers. */
252 type = TYPE_MAIN_VARIANT (type);
254 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
255 if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
256 type = complete_type_or_else (type, exp);
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. */
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);
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, NULL, tf_warning_or_error);
283 tree pseudo_type_info, type_info_type;
287 error ("cannot use typeid with -fno-rtti");
291 if (!COMPLETE_TYPE_P (const_type_info_type_node))
293 error ("must #include <typeinfo> before using typeid");
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);
306 gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
307 == get_alias_set (type_info_type));
312 /* Return an expression for "typeid(EXP)". The expression returned is
313 an lvalue of type "const std::type_info". */
316 build_typeid (tree exp)
318 tree cond = NULL_TREE;
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 if (TREE_CODE (exp) == INDIRECT_REF
328 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
329 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
330 && ! resolves_to_fixed_type_p (exp, &nonnull)
333 exp = stabilize_reference (exp);
334 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
337 exp = get_tinfo_decl_dynamic (exp);
339 if (exp == error_mark_node)
340 return error_mark_node;
344 tree bad = throw_bad_typeid ();
346 exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
352 /* Generate the NTBS name of a type. */
354 tinfo_name (tree type)
359 name = mangle_type_string_for_rtti (type);
360 name_string = fix_string_type (build_string (strlen (name) + 1, name));
364 /* Return a VAR_DECL for the internal ABI defined type_info object for
365 TYPE. You must arrange that the decl is mark_used, if actually use
366 it --- decls in vtables are only used if the vtable is output. */
369 get_tinfo_decl (tree type)
374 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
376 error ("cannot create type information for type %qT because "
377 "it involves types of variable size",
379 return error_mark_node;
382 if (TREE_CODE (type) == METHOD_TYPE)
383 type = build_function_type (TREE_TYPE (type),
384 TREE_CHAIN (TYPE_ARG_TYPES (type)));
386 /* For a class type, the variable is cached in the type node
388 if (CLASS_TYPE_P (type))
390 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
395 name = mangle_typeinfo_for_type (type);
397 d = IDENTIFIER_GLOBAL_VALUE (name);
400 int ix = get_pseudo_ti_index (type);
401 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
403 d = build_lang_decl (VAR_DECL, name, ti->type);
404 SET_DECL_ASSEMBLER_NAME (d, name);
405 /* Remember the type it is for. */
406 TREE_TYPE (name) = type;
407 DECL_TINFO_P (d) = 1;
408 DECL_ARTIFICIAL (d) = 1;
409 DECL_IGNORED_P (d) = 1;
410 TREE_READONLY (d) = 1;
412 /* Mark the variable as undefined -- but remember that we can
413 define it later if we need to do so. */
414 DECL_EXTERNAL (d) = 1;
415 DECL_NOT_REALLY_EXTERN (d) = 1;
416 set_linkage_according_to_type (type, d);
418 d = pushdecl_top_level_and_finish (d, NULL_TREE);
419 if (CLASS_TYPE_P (type))
420 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
422 /* Add decl to the global array of tinfo decls. */
423 VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
429 /* Return a pointer to a type_info object describing TYPE, suitably
430 cast to the language defined type. */
433 get_tinfo_ptr (tree type)
435 tree decl = get_tinfo_decl (type);
438 return build_nop (type_info_ptr_type,
439 build_address (decl));
442 /* Return the type_info object for TYPE. */
445 get_typeid (tree type)
447 if (type == error_mark_node || !typeid_ok_p ())
448 return error_mark_node;
450 if (processing_template_decl)
451 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
453 /* If the type of the type-id is a reference type, the result of the
454 typeid expression refers to a type_info object representing the
456 type = non_reference (type);
458 /* The top-level cv-qualifiers of the lvalue expression or the type-id
459 that is the operand of typeid are always ignored. */
460 type = TYPE_MAIN_VARIANT (type);
462 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
463 if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
464 type = complete_type_or_else (type, NULL_TREE);
467 return error_mark_node;
469 return cp_build_indirect_ref (get_tinfo_ptr (type), NULL,
470 tf_warning_or_error);
473 /* Check whether TEST is null before returning RESULT. If TEST is used in
474 RESULT, it must have previously had a save_expr applied to it. */
477 ifnonnull (tree test, tree result)
479 return build3 (COND_EXPR, TREE_TYPE (result),
480 build2 (EQ_EXPR, boolean_type_node, test,
481 cp_convert (TREE_TYPE (test), integer_zero_node)),
482 cp_convert (TREE_TYPE (result), integer_zero_node),
486 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
490 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
492 enum tree_code tc = TREE_CODE (type);
493 tree exprtype = TREE_TYPE (expr);
495 tree old_expr = expr;
496 const char *errstr = NULL;
498 /* Save casted types in the function's used types hash table. */
499 used_types_insert (type);
501 /* T shall be a pointer or reference to a complete class type, or
502 `pointer to cv void''. */
506 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
510 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
512 errstr = "target is not pointer or reference to class";
515 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
517 errstr = "target is not pointer or reference to complete type";
523 errstr = "target is not pointer or reference";
527 if (tc == POINTER_TYPE)
529 /* If T is a pointer type, v shall be an rvalue of a pointer to
530 complete class type, and the result is an rvalue of type T. */
532 if (TREE_CODE (exprtype) != POINTER_TYPE)
534 errstr = "source is not a pointer";
537 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
539 errstr = "source is not a pointer to class";
542 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
544 errstr = "source is a pointer to incomplete type";
550 exprtype = build_reference_type (exprtype);
552 /* T is a reference type, v shall be an lvalue of a complete class
553 type, and the result is an lvalue of the type referred to by T. */
555 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
557 errstr = "source is not of class type";
560 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
562 errstr = "source is of incomplete class type";
566 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
567 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
568 LOOKUP_NORMAL, NULL_TREE);
571 /* The dynamic_cast operator shall not cast away constness. */
572 if (!at_least_as_qualified_p (TREE_TYPE (type),
573 TREE_TYPE (exprtype)))
575 errstr = "conversion casts away constness";
579 /* If *type is an unambiguous accessible base class of *exprtype,
580 convert statically. */
584 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
589 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
591 if (TREE_CODE (exprtype) == POINTER_TYPE)
592 expr = rvalue (expr);
597 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
598 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
601 /* if TYPE is `void *', return pointer to complete object. */
602 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
604 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
605 if (TREE_CODE (expr) == ADDR_EXPR
606 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
607 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
608 return build1 (NOP_EXPR, type, expr);
610 /* Since expr is used twice below, save it. */
611 expr = save_expr (expr);
613 expr1 = build_headof (expr);
614 if (TREE_TYPE (expr1) != type)
615 expr1 = build1 (NOP_EXPR, type, expr1);
616 return ifnonnull (expr, expr1);
621 tree result, td2, td3;
623 tree static_type, target_type, boff;
625 /* If we got here, we can't convert statically. Therefore,
626 dynamic_cast<D&>(b) (b an object) cannot succeed. */
627 if (tc == REFERENCE_TYPE)
629 if (TREE_CODE (old_expr) == VAR_DECL
630 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
632 tree expr = throw_bad_cast ();
633 if (complain & tf_warning)
634 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
636 /* Bash it to the expected type. */
637 TREE_TYPE (expr) = type;
641 /* Ditto for dynamic_cast<D*>(&b). */
642 else if (TREE_CODE (expr) == ADDR_EXPR)
644 tree op = TREE_OPERAND (expr, 0);
645 if (TREE_CODE (op) == VAR_DECL
646 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
648 if (complain & tf_warning)
649 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
651 retval = build_int_cst (type, 0);
656 /* Use of dynamic_cast when -fno-rtti is prohibited. */
659 if (complain & tf_error)
660 error ("%<dynamic_cast%> not permitted with -fno-rtti");
661 return error_mark_node;
664 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
665 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
666 td2 = get_tinfo_decl (target_type);
668 td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
669 td3 = get_tinfo_decl (static_type);
671 td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
673 /* Determine how T and V are related. */
674 boff = dcast_base_hint (static_type, target_type);
676 /* Since expr is used twice below, save it. */
677 expr = save_expr (expr);
680 if (tc == REFERENCE_TYPE)
681 expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
688 dcast_fn = dynamic_cast_node;
695 push_abi_namespace ();
696 tinfo_ptr = xref_tag (class_type,
697 get_identifier ("__class_type_info"),
698 /*tag_scope=*/ts_current, false);
700 tinfo_ptr = build_pointer_type
701 (build_qualified_type
702 (tinfo_ptr, TYPE_QUAL_CONST));
703 name = "__dynamic_cast";
705 (NULL_TREE, const_ptr_type_node, tree_cons
706 (NULL_TREE, tinfo_ptr, tree_cons
707 (NULL_TREE, tinfo_ptr, tree_cons
708 (NULL_TREE, ptrdiff_type_node, void_list_node))));
709 tmp = build_function_type (ptr_type_node, tmp);
710 dcast_fn = build_library_fn_ptr (name, tmp);
711 DECL_PURE_P (dcast_fn) = 1;
712 pop_abi_namespace ();
713 dynamic_cast_node = dcast_fn;
715 result = build_cxx_call (dcast_fn, 4, elems);
717 if (tc == REFERENCE_TYPE)
719 tree bad = throw_bad_cast ();
722 result = save_expr (result);
723 neq = c_common_truthvalue_conversion (input_location, result);
724 return cp_convert (type,
725 build3 (COND_EXPR, TREE_TYPE (result),
729 /* Now back to the type we want from a void*. */
730 result = cp_convert (type, result);
731 return ifnonnull (expr, result);
735 errstr = "source type is not polymorphic";
738 if (complain & tf_error)
739 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
740 expr, exprtype, type, errstr);
741 return error_mark_node;
745 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
747 if (type == error_mark_node || expr == error_mark_node)
748 return error_mark_node;
750 if (processing_template_decl)
752 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
753 TREE_SIDE_EFFECTS (expr) = 1;
754 return convert_from_reference (expr);
757 return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
760 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
763 qualifier_flags (tree type)
766 int quals = cp_type_quals (type);
768 if (quals & TYPE_QUAL_CONST)
770 if (quals & TYPE_QUAL_VOLATILE)
772 if (quals & TYPE_QUAL_RESTRICT)
777 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
778 contains a pointer to member of an incomplete class. */
781 target_incomplete_p (tree type)
784 if (TYPE_PTRMEM_P (type))
786 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
788 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
790 else if (TREE_CODE (type) == POINTER_TYPE)
791 type = TREE_TYPE (type);
793 return !COMPLETE_OR_VOID_TYPE_P (type);
796 /* Returns true if TYPE involves an incomplete class type; in that
797 case, typeinfo variables for TYPE should be emitted with internal
801 involves_incomplete_p (tree type)
803 switch (TREE_CODE (type))
806 return target_incomplete_p (TREE_TYPE (type));
811 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
812 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
815 if (TYPE_PTRMEMFUNC_P (type))
819 if (!COMPLETE_TYPE_P (type))
823 /* All other types do not involve incomplete class types. */
828 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
829 is the vtable pointer and NTBS name. The NTBS name is emitted as a
830 comdat const char array, so it becomes a unique key for the type. Generate
831 and emit that VAR_DECL here. (We can't always emit the type_info itself
832 as comdat, because of pointers to incomplete.) */
835 tinfo_base_init (tinfo_s *ti, tree target)
837 tree init = NULL_TREE;
844 /* Generate the NTBS array variable. */
845 tree name_type = build_cplus_array_type
846 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
848 tree name_string = tinfo_name (target);
850 /* Determine the name of the variable -- and remember with which
851 type it is associated. */
852 name_name = mangle_typeinfo_string_for_type (target);
853 TREE_TYPE (name_name) = target;
855 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
856 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
857 DECL_ARTIFICIAL (name_decl) = 1;
858 DECL_IGNORED_P (name_decl) = 1;
859 TREE_READONLY (name_decl) = 1;
860 TREE_STATIC (name_decl) = 1;
861 DECL_EXTERNAL (name_decl) = 0;
862 DECL_TINFO_P (name_decl) = 1;
863 set_linkage_according_to_type (target, name_decl);
864 import_export_decl (name_decl);
865 DECL_INITIAL (name_decl) = name_string;
866 mark_used (name_decl);
867 pushdecl_top_level_and_finish (name_decl, name_string);
870 vtable_ptr = ti->vtable;
874 push_abi_namespace ();
875 real_type = xref_tag (class_type, ti->name,
876 /*tag_scope=*/ts_current, false);
877 pop_abi_namespace ();
879 if (!COMPLETE_TYPE_P (real_type))
881 /* We never saw a definition of this type, so we need to
882 tell the compiler that this is an exported class, as
883 indeed all of the __*_type_info classes are. */
884 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
885 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
888 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
889 vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0,
890 tf_warning_or_error);
892 /* We need to point into the middle of the vtable. */
894 (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
895 size_binop (MULT_EXPR,
896 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
897 TYPE_SIZE_UNIT (vtable_entry_type)));
899 ti->vtable = vtable_ptr;
902 init = tree_cons (NULL_TREE, vtable_ptr, init);
904 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
906 init = build_constructor_from_list (init_list_type_node, nreverse (init));
907 TREE_CONSTANT (init) = 1;
908 TREE_STATIC (init) = 1;
909 init = tree_cons (NULL_TREE, init, NULL_TREE);
914 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
915 information about the particular type_info derivation, which adds no
916 additional fields to the type_info base. */
919 generic_initializer (tinfo_s *ti, tree target)
921 tree init = tinfo_base_init (ti, target);
923 init = build_constructor_from_list (init_list_type_node, init);
924 TREE_CONSTANT (init) = 1;
925 TREE_STATIC (init) = 1;
929 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
930 TI provides information about the particular type_info derivation,
931 which adds target type and qualifier flags members to the type_info base. */
934 ptr_initializer (tinfo_s *ti, tree target)
936 tree init = tinfo_base_init (ti, target);
937 tree to = TREE_TYPE (target);
938 int flags = qualifier_flags (to);
939 bool incomplete = target_incomplete_p (to);
943 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
944 init = tree_cons (NULL_TREE,
945 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
948 init = build_constructor_from_list (init_list_type_node, nreverse (init));
949 TREE_CONSTANT (init) = 1;
950 TREE_STATIC (init) = 1;
954 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
955 TI provides information about the particular type_info derivation,
956 which adds class, target type and qualifier flags members to the type_info
960 ptm_initializer (tinfo_s *ti, tree target)
962 tree init = tinfo_base_init (ti, target);
963 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
964 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
965 int flags = qualifier_flags (to);
966 bool incomplete = target_incomplete_p (to);
970 if (!COMPLETE_TYPE_P (klass))
972 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
973 init = tree_cons (NULL_TREE,
974 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
976 init = tree_cons (NULL_TREE,
977 get_tinfo_ptr (klass),
980 init = build_constructor_from_list (init_list_type_node, nreverse (init));
981 TREE_CONSTANT (init) = 1;
982 TREE_STATIC (init) = 1;
986 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
987 TI provides information about the particular __class_type_info derivation,
988 which adds hint flags and TRAIL initializers to the type_info base. */
991 class_initializer (tinfo_s *ti, tree target, tree trail)
993 tree init = tinfo_base_init (ti, target);
995 TREE_CHAIN (init) = trail;
996 init = build_constructor_from_list (init_list_type_node, init);
997 TREE_CONSTANT (init) = 1;
998 TREE_STATIC (init) = 1;
1002 /* Returns true if the typeinfo for type should be placed in
1003 the runtime library. */
1006 typeinfo_in_lib_p (tree type)
1008 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1009 library for simple types T. */
1010 if (TREE_CODE (type) == POINTER_TYPE
1011 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1012 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1013 type = TREE_TYPE (type);
1015 switch (TREE_CODE (type))
1028 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1029 the index of the descriptor in the tinfo_desc vector. */
1032 get_pseudo_ti_init (tree type, unsigned tk_index)
1034 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1036 gcc_assert (at_eof);
1039 case TK_POINTER_MEMBER_TYPE:
1040 return ptm_initializer (ti, type);
1042 case TK_POINTER_TYPE:
1043 return ptr_initializer (ti, type);
1045 case TK_BUILTIN_TYPE:
1046 case TK_ENUMERAL_TYPE:
1047 case TK_FUNCTION_TYPE:
1049 return generic_initializer (ti, type);
1052 return class_initializer (ti, type, NULL_TREE);
1054 case TK_SI_CLASS_TYPE:
1056 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1057 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1058 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1060 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1061 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1062 return class_initializer (ti, type, base_inits);
1067 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1068 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1069 tree binfo = TYPE_BINFO (type);
1070 int nbases = BINFO_N_BASE_BINFOS (binfo);
1071 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1072 tree offset_type = integer_types[itk_long];
1073 tree base_inits = NULL_TREE;
1076 gcc_assert (tk_index >= TK_FIXED);
1078 /* Generate the base information initializer. */
1079 for (ix = nbases; ix--;)
1081 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1082 tree base_init = NULL_TREE;
1087 if (VEC_index (tree, base_accesses, ix) == access_public_node)
1089 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1090 if (BINFO_VIRTUAL_P (base_binfo))
1092 /* We store the vtable offset at which the virtual
1093 base offset can be found. */
1094 offset = BINFO_VPTR_FIELD (base_binfo);
1098 offset = BINFO_OFFSET (base_binfo);
1100 /* Combine offset and flags into one field. */
1101 offset = fold_convert (offset_type, offset);
1102 offset = fold_build2_loc (input_location,
1103 LSHIFT_EXPR, offset_type, offset,
1104 build_int_cst (offset_type, 8));
1105 offset = fold_build2_loc (input_location,
1106 BIT_IOR_EXPR, offset_type, offset,
1107 build_int_cst (offset_type, flags));
1108 base_init = tree_cons (NULL_TREE, offset, base_init);
1109 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1110 base_init = build_constructor_from_list (init_list_type_node, base_init);
1111 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1113 base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1114 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1115 /* Prepend the number of bases. */
1116 base_inits = tree_cons (NULL_TREE,
1117 build_int_cst (NULL_TREE, nbases),
1119 /* Prepend the hint flags. */
1120 base_inits = tree_cons (NULL_TREE,
1121 build_int_cst (NULL_TREE, hint),
1124 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1125 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1126 return class_initializer (ti, type, base_inits);
1131 /* Generate the RECORD_TYPE containing the data layout of a type_info
1132 derivative as used by the runtime. This layout must be consistent with
1133 that defined in the runtime support. Also generate the VAR_DECL for the
1134 type's vtable. We explicitly manage the vtable member, and name it for
1135 real type as used in the runtime. The RECORD type has a different name,
1136 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1137 is the generated type and TINFO_VTABLE_NAME is the name of the
1138 vtable. We have to delay generating the VAR_DECL of the vtable
1139 until the end of the translation, when we'll have seen the library
1140 definition, if there was one.
1142 REAL_NAME is the runtime's name of the type. Trailing arguments are
1143 additional FIELD_DECL's for the structure. The final argument must be
1147 create_pseudo_type_info (int tk, const char *real_name, ...)
1156 va_start (ap, real_name);
1158 /* Generate the pseudo type name. */
1159 pseudo_name = (char *) alloca (strlen (real_name) + 30);
1160 strcpy (pseudo_name, real_name);
1161 strcat (pseudo_name, "_pseudo");
1163 sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1165 /* First field is the pseudo type_info base class. */
1166 fields = build_decl (input_location,
1167 FIELD_DECL, NULL_TREE,
1168 VEC_index (tinfo_s, tinfo_descs,
1169 TK_TYPE_INFO_TYPE)->type);
1171 /* Now add the derived fields. */
1172 while ((field_decl = va_arg (ap, tree)))
1174 TREE_CHAIN (field_decl) = fields;
1175 fields = field_decl;
1178 /* Create the pseudo type. */
1179 pseudo_type = make_class_type (RECORD_TYPE);
1180 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1181 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1183 ti = VEC_index (tinfo_s, tinfo_descs, tk);
1184 ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1185 ti->name = get_identifier (real_name);
1186 ti->vtable = NULL_TREE;
1188 /* Pretend this is public so determine_visibility doesn't give vtables
1189 internal linkage. */
1190 TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1195 /* Return the index of a pseudo type info type node used to describe
1196 TYPE. TYPE must be a complete type (or cv void), except at the end
1197 of the translation unit. */
1200 get_pseudo_ti_index (tree type)
1204 switch (TREE_CODE (type))
1207 ix = TK_POINTER_MEMBER_TYPE;
1211 ix = TK_POINTER_TYPE;
1215 ix = TK_ENUMERAL_TYPE;
1219 ix = TK_FUNCTION_TYPE;
1228 if (TYPE_PTRMEMFUNC_P (type))
1230 ix = TK_POINTER_MEMBER_TYPE;
1233 else if (!COMPLETE_TYPE_P (type))
1236 cxx_incomplete_type_error (NULL_TREE, type);
1240 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1247 tree binfo = TYPE_BINFO (type);
1248 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1249 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1250 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1253 && VEC_index (tree, base_accesses, 0) == access_public_node
1254 && !BINFO_VIRTUAL_P (base_binfo)
1255 && integer_zerop (BINFO_OFFSET (base_binfo)))
1257 /* single non-virtual public. */
1258 ix = TK_SI_CLASS_TYPE;
1264 tree array_domain, base_array;
1266 ix = TK_FIXED + num_bases;
1267 if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1269 /* too short, extend. */
1270 unsigned len = VEC_length (tinfo_s, tinfo_descs);
1272 VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1273 while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1274 ti->type = ti->vtable = ti->name = NULL_TREE;
1276 else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1277 /* already created. */
1280 /* Create the array of __base_class_type_info entries.
1281 G++ 3.2 allocated an array that had one too many
1282 entries, and then filled that extra entries with
1284 if (abi_version_at_least (2))
1285 array_domain = build_index_type (size_int (num_bases - 1));
1287 array_domain = build_index_type (size_int (num_bases));
1289 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1290 TK_BASE_TYPE)->type,
1293 push_abi_namespace ();
1294 create_pseudo_type_info
1295 (ix, "__vmi_class_type_info",
1296 build_decl (input_location,
1297 FIELD_DECL, NULL_TREE, integer_type_node),
1298 build_decl (input_location,
1299 FIELD_DECL, NULL_TREE, integer_type_node),
1300 build_decl (input_location,
1301 FIELD_DECL, NULL_TREE, base_array),
1303 pop_abi_namespace ();
1308 ix = TK_BUILTIN_TYPE;
1314 /* Make sure the required builtin types exist for generating the type_info
1315 variable definitions. */
1318 create_tinfo_types (void)
1322 gcc_assert (!tinfo_descs);
1324 VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1326 push_abi_namespace ();
1328 /* Create the internal type_info structure. This is used as a base for
1329 the other structures. */
1333 field = build_decl (BUILTINS_LOCATION,
1334 FIELD_DECL, NULL_TREE, const_ptr_type_node);
1337 field = build_decl (BUILTINS_LOCATION,
1338 FIELD_DECL, NULL_TREE, const_string_type_node);
1339 TREE_CHAIN (field) = fields;
1342 ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1343 ti->type = make_class_type (RECORD_TYPE);
1344 ti->vtable = NULL_TREE;
1345 ti->name = NULL_TREE;
1346 finish_builtin_struct (ti->type, "__type_info_pseudo",
1350 /* Fundamental type_info */
1351 create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1353 /* Array, function and enum type_info. No additional fields. */
1354 create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1355 create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1356 create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1358 /* Class type_info. No additional fields. */
1359 create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1361 /* Single public non-virtual base class. Add pointer to base class.
1362 This is really a descendant of __class_type_info. */
1363 create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1364 build_decl (BUILTINS_LOCATION,
1365 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1368 /* Base class internal helper. Pointer to base type, offset to base,
1373 field = build_decl (BUILTINS_LOCATION,
1374 FIELD_DECL, NULL_TREE, type_info_ptr_type);
1377 field = build_decl (BUILTINS_LOCATION,
1378 FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1379 TREE_CHAIN (field) = fields;
1382 ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1384 ti->type = make_class_type (RECORD_TYPE);
1385 ti->vtable = NULL_TREE;
1386 ti->name = NULL_TREE;
1387 finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1391 /* Pointer type_info. Adds two fields, qualification mask
1392 and pointer to the pointed to type. This is really a descendant of
1393 __pbase_type_info. */
1394 create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1395 build_decl (BUILTINS_LOCATION,
1396 FIELD_DECL, NULL_TREE, integer_type_node),
1397 build_decl (BUILTINS_LOCATION,
1398 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1401 /* Pointer to member data type_info. Add qualifications flags,
1402 pointer to the member's type info and pointer to the class.
1403 This is really a descendant of __pbase_type_info. */
1404 create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1405 "__pointer_to_member_type_info",
1406 build_decl (BUILTINS_LOCATION,
1407 FIELD_DECL, NULL_TREE, integer_type_node),
1408 build_decl (BUILTINS_LOCATION,
1409 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1410 build_decl (BUILTINS_LOCATION,
1411 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1414 pop_abi_namespace ();
1417 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1418 support. Generating them here guarantees consistency with the other
1419 structures. We use the following heuristic to determine when the runtime
1420 is being generated. If std::__fundamental_type_info is defined, and its
1421 destructor is defined, then the runtime is being built. */
1424 emit_support_tinfos (void)
1426 static tree *const fundamentals[] =
1430 &wchar_type_node, &char16_type_node, &char32_type_node,
1431 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1432 &short_integer_type_node, &short_unsigned_type_node,
1433 &integer_type_node, &unsigned_type_node,
1434 &long_integer_type_node, &long_unsigned_type_node,
1435 &long_long_integer_type_node, &long_long_unsigned_type_node,
1436 &float_type_node, &double_type_node, &long_double_type_node,
1440 tree bltn_type, dtor;
1442 push_abi_namespace ();
1443 bltn_type = xref_tag (class_type,
1444 get_identifier ("__fundamental_type_info"),
1445 /*tag_scope=*/ts_current, false);
1446 pop_abi_namespace ();
1447 if (!COMPLETE_TYPE_P (bltn_type))
1449 dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1450 if (!dtor || DECL_EXTERNAL (dtor))
1453 for (ix = 0; fundamentals[ix]; ix++)
1455 tree bltn = *fundamentals[ix];
1460 types[1] = build_pointer_type (bltn);
1461 types[2] = build_pointer_type (build_qualified_type (bltn,
1464 for (i = 0; i < 3; ++i)
1468 tinfo = get_tinfo_decl (types[i]);
1469 TREE_USED (tinfo) = 1;
1470 mark_needed (tinfo);
1471 /* The C++ ABI requires that these objects be COMDAT. But,
1472 On systems without weak symbols, initialized COMDAT
1473 objects are emitted with internal linkage. (See
1474 comdat_linkage for details.) Since we want these objects
1475 to have external linkage so that copies do not have to be
1476 emitted in code outside the runtime library, we make them
1479 It might also not be necessary to follow this detail of the
1481 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1483 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1484 DECL_INTERFACE_KNOWN (tinfo) = 1;
1490 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1491 tinfo decl. Determine whether it needs emitting, and if so
1492 generate the initializer. */
1495 emit_tinfo_decl (tree decl)
1497 tree type = TREE_TYPE (DECL_NAME (decl));
1498 int in_library = typeinfo_in_lib_p (type);
1500 gcc_assert (DECL_TINFO_P (decl));
1505 DECL_EXTERNAL (decl) = 0;
1508 /* If we're not in the runtime, then DECL (which is already
1509 DECL_EXTERNAL) will not be defined here. */
1510 DECL_INTERFACE_KNOWN (decl) = 1;
1514 else if (involves_incomplete_p (type))
1516 if (!decl_needed_p (decl))
1518 /* If TYPE involves an incomplete class type, then the typeinfo
1519 object will be emitted with internal linkage. There is no
1520 way to know whether or not types are incomplete until the end
1521 of the compilation, so this determination must be deferred
1522 until this point. */
1523 TREE_PUBLIC (decl) = 0;
1524 DECL_EXTERNAL (decl) = 0;
1525 DECL_INTERFACE_KNOWN (decl) = 1;
1528 import_export_decl (decl);
1529 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1533 DECL_EXTERNAL (decl) = 0;
1534 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1535 DECL_INITIAL (decl) = init;
1537 cp_finish_decl (decl, init, false, NULL_TREE, 0);
1544 #include "gt-cp-rtti.h"