Skip to content

Commit 0ddbf20

Browse files
authored
Merge pull request #109 from kornilova-l/proper-implementation-of-usesType
Proper implementation of usesType method
2 parents d872294 + b92448d commit 0ddbf20

16 files changed

+158
-29
lines changed

bindgen/ir/Struct.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ StructOrUnion::~StructOrUnion() {
4949
}
5050
}
5151

52+
bool StructOrUnion::operator==(const StructOrUnion &other) const {
53+
if (this == &other) {
54+
return true;
55+
}
56+
if (isInstanceOf<const Struct>(&other)) {
57+
auto *s = dynamic_cast<const Struct *>(&other);
58+
if (name != s->name) {
59+
return false;
60+
}
61+
if (fields.size() != s->fields.size()) {
62+
return false;
63+
}
64+
for (size_t i = 0; i < fields.size(); i++) {
65+
if (*fields[i] != *s->fields[i]) {
66+
return false;
67+
}
68+
}
69+
return true;
70+
}
71+
return false;
72+
}
73+
5274
Struct::Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize)
5375
: StructOrUnion(std::move(name), std::move(fields)), typeSize(typeSize) {}
5476

@@ -112,12 +134,9 @@ std::string Struct::str() const {
112134
return ss.str();
113135
}
114136

115-
bool Struct::usesType(std::shared_ptr<Type> type) const {
116-
if (this == type.get()) {
117-
return true;
118-
}
137+
bool Struct::usesType(const std::shared_ptr<Type> &type) const {
119138
for (const auto &field : fields) {
120-
if (field->getType() == type) {
139+
if (*field->getType() == *type) {
121140
return true;
122141
}
123142
}

bindgen/ir/Struct.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class StructOrUnion {
3030

3131
std::string getName() const;
3232

33+
bool operator==(const StructOrUnion &other) const;
34+
3335
protected:
3436
std::string name;
3537
std::vector<Field *> fields;
@@ -52,10 +54,12 @@ class Struct : public StructOrUnion,
5254
*/
5355
bool hasHelperMethods() const;
5456

55-
bool usesType(std::shared_ptr<Type> type) const override;
57+
bool usesType(const std::shared_ptr<Type> &type) const override;
5658

5759
std::string str() const override;
5860

61+
using StructOrUnion::operator==;
62+
5963
private:
6064
/* type size is needed if number of fields is bigger than 22 */
6165
uint64_t typeSize;
@@ -71,6 +75,8 @@ class Union : public StructOrUnion,
7175

7276
std::string generateHelperClass() const override;
7377

78+
using StructOrUnion::operator==;
79+
7480
private:
7581
std::string getTypeAlias() const;
7682
};

bindgen/ir/TypeAndName.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ std::shared_ptr<Type> TypeAndName::getType() const { return type; }
99
std::string TypeAndName::getName() const { return name; }
1010

1111
void TypeAndName::setType(std::shared_ptr<Type> type) { this->type = type; }
12+
13+
bool TypeAndName::operator==(const TypeAndName &other) const {
14+
return name == other.name && *type == *other.type;
15+
}
16+
17+
bool TypeAndName::operator!=(const TypeAndName &other) const {
18+
return !(*this == other);
19+
}

bindgen/ir/TypeAndName.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class TypeAndName {
1919

2020
std::string getName() const;
2121

22+
bool operator==(const TypeAndName &other) const;
23+
24+
bool operator!=(const TypeAndName &other) const;
25+
2226
protected:
2327
std::string name;
2428
std::shared_ptr<Type> type;

bindgen/ir/TypeDef.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "TypeDef.h"
22
#include "../Utils.h"
3+
#include "Enum.h"
4+
#include "Struct.h"
35

46
TypeDef::TypeDef(std::string name, std::shared_ptr<Type> type)
57
: TypeAndName(std::move(name), std::move(type)) {}
@@ -16,8 +18,22 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
1618
return s;
1719
}
1820

19-
bool TypeDef::usesType(std::shared_ptr<Type> type) const {
20-
return this == type.get() || this->type == type;
21+
bool TypeDef::usesType(const std::shared_ptr<Type> &type) const {
22+
return *this->type == *type;
2123
}
2224

2325
std::string TypeDef::str() const { return handleReservedWords(name); }
26+
27+
bool TypeDef::operator==(const Type &other) const {
28+
if (this == &other) {
29+
return true;
30+
}
31+
if (isInstanceOf<const TypeDef>(&other)) {
32+
auto *typDef = dynamic_cast<const TypeDef *>(&other);
33+
if (name != typDef->name) {
34+
return false;
35+
}
36+
return *type == *typDef->type;
37+
}
38+
return false;
39+
}

bindgen/ir/TypeDef.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ class TypeDef : public TypeAndName, public Type {
1212
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1313
const TypeDef &type);
1414

15-
bool usesType(std::shared_ptr<Type> type) const override;
15+
bool usesType(const std::shared_ptr<Type> &type) const override;
1616

1717
std::string str() const override;
18+
19+
bool operator==(const Type &other) const override;
1820
};
1921

2022
#endif // SCALA_NATIVE_BINDGEN_TYPEDEF_H

bindgen/ir/types/ArrayType.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ std::string ArrayType::str() const {
99
uint64ToScalaNat(size) + "]";
1010
}
1111

12-
bool ArrayType::usesType(std::shared_ptr<Type> type) const {
13-
return this == type.get() || elementsType == type;
12+
bool ArrayType::usesType(const std::shared_ptr<Type> &type) const {
13+
return *elementsType == *type;
14+
}
15+
16+
bool ArrayType::operator==(const Type &other) const {
17+
if (this == &other) {
18+
return true;
19+
}
20+
if (isInstanceOf<const ArrayType>(&other)) {
21+
auto *arrayType = dynamic_cast<const ArrayType *>(&other);
22+
if (size != arrayType->size) {
23+
return false;
24+
}
25+
return *elementsType == *arrayType->elementsType.get();
26+
}
27+
return false;
1428
}

bindgen/ir/types/ArrayType.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
#define SCALA_NATIVE_BINDGEN_ARRAYTYPE_H
33

44
#include "Type.h"
5+
#include <stdint-gcc.h>
56

67
class ArrayType : public Type {
78
public:
89
ArrayType(std::shared_ptr<Type> elementsType, uint64_t size);
910

1011
~ArrayType() override = default;
1112

12-
bool usesType(std::shared_ptr<Type> type) const override;
13+
bool usesType(const std::shared_ptr<Type> &type) const override;
1314

1415
std::string str() const override;
1516

17+
bool operator==(const Type &other) const override;
18+
1619
private:
1720
const uint64_t size;
1821
std::shared_ptr<Type> elementsType;

bindgen/ir/types/FunctionPointerType.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "FunctionPointerType.h"
2+
#include "../../Utils.h"
23
#include <sstream>
34

45
FunctionPointerType::FunctionPointerType(
@@ -22,18 +23,43 @@ std::string FunctionPointerType::str() const {
2223
return ss.str();
2324
}
2425

25-
bool FunctionPointerType::usesType(std::shared_ptr<Type> type) const {
26-
if (this == type.get()) {
27-
return true;
28-
}
29-
if (returnType == type) {
26+
bool FunctionPointerType::usesType(const std::shared_ptr<Type> &type) const {
27+
if (*returnType == *type) {
3028
return true;
3129
}
3230

3331
for (const auto &parameterType : parametersTypes) {
34-
if (parameterType == type) {
32+
if (*parameterType == *type) {
3533
return true;
3634
}
3735
}
3836
return false;
3937
}
38+
39+
bool FunctionPointerType::operator==(const Type &other) const {
40+
if (this == &other) {
41+
return true;
42+
}
43+
if (isInstanceOf<const FunctionPointerType>(&other)) {
44+
auto *functionPointerType =
45+
dynamic_cast<const FunctionPointerType *>(&other);
46+
if (isVariadic != functionPointerType->isVariadic) {
47+
return false;
48+
}
49+
if (*returnType != *functionPointerType->returnType) {
50+
return false;
51+
}
52+
if (parametersTypes.size() !=
53+
functionPointerType->parametersTypes.size()) {
54+
return false;
55+
}
56+
for (size_t i = 0; i < parametersTypes.size(); i++) {
57+
if (*parametersTypes[i] !=
58+
*functionPointerType->parametersTypes[i]) {
59+
return false;
60+
}
61+
}
62+
return true;
63+
}
64+
return false;
65+
}

bindgen/ir/types/FunctionPointerType.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ class FunctionPointerType : public Type {
1313

1414
~FunctionPointerType() override = default;
1515

16-
bool usesType(std::shared_ptr<Type> type) const override;
16+
bool usesType(const std::shared_ptr<Type> &type) const override;
1717

1818
std::string str() const override;
1919

20+
bool operator==(const Type &other) const override;
21+
2022
private:
2123
std::shared_ptr<Type> returnType;
2224
std::vector<std::shared_ptr<Type>> parametersTypes;

0 commit comments

Comments
 (0)