Skip to content

Commit 0e5ac3e

Browse files
authored
Merge pull request #120 from kornilova-l/struct-fields
Small refactoring of StructOrUnion class
2 parents 67ef777 + 8fabd88 commit 0e5ac3e

File tree

5 files changed

+61
-65
lines changed

5 files changed

+61
-65
lines changed

bindgen/ir/IR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void IR::addEnum(std::string name, const std::string &type,
3232
}
3333
}
3434

35-
void IR::addStruct(std::string name, std::vector<Field *> fields,
35+
void IR::addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
3636
uint64_t typeSize, std::shared_ptr<Location> location) {
3737
std::shared_ptr<Struct> s = std::make_shared<Struct>(
3838
name, std::move(fields), typeSize, std::move(location));
@@ -46,7 +46,7 @@ void IR::addStruct(std::string name, std::vector<Field *> fields,
4646
}
4747
}
4848

49-
void IR::addUnion(std::string name, std::vector<Field *> fields,
49+
void IR::addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
5050
uint64_t maxSize, std::shared_ptr<Location> location) {
5151
std::shared_ptr<Union> u = std::make_shared<Union>(
5252
name, std::move(fields), maxSize, std::move(location));

bindgen/ir/IR.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class IR {
3434
std::vector<Enumerator> enumerators,
3535
std::shared_ptr<Location> location);
3636

37-
void addStruct(std::string name, std::vector<Field *> fields,
37+
void addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
3838
uint64_t typeSize, std::shared_ptr<Location> location);
3939

40-
void addUnion(std::string name, std::vector<Field *> fields,
40+
void addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
4141
uint64_t maxSize, std::shared_ptr<Location> location);
4242

4343
void addLiteralDefine(std::string name, std::string literal,

bindgen/ir/Struct.cpp

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,14 @@
77
Field::Field(std::string name, std::shared_ptr<Type> type)
88
: TypeAndName(std::move(name), std::move(type)) {}
99

10-
std::string Field::generateSetter(int fieldIndex) {
11-
std::string setter = handleReservedWords(getName(), "_=");
12-
std::string parameterType = type->str();
13-
std::string value = "value";
14-
if (isAliasForType<ArrayType>(type.get()) ||
15-
isAliasForType<Struct>(type.get())) {
16-
parameterType = "native.Ptr[" + parameterType + "]";
17-
value = "!" + value;
18-
}
19-
std::stringstream s;
20-
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
21-
<< std::to_string(fieldIndex + 1) << " = " << value;
22-
return s.str();
23-
}
24-
25-
std::string Field::generateGetter(int fieldIndex) {
26-
std::string getter = handleReservedWords(getName());
27-
std::string returnType = type->str();
28-
std::string methodBody;
29-
if (isAliasForType<ArrayType>(type.get()) ||
30-
isAliasForType<Struct>(type.get())) {
31-
returnType = "native.Ptr[" + returnType + "]";
32-
methodBody = "p._" + std::to_string(fieldIndex + 1);
33-
} else {
34-
methodBody = "!p._" + std::to_string(fieldIndex + 1);
35-
}
36-
std::stringstream s;
37-
s << " def " << getter << ": " << returnType << " = " << methodBody;
38-
return s.str();
39-
}
40-
41-
StructOrUnion::StructOrUnion(std::string name, std::vector<Field *> fields,
10+
StructOrUnion::StructOrUnion(std::string name,
11+
std::vector<std::shared_ptr<Field>> fields,
4212
std::shared_ptr<Location> location)
4313
: name(std::move(name)), fields(std::move(fields)),
4414
location(std::move(location)) {}
4515

4616
std::string StructOrUnion::getName() const { return name; }
4717

48-
StructOrUnion::~StructOrUnion() {
49-
for (const auto &field : fields) {
50-
delete field;
51-
}
52-
}
53-
5418
bool StructOrUnion::equals(const StructOrUnion &other) const {
5519
if (this == &other) {
5620
return true;
@@ -77,8 +41,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
7741
return location;
7842
}
7943

80-
Struct::Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
81-
std::shared_ptr<Location> location)
44+
Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
45+
uint64_t typeSize, std::shared_ptr<Location> location)
8246
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
8347
typeSize(typeSize) {}
8448

@@ -106,11 +70,11 @@ std::string Struct::generateHelperClass() const {
10670
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
10771
<< "])"
10872
<< " extends AnyVal {\n";
109-
int fieldIndex = 0;
73+
unsigned fieldIndex = 0;
11074
for (const auto &field : fields) {
11175
if (!field->getName().empty()) {
112-
s << field->generateGetter(fieldIndex) << "\n";
113-
s << field->generateSetter(fieldIndex) << "\n";
76+
s << generateGetter(fieldIndex) << "\n";
77+
s << generateSetter(fieldIndex) << "\n";
11478
}
11579
fieldIndex++;
11680
}
@@ -163,8 +127,41 @@ bool Struct::operator==(const Type &other) const {
163127
return false;
164128
}
165129

166-
Union::Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
167-
std::shared_ptr<Location> location)
130+
std::string Struct::generateSetter(unsigned fieldIndex) const {
131+
std::shared_ptr<Field> field = fields[fieldIndex];
132+
std::string setter = handleReservedWords(field->getName(), "_=");
133+
std::string parameterType = field->getType()->str();
134+
std::string value = "value";
135+
if (isAliasForType<ArrayType>(field->getType().get()) ||
136+
isAliasForType<Struct>(field->getType().get())) {
137+
parameterType = "native.Ptr[" + parameterType + "]";
138+
value = "!" + value;
139+
}
140+
std::stringstream s;
141+
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
142+
<< std::to_string(fieldIndex + 1) << " = " << value;
143+
return s.str();
144+
}
145+
146+
std::string Struct::generateGetter(unsigned fieldIndex) const {
147+
std::shared_ptr<Field> field = fields[fieldIndex];
148+
std::string getter = handleReservedWords(field->getName());
149+
std::string returnType = field->getType()->str();
150+
std::string methodBody;
151+
if (isAliasForType<ArrayType>(field->getType().get()) ||
152+
isAliasForType<Struct>(field->getType().get())) {
153+
returnType = "native.Ptr[" + returnType + "]";
154+
methodBody = "p._" + std::to_string(fieldIndex + 1);
155+
} else {
156+
methodBody = "!p._" + std::to_string(fieldIndex + 1);
157+
}
158+
std::stringstream s;
159+
s << " def " << getter << ": " << returnType << " = " << methodBody;
160+
return s.str();
161+
}
162+
163+
Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
164+
uint64_t maxSize, std::shared_ptr<Location> location)
168165
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
169166
ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {}
170167

bindgen/ir/Struct.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212
class Field : public TypeAndName {
1313
public:
1414
Field(std::string name, std::shared_ptr<Type> type);
15-
16-
std::string generateSetter(int fieldIndex);
17-
18-
std::string generateGetter(int fieldIndex);
1915
};
2016

2117
class StructOrUnion {
2218
public:
23-
StructOrUnion(std::string name, std::vector<Field *> fields,
19+
StructOrUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
2420
std::shared_ptr<Location> location);
2521

26-
~StructOrUnion();
27-
2822
virtual std::shared_ptr<TypeDef> generateTypeDef() = 0;
2923

3024
virtual std::string generateHelperClass() const = 0;
@@ -39,16 +33,16 @@ class StructOrUnion {
3933

4034
protected:
4135
std::string name;
42-
std::vector<Field *> fields;
36+
std::vector<std::shared_ptr<Field>> fields;
4337
std::shared_ptr<Location> location;
4438
};
4539

4640
class Struct : public StructOrUnion,
4741
public Type,
4842
public std::enable_shared_from_this<Struct> {
4943
public:
50-
Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
51-
std::shared_ptr<Location> location);
44+
Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
45+
uint64_t typeSize, std::shared_ptr<Location> location);
5246

5347
std::shared_ptr<TypeDef> generateTypeDef() override;
5448

@@ -68,6 +62,10 @@ class Struct : public StructOrUnion,
6862

6963
bool operator==(const Type &other) const override;
7064

65+
std::string generateSetter(unsigned fieldIndex) const;
66+
67+
std::string generateGetter(unsigned fieldIndex) const;
68+
7169
private:
7270
/* type size is needed if number of fields is bigger than 22 */
7371
uint64_t typeSize;
@@ -77,8 +75,8 @@ class Union : public StructOrUnion,
7775
public ArrayType,
7876
public std::enable_shared_from_this<Union> {
7977
public:
80-
Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
81-
std::shared_ptr<Location> location);
78+
Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
79+
uint64_t maxSize, std::shared_ptr<Location> location);
8280

8381
std::shared_ptr<TypeDef> generateTypeDef() override;
8482

bindgen/visitor/TreeVisitor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record) {
101101
void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
102102
uint64_t maxSize = 0;
103103

104-
std::vector<Field *> fields;
104+
std::vector<std::shared_ptr<Field>> fields;
105105

106106
for (const clang::FieldDecl *field : record->fields()) {
107107
uint64_t sizeInBits = astContext->getTypeSize(field->getType());
@@ -111,7 +111,7 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
111111
std::shared_ptr<Type> ftype =
112112
typeTranslator.translate(field->getType(), &name);
113113

114-
fields.push_back(new Field(fname, ftype));
114+
fields.push_back(std::make_shared<Field>(fname, ftype));
115115
}
116116

117117
ir.addUnion(name, std::move(fields), maxSize, getLocation(record));
@@ -128,12 +128,13 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
128128
}
129129

130130
int fieldCnt = 0;
131-
std::vector<Field *> fields;
131+
std::vector<std::shared_ptr<Field>> fields;
132132

133133
for (const clang::FieldDecl *field : record->fields()) {
134134
std::shared_ptr<Type> ftype =
135135
typeTranslator.translate(field->getType(), &name);
136-
fields.push_back(new Field(field->getNameAsString(), ftype));
136+
fields.push_back(
137+
std::make_shared<Field>(field->getNameAsString(), ftype));
137138

138139
cycleDetection.AddDependency(newName, field->getType());
139140

0 commit comments

Comments
 (0)