Skip to content

Commit c244c88

Browse files
committed
Check if TypeDef already exists in addStruct and addUnion methods
1 parent 9834ee7 commit c244c88

File tree

5 files changed

+26
-67
lines changed

5 files changed

+26
-67
lines changed

bindgen/ir/IR.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,32 @@ std::shared_ptr<Type> IR::addEnum(std::string name, const std::string &type,
3030
return nullptr;
3131
}
3232

33-
std::shared_ptr<Type> IR::addStruct(std::string name,
34-
std::vector<Field *> fields,
35-
uint64_t typeSize) {
36-
std::shared_ptr<Struct> s =
37-
std::make_shared<Struct>(std::move(name), std::move(fields), typeSize);
38-
structs.push_back(s);
39-
typeDefs.push_back(s->generateTypeDef());
40-
return typeDefs.back();
41-
}
42-
4333
void IR::addStruct(std::string name, std::vector<Field *> fields,
44-
uint64_t typeSize, const std::shared_ptr<TypeDef> &typeDef) {
34+
uint64_t typeSize) {
4535
std::shared_ptr<Struct> s =
46-
std::make_shared<Struct>(std::move(name), std::move(fields), typeSize);
36+
std::make_shared<Struct>(name, std::move(fields), typeSize);
4737
structs.push_back(s);
48-
typeDef.get()->setType(s);
49-
}
50-
51-
std::shared_ptr<Type>
52-
IR::addUnion(std::string name, std::vector<Field *> fields, uint64_t maxSize) {
53-
std::shared_ptr<Union> u =
54-
std::make_shared<Union>(std::move(name), std::move(fields), maxSize);
55-
unions.push_back(u);
56-
typeDefs.push_back(u->generateTypeDef());
57-
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+
}
5845
}
5946

6047
void IR::addUnion(std::string name, std::vector<Field *> fields,
61-
uint64_t maxSize, const std::shared_ptr<TypeDef> &typeDef) {
48+
uint64_t maxSize) {
6249
std::shared_ptr<Union> u =
63-
std::make_shared<Union>(std::move(name), std::move(fields), maxSize);
50+
std::make_shared<Union>(name, std::move(fields), maxSize);
6451
unions.push_back(u);
65-
typeDef.get()->setType(u);
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+
}
6659
}
6760

6861
void IR::addLiteralDefine(std::string name, std::string literal,

bindgen/ir/IR.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,11 @@ class IR {
3131
std::shared_ptr<Type> addEnum(std::string name, const std::string &type,
3232
std::vector<Enumerator> enumerators);
3333

34-
/**
35-
* @return type alias for the struct
36-
*/
37-
std::shared_ptr<Type>
38-
addStruct(std::string name, std::vector<Field *> fields, uint64_t typeSize);
39-
40-
/**
41-
* Add struct for which TypeDef already exists
42-
*/
4334
void addStruct(std::string name, std::vector<Field *> fields,
44-
uint64_t typeSize, const std::shared_ptr<TypeDef> &typeDef);
35+
uint64_t typeSize);
4536

46-
/**
47-
* @return type alias for the union
48-
*/
49-
std::shared_ptr<Type>
50-
addUnion(std::string name, std::vector<Field *> fields, uint64_t maxSize);
51-
52-
/**
53-
* Add union for which TypeDef already exists
54-
*/
5537
void addUnion(std::string name, std::vector<Field *> fields,
56-
uint64_t maxSize, const std::shared_ptr<TypeDef> &typeDef);
38+
uint64_t maxSize);
5739

5840
void addLiteralDefine(std::string name, std::string literal,
5941
std::shared_ptr<Type> type);

bindgen/visitor/TreeVisitor.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,7 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
114114
fields.push_back(new Field(fname, ftype));
115115
}
116116

117-
std::shared_ptr<Type> alias = nullptr;
118-
119-
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName("union_" + name);
120-
if (typeDef) {
121-
/* typedef for this union already exists */
122-
ir.addUnion(name, std::move(fields), maxSize, typeDef);
123-
alias = typeDef;
124-
} else {
125-
alias = ir.addUnion(name, std::move(fields), maxSize);
126-
}
117+
ir.addUnion(name, std::move(fields), maxSize);
127118
}
128119

129120
void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
@@ -161,16 +152,8 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
161152

162153
uint64_t sizeInBits = astContext->getTypeSize(record->getTypeForDecl());
163154
assert(sizeInBits % 8 == 0);
164-
std::shared_ptr<Type> alias = nullptr;
165-
166-
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(newName);
167-
if (typeDef) {
168-
/* typedef for this struct already exists */
169-
alias = typeDef;
170-
ir.addStruct(name, std::move(fields), sizeInBits / 8, typeDef);
171-
} else {
172-
alias = ir.addStruct(name, std::move(fields), sizeInBits / 8);
173-
}
155+
156+
ir.addStruct(name, std::move(fields), sizeInBits / 8);
174157
}
175158

176159
bool TreeVisitor::VisitVarDecl(clang::VarDecl *varDecl) {

tests/samples/OpaqueTypes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
typedef struct points points;
2+
13
struct point;
24
struct point *move(struct point *point, int x, int y);
35

4-
typedef struct points points;
56
typedef union u u;
67

78
union u *processPoints(points *p);

tests/samples/OpaqueTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import scala.scalanative.native._
66
@native.link("bindgentests")
77
@native.extern
88
object OpaqueTypes {
9-
type struct_point = native.CStruct2[native.CInt, native.CInt]
109
type struct_points = native.CStruct2[native.Ptr[struct_point], native.Ptr[struct_point]]
1110
type points = struct_points
11+
type struct_point = native.CStruct2[native.CInt, native.CInt]
1212
type union_u = native.CArray[Byte, native.Nat._4]
1313
type u = union_u
1414
def move(point: native.Ptr[struct_point], x: native.CInt, y: native.CInt): native.Ptr[struct_point] = native.extern

0 commit comments

Comments
 (0)