Skip to content

Commit 9834ee7

Browse files
committed
Remove redundant aliasesMap
1 parent 5f1958d commit 9834ee7

File tree

5 files changed

+20
-32
lines changed

5 files changed

+20
-32
lines changed

bindgen/TypeTranslator.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,22 @@ TypeTranslator::translatePointer(const clang::QualType &pte,
8787
std::shared_ptr<Type>
8888
TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
8989
std::string name = qtpe.getUnqualifiedType().getAsString();
90+
std::string nameWithoutSpace = replaceChar(name, " ", "_");
9091

91-
auto it = aliasesMap.find(name);
92-
if (it != aliasesMap.end()) {
93-
/* name contains space: struct <name>.
94-
* Use type alias instead struct type */
95-
return (*it).second;
96-
}
97-
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(name);
92+
/* If the struct was already declared then there is a TypeDef instance
93+
* with appropriate name.
94+
*
95+
* If there is no such TypeDef then the type is opaque and TypeDef with
96+
* nullptr will be generated for the type. */
97+
98+
std::shared_ptr<TypeDef> typeDef = ir.getTypeDefWithName(nameWithoutSpace);
9899
if (typeDef) {
99-
/* type has typedef alias */
100100
return typeDef;
101101
}
102102
/* type is not yet defined.
103-
* nullptr will be replaced by actual type */
104-
typeDef = ir.addTypeDef(replaceChar(name, " ", "_"), nullptr);
105-
addAlias(name, typeDef);
103+
* TypeDef with nullptr will be created.
104+
* nullptr will be replaced by actual type when the type is declared. */
105+
typeDef = ir.addTypeDef(nameWithoutSpace, nullptr);
106106
return typeDef;
107107
}
108108

@@ -184,10 +184,6 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
184184
}
185185
}
186186

187-
void TypeTranslator::addAlias(std::string cName, std::shared_ptr<Type> type) {
188-
aliasesMap[cName] = type;
189-
}
190-
191187
std::string TypeTranslator::getTypeFromTypeMap(std::string cType) {
192188
auto it = typeMap.find(cType);
193189
if (it != typeMap.end()) {

bindgen/TypeTranslator.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class TypeTranslator {
1717
std::shared_ptr<Type> translate(const clang::QualType &tpe,
1818
const std::string * = nullptr);
1919

20-
void addAlias(std::string cName, std::shared_ptr<Type> type);
21-
2220
std::string getTypeFromTypeMap(std::string cType);
2321

2422
private:
@@ -30,11 +28,6 @@ class TypeTranslator {
3028
*/
3129
std::map<std::string, std::string> typeMap;
3230

33-
/**
34-
* Maps C struct, union or enum name to Type alias
35-
*/
36-
std::map<std::string, std::shared_ptr<Type>> aliasesMap;
37-
3831
std::shared_ptr<Type>
3932
translateStructOrUnionOrEnum(const clang::QualType &qtpe);
4033

bindgen/Utils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ 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,
103+
static inline std::string replaceChar(const std::string &str,
104+
const std::string &c1,
104105
const std::string &c2) {
105106
auto f = str.find(c1);
106107
if (f != std::string::npos) {
107-
return str.replace(f, c1.length(), c2);
108+
return std::string(str).replace(f, c1.length(), c2);
108109
}
109-
return str;
110+
return std::string(str);
110111
}
111112

112113
#endif // UTILS_H

bindgen/ir/IR.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ std::shared_ptr<Variable> IR::addVariable(const std::string &name,
322322
}
323323

324324
std::shared_ptr<TypeDef> IR::getTypeDefWithName(const std::string &name) {
325+
/* nullptr is returned in 2 cases:
326+
* 1. TypeTranslator translates opaque struct/union type for which TypeDef
327+
* was not created.
328+
* 2. TreeVisitor visits struct/union declaration and it checks whether a
329+
* TypeDef already exists for it.*/
325330
return getDeclarationWithName(typeDefs, name);
326331
}
327332

bindgen/visitor/TreeVisitor.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl) {
7171

7272
std::shared_ptr<Type> alias =
7373
ir.addEnum(name, scalaType, std::move(enumerators));
74-
if (alias != nullptr) {
75-
typeTranslator.addAlias("enum " + name, alias);
76-
}
7774

7875
return true;
7976
}
@@ -127,8 +124,6 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
127124
} else {
128125
alias = ir.addUnion(name, std::move(fields), maxSize);
129126
}
130-
131-
typeTranslator.addAlias("union " + name, alias);
132127
}
133128

134129
void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
@@ -176,8 +171,6 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
176171
} else {
177172
alias = ir.addStruct(name, std::move(fields), sizeInBits / 8);
178173
}
179-
180-
typeTranslator.addAlias("struct " + name, alias);
181174
}
182175

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

0 commit comments

Comments
 (0)