Skip to content

Commit

Permalink
Merge pull request #120 from kornilova-l/struct-fields
Browse files Browse the repository at this point in the history
Small refactoring of StructOrUnion class
  • Loading branch information
kornilova203 authored Jul 13, 2018
2 parents 67ef777 + 8fabd88 commit 0e5ac3e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 65 deletions.
4 changes: 2 additions & 2 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void IR::addEnum(std::string name, const std::string &type,
}
}

void IR::addStruct(std::string name, std::vector<Field *> fields,
void IR::addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t typeSize, std::shared_ptr<Location> location) {
std::shared_ptr<Struct> s = std::make_shared<Struct>(
name, std::move(fields), typeSize, std::move(location));
Expand All @@ -46,7 +46,7 @@ void IR::addStruct(std::string name, std::vector<Field *> fields,
}
}

void IR::addUnion(std::string name, std::vector<Field *> fields,
void IR::addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t maxSize, std::shared_ptr<Location> location) {
std::shared_ptr<Union> u = std::make_shared<Union>(
name, std::move(fields), maxSize, std::move(location));
Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class IR {
std::vector<Enumerator> enumerators,
std::shared_ptr<Location> location);

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

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

void addLiteralDefine(std::string name, std::string literal,
Expand Down
87 changes: 42 additions & 45 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,14 @@
Field::Field(std::string name, std::shared_ptr<Type> type)
: TypeAndName(std::move(name), std::move(type)) {}

std::string Field::generateSetter(int fieldIndex) {
std::string setter = handleReservedWords(getName(), "_=");
std::string parameterType = type->str();
std::string value = "value";
if (isAliasForType<ArrayType>(type.get()) ||
isAliasForType<Struct>(type.get())) {
parameterType = "native.Ptr[" + parameterType + "]";
value = "!" + value;
}
std::stringstream s;
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
<< std::to_string(fieldIndex + 1) << " = " << value;
return s.str();
}

std::string Field::generateGetter(int fieldIndex) {
std::string getter = handleReservedWords(getName());
std::string returnType = type->str();
std::string methodBody;
if (isAliasForType<ArrayType>(type.get()) ||
isAliasForType<Struct>(type.get())) {
returnType = "native.Ptr[" + returnType + "]";
methodBody = "p._" + std::to_string(fieldIndex + 1);
} else {
methodBody = "!p._" + std::to_string(fieldIndex + 1);
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody;
return s.str();
}

StructOrUnion::StructOrUnion(std::string name, std::vector<Field *> fields,
StructOrUnion::StructOrUnion(std::string name,
std::vector<std::shared_ptr<Field>> fields,
std::shared_ptr<Location> location)
: name(std::move(name)), fields(std::move(fields)),
location(std::move(location)) {}

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

StructOrUnion::~StructOrUnion() {
for (const auto &field : fields) {
delete field;
}
}

bool StructOrUnion::equals(const StructOrUnion &other) const {
if (this == &other) {
return true;
Expand All @@ -77,8 +41,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
return location;
}

Struct::Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
std::shared_ptr<Location> location)
Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t typeSize, std::shared_ptr<Location> location)
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
typeSize(typeSize) {}

Expand Down Expand Up @@ -106,11 +70,11 @@ std::string Struct::generateHelperClass() const {
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
<< "])"
<< " extends AnyVal {\n";
int fieldIndex = 0;
unsigned fieldIndex = 0;
for (const auto &field : fields) {
if (!field->getName().empty()) {
s << field->generateGetter(fieldIndex) << "\n";
s << field->generateSetter(fieldIndex) << "\n";
s << generateGetter(fieldIndex) << "\n";
s << generateSetter(fieldIndex) << "\n";
}
fieldIndex++;
}
Expand Down Expand Up @@ -163,8 +127,41 @@ bool Struct::operator==(const Type &other) const {
return false;
}

Union::Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
std::shared_ptr<Location> location)
std::string Struct::generateSetter(unsigned fieldIndex) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string setter = handleReservedWords(field->getName(), "_=");
std::string parameterType = field->getType()->str();
std::string value = "value";
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
parameterType = "native.Ptr[" + parameterType + "]";
value = "!" + value;
}
std::stringstream s;
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
<< std::to_string(fieldIndex + 1) << " = " << value;
return s.str();
}

std::string Struct::generateGetter(unsigned fieldIndex) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string getter = handleReservedWords(field->getName());
std::string returnType = field->getType()->str();
std::string methodBody;
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
returnType = "native.Ptr[" + returnType + "]";
methodBody = "p._" + std::to_string(fieldIndex + 1);
} else {
methodBody = "!p._" + std::to_string(fieldIndex + 1);
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody;
return s.str();
}

Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t maxSize, std::shared_ptr<Location> location)
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {}

Expand Down
22 changes: 10 additions & 12 deletions bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
class Field : public TypeAndName {
public:
Field(std::string name, std::shared_ptr<Type> type);

std::string generateSetter(int fieldIndex);

std::string generateGetter(int fieldIndex);
};

class StructOrUnion {
public:
StructOrUnion(std::string name, std::vector<Field *> fields,
StructOrUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
std::shared_ptr<Location> location);

~StructOrUnion();

virtual std::shared_ptr<TypeDef> generateTypeDef() = 0;

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

protected:
std::string name;
std::vector<Field *> fields;
std::vector<std::shared_ptr<Field>> fields;
std::shared_ptr<Location> location;
};

class Struct : public StructOrUnion,
public Type,
public std::enable_shared_from_this<Struct> {
public:
Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
std::shared_ptr<Location> location);
Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t typeSize, std::shared_ptr<Location> location);

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

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

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

std::string generateSetter(unsigned fieldIndex) const;

std::string generateGetter(unsigned fieldIndex) const;

private:
/* type size is needed if number of fields is bigger than 22 */
uint64_t typeSize;
Expand All @@ -77,8 +75,8 @@ class Union : public StructOrUnion,
public ArrayType,
public std::enable_shared_from_this<Union> {
public:
Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
std::shared_ptr<Location> location);
Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t maxSize, std::shared_ptr<Location> location);

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

Expand Down
9 changes: 5 additions & 4 deletions bindgen/visitor/TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record) {
void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
uint64_t maxSize = 0;

std::vector<Field *> fields;
std::vector<std::shared_ptr<Field>> fields;

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

fields.push_back(new Field(fname, ftype));
fields.push_back(std::make_shared<Field>(fname, ftype));
}

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

int fieldCnt = 0;
std::vector<Field *> fields;
std::vector<std::shared_ptr<Field>> fields;

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

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

Expand Down

0 comments on commit 0e5ac3e

Please sign in to comment.