Skip to content

Commit

Permalink
Change type of StructOrUnion::fields to vector of shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
kornilova203 committed Jul 13, 2018
1 parent 67ef777 commit 699f4d5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 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
17 changes: 6 additions & 11 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ std::string Field::generateGetter(int fieldIndex) {
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 +72,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 @@ -163,8 +158,8 @@ 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)
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
14 changes: 6 additions & 8 deletions bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class Field : public TypeAndName {

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 +37,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 Down Expand Up @@ -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 699f4d5

Please sign in to comment.