1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
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. */
34 #define INT_TYPE_SIZE BITS_PER_WORD
37 /* Accessors for the type_info objects. We need to remember several things
38 about each of the type_info types. The global tree nodes such as
39 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
40 the required information. */
41 /* The RECORD_TYPE of a type_info derived class. */
42 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
43 /* The VAR_DECL of the vtable for the type_info derived class. */
44 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
46 extern struct obstack permanent_obstack
;
48 static tree build_headof_sub
PARAMS((tree
));
49 static tree build_headof
PARAMS((tree
));
50 static tree get_tinfo_var
PARAMS((tree
));
51 static tree ifnonnull
PARAMS((tree
, tree
));
52 static tree tinfo_name
PARAMS((tree
));
53 static tree get_base_offset
PARAMS((tree
, tree
));
54 static tree build_dynamic_cast_1
PARAMS((tree
, tree
));
55 static void expand_si_desc
PARAMS((tree
, tree
));
56 static void expand_class_desc
PARAMS((tree
, tree
));
57 static void expand_attr_desc
PARAMS((tree
, tree
));
58 static void expand_ptr_desc
PARAMS((tree
, tree
));
59 static void expand_generic_desc
PARAMS((tree
, tree
, const char *));
60 static tree throw_bad_cast
PARAMS((void));
61 static tree throw_bad_typeid
PARAMS((void));
62 static tree get_tinfo_decl_dynamic
PARAMS((tree
));
63 static tree tinfo_from_decl
PARAMS((tree
));
64 static int qualifier_flags
PARAMS((tree
));
65 static tree tinfo_base_init
PARAMS((tree
, tree
));
66 static tree generic_initializer
PARAMS((tree
, tree
));
67 static tree ptr_initializer
PARAMS((tree
, tree
));
68 static tree ptmd_initializer
PARAMS((tree
, tree
));
69 static tree dfs_class_hint_mark
PARAMS ((tree
, void *));
70 static tree dfs_class_hint_unmark
PARAMS ((tree
, void *));
71 static int class_hint_flags
PARAMS((tree
));
72 static tree class_initializer
PARAMS((tree
, tree
, tree
));
73 static tree synthesize_tinfo_var
PARAMS((tree
, tree
));
74 static tree create_real_tinfo_var
PARAMS((tree
, tree
, tree
));
75 static tree create_pseudo_type_info
PARAMS((const char *, int, ...));
76 static tree get_vmi_pseudo_type_info
PARAMS((int));
77 static void create_tinfo_types
PARAMS((void));
79 static int doing_runtime
= 0;
82 init_rtti_processing ()
85 push_namespace (get_identifier ("std"));
86 type_info_type_node
= xref_tag
87 (class_type_node
, get_identifier ("type_info"), 1);
90 if (!new_abi_rtti_p ())
92 tinfo_decl_id
= get_identifier ("__tf");
93 tinfo_decl_type
= build_function_type
96 (type_info_type_node
, TYPE_QUAL_CONST
)),
101 tinfo_decl_id
= get_identifier ("__ti");
102 tinfo_decl_type
= build_qualified_type
103 (type_info_type_node
, TYPE_QUAL_CONST
);
105 tinfo_var_id
= get_identifier ("__ti");
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 ("__throw_bad_cast");
186 if (IDENTIFIER_GLOBAL_VALUE (fn
))
187 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
189 fn
= push_throw_library_fn (fn
, build_function_type (ptr_type_node
,
192 return build_call (fn
, NULL_TREE
);
198 tree fn
= get_identifier ("__throw_bad_typeid");
199 if (IDENTIFIER_GLOBAL_VALUE (fn
))
200 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
203 tree t
= build_qualified_type (type_info_type_node
, TYPE_QUAL_CONST
);
204 t
= build_function_type (build_reference_type (t
), void_list_node
);
205 fn
= push_throw_library_fn (fn
, t
);
208 return build_call (fn
, NULL_TREE
);
211 /* Return a pointer to type_info function associated with the expression EXP.
212 If EXP is a reference to a polymorphic class, return the dynamic type;
213 otherwise return the static type of the expression. */
216 get_tinfo_decl_dynamic (exp
)
221 if (exp
== error_mark_node
)
222 return error_mark_node
;
224 type
= TREE_TYPE (exp
);
226 /* peel back references, so they match. */
227 if (TREE_CODE (type
) == REFERENCE_TYPE
)
228 type
= TREE_TYPE (type
);
230 /* Peel off cv qualifiers. */
231 type
= TYPE_MAIN_VARIANT (type
);
233 if (type
!= void_type_node
)
234 type
= complete_type_or_else (type
, exp
);
237 return error_mark_node
;
239 /* If exp is a reference to polymorphic type, get the real type_info. */
240 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
242 /* build reference to type_info from vtable. */
247 error ("taking dynamic typeid of object with -fno-rtti");
248 if (CLASSTYPE_COM_INTERFACE (type
))
250 cp_error ("RTTI not supported for COM interface type `%T'", type
);
251 return error_mark_node
;
254 /* If we don't have rtti stuff, get to a sub-object that does. */
255 if (! CLASSTYPE_VFIELDS (type
))
257 exp
= build_unary_op (ADDR_EXPR
, exp
, 0);
258 exp
= build_headof_sub (exp
);
259 exp
= build_indirect_ref (exp
, NULL_PTR
);
262 /* The RTTI information is always in the vtable, but it's at
263 different indices depending on the ABI. */
264 if (new_abi_rtti_p ())
265 index
= minus_one_node
;
266 else if (flag_vtable_thunks
)
267 index
= integer_one_node
;
269 index
= integer_zero_node
;
270 t
= build_vfn_ref ((tree
*) 0, exp
, index
);
271 TREE_TYPE (t
) = build_pointer_type (tinfo_decl_type
);
275 /* otherwise return the type_info for the static type of the expr. */
276 exp
= get_tinfo_decl (TYPE_MAIN_VARIANT (type
));
277 return build_unary_op (ADDR_EXPR
, exp
, 0);
284 tree cond
= NULL_TREE
;
289 error ("cannot use typeid with -fno-rtti");
290 return error_mark_node
;
293 if (!COMPLETE_TYPE_P (type_info_type_node
))
295 error ("must #include <typeinfo> before using typeid");
296 return error_mark_node
;
299 if (processing_template_decl
)
300 return build_min_nt (TYPEID_EXPR
, exp
);
302 if (TREE_CODE (exp
) == INDIRECT_REF
303 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == POINTER_TYPE
304 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
305 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
308 exp
= stabilize_reference (exp
);
309 cond
= cp_convert (boolean_type_node
, TREE_OPERAND (exp
, 0));
312 exp
= get_tinfo_decl_dynamic (exp
);
314 if (exp
== error_mark_node
)
315 return error_mark_node
;
317 exp
= tinfo_from_decl (exp
);
321 tree bad
= throw_bad_typeid ();
323 exp
= build (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
326 return convert_from_reference (exp
);
333 tree tname
= build_overload_with_type (tinfo_var_id
, type
);
337 my_friendly_assert (!new_abi_rtti_p (), 20000118);
338 if (IDENTIFIER_GLOBAL_VALUE (tname
))
339 return IDENTIFIER_GLOBAL_VALUE (tname
);
341 /* Figure out how much space we need to allocate for the type_info object.
342 If our struct layout or the type_info classes are changed, this will
343 need to be modified. */
344 if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
345 size
= 3 * POINTER_SIZE
+ INT_TYPE_SIZE
;
346 else if (TREE_CODE (type
) == POINTER_TYPE
347 && ! (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
348 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
349 size
= 3 * POINTER_SIZE
;
350 else if (IS_AGGR_TYPE (type
))
352 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
353 size
= 2 * POINTER_SIZE
;
354 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
356 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
357 size
= 3 * POINTER_SIZE
;
359 size
= 3 * POINTER_SIZE
+ TYPE_PRECISION (sizetype
);
362 size
= 2 * POINTER_SIZE
;
364 /* The type for a character array of the appropriate size. */
365 arrtype
= build_cplus_array_type
366 (unsigned_char_type_node
,
367 build_index_type (size_int (size
/ BITS_PER_UNIT
- 1)));
369 return declare_global_var (tname
, arrtype
);
372 /* Generate the NTBS name of a type. */
377 const char *name
= build_overload_name (type
, 1, 1);
378 tree name_string
= combine_strings (build_string (strlen (name
) + 1, name
));
382 /* Returns a decl for a function or variable which can be used to obtain a
383 type_info object for TYPE. The old-abi uses functions, the new-abi
384 uses the type_info object directly. You can take the address of the
385 returned decl, to save the decl. To use the decl call
386 tinfo_from_decl. You must arrange that the decl is mark_used, if
387 actually use it --- decls in vtables are only used if the vtable is
391 get_tinfo_decl (type
)
397 if (TREE_CODE (type
) == OFFSET_TYPE
)
398 type
= TREE_TYPE (type
);
399 if (TREE_CODE (type
) == METHOD_TYPE
)
400 type
= build_function_type (TREE_TYPE (type
),
401 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
403 name
= build_overload_with_type (tinfo_decl_id
, type
);
405 d
= IDENTIFIER_GLOBAL_VALUE (name
);
408 else if (!new_abi_rtti_p ())
410 /* The tinfo decl is a function returning a reference to the type_info
412 d
= push_library_fn (name
, tinfo_decl_type
);
413 DECL_NOT_REALLY_EXTERN (d
) = 1;
414 SET_DECL_TINFO_FN_P (d
);
415 TREE_TYPE (name
) = type
;
420 /* The tinfo decl is the type_info object itself. We make all
421 tinfo objects look as type_info, even though they will end up
422 being a subclass of that when emitted. This means the we'll
423 erroneously think we know the dynamic type -- be careful in the
425 d
= build_lang_decl (VAR_DECL
, name
, tinfo_decl_type
);
427 DECL_ARTIFICIAL (d
) = 1;
428 DECL_ALIGN (d
) = TYPE_ALIGN (ptr_type_node
);
429 TREE_READONLY (d
) = 1;
431 DECL_EXTERNAL (d
) = 1;
434 DECL_ASSEMBLER_NAME (d
) = DECL_NAME (d
);
435 cp_finish_decl (d
, NULL_TREE
, NULL_TREE
, 0);
437 pushdecl_top_level (d
);
438 /* Remember the type it is for. */
439 TREE_TYPE (name
) = type
;
440 TREE_USED (name
) = 1;
445 /* Given an expr produced by get_tinfo_decl, return an expr which
446 produces a reference to the type_info object. */
449 tinfo_from_decl (expr
)
454 if (!new_abi_rtti_p ())
455 t
= build_call (expr
, NULL_TREE
);
456 else if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
457 t
= build_indirect_ref (expr
, NULL
);
470 t
= get_tinfo_decl (type
);
471 t
= tinfo_from_decl (t
);
472 return convert_from_reference (t
);
475 /* Return the type_info object for TYPE. */
481 if (type
== error_mark_node
)
482 return error_mark_node
;
484 if (!COMPLETE_TYPE_P (type_info_type_node
))
486 error ("must #include <typeinfo> before using typeid");
487 return error_mark_node
;
490 if (processing_template_decl
)
491 return build_min_nt (TYPEID_EXPR
, type
);
493 /* If the type of the type-id is a reference type, the result of the
494 typeid expression refers to a type_info object representing the
496 if (TREE_CODE (type
) == REFERENCE_TYPE
)
497 type
= TREE_TYPE (type
);
499 /* The top-level cv-qualifiers of the lvalue expression or the type-id
500 that is the operand of typeid are always ignored. */
501 type
= TYPE_MAIN_VARIANT (type
);
503 if (type
!= void_type_node
)
504 type
= complete_type_or_else (type
, NULL_TREE
);
507 return error_mark_node
;
509 return get_typeid_1 (type
);
512 /* Check whether TEST is null before returning RESULT. If TEST is used in
513 RESULT, it must have previously had a save_expr applied to it. */
516 ifnonnull (test
, result
)
519 return build (COND_EXPR
, TREE_TYPE (result
),
520 build (EQ_EXPR
, boolean_type_node
, test
, integer_zero_node
),
521 cp_convert (TREE_TYPE (result
), integer_zero_node
),
525 /* Generate the constant expression describing where direct base BINFO
526 appears within the PARENT. How to interpret this expression depends on
527 details of the ABI, which the runtime must be aware of. */
530 get_base_offset (binfo
, parent
)
534 if (! TREE_VIA_VIRTUAL (binfo
))
535 return BINFO_OFFSET (binfo
);
536 else if (! vbase_offsets_in_vtable_p ())
540 FORMAT_VBASE_NAME (name
, BINFO_TYPE (binfo
));
541 return byte_position (lookup_field (parent
, get_identifier (name
),
545 /* Under the new ABI, we store the vtable offset at which
546 the virtual base offset can be found. */
547 return convert (sizetype
,
548 BINFO_VPTR_FIELD (BINFO_FOR_VBASE (BINFO_TYPE (binfo
),
553 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
557 build_dynamic_cast_1 (type
, expr
)
560 enum tree_code tc
= TREE_CODE (type
);
563 tree old_expr
= expr
;
564 const char *errstr
= NULL
;
566 /* T shall be a pointer or reference to a complete class type, or
567 `pointer to cv void''. */
571 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
574 if (! IS_AGGR_TYPE (TREE_TYPE (type
)))
576 errstr
= "target is not pointer or reference to class";
579 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type
))))
581 errstr
= "target is not pointer or reference to complete type";
587 errstr
= "target is not pointer or reference";
591 if (TREE_CODE (expr
) == OFFSET_REF
)
592 expr
= resolve_offset_ref (expr
);
594 exprtype
= TREE_TYPE (expr
);
595 assert (exprtype
!= NULL_TREE
);
597 if (tc
== POINTER_TYPE
)
598 expr
= convert_from_reference (expr
);
599 else if (TREE_CODE (exprtype
) != REFERENCE_TYPE
)
601 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
602 exprtype
= build_reference_type (exprtype
);
603 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
604 LOOKUP_NORMAL
, NULL_TREE
);
607 exprtype
= TREE_TYPE (expr
);
609 if (tc
== POINTER_TYPE
)
611 /* If T is a pointer type, v shall be an rvalue of a pointer to
612 complete class type, and the result is an rvalue of type T. */
614 if (TREE_CODE (exprtype
) != POINTER_TYPE
)
616 errstr
= "source is not a pointer";
619 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
621 errstr
= "source is not a pointer to class";
624 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
626 errstr
= "source is a pointer to incomplete type";
632 /* T is a reference type, v shall be an lvalue of a complete class
633 type, and the result is an lvalue of the type referred to by T. */
635 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype
)))
637 errstr
= "source is not of class type";
640 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype
))))
642 errstr
= "source is of incomplete class type";
648 /* The dynamic_cast operator shall not cast away constness. */
649 if (!at_least_as_qualified_p (TREE_TYPE (type
),
650 TREE_TYPE (exprtype
)))
652 errstr
= "conversion casts away constness";
656 /* If *type is an unambiguous accessible base class of *exprtype,
657 convert statically. */
662 distance
= get_base_distance (TREE_TYPE (type
), TREE_TYPE (exprtype
), 1,
667 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
668 TREE_TYPE (exprtype
), TREE_TYPE (type
));
669 return error_mark_node
;
673 cp_error ("dynamic_cast from `%T' to private base class `%T'",
674 TREE_TYPE (exprtype
), TREE_TYPE (type
));
675 return error_mark_node
;
679 return build_vbase_path (PLUS_EXPR
, type
, expr
, path
, 0);
682 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
683 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
686 /* if TYPE is `void *', return pointer to complete object. */
687 if (tc
== POINTER_TYPE
688 && TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
690 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
691 if (TREE_CODE (expr
) == ADDR_EXPR
692 && TREE_CODE (TREE_OPERAND (expr
, 0)) == VAR_DECL
693 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
694 return build1 (NOP_EXPR
, type
, expr
);
696 /* Since expr is used twice below, save it. */
697 expr
= save_expr (expr
);
699 expr1
= build_headof (expr
);
700 if (TREE_TYPE (expr1
) != type
)
701 expr1
= build1 (NOP_EXPR
, type
, expr1
);
702 return ifnonnull (expr
, expr1
);
707 tree result
, td2
, td3
, elems
;
708 tree static_type
, target_type
, boff
;
710 /* If we got here, we can't convert statically. Therefore,
711 dynamic_cast<D&>(b) (b an object) cannot succeed. */
712 if (tc
== REFERENCE_TYPE
)
714 if (TREE_CODE (old_expr
) == VAR_DECL
715 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
717 tree expr
= throw_bad_cast ();
718 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
720 /* Bash it to the expected type. */
721 TREE_TYPE (expr
) = type
;
725 /* Ditto for dynamic_cast<D*>(&b). */
726 else if (TREE_CODE (expr
) == ADDR_EXPR
)
728 tree op
= TREE_OPERAND (expr
, 0);
729 if (TREE_CODE (op
) == VAR_DECL
730 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
732 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
734 retval
= build_int_2 (0, 0);
735 TREE_TYPE (retval
) = type
;
740 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
741 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
742 td2
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (target_type
), 0);
743 td3
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (static_type
), 0);
745 /* Determine how T and V are related. */
746 boff
= get_dynamic_cast_base_type (static_type
, target_type
);
748 /* Since expr is used twice below, save it. */
749 expr
= save_expr (expr
);
752 if (tc
== REFERENCE_TYPE
)
753 expr1
= build_unary_op (ADDR_EXPR
, expr1
, 0);
755 if (!new_abi_rtti_p ())
757 tree expr2
= build_headof (expr1
);
760 if (tc
== POINTER_TYPE
)
761 td1
= build_indirect_ref (td1
, NULL_PTR
);
762 td1
= get_tinfo_decl_dynamic (td1
);
765 (NULL_TREE
, td1
, tree_cons
766 (NULL_TREE
, td2
, tree_cons
767 (NULL_TREE
, boff
, tree_cons
768 (NULL_TREE
, expr2
, tree_cons
769 (NULL_TREE
, td3
, tree_cons
770 (NULL_TREE
, expr1
, NULL_TREE
))))));
774 (NULL_TREE
, expr1
, tree_cons
775 (NULL_TREE
, td3
, tree_cons
776 (NULL_TREE
, td2
, tree_cons
777 (NULL_TREE
, boff
, NULL_TREE
))));
779 dcast_fn
= dynamic_cast_node
;
784 tree ns
= new_abi_rtti_p () ? abi_node
: global_namespace
;
787 push_nested_namespace (ns
);
788 if (!new_abi_rtti_p ())
790 tinfo_ptr
= build_pointer_type (tinfo_decl_type
);
791 name
= "__dynamic_cast_2";
793 (NULL_TREE
, tinfo_ptr
, tree_cons
794 (NULL_TREE
, tinfo_ptr
, tree_cons
795 (NULL_TREE
, integer_type_node
, tree_cons
796 (NULL_TREE
, ptr_type_node
, tree_cons
797 (NULL_TREE
, tinfo_ptr
, tree_cons
798 (NULL_TREE
, ptr_type_node
, void_list_node
))))));
802 tinfo_ptr
= xref_tag (class_type_node
,
803 get_identifier ("__class_type_info"),
806 tinfo_ptr
= build_pointer_type
807 (build_qualified_type
808 (tinfo_ptr
, TYPE_QUAL_CONST
));
809 name
= "__dynamic_cast";
811 (NULL_TREE
, const_ptr_type_node
, tree_cons
812 (NULL_TREE
, tinfo_ptr
, tree_cons
813 (NULL_TREE
, tinfo_ptr
, tree_cons
814 (NULL_TREE
, ptrdiff_type_node
, void_list_node
))));
816 tmp
= build_function_type (ptr_type_node
, tmp
);
817 if (new_abi_rtti_p ())
818 /* We want its name mangling. */
819 dcast_fn
= build_cp_library_fn_ptr (name
, tmp
);
821 dcast_fn
= build_library_fn_ptr (name
, tmp
);
822 pop_nested_namespace (ns
);
823 dynamic_cast_node
= dcast_fn
;
825 result
= build_call (dcast_fn
, elems
);
827 if (tc
== REFERENCE_TYPE
)
829 tree bad
= throw_bad_cast ();
831 result
= save_expr (result
);
832 return build (COND_EXPR
, type
, result
, result
, bad
);
835 /* Now back to the type we want from a void*. */
836 result
= cp_convert (type
, result
);
837 return ifnonnull (expr
, result
);
841 errstr
= "source type is not polymorphic";
844 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
845 expr
, exprtype
, type
, errstr
);
846 return error_mark_node
;
850 build_dynamic_cast (type
, expr
)
853 if (type
== error_mark_node
|| expr
== error_mark_node
)
854 return error_mark_node
;
856 if (processing_template_decl
)
857 return build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
859 return convert_from_reference (build_dynamic_cast_1 (type
, expr
));
862 /* Build and initialize various sorts of descriptors. Every descriptor
863 node has a name associated with it (the name created by mangling).
864 For this reason, we use the identifier as our access to the __*_desc
865 nodes, instead of sticking them directly in the types. Otherwise we
866 would burden all built-in types (and pointer types) with slots that
867 we don't necessarily want to use.
869 For each descriptor we build, we build a variable that contains
870 the descriptor's information. When we need this info at runtime,
871 all we need is access to these variables.
873 Note: these constructors always return the address of the descriptor
874 info, since that is simplest for their mutual interaction. */
876 /* Build an initializer for a __si_type_info node. */
879 expand_si_desc (tdecl
, type
)
884 tree name_string
= tinfo_name (type
);
886 type
= BINFO_TYPE (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0));
887 finish_expr_stmt (get_typeid_1 (type
));
888 t
= decay_conversion (get_tinfo_var (type
));
890 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
891 (NULL_TREE
, decay_conversion (name_string
), tree_cons
892 (NULL_TREE
, t
, NULL_TREE
)));
894 fn
= get_identifier ("__rtti_si");
895 if (IDENTIFIER_GLOBAL_VALUE (fn
))
896 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
901 (NULL_TREE
, ptr_type_node
, tree_cons
902 (NULL_TREE
, const_string_type_node
, tree_cons
903 (NULL_TREE
, build_pointer_type (type_info_type_node
),
905 fn
= push_void_library_fn (fn
, tmp
);
908 fn
= build_call (fn
, elems
);
909 finish_expr_stmt (fn
);
912 /* Build an initializer for a __class_type_info node. */
915 expand_class_desc (tdecl
, type
)
922 int i
= CLASSTYPE_N_BASECLASSES (type
);
924 tree binfos
= TYPE_BINFO_BASETYPES (type
);
926 /* See code below that used these. */
927 tree vb
= CLASSTYPE_VBASECLASSES (type
);
930 tree base
, elems
, access
, offset
, isvir
;
931 tree elt
, elts
= NULL_TREE
;
933 if (base_desc_type_node
== NULL_TREE
)
937 /* A reasonably close approximation of __class_type_info::base_info */
939 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
941 /* Actually const __user_type_info * */
942 fields
[0] = build_lang_decl
943 (FIELD_DECL
, NULL_TREE
,
944 build_pointer_type (build_qualified_type
945 (type_info_type_node
,
947 fields
[1] = build_lang_decl
948 (FIELD_DECL
, NULL_TREE
,
949 flag_new_abi
? intSI_type_node
: unsigned_intSI_type_node
);
950 DECL_BIT_FIELD (fields
[1]) = 1;
951 DECL_SIZE (fields
[1]) = bitsize_int (29);
953 fields
[2] = build_lang_decl (FIELD_DECL
, NULL_TREE
, boolean_type_node
);
954 DECL_BIT_FIELD (fields
[2]) = 1;
955 DECL_SIZE (fields
[2]) = bitsize_one_node
;
957 /* Actually enum access */
958 fields
[3] = build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
);
959 DECL_BIT_FIELD (fields
[3]) = 1;
960 DECL_SIZE (fields
[3]) = bitsize_int (2);
962 finish_builtin_type (base_desc_type_node
, "__base_info", fields
,
968 tree binfo
= TREE_VEC_ELT (binfos
, i
);
970 finish_expr_stmt (get_typeid_1 (BINFO_TYPE (binfo
)));
971 base
= decay_conversion (get_tinfo_var (BINFO_TYPE (binfo
)));
972 offset
= get_base_offset (binfo
, type
);
974 if (TREE_VIA_PUBLIC (binfo
))
975 access
= access_public_node
;
976 else if (TREE_VIA_PROTECTED (binfo
))
977 access
= access_protected_node
;
979 access
= access_private_node
;
980 if (TREE_VIA_VIRTUAL (binfo
))
981 isvir
= boolean_true_node
;
983 isvir
= boolean_false_node
;
986 (CONSTRUCTOR
, base_desc_type_node
, NULL_TREE
, tree_cons
987 (NULL_TREE
, base
, tree_cons
988 (NULL_TREE
, offset
, tree_cons
989 (NULL_TREE
, isvir
, tree_cons
990 (NULL_TREE
, access
, NULL_TREE
)))));
991 TREE_HAS_CONSTRUCTOR (elt
) = TREE_CONSTANT (elt
) = TREE_STATIC (elt
) = 1;
992 elts
= tree_cons (NULL_TREE
, elt
, elts
);
1000 access
= access_public_node
;
1003 b
= TREE_VEC_ELT (binfos
, i
);
1004 if (BINFO_TYPE (vb
) == BINFO_TYPE (b
) && TREE_VIA_VIRTUAL (b
))
1006 if (TREE_VIA_PUBLIC (b
))
1007 access
= access_public_node
;
1008 else if (TREE_VIA_PROTECTED (b
))
1009 access
= access_protected_node
;
1011 access
= access_private_node
;
1015 base
= build_t_desc (BINFO_TYPE (vb
), 1);
1016 offset
= BINFO_OFFSET (vb
);
1017 isvir
= build_int_2 (1, 0);
1019 base_list
= tree_cons (NULL_TREE
, base
, base_list
);
1020 isvir_list
= tree_cons (NULL_TREE
, isvir
, isvir_list
);
1021 acc_list
= tree_cons (NULL_TREE
, access
, acc_list
);
1022 off_list
= tree_cons (NULL_TREE
, offset
, off_list
);
1025 vb
= TREE_CHAIN (vb
);
1029 name_string
= tinfo_name (type
);
1032 tree arrtype
= build_array_type (base_desc_type_node
, NULL_TREE
);
1033 elts
= build (CONSTRUCTOR
, arrtype
, NULL_TREE
, elts
);
1034 TREE_HAS_CONSTRUCTOR (elts
) = TREE_CONSTANT (elts
)
1035 = TREE_STATIC (elts
) = 1;
1036 complete_array_type (arrtype
, elts
, 1);
1040 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1041 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1042 (NULL_TREE
, decay_conversion (elts
), tree_cons
1043 (NULL_TREE
, cp_convert (sizetype
, build_int_2 (base_cnt
, 0)),
1046 fn
= get_identifier ("__rtti_class");
1047 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1048 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1052 (NULL_TREE
, ptr_type_node
, tree_cons
1053 (NULL_TREE
, const_string_type_node
, tree_cons
1054 (NULL_TREE
, build_pointer_type (base_desc_type_node
), tree_cons
1055 (NULL_TREE
, sizetype
, void_list_node
))));
1057 fn
= push_void_library_fn (fn
, tmp
);
1060 fn
= build_call (fn
, elems
);
1061 finish_expr_stmt (fn
);
1064 /* Build an initializer for a __pointer_type_info node. */
1067 expand_ptr_desc (tdecl
, type
)
1072 tree name_string
= tinfo_name (type
);
1074 type
= TREE_TYPE (type
);
1075 finish_expr_stmt (get_typeid_1 (type
));
1076 t
= decay_conversion (get_tinfo_var (type
));
1078 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1079 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1080 (NULL_TREE
, t
, NULL_TREE
)));
1082 fn
= get_identifier ("__rtti_ptr");
1083 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1084 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1089 (NULL_TREE
, ptr_type_node
, tree_cons
1090 (NULL_TREE
, const_string_type_node
, tree_cons
1091 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1093 fn
= push_void_library_fn (fn
, tmp
);
1096 fn
= build_call (fn
, elems
);
1097 finish_expr_stmt (fn
);
1100 /* Build an initializer for a __attr_type_info node. */
1103 expand_attr_desc (tdecl
, type
)
1108 tree name_string
= tinfo_name (type
);
1109 tree attrval
= build_int_2 (TYPE_QUALS (type
), 0);
1111 finish_expr_stmt (get_typeid_1 (TYPE_MAIN_VARIANT (type
)));
1112 t
= decay_conversion (get_tinfo_var (TYPE_MAIN_VARIANT (type
)));
1114 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1115 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1116 (NULL_TREE
, attrval
, tree_cons (NULL_TREE
, t
, NULL_TREE
))));
1118 fn
= get_identifier ("__rtti_attr");
1119 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1120 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1125 (NULL_TREE
, ptr_type_node
, tree_cons
1126 (NULL_TREE
, const_string_type_node
, tree_cons
1127 (NULL_TREE
, integer_type_node
, tree_cons
1128 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1130 fn
= push_void_library_fn (fn
, tmp
);
1133 fn
= build_call (fn
, elems
);
1134 finish_expr_stmt (fn
);
1137 /* Build an initializer for a type_info node that just has a name. */
1140 expand_generic_desc (tdecl
, type
, fnname
)
1145 tree name_string
= tinfo_name (type
);
1146 tree elems
= tree_cons
1147 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1148 (NULL_TREE
, decay_conversion (name_string
), NULL_TREE
));
1150 tree fn
= get_identifier (fnname
);
1151 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1152 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1157 (NULL_TREE
, ptr_type_node
, tree_cons
1158 (NULL_TREE
, const_string_type_node
, void_list_node
));
1159 fn
= push_void_library_fn (fn
, tmp
);
1162 fn
= build_call (fn
, elems
);
1163 finish_expr_stmt (fn
);
1166 /* Generate the code for a type_info initialization function.
1167 Note that we take advantage of the passage
1169 5.2.7 Type identification [expr.typeid]
1171 Whether or not the destructor is called for the type_info object at the
1172 end of the program is unspecified.
1174 and don't bother to arrange for these objects to be destroyed. It
1175 doesn't matter, anyway, since the destructors don't do anything.
1177 This must only be called from toplevel (i.e. from finish_file)! */
1180 synthesize_tinfo_fn (fndecl
)
1183 tree type
= TREE_TYPE (DECL_NAME (fndecl
));
1184 tree tmp
, addr
, tdecl
;
1189 my_friendly_assert (!new_abi_rtti_p (), 20000118);
1192 import_export_decl (fndecl
);
1193 if (DECL_REALLY_EXTERN (fndecl
))
1197 /* Declare the static typeinfo variable. */
1198 tdecl
= get_tinfo_var (type
);
1199 DECL_EXTERNAL (tdecl
) = 0;
1200 TREE_STATIC (tdecl
) = 1;
1201 DECL_COMMON (tdecl
) = 1;
1202 TREE_USED (tdecl
) = 1;
1203 DECL_ALIGN (tdecl
) = TYPE_ALIGN (ptr_type_node
);
1204 cp_finish_decl (tdecl
, NULL_TREE
, NULL_TREE
, 0);
1206 /* Begin processing the function. */
1207 start_function (NULL_TREE
, fndecl
, NULL_TREE
,
1208 SF_DEFAULT
| SF_PRE_PARSED
);
1209 DECL_DEFER_OUTPUT (fndecl
) = 1;
1210 store_parm_decls ();
1213 /* Begin the body of the function. */
1214 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
1216 /* For convenience, we save away the address of the static
1218 addr
= decay_conversion (tdecl
);
1220 /* If the first word of the array (the vtable) is non-zero, we've already
1221 initialized the object, so don't do it again. */
1222 if_stmt
= begin_if_stmt ();
1223 tmp
= cp_convert (build_pointer_type (ptr_type_node
), addr
);
1224 tmp
= build_indirect_ref (tmp
, 0);
1225 tmp
= build_binary_op (EQ_EXPR
, tmp
, integer_zero_node
);
1226 finish_if_stmt_cond (tmp
, if_stmt
);
1227 then_clause
= begin_compound_stmt (/*has_no_scope=*/0);
1229 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1230 expand_generic_desc (tdecl
, type
, "__rtti_func");
1231 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1232 expand_generic_desc (tdecl
, type
, "__rtti_array");
1233 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1234 expand_attr_desc (tdecl
, type
);
1235 else if (TREE_CODE (type
) == POINTER_TYPE
)
1237 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
)
1238 expand_generic_desc (tdecl
, type
, "__rtti_ptmd");
1239 else if (TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
1240 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1242 expand_ptr_desc (tdecl
, type
);
1244 else if (TYPE_PTRMEMFUNC_P (type
))
1245 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1246 else if (IS_AGGR_TYPE (type
))
1248 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
1249 expand_generic_desc (tdecl
, type
, "__rtti_user");
1250 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
1252 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
1253 expand_si_desc (tdecl
, type
);
1255 expand_class_desc (tdecl
, type
);
1257 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1258 expand_generic_desc (tdecl
, type
, "__rtti_user");
1260 my_friendly_abort (252);
1262 finish_compound_stmt (/*has_no_scope=*/0, then_clause
);
1263 finish_then_clause (if_stmt
);
1266 /* OK, now return the type_info object. */
1267 tmp
= cp_convert (build_pointer_type (type_info_type_node
), addr
);
1268 tmp
= build_indirect_ref (tmp
, 0);
1269 finish_return_stmt (tmp
);
1270 /* Finish the function body. */
1271 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
1272 expand_body (finish_function (lineno
, 0));
1275 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
1278 qualifier_flags (type
)
1282 /* we want the qualifiers on this type, not any array core, it might have */
1283 int quals
= TYPE_QUALS (type
);
1285 if (quals
& TYPE_QUAL_CONST
)
1287 if (quals
& TYPE_QUAL_VOLATILE
)
1292 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
1293 is the vtable pointer and NTBS name. */
1296 tinfo_base_init (desc
, target
)
1300 tree name_string
= tinfo_name (target
);
1301 tree init
= NULL_TREE
;
1303 if (TINFO_VTABLE_DECL (desc
))
1305 tree vtbl_ptr
= TINFO_VTABLE_DECL (desc
);
1306 init
= tree_cons (NULL_TREE
, vtbl_ptr
, init
);
1309 init
= tree_cons (NULL_TREE
, decay_conversion (name_string
), init
);
1311 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1312 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1313 init
= tree_cons (NULL_TREE
, init
, NULL_TREE
);
1318 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
1319 information about the particular type_info derivation, which adds no
1320 additional fields to the type_info base. */
1323 generic_initializer (desc
, target
)
1327 tree init
= tinfo_base_init (desc
, target
);
1329 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1330 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1334 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
1335 DESC provides information about the particular type_info derivation,
1336 which adds target type and qualifier flags members to the type_info base. */
1339 ptr_initializer (desc
, target
)
1343 tree init
= tinfo_base_init (desc
, target
);
1344 tree to
= TREE_TYPE (target
);
1345 int flags
= qualifier_flags (to
);
1347 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1348 init
= tree_cons (NULL_TREE
,
1349 build_unary_op (ADDR_EXPR
,
1350 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1353 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1354 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1358 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
1359 DESC provides information about the particular type_info derivation,
1360 which adds target type and qualifier flags members to the type_info base. */
1363 ptmd_initializer (desc
, target
)
1367 tree init
= tinfo_base_init (desc
, target
);
1368 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
1369 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
1370 int flags
= qualifier_flags (to
);
1372 init
= tree_cons (NULL_TREE
,
1373 build_unary_op (ADDR_EXPR
, get_tinfo_decl (klass
), 0),
1375 init
= tree_cons (NULL_TREE
,
1376 build_unary_op (ADDR_EXPR
,
1377 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1379 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1381 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1382 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1386 /* Check base BINFO to set hint flags in *DATA, which is really an int.
1387 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
1388 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
1389 possible for a type to be both a virtual and non-virtual base. */
1392 dfs_class_hint_mark (binfo
, data
)
1396 tree basetype
= BINFO_TYPE (binfo
);
1397 int *hint
= (int *) data
;
1399 if (TREE_VIA_VIRTUAL (binfo
))
1401 if (CLASSTYPE_MARKED (basetype
))
1403 if (CLASSTYPE_MARKED2 (basetype
))
1405 SET_CLASSTYPE_MARKED2 (basetype
);
1409 if (CLASSTYPE_MARKED (basetype
) || CLASSTYPE_MARKED2 (basetype
))
1411 SET_CLASSTYPE_MARKED (basetype
);
1413 if (!TREE_VIA_PUBLIC (binfo
) && TYPE_BINFO (basetype
) != binfo
)
1418 /* Clear the base's dfs marks, after searching for duplicate bases. */
1421 dfs_class_hint_unmark (binfo
, data
)
1423 void *data ATTRIBUTE_UNUSED
;
1425 tree basetype
= BINFO_TYPE (binfo
);
1427 CLEAR_CLASSTYPE_MARKED (basetype
);
1428 CLEAR_CLASSTYPE_MARKED2 (basetype
);
1432 /* Determine the hint flags describing the features of a class's heirarchy. */
1435 class_hint_flags (type
)
1441 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_mark
, NULL
, &hint_flags
);
1442 dfs_walk (TYPE_BINFO (type
), dfs_class_hint_unmark
, NULL
, NULL
);
1444 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); ++i
)
1446 tree base_binfo
= BINFO_BASETYPE (TYPE_BINFO (type
), i
);
1448 if (TREE_VIA_PUBLIC (base_binfo
))
1454 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
1455 DESC provides information about the particular __class_type_info derivation,
1456 which adds hint flags and TRAIL initializers to the type_info base. */
1459 class_initializer (desc
, target
, trail
)
1464 tree init
= tinfo_base_init (desc
, target
);
1466 TREE_CHAIN (init
) = trail
;
1467 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1468 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1472 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1473 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1474 the runtime, and therefore has additional fields. If we need not emit a
1475 definition (because the runtime must contain it), return NULL_TREE,
1476 otherwise return the VAR_DECL. */
1479 synthesize_tinfo_var (target_type
, real_name
)
1483 tree var_init
= NULL_TREE
;
1484 tree var_type
= NULL_TREE
;
1486 my_friendly_assert (new_abi_rtti_p (), 20000118);
1488 switch (TREE_CODE (target_type
))
1491 if (TYPE_PTRMEM_P (target_type
))
1493 var_type
= ptmd_desc_type_node
;
1494 var_init
= ptmd_initializer (var_type
, target_type
);
1498 int code
= TREE_CODE (TREE_TYPE (target_type
));
1500 if ((CP_TYPE_QUALS (TREE_TYPE (target_type
)) | TYPE_QUAL_CONST
)
1502 && (code
== INTEGER_TYPE
|| code
== BOOLEAN_TYPE
1503 || code
== CHAR_TYPE
|| code
== REAL_TYPE
1504 || code
== VOID_TYPE
)
1506 /* These are in the runtime. */
1508 var_type
= ptr_desc_type_node
;
1509 var_init
= ptr_initializer (var_type
, target_type
);
1513 var_type
= enum_desc_type_node
;
1514 var_init
= generic_initializer (var_type
, target_type
);
1517 var_type
= func_desc_type_node
;
1518 var_init
= generic_initializer (var_type
, target_type
);
1521 var_type
= ary_desc_type_node
;
1522 var_init
= generic_initializer (var_type
, target_type
);
1526 if (!COMPLETE_TYPE_P (target_type
))
1528 /* FIXME: incomplete type. Awaiting specification. */
1531 else if (!CLASSTYPE_N_BASECLASSES (target_type
))
1533 var_type
= class_desc_type_node
;
1534 var_init
= class_initializer (var_type
, target_type
, NULL_TREE
);
1538 /* if this has a single public non-virtual base, it's easier */
1539 tree binfo
= TYPE_BINFO (target_type
);
1540 int nbases
= BINFO_N_BASETYPES (binfo
);
1541 tree base_binfos
= BINFO_BASETYPES (binfo
);
1542 tree base_inits
= NULL_TREE
;
1543 int is_simple
= nbases
== 1;
1546 /* Generate the base information initializer. */
1547 for (ix
= nbases
; ix
--;)
1549 tree base_binfo
= TREE_VEC_ELT (base_binfos
, ix
);
1550 tree base_init
= NULL_TREE
;
1555 if (TREE_VIA_VIRTUAL (base_binfo
))
1557 if (TREE_PUBLIC (base_binfo
))
1559 tinfo
= get_tinfo_decl (BINFO_TYPE (base_binfo
));
1560 tinfo
= build_unary_op (ADDR_EXPR
, tinfo
, 0);
1561 offset
= get_base_offset (base_binfo
, target_type
);
1563 /* is it a single public inheritance? */
1564 if (is_simple
&& flags
== 2 && integer_zerop (offset
))
1566 base_inits
= tree_cons (NULL_TREE
, tinfo
, NULL_TREE
);
1571 /* combine offset and flags into one field */
1572 offset
= build_binary_op (LSHIFT_EXPR
, offset
,
1573 build_int_2 (8, 0));
1574 offset
= build_binary_op (BIT_IOR_EXPR
, offset
,
1575 build_int_2 (flags
, 0));
1576 base_init
= tree_cons (NULL_TREE
, offset
, base_init
);
1577 base_init
= tree_cons (NULL_TREE
, tinfo
, base_init
);
1578 base_init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_init
);
1579 base_inits
= tree_cons (NULL_TREE
, base_init
, base_inits
);
1583 var_type
= si_class_desc_type_node
;
1586 int hint
= class_hint_flags (target_type
);
1588 base_inits
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_inits
);
1589 base_inits
= tree_cons (NULL_TREE
, base_inits
, NULL_TREE
);
1590 /* Prepend the number of bases. */
1591 base_inits
= tree_cons (NULL_TREE
,
1592 build_int_2 (nbases
, 0), base_inits
);
1593 /* Prepend the hint flags. */
1594 base_inits
= tree_cons (NULL_TREE
,
1595 build_int_2 (hint
, 0), base_inits
);
1596 var_type
= get_vmi_pseudo_type_info (nbases
);
1598 var_init
= class_initializer (var_type
, target_type
, base_inits
);
1607 /* These are guaranteed to be in the runtime. */
1609 var_type
= bltn_desc_type_node
;
1610 var_init
= generic_initializer (var_type
, target_type
);
1613 my_friendly_abort (20000117);
1616 return create_real_tinfo_var (real_name
, TINFO_PSEUDO_TYPE (var_type
), var_init
);
1619 /* Create the real typeinfo variable. */
1622 create_real_tinfo_var (name
, type
, init
)
1629 decl
= build_lang_decl (VAR_DECL
, name
,
1630 build_qualified_type (type
, TYPE_QUAL_CONST
));
1631 DECL_ARTIFICIAL (decl
) = 1;
1632 TREE_READONLY (decl
) = 1;
1633 TREE_STATIC (decl
) = 1;
1634 TREE_PUBLIC (decl
) = 1;
1635 DECL_EXTERNAL (decl
) = 0;
1637 comdat_linkage (decl
);
1638 DECL_ASSEMBLER_NAME (decl
) = name
;
1639 DECL_INITIAL (decl
) = init
;
1640 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
1645 /* Generate the RECORD_TYPE containing the data layout of a type_info
1646 derivative as used by the runtime. This layout must be consistent with
1647 that defined in the runtime support. Also generate the VAR_DECL for the
1648 type's vtable. We explicitly manage the vtable member, and name it for
1649 real type as used in the runtime. The RECORD type has a different name,
1650 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1651 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1653 REAL_NAME is the runtime's name of the type. Trailing arguments are
1654 additional FIELD_DECL's for the structure. The final argument must be
1658 create_pseudo_type_info
VPARAMS((const char *real_name
, int ident
, ...))
1660 #ifndef ANSI_PROTOTYPES
1661 char const *real_name
;
1665 tree real_type
, pseudo_type
;
1673 VA_START (ap
, ident
);
1674 #ifndef ANSI_PROTOTYPES
1675 real_name
= va_arg (ap
, char const *);
1676 ident
= va_arg (app
, int);
1679 /* Generate the pseudo type name. */
1680 pseudo_name
= (char *)alloca (strlen (real_name
) + 30);
1681 strcpy (pseudo_name
, real_name
);
1682 strcat (pseudo_name
, "_pseudo");
1684 sprintf (pseudo_name
+ strlen (pseudo_name
), "%d", ident
);
1686 /* Get the vtable decl. */
1687 real_type
= xref_tag (class_type_node
, get_identifier (real_name
), 1);
1688 vtable_decl
= get_vtable_decl (real_type
, /*complete=*/1);
1689 vtable_decl
= build_unary_op (ADDR_EXPR
, vtable_decl
, 0);
1691 /* Under the new ABI, we need to point into the middle of the
1695 vtable_decl
= build (PLUS_EXPR
,
1696 TREE_TYPE (vtable_decl
),
1698 size_binop (MULT_EXPR
,
1700 TYPE_SIZE_UNIT (vtable_entry_type
)));
1701 TREE_CONSTANT (vtable_decl
) = 1;
1704 /* First field is the pseudo type_info base class. */
1705 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, ti_desc_type_node
);
1707 /* Now add the derived fields. */
1708 for (ix
= 0; (field_decl
= va_arg (ap
, tree
));)
1709 fields
[++ix
] = field_decl
;
1711 /* Create the pseudo type. */
1712 pseudo_type
= make_aggr_type (RECORD_TYPE
);
1713 finish_builtin_type (pseudo_type
, pseudo_name
, fields
, ix
, ptr_type_node
);
1714 TYPE_HAS_CONSTRUCTOR (pseudo_type
) = 1;
1717 result
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
1718 TINFO_VTABLE_DECL (result
) = vtable_decl
;
1719 TINFO_PSEUDO_TYPE (result
) = pseudo_type
;
1724 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1727 get_vmi_pseudo_type_info (num_bases
)
1731 tree array_domain
, base_array
;
1733 if (TREE_VEC_LENGTH (vmi_class_desc_type_node
) <= num_bases
)
1736 tree extend
= make_tree_vec (num_bases
+ 5);
1738 for (ix
= TREE_VEC_LENGTH (vmi_class_desc_type_node
); ix
--;)
1739 TREE_VEC_ELT (extend
, ix
) = TREE_VEC_ELT (vmi_class_desc_type_node
, ix
);
1740 vmi_class_desc_type_node
= extend
;
1742 desc
= TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
);
1747 /* Add number of bases and trailing array of base_class_type_info. */
1748 array_domain
= build_index_type (build_int_2 (num_bases
, 0));
1749 base_array
= build_array_type (base_desc_type_node
, array_domain
);
1751 push_nested_namespace (abi_node
);
1753 desc
= create_pseudo_type_info
1754 ("__vmi_class_type_info", num_bases
,
1755 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1756 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1757 build_lang_decl (FIELD_DECL
, NULL_TREE
, base_array
),
1760 pop_nested_namespace (abi_node
);
1762 TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
) = desc
;
1766 /* Make sure the required builtin types exist for generating the type_info
1767 varable definitions. */
1770 create_tinfo_types ()
1774 if (bltn_desc_type_node
)
1776 push_nested_namespace (abi_node
);
1778 ptr_type_info
= build_pointer_type
1779 (build_qualified_type
1780 (type_info_type_node
, TYPE_QUAL_CONST
));
1782 /* Create the internal type_info structure. This is used as a base for
1783 the other structures. */
1787 ti_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1788 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, const_ptr_type_node
);
1789 fields
[1] = build_lang_decl (FIELD_DECL
, NULL_TREE
, const_string_type_node
);
1790 finish_builtin_type (ti_desc_type_node
, "__type_info_pseudo",
1791 fields
, 1, ptr_type_node
);
1792 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node
) = 1;
1795 /* Fundamental type_info */
1796 bltn_desc_type_node
= create_pseudo_type_info
1797 ("__fundamental_type_info", 0,
1800 /* Pointer type_info. Adds two fields, qualification mask
1801 and pointer to the pointed to type. */
1802 ptr_desc_type_node
= create_pseudo_type_info
1803 ("__pointer_type_info", 0,
1804 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1805 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1808 /* Array, function and enum type_info. No additional fields. */
1809 ary_desc_type_node
= create_pseudo_type_info
1810 ("__array_type_info", 0,
1812 func_desc_type_node
= create_pseudo_type_info
1813 ("__function_type_info", 0,
1815 enum_desc_type_node
= create_pseudo_type_info
1816 ("__enum_type_info", 0,
1819 /* Class type_info. Add a flags field. */
1820 class_desc_type_node
= create_pseudo_type_info
1821 ("__class_type_info", 0,
1824 /* Single public non-virtual base class. Add pointer to base class. */
1825 si_class_desc_type_node
= create_pseudo_type_info
1826 ("__si_class_type_info", 0,
1827 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1830 /* Base class internal helper. Pointer to base type, offset to base,
1835 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
);
1836 fields
[1] = build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_types
[itk_long
]);
1837 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1838 finish_builtin_type (base_desc_type_node
, "__base_class_type_info_pseudo",
1839 fields
, 1, ptr_type_node
);
1840 TYPE_HAS_CONSTRUCTOR (base_desc_type_node
) = 1;
1843 /* General heirarchy is created as necessary in this vector. */
1844 vmi_class_desc_type_node
= make_tree_vec (10);
1846 /* Pointer to member data type_info. Add pointer to the class, pointer
1847 to the member's type info and qualifications flags. */
1848 ptmd_desc_type_node
= create_pseudo_type_info
1849 ("__pointer_to_member_type_info", 0,
1850 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1851 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1852 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1855 pop_nested_namespace (abi_node
);
1858 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1859 support. Generating them here guarantees consistency with the other
1860 structures. We use the following heuristic to determine when the runtime
1861 is being generated. If std::__fundamental_type_info is defined, and it's
1862 destructor is defined, then the runtime is being built. */
1865 emit_support_tinfos ()
1867 static tree
*const fundamentals
[] =
1873 &signed_wchar_type_node
, &unsigned_wchar_type_node
,
1875 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1876 &short_integer_type_node
, &short_unsigned_type_node
,
1877 &integer_type_node
, &unsigned_type_node
,
1878 &long_integer_type_node
, &long_unsigned_type_node
,
1879 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1880 &float_type_node
, &double_type_node
, &long_double_type_node
,
1882 /* GCC extension types */
1884 &complex_integer_type_node
,
1885 &complex_float_type_node
, &complex_double_type_node
,
1886 &complex_long_double_type_node
,
1892 tree bltn_type
, dtor
;
1894 push_nested_namespace (abi_node
);
1895 bltn_type
= xref_tag (class_type_node
,
1896 get_identifier ("__fundamental_type_info"), 1);
1897 pop_nested_namespace (abi_node
);
1898 if (!COMPLETE_TYPE_P (bltn_type
))
1900 dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type
), 1);
1901 if (DECL_EXTERNAL (dtor
))
1904 for (ix
= 0; fundamentals
[ix
]; ix
++)
1906 tree bltn
= *fundamentals
[ix
];
1907 tree bltn_ptr
= build_pointer_type (bltn
);
1908 tree bltn_const_ptr
= build_pointer_type
1909 (build_qualified_type (bltn
, TYPE_QUAL_CONST
));
1912 tinfo
= get_tinfo_decl (bltn
);
1913 TREE_USED (tinfo
) = 1;
1914 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1916 tinfo
= get_tinfo_decl (bltn_ptr
);
1917 TREE_USED (tinfo
) = 1;
1918 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1920 tinfo
= get_tinfo_decl (bltn_const_ptr
);
1921 TREE_USED (tinfo
) = 1;
1922 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1926 /* Return non-zero, iff T is a type_info variable which has not had a
1927 definition emitted for it. */
1930 tinfo_decl_p (t
, data
)
1932 void *data ATTRIBUTE_UNUSED
;
1934 return TREE_CODE (t
) == VAR_DECL
1935 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t
)) == (t
)
1936 && TREE_TYPE (t
) == tinfo_decl_type
1937 && TREE_TYPE (DECL_NAME (t
));
1940 /* Emit a suitable type_info definition for the type_info decl pointed to by
1941 DECL_PTR. We emit a completely new variable, of the correct type for the
1942 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1943 definition is set to that of the supplied decl, so that they can be tied
1944 up. Mark the supplied decl as having been dealt with. Emitting one
1945 definitions might cause other declarations to be emitted.
1947 We need to do things this way, because we're trying to do something like
1953 extern const A tinfo_var;
1955 const B tinfo_var = {...};
1957 which is not permitted. Also, we've not necessarily seen the definition of B.
1958 So we do something like the following,
1960 extern const A tinfo_var;
1963 const void *vtable_ptr;
1971 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1973 {&B::vtable, "..."},
1977 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1981 emit_tinfo_decl (decl_ptr
, data
)
1983 void *data ATTRIBUTE_UNUSED
;
1985 tree tinfo_decl
= *decl_ptr
;
1986 tree tinfo_type
, decl
;
1988 my_friendly_assert (TREE_TYPE (tinfo_decl
) == tinfo_decl_type
, 20000121);
1989 tinfo_type
= TREE_TYPE (DECL_NAME (tinfo_decl
));
1990 my_friendly_assert (tinfo_type
!= NULL_TREE
, 20000120);
1992 if (!DECL_NEEDED_P (tinfo_decl
))
1994 /* Say we've dealt with it. */
1995 TREE_TYPE (DECL_NAME (tinfo_decl
)) = NULL_TREE
;
1997 create_tinfo_types ();
1998 decl
= synthesize_tinfo_var (tinfo_type
, DECL_ASSEMBLER_NAME (tinfo_decl
));