1 /* RunTime Type Identification
2 Copyright (C) 1995, 96-97, 1998, 1999, 2000 Free Software Foundation, Inc.
3 Mostly written by Jason Merrill (jason@cygnus.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
33 #define INT_TYPE_SIZE BITS_PER_WORD
36 /* Accessors for the type_info objects. We need to remember several things
37 about each of the type_info types. The global tree nodes such as
38 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
39 the required information. */
40 /* The RECORD_TYPE of a type_info derived class. */
41 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
42 /* The VAR_DECL of the vtable for the type_info derived class. */
43 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
45 extern struct obstack permanent_obstack
;
47 static tree build_runtime_decl
PARAMS((const char *, tree
));
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_ref_initializer
PARAMS((tree
, tree
));
68 static tree ptmd_initializer
PARAMS((tree
, tree
));
69 static int class_hint_flags
PARAMS((tree
));
70 static tree class_initializer
PARAMS((tree
, tree
, tree
));
71 static tree synthesize_tinfo_var
PARAMS((tree
, tree
));
72 static tree create_real_tinfo_var
PARAMS((tree
, tree
, tree
));
73 static tree create_pseudo_type_info
PARAMS((const char *, int, ...));
74 static tree get_vmi_pseudo_type_info
PARAMS((int));
75 static void create_tinfo_types
PARAMS((void));
77 static int doing_runtime
= 0;
80 init_rtti_processing ()
83 push_namespace (get_identifier ("std"));
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
)),
99 tinfo_decl_id
= get_identifier ("__ti");
100 tinfo_decl_type
= build_qualified_type
101 (type_info_type_node
, TYPE_QUAL_CONST
);
103 tinfo_var_id
= get_identifier ("__ti");
106 /* Given a pointer to an object with at least one virtual table
107 pointer somewhere, return a pointer to a possible sub-object that
108 has a virtual table pointer in it that is the vtable parent for
112 build_headof_sub (exp
)
115 tree type
= TREE_TYPE (TREE_TYPE (exp
));
116 tree basetype
= CLASSTYPE_RTTI (type
);
117 tree binfo
= get_binfo (basetype
, type
, 0);
119 exp
= convert_pointer_to_real (binfo
, exp
);
123 /* Given the expression EXP of type `class *', return the head of the
124 object pointed to by EXP with type cv void*, if the class has any
125 virtual functions (TYPE_POLYMORPHIC_P), else just return the
132 tree type
= TREE_TYPE (exp
);
136 my_friendly_assert (TREE_CODE (type
) == POINTER_TYPE
, 20000112);
137 type
= TREE_TYPE (type
);
139 if (!TYPE_POLYMORPHIC_P (type
))
141 if (CLASSTYPE_COM_INTERFACE (type
))
143 cp_error ("RTTI not supported for COM interface type `%T'", type
);
144 return error_mark_node
;
147 /* If we don't have rtti stuff, get to a sub-object that does. */
148 if (!CLASSTYPE_VFIELDS (TREE_TYPE (TREE_TYPE (exp
))))
149 exp
= build_headof_sub (exp
);
151 /* We use this a couple of times below, protect it. */
152 exp
= save_expr (exp
);
154 aref
= build_vtbl_ref (build_indirect_ref (exp
, NULL_PTR
), integer_zero_node
);
156 if (flag_vtable_thunks
)
159 offset
= build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0);
161 type
= build_qualified_type (ptr_type_node
,
162 CP_TYPE_QUALS (TREE_TYPE (exp
)));
163 return build (PLUS_EXPR
, type
, exp
,
164 cp_convert (ptrdiff_type_node
, offset
));
167 /* Build a decl to a runtime entry point taking void and returning TYPE.
168 Although the entry point may never return, making its return type
169 consistent is necessary. */
172 build_runtime_decl (name
, type
)
176 tree d
= get_identifier (name
);
178 if (IDENTIFIER_GLOBAL_VALUE (d
))
179 d
= IDENTIFIER_GLOBAL_VALUE (d
);
182 type
= build_function_type (type
, void_list_node
);
183 d
= build_lang_decl (FUNCTION_DECL
, d
, type
);
184 DECL_EXTERNAL (d
) = 1;
186 DECL_ARTIFICIAL (d
) = 1;
187 pushdecl_top_level (d
);
188 make_function_rtl (d
);
195 /* Get a bad_cast node for the program to throw...
197 See libstdc++/exception.cc for __throw_bad_cast */
202 if (!throw_bad_cast_node
)
203 throw_bad_cast_node
= build_runtime_decl
204 ("__throw_bad_cast", ptr_type_node
);
206 return build_call (throw_bad_cast_node
,
207 TREE_TYPE (TREE_TYPE (throw_bad_cast_node
)),
214 if (!throw_bad_typeid_node
)
215 throw_bad_typeid_node
= build_runtime_decl
216 ("__throw_bad_typeid",
218 (build_qualified_type
219 (type_info_type_node
, TYPE_QUAL_CONST
)));
221 return build_call (throw_bad_typeid_node
,
222 TREE_TYPE (TREE_TYPE (throw_bad_typeid_node
)),
226 /* Return a pointer to type_info function associated with the expression EXP.
227 If EXP is a reference to a polymorphic class, return the dynamic type;
228 otherwise return the static type of the expression. */
231 get_tinfo_decl_dynamic (exp
)
236 if (exp
== error_mark_node
)
237 return error_mark_node
;
239 type
= TREE_TYPE (exp
);
241 /* peel back references, so they match. */
242 if (TREE_CODE (type
) == REFERENCE_TYPE
)
243 type
= TREE_TYPE (type
);
245 /* Peel off cv qualifiers. */
246 type
= TYPE_MAIN_VARIANT (type
);
248 if (type
!= void_type_node
)
249 type
= complete_type_or_else (type
, exp
);
252 return error_mark_node
;
254 /* If exp is a reference to polymorphic type, get the real type_info. */
255 if (TYPE_POLYMORPHIC_P (type
) && ! resolves_to_fixed_type_p (exp
, 0))
257 /* build reference to type_info from vtable. */
261 error ("taking dynamic typeid of object with -fno-rtti");
262 if (CLASSTYPE_COM_INTERFACE (type
))
264 cp_error ("RTTI not supported for COM interface type `%T'", type
);
265 return error_mark_node
;
268 /* If we don't have rtti stuff, get to a sub-object that does. */
269 if (! CLASSTYPE_VFIELDS (type
))
271 exp
= build_unary_op (ADDR_EXPR
, exp
, 0);
272 exp
= build_headof_sub (exp
);
273 exp
= build_indirect_ref (exp
, NULL_PTR
);
276 if (flag_vtable_thunks
)
277 t
= build_vfn_ref ((tree
*) 0, exp
, integer_one_node
);
279 t
= build_vfn_ref ((tree
*) 0, exp
, integer_zero_node
);
280 TREE_TYPE (t
) = build_pointer_type (tinfo_decl_type
);
284 /* otherwise return the type_info for the static type of the expr. */
285 exp
= get_tinfo_decl (TYPE_MAIN_VARIANT (type
));
286 return build_unary_op (ADDR_EXPR
, exp
, 0);
293 tree cond
= NULL_TREE
;
298 error ("cannot use typeid with -fno-rtti");
299 return error_mark_node
;
302 if (TYPE_SIZE (type_info_type_node
) == NULL_TREE
)
304 error ("must #include <typeinfo> before using typeid");
305 return error_mark_node
;
308 if (processing_template_decl
)
309 return build_min_nt (TYPEID_EXPR
, exp
);
311 if (TREE_CODE (exp
) == INDIRECT_REF
312 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp
, 0))) == POINTER_TYPE
313 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp
))
314 && ! resolves_to_fixed_type_p (exp
, &nonnull
)
317 exp
= stabilize_reference (exp
);
318 cond
= cp_convert (boolean_type_node
, TREE_OPERAND (exp
, 0));
321 exp
= get_tinfo_decl_dynamic (exp
);
323 if (exp
== error_mark_node
)
324 return error_mark_node
;
326 exp
= tinfo_from_decl (exp
);
330 tree bad
= throw_bad_typeid ();
332 exp
= build (COND_EXPR
, TREE_TYPE (exp
), cond
, exp
, bad
);
335 return convert_from_reference (exp
);
342 tree tname
= build_overload_with_type (tinfo_var_id
, type
);
346 my_friendly_assert (!new_abi_rtti_p (), 20000118);
347 if (IDENTIFIER_GLOBAL_VALUE (tname
))
348 return IDENTIFIER_GLOBAL_VALUE (tname
);
350 /* Figure out how much space we need to allocate for the type_info object.
351 If our struct layout or the type_info classes are changed, this will
352 need to be modified. */
353 if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
354 size
= 3 * POINTER_SIZE
+ INT_TYPE_SIZE
;
355 else if (TREE_CODE (type
) == POINTER_TYPE
356 && ! (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
357 || TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
358 size
= 3 * POINTER_SIZE
;
359 else if (IS_AGGR_TYPE (type
))
361 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
362 size
= 2 * POINTER_SIZE
;
363 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
365 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
366 size
= 3 * POINTER_SIZE
;
368 size
= 3 * POINTER_SIZE
+ TYPE_PRECISION (sizetype
);
371 size
= 2 * POINTER_SIZE
;
373 /* The type for a character array of the appropriate size. */
374 arrtype
= build_cplus_array_type
375 (unsigned_char_type_node
,
376 build_index_type (size_int (size
/ BITS_PER_UNIT
- 1)));
378 return declare_global_var (tname
, arrtype
);
381 /* Generate the NTBS name of a type. */
386 const char *name
= build_overload_name (type
, 1, 1);
387 tree name_string
= combine_strings (build_string (strlen (name
) + 1, name
));
391 /* Returns a decl for a function or variable which can be used to obtain a
392 type_info object for TYPE. The old-abi uses functions, the new-abi
393 uses the type_info object directly. You can take the address of the
394 returned decl, to save the decl. To use the decl call
395 tinfo_from_decl. You must arrange that the decl is mark_used, if
396 actually use it --- decls in vtables are only used if the vtable is
400 get_tinfo_decl (type
)
406 if (TREE_CODE (type
) == OFFSET_TYPE
)
407 type
= TREE_TYPE (type
);
408 if (TREE_CODE (type
) == METHOD_TYPE
)
409 type
= build_function_type (TREE_TYPE (type
),
410 TREE_CHAIN (TYPE_ARG_TYPES (type
)));
412 name
= build_overload_with_type (tinfo_decl_id
, type
);
414 d
= IDENTIFIER_GLOBAL_VALUE (name
);
417 else if (!new_abi_rtti_p ())
419 /* The tinfo decl is a function returning a reference to the type_info
421 d
= build_lang_decl (FUNCTION_DECL
, name
, tinfo_decl_type
);
422 DECL_EXTERNAL (d
) = 1;
424 DECL_ARTIFICIAL (d
) = 1;
425 DECL_NOT_REALLY_EXTERN (d
) = 1;
426 SET_DECL_TINFO_FN_P (d
);
427 TREE_TYPE (name
) = type
;
429 pushdecl_top_level (d
);
430 make_function_rtl (d
);
431 mark_inline_for_output (d
);
435 /* The tinfo decl is the type_info object itself. We make all
436 tinfo objects look as type_info, even though they will end up
437 being a subclass of that when emitted. This means the we'll
438 erroneously think we know the dynamic type -- be careful in the
440 d
= build_lang_decl (VAR_DECL
, name
, tinfo_decl_type
);
442 DECL_ARTIFICIAL (d
) = 1;
443 DECL_ALIGN (d
) = TYPE_ALIGN (ptr_type_node
);
444 TREE_READONLY (d
) = 1;
446 DECL_EXTERNAL (d
) = 1;
448 DECL_ASSEMBLER_NAME (d
) = DECL_NAME (d
);
449 cp_finish_decl (d
, NULL_TREE
, NULL_TREE
, 0);
451 pushdecl_top_level (d
);
452 /* Remember the type it is for. */
453 TREE_TYPE (name
) = type
;
458 /* Given an expr produced by get_tinfo_decl, return an expr which
459 produces a reference to the type_info object. */
462 tinfo_from_decl (expr
)
467 if (!new_abi_rtti_p ())
468 t
= build_call (expr
, TREE_TYPE (tinfo_decl_type
), NULL_TREE
);
469 else if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
470 t
= build_indirect_ref (expr
, NULL
);
483 t
= get_tinfo_decl (type
);
484 t
= tinfo_from_decl (t
);
485 return convert_from_reference (t
);
488 /* Return the type_info object for TYPE. */
494 if (type
== error_mark_node
)
495 return error_mark_node
;
497 if (TYPE_SIZE (type_info_type_node
) == NULL_TREE
)
499 error ("must #include <typeinfo> before using typeid");
500 return error_mark_node
;
503 if (processing_template_decl
)
504 return build_min_nt (TYPEID_EXPR
, type
);
506 /* If the type of the type-id is a reference type, the result of the
507 typeid expression refers to a type_info object representing the
509 if (TREE_CODE (type
) == REFERENCE_TYPE
)
510 type
= TREE_TYPE (type
);
512 /* The top-level cv-qualifiers of the lvalue expression or the type-id
513 that is the operand of typeid are always ignored. */
514 type
= TYPE_MAIN_VARIANT (type
);
516 if (type
!= void_type_node
)
517 type
= complete_type_or_else (type
, NULL_TREE
);
520 return error_mark_node
;
522 return get_typeid_1 (type
);
525 /* Check whether TEST is null before returning RESULT. If TEST is used in
526 RESULT, it must have previously had a save_expr applied to it. */
529 ifnonnull (test
, result
)
532 return build (COND_EXPR
, TREE_TYPE (result
),
533 build (EQ_EXPR
, boolean_type_node
, test
, integer_zero_node
),
534 cp_convert (TREE_TYPE (result
), integer_zero_node
),
538 /* Generate the constant expression describing where direct base BINFO
539 appears within the PARENT. How to interpret this expression depends on
540 details of the ABI, which the runtime must be aware of. */
543 get_base_offset (binfo
, parent
)
549 if (!TREE_VIA_VIRTUAL (binfo
))
550 offset
= BINFO_OFFSET (binfo
);
551 else if (!vbase_offsets_in_vtable_p ())
553 tree t
= BINFO_TYPE (binfo
);
557 FORMAT_VBASE_NAME (name
, t
);
558 field
= lookup_field (parent
, get_identifier (name
), 0, 0);
559 offset
= size_binop (FLOOR_DIV_EXPR
,
560 DECL_FIELD_BITPOS (field
),
561 size_int (BITS_PER_UNIT
));
562 offset
= convert (sizetype
, offset
);
566 /* Under the new ABI, we store the vtable offset at which
567 the virtual base offset can be found. */
568 tree vbase
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), parent
);
569 offset
= convert (sizetype
, BINFO_VPTR_FIELD (vbase
));
574 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
578 build_dynamic_cast_1 (type
, expr
)
581 enum tree_code tc
= TREE_CODE (type
);
585 tree old_expr
= expr
;
587 if (TREE_CODE (expr
) == OFFSET_REF
)
588 expr
= resolve_offset_ref (expr
);
590 exprtype
= TREE_TYPE (expr
);
591 assert (exprtype
!= NULL_TREE
);
592 ec
= TREE_CODE (exprtype
);
597 if (ec
== REFERENCE_TYPE
)
599 expr
= convert_from_reference (expr
);
600 exprtype
= TREE_TYPE (expr
);
601 ec
= TREE_CODE (exprtype
);
603 if (ec
!= POINTER_TYPE
)
605 if (TREE_CODE (TREE_TYPE (exprtype
)) != RECORD_TYPE
)
607 if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype
))) == NULL_TREE
)
609 if (!at_least_as_qualified_p (TREE_TYPE (type
),
610 TREE_TYPE (exprtype
)))
612 if (TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
614 /* else fall through */
616 if (TREE_CODE (TREE_TYPE (type
)) != RECORD_TYPE
)
618 if (TYPE_SIZE (complete_type (TREE_TYPE (type
))) == NULL_TREE
)
621 /* else fall through */
626 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
627 if (ec
== RECORD_TYPE
)
629 exprtype
= build_reference_type (exprtype
);
630 expr
= convert_to_reference (exprtype
, expr
, CONV_IMPLICIT
,
631 LOOKUP_NORMAL
, NULL_TREE
);
635 if (tc
== REFERENCE_TYPE
)
637 if (ec
!= REFERENCE_TYPE
)
639 if (TREE_CODE (TREE_TYPE (exprtype
)) != RECORD_TYPE
)
641 if (TYPE_SIZE (complete_type (TREE_TYPE (exprtype
))) == NULL_TREE
)
643 if (!at_least_as_qualified_p (TREE_TYPE (type
),
644 TREE_TYPE (exprtype
)))
648 /* If *type is an unambiguous accessible base class of *exprtype,
649 convert statically. */
654 distance
= get_base_distance (TREE_TYPE (type
), TREE_TYPE (exprtype
), 1,
659 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
660 TREE_TYPE (exprtype
), TREE_TYPE (type
));
661 return error_mark_node
;
665 cp_error ("dynamic_cast from `%T' to private base class `%T'",
666 TREE_TYPE (exprtype
), TREE_TYPE (type
));
667 return error_mark_node
;
671 return build_vbase_path (PLUS_EXPR
, type
, expr
, path
, 0);
674 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
675 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype
)))
678 /* if TYPE is `void *', return pointer to complete object. */
679 if (tc
== POINTER_TYPE
680 && TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == void_type_node
)
682 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
683 if (TREE_CODE (expr
) == ADDR_EXPR
684 && TREE_CODE (TREE_OPERAND (expr
, 0)) == VAR_DECL
685 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == RECORD_TYPE
)
686 return build1 (NOP_EXPR
, type
, expr
);
688 /* Since expr is used twice below, save it. */
689 expr
= save_expr (expr
);
691 expr1
= build_headof (expr
);
692 if (TREE_TYPE (expr1
) != type
)
693 expr1
= build1 (NOP_EXPR
, type
, expr1
);
694 return ifnonnull (expr
, expr1
);
699 tree result
, td2
, td3
, elems
;
700 tree static_type
, target_type
, boff
;
702 /* If we got here, we can't convert statically. Therefore,
703 dynamic_cast<D&>(b) (b an object) cannot succeed. */
704 if (ec
== REFERENCE_TYPE
)
706 if (TREE_CODE (old_expr
) == VAR_DECL
707 && TREE_CODE (TREE_TYPE (old_expr
)) == RECORD_TYPE
)
709 tree expr
= throw_bad_cast ();
710 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
712 /* Bash it to the expected type. */
713 TREE_TYPE (expr
) = type
;
717 /* Ditto for dynamic_cast<D*>(&b). */
718 else if (TREE_CODE (expr
) == ADDR_EXPR
)
720 tree op
= TREE_OPERAND (expr
, 0);
721 if (TREE_CODE (op
) == VAR_DECL
722 && TREE_CODE (TREE_TYPE (op
)) == RECORD_TYPE
)
724 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
726 retval
= build_int_2 (0, 0);
727 TREE_TYPE (retval
) = type
;
732 target_type
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
733 static_type
= TYPE_MAIN_VARIANT (TREE_TYPE (exprtype
));
734 td2
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (target_type
), 0);
735 td3
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (static_type
), 0);
737 /* Determine how T and V are related. */
738 boff
= get_dynamic_cast_base_type (static_type
, target_type
);
740 /* Since expr is used twice below, save it. */
741 expr
= save_expr (expr
);
744 if (tc
== REFERENCE_TYPE
)
745 expr1
= build_unary_op (ADDR_EXPR
, expr1
, 0);
747 if (!new_abi_rtti_p ())
749 tree expr2
= build_headof (expr1
);
752 if (ec
== POINTER_TYPE
)
753 td1
= build_indirect_ref (td1
, NULL_PTR
);
754 td1
= get_tinfo_decl_dynamic (td1
);
757 (NULL_TREE
, td1
, tree_cons
758 (NULL_TREE
, td2
, tree_cons
759 (NULL_TREE
, boff
, tree_cons
760 (NULL_TREE
, expr2
, tree_cons
761 (NULL_TREE
, td3
, tree_cons
762 (NULL_TREE
, expr1
, NULL_TREE
))))));
766 (NULL_TREE
, expr1
, tree_cons
767 (NULL_TREE
, td3
, tree_cons
768 (NULL_TREE
, td2
, tree_cons
769 (NULL_TREE
, boff
, NULL_TREE
))));
771 dcast_fn
= dynamic_cast_node
;
776 tree ns
= global_namespace
;
779 push_nested_namespace (ns
);
780 if (!new_abi_rtti_p ())
782 tinfo_ptr
= build_pointer_type (tinfo_decl_type
);
783 name
= "__dynamic_cast_2";
785 (NULL_TREE
, tinfo_ptr
, tree_cons
786 (NULL_TREE
, tinfo_ptr
, tree_cons
787 (NULL_TREE
, integer_type_node
, tree_cons
788 (NULL_TREE
, ptr_type_node
, tree_cons
789 (NULL_TREE
, tinfo_ptr
, tree_cons
790 (NULL_TREE
, ptr_type_node
, void_list_node
))))));
796 push_namespace (get_identifier ("std"));
797 ns
= current_namespace
;
799 tinfo_ptr
= xref_tag (class_type_node
,
800 get_identifier ("__class_type_info"),
803 tinfo_ptr
= build_pointer_type
804 (build_qualified_type
805 (tinfo_ptr
, TYPE_QUAL_CONST
));
806 name
= "__dynamic_cast";
808 (NULL_TREE
, const_ptr_type_node
, tree_cons
809 (NULL_TREE
, tinfo_ptr
, tree_cons
810 (NULL_TREE
, tinfo_ptr
, tree_cons
811 (NULL_TREE
, ptrdiff_type_node
, void_list_node
))));
813 tmp
= build_function_type (ptr_type_node
, tmp
);
814 dcast_fn
= build_lang_decl (FUNCTION_DECL
,
815 get_identifier (name
),
817 DECL_EXTERNAL (dcast_fn
) = 1;
818 TREE_PUBLIC (dcast_fn
) = 1;
819 DECL_ARTIFICIAL (dcast_fn
) = 1;
821 if (new_abi_rtti_p ())
822 /* We want it's name mangling. */
823 set_mangled_name_for_decl (dcast_fn
);
824 make_function_rtl (dcast_fn
);
825 pop_nested_namespace (ns
);
826 dynamic_cast_node
= dcast_fn
;
828 mark_used (dcast_fn
);
830 (dcast_fn
, TREE_TYPE (TREE_TYPE (dcast_fn
)), elems
);
832 if (tc
== REFERENCE_TYPE
)
834 tree bad
= throw_bad_cast ();
836 result
= save_expr (result
);
837 return build (COND_EXPR
, type
, result
, result
, bad
);
840 /* Now back to the type we want from a void*. */
841 result
= cp_convert (type
, result
);
842 return ifnonnull (expr
, result
);
846 cp_error ("dynamic_cast from non-polymorphic type `%#T'", exprtype
);
847 return error_mark_node
;
850 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T'",
851 expr
, exprtype
, type
);
852 return error_mark_node
;
856 build_dynamic_cast (type
, expr
)
859 if (type
== error_mark_node
|| expr
== error_mark_node
)
860 return error_mark_node
;
862 if (processing_template_decl
)
863 return build_min (DYNAMIC_CAST_EXPR
, type
, expr
);
865 return convert_from_reference (build_dynamic_cast_1 (type
, expr
));
868 /* Build and initialize various sorts of descriptors. Every descriptor
869 node has a name associated with it (the name created by mangling).
870 For this reason, we use the identifier as our access to the __*_desc
871 nodes, instead of sticking them directly in the types. Otherwise we
872 would burden all built-in types (and pointer types) with slots that
873 we don't necessarily want to use.
875 For each descriptor we build, we build a variable that contains
876 the descriptor's information. When we need this info at runtime,
877 all we need is access to these variables.
879 Note: these constructors always return the address of the descriptor
880 info, since that is simplest for their mutual interaction. */
882 /* Build an initializer for a __si_type_info node. */
885 expand_si_desc (tdecl
, type
)
890 tree name_string
= tinfo_name (type
);
892 type
= BINFO_TYPE (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0));
893 finish_expr_stmt (get_typeid_1 (type
));
894 t
= decay_conversion (get_tinfo_var (type
));
896 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
897 (NULL_TREE
, decay_conversion (name_string
), tree_cons
898 (NULL_TREE
, t
, NULL_TREE
)));
900 fn
= get_identifier ("__rtti_si");
901 if (IDENTIFIER_GLOBAL_VALUE (fn
))
902 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
907 (NULL_TREE
, ptr_type_node
, tree_cons
908 (NULL_TREE
, const_string_type_node
, tree_cons
909 (NULL_TREE
, build_pointer_type (type_info_type_node
),
911 tmp
= build_function_type (void_type_node
, tmp
);
913 fn
= build_lang_decl (FUNCTION_DECL
, fn
, tmp
);
914 DECL_EXTERNAL (fn
) = 1;
915 TREE_PUBLIC (fn
) = 1;
916 DECL_ARTIFICIAL (fn
) = 1;
917 pushdecl_top_level (fn
);
918 make_function_rtl (fn
);
922 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (fn
)), elems
);
923 finish_expr_stmt (fn
);
926 /* Build an initializer for a __class_type_info node. */
929 expand_class_desc (tdecl
, type
)
936 int i
= CLASSTYPE_N_BASECLASSES (type
);
938 tree binfos
= TYPE_BINFO_BASETYPES (type
);
940 /* See code below that used these. */
941 tree vb
= CLASSTYPE_VBASECLASSES (type
);
944 tree base
, elems
, access
, offset
, isvir
;
945 tree elt
, elts
= NULL_TREE
;
947 if (base_desc_type_node
== NULL_TREE
)
951 /* A reasonably close approximation of __class_type_info::base_info */
953 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
955 /* Actually const __user_type_info * */
956 fields
[0] = build_lang_decl
957 (FIELD_DECL
, NULL_TREE
,
958 build_pointer_type (build_qualified_type
959 (type_info_type_node
,
961 fields
[1] = build_lang_decl
962 (FIELD_DECL
, NULL_TREE
,
963 flag_new_abi
? intSI_type_node
: unsigned_intSI_type_node
);
964 DECL_BIT_FIELD (fields
[1]) = 1;
965 DECL_FIELD_SIZE (fields
[1]) = 29;
967 fields
[2] = build_lang_decl
968 (FIELD_DECL
, NULL_TREE
, boolean_type_node
);
969 DECL_BIT_FIELD (fields
[2]) = 1;
970 DECL_FIELD_SIZE (fields
[2]) = 1;
972 /* Actually enum access */
973 fields
[3] = build_lang_decl
974 (FIELD_DECL
, NULL_TREE
, integer_type_node
);
975 DECL_BIT_FIELD (fields
[3]) = 1;
976 DECL_FIELD_SIZE (fields
[3]) = 2;
978 finish_builtin_type (base_desc_type_node
, "__base_info", fields
,
984 tree binfo
= TREE_VEC_ELT (binfos
, i
);
986 finish_expr_stmt (get_typeid_1 (BINFO_TYPE (binfo
)));
987 base
= decay_conversion (get_tinfo_var (BINFO_TYPE (binfo
)));
988 offset
= get_base_offset (binfo
, type
);
990 if (TREE_VIA_PUBLIC (binfo
))
991 access
= access_public_node
;
992 else if (TREE_VIA_PROTECTED (binfo
))
993 access
= access_protected_node
;
995 access
= access_private_node
;
996 if (TREE_VIA_VIRTUAL (binfo
))
997 isvir
= boolean_true_node
;
999 isvir
= boolean_false_node
;
1002 (CONSTRUCTOR
, base_desc_type_node
, NULL_TREE
, tree_cons
1003 (NULL_TREE
, base
, tree_cons
1004 (NULL_TREE
, offset
, tree_cons
1005 (NULL_TREE
, isvir
, tree_cons
1006 (NULL_TREE
, access
, NULL_TREE
)))));
1007 TREE_HAS_CONSTRUCTOR (elt
) = TREE_CONSTANT (elt
) = TREE_STATIC (elt
) = 1;
1008 elts
= tree_cons (NULL_TREE
, elt
, elts
);
1016 access
= access_public_node
;
1019 b
= TREE_VEC_ELT (binfos
, i
);
1020 if (BINFO_TYPE (vb
) == BINFO_TYPE (b
) && TREE_VIA_VIRTUAL (b
))
1022 if (TREE_VIA_PUBLIC (b
))
1023 access
= access_public_node
;
1024 else if (TREE_VIA_PROTECTED (b
))
1025 access
= access_protected_node
;
1027 access
= access_private_node
;
1031 base
= build_t_desc (BINFO_TYPE (vb
), 1);
1032 offset
= BINFO_OFFSET (vb
);
1033 isvir
= build_int_2 (1, 0);
1035 base_list
= tree_cons (NULL_TREE
, base
, base_list
);
1036 isvir_list
= tree_cons (NULL_TREE
, isvir
, isvir_list
);
1037 acc_list
= tree_cons (NULL_TREE
, access
, acc_list
);
1038 off_list
= tree_cons (NULL_TREE
, offset
, off_list
);
1041 vb
= TREE_CHAIN (vb
);
1045 name_string
= tinfo_name (type
);
1048 tree arrtype
= build_array_type (base_desc_type_node
, NULL_TREE
);
1049 elts
= build (CONSTRUCTOR
, arrtype
, NULL_TREE
, elts
);
1050 TREE_HAS_CONSTRUCTOR (elts
) = TREE_CONSTANT (elts
)
1051 = TREE_STATIC (elts
) = 1;
1052 complete_array_type (arrtype
, elts
, 1);
1056 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1057 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1058 (NULL_TREE
, decay_conversion (elts
), tree_cons
1059 (NULL_TREE
, cp_convert (sizetype
, build_int_2 (base_cnt
, 0)),
1062 fn
= get_identifier ("__rtti_class");
1063 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1064 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1068 (NULL_TREE
, ptr_type_node
, tree_cons
1069 (NULL_TREE
, const_string_type_node
, tree_cons
1070 (NULL_TREE
, build_pointer_type (base_desc_type_node
), tree_cons
1071 (NULL_TREE
, sizetype
, void_list_node
))));
1072 tmp
= build_function_type (void_type_node
, tmp
);
1074 fn
= build_lang_decl (FUNCTION_DECL
, fn
, tmp
);
1075 DECL_EXTERNAL (fn
) = 1;
1076 TREE_PUBLIC (fn
) = 1;
1077 DECL_ARTIFICIAL (fn
) = 1;
1078 pushdecl_top_level (fn
);
1079 make_function_rtl (fn
);
1083 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (fn
)), elems
);
1084 finish_expr_stmt (fn
);
1087 /* Build an initializer for a __pointer_type_info node. */
1090 expand_ptr_desc (tdecl
, type
)
1095 tree name_string
= tinfo_name (type
);
1097 type
= TREE_TYPE (type
);
1098 finish_expr_stmt (get_typeid_1 (type
));
1099 t
= decay_conversion (get_tinfo_var (type
));
1101 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1102 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1103 (NULL_TREE
, t
, NULL_TREE
)));
1105 fn
= get_identifier ("__rtti_ptr");
1106 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1107 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1112 (NULL_TREE
, ptr_type_node
, tree_cons
1113 (NULL_TREE
, const_string_type_node
, tree_cons
1114 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1116 tmp
= build_function_type (void_type_node
, tmp
);
1118 fn
= build_lang_decl (FUNCTION_DECL
, fn
, tmp
);
1119 DECL_EXTERNAL (fn
) = 1;
1120 TREE_PUBLIC (fn
) = 1;
1121 DECL_ARTIFICIAL (fn
) = 1;
1122 pushdecl_top_level (fn
);
1123 make_function_rtl (fn
);
1127 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (fn
)), elems
);
1128 finish_expr_stmt (fn
);
1131 /* Build an initializer for a __attr_type_info node. */
1134 expand_attr_desc (tdecl
, type
)
1139 tree name_string
= tinfo_name (type
);
1140 tree attrval
= build_int_2 (TYPE_QUALS (type
), 0);
1142 finish_expr_stmt (get_typeid_1 (TYPE_MAIN_VARIANT (type
)));
1143 t
= decay_conversion (get_tinfo_var (TYPE_MAIN_VARIANT (type
)));
1145 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1146 (NULL_TREE
, decay_conversion (name_string
), tree_cons
1147 (NULL_TREE
, attrval
, tree_cons (NULL_TREE
, t
, NULL_TREE
))));
1149 fn
= get_identifier ("__rtti_attr");
1150 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1151 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1156 (NULL_TREE
, ptr_type_node
, tree_cons
1157 (NULL_TREE
, const_string_type_node
, tree_cons
1158 (NULL_TREE
, integer_type_node
, tree_cons
1159 (NULL_TREE
, build_pointer_type (type_info_type_node
),
1161 tmp
= build_function_type (void_type_node
, tmp
);
1163 fn
= build_lang_decl (FUNCTION_DECL
, fn
, tmp
);
1164 DECL_EXTERNAL (fn
) = 1;
1165 TREE_PUBLIC (fn
) = 1;
1166 DECL_ARTIFICIAL (fn
) = 1;
1167 pushdecl_top_level (fn
);
1168 make_function_rtl (fn
);
1172 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (fn
)), elems
);
1173 finish_expr_stmt (fn
);
1176 /* Build an initializer for a type_info node that just has a name. */
1179 expand_generic_desc (tdecl
, type
, fnname
)
1184 tree name_string
= tinfo_name (type
);
1185 tree elems
= tree_cons
1186 (NULL_TREE
, decay_conversion (tdecl
), tree_cons
1187 (NULL_TREE
, decay_conversion (name_string
), NULL_TREE
));
1189 tree fn
= get_identifier (fnname
);
1190 if (IDENTIFIER_GLOBAL_VALUE (fn
))
1191 fn
= IDENTIFIER_GLOBAL_VALUE (fn
);
1196 (NULL_TREE
, ptr_type_node
, tree_cons
1197 (NULL_TREE
, const_string_type_node
, void_list_node
));
1198 tmp
= build_function_type (void_type_node
, tmp
);
1200 fn
= build_lang_decl (FUNCTION_DECL
, fn
, tmp
);
1201 DECL_EXTERNAL (fn
) = 1;
1202 TREE_PUBLIC (fn
) = 1;
1203 DECL_ARTIFICIAL (fn
) = 1;
1204 pushdecl_top_level (fn
);
1205 make_function_rtl (fn
);
1209 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (fn
)), elems
);
1210 finish_expr_stmt (fn
);
1213 /* Generate the code for a type_info initialization function.
1214 Note that we take advantage of the passage
1216 5.2.7 Type identification [expr.typeid]
1218 Whether or not the destructor is called for the type_info object at the
1219 end of the program is unspecified.
1221 and don't bother to arrange for these objects to be destroyed. It
1222 doesn't matter, anyway, since the destructors don't do anything.
1224 This must only be called from toplevel (i.e. from finish_file)! */
1227 synthesize_tinfo_fn (fndecl
)
1230 tree type
= TREE_TYPE (DECL_NAME (fndecl
));
1231 tree tmp
, addr
, tdecl
;
1236 my_friendly_assert (!new_abi_rtti_p (), 20000118);
1239 import_export_decl (fndecl
);
1240 if (DECL_REALLY_EXTERN (fndecl
))
1244 /* Declare the static typeinfo variable. */
1245 tdecl
= get_tinfo_var (type
);
1246 DECL_EXTERNAL (tdecl
) = 0;
1247 TREE_STATIC (tdecl
) = 1;
1248 DECL_COMMON (tdecl
) = 1;
1249 TREE_USED (tdecl
) = 1;
1250 DECL_ALIGN (tdecl
) = TYPE_ALIGN (ptr_type_node
);
1251 cp_finish_decl (tdecl
, NULL_TREE
, NULL_TREE
, 0);
1253 /* Begin processing the function. */
1254 start_function (NULL_TREE
, fndecl
, NULL_TREE
,
1255 SF_DEFAULT
| SF_PRE_PARSED
);
1256 DECL_DEFER_OUTPUT (fndecl
) = 1;
1257 store_parm_decls ();
1260 /* Begin the body of the function. */
1261 compound_stmt
= begin_compound_stmt (/*has_no_scope=*/0);
1263 /* For convenience, we save away the address of the static
1265 addr
= decay_conversion (tdecl
);
1267 /* If the first word of the array (the vtable) is non-zero, we've already
1268 initialized the object, so don't do it again. */
1269 if_stmt
= begin_if_stmt ();
1270 tmp
= cp_convert (build_pointer_type (ptr_type_node
), addr
);
1271 tmp
= build_indirect_ref (tmp
, 0);
1272 tmp
= build_binary_op (EQ_EXPR
, tmp
, integer_zero_node
);
1273 finish_if_stmt_cond (tmp
, if_stmt
);
1274 then_clause
= begin_compound_stmt (/*has_no_scope=*/0);
1276 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1277 expand_generic_desc (tdecl
, type
, "__rtti_func");
1278 else if (TREE_CODE (type
) == ARRAY_TYPE
)
1279 expand_generic_desc (tdecl
, type
, "__rtti_array");
1280 else if (TYPE_QUALS (type
) != TYPE_UNQUALIFIED
)
1281 expand_attr_desc (tdecl
, type
);
1282 else if (TREE_CODE (type
) == POINTER_TYPE
)
1284 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
)
1285 expand_generic_desc (tdecl
, type
, "__rtti_ptmd");
1286 else if (TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
)
1287 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1289 expand_ptr_desc (tdecl
, type
);
1291 else if (TYPE_PTRMEMFUNC_P (type
))
1292 expand_generic_desc (tdecl
, type
, "__rtti_ptmf");
1293 else if (IS_AGGR_TYPE (type
))
1295 if (CLASSTYPE_N_BASECLASSES (type
) == 0)
1296 expand_generic_desc (tdecl
, type
, "__rtti_user");
1297 else if (! TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type
)
1299 (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type
), 0))))
1300 expand_si_desc (tdecl
, type
);
1302 expand_class_desc (tdecl
, type
);
1304 else if (TREE_CODE (type
) == ENUMERAL_TYPE
)
1305 expand_generic_desc (tdecl
, type
, "__rtti_user");
1307 my_friendly_abort (252);
1309 finish_compound_stmt (/*has_no_scope=*/0, then_clause
);
1310 finish_then_clause (if_stmt
);
1313 /* OK, now return the type_info object. */
1314 tmp
= cp_convert (build_pointer_type (type_info_type_node
), addr
);
1315 tmp
= build_indirect_ref (tmp
, 0);
1316 finish_return_stmt (tmp
);
1317 /* Finish the function body. */
1318 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt
);
1319 expand_body (finish_function (lineno
, 0));
1322 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
1325 qualifier_flags (type
)
1329 /* we want the qualifiers on this type, not any array core, it might have */
1330 int quals
= TYPE_QUALS (type
);
1332 if (quals
& TYPE_QUAL_CONST
)
1334 if (quals
& TYPE_QUAL_VOLATILE
)
1339 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
1340 is the vtable pointer and NTBS name. */
1343 tinfo_base_init (desc
, target
)
1347 tree name_string
= tinfo_name (target
);
1348 tree init
= NULL_TREE
;
1350 if (TINFO_VTABLE_DECL (desc
))
1352 tree vtbl_ptr
= build_unary_op (ADDR_EXPR
, TINFO_VTABLE_DECL (desc
), 0);
1354 init
= tree_cons (NULL_TREE
, vtbl_ptr
, init
);
1357 init
= tree_cons (NULL_TREE
, decay_conversion (name_string
), init
);
1359 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse(init
));
1360 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1361 init
= tree_cons (NULL_TREE
, init
, NULL_TREE
);
1366 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
1367 information about the particular type_info derivation, which adds no
1368 additional fields to the type_info base. */
1371 generic_initializer (desc
, target
)
1375 tree init
= tinfo_base_init (desc
, target
);
1377 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1378 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1382 /* Return the CONSTRUCTOR expr for a type_info of pointer or reference TYPE.
1383 DESC provides information about the particular type_info derivation,
1384 which adds target type and qualifier flags members to the type_info base. */
1387 ptr_ref_initializer (desc
, target
)
1391 tree init
= tinfo_base_init (desc
, target
);
1392 tree to
= TREE_TYPE (target
);
1393 int flags
= qualifier_flags (to
);
1395 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1396 init
= tree_cons (NULL_TREE
,
1397 build_unary_op (ADDR_EXPR
,
1398 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1401 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1402 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1406 /* Return the CONSTRUCTOR expr for a type_info of pointer or reference TYPE.
1407 DESC provides information about the particular type_info derivation,
1408 which adds target type and qualifier flags members to the type_info base. */
1411 ptmd_initializer (desc
, target
)
1415 tree init
= tinfo_base_init (desc
, target
);
1416 tree to
= TYPE_PTRMEM_POINTED_TO_TYPE (target
);
1417 tree klass
= TYPE_PTRMEM_CLASS_TYPE (target
);
1418 int flags
= qualifier_flags (to
);
1420 init
= tree_cons (NULL_TREE
,
1421 build_unary_op (ADDR_EXPR
, get_tinfo_decl (klass
), 0),
1423 init
= tree_cons (NULL_TREE
,
1424 build_unary_op (ADDR_EXPR
,
1425 get_tinfo_decl (TYPE_MAIN_VARIANT (to
)), 0),
1427 init
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), init
);
1429 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, nreverse (init
));
1430 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1434 /* Determine the hint flags describing the features of a class's heirarchy.
1435 FIXME: better set the hint_flags here! For now set them
1436 to safe 'don't know' values. The specification is under
1437 review. Don't forget to check the runtime dynamic_cast and
1438 catch machinery if these change. */
1441 class_hint_flags (type
)
1445 hint_flags
|= 0x1; /* contains multiply inherited sub object */
1446 hint_flags
|= 0x4; /* has virtual bases */
1447 hint_flags
|= 0x8; /* has private base */
1448 if (TYPE_POLYMORPHIC_P (type
))
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
);
1465 int flags
= class_hint_flags (target
);
1467 trail
= tree_cons (NULL_TREE
, build_int_2 (flags
, 0), trail
);
1468 TREE_CHAIN (init
) = trail
;
1469 init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, init
);
1470 TREE_HAS_CONSTRUCTOR (init
) = TREE_CONSTANT (init
) = TREE_STATIC (init
) = 1;
1474 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
1475 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
1476 the runtime, and therefore has additional fields. If we need not emit a
1477 definition (because the runtime must contain it), return NULL_TREE,
1478 otherwise return the VAR_DECL. */
1481 synthesize_tinfo_var (target_type
, real_name
)
1485 tree var_init
= NULL_TREE
;
1486 tree var_type
= NULL_TREE
;
1488 my_friendly_assert (new_abi_rtti_p (), 20000118);
1490 switch (TREE_CODE (target_type
))
1493 if (TYPE_PTRMEM_P (target_type
))
1495 var_type
= ptmd_desc_type_node
;
1496 var_init
= ptmd_initializer (var_type
, target_type
);
1500 int code
= TREE_CODE (TREE_TYPE (target_type
));
1502 if ((CP_TYPE_QUALS (TREE_TYPE (target_type
)) | TYPE_QUAL_CONST
)
1504 && (code
== INTEGER_TYPE
|| code
== BOOLEAN_TYPE
1505 || code
== CHAR_TYPE
|| code
== REAL_TYPE
1506 || code
== VOID_TYPE
)
1508 /* These are in the runtime. */
1510 var_type
= ptr_desc_type_node
;
1511 var_init
= ptr_ref_initializer (var_type
, target_type
);
1514 case REFERENCE_TYPE
:
1515 var_type
= ref_desc_type_node
;
1516 var_init
= ptr_ref_initializer (var_type
, target_type
);
1519 var_type
= enum_desc_type_node
;
1520 var_init
= generic_initializer (var_type
, target_type
);
1523 var_type
= func_desc_type_node
;
1524 var_init
= generic_initializer (var_type
, target_type
);
1527 var_type
= ary_desc_type_node
;
1528 var_init
= generic_initializer (var_type
, target_type
);
1532 if (!TYPE_SIZE (target_type
))
1534 /* FIXME: incomplete type. Awaiting specification. */
1537 else if (!CLASSTYPE_N_BASECLASSES (target_type
))
1539 var_type
= class_desc_type_node
;
1540 var_init
= class_initializer (var_type
, target_type
, NULL_TREE
);
1544 /* if this has a single public non-virtual base, it's easier */
1545 tree binfo
= TYPE_BINFO (target_type
);
1546 int nbases
= BINFO_N_BASETYPES (binfo
);
1547 tree base_binfos
= BINFO_BASETYPES (binfo
);
1548 tree base_inits
= NULL_TREE
;
1549 int is_simple
= nbases
== 1;
1552 /* Generate the base information initializer. */
1553 for (ix
= nbases
; ix
--;)
1555 tree base_binfo
= TREE_VEC_ELT (base_binfos
, ix
);
1556 tree base_init
= NULL_TREE
;
1561 if (TREE_VIA_VIRTUAL (base_binfo
))
1563 if (TREE_PUBLIC (base_binfo
))
1565 tinfo
= get_tinfo_decl (BINFO_TYPE (base_binfo
));
1566 tinfo
= build_unary_op (ADDR_EXPR
, tinfo
, 0);
1567 offset
= get_base_offset (base_binfo
, target_type
);
1569 /* is it a single public inheritance? */
1570 if (is_simple
&& flags
== 2 && integer_zerop (offset
))
1572 base_inits
= tree_cons (NULL_TREE
, tinfo
, NULL_TREE
);
1577 base_init
= tree_cons
1578 (NULL_TREE
, build_int_2 (flags
, 0), base_init
);
1579 base_init
= tree_cons (NULL_TREE
, offset
, base_init
);
1580 base_init
= tree_cons (NULL_TREE
, tinfo
, base_init
);
1581 base_init
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_init
);
1582 base_inits
= tree_cons (NULL_TREE
, base_init
, base_inits
);
1586 var_type
= si_class_desc_type_node
;
1589 /* Prepend the number of bases. */
1590 base_inits
= build (CONSTRUCTOR
, NULL_TREE
, NULL_TREE
, base_inits
);
1591 base_inits
= tree_cons (NULL_TREE
, base_inits
, NULL_TREE
);
1592 base_inits
= tree_cons (NULL_TREE
,
1593 build_int_2 (nbases
, 0), base_inits
);
1595 var_type
= get_vmi_pseudo_type_info (nbases
);
1597 var_init
= class_initializer (var_type
, target_type
, base_inits
);
1606 /* These are guaranteed to be in the runtime. */
1608 var_type
= bltn_desc_type_node
;
1609 var_init
= generic_initializer (var_type
, target_type
);
1612 my_friendly_abort (20000117);
1615 return create_real_tinfo_var (real_name
, TINFO_PSEUDO_TYPE (var_type
), var_init
);
1618 /* Create the real typeinfo variable. */
1621 create_real_tinfo_var (name
, type
, init
)
1628 decl
= build_lang_decl (VAR_DECL
, name
,
1629 build_qualified_type (type
, TYPE_QUAL_CONST
));
1630 DECL_ARTIFICIAL (decl
) = 1;
1631 TREE_READONLY (decl
) = 1;
1632 TREE_STATIC (decl
) = 1;
1633 TREE_PUBLIC (decl
) = 1;
1634 DECL_EXTERNAL (decl
) = 0;
1636 comdat_linkage (decl
);
1637 DECL_ASSEMBLER_NAME (decl
) = name
;
1638 DECL_INITIAL (decl
) = init
;
1639 cp_finish_decl (decl
, init
, NULL_TREE
, 0);
1644 /* Generate the RECORD_TYPE containing the data layout of a type_info
1645 derivative as used by the runtime. This layout must be consistent with
1646 that defined in the runtime support. Also generate the VAR_DECL for the
1647 type's vtable. We explicitly manage the vtable member, and name it for
1648 real type as used in the runtime. The RECORD type has a different name,
1649 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1650 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1652 REAL_NAME is the runtime's name of the type. Trailing arguments are
1653 additional FIELD_DECL's for the structure. The final argument must be
1657 create_pseudo_type_info
VPARAMS((const char *real_name
, int ident
, ...))
1659 #ifndef ANSI_PROTOTYPES
1660 char const *real_name
;
1664 tree real_type
, pseudo_type
;
1672 VA_START (ap
, ident
);
1673 #ifndef ANSI_PROTOTYPES
1674 real_name
= va_arg (ap
, char const *);
1675 ident
= va_arg (app
, int);
1678 /* Generate the pseudo type name. */
1679 pseudo_name
= (char *)alloca (strlen (real_name
) + 30);
1680 strcpy (pseudo_name
, real_name
);
1681 strcat (pseudo_name
, "_pseudo");
1683 sprintf (pseudo_name
+ strlen (pseudo_name
), "%d", ident
);
1685 /* Get the vtable decl. */
1686 real_type
= xref_tag (class_type_node
, get_identifier (real_name
), 1);
1687 vtable_decl
= get_vtable_decl (real_type
, /*complete=*/1);
1689 /* First field is the pseudo type_info base class. */
1690 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, ti_desc_type_node
);
1692 /* Now add the derived fields. */
1693 for (ix
= 0; (field_decl
= va_arg (ap
, tree
));)
1694 fields
[++ix
] = field_decl
;
1696 /* Create the pseudo type. */
1697 pseudo_type
= make_aggr_type (RECORD_TYPE
);
1698 finish_builtin_type (pseudo_type
, pseudo_name
, fields
, ix
, ptr_type_node
);
1699 TYPE_HAS_CONSTRUCTOR (pseudo_type
) = 1;
1702 result
= tree_cons (NULL_TREE
, NULL_TREE
, NULL_TREE
);
1703 TINFO_VTABLE_DECL (result
) = vtable_decl
;
1704 TINFO_PSEUDO_TYPE (result
) = pseudo_type
;
1709 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1712 get_vmi_pseudo_type_info (num_bases
)
1716 tree array_domain
, base_array
;
1718 if (TREE_VEC_LENGTH (vmi_class_desc_type_node
) <= num_bases
)
1721 tree extend
= make_tree_vec (num_bases
+ 5);
1723 for (ix
= TREE_VEC_LENGTH (vmi_class_desc_type_node
); ix
--;)
1724 TREE_VEC_ELT (extend
, ix
) = TREE_VEC_ELT (vmi_class_desc_type_node
, ix
);
1725 vmi_class_desc_type_node
= extend
;
1727 desc
= TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
);
1732 /* Add number of bases and trailing array of base_class_type_info. */
1733 array_domain
= build_index_type (build_int_2 (num_bases
, 0));
1734 base_array
= build_array_type (base_desc_type_node
, array_domain
);
1736 desc
= create_pseudo_type_info
1737 ("__vmi_class_type_info", num_bases
,
1738 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1739 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1740 build_lang_decl (FIELD_DECL
, NULL_TREE
, base_array
),
1742 TREE_VEC_ELT (vmi_class_desc_type_node
, num_bases
) = desc
;
1746 /* Make sure the required builtin types exist for generating the type_info
1747 varable definitions. */
1750 create_tinfo_types ()
1754 if (bltn_desc_type_node
)
1757 push_namespace (get_identifier ("std"));
1759 ptr_type_info
= build_pointer_type
1760 (build_qualified_type
1761 (type_info_type_node
, TYPE_QUAL_CONST
));
1763 /* Create the internal type_info structure. This is used as a base for
1764 the other structures. */
1768 ti_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1769 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, const_ptr_type_node
);
1770 fields
[1] = build_lang_decl (FIELD_DECL
, NULL_TREE
, const_string_type_node
);
1771 finish_builtin_type (ti_desc_type_node
, "__type_info_pseudo",
1772 fields
, 1, ptr_type_node
);
1773 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node
) = 1;
1776 /* Fundamental type_info */
1777 bltn_desc_type_node
= create_pseudo_type_info
1778 ("__fundamental_type_info", 0,
1781 /* Pointer and reference type_info. These two fields, qualification mask
1782 and pointer to the pointed to (referenced) type. */
1783 ptr_desc_type_node
= create_pseudo_type_info
1784 ("__pointer_type_info", 0,
1785 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1786 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1788 ref_desc_type_node
= create_pseudo_type_info
1789 ("__reference_type_info", 0,
1790 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1791 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1794 /* Array, function and enum type_info. No additional fields. */
1795 ary_desc_type_node
= create_pseudo_type_info
1796 ("__array_type_info", 0,
1798 func_desc_type_node
= create_pseudo_type_info
1799 ("__function_type_info", 0,
1801 enum_desc_type_node
= create_pseudo_type_info
1802 ("__enum_type_info", 0,
1805 /* Class type_info. Add a flags field. */
1806 class_desc_type_node
= create_pseudo_type_info
1807 ("__class_type_info", 0,
1808 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1811 /* Single public non-virtual base class. Add pointer to base class. */
1812 si_class_desc_type_node
= create_pseudo_type_info
1813 ("__si_class_type_info", 0,
1814 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1815 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1818 /* Base class internal helper. Pointer to base type, offset to base,
1823 fields
[0] = build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1824 fields
[1] = build_lang_decl (FIELD_DECL
, NULL_TREE
, ptrdiff_type_node
),
1825 fields
[2] = build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1826 base_desc_type_node
= make_aggr_type (RECORD_TYPE
);
1827 finish_builtin_type (base_desc_type_node
, "__base_class_type_info_pseudo",
1828 fields
, 2, ptr_type_node
);
1829 TYPE_HAS_CONSTRUCTOR (base_desc_type_node
) = 1;
1832 /* General heirarchy is created as necessary in this vector. */
1833 vmi_class_desc_type_node
= make_tree_vec (10);
1835 /* Pointer to member data type_info. Add pointer to the class, pointer
1836 to the member's type info and qualifications flags. */
1837 ptmd_desc_type_node
= create_pseudo_type_info
1838 ("__ptr_to_member_type_info", 0,
1839 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1840 build_lang_decl (FIELD_DECL
, NULL_TREE
, ptr_type_info
),
1841 build_lang_decl (FIELD_DECL
, NULL_TREE
, integer_type_node
),
1848 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1849 support. Generating them here guarantees consistency with the other
1850 structures. We use the following heuristic to determine when the runtime
1851 is being generated. If std::__fundamental_type_info is defined, and it's
1852 destructor is defined, then the runtime is being built. */
1855 emit_support_tinfos ()
1857 static tree
*const fundamentals
[] =
1863 &signed_wchar_type_node
, &unsigned_wchar_type_node
,
1865 &char_type_node
, &signed_char_type_node
, &unsigned_char_type_node
,
1866 &short_integer_type_node
, &short_unsigned_type_node
,
1867 &integer_type_node
, &unsigned_type_node
,
1868 &long_integer_type_node
, &long_unsigned_type_node
,
1869 &long_long_integer_type_node
, &long_long_unsigned_type_node
,
1870 &float_type_node
, &double_type_node
, &long_double_type_node
,
1872 /* GCC extension types */
1874 &complex_integer_type_node
,
1875 &complex_float_type_node
, &complex_double_type_node
,
1876 &complex_long_double_type_node
,
1882 tree bltn_type
, dtor
;
1885 push_namespace (get_identifier ("std"));
1886 bltn_type
= xref_tag (class_type_node
,
1887 get_identifier ("__fundamental_type_info"), 1);
1890 if (!TYPE_SIZE (bltn_type
))
1892 dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (bltn_type
), 1);
1893 if (DECL_EXTERNAL (dtor
))
1896 for (ix
= 0; fundamentals
[ix
]; ix
++)
1898 tree bltn
= *fundamentals
[ix
];
1899 tree bltn_ptr
= build_pointer_type (bltn
);
1900 tree bltn_const_ptr
= build_pointer_type
1901 (build_qualified_type (bltn
, TYPE_QUAL_CONST
));
1904 tinfo
= get_tinfo_decl (bltn
);
1905 TREE_USED (tinfo
) = 1;
1906 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1908 tinfo
= get_tinfo_decl (bltn_ptr
);
1909 TREE_USED (tinfo
) = 1;
1910 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1912 tinfo
= get_tinfo_decl (bltn_const_ptr
);
1913 TREE_USED (tinfo
) = 1;
1914 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo
)) = 1;
1918 /* Return non-zero, iff T is a type_info variable which has not had a
1919 definition emitted for it. */
1922 tinfo_decl_p (t
, data
)
1924 void *data ATTRIBUTE_UNUSED
;
1926 return TREE_CODE (t
) == VAR_DECL
1927 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t
)) == (t
)
1928 && TREE_TYPE (t
) == tinfo_decl_type
1929 && TREE_TYPE (DECL_NAME (t
));
1932 /* Emit a suitable type_info definition for the type_info decl pointed to by
1933 DECL_PTR. We emit a completely new variable, of the correct type for the
1934 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1935 definition is set to that of the supplied decl, so that they can be tied
1936 up. Mark the supplied decl as having been dealt with. Emitting one
1937 definitions might cause other declarations to be emitted.
1939 We need to do things this way, because we're trying to do something like
1945 extern const A tinfo_var;
1947 const B tinfo_var = {...};
1949 which is not permitted. Also, we've not necessarily seen the definition of B.
1950 So we do something like the following,
1952 extern const A tinfo_var;
1955 const void *vtable_ptr;
1963 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1965 {&B::vtable, "..."},
1969 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1973 emit_tinfo_decl (decl_ptr
, data
)
1975 void *data ATTRIBUTE_UNUSED
;
1977 tree tinfo_decl
= *decl_ptr
;
1978 tree tinfo_type
, decl
;
1980 my_friendly_assert (TREE_TYPE (tinfo_decl
) == tinfo_decl_type
, 20000121);
1981 tinfo_type
= TREE_TYPE (DECL_NAME (tinfo_decl
));
1982 my_friendly_assert (tinfo_type
!= NULL_TREE
, 20000120);
1984 /* Say we've dealt with it. */
1985 TREE_TYPE (DECL_NAME (tinfo_decl
)) = NULL_TREE
;
1987 if (!DECL_NEEDED_P (tinfo_decl
))
1989 create_tinfo_types ();
1990 decl
= synthesize_tinfo_var (tinfo_type
, DECL_ASSEMBLER_NAME (tinfo_decl
));