Debian Codegraph @

gcc/d/dmd/visitor/foreachvar.d

raw
1 /**
2 * Utility to visit every variable in an expression.
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/visitor/foreachvar.d, _foreachvar.d)
8 * Documentation: https://dlang.org/phobos/dmd_foreachvar.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/visitor/foreachvar.d
10 */
11
12 module dmd.visitor.foreachvar;
13
14 import core.stdc.stdio;
15 import core.stdc.stdlib;
16 import core.stdc.string;
17
18 import dmd.arraytypes;
19 import dmd.astenums;
20 import dmd.attrib;
21 import dmd.dclass;
22 import dmd.declaration;
23 import dmd.dstruct;
24 import dmd.dsymbol;
25 import dmd.dsymbolsem;
26 import dmd.dtemplate;
27 import dmd.expression;
28 import dmd.func;
29 import dmd.id;
30 import dmd.identifier;
31 import dmd.init;
32 import dmd.initsem;
33 import dmd.mtype;
34 import dmd.printast;
35 import dmd.root.array;
36 import dmd.rootobject;
37 import dmd.statement;
38 import dmd.tokens;
39 import dmd.visitor;
40 import dmd.visitor.postorder;
41
42 /*********************************************
43 * Visit each Expression in e, and call dgVar() on each variable declared in it.
44 * Params:
45 * e = expression tree to visit
46 * dgVar = call when a variable is declared
47 */
48 void foreachVar(Expression e, void delegate(VarDeclaration) dgVar)
49 {
50 if (!e)
51 return;
52
53 extern (C++) final class VarWalker : StoppableVisitor
54 {
55 alias visit = typeof(super).visit;
56 extern (D) void delegate(VarDeclaration) dgVar;
57
58 extern (D) this(void delegate(VarDeclaration) dgVar) scope @safe
59 {
60 this.dgVar = dgVar;
61 }
62
63 override void visit(Expression e)
64 {
65 }
66
67 override void visit(ErrorExp e)
68 {
69 }
70
71 override void visit(DeclarationExp e)
72 {
73 VarDeclaration v = e.declaration.isVarDeclaration();
74 if (!v)
75 return;
76 if (TupleDeclaration td = v.toAlias().isTupleDeclaration())
77 td.foreachVar((s) { dgVar(s.isVarDeclaration()); });
78 else
79 dgVar(v);
80 Dsymbol s = v.toAlias();
81 if (s == v && !v.isStatic() && v._init)
82 {
83 if (auto ie = v._init.isExpInitializer())
84 ie.exp.foreachVar(dgVar);
85 }
86 }
87
88 override void visit(IndexExp e)
89 {
90 if (e.lengthVar)
91 dgVar(e.lengthVar);
92 }
93
94 override void visit(SliceExp e)
95 {
96 if (e.lengthVar)
97 dgVar(e.lengthVar);
98 }
99 }
100
101 scope VarWalker v = new VarWalker(dgVar);
102 walkPostorder(e, v);
103 }
104
105 /***************
106 * Transitively walk Statement s, pass Expressions to dgExp(), VarDeclarations to dgVar().
107 * Params:
108 * s = Statement to traverse
109 * dgExp = delegate to pass found Expressions to
110 * dgVar = delegate to pass found VarDeclarations to
111 */
112 void foreachExpAndVar(Statement s,
113 void delegate(Expression) dgExp,
114 void delegate(VarDeclaration) dgVar)
115 {
116 void visit(Statement s)
117 {
118 void visitExp(ExpStatement s)
119 {
120 if (s.exp)
121 dgExp(s.exp);
122 }
123
124 void visitDtorExp(DtorExpStatement s)
125 {
126 if (s.exp)
127 dgExp(s.exp);
128 }
129
130 void visitIf(IfStatement s)
131 {
132 dgExp(s.condition);
133 visit(s.ifbody);
134 visit(s.elsebody);
135 }
136
137 void visitDo(DoStatement s)
138 {
139 dgExp(s.condition);
140 visit(s._body);
141 }
142
143 void visitFor(ForStatement s)
144 {
145 visit(s._init);
146 if (s.condition)
147 dgExp(s.condition);
148 if (s.increment)
149 dgExp(s.increment);
150 visit(s._body);
151 }
152
153 void visitSwitch(SwitchStatement s)
154 {
155 dgExp(s.condition);
156 // Note that the body contains the Case and Default
157 // statements, so we only need to compile the expressions
158 foreach (cs; *s.cases)
159 {
160 dgExp(cs.exp);
161 }
162 visit(s._body);
163 }
164
165 void visitCase(CaseStatement s)
166 {
167 visit(s.statement);
168 }
169
170 void visitReturn(ReturnStatement s)
171 {
172 if (s.exp)
173 dgExp(s.exp);
174 }
175
176 void visitCompound(CompoundStatement s)
177 {
178 if (s.statements)
179 {
180 foreach (s2; *s.statements)
181 {
182 visit(s2);
183 }
184 }
185 }
186
187 void visitCompoundDeclaration(CompoundDeclarationStatement s)
188 {
189 visitCompound(s);
190 }
191
192 void visitUnrolledLoop(UnrolledLoopStatement s)
193 {
194 foreach (s2; *s.statements)
195 {
196 visit(s2);
197 }
198 }
199
200 void visitScope(ScopeStatement s)
201 {
202 visit(s.statement);
203 }
204
205 void visitDefault(DefaultStatement s)
206 {
207 visit(s.statement);
208 }
209
210 void visitWith(WithStatement s)
211 {
212 // If it is with(Enum) {...}, just execute the body.
213 if (s.exp.op == EXP.scope_ || s.exp.op == EXP.type)
214 {
215 }
216 else
217 {
218 dgVar(s.wthis);
219 dgExp(s.exp);
220 }
221 visit(s._body);
222 }
223
224 void visitTryCatch(TryCatchStatement s)
225 {
226 visit(s._body);
227 foreach (ca; *s.catches)
228 {
229 if (ca.var)
230 dgVar(ca.var);
231 visit(ca.handler);
232 }
233 }
234
235 void visitTryFinally(TryFinallyStatement s)
236 {
237 visit(s._body);
238 visit(s.finalbody);
239 }
240
241 void visitThrow(ThrowStatement s)
242 {
243 dgExp(s.exp);
244 }
245
246 void visitLabel(LabelStatement s)
247 {
248 visit(s.statement);
249 }
250
251 if (!s)
252 return;
253
254 final switch (s.stmt)
255 {
256 case STMT.Exp: visitExp(s.isExpStatement()); break;
257 case STMT.DtorExp: visitDtorExp(s.isDtorExpStatement()); break;
258 case STMT.Compound: visitCompound(s.isCompoundStatement()); break;
259 case STMT.CompoundDeclaration: visitCompoundDeclaration(s.isCompoundDeclarationStatement()); break;
260 case STMT.UnrolledLoop: visitUnrolledLoop(s.isUnrolledLoopStatement()); break;
261 case STMT.Scope: visitScope(s.isScopeStatement()); break;
262 case STMT.Do: visitDo(s.isDoStatement()); break;
263 case STMT.For: visitFor(s.isForStatement()); break;
264 case STMT.If: visitIf(s.isIfStatement()); break;
265 case STMT.Switch: visitSwitch(s.isSwitchStatement()); break;
266 case STMT.Case: visitCase(s.isCaseStatement()); break;
267 case STMT.Default: visitDefault(s.isDefaultStatement()); break;
268 case STMT.Return: visitReturn(s.isReturnStatement()); break;
269 case STMT.With: visitWith(s.isWithStatement()); break;
270 case STMT.TryCatch: visitTryCatch(s.isTryCatchStatement()); break;
271 case STMT.TryFinally: visitTryFinally(s.isTryFinallyStatement()); break;
272 case STMT.Throw: visitThrow(s.isThrowStatement()); break;
273 case STMT.Label: visitLabel(s.isLabelStatement()); break;
274
275 case STMT.CompoundAsm:
276 case STMT.Asm:
277 case STMT.InlineAsm:
278 case STMT.GccAsm:
279
280 case STMT.Break:
281 case STMT.Continue:
282 case STMT.GotoDefault:
283 case STMT.GotoCase:
284 case STMT.SwitchError:
285 case STMT.Goto:
286 case STMT.Pragma:
287 case STMT.Import:
288 case STMT.Error:
289 break; // ignore these
290
291 case STMT.ScopeGuard:
292 case STMT.Foreach:
293 case STMT.ForeachRange:
294 case STMT.Debug:
295 case STMT.CaseRange:
296 case STMT.StaticForeach:
297 case STMT.StaticAssert:
298 case STMT.Conditional:
299 case STMT.While:
300 case STMT.Forwarding:
301 case STMT.Mixin:
302 case STMT.Peel:
303 case STMT.Synchronized:
304 assert(0); // should have been rewritten
305 }
306 }
307
308 visit(s);
309 }