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, bool);
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. If MARK_PRIVATE, put a '*' in front so that
353 comparisons will be done by pointer rather than string comparison. */
355 tinfo_name (tree type, bool mark_private)
361 name = mangle_type_string (type);
362 length = strlen (name);
366 /* Inject '*' at beginning of name to force pointer comparison. */
367 char* buf = (char*) XALLOCAVEC (char, length + 1);
369 memcpy (buf + 1, name, length);
370 name_string = build_string (length + 1, buf);
373 name_string = build_string (length + 1, name);
375 return fix_string_type (name_string);
378 /* Return a VAR_DECL for the internal ABI defined type_info object for
379 TYPE. You must arrange that the decl is mark_used, if actually use
380 it --- decls in vtables are only used if the vtable is output. */
383 get_tinfo_decl (tree type)
388 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
390 error ("cannot create type information for type %qT because "
391 "it involves types of variable size",
393 return error_mark_node;
396 if (TREE_CODE (type) == METHOD_TYPE)
397 type = build_function_type (TREE_TYPE (type),
398 TREE_CHAIN (TYPE_ARG_TYPES (type)));
400 /* For a class type, the variable is cached in the type node
402 if (CLASS_TYPE_P (type))
404 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
409 name = mangle_typeinfo_for_type (type);
411 d = IDENTIFIER_GLOBAL_VALUE (name);
414 int ix = get_pseudo_ti_index (type);
415 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
417 d = build_lang_decl (VAR_DECL, name, ti->type);
418 SET_DECL_ASSEMBLER_NAME (d, name);
419 /* Remember the type it is for. */
420 TREE_TYPE (name) = type;
421 DECL_TINFO_P (d) = 1;
422 DECL_ARTIFICIAL (d) = 1;
423 DECL_IGNORED_P (d) = 1;
424 TREE_READONLY (d) = 1;
426 /* Mark the variable as undefined -- but remember that we can
427 define it later if we need to do so. */
428 DECL_EXTERNAL (d) = 1;
429 DECL_NOT_REALLY_EXTERN (d) = 1;
430 set_linkage_according_to_type (type, d);
432 d = pushdecl_top_level_and_finish (d, NULL_TREE);
433 if (CLASS_TYPE_P (type))
434 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
436 /* Add decl to the global array of tinfo decls. */
437 VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
443 /* Return a pointer to a type_info object describing TYPE, suitably
444 cast to the language defined type. */
447 get_tinfo_ptr (tree type)
449 tree decl = get_tinfo_decl (type);
452 return build_nop (type_info_ptr_type,
453 build_address (decl));
456 /* Return the type_info object for TYPE. */
459 get_typeid (tree type)
461 if (type == error_mark_node || !typeid_ok_p ())
462 return error_mark_node;
464 if (processing_template_decl)
465 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
467 /* If the type of the type-id is a reference type, the result of the
468 typeid expression refers to a type_info object representing the
470 type = non_reference (type);
472 /* The top-level cv-qualifiers of the lvalue expression or the type-id
473 that is the operand of typeid are always ignored. */
474 type = TYPE_MAIN_VARIANT (type);
476 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
477 if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
478 type = complete_type_or_else (type, NULL_TREE);
481 return error_mark_node;
483 return cp_build_indirect_ref (get_tinfo_ptr (type), NULL,
484 tf_warning_or_error);
487 /* Check whether TEST is null before returning RESULT. If TEST is used in
488 RESULT, it must have previously had a save_expr applied to it. */
491 ifnonnull (tree test, tree result)
493 return build3 (COND_EXPR, TREE_TYPE (result),
494 build2 (EQ_EXPR, boolean_type_node, test,
495 cp_convert (TREE_TYPE (test), integer_zero_node)),
496 cp_convert (TREE_TYPE (result), integer_zero_node),
500 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
504 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
506 enum tree_code tc = TREE_CODE (type);
507 tree exprtype = TREE_TYPE (expr);
509 tree old_expr = expr;
510 const char *errstr = NULL;
512 /* Save casted types in the function's used types hash table. */
513 used_types_insert (type);
515 /* T shall be a pointer or reference to a complete class type, or
516 `pointer to cv void''. */
520 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
524 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
526 errstr = "target is not pointer or reference to class";
529 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
531 errstr = "target is not pointer or reference to complete type";
537 errstr = "target is not pointer or reference";
541 if (tc == POINTER_TYPE)
543 /* If T is a pointer type, v shall be an rvalue of a pointer to
544 complete class type, and the result is an rvalue of type T. */
546 if (TREE_CODE (exprtype) != POINTER_TYPE)
548 errstr = "source is not a pointer";
551 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
553 errstr = "source is not a pointer to class";
556 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
558 errstr = "source is a pointer to incomplete type";
564 exprtype = build_reference_type (exprtype);
566 /* T is a reference type, v shall be an lvalue of a complete class
567 type, and the result is an lvalue of the type referred to by T. */
569 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
571 errstr = "source is not of class type";
574 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
576 errstr = "source is of incomplete class type";
580 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
581 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
582 LOOKUP_NORMAL, NULL_TREE);
585 /* The dynamic_cast operator shall not cast away constness. */
586 if (!at_least_as_qualified_p (TREE_TYPE (type),
587 TREE_TYPE (exprtype)))
589 errstr = "conversion casts away constness";
593 /* If *type is an unambiguous accessible base class of *exprtype,
594 convert statically. */
598 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
603 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
605 if (TREE_CODE (exprtype) == POINTER_TYPE)
606 expr = rvalue (expr);
611 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
612 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
615 /* if TYPE is `void *', return pointer to complete object. */
616 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
618 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
619 if (TREE_CODE (expr) == ADDR_EXPR
620 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
621 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
622 return build1 (NOP_EXPR, type, expr);
624 /* Since expr is used twice below, save it. */
625 expr = save_expr (expr);
627 expr1 = build_headof (expr);
628 if (TREE_TYPE (expr1) != type)
629 expr1 = build1 (NOP_EXPR, type, expr1);
630 return ifnonnull (expr, expr1);
635 tree result, td2, td3;
637 tree static_type, target_type, boff;
639 /* If we got here, we can't convert statically. Therefore,
640 dynamic_cast<D&>(b) (b an object) cannot succeed. */
641 if (tc == REFERENCE_TYPE)
643 if (TREE_CODE (old_expr) == VAR_DECL
644 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
646 tree expr = throw_bad_cast ();
647 if (complain & tf_warning)
648 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
650 /* Bash it to the expected type. */
651 TREE_TYPE (expr) = type;
655 /* Ditto for dynamic_cast<D*>(&b). */
656 else if (TREE_CODE (expr) == ADDR_EXPR)
658 tree op = TREE_OPERAND (expr, 0);
659 if (TREE_CODE (op) == VAR_DECL
660 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
662 if (complain & tf_warning)
663 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
665 retval = build_int_cst (type, 0);
670 /* Use of dynamic_cast when -fno-rtti is prohibited. */
673 if (complain & tf_error)
674 error ("%<dynamic_cast%> not permitted with -fno-rtti");
675 return error_mark_node;
678 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
679 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
680 td2 = get_tinfo_decl (target_type);
682 td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
683 td3 = get_tinfo_decl (static_type);
685 td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
687 /* Determine how T and V are related. */
688 boff = dcast_base_hint (static_type, target_type);
690 /* Since expr is used twice below, save it. */
691 expr = save_expr (expr);
694 if (tc == REFERENCE_TYPE)
695 expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
702 dcast_fn = dynamic_cast_node;
709 push_abi_namespace ();
710 tinfo_ptr = xref_tag (class_type,
711 get_identifier ("__class_type_info"),
712 /*tag_scope=*/ts_current, false);
714 tinfo_ptr = build_pointer_type
715 (build_qualified_type
716 (tinfo_ptr, TYPE_QUAL_CONST));
717 name = "__dynamic_cast";
719 (NULL_TREE, const_ptr_type_node, tree_cons
720 (NULL_TREE, tinfo_ptr, tree_cons
721 (NULL_TREE, tinfo_ptr, tree_cons
722 (NULL_TREE, ptrdiff_type_node, void_list_node))));
723 tmp = build_function_type (ptr_type_node, tmp);
724 dcast_fn = build_library_fn_ptr (name, tmp);
725 DECL_PURE_P (dcast_fn) = 1;
726 pop_abi_namespace ();
727 dynamic_cast_node = dcast_fn;
729 result = build_cxx_call (dcast_fn, 4, elems);
731 if (tc == REFERENCE_TYPE)
733 tree bad = throw_bad_cast ();
736 result = save_expr (result);
737 neq = c_common_truthvalue_conversion (input_location, result);
738 return cp_convert (type,
739 build3 (COND_EXPR, TREE_TYPE (result),
743 /* Now back to the type we want from a void*. */
744 result = cp_convert (type, result);
745 return ifnonnull (expr, result);
749 errstr = "source type is not polymorphic";
752 if (complain & tf_error)
753 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
754 expr, exprtype, type, errstr);
755 return error_mark_node;
759 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
761 if (type == error_mark_node || expr == error_mark_node)
762 return error_mark_node;
764 if (processing_template_decl)
766 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
767 TREE_SIDE_EFFECTS (expr) = 1;
768 return convert_from_reference (expr);
771 return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
774 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
777 qualifier_flags (tree type)
780 int quals = cp_type_quals (type);
782 if (quals & TYPE_QUAL_CONST)
784 if (quals & TYPE_QUAL_VOLATILE)
786 if (quals & TYPE_QUAL_RESTRICT)
791 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
792 contains a pointer to member of an incomplete class. */
795 target_incomplete_p (tree type)
798 if (TYPE_PTRMEM_P (type))
800 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
802 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
804 else if (TREE_CODE (type) == POINTER_TYPE)
805 type = TREE_TYPE (type);
807 return !COMPLETE_OR_VOID_TYPE_P (type);
810 /* Returns true if TYPE involves an incomplete class type; in that
811 case, typeinfo variables for TYPE should be emitted with internal
815 involves_incomplete_p (tree type)
817 switch (TREE_CODE (type))
820 return target_incomplete_p (TREE_TYPE (type));
825 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
826 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
829 if (TYPE_PTRMEMFUNC_P (type))
833 if (!COMPLETE_TYPE_P (type))
837 /* All other types do not involve incomplete class types. */
842 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
843 is the vtable pointer and NTBS name. The NTBS name is emitted as a
844 comdat const char array, so it becomes a unique key for the type. Generate
845 and emit that VAR_DECL here. (We can't always emit the type_info itself
846 as comdat, because of pointers to incomplete.) */
849 tinfo_base_init (tinfo_s *ti, tree target)
851 tree init = NULL_TREE;
856 tree name_name, name_string;
858 /* Generate the NTBS array variable. */
859 tree name_type = build_cplus_array_type
860 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
863 /* Determine the name of the variable -- and remember with which
864 type it is associated. */
865 name_name = mangle_typeinfo_string_for_type (target);
866 TREE_TYPE (name_name) = target;
868 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
869 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
870 DECL_ARTIFICIAL (name_decl) = 1;
871 DECL_IGNORED_P (name_decl) = 1;
872 TREE_READONLY (name_decl) = 1;
873 TREE_STATIC (name_decl) = 1;
874 DECL_EXTERNAL (name_decl) = 0;
875 DECL_TINFO_P (name_decl) = 1;
876 set_linkage_according_to_type (target, name_decl);
877 import_export_decl (name_decl);
878 name_string = tinfo_name (target, !TREE_PUBLIC (name_decl));
879 DECL_INITIAL (name_decl) = name_string;
880 mark_used (name_decl);
881 pushdecl_top_level_and_finish (name_decl, name_string);
884 vtable_ptr = ti->vtable;
888 push_abi_namespace ();
889 real_type = xref_tag (class_type, ti->name,
890 /*tag_scope=*/ts_current, false);
891 pop_abi_namespace ();
893 if (!COMPLETE_TYPE_P (real_type))
895 /* We never saw a definition of this type, so we need to
896 tell the compiler that this is an exported class, as
897 indeed all of the __*_type_info classes are. */
898 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
899 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
902 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
903 vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0,
904 tf_warning_or_error);
906 /* We need to point into the middle of the vtable. */
908 (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
909 size_binop (MULT_EXPR,
910 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
911 TYPE_SIZE_UNIT (vtable_entry_type)));
913 ti->vtable = vtable_ptr;
916 init = tree_cons (NULL_TREE, vtable_ptr, init);
918 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
920 init = build_constructor_from_list (init_list_type_node, nreverse (init));
921 TREE_CONSTANT (init) = 1;
922 TREE_STATIC (init) = 1;
923 init = tree_cons (NULL_TREE, init, NULL_TREE);
928 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
929 information about the particular type_info derivation, which adds no
930 additional fields to the type_info base. */
933 generic_initializer (tinfo_s *ti, tree target)
935 tree init = tinfo_base_init (ti, target);
937 init = build_constructor_from_list (init_list_type_node, init);
938 TREE_CONSTANT (init) = 1;
939 TREE_STATIC (init) = 1;
943 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
944 TI provides information about the particular type_info derivation,
945 which adds target type and qualifier flags members to the type_info base. */
948 ptr_initializer (tinfo_s *ti, tree target)
950 tree init = tinfo_base_init (ti, target);
951 tree to = TREE_TYPE (target);
952 int flags = qualifier_flags (to);
953 bool incomplete = target_incomplete_p (to);
957 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
958 init = tree_cons (NULL_TREE,
959 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
962 init = build_constructor_from_list (init_list_type_node, nreverse (init));
963 TREE_CONSTANT (init) = 1;
964 TREE_STATIC (init) = 1;
968 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
969 TI provides information about the particular type_info derivation,
970 which adds class, target type and qualifier flags members to the type_info
974 ptm_initializer (tinfo_s *ti, tree target)
976 tree init = tinfo_base_init (ti, target);
977 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
978 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
979 int flags = qualifier_flags (to);
980 bool incomplete = target_incomplete_p (to);
984 if (!COMPLETE_TYPE_P (klass))
986 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
987 init = tree_cons (NULL_TREE,
988 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
990 init = tree_cons (NULL_TREE,
991 get_tinfo_ptr (klass),
994 init = build_constructor_from_list (init_list_type_node, nreverse (init));
995 TREE_CONSTANT (init) = 1;
996 TREE_STATIC (init) = 1;
1000 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1001 TI provides information about the particular __class_type_info derivation,
1002 which adds hint flags and TRAIL initializers to the type_info base. */
1005 class_initializer (tinfo_s *ti, tree target, tree trail)
1007 tree init = tinfo_base_init (ti, target);
1009 TREE_CHAIN (init) = trail;
1010 init = build_constructor_from_list (init_list_type_node, init);
1011 TREE_CONSTANT (init) = 1;
1012 TREE_STATIC (init) = 1;
1016 /* Returns true if the typeinfo for type should be placed in
1017 the runtime library. */
1020 typeinfo_in_lib_p (tree type)
1022 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1023 library for simple types T. */
1024 if (TREE_CODE (type) == POINTER_TYPE
1025 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1026 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1027 type = TREE_TYPE (type);
1029 switch (TREE_CODE (type))
1042 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1043 the index of the descriptor in the tinfo_desc vector. */
1046 get_pseudo_ti_init (tree type, unsigned tk_index)
1048 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1050 gcc_assert (at_eof);
1053 case TK_POINTER_MEMBER_TYPE:
1054 return ptm_initializer (ti, type);
1056 case TK_POINTER_TYPE:
1057 return ptr_initializer (ti, type);
1059 case TK_BUILTIN_TYPE:
1060 case TK_ENUMERAL_TYPE:
1061 case TK_FUNCTION_TYPE:
1063 return generic_initializer (ti, type);
1066 return class_initializer (ti, type, NULL_TREE);
1068 case TK_SI_CLASS_TYPE:
1070 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1071 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1072 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1074 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1075 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1076 return class_initializer (ti, type, base_inits);
1081 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1082 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1083 tree binfo = TYPE_BINFO (type);
1084 int nbases = BINFO_N_BASE_BINFOS (binfo);
1085 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1086 tree offset_type = integer_types[itk_long];
1087 tree base_inits = NULL_TREE;
1090 gcc_assert (tk_index >= TK_FIXED);
1092 /* Generate the base information initializer. */
1093 for (ix = nbases; ix--;)
1095 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1096 tree base_init = NULL_TREE;
1101 if (VEC_index (tree, base_accesses, ix) == access_public_node)
1103 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1104 if (BINFO_VIRTUAL_P (base_binfo))
1106 /* We store the vtable offset at which the virtual
1107 base offset can be found. */
1108 offset = BINFO_VPTR_FIELD (base_binfo);
1112 offset = BINFO_OFFSET (base_binfo);
1114 /* Combine offset and flags into one field. */
1115 offset = fold_convert (offset_type, offset);
1116 offset = fold_build2_loc (input_location,
1117 LSHIFT_EXPR, offset_type, offset,
1118 build_int_cst (offset_type, 8));
1119 offset = fold_build2_loc (input_location,
1120 BIT_IOR_EXPR, offset_type, offset,
1121 build_int_cst (offset_type, flags));
1122 base_init = tree_cons (NULL_TREE, offset, base_init);
1123 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1124 base_init = build_constructor_from_list (init_list_type_node, base_init);
1125 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1127 base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1128 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1129 /* Prepend the number of bases. */
1130 base_inits = tree_cons (NULL_TREE,
1131 build_int_cst (NULL_TREE, nbases),
1133 /* Prepend the hint flags. */
1134 base_inits = tree_cons (NULL_TREE,
1135 build_int_cst (NULL_TREE, hint),
1138 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1139 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1140 return class_initializer (ti, type, base_inits);
1145 /* Generate the RECORD_TYPE containing the data layout of a type_info
1146 derivative as used by the runtime. This layout must be consistent with
1147 that defined in the runtime support. Also generate the VAR_DECL for the
1148 type's vtable. We explicitly manage the vtable member, and name it for
1149 real type as used in the runtime. The RECORD type has a different name,
1150 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1151 is the generated type and TINFO_VTABLE_NAME is the name of the
1152 vtable. We have to delay generating the VAR_DECL of the vtable
1153 until the end of the translation, when we'll have seen the library
1154 definition, if there was one.
1156 REAL_NAME is the runtime's name of the type. Trailing arguments are
1157 additional FIELD_DECL's for the structure. The final argument must be
1161 create_pseudo_type_info (int tk, const char *real_name, ...)
1170 va_start (ap, real_name);
1172 /* Generate the pseudo type name. */
1173 pseudo_name = (char *) alloca (strlen (real_name) + 30);
1174 strcpy (pseudo_name, real_name);
1175 strcat (pseudo_name, "_pseudo");
1177 sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1179 /* First field is the pseudo type_info base class. */
1180 fields = build_decl (input_location,
1181 FIELD_DECL, NULL_TREE,
1182 VEC_index (tinfo_s, tinfo_descs,
1183 TK_TYPE_INFO_TYPE)->type);
1185 /* Now add the derived fields. */
1186 while ((field_decl = va_arg (ap, tree)))
1188 TREE_CHAIN (field_decl) = fields;
1189 fields = field_decl;
1192 /* Create the pseudo type. */
1193 pseudo_type = make_class_type (RECORD_TYPE);
1194 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1195 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1197 ti = VEC_index (tinfo_s, tinfo_descs, tk);
1198 ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1199 ti->name = get_identifier (real_name);
1200 ti->vtable = NULL_TREE;
1202 /* Pretend this is public so determine_visibility doesn't give vtables
1203 internal linkage. */
1204 TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1209 /* Return the index of a pseudo type info type node used to describe
1210 TYPE. TYPE must be a complete type (or cv void), except at the end
1211 of the translation unit. */
1214 get_pseudo_ti_index (tree type)
1218 switch (TREE_CODE (type))
1221 ix = TK_POINTER_MEMBER_TYPE;
1225 ix = TK_POINTER_TYPE;
1229 ix = TK_ENUMERAL_TYPE;
1233 ix = TK_FUNCTION_TYPE;
1242 if (TYPE_PTRMEMFUNC_P (type))
1244 ix = TK_POINTER_MEMBER_TYPE;
1247 else if (!COMPLETE_TYPE_P (type))
1250 cxx_incomplete_type_error (NULL_TREE, type);
1254 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1261 tree binfo = TYPE_BINFO (type);
1262 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1263 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1264 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1267 && VEC_index (tree, base_accesses, 0) == access_public_node
1268 && !BINFO_VIRTUAL_P (base_binfo)
1269 && integer_zerop (BINFO_OFFSET (base_binfo)))
1271 /* single non-virtual public. */
1272 ix = TK_SI_CLASS_TYPE;
1278 tree array_domain, base_array;
1280 ix = TK_FIXED + num_bases;
1281 if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1283 /* too short, extend. */
1284 unsigned len = VEC_length (tinfo_s, tinfo_descs);
1286 VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1287 while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1288 ti->type = ti->vtable = ti->name = NULL_TREE;
1290 else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1291 /* already created. */
1294 /* Create the array of __base_class_type_info entries.
1295 G++ 3.2 allocated an array that had one too many
1296 entries, and then filled that extra entries with
1298 if (abi_version_at_least (2))
1299 array_domain = build_index_type (size_int (num_bases - 1));
1301 array_domain = build_index_type (size_int (num_bases));
1303 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1304 TK_BASE_TYPE)->type,
1307 push_abi_namespace ();
1308 create_pseudo_type_info
1309 (ix, "__vmi_class_type_info",
1310 build_decl (input_location,
1311 FIELD_DECL, NULL_TREE, integer_type_node),
1312 build_decl (input_location,
1313 FIELD_DECL, NULL_TREE, integer_type_node),
1314 build_decl (input_location,
1315 FIELD_DECL, NULL_TREE, base_array),
1317 pop_abi_namespace ();
1322 ix = TK_BUILTIN_TYPE;
1328 /* Make sure the required builtin types exist for generating the type_info
1329 variable definitions. */
1332 create_tinfo_types (void)
1336 gcc_assert (!tinfo_descs);
1338 VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1340 push_abi_namespace ();
1342 /* Create the internal type_info structure. This is used as a base for
1343 the other structures. */
1347 field = build_decl (BUILTINS_LOCATION,
1348 FIELD_DECL, NULL_TREE, const_ptr_type_node);
1351 field = build_decl (BUILTINS_LOCATION,
1352 FIELD_DECL, NULL_TREE, const_string_type_node);
1353 TREE_CHAIN (field) = fields;
1356 ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1357 ti->type = make_class_type (RECORD_TYPE);
1358 ti->vtable = NULL_TREE;
1359 ti->name = NULL_TREE;
1360 finish_builtin_struct (ti->type, "__type_info_pseudo",
1364 /* Fundamental type_info */
1365 create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1367 /* Array, function and enum type_info. No additional fields. */
1368 create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1369 create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1370 create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1372 /* Class type_info. No additional fields. */
1373 create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1375 /* Single public non-virtual base class. Add pointer to base class.
1376 This is really a descendant of __class_type_info. */
1377 create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1378 build_decl (BUILTINS_LOCATION,
1379 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1382 /* Base class internal helper. Pointer to base type, offset to base,
1387 field = build_decl (BUILTINS_LOCATION,
1388 FIELD_DECL, NULL_TREE, type_info_ptr_type);
1391 field = build_decl (BUILTINS_LOCATION,
1392 FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1393 TREE_CHAIN (field) = fields;
1396 ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1398 ti->type = make_class_type (RECORD_TYPE);
1399 ti->vtable = NULL_TREE;
1400 ti->name = NULL_TREE;
1401 finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1405 /* Pointer type_info. Adds two fields, qualification mask
1406 and pointer to the pointed to type. This is really a descendant of
1407 __pbase_type_info. */
1408 create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1409 build_decl (BUILTINS_LOCATION,
1410 FIELD_DECL, NULL_TREE, integer_type_node),
1411 build_decl (BUILTINS_LOCATION,
1412 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1415 /* Pointer to member data type_info. Add qualifications flags,
1416 pointer to the member's type info and pointer to the class.
1417 This is really a descendant of __pbase_type_info. */
1418 create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1419 "__pointer_to_member_type_info",
1420 build_decl (BUILTINS_LOCATION,
1421 FIELD_DECL, NULL_TREE, integer_type_node),
1422 build_decl (BUILTINS_LOCATION,
1423 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1424 build_decl (BUILTINS_LOCATION,
1425 FIELD_DECL, NULL_TREE, type_info_ptr_type),
1428 pop_abi_namespace ();
1431 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1432 support. Generating them here guarantees consistency with the other
1433 structures. We use the following heuristic to determine when the runtime
1434 is being generated. If std::__fundamental_type_info is defined, and its
1435 destructor is defined, then the runtime is being built. */
1438 emit_support_tinfos (void)
1440 static tree *const fundamentals[] =
1444 &wchar_type_node, &char16_type_node, &char32_type_node,
1445 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1446 &short_integer_type_node, &short_unsigned_type_node,
1447 &integer_type_node, &unsigned_type_node,
1448 &long_integer_type_node, &long_unsigned_type_node,
1449 &long_long_integer_type_node, &long_long_unsigned_type_node,
1450 &float_type_node, &double_type_node, &long_double_type_node,
1454 tree bltn_type, dtor;
1456 push_abi_namespace ();
1457 bltn_type = xref_tag (class_type,
1458 get_identifier ("__fundamental_type_info"),
1459 /*tag_scope=*/ts_current, false);
1460 pop_abi_namespace ();
1461 if (!COMPLETE_TYPE_P (bltn_type))
1463 dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1464 if (!dtor || DECL_EXTERNAL (dtor))
1467 for (ix = 0; fundamentals[ix]; ix++)
1469 tree bltn = *fundamentals[ix];
1474 types[1] = build_pointer_type (bltn);
1475 types[2] = build_pointer_type (build_qualified_type (bltn,
1478 for (i = 0; i < 3; ++i)
1482 tinfo = get_tinfo_decl (types[i]);
1483 TREE_USED (tinfo) = 1;
1484 mark_needed (tinfo);
1485 /* The C++ ABI requires that these objects be COMDAT. But,
1486 On systems without weak symbols, initialized COMDAT
1487 objects are emitted with internal linkage. (See
1488 comdat_linkage for details.) Since we want these objects
1489 to have external linkage so that copies do not have to be
1490 emitted in code outside the runtime library, we make them
1493 It might also not be necessary to follow this detail of the
1495 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1497 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1498 DECL_INTERFACE_KNOWN (tinfo) = 1;
1504 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1505 tinfo decl. Determine whether it needs emitting, and if so
1506 generate the initializer. */
1509 emit_tinfo_decl (tree decl)
1511 tree type = TREE_TYPE (DECL_NAME (decl));
1512 int in_library = typeinfo_in_lib_p (type);
1514 gcc_assert (DECL_TINFO_P (decl));
1519 DECL_EXTERNAL (decl) = 0;
1522 /* If we're not in the runtime, then DECL (which is already
1523 DECL_EXTERNAL) will not be defined here. */
1524 DECL_INTERFACE_KNOWN (decl) = 1;
1528 else if (involves_incomplete_p (type))
1530 if (!decl_needed_p (decl))
1532 /* If TYPE involves an incomplete class type, then the typeinfo
1533 object will be emitted with internal linkage. There is no
1534 way to know whether or not types are incomplete until the end
1535 of the compilation, so this determination must be deferred
1536 until this point. */
1537 TREE_PUBLIC (decl) = 0;
1538 DECL_EXTERNAL (decl) = 0;
1539 DECL_INTERFACE_KNOWN (decl) = 1;
1542 import_export_decl (decl);
1543 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1547 DECL_EXTERNAL (decl) = 0;
1548 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1549 DECL_INITIAL (decl) = init;
1551 cp_finish_decl (decl, init, false, NULL_TREE, 0);
1558 #include "gt-cp-rtti.h"