@@ -12,8 +12,10 @@ void IR::addFunction(std::string name, std::vector<Parameter *> parameters,
12
12
retType, isVariadic));
13
13
}
14
14
15
- void IR::addTypeDef (std::string name, std::shared_ptr<Type> type) {
15
+ std::shared_ptr<TypeDef> IR::addTypeDef (std::string name,
16
+ std::shared_ptr<Type> type) {
16
17
typeDefs.push_back (std::make_shared<TypeDef>(std::move (name), type));
18
+ return typeDefs.back ();
17
19
}
18
20
19
21
std::shared_ptr<Type> IR::addEnum (std::string name, const std::string &type,
@@ -28,23 +30,32 @@ std::shared_ptr<Type> IR::addEnum(std::string name, const std::string &type,
28
30
return nullptr ;
29
31
}
30
32
31
- std::shared_ptr<Type> IR::addStruct (std::string name,
32
- std::vector<Field *> fields,
33
- uint64_t typeSize) {
33
+ void IR::addStruct (std::string name, std::vector<Field *> fields,
34
+ uint64_t typeSize) {
34
35
std::shared_ptr<Struct> s =
35
- std::make_shared<Struct>(std::move ( name) , std::move (fields), typeSize);
36
+ std::make_shared<Struct>(name, std::move (fields), typeSize);
36
37
structs.push_back (s);
37
- typeDefs.push_back (s->generateTypeDef ());
38
- return typeDefs.back ();
38
+ std::shared_ptr<TypeDef> typeDef = getTypeDefWithName (" struct_" + name);
39
+ if (typeDef) {
40
+ /* the struct type used to be opaque type, typeDef contains nullptr */
41
+ typeDef.get ()->setType (s);
42
+ } else {
43
+ typeDefs.push_back (s->generateTypeDef ());
44
+ }
39
45
}
40
46
41
- std::shared_ptr<Type>
42
- IR::addUnion (std::string name, std::vector<Field *> fields, uint64_t maxSize) {
47
+ void IR::addUnion ( std::string name, std::vector<Field *> fields,
48
+ uint64_t maxSize) {
43
49
std::shared_ptr<Union> u =
44
- std::make_shared<Union>(std::move ( name) , std::move (fields), maxSize);
50
+ std::make_shared<Union>(name, std::move (fields), maxSize);
45
51
unions.push_back (u);
46
- typeDefs.push_back (u->generateTypeDef ());
47
- return typeDefs.back ();
52
+ std::shared_ptr<TypeDef> typeDef = getTypeDefWithName (" union_" + name);
53
+ if (typeDef) {
54
+ /* the union type used to be opaque type, typeDef contains nullptr */
55
+ typeDef.get ()->setType (u);
56
+ } else {
57
+ typeDefs.push_back (u->generateTypeDef ());
58
+ }
48
59
}
49
60
50
61
void IR::addLiteralDefine (std::string name, std::string literal,
@@ -304,6 +315,11 @@ std::shared_ptr<Variable> IR::addVariable(const std::string &name,
304
315
}
305
316
306
317
std::shared_ptr<TypeDef> IR::getTypeDefWithName (const std::string &name) {
318
+ /* nullptr is returned in 2 cases:
319
+ * 1. TypeTranslator translates opaque struct/union type for which TypeDef
320
+ * was not created.
321
+ * 2. TreeVisitor visits struct/union declaration and it checks whether a
322
+ * TypeDef already exists for it.*/
307
323
return getDeclarationWithName (typeDefs, name);
308
324
}
309
325
@@ -317,7 +333,6 @@ T IR::getDeclarationWithName(std::vector<T> &declarations,
317
333
return declaration;
318
334
}
319
335
}
320
- llvm::errs () << " Failed to get declaration for " << name << " \n " ;
321
336
return nullptr ;
322
337
}
323
338
@@ -331,4 +346,4 @@ IR::~IR() {
331
346
possibleVarDefines.clear ();
332
347
variables.clear ();
333
348
varDefines.clear ();
334
- }
349
+ }
0 commit comments