1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/**
* Checks that a function marked `@nogc` does not invoke the Garbage Collector.
*
* Specification: $(LINK2 https://dlang.org/spec/function.html#nogc-functions, No-GC Functions)
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/nogc.d, _nogc.d)
* Documentation: https://dlang.org/phobos/dmd_nogc.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/nogc.d
*/
module dmd.nogc;
import core.stdc.stdio;
import dmd.aggregate;
import dmd.astenums;
import dmd.declaration;
import dmd.dscope;
import dmd.dtemplate : isDsymbol;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.globals;
import dmd.init;
import dmd.mtype;
import dmd.postordervisitor;
import dmd.tokens;
import dmd.visitor;
/**************************************
* Look for GC-allocations
*/
extern (C++) final class NOGCVisitor : StoppableVisitor
{
alias visit = typeof(super).visit;
public:
FuncDeclaration f;
bool checkOnly; // don't print errors
bool err;
extern (D) this(FuncDeclaration f) scope @safe
{
this.f = f;
}
void doCond(Expression exp)
{
if (exp)
walkPostorder(exp, this);
}
override void visit(Expression e)
{
}
override void visit(DeclarationExp e)
{
// Note that, walkPostorder does not support DeclarationExp today.
VarDeclaration v = e.declaration.isVarDeclaration();
if (v && !(v.storage_class & STC.manifest) && !v.isDataseg() && v._init)
{
if (ExpInitializer ei = v._init.isExpInitializer())
{
doCond(ei.exp);
}
}
}
/**
* Register that expression `e` requires the GC
* Params:
* e = expression that uses GC
* format = error message when `e` is used in a `@nogc` function.
* Must contain format strings "`@nogc` %s `%s`" referring to the function.
* Returns: `true` if `err` was set, `false` if it's not in a `@nogc` and not checkonly (-betterC)
*/
private bool setGC(Expression e, const(char)* format)
{
if (checkOnly)
{
err = true;
return true;
}
if (f.setGC(e.loc, format))
{
error(e.loc, format, f.kind(), f.toPrettyChars());
err = true;
return true;
}
return false;
}
override void visit(CallExp e)
{
import dmd.id : Id;
import core.stdc.stdio : printf;
if (!e.f)
return;
// Treat lowered hook calls as their original expressions.
auto fd = stripHookTraceImpl(e.f);
if (fd.ident == Id._d_arraysetlengthT)
{
if (setGC(e, "setting `length` in `@nogc` %s `%s` may cause a GC allocation"))
return;
f.printGCUsage(e.loc, "setting `length` may cause a GC allocation");
}
}
override void visit(ArrayLiteralExp e)
{
if (e.type.ty != Tarray || !e.elements || !e.elements.length || e.onstack)
return;
if (setGC(e, "array literal in `@nogc` %s `%s` may cause a GC allocation"))
return;
f.printGCUsage(e.loc, "array literal may cause a GC allocation");
}
override void visit(AssocArrayLiteralExp e)
{
if (!e.keys.length)
return;
if (setGC(e, "associative array literal in `@nogc` %s `%s` may cause a GC allocation"))
return;
f.printGCUsage(e.loc, "associative array literal may cause a GC allocation");
}
override void visit(NewExp e)
{
if (e.member && !e.member.isNogc() && f.setGC(e.loc, null))
{
// @nogc-ness is already checked in NewExp::semantic
return;
}
if (e.onstack)
return;
if (global.params.ehnogc && e.thrownew)
return; // separate allocator is called for this, not the GC
if (setGC(e, "cannot use `new` in `@nogc` %s `%s`"))
return;
f.printGCUsage(e.loc, "`new` causes a GC allocation");
}
override void visit(DeleteExp e)
{
if (VarExp ve = e.e1.isVarExp())
{
VarDeclaration v = ve.var.isVarDeclaration();
if (v && v.onstack)
return; // delete for scope allocated class object
}
// Semantic should have already handled this case.
assert(0);
}
override void visit(IndexExp e)
{
Type t1b = e.e1.type.toBasetype();
if (e.modifiable && t1b.ty == Taarray)
{
if (setGC(e, "assigning an associative array element in `@nogc` %s `%s` may cause a GC allocation"))
return;
f.printGCUsage(e.loc, "assigning an associative array element may cause a GC allocation");
}
}
override void visit(AssignExp e)
{
if (e.e1.op == EXP.arrayLength)
{
if (setGC(e, "setting `length` in `@nogc` %s `%s` may cause a GC allocation"))
return;
f.printGCUsage(e.loc, "setting `length` may cause a GC allocation");
}
}
override void visit(CatAssignExp e)
{
if (checkOnly)
{
err = true;
return;
}
if (setGC(e, "cannot use operator `~=` in `@nogc` %s `%s`"))
return;
f.printGCUsage(e.loc, "operator `~=` may cause a GC allocation");
}
override void visit(CatExp e)
{
if (setGC(e, "cannot use operator `~` in `@nogc` %s `%s`"))
return;
f.printGCUsage(e.loc, "operator `~` may cause a GC allocation");
}
}
Expression checkGC(Scope* sc, Expression e)
{
if (sc.flags & SCOPE.ctfeBlock) // ignore GC in ctfe blocks
return e;
/* If betterC, allow GC to happen in non-CTFE code.
* Just don't generate code for it.
* Detect non-CTFE use of the GC in betterC code.
*/
const betterC = !global.params.useGC;
FuncDeclaration f = sc.func;
if (e && e.op != EXP.error && f && sc.intypeof != 1 &&
(!(sc.flags & SCOPE.ctfe) || betterC) &&
(f.type.ty == Tfunction &&
(cast(TypeFunction)f.type).isnogc || f.nogcInprocess || global.params.v.gc) &&
!(sc.flags & SCOPE.debug_))
{
scope NOGCVisitor gcv = new NOGCVisitor(f);
gcv.checkOnly = betterC;
walkPostorder(e, gcv);
if (gcv.err)
{
if (betterC)
{
/* Allow ctfe to use the gc code, but don't let it into the runtime
*/
f.skipCodegen = true;
}
else
return ErrorExp.get();
}
}
return e;
}
/**
* Removes `_d_HookTraceImpl` if found from `fd`.
* This is needed to be able to find hooks that are called though the hook's `*Trace` wrapper.
* Parameters:
* fd = The function declaration to remove `_d_HookTraceImpl` from
*/
private FuncDeclaration stripHookTraceImpl(FuncDeclaration fd)
{
import dmd.id : Id;
import dmd.dsymbol : Dsymbol;
import dmd.rootobject : RootObject, DYNCAST;
if (fd.ident != Id._d_HookTraceImpl)
return fd;
// Get the Hook from the second template parameter
auto templateInstance = fd.parent.isTemplateInstance;
RootObject hook = (*templateInstance.tiargs)[1];
Dsymbol s = hook.isDsymbol();
assert(s, "Expected _d_HookTraceImpl's second template parameter to be an alias to the hook!");
return s.isFuncDeclaration;
}
|