Debian Codegraph @

gcc/d/dmd/typinf.d

raw
1 /**
2 * Generate `TypeInfo` objects, which are needed for run-time introspection of types.
3 *
4 * Copyright: Copyright (C) 1999-2025 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/compiler/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/compiler/src/dmd/typinf.d
10 */
11
12 module dmd.typinf;
13
14 import dmd.astenums;
15 import dmd.declaration;
16 import dmd.dmodule;
17 import dmd.dscope;
18 import dmd.dclass;
19 import dmd.dstruct;
20 import dmd.errors;
21 import dmd.expression;
22 import dmd.globals;
23 import dmd.location;
24 import dmd.mtype;
25 import dmd.typesem;
26 import core.stdc.stdio;
27
28 /****************************************************
29 * Generates the `TypeInfo` object associated with `torig` if it
30 * hasn't already been generated
31 * Params:
32 * e = if not null, then expression for pretty-printing errors
33 * loc = the location for reporting line numbers in errors
34 * torig = the type to generate the `TypeInfo` object for
35 * sc = the scope
36 * Returns:
37 * true if `TypeInfo` was generated and needs compiling to object file
38 */
39 bool genTypeInfo(Expression e, Loc loc, Type torig, Scope* sc)
40 {
41 // printf("genTypeInfo() %s\n", torig.toChars());
42
43 // Even when compiling without `useTypeInfo` (e.g. -betterC) we should
44 // still be able to evaluate `TypeInfo` at compile-time, just not at runtime.
45 // https://issues.dlang.org/show_bug.cgi?id=18472
46 if (!sc || !sc.ctfe)
47 {
48 if (!global.params.useTypeInfo)
49 {
50 global.gag = 0;
51 if (e)
52 .error(loc, "expression `%s` uses the GC and cannot be used with switch `-betterC`", e.toChars());
53 else
54 .error(loc, "`TypeInfo` cannot be used with -betterC");
55
56 if (sc && sc.tinst)
57 sc.tinst.printInstantiationTrace(Classification.error, uint.max);
58
59 fatal();
60 }
61 }
62
63 if (!Type.dtypeinfo)
64 {
65 .error(loc, "`object.TypeInfo` could not be found, but is implicitly used");
66 fatal();
67 }
68
69 import dmd.typesem : merge2;
70 Type t = torig.merge2(); // do this since not all Type's are merge'd
71 if (t.ty == Taarray)
72 t = makeNakedAssociativeArray(cast(TypeAArray)t);
73
74 bool needsCodegen = false;
75 if (!t.vtinfo)
76 {
77 if (t.isShared()) // does both 'shared' and 'shared const'
78 t.vtinfo = TypeInfoSharedDeclaration.create(t);
79 else if (t.isConst())
80 t.vtinfo = TypeInfoConstDeclaration.create(t);
81 else if (t.isImmutable())
82 t.vtinfo = TypeInfoInvariantDeclaration.create(t);
83 else if (t.isWild())
84 t.vtinfo = TypeInfoWildDeclaration.create(t);
85 else
86 t.vtinfo = getTypeInfoDeclaration(t, sc);
87 assert(t.vtinfo);
88
89 // ClassInfos are generated as part of ClassDeclaration codegen
90 const isUnqualifiedClassInfo = (t.ty == Tclass && !t.mod);
91
92 if (!isUnqualifiedClassInfo && !builtinTypeInfo(t))
93 needsCodegen = true;
94 }
95 if (!torig.vtinfo)
96 torig.vtinfo = t.vtinfo; // Types aren't merged, but we can share the vtinfo's
97 assert(torig.vtinfo);
98 return needsCodegen;
99 }
100
101 /****************************************************
102 * Gets the type of the `TypeInfo` object associated with `t`
103 * Params:
104 * loc = the location for reporting line nunbers in errors
105 * t = the type to get the type of the `TypeInfo` object for
106 * sc = the scope
107 * Returns:
108 * The type of the `TypeInfo` object associated with `t`
109 */
110 extern (C++) Type getTypeInfoType(Loc loc, Type t, Scope* sc);
111
112 private TypeInfoDeclaration getTypeInfoDeclaration(Type t, Scope* sc)
113 {
114 //printf("Type::getTypeInfoDeclaration() %s\n", t.toChars());
115 switch (t.ty)
116 {
117 case Tpointer:
118 return TypeInfoPointerDeclaration.create(t);
119 case Tarray:
120 return TypeInfoArrayDeclaration.create(t);
121 case Tsarray:
122 return TypeInfoStaticArrayDeclaration.create(t);
123 case Taarray:
124 return getTypeInfoAssocArrayDeclaration(cast(TypeAArray)t, sc);
125 case Tstruct:
126 return TypeInfoStructDeclaration.create(t);
127 case Tvector:
128 return TypeInfoVectorDeclaration.create(t);
129 case Tenum:
130 return TypeInfoEnumDeclaration.create(t);
131 case Tfunction:
132 return TypeInfoFunctionDeclaration.create(t);
133 case Tdelegate:
134 return TypeInfoDelegateDeclaration.create(t);
135 case Ttuple:
136 return TypeInfoTupleDeclaration.create(t);
137 case Tclass:
138 if ((cast(TypeClass)t).sym.isInterfaceDeclaration())
139 return TypeInfoInterfaceDeclaration.create(t);
140 else
141 return TypeInfoClassDeclaration.create(t);
142
143 default:
144 return TypeInfoDeclaration.create(t);
145 }
146 }
147
148 /******************************************
149 * Instantiate TypeInfoAssociativeArrayDeclaration and fill
150 * the entry with TypeInfo_AssociativeArray.Entry!(t.index, t.next)
151 *
152 * Params:
153 * t = TypeAArray to generate TypeInfo_AssociativeArray for
154 * sc = context
155 * Returns:
156 * a TypeInfoAssociativeArrayDeclaration with field entry initialized
157 */
158 TypeInfoDeclaration getTypeInfoAssocArrayDeclaration(TypeAArray t, Scope* sc)
159 {
160 import dmd.arraytypes;
161 import dmd.expressionsem;
162 import dmd.id;
163
164 assert(sc); // must not be called in the code generation phase
165
166 auto ti = TypeInfoAssociativeArrayDeclaration.create(t);
167 t.vtinfo = ti; // assign it early to avoid recursion in expressionSemantic
168 Loc loc = t.loc;
169 auto tiargs = new Objects();
170 tiargs.push(t.index); // always called with naked types
171 tiargs.push(t.next);
172
173 Expression id = new IdentifierExp(loc, Id.empty);
174 id = new DotIdExp(loc, id, Id.object);
175 id = new DotIdExp(loc, id, Id.TypeInfo_AssociativeArray);
176 auto tempinst = new DotTemplateInstanceExp(loc, id, Id.Entry, tiargs);
177 auto e = expressionSemantic(tempinst, sc);
178 assert(e.type);
179 ti.entry = e.type;
180 if (auto ts = ti.entry.isTypeStruct())
181 {
182 ts.sym.requestTypeInfo = true;
183 if (auto tmpl = ts.sym.isInstantiated())
184 tmpl.minst = sc._module.importedFrom; // ensure it get's emitted
185 }
186 getTypeInfoType(loc, ti.entry, sc);
187 assert(ti.entry.vtinfo);
188
189 return ti;
190 }
191
192 /******************************************
193 * Find or create a TypeAArray with index and next without
194 * any head modifiers, tail `inout` is replaced with `const`
195 *
196 * Params:
197 * t = TypeAArray to convert
198 * Returns:
199 * t = found type
200 */
201 Type makeNakedAssociativeArray(TypeAArray t)
202 {
203 Type tindex = t.index.toBasetype().nakedOf().substWildTo(MODFlags.const_);
204 Type tnext = t.next.toBasetype().nakedOf().substWildTo(MODFlags.const_);
205 if (tindex == t.index && tnext == t.next)
206 return t;
207
208 t = new TypeAArray(tnext, tindex);
209 return t.merge();
210 }
211
212 /**************************************************
213 * Returns:
214 * true if any part of type t is speculative.
215 * if t is null, returns false.
216 */
217 bool isSpeculativeType(Type t)
218 {
219 static bool visitVector(TypeVector t)
220 {
221 return isSpeculativeType(t.basetype);
222 }
223
224 static bool visitAArray(TypeAArray t)
225 {
226 return isSpeculativeType(t.index) ||
227 isSpeculativeType(t.next);
228 }
229
230 static bool visitStruct(TypeStruct t)
231 {
232 StructDeclaration sd = t.sym;
233 if (auto ti = sd.isInstantiated())
234 {
235 if (!ti.needsCodegen())
236 {
237 if (ti.minst || sd.requestTypeInfo)
238 return false;
239
240 /* https://issues.dlang.org/show_bug.cgi?id=14425
241 * TypeInfo_Struct would refer the members of
242 * struct (e.g. opEquals via xopEquals field), so if it's instantiated
243 * in speculative context, TypeInfo creation should also be
244 * stopped to avoid 'unresolved symbol' linker errors.
245 */
246 /* When -debug/-unittest is specified, all of non-root instances are
247 * automatically changed to speculative, and here is always reached
248 * from those instantiated non-root structs.
249 * Therefore, if the TypeInfo is not auctually requested,
250 * we have to elide its codegen.
251 */
252 return true;
253 }
254 }
255 else
256 {
257 //assert(!sd.inNonRoot() || sd.requestTypeInfo); // valid?
258 }
259 return false;
260 }
261
262 static bool visitClass(TypeClass t)
263 {
264 ClassDeclaration sd = t.sym;
265 if (auto ti = sd.isInstantiated())
266 {
267 if (!ti.needsCodegen() && !ti.minst)
268 {
269 return true;
270 }
271 }
272 return false;
273 }
274
275
276 static bool visitTuple(TypeTuple t)
277 {
278 if (t.arguments)
279 {
280 foreach (arg; *t.arguments)
281 {
282 if (isSpeculativeType(arg.type))
283 return true;
284 }
285 }
286 return false;
287 }
288
289 if (!t)
290 return false;
291 Type tb = t.toBasetype();
292 switch (tb.ty)
293 {
294 case Tvector: return visitVector(tb.isTypeVector());
295 case Taarray: return visitAArray(tb.isTypeAArray());
296 case Tstruct: return visitStruct(tb.isTypeStruct());
297 case Tclass: return visitClass(tb.isTypeClass());
298 case Ttuple: return visitTuple(tb.isTypeTuple());
299 case Tenum: return false;
300 default:
301 return isSpeculativeType(tb.nextOf());
302
303 /* For TypeFunction, TypeInfo_Function doesn't store parameter types,
304 * so only the .next (the return type) is checked here.
305 */
306 }
307 }
308
309 /* ========================================================================= */
310
311 /* Indicates whether druntime already contains an appropriate TypeInfo instance
312 * for the specified type (in module rt.util.typeinfo).
313 */
314 bool builtinTypeInfo(Type t)
315 {
316 if (!t.mod) // unqualified types only
317 {
318 // unqualified basic types + typeof(null)
319 if (t.isTypeBasic() || t.ty == Tnull)
320 return true;
321 // some unqualified arrays
322 if (t.ty == Tarray)
323 {
324 Type next = t.nextOf();
325 return (next.isTypeBasic() && !next.mod) // of unqualified basic types
326 || (next.ty == Tchar && next.mod == MODFlags.immutable_) // string
327 || (next.ty == Tchar && next.mod == MODFlags.const_); // const(char)[]
328 }
329 }
330 return false;
331 }