d: Merge dmd, druntime d8e3976a58, phobos 7a6e95688
[official-gcc.git] / gcc / d / dmd / typinf.d
blob9e062bd631d7e376edeab70b70ec67bf40688b8a
1 /**
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
12 module dmd.typinf;
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 core.stdc.stdio;
27 /****************************************************
28 * Generates the `TypeInfo` object associated with `torig` if it
29 * hasn't already been generated
30 * Params:
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
34 * sc = the scope
35 * Returns:
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)
49 global.gag = 0;
50 if (e)
51 .error(loc, "expression `%s` uses the GC and cannot be used with switch `-betterC`", e.toChars());
52 else
53 .error(loc, "`TypeInfo` cannot be used with -betterC");
55 if (sc && sc.tinst)
56 sc.tinst.printInstantiationTrace(Classification.error, uint.max);
58 fatal();
62 if (!Type.dtypeinfo)
64 .error(loc, "`object.TypeInfo` could not be found, but is implicitly used");
65 fatal();
68 Type t = torig.merge2(); // do this since not all Type's are merge'd
69 bool needsCodegen = false;
70 if (!t.vtinfo)
72 if (t.isShared()) // does both 'shared' and 'shared const'
73 t.vtinfo = TypeInfoSharedDeclaration.create(t);
74 else if (t.isConst())
75 t.vtinfo = TypeInfoConstDeclaration.create(t);
76 else if (t.isImmutable())
77 t.vtinfo = TypeInfoInvariantDeclaration.create(t);
78 else if (t.isWild())
79 t.vtinfo = TypeInfoWildDeclaration.create(t);
80 else
81 t.vtinfo = getTypeInfoDeclaration(t);
82 assert(t.vtinfo);
84 // ClassInfos are generated as part of ClassDeclaration codegen
85 const isUnqualifiedClassInfo = (t.ty == Tclass && !t.mod);
87 if (!isUnqualifiedClassInfo && !builtinTypeInfo(t))
88 needsCodegen = true;
90 if (!torig.vtinfo)
91 torig.vtinfo = t.vtinfo; // Types aren't merged, but we can share the vtinfo's
92 assert(torig.vtinfo);
93 return needsCodegen;
96 /****************************************************
97 * Gets the type of the `TypeInfo` object associated with `t`
98 * Params:
99 * loc = the location for reporting line nunbers in errors
100 * t = the type to get the type of the `TypeInfo` object for
101 * sc = the scope
102 * genObjCode = if true, object code will be generated for the obtained TypeInfo
103 * Returns:
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());
111 switch (t.ty)
113 case Tpointer:
114 return TypeInfoPointerDeclaration.create(t);
115 case Tarray:
116 return TypeInfoArrayDeclaration.create(t);
117 case Tsarray:
118 return TypeInfoStaticArrayDeclaration.create(t);
119 case Taarray:
120 return TypeInfoAssociativeArrayDeclaration.create(t);
121 case Tstruct:
122 return TypeInfoStructDeclaration.create(t);
123 case Tvector:
124 return TypeInfoVectorDeclaration.create(t);
125 case Tenum:
126 return TypeInfoEnumDeclaration.create(t);
127 case Tfunction:
128 return TypeInfoFunctionDeclaration.create(t);
129 case Tdelegate:
130 return TypeInfoDelegateDeclaration.create(t);
131 case Ttuple:
132 return TypeInfoTupleDeclaration.create(t);
133 case Tclass:
134 if ((cast(TypeClass)t).sym.isInterfaceDeclaration())
135 return TypeInfoInterfaceDeclaration.create(t);
136 else
137 return TypeInfoClassDeclaration.create(t);
139 default:
140 return TypeInfoDeclaration.create(t);
144 /**************************************************
145 * Returns:
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)
170 return false;
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.
184 return true;
187 else
189 //assert(!sd.inNonRoot() || sd.requestTypeInfo); // valid?
191 return false;
194 static bool visitClass(TypeClass t)
196 ClassDeclaration sd = t.sym;
197 if (auto ti = sd.isInstantiated())
199 if (!ti.needsCodegen() && !ti.minst)
201 return true;
204 return false;
208 static bool visitTuple(TypeTuple t)
210 if (t.arguments)
212 foreach (arg; *t.arguments)
214 if (isSpeculativeType(arg.type))
215 return true;
218 return false;
221 if (!t)
222 return false;
223 Type tb = t.toBasetype();
224 switch (tb.ty)
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;
232 default:
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)
252 return true;
253 // some unqualified arrays
254 if (t.ty == Tarray)
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)[]
262 return false;