-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexprtype.cpp
181 lines (169 loc) · 4.88 KB
/
exprtype.cpp
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
#include "exprtype.h"
expr_type_t &ExprType::get() {
return type;
}
bool ExprType::change(int ty) {
type.type = ty;
return true;
}
bool ExprType::change(std::string ty) {
type.type = T_USER_TYPE;
type.user_type = ty;
return true;
}
bool ExprType::change(ExprType *ty) {
type= ty->type;
next = ty->next;
return true;
}
bool ExprType::change(int ary, ExprType *ty) {
type.type = ary;
next = ty;
return true;
}
bool ExprType::is_array() {
if(type.type == T_ARRAY) return true;
else return false;
}
bool ExprType::is_ref() {
return type.ref;
}
void ExprType::set_ref(bool b) {
type.ref = b;
}
bool ExprType::eql_type(int ty, bool is_ary) {
if(is_ary) {
return (type.type & ty) && (type.type & T_ARRAY);
} else {
return type.type == ty;
}
}
bool ExprType::eql_type(std::string userty) {
if(type.user_type == userty) return true;
return false;
}
bool ExprType::eql_type(ExprType *ty) {
if(ty->eql_type(type.type) && ty->eql_type(type.user_type)) {
if(next && ty->next) {
ExprType *_next = this;
ty = ty->next;
bool res = false;
res = ty->eql_type(next);
return res;
}
return true;
} else return false;
}
std::string ExprType::to_string() {
return Type::type_to_str(this);
}
namespace Type {
ExprType *str_to_type(std::string str) {
int is_ary = 0;
auto ary = [](std::string str, ExprType *ty) -> ExprType * {
size_t p=0;
while((p=str.find("[]")) != std::string::npos) {
str = str.substr(p+1);
ty->next = new ExprType(ty->get().type);
ty->change(T_ARRAY);
}
return ty;
};
if(str.find("int64") != std::string::npos) {
ExprType *ty = new ExprType(T_INT64);
ty = ary(str, ty);
return ty;
} else if(str.find("int") != std::string::npos) {
ExprType *ty = new ExprType(T_INT);
ty = ary(str, ty);
return ty;
} else if(str == "void") {
return new ExprType(T_VOID);
} else if(str == "char") {
return new ExprType(T_CHAR);
} else if(str == "bool") {
return new ExprType(T_BOOL);
} else if(str == "string") {
return new ExprType(T_STRING);
} else if(str == "double") {
return new ExprType(T_DOUBLE);
} else {
return new ExprType(str);
}
}
std::string type_to_str(ExprType *type) {
std::string str_ty;
switch(type->get().type) {
case T_INT:
str_ty = "int";
break;
case T_INT64:
str_ty = "int64";
break;
case T_CHAR:
str_ty = "char";
break;
case T_BOOL:// bool is exist but unsupport now..
str_ty = "bool";
break;
case T_STRING:
str_ty = "string";
break;
case T_DOUBLE:
str_ty = "double";
break;
case T_USER_TYPE:
str_ty = type->get().user_type;
break;
case T_ARRAY:
str_ty = "[" + (type->next ? type_to_str(type->next) : "") + "]";
break;
}
return str_ty;
}
llvm::Type *type_to_llvmty(ExprType *type) {
ExprType *ty = type;
llvm::Type *out;
int ary_count = 0;
while(ty->is_array()) {
ty = ty->next;
ary_count++;
}
if(ty->eql_type(T_VOID)) {
out = llvm::Type::getVoidTy(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_STRING)) {
out = llvm::Type::getInt8PtrTy(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_DOUBLE)) {
out = llvm::Type::getDoubleTy(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_INT)) {
out = llvm::Type::getInt32Ty(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_INT64)) {
out = llvm::Type::getInt64Ty(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_CHAR)) {
out = llvm::Type::getInt32Ty(llvm::getGlobalContext());
while(ary_count--) out = out->getPointerTo();
} else if(ty->eql_type(T_USER_TYPE)) {
return nullptr;
// out = builder.getInt8PtrTy();
// while(ary_count--) out = out->getPointerTo();
}
// llvm::Type *func_ret_type =
// info.type.eql_type(T_STRING) ?
// (llvm::Type *)builder.getInt8PtrTy() :
// info.type.eql_type(T_DOUBLE) ?
// (llvm::Type *)builder.getDoubleTy() :
// info.type.eql_type(T_USER_TYPE) ?
// (llvm::Type *)f_list.structs.get(info.type.get().user_type)->strct->getPointerTo() :
// (info.type.eql_type(T_ARRAY)) ?
// (info.type.next->eql_type(T_STRING)) ?
// (llvm::Type *)builder.getInt8PtrTy()->getPointerTo() :
// (llvm::Type *)builder.getInt32Ty()->getPointerTo() :
// (llvm::Type *)builder.getInt32Ty();
return out;
}
};