Skip to content

Commit 786e311

Browse files
committed
Use OpaqueType when type is not yet defined
1 parent fb3c4de commit 786e311

22 files changed

+186
-33
lines changed

bindgen/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ add_executable(bindgen
7272
ir/types/FunctionPointerType.h
7373
ir/types/ArrayType.cpp
7474
ir/types/ArrayType.h
75+
ir/types/OpaqueType.cpp
76+
ir/types/OpaqueType.h
7577
)
7678

7779
if (STATIC_LINKING)

bindgen/CycleDetection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include "TypeTranslator.h"
4+
#include "Utils.h"
5+
#include "ir/types/OpaqueType.h"
46

57
#include <map>
68
#include <set>
@@ -35,7 +37,7 @@ class CycleDetection {
3537
}
3638

3739
std::shared_ptr<Type> type = tpeTransl.translate(qtpe);
38-
if (type == nullptr) {
40+
if (isInstanceOf<OpaqueType>(type.get())) {
3941
return;
4042
}
4143

bindgen/TypeTranslator.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "TypeTranslator.h"
22
#include "Utils.h"
33
#include "ir/types/FunctionPointerType.h"
4+
#include "ir/types/OpaqueType.h"
45
#include "ir/types/PointerType.h"
56

67
TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
@@ -93,8 +94,17 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
9394
* Use type alias instead struct type */
9495
return (*it).second;
9596
}
96-
/* type has typedef alias */
97-
return ir.getTypeDefWithName(name);
97+
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(name);
98+
if (typeDef) {
99+
/* type has typedef alias */
100+
return typeDef;
101+
}
102+
/* type is not yet defined.
103+
* use OpaqueType that will be replaced by actual type */
104+
typeDef = ir.addTypeDef(replaceChar(name, " ", "_"),
105+
std::make_shared<OpaqueType>(name));
106+
addAlias(name, typeDef);
107+
return typeDef;
98108
}
99109

100110
std::shared_ptr<Type>

bindgen/Utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,13 @@ template <typename T, typename PT> static inline bool isInstanceOf(PT *type) {
100100
return p != nullptr;
101101
}
102102

103+
static inline std::string replaceChar(std::string str, const std::string &c1,
104+
const std::string &c2) {
105+
auto f = str.find(c1);
106+
if (f != std::string::npos) {
107+
return str.replace(f, c1.length(), c2);
108+
}
109+
return str;
110+
}
111+
103112
#endif // UTILS_H

bindgen/ir/Function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Parameter::Parameter(std::string name, std::shared_ptr<Type> type)
77
Function::Function(const std::string &name, std::vector<Parameter *> parameters,
88
std::shared_ptr<Type> retType, bool isVariadic)
99
: name(name), scalaName(name), parameters(std::move(parameters)),
10-
retType(retType), isVariadic(isVariadic) {}
10+
retType(std::move(retType)), isVariadic(isVariadic) {}
1111

1212
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
1313
if (func.scalaName != func.name) {

bindgen/ir/IR.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1921
std::shared_ptr<Type> IR::addEnum(std::string name, const std::string &type,
@@ -38,6 +40,14 @@ std::shared_ptr<Type> IR::addStruct(std::string name,
3840
return typeDefs.back();
3941
}
4042

43+
void IR::addStruct(std::string name, std::vector<Field *> fields,
44+
uint64_t typeSize, const std::shared_ptr<TypeDef> &typeDef) {
45+
std::shared_ptr<Struct> s =
46+
std::make_shared<Struct>(std::move(name), std::move(fields), typeSize);
47+
structs.push_back(s);
48+
typeDef.get()->setType(s);
49+
}
50+
4151
std::shared_ptr<Type>
4252
IR::addUnion(std::string name, std::vector<Field *> fields, uint64_t maxSize) {
4353
std::shared_ptr<Union> u =
@@ -47,6 +57,14 @@ IR::addUnion(std::string name, std::vector<Field *> fields, uint64_t maxSize) {
4757
return typeDefs.back();
4858
}
4959

60+
void IR::addUnion(std::string name, std::vector<Field *> fields,
61+
uint64_t maxSize, const std::shared_ptr<TypeDef> &typeDef) {
62+
std::shared_ptr<Union> u =
63+
std::make_shared<Union>(std::move(name), std::move(fields), maxSize);
64+
unions.push_back(u);
65+
typeDef.get()->setType(u);
66+
}
67+
5068
void IR::addLiteralDefine(std::string name, std::string literal,
5169
std::shared_ptr<Type> type) {
5270
literalDefines.push_back(std::make_shared<LiteralDefine>(
@@ -317,7 +335,6 @@ T IR::getDeclarationWithName(std::vector<T> &declarations,
317335
return declaration;
318336
}
319337
}
320-
llvm::errs() << "Failed to get declaration for " << name << "\n";
321338
return nullptr;
322339
}
323340

@@ -331,4 +348,4 @@ IR::~IR() {
331348
possibleVarDefines.clear();
332349
variables.clear();
333350
varDefines.clear();
334-
}
351+
}

bindgen/ir/IR.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class IR {
2222
void addFunction(std::string name, std::vector<Parameter *> parameters,
2323
std::shared_ptr<Type> retType, bool isVariadic);
2424

25-
void addTypeDef(std::string name, std::shared_ptr<Type> type);
25+
std::shared_ptr<TypeDef> addTypeDef(std::string name,
26+
std::shared_ptr<Type> type);
2627

2728
/**
2829
* @return type alias for the enum
@@ -36,12 +37,24 @@ class IR {
3637
std::shared_ptr<Type>
3738
addStruct(std::string name, std::vector<Field *> fields, uint64_t typeSize);
3839

40+
/**
41+
* Add struct for which TypeDef already exists
42+
*/
43+
void addStruct(std::string name, std::vector<Field *> fields,
44+
uint64_t typeSize, const std::shared_ptr<TypeDef> &typeDef);
45+
3946
/**
4047
* @return type alias for the union
4148
*/
4249
std::shared_ptr<Type>
4350
addUnion(std::string name, std::vector<Field *> fields, uint64_t maxSize);
4451

52+
/**
53+
* Add union for which TypeDef already exists
54+
*/
55+
void addUnion(std::string name, std::vector<Field *> fields,
56+
uint64_t maxSize, const std::shared_ptr<TypeDef> &typeDef);
57+
4558
void addLiteralDefine(std::string name, std::string literal,
4659
std::shared_ptr<Type> type);
4760

@@ -138,8 +151,6 @@ class IR {
138151
T getDeclarationWithName(std::vector<T> &declarations,
139152
const std::string &name);
140153

141-
template <typename T> void clearVector(std::vector<T> v);
142-
143154
std::string libName; // name of the library
144155
std::string linkName; // name of the library to link with
145156
std::string objectName; // name of Scala object

bindgen/ir/Struct.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "types/ArrayType.h"
44
#include "types/PrimitiveType.h"
55
#include <sstream>
6-
#include <utility>
76

87
Field::Field(std::string name, std::shared_ptr<Type> type)
98
: TypeAndName(std::move(name), std::move(type)) {}

bindgen/ir/TypeAndName.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <clang/Tooling/Tooling.h>
33

44
TypeAndName::TypeAndName(std::string name, std::shared_ptr<Type> type)
5-
: name(std::move(name)), type(type) {}
5+
: name(std::move(name)), type(std::move(type)) {}
66

77
std::shared_ptr<Type> TypeAndName::getType() const { return type; }
88

bindgen/ir/types/ArrayType.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "../../Utils.h"
33

44
ArrayType::ArrayType(std::shared_ptr<Type> elementsType, uint64_t size)
5-
: size(size), elementsType(elementsType) {}
5+
: size(size), elementsType(std::move(elementsType)) {}
66

77
std::string ArrayType::str() const {
88
return "native.CArray[" + elementsType->str() + ", " +
@@ -12,5 +12,3 @@ std::string ArrayType::str() const {
1212
bool ArrayType::usesType(std::shared_ptr<Type> type) const {
1313
return this == type.get() || elementsType == type;
1414
}
15-
16-
ArrayType::~ArrayType() {}

0 commit comments

Comments
 (0)