aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/sapply.c
blob: 35e38c151ac070f8d4335280596e234d085e8474 (plain)
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

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
 * written by Walter Bright
 * http://www.digitalmars.com
 * Distributed under the Boost Software License, Version 1.0.
 * http://www.boost.org/LICENSE_1_0.txt
 * https://github.com/D-Programming-Language/dmd/blob/master/src/sapply.c
 */

#include "root/dsystem.h"

#include "mars.h"
#include "statement.h"
#include "visitor.h"


/**************************************
 * A Statement tree walker that will visit each Statement s in the tree,
 * in depth-first evaluation order, and call fp(s,param) on it.
 * fp() signals whether the walking continues with its return value:
 * Returns:
 *      0       continue
 *      1       done
 * It's a bit slower than using virtual functions, but more encapsulated and less brittle.
 * Creating an iterator for this would be much more complex.
 */

class PostorderStatementVisitor : public StoppableVisitor
{
public:
    StoppableVisitor *v;
    PostorderStatementVisitor(StoppableVisitor *v) : v(v) {}

    bool doCond(Statement *s)
    {
        if (!stop && s)
            s->accept(this);
        return stop;
    }
    bool applyTo(Statement *s)
    {
        s->accept(v);
        stop = v->stop;
        return true;
    }

    void visit(Statement *s)
    {
        applyTo(s);
    }
    void visit(PeelStatement *s)
    {
        doCond(s->s) || applyTo(s);
    }
    void visit(CompoundStatement *s)
    {
        for (size_t i = 0; i < s->statements->length; i++)
            if (doCond((*s->statements)[i]))
                return;
        applyTo(s);
    }
    void visit(UnrolledLoopStatement *s)
    {
        for (size_t i = 0; i < s->statements->length; i++)
            if (doCond((*s->statements)[i]))
                return;
        applyTo(s);
    }
    void visit(ScopeStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
    void visit(WhileStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(DoStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(ForStatement *s)
    {
        doCond(s->_init) || doCond(s->_body) || applyTo(s);
    }
    void visit(ForeachStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(ForeachRangeStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(IfStatement *s)
    {
        doCond(s->ifbody) || doCond(s->elsebody) || applyTo(s);
    }
    void visit(PragmaStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(SwitchStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(CaseStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
    void visit(DefaultStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
    void visit(SynchronizedStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(WithStatement *s)
    {
        doCond(s->_body) || applyTo(s);
    }
    void visit(TryCatchStatement *s)
    {
        if (doCond(s->_body))
            return;

        for (size_t i = 0; i < s->catches->length; i++)
            if (doCond((*s->catches)[i]->handler))
                return;
        applyTo(s);
    }
    void visit(TryFinallyStatement *s)
    {
        doCond(s->_body) || doCond(s->finalbody) || applyTo(s);
    }
    void visit(ScopeGuardStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
    void visit(DebugStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
    void visit(LabelStatement *s)
    {
        doCond(s->statement) || applyTo(s);
    }
};

bool walkPostorder(Statement *s, StoppableVisitor *v)
{
    PostorderStatementVisitor pv(v);
    s->accept(&pv);
    return v->stop;
}