1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 /* Accessors for the type_info objects. We need to remember several things
34 about each of the type_info types. The global tree nodes such as
35 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
36 the required information. */
37 /* The RECORD_TYPE of a type_info derived class. */
38 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
39 /* The VAR_DECL of the vtable for the type_info derived class. */
40 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
42 extern struct obstack permanent_obstack;
44 static tree build_headof PARAMS((tree));
45 static tree ifnonnull PARAMS((tree, tree));
46 static tree tinfo_name PARAMS((tree));
47 static tree build_dynamic_cast_1 PARAMS((tree, tree));
48 static tree throw_bad_cast PARAMS((void));
49 static tree throw_bad_typeid PARAMS((void));
50 static tree get_tinfo_decl_dynamic PARAMS((tree));
51 static bool typeid_ok_p PARAMS ((void));
52 static int qualifier_flags PARAMS((tree));
53 static int target_incomplete_p PARAMS((tree));
54 static tree tinfo_base_init PARAMS((tree, tree));
55 static tree generic_initializer PARAMS((tree, tree));
56 static tree ptr_initializer PARAMS((tree, tree, int *));
57 static tree ptm_initializer PARAMS((tree, tree, int *));
58 static tree dfs_class_hint_mark PARAMS ((tree, void *));
59 static tree dfs_class_hint_unmark PARAMS ((tree, void *));
60 static int class_hint_flags PARAMS((tree));
61 static tree class_initializer PARAMS((tree, tree, tree));
62 static tree synthesize_tinfo_var PARAMS((tree, tree));
63 static tree create_real_tinfo_var PARAMS((tree, tree, tree, tree, int));
64 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
65 static tree get_vmi_pseudo_type_info PARAMS((int));
66 static void create_tinfo_types PARAMS((void));
67 static int typeinfo_in_lib_p PARAMS((tree));
69 static int doing_runtime = 0;
72 init_rtti_processing ()
75 push_namespace (std_identifier);
76 type_info_type_node = xref_tag
77 (class_type_node, get_identifier ("type_info"), 1);
81 build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
84 /* Given the expression EXP of type `class *', return the head of the
85 object pointed to by EXP with type cv void*, if the class has any
86 virtual functions (TYPE_POLYMORPHIC_P), else just return the
93 tree type = TREE_TYPE (exp);
97 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
98 type = TREE_TYPE (type);
100 if (!TYPE_POLYMORPHIC_P (type))
103 /* We use this a couple of times below, protect it. */
104 exp = save_expr (exp);
106 /* The offset-to-top field is at index -2 from the vptr. */
107 index = build_int_2 (-2, -1);
109 offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
111 type = build_qualified_type (ptr_type_node,
112 CP_TYPE_QUALS (TREE_TYPE (exp)));
113 return build (PLUS_EXPR, type, exp,
114 cp_convert (ptrdiff_type_node, offset));
117 /* Get a bad_cast node for the program to throw...
119 See libstdc++/exception.cc for __throw_bad_cast */
124 tree fn = get_identifier ("__cxa_bad_cast");
125 if (IDENTIFIER_GLOBAL_VALUE (fn))
126 fn = IDENTIFIER_GLOBAL_VALUE (fn);
128 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
131 return build_call (fn, NULL_TREE);
137 tree fn = get_identifier ("__cxa_bad_typeid");
138 if (IDENTIFIER_GLOBAL_VALUE (fn))
139 fn = IDENTIFIER_GLOBAL_VALUE (fn);
142 tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
143 t = build_function_type (build_reference_type (t), void_list_node);
144 fn = push_throw_library_fn (fn, t);
147 return build_call (fn, NULL_TREE);
150 /* Return a pointer to type_info function associated with the expression EXP.
151 If EXP is a reference to a polymorphic class, return the dynamic type;
152 otherwise return the static type of the expression. */
155 get_tinfo_decl_dynamic (exp)
160 if (exp == error_mark_node)
161 return error_mark_node;
163 type = TREE_TYPE (exp);
165 /* peel back references, so they match. */
166 if (TREE_CODE (type) == REFERENCE_TYPE)
167 type = TREE_TYPE (type);
169 /* Peel off cv qualifiers. */
170 type = TYPE_MAIN_VARIANT (type);
172 if (!VOID_TYPE_P (type))
173 type = complete_type_or_else (type, exp);
176 return error_mark_node;
178 /* If exp is a reference to polymorphic type, get the real type_info. */
179 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
181 /* build reference to type_info from vtable. */
185 /* The RTTI information is at index -1. */
186 index = integer_minus_one_node;
187 t = build_vfn_ref (exp, index);
188 TREE_TYPE (t) = build_pointer_type (tinfo_decl_type);
192 /* otherwise return the type_info for the static type of the expr. */
193 exp = get_tinfo_decl (TYPE_MAIN_VARIANT (type));
194 return build_unary_op (ADDR_EXPR, exp, 0);
202 error ("cannot use typeid with -fno-rtti");
206 if (!COMPLETE_TYPE_P (type_info_type_node))
208 error ("must #include <typeinfo> before using typeid");
219 tree cond = NULL_TREE;
222 if (exp == error_mark_node || !typeid_ok_p ())
223 return error_mark_node;
225 if (processing_template_decl)
226 return build_min_nt (TYPEID_EXPR, exp);
228 if (TREE_CODE (exp) == INDIRECT_REF
229 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
230 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
231 && ! resolves_to_fixed_type_p (exp, &nonnull)
234 exp = stabilize_reference (exp);
235 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
238 exp = get_tinfo_decl_dynamic (exp);
240 if (exp == error_mark_node)
241 return error_mark_node;
243 exp = build_indirect_ref (exp, NULL);
247 tree bad = throw_bad_typeid ();
249 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
252 return convert_from_reference (exp);
255 /* Generate the NTBS name of a type. */
263 name = mangle_type_string (type);
264 name_string = combine_strings (build_string (strlen (name) + 1, name));
268 /* Returns a decl for the type_info variable for TYPE. You must
269 arrange that the decl is mark_used, if actually use it --- decls in
270 vtables are only used if the vtable is output. */
273 get_tinfo_decl (type)
279 if (COMPLETE_TYPE_P (type)
280 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
282 cp_error ("cannot create type information for type `%T' because its size is variable",
284 return error_mark_node;
287 if (TREE_CODE (type) == OFFSET_TYPE)
288 type = TREE_TYPE (type);
289 if (TREE_CODE (type) == METHOD_TYPE)
290 type = build_function_type (TREE_TYPE (type),
291 TREE_CHAIN (TYPE_ARG_TYPES (type)));
293 name = mangle_typeinfo_for_type (type);
295 d = IDENTIFIER_GLOBAL_VALUE (name);
300 /* The tinfo decl is the type_info object itself. We make all
301 tinfo objects look as type_info, even though they will end up
302 being a subclass of that when emitted. This means that we'll
303 erroneously think we know the dynamic type -- be careful in the
305 d = build_lang_decl (VAR_DECL, name, tinfo_decl_type);
307 DECL_ARTIFICIAL (d) = 1;
308 DECL_ALIGN (d) = TYPE_ALIGN (ptr_type_node);
309 DECL_USER_ALIGN (d) = 0;
310 TREE_READONLY (d) = 1;
312 DECL_EXTERNAL (d) = 1;
314 if (flag_weak || !typeinfo_in_lib_p (d))
316 SET_DECL_ASSEMBLER_NAME (d, name);
317 cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
319 pushdecl_top_level (d);
320 /* Remember the type it is for. */
321 TREE_TYPE (name) = type;
322 TREE_USED (name) = 1;
327 /* Return the type_info object for TYPE. */
333 if (type == error_mark_node || !typeid_ok_p ())
334 return error_mark_node;
336 if (processing_template_decl)
337 return build_min_nt (TYPEID_EXPR, type);
339 /* If the type of the type-id is a reference type, the result of the
340 typeid expression refers to a type_info object representing the
342 if (TREE_CODE (type) == REFERENCE_TYPE)
343 type = TREE_TYPE (type);
345 /* The top-level cv-qualifiers of the lvalue expression or the type-id
346 that is the operand of typeid are always ignored. */
347 type = TYPE_MAIN_VARIANT (type);
349 if (!VOID_TYPE_P (type))
350 type = complete_type_or_else (type, NULL_TREE);
353 return error_mark_node;
355 return get_tinfo_decl (type);
358 /* Check whether TEST is null before returning RESULT. If TEST is used in
359 RESULT, it must have previously had a save_expr applied to it. */
362 ifnonnull (test, result)
365 return build (COND_EXPR, TREE_TYPE (result),
366 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
367 cp_convert (TREE_TYPE (result), integer_zero_node),
371 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
375 build_dynamic_cast_1 (type, expr)
378 enum tree_code tc = TREE_CODE (type);
379 tree exprtype = TREE_TYPE (expr);
381 tree old_expr = expr;
382 const char *errstr = NULL;
384 /* T shall be a pointer or reference to a complete class type, or
385 `pointer to cv void''. */
389 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
392 if (! IS_AGGR_TYPE (TREE_TYPE (type)))
394 errstr = "target is not pointer or reference to class";
397 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
399 errstr = "target is not pointer or reference to complete type";
405 errstr = "target is not pointer or reference";
409 if (TREE_CODE (expr) == OFFSET_REF)
411 expr = resolve_offset_ref (expr);
412 exprtype = TREE_TYPE (expr);
415 if (tc == POINTER_TYPE)
416 expr = convert_from_reference (expr);
417 else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
419 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
420 exprtype = build_reference_type (exprtype);
421 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
422 LOOKUP_NORMAL, NULL_TREE);
425 exprtype = TREE_TYPE (expr);
427 if (tc == POINTER_TYPE)
429 /* If T is a pointer type, v shall be an rvalue of a pointer to
430 complete class type, and the result is an rvalue of type T. */
432 if (TREE_CODE (exprtype) != POINTER_TYPE)
434 errstr = "source is not a pointer";
437 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
439 errstr = "source is not a pointer to class";
442 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
444 errstr = "source is a pointer to incomplete type";
450 /* T is a reference type, v shall be an lvalue of a complete class
451 type, and the result is an lvalue of the type referred to by T. */
453 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
455 errstr = "source is not of class type";
458 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
460 errstr = "source is of incomplete class type";
466 /* The dynamic_cast operator shall not cast away constness. */
467 if (!at_least_as_qualified_p (TREE_TYPE (type),
468 TREE_TYPE (exprtype)))
470 errstr = "conversion casts away constness";
474 /* If *type is an unambiguous accessible base class of *exprtype,
475 convert statically. */
480 distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
485 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
486 TREE_TYPE (exprtype), TREE_TYPE (type));
487 return error_mark_node;
491 cp_error ("dynamic_cast from `%T' to private base class `%T'",
492 TREE_TYPE (exprtype), TREE_TYPE (type));
493 return error_mark_node;
498 expr = build_vbase_path (PLUS_EXPR, type, expr, path, 0);
499 if (TREE_CODE (exprtype) == POINTER_TYPE)
500 expr = non_lvalue (expr);
505 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
506 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
509 /* if TYPE is `void *', return pointer to complete object. */
510 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
512 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
513 if (TREE_CODE (expr) == ADDR_EXPR
514 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
515 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
516 return build1 (NOP_EXPR, type, expr);
518 /* Since expr is used twice below, save it. */
519 expr = save_expr (expr);
521 expr1 = build_headof (expr);
522 if (TREE_TYPE (expr1) != type)
523 expr1 = build1 (NOP_EXPR, type, expr1);
524 return ifnonnull (expr, expr1);
529 tree result, td2, td3, elems;
530 tree static_type, target_type, boff;
532 /* If we got here, we can't convert statically. Therefore,
533 dynamic_cast<D&>(b) (b an object) cannot succeed. */
534 if (tc == REFERENCE_TYPE)
536 if (TREE_CODE (old_expr) == VAR_DECL
537 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
539 tree expr = throw_bad_cast ();
540 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
542 /* Bash it to the expected type. */
543 TREE_TYPE (expr) = type;
547 /* Ditto for dynamic_cast<D*>(&b). */
548 else if (TREE_CODE (expr) == ADDR_EXPR)
550 tree op = TREE_OPERAND (expr, 0);
551 if (TREE_CODE (op) == VAR_DECL
552 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
554 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
556 retval = build_int_2 (0, 0);
557 TREE_TYPE (retval) = type;
562 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
563 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
564 td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
565 td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
567 /* Determine how T and V are related. */
568 boff = get_dynamic_cast_base_type (static_type, target_type);
570 /* Since expr is used twice below, save it. */
571 expr = save_expr (expr);
574 if (tc == REFERENCE_TYPE)
575 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
578 (NULL_TREE, expr1, tree_cons
579 (NULL_TREE, td3, tree_cons
580 (NULL_TREE, td2, tree_cons
581 (NULL_TREE, boff, NULL_TREE))));
583 dcast_fn = dynamic_cast_node;
591 push_nested_namespace (ns);
592 tinfo_ptr = xref_tag (class_type_node,
593 get_identifier ("__class_type_info"),
596 tinfo_ptr = build_pointer_type
597 (build_qualified_type
598 (tinfo_ptr, TYPE_QUAL_CONST));
599 name = "__dynamic_cast";
601 (NULL_TREE, const_ptr_type_node, tree_cons
602 (NULL_TREE, tinfo_ptr, tree_cons
603 (NULL_TREE, tinfo_ptr, tree_cons
604 (NULL_TREE, ptrdiff_type_node, void_list_node))));
605 tmp = build_function_type (ptr_type_node, tmp);
606 dcast_fn = build_library_fn_ptr (name, tmp);
607 pop_nested_namespace (ns);
608 dynamic_cast_node = dcast_fn;
610 result = build_call (dcast_fn, elems);
612 if (tc == REFERENCE_TYPE)
614 tree bad = throw_bad_cast ();
616 result = save_expr (result);
617 return build (COND_EXPR, type, result, result, bad);
620 /* Now back to the type we want from a void*. */
621 result = cp_convert (type, result);
622 return ifnonnull (expr, result);
626 errstr = "source type is not polymorphic";
629 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
630 expr, exprtype, type, errstr);
631 return error_mark_node;
635 build_dynamic_cast (type, expr)
638 if (type == error_mark_node || expr == error_mark_node)
639 return error_mark_node;
641 if (processing_template_decl)
642 return build_min (DYNAMIC_CAST_EXPR, type, expr);
644 return convert_from_reference (build_dynamic_cast_1 (type, expr));
647 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
650 qualifier_flags (type)
654 /* we want the qualifiers on this type, not any array core, it might have */
655 int quals = TYPE_QUALS (type);
657 if (quals & TYPE_QUAL_CONST)
659 if (quals & TYPE_QUAL_VOLATILE)
661 if (quals & TYPE_QUAL_RESTRICT)
666 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
667 contains a pointer to member of an incomplete class. */
670 target_incomplete_p (type)
673 while (TREE_CODE (type) == POINTER_TYPE)
674 if (TYPE_PTRMEM_P (type))
676 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
678 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
681 type = TREE_TYPE (type);
682 if (!COMPLETE_OR_VOID_TYPE_P (type))
688 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
689 is the vtable pointer and NTBS name. The NTBS name is emitted as a
690 comdat const char array, so it becomes a unique key for the type. Generate
691 and emit that VAR_DECL here. (We can't always emit the type_info itself
692 as comdat, because of pointers to incomplete.) */
695 tinfo_base_init (desc, target)
699 tree init = NULL_TREE;
705 /* Generate the NTBS array variable. */
706 tree name_type = build_cplus_array_type
707 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
709 tree name_string = tinfo_name (target);
711 name_name = mangle_typeinfo_string_for_type (target);
712 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
714 DECL_ARTIFICIAL (name_decl) = 1;
715 TREE_READONLY (name_decl) = 1;
716 TREE_STATIC (name_decl) = 1;
717 DECL_EXTERNAL (name_decl) = 0;
718 TREE_PUBLIC (name_decl) = 1;
719 comdat_linkage (name_decl);
720 /* External name of the string containing the type's name has a
722 SET_DECL_ASSEMBLER_NAME (name_decl,
723 mangle_typeinfo_string_for_type (target));
724 DECL_INITIAL (name_decl) = name_string;
725 cp_finish_decl (name_decl, name_string, NULL_TREE, 0);
726 pushdecl_top_level (name_decl);
729 if (TINFO_VTABLE_DECL (desc))
731 tree vtbl_ptr = TINFO_VTABLE_DECL (desc);
732 init = tree_cons (NULL_TREE, vtbl_ptr, init);
735 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
737 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
738 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
739 init = tree_cons (NULL_TREE, init, NULL_TREE);
744 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
745 information about the particular type_info derivation, which adds no
746 additional fields to the type_info base. */
749 generic_initializer (desc, target)
753 tree init = tinfo_base_init (desc, target);
755 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
756 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
760 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
761 DESC provides information about the particular type_info derivation,
762 which adds target type and qualifier flags members to the type_info base. */
765 ptr_initializer (desc, target, non_public_ptr)
770 tree init = tinfo_base_init (desc, target);
771 tree to = TREE_TYPE (target);
772 int flags = qualifier_flags (to);
773 int incomplete = target_incomplete_p (to);
780 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
781 init = tree_cons (NULL_TREE,
782 build_unary_op (ADDR_EXPR,
783 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
786 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
787 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
791 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
792 DESC provides information about the particular type_info derivation,
793 which adds class, target type and qualifier flags members to the type_info
797 ptm_initializer (desc, target, non_public_ptr)
802 tree init = tinfo_base_init (desc, target);
803 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
804 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
805 int flags = qualifier_flags (to);
806 int incomplete = target_incomplete_p (to);
813 if (!COMPLETE_TYPE_P (klass))
818 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
819 init = tree_cons (NULL_TREE,
820 build_unary_op (ADDR_EXPR,
821 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
823 init = tree_cons (NULL_TREE,
824 build_unary_op (ADDR_EXPR, get_tinfo_decl (klass), 0),
827 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
828 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
832 /* Check base BINFO to set hint flags in *DATA, which is really an int.
833 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
834 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
835 possible for a type to be both a virtual and non-virtual base. */
838 dfs_class_hint_mark (binfo, data)
842 tree basetype = BINFO_TYPE (binfo);
843 int *hint = (int *) data;
845 if (TREE_VIA_VIRTUAL (binfo))
847 if (CLASSTYPE_MARKED (basetype))
849 if (CLASSTYPE_MARKED2 (basetype))
851 SET_CLASSTYPE_MARKED2 (basetype);
855 if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
857 SET_CLASSTYPE_MARKED (basetype);
859 if (!TREE_VIA_PUBLIC (binfo) && TYPE_BINFO (basetype) != binfo)
864 /* Clear the base's dfs marks, after searching for duplicate bases. */
867 dfs_class_hint_unmark (binfo, data)
869 void *data ATTRIBUTE_UNUSED;
871 tree basetype = BINFO_TYPE (binfo);
873 CLEAR_CLASSTYPE_MARKED (basetype);
874 CLEAR_CLASSTYPE_MARKED2 (basetype);
878 /* Determine the hint flags describing the features of a class's heirarchy. */
881 class_hint_flags (type)
887 dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
888 dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
890 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
892 tree base_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
894 if (TREE_VIA_PUBLIC (base_binfo))
900 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
901 DESC provides information about the particular __class_type_info derivation,
902 which adds hint flags and TRAIL initializers to the type_info base. */
905 class_initializer (desc, target, trail)
910 tree init = tinfo_base_init (desc, target);
912 TREE_CHAIN (init) = trail;
913 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
914 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
918 /* Returns non-zero if the typeinfo for type should be placed in
919 the runtime library. */
922 typeinfo_in_lib_p (type)
925 /* The typeinfo objects for `T*' and `const T*' are in the runtime
926 library for simple types T. */
927 if (TREE_CODE (type) == POINTER_TYPE
928 && (CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_QUAL_CONST
929 || CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
930 type = TREE_TYPE (type);
932 switch (TREE_CODE (type))
946 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
947 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
948 the runtime, and therefore has additional fields. If we need not emit a
949 definition (because the runtime must contain it), return NULL_TREE,
950 otherwise return the VAR_DECL. */
953 synthesize_tinfo_var (target_type, real_name)
957 tree var_init = NULL_TREE;
958 tree var_type = NULL_TREE;
961 switch (TREE_CODE (target_type))
964 if (TYPE_PTRMEM_P (target_type))
966 var_type = ptm_desc_type_node;
967 var_init = ptm_initializer (var_type, target_type, &non_public);
971 if (typeinfo_in_lib_p (target_type) && !doing_runtime)
972 /* These are in the runtime. */
974 var_type = ptr_desc_type_node;
975 var_init = ptr_initializer (var_type, target_type, &non_public);
979 var_type = enum_desc_type_node;
980 var_init = generic_initializer (var_type, target_type);
983 var_type = func_desc_type_node;
984 var_init = generic_initializer (var_type, target_type);
987 var_type = ary_desc_type_node;
988 var_init = generic_initializer (var_type, target_type);
992 if (TYPE_PTRMEMFUNC_P (target_type))
994 var_type = ptm_desc_type_node;
995 var_init = ptm_initializer (var_type, target_type, &non_public);
997 else if (!COMPLETE_TYPE_P (target_type))
999 /* Emit a non-public class_type_info. */
1001 var_type = class_desc_type_node;
1002 var_init = class_initializer (var_type, target_type, NULL_TREE);
1004 else if (!CLASSTYPE_N_BASECLASSES (target_type))
1006 var_type = class_desc_type_node;
1007 var_init = class_initializer (var_type, target_type, NULL_TREE);
1011 /* if this has a single public non-virtual base, it's easier */
1012 tree binfo = TYPE_BINFO (target_type);
1013 int nbases = BINFO_N_BASETYPES (binfo);
1014 tree base_binfos = BINFO_BASETYPES (binfo);
1015 tree base_inits = NULL_TREE;
1016 int is_simple = nbases == 1;
1019 /* Generate the base information initializer. */
1020 for (ix = nbases; ix--;)
1022 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1023 tree base_init = NULL_TREE;
1028 if (TREE_PUBLIC (base_binfo))
1030 tinfo = get_tinfo_decl (BINFO_TYPE (base_binfo));
1031 tinfo = build_unary_op (ADDR_EXPR, tinfo, 0);
1032 if (TREE_VIA_VIRTUAL (base_binfo))
1034 /* We store the vtable offset at which the virtual
1035 base offset can be found. */
1036 offset = BINFO_VPTR_FIELD (binfo_for_vbase (BINFO_TYPE (base_binfo),
1038 offset = convert (sizetype, offset);
1042 offset = BINFO_OFFSET (base_binfo);
1044 /* is it a single public inheritance? */
1045 if (is_simple && flags == 2 && integer_zerop (offset))
1047 base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1052 /* combine offset and flags into one field */
1053 offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1054 build_int_2 (8, 0));
1055 offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1056 build_int_2 (flags, 0));
1057 base_init = tree_cons (NULL_TREE, offset, base_init);
1058 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1059 base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1060 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1064 var_type = si_class_desc_type_node;
1067 int hint = class_hint_flags (target_type);
1069 base_inits = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_inits);
1070 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1071 /* Prepend the number of bases. */
1072 base_inits = tree_cons (NULL_TREE,
1073 build_int_2 (nbases, 0), base_inits);
1074 /* Prepend the hint flags. */
1075 base_inits = tree_cons (NULL_TREE,
1076 build_int_2 (hint, 0), base_inits);
1077 var_type = get_vmi_pseudo_type_info (nbases);
1079 var_init = class_initializer (var_type, target_type, base_inits);
1084 if (typeinfo_in_lib_p (target_type))
1087 /* These are guaranteed to be in the runtime. */
1089 var_type = bltn_desc_type_node;
1090 var_init = generic_initializer (var_type, target_type);
1093 my_friendly_abort (20000117);
1096 return create_real_tinfo_var (target_type,
1097 real_name, TINFO_PSEUDO_TYPE (var_type),
1098 var_init, non_public);
1101 /* Create the real typeinfo variable. NON_PUBLIC indicates that we cannot
1102 make this variable public (comdat). */
1105 create_real_tinfo_var (target_type, name, type, init, non_public)
1112 static int count = 0;
1117 /* We cannot give this the name NAME, as that already is globally
1118 bound to the tinfo_decl we originally created for this type in
1120 sprintf (hidden, "__ti_%d", count++);
1121 hidden_name = get_identifier (hidden);
1123 decl = build_lang_decl (VAR_DECL, hidden_name,
1124 build_qualified_type (type, TYPE_QUAL_CONST));
1125 DECL_ARTIFICIAL (decl) = 1;
1126 TREE_READONLY (decl) = 1;
1127 TREE_STATIC (decl) = 1;
1128 DECL_EXTERNAL (decl) = 0;
1132 TREE_PUBLIC (decl) = 1;
1133 if (flag_weak || !typeinfo_in_lib_p (target_type))
1134 comdat_linkage (decl);
1136 SET_DECL_ASSEMBLER_NAME (decl, name);
1137 DECL_INITIAL (decl) = init;
1138 cp_finish_decl (decl, init, NULL_TREE, 0);
1139 pushdecl_top_level (decl);
1140 TREE_USED (decl) = 1;
1144 /* Generate the RECORD_TYPE containing the data layout of a type_info
1145 derivative as used by the runtime. This layout must be consistent with
1146 that defined in the runtime support. Also generate the VAR_DECL for the
1147 type's vtable. We explicitly manage the vtable member, and name it for
1148 real type as used in the runtime. The RECORD type has a different name,
1149 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1150 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1152 REAL_NAME is the runtime's name of the type. Trailing arguments are
1153 additional FIELD_DECL's for the structure. The final argument must be
1157 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1159 #ifndef ANSI_PROTOTYPES
1160 char const *real_name;
1164 tree real_type, pseudo_type;
1172 VA_START (ap, ident);
1173 #ifndef ANSI_PROTOTYPES
1174 real_name = va_arg (ap, char const *);
1175 ident = va_arg (app, int);
1178 /* Generate the pseudo type name. */
1179 pseudo_name = (char *)alloca (strlen (real_name) + 30);
1180 strcpy (pseudo_name, real_name);
1181 strcat (pseudo_name, "_pseudo");
1183 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1185 /* Get the vtable decl. */
1186 real_type = xref_tag (class_type_node, get_identifier (real_name), 1);
1187 vtable_decl = get_vtable_decl (real_type, /*complete=*/1);
1188 vtable_decl = build_unary_op (ADDR_EXPR, vtable_decl, 0);
1190 /* We need to point into the middle of the vtable. */
1191 vtable_decl = build (PLUS_EXPR,
1192 TREE_TYPE (vtable_decl),
1194 size_binop (MULT_EXPR,
1196 TYPE_SIZE_UNIT (vtable_entry_type)));
1197 TREE_CONSTANT (vtable_decl) = 1;
1199 /* First field is the pseudo type_info base class. */
1200 fields[0] = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1202 /* Now add the derived fields. */
1203 for (ix = 0; (field_decl = va_arg (ap, tree));)
1204 fields[++ix] = field_decl;
1206 /* Create the pseudo type. */
1207 pseudo_type = make_aggr_type (RECORD_TYPE);
1208 finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1209 TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1212 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1213 TINFO_VTABLE_DECL (result) = vtable_decl;
1214 TINFO_PSEUDO_TYPE (result) = pseudo_type;
1219 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1222 get_vmi_pseudo_type_info (num_bases)
1226 tree array_domain, base_array;
1228 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1231 tree extend = make_tree_vec (num_bases + 5);
1233 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1234 TREE_VEC_ELT (extend, ix) = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1235 vmi_class_desc_type_node = extend;
1237 desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1242 /* Add number of bases and trailing array of base_class_type_info. */
1243 array_domain = build_index_type (size_int (num_bases));
1244 base_array = build_array_type (base_desc_type_node, array_domain);
1246 push_nested_namespace (abi_node);
1248 desc = create_pseudo_type_info
1249 ("__vmi_class_type_info", num_bases,
1250 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1251 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1252 build_decl (FIELD_DECL, NULL_TREE, base_array),
1255 pop_nested_namespace (abi_node);
1257 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = desc;
1261 /* Make sure the required builtin types exist for generating the type_info
1262 varable definitions. */
1265 create_tinfo_types ()
1269 if (bltn_desc_type_node)
1271 push_nested_namespace (abi_node);
1273 ptr_type_info = build_pointer_type
1274 (build_qualified_type
1275 (type_info_type_node, TYPE_QUAL_CONST));
1277 /* Create the internal type_info structure. This is used as a base for
1278 the other structures. */
1282 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1283 fields[0] = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1284 fields[1] = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1285 finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1286 fields, 1, ptr_type_node);
1287 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1290 /* Fundamental type_info */
1291 bltn_desc_type_node = create_pseudo_type_info
1292 ("__fundamental_type_info", 0,
1295 /* Array, function and enum type_info. No additional fields. */
1296 ary_desc_type_node = create_pseudo_type_info
1297 ("__array_type_info", 0,
1299 func_desc_type_node = create_pseudo_type_info
1300 ("__function_type_info", 0,
1302 enum_desc_type_node = create_pseudo_type_info
1303 ("__enum_type_info", 0,
1306 /* Class type_info. Add a flags field. */
1307 class_desc_type_node = create_pseudo_type_info
1308 ("__class_type_info", 0,
1311 /* Single public non-virtual base class. Add pointer to base class.
1312 This is really a descendant of __class_type_info. */
1313 si_class_desc_type_node = create_pseudo_type_info
1314 ("__si_class_type_info", 0,
1315 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1318 /* Base class internal helper. Pointer to base type, offset to base,
1323 fields[0] = build_decl (FIELD_DECL, NULL_TREE, ptr_type_info);
1324 fields[1] = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1325 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1326 finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1327 fields, 1, ptr_type_node);
1328 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1331 /* General heirarchy is created as necessary in this vector. */
1332 vmi_class_desc_type_node = make_tree_vec (10);
1334 /* Pointer type_info. Adds two fields, qualification mask
1335 and pointer to the pointed to type. This is really a descendant of
1336 __pbase_type_info. */
1337 ptr_desc_type_node = create_pseudo_type_info
1338 ("__pointer_type_info", 0,
1339 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1340 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1343 /* Pointer to member data type_info. Add qualifications flags,
1344 pointer to the member's type info and pointer to the class.
1345 This is really a descendant of __pbase_type_info. */
1346 ptm_desc_type_node = create_pseudo_type_info
1347 ("__pointer_to_member_type_info", 0,
1348 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1349 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1350 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1353 pop_nested_namespace (abi_node);
1356 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1357 support. Generating them here guarantees consistency with the other
1358 structures. We use the following heuristic to determine when the runtime
1359 is being generated. If std::__fundamental_type_info is defined, and its
1360 destructor is defined, then the runtime is being built. */
1363 emit_support_tinfos ()
1365 static tree *const fundamentals[] =
1370 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1371 &short_integer_type_node, &short_unsigned_type_node,
1372 &integer_type_node, &unsigned_type_node,
1373 &long_integer_type_node, &long_unsigned_type_node,
1374 &long_long_integer_type_node, &long_long_unsigned_type_node,
1375 &float_type_node, &double_type_node, &long_double_type_node,
1379 tree bltn_type, dtor;
1381 push_nested_namespace (abi_node);
1382 bltn_type = xref_tag (class_type_node,
1383 get_identifier ("__fundamental_type_info"), 1);
1384 pop_nested_namespace (abi_node);
1385 if (!COMPLETE_TYPE_P (bltn_type))
1387 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type), 1);
1388 if (DECL_EXTERNAL (dtor))
1391 for (ix = 0; fundamentals[ix]; ix++)
1393 tree bltn = *fundamentals[ix];
1394 tree bltn_ptr = build_pointer_type (bltn);
1395 tree bltn_const_ptr = build_pointer_type
1396 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1399 tinfo = get_tinfo_decl (bltn);
1400 TREE_USED (tinfo) = 1;
1401 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1403 tinfo = get_tinfo_decl (bltn_ptr);
1404 TREE_USED (tinfo) = 1;
1405 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1407 tinfo = get_tinfo_decl (bltn_const_ptr);
1408 TREE_USED (tinfo) = 1;
1409 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1413 /* Return non-zero, iff T is a type_info variable which has not had a
1414 definition emitted for it. */
1417 tinfo_decl_p (t, data)
1419 void *data ATTRIBUTE_UNUSED;
1421 return TREE_CODE (t) == VAR_DECL
1422 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == (t)
1423 && TREE_TYPE (t) == tinfo_decl_type
1424 && TREE_TYPE (DECL_NAME (t));
1427 /* Emit a suitable type_info definition for the type_info decl pointed to by
1428 DECL_PTR. We emit a completely new variable, of the correct type for the
1429 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1430 definition is set to that of the supplied decl, so that they can be tied
1431 up. Mark the supplied decl as having been dealt with. Emitting one
1432 definition might cause other definitions to be required.
1434 We need to do things this way, because we're trying to do something like
1440 extern const A tinfo_var;
1442 const B tinfo_var = {...};
1444 which is not permitted. Also, we've not necessarily seen the definition of B.
1445 So we do something like the following,
1447 extern const A tinfo_var;
1450 const void *vtable_ptr;
1458 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1460 {&B::vtable, "..."},
1464 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1468 emit_tinfo_decl (decl_ptr, data)
1470 void *data ATTRIBUTE_UNUSED;
1472 tree tinfo_decl = *decl_ptr;
1473 tree tinfo_type, decl;
1475 my_friendly_assert (TREE_TYPE (tinfo_decl) == tinfo_decl_type, 20000121);
1476 tinfo_type = TREE_TYPE (DECL_NAME (tinfo_decl));
1477 my_friendly_assert (tinfo_type != NULL_TREE, 20000120);
1479 if (!DECL_NEEDED_P (tinfo_decl))
1481 /* Say we've dealt with it. */
1482 TREE_TYPE (DECL_NAME (tinfo_decl)) = NULL_TREE;
1484 create_tinfo_types ();
1485 decl = synthesize_tinfo_var (tinfo_type, DECL_ASSEMBLER_NAME (tinfo_decl));