-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.h
326 lines (263 loc) · 8.84 KB
/
type.h
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
#ifndef TYPE_H_
#define TYPE_H_
#include <memory>
#include <string>
#include <bitset>
#include <vector>
#include <cinttypes>
#include "SymbolTable.h"
class Token;
class Type;
class BuiltInType;
class PointerType;
class ArrayType;
class CompoundType;
class IncompleteType;
class UserDefinedTypeRef;
class FuncType;
class AliasType;
class EnumType;
class Type
{
public:
enum Tag
{
Incomplete,
BuiltIn, Array, Pointer,
/* will be in symbol table: */
Alias /* typedef */, Enum, Compound /* I mean struct and union */,
UserDefined,
Func
};
enum Qualifier
{
Const = 1, Volatile = 2, /** volatile is not implemented */
};
using QualifierHolder = uint8_t;
explicit Type(Tag, QualifierHolder);
virtual ~Type() {};
Tag tag() const;
bool isConst() const;
void isConst(bool b);
/* bool isStatic() const;
bool isExternal() const;*/
void width(size_t w) { width_ = w; } // TODO : some more reasonable way
size_t width() const { return width_; }
virtual std::string toString() const = 0; // for test
virtual bool equalUnqual(const std::shared_ptr<Type>& t) = 0;
bool equal(const std::shared_ptr<Type>& t);
virtual std::shared_ptr<Type> shallowCopy() const {
assert(false);
}
static bool isInteger(const std::shared_ptr<Type>& ty);
static bool isUnsignedInteger(const std::shared_ptr<Type>& ty);
static bool isFloating(const std::shared_ptr<Type>& ty);
static bool isVoid(const std::shared_ptr<Type>& ty);
static bool isArithmetic(const std::shared_ptr<Type>& ty);
static bool isPointer(const std::shared_ptr<Type>& ty);
static bool isArray(const std::shared_ptr<Type>& ty);
static bool isScalar(const std::shared_ptr<Type>& ty);
static std::shared_ptr<PointerType> arrayDecay(const std::shared_ptr<Type>& ty);
static std::shared_ptr<Type> derefIfIsUserDefinedType(const std::shared_ptr<Type>& ty);
static size_t alignAt(const std::shared_ptr<Type>& ty);
protected:
const Tag tag_;
QualifierHolder qualifiers_;
size_t width_;
};
class BuiltInType : public Type
{
public:
enum BuiltInSpecifier
{
BI_Unsigned = 32, BI_Short = 64, BI_Long = 128, /* prefix */
BI_Int = 1, BI_Char = 2, BI_Double = 4, BI_Float = 8, BI_VOID = 16
};
enum Category
{
Void = 0, Integer, Floating,
};
using SpecifierHolder = uint8_t;
BuiltInType(Category, bool isUnsigned, size_t width, QualifierHolder);
static std::shared_ptr<BuiltInType> make(SpecifierHolder, QualifierHolder);
Category cat() const { return cat_; }
std::string toString() const override;
std::shared_ptr<BuiltInType> builtinClone() const;
std::shared_ptr<Type> shallowCopy() const override {
return builtinClone();
}
bool equalUnqual(const std::shared_ptr<Type>& t) override;
bool isInteger() const {
return cat_ == Integer;
}
bool isFloating() const {
return cat_ == Floating;
}
bool isUnsigned() const {
assert(isInteger());
return isUnsigned_;
}
void isUnsigned(bool u) {
assert(isInteger());
isUnsigned_ = u;
}
static std::shared_ptr<BuiltInType> maxUIntType();
static std::shared_ptr<BuiltInType> intType();
static std::shared_ptr<BuiltInType> uintType();
static std::shared_ptr<BuiltInType> charType();
static std::shared_ptr<BuiltInType> doubleType();
static std::shared_ptr<BuiltInType> voidType();
private:
Category cat_;
bool isUnsigned_;
};
class PointerType : public Type
{
public:
PointerType(const std::shared_ptr<Type>&, QualifierHolder);
std::shared_ptr<Type> base() const;
std::string toString() const override;
bool equalUnqual(const std::shared_ptr<Type>& t) override;
std::shared_ptr<Type> shallowCopy() const override {
return std::make_shared<PointerType>(base_, qualifiers_);
}
static std::shared_ptr<PointerType> strLiteralType();
private:
std::shared_ptr<Type> base_;
};
class ArrayType : public Type
{
public:
ArrayType(const std::shared_ptr<Type>&, size_t len,
QualifierHolder = 0);
explicit ArrayType(size_t len, QualifierHolder = 0);
std::shared_ptr<Type> base() const;
void base(std::shared_ptr<Type> ty);
size_t len() const;
std::string toString() const override;
bool equalUnqual(const std::shared_ptr<Type>& rhs) override;
std::shared_ptr<Type> shallowCopy() const override {
return std::make_shared<ArrayType>(base_, len_, qualifiers_);
}
private:
std::shared_ptr<Type> base_;
size_t len_;
};
// user-defined types:
// the following three type
// CompoundType, AliasType and EnumType are only used for meta item in symbol table
// Normal variables refer to the meta item
class CompoundType : public Type
{
public:
enum MemModel { Compound_Struct, Compound_Union };
using value_type = std::pair<std::string, std::shared_ptr<Type>>; // (name, type) pair
CompoundType(std::string, MemModel, SymbolTable* , ListSymtab&&);
MemModel model() const { return model_; }
ListSymtab::iterator begin();
ListSymtab::iterator end();
ListSymtab& members() { return members_; }
std::string toString() const override;
std::string name() const { return name_; }
SymbolTable* whereDefined() { return whereDefined_; }
bool equalUnqual(const std::shared_ptr<Type>& t) override;
size_t alignAt() const { return alignAt_; }
private:
std::string name_;
MemModel model_;
ListSymtab members_;
std::vector<int> memOff_;
SymbolTable* whereDefined_;
size_t alignAt_;
};
class IncompleteType : public Type
{
// for undefined struct and union
public:
explicit IncompleteType(std::string name, CompoundType::MemModel m,
SymbolTable* where)
: Type(Incomplete, 0), name_(std::move(name)), model_(m), whereDefined_(where) {}
CompoundType::MemModel model() const { return model_; }
std::string toString() const override
{
if (model_ == CompoundType::Compound_Struct)
return std::string("(struct ") + name_ + ")";
else
return std::string("(union ") + name_ + ")";
}
bool equalUnqual(const std::shared_ptr<Type>& t) override;
SymbolTable* whereDefined() { return whereDefined_; }
private:
std::string name_;
CompoundType::MemModel model_;
SymbolTable* whereDefined_;
};
class UserDefinedTypeRef : public Type
{
public:
UserDefinedTypeRef(std::string name, SymbolTable* t,
QualifierHolder qh, size_t w)
: Type(UserDefined, qh), typeName_(std::move(name)), scope_(t)
{ width_ = w; }
std::string toString() const override;
bool equalUnqual(const std::shared_ptr<Type>& t) override;
const std::string& typeName() { return typeName_; }
SymbolTable& scope() { return *scope_; }
std::shared_ptr<Type> shallowCopy() const override {
return std::make_shared<UserDefinedTypeRef>(typeName_, scope_, qualifiers_, width_);
}
private:
std::string typeName_;
SymbolTable* scope_;
};
class FuncType : public Type
{
public:
using container = std::vector<std::shared_ptr<Type>>;
using iterator = container::iterator;
FuncType(std::string, const std::shared_ptr<Type>& ret);
std::shared_ptr<Type> retType() const { return retType_; };
const std::string& name() const { return name_; }
void addParam(const std::shared_ptr<Type>& p) { params_.push_back(p); }
container::iterator begin() { return params_.begin(); }
container::iterator end() { return params_.end(); }
size_t paramCount() { return params_.size(); }
void defined(bool d) { defined_ = d; }
bool defined() const { return defined_; }
void hasVarArgs(bool h) { hasVarArgs_ = h; }
bool hasVarArgs() const { return hasVarArgs_; }
std::string toString() const override;
bool equalUnqual(const std::shared_ptr<Type>& t) override;
// int typeCode() const override { return (int)Category::Fn; }
private:
std::string name_;
std::shared_ptr<Type> retType_;
std::vector<std::shared_ptr<Type>> params_;
bool defined_ = false;
bool hasVarArgs_ = false;
};
// TODO : addQuad support to enum
class AliasType : public Type
{
public:
explicit AliasType(std::string, const std::shared_ptr<Type>&);
std::shared_ptr<Type> base() const;
std::string toString() const override { return "" ;};
bool equalUnqual(const std::shared_ptr<Type>& t) override { /*TODO : implement*/ return false;};
private:
std::string name_;
std::shared_ptr<Type> base_;
};
class EnumType : public Type
{
public:
using value_type = std::pair<std::string, int>;
EnumType(std::string, std::initializer_list<value_type>);
int value(const std::string&) const;
std::string toString() const override {return "";};
bool equalUnqual(const std::shared_ptr<Type>& t) override {/*TODO : implement*/return false;};
private:
std::string name_;
std::vector<value_type> members_;
};
#endif