/
model.hh
277 lines (210 loc) · 6.26 KB
/
model.hh
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
/**
* @file model.hh
* @brief Model module
*
* This module contains definitions and services that implement an
* optimized storage for expressions. Expressions are stored in a
* Directed Acyclic Graph (DAG) for data sharing.
*
* Copyright (C) 2012 Marco Pensallorto < marco AT pensallorto DOT gmail DOT com >
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
**/
#ifndef MODEL_H
#define MODEL_H
#include <common.hh>
#include <expr.hh>
#include <expr_mgr.hh>
#include <type.hh>
#include <enc.hh>
#include <symbol.hh>
class IModule;
typedef IModule* IModule_ptr;
typedef unordered_map<Expr_ptr, IModule_ptr, PtrHash, PtrEq> Modules;
class IModel;
typedef IModel* IModel_ptr;
// -- composite decls ----------------------------------------------------------
class IModule : public IObject {
public:
virtual const Expr_ptr expr() const =0;
// Symbols management
virtual const Variables& vars() const =0;
virtual void add_var(Expr_ptr expr, IVariable_ptr var) =0;
virtual const Constants& consts() const =0;
virtual void add_const(Expr_ptr expr, IConstant_ptr k) =0;
virtual const Defines& defs() const =0;
virtual void add_def(Expr_ptr expr, IDefine_ptr def) =0;
// Finite State Machine definition
virtual const ExprVector& init() const =0;
virtual void add_init(Expr_ptr expr) =0;
virtual const ExprVector& trans() const =0;
virtual void add_trans(Expr_ptr expr) =0;
};
class IModel : public IObject {
public:
virtual Expr_ptr name() const =0;
virtual void set_name(Expr_ptr name) =0;
virtual void add_module(Expr_ptr name, IModule_ptr module) =0;
virtual IModule& get_module(Expr_ptr name) =0;
virtual const Modules& modules() const =0;
};
class Module : public IModule {
Expr_ptr f_name;
Constants f_localConsts;
Variables f_localVars;
Defines f_localDefs;
ExprVector f_init;
ExprVector f_trans;
public:
Module(const Expr_ptr name);
inline const Expr_ptr expr() const
{ return f_name; }
bool is_main() const
{ return f_name == ExprMgr::INSTANCE().make_main(); }
void add_var(Expr_ptr name, IVariable_ptr var);
const Variables& vars() const
{ return f_localVars; }
void add_def(Expr_ptr name, IDefine_ptr def);
const Defines& defs() const
{ return f_localDefs; }
void add_const(Expr_ptr name, IConstant_ptr k);
const Constants& consts() const
{ return f_localConsts; }
void add_init(Expr_ptr expr);
const ExprVector& init() const
{ return f_init; }
void add_trans(Expr_ptr expr);
const ExprVector& trans() const
{ return f_trans; }
};
ostream& operator<<(ostream& os, Module& module);
class Variable : public IVariable {
Expr_ptr f_ctx;
Expr_ptr f_name;
Type_ptr f_type;
public:
Variable(Expr_ptr ctx, Expr_ptr name, Type_ptr type)
: f_ctx(ctx)
, f_name(name)
, f_type(type)
{}
const Expr_ptr ctx() const
{ return f_ctx; }
const Expr_ptr expr() const
{ return f_name; }
const Type_ptr type() const
{ return f_type; }
};
class Temporary : public ITemporary {
private:
Expr_ptr f_ctx;
Expr_ptr f_name;
Type_ptr f_type;
public:
Temporary(Expr_ptr name, Type_ptr type)
: f_ctx(ExprMgr::INSTANCE().make_main())
, f_name(name)
, f_type(type)
{}
const Expr_ptr ctx() const
{ return f_ctx; }
const Expr_ptr expr() const
{ return f_name; }
const Type_ptr type() const
{ return f_type; }
};
class Constant : public IConstant {
const Expr_ptr f_ctx;
const Expr_ptr f_name;
const Type_ptr f_type;
value_t f_value;
public:
Constant(const Expr_ptr ctx, const Expr_ptr name, Type_ptr type, value_t value)
: f_ctx(ctx)
, f_name(name)
, f_type(type)
, f_value(value)
{}
const Expr_ptr ctx() const
{ return f_ctx; }
const Expr_ptr expr() const
{ return f_name; }
const Type_ptr type() const
{ return f_type; }
value_t value() const
{ return f_value; }
};
class Define : public IDefine {
const Expr_ptr f_ctx;
const Expr_ptr f_name;
const Expr_ptr f_body;
public:
Define(const Expr_ptr ctx, const Expr_ptr name, const Expr_ptr body)
: f_ctx(ctx)
, f_name(name)
, f_body(body)
{}
const Expr_ptr ctx() const
{ return f_ctx; }
const Expr_ptr expr() const
{ return f_name; }
const Expr_ptr body() const
{ return f_body; }
};
class SymbIter {
public:
/* Calculates COI if formula is non-NULL */
SymbIter(IModel& model, Expr_ptr formula = NULL);
~SymbIter();
/* true iff there are more symbols to be processed */
inline bool has_next() const
{ return f_iter != f_symbols.end(); }
inline ISymbol_ptr next()
{
ISymbol_ptr res = (* f_iter);
++ f_iter;
return res;
}
private:
IModel& f_model;
Expr_ptr f_formula; /* for COI */
vector<ISymbol_ptr> f_symbols;
vector<ISymbol_ptr>::const_iterator f_iter;
};
class Model : public IModel {
public:
Model();
~Model();
Expr_ptr name() const
{ return f_name; }
void set_name(Expr_ptr name)
{ f_name = name; }
const Modules& modules() const
{ return f_modules; }
void add_module(Expr_ptr name, IModule_ptr module);
IModule& get_module(Expr_ptr name)
{
const pair <Expr_ptr const, IModule_ptr> found = (*f_modules.find(name));
if (!found.second) {
// throw ModuleNotFoundException(name);
}
return *found.second;
}
private:
Modules f_modules;
Expr_ptr f_name;
};
#endif