-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_mgr.hh
171 lines (129 loc) · 5.77 KB
/
type_mgr.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
/**
* @file type_mgr.hh
* @brief Type system classes (TypeMgr)
*
* 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 TYPE_MGR_H
#define TYPE_MGR_H
#include <expr.hh>
#include <expr_mgr.hh>
typedef class Type* Type_ptr; // fwd
typedef unordered_map<Expr_ptr, Type_ptr, PtrHash, PtrEq> TypeMap;
typedef pair<TypeMap::iterator, bool> TypeHit;
/**
The TypeMgr has two well-defined responsibilites:
1. It keeps track of types that has been defined;
2. It instantiates (and owns) type descriptors (Type objects);
NOTE: there at distinct concepts of type that should not be mixed
together: Here we talk about the type of an expression from a
static analysis PoV. An expression can be boolean, integer, a
particular enum (TODO: prohibit intersection) or an instance of a
certain module. And that's final.
When it comes to encoding though, a variable can be either signed
or unsigned, with a given number of bits. But this, *by design* is
completely ignored by the type inferrer.
Moreover, a wff temporal formula is considered boolean w.r.t. type
inference, it will be responsibility of the formula-analyzer to
determine whether its a CTL/LTL/INVAR, and in different section of
the system different actions can be taken according to this.
*/
typedef class TypeMgr* TypeMgr_ptr;
class TypeMgr {
public:
/* -- inference --------------------------------------------------------- */
inline const Type_ptr find_boolean()
{ return f_register[ f_em.make_boolean_type() ]; }
inline const Type_ptr find_integer()
{ return f_register[ f_em.make_integer_type() ]; }
/* -- decls ------------------------------------------------------------- */
const Type_ptr find_unsigned(unsigned digits);
const Type_ptr find_unsigned_array(unsigned digits, unsigned size);
const Type_ptr find_signed(unsigned digits);
const Type_ptr find_signed_array(unsigned digits, unsigned size);
const Type_ptr find_fixed(unsigned int_digits, unsigned fract_digits);
const Type_ptr find_fixed_array(unsigned int_digits, unsigned fract_digits, unsigned size);
const Type_ptr find_enum(ExprSet& lits);
const Type_ptr find_instance(Expr_ptr identifier);
// -- is_xxx predicates ----------------------------------------------------
/* int_enum is an important subcase of enumeratives. An
all-integer valued enumerative can safely be treated as an
integer w.r.t. type inferring. */
bool is_boolean(const Type_ptr tp) const;
// 42 is an integer constant
bool is_integer(const Type_ptr tp) const;
// an algebraic variable
bool is_algebraic(const Type_ptr tp) const;
bool is_signed_algebraic(const Type_ptr tp) const;
bool is_unsigned_algebraic(const Type_ptr tp) const;
bool is_fixed_algebraic(const Type_ptr tp) const;
// an ENUM
bool is_enum(const Type_ptr tp) const;
// an INSTANCE
bool is_instance(const Type_ptr tp) const;
// an ARRAY
bool is_array(const Type_ptr tp) const;
// -- as_xxx accessors ------------------------------------------------------
typedef class BooleanType* BooleanType_ptr;
BooleanType_ptr as_boolean(const Type_ptr tp) const;
typedef class IntegerType* IntegerType_ptr;
IntegerType_ptr as_integer(const Type_ptr tp) const;
typedef class AlgebraicType* AlgebraicType_ptr;
AlgebraicType_ptr as_algebraic(const Type_ptr tp) const;
typedef class SignedAlgebraicType* SignedAlgebraicType_ptr;
SignedAlgebraicType_ptr as_signed_algebraic(const Type_ptr tp) const;
typedef class UnsignedAlgebraicType* UnsignedAlgebraicType_ptr;
UnsignedAlgebraicType_ptr as_unsigned_algebraic(const Type_ptr tp) const;
typedef class FixedAlgebraicType* FixedAlgebraicType_ptr;
FixedAlgebraicType_ptr as_fixed_algebraic(const Type_ptr tp) const;
typedef class EnumType* EnumType_ptr;
EnumType_ptr as_enum(const Type_ptr tp) const;
typedef class Instance* Instance_ptr;
Instance_ptr as_instance(const Type_ptr tp) const;
typedef class ArrayType* ArrayType_ptr;
ArrayType_ptr as_array(const Type_ptr tp) const;
// singleton instance accessor
static inline TypeMgr& INSTANCE() {
if (! f_instance) {
f_instance = new TypeMgr();
}
return (*f_instance);
}
inline ExprMgr& em() const
{ return f_em; }
protected:
TypeMgr();
~TypeMgr();
private:
static TypeMgr_ptr f_instance;
/* --- low-level services ----------------------------------------------- */
// register a type
void register_type(const Expr_ptr expr, Type_ptr vtype);
// lookup up a type, returns NULL if not found
inline Type_ptr lookup_type(const Expr_ptr expr)
{ return f_register [ expr ]; }
/* local data */
TypeMap f_register;
// ref to expr manager
ExprMgr& f_em;
};
#endif