Skip to content

Commit 3e13e92

Browse files
authored
Merge pull request #133 from kornilova-l/add-const-qualifiers
Add const qualifiers to fields of type `Type`
2 parents 40f6670 + e5d86c5 commit 3e13e92

23 files changed

+60
-59
lines changed

bindgen/TypeTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
4343
const auto *fc = inner->getAs<clang::FunctionProtoType>();
4444
std::shared_ptr<Type> returnType =
4545
translate(fc->getReturnType(), avoid);
46-
std::vector<std::shared_ptr<Type>> parametersTypes;
46+
std::vector<std::shared_ptr<const Type>> parametersTypes;
4747

4848
for (const clang::QualType &param : fc->param_types()) {
4949
parametersTypes.push_back(translate(param, avoid));

bindgen/Utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static inline bool startsWith(const std::string &str,
6464
}
6565

6666
template <typename T, typename PT> static inline bool isInstanceOf(PT *type) {
67-
auto *p = dynamic_cast<T *>(type);
67+
auto *p = dynamic_cast<const T *>(type);
6868
return p != nullptr;
6969
}
7070

@@ -82,13 +82,13 @@ static inline std::string replaceChar(const std::string &str,
8282
* Types may be wrapper in a chain of typedefs.
8383
* @return true if given type is of type T or is an alias for type T.
8484
*/
85-
template <typename T> static inline bool isAliasForType(Type *type) {
85+
template <typename T> static inline bool isAliasForType(const Type *type) {
8686
if (isInstanceOf<T>(type)) {
8787
return true;
8888
}
89-
auto *typeDef = dynamic_cast<TypeDef *>(type);
89+
auto *typeDef = dynamic_cast<const TypeDef *>(type);
9090
if (typeDef) {
91-
return isAliasForType<T>(typeDef->getType().get());
91+
return isAliasForType<const T>(typeDef->getType().get());
9292
}
9393
return false;
9494
}

bindgen/ir/Function.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#include "../Utils.h"
33
#include "Struct.h"
44

5-
Parameter::Parameter(std::string name, std::shared_ptr<Type> type)
5+
Parameter::Parameter(std::string name, std::shared_ptr<const Type> type)
66
: TypeAndName(std::move(name), type) {}
77

88
Function::Function(const std::string &name,
99
std::vector<std::shared_ptr<Parameter>> parameters,
10-
std::shared_ptr<Type> retType, bool isVariadic)
10+
std::shared_ptr<const Type> retType, bool isVariadic)
1111
: name(name), scalaName(name), parameters(std::move(parameters)),
1212
retType(std::move(retType)), isVariadic(isVariadic) {}
1313

bindgen/ir/Function.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
class Parameter : public TypeAndName {
1111
public:
12-
Parameter(std::string name, std::shared_ptr<Type> type);
12+
Parameter(std::string name, std::shared_ptr<const Type> type);
1313
};
1414

1515
class Function {
1616
public:
1717
Function(const std::string &name,
1818
std::vector<std::shared_ptr<Parameter>> parameters,
19-
std::shared_ptr<Type> retType, bool isVariadic);
19+
std::shared_ptr<const Type> retType, bool isVariadic);
2020

2121
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2222
const Function &func);
@@ -41,7 +41,7 @@ class Function {
4141
std::string name; // real name of the function
4242
std::string scalaName; // not empty
4343
std::vector<std::shared_ptr<Parameter>> parameters;
44-
std::shared_ptr<Type> retType;
44+
std::shared_ptr<const Type> retType;
4545
bool isVariadic;
4646
};
4747

bindgen/ir/IR.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ void IR::filterTypeDefs(const std::string &excludePrefix) {
260260
}
261261
}
262262

263-
void IR::replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,
264-
std::shared_ptr<Type> newType) {
263+
void IR::replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
264+
std::shared_ptr<const Type> newType) {
265265
for (auto &typeDef : typeDefs) {
266266
if (typeDef->getType() == oldType) {
267267
typeDef->setType(newType);
@@ -443,15 +443,15 @@ template <typename T> bool IR::inMainFile(const T &type) const {
443443
/* generated TypeDef */
444444
auto *typeDef = dynamic_cast<const TypeDef *>(&type);
445445
assert(typeDef);
446-
Type *innerType = typeDef->getType().get();
446+
const Type *innerType = typeDef->getType().get();
447447
if (isInstanceOf<Struct>(innerType)) {
448-
return inMainFile(*dynamic_cast<Struct *>(innerType));
448+
return inMainFile(*dynamic_cast<const Struct *>(innerType));
449449
}
450450
if (isInstanceOf<Union>(innerType)) {
451-
return inMainFile(*dynamic_cast<Union *>(innerType));
451+
return inMainFile(*dynamic_cast<const Union *>(innerType));
452452
}
453453
if (isInstanceOf<Enum>(innerType)) {
454-
return inMainFile(*dynamic_cast<Enum *>(innerType));
454+
return inMainFile(*dynamic_cast<const Enum *>(innerType));
455455
}
456456
}
457457
return location && locationManager.inMainFile(*location);

bindgen/ir/IR.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class IR {
108108
/**
109109
* Find all typedefs that use oldType and replace it with newType.
110110
*/
111-
void replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,
112-
std::shared_ptr<Type> newType);
111+
void replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
112+
std::shared_ptr<const Type> newType);
113113

114114
/**
115115
* @return true if given type is used only in typedefs.

bindgen/ir/LiteralDefine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "LiteralDefine.h"
22

33
LiteralDefine::LiteralDefine(std::string name, std::string literal,
4-
std::shared_ptr<Type> type)
4+
std::shared_ptr<const Type> type)
55
: Define(std::move(name)), literal(std::move(literal)), type(type) {}
66

77
llvm::raw_ostream &operator<<(llvm::raw_ostream &s,

bindgen/ir/LiteralDefine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class LiteralDefine : public Define {
99
public:
1010
LiteralDefine(std::string name, std::string literal,
11-
std::shared_ptr<Type> type);
11+
std::shared_ptr<const Type> type);
1212

1313
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1414
const LiteralDefine &literalDefine);
@@ -17,7 +17,7 @@ class LiteralDefine : public Define {
1717

1818
private:
1919
std::string literal;
20-
std::shared_ptr<Type> type;
20+
std::shared_ptr<const Type> type;
2121
};
2222

2323
#endif // SCALA_NATIVE_BINDGEN_LITERALDEFINE_H

bindgen/ir/Struct.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include "types/PrimitiveType.h"
66
#include <sstream>
77

8-
Field::Field(std::string name, std::shared_ptr<Type> type)
8+
Field::Field(std::string name, std::shared_ptr<const Type> type)
99
: TypeAndName(std::move(name), std::move(type)) {}
1010

11-
Field::Field(std::string name, std::shared_ptr<Type> type,
11+
Field::Field(std::string name, std::shared_ptr<const Type> type,
1212
uint64_t offsetInBits)
1313
: TypeAndName(std::move(name), std::move(type)),
1414
offsetInBits(offsetInBits) {}
@@ -262,7 +262,7 @@ std::string Union::generateHelperClass() const {
262262
if (!field->getName().empty()) {
263263
std::string getter = handleReservedWords(field->getName());
264264
std::string setter = handleReservedWords(field->getName(), "_=");
265-
std::shared_ptr<Type> ftype = field->getType();
265+
std::shared_ptr<const Type> ftype = field->getType();
266266
s << " def " << getter << ": native.Ptr[" << ftype->str()
267267
<< "] = p.cast[native.Ptr[" << ftype->str() << "]]\n";
268268

bindgen/ir/Struct.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
class Field : public TypeAndName {
1313
public:
14-
Field(std::string name, std::shared_ptr<Type> type);
14+
Field(std::string name, std::shared_ptr<const Type> type);
1515

16-
Field(std::string name, std::shared_ptr<Type> type, uint64_t offsetInBits);
16+
Field(std::string name, std::shared_ptr<const Type> type,
17+
uint64_t offsetInBits);
1718

1819
uint64_t getOffsetInBits() const;
1920

bindgen/ir/TypeAndName.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#include "TypeAndName.h"
22
#include <clang/Tooling/Tooling.h>
33

4-
TypeAndName::TypeAndName(std::string name, std::shared_ptr<Type> type)
4+
TypeAndName::TypeAndName(std::string name, std::shared_ptr<const Type> type)
55
: name(std::move(name)), type(std::move(type)) {}
66

7-
std::shared_ptr<Type> TypeAndName::getType() const { return type; }
7+
std::shared_ptr<const Type> TypeAndName::getType() const { return type; }
88

99
std::string TypeAndName::getName() const { return name; }
1010

11-
void TypeAndName::setType(std::shared_ptr<Type> type) { this->type = type; }
11+
void TypeAndName::setType(std::shared_ptr<const Type> type) {
12+
this->type = type;
13+
}
1214

1315
bool TypeAndName::operator==(const TypeAndName &other) const {
1416
if (this == &other) {

bindgen/ir/TypeAndName.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
*/
1212
class TypeAndName {
1313
public:
14-
TypeAndName(std::string name, std::shared_ptr<Type> type);
14+
TypeAndName(std::string name, std::shared_ptr<const Type> type);
1515

16-
std::shared_ptr<Type> getType() const;
16+
std::shared_ptr<const Type> getType() const;
1717

18-
void setType(std::shared_ptr<Type> name);
18+
void setType(std::shared_ptr<const Type> name);
1919

2020
std::string getName() const;
2121

@@ -27,7 +27,7 @@ class TypeAndName {
2727

2828
protected:
2929
std::string name;
30-
std::shared_ptr<Type> type;
30+
std::shared_ptr<const Type> type;
3131
};
3232

3333
#endif // SCALA_NATIVE_BINDGEN_TYPEANDNAME_H

bindgen/ir/TypeDef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "Enum.h"
44
#include "Struct.h"
55

6-
TypeDef::TypeDef(std::string name, std::shared_ptr<Type> type,
6+
TypeDef::TypeDef(std::string name, std::shared_ptr<const Type> type,
77
std::shared_ptr<Location> location)
88
: TypeAndName(std::move(name), std::move(type)),
99
location(std::move(location)) {}
@@ -36,7 +36,7 @@ bool TypeDef::operator==(const Type &other) const {
3636
if (this == &other) {
3737
return true;
3838
}
39-
if (isInstanceOf<const TypeDef>(&other)) {
39+
if (isInstanceOf<TypeDef>(&other)) {
4040
auto *typDef = dynamic_cast<const TypeDef *>(&other);
4141
if (name != typDef->name) {
4242
return false;

bindgen/ir/TypeDef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TypeDef : public TypeAndName, public Type {
1010
public:
11-
TypeDef(std::string name, std::shared_ptr<Type> type,
11+
TypeDef(std::string name, std::shared_ptr<const Type> type,
1212
std::shared_ptr<Location> location);
1313

1414
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,

bindgen/ir/Variable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "Variable.h"
22
#include "../Utils.h"
33

4-
Variable::Variable(const std::string &name, std::shared_ptr<Type> type)
4+
Variable::Variable(const std::string &name, std::shared_ptr<const Type> type)
55
: TypeAndName(name, type) {}
66

77
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Variable &variable) {

bindgen/ir/Variable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Variable : public TypeAndName {
88
public:
9-
Variable(const std::string &name, std::shared_ptr<Type> type);
9+
Variable(const std::string &name, std::shared_ptr<const Type> type);
1010

1111
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1212
const Variable &variable);

bindgen/ir/types/ArrayType.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "../../Utils.h"
33
#include "../Struct.h"
44

5-
ArrayType::ArrayType(std::shared_ptr<Type> elementsType, uint64_t size)
5+
ArrayType::ArrayType(std::shared_ptr<const Type> elementsType, uint64_t size)
66
: size(size), elementsType(std::move(elementsType)) {}
77

88
std::string ArrayType::str() const {
@@ -20,8 +20,7 @@ bool ArrayType::operator==(const Type &other) const {
2020
if (this == &other) {
2121
return true;
2222
}
23-
if (isInstanceOf<const ArrayType>(&other) &&
24-
!isInstanceOf<const Union>(&other)) {
23+
if (isInstanceOf<ArrayType>(&other) && !isInstanceOf<Union>(&other)) {
2524
auto *arrayType = dynamic_cast<const ArrayType *>(&other);
2625
if (size != arrayType->size) {
2726
return false;

bindgen/ir/types/ArrayType.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class ArrayType : public virtual Type {
77
public:
8-
ArrayType(std::shared_ptr<Type> elementsType, uint64_t size);
8+
ArrayType(std::shared_ptr<const Type> elementsType, uint64_t size);
99

1010
bool usesType(const std::shared_ptr<Type> &type,
1111
bool stopOnTypeDefs) const override;
@@ -16,7 +16,7 @@ class ArrayType : public virtual Type {
1616

1717
private:
1818
const uint64_t size;
19-
std::shared_ptr<Type> elementsType;
19+
std::shared_ptr<const Type> elementsType;
2020
};
2121

2222
#endif // SCALA_NATIVE_BINDGEN_ARRAYTYPE_H

bindgen/ir/types/FunctionPointerType.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <sstream>
44

55
FunctionPointerType::FunctionPointerType(
6-
std::shared_ptr<Type> returnType,
7-
const std::vector<std::shared_ptr<Type>> &parametersTypes, bool isVariadic)
6+
std::shared_ptr<const Type> returnType,
7+
std::vector<std::shared_ptr<const Type>> &parametersTypes, bool isVariadic)
88
: returnType(std::move(returnType)), parametersTypes(parametersTypes),
99
isVariadic(isVariadic) {}
1010

@@ -25,14 +25,13 @@ std::string FunctionPointerType::str() const {
2525

2626
bool FunctionPointerType::usesType(const std::shared_ptr<Type> &type,
2727
bool stopOnTypeDefs) const {
28-
if (*returnType == *type ||
29-
returnType.get()->usesType(type, stopOnTypeDefs)) {
28+
if (*returnType == *type || returnType->usesType(type, stopOnTypeDefs)) {
3029
return true;
3130
}
3231

3332
for (const auto &parameterType : parametersTypes) {
3433
if (*parameterType == *type ||
35-
parameterType.get()->usesType(type, stopOnTypeDefs)) {
34+
parameterType->usesType(type, stopOnTypeDefs)) {
3635
return true;
3736
}
3837
}
@@ -43,7 +42,7 @@ bool FunctionPointerType::operator==(const Type &other) const {
4342
if (this == &other) {
4443
return true;
4544
}
46-
if (isInstanceOf<const FunctionPointerType>(&other)) {
45+
if (isInstanceOf<FunctionPointerType>(&other)) {
4746
auto *functionPointerType =
4847
dynamic_cast<const FunctionPointerType *>(&other);
4948
if (isVariadic != functionPointerType->isVariadic) {

bindgen/ir/types/FunctionPointerType.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
class FunctionPointerType : public Type {
88
public:
99
FunctionPointerType(
10-
std::shared_ptr<Type> returnType,
11-
const std::vector<std::shared_ptr<Type>> &parametersTypes,
10+
std::shared_ptr<const Type> returnType,
11+
std::vector<std::shared_ptr<const Type>> &parametersTypes,
1212
bool isVariadic);
1313

1414
bool usesType(const std::shared_ptr<Type> &type,
@@ -19,8 +19,8 @@ class FunctionPointerType : public Type {
1919
bool operator==(const Type &other) const override;
2020

2121
private:
22-
std::shared_ptr<Type> returnType;
23-
std::vector<std::shared_ptr<Type>> parametersTypes;
22+
std::shared_ptr<const Type> returnType;
23+
std::vector<std::shared_ptr<const Type>> parametersTypes;
2424
bool isVariadic;
2525
};
2626

bindgen/ir/types/PointerType.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "PointerType.h"
22
#include "../../Utils.h"
33

4-
PointerType::PointerType(std::shared_ptr<Type> type) : type(std::move(type)) {}
4+
PointerType::PointerType(std::shared_ptr<const Type> type)
5+
: type(std::move(type)) {}
56

67
std::string PointerType::str() const {
78
return "native.Ptr[" + type->str() + "]";
@@ -17,7 +18,7 @@ bool PointerType::operator==(const Type &other) const {
1718
if (this == &other) {
1819
return true;
1920
}
20-
if (isInstanceOf<const PointerType>(&other)) {
21+
if (isInstanceOf<PointerType>(&other)) {
2122
auto *pointerType = dynamic_cast<const PointerType *>(&other);
2223
return *type == *pointerType->type;
2324
}

0 commit comments

Comments
 (0)