-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStmt.h
More file actions
224 lines (173 loc) · 6.32 KB
/
Stmt.h
File metadata and controls
224 lines (173 loc) · 6.32 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#include "Exp.h"
#include "VarDecl.h"
/// Base class for statement syntax.
class Stmt
{
public:
/// The destructor is virtual, ensuring that the destructor for a derived
/// class will be properly invoked.
virtual ~Stmt() {}
/// Dispatch to a visitor. \see StmtVisitor
virtual void Dispatch( StmtVisitor& visitor ) = 0;
};
/// Unique pointer to statement.
using StmtPtr = std::unique_ptr<Stmt>;
/// Function call statement, which simply holds a CallExp.
class CallStmt : public Stmt
{
public:
/// Construct from the given function call expression.
CallStmt( CallExpPtr&& callExp )
: m_callExp( std::move( callExp ) )
{
}
/// Get the function call expression.
const CallExp& GetCallExp() const { return *m_callExp; }
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
CallExpPtr m_callExp;
};
// Assignment statement.
class AssignStmt : public Stmt
{
public:
/// Construct assignment statement. The lvalue is a variable, and the
/// rvalue is an arbitrary expression.
AssignStmt( const std::string& varName, ExpPtr&& rvalue )
: m_varName( varName )
, m_rvalue( std::move( rvalue ) )
{
}
/// Get the variable name (lvalue).
const std::string& GetVarName() const { return m_varName; }
/// Get the rvalue (the right-hand side of the assignment).
const Exp& GetRvalue() const { return *m_rvalue; }
/// Get the declaration of the assigned variable (null until typechecked).
const VarDecl* GetVarDecl() const { return m_varDecl; }
/// Link the assignment to the declaration of the assigned variable
/// (called by the typechecker).
void SetVarDecl( const VarDecl* varDecl ) { m_varDecl = varDecl; }
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
std::string m_varName;
ExpPtr m_rvalue;
const VarDecl* m_varDecl;
};
/// A declaration statement (e.g. "int x = 0;") declares a local variable with
/// an optional initializer.
class DeclStmt : public Stmt
{
public:
/// Construct a declaration statement from the specified variable declaration
/// and optional initializer expression.
DeclStmt( VarDeclPtr&& varDecl, ExpPtr&& initExp = ExpPtr() )
: m_varDecl( std::move( varDecl ) )
, m_initExp( std::move( initExp ) )
{
}
/// Get pointer to variable declaration, which is stored at use sites by the typechecker.
const VarDecl* GetVarDecl() const { return m_varDecl.get(); }
/// Check whether this declaration has an initializer expression.
bool HasInitExp() const { return bool( m_initExp ); }
/// Get the initializer expression. Check HasInitExp() before calling.
const Exp& GetInitExp() const
{
assert( HasInitExp() && "Expected initializer expression in variable declaration" );
return *m_initExp;
}
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
VarDeclPtr m_varDecl;
ExpPtr m_initExp;
};
/// Return statement.
class ReturnStmt : public Stmt
{
public:
/// Construct return statement with the given return value expression.
/// (Note that void functions are not permitted, so the return value is required.)
ReturnStmt( ExpPtr&& exp )
: m_exp( std::move( exp ) )
{
}
/// Get the return value expression.
const Exp& GetExp() const { return *m_exp; }
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
ExpPtr m_exp;
};
/// A sequence of statements.
class SeqStmt : public Stmt
{
public:
/// Construct sequence of statements from a vector of unique pointers.
SeqStmt( std::vector<StmtPtr>&& stmts )
: m_stmts( std::move( stmts ) )
{
}
/// Get the sequence of statements.
const std::vector<StmtPtr>& Get() const { return m_stmts; }
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
std::vector<StmtPtr> m_stmts;
};
/// Unique pointer to a sequence of statements.
using SeqStmtPtr = std::unique_ptr<SeqStmt>;
/// If statement syntax.
class IfStmt : public Stmt
{
public:
/// Construct "if" statement with conditional expression, "then" statement
/// (which might be a sequence), and an optional "else" statement.
IfStmt( ExpPtr condExp, StmtPtr thenStmt, StmtPtr elseStmt = StmtPtr() )
: m_condExp( std::move( condExp ) )
, m_thenStmt( std::move( thenStmt ) )
, m_elseStmt( std::move( elseStmt ) )
{
}
/// Get the conditional expression.
const Exp& GetCondExp() const { return *m_condExp; }
/// Get the "then" statement, which might be a sequence.
const Stmt& GetThenStmt() const { return *m_thenStmt; }
/// Check whether this "if" statement has an "else" statement.
bool HasElseStmt() const { return bool( m_elseStmt ); }
/// Get the "else" statement.
const Stmt& GetElseStmt() const
{
assert( HasElseStmt() && "Expected else statement" );
return *m_elseStmt;
}
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
ExpPtr m_condExp;
StmtPtr m_thenStmt;
StmtPtr m_elseStmt;
};
/// While statement.
class WhileStmt : public Stmt
{
public:
/// Construct while statement from a conditional expression and the loop body
/// statement (which might be a sequence).
WhileStmt( ExpPtr condExp, StmtPtr bodyStmt )
: m_condExp( std::move( condExp ) )
, m_bodyStmt( std::move( bodyStmt ) )
{
}
/// Get the conditional expression.
const Exp& GetCondExp() const { return *m_condExp; }
/// Get the loop body statement (which might be a sequence).
const Stmt& GetBodyStmt() const { return *m_bodyStmt; }
/// Dispatch to a visitor.
void Dispatch( StmtVisitor& visitor ) override { visitor.Visit( *this ); }
private:
ExpPtr m_condExp;
StmtPtr m_bodyStmt;
};