Skip to content

Commit 01a8ffb

Browse files
authored
Merge pull request #145 from kornilova-l/split-struct-file
Move Struct and Union classes to separate files
2 parents 64eb1c6 + 0c9d026 commit 01a8ffb

14 files changed

+234
-203
lines changed

bindgen/CMakeLists.txt

+4
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/Record.cpp
43+
ir/Record.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/TypeTranslator.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TypeTranslator::translatePointer(const clang::QualType &pte) {
8282
}
8383

8484
std::shared_ptr<Type>
85-
TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
85+
TypeTranslator::translateRecordOrEnum(const clang::QualType &qtpe) {
8686
std::string name = qtpe.getUnqualifiedType().getAsString();
8787
std::string nameWithoutSpace = replaceChar(name, " ", "_");
8888

@@ -104,7 +104,7 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
104104
}
105105

106106
std::shared_ptr<Type>
107-
TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
107+
TypeTranslator::translateRecord(const clang::QualType &qtpe) {
108108
if (qtpe->hasUnnamedOrLocalType()) {
109109
if (qtpe->isStructureType()) {
110110
std::string name =
@@ -119,7 +119,7 @@ TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
119119
}
120120
return nullptr;
121121
}
122-
return translateStructOrUnionOrEnum(qtpe);
122+
return translateRecordOrEnum(qtpe);
123123
}
124124

125125
std::shared_ptr<Type>
@@ -151,7 +151,7 @@ std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe) {
151151
tpe->getAs<clang::PointerType>()->getPointeeType());
152152

153153
} else if (qtpe->isStructureType() || qtpe->isUnionType()) {
154-
return translateStructOrUnion(qtpe);
154+
return translateRecord(qtpe);
155155

156156
} else if (qtpe->isEnumeralType()) {
157157
return translateEnum(qtpe);
@@ -251,5 +251,5 @@ TypeTranslator::translateEnum(const clang::QualType &type) {
251251
return std::make_shared<PrimitiveType>(getTypeFromTypeMap(
252252
enumDecl->getIntegerType().getUnqualifiedType().getAsString()));
253253
}
254-
return translateStructOrUnionOrEnum(type);
254+
return translateRecordOrEnum(type);
255255
}

bindgen/TypeTranslator.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class TypeTranslator {
3535
*/
3636
std::map<std::string, std::string> typeMap;
3737

38-
std::shared_ptr<Type>
39-
translateStructOrUnionOrEnum(const clang::QualType &qtpe);
38+
std::shared_ptr<Type> translateRecordOrEnum(const clang::QualType &qtpe);
4039

41-
std::shared_ptr<Type> translateStructOrUnion(const clang::QualType &qtpe);
40+
std::shared_ptr<Type> translateRecord(const clang::QualType &qtpe);
4241

4342
std::shared_ptr<Type> translateFunctionPointer(const clang::QualType &qtpe);
4443

bindgen/ir/Function.cpp

+1
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

+1
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/Record.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "Record.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+
Record::Record(std::string name, std::vector<std::shared_ptr<Field>> fields,
15+
std::shared_ptr<Location> location)
16+
: LocatableType(std::move(location)), name(std::move(name)),
17+
fields(std::move(fields)) {}
18+
19+
bool Record::usesType(
20+
const std::shared_ptr<const Type> &type, bool stopOnTypeDefs,
21+
std::vector<std::shared_ptr<const Type>> &visitedTypes) const {
22+
23+
if (contains(this, visitedTypes)) {
24+
return false;
25+
}
26+
visitedTypes.push_back(shared_from_this());
27+
28+
for (const auto &field : fields) {
29+
if (*field->getType() == *type ||
30+
field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) {
31+
visitedTypes.pop_back();
32+
return true;
33+
}
34+
}
35+
visitedTypes.pop_back();
36+
return false;
37+
}
38+
39+
std::string Record::getName() const { return name; }
40+
41+
bool Record::hasHelperMethods() const { return !fields.empty(); }

bindgen/ir/Record.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_RECORD_H
2+
#define SCALA_NATIVE_BINDGEN_RECORD_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 Record : public LocatableType {
27+
public:
28+
Record(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_RECORD_H

bindgen/ir/Struct.cpp

+2-118
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)
32-
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
13+
: Record(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-
}

0 commit comments

Comments
 (0)