1
/**
2
 * Provides a visitor for statements that allows rewriting the currently visited node.
3
 *
4
 * Copyright:   Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
5
 * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
6
 * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7
 * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/statement_rewrite_walker.d, _statement_rewrite_walker.d)
8
 * Documentation:  https://dlang.org/phobos/dmd_statement_rewrite_walker.html
9
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/statement_rewrite_walker.d
10
 */
11

12
module dmd.statement_rewrite_walker;
13

14
import core.stdc.stdio;
15

16
import dmd.statement;
17
import dmd.visitor;
18

19

20
/** A visitor to walk entire statements and provides ability to replace any sub-statements.
21
 */
22
extern (C++) class StatementRewriteWalker : SemanticTimePermissiveVisitor
23
{
24
    alias visit = SemanticTimePermissiveVisitor.visit;
25

26
    /* Point the currently visited statement.
27
     * By using replaceCurrent() method, you can replace AST during walking.
28
     */
29
    Statement* ps;
30

31
public:
32
    final void visitStmt(ref Statement s)
33
    {
34 1
        ps = &s;
35 1
        s.accept(this);
36
    }
37

38
    final void replaceCurrent(Statement s)
39
    {
40 1
        *ps = s;
41
    }
42

43
    override void visit(PeelStatement s)
44
    {
45 0
        if (s.s)
46 0
            visitStmt(s.s);
47
    }
48

49
    override void visit(CompoundStatement s)
50
    {
51 1
        if (s.statements && s.statements.dim)
52
        {
53 1
            for (size_t i = 0; i < s.statements.dim; i++)
54
            {
55 1
                if ((*s.statements)[i])
56 1
                    visitStmt((*s.statements)[i]);
57
            }
58
        }
59
    }
60

61
    override void visit(CompoundDeclarationStatement s)
62
    {
63 0
        visit(cast(CompoundStatement)s);
64
    }
65

66
    override void visit(UnrolledLoopStatement s)
67
    {
68 1
        if (s.statements && s.statements.dim)
69
        {
70 1
            for (size_t i = 0; i < s.statements.dim; i++)
71
            {
72 1
                if ((*s.statements)[i])
73 1
                    visitStmt((*s.statements)[i]);
74
            }
75
        }
76
    }
77

78
    override void visit(ScopeStatement s)
79
    {
80 1
        if (s.statement)
81 1
            visitStmt(s.statement);
82
    }
83

84
    override void visit(WhileStatement s)
85
    {
86 0
        if (s._body)
87 0
            visitStmt(s._body);
88
    }
89

90
    override void visit(DoStatement s)
91
    {
92 1
        if (s._body)
93 1
            visitStmt(s._body);
94
    }
95

96
    override void visit(ForStatement s)
97
    {
98 1
        if (s._init)
99 0
            visitStmt(s._init);
100 1
        if (s._body)
101 1
            visitStmt(s._body);
102
    }
103

104
    override void visit(ForeachStatement s)
105
    {
106 0
        if (s._body)
107 0
            visitStmt(s._body);
108
    }
109

110
    override void visit(ForeachRangeStatement s)
111
    {
112 0
        if (s._body)
113 0
            visitStmt(s._body);
114
    }
115

116
    override void visit(IfStatement s)
117
    {
118 1
        if (s.ifbody)
119 1
            visitStmt(s.ifbody);
120 1
        if (s.elsebody)
121 1
            visitStmt(s.elsebody);
122
    }
123

124
    override void visit(SwitchStatement s)
125
    {
126 1
        if (s._body)
127 1
            visitStmt(s._body);
128
    }
129

130
    override void visit(CaseStatement s)
131
    {
132 1
        if (s.statement)
133 1
            visitStmt(s.statement);
134
    }
135

136
    override void visit(CaseRangeStatement s)
137
    {
138 0
        if (s.statement)
139 0
            visitStmt(s.statement);
140
    }
141

142
    override void visit(DefaultStatement s)
143
    {
144 1
        if (s.statement)
145 1
            visitStmt(s.statement);
146
    }
147

148
    override void visit(SynchronizedStatement s)
149
    {
150 0
        if (s._body)
151 0
            visitStmt(s._body);
152
    }
153

154
    override void visit(WithStatement s)
155
    {
156 1
        if (s._body)
157 1
            visitStmt(s._body);
158
    }
159

160
    override void visit(TryCatchStatement s)
161
    {
162 1
        if (s._body)
163 1
            visitStmt(s._body);
164 1
        if (s.catches && s.catches.dim)
165
        {
166 1
            for (size_t i = 0; i < s.catches.dim; i++)
167
            {
168 1
                Catch c = (*s.catches)[i];
169 1
                if (c && c.handler)
170 1
                    visitStmt(c.handler);
171
            }
172
        }
173
    }
174

175
    override void visit(TryFinallyStatement s)
176
    {
177 1
        if (s._body)
178 1
            visitStmt(s._body);
179 1
        if (s.finalbody)
180 1
            visitStmt(s.finalbody);
181
    }
182

183
    override void visit(DebugStatement s)
184
    {
185 0
        if (s.statement)
186 0
            visitStmt(s.statement);
187
    }
188

189
    override void visit(LabelStatement s)
190
    {
191 1
        if (s.statement)
192 1
            visitStmt(s.statement);
193
    }
194
}

Read our documentation on viewing source code .

Loading