Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add const qualifiers to fields of type Type #133

Merged
merged 1 commit into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindgen/TypeTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
const auto *fc = inner->getAs<clang::FunctionProtoType>();
std::shared_ptr<Type> returnType =
translate(fc->getReturnType(), avoid);
std::vector<std::shared_ptr<Type>> parametersTypes;
std::vector<std::shared_ptr<const Type>> parametersTypes;

for (const clang::QualType &param : fc->param_types()) {
parametersTypes.push_back(translate(param, avoid));
Expand Down
8 changes: 4 additions & 4 deletions bindgen/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static inline bool startsWith(const std::string &str,
}

template <typename T, typename PT> static inline bool isInstanceOf(PT *type) {
auto *p = dynamic_cast<T *>(type);
auto *p = dynamic_cast<const T *>(type);
return p != nullptr;
}

Expand All @@ -82,13 +82,13 @@ static inline std::string replaceChar(const std::string &str,
* Types may be wrapper in a chain of typedefs.
* @return true if given type is of type T or is an alias for type T.
*/
template <typename T> static inline bool isAliasForType(Type *type) {
template <typename T> static inline bool isAliasForType(const Type *type) {
if (isInstanceOf<T>(type)) {
return true;
}
auto *typeDef = dynamic_cast<TypeDef *>(type);
auto *typeDef = dynamic_cast<const TypeDef *>(type);
if (typeDef) {
return isAliasForType<T>(typeDef->getType().get());
return isAliasForType<const T>(typeDef->getType().get());
}
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "../Utils.h"
#include "Struct.h"

Parameter::Parameter(std::string name, std::shared_ptr<Type> type)
Parameter::Parameter(std::string name, std::shared_ptr<const Type> type)
: TypeAndName(std::move(name), type) {}

Function::Function(const std::string &name,
std::vector<std::shared_ptr<Parameter>> parameters,
std::shared_ptr<Type> retType, bool isVariadic)
std::shared_ptr<const Type> retType, bool isVariadic)
: name(name), scalaName(name), parameters(std::move(parameters)),
retType(std::move(retType)), isVariadic(isVariadic) {}

Expand Down
6 changes: 3 additions & 3 deletions bindgen/ir/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

class Parameter : public TypeAndName {
public:
Parameter(std::string name, std::shared_ptr<Type> type);
Parameter(std::string name, std::shared_ptr<const Type> type);
};

class Function {
public:
Function(const std::string &name,
std::vector<std::shared_ptr<Parameter>> parameters,
std::shared_ptr<Type> retType, bool isVariadic);
std::shared_ptr<const Type> retType, bool isVariadic);

friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
const Function &func);
Expand All @@ -41,7 +41,7 @@ class Function {
std::string name; // real name of the function
std::string scalaName; // not empty
std::vector<std::shared_ptr<Parameter>> parameters;
std::shared_ptr<Type> retType;
std::shared_ptr<const Type> retType;
bool isVariadic;
};

Expand Down
12 changes: 6 additions & 6 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ void IR::filterTypeDefs(const std::string &excludePrefix) {
}
}

void IR::replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,
std::shared_ptr<Type> newType) {
void IR::replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
std::shared_ptr<const Type> newType) {
for (auto &typeDef : typeDefs) {
if (typeDef->getType() == oldType) {
typeDef->setType(newType);
Expand Down Expand Up @@ -443,15 +443,15 @@ template <typename T> bool IR::inMainFile(const T &type) const {
/* generated TypeDef */
auto *typeDef = dynamic_cast<const TypeDef *>(&type);
assert(typeDef);
Type *innerType = typeDef->getType().get();
const Type *innerType = typeDef->getType().get();
if (isInstanceOf<Struct>(innerType)) {
return inMainFile(*dynamic_cast<Struct *>(innerType));
return inMainFile(*dynamic_cast<const Struct *>(innerType));
}
if (isInstanceOf<Union>(innerType)) {
return inMainFile(*dynamic_cast<Union *>(innerType));
return inMainFile(*dynamic_cast<const Union *>(innerType));
}
if (isInstanceOf<Enum>(innerType)) {
return inMainFile(*dynamic_cast<Enum *>(innerType));
return inMainFile(*dynamic_cast<const Enum *>(innerType));
}
}
return location && locationManager.inMainFile(*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 @@ -108,8 +108,8 @@ class IR {
/**
* Find all typedefs that use oldType and replace it with newType.
*/
void replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,
std::shared_ptr<Type> newType);
void replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
std::shared_ptr<const Type> newType);

/**
* @return true if given type is used only in typedefs.
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/LiteralDefine.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "LiteralDefine.h"

LiteralDefine::LiteralDefine(std::string name, std::string literal,
std::shared_ptr<Type> type)
std::shared_ptr<const Type> type)
: Define(std::move(name)), literal(std::move(literal)), type(type) {}

llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/LiteralDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class LiteralDefine : public Define {
public:
LiteralDefine(std::string name, std::string literal,
std::shared_ptr<Type> type);
std::shared_ptr<const Type> type);

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

private:
std::string literal;
std::shared_ptr<Type> type;
std::shared_ptr<const Type> type;
};

#endif // SCALA_NATIVE_BINDGEN_LITERALDEFINE_H
6 changes: 3 additions & 3 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "types/PrimitiveType.h"
#include <sstream>

Field::Field(std::string name, std::shared_ptr<Type> type)
Field::Field(std::string name, std::shared_ptr<const Type> type)
: TypeAndName(std::move(name), std::move(type)) {}

Field::Field(std::string name, std::shared_ptr<Type> type,
Field::Field(std::string name, std::shared_ptr<const Type> type,
uint64_t offsetInBits)
: TypeAndName(std::move(name), std::move(type)),
offsetInBits(offsetInBits) {}
Expand Down Expand Up @@ -262,7 +262,7 @@ std::string Union::generateHelperClass() const {
if (!field->getName().empty()) {
std::string getter = handleReservedWords(field->getName());
std::string setter = handleReservedWords(field->getName(), "_=");
std::shared_ptr<Type> ftype = field->getType();
std::shared_ptr<const Type> ftype = field->getType();
s << " def " << getter << ": native.Ptr[" << ftype->str()
<< "] = p.cast[native.Ptr[" << ftype->str() << "]]\n";

Expand Down
5 changes: 3 additions & 2 deletions bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

class Field : public TypeAndName {
public:
Field(std::string name, std::shared_ptr<Type> type);
Field(std::string name, std::shared_ptr<const Type> type);

Field(std::string name, std::shared_ptr<Type> type, uint64_t offsetInBits);
Field(std::string name, std::shared_ptr<const Type> type,
uint64_t offsetInBits);

uint64_t getOffsetInBits() const;

Expand Down
8 changes: 5 additions & 3 deletions bindgen/ir/TypeAndName.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "TypeAndName.h"
#include <clang/Tooling/Tooling.h>

TypeAndName::TypeAndName(std::string name, std::shared_ptr<Type> type)
TypeAndName::TypeAndName(std::string name, std::shared_ptr<const Type> type)
: name(std::move(name)), type(std::move(type)) {}

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

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

void TypeAndName::setType(std::shared_ptr<Type> type) { this->type = type; }
void TypeAndName::setType(std::shared_ptr<const Type> type) {
this->type = type;
}

bool TypeAndName::operator==(const TypeAndName &other) const {
if (this == &other) {
Expand Down
8 changes: 4 additions & 4 deletions bindgen/ir/TypeAndName.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*/
class TypeAndName {
public:
TypeAndName(std::string name, std::shared_ptr<Type> type);
TypeAndName(std::string name, std::shared_ptr<const Type> type);

std::shared_ptr<Type> getType() const;
std::shared_ptr<const Type> getType() const;

void setType(std::shared_ptr<Type> name);
void setType(std::shared_ptr<const Type> name);

std::string getName() const;

Expand All @@ -27,7 +27,7 @@ class TypeAndName {

protected:
std::string name;
std::shared_ptr<Type> type;
std::shared_ptr<const Type> type;
};

#endif // SCALA_NATIVE_BINDGEN_TYPEANDNAME_H
4 changes: 2 additions & 2 deletions bindgen/ir/TypeDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Enum.h"
#include "Struct.h"

TypeDef::TypeDef(std::string name, std::shared_ptr<Type> type,
TypeDef::TypeDef(std::string name, std::shared_ptr<const Type> type,
std::shared_ptr<Location> location)
: TypeAndName(std::move(name), std::move(type)),
location(std::move(location)) {}
Expand Down Expand Up @@ -36,7 +36,7 @@ bool TypeDef::operator==(const Type &other) const {
if (this == &other) {
return true;
}
if (isInstanceOf<const TypeDef>(&other)) {
if (isInstanceOf<TypeDef>(&other)) {
auto *typDef = dynamic_cast<const TypeDef *>(&other);
if (name != typDef->name) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/TypeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TypeDef : public TypeAndName, public Type {
public:
TypeDef(std::string name, std::shared_ptr<Type> type,
TypeDef(std::string name, std::shared_ptr<const Type> type,
std::shared_ptr<Location> location);

friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/Variable.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Variable.h"
#include "../Utils.h"

Variable::Variable(const std::string &name, std::shared_ptr<Type> type)
Variable::Variable(const std::string &name, std::shared_ptr<const Type> type)
: TypeAndName(name, type) {}

llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Variable &variable) {
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/Variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Variable : public TypeAndName {
public:
Variable(const std::string &name, std::shared_ptr<Type> type);
Variable(const std::string &name, std::shared_ptr<const Type> type);

friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
const Variable &variable);
Expand Down
5 changes: 2 additions & 3 deletions bindgen/ir/types/ArrayType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../../Utils.h"
#include "../Struct.h"

ArrayType::ArrayType(std::shared_ptr<Type> elementsType, uint64_t size)
ArrayType::ArrayType(std::shared_ptr<const Type> elementsType, uint64_t size)
: size(size), elementsType(std::move(elementsType)) {}

std::string ArrayType::str() const {
Expand All @@ -20,8 +20,7 @@ bool ArrayType::operator==(const Type &other) const {
if (this == &other) {
return true;
}
if (isInstanceOf<const ArrayType>(&other) &&
!isInstanceOf<const Union>(&other)) {
if (isInstanceOf<ArrayType>(&other) && !isInstanceOf<Union>(&other)) {
auto *arrayType = dynamic_cast<const ArrayType *>(&other);
if (size != arrayType->size) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions bindgen/ir/types/ArrayType.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class ArrayType : public virtual Type {
public:
ArrayType(std::shared_ptr<Type> elementsType, uint64_t size);
ArrayType(std::shared_ptr<const Type> elementsType, uint64_t size);

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

private:
const uint64_t size;
std::shared_ptr<Type> elementsType;
std::shared_ptr<const Type> elementsType;
};

#endif // SCALA_NATIVE_BINDGEN_ARRAYTYPE_H
11 changes: 5 additions & 6 deletions bindgen/ir/types/FunctionPointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <sstream>

FunctionPointerType::FunctionPointerType(
std::shared_ptr<Type> returnType,
const std::vector<std::shared_ptr<Type>> &parametersTypes, bool isVariadic)
std::shared_ptr<const Type> returnType,
std::vector<std::shared_ptr<const Type>> &parametersTypes, bool isVariadic)
: returnType(std::move(returnType)), parametersTypes(parametersTypes),
isVariadic(isVariadic) {}

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

bool FunctionPointerType::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
if (*returnType == *type ||
returnType.get()->usesType(type, stopOnTypeDefs)) {
if (*returnType == *type || returnType->usesType(type, stopOnTypeDefs)) {
return true;
}

for (const auto &parameterType : parametersTypes) {
if (*parameterType == *type ||
parameterType.get()->usesType(type, stopOnTypeDefs)) {
parameterType->usesType(type, stopOnTypeDefs)) {
return true;
}
}
Expand All @@ -43,7 +42,7 @@ bool FunctionPointerType::operator==(const Type &other) const {
if (this == &other) {
return true;
}
if (isInstanceOf<const FunctionPointerType>(&other)) {
if (isInstanceOf<FunctionPointerType>(&other)) {
auto *functionPointerType =
dynamic_cast<const FunctionPointerType *>(&other);
if (isVariadic != functionPointerType->isVariadic) {
Expand Down
8 changes: 4 additions & 4 deletions bindgen/ir/types/FunctionPointerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class FunctionPointerType : public Type {
public:
FunctionPointerType(
std::shared_ptr<Type> returnType,
const std::vector<std::shared_ptr<Type>> &parametersTypes,
std::shared_ptr<const Type> returnType,
std::vector<std::shared_ptr<const Type>> &parametersTypes,
bool isVariadic);

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

private:
std::shared_ptr<Type> returnType;
std::vector<std::shared_ptr<Type>> parametersTypes;
std::shared_ptr<const Type> returnType;
std::vector<std::shared_ptr<const Type>> parametersTypes;
bool isVariadic;
};

Expand Down
5 changes: 3 additions & 2 deletions bindgen/ir/types/PointerType.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "PointerType.h"
#include "../../Utils.h"

PointerType::PointerType(std::shared_ptr<Type> type) : type(std::move(type)) {}
PointerType::PointerType(std::shared_ptr<const Type> type)
: type(std::move(type)) {}

std::string PointerType::str() const {
return "native.Ptr[" + type->str() + "]";
Expand All @@ -17,7 +18,7 @@ bool PointerType::operator==(const Type &other) const {
if (this == &other) {
return true;
}
if (isInstanceOf<const PointerType>(&other)) {
if (isInstanceOf<PointerType>(&other)) {
auto *pointerType = dynamic_cast<const PointerType *>(&other);
return *type == *pointerType->type;
}
Expand Down
Loading