-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTypeTranslator.cpp
259 lines (217 loc) · 9.06 KB
/
TypeTranslator.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
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
#include "TypeTranslator.h"
#include "Utils.h"
#include "ir/types/FunctionPointerType.h"
#include "clang/AST/RecordLayout.h"
TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
: ctx(ctx_), ir(ir), typeMap() {
// Native Types
typeMap["void"] = "Unit";
typeMap["bool"] = "CBool";
typeMap["_Bool"] = "CBool";
typeMap["char"] = "CChar";
typeMap["signed char"] = "CSignedChar";
typeMap["unsigned char"] = "CUnsignedChar";
typeMap["short"] = "CShort";
typeMap["unsigned short"] = "CUnsignedShort";
typeMap["int"] = "CInt";
typeMap["long int"] = "CLongInt";
typeMap["unsigned int"] = "CUnsignedInt";
typeMap["unsigned long int"] = "CUnsignedLongInt";
typeMap["long"] = "CLong";
typeMap["unsigned long"] = "CUnsignedLong";
typeMap["long long"] = "CLongLong";
typeMap["unsigned long long"] = "CUnsignedLongLong";
typeMap["size_t"] = "CSize";
typeMap["ptrdiff_t"] = "CPtrDiff";
typeMap["wchar_t"] = "CWideChar";
typeMap["char16_t"] = "CChar16";
typeMap["char32_t"] = "CChar32";
typeMap["float"] = "CFloat";
typeMap["double"] = "CDouble";
typeMap["long double"] = "CDouble";
}
std::shared_ptr<Type>
TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe) {
const auto *ptr = qtpe.getTypePtr()->getAs<clang::PointerType>();
const clang::QualType &inner = ptr->getPointeeType();
if (inner->isFunctionProtoType()) {
const auto *fc = inner->getAs<clang::FunctionProtoType>();
std::shared_ptr<Type> returnType = translate(fc->getReturnType());
std::vector<std::shared_ptr<const Type>> parametersTypes;
for (const clang::QualType ¶m : fc->param_types()) {
parametersTypes.push_back(translate(param));
}
return std::make_shared<FunctionPointerType>(
returnType, parametersTypes, fc->isVariadic());
} else {
llvm::errs() << "Unsupported function pointer type: "
<< qtpe.getAsString() << "\n";
llvm::errs().flush();
exit(-1);
}
}
std::shared_ptr<Type>
TypeTranslator::translatePointer(const clang::QualType &pte) {
if (pte->isBuiltinType()) {
const clang::BuiltinType *as = pte->getAs<clang::BuiltinType>();
// Take care of void*
if (as->getKind() == clang::BuiltinType::Void) {
return std::make_shared<PointerType>(
std::make_shared<PrimitiveType>("Byte"));
}
// Take care of char*
if (as->getKind() == clang::BuiltinType::Char_S ||
as->getKind() == clang::BuiltinType::SChar) {
// TODO: new PointerType(new PrimitiveType("CChar"))
return std::make_shared<PrimitiveType>("CString");
}
}
return std::make_shared<PointerType>(translate(pte));
}
std::shared_ptr<Type>
TypeTranslator::translateNonAnonymousRecord(const clang::QualType &qtpe) {
std::string name = qtpe.getUnqualifiedType().getAsString();
/* If the struct was already declared then there is a TypeDef instance
* with appropriate name.
*
* If there is no such TypeDef then the type is opaque and TypeDef with
* nullptr will be generated for the type. */
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(name);
if (typeDef) {
return typeDef;
}
/* type is not yet defined.
* TypeDef with nullptr will be created.
* nullptr will be replaced by actual type when the type is declared. */
typeDef = ir.addTypeDef(name, nullptr, nullptr);
return typeDef;
}
std::shared_ptr<Type>
TypeTranslator::translateRecord(const clang::QualType &qtpe) {
if (qtpe->hasUnnamedOrLocalType()) {
if (qtpe->isStructureType()) {
std::string name =
"anonymous_" + std::to_string(anonymousStructId++);
clang::RecordDecl *record = qtpe->getAsStructureType()->getDecl();
return addStructDefinition(record, name);
} else if (qtpe->isUnionType()) {
std::string name =
"anonymous_" + std::to_string(anonymousUnionId++);
clang::RecordDecl *record = qtpe->getAsUnionType()->getDecl();
return addUnionDefinition(record, name);
}
return nullptr;
}
return translateNonAnonymousRecord(qtpe);
}
std::shared_ptr<Type>
TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar) {
const uint64_t size = ar->getSize().getZExtValue();
std::shared_ptr<Type> elementType = translate(ar->getElementType());
if (elementType == nullptr) {
llvm::errs() << "Failed to translate array type "
<< ar->getElementType().getAsString() << "\n";
elementType = std::make_shared<PrimitiveType>("Byte");
}
return std::make_shared<ArrayType>(elementType, size);
}
std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe) {
const clang::Type *tpe = qtpe.getTypePtr();
if (tpe->isFunctionType()) {
return nullptr;
}
if (tpe->isFunctionPointerType()) {
return translateFunctionPointer(qtpe);
} else if (tpe->isPointerType()) {
return translatePointer(
tpe->getAs<clang::PointerType>()->getPointeeType());
} else if (qtpe->isStructureType() || qtpe->isUnionType()) {
return translateRecord(qtpe);
} else if (qtpe->isEnumeralType()) {
return translateEnum(qtpe);
} else if (qtpe->isConstantArrayType()) {
return translateConstantArray(ctx->getAsConstantArrayType(qtpe));
} else if (qtpe->isArrayType()) {
return translatePointer(ctx->getAsArrayType(qtpe)->getElementType());
} else {
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
if (found != typeMap.end()) {
return std::make_shared<PrimitiveType>(found->second);
} else {
return ir.getTypeDefWithName(
qtpe.getUnqualifiedType().getAsString());
}
}
}
std::string TypeTranslator::getTypeFromTypeMap(std::string cType) {
auto it = typeMap.find(cType);
if (it != typeMap.end()) {
return (*it).second;
}
return "";
}
std::shared_ptr<Location> TypeTranslator::getLocation(clang::Decl *decl) {
clang::SourceManager &sm = ctx->getSourceManager();
std::string filename = std::string(sm.getFilename(decl->getLocation()));
std::string path = getRealPath(filename.c_str());
unsigned lineNumber = sm.getSpellingLineNumber(decl->getLocation());
return std::make_shared<Location>(path, lineNumber);
}
std::shared_ptr<TypeDef>
TypeTranslator::addUnionDefinition(clang::RecordDecl *record,
std::string name) {
std::vector<std::shared_ptr<Field>> fields;
for (const clang::FieldDecl *field : record->fields()) {
std::string fname = field->getNameAsString();
std::shared_ptr<Type> ftype = translate(field->getType());
fields.push_back(std::make_shared<Field>(fname, ftype));
}
uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());
assert(sizeInBits % 8 == 0);
return ir.addUnion(name, std::move(fields), sizeInBits / 8,
getLocation(record));
}
std::shared_ptr<TypeDef>
TypeTranslator::addStructDefinition(clang::RecordDecl *record,
std::string name) {
if (record->hasAttr<clang::PackedAttr>()) {
llvm::errs() << "Warning: struct " << name << " is packed. "
<< "Packed structs are not supported by Scala Native. "
<< "Access to fields will not work correctly.\n";
llvm::errs().flush();
}
std::vector<std::shared_ptr<Field>> fields;
const clang::ASTRecordLayout &recordLayout =
ctx->getASTRecordLayout(record);
bool isBitFieldStruct = false;
for (const clang::FieldDecl *field : record->fields()) {
if (field->isBitField()) {
isBitFieldStruct = true;
}
std::shared_ptr<Type> ftype = translate(field->getType());
uint64_t recordOffsetInBits =
recordLayout.getFieldOffset(field->getFieldIndex());
fields.push_back(std::make_shared<Field>(field->getNameAsString(),
ftype, recordOffsetInBits));
}
uint64_t sizeInBits = ctx->getTypeSize(record->getTypeForDecl());
assert(sizeInBits % 8 == 0);
return ir.addStruct(name, std::move(fields), sizeInBits / 8,
getLocation(record),
record->hasAttr<clang::PackedAttr>(), isBitFieldStruct);
}
std::shared_ptr<Type>
TypeTranslator::translateEnum(const clang::QualType &type) {
clang::EnumDecl *enumDecl = type->getAs<clang::EnumType>()->getDecl();
if (type->hasUnnamedOrLocalType()) {
return std::make_shared<PrimitiveType>(getTypeFromTypeMap(
enumDecl->getIntegerType().getUnqualifiedType().getAsString()));
}
std::string name = enumDecl->getNameAsString();
if (name.empty()) {
name = enumDecl->getTypedefNameForAnonDecl()->getNameAsString();
return ir.getTypeDefWithName(name);
}
assert(!name.empty());
return ir.getEnumWithName(name);
}