File: Rule.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 (183 lines) | stat: -rw-r--r-- 4,557 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
#include <cassert>
#include <cstddef>
#include <iostream>
#include "location.hh"
#include <memory>
#include <rumur/Decl.h>
#include <rumur/except.h>
#include <rumur/Expr.h>
#include <rumur/Property.h>
#include <rumur/Ptr.h>
#include <rumur/Rule.h>
#include <rumur/Stmt.h>
#include <rumur/traverse.h>
#include <string>
#include <vector>

namespace rumur {

namespace {
  /* A traversal pass that checks any return statements within a rule do not
   * have a trailing expression.
   */
  class ReturnChecker : public ConstTraversal {

   public:
    /* Avoid recursing into functions, that may have return statements with an
     * expression.
     */
    void visit_function(const Function&) final { }
    void visit_functioncall(const FunctionCall&) final { }
    void visit_procedurecall(const ProcedureCall&) final { }

    void visit_return(const Return &n) final {
      if (n.expr != nullptr)
        throw Error("return statement in rule or startstate returns a value",
          n.loc);

      // No need to recurse into the return statement's child.
    }

    static void check(const Node &n) {
      ReturnChecker c;
      c.dispatch(n);
    }

    virtual ~ReturnChecker() = default;
  };
}

Rule::Rule(const std::string &name_, const location &loc_):
  Node(loc_), name(name_) { }

std::vector<Ptr<Rule>> Rule::flatten() const {
  return { Ptr<Rule>(clone()) };
}

AliasRule::AliasRule(const std::vector<Ptr<AliasDecl>> &aliases_,
  const std::vector<Ptr<Rule>> &rules_, const location &loc_):
  Rule("", loc_), rules(rules_) {

  aliases = aliases_;
}

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

void AliasRule::visit(BaseTraversal &visitor) {
  visitor.visit_aliasrule(*this);
}

void AliasRule::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_aliasrule(*this);
}

std::vector<Ptr<Rule>> AliasRule::flatten() const {
  std::vector<Ptr<Rule>> rs;
  for (const Ptr<Rule> &r : rules) {
    for (Ptr<Rule> &f : r->flatten()) {
      f->aliases.insert(f->aliases.begin(), aliases.begin(), aliases.end());
      rs.push_back(f);
    }
  }
  return rs;
}

SimpleRule::SimpleRule(const std::string &name_, const Ptr<Expr> &guard_,
  const std::vector<Ptr<Decl>> &decls_,
  const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Rule(name_, loc_), guard(guard_), decls(decls_), body(body_) { }

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

void SimpleRule::validate() const {
  ReturnChecker::check(*this);
}

void SimpleRule::visit(BaseTraversal &visitor) {
  visitor.visit_simplerule(*this);
}

void SimpleRule::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_simplerule(*this);
}

StartState::StartState(const std::string &name_,
  const std::vector<Ptr<Decl>> &decls_,
  const std::vector<Ptr<Stmt>> &body_, const location &loc_):
  Rule(name_, loc_), decls(decls_), body(body_) { }

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

void StartState::validate() const {
  ReturnChecker::check(*this);
}

void StartState::visit(BaseTraversal &visitor) {
  visitor.visit_startstate(*this);
}

void StartState::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_startstate(*this);
}

PropertyRule::PropertyRule(const std::string &name_, const Property &property_,
  const location &loc_):
  Rule(name_, loc_), property(property_) { }

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

void PropertyRule::visit(BaseTraversal &visitor) {
  visitor.visit_propertyrule(*this);
}

void PropertyRule::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_propertyrule(*this);
}

Ruleset::Ruleset(const std::vector<Quantifier> &quantifiers_,
  const std::vector<Ptr<Rule>> &rules_, const location &loc_):
  Rule("", loc_), rules(rules_) {
  quantifiers = quantifiers_;
}

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

void Ruleset::validate() const {
  for (const Quantifier &q : quantifiers) {
    if (!q.constant())
      throw Error("non-constant quantifier expression as ruleset parameter",
        q.loc);
  }
}

void Ruleset::visit(BaseTraversal &visitor) {
  visitor.visit_ruleset(*this);
}

void Ruleset::visit(ConstBaseTraversal &visitor) const {
  visitor.visit_ruleset(*this);
}

std::vector<Ptr<Rule>> Ruleset::flatten() const {
  std::vector<Ptr<Rule>> rs;
  for (const Ptr<Rule> &r : rules) {
    for (Ptr<Rule> &f : r->flatten()) {
      for (const Quantifier &q : quantifiers)
        f->quantifiers.push_back(q);
      rs.push_back(f);
    }
  }
  return rs;
}

}