Skip to content

Commit e9b676b

Browse files
committed
Move Struct and Union classes to separate files
1 parent c2845fb commit e9b676b

File tree

11 files changed

+222
-188
lines changed

11 files changed

+222
-188
lines changed

bindgen/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ add_executable(bindgen
3939
Utils.h
4040
ir/IR.h
4141
ir/IR.cpp
42+
ir/StructOrUnion.cpp
43+
ir/StructOrUnion.h
4244
ir/Struct.cpp
4345
ir/Struct.h
46+
ir/Union.cpp
47+
ir/Union.h
4448
ir/Function.cpp
4549
ir/Function.h
4650
ir/TypeDef.cpp

bindgen/ir/Function.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Function.h"
22
#include "../Utils.h"
33
#include "Struct.h"
4+
#include "Union.h"
45

56
Parameter::Parameter(std::string name, std::shared_ptr<const Type> type)
67
: TypeAndName(std::move(name), type) {}

bindgen/ir/IR.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "PossibleVarDefine.h"
99
#include "Struct.h"
1010
#include "TypeDef.h"
11+
#include "Union.h"
1112
#include "VarDefine.h"
1213

1314
/**

bindgen/ir/Struct.cpp

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,18 @@
11
#include "Struct.h"
22
#include "../Utils.h"
3+
#include "Union.h"
34
#include "types/ArrayType.h"
45
#include "types/FunctionPointerType.h"
56
#include "types/PointerType.h"
67
#include "types/PrimitiveType.h"
78
#include <sstream>
89

9-
Field::Field(std::string name, std::shared_ptr<const Type> type)
10-
: TypeAndName(std::move(name), std::move(type)) {}
11-
12-
Field::Field(std::string name, std::shared_ptr<const Type> type,
13-
uint64_t offsetInBits)
14-
: TypeAndName(std::move(name), std::move(type)),
15-
offsetInBits(offsetInBits) {}
16-
17-
uint64_t Field::getOffsetInBits() const { return offsetInBits; }
18-
19-
StructOrUnion::StructOrUnion(std::string name,
20-
std::vector<std::shared_ptr<Field>> fields,
21-
std::shared_ptr<Location> location)
22-
: LocatableType(std::move(location)), name(std::move(name)),
23-
fields(std::move(fields)) {}
24-
25-
std::string StructOrUnion::getName() const { return name; }
26-
27-
bool StructOrUnion::hasHelperMethods() const { return !fields.empty(); }
28-
2910
Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
3011
uint64_t typeSize, std::shared_ptr<Location> location,
3112
bool isPacked, bool isBitField)
3213
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
3314
typeSize(typeSize), isPacked(isPacked), hasBitField(isBitField) {}
3415

35-
bool StructOrUnion::usesType(
36-
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
37-
std::vector<std::shared_ptr<const Type>> &visitedTypes) const {
38-
39-
if (contains(this, visitedTypes)) {
40-
return false;
41-
}
42-
visitedTypes.push_back(shared_from_this());
43-
44-
for (const auto &field : fields) {
45-
if (*field->getType() == *type ||
46-
field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) {
47-
visitedTypes.pop_back();
48-
return true;
49-
}
50-
}
51-
visitedTypes.pop_back();
52-
return false;
53-
}
54-
5516
std::shared_ptr<TypeDef> Struct::generateTypeDef() {
5617
if (isRepresentedAsStruct()) {
5718
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
@@ -382,80 +343,3 @@ bool Struct::hasBiggestName(const CycleNode &node,
382343
}
383344
return false;
384345
}
385-
386-
Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
387-
uint64_t maxSize, std::shared_ptr<Location> location)
388-
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
389-
ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {}
390-
391-
std::shared_ptr<TypeDef> Union::generateTypeDef() {
392-
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
393-
nullptr);
394-
}
395-
396-
std::string Union::generateHelperClass() const {
397-
assert(hasHelperMethods());
398-
std::stringstream s;
399-
std::string type = getTypeAlias();
400-
s << " implicit class " << type << "_pos"
401-
<< "(val p: native.Ptr[" << type << "]) extends AnyVal {\n";
402-
for (const auto &field : fields) {
403-
if (!field->getName().empty()) {
404-
s << generateGetter(field);
405-
s << generateSetter(field);
406-
}
407-
}
408-
s << " }\n";
409-
return s.str();
410-
}
411-
412-
std::string Union::getTypeAlias() const { return "union_" + name; }
413-
414-
bool Union::operator==(const Type &other) const {
415-
if (this == &other) {
416-
return true;
417-
}
418-
auto *u = dynamic_cast<const Union *>(&other);
419-
if (u) {
420-
/* unions have unique names */
421-
return name == u->name;
422-
}
423-
return false;
424-
}
425-
426-
bool Union::usesType(
427-
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
428-
std::vector<std::shared_ptr<const Type>> &visitedTypes) const {
429-
430-
if (contains(this, visitedTypes)) {
431-
return false;
432-
}
433-
visitedTypes.push_back(shared_from_this());
434-
435-
if (ArrayType::usesType(type, stopOnTypeDefs, visitedTypes)) {
436-
visitedTypes.pop_back();
437-
return true;
438-
}
439-
visitedTypes.pop_back();
440-
441-
return StructOrUnion::usesType(type, stopOnTypeDefs, visitedTypes);
442-
}
443-
444-
std::string Union::generateGetter(const std::shared_ptr<Field> &field) const {
445-
std::string getter = handleReservedWords(field->getName());
446-
std::string ftype = field->getType()->str();
447-
return " def " + getter + ": native.Ptr[" + ftype +
448-
"] = p.cast[native.Ptr[" + ftype + "]]\n";
449-
}
450-
451-
std::string Union::generateSetter(const std::shared_ptr<Field> &field) const {
452-
std::string setter = handleReservedWords(field->getName(), "_=");
453-
std::string ftype = field->getType()->str();
454-
if (isAliasForType<ArrayType>(field->getType().get()) ||
455-
isAliasForType<Struct>(field->getType().get())) {
456-
return " def " + setter + "(value: native.Ptr[" + ftype +
457-
"]): Unit = !p.cast[native.Ptr[" + ftype + "]] = !value\n";
458-
}
459-
return " def " + setter + "(value: " + ftype +
460-
"): Unit = !p.cast[native.Ptr[" + ftype + "]] = value\n";
461-
}

bindgen/ir/Struct.h

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,10 @@
11
#ifndef SCALA_NATIVE_BINDGEN_STRUCT_H
22
#define SCALA_NATIVE_BINDGEN_STRUCT_H
33

4-
#include "LocatableType.h"
5-
#include "TypeAndName.h"
6-
#include "TypeDef.h"
7-
#include "types/ArrayType.h"
8-
#include "types/PointerType.h"
9-
#include <string>
10-
#include <vector>
4+
#include "StructOrUnion.h"
115

126
#define SCALA_NATIVE_MAX_STRUCT_FIELDS 22
137

14-
class Field : public TypeAndName {
15-
public:
16-
Field(std::string name, std::shared_ptr<const Type> type);
17-
18-
Field(std::string name, std::shared_ptr<const Type> type,
19-
uint64_t offsetInBits);
20-
21-
uint64_t getOffsetInBits() const;
22-
23-
protected:
24-
/**
25-
* Offset in bytes from address of struct/union.
26-
*/
27-
uint64_t offsetInBits = 0;
28-
};
29-
30-
class StructOrUnion : public LocatableType {
31-
public:
32-
StructOrUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
33-
std::shared_ptr<Location> location);
34-
35-
virtual std::shared_ptr<TypeDef> generateTypeDef() = 0;
36-
37-
virtual std::string generateHelperClass() const = 0;
38-
39-
std::string getName() const;
40-
41-
virtual std::string getTypeAlias() const = 0;
42-
43-
virtual bool hasHelperMethods() const;
44-
45-
bool usesType(
46-
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
47-
std::vector<std::shared_ptr<const Type>> &visitedTypes) const override;
48-
49-
protected:
50-
std::string name;
51-
std::vector<std::shared_ptr<Field>> fields;
52-
};
53-
548
class Struct : public StructOrUnion {
559
public:
5610
Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
@@ -164,27 +118,4 @@ class Struct : public StructOrUnion {
164118
std::vector<std::string> namesInCycle) const;
165119
};
166120

167-
class Union : public StructOrUnion, public ArrayType {
168-
public:
169-
Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
170-
uint64_t maxSize, std::shared_ptr<Location> location);
171-
172-
std::shared_ptr<TypeDef> generateTypeDef() override;
173-
174-
std::string generateHelperClass() const override;
175-
176-
bool operator==(const Type &other) const override;
177-
178-
std::string getTypeAlias() const override;
179-
180-
bool usesType(
181-
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
182-
std::vector<std::shared_ptr<const Type>> &visitedTypes) const override;
183-
184-
private:
185-
std::string generateGetter(const std::shared_ptr<Field> &field) const;
186-
187-
std::string generateSetter(const std::shared_ptr<Field> &field) const;
188-
};
189-
190121
#endif // SCALA_NATIVE_BINDGEN_STRUCT_H

bindgen/ir/StructOrUnion.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "StructOrUnion.h"
2+
#include "../Utils.h"
3+
4+
Field::Field(std::string name, std::shared_ptr<const Type> type)
5+
: TypeAndName(std::move(name), std::move(type)) {}
6+
7+
Field::Field(std::string name, std::shared_ptr<const Type> type,
8+
uint64_t offsetInBits)
9+
: TypeAndName(std::move(name), std::move(type)),
10+
offsetInBits(offsetInBits) {}
11+
12+
uint64_t Field::getOffsetInBits() const { return offsetInBits; }
13+
14+
StructOrUnion::StructOrUnion(std::string name,
15+
std::vector<std::shared_ptr<Field>> fields,
16+
std::shared_ptr<Location> location)
17+
: LocatableType(std::move(location)), name(std::move(name)),
18+
fields(std::move(fields)) {}
19+
20+
bool StructOrUnion::usesType(
21+
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
22+
std::vector<std::shared_ptr<const Type>> &visitedTypes) const {
23+
24+
if (contains(this, visitedTypes)) {
25+
return false;
26+
}
27+
visitedTypes.push_back(shared_from_this());
28+
29+
for (const auto &field : fields) {
30+
if (*field->getType() == *type ||
31+
field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) {
32+
visitedTypes.pop_back();
33+
return true;
34+
}
35+
}
36+
visitedTypes.pop_back();
37+
return false;
38+
}
39+
40+
std::string StructOrUnion::getName() const { return name; }
41+
42+
bool StructOrUnion::hasHelperMethods() const { return !fields.empty(); }

bindgen/ir/StructOrUnion.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_STRUCTORUNION_H
2+
#define SCALA_NATIVE_BINDGEN_STRUCTORUNION_H
3+
4+
#include "LocatableType.h"
5+
#include "TypeAndName.h"
6+
#include "TypeDef.h"
7+
#include <string>
8+
#include <vector>
9+
10+
class Field : public TypeAndName {
11+
public:
12+
Field(std::string name, std::shared_ptr<const Type> type);
13+
14+
Field(std::string name, std::shared_ptr<const Type> type,
15+
uint64_t offsetInBits);
16+
17+
uint64_t getOffsetInBits() const;
18+
19+
protected:
20+
/**
21+
* Offset in bytes from address of struct/union.
22+
*/
23+
uint64_t offsetInBits = 0;
24+
};
25+
26+
class StructOrUnion : public LocatableType {
27+
public:
28+
StructOrUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
29+
std::shared_ptr<Location> location);
30+
31+
virtual std::shared_ptr<TypeDef> generateTypeDef() = 0;
32+
33+
virtual std::string generateHelperClass() const = 0;
34+
35+
std::string getName() const;
36+
37+
virtual std::string getTypeAlias() const = 0;
38+
39+
virtual bool hasHelperMethods() const;
40+
41+
bool usesType(
42+
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
43+
std::vector<std::shared_ptr<const Type>> &visitedTypes) const override;
44+
45+
protected:
46+
std::string name;
47+
std::vector<std::shared_ptr<Field>> fields;
48+
};
49+
50+
#endif // SCALA_NATIVE_BINDGEN_STRUCTORUNION_H

bindgen/ir/TypeDef.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../Utils.h"
33
#include "Enum.h"
44
#include "Struct.h"
5+
#include "Union.h"
56
#include <stdexcept>
67

78
TypeDef::TypeDef(std::string name, std::shared_ptr<const Type> type,

0 commit comments

Comments
 (0)