Skip to content

Commit 4bc278e

Browse files
committed
Support breaking cycles on complex types
Remove `avoid` parameter in TypeTranslator methods
1 parent e7d4728 commit 4bc278e

22 files changed

+579
-272
lines changed

bindgen/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ add_executable(bindgen
7373
ir/Location.cpp
7474
ir/LocationManager.h
7575
ir/LocationManager.cpp
76-
ir/CycleNode.cpp
77-
ir/CycleNode.h
7876
)
7977

8078
if (STATIC_LINKING)

bindgen/TypeTranslator.cpp

+12-27
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
3434
}
3535

3636
std::shared_ptr<Type>
37-
TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
38-
const std::string *avoid) {
37+
TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe) {
3938
const auto *ptr = qtpe.getTypePtr()->getAs<clang::PointerType>();
4039
const clang::QualType &inner = ptr->getPointeeType();
4140

4241
if (inner->isFunctionProtoType()) {
4342
const auto *fc = inner->getAs<clang::FunctionProtoType>();
44-
std::shared_ptr<Type> returnType =
45-
translate(fc->getReturnType(), avoid);
43+
std::shared_ptr<Type> returnType = translate(fc->getReturnType());
4644
std::vector<std::shared_ptr<const Type>> parametersTypes;
4745

4846
for (const clang::QualType &param : fc->param_types()) {
49-
parametersTypes.push_back(translate(param, avoid));
47+
parametersTypes.push_back(translate(param));
5048
}
5149

5250
return std::make_shared<FunctionPointerType>(
@@ -61,8 +59,7 @@ TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
6159
}
6260

6361
std::shared_ptr<Type>
64-
TypeTranslator::translatePointer(const clang::QualType &pte,
65-
const std::string *avoid) {
62+
TypeTranslator::translatePointer(const clang::QualType &pte) {
6663

6764
if (pte->isBuiltinType()) {
6865
const clang::BuiltinType *as = pte->getAs<clang::BuiltinType>();
@@ -81,7 +78,7 @@ TypeTranslator::translatePointer(const clang::QualType &pte,
8178
}
8279
}
8380

84-
return std::make_shared<PointerType>(translate(pte, avoid));
81+
return std::make_shared<PointerType>(translate(pte));
8582
}
8683

8784
std::shared_ptr<Type>
@@ -120,10 +117,9 @@ TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
120117
}
121118

122119
std::shared_ptr<Type>
123-
TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar,
124-
const std::string *avoid) {
120+
TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar) {
125121
const uint64_t size = ar->getSize().getZExtValue();
126-
std::shared_ptr<Type> elementType = translate(ar->getElementType(), avoid);
122+
std::shared_ptr<Type> elementType = translate(ar->getElementType());
127123
if (elementType == nullptr) {
128124
llvm::errs() << "Failed to translate array type "
129125
<< ar->getElementType().getAsString() << "\n";
@@ -133,30 +129,20 @@ TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar,
133129
return std::make_shared<ArrayType>(elementType, size);
134130
}
135131

136-
std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
137-
const std::string *avoid) {
132+
std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe) {
138133

139134
const clang::Type *tpe = qtpe.getTypePtr();
140135

141-
if (typeEquals(tpe, avoid)) {
142-
// This is a type that we want to avoid the usage.
143-
// Êxample: A struct that has a pointer to itself
144-
uint64_t sizeInBits = ctx->getTypeSize(tpe);
145-
assert(sizeInBits % 8 == 0);
146-
return std::make_shared<ArrayType>(
147-
std::make_shared<PrimitiveType>("Byte"), sizeInBits / 8);
148-
}
149-
150136
if (tpe->isFunctionType()) {
151137
return nullptr;
152138
}
153139

154140
if (tpe->isFunctionPointerType()) {
155-
return translateFunctionPointer(qtpe, avoid);
141+
return translateFunctionPointer(qtpe);
156142

157143
} else if (tpe->isPointerType()) {
158144
return translatePointer(
159-
tpe->getAs<clang::PointerType>()->getPointeeType(), avoid);
145+
tpe->getAs<clang::PointerType>()->getPointeeType());
160146

161147
} else if (qtpe->isStructureType()) {
162148
return translateStructOrUnion(qtpe);
@@ -168,10 +154,9 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
168154
return translateStructOrUnionOrEnum(qtpe);
169155

170156
} else if (qtpe->isConstantArrayType()) {
171-
return translateConstantArray(ctx->getAsConstantArrayType(qtpe), avoid);
157+
return translateConstantArray(ctx->getAsConstantArrayType(qtpe));
172158
} else if (qtpe->isArrayType()) {
173-
return translatePointer(ctx->getAsArrayType(qtpe)->getElementType(),
174-
avoid);
159+
return translatePointer(ctx->getAsArrayType(qtpe)->getElementType());
175160
} else {
176161

177162
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());

bindgen/TypeTranslator.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ class TypeTranslator {
1010
/**
1111
* @brief Translate the qualified type from c to a scala type
1212
* @param tpe The type to translate
13-
* @param avoid A type to avoid, useful to avoid cyclic definitions inside
14-
* structs, unions, ...
1513
* @return the type translated or nullptr if type is function type.
1614
*/
17-
std::shared_ptr<Type> translate(const clang::QualType &tpe,
18-
const std::string *avoid = nullptr);
15+
std::shared_ptr<Type> translate(const clang::QualType &tpe);
1916

2017
std::string getTypeFromTypeMap(std::string cType);
2118

@@ -33,13 +30,10 @@ class TypeTranslator {
3330

3431
std::shared_ptr<Type> translateStructOrUnion(const clang::QualType &qtpe);
3532

36-
std::shared_ptr<Type> translateFunctionPointer(const clang::QualType &qtpe,
37-
const std::string *avoid);
33+
std::shared_ptr<Type> translateFunctionPointer(const clang::QualType &qtpe);
3834

39-
std::shared_ptr<Type> translatePointer(const clang::QualType &pointee,
40-
const std::string *avoid);
35+
std::shared_ptr<Type> translatePointer(const clang::QualType &pointee);
4136

4237
std::shared_ptr<Type>
43-
translateConstantArray(const clang::ConstantArrayType *ar,
44-
const std::string *avoid);
38+
translateConstantArray(const clang::ConstantArrayType *ar);
4539
};

bindgen/ir/CycleNode.cpp

-4
This file was deleted.

bindgen/ir/CycleNode.h

-17
This file was deleted.

0 commit comments

Comments
 (0)