2 * Generate `TypeInfo` objects, which are needed for run-time introspection of types.
4 * Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/typinf.d, _typinf.d)
8 * Documentation: https://dlang.org/phobos/dmd_typinf.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/typinf.d
15 import dmd
.declaration
;
21 import dmd
.expression
;
25 import core
.stdc
.stdio
;
27 /****************************************************
28 * Generates the `TypeInfo` object associated with `torig` if it
29 * hasn't already been generated
31 * e = if not null, then expression for pretty-printing errors
32 * loc = the location for reporting line numbers in errors
33 * torig = the type to generate the `TypeInfo` object for
36 * true if `TypeInfo` was generated and needs compiling to object file
38 extern (C
++) bool genTypeInfo(Expression e
, const ref Loc loc
, Type torig
, Scope
* sc
)
40 // printf("genTypeInfo() %s\n", torig.toChars());
42 // Even when compiling without `useTypeInfo` (e.g. -betterC) we should
43 // still be able to evaluate `TypeInfo` at compile-time, just not at runtime.
44 // https://issues.dlang.org/show_bug.cgi?id=18472
45 if (!sc ||
!(sc
.flags
& SCOPE
.ctfe
))
47 if (!global
.params
.useTypeInfo
)
51 .error(loc
, "expression `%s` uses the GC and cannot be used with switch `-betterC`", e
.toChars());
53 .error(loc
, "`TypeInfo` cannot be used with -betterC");
56 sc
.tinst
.printInstantiationTrace(Classification
.error
, uint.max
);
64 .error(loc
, "`object.TypeInfo` could not be found, but is implicitly used");
68 Type t
= torig
.merge2(); // do this since not all Type's are merge'd
69 bool needsCodegen
= false;
72 if (t
.isShared()) // does both 'shared' and 'shared const'
73 t
.vtinfo
= TypeInfoSharedDeclaration
.create(t
);
75 t
.vtinfo
= TypeInfoConstDeclaration
.create(t
);
76 else if (t
.isImmutable())
77 t
.vtinfo
= TypeInfoInvariantDeclaration
.create(t
);
79 t
.vtinfo
= TypeInfoWildDeclaration
.create(t
);
81 t
.vtinfo
= getTypeInfoDeclaration(t
);
84 // ClassInfos are generated as part of ClassDeclaration codegen
85 const isUnqualifiedClassInfo
= (t
.ty
== Tclass
&& !t
.mod
);
87 if (!isUnqualifiedClassInfo
&& !builtinTypeInfo(t
))
91 torig
.vtinfo
= t
.vtinfo
; // Types aren't merged, but we can share the vtinfo's
96 /****************************************************
97 * Gets the type of the `TypeInfo` object associated with `t`
99 * loc = the location for reporting line nunbers in errors
100 * t = the type to get the type of the `TypeInfo` object for
102 * genObjCode = if true, object code will be generated for the obtained TypeInfo
104 * The type of the `TypeInfo` object associated with `t`
106 extern (C
++) Type
getTypeInfoType(const ref Loc loc
, Type t
, Scope
* sc
, bool genObjCode
= true);
108 private TypeInfoDeclaration
getTypeInfoDeclaration(Type t
)
110 //printf("Type::getTypeInfoDeclaration() %s\n", t.toChars());
114 return TypeInfoPointerDeclaration
.create(t
);
116 return TypeInfoArrayDeclaration
.create(t
);
118 return TypeInfoStaticArrayDeclaration
.create(t
);
120 return TypeInfoAssociativeArrayDeclaration
.create(t
);
122 return TypeInfoStructDeclaration
.create(t
);
124 return TypeInfoVectorDeclaration
.create(t
);
126 return TypeInfoEnumDeclaration
.create(t
);
128 return TypeInfoFunctionDeclaration
.create(t
);
130 return TypeInfoDelegateDeclaration
.create(t
);
132 return TypeInfoTupleDeclaration
.create(t
);
134 if ((cast(TypeClass
)t
).sym
.isInterfaceDeclaration())
135 return TypeInfoInterfaceDeclaration
.create(t
);
137 return TypeInfoClassDeclaration
.create(t
);
140 return TypeInfoDeclaration
.create(t
);
144 /**************************************************
146 * true if any part of type t is speculative.
147 * if t is null, returns false.
149 extern (C
++) bool isSpeculativeType(Type t
)
151 static bool visitVector(TypeVector t
)
153 return isSpeculativeType(t
.basetype
);
156 static bool visitAArray(TypeAArray t
)
158 return isSpeculativeType(t
.index
) ||
159 isSpeculativeType(t
.next
);
162 static bool visitStruct(TypeStruct t
)
164 StructDeclaration sd
= t
.sym
;
165 if (auto ti
= sd
.isInstantiated())
167 if (!ti
.needsCodegen())
169 if (ti
.minst || sd
.requestTypeInfo
)
172 /* https://issues.dlang.org/show_bug.cgi?id=14425
173 * TypeInfo_Struct would refer the members of
174 * struct (e.g. opEquals via xopEquals field), so if it's instantiated
175 * in speculative context, TypeInfo creation should also be
176 * stopped to avoid 'unresolved symbol' linker errors.
178 /* When -debug/-unittest is specified, all of non-root instances are
179 * automatically changed to speculative, and here is always reached
180 * from those instantiated non-root structs.
181 * Therefore, if the TypeInfo is not auctually requested,
182 * we have to elide its codegen.
189 //assert(!sd.inNonRoot() || sd.requestTypeInfo); // valid?
194 static bool visitClass(TypeClass t
)
196 ClassDeclaration sd
= t
.sym
;
197 if (auto ti
= sd
.isInstantiated())
199 if (!ti
.needsCodegen() && !ti
.minst
)
208 static bool visitTuple(TypeTuple t
)
212 foreach (arg
; *t
.arguments
)
214 if (isSpeculativeType(arg
.type
))
223 Type tb
= t
.toBasetype();
226 case Tvector
: return visitVector(tb
.isTypeVector());
227 case Taarray
: return visitAArray(tb
.isTypeAArray());
228 case Tstruct
: return visitStruct(tb
.isTypeStruct());
229 case Tclass
: return visitClass(tb
.isTypeClass());
230 case Ttuple
: return visitTuple(tb
.isTypeTuple());
231 case Tenum
: return false;
233 return isSpeculativeType(tb
.nextOf());
235 /* For TypeFunction, TypeInfo_Function doesn't store parameter types,
236 * so only the .next (the return type) is checked here.
241 /* ========================================================================= */
243 /* Indicates whether druntime already contains an appropriate TypeInfo instance
244 * for the specified type (in module rt.util.typeinfo).
246 extern (C
++) bool builtinTypeInfo(Type t
)
248 if (!t
.mod
) // unqualified types only
250 // unqualified basic types + typeof(null)
251 if (t
.isTypeBasic() || t
.ty
== Tnull
)
253 // some unqualified arrays
256 Type next
= t
.nextOf();
257 return (next
.isTypeBasic() && !next
.mod
) // of unqualified basic types
258 ||
(next
.ty
== Tchar
&& next
.mod
== MODFlags
.immutable_
) // string
259 ||
(next
.ty
== Tchar
&& next
.mod
== MODFlags
.const_
); // const(char)[]