1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Mostly written by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
33 /* C++ returns type information to the user in struct type_info
34 objects. We also use type information to implement dynamic_cast and
35 exception handlers. Type information for a particular type is
36 indicated with an ABI defined structure derived from type_info.
37 This would all be very straight forward, but for the fact that the
38 runtime library provides the definitions of the type_info structure
39 and the ABI defined derived classes. We cannot build declarations
40 of them directly in the compiler, but we need to layout objects of
41 their type. Somewhere we have to lie.
43 We define layout compatible POD-structs with compiler-defined names
44 and generate the appropriate initializations for them (complete
45 with explicit mention of their vtable). When we have to provide a
46 type_info to the user we reinterpret_cast the internal compiler
47 type to type_info. A well formed program can only explicitly refer
48 to the type_infos of complete types (& cv void). However, we chain
49 pointer type_infos to the pointed-to-type, and that can be
50 incomplete. We only need the addresses of such incomplete
51 type_info objects for static initialization.
53 The type information VAR_DECL of a type is held on the
54 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
55 will be the internal type. It will usually have the correct
56 internal type reflecting the kind of type it represents (pointer,
57 array, function, class, inherited class, etc). When the type it
58 represents is incomplete, it will have the internal type
59 corresponding to type_info. That will only happen at the end of
60 translation, when we are emitting the type info objects. */
62 /* Accessors for the type_info objects. We need to remember several things
63 about each of the type_info types. The global tree nodes such as
64 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
65 the required information. */
66 /* The RECORD_TYPE of a type_info derived class. */
67 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
68 /* The VAR_DECL of the vtable for the type_info derived class.
69 This is only filled in at the end of the translation. */
70 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
71 /* The IDENTIFIER_NODE naming the real class. */
72 #define TINFO_REAL_NAME(NODE) TREE_PURPOSE (NODE)
74 static tree build_headof PARAMS((tree));
75 static tree ifnonnull PARAMS((tree, tree));
76 static tree tinfo_name PARAMS((tree));
77 static tree build_dynamic_cast_1 PARAMS((tree, tree));
78 static tree throw_bad_cast PARAMS((void));
79 static tree throw_bad_typeid PARAMS((void));
80 static tree get_tinfo_decl_dynamic PARAMS((tree));
81 static tree get_tinfo_ptr PARAMS((tree));
82 static bool typeid_ok_p PARAMS((void));
83 static int qualifier_flags PARAMS((tree));
84 static int target_incomplete_p PARAMS((tree));
85 static tree tinfo_base_init PARAMS((tree, tree));
86 static tree generic_initializer PARAMS((tree, tree));
87 static tree ptr_initializer PARAMS((tree, tree, int *));
88 static tree ptm_initializer PARAMS((tree, tree, int *));
89 static tree dfs_class_hint_mark PARAMS ((tree, void *));
90 static tree dfs_class_hint_unmark PARAMS ((tree, void *));
91 static int class_hint_flags PARAMS((tree));
92 static tree class_initializer PARAMS((tree, tree, tree));
93 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
94 static tree get_pseudo_ti_init PARAMS ((tree, tree, int *));
95 static tree get_pseudo_ti_desc PARAMS((tree));
96 static void create_tinfo_types PARAMS((void));
97 static int typeinfo_in_lib_p PARAMS((tree));
99 static int doing_runtime = 0;
102 /* Declare language defined type_info type and a pointer to const
103 type_info. This is incomplete here, and will be completed when
104 the user #includes <typeinfo>. There are language defined
105 restrictions on what can be done until that is included. Create
106 the internal versions of the ABI types. */
109 init_rtti_processing ()
111 push_namespace (std_identifier);
112 type_info_type_node = xref_tag
113 (class_type_node, get_identifier ("type_info"), 1);
117 (build_qualified_type (type_info_type_node, TYPE_QUAL_CONST));
119 create_tinfo_types ();
122 /* Given the expression EXP of type `class *', return the head of the
123 object pointed to by EXP with type cv void*, if the class has any
124 virtual functions (TYPE_POLYMORPHIC_P), else just return the
131 tree type = TREE_TYPE (exp);
135 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
136 type = TREE_TYPE (type);
138 if (!TYPE_POLYMORPHIC_P (type))
141 /* We use this a couple of times below, protect it. */
142 exp = save_expr (exp);
144 /* The offset-to-top field is at index -2 from the vptr. */
145 index = build_int_2 (-2, -1);
147 offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
149 type = build_qualified_type (ptr_type_node,
150 cp_type_quals (TREE_TYPE (exp)));
151 return build (PLUS_EXPR, type, exp,
152 cp_convert (ptrdiff_type_node, offset));
155 /* Get a bad_cast node for the program to throw...
157 See libstdc++/exception.cc for __throw_bad_cast */
162 tree fn = get_identifier ("__cxa_bad_cast");
163 if (IDENTIFIER_GLOBAL_VALUE (fn))
164 fn = IDENTIFIER_GLOBAL_VALUE (fn);
166 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
169 return build_call (fn, NULL_TREE);
175 tree fn = get_identifier ("__cxa_bad_typeid");
176 if (IDENTIFIER_GLOBAL_VALUE (fn))
177 fn = IDENTIFIER_GLOBAL_VALUE (fn);
180 tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
181 t = build_function_type (build_reference_type (t), void_list_node);
182 fn = push_throw_library_fn (fn, t);
185 return build_call (fn, NULL_TREE);
188 /* Return a pointer to type_info function associated with the expression EXP.
189 If EXP is a reference to a polymorphic class, return the dynamic type;
190 otherwise return the static type of the expression. */
193 get_tinfo_decl_dynamic (exp)
198 if (exp == error_mark_node)
199 return error_mark_node;
201 type = TREE_TYPE (exp);
203 /* peel back references, so they match. */
204 if (TREE_CODE (type) == REFERENCE_TYPE)
205 type = TREE_TYPE (type);
207 /* Peel off cv qualifiers. */
208 type = TYPE_MAIN_VARIANT (type);
210 if (!VOID_TYPE_P (type))
211 type = complete_type_or_else (type, exp);
214 return error_mark_node;
216 /* If exp is a reference to polymorphic type, get the real type_info. */
217 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
219 /* build reference to type_info from vtable. */
223 /* The RTTI information is at index -1. */
224 index = integer_minus_one_node;
225 t = build_vtbl_ref (exp, index);
226 TREE_TYPE (t) = type_info_ptr_type;
230 /* Otherwise return the type_info for the static type of the expr. */
231 return get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
239 error ("cannot use typeid with -fno-rtti");
243 if (!COMPLETE_TYPE_P (type_info_type_node))
245 error ("must #include <typeinfo> before using typeid");
256 tree cond = NULL_TREE;
259 if (exp == error_mark_node || !typeid_ok_p ())
260 return error_mark_node;
262 if (processing_template_decl)
263 return build_min_nt (TYPEID_EXPR, exp);
265 if (TREE_CODE (exp) == INDIRECT_REF
266 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
267 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
268 && ! resolves_to_fixed_type_p (exp, &nonnull)
271 exp = stabilize_reference (exp);
272 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
275 exp = get_tinfo_decl_dynamic (exp);
277 if (exp == error_mark_node)
278 return error_mark_node;
280 exp = build_indirect_ref (exp, NULL);
284 tree bad = throw_bad_typeid ();
286 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
289 return convert_from_reference (exp);
292 /* Generate the NTBS name of a type. */
300 name = mangle_type_string (type);
301 name_string = combine_strings (build_string (strlen (name) + 1, name));
305 /* Return a VAR_DECL for the internal ABI defined type_info object for
306 TYPE. You must arrange that the decl is mark_used, if actually use
307 it --- decls in vtables are only used if the vtable is output. */
310 get_tinfo_decl (type)
316 if (COMPLETE_TYPE_P (type)
317 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
319 error ("cannot create type information for type `%T' because its size is variable",
321 return error_mark_node;
324 if (TREE_CODE (type) == OFFSET_TYPE)
325 type = TREE_TYPE (type);
326 if (TREE_CODE (type) == METHOD_TYPE)
327 type = build_function_type (TREE_TYPE (type),
328 TREE_CHAIN (TYPE_ARG_TYPES (type)));
330 name = mangle_typeinfo_for_type (type);
332 d = IDENTIFIER_GLOBAL_VALUE (name);
335 tree var_desc = get_pseudo_ti_desc (type);
337 d = build_lang_decl (VAR_DECL, name, TINFO_PSEUDO_TYPE (var_desc));
339 DECL_ARTIFICIAL (d) = 1;
340 TREE_READONLY (d) = 1;
342 DECL_EXTERNAL (d) = 1;
343 SET_DECL_ASSEMBLER_NAME (d, name);
345 cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
347 pushdecl_top_level (d);
349 /* Remember the type it is for. */
350 TREE_TYPE (name) = type;
356 /* Return a pointer to a type_info object describing TYPE, suitably
357 cast to the language defined type. */
363 tree exp = get_tinfo_decl (type);
365 /* Convert to type_info type. */
366 exp = build_unary_op (ADDR_EXPR, exp, 0);
367 exp = ocp_convert (type_info_ptr_type, exp, CONV_REINTERPRET, 0);
372 /* Return the type_info object for TYPE. */
378 if (type == error_mark_node || !typeid_ok_p ())
379 return error_mark_node;
381 if (processing_template_decl)
382 return build_min_nt (TYPEID_EXPR, type);
384 /* If the type of the type-id is a reference type, the result of the
385 typeid expression refers to a type_info object representing the
387 if (TREE_CODE (type) == REFERENCE_TYPE)
388 type = TREE_TYPE (type);
390 /* The top-level cv-qualifiers of the lvalue expression or the type-id
391 that is the operand of typeid are always ignored. */
392 type = TYPE_MAIN_VARIANT (type);
394 if (!VOID_TYPE_P (type))
395 type = complete_type_or_else (type, NULL_TREE);
398 return error_mark_node;
400 return build_indirect_ref (get_tinfo_ptr (type), NULL);
403 /* Check whether TEST is null before returning RESULT. If TEST is used in
404 RESULT, it must have previously had a save_expr applied to it. */
407 ifnonnull (test, result)
410 return build (COND_EXPR, TREE_TYPE (result),
411 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
412 cp_convert (TREE_TYPE (result), integer_zero_node),
416 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
420 build_dynamic_cast_1 (type, expr)
423 enum tree_code tc = TREE_CODE (type);
424 tree exprtype = TREE_TYPE (expr);
426 tree old_expr = expr;
427 const char *errstr = NULL;
429 /* T shall be a pointer or reference to a complete class type, or
430 `pointer to cv void''. */
434 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
437 if (! IS_AGGR_TYPE (TREE_TYPE (type)))
439 errstr = "target is not pointer or reference to class";
442 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
444 errstr = "target is not pointer or reference to complete type";
450 errstr = "target is not pointer or reference";
454 if (TREE_CODE (expr) == OFFSET_REF)
456 expr = resolve_offset_ref (expr);
457 exprtype = TREE_TYPE (expr);
460 if (tc == POINTER_TYPE)
461 expr = convert_from_reference (expr);
462 else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
464 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
465 exprtype = build_reference_type (exprtype);
466 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
467 LOOKUP_NORMAL, NULL_TREE);
470 exprtype = TREE_TYPE (expr);
472 if (tc == POINTER_TYPE)
474 /* If T is a pointer type, v shall be an rvalue of a pointer to
475 complete class type, and the result is an rvalue of type T. */
477 if (TREE_CODE (exprtype) != POINTER_TYPE)
479 errstr = "source is not a pointer";
482 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
484 errstr = "source is not a pointer to class";
487 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
489 errstr = "source is a pointer to incomplete type";
495 /* T is a reference type, v shall be an lvalue of a complete class
496 type, and the result is an lvalue of the type referred to by T. */
498 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
500 errstr = "source is not of class type";
503 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
505 errstr = "source is of incomplete class type";
511 /* The dynamic_cast operator shall not cast away constness. */
512 if (!at_least_as_qualified_p (TREE_TYPE (type),
513 TREE_TYPE (exprtype)))
515 errstr = "conversion casts away constness";
519 /* If *type is an unambiguous accessible base class of *exprtype,
520 convert statically. */
524 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
525 ba_not_special, NULL);
529 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
531 if (TREE_CODE (exprtype) == POINTER_TYPE)
532 expr = non_lvalue (expr);
537 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
538 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
541 /* if TYPE is `void *', return pointer to complete object. */
542 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
544 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
545 if (TREE_CODE (expr) == ADDR_EXPR
546 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
547 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
548 return build1 (NOP_EXPR, type, expr);
550 /* Since expr is used twice below, save it. */
551 expr = save_expr (expr);
553 expr1 = build_headof (expr);
554 if (TREE_TYPE (expr1) != type)
555 expr1 = build1 (NOP_EXPR, type, expr1);
556 return ifnonnull (expr, expr1);
561 tree result, td2, td3, elems;
562 tree static_type, target_type, boff;
564 /* If we got here, we can't convert statically. Therefore,
565 dynamic_cast<D&>(b) (b an object) cannot succeed. */
566 if (tc == REFERENCE_TYPE)
568 if (TREE_CODE (old_expr) == VAR_DECL
569 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
571 tree expr = throw_bad_cast ();
572 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
574 /* Bash it to the expected type. */
575 TREE_TYPE (expr) = type;
579 /* Ditto for dynamic_cast<D*>(&b). */
580 else if (TREE_CODE (expr) == ADDR_EXPR)
582 tree op = TREE_OPERAND (expr, 0);
583 if (TREE_CODE (op) == VAR_DECL
584 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
586 warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
588 retval = build_int_2 (0, 0);
589 TREE_TYPE (retval) = type;
594 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
595 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
596 td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
597 td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
599 /* Determine how T and V are related. */
600 boff = get_dynamic_cast_base_type (static_type, target_type);
602 /* Since expr is used twice below, save it. */
603 expr = save_expr (expr);
606 if (tc == REFERENCE_TYPE)
607 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
610 (NULL_TREE, expr1, tree_cons
611 (NULL_TREE, td3, tree_cons
612 (NULL_TREE, td2, tree_cons
613 (NULL_TREE, boff, NULL_TREE))));
615 dcast_fn = dynamic_cast_node;
623 push_nested_namespace (ns);
624 tinfo_ptr = xref_tag (class_type_node,
625 get_identifier ("__class_type_info"),
628 tinfo_ptr = build_pointer_type
629 (build_qualified_type
630 (tinfo_ptr, TYPE_QUAL_CONST));
631 name = "__dynamic_cast";
633 (NULL_TREE, const_ptr_type_node, tree_cons
634 (NULL_TREE, tinfo_ptr, tree_cons
635 (NULL_TREE, tinfo_ptr, tree_cons
636 (NULL_TREE, ptrdiff_type_node, void_list_node))));
637 tmp = build_function_type (ptr_type_node, tmp);
638 dcast_fn = build_library_fn_ptr (name, tmp);
639 pop_nested_namespace (ns);
640 dynamic_cast_node = dcast_fn;
642 result = build_call (dcast_fn, elems);
644 if (tc == REFERENCE_TYPE)
646 tree bad = throw_bad_cast ();
648 result = save_expr (result);
649 return build (COND_EXPR, type, result, result, bad);
652 /* Now back to the type we want from a void*. */
653 result = cp_convert (type, result);
654 return ifnonnull (expr, result);
658 errstr = "source type is not polymorphic";
661 error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
662 expr, exprtype, type, errstr);
663 return error_mark_node;
667 build_dynamic_cast (type, expr)
670 if (type == error_mark_node || expr == error_mark_node)
671 return error_mark_node;
673 if (processing_template_decl)
674 return build_min (DYNAMIC_CAST_EXPR, type, expr);
676 return convert_from_reference (build_dynamic_cast_1 (type, expr));
679 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
682 qualifier_flags (type)
686 /* we want the qualifiers on this type, not any array core, it might have */
687 int quals = TYPE_QUALS (type);
689 if (quals & TYPE_QUAL_CONST)
691 if (quals & TYPE_QUAL_VOLATILE)
693 if (quals & TYPE_QUAL_RESTRICT)
698 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
699 contains a pointer to member of an incomplete class. */
702 target_incomplete_p (type)
705 while (TREE_CODE (type) == POINTER_TYPE)
706 if (TYPE_PTRMEM_P (type))
708 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
710 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
713 type = TREE_TYPE (type);
714 if (!COMPLETE_OR_VOID_TYPE_P (type))
720 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
721 is the vtable pointer and NTBS name. The NTBS name is emitted as a
722 comdat const char array, so it becomes a unique key for the type. Generate
723 and emit that VAR_DECL here. (We can't always emit the type_info itself
724 as comdat, because of pointers to incomplete.) */
727 tinfo_base_init (desc, target)
731 tree init = NULL_TREE;
738 /* Generate the NTBS array variable. */
739 tree name_type = build_cplus_array_type
740 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
742 tree name_string = tinfo_name (target);
744 name_name = mangle_typeinfo_string_for_type (target);
745 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
747 DECL_ARTIFICIAL (name_decl) = 1;
748 TREE_READONLY (name_decl) = 1;
749 TREE_STATIC (name_decl) = 1;
750 DECL_EXTERNAL (name_decl) = 0;
751 TREE_PUBLIC (name_decl) = 1;
752 comdat_linkage (name_decl);
753 /* External name of the string containing the type's name has a
755 SET_DECL_ASSEMBLER_NAME (name_decl,
756 mangle_typeinfo_string_for_type (target));
757 DECL_INITIAL (name_decl) = name_string;
758 cp_finish_decl (name_decl, name_string, NULL_TREE, 0);
759 pushdecl_top_level (name_decl);
762 vtable_ptr = TINFO_VTABLE_DECL (desc);
767 push_nested_namespace (abi_node);
768 real_type = xref_tag (class_type_node, TINFO_REAL_NAME (desc), 1);
769 pop_nested_namespace (abi_node);
771 if (!COMPLETE_TYPE_P (real_type))
773 /* We never saw a definition of this type, so we need to
774 tell the compiler that this is an exported class, as
775 indeed all of the __*_type_info classes are. */
776 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
777 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
780 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
781 vtable_ptr = build_unary_op (ADDR_EXPR, vtable_ptr, 0);
783 /* We need to point into the middle of the vtable. */
785 (PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
786 size_binop (MULT_EXPR,
788 TYPE_SIZE_UNIT (vtable_entry_type)));
789 TREE_CONSTANT (vtable_ptr) = 1;
791 TINFO_VTABLE_DECL (desc) = vtable_ptr;
794 init = tree_cons (NULL_TREE, vtable_ptr, init);
796 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
798 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
799 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
800 init = tree_cons (NULL_TREE, init, NULL_TREE);
805 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
806 information about the particular type_info derivation, which adds no
807 additional fields to the type_info base. */
810 generic_initializer (desc, target)
814 tree init = tinfo_base_init (desc, target);
816 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
817 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
821 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
822 DESC provides information about the particular type_info derivation,
823 which adds target type and qualifier flags members to the type_info base. */
826 ptr_initializer (desc, target, non_public_ptr)
831 tree init = tinfo_base_init (desc, target);
832 tree to = TREE_TYPE (target);
833 int flags = qualifier_flags (to);
834 int incomplete = target_incomplete_p (to);
841 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
842 init = tree_cons (NULL_TREE,
843 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
846 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
847 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
851 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
852 DESC provides information about the particular type_info derivation,
853 which adds class, target type and qualifier flags members to the type_info
857 ptm_initializer (desc, target, non_public_ptr)
862 tree init = tinfo_base_init (desc, target);
863 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
864 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
865 int flags = qualifier_flags (to);
866 int incomplete = target_incomplete_p (to);
873 if (!COMPLETE_TYPE_P (klass))
878 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
879 init = tree_cons (NULL_TREE,
880 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
882 init = tree_cons (NULL_TREE,
883 get_tinfo_ptr (klass),
886 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
887 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
891 /* Check base BINFO to set hint flags in *DATA, which is really an int.
892 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
893 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
894 possible for a type to be both a virtual and non-virtual base. */
897 dfs_class_hint_mark (binfo, data)
901 tree basetype = BINFO_TYPE (binfo);
902 int *hint = (int *) data;
904 if (TREE_VIA_VIRTUAL (binfo))
906 if (CLASSTYPE_MARKED (basetype))
908 if (CLASSTYPE_MARKED2 (basetype))
910 SET_CLASSTYPE_MARKED2 (basetype);
914 if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
916 SET_CLASSTYPE_MARKED (basetype);
918 if (!TREE_VIA_PUBLIC (binfo) && TYPE_BINFO (basetype) != binfo)
923 /* Clear the base's dfs marks, after searching for duplicate bases. */
926 dfs_class_hint_unmark (binfo, data)
928 void *data ATTRIBUTE_UNUSED;
930 tree basetype = BINFO_TYPE (binfo);
932 CLEAR_CLASSTYPE_MARKED (basetype);
933 CLEAR_CLASSTYPE_MARKED2 (basetype);
937 /* Determine the hint flags describing the features of a class's hierarchy. */
940 class_hint_flags (type)
946 dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
947 dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
949 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
951 tree base_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
953 if (TREE_VIA_PUBLIC (base_binfo))
959 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
960 DESC provides information about the particular __class_type_info derivation,
961 which adds hint flags and TRAIL initializers to the type_info base. */
964 class_initializer (desc, target, trail)
969 tree init = tinfo_base_init (desc, target);
971 TREE_CHAIN (init) = trail;
972 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
973 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
977 /* Returns non-zero if the typeinfo for type should be placed in
978 the runtime library. */
981 typeinfo_in_lib_p (type)
984 /* The typeinfo objects for `T*' and `const T*' are in the runtime
985 library for simple types T. */
986 if (TREE_CODE (type) == POINTER_TYPE
987 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
988 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
989 type = TREE_TYPE (type);
991 switch (TREE_CODE (type))
1005 /* Generate the initializer for the type info describing
1006 TYPE. VAR_DESC is a . NON_PUBLIC_P is set non-zero, if the VAR_DECL
1007 should not be exported from this object file. This should only be
1008 called at the end of translation, when we know that no further
1009 types will be completed. */
1012 get_pseudo_ti_init (type, var_desc, non_public_p)
1017 my_friendly_assert (at_eof, 20021120);
1018 switch (TREE_CODE (type))
1021 if (TYPE_PTRMEM_P (type))
1022 return ptm_initializer (var_desc, type, non_public_p);
1024 return ptr_initializer (var_desc, type, non_public_p);
1027 return generic_initializer (var_desc, type);
1030 return generic_initializer (var_desc, type);
1033 return generic_initializer (var_desc, type);
1037 if (TYPE_PTRMEMFUNC_P (type))
1038 return ptm_initializer (var_desc, type, non_public_p);
1039 else if (var_desc == class_desc_type_node)
1041 if (!COMPLETE_TYPE_P (type))
1042 /* Emit a non-public class_type_info. */
1044 return class_initializer (var_desc, type, NULL_TREE);
1046 else if (var_desc == si_class_desc_type_node)
1048 tree base_binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1049 tree base_binfo = TREE_VEC_ELT (base_binfos, 0);
1050 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1051 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1053 return class_initializer (var_desc, type, base_inits);
1057 int hint = class_hint_flags (type);
1058 tree binfo = TYPE_BINFO (type);
1059 int nbases = BINFO_N_BASETYPES (binfo);
1060 tree base_binfos = BINFO_BASETYPES (binfo);
1061 tree base_inits = NULL_TREE;
1064 /* Generate the base information initializer. */
1065 for (ix = nbases; ix--;)
1067 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1068 tree base_init = NULL_TREE;
1073 if (TREE_PUBLIC (base_binfo))
1075 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1076 if (TREE_VIA_VIRTUAL (base_binfo))
1078 /* We store the vtable offset at which the virtual
1079 base offset can be found. */
1080 offset = BINFO_VPTR_FIELD
1081 (binfo_for_vbase (BINFO_TYPE (base_binfo), type));
1082 offset = convert (sizetype, offset);
1086 offset = BINFO_OFFSET (base_binfo);
1088 /* combine offset and flags into one field */
1089 offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1090 build_int_2 (8, 0));
1091 offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1092 build_int_2 (flags, 0));
1093 base_init = tree_cons (NULL_TREE, offset, base_init);
1094 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1095 base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1096 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1098 base_inits = build (CONSTRUCTOR,
1099 NULL_TREE, NULL_TREE, base_inits);
1100 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1101 /* Prepend the number of bases. */
1102 base_inits = tree_cons (NULL_TREE,
1103 build_int_2 (nbases, 0), base_inits);
1104 /* Prepend the hint flags. */
1105 base_inits = tree_cons (NULL_TREE,
1106 build_int_2 (hint, 0), base_inits);
1108 return class_initializer (var_desc, type, base_inits);
1113 return generic_initializer (var_desc, type);
1117 /* Generate the RECORD_TYPE containing the data layout of a type_info
1118 derivative as used by the runtime. This layout must be consistent with
1119 that defined in the runtime support. Also generate the VAR_DECL for the
1120 type's vtable. We explicitly manage the vtable member, and name it for
1121 real type as used in the runtime. The RECORD type has a different name,
1122 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1123 is the generated type and TINFO_VTABLE_NAME is the name of the
1124 vtable. We have to delay generating the VAR_DECL of the vtable
1125 until the end of the translation, when we'll have seen the library
1126 definition, if there was one.
1128 REAL_NAME is the runtime's name of the type. Trailing arguments are
1129 additional FIELD_DECL's for the structure. The final argument must be
1133 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1142 VA_OPEN (ap, ident);
1143 VA_FIXEDARG (ap, const char *, real_name);
1144 VA_FIXEDARG (ap, int, ident);
1146 /* Generate the pseudo type name. */
1147 pseudo_name = (char *)alloca (strlen (real_name) + 30);
1148 strcpy (pseudo_name, real_name);
1149 strcat (pseudo_name, "_pseudo");
1151 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1153 /* First field is the pseudo type_info base class. */
1154 fields[0] = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1156 /* Now add the derived fields. */
1157 for (ix = 0; (field_decl = va_arg (ap, tree));)
1158 fields[++ix] = field_decl;
1160 /* Create the pseudo type. */
1161 pseudo_type = make_aggr_type (RECORD_TYPE);
1162 finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1163 TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1165 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1166 TINFO_REAL_NAME (result) = get_identifier (real_name);
1167 TINFO_PSEUDO_TYPE (result) =
1168 cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1174 /* Return a pseudo type info type node used to describe TYPE. TYPE
1175 must be a complete type (or cv void), except at the end of the
1176 translation unit. */
1179 get_pseudo_ti_desc (type)
1182 switch (TREE_CODE (type))
1185 return TYPE_PTRMEM_P (type) ? ptm_desc_type_node : ptr_desc_type_node;
1187 return enum_desc_type_node;
1189 return func_desc_type_node;
1191 return ary_desc_type_node;
1194 if (TYPE_PTRMEMFUNC_P (type))
1195 return ptm_desc_type_node;
1196 else if (!COMPLETE_TYPE_P (type))
1198 my_friendly_assert (at_eof, 20020609);
1199 return class_desc_type_node;
1201 else if (!CLASSTYPE_N_BASECLASSES (type))
1202 return class_desc_type_node;
1206 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (type)), 0);
1207 int num_bases = BINFO_N_BASETYPES (TYPE_BINFO (type));
1210 && TREE_PUBLIC (base_binfo)
1211 && !TREE_VIA_VIRTUAL (base_binfo)
1212 && integer_zerop (BINFO_OFFSET (base_binfo)))
1213 /* single non-virtual public. */
1214 return si_class_desc_type_node;
1218 tree array_domain, base_array;
1220 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1223 tree extend = make_tree_vec (num_bases + 5);
1225 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1226 TREE_VEC_ELT (extend, ix)
1227 = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1228 vmi_class_desc_type_node = extend;
1230 var_desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1234 /* Add number of bases and trailing array of
1235 base_class_type_info. */
1236 array_domain = build_index_type (size_int (num_bases));
1238 build_array_type (base_desc_type_node, array_domain);
1240 push_nested_namespace (abi_node);
1241 var_desc = create_pseudo_type_info
1242 ("__vmi_class_type_info", num_bases,
1243 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1244 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1245 build_decl (FIELD_DECL, NULL_TREE, base_array),
1247 pop_nested_namespace (abi_node);
1249 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = var_desc;
1254 return bltn_desc_type_node;
1258 /* Make sure the required builtin types exist for generating the type_info
1259 varable definitions. */
1262 create_tinfo_types ()
1264 my_friendly_assert (!ti_desc_type_node, 20020609);
1266 push_nested_namespace (abi_node);
1268 /* Create the internal type_info structure. This is used as a base for
1269 the other structures. */
1273 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1274 fields[0] = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1275 fields[1] = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1276 finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1277 fields, 1, ptr_type_node);
1278 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1281 /* Fundamental type_info */
1282 bltn_desc_type_node = create_pseudo_type_info
1283 ("__fundamental_type_info", 0,
1286 /* Array, function and enum type_info. No additional fields. */
1287 ary_desc_type_node = create_pseudo_type_info
1288 ("__array_type_info", 0,
1290 func_desc_type_node = create_pseudo_type_info
1291 ("__function_type_info", 0,
1293 enum_desc_type_node = create_pseudo_type_info
1294 ("__enum_type_info", 0,
1297 /* Class type_info. Add a flags field. */
1298 class_desc_type_node = create_pseudo_type_info
1299 ("__class_type_info", 0,
1302 /* Single public non-virtual base class. Add pointer to base class.
1303 This is really a descendant of __class_type_info. */
1304 si_class_desc_type_node = create_pseudo_type_info
1305 ("__si_class_type_info", 0,
1306 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1309 /* Base class internal helper. Pointer to base type, offset to base,
1314 fields[0] = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1315 fields[1] = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1316 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1317 finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1318 fields, 1, ptr_type_node);
1319 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1322 /* General hierarchy is created as necessary in this vector. */
1323 vmi_class_desc_type_node = make_tree_vec (10);
1325 /* Pointer type_info. Adds two fields, qualification mask
1326 and pointer to the pointed to type. This is really a descendant of
1327 __pbase_type_info. */
1328 ptr_desc_type_node = create_pseudo_type_info
1329 ("__pointer_type_info", 0,
1330 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1331 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1334 /* Pointer to member data type_info. Add qualifications flags,
1335 pointer to the member's type info and pointer to the class.
1336 This is really a descendant of __pbase_type_info. */
1337 ptm_desc_type_node = create_pseudo_type_info
1338 ("__pointer_to_member_type_info", 0,
1339 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1340 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1341 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1344 pop_nested_namespace (abi_node);
1347 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1348 support. Generating them here guarantees consistency with the other
1349 structures. We use the following heuristic to determine when the runtime
1350 is being generated. If std::__fundamental_type_info is defined, and its
1351 destructor is defined, then the runtime is being built. */
1354 emit_support_tinfos ()
1356 static tree *const fundamentals[] =
1361 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1362 &short_integer_type_node, &short_unsigned_type_node,
1363 &integer_type_node, &unsigned_type_node,
1364 &long_integer_type_node, &long_unsigned_type_node,
1365 &long_long_integer_type_node, &long_long_unsigned_type_node,
1366 &float_type_node, &double_type_node, &long_double_type_node,
1370 tree bltn_type, dtor;
1372 push_nested_namespace (abi_node);
1373 bltn_type = xref_tag (class_type_node,
1374 get_identifier ("__fundamental_type_info"), 1);
1375 pop_nested_namespace (abi_node);
1376 if (!COMPLETE_TYPE_P (bltn_type))
1378 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1379 if (DECL_EXTERNAL (dtor))
1382 for (ix = 0; fundamentals[ix]; ix++)
1384 tree bltn = *fundamentals[ix];
1385 tree bltn_ptr = build_pointer_type (bltn);
1386 tree bltn_const_ptr = build_pointer_type
1387 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1390 tinfo = get_tinfo_decl (bltn);
1391 TREE_USED (tinfo) = 1;
1392 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1394 tinfo = get_tinfo_decl (bltn_ptr);
1395 TREE_USED (tinfo) = 1;
1396 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1398 tinfo = get_tinfo_decl (bltn_const_ptr);
1399 TREE_USED (tinfo) = 1;
1400 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1404 /* Return non-zero, iff T is a type_info variable which has not had a
1405 definition emitted for it. */
1408 unemitted_tinfo_decl_p (t, data)
1410 void *data ATTRIBUTE_UNUSED;
1412 if (/* It's a var decl */
1413 TREE_CODE (t) == VAR_DECL
1414 /* whos name points back to itself */
1415 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == t
1416 /* whos name's type is non-null */
1417 && TREE_TYPE (DECL_NAME (t))
1418 /* and whos type is a struct */
1419 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1420 /* with a first field of our pseudo type info */
1421 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (t))) == ti_desc_type_node)
1426 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1427 tinfo decl. Determine whether it needs emitting, and if so
1428 generate the initializer. */
1431 emit_tinfo_decl (decl_ptr, data)
1433 void *data ATTRIBUTE_UNUSED;
1435 tree decl = *decl_ptr;
1436 tree type = TREE_TYPE (DECL_NAME (decl));
1438 int in_library = typeinfo_in_lib_p (type);
1439 tree var_desc, var_init;
1441 import_export_tinfo (decl, type, in_library);
1442 if (DECL_REALLY_EXTERN (decl) || !DECL_NEEDED_P (decl))
1445 if (!doing_runtime && in_library)
1449 var_desc = get_pseudo_ti_desc (type);
1450 var_init = get_pseudo_ti_init (type, var_desc, &non_public);
1452 DECL_EXTERNAL (decl) = 0;
1453 TREE_PUBLIC (decl) = !non_public;
1455 DECL_COMDAT (decl) = 0;
1457 DECL_INITIAL (decl) = var_init;
1458 cp_finish_decl (decl, var_init, NULL_TREE, 0);
1459 /* cp_finish_decl will have dealt with linkage. */
1461 /* Say we've dealt with it. */
1462 TREE_TYPE (DECL_NAME (decl)) = NULL_TREE;