1
1
#include " TypeTranslator.h"
2
2
#include " Utils.h"
3
3
#include " ir/types/FunctionPointerType.h"
4
- #include " ir/types/PointerType .h"
4
+ #include " clang/AST/RecordLayout .h"
5
5
6
6
TypeTranslator::TypeTranslator (clang::ASTContext *ctx_, IR &ir)
7
7
: ctx(ctx_), ir(ir), typeMap() {
@@ -106,13 +106,19 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
106
106
std::shared_ptr<Type>
107
107
TypeTranslator::translateStructOrUnion (const clang::QualType &qtpe) {
108
108
if (qtpe->hasUnnamedOrLocalType ()) {
109
- // TODO: Verify that the local part is not a problem
110
- uint64_t sizeInBits = ctx->getTypeSize (qtpe);
111
- assert (sizeInBits % 8 == 0 );
112
- return std::make_shared<ArrayType>(
113
- std::make_shared<PrimitiveType>(" Byte" ), sizeInBits / 8 );
109
+ if (qtpe->isStructureType ()) {
110
+ std::string name =
111
+ " anonymous_" + std::to_string (anonymousStructId++);
112
+ clang::RecordDecl *record = qtpe->getAsStructureType ()->getDecl ();
113
+ return addStructDefinition (record, name);
114
+ } else if (qtpe->isUnionType ()) {
115
+ std::string name =
116
+ " anonymous_" + std::to_string (anonymousUnionId++);
117
+ clang::RecordDecl *record = qtpe->getAsUnionType ()->getDecl ();
118
+ return addUnionDefinition (record, name);
119
+ }
120
+ return nullptr ;
114
121
}
115
-
116
122
return translateStructOrUnionOrEnum (qtpe);
117
123
}
118
124
@@ -144,14 +150,11 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe) {
144
150
return translatePointer (
145
151
tpe->getAs <clang::PointerType>()->getPointeeType ());
146
152
147
- } else if (qtpe->isStructureType ()) {
148
- return translateStructOrUnion (qtpe);
149
-
150
- } else if (qtpe->isUnionType ()) {
153
+ } else if (qtpe->isStructureType () || qtpe->isUnionType ()) {
151
154
return translateStructOrUnion (qtpe);
152
155
153
156
} else if (qtpe->isEnumeralType ()) {
154
- return translateStructOrUnionOrEnum (qtpe);
157
+ return translateEnum (qtpe);
155
158
156
159
} else if (qtpe->isConstantArrayType ()) {
157
160
return translateConstantArray (ctx->getAsConstantArrayType (qtpe));
@@ -176,3 +179,77 @@ std::string TypeTranslator::getTypeFromTypeMap(std::string cType) {
176
179
}
177
180
return " " ;
178
181
}
182
+
183
+ std::shared_ptr<Location> TypeTranslator::getLocation (clang::Decl *decl) {
184
+ clang::SourceManager &sm = ctx->getSourceManager ();
185
+ std::string filename = std::string (sm.getFilename (decl->getLocation ()));
186
+ std::string path = getRealPath (filename.c_str ());
187
+
188
+ unsigned lineNumber = sm.getSpellingLineNumber (decl->getLocation ());
189
+ return std::make_shared<Location>(path, lineNumber);
190
+ }
191
+
192
+ std::shared_ptr<TypeDef>
193
+ TypeTranslator::addUnionDefinition (clang::RecordDecl *record,
194
+ std::string name) {
195
+ std::vector<std::shared_ptr<Field>> fields;
196
+
197
+ for (const clang::FieldDecl *field : record->fields ()) {
198
+ std::string fname = field->getNameAsString ();
199
+ std::shared_ptr<Type> ftype = translate (field->getType ());
200
+
201
+ fields.push_back (std::make_shared<Field>(fname, ftype));
202
+ }
203
+
204
+ uint64_t sizeInBits = ctx->getTypeSize (record->getTypeForDecl ());
205
+ assert (sizeInBits % 8 == 0 );
206
+
207
+ return ir.addUnion (name, std::move (fields), sizeInBits / 8 ,
208
+ getLocation (record));
209
+ }
210
+
211
+ std::shared_ptr<TypeDef>
212
+ TypeTranslator::addStructDefinition (clang::RecordDecl *record,
213
+ std::string name) {
214
+ std::string newName = " struct_" + name;
215
+
216
+ if (record->hasAttr <clang::PackedAttr>()) {
217
+ llvm::errs () << " Warning: struct " << name << " is packed. "
218
+ << " Packed structs are not supported by Scala Native. "
219
+ << " Access to fields will not work correctly.\n " ;
220
+ llvm::errs ().flush ();
221
+ }
222
+
223
+ std::vector<std::shared_ptr<Field>> fields;
224
+ const clang::ASTRecordLayout &recordLayout =
225
+ ctx->getASTRecordLayout (record);
226
+
227
+ bool isBitFieldStruct = false ;
228
+ for (const clang::FieldDecl *field : record->fields ()) {
229
+ if (field->isBitField ()) {
230
+ isBitFieldStruct = true ;
231
+ }
232
+ std::shared_ptr<Type> ftype = translate (field->getType ());
233
+ uint64_t recordOffsetInBits =
234
+ recordLayout.getFieldOffset (field->getFieldIndex ());
235
+ fields.push_back (std::make_shared<Field>(field->getNameAsString (),
236
+ ftype, recordOffsetInBits));
237
+ }
238
+
239
+ uint64_t sizeInBits = ctx->getTypeSize (record->getTypeForDecl ());
240
+ assert (sizeInBits % 8 == 0 );
241
+
242
+ return ir.addStruct (name, std::move (fields), sizeInBits / 8 ,
243
+ getLocation (record),
244
+ record->hasAttr <clang::PackedAttr>(), isBitFieldStruct);
245
+ }
246
+
247
+ std::shared_ptr<Type>
248
+ TypeTranslator::translateEnum (const clang::QualType &type) {
249
+ if (type->hasUnnamedOrLocalType ()) {
250
+ clang::EnumDecl *enumDecl = type->getAs <clang::EnumType>()->getDecl ();
251
+ return std::make_shared<PrimitiveType>(getTypeFromTypeMap (
252
+ enumDecl->getIntegerType ().getUnqualifiedType ().getAsString ()));
253
+ }
254
+ return translateStructOrUnionOrEnum (type);
255
+ }
0 commit comments