-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.h
55 lines (48 loc) · 1.2 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
#pragma once
#include "symbol.h"
#include <vector>
namespace tiger {
enum TypeType {Ty_Nil, Ty_Void, Ty_Int, Ty_String, Ty_Array, Ty_Record, Ty_Name};
struct Type {
Type(TypeType ty) :tt(ty) {}
const TypeType tt;
};
struct ArrayType: public Type {
ArrayType(Type* ty) : Type(Ty_Array), arrTy(ty) {}
Type* arrTy;
};
struct NameType : public Type {
NameType(Symbol sym, Type* ty = nullptr) :Type(Ty_Name), name(sym), type(ty) {}
Symbol name;
Type* type;
};
struct RecordType : public Type {
typedef std::pair<Symbol, Type*> Field;
RecordType(const std::vector<Field>& fields) : Type(Ty_Record), fieldList(fields) {}
std::vector<Field> fieldList;
Type* GetMemberTy(Symbol name) {
for (auto f : fieldList) {
if (f.first == name) return f.second;
}
return nullptr;
}
int GetMemberOffset(Symbol name) {
int offset = -1;
for (size_t i = 0; i < fieldList.size(); ++i) {
if (fieldList[i].first == name) {
offset = (int)i;
break;
}
}
return offset;
}
int GetMemberNum()const {
return (int)fieldList.size();
}
};
//nil, void, int, string types are static allocated
Type* NilType();
Type* VoidType();
Type* IntType();
Type* StringType();
}