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_sub
PARAMS((tree
));
45 static tree build_headof
PARAMS((tree
));
46 static tree get_tinfo_var
PARAMS((tree
));
47 static tree ifnonnull
PARAMS((tree
, tree
));
48 static tree tinfo_name
PARAMS((tree
));
49 static tree get_base_offset
PARAMS((tree
, tree
));
50 static tree build_dynamic_cast_1
PARAMS((tree
, tree
));
51 static void expand_si_desc
PARAMS((tree
, tree
));
52 static void expand_class_desc
PARAMS((tree
, tree
));
53 static void expand_attr_desc
PARAMS((tree
, tree
));
54 static void expand_ptr_desc
PARAMS((tree
, tree
));
55 static void expand_generic_desc
PARAMS((tree
, tree
, const char *));
56 static tree throw_bad_cast
PARAMS((void));
57 static tree throw_bad_typeid
PARAMS((void));
58 static tree get_tinfo_decl_dynamic
PARAMS((tree
));
59 static tree tinfo_from_decl
PARAMS((tree
));
60 static int qualifier_flags
PARAMS((tree
));
61 static int target_incomplete_p
PARAMS((tree
));
62 static tree tinfo_base_init
PARAMS((tree
, tree
));
63 static tree generic_initializer
PARAMS((tree
, tree
));
64 static tree ptr_initializer
PARAMS((tree
, tree
, int *));
65 static tree ptm_initializer
PARAMS((tree
, tree
, int *));
66 static tree dfs_class_hint_mark
PARAMS ((tree
, void *));
67 static tree dfs_class_hint_unmark
PARAMS ((tree
, void *));
68 static int class_hint_flags
PARAMS((tree
));
69 static tree class_initializer
PARAMS((tree
, tree
, tree
));
70 static tree synthesize_tinfo_var
PARAMS((tree
, tree
));
71 static tree create_real_tinfo_var
PARAMS((tree
, tree
, tree
, tree
, int));
72 static tree create_pseudo_type_info
PARAMS((const char *, int, ...));
73 static tree get_vmi_pseudo_type_info
PARAMS((int));
74 static void create_tinfo_types
PARAMS((void));
75 static int typeinfo_in_lib_p
PARAMS((tree
));
77 static int doing_runtime
= 0;
80 init_rtti_processing ()
83 push_namespace (std_identifier
);
84 type_info_type_node
= xref_tag
85 (class_type_node
, get_identifier ("type_info"), 1);
88 if (!new_abi_rtti_p ())
90 tinfo_decl_id
= get_identifier ("__tf");
91 tinfo_decl_type
= build_function_type
94 (type_info_type_node
, TYPE_QUAL_CONST
)),
96 tinfo_var_id
= get_identifier ("__ti");
100 /* FIXME: These identifier prefixes are not set in stone yet. */
101 tinfo_decl_id
= get_identifier ("__ti");
102 tinfo_var_id
= get_identifier ("__tn");
103 tinfo_decl_type
= build_qualified_type
104 (type_info_type_node
, TYPE_QUAL_CONST
);
108 /* Given a pointer to an object with at least one virtual table
109 pointer somewhere, return a pointer to a possible sub-object that
110 has a virtual table pointer in it that is the vtable parent for
114 build_headof_sub (exp
)
117 tree type
= TREE_TYPE (TREE_TYPE (exp
));
118 tree basetype
= CLASSTYPE_RTTI (type
);
119 tree binfo
= get_binfo (basetype
, type
, 0);
121 exp
= convert_pointer_to_real (binfo
, exp
);
125 /* Given the expression EXP of type `class *', return the head of the
126 object pointed to by EXP with type cv void*, if the class has any
127 virtual functions (TYPE_POLYMORPHIC_P), else just return the
134 tree type
= TREE_TYPE (exp
);
139 my_friendly_assert (TREE_CODE (type
) == POINTER_TYPE
, 20000112);
140 type
= TREE_TYPE (type
);
142 if (!TYPE_POLYMORPHIC_P (type
))
144 if (CLASSTYPE_COM_INTERFACE (type
))
146 cp_error ("RTTI not supported for COM interface type `%T'", type
);
147 return error_mark_node
;
150 /* If we don't have rtti stuff, get to a sub-object that does. */
151 if (!CLASSTYPE_VFIELDS (TREE_TYPE (TREE_TYPE (exp
))))
152 exp
= build_headof_sub (exp
);
154 /* We use this a couple of times below, protect it. */
155 exp
= save_expr (exp
);
157 /* Under the new ABI, the offset-to-top field is at index -2 from
159 if (new_abi_rtti_p ())
160 index
= build_int_2 (-2, -1);
161 /* But under the old ABI, it is at offset zero. */
163 index
= integer_zero_node
;
165 aref
= build_vtbl_ref (build_indirect_ref (exp
, NULL_PTR
), index
);
167 if (flag_vtable_thunks
)
170 offset
= build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0);
172 type
= build_qualified_type (ptr_type_node
,
173 CP_TYPE_QUALS (TREE_TYPE (exp
)));
174 return build (PLUS_EXPR
, type
, exp
,
175 cp_convert (ptrdiff_type_node
, offset
));
178 /* Get a bad_cast node for the program to throw...
180 See libstdc++/exception.cc for __throw_bad_cast */
185 tree fn
= get_identifier (flag_new_abi
188 if (IDENTIFIER_GLOBAL_VALUE (fn
))
189 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
191 fn
= push_throw_library_fn (fn
, build_function_type (ptr_type_node
,
194 return build_call (fn
, NULL_TREE
);
200 tree fn
= get_identifier (flag_new_abi
202 : "__throw_bad_typeid");
203 if (IDENTIFIER_GLOBAL_VALUE (fn
))
204 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
207 tree t
= build_qualified_type (type_info_type_node
, TYPE_QUAL_CONST
);
208 t
= build_function_type (build_reference_type (t
), void_list_node
);
209 fn
= push_throw_library_fn (fn
, t
);
212 return build_call (fn
, NULL_TREE
);
215 /* Return a pointer to type_info function associated with the expression EXP.
216 If EXP is a reference to a polymorphic class, return the dynamic type;
217 otherwise return the static type of the expression. */
220 get_tinfo_decl_dynamic (exp
)
225 if (exp
== error_mark_node
)
226 return error_mark_node
;
228 type
= TREE_TYPE (exp
);
230 /* peel back references, so they match. */
231 if (TREE_CODE (type
) == REFERENCE_TYPE
)
232 type
= TREE_TYPE (type
);
234 /* Peel off cv qualifiers. */
235 type
= TYPE_MAIN_VARIANT (type
);
237 if (!VOID_TYPE_P (type
))
238 type
= complete_type_or_else (type
, exp
);
241 return error_mark_node
;
243 /* If exp is a reference to polymorphic type, get the real type_info. */
244 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
246 /* build reference to type_info from vtable. */
251 error ("taking dynamic typeid of object with -fno-rtti");
252 if (CLASSTYPE_COM_INTERFACE (type
))
254 cp_error ("RTTI not supported for COM interface type `%T'", type
);
255 return error_mark_node
;
258 /* If we don't have rtti stuff, get to a sub-object that does. */
259 if (! CLASSTYPE_VFIELDS (type
))
261 exp
= build_unary_op (ADDR_EXPR
, exp
, 0);
262 exp
= build_headof_sub (exp
);
263 exp
= build_indirect_ref (exp
, NULL_PTR
);
266 /* The RTTI information is always in the vtable, but it's at
267 different indices depending on the ABI. */
268 if (new_abi_rtti_p ())
269 index
= integer_minus_one_node
;
270 else if (flag_vtable_thunks
)
271 index
= integer_one_node
;
273 index
= integer_zero_node
;
274 t
= build_vfn_ref ((tree
*) 0, exp
, index
);
275 TREE_TYPE (t
) = build_pointer_type (tinfo_decl_type
);
279 /* otherwise return the type_info for the static type of the expr. */
280 exp
= get_tinfo_decl (TYPE_MAIN_VARIANT (type
));
281 return build_unary_op (ADDR_EXPR
, exp
, 0);
288 tree cond
= NULL_TREE
;
293 error ("cannot use typeid with -fno-rtti");
294 return error_mark_node
;
297 if (!COMPLETE_TYPE_P (type_info_type_node
))
299 error ("must #include <typeinfo> before using typeid");
300 return error_mark_node
;
303 if (processing_template_decl
)
304 return build_min_nt (TYPEID_EXPR
, exp
);
306 if (TREE_CODE (exp
) == INDIRECT_REF
307 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == POINTER_TYPE
308 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
309 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
312 exp
= stabilize_reference (exp
);
313 cond
= cp_convert (boolean_type_node
, TREE_OPERAND (exp
, 0));
316 exp
= get_tinfo_decl_dynamic (exp
);
318 if (exp
== error_mark_node
)
319 return error_mark_node
;
321 exp
= tinfo_from_decl (exp
);
325 tree bad
= throw_bad_typeid ();
327 exp
= build (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
330 return convert_from_reference (exp
);
337 tree tname
= build_overload_with_type (tinfo_var_id
, type
);
341 my_friendly_assert (!new_abi_rtti_p (), 20000118);
342 if (IDENTIFIER_GLOBAL_VALUE (tname
))
343 return IDENTIFIER_GLOBAL_VALUE (tname
);
345 /* Figure out how much space we need to allocate for the type_info object.
346 If our struct layout or the type_info classes are changed, this will
347 need to be modified. */
348 if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
349 size
= 3 * POINTER_SIZE
+ INT_TYPE_SIZE
;
350 else if (TREE_CODE (type
) == POINTER_TYPE
351 && ! (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
352 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
353 size
= 3 * POINTER_SIZE
;
354 else if (IS_AGGR_TYPE (type
))
356 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
357 size
= 2 * POINTER_SIZE
;
358 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
360 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
361 size
= 3 * POINTER_SIZE
;
363 size
= 3 * POINTER_SIZE
+ TYPE_PRECISION (sizetype
);
366 size
= 2 * POINTER_SIZE
;
368 /* The type for a character array of the appropriate size. */
369 arrtype
= build_cplus_array_type
370 (unsigned_char_type_node
,
371 build_index_type (size_int (size
/ BITS_PER_UNIT
- 1)));
373 return declare_global_var (tname
, arrtype
);
376 /* Generate the NTBS name of a type. */
385 name
= mangle_type_string (type
);
387 name
= build_overload_name (type
, 1, 1);
388 name_string
= combine_strings (build_string (strlen (name
) + 1, name
));
392 /* Returns a decl for a function or variable which can be used to obtain a
393 type_info object for TYPE. The old-abi uses functions, the new-abi
394 uses the type_info object directly. You can take the address of the
395 returned decl, to save the decl. To use the decl call
396 tinfo_from_decl. You must arrange that the decl is mark_used, if
397 actually use it --- decls in vtables are only used if the vtable is
401 get_tinfo_decl (type
)
407 if (TREE_CODE (type
) == OFFSET_TYPE
)
408 type
= TREE_TYPE (type
);
409 if (TREE_CODE (type
) == METHOD_TYPE
)
410 type
= build_function_type (TREE_TYPE (type
),
411 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
414 name
= mangle_typeinfo_for_type (type
);
416 name
= build_overload_with_type (tinfo_decl_id
, type
);
418 d
= IDENTIFIER_GLOBAL_VALUE (name
);
421 else if (!new_abi_rtti_p ())
423 /* The tinfo decl is a function returning a reference to the
425 d
= push_library_fn (name
, tinfo_decl_type
);
426 DECL_NOT_REALLY_EXTERN (d
) = 1;
427 SET_DECL_TINFO_FN_P (d
);
428 TREE_TYPE (name
) = type
;
433 /* The tinfo decl is the type_info object itself. We make all
434 tinfo objects look as type_info, even though they will end up
435 being a subclass of that when emitted. This means the we'll
436 erroneously think we know the dynamic type -- be careful in the
438 d
= build_lang_decl (VAR_DECL
, name
, tinfo_decl_type
);
440 DECL_ARTIFICIAL (d
) = 1;
441 DECL_ALIGN (d
) = TYPE_ALIGN (ptr_type_node
);
442 DECL_USER_ALIGN (d
) = 0;
443 TREE_READONLY (d
) = 1;
445 DECL_EXTERNAL (d
) = 1;
447 if (flag_weak
|| !typeinfo_in_lib_p (d
))
449 DECL_ASSEMBLER_NAME (d
) = DECL_NAME (d
);
450 cp_finish_decl (d
, NULL_TREE
, NULL_TREE
, 0);
452 pushdecl_top_level (d
);
453 /* Remember the type it is for. */
454 TREE_TYPE (name
) = type
;
455 TREE_USED (name
) = 1;
460 /* Given an expr produced by get_tinfo_decl, return an expr which
461 produces a reference to the type_info object. */
464 tinfo_from_decl (expr
)
469 if (!new_abi_rtti_p ())
470 t
= build_call (expr
, NULL_TREE
);
471 else if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
472 t
= build_indirect_ref (expr
, NULL
);
485 t
= get_tinfo_decl (type
);
486 t
= tinfo_from_decl (t
);
487 return convert_from_reference (t
);
490 /* Return the type_info object for TYPE. */
496 if (type
== error_mark_node
)
497 return error_mark_node
;
499 if (!COMPLETE_TYPE_P (type_info_type_node
))
501 error ("must #include <typeinfo> before using typeid");
502 return error_mark_node
;
505 if (processing_template_decl
)
506 return build_min_nt (TYPEID_EXPR
, type
);
508 /* If the type of the type-id is a reference type, the result of the
509 typeid expression refers to a type_info object representing the
511 if (TREE_CODE (type
) == REFERENCE_TYPE
)
512 type
= TREE_TYPE (type
);
514 /* The top-level cv-qualifiers of the lvalue expression or the type-id
515 that is the operand of typeid are always ignored. */
516 type
= TYPE_MAIN_VARIANT (type
);
518 if (!VOID_TYPE_P (type
))
519 type
= complete_type_or_else (type
, NULL_TREE
);
522 return error_mark_node
;
524 return get_typeid_1 (type
);
527 /* Check whether TEST is null before returning RESULT. If TEST is used in
528 RESULT, it must have previously had a save_expr applied to it. */
531 ifnonnull (test
, result
)
534 return build (COND_EXPR
, TREE_TYPE (result
),
535 build (EQ_EXPR
, boolean_type_node
, test
, integer_zero_node
),
536 cp_convert (TREE_TYPE (result
), integer_zero_node
),
540 /* Generate the constant expression describing where direct base BINFO
541 appears within the PARENT. How to interpret this expression depends on
542 details of the ABI, which the runtime must be aware of. */
545 get_base_offset (binfo
, parent
)
549 if (! TREE_VIA_VIRTUAL (binfo
))
550 return BINFO_OFFSET (binfo
);
551 else if (! vbase_offsets_in_vtable_p ())
557 FORMAT_VBASE_NAME (name
, BINFO_TYPE (binfo
));
558 field
= lookup_field (parent
, get_identifier (name
), 0, 0);
559 result
= byte_position (field
);
561 if (DECL_CONTEXT (field
) != parent
)
563 /* The vbase pointer might be in a non-virtual base of PARENT.
564 * Adjust for the offset of that base in PARENT. */
567 get_base_distance (DECL_CONTEXT (field
), parent
, -1, &path
);
568 result
= build (PLUS_EXPR
, TREE_TYPE (result
),
569 result
, BINFO_OFFSET (path
));
570 result
= fold (result
);
575 /* Under the new ABI, we store the vtable offset at which
576 the virtual base offset can be found. */
577 return convert (sizetype
,
578 BINFO_VPTR_FIELD (binfo_for_vbase (BINFO_TYPE (binfo
),
583 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
587 build_dynamic_cast_1 (type
, expr
)
590 enum tree_code tc
= TREE_CODE (type
);
591 tree exprtype
= TREE_TYPE (expr
);
593 tree old_expr
= expr
;
594 const char *errstr
= NULL
;
596 /* T shall be a pointer or reference to a complete class type, or
597 `pointer to cv void''. */
601 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
604 if (! IS_AGGR_TYPE (TREE_TYPE (type
)))
606 errstr
= "target is not pointer or reference to class";
609 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
611 errstr
= "target is not pointer or reference to complete type";
617 errstr
= "target is not pointer or reference";
621 if (TREE_CODE (expr
) == OFFSET_REF
)
623 expr
= resolve_offset_ref (expr
);
624 exprtype
= TREE_TYPE (expr
);
627 if (tc
== POINTER_TYPE
)
628 expr
= convert_from_reference (expr
);
629 else if (TREE_CODE (exprtype
) != REFERENCE_TYPE
)
631 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
632 exprtype
= build_reference_type (exprtype
);
633 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
634 LOOKUP_NORMAL
, NULL_TREE
);
637 exprtype
= TREE_TYPE (expr
);
639 if (tc
== POINTER_TYPE
)
641 /* If T is a pointer type, v shall be an rvalue of a pointer to
642 complete class type, and the result is an rvalue of type T. */
644 if (TREE_CODE (exprtype
) != POINTER_TYPE
)
646 errstr
= "source is not a pointer";
649 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
651 errstr
= "source is not a pointer to class";
654 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
656 errstr
= "source is a pointer to incomplete type";
662 /* T is a reference type, v shall be an lvalue of a complete class
663 type, and the result is an lvalue of the type referred to by T. */
665 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
667 errstr
= "source is not of class type";
670 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
672 errstr
= "source is of incomplete class type";
678 /* The dynamic_cast operator shall not cast away constness. */
679 if (!at_least_as_qualified_p (TREE_TYPE (type
),
680 TREE_TYPE (exprtype
)))
682 errstr
= "conversion casts away constness";
686 /* If *type is an unambiguous accessible base class of *exprtype,
687 convert statically. */
692 distance
= get_base_distance (TREE_TYPE (type
), TREE_TYPE (exprtype
), 1,
697 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
698 TREE_TYPE (exprtype
), TREE_TYPE (type
));
699 return error_mark_node
;
703 cp_error ("dynamic_cast from `%T' to private base class `%T'",
704 TREE_TYPE (exprtype
), TREE_TYPE (type
));
705 return error_mark_node
;
710 expr
= build_vbase_path (PLUS_EXPR
, type
, expr
, path
, 0);
711 if (TREE_CODE (exprtype
) == POINTER_TYPE
)
712 expr
= non_lvalue (expr
);
717 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
718 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
721 /* if TYPE is `void *', return pointer to complete object. */
722 if (tc
== POINTER_TYPE
&& VOID_TYPE_P (TREE_TYPE (type
)))
724 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
725 if (TREE_CODE (expr
) == ADDR_EXPR
726 && TREE_CODE (TREE_OPERAND (expr
, 0)) == VAR_DECL
727 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
728 return build1 (NOP_EXPR
, type
, expr
);
730 /* Since expr is used twice below, save it. */
731 expr
= save_expr (expr
);
733 expr1
= build_headof (expr
);
734 if (TREE_TYPE (expr1
) != type
)
735 expr1
= build1 (NOP_EXPR
, type
, expr1
);
736 return ifnonnull (expr
, expr1
);
741 tree result
, td2
, td3
, elems
;
742 tree static_type
, target_type
, boff
;
744 /* If we got here, we can't convert statically. Therefore,
745 dynamic_cast<D&>(b) (b an object) cannot succeed. */
746 if (tc
== REFERENCE_TYPE
)
748 if (TREE_CODE (old_expr
) == VAR_DECL
749 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
751 tree expr
= throw_bad_cast ();
752 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
754 /* Bash it to the expected type. */
755 TREE_TYPE (expr
) = type
;
759 /* Ditto for dynamic_cast<D*>(&b). */
760 else if (TREE_CODE (expr
) == ADDR_EXPR
)
762 tree op
= TREE_OPERAND (expr
, 0);
763 if (TREE_CODE (op
) == VAR_DECL
764 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
766 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
768 retval
= build_int_2 (0, 0);
769 TREE_TYPE (retval
) = type
;
774 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
775 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
776 td2
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (target_type
), 0);
777 td3
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (static_type
), 0);
779 /* Determine how T and V are related. */
780 boff
= get_dynamic_cast_base_type (static_type
, target_type
);
782 /* Since expr is used twice below, save it. */
783 expr
= save_expr (expr
);
786 if (tc
== REFERENCE_TYPE
)
787 expr1
= build_unary_op (ADDR_EXPR
, expr1
, 0);
789 if (!new_abi_rtti_p ())
791 tree expr2
= build_headof (expr1
);
794 if (tc
== POINTER_TYPE
)
795 td1
= build_indirect_ref (td1
, NULL_PTR
);
796 td1
= get_tinfo_decl_dynamic (td1
);
799 (NULL_TREE
, td1
, tree_cons
800 (NULL_TREE
, td2
, tree_cons
801 (NULL_TREE
, boff
, tree_cons
802 (NULL_TREE
, expr2
, tree_cons
803 (NULL_TREE
, td3
, tree_cons
804 (NULL_TREE
, expr1
, NULL_TREE
))))));
808 (NULL_TREE
, expr1
, tree_cons
809 (NULL_TREE
, td3
, tree_cons
810 (NULL_TREE
, td2
, tree_cons
811 (NULL_TREE
, boff
, NULL_TREE
))));
813 dcast_fn
= dynamic_cast_node
;
818 tree ns
= new_abi_rtti_p () ? abi_node
: global_namespace
;
821 push_nested_namespace (ns
);
822 if (!new_abi_rtti_p ())
824 tinfo_ptr
= build_pointer_type (tinfo_decl_type
);
825 name
= "__dynamic_cast_2";
827 (NULL_TREE
, tinfo_ptr
, tree_cons
828 (NULL_TREE
, tinfo_ptr
, tree_cons
829 (NULL_TREE
, integer_type_node
, tree_cons
830 (NULL_TREE
, ptr_type_node
, tree_cons
831 (NULL_TREE
, tinfo_ptr
, tree_cons
832 (NULL_TREE
, ptr_type_node
, void_list_node
))))));
836 tinfo_ptr
= xref_tag (class_type_node
,
837 get_identifier ("__class_type_info"),
840 tinfo_ptr
= build_pointer_type
841 (build_qualified_type
842 (tinfo_ptr
, TYPE_QUAL_CONST
));
843 name
= "__dynamic_cast";
845 (NULL_TREE
, const_ptr_type_node
, tree_cons
846 (NULL_TREE
, tinfo_ptr
, tree_cons
847 (NULL_TREE
, tinfo_ptr
, tree_cons
848 (NULL_TREE
, ptrdiff_type_node
, void_list_node
))));
850 tmp
= build_function_type (ptr_type_node
, tmp
);
851 dcast_fn
= build_library_fn_ptr (name
, tmp
);
852 pop_nested_namespace (ns
);
853 dynamic_cast_node
= dcast_fn
;
855 result
= build_call (dcast_fn
, elems
);
857 if (tc
== REFERENCE_TYPE
)
859 tree bad
= throw_bad_cast ();
861 result
= save_expr (result
);
862 return build (COND_EXPR
, type
, result
, result
, bad
);
865 /* Now back to the type we want from a void*. */
866 result
= cp_convert (type
, result
);
867 return ifnonnull (expr
, result
);
871 errstr
= "source type is not polymorphic";
874 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
875 expr
, exprtype
, type
, errstr
);
876 return error_mark_node
;
880 build_dynamic_cast (type
, expr
)
883 if (type
== error_mark_node
|| expr
== error_mark_node
)
884 return error_mark_node
;
886 if (processing_template_decl
)
887 return build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
889 return convert_from_reference (build_dynamic_cast_1 (type
, expr
));
892 /* Build and initialize various sorts of descriptors. Every descriptor
893 node has a name associated with it (the name created by mangling).
894 For this reason, we use the identifier as our access to the __*_desc
895 nodes, instead of sticking them directly in the types. Otherwise we
896 would burden all built-in types (and pointer types) with slots that
897 we don't necessarily want to use.
899 For each descriptor we build, we build a variable that contains
900 the descriptor's information. When we need this info at runtime,
901 all we need is access to these variables.
903 Note: these constructors always return the address of the descriptor
904 info, since that is simplest for their mutual interaction. */
906 /* Build an initializer for a __si_type_info node. */
909 expand_si_desc (tdecl
, type
)
914 tree name_string
= tinfo_name (type
);
916 type
= BINFO_TYPE (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0));
917 finish_expr_stmt (get_typeid_1 (type
));
918 t
= decay_conversion (get_tinfo_var (type
));
920 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
921 (NULL_TREE
, decay_conversion (name_string
), tree_cons
922 (NULL_TREE
, t
, NULL_TREE
)));
924 fn
= get_identifier ("__rtti_si");
925 if (IDENTIFIER_GLOBAL_VALUE (fn
))
926 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
931 (NULL_TREE
, ptr_type_node
, tree_cons
932 (NULL_TREE
, const_string_type_node
, tree_cons
933 (NULL_TREE
, build_pointer_type (type_info_type_node
),
935 fn
= push_void_library_fn (fn
, tmp
);
938 fn
= build_call (fn
, elems
);
939 finish_expr_stmt (fn
);
942 /* Build an initializer for a __class_type_info node. */
945 expand_class_desc (tdecl
, type
)
952 int i
= CLASSTYPE_N_BASECLASSES (type
);
954 tree binfos
= TYPE_BINFO_BASETYPES (type
);
955 tree base
, elems
, access
, offset
, isvir
;
956 tree elt
, elts
= NULL_TREE
;
958 if (base_desc_type_node
== NULL_TREE
)
962 /* A reasonably close approximation of __class_type_info::base_info */
964 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
966 /* Actually const __user_type_info * */
967 fields
[0] = build_decl
968 (FIELD_DECL
, NULL_TREE
,
969 build_pointer_type (build_qualified_type
970 (type_info_type_node
,
972 fields
[1] = build_decl
973 (FIELD_DECL
, NULL_TREE
,
974 flag_new_abi
? intSI_type_node
: unsigned_intSI_type_node
);
975 DECL_BIT_FIELD (fields
[1]) = 1;
976 DECL_SIZE (fields
[1]) = bitsize_int (29);
978 fields
[2] = build_decl (FIELD_DECL
, NULL_TREE
, boolean_type_node
);
979 DECL_BIT_FIELD (fields
[2]) = 1;
980 DECL_SIZE (fields
[2]) = bitsize_one_node
;
982 /* Actually enum access */
983 fields
[3] = build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
);
984 DECL_BIT_FIELD (fields
[3]) = 1;
985 DECL_SIZE (fields
[3]) = bitsize_int (2);
987 finish_builtin_type (base_desc_type_node
, "__base_info", fields
,
993 tree binfo
= TREE_VEC_ELT (binfos
, i
);
995 finish_expr_stmt (get_typeid_1 (BINFO_TYPE (binfo
)));
996 base
= decay_conversion (get_tinfo_var (BINFO_TYPE (binfo
)));
997 offset
= get_base_offset (binfo
, type
);
999 if (TREE_VIA_PUBLIC (binfo
))
1000 access
= access_public_node
;
1001 else if (TREE_VIA_PROTECTED (binfo
))
1002 access
= access_protected_node
;
1004 access
= access_private_node
;
1005 if (TREE_VIA_VIRTUAL (binfo
))
1006 isvir
= boolean_true_node
;
1008 isvir
= boolean_false_node
;
1011 (CONSTRUCTOR
, base_desc_type_node
, NULL_TREE
, tree_cons
1012 (NULL_TREE
, base
, tree_cons
1013 (NULL_TREE
, offset
, tree_cons
1014 (NULL_TREE
, isvir
, tree_cons
1015 (NULL_TREE
, access
, NULL_TREE
)))));
1016 TREE_HAS_CONSTRUCTOR (elt
) = TREE_CONSTANT (elt
) = TREE_STATIC (elt
) = 1;
1017 elts
= tree_cons (NULL_TREE
, elt
, elts
);
1021 name_string
= tinfo_name (type
);
1024 tree arrtype
= build_array_type (base_desc_type_node
, NULL_TREE
);
1025 elts
= build (CONSTRUCTOR
, arrtype
, NULL_TREE
, elts
);
1026 TREE_HAS_CONSTRUCTOR (elts
) = TREE_CONSTANT (elts
)
1027 = TREE_STATIC (elts
) = 1;
1028 complete_array_type (arrtype
, elts
, 1);
1032 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1033 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1034 (NULL_TREE
, decay_conversion (elts
), tree_cons
1035 (NULL_TREE
, cp_convert (sizetype
, build_int_2 (base_cnt
, 0)),
1038 fn
= get_identifier ("__rtti_class");
1039 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1040 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1044 (NULL_TREE
, ptr_type_node
, tree_cons
1045 (NULL_TREE
, const_string_type_node
, tree_cons
1046 (NULL_TREE
, build_pointer_type (base_desc_type_node
), tree_cons
1047 (NULL_TREE
, sizetype
, void_list_node
))));
1049 fn
= push_void_library_fn (fn
, tmp
);
1052 fn
= build_call (fn
, elems
);
1053 finish_expr_stmt (fn
);
1056 /* Build an initializer for a __pointer_type_info node. */
1059 expand_ptr_desc (tdecl
, type
)
1064 tree name_string
= tinfo_name (type
);
1066 type
= TREE_TYPE (type
);
1067 finish_expr_stmt (get_typeid_1 (type
));
1068 t
= decay_conversion (get_tinfo_var (type
));
1070 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1071 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1072 (NULL_TREE
, t
, NULL_TREE
)));
1074 fn
= get_identifier ("__rtti_ptr");
1075 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1076 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1081 (NULL_TREE
, ptr_type_node
, tree_cons
1082 (NULL_TREE
, const_string_type_node
, tree_cons
1083 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1085 fn
= push_void_library_fn (fn
, tmp
);
1088 fn
= build_call (fn
, elems
);
1089 finish_expr_stmt (fn
);
1092 /* Build an initializer for a __attr_type_info node. */
1095 expand_attr_desc (tdecl
, type
)
1100 tree name_string
= tinfo_name (type
);
1101 tree attrval
= build_int_2 (TYPE_QUALS (type
), 0);
1103 finish_expr_stmt (get_typeid_1 (TYPE_MAIN_VARIANT (type
)));
1104 t
= decay_conversion (get_tinfo_var (TYPE_MAIN_VARIANT (type
)));
1106 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1107 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1108 (NULL_TREE
, attrval
, tree_cons (NULL_TREE
, t
, NULL_TREE
))));
1110 fn
= get_identifier ("__rtti_attr");
1111 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1112 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1117 (NULL_TREE
, ptr_type_node
, tree_cons
1118 (NULL_TREE
, const_string_type_node
, tree_cons
1119 (NULL_TREE
, integer_type_node
, tree_cons
1120 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1122 fn
= push_void_library_fn (fn
, tmp
);
1125 fn
= build_call (fn
, elems
);
1126 finish_expr_stmt (fn
);
1129 /* Build an initializer for a type_info node that just has a name. */
1132 expand_generic_desc (tdecl
, type
, fnname
)
1137 tree name_string
= tinfo_name (type
);
1138 tree elems
= tree_cons
1139 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1140 (NULL_TREE
, decay_conversion (name_string
), NULL_TREE
));
1142 tree fn
= get_identifier (fnname
);
1143 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1144 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1149 (NULL_TREE
, ptr_type_node
, tree_cons
1150 (NULL_TREE
, const_string_type_node
, void_list_node
));
1151 fn
= push_void_library_fn (fn
, tmp
);
1154 fn
= build_call (fn
, elems
);
1155 finish_expr_stmt (fn
);
1158 /* Generate the code for a type_info initialization function.
1159 Note that we take advantage of the passage
1161 5.2.7 Type identification [expr.typeid]
1163 Whether or not the destructor is called for the type_info object at the
1164 end of the program is unspecified.
1166 and don't bother to arrange for these objects to be destroyed. It
1167 doesn't matter, anyway, since the destructors don't do anything.
1169 This must only be called from toplevel (i.e. from finish_file)! */
1172 synthesize_tinfo_fn (fndecl
)
1175 tree type
= TREE_TYPE (DECL_NAME (fndecl
));
1176 tree tmp
, addr
, tdecl
;
1181 my_friendly_assert (!new_abi_rtti_p (), 20000118);
1184 import_export_decl (fndecl
);
1185 if (DECL_REALLY_EXTERN (fndecl
))
1189 /* Declare the static typeinfo variable. */
1190 tdecl
= get_tinfo_var (type
);
1191 DECL_EXTERNAL (tdecl
) = 0;
1192 TREE_STATIC (tdecl
) = 1;
1193 DECL_COMMON (tdecl
) = 1;
1194 TREE_USED (tdecl
) = 1;
1195 DECL_ALIGN (tdecl
) = TYPE_ALIGN (ptr_type_node
);
1196 DECL_USER_ALIGN (tdecl
) = 0;
1197 cp_finish_decl (tdecl
, NULL_TREE
, NULL_TREE
, 0);
1199 /* Begin processing the function. */
1200 start_function (NULL_TREE
, fndecl
, NULL_TREE
,
1201 SF_DEFAULT
| SF_PRE_PARSED
);
1202 DECL_DEFER_OUTPUT (fndecl
) = 1;
1205 /* Begin the body of the function. */
1206 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
1208 /* For convenience, we save away the address of the static
1210 addr
= decay_conversion (tdecl
);
1212 /* If the first word of the array (the vtable) is non-zero, we've already
1213 initialized the object, so don't do it again. */
1214 if_stmt
= begin_if_stmt ();
1215 tmp
= cp_convert (build_pointer_type (ptr_type_node
), addr
);
1216 tmp
= build_indirect_ref (tmp
, 0);
1217 tmp
= cp_build_binary_op (EQ_EXPR
, tmp
, integer_zero_node
);
1218 finish_if_stmt_cond (tmp
, if_stmt
);
1219 then_clause
= begin_compound_stmt (/*has_no_scope=*/0);
1221 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1222 expand_generic_desc (tdecl
, type
, "__rtti_func");
1223 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1224 expand_generic_desc (tdecl
, type
, "__rtti_array");
1225 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1226 expand_attr_desc (tdecl
, type
);
1227 else if (TREE_CODE (type
) == POINTER_TYPE
)
1229 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
)
1230 expand_generic_desc (tdecl
, type
, "__rtti_ptmd");
1231 else if (TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
1232 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1234 expand_ptr_desc (tdecl
, type
);
1236 else if (TYPE_PTRMEMFUNC_P (type
))
1237 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1238 else if (IS_AGGR_TYPE (type
))
1240 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
1241 expand_generic_desc (tdecl
, type
, "__rtti_user");
1242 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
1244 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
1245 expand_si_desc (tdecl
, type
);
1247 expand_class_desc (tdecl
, type
);
1249 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1250 expand_generic_desc (tdecl
, type
, "__rtti_user");
1252 my_friendly_abort (252);
1254 finish_compound_stmt (/*has_no_scope=*/0, then_clause
);
1255 finish_then_clause (if_stmt
);
1258 /* OK, now return the type_info object. */
1259 tmp
= cp_convert (build_pointer_type (type_info_type_node
), addr
);
1260 tmp
= build_indirect_ref (tmp
, 0);
1261 finish_return_stmt (tmp
);
1262 /* Finish the function body. */
1263 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
1264 expand_body (finish_function (0));
1267 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
1270 qualifier_flags (type
)
1274 /* we want the qualifiers on this type, not any array core, it might have */
1275 int quals
= TYPE_QUALS (type
);
1277 if (quals
& TYPE_QUAL_CONST
)
1279 if (quals
& TYPE_QUAL_VOLATILE
)
1281 if (quals
& TYPE_QUAL_RESTRICT
)
1286 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
1287 contains a pointer to member of an incomplete class. */
1290 target_incomplete_p (type
)
1293 while (TREE_CODE (type
) == POINTER_TYPE
)
1294 if (TYPE_PTRMEM_P (type
))
1296 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type
)))
1298 type
= TYPE_PTRMEM_POINTED_TO_TYPE (type
);
1301 type
= TREE_TYPE (type
);
1302 if (!COMPLETE_OR_VOID_TYPE_P (type
))
1308 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
1309 is the vtable pointer and NTBS name. The NTBS name is emitted as a
1310 comdat const char array, so it becomes a unique key for the type. Generate
1311 and emit that VAR_DECL here. (We can't always emit the type_info itself
1312 as comdat, because of pointers to incomplete.) */
1315 tinfo_base_init (desc
, target
)
1319 tree init
= NULL_TREE
;
1325 /* Generate the NTBS array variable. */
1326 tree name_type
= build_cplus_array_type
1327 (build_qualified_type (char_type_node
, TYPE_QUAL_CONST
),
1329 tree name_string
= tinfo_name (target
);
1332 name_name
= mangle_typeinfo_string_for_type (target
);
1334 name_name
= build_overload_with_type (tinfo_var_id
, target
);
1335 name_decl
= build_lang_decl (VAR_DECL
, name_name
, name_type
);
1337 DECL_ARTIFICIAL (name_decl
) = 1;
1338 TREE_READONLY (name_decl
) = 1;
1339 TREE_STATIC (name_decl
) = 1;
1340 DECL_EXTERNAL (name_decl
) = 0;
1341 TREE_PUBLIC (name_decl
) = 1;
1342 comdat_linkage (name_decl
);
1344 /* The new ABI specifies the external name of the string
1345 containing the type's name. */
1346 DECL_ASSEMBLER_NAME (name_decl
)
1347 = mangle_typeinfo_string_for_type (target
);
1349 DECL_ASSEMBLER_NAME (name_decl
) = DECL_NAME (name_decl
);
1350 DECL_INITIAL (name_decl
) = name_string
;
1351 cp_finish_decl (name_decl
, name_string
, NULL_TREE
, 0);
1352 pushdecl_top_level (name_decl
);
1355 if (TINFO_VTABLE_DECL (desc
))
1357 tree vtbl_ptr
= TINFO_VTABLE_DECL (desc
);
1358 init
= tree_cons (NULL_TREE
, vtbl_ptr
, init
);
1361 init
= tree_cons (NULL_TREE
, decay_conversion (name_decl
), init
);
1363 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1364 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1365 init
= tree_cons (NULL_TREE
, init
, NULL_TREE
);
1370 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
1371 information about the particular type_info derivation, which adds no
1372 additional fields to the type_info base. */
1375 generic_initializer (desc
, target
)
1379 tree init
= tinfo_base_init (desc
, target
);
1381 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1382 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1386 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1387 DESC provides information about the particular type_info derivation,
1388 which adds target type and qualifier flags members to the type_info base. */
1391 ptr_initializer (desc
, target
, non_public_ptr
)
1394 int *non_public_ptr
;
1396 tree init
= tinfo_base_init (desc
, target
);
1397 tree to
= TREE_TYPE (target
);
1398 int flags
= qualifier_flags (to
);
1399 int incomplete
= target_incomplete_p (to
);
1404 *non_public_ptr
= 1;
1406 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1407 init
= tree_cons (NULL_TREE
,
1408 build_unary_op (ADDR_EXPR
,
1409 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1412 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1413 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1417 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1418 DESC provides information about the particular type_info derivation,
1419 which adds class, target type and qualifier flags members to the type_info
1423 ptm_initializer (desc
, target
, non_public_ptr
)
1426 int *non_public_ptr
;
1428 tree init
= tinfo_base_init (desc
, target
);
1429 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
1430 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
1431 int flags
= qualifier_flags (to
);
1432 int incomplete
= target_incomplete_p (to
);
1437 *non_public_ptr
= 1;
1439 if (!COMPLETE_TYPE_P (klass
))
1442 *non_public_ptr
= 1;
1444 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1445 init
= tree_cons (NULL_TREE
,
1446 build_unary_op (ADDR_EXPR
,
1447 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1449 init
= tree_cons (NULL_TREE
,
1450 build_unary_op (ADDR_EXPR
, get_tinfo_decl (klass
), 0),
1453 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1454 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1458 /* Check base BINFO to set hint flags in *DATA, which is really an int.
1459 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
1460 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
1461 possible for a type to be both a virtual and non-virtual base. */
1464 dfs_class_hint_mark (binfo
, data
)
1468 tree basetype
= BINFO_TYPE (binfo
);
1469 int *hint
= (int *) data
;
1471 if (TREE_VIA_VIRTUAL (binfo
))
1473 if (CLASSTYPE_MARKED (basetype
))
1475 if (CLASSTYPE_MARKED2 (basetype
))
1477 SET_CLASSTYPE_MARKED2 (basetype
);
1481 if (CLASSTYPE_MARKED (basetype
) || CLASSTYPE_MARKED2 (basetype
))
1483 SET_CLASSTYPE_MARKED (basetype
);
1485 if (!TREE_VIA_PUBLIC (binfo
) && TYPE_BINFO (basetype
) != binfo
)
1490 /* Clear the base's dfs marks, after searching for duplicate bases. */
1493 dfs_class_hint_unmark (binfo
, data
)
1495 void *data ATTRIBUTE_UNUSED
;
1497 tree basetype
= BINFO_TYPE (binfo
);
1499 CLEAR_CLASSTYPE_MARKED (basetype
);
1500 CLEAR_CLASSTYPE_MARKED2 (basetype
);
1504 /* Determine the hint flags describing the features of a class's heirarchy. */
1507 class_hint_flags (type
)
1513 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_mark
, NULL
, &hint_flags
);
1514 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_unmark
, NULL
, NULL
);
1516 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); ++i
)
1518 tree base_binfo
= BINFO_BASETYPE (TYPE_BINFO (type
), i
);
1520 if (TREE_VIA_PUBLIC (base_binfo
))
1526 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1527 DESC provides information about the particular __class_type_info derivation,
1528 which adds hint flags and TRAIL initializers to the type_info base. */
1531 class_initializer (desc
, target
, trail
)
1536 tree init
= tinfo_base_init (desc
, target
);
1538 TREE_CHAIN (init
) = trail
;
1539 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1540 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1544 /* Returns non-zero if the typeinfo for type should be placed in
1545 the runtime library. */
1548 typeinfo_in_lib_p (type
)
1551 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1552 library for simple types T. */
1553 if (TREE_CODE (type
) == POINTER_TYPE
1554 && (CP_TYPE_QUALS (TREE_TYPE (type
)) == TYPE_QUAL_CONST
1555 || CP_TYPE_QUALS (TREE_TYPE (type
)) == TYPE_UNQUALIFIED
))
1556 type
= TREE_TYPE (type
);
1558 switch (TREE_CODE (type
))
1572 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1573 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1574 the runtime, and therefore has additional fields. If we need not emit a
1575 definition (because the runtime must contain it), return NULL_TREE,
1576 otherwise return the VAR_DECL. */
1579 synthesize_tinfo_var (target_type
, real_name
)
1583 tree var_init
= NULL_TREE
;
1584 tree var_type
= NULL_TREE
;
1587 my_friendly_assert (new_abi_rtti_p (), 20000118);
1589 switch (TREE_CODE (target_type
))
1592 if (TYPE_PTRMEM_P (target_type
))
1594 var_type
= ptm_desc_type_node
;
1595 var_init
= ptm_initializer (var_type
, target_type
, &non_public
);
1599 if (typeinfo_in_lib_p (target_type
) && !doing_runtime
)
1600 /* These are in the runtime. */
1602 var_type
= ptr_desc_type_node
;
1603 var_init
= ptr_initializer (var_type
, target_type
, &non_public
);
1607 var_type
= enum_desc_type_node
;
1608 var_init
= generic_initializer (var_type
, target_type
);
1611 var_type
= func_desc_type_node
;
1612 var_init
= generic_initializer (var_type
, target_type
);
1615 var_type
= ary_desc_type_node
;
1616 var_init
= generic_initializer (var_type
, target_type
);
1620 if (TYPE_PTRMEMFUNC_P (target_type
))
1622 var_type
= ptm_desc_type_node
;
1623 var_init
= ptm_initializer (var_type
, target_type
, &non_public
);
1625 else if (!COMPLETE_TYPE_P (target_type
))
1627 /* Emit a non-public class_type_info. */
1629 var_type
= class_desc_type_node
;
1630 var_init
= class_initializer (var_type
, target_type
, NULL_TREE
);
1632 else if (!CLASSTYPE_N_BASECLASSES (target_type
))
1634 var_type
= class_desc_type_node
;
1635 var_init
= class_initializer (var_type
, target_type
, NULL_TREE
);
1639 /* if this has a single public non-virtual base, it's easier */
1640 tree binfo
= TYPE_BINFO (target_type
);
1641 int nbases
= BINFO_N_BASETYPES (binfo
);
1642 tree base_binfos
= BINFO_BASETYPES (binfo
);
1643 tree base_inits
= NULL_TREE
;
1644 int is_simple
= nbases
== 1;
1647 /* Generate the base information initializer. */
1648 for (ix
= nbases
; ix
--;)
1650 tree base_binfo
= TREE_VEC_ELT (base_binfos
, ix
);
1651 tree base_init
= NULL_TREE
;
1656 if (TREE_VIA_VIRTUAL (base_binfo
))
1658 if (TREE_PUBLIC (base_binfo
))
1660 tinfo
= get_tinfo_decl (BINFO_TYPE (base_binfo
));
1661 tinfo
= build_unary_op (ADDR_EXPR
, tinfo
, 0);
1662 offset
= get_base_offset (base_binfo
, target_type
);
1664 /* is it a single public inheritance? */
1665 if (is_simple
&& flags
== 2 && integer_zerop (offset
))
1667 base_inits
= tree_cons (NULL_TREE
, tinfo
, NULL_TREE
);
1672 /* combine offset and flags into one field */
1673 offset
= cp_build_binary_op (LSHIFT_EXPR
, offset
,
1674 build_int_2 (8, 0));
1675 offset
= cp_build_binary_op (BIT_IOR_EXPR
, offset
,
1676 build_int_2 (flags
, 0));
1677 base_init
= tree_cons (NULL_TREE
, offset
, base_init
);
1678 base_init
= tree_cons (NULL_TREE
, tinfo
, base_init
);
1679 base_init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_init
);
1680 base_inits
= tree_cons (NULL_TREE
, base_init
, base_inits
);
1684 var_type
= si_class_desc_type_node
;
1687 int hint
= class_hint_flags (target_type
);
1689 base_inits
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_inits
);
1690 base_inits
= tree_cons (NULL_TREE
, base_inits
, NULL_TREE
);
1691 /* Prepend the number of bases. */
1692 base_inits
= tree_cons (NULL_TREE
,
1693 build_int_2 (nbases
, 0), base_inits
);
1694 /* Prepend the hint flags. */
1695 base_inits
= tree_cons (NULL_TREE
,
1696 build_int_2 (hint
, 0), base_inits
);
1697 var_type
= get_vmi_pseudo_type_info (nbases
);
1699 var_init
= class_initializer (var_type
, target_type
, base_inits
);
1704 if (typeinfo_in_lib_p (target_type
))
1707 /* These are guaranteed to be in the runtime. */
1709 var_type
= bltn_desc_type_node
;
1710 var_init
= generic_initializer (var_type
, target_type
);
1713 my_friendly_abort (20000117);
1717 return create_real_tinfo_var (target_type
,
1718 real_name
, TINFO_PSEUDO_TYPE (var_type
),
1719 var_init
, non_public
);
1722 /* Create the real typeinfo variable. NON_PUBLIC indicates that we cannot
1723 make this variable public (comdat). */
1726 create_real_tinfo_var (target_type
, name
, type
, init
, non_public
)
1733 static int count
= 0;
1738 sprintf (hidden
, "%.*s_%d",
1739 IDENTIFIER_LENGTH (tinfo_decl_id
), IDENTIFIER_POINTER (tinfo_decl_id
),
1741 hidden_name
= get_identifier (hidden
);
1743 decl
= build_lang_decl (VAR_DECL
, hidden_name
,
1744 build_qualified_type (type
, TYPE_QUAL_CONST
));
1745 DECL_ARTIFICIAL (decl
) = 1;
1746 TREE_READONLY (decl
) = 1;
1747 TREE_STATIC (decl
) = 1;
1748 DECL_EXTERNAL (decl
) = 0;
1752 TREE_PUBLIC (decl
) = 1;
1753 if (flag_weak
|| !typeinfo_in_lib_p (target_type
))
1754 comdat_linkage (decl
);
1756 DECL_ASSEMBLER_NAME (decl
) = name
;
1757 DECL_INITIAL (decl
) = init
;
1758 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
1759 pushdecl_top_level (decl
);
1760 TREE_USED (decl
) = 1;
1764 /* Generate the RECORD_TYPE containing the data layout of a type_info
1765 derivative as used by the runtime. This layout must be consistent with
1766 that defined in the runtime support. Also generate the VAR_DECL for the
1767 type's vtable. We explicitly manage the vtable member, and name it for
1768 real type as used in the runtime. The RECORD type has a different name,
1769 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1770 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1772 REAL_NAME is the runtime's name of the type. Trailing arguments are
1773 additional FIELD_DECL's for the structure. The final argument must be
1777 create_pseudo_type_info
VPARAMS((const char *real_name
, int ident
, ...))
1779 #ifndef ANSI_PROTOTYPES
1780 char const *real_name
;
1784 tree real_type
, pseudo_type
;
1792 VA_START (ap
, ident
);
1793 #ifndef ANSI_PROTOTYPES
1794 real_name
= va_arg (ap
, char const *);
1795 ident
= va_arg (app
, int);
1798 /* Generate the pseudo type name. */
1799 pseudo_name
= (char *)alloca (strlen (real_name
) + 30);
1800 strcpy (pseudo_name
, real_name
);
1801 strcat (pseudo_name
, "_pseudo");
1803 sprintf (pseudo_name
+ strlen (pseudo_name
), "%d", ident
);
1805 /* Get the vtable decl. */
1806 real_type
= xref_tag (class_type_node
, get_identifier (real_name
), 1);
1807 vtable_decl
= get_vtable_decl (real_type
, /*complete=*/1);
1808 vtable_decl
= build_unary_op (ADDR_EXPR
, vtable_decl
, 0);
1810 /* Under the new ABI, we need to point into the middle of the
1814 vtable_decl
= build (PLUS_EXPR
,
1815 TREE_TYPE (vtable_decl
),
1817 size_binop (MULT_EXPR
,
1819 TYPE_SIZE_UNIT (vtable_entry_type
)));
1820 TREE_CONSTANT (vtable_decl
) = 1;
1823 /* First field is the pseudo type_info base class. */
1824 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, ti_desc_type_node
);
1826 /* Now add the derived fields. */
1827 for (ix
= 0; (field_decl
= va_arg (ap
, tree
));)
1828 fields
[++ix
] = field_decl
;
1830 /* Create the pseudo type. */
1831 pseudo_type
= make_aggr_type (RECORD_TYPE
);
1832 finish_builtin_type (pseudo_type
, pseudo_name
, fields
, ix
, ptr_type_node
);
1833 TYPE_HAS_CONSTRUCTOR (pseudo_type
) = 1;
1836 result
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
1837 TINFO_VTABLE_DECL (result
) = vtable_decl
;
1838 TINFO_PSEUDO_TYPE (result
) = pseudo_type
;
1843 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1846 get_vmi_pseudo_type_info (num_bases
)
1850 tree array_domain
, base_array
;
1852 if (TREE_VEC_LENGTH (vmi_class_desc_type_node
) <= num_bases
)
1855 tree extend
= make_tree_vec (num_bases
+ 5);
1857 for (ix
= TREE_VEC_LENGTH (vmi_class_desc_type_node
); ix
--;)
1858 TREE_VEC_ELT (extend
, ix
) = TREE_VEC_ELT (vmi_class_desc_type_node
, ix
);
1859 vmi_class_desc_type_node
= extend
;
1861 desc
= TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
);
1866 /* Add number of bases and trailing array of base_class_type_info. */
1867 array_domain
= build_index_type (size_int (num_bases
));
1868 base_array
= build_array_type (base_desc_type_node
, array_domain
);
1870 push_nested_namespace (abi_node
);
1872 desc
= create_pseudo_type_info
1873 ("__vmi_class_type_info", num_bases
,
1874 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1875 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1876 build_decl (FIELD_DECL
, NULL_TREE
, base_array
),
1879 pop_nested_namespace (abi_node
);
1881 TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
) = desc
;
1885 /* Make sure the required builtin types exist for generating the type_info
1886 varable definitions. */
1889 create_tinfo_types ()
1893 if (bltn_desc_type_node
)
1895 push_nested_namespace (abi_node
);
1897 ptr_type_info
= build_pointer_type
1898 (build_qualified_type
1899 (type_info_type_node
, TYPE_QUAL_CONST
));
1901 /* Create the internal type_info structure. This is used as a base for
1902 the other structures. */
1906 ti_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1907 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, const_ptr_type_node
);
1908 fields
[1] = build_decl (FIELD_DECL
, NULL_TREE
, const_string_type_node
);
1909 finish_builtin_type (ti_desc_type_node
, "__type_info_pseudo",
1910 fields
, 1, ptr_type_node
);
1911 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node
) = 1;
1914 /* Fundamental type_info */
1915 bltn_desc_type_node
= create_pseudo_type_info
1916 ("__fundamental_type_info", 0,
1919 /* Array, function and enum type_info. No additional fields. */
1920 ary_desc_type_node
= create_pseudo_type_info
1921 ("__array_type_info", 0,
1923 func_desc_type_node
= create_pseudo_type_info
1924 ("__function_type_info", 0,
1926 enum_desc_type_node
= create_pseudo_type_info
1927 ("__enum_type_info", 0,
1930 /* Class type_info. Add a flags field. */
1931 class_desc_type_node
= create_pseudo_type_info
1932 ("__class_type_info", 0,
1935 /* Single public non-virtual base class. Add pointer to base class.
1936 This is really a descendant of __class_type_info. */
1937 si_class_desc_type_node
= create_pseudo_type_info
1938 ("__si_class_type_info", 0,
1939 build_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1942 /* Base class internal helper. Pointer to base type, offset to base,
1947 fields
[0] = build_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
);
1948 fields
[1] = build_decl (FIELD_DECL
, NULL_TREE
, integer_types
[itk_long
]);
1949 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1950 finish_builtin_type (base_desc_type_node
, "__base_class_type_info_pseudo",
1951 fields
, 1, ptr_type_node
);
1952 TYPE_HAS_CONSTRUCTOR (base_desc_type_node
) = 1;
1955 /* General heirarchy is created as necessary in this vector. */
1956 vmi_class_desc_type_node
= make_tree_vec (10);
1958 /* Pointer type_info. Adds two fields, qualification mask
1959 and pointer to the pointed to type. This is really a descendant of
1960 __pbase_type_info. */
1961 ptr_desc_type_node
= create_pseudo_type_info
1962 ("__pointer_type_info", 0,
1963 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1964 build_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1967 /* Pointer to member data type_info. Add qualifications flags,
1968 pointer to the member's type info and pointer to the class.
1969 This is really a descendant of __pbase_type_info. */
1970 ptm_desc_type_node
= create_pseudo_type_info
1971 ("__pointer_to_member_type_info", 0,
1972 build_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1973 build_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1974 build_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1977 pop_nested_namespace (abi_node
);
1980 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1981 support. Generating them here guarantees consistency with the other
1982 structures. We use the following heuristic to determine when the runtime
1983 is being generated. If std::__fundamental_type_info is defined, and it's
1984 destructor is defined, then the runtime is being built. */
1987 emit_support_tinfos ()
1989 static tree
*const fundamentals
[] =
1994 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1995 &short_integer_type_node
, &short_unsigned_type_node
,
1996 &integer_type_node
, &unsigned_type_node
,
1997 &long_integer_type_node
, &long_unsigned_type_node
,
1998 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1999 &float_type_node
, &double_type_node
, &long_double_type_node
,
2003 tree bltn_type
, dtor
;
2005 push_nested_namespace (abi_node
);
2006 bltn_type
= xref_tag (class_type_node
,
2007 get_identifier ("__fundamental_type_info"), 1);
2008 pop_nested_namespace (abi_node
);
2009 if (!COMPLETE_TYPE_P (bltn_type
))
2011 dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type
), 1);
2012 if (DECL_EXTERNAL (dtor
))
2015 for (ix
= 0; fundamentals
[ix
]; ix
++)
2017 tree bltn
= *fundamentals
[ix
];
2018 tree bltn_ptr
= build_pointer_type (bltn
);
2019 tree bltn_const_ptr
= build_pointer_type
2020 (build_qualified_type (bltn
, TYPE_QUAL_CONST
));
2023 tinfo
= get_tinfo_decl (bltn
);
2024 TREE_USED (tinfo
) = 1;
2025 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
2027 tinfo
= get_tinfo_decl (bltn_ptr
);
2028 TREE_USED (tinfo
) = 1;
2029 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
2031 tinfo
= get_tinfo_decl (bltn_const_ptr
);
2032 TREE_USED (tinfo
) = 1;
2033 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
2037 /* Return non-zero, iff T is a type_info variable which has not had a
2038 definition emitted for it. */
2041 tinfo_decl_p (t
, data
)
2043 void *data ATTRIBUTE_UNUSED
;
2045 return TREE_CODE (t
) == VAR_DECL
2046 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t
)) == (t
)
2047 && TREE_TYPE (t
) == tinfo_decl_type
2048 && TREE_TYPE (DECL_NAME (t
));
2051 /* Emit a suitable type_info definition for the type_info decl pointed to by
2052 DECL_PTR. We emit a completely new variable, of the correct type for the
2053 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
2054 definition is set to that of the supplied decl, so that they can be tied
2055 up. Mark the supplied decl as having been dealt with. Emitting one
2056 definition might cause other definitions to be required.
2058 We need to do things this way, because we're trying to do something like
2064 extern const A tinfo_var;
2066 const B tinfo_var = {...};
2068 which is not permitted. Also, we've not necessarily seen the definition of B.
2069 So we do something like the following,
2071 extern const A tinfo_var;
2074 const void *vtable_ptr;
2082 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
2084 {&B::vtable, "..."},
2088 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
2092 emit_tinfo_decl (decl_ptr
, data
)
2094 void *data ATTRIBUTE_UNUSED
;
2096 tree tinfo_decl
= *decl_ptr
;
2097 tree tinfo_type
, decl
;
2099 my_friendly_assert (TREE_TYPE (tinfo_decl
) == tinfo_decl_type
, 20000121);
2100 tinfo_type
= TREE_TYPE (DECL_NAME (tinfo_decl
));
2101 my_friendly_assert (tinfo_type
!= NULL_TREE
, 20000120);
2103 if (!DECL_NEEDED_P (tinfo_decl
))
2105 /* Say we've dealt with it. */
2106 TREE_TYPE (DECL_NAME (tinfo_decl
)) = NULL_TREE
;
2108 create_tinfo_types ();
2109 decl
= synthesize_tinfo_var (tinfo_type
, DECL_ASSEMBLER_NAME (tinfo_decl
));