Skip to content

Commit 667ad50

Browse files
authored
Merge pull request #148 from kornilova-l/store-types-with-spaces
Internally store type names with spaces
2 parents 01a8ffb + 5f0f048 commit 667ad50

File tree

10 files changed

+26
-27
lines changed

10 files changed

+26
-27
lines changed

bindgen/TypeTranslator.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,21 @@ TypeTranslator::translatePointer(const clang::QualType &pte) {
8484
std::shared_ptr<Type>
8585
TypeTranslator::translateRecordOrEnum(const clang::QualType &qtpe) {
8686
std::string name = qtpe.getUnqualifiedType().getAsString();
87-
std::string nameWithoutSpace = replaceChar(name, " ", "_");
8887

8988
/* If the struct was already declared then there is a TypeDef instance
9089
* with appropriate name.
9190
*
9291
* If there is no such TypeDef then the type is opaque and TypeDef with
9392
* nullptr will be generated for the type. */
9493

95-
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(nameWithoutSpace);
94+
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(name);
9695
if (typeDef) {
9796
return typeDef;
9897
}
9998
/* type is not yet defined.
10099
* TypeDef with nullptr will be created.
101100
* nullptr will be replaced by actual type when the type is declared. */
102-
typeDef = ir.addTypeDef(nameWithoutSpace, nullptr, nullptr);
101+
typeDef = ir.addTypeDef(name, nullptr, nullptr);
103102
return typeDef;
104103
}
105104

@@ -211,8 +210,6 @@ TypeTranslator::addUnionDefinition(clang::RecordDecl *record,
211210
std::shared_ptr<TypeDef>
212211
TypeTranslator::addStructDefinition(clang::RecordDecl *record,
213212
std::string name) {
214-
std::string newName = "struct_" + name;
215-
216213
if (record->hasAttr<clang::PackedAttr>()) {
217214
llvm::errs() << "Warning: struct " << name << " is packed. "
218215
<< "Packed structs are not supported by Scala Native. "

bindgen/ir/Enum.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Enum::Enum(std::string name, std::string type,
1616
bool Enum::isAnonymous() const { return name.empty(); }
1717

1818
std::shared_ptr<TypeDef> Enum::generateTypeDef() {
19-
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
19+
return std::make_shared<TypeDef>(getTypeName(), shared_from_this(),
2020
nullptr);
2121
}
2222

@@ -51,7 +51,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
5151

5252
std::string Enum::getName() const { return name; }
5353

54-
std::string Enum::getTypeAlias() const {
54+
std::string Enum::getTypeName() const {
5555
assert(!isAnonymous());
56-
return "enum_" + name;
56+
return "enum " + name;
5757
}

bindgen/ir/Enum.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Enum : public PrimitiveType, public LocatableType {
3333

3434
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);
3535

36-
std::string getTypeAlias() const;
36+
std::string getTypeName() const;
3737

3838
private:
3939
std::string name; // might be empty

bindgen/ir/IR.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ IR::addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
4141
std::make_shared<Struct>(name, std::move(fields), typeSize,
4242
std::move(location), isPacked, isBitField);
4343
structs.push_back(s);
44-
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("struct_" + name);
44+
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("struct " + name);
4545
if (typeDef) {
4646
/* the struct type used to be opaque type, typeDef contains nullptr */
4747
typeDef.get()->setType(s);
@@ -58,7 +58,7 @@ IR::addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
5858
std::shared_ptr<Union> u = std::make_shared<Union>(
5959
name, std::move(fields), maxSize, std::move(location));
6060
unions.push_back(u);
61-
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("union_" + name);
61+
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("union " + name);
6262
if (typeDef) {
6363
/* the union type used to be opaque type, typeDef contains nullptr */
6464
typeDef.get()->setType(u);

bindgen/ir/Record.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Record : public LocatableType {
3434

3535
std::string getName() const;
3636

37-
virtual std::string getTypeAlias() const = 0;
37+
virtual std::string getTypeName() const = 0;
3838

3939
virtual bool hasHelperMethods() const;
4040

@@ -43,7 +43,7 @@ class Record : public LocatableType {
4343
std::vector<std::shared_ptr<const Type>> &visitedTypes) const override;
4444

4545
protected:
46-
std::string name;
46+
std::string name; // does not contain 'struct' or 'union' word
4747
std::vector<std::shared_ptr<Field>> fields;
4848
};
4949

bindgen/ir/Struct.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
1515

1616
std::shared_ptr<TypeDef> Struct::generateTypeDef() {
1717
if (isRepresentedAsStruct()) {
18-
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
18+
return std::make_shared<TypeDef>(getTypeName(), shared_from_this(),
1919
nullptr);
2020
} else {
2121
// There is no easy way to represent it as a struct in scala native,
2222
// have to represent it as an array and then Add helpers to help with
2323
// its manipulation
2424
return std::make_shared<TypeDef>(
25-
getTypeAlias(),
25+
getTypeName(),
2626
std::make_shared<ArrayType>(std::make_shared<PrimitiveType>("Byte"),
2727
typeSize),
2828
location);
@@ -32,7 +32,7 @@ std::shared_ptr<TypeDef> Struct::generateTypeDef() {
3232
std::string Struct::generateHelperClass() const {
3333
assert(hasHelperMethods());
3434
std::stringstream s;
35-
std::string type = getTypeAlias();
35+
std::string type = replaceChar(getTypeName(), " ", "_");
3636
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
3737
<< "])"
3838
<< " extends AnyVal {\n";
@@ -83,7 +83,7 @@ std::string Struct::generateHelperClassMethodsForArrayRepresentation() const {
8383
return s.str();
8484
}
8585

86-
std::string Struct::getTypeAlias() const { return "struct_" + name; }
86+
std::string Struct::getTypeName() const { return "struct " + name; }
8787

8888
std::string Struct::str() const {
8989
std::stringstream ss;
@@ -248,7 +248,7 @@ Struct::getTypeReplacement(std::shared_ptr<const Type> type,
248248
std::make_shared<PointerType>(std::make_shared<PrimitiveType>("Byte"));
249249
for (const auto &recordType : structTypesThatShouldBeReplaced) {
250250
std::shared_ptr<TypeDef> recordTypeDef = std::make_shared<TypeDef>(
251-
recordType->getTypeAlias(), recordType, nullptr);
251+
recordType->getTypeName(), recordType, nullptr);
252252
std::shared_ptr<Type> pointerToRecord =
253253
std::make_shared<PointerType>(recordTypeDef);
254254
if (*replacementType == *pointerToRecord) {
@@ -330,11 +330,11 @@ bool Struct::findAllCycles(
330330
bool Struct::hasBiggestName(const CycleNode &node,
331331
std::vector<std::string> namesInCycle) const {
332332
if (!node.isValueType) {
333-
namesInCycle.push_back(node.s->getTypeAlias());
333+
namesInCycle.push_back(node.s->getTypeName());
334334
}
335335
if (node.cycleNodes.empty()) {
336336
std::sort(namesInCycle.begin(), namesInCycle.end());
337-
return getTypeAlias() >= namesInCycle.back();
337+
return getTypeName() >= namesInCycle.back();
338338
}
339339
for (const auto &cycleNode : node.cycleNodes) {
340340
if (hasBiggestName(cycleNode, namesInCycle)) {

bindgen/ir/Struct.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Struct : public Record {
1515

1616
std::string generateHelperClass() const override;
1717

18-
std::string getTypeAlias() const override;
18+
std::string getTypeName() const override;
1919

2020
/**
2121
* @return true if helper methods will be generated for this struct

bindgen/ir/TypeDef.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TypeDef::TypeDef(std::string name, std::shared_ptr<const Type> type,
1111
LocatableType(std::move(location)) {}
1212

1313
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
14-
s << " type " << handleReservedWords(typeDef.name) << " = ";
14+
s << " type " << typeDef.str() << " = ";
1515
if (typeDef.type) {
1616
s << typeDef.getType()->str();
1717
} else {
@@ -40,7 +40,9 @@ bool TypeDef::usesType(
4040
return result;
4141
}
4242

43-
std::string TypeDef::str() const { return handleReservedWords(name); }
43+
std::string TypeDef::str() const {
44+
return handleReservedWords(replaceChar(name, " ", "_"));
45+
}
4446

4547
bool TypeDef::operator==(const Type &other) const {
4648
if (this == &other) {

bindgen/ir/Union.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
1313
ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {}
1414

1515
std::shared_ptr<TypeDef> Union::generateTypeDef() {
16-
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
16+
return std::make_shared<TypeDef>(getTypeName(), shared_from_this(),
1717
nullptr);
1818
}
1919

2020
std::string Union::generateHelperClass() const {
2121
assert(hasHelperMethods());
2222
std::stringstream s;
23-
std::string type = getTypeAlias();
23+
std::string type = replaceChar(getTypeName(), " ", "_");
2424
s << " implicit class " << type << "_pos"
2525
<< "(val p: native.Ptr[" << type << "]) extends AnyVal {\n";
2626
for (const auto &field : fields) {
@@ -33,7 +33,7 @@ std::string Union::generateHelperClass() const {
3333
return s.str();
3434
}
3535

36-
std::string Union::getTypeAlias() const { return "union_" + name; }
36+
std::string Union::getTypeName() const { return "union " + name; }
3737

3838
bool Union::operator==(const Type &other) const {
3939
if (this == &other) {

bindgen/ir/Union.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Union : public Record, public ArrayType {
2020

2121
bool operator==(const Type &other) const override;
2222

23-
std::string getTypeAlias() const override;
23+
std::string getTypeName() const override;
2424

2525
bool usesType(
2626
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,

0 commit comments

Comments
 (0)