blob: e1ed16594a8cbeef0ae1a55abf0047f01155f662 (
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
|
/**
* Used to help transform statement AST into flow graph.
*
* 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/stmtstate.d, _stmtstate.d)
* Documentation: https://dlang.org/phobos/dmd_stmtstate.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/stmtstate.d
*/
module dmd.stmtstate;
import dmd.identifier;
import dmd.statement;
/************************************************
* Used to traverse the statement AST to transform it into
* a flow graph.
* Keeps track of things like "where does the `break` go".
* Params:
* block = type of the flow graph node
*/
struct StmtState(block)
{
StmtState* prev;
Statement statement;
Identifier ident;
block* breakBlock;
block* contBlock;
block* switchBlock;
block* defaultBlock;
block* finallyBlock;
block* tryBlock;
this(StmtState* prev, Statement statement)
{
this.prev = prev;
this.statement = statement;
if (prev)
this.tryBlock = prev.tryBlock;
}
block* getBreakBlock(Identifier ident)
{
StmtState* bc;
if (ident)
{
Statement related = null;
block* ret = null;
for (bc = &this; bc; bc = bc.prev)
{
// The label for a breakBlock may actually be some levels up (e.g.
// on a try/finally wrapping a loop). We'll see if this breakBlock
// is the one to return once we reach that outer statement (which
// in many cases will be this same statement).
if (bc.breakBlock)
{
related = bc.statement.getRelatedLabeled();
ret = bc.breakBlock;
}
if (bc.statement == related && bc.prev.ident == ident)
return ret;
}
}
else
{
for (bc = &this; bc; bc = bc.prev)
{
if (bc.breakBlock)
return bc.breakBlock;
}
}
return null;
}
block* getContBlock(Identifier ident)
{
StmtState* bc;
if (ident)
{
block* ret = null;
for (bc = &this; bc; bc = bc.prev)
{
// The label for a contBlock may actually be some levels up (e.g.
// on a try/finally wrapping a loop). We'll see if this contBlock
// is the one to return once we reach that outer statement (which
// in many cases will be this same statement).
if (bc.contBlock)
{
ret = bc.contBlock;
}
if (bc.prev && bc.prev.ident == ident)
return ret;
}
}
else
{
for (bc = &this; bc; bc = bc.prev)
{
if (bc.contBlock)
return bc.contBlock;
}
}
return null;
}
block* getSwitchBlock()
{
StmtState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.switchBlock)
return bc.switchBlock;
}
return null;
}
block* getDefaultBlock()
{
StmtState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.defaultBlock)
return bc.defaultBlock;
}
return null;
}
block* getFinallyBlock()
{
StmtState* bc;
for (bc = &this; bc; bc = bc.prev)
{
if (bc.finallyBlock)
return bc.finallyBlock;
}
return null;
}
}
|