2 * Generate `TypeInfo` objects, which are needed for run-time introspection of types.
4 * Copyright: Copyright (C) 1999-2023 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/typeinf.d, _typeinf.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;
27 import core.stdc.stdio;
29 /****************************************************
30 * Generates the `TypeInfo` object associated with `torig` if it
31 * hasn't already been generated
33 * e = if not null, then expression for pretty-printing errors
34 * loc = the location for reporting line numbers in errors
35 * torig = the type to generate the `TypeInfo` object for
38 extern (C++) void 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)
50 .error(loc, "expression `%s` uses the GC and cannot be used with switch `-betterC`", e.toChars());
52 .error(loc, "`TypeInfo` cannot be used with -betterC");
59 .error(loc, "`object.TypeInfo` could not be found, but is implicitly used");
63 Type t = torig.merge2(); // do this since not all Type's are merge'd
66 if (t.isShared()) // does both 'shared' and 'shared const'
67 t.vtinfo = TypeInfoSharedDeclaration.create(t);
69 t.vtinfo = TypeInfoConstDeclaration.create(t);
70 else if (t.isImmutable())
71 t.vtinfo = TypeInfoInvariantDeclaration.create(t);
73 t.vtinfo = TypeInfoWildDeclaration.create(t);
75 t.vtinfo = getTypeInfoDeclaration(t);
78 // ClassInfos are generated as part of ClassDeclaration codegen
79 const isUnqualifiedClassInfo = (t.ty == Tclass && !t.mod);
81 // generate a COMDAT for other TypeInfos not available as builtins in
83 if (!isUnqualifiedClassInfo && !builtinTypeInfo(t))
85 if (sc) // if in semantic() pass
87 // Find module that will go all the way to an object file
88 Module m = sc._module.importedFrom;
89 m.members.push(t.vtinfo);
91 else // if in obj generation pass
93 toObjFile(t.vtinfo, global.params.multiobj);
98 torig.vtinfo = t.vtinfo; // Types aren't merged, but we can share the vtinfo's
102 /****************************************************
103 * Gets the type of the `TypeInfo` object associated with `t`
105 * loc = the location for reporting line nunbers in errors
106 * t = the type to get the type of the `TypeInfo` object for
108 * genObjCode = if true, object code will be generated for the obtained TypeInfo
110 * The type of the `TypeInfo` object associated with `t`
112 extern (C++) Type getTypeInfoType(const ref Loc loc, Type t, Scope* sc, bool genObjCode = true);
114 private TypeInfoDeclaration getTypeInfoDeclaration(Type t)
116 //printf("Type::getTypeInfoDeclaration() %s\n", t.toChars());
120 return TypeInfoPointerDeclaration.create(t);
122 return TypeInfoArrayDeclaration.create(t);
124 return TypeInfoStaticArrayDeclaration.create(t);
126 return TypeInfoAssociativeArrayDeclaration.create(t);
128 return TypeInfoStructDeclaration.create(t);
130 return TypeInfoVectorDeclaration.create(t);
132 return TypeInfoEnumDeclaration.create(t);
134 return TypeInfoFunctionDeclaration.create(t);
136 return TypeInfoDelegateDeclaration.create(t);
138 return TypeInfoTupleDeclaration.create(t);
140 if ((cast(TypeClass)t).sym.isInterfaceDeclaration())
141 return TypeInfoInterfaceDeclaration.create(t);
143 return TypeInfoClassDeclaration.create(t);
146 return TypeInfoDeclaration.create(t);
150 /**************************************************
152 * true if any part of type t is speculative.
153 * if t is null, returns false.
155 bool isSpeculativeType(Type t)
157 static bool visitVector(TypeVector t)
159 return isSpeculativeType(t.basetype);
162 static bool visitAArray(TypeAArray t)
164 return isSpeculativeType(t.index) ||
165 isSpeculativeType(t.next);
168 static bool visitStruct(TypeStruct t)
170 StructDeclaration sd = t.sym;
171 if (auto ti = sd.isInstantiated())
173 if (!ti.needsCodegen())
175 if (ti.minst || sd.requestTypeInfo)
178 /* https://issues.dlang.org/show_bug.cgi?id=14425
179 * TypeInfo_Struct would refer the members of
180 * struct (e.g. opEquals via xopEquals field), so if it's instantiated
181 * in speculative context, TypeInfo creation should also be
182 * stopped to avoid 'unresolved symbol' linker errors.
184 /* When -debug/-unittest is specified, all of non-root instances are
185 * automatically changed to speculative, and here is always reached
186 * from those instantiated non-root structs.
187 * Therefore, if the TypeInfo is not auctually requested,
188 * we have to elide its codegen.
195 //assert(!sd.inNonRoot() || sd.requestTypeInfo); // valid?
200 static bool visitClass(TypeClass t)
202 ClassDeclaration sd = t.sym;
203 if (auto ti = sd.isInstantiated())
205 if (!ti.needsCodegen() && !ti.minst)
214 static bool visitTuple(TypeTuple t)
218 foreach (arg; *t.arguments)
220 if (isSpeculativeType(arg.type))
229 Type tb = t.toBasetype();
232 case Tvector: return visitVector(tb.isTypeVector());
233 case Taarray: return visitAArray(tb.isTypeAArray());
234 case Tstruct: return visitStruct(tb.isTypeStruct());
235 case Tclass: return visitClass(tb.isTypeClass());
236 case Ttuple: return visitTuple(tb.isTypeTuple());
237 case Tenum: return false;
239 return isSpeculativeType(tb.nextOf());
241 /* For TypeFunction, TypeInfo_Function doesn't store parameter types,
242 * so only the .next (the return type) is checked here.
247 /* ========================================================================= */
249 /* Indicates whether druntime already contains an appropriate TypeInfo instance
250 * for the specified type (in module rt.util.typeinfo).
252 extern (C++) bool builtinTypeInfo(Type t)
254 if (!t.mod) // unqualified types only
256 // unqualified basic types + typeof(null)
257 if (t.isTypeBasic() || t.ty == Tnull)
259 // some unqualified arrays
262 Type next = t.nextOf();
263 return (next.isTypeBasic() && !next.mod) // of unqualified basic types
264 || (next.ty == Tchar && next.mod == MODFlags.immutable_) // string
265 || (next.ty == Tchar && next.mod == MODFlags.const_); // const(char)[]