1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Mostly written by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "coretypes.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 /* Accessors for the type_info objects. We need to remember several things
65 about each of the type_info types. The global tree nodes such as
66 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
67 the required information. */
68 /* The RECORD_TYPE of a type_info derived class. */
69 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
70 /* The VAR_DECL of the vtable for the type_info derived class.
71 This is only filled in at the end of the translation. */
72 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
73 /* The IDENTIFIER_NODE naming the real class. */
74 #define TINFO_REAL_NAME(NODE) TREE_PURPOSE (NODE)
76 /* A varray of all tinfo decls that haven't yet been emitted. */
77 varray_type unemitted_tinfo_decls;
79 static tree build_headof (tree);
80 static tree ifnonnull (tree, tree);
81 static tree tinfo_name (tree);
82 static tree build_dynamic_cast_1 (tree, tree);
83 static tree throw_bad_cast (void);
84 static tree throw_bad_typeid (void);
85 static tree get_tinfo_decl_dynamic (tree);
86 static tree get_tinfo_ptr (tree);
87 static bool typeid_ok_p (void);
88 static int qualifier_flags (tree);
89 static bool target_incomplete_p (tree);
90 static tree tinfo_base_init (tree, tree);
91 static tree generic_initializer (tree, tree);
92 static tree dfs_class_hint_mark (tree, void *);
93 static tree dfs_class_hint_unmark (tree, void *);
94 static int class_hint_flags (tree);
95 static tree class_initializer (tree, tree, tree);
96 static tree create_pseudo_type_info (const char *, int, ...);
97 static tree get_pseudo_ti_init (tree, tree);
98 static tree get_pseudo_ti_desc (tree);
99 static void create_tinfo_types (void);
100 static bool typeinfo_in_lib_p (tree);
102 static int doing_runtime = 0;
105 /* Declare language defined type_info type and a pointer to const
106 type_info. This is incomplete here, and will be completed when
107 the user #includes <typeinfo>. There are language defined
108 restrictions on what can be done until that is included. Create
109 the internal versions of the ABI types. */
112 init_rtti_processing (void)
114 tree const_type_info_type;
116 push_namespace (std_identifier);
118 = xref_tag (class_type, get_identifier ("type_info"),
121 const_type_info_type = build_qualified_type (type_info_type_node,
123 type_info_ptr_type = build_pointer_type (const_type_info_type);
124 type_info_ref_type = build_reference_type (const_type_info_type);
126 VARRAY_TREE_INIT (unemitted_tinfo_decls, 10, "RTTI decls");
128 create_tinfo_types ();
131 /* Given the expression EXP of type `class *', return the head of the
132 object pointed to by EXP with type cv void*, if the class has any
133 virtual functions (TYPE_POLYMORPHIC_P), else just return the
137 build_headof (tree exp)
139 tree type = TREE_TYPE (exp);
143 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
144 type = TREE_TYPE (type);
146 if (!TYPE_POLYMORPHIC_P (type))
149 /* We use this a couple of times below, protect it. */
150 exp = save_expr (exp);
152 /* The offset-to-top field is at index -2 from the vptr. */
153 index = build_int_cst (NULL_TREE,
154 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE, -1);
156 offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
158 type = build_qualified_type (ptr_type_node,
159 cp_type_quals (TREE_TYPE (exp)));
160 return build (PLUS_EXPR, type, exp,
161 convert_to_integer (ptrdiff_type_node, offset));
164 /* Get a bad_cast node for the program to throw...
166 See libstdc++/exception.cc for __throw_bad_cast */
169 throw_bad_cast (void)
171 tree fn = get_identifier ("__cxa_bad_cast");
172 if (!get_global_value_if_present (fn, &fn))
173 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
176 return build_cxx_call (fn, NULL_TREE);
179 /* Return an expression for "__cxa_bad_typeid()". The expression
180 returned is an lvalue of type "const std::type_info". */
183 throw_bad_typeid (void)
185 tree fn = get_identifier ("__cxa_bad_typeid");
186 if (!get_global_value_if_present (fn, &fn))
188 tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
189 t = build_function_type (build_reference_type (t), void_list_node);
190 fn = push_throw_library_fn (fn, t);
193 return convert_from_reference (build_cxx_call (fn, NULL_TREE));
196 /* Return an lvalue expression whose type is "const std::type_info"
197 and whose value indicates the type of the expression EXP. If EXP
198 is a reference to a polymorphic class, return the dynamic type;
199 otherwise return the static type of the expression. */
202 get_tinfo_decl_dynamic (tree exp)
207 if (exp == error_mark_node)
208 return error_mark_node;
210 /* peel back references, so they match. */
211 type = non_reference (TREE_TYPE (exp));
213 /* Peel off cv qualifiers. */
214 type = TYPE_MAIN_VARIANT (type);
216 if (!VOID_TYPE_P (type))
217 type = complete_type_or_else (type, exp);
220 return error_mark_node;
222 /* If exp is a reference to polymorphic type, get the real type_info. */
223 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
225 /* build reference to type_info from vtable. */
228 /* The RTTI information is at index -1. */
229 index = build_int_cst (NULL_TREE,
230 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE, -1);
231 t = build_vtbl_ref (exp, index);
232 t = convert (type_info_ptr_type, t);
235 /* Otherwise return the type_info for the static type of the expr. */
236 t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
238 return build_indirect_ref (t, NULL);
246 error ("cannot use typeid with -fno-rtti");
250 if (!COMPLETE_TYPE_P (type_info_type_node))
252 error ("must #include <typeinfo> before using typeid");
259 /* Return an expression for "typeid(EXP)". The expression returned is
260 an lvalue of type "const std::type_info". */
263 build_typeid (tree exp)
265 tree cond = NULL_TREE;
268 if (exp == error_mark_node || !typeid_ok_p ())
269 return error_mark_node;
271 if (processing_template_decl)
272 return build_min (TYPEID_EXPR, type_info_ref_type, exp);
274 if (TREE_CODE (exp) == INDIRECT_REF
275 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
276 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
277 && ! resolves_to_fixed_type_p (exp, &nonnull)
280 exp = stabilize_reference (exp);
281 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
284 exp = get_tinfo_decl_dynamic (exp);
286 if (exp == error_mark_node)
287 return error_mark_node;
291 tree bad = throw_bad_typeid ();
293 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
299 /* Generate the NTBS name of a type. */
301 tinfo_name (tree type)
306 name = mangle_type_string (type);
307 name_string = fix_string_type (build_string (strlen (name) + 1, name));
311 /* Return a VAR_DECL for the internal ABI defined type_info object for
312 TYPE. You must arrange that the decl is mark_used, if actually use
313 it --- decls in vtables are only used if the vtable is output. */
316 get_tinfo_decl (tree type)
321 if (COMPLETE_TYPE_P (type)
322 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
324 error ("cannot create type information for type `%T' because its size is variable",
326 return error_mark_node;
329 if (TREE_CODE (type) == METHOD_TYPE)
330 type = build_function_type (TREE_TYPE (type),
331 TREE_CHAIN (TYPE_ARG_TYPES (type)));
333 /* For a class type, the variable is cached in the type node
335 if (CLASS_TYPE_P (type))
337 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
342 name = mangle_typeinfo_for_type (type);
344 d = IDENTIFIER_GLOBAL_VALUE (name);
347 tree var_desc = get_pseudo_ti_desc (type);
349 d = build_lang_decl (VAR_DECL, name, TINFO_PSEUDO_TYPE (var_desc));
350 SET_DECL_ASSEMBLER_NAME (d, name);
351 /* Remember the type it is for. */
352 TREE_TYPE (name) = type;
353 DECL_TINFO_P (d) = 1;
354 DECL_ARTIFICIAL (d) = 1;
355 TREE_READONLY (d) = 1;
357 /* Mark the variable as undefined -- but remember that we can
358 define it later if we need to do so. */
359 DECL_EXTERNAL (d) = 1;
360 DECL_NOT_REALLY_EXTERN (d) = 1;
361 if (CLASS_TYPE_P (type))
362 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
363 set_linkage_according_to_type (type, d);
364 pushdecl_top_level_and_finish (d, NULL_TREE);
366 /* Add decl to the global array of tinfo decls. */
367 my_friendly_assert (unemitted_tinfo_decls != 0, 20030312);
368 VARRAY_PUSH_TREE (unemitted_tinfo_decls, d);
374 /* Return a pointer to a type_info object describing TYPE, suitably
375 cast to the language defined type. */
378 get_tinfo_ptr (tree type)
380 tree decl = get_tinfo_decl (type);
383 return build_nop (type_info_ptr_type,
384 build_address (decl));
387 /* Return the type_info object for TYPE. */
390 get_typeid (tree type)
392 if (type == error_mark_node || !typeid_ok_p ())
393 return error_mark_node;
395 if (processing_template_decl)
396 return build_min (TYPEID_EXPR, type_info_ref_type, type);
398 /* If the type of the type-id is a reference type, the result of the
399 typeid expression refers to a type_info object representing the
401 type = non_reference (type);
403 /* The top-level cv-qualifiers of the lvalue expression or the type-id
404 that is the operand of typeid are always ignored. */
405 type = TYPE_MAIN_VARIANT (type);
407 if (!VOID_TYPE_P (type))
408 type = complete_type_or_else (type, NULL_TREE);
411 return error_mark_node;
413 return build_indirect_ref (get_tinfo_ptr (type), NULL);
416 /* Check whether TEST is null before returning RESULT. If TEST is used in
417 RESULT, it must have previously had a save_expr applied to it. */
420 ifnonnull (tree test, tree result)
422 return build (COND_EXPR, TREE_TYPE (result),
423 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
424 cp_convert (TREE_TYPE (result), integer_zero_node),
428 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
432 build_dynamic_cast_1 (tree type, tree expr)
434 enum tree_code tc = TREE_CODE (type);
435 tree exprtype = TREE_TYPE (expr);
437 tree old_expr = expr;
438 const char *errstr = NULL;
440 /* T shall be a pointer or reference to a complete class type, or
441 `pointer to cv void''. */
445 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
448 if (! IS_AGGR_TYPE (TREE_TYPE (type)))
450 errstr = "target is not pointer or reference to class";
453 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
455 errstr = "target is not pointer or reference to complete type";
461 errstr = "target is not pointer or reference";
465 if (tc == POINTER_TYPE)
466 expr = convert_from_reference (expr);
467 else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
469 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
470 exprtype = build_reference_type (exprtype);
471 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
472 LOOKUP_NORMAL, NULL_TREE);
475 exprtype = TREE_TYPE (expr);
477 if (tc == POINTER_TYPE)
479 /* If T is a pointer type, v shall be an rvalue of a pointer to
480 complete class type, and the result is an rvalue of type T. */
482 if (TREE_CODE (exprtype) != POINTER_TYPE)
484 errstr = "source is not a pointer";
487 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
489 errstr = "source is not a pointer to class";
492 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
494 errstr = "source is a pointer to incomplete type";
500 /* T is a reference type, v shall be an lvalue of a complete class
501 type, and the result is an lvalue of the type referred to by T. */
503 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
505 errstr = "source is not of class type";
508 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
510 errstr = "source is of incomplete class type";
516 /* The dynamic_cast operator shall not cast away constness. */
517 if (!at_least_as_qualified_p (TREE_TYPE (type),
518 TREE_TYPE (exprtype)))
520 errstr = "conversion casts away constness";
524 /* If *type is an unambiguous accessible base class of *exprtype,
525 convert statically. */
529 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
530 ba_not_special, NULL);
534 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
536 if (TREE_CODE (exprtype) == POINTER_TYPE)
537 expr = non_lvalue (expr);
542 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
543 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
546 /* if TYPE is `void *', return pointer to complete object. */
547 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
549 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
550 if (TREE_CODE (expr) == ADDR_EXPR
551 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
552 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
553 return build1 (NOP_EXPR, type, expr);
555 /* Since expr is used twice below, save it. */
556 expr = save_expr (expr);
558 expr1 = build_headof (expr);
559 if (TREE_TYPE (expr1) != type)
560 expr1 = build1 (NOP_EXPR, type, expr1);
561 return ifnonnull (expr, expr1);
566 tree result, td2, td3, elems;
567 tree static_type, target_type, boff;
569 /* If we got here, we can't convert statically. Therefore,
570 dynamic_cast<D&>(b) (b an object) cannot succeed. */
571 if (tc == REFERENCE_TYPE)
573 if (TREE_CODE (old_expr) == VAR_DECL
574 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
576 tree expr = throw_bad_cast ();
577 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
579 /* Bash it to the expected type. */
580 TREE_TYPE (expr) = type;
584 /* Ditto for dynamic_cast<D*>(&b). */
585 else if (TREE_CODE (expr) == ADDR_EXPR)
587 tree op = TREE_OPERAND (expr, 0);
588 if (TREE_CODE (op) == VAR_DECL
589 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
591 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
593 retval = build_int_cst (type, 0, 0);
598 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
599 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
600 td2 = get_tinfo_decl (target_type);
602 td2 = build_unary_op (ADDR_EXPR, td2, 0);
603 td3 = get_tinfo_decl (static_type);
605 td3 = build_unary_op (ADDR_EXPR, td3, 0);
607 /* Determine how T and V are related. */
608 boff = get_dynamic_cast_base_type (static_type, target_type);
610 /* Since expr is used twice below, save it. */
611 expr = save_expr (expr);
614 if (tc == REFERENCE_TYPE)
615 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
618 (NULL_TREE, expr1, tree_cons
619 (NULL_TREE, td3, tree_cons
620 (NULL_TREE, td2, tree_cons
621 (NULL_TREE, boff, NULL_TREE))));
623 dcast_fn = dynamic_cast_node;
631 push_nested_namespace (ns);
632 tinfo_ptr = xref_tag (class_type,
633 get_identifier ("__class_type_info"),
636 tinfo_ptr = build_pointer_type
637 (build_qualified_type
638 (tinfo_ptr, TYPE_QUAL_CONST));
639 name = "__dynamic_cast";
641 (NULL_TREE, const_ptr_type_node, tree_cons
642 (NULL_TREE, tinfo_ptr, tree_cons
643 (NULL_TREE, tinfo_ptr, tree_cons
644 (NULL_TREE, ptrdiff_type_node, void_list_node))));
645 tmp = build_function_type (ptr_type_node, tmp);
646 dcast_fn = build_library_fn_ptr (name, tmp);
647 DECL_IS_PURE (dcast_fn) = 1;
648 pop_nested_namespace (ns);
649 dynamic_cast_node = dcast_fn;
651 result = build_cxx_call (dcast_fn, elems);
653 if (tc == REFERENCE_TYPE)
655 tree bad = throw_bad_cast ();
657 result = save_expr (result);
658 return build (COND_EXPR, type, result, result, bad);
661 /* Now back to the type we want from a void*. */
662 result = cp_convert (type, result);
663 return ifnonnull (expr, result);
667 errstr = "source type is not polymorphic";
670 error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
671 expr, exprtype, type, errstr);
672 return error_mark_node;
676 build_dynamic_cast (tree type, tree expr)
678 if (type == error_mark_node || expr == error_mark_node)
679 return error_mark_node;
681 if (processing_template_decl)
683 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
684 TREE_SIDE_EFFECTS (expr) = 1;
689 return convert_from_reference (build_dynamic_cast_1 (type, expr));
692 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
695 qualifier_flags (tree type)
698 int quals = cp_type_quals (type);
700 if (quals & TYPE_QUAL_CONST)
702 if (quals & TYPE_QUAL_VOLATILE)
704 if (quals & TYPE_QUAL_RESTRICT)
709 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
710 contains a pointer to member of an incomplete class. */
713 target_incomplete_p (tree type)
716 if (TYPE_PTRMEM_P (type))
718 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
720 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
722 else if (TREE_CODE (type) == POINTER_TYPE)
723 type = TREE_TYPE (type);
725 return !COMPLETE_OR_VOID_TYPE_P (type);
728 /* Returns true if TYPE involves an incomplete class type; in that
729 case, typeinfo variables for TYPE should be emitted with internal
733 involves_incomplete_p (tree type)
735 switch (TREE_CODE (type))
738 return target_incomplete_p (TREE_TYPE (type));
743 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
744 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
747 if (TYPE_PTRMEMFUNC_P (type))
751 if (!COMPLETE_TYPE_P (type))
755 /* All other types do not involve incomplete class types. */
760 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
761 is the vtable pointer and NTBS name. The NTBS name is emitted as a
762 comdat const char array, so it becomes a unique key for the type. Generate
763 and emit that VAR_DECL here. (We can't always emit the type_info itself
764 as comdat, because of pointers to incomplete.) */
767 tinfo_base_init (tree desc, tree target)
769 tree init = NULL_TREE;
776 /* Generate the NTBS array variable. */
777 tree name_type = build_cplus_array_type
778 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
780 tree name_string = tinfo_name (target);
782 /* Determine the name of the variable -- and remember with which
783 type it is associated. */
784 name_name = mangle_typeinfo_string_for_type (target);
785 TREE_TYPE (name_name) = target;
787 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
788 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
789 DECL_ARTIFICIAL (name_decl) = 1;
790 TREE_READONLY (name_decl) = 1;
791 TREE_STATIC (name_decl) = 1;
792 DECL_EXTERNAL (name_decl) = 0;
793 DECL_TINFO_P (name_decl) = 1;
794 if (involves_incomplete_p (target))
796 TREE_PUBLIC (name_decl) = 0;
797 DECL_INTERFACE_KNOWN (name_decl) = 1;
800 set_linkage_according_to_type (target, name_decl);
801 import_export_decl (name_decl);
802 DECL_INITIAL (name_decl) = name_string;
803 mark_used (name_decl);
804 pushdecl_top_level_and_finish (name_decl, name_string);
807 vtable_ptr = TINFO_VTABLE_DECL (desc);
812 push_nested_namespace (abi_node);
813 real_type = xref_tag (class_type, TINFO_REAL_NAME (desc),
815 pop_nested_namespace (abi_node);
817 if (!COMPLETE_TYPE_P (real_type))
819 /* We never saw a definition of this type, so we need to
820 tell the compiler that this is an exported class, as
821 indeed all of the __*_type_info classes are. */
822 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
823 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
826 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
827 vtable_ptr = build_unary_op (ADDR_EXPR, vtable_ptr, 0);
829 /* We need to point into the middle of the vtable. */
831 (PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
832 size_binop (MULT_EXPR,
833 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
834 TYPE_SIZE_UNIT (vtable_entry_type)));
836 TINFO_VTABLE_DECL (desc) = vtable_ptr;
839 init = tree_cons (NULL_TREE, vtable_ptr, init);
841 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
843 init = build_constructor (NULL_TREE, nreverse (init));
844 TREE_CONSTANT (init) = 1;
845 TREE_INVARIANT (init) = 1;
846 TREE_STATIC (init) = 1;
847 init = tree_cons (NULL_TREE, init, NULL_TREE);
852 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
853 information about the particular type_info derivation, which adds no
854 additional fields to the type_info base. */
857 generic_initializer (tree desc, tree target)
859 tree init = tinfo_base_init (desc, target);
861 init = build_constructor (NULL_TREE, init);
862 TREE_CONSTANT (init) = 1;
863 TREE_INVARIANT (init) = 1;
864 TREE_STATIC (init) = 1;
868 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
869 DESC provides information about the particular type_info derivation,
870 which adds target type and qualifier flags members to the type_info base. */
873 ptr_initializer (tree desc, tree target)
875 tree init = tinfo_base_init (desc, target);
876 tree to = TREE_TYPE (target);
877 int flags = qualifier_flags (to);
878 bool incomplete = target_incomplete_p (to);
882 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags, 0), init);
883 init = tree_cons (NULL_TREE,
884 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
887 init = build_constructor (NULL_TREE, nreverse (init));
888 TREE_CONSTANT (init) = 1;
889 TREE_INVARIANT (init) = 1;
890 TREE_STATIC (init) = 1;
894 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
895 DESC provides information about the particular type_info derivation,
896 which adds class, target type and qualifier flags members to the type_info
900 ptm_initializer (tree desc, tree target)
902 tree init = tinfo_base_init (desc, target);
903 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
904 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
905 int flags = qualifier_flags (to);
906 bool incomplete = target_incomplete_p (to);
910 if (!COMPLETE_TYPE_P (klass))
912 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags, 0), init);
913 init = tree_cons (NULL_TREE,
914 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
916 init = tree_cons (NULL_TREE,
917 get_tinfo_ptr (klass),
920 init = build_constructor (NULL_TREE, nreverse (init));
921 TREE_CONSTANT (init) = 1;
922 TREE_INVARIANT (init) = 1;
923 TREE_STATIC (init) = 1;
927 /* Check base BINFO to set hint flags in *DATA, which is really an int.
928 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
929 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
930 possible for a type to be both a virtual and non-virtual base. */
933 dfs_class_hint_mark (tree binfo, void *data)
935 tree basetype = BINFO_TYPE (binfo);
936 int *hint = (int *) data;
938 if (BINFO_VIRTUAL_P (binfo))
940 if (CLASSTYPE_MARKED (basetype))
942 if (CLASSTYPE_MARKED2 (basetype))
944 SET_CLASSTYPE_MARKED2 (basetype);
948 if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
950 SET_CLASSTYPE_MARKED (basetype);
955 /* Clear the base's dfs marks, after searching for duplicate bases. */
958 dfs_class_hint_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
960 tree basetype = BINFO_TYPE (binfo);
962 CLEAR_CLASSTYPE_MARKED (basetype);
963 CLEAR_CLASSTYPE_MARKED2 (basetype);
967 /* Determine the hint flags describing the features of a class's hierarchy. */
970 class_hint_flags (tree type)
974 dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
975 dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
980 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
981 DESC provides information about the particular __class_type_info derivation,
982 which adds hint flags and TRAIL initializers to the type_info base. */
985 class_initializer (tree desc, tree target, tree trail)
987 tree init = tinfo_base_init (desc, target);
989 TREE_CHAIN (init) = trail;
990 init = build_constructor (NULL_TREE, init);
991 TREE_CONSTANT (init) = 1;
992 TREE_INVARIANT (init) = 1;
993 TREE_STATIC (init) = 1;
997 /* Returns true if the typeinfo for type should be placed in
998 the runtime library. */
1001 typeinfo_in_lib_p (tree type)
1003 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1004 library for simple types T. */
1005 if (TREE_CODE (type) == POINTER_TYPE
1006 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1007 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1008 type = TREE_TYPE (type);
1010 switch (TREE_CODE (type))
1024 /* Generate the initializer for the type info describing TYPE. */
1027 get_pseudo_ti_init (tree type, tree var_desc)
1029 my_friendly_assert (at_eof, 20021120);
1030 switch (TREE_CODE (type))
1033 return ptm_initializer (var_desc, type);
1035 return ptr_initializer (var_desc, type);
1037 return generic_initializer (var_desc, type);
1040 return generic_initializer (var_desc, type);
1043 return generic_initializer (var_desc, type);
1047 if (TYPE_PTRMEMFUNC_P (type))
1048 return ptm_initializer (var_desc, type);
1049 else if (var_desc == class_desc_type_node)
1050 return class_initializer (var_desc, type, NULL_TREE);
1051 else if (var_desc == si_class_desc_type_node)
1053 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1054 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1055 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1057 return class_initializer (var_desc, type, base_inits);
1061 int hint = class_hint_flags (type);
1062 tree binfo = TYPE_BINFO (type);
1063 int nbases = BINFO_N_BASE_BINFOS (binfo);
1064 VEC (tree) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1065 tree base_inits = NULL_TREE;
1068 /* Generate the base information initializer. */
1069 for (ix = nbases; ix--;)
1071 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1072 tree base_init = NULL_TREE;
1077 if (VEC_index (tree, base_accesses, ix) == access_public_node)
1079 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1080 if (BINFO_VIRTUAL_P (base_binfo))
1082 /* We store the vtable offset at which the virtual
1083 base offset can be found. */
1084 offset = BINFO_VPTR_FIELD (base_binfo);
1085 offset = convert (sizetype, offset);
1089 offset = BINFO_OFFSET (base_binfo);
1091 /* Combine offset and flags into one field. */
1092 offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1093 build_int_cst (NULL_TREE, 8, 0));
1094 offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1095 build_int_cst (NULL_TREE,
1097 base_init = tree_cons (NULL_TREE, offset, base_init);
1098 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1099 base_init = build_constructor (NULL_TREE, base_init);
1100 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1102 base_inits = build_constructor (NULL_TREE, base_inits);
1103 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1104 /* Prepend the number of bases. */
1105 base_inits = tree_cons (NULL_TREE,
1106 build_int_cst (NULL_TREE, nbases, 0),
1108 /* Prepend the hint flags. */
1109 base_inits = tree_cons (NULL_TREE,
1110 build_int_cst (NULL_TREE, hint, 0),
1113 return class_initializer (var_desc, type, base_inits);
1118 return generic_initializer (var_desc, type);
1122 /* Generate the RECORD_TYPE containing the data layout of a type_info
1123 derivative as used by the runtime. This layout must be consistent with
1124 that defined in the runtime support. Also generate the VAR_DECL for the
1125 type's vtable. We explicitly manage the vtable member, and name it for
1126 real type as used in the runtime. The RECORD type has a different name,
1127 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1128 is the generated type and TINFO_VTABLE_NAME is the name of the
1129 vtable. We have to delay generating the VAR_DECL of the vtable
1130 until the end of the translation, when we'll have seen the library
1131 definition, if there was one.
1133 REAL_NAME is the runtime's name of the type. Trailing arguments are
1134 additional FIELD_DECL's for the structure. The final argument must be
1138 create_pseudo_type_info (const char *real_name, int ident, ...)
1147 va_start (ap, ident);
1149 /* Generate the pseudo type name. */
1150 pseudo_name = alloca (strlen (real_name) + 30);
1151 strcpy (pseudo_name, real_name);
1152 strcat (pseudo_name, "_pseudo");
1154 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1156 /* First field is the pseudo type_info base class. */
1157 fields = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1159 /* Now add the derived fields. */
1160 while ((field_decl = va_arg (ap, tree)))
1162 TREE_CHAIN (field_decl) = fields;
1163 fields = field_decl;
1166 /* Create the pseudo type. */
1167 pseudo_type = make_aggr_type (RECORD_TYPE);
1168 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1169 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1171 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1172 TINFO_REAL_NAME (result) = get_identifier (real_name);
1173 TINFO_PSEUDO_TYPE (result) =
1174 cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1180 /* Return a pseudo type info type node used to describe TYPE. TYPE
1181 must be a complete type (or cv void), except at the end of the
1182 translation unit. */
1185 get_pseudo_ti_desc (tree type)
1187 switch (TREE_CODE (type))
1190 return ptm_desc_type_node;
1192 return ptr_desc_type_node;
1194 return enum_desc_type_node;
1196 return func_desc_type_node;
1198 return ary_desc_type_node;
1201 if (TYPE_PTRMEMFUNC_P (type))
1202 return ptm_desc_type_node;
1203 else if (!COMPLETE_TYPE_P (type))
1206 cxx_incomplete_type_error (NULL_TREE, type);
1207 return class_desc_type_node;
1209 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1210 return class_desc_type_node;
1213 tree binfo = TYPE_BINFO (type);
1214 VEC (tree) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1215 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1216 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1219 && VEC_index (tree, base_accesses, 0) == access_public_node
1220 && !BINFO_VIRTUAL_P (base_binfo)
1221 && integer_zerop (BINFO_OFFSET (base_binfo)))
1222 /* single non-virtual public. */
1223 return si_class_desc_type_node;
1227 tree array_domain, base_array;
1229 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1232 tree extend = make_tree_vec (num_bases + 5);
1234 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1235 TREE_VEC_ELT (extend, ix)
1236 = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1237 vmi_class_desc_type_node = extend;
1239 var_desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1243 /* Create the array of __base_class_type_info entries.
1244 G++ 3.2 allocated an array that had one too many
1245 entries, and then filled that extra entries with
1247 if (abi_version_at_least (2))
1248 array_domain = build_index_type (size_int (num_bases - 1));
1250 array_domain = build_index_type (size_int (num_bases));
1252 build_array_type (base_desc_type_node, array_domain);
1254 push_nested_namespace (abi_node);
1255 var_desc = create_pseudo_type_info
1256 ("__vmi_class_type_info", num_bases,
1257 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1258 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1259 build_decl (FIELD_DECL, NULL_TREE, base_array),
1261 pop_nested_namespace (abi_node);
1263 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = var_desc;
1268 return bltn_desc_type_node;
1272 /* Make sure the required builtin types exist for generating the type_info
1273 variable definitions. */
1276 create_tinfo_types (void)
1278 my_friendly_assert (!ti_desc_type_node, 20020609);
1280 push_nested_namespace (abi_node);
1282 /* Create the internal type_info structure. This is used as a base for
1283 the other structures. */
1287 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1288 field = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1291 field = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1292 TREE_CHAIN (field) = fields;
1295 finish_builtin_struct (ti_desc_type_node, "__type_info_pseudo",
1297 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1300 /* Fundamental type_info */
1301 bltn_desc_type_node = create_pseudo_type_info
1302 ("__fundamental_type_info", 0,
1305 /* Array, function and enum type_info. No additional fields. */
1306 ary_desc_type_node = create_pseudo_type_info
1307 ("__array_type_info", 0,
1309 func_desc_type_node = create_pseudo_type_info
1310 ("__function_type_info", 0,
1312 enum_desc_type_node = create_pseudo_type_info
1313 ("__enum_type_info", 0,
1316 /* Class type_info. Add a flags field. */
1317 class_desc_type_node = create_pseudo_type_info
1318 ("__class_type_info", 0,
1321 /* Single public non-virtual base class. Add pointer to base class.
1322 This is really a descendant of __class_type_info. */
1323 si_class_desc_type_node = create_pseudo_type_info
1324 ("__si_class_type_info", 0,
1325 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1328 /* Base class internal helper. Pointer to base type, offset to base,
1333 field = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1336 field = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1337 TREE_CHAIN (field) = fields;
1340 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1341 finish_builtin_struct (base_desc_type_node, "__base_class_type_info_pseudo",
1343 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1346 /* General hierarchy is created as necessary in this vector. */
1347 vmi_class_desc_type_node = make_tree_vec (10);
1349 /* Pointer type_info. Adds two fields, qualification mask
1350 and pointer to the pointed to type. This is really a descendant of
1351 __pbase_type_info. */
1352 ptr_desc_type_node = create_pseudo_type_info
1353 ("__pointer_type_info", 0,
1354 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1355 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1358 /* Pointer to member data type_info. Add qualifications flags,
1359 pointer to the member's type info and pointer to the class.
1360 This is really a descendant of __pbase_type_info. */
1361 ptm_desc_type_node = create_pseudo_type_info
1362 ("__pointer_to_member_type_info", 0,
1363 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1364 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1365 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1368 pop_nested_namespace (abi_node);
1371 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1372 support. Generating them here guarantees consistency with the other
1373 structures. We use the following heuristic to determine when the runtime
1374 is being generated. If std::__fundamental_type_info is defined, and its
1375 destructor is defined, then the runtime is being built. */
1378 emit_support_tinfos (void)
1380 static tree *const fundamentals[] =
1385 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1386 &short_integer_type_node, &short_unsigned_type_node,
1387 &integer_type_node, &unsigned_type_node,
1388 &long_integer_type_node, &long_unsigned_type_node,
1389 &long_long_integer_type_node, &long_long_unsigned_type_node,
1390 &float_type_node, &double_type_node, &long_double_type_node,
1394 tree bltn_type, dtor;
1396 push_nested_namespace (abi_node);
1397 bltn_type = xref_tag (class_type,
1398 get_identifier ("__fundamental_type_info"),
1400 pop_nested_namespace (abi_node);
1401 if (!COMPLETE_TYPE_P (bltn_type))
1403 dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1404 if (DECL_EXTERNAL (dtor))
1407 for (ix = 0; fundamentals[ix]; ix++)
1409 tree bltn = *fundamentals[ix];
1410 tree bltn_ptr = build_pointer_type (bltn);
1411 tree bltn_const_ptr = build_pointer_type
1412 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1415 tinfo = get_tinfo_decl (bltn);
1416 TREE_USED (tinfo) = 1;
1417 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1419 tinfo = get_tinfo_decl (bltn_ptr);
1420 TREE_USED (tinfo) = 1;
1421 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1423 tinfo = get_tinfo_decl (bltn_const_ptr);
1424 TREE_USED (tinfo) = 1;
1425 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1429 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1430 tinfo decl. Determine whether it needs emitting, and if so
1431 generate the initializer. */
1434 emit_tinfo_decl (tree decl)
1436 tree type = TREE_TYPE (DECL_NAME (decl));
1437 int in_library = typeinfo_in_lib_p (type);
1438 tree var_desc, var_init;
1440 my_friendly_assert (DECL_TINFO_P (decl), 20030307);
1445 DECL_EXTERNAL (decl) = 0;
1448 /* If we're not in the runtime, then DECL (which is already
1449 DECL_EXTERNAL) will not be defined here. */
1450 DECL_INTERFACE_KNOWN (decl) = 1;
1454 else if (involves_incomplete_p (type))
1456 if (!decl_needed_p (decl))
1458 /* If TYPE involves an incomplete class type, then the typeinfo
1459 object will be emitted with internal linkage. There is no
1460 way to know whether or not types are incomplete until the end
1461 of the compilation, so this determination must be deferred
1462 until this point. */
1463 TREE_PUBLIC (decl) = 0;
1464 DECL_EXTERNAL (decl) = 0;
1465 DECL_INTERFACE_KNOWN (decl) = 1;
1468 import_export_decl (decl);
1469 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1471 DECL_EXTERNAL (decl) = 0;
1472 var_desc = get_pseudo_ti_desc (type);
1473 var_init = get_pseudo_ti_init (type, var_desc);
1474 DECL_INITIAL (decl) = var_init;
1476 cp_finish_decl (decl, var_init, NULL_TREE, 0);