File: Stmt.cc

package info (click to toggle)
rumur 2020.12.20-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,292 kB
  • sloc: cpp: 17,090; ansic: 2,537; objc: 1,542; python: 1,120; sh: 538; yacc: 536; lex: 229; lisp: 15; makefile: 5
file content (334 lines) | stat: -rw-r--r-- 8,194 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <cassert>
#include <cstddef>
#include <iostream>
#include <memory>
#include <rumur/Decl.h>
#include <rumur/except.h>
#include <rumur/Expr.h>
#include <rumur/Function.h>
#include <rumur/Property.h>
#include <rumur/Ptr.h>
#include <rumur/Stmt.h>
#include <rumur/traverse.h>
#include <rumur/TypeExpr.h>
#include <string>
#include <utility>
#include <vector>

namespace rumur {

Stmt::Stmt(const location &loc_): Node(loc_) { }

AliasStmt::AliasStmt(const std::vector<Ptr<AliasDecl>> &aliases_,
  const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Stmt(loc_), aliases(aliases_), body(body_) { }

AliasStmt *AliasStmt::clone() const {
  return new AliasStmt(*this);
}

void AliasStmt::visit(BaseTraversal &visitor) {
  visitor.visit_aliasstmt(*this);
}

void AliasStmt::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_aliasstmt(*this);
}

PropertyStmt::PropertyStmt(const Property &property_,
  const std::string &message_, const location &loc_):
  Stmt(loc_), property(property_), message(message_) { }

PropertyStmt *PropertyStmt::clone() const {
  return new PropertyStmt(*this);
}

void PropertyStmt::validate() const {
  if (property.category == Property::LIVENESS)
    throw Error("liveness property appearing as a statement instead of a top "
      "level property", loc);
}

void PropertyStmt::visit(BaseTraversal &visitor) {
  visitor.visit_propertystmt(*this);
}

void PropertyStmt::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_propertystmt(*this);
}

Assignment::Assignment(const Ptr<Expr> &lhs_, const Ptr<Expr> &rhs_,
  const location &loc_):
  Stmt(loc_), lhs(lhs_), rhs(rhs_) { }

Assignment *Assignment::clone() const {
  return new Assignment(*this);
}

void Assignment::validate() const {

  if (!lhs->is_lvalue())
    throw Error("non-lvalue expression cannot be assigned to", loc);

  if (lhs->is_readonly())
    throw Error("read-only expression cannot be assigned to", loc);

  if (!lhs->type()->coerces_to(*rhs->type()))
    throw Error("invalid assignment from incompatible type", loc);
}

void Assignment::visit(BaseTraversal &visitor) {
  visitor.visit_assignment(*this);
}

void Assignment::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_assignment(*this);
}

Clear::Clear(const Ptr<Expr> &rhs_, const location &loc_):
  Stmt(loc_), rhs(rhs_) { }

Clear *Clear::clone() const {
  return new Clear(*this);
}

void Clear::validate() const {
  if (!rhs->is_lvalue())
    throw Error("invalid clear of non-lvalue expression", loc);

  if (rhs->is_readonly())
    throw Error("invalid clear of read-only expression", loc);
}

void Clear::visit(BaseTraversal &visitor) {
  visitor.visit_clear(*this);
}

void Clear::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_clear(*this);
}

ErrorStmt::ErrorStmt(const std::string &message_, const location &loc_):
  Stmt(loc_), message(message_) { }

ErrorStmt *ErrorStmt::clone() const {
  return new ErrorStmt(*this);
}

void ErrorStmt::visit(BaseTraversal &visitor) {
  visitor.visit_errorstmt(*this);
}

void ErrorStmt::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_errorstmt(*this);
}

For::For(const Quantifier &quantifier_,
  const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Stmt(loc_), quantifier(quantifier_), body(body_) { }

For *For::clone() const {
  return new For(*this);
}

void For::visit(BaseTraversal &visitor) {
  visitor.visit_for(*this);
}

void For::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_for(*this);
}

IfClause::IfClause(const Ptr<Expr> &condition_,
  const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Node(loc_), condition(condition_), body(body_) { }

IfClause *IfClause::clone() const {
  return new IfClause(*this);
}

void IfClause::validate() const {

  if (condition == nullptr) {
    // This is an 'else' block.
    return;
  }

  // Only boolean conditions are supported.
  if (!condition->is_boolean())
    throw Error("condition of if clause is not a boolean expression", loc);
}

void IfClause::visit(BaseTraversal &visitor) {
  visitor.visit_ifclause(*this);
}

void IfClause::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_ifclause(*this);
}

If::If(const std::vector<IfClause> &clauses_, const location &loc_):
  Stmt(loc_), clauses(clauses_) { }

If *If::clone() const {
  return new If(*this);
}

void If::visit(BaseTraversal &visitor) {
  visitor.visit_if(*this);
}

void If::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_if(*this);
}

ProcedureCall::ProcedureCall(const std::string &name,
  const std::vector<Ptr<Expr>> &arguments, const location &loc_):
  Stmt(loc_), call(name, arguments, loc_) {

  call.within_procedure_call = true;
}

ProcedureCall *ProcedureCall::clone() const {
  return new ProcedureCall(*this);
}

void ProcedureCall::visit(BaseTraversal &visitor) {
  visitor.visit_procedurecall(*this);
}

void ProcedureCall::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_procedurecall(*this);
}

Put::Put(const std::string &value_, const location &loc_):
  Stmt(loc_), value(value_) { }

Put::Put(const Ptr<Expr> &expr_, const location &loc_):
  Stmt(loc_), expr(expr_) { }

Put *Put::clone() const {
  return new Put(*this);
}

void Put::validate() const {
  if (expr != nullptr && !expr->is_lvalue() && !expr->type()->is_simple())
    throw Error("printing a complex non-lvalue is not supported", loc);
}

void Put::visit(BaseTraversal &visitor) {
  visitor.visit_put(*this);
}

void Put::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_put(*this);
}

Return::Return(const Ptr<Expr> &expr_, const location &loc_):
  Stmt(loc_), expr(expr_) { }

Return *Return::clone() const {
  return new Return(*this);
}

void Return::visit(BaseTraversal &visitor) {
  visitor.visit_return(*this);
}

void Return::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_return(*this);
}

SwitchCase::SwitchCase(const std::vector<Ptr<Expr>> &matches_,
    const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Node(loc_), matches(matches_), body(body_) { }

SwitchCase *SwitchCase::clone() const {
  return new SwitchCase(*this);
}

void SwitchCase::visit(BaseTraversal &visitor) {
  visitor.visit_switchcase(*this);
}

void SwitchCase::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_switchcase(*this);
}

Switch::Switch(const Ptr<Expr> &expr_, const std::vector<SwitchCase> &cases_,
    const location &loc_):
  Stmt(loc_), expr(expr_), cases(cases_) { }

Switch *Switch::clone() const {
  return new Switch(*this);
}

void Switch::validate() const {

  const Ptr<TypeExpr> t = expr->type();
  if (!t->is_simple())
    throw Error("switch expression has complex type", expr->loc);

  for (const SwitchCase &c : cases) {
    for (const Ptr<Expr> &e : c.matches) {
      if (!t->coerces_to(*e->type()))
        throw Error("expression in case cannot be compared to switch "
          "expression", e->loc);
    }
  }
}

void Switch::visit(BaseTraversal &visitor) {
  visitor.visit_switch(*this);
}

void Switch::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_switch(*this);
}

Undefine::Undefine(const Ptr<Expr> &rhs_, const location &loc_):
  Stmt(loc_), rhs(rhs_) { }

Undefine *Undefine::clone() const {
  return new Undefine(*this);
}

void Undefine::validate() const {
  if (!rhs->is_lvalue())
    throw Error("invalid undefine of non-lvalue expression", loc);

  if (rhs->is_readonly())
    throw Error("invalid undefine of read-only expression", loc);
}

void Undefine::visit(BaseTraversal &visitor) {
  visitor.visit_undefine(*this);
}

void Undefine::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_undefine(*this);
}

While::While(const Ptr<Expr> &condition_, const std::vector<Ptr<Stmt>> &body_,
    const location &loc_):
  Stmt(loc_), condition(condition_), body(body_) { }

While *While::clone() const {
  return new While(*this);
}

void While::validate() const {
  if (!condition->is_boolean())
    throw Error("condition in while loop is not a boolean expression",
      condition->loc);
}

void While::visit(BaseTraversal &visitor) {
  visitor.visit_while(*this);
}

void While::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_while(*this);
}

}