@@ -12,8 +12,10 @@ void IR::addFunction(std::string name, std::vector<Parameter *> parameters,
1212 retType, isVariadic));
1313}
1414
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) {
1617 typeDefs.push_back (std::make_shared<TypeDef>(std::move (name), type));
18+ return typeDefs.back ();
1719}
1820
1921std::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,
2830 return nullptr ;
2931}
3032
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) {
3435 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);
3637 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+ }
3945}
4046
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) {
4349 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);
4551 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+ }
4859}
4960
5061void IR::addLiteralDefine (std::string name, std::string literal,
@@ -304,6 +315,11 @@ std::shared_ptr<Variable> IR::addVariable(const std::string &name,
304315}
305316
306317std::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.*/
307323 return getDeclarationWithName (typeDefs, name);
308324}
309325
@@ -317,7 +333,6 @@ T IR::getDeclarationWithName(std::vector<T> &declarations,
317333 return declaration;
318334 }
319335 }
320- llvm::errs () << " Failed to get declaration for " << name << " \n " ;
321336 return nullptr ;
322337}
323338
@@ -331,4 +346,4 @@ IR::~IR() {
331346 possibleVarDefines.clear ();
332347 variables.clear ();
333348 varDefines.clear ();
334- }
349+ }
0 commit comments