1 /* typeinfo.cc -- D runtime type identification.
2 Copyright (C) 2013-2020 Free Software Foundation, Inc.
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
20 #include "coretypes.h"
22 #include "dmd/aggregate.h"
24 #include "dmd/errors.h"
25 #include "dmd/expression.h"
26 #include "dmd/globals.h"
27 #include "dmd/identifier.h"
28 #include "dmd/module.h"
29 #include "dmd/mtype.h"
30 #include "dmd/template.h"
31 #include "dmd/target.h"
34 #include "fold-const.h"
35 #include "diagnostic.h"
36 #include "stringpool.h"
38 #include "stor-layout.h"
44 /* D returns type information to the user as TypeInfo class objects, and can
45 be retrieved for any type using `typeid()'. We also use type information
46 to implement many runtime library helpers, including `new', `delete', most
47 dynamic array operations, and all associative array operations.
49 Type information for a particular type is indicated with an ABI defined
50 structure derived from TypeInfo. This would all be very straight forward,
51 but for the fact that the runtime library provides the definitions of the
52 TypeInfo structure and the ABI defined derived classes in `object.d', as
53 well as having specific implementations of TypeInfo for built-in types
54 in `rt/typeinfo`. We cannot build declarations of these directly in the
55 compiler, but we need to layout objects of their type.
57 To get around this, we define layout compatible POD-structs and generate the
58 appropriate initializations for them. When we have to provide a TypeInfo to
59 the user, we cast the internal compiler type to TypeInfo.
61 It is only required that TypeInfo has a definition in `object.d'. It could
62 happen that we are generating a type information for a TypeInfo object that
63 has no declaration. We however only need the addresses of such incomplete
64 TypeInfo objects for static initialization. */
68 TK_TYPEINFO_TYPE
, /* object.TypeInfo */
69 TK_CLASSINFO_TYPE
, /* object.TypeInfo_Class */
70 TK_INTERFACE_TYPE
, /* object.TypeInfo_Interface */
71 TK_STRUCT_TYPE
, /* object.TypeInfo_Struct */
72 TK_POINTER_TYPE
, /* object.TypeInfo_Pointer */
73 TK_ARRAY_TYPE
, /* object.TypeInfo_Array */
74 TK_STATICARRAY_TYPE
, /* object.TypeInfo_StaticArray */
75 TK_ASSOCIATIVEARRAY_TYPE
, /* object.TypeInfo_AssociativeArray */
76 TK_VECTOR_TYPE
, /* object.TypeInfo_Vector */
77 TK_ENUMERAL_TYPE
, /* object.TypeInfo_Enum */
78 TK_FUNCTION_TYPE
, /* object.TypeInfo_Function */
79 TK_DELEGATE_TYPE
, /* object.TypeInfo_Delegate */
80 TK_TYPELIST_TYPE
, /* object.TypeInfo_Tuple */
81 TK_CONST_TYPE
, /* object.TypeInfo_Const */
82 TK_IMMUTABLE_TYPE
, /* object.TypeInfo_Invariant */
83 TK_SHARED_TYPE
, /* object.TypeInfo_Shared */
84 TK_INOUT_TYPE
, /* object.TypeInfo_Inout */
85 TK_CPPTI_TYPE
, /* object.__cpp_type_info_ptr */
89 /* An array of all internal TypeInfo derived types we need.
90 The TypeInfo and ClassInfo types are created early, the
91 remainder are generated as needed. */
93 static GTY(()) tree tinfo_types
[TK_END
];
95 /* Return the kind of TypeInfo used to describe TYPE. */
98 get_typeinfo_kind (Type
*type
)
100 /* Check head shared/const modifiers first. */
101 if (type
->isShared ())
102 return TK_SHARED_TYPE
;
103 else if (type
->isConst ())
104 return TK_CONST_TYPE
;
105 else if (type
->isImmutable ())
106 return TK_IMMUTABLE_TYPE
;
107 else if (type
->isWild ())
108 return TK_INOUT_TYPE
;
113 return TK_POINTER_TYPE
;
116 return TK_ARRAY_TYPE
;
119 return TK_STATICARRAY_TYPE
;
122 return TK_ASSOCIATIVEARRAY_TYPE
;
125 return TK_STRUCT_TYPE
;
128 return TK_VECTOR_TYPE
;
131 return TK_ENUMERAL_TYPE
;
134 return TK_FUNCTION_TYPE
;
137 return TK_DELEGATE_TYPE
;
140 return TK_TYPELIST_TYPE
;
143 if (((TypeClass
*) type
)->sym
->isInterfaceDeclaration ())
144 return TK_INTERFACE_TYPE
;
146 return TK_CLASSINFO_TYPE
;
149 return TK_TYPEINFO_TYPE
;
153 /* Generate the RECORD_TYPE containing the data layout of a TypeInfo derivative
154 as used by the runtime. This layout must be consistent with that defined in
155 the `object.d' module. */
158 make_internal_typeinfo (tinfo_kind tk
, Identifier
*ident
, ...)
162 va_start (ap
, ident
);
164 /* First two fields are from the TypeInfo base class.
165 Note, finish_builtin_struct() expects these fields in reverse order. */
166 tree fields
= create_field_decl (ptr_type_node
, NULL
, 1, 1);
167 DECL_CHAIN (fields
) = create_field_decl (vtbl_ptr_type_node
, NULL
, 1, 1);
169 /* Now add the derived fields. */
170 tree field_type
= va_arg (ap
, tree
);
171 while (field_type
!= NULL_TREE
)
173 tree field
= create_field_decl (field_type
, NULL
, 1, 1);
174 DECL_CHAIN (field
) = fields
;
176 field_type
= va_arg (ap
, tree
);
179 /* Create the TypeInfo type. */
180 tree type
= make_node (RECORD_TYPE
);
181 finish_builtin_struct (type
, ident
->toChars (), fields
, NULL_TREE
);
183 tinfo_types
[tk
] = type
;
188 /* Reference to the `object` module, where all TypeInfo is defined. */
190 static Module
*object_module
;
192 /* Helper for create_frontend_tinfo_types. Creates a typeinfo class
193 declaration incase one wasn't supplied by reading `object.d'. */
196 make_frontend_typeinfo (Identifier
*ident
, ClassDeclaration
*base
= NULL
)
199 base
= Type::dtypeinfo
;
201 gcc_assert (object_module
);
203 /* Create object module in order to complete the semantic. */
204 if (!object_module
->_scope
)
205 object_module
->importAll (NULL
);
207 /* Assignment of global typeinfo variables is managed by the ClassDeclaration
208 constructor, so only need to new the declaration here. */
209 Loc loc
= (object_module
->md
) ? object_module
->md
->loc
: object_module
->loc
;
210 ClassDeclaration
*tinfo
= ClassDeclaration::create (loc
, ident
, NULL
, NULL
,
212 tinfo
->parent
= object_module
;
213 tinfo
->semantic (object_module
->_scope
);
214 tinfo
->baseClass
= base
;
215 /* This is a compiler generated class, and shouldn't be mistaken for being
216 the type declared in the runtime library. */
217 tinfo
->storage_class
|= STCtemp
;
220 /* Make sure the required builtin types exist for generating the TypeInfo
221 variable definitions. */
224 create_tinfo_types (Module
*mod
)
226 /* Build the internal TypeInfo and ClassInfo types.
227 See TypeInfoVisitor for documentation of field layout. */
228 make_internal_typeinfo (TK_TYPEINFO_TYPE
, Identifier::idPool ("TypeInfo"),
231 make_internal_typeinfo (TK_CLASSINFO_TYPE
,
232 Identifier::idPool ("TypeInfo_Class"),
233 array_type_node
, array_type_node
, array_type_node
,
234 array_type_node
, ptr_type_node
, ptr_type_node
,
235 ptr_type_node
, d_uint_type
, ptr_type_node
,
236 array_type_node
, ptr_type_node
, ptr_type_node
, NULL
);
241 /* Same as create_tinfo_types, but builds all front-end TypeInfo variable
245 create_frontend_tinfo_types (void)
247 /* If there's no Object class defined, then neither can TypeInfo be. */
248 if (object_module
== NULL
|| ClassDeclaration::object
== NULL
)
251 /* Create all frontend TypeInfo classes declarations. We rely on all
252 existing, even if only just as stubs. */
253 if (!Type::dtypeinfo
)
254 make_frontend_typeinfo (Identifier::idPool ("TypeInfo"),
255 ClassDeclaration::object
);
257 if (!Type::typeinfoclass
)
258 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Class"));
260 if (!Type::typeinfointerface
)
261 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Interface"));
263 if (!Type::typeinfostruct
)
264 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Struct"));
266 if (!Type::typeinfopointer
)
267 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Pointer"));
269 if (!Type::typeinfoarray
)
270 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Array"));
272 if (!Type::typeinfostaticarray
)
273 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_StaticArray"));
275 if (!Type::typeinfoassociativearray
)
276 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_AssociativeArray"));
278 if (!Type::typeinfoenum
)
279 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Enum"));
281 if (!Type::typeinfofunction
)
282 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Function"));
284 if (!Type::typeinfodelegate
)
285 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Delegate"));
287 if (!Type::typeinfotypelist
)
288 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Tuple"));
290 if (!Type::typeinfoconst
)
291 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Const"));
293 if (!Type::typeinfoinvariant
)
294 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Invariant"),
295 Type::typeinfoconst
);
297 if (!Type::typeinfoshared
)
298 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Shared"),
299 Type::typeinfoconst
);
301 if (!Type::typeinfowild
)
302 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Wild"),
303 Type::typeinfoconst
);
305 if (!Type::typeinfovector
)
306 make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Vector"));
308 if (!ClassDeclaration::cpp_type_info_ptr
)
309 make_frontend_typeinfo (Identifier::idPool ("__cpp_type_info_ptr"),
310 ClassDeclaration::object
);
313 /* Return true if TypeInfo class TINFO is available in the runtime library. */
316 have_typeinfo_p (ClassDeclaration
*tinfo
)
318 /* Run-time typeinfo disabled on command line. */
319 if (!global
.params
.useTypeInfo
)
322 /* Can't layout TypeInfo if type is not declared, or is an opaque
323 declaration in the object module. */
324 if (!tinfo
|| !tinfo
->members
)
327 /* Typeinfo is compiler-generated. */
328 if (tinfo
->storage_class
& STCtemp
)
334 /* Implements the visitor interface to build the TypeInfo layout of all
335 TypeInfoDeclaration AST classes emitted from the D Front-end.
336 All visit methods accept one parameter D, which holds the frontend AST
337 of the TypeInfo class. They also don't return any value, instead the
338 generated symbol is cached internally and returned from the caller. */
340 class TypeInfoVisitor
: public Visitor
342 using Visitor::visit
;
345 vec
<constructor_elt
, va_gc
> *init_
;
347 /* Build an internal comdat symbol for the manifest constant VALUE, so that
348 its address can be taken. */
350 tree
internal_reference (tree value
)
352 /* Use the typeinfo decl name as a prefix for the internal symbol. */
353 const char *prefix
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (this->decl_
));
354 tree decl
= build_artificial_decl (TREE_TYPE (value
), value
, prefix
);
356 /* The internal pointer reference should be public, but not visible outside
357 the compilation unit. */
358 DECL_EXTERNAL (decl
) = 0;
359 TREE_PUBLIC (decl
) = 1;
360 DECL_VISIBILITY (decl
) = VISIBILITY_INTERNAL
;
361 d_comdat_linkage (decl
);
367 /* Add VALUE to the constructor values list. */
369 void layout_field (tree value
)
371 CONSTRUCTOR_APPEND_ELT (this->init_
, NULL_TREE
, value
);
374 /* Write out STR as a static D string literal. */
376 void layout_string (const char *str
)
378 unsigned len
= strlen (str
);
379 tree value
= build_string (len
, str
);
381 TREE_TYPE (value
) = make_array_type (Type::tchar
, len
);
382 TREE_CONSTANT (value
) = 1;
383 TREE_READONLY (value
) = 1;
384 TREE_STATIC (value
) = 1;
386 /* Taking the address, so assign the literal to a static var. */
387 tree decl
= this->internal_reference (value
);
388 TREE_READONLY (decl
) = 1;
390 value
= d_array_value (build_ctype (Type::tchar
->arrayOf ()),
391 size_int (len
), build_address (decl
));
392 this->layout_field (value
);
396 /* Write out the __vptr and __monitor fields of class CD. */
398 void layout_base (ClassDeclaration
*cd
)
400 gcc_assert (cd
!= NULL
);
402 if (have_typeinfo_p (cd
))
403 this->layout_field (build_address (get_vtable_decl (cd
)));
405 this->layout_field (null_pointer_node
);
407 this->layout_field (null_pointer_node
);
410 /* Write out the interfaces field of class CD.
411 Returns the array of interfaces that the field is pointing to. */
413 tree
layout_interfaces (ClassDeclaration
*cd
)
415 size_t offset
= int_size_in_bytes (tinfo_types
[TK_CLASSINFO_TYPE
]);
416 tree csym
= build_address (get_classinfo_decl (cd
));
418 /* Put out the offset to where vtblInterfaces are written. */
419 tree value
= d_array_value (array_type_node
,
420 size_int (cd
->vtblInterfaces
->length
),
421 build_offset (csym
, size_int (offset
)));
422 this->layout_field (value
);
424 /* Internally, the compiler sees Interface as:
427 The run-time layout of Interface is:
428 TypeInfo_Class classinfo;
431 vec
<constructor_elt
, va_gc
> *elms
= NULL
;
433 for (size_t i
= 0; i
< cd
->vtblInterfaces
->length
; i
++)
435 BaseClass
*b
= (*cd
->vtblInterfaces
)[i
];
436 ClassDeclaration
*id
= b
->sym
;
437 vec
<constructor_elt
, va_gc
> *v
= NULL
;
439 /* Fill in the vtbl[]. */
440 if (!cd
->isInterfaceDeclaration ())
441 b
->fillVtbl (cd
, &b
->vtbl
, 1);
443 /* ClassInfo for the interface. */
444 value
= build_address (get_classinfo_decl (id
));
445 CONSTRUCTOR_APPEND_ELT (v
, size_int (0), value
);
447 if (!cd
->isInterfaceDeclaration ())
449 /* The vtable of the interface length and ptr. */
450 unsigned voffset
= base_vtable_offset (cd
, b
);
451 gcc_assert (voffset
!= 0u);
452 value
= build_offset (csym
, size_int (voffset
));
454 CONSTRUCTOR_APPEND_ELT (v
, size_int (1), size_int (id
->vtbl
.length
));
455 CONSTRUCTOR_APPEND_ELT (v
, size_int (2), value
);
458 /* The 'this' offset. */
459 CONSTRUCTOR_APPEND_ELT (v
, size_int (3), size_int (b
->offset
));
461 /* Add to the array of interfaces. */
462 value
= build_constructor (vtbl_interface_type_node
, v
);
463 CONSTRUCTOR_APPEND_ELT (elms
, size_int (i
), value
);
466 tree domain
= size_int (cd
->vtblInterfaces
->length
- 1);
467 tree arrtype
= build_array_type (vtbl_interface_type_node
,
468 build_index_type (domain
));
469 return build_constructor (arrtype
, elms
);
472 /* Write out the interfacing vtable[] of base class BCD that will be accessed
473 from the overriding class CD. If both are the same class, then this will
474 be its own vtable. INDEX is the offset in the interfaces array of the
475 base class where the Interface reference can be found.
476 This must be mirrored with base_vtable_offset(). */
478 void layout_base_vtable (ClassDeclaration
*cd
, ClassDeclaration
*bcd
,
481 BaseClass
*bs
= (*bcd
->vtblInterfaces
)[index
];
482 ClassDeclaration
*id
= bs
->sym
;
483 vec
<constructor_elt
, va_gc
> *elms
= NULL
;
484 FuncDeclarations bvtbl
;
486 if (id
->vtbl
.length
== 0 || base_vtable_offset (cd
, bs
) == ~0u)
489 /* Fill bvtbl with the functions we want to put out. */
490 if (cd
!= bcd
&& !bs
->fillVtbl (cd
, &bvtbl
, 0))
493 /* First entry is struct Interface reference. */
494 if (id
->vtblOffset ())
496 size_t offset
= int_size_in_bytes (tinfo_types
[TK_CLASSINFO_TYPE
]);
497 offset
+= (index
* int_size_in_bytes (vtbl_interface_type_node
));
498 tree value
= build_offset (build_address (get_classinfo_decl (bcd
)),
500 CONSTRUCTOR_APPEND_ELT (elms
, size_zero_node
, value
);
503 for (size_t i
= id
->vtblOffset () ? 1 : 0; i
< id
->vtbl
.length
; i
++)
505 FuncDeclaration
*fd
= (cd
== bcd
) ? bs
->vtbl
[i
] : bvtbl
[i
];
508 tree value
= build_address (make_thunk (fd
, bs
->offset
));
509 CONSTRUCTOR_APPEND_ELT (elms
, size_int (i
), value
);
513 tree vtbldomain
= build_index_type (size_int (id
->vtbl
.length
- 1));
514 tree vtbltype
= build_array_type (vtable_entry_type
, vtbldomain
);
515 tree value
= build_constructor (vtbltype
, elms
);
516 this->layout_field (value
);
521 TypeInfoVisitor (tree decl
)
527 /* Return the completed constructor for the TypeInfo record. */
531 return build_struct_literal (TREE_TYPE (this->decl_
), this->init_
);
534 /* Layout of TypeInfo is:
538 void visit (TypeInfoDeclaration
*)
540 /* The vtable for TypeInfo. */
541 this->layout_base (Type::dtypeinfo
);
544 /* Layout of TypeInfo_Const is:
549 void visit (TypeInfoConstDeclaration
*d
)
551 Type
*tm
= d
->tinfo
->mutableOf ();
554 /* The vtable for TypeInfo_Const. */
555 this->layout_base (Type::typeinfoconst
);
557 /* TypeInfo for the mutable type. */
558 this->layout_field (build_typeinfo (d
->loc
, tm
));
561 /* Layout of TypeInfo_Immutable is:
566 void visit (TypeInfoInvariantDeclaration
*d
)
568 Type
*tm
= d
->tinfo
->mutableOf ();
571 /* The vtable for TypeInfo_Invariant. */
572 this->layout_base (Type::typeinfoinvariant
);
574 /* TypeInfo for the mutable type. */
575 this->layout_field (build_typeinfo (d
->loc
, tm
));
578 /* Layout of TypeInfo_Shared is:
583 void visit (TypeInfoSharedDeclaration
*d
)
585 Type
*tm
= d
->tinfo
->unSharedOf ();
588 /* The vtable for TypeInfo_Shared. */
589 this->layout_base (Type::typeinfoshared
);
591 /* TypeInfo for the unshared type. */
592 this->layout_field (build_typeinfo (d
->loc
, tm
));
595 /* Layout of TypeInfo_Inout is:
600 void visit (TypeInfoWildDeclaration
*d
)
602 Type
*tm
= d
->tinfo
->mutableOf ();
605 /* The vtable for TypeInfo_Inout. */
606 this->layout_base (Type::typeinfowild
);
608 /* TypeInfo for the mutable type. */
609 this->layout_field (build_typeinfo (d
->loc
, tm
));
612 /* Layout of TypeInfo_Enum is:
619 void visit (TypeInfoEnumDeclaration
*d
)
621 gcc_assert (d
->tinfo
->ty
== Tenum
);
622 TypeEnum
*ti
= (TypeEnum
*) d
->tinfo
;
623 EnumDeclaration
*ed
= ti
->sym
;
625 /* The vtable for TypeInfo_Enum. */
626 this->layout_base (Type::typeinfoenum
);
628 /* TypeInfo for enum members. */
629 tree memtype
= (ed
->memtype
) ? build_typeinfo (d
->loc
, ed
->memtype
)
631 this->layout_field (memtype
);
633 /* Name of the enum declaration. */
634 this->layout_string (ed
->toPrettyChars ());
636 /* Default initializer for enum. */
637 if (ed
->members
&& !d
->tinfo
->isZeroInit ())
639 tree length
= size_int (ed
->type
->size ());
640 tree ptr
= build_address (enum_initializer_decl (ed
));
641 this->layout_field (d_array_value (array_type_node
, length
, ptr
));
644 this->layout_field (null_array_node
);
647 /* Layout of TypeInfo_Pointer is:
652 void visit (TypeInfoPointerDeclaration
*d
)
654 gcc_assert (d
->tinfo
->ty
== Tpointer
);
655 TypePointer
*ti
= (TypePointer
*) d
->tinfo
;
657 /* The vtable for TypeInfo_Pointer. */
658 this->layout_base (Type::typeinfopointer
);
660 /* TypeInfo for pointer-to type. */
661 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
664 /* Layout of TypeInfo_Array is:
669 void visit (TypeInfoArrayDeclaration
*d
)
671 gcc_assert (d
->tinfo
->ty
== Tarray
);
672 TypeDArray
*ti
= (TypeDArray
*) d
->tinfo
;
674 /* The vtable for TypeInfo_Array. */
675 this->layout_base (Type::typeinfoarray
);
677 /* TypeInfo for array of type. */
678 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
681 /* Layout of TypeInfo_StaticArray is:
687 void visit (TypeInfoStaticArrayDeclaration
*d
)
689 gcc_assert (d
->tinfo
->ty
== Tsarray
);
690 TypeSArray
*ti
= (TypeSArray
*) d
->tinfo
;
692 /* The vtable for TypeInfo_StaticArray. */
693 this->layout_base (Type::typeinfostaticarray
);
695 /* TypeInfo for array of type. */
696 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
698 /* Static array length. */
699 this->layout_field (size_int (ti
->dim
->toInteger ()));
702 /* Layout of TypeInfo_AssociativeArray is:
708 void visit (TypeInfoAssociativeArrayDeclaration
*d
)
710 gcc_assert (d
->tinfo
->ty
== Taarray
);
711 TypeAArray
*ti
= (TypeAArray
*) d
->tinfo
;
713 /* The vtable for TypeInfo_AssociativeArray. */
714 this->layout_base (Type::typeinfoassociativearray
);
716 /* TypeInfo for value of type. */
717 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
719 /* TypeInfo for index of type. */
720 this->layout_field (build_typeinfo (d
->loc
, ti
->index
));
723 /* Layout of TypeInfo_Vector is:
728 void visit (TypeInfoVectorDeclaration
*d
)
730 gcc_assert (d
->tinfo
->ty
== Tvector
);
731 TypeVector
*ti
= (TypeVector
*) d
->tinfo
;
733 /* The vtable for TypeInfo_Vector. */
734 this->layout_base (Type::typeinfovector
);
736 /* TypeInfo for equivalent static array. */
737 this->layout_field (build_typeinfo (d
->loc
, ti
->basetype
));
740 /* Layout of TypeInfo_Function is:
746 void visit (TypeInfoFunctionDeclaration
*d
)
748 gcc_assert (d
->tinfo
->ty
== Tfunction
&& d
->tinfo
->deco
!= NULL
);
749 TypeFunction
*ti
= (TypeFunction
*) d
->tinfo
;
751 /* The vtable for TypeInfo_Function. */
752 this->layout_base (Type::typeinfofunction
);
754 /* TypeInfo for function return value. */
755 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
757 /* Mangled name of function declaration. */
758 this->layout_string (d
->tinfo
->deco
);
761 /* Layout of TypeInfo_Delegate is:
767 void visit (TypeInfoDelegateDeclaration
*d
)
769 gcc_assert (d
->tinfo
->ty
== Tdelegate
&& d
->tinfo
->deco
!= NULL
);
770 TypeDelegate
*ti
= (TypeDelegate
*) d
->tinfo
;
772 /* The vtable for TypeInfo_Delegate. */
773 this->layout_base (Type::typeinfodelegate
);
775 /* TypeInfo for delegate return value. */
776 this->layout_field (build_typeinfo (d
->loc
, ti
->next
));
778 /* Mangled name of delegate declaration. */
779 this->layout_string (d
->tinfo
->deco
);
782 /* Layout of ClassInfo/TypeInfo_Class is:
788 Interface[] interfaces;
791 void function(Object) classInvariant;
794 OffsetTypeInfo[] m_offTi;
795 void function(Object) defaultConstructor;
796 immutable(void)* m_RTInfo;
798 Information relating to interfaces, and their vtables are laid out
799 immediately after the named fields, if there is anything to write. */
801 void visit (TypeInfoClassDeclaration
*d
)
803 gcc_assert (d
->tinfo
->ty
== Tclass
);
804 TypeClass
*ti
= (TypeClass
*) d
->tinfo
;
805 ClassDeclaration
*cd
= ti
->sym
;
807 /* The vtable for ClassInfo. */
808 this->layout_base (Type::typeinfoclass
);
813 tree interfaces
= NULL_TREE
;
815 if (!cd
->isInterfaceDeclaration ())
817 /* Default initializer for class. */
818 tree init
= aggregate_initializer_decl (cd
);
819 tree value
= d_array_value (array_type_node
, size_int (cd
->structsize
),
820 build_address (init
));
821 this->layout_field (value
);
823 /* Name of the class declaration. */
824 const char *name
= cd
->ident
->toChars ();
825 if (!(strlen (name
) > 9 && memcmp (name
, "TypeInfo_", 9) == 0))
826 name
= cd
->toPrettyChars ();
827 this->layout_string (name
);
829 /* The vtable of the class declaration. */
830 value
= d_array_value (array_type_node
, size_int (cd
->vtbl
.length
),
831 build_address (get_vtable_decl (cd
)));
832 this->layout_field (value
);
834 /* Array of base interfaces that have their own vtable. */
835 if (cd
->vtblInterfaces
->length
)
836 interfaces
= this->layout_interfaces (cd
);
838 this->layout_field (null_array_node
);
840 /* TypeInfo_Class base; */
841 tree base
= (cd
->baseClass
)
842 ? build_address (get_classinfo_decl (cd
->baseClass
))
844 this->layout_field (base
);
846 /* void *destructor; */
847 tree dtor
= (cd
->dtor
) ? build_address (get_symbol_decl (cd
->dtor
))
849 this->layout_field (dtor
);
851 /* void function(Object) classInvariant; */
852 tree inv
= (cd
->inv
) ? build_address (get_symbol_decl (cd
->inv
))
854 this->layout_field (inv
);
856 /* ClassFlags m_flags; */
857 ClassFlags::Type flags
= ClassFlags::hasOffTi
;
858 if (cd
->isCOMclass ())
859 flags
|= ClassFlags::isCOMclass
;
861 if (cd
->isCPPclass ())
862 flags
|= ClassFlags::isCPPclass
;
864 flags
|= ClassFlags::hasGetMembers
;
865 flags
|= ClassFlags::hasTypeInfo
;
868 flags
|= ClassFlags::hasCtor
;
870 for (ClassDeclaration
*bcd
= cd
; bcd
; bcd
= bcd
->baseClass
)
874 flags
|= ClassFlags::hasDtor
;
879 if (cd
->isAbstract ())
880 flags
|= ClassFlags::isAbstract
;
882 for (ClassDeclaration
*bcd
= cd
; bcd
; bcd
= bcd
->baseClass
)
887 for (size_t i
= 0; i
< bcd
->members
->length
; i
++)
889 Dsymbol
*sm
= (*bcd
->members
)[i
];
890 if (sm
->hasPointers ())
895 flags
|= ClassFlags::noPointers
;
898 this->layout_field (build_integer_cst (flags
, d_uint_type
));
900 /* void *deallocator; */
901 tree ddtor
= (cd
->aggDelete
)
902 ? build_address (get_symbol_decl (cd
->aggDelete
))
904 this->layout_field (ddtor
);
906 /* OffsetTypeInfo[] m_offTi; (not implemented) */
907 this->layout_field (null_array_node
);
909 /* void function(Object) defaultConstructor; */
910 if (cd
->defaultCtor
&& !(cd
->defaultCtor
->storage_class
& STCdisable
))
912 tree dctor
= get_symbol_decl (cd
->defaultCtor
);
913 this->layout_field (build_address (dctor
));
916 this->layout_field (null_pointer_node
);
918 /* immutable(void)* m_RTInfo; */
920 this->layout_field (build_expr (cd
->getRTInfo
, true));
921 else if (!(flags
& ClassFlags::noPointers
))
922 this->layout_field (size_one_node
);
926 /* No initializer for interface. */
927 this->layout_field (null_array_node
);
929 /* Name of the interface declaration. */
930 this->layout_string (cd
->toPrettyChars ());
932 /* No vtable for interface declaration. */
933 this->layout_field (null_array_node
);
935 /* Array of base interfaces that have their own vtable. */
936 if (cd
->vtblInterfaces
->length
)
937 interfaces
= this->layout_interfaces (cd
);
939 this->layout_field (null_array_node
);
941 /* TypeInfo_Class base;
943 void function(Object) classInvariant; */
944 this->layout_field (null_pointer_node
);
945 this->layout_field (null_pointer_node
);
946 this->layout_field (null_pointer_node
);
948 /* ClassFlags m_flags; */
949 ClassFlags::Type flags
= ClassFlags::hasOffTi
;
950 flags
|= ClassFlags::hasTypeInfo
;
951 if (cd
->isCOMinterface ())
952 flags
|= ClassFlags::isCOMclass
;
954 this->layout_field (build_integer_cst (flags
, d_uint_type
));
956 /* void *deallocator;
957 OffsetTypeInfo[] m_offTi; (not implemented)
958 void function(Object) defaultConstructor; */
959 this->layout_field (null_pointer_node
);
960 this->layout_field (null_array_node
);
961 this->layout_field (null_pointer_node
);
963 /* immutable(void)* m_RTInfo; */
965 this->layout_field (build_expr (cd
->getRTInfo
, true));
967 this->layout_field (null_pointer_node
);
970 /* Put out array of Interfaces. */
971 if (interfaces
!= NULL_TREE
)
972 this->layout_field (interfaces
);
974 if (!cd
->isInterfaceDeclaration ())
976 /* Put out this class' interface vtables[]. */
977 for (size_t i
= 0; i
< cd
->vtblInterfaces
->length
; i
++)
978 this->layout_base_vtable (cd
, cd
, i
);
980 /* Put out the overriding interface vtables[]. */
981 for (ClassDeclaration
*bcd
= cd
->baseClass
; bcd
; bcd
= bcd
->baseClass
)
983 for (size_t i
= 0; i
< bcd
->vtblInterfaces
->length
; i
++)
984 this->layout_base_vtable (cd
, bcd
, i
);
989 /* Layout of TypeInfo_Interface is:
992 TypeInfo_Class info; */
994 void visit (TypeInfoInterfaceDeclaration
*d
)
996 gcc_assert (d
->tinfo
->ty
== Tclass
);
997 TypeClass
*ti
= (TypeClass
*) d
->tinfo
;
999 if (!ti
->sym
->vclassinfo
)
1000 ti
->sym
->vclassinfo
= TypeInfoClassDeclaration::create (ti
);
1002 /* The vtable for TypeInfo_Interface. */
1003 this->layout_base (Type::typeinfointerface
);
1005 /* TypeInfo for class inheriting the interface. */
1006 tree tidecl
= get_typeinfo_decl (ti
->sym
->vclassinfo
);
1007 this->layout_field (build_address (tidecl
));
1010 /* Layout of TypeInfo_Struct is:
1015 hash_t function(in void*) xtoHash;
1016 bool function(in void*, in void*) xopEquals;
1017 int function(in void*, in void*) xopCmp;
1018 string function(const(void)*) xtoString;
1019 StructFlags m_flags;
1020 void function(void*) xdtor;
1021 void function(void*) xpostblit;
1026 immutable(void)* xgetRTInfo; */
1028 void visit (TypeInfoStructDeclaration
*d
)
1030 gcc_assert (d
->tinfo
->ty
== Tstruct
);
1031 TypeStruct
*ti
= (TypeStruct
*) d
->tinfo
;
1032 StructDeclaration
*sd
= ti
->sym
;
1034 /* The vtable for TypeInfo_Struct. */
1035 this->layout_base (Type::typeinfostruct
);
1040 /* Name of the struct declaration. */
1041 this->layout_string (sd
->toPrettyChars ());
1043 /* Default initializer for struct. */
1044 tree ptr
= (sd
->zeroInit
) ? null_pointer_node
1045 : build_address (aggregate_initializer_decl (sd
));
1046 this->layout_field (d_array_value (array_type_node
,
1047 size_int (sd
->structsize
), ptr
));
1049 /* hash_t function (in void*) xtoHash; */
1050 tree xhash
= (sd
->xhash
) ? build_address (get_symbol_decl (sd
->xhash
))
1051 : null_pointer_node
;
1052 this->layout_field (xhash
);
1056 TypeFunction
*tf
= (TypeFunction
*) sd
->xhash
->type
;
1057 gcc_assert (tf
->ty
== Tfunction
);
1058 if (!tf
->isnothrow
|| tf
->trust
== TRUSTsystem
)
1060 warning (sd
->xhash
->loc
, "toHash() must be declared as "
1061 "extern (D) size_t toHash() const nothrow @safe, "
1062 "not %s", tf
->toChars ());
1066 /* bool function(in void*, in void*) xopEquals; */
1067 tree xeq
= (sd
->xeq
) ? build_address (get_symbol_decl (sd
->xeq
))
1068 : null_pointer_node
;
1069 this->layout_field (xeq
);
1071 /* int function(in void*, in void*) xopCmp; */
1072 tree xcmp
= (sd
->xcmp
) ? build_address (get_symbol_decl (sd
->xcmp
))
1073 : null_pointer_node
;
1074 this->layout_field (xcmp
);
1076 /* string function(const(void)*) xtoString; */
1077 FuncDeclaration
*fdx
= search_toString (sd
);
1079 this->layout_field (build_address (get_symbol_decl (fdx
)));
1081 this->layout_field (null_pointer_node
);
1083 /* StructFlags m_flags; */
1084 StructFlags::Type m_flags
= 0;
1085 if (ti
->hasPointers ())
1086 m_flags
|= StructFlags::hasPointers
;
1087 this->layout_field (build_integer_cst (m_flags
, d_uint_type
));
1089 /* void function(void*) xdtor; */
1090 tree dtor
= (sd
->dtor
) ? build_address (get_symbol_decl (sd
->dtor
))
1091 : null_pointer_node
;
1092 this->layout_field (dtor
);
1094 /* void function(void*) xpostblit; */
1095 if (sd
->postblit
&& !(sd
->postblit
->storage_class
& STCdisable
))
1096 this->layout_field (build_address (get_symbol_decl (sd
->postblit
)));
1098 this->layout_field (null_pointer_node
);
1101 this->layout_field (build_integer_cst (ti
->alignsize (), d_uint_type
));
1103 if (global
.params
.is64bit
)
1105 /* TypeInfo m_arg1; */
1106 tree arg1type
= (sd
->arg1type
) ? build_typeinfo (d
->loc
, sd
->arg1type
)
1107 : null_pointer_node
;
1108 this->layout_field (arg1type
);
1110 /* TypeInfo m_arg2; */
1111 tree arg2type
= (sd
->arg2type
) ? build_typeinfo (d
->loc
, sd
->arg2type
)
1112 : null_pointer_node
;
1113 this->layout_field (arg2type
);
1116 /* immutable(void)* xgetRTInfo; */
1118 this->layout_field (build_expr (sd
->getRTInfo
, true));
1119 else if (m_flags
& StructFlags::hasPointers
)
1120 this->layout_field (size_one_node
);
1123 /* Layout of TypeInfo_Tuple is:
1126 TypeInfo[] elements; */
1128 void visit (TypeInfoTupleDeclaration
*d
)
1130 gcc_assert (d
->tinfo
->ty
== Ttuple
);
1131 TypeTuple
*ti
= (TypeTuple
*) d
->tinfo
;
1133 /* The vtable for TypeInfo_Tuple. */
1134 this->layout_base (Type::typeinfotypelist
);
1136 /* TypeInfo[] elements; */
1137 Type
*satype
= Type::tvoidptr
->sarrayOf (ti
->arguments
->length
);
1138 vec
<constructor_elt
, va_gc
> *elms
= NULL
;
1139 for (size_t i
= 0; i
< ti
->arguments
->length
; i
++)
1141 Parameter
*arg
= (*ti
->arguments
)[i
];
1142 CONSTRUCTOR_APPEND_ELT (elms
, size_int (i
),
1143 build_typeinfo (d
->loc
, arg
->type
));
1145 tree ctor
= build_constructor (build_ctype (satype
), elms
);
1146 tree decl
= this->internal_reference (ctor
);
1148 tree length
= size_int (ti
->arguments
->length
);
1149 tree ptr
= build_address (decl
);
1150 this->layout_field (d_array_value (array_type_node
, length
, ptr
));
1152 rest_of_decl_compilation (decl
, 1, 0);
1157 /* Main entry point for TypeInfoVisitor interface to generate
1158 TypeInfo constructor for the TypeInfoDeclaration AST class D. */
1161 layout_typeinfo (TypeInfoDeclaration
*d
)
1163 if (!Type::dtypeinfo
)
1164 create_frontend_tinfo_types ();
1166 TypeInfoVisitor v
= TypeInfoVisitor (get_typeinfo_decl (d
));
1171 /* Like layout_typeinfo, but generates the TypeInfo_Class for
1172 the class or interface declaration CD. */
1175 layout_classinfo (ClassDeclaration
*cd
)
1177 if (!Type::dtypeinfo
)
1178 create_frontend_tinfo_types ();
1180 TypeInfoClassDeclaration
*d
= TypeInfoClassDeclaration::create (cd
->type
);
1181 TypeInfoVisitor v
= TypeInfoVisitor (get_classinfo_decl (cd
));
1186 /* Layout fields that immediately come after the classinfo type for DECL if
1187 there's any interfaces or interface vtables to be added.
1188 This must be mirrored with base_vtable_offset(). */
1191 layout_classinfo_interfaces (ClassDeclaration
*decl
)
1193 tree type
= tinfo_types
[TK_CLASSINFO_TYPE
];
1194 size_t structsize
= int_size_in_bytes (type
);
1196 if (decl
->vtblInterfaces
->length
)
1198 size_t interfacesize
= int_size_in_bytes (vtbl_interface_type_node
);
1201 type
= copy_aggregate_type (type
);
1203 /* First layout the static array of Interface, which provides information
1204 about the vtables that follow. */
1205 tree domain
= size_int (decl
->vtblInterfaces
->length
- 1);
1206 tree arrtype
= build_array_type (vtbl_interface_type_node
,
1207 build_index_type (domain
));
1208 field
= create_field_decl (arrtype
, NULL
, 1, 1);
1209 insert_aggregate_field (type
, field
, structsize
);
1210 structsize
+= decl
->vtblInterfaces
->length
* interfacesize
;
1212 /* For each interface, layout each vtable. */
1213 for (size_t i
= 0; i
< decl
->vtblInterfaces
->length
; i
++)
1215 BaseClass
*b
= (*decl
->vtblInterfaces
)[i
];
1216 ClassDeclaration
*id
= b
->sym
;
1217 unsigned offset
= base_vtable_offset (decl
, b
);
1219 if (id
->vtbl
.length
&& offset
!= ~0u)
1221 tree vtbldomain
= build_index_type (size_int (id
->vtbl
.length
- 1));
1222 tree vtbltype
= build_array_type (vtable_entry_type
, vtbldomain
);
1224 field
= create_field_decl (vtbltype
, NULL
, 1, 1);
1225 insert_aggregate_field (type
, field
, offset
);
1226 structsize
+= id
->vtbl
.length
* Target::ptrsize
;
1231 /* Layout the arrays of overriding interface vtables. */
1232 for (ClassDeclaration
*bcd
= decl
->baseClass
; bcd
; bcd
= bcd
->baseClass
)
1234 for (size_t i
= 0; i
< bcd
->vtblInterfaces
->length
; i
++)
1236 BaseClass
*b
= (*bcd
->vtblInterfaces
)[i
];
1237 ClassDeclaration
*id
= b
->sym
;
1238 unsigned offset
= base_vtable_offset (decl
, b
);
1240 if (id
->vtbl
.length
&& offset
!= ~0u)
1242 if (type
== tinfo_types
[TK_CLASSINFO_TYPE
])
1243 type
= copy_aggregate_type (type
);
1245 tree vtbldomain
= build_index_type (size_int (id
->vtbl
.length
- 1));
1246 tree vtbltype
= build_array_type (vtable_entry_type
, vtbldomain
);
1248 tree field
= create_field_decl (vtbltype
, NULL
, 1, 1);
1249 insert_aggregate_field (type
, field
, offset
);
1250 structsize
+= id
->vtbl
.length
* Target::ptrsize
;
1255 /* Update the type size and record mode for the classinfo type. */
1256 if (type
!= tinfo_types
[TK_CLASSINFO_TYPE
])
1257 finish_aggregate_type (structsize
, TYPE_ALIGN_UNIT (type
), type
);
1262 /* Returns true if the TypeInfo for TYPE should be placed in
1263 the runtime library. */
1266 builtin_typeinfo_p (Type
*type
)
1268 if (type
->isTypeBasic () || type
->ty
== Tclass
|| type
->ty
== Tnull
)
1271 if (type
->ty
== Tarray
)
1273 /* Strings are so common, make them builtin. */
1274 Type
*next
= type
->nextOf ();
1276 && ((next
->isTypeBasic () != NULL
&& !next
->mod
)
1277 || (next
->ty
== Tchar
&& next
->mod
== MODimmutable
)
1278 || (next
->ty
== Tchar
&& next
->mod
== MODconst
));
1284 /* Implements a visitor interface to create the decl tree for TypeInfo decls.
1285 TypeInfo_Class objects differ in that they also have information about
1286 the class type packed immediately after the TypeInfo symbol.
1288 If the frontend had an interface to allow distinguishing being these two
1289 AST types, then that would be better for us. */
1291 class TypeInfoDeclVisitor
: public Visitor
1293 using Visitor::visit
;
1296 TypeInfoDeclVisitor (void)
1300 void visit (TypeInfoDeclaration
*tid
)
1302 tree ident
= get_identifier (tid
->ident
->toChars ());
1303 tree type
= tinfo_types
[get_typeinfo_kind (tid
->tinfo
)];
1304 gcc_assert (type
!= NULL_TREE
);
1306 tid
->csym
= declare_extern_var (ident
, type
);
1307 DECL_LANG_SPECIFIC (tid
->csym
) = build_lang_decl (tid
);
1309 DECL_CONTEXT (tid
->csym
) = d_decl_context (tid
);
1310 TREE_READONLY (tid
->csym
) = 1;
1312 /* Built-in typeinfo will be referenced as one-only. */
1313 gcc_assert (!tid
->isInstantiated ());
1315 if (builtin_typeinfo_p (tid
->tinfo
))
1316 d_linkonce_linkage (tid
->csym
);
1318 d_comdat_linkage (tid
->csym
);
1321 void visit (TypeInfoClassDeclaration
*tid
)
1323 gcc_assert (tid
->tinfo
->ty
== Tclass
);
1324 TypeClass
*tc
= (TypeClass
*) tid
->tinfo
;
1325 tid
->csym
= get_classinfo_decl (tc
->sym
);
1329 /* Get the VAR_DECL of the TypeInfo for DECL. If this does not yet exist,
1330 create it. The TypeInfo decl provides information about the type of a given
1331 expression or object. */
1334 get_typeinfo_decl (TypeInfoDeclaration
*decl
)
1339 gcc_assert (decl
->tinfo
->ty
!= Terror
);
1341 TypeInfoDeclVisitor v
= TypeInfoDeclVisitor ();
1343 gcc_assert (decl
->csym
!= NULL_TREE
);
1348 /* Get the VAR_DECL of the ClassInfo for DECL. If this does not yet exist,
1349 create it. The ClassInfo decl provides information about the dynamic type
1350 of a given class type or object. */
1353 get_classinfo_decl (ClassDeclaration
*decl
)
1358 InterfaceDeclaration
*id
= decl
->isInterfaceDeclaration ();
1359 tree ident
= mangle_internal_decl (decl
, id
? "__Interface" : "__Class", "Z");
1360 tree type
= layout_classinfo_interfaces (decl
);
1362 decl
->csym
= declare_extern_var (ident
, type
);
1363 DECL_LANG_SPECIFIC (decl
->csym
) = build_lang_decl (NULL
);
1365 /* Class is a reference, want the record type. */
1366 DECL_CONTEXT (decl
->csym
) = TREE_TYPE (build_ctype (decl
->type
));
1367 /* ClassInfo cannot be const data, because we use the monitor on it. */
1368 TREE_READONLY (decl
->csym
) = 0;
1373 /* Returns typeinfo reference for TYPE. */
1376 build_typeinfo (const Loc
&loc
, Type
*type
)
1378 if (!global
.params
.useTypeInfo
)
1380 static int warned
= 0;
1384 error_at (make_location_t (loc
),
1385 "%<object.TypeInfo%> cannot be used with %<-fno-rtti%>");
1390 gcc_assert (type
->ty
!= Terror
);
1391 create_typeinfo (type
, NULL
);
1392 return build_address (get_typeinfo_decl (type
->vtinfo
));
1395 /* Like layout_classinfo, but generates an Object that wraps around a
1396 pointer to C++ type_info so it can be distinguished from D TypeInfo. */
1399 layout_cpp_typeinfo (ClassDeclaration
*cd
)
1401 if (!Type::dtypeinfo
)
1402 create_frontend_tinfo_types ();
1404 gcc_assert (cd
->isCPPclass ());
1406 tree decl
= get_cpp_typeinfo_decl (cd
);
1407 vec
<constructor_elt
, va_gc
> *init
= NULL
;
1409 /* Use the vtable of __cpp_type_info_ptr, the EH personality routine
1410 expects this, as it uses .classinfo identity comparison to test for
1411 C++ catch handlers. */
1412 tree vptr
= get_vtable_decl (ClassDeclaration::cpp_type_info_ptr
);
1413 CONSTRUCTOR_APPEND_ELT (init
, NULL_TREE
, build_address (vptr
));
1414 CONSTRUCTOR_APPEND_ELT (init
, NULL_TREE
, null_pointer_node
);
1416 /* Let C++ do the RTTI generation, and just reference the symbol as
1417 extern, knowing the underlying type is not required. */
1418 const char *ident
= Target::cppTypeInfoMangle (cd
);
1419 tree typeinfo
= declare_extern_var (get_identifier (ident
),
1421 TREE_READONLY (typeinfo
) = 1;
1422 CONSTRUCTOR_APPEND_ELT (init
, NULL_TREE
, build_address (typeinfo
));
1424 /* Build the initializer and emit. */
1425 DECL_INITIAL (decl
) = build_struct_literal (TREE_TYPE (decl
), init
);
1426 DECL_EXTERNAL (decl
) = 0;
1428 rest_of_decl_compilation (decl
, 1, 0);
1431 /* Get the VAR_DECL of the __cpp_type_info_ptr for DECL. If this does not yet
1432 exist, create it. The __cpp_type_info_ptr decl is then initialized with a
1433 pointer to the C++ type_info for the given class. */
1436 get_cpp_typeinfo_decl (ClassDeclaration
*decl
)
1438 gcc_assert (decl
->isCPPclass ());
1440 if (decl
->cpp_type_info_ptr_sym
)
1441 return decl
->cpp_type_info_ptr_sym
;
1443 if (!tinfo_types
[TK_CPPTI_TYPE
])
1444 make_internal_typeinfo (TK_CPPTI_TYPE
,
1445 Identifier::idPool ("__cpp_type_info_ptr"),
1446 ptr_type_node
, NULL
);
1448 tree ident
= mangle_internal_decl (decl
, "_cpp_type_info_ptr", "");
1449 tree type
= tinfo_types
[TK_CPPTI_TYPE
];
1451 decl
->cpp_type_info_ptr_sym
= declare_extern_var (ident
, type
);
1452 DECL_LANG_SPECIFIC (decl
->cpp_type_info_ptr_sym
) = build_lang_decl (NULL
);
1454 /* Class is a reference, want the record type. */
1455 DECL_CONTEXT (decl
->cpp_type_info_ptr_sym
)
1456 = TREE_TYPE (build_ctype (decl
->type
));
1457 TREE_READONLY (decl
->cpp_type_info_ptr_sym
) = 1;
1459 d_comdat_linkage (decl
->cpp_type_info_ptr_sym
);
1461 /* Layout the initializer and emit the symbol. */
1462 layout_cpp_typeinfo (decl
);
1464 return decl
->cpp_type_info_ptr_sym
;
1467 /* Get the exact TypeInfo for TYPE, if it doesn't exist, create it. */
1470 create_typeinfo (Type
*type
, Module
*mod
)
1472 if (!Type::dtypeinfo
)
1473 create_frontend_tinfo_types ();
1475 /* Do this since not all Type's are merged. */
1476 Type
*t
= type
->merge2 ();
1481 tinfo_kind tk
= get_typeinfo_kind (t
);
1484 case TK_SHARED_TYPE
:
1486 case TK_IMMUTABLE_TYPE
:
1488 case TK_POINTER_TYPE
:
1490 case TK_VECTOR_TYPE
:
1491 case TK_INTERFACE_TYPE
:
1492 /* Kinds of TypeInfo that add one extra pointer field. */
1493 if (tk
== TK_SHARED_TYPE
)
1495 /* Does both 'shared' and 'shared const'. */
1496 t
->vtinfo
= TypeInfoSharedDeclaration::create (t
);
1497 ident
= Identifier::idPool ("TypeInfo_Shared");
1499 else if (tk
== TK_CONST_TYPE
)
1501 t
->vtinfo
= TypeInfoConstDeclaration::create (t
);
1502 ident
= Identifier::idPool ("TypeInfo_Const");
1504 else if (tk
== TK_IMMUTABLE_TYPE
)
1506 t
->vtinfo
= TypeInfoInvariantDeclaration::create (t
);
1507 ident
= Identifier::idPool ("TypeInfo_Invariant");
1509 else if (tk
== TK_INOUT_TYPE
)
1511 t
->vtinfo
= TypeInfoWildDeclaration::create (t
);
1512 ident
= Identifier::idPool ("TypeInfo_Wild");
1514 else if (tk
== TK_POINTER_TYPE
)
1516 t
->vtinfo
= TypeInfoPointerDeclaration::create (t
);
1517 ident
= Identifier::idPool ("TypeInfo_Pointer");
1519 else if (tk
== TK_ARRAY_TYPE
)
1521 t
->vtinfo
= TypeInfoArrayDeclaration::create (t
);
1522 ident
= Identifier::idPool ("TypeInfo_Array");
1524 else if (tk
== TK_VECTOR_TYPE
)
1526 t
->vtinfo
= TypeInfoVectorDeclaration::create (t
);
1527 ident
= Identifier::idPool ("TypeInfo_Vector");
1529 else if (tk
== TK_INTERFACE_TYPE
)
1531 t
->vtinfo
= TypeInfoInterfaceDeclaration::create (t
);
1532 ident
= Identifier::idPool ("TypeInfo_Interface");
1537 if (!tinfo_types
[tk
])
1538 make_internal_typeinfo (tk
, ident
, ptr_type_node
, NULL
);
1541 case TK_STATICARRAY_TYPE
:
1542 if (!tinfo_types
[tk
])
1544 ident
= Identifier::idPool ("TypeInfo_StaticArray");
1545 make_internal_typeinfo (tk
, ident
, ptr_type_node
, size_type_node
,
1548 t
->vtinfo
= TypeInfoStaticArrayDeclaration::create (t
);
1551 case TK_ASSOCIATIVEARRAY_TYPE
:
1552 if (!tinfo_types
[tk
])
1554 ident
= Identifier::idPool ("TypeInfo_AssociativeArray");
1555 make_internal_typeinfo (tk
, ident
, ptr_type_node
, ptr_type_node
,
1558 t
->vtinfo
= TypeInfoAssociativeArrayDeclaration::create (t
);
1561 case TK_STRUCT_TYPE
:
1562 if (!tinfo_types
[tk
])
1564 /* Some ABIs add extra TypeInfo fields on the end. */
1565 tree argtype
= global
.params
.is64bit
? ptr_type_node
: NULL_TREE
;
1567 ident
= Identifier::idPool ("TypeInfo_Struct");
1568 make_internal_typeinfo (tk
, ident
,
1569 array_type_node
, array_type_node
,
1570 ptr_type_node
, ptr_type_node
,
1571 ptr_type_node
, ptr_type_node
,
1572 d_uint_type
, ptr_type_node
,
1573 ptr_type_node
, d_uint_type
,
1574 ptr_type_node
, argtype
, argtype
, NULL
);
1576 t
->vtinfo
= TypeInfoStructDeclaration::create (t
);
1579 case TK_ENUMERAL_TYPE
:
1580 if (!tinfo_types
[tk
])
1582 ident
= Identifier::idPool ("TypeInfo_Enum");
1583 make_internal_typeinfo (tk
, ident
,
1584 ptr_type_node
, array_type_node
,
1585 array_type_node
, NULL
);
1587 t
->vtinfo
= TypeInfoEnumDeclaration::create (t
);
1590 case TK_FUNCTION_TYPE
:
1591 case TK_DELEGATE_TYPE
:
1592 /* Functions and delegates share a common TypeInfo layout. */
1593 if (tk
== TK_FUNCTION_TYPE
)
1595 t
->vtinfo
= TypeInfoFunctionDeclaration::create (t
);
1596 ident
= Identifier::idPool ("TypeInfo_Function");
1598 else if (tk
== TK_DELEGATE_TYPE
)
1600 t
->vtinfo
= TypeInfoDelegateDeclaration::create (t
);
1601 ident
= Identifier::idPool ("TypeInfo_Delegate");
1606 if (!tinfo_types
[tk
])
1607 make_internal_typeinfo (tk
, ident
, ptr_type_node
,
1608 array_type_node
, NULL
);
1611 case TK_TYPELIST_TYPE
:
1612 if (!tinfo_types
[tk
])
1614 ident
= Identifier::idPool ("TypeInfo_Tuple");
1615 make_internal_typeinfo (tk
, ident
, array_type_node
, NULL
);
1617 t
->vtinfo
= TypeInfoTupleDeclaration::create (t
);
1620 case TK_CLASSINFO_TYPE
:
1621 t
->vtinfo
= TypeInfoClassDeclaration::create (t
);
1625 t
->vtinfo
= TypeInfoDeclaration::create (t
);
1627 gcc_assert (t
->vtinfo
);
1629 /* If this has a custom implementation in rt/typeinfo, then
1630 do not generate a COMDAT for it. */
1631 if (!builtin_typeinfo_p (t
))
1633 /* Find module that will go all the way to an object file. */
1635 mod
->members
->push (t
->vtinfo
);
1637 build_decl_tree (t
->vtinfo
);
1640 /* Types aren't merged, but we can share the vtinfo's. */
1642 type
->vtinfo
= t
->vtinfo
;
1644 gcc_assert (type
->vtinfo
!= NULL
);
1647 /* Implements a visitor interface to check whether a type is speculative.
1648 TypeInfo_Struct would reference the members of the struct it is representing
1649 (e.g: opEquals via xopEquals field), so if it's instantiated in speculative
1650 context, TypeInfo creation should also be stopped to avoid possible
1651 `unresolved symbol' linker errors. */
1653 class SpeculativeTypeVisitor
: public Visitor
1655 using Visitor::visit
;
1660 SpeculativeTypeVisitor (void)
1662 this->result_
= false;
1667 return this->result_
;
1670 void visit (Type
*t
)
1672 Type
*tb
= t
->toBasetype ();
1677 void visit (TypeNext
*t
)
1680 t
->next
->accept (this);
1683 void visit (TypeBasic
*)
1687 void visit (TypeVector
*t
)
1689 t
->basetype
->accept (this);
1692 void visit (TypeAArray
*t
)
1694 t
->index
->accept (this);
1695 visit ((TypeNext
*)t
);
1698 void visit (TypeFunction
*t
)
1700 visit ((TypeNext
*)t
);
1703 void visit (TypeStruct
*t
)
1705 StructDeclaration
*sd
= t
->sym
;
1706 if (TemplateInstance
*ti
= sd
->isInstantiated ())
1708 if (!ti
->needsCodegen ())
1710 if (ti
->minst
|| sd
->requestTypeInfo
)
1713 this->result_
|= true;
1718 void visit (TypeClass
*t
)
1720 ClassDeclaration
*cd
= t
->sym
;
1721 if (TemplateInstance
*ti
= cd
->isInstantiated ())
1723 if (!ti
->needsCodegen () && !ti
->minst
)
1725 this->result_
|= true;
1730 void visit (TypeTuple
*t
)
1735 for (size_t i
= 0; i
< t
->arguments
->length
; i
++)
1737 Type
*tprm
= (*t
->arguments
)[i
]->type
;
1739 tprm
->accept (this);
1746 /* Return true if type was instantiated in a speculative context. */
1749 speculative_type_p (Type
*t
)
1751 SpeculativeTypeVisitor v
= SpeculativeTypeVisitor ();
1756 #include "gt-d-typeinfo.h"