2 * Checks that a function marked `@nogc` does not invoke the Garbage Collector.
4 * Specification: $(LINK2 https://dlang.org/spec/function.html#nogc-functions, No-GC Functions)
6 * Copyright: Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
7 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
8 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/nogc.d, _nogc.d)
10 * Documentation: https://dlang.org/phobos/dmd_nogc.html
11 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/nogc.d
19 import dmd.declaration;
21 import dmd.expression;
29 /**************************************
30 * Look for GC-allocations
32 extern (C++) final class NOGCVisitor : StoppableVisitor
34 alias visit = typeof(super).visit;
39 extern (D) this(FuncDeclaration f)
44 void doCond(Expression exp)
47 walkPostorder(exp, this);
50 override void visit(Expression e)
54 override void visit(DeclarationExp e)
56 // Note that, walkPostorder does not support DeclarationExp today.
57 VarDeclaration v = e.declaration.isVarDeclaration();
58 if (v && !(v.storage_class & STC.manifest) && !v.isDataseg() && v._init)
60 if (ExpInitializer ei = v._init.isExpInitializer())
67 override void visit(CallExp e)
70 import core.stdc.stdio : printf;
74 auto fd = stripHookTraceImpl(e.f);
75 if (fd.ident == Id._d_arraysetlengthT)
79 e.error("setting `length` in `@nogc` %s `%s` may cause a GC allocation",
80 f.kind(), f.toPrettyChars());
84 f.printGCUsage(e.loc, "setting `length` may cause a GC allocation");
88 override void visit(ArrayLiteralExp e)
90 if (e.type.ty != Tarray || !e.elements || !e.elements.dim)
94 e.error("array literal in `@nogc` %s `%s` may cause a GC allocation",
95 f.kind(), f.toPrettyChars());
99 f.printGCUsage(e.loc, "array literal may cause a GC allocation");
102 override void visit(AssocArrayLiteralExp e)
108 e.error("associative array literal in `@nogc` %s `%s` may cause a GC allocation",
109 f.kind(), f.toPrettyChars());
113 f.printGCUsage(e.loc, "associative array literal may cause a GC allocation");
116 override void visit(NewExp e)
118 if (e.member && !e.member.isNogc() && f.setGC())
120 // @nogc-ness is already checked in NewExp::semantic
125 if (global.params.ehnogc && e.thrownew)
126 return; // separate allocator is called for this, not the GC
129 e.error("cannot use `new` in `@nogc` %s `%s`",
130 f.kind(), f.toPrettyChars());
134 f.printGCUsage(e.loc, "`new` causes a GC allocation");
137 override void visit(DeleteExp e)
139 if (e.e1.op == EXP.variable)
141 VarDeclaration v = (cast(VarExp)e.e1).var.isVarDeclaration();
143 return; // delete for scope allocated class object
146 Type tb = e.e1.type.toBasetype();
147 AggregateDeclaration ad = null;
151 ad = (cast(TypeClass)tb).sym;
155 tb = (cast(TypePointer)tb).next.toBasetype();
156 if (tb.ty == Tstruct)
157 ad = (cast(TypeStruct)tb).sym;
166 e.error("cannot use `delete` in `@nogc` %s `%s`",
167 f.kind(), f.toPrettyChars());
171 f.printGCUsage(e.loc, "`delete` requires the GC");
174 override void visit(IndexExp e)
176 Type t1b = e.e1.type.toBasetype();
177 if (t1b.ty == Taarray)
181 e.error("indexing an associative array in `@nogc` %s `%s` may cause a GC allocation",
182 f.kind(), f.toPrettyChars());
186 f.printGCUsage(e.loc, "indexing an associative array may cause a GC allocation");
190 override void visit(AssignExp e)
192 if (e.e1.op == EXP.arrayLength)
196 e.error("setting `length` in `@nogc` %s `%s` may cause a GC allocation",
197 f.kind(), f.toPrettyChars());
201 f.printGCUsage(e.loc, "setting `length` may cause a GC allocation");
205 override void visit(CatAssignExp e)
209 e.error("cannot use operator `~=` in `@nogc` %s `%s`",
210 f.kind(), f.toPrettyChars());
214 f.printGCUsage(e.loc, "operator `~=` may cause a GC allocation");
217 override void visit(CatExp e)
221 e.error("cannot use operator `~` in `@nogc` %s `%s`",
222 f.kind(), f.toPrettyChars());
226 f.printGCUsage(e.loc, "operator `~` may cause a GC allocation");
230 Expression checkGC(Scope* sc, Expression e)
232 FuncDeclaration f = sc.func;
233 if (e && e.op != EXP.error && f && sc.intypeof != 1 && !(sc.flags & SCOPE.ctfe) &&
234 (f.type.ty == Tfunction &&
235 (cast(TypeFunction)f.type).isnogc || (f.flags & FUNCFLAG.nogcInprocess) || global.params.vgc) &&
236 !(sc.flags & SCOPE.debug_))
238 scope NOGCVisitor gcv = new NOGCVisitor(f);
239 walkPostorder(e, gcv);
241 return ErrorExp.get();
247 * Removes `_d_HookTraceImpl` if found from `fd`.
248 * This is needed to be able to find hooks that are called though the hook's `*Trace` wrapper.
250 * fd = The function declaration to remove `_d_HookTraceImpl` from
252 private FuncDeclaration stripHookTraceImpl(FuncDeclaration fd)
255 import dmd.dsymbol : Dsymbol;
256 import dmd.root.rootobject : RootObject, DYNCAST;
258 if (fd.ident != Id._d_HookTraceImpl)
261 // Get the Hook from the second template parameter
262 auto templateInstance = fd.parent.isTemplateInstance;
263 RootObject hook = (*templateInstance.tiargs)[1];
264 assert(hook.dyncast() == DYNCAST.dsymbol, "Expected _d_HookTraceImpl's second template parameter to be an alias to the hook!");
265 return (cast(Dsymbol)hook).isFuncDeclaration;