Skip to content

Commit 4d5fe4b

Browse files
committed
handleReservedWords only where needed and small fixes
1 parent 8db4611 commit 4d5fe4b

20 files changed

+63
-107
lines changed

bindgen/CycleDetection.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,31 @@ class CycleDetection {
1010
private:
1111
TypeTranslator &tpeTransl;
1212

13-
bool contains(std::string &k) { return !!dependencies.count(k); }
13+
bool contains(const std::string &k) const {
14+
return dependencies.count(k) != 0;
15+
}
1416

1517
public:
1618
std::map<std::string, std::set<std::string>> dependencies;
17-
CycleDetection(TypeTranslator &tpeTransl_)
19+
20+
explicit CycleDetection(TypeTranslator &tpeTransl_)
1821
: tpeTransl(tpeTransl_), dependencies{} {}
1922

20-
void AddDependcy(std::string name, const clang::QualType &qtpe) {
23+
void AddDependency(const std::string &name, const clang::QualType &qtpe) {
2124

22-
// TODO: function pointer
25+
if (qtpe->isFunctionPointerType()) {
26+
// TODO: function pointer
27+
/* type translator cannot translate function type */
28+
return;
29+
}
2330

2431
if (qtpe->isPointerType()) {
25-
const clang::PointerType *ptr =
26-
qtpe.getTypePtr()->getAs<clang::PointerType>();
27-
AddDependcy(name, ptr->getPointeeType());
32+
const auto *ptr = qtpe.getTypePtr()->getAs<clang::PointerType>();
33+
AddDependency(name, ptr->getPointeeType());
2834
return;
2935
}
3036

3137
Type *type = tpeTransl.translate(qtpe);
32-
if (type == nullptr) {
33-
/* temp fix for function pointer */
34-
return;
35-
}
3638
std::string qtpeString = type->str();
3739

3840
// Add the dependence of qtpe
@@ -44,7 +46,7 @@ class CycleDetection {
4446
dependencies[name].insert(qtpeString);
4547
}
4648

47-
bool isCyclic(std::string &name) {
49+
bool isCyclic(const std::string &name) {
4850
if (contains(name)) {
4951
if (dependencies[name].count(name) != 0) {
5052
return true;

bindgen/TypeTranslator.cpp

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Type *TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
5757
}
5858
}
5959

60-
Type *TypeTranslator::TranslatePointer(const clang::QualType &pte,
60+
Type *TypeTranslator::translatePointer(const clang::QualType &pte,
6161
const std::string *avoid) {
6262

6363
if (pte->isBuiltinType()) {
@@ -71,28 +71,18 @@ Type *TypeTranslator::TranslatePointer(const clang::QualType &pte,
7171
// Take care of char*
7272
if (as->getKind() == clang::BuiltinType::Char_S ||
7373
as->getKind() == clang::BuiltinType::SChar) {
74+
// TODO: new PointerType(new PrimitiveType("native.CChar"))
7475
return new PrimitiveType("native.CString");
7576
}
7677
}
7778

7879
return new PointerType(translate(pte, avoid));
7980
}
8081

81-
Type *TypeTranslator::translateStruct(const clang::QualType &qtpe) {
82-
if (qtpe->hasUnnamedOrLocalType()) {
83-
// TODO: Verify that the local part is not a problem
84-
uint64_t size = ctx->getTypeSize(qtpe);
85-
return new ArrayType(new PrimitiveType("Byte"), size);
86-
}
87-
82+
Type *
83+
TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
8884
std::string name = qtpe.getUnqualifiedType().getAsString();
8985

90-
// TODO: do it properly
91-
size_t f = name.find(std::string("struct __dirstream"));
92-
if (f != std::string::npos) {
93-
return new ArrayType(new PrimitiveType("Byte"), 320);
94-
}
95-
9686
auto it = aliasesMap.find(name);
9787
if (it != aliasesMap.end()) {
9888
/* name contains space: struct <name>.
@@ -103,36 +93,14 @@ Type *TypeTranslator::translateStruct(const clang::QualType &qtpe) {
10393
return ir.getTypeDefWithName(name);
10494
}
10595

106-
Type *TypeTranslator::translateUnion(const clang::QualType &qtpe) {
96+
Type *TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
10797
if (qtpe->hasUnnamedOrLocalType()) {
10898
// TODO: Verify that the local part is not a problem
10999
uint64_t size = ctx->getTypeSize(qtpe);
110100
return new ArrayType(new PrimitiveType("Byte"), size);
111101
}
112102

113-
std::string name = qtpe.getUnqualifiedType().getAsString();
114-
115-
auto it = aliasesMap.find(name);
116-
if (it != aliasesMap.end()) {
117-
/* name contains space: union <name>.
118-
* Use type alias instead union type */
119-
return (*it).second;
120-
}
121-
/* type has typedef alias */
122-
return ir.getTypeDefWithName(name);
123-
}
124-
125-
Type *TypeTranslator::translateEnum(const clang::QualType &qtpe) {
126-
std::string name = qtpe.getUnqualifiedType().getAsString();
127-
128-
auto it = aliasesMap.find(name);
129-
if (it != aliasesMap.end()) {
130-
/* name contains space: enum <name>.
131-
* Use type alias instead enum type */
132-
return (*it).second;
133-
}
134-
/* type has typedef alias */
135-
return ir.getTypeDefWithName(name);
103+
return translateStructOrUnionOrEnum(qtpe);
136104
}
137105

138106
Type *TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar,
@@ -157,22 +125,22 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
157125
return translateFunctionPointer(qtpe, avoid);
158126

159127
} else if (tpe->isPointerType()) {
160-
return TranslatePointer(
128+
return translatePointer(
161129
tpe->getAs<clang::PointerType>()->getPointeeType(), avoid);
162130

163131
} else if (qtpe->isStructureType()) {
164-
return translateStruct(qtpe);
132+
return translateStructOrUnion(qtpe);
165133

166134
} else if (qtpe->isUnionType()) {
167-
return translateUnion(qtpe);
135+
return translateStructOrUnion(qtpe);
168136

169137
} else if (qtpe->isEnumeralType()) {
170-
return translateEnum(qtpe);
138+
return translateStructOrUnionOrEnum(qtpe);
171139

172140
} else if (qtpe->isConstantArrayType()) {
173141
return translateConstantArray(ctx->getAsConstantArrayType(qtpe), avoid);
174142
} else if (qtpe->isArrayType()) {
175-
return TranslatePointer(ctx->getAsArrayType(qtpe)->getElementType(),
143+
return translatePointer(ctx->getAsArrayType(qtpe)->getElementType(),
176144
avoid);
177145
} else {
178146

bindgen/TypeTranslator.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ class TypeTranslator {
3434
*/
3535
std::map<std::string, Type *> aliasesMap;
3636

37-
Type *translateEnum(const clang::QualType &qtpe);
37+
Type *translateStructOrUnionOrEnum(const clang::QualType &qtpe);
3838

39-
Type *translateStruct(const clang::QualType &qtpe);
40-
41-
Type *translateUnion(const clang::QualType &qtpe);
39+
Type *translateStructOrUnion(const clang::QualType &qtpe);
4240

4341
Type *translateFunctionPointer(const clang::QualType &qtpe,
4442
const std::string *avoid);
4543

46-
Type *TranslatePointer(const clang::QualType &pointee,
44+
Type *translatePointer(const clang::QualType &pointee,
4745
const std::string *avoid);
4846

4947
Type *translateConstantArray(const clang::ConstantArrayType *ar,

bindgen/defines/DefineFinder.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,63 +111,60 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,
111111
const clang::Token &token,
112112
bool positive) {
113113
clang::NumericLiteralParser parser(literal, token.getLocation(), pp);
114-
Type *type = nullptr;
114+
std::string type;
115115
std::string scalaLiteral;
116116
if (parser.isIntegerLiteral()) {
117117
if (parser.isLongLong) {
118118
/* literal has `LL` ending. `long long` is represented as `Long`
119119
* in Scala Native */
120-
type = new PrimitiveType("native.CLongLong");
120+
type = "native.CLongLong";
121121

122122
/* must fit into Scala integer type */
123123
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
124-
std::free(type);
125-
type = nullptr;
124+
type.clear();
126125
}
127126
} else if (parser.isLong) {
128127
/* literal has `L` ending */
129-
type = new PrimitiveType("native.CLong");
128+
type = "native.CLong";
130129

131130
/* must fit into Scala integer type */
132131
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
133-
std::free(type);
134-
type = nullptr;
132+
type.clear();
135133
}
136134
} else {
137135
type = getTypeOfIntegerLiteral(parser, literal, positive);
138136
}
139137

140-
if (type != nullptr) {
138+
if (!type.empty()) {
141139
scalaLiteral = getDecimalLiteral(parser);
142-
if (type->str() == "native.CLong" ||
143-
type->str() == "native.CLongLong") {
140+
if (type == "native.CLong" || type == "native.CLongLong") {
144141
scalaLiteral = scalaLiteral + "L";
145142
}
146143
}
147144
} else if (parser.isFloatingLiteral()) {
148145
if (fitsIntoDouble(parser)) {
149-
type = new PrimitiveType("native.CDouble");
146+
type = "native.CDouble";
150147
scalaLiteral = getDoubleLiteral(parser);
151148
}
152149
}
153150

154-
if (type != nullptr) {
151+
if (!type.empty()) {
155152
if (!positive) {
156153
scalaLiteral = "-" + scalaLiteral;
157154
}
158-
ir.addLiteralDefine(macroName, scalaLiteral, type);
155+
ir.addLiteralDefine(macroName, scalaLiteral, new PrimitiveType(type));
159156
}
160157
}
161158

162-
Type *
159+
std::string
163160
DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
164161
const std::string &literal,
165162
bool positive) {
166163

167164
if (integerFitsIntoType<int, uint>(parser, positive)) {
168-
return new PrimitiveType("native.CInt");
165+
return "native.CInt";
169166
} else if (integerFitsIntoType<long, unsigned long>(parser, positive)) {
170-
return new PrimitiveType("native.CLong");
167+
return "native.CLong";
171168
} else {
172169
llvm::errs() << "Warning: integer value does not fit into 8 bytes: "
173170
<< literal << "\n";
@@ -180,7 +177,7 @@ DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
180177
* @endcode
181178
* Therefore the case of `long long` is not considered here.
182179
*/
183-
return nullptr;
180+
return "";
184181
}
185182
}
186183

bindgen/defines/DefineFinder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ class DefineFinder : public clang::PPCallbacks {
3333
*
3434
* @return type of the number
3535
*/
36-
Type *getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
37-
const std::string &literal, bool positive);
36+
std::string
37+
getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
38+
const std::string &literal, bool positive);
3839

3940
std::vector<clang::Token> *expandDefine(const clang::MacroDirective &md);
4041

bindgen/ir/Struct.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bool Struct::hasHelperMethods() const {
6868

6969
std::string Struct::getAliasType() const { return "struct_" + name; }
7070

71-
std::string Struct::_str() const {
71+
std::string Struct::str() const {
7272
std::stringstream ss;
7373
ss << "native.CStruct" << std::to_string(fields.size()) << "[";
7474

bindgen/ir/Struct.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class Struct : public StructOrUnion, public Type {
4646

4747
bool usesType(Type *type) const override;
4848

49-
protected:
50-
std::string _str() const override;
49+
std::string str() const override;
5150

5251
private:
5352
/* type size is needed if number of fields is bigger than 22 */

bindgen/ir/TypeDef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ bool TypeDef::usesType(Type *type) const {
1414
return this == type || this->type == type;
1515
}
1616

17-
std::string TypeDef::_str() const { return name; }
17+
std::string TypeDef::str() const { return handleReservedWords(name); }

bindgen/ir/TypeDef.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class TypeDef : public TypeAndName, public Type {
1414

1515
bool usesType(Type *type) const override;
1616

17-
protected:
18-
std::string _str() const override;
17+
std::string str() const override;
1918
};
2019

2120
#endif // SCALA_NATIVE_BINDGEN_TYPEDEF_H

bindgen/ir/types/ArrayType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
ArrayType::ArrayType(Type *elementsType, uint64_t size)
55
: size(size), elementsType(elementsType) {}
66

7-
std::string ArrayType::_str() const {
7+
std::string ArrayType::str() const {
88
return "native.CArray[" + elementsType->str() + ", " +
99
uint64ToScalaNat(size) + "]";
1010
}

0 commit comments

Comments
 (0)