Skip to content

Commit 7f1ebd4

Browse files
authored
Merge pull request #139 from kornilova-l/locatable-type
Implement LocatableType class
2 parents 596b12d + 97d3dea commit 7f1ebd4

26 files changed

+116
-89
lines changed

bindgen/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ add_executable(bindgen
7474
ir/Location.cpp
7575
ir/LocationManager.h
7676
ir/LocationManager.cpp
77+
ir/LocatableType.cpp
78+
ir/LocatableType.h
7779
)
7880

7981
if (STATIC_LINKING)

bindgen/ir/Enum.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ int64_t Enumerator::getValue() { return value; }
1010
Enum::Enum(std::string name, std::string type,
1111
std::vector<Enumerator> enumerators,
1212
std::shared_ptr<Location> location)
13-
: PrimitiveType(std::move(type)), name(std::move(name)),
14-
enumerators(std::move(enumerators)), location(std::move(location)) {}
13+
: PrimitiveType(std::move(type)), LocatableType(std::move(location)),
14+
name(std::move(name)), enumerators(std::move(enumerators)) {}
1515

1616
bool Enum::isAnonymous() const { return name.empty(); }
1717

@@ -55,5 +55,3 @@ std::string Enum::getTypeAlias() const {
5555
assert(!isAnonymous());
5656
return "enum_" + name;
5757
}
58-
59-
std::shared_ptr<Location> Enum::getLocation() const { return location; }

bindgen/ir/Enum.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SCALA_NATIVE_BINDGEN_ENUM_H
22
#define SCALA_NATIVE_BINDGEN_ENUM_H
33

4+
#include "LocatableType.h"
45
#include "TypeDef.h"
56
#include "types/PrimitiveType.h"
67
#include <vector>
@@ -18,7 +19,7 @@ class Enumerator {
1819
int64_t value;
1920
};
2021

21-
class Enum : public PrimitiveType {
22+
class Enum : public PrimitiveType, public LocatableType {
2223
public:
2324
Enum(std::string name, std::string type,
2425
std::vector<Enumerator> enumerators,
@@ -34,12 +35,9 @@ class Enum : public PrimitiveType {
3435

3536
std::string getTypeAlias() const;
3637

37-
std::shared_ptr<Location> getLocation() const;
38-
3938
private:
4039
std::string name; // might be empty
4140
std::vector<Enumerator> enumerators;
42-
std::shared_ptr<Location> location;
4341
};
4442

4543
#endif // SCALA_NATIVE_BINDGEN_ENUM_H

bindgen/ir/Function.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
3131
return s;
3232
}
3333

34-
bool Function::usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
34+
bool Function::usesType(std::shared_ptr<const Type> type,
35+
bool stopOnTypeDefs) const {
3536
if (*retType == *type || retType->usesType(type, stopOnTypeDefs)) {
3637
return true;
3738
}

bindgen/ir/Function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Function {
2121
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2222
const Function &func);
2323

24-
bool usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const;
24+
bool usesType(std::shared_ptr<const Type> type, bool stopOnTypeDefs) const;
2525

2626
std::string getName() const;
2727

bindgen/ir/IR.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
116116
for (const auto &typeDef : ir.typeDefs) {
117117
if (ir.shouldOutput(typeDef)) {
118118
s << *typeDef;
119-
} else if (isAliasForOpaqueType(typeDef.get()) &&
120-
ir.inMainFile(*typeDef)) {
119+
} else if (typeDef->hasLocation() &&
120+
isAliasForOpaqueType(typeDef.get()) &&
121+
ir.locationManager.inMainFile(*typeDef->getLocation())) {
121122
llvm::errs() << "Warning: type alias " + typeDef->getName()
122123
<< " is skipped because it is an unused alias for "
123124
"incomplete type."
@@ -271,7 +272,8 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr<const Type> oldType,
271272

272273
template <typename T>
273274
bool IR::isTypeUsed(const std::vector<T> &declarations,
274-
std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
275+
std::shared_ptr<const Type> type,
276+
bool stopOnTypeDefs) const {
275277
for (const auto &decl : declarations) {
276278
if (decl->usesType(type, stopOnTypeDefs)) {
277279
return true;
@@ -280,7 +282,8 @@ bool IR::isTypeUsed(const std::vector<T> &declarations,
280282
return false;
281283
}
282284

283-
bool IR::typeIsUsedOnlyInTypeDefs(const std::shared_ptr<Type> &type) const {
285+
bool IR::typeIsUsedOnlyInTypeDefs(
286+
const std::shared_ptr<const Type> &type) const {
284287
/* varDefines are not checked here because they are simply
285288
* aliases for variables.*/
286289
return !(
@@ -289,7 +292,7 @@ bool IR::typeIsUsedOnlyInTypeDefs(const std::shared_ptr<Type> &type) const {
289292
isTypeUsed(literalDefines, type, true));
290293
}
291294

292-
bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
295+
bool IR::isTypeUsed(const std::shared_ptr<const Type> &type,
293296
bool checkRecursively) const {
294297
if (checkRecursively) {
295298
if (isTypeUsed(functions, type, true) ||
@@ -437,26 +440,6 @@ IR::~IR() {
437440
varDefines.clear();
438441
}
439442

440-
template <typename T> bool IR::inMainFile(const T &type) const {
441-
std::shared_ptr<Location> location = type.getLocation();
442-
if (!location) {
443-
/* generated TypeDef */
444-
auto *typeDef = dynamic_cast<const TypeDef *>(&type);
445-
assert(typeDef);
446-
const Type *innerType = typeDef->getType().get();
447-
if (isInstanceOf<Struct>(innerType)) {
448-
return inMainFile(*dynamic_cast<const Struct *>(innerType));
449-
}
450-
if (isInstanceOf<Union>(innerType)) {
451-
return inMainFile(*dynamic_cast<const Union *>(innerType));
452-
}
453-
if (isInstanceOf<Enum>(innerType)) {
454-
return inMainFile(*dynamic_cast<const Enum *>(innerType));
455-
}
456-
}
457-
return location && locationManager.inMainFile(*location);
458-
}
459-
460443
template <typename T>
461444
bool IR::hasOutputtedDeclaration(
462445
const std::vector<std::shared_ptr<T>> &declarations) const {
@@ -468,20 +451,13 @@ bool IR::hasOutputtedDeclaration(
468451
return false;
469452
}
470453

471-
template <typename T>
472-
bool IR::shouldOutput(const std::shared_ptr<T> &type) const {
454+
bool IR::shouldOutput(const std::shared_ptr<const LocatableType> &type) const {
473455
if (isTypeUsed(type, true)) {
474456
return true;
475457
}
476-
if (!inMainFile(*type)) {
477-
/* remove unused types from included files */
458+
if (isAliasForOpaqueType(type.get())) {
478459
return false;
479460
}
480-
auto *typeDef = dynamic_cast<TypeDef *>(type.get());
481-
if (typeDef) {
482-
/* unused typedefs from main file are printed only if they are not
483-
* aliases for an opaque type. */
484-
return !isAliasForOpaqueType(typeDef);
485-
}
486-
return true;
461+
/* remove unused types from included files */
462+
return locationManager.inMainFile(*type->getLocation());
487463
}

bindgen/ir/IR.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,24 @@ class IR {
114114
/**
115115
* @return true if given type is used only in typedefs.
116116
*/
117-
bool typeIsUsedOnlyInTypeDefs(const std::shared_ptr<Type> &type) const;
117+
bool
118+
typeIsUsedOnlyInTypeDefs(const std::shared_ptr<const Type> &type) const;
118119

119120
/**
120121
* @param checkRecursively if this parameter is true then the method will
121122
* output false if the type is used only in unused types
122123
* @return true if type is used in one of declarations
123124
*/
124-
bool isTypeUsed(const std::shared_ptr<Type> &type,
125+
bool isTypeUsed(const std::shared_ptr<const Type> &type,
125126
bool checkRecursively = false) const;
126127

127128
/**
128129
* @return true if type is used in one of given declarations.
129130
*/
130131
template <typename T>
131132
bool isTypeUsed(const std::vector<T> &declarations,
132-
std::shared_ptr<Type> type, bool stopOnTypeDefs) const;
133+
std::shared_ptr<const Type> type,
134+
bool stopOnTypeDefs) const;
133135

134136
void setScalaNames();
135137

@@ -146,19 +148,15 @@ class IR {
146148
T getDeclarationWithName(const std::vector<T> &declarations,
147149
const std::string &name) const;
148150

149-
template <typename T> bool inMainFile(const T &type) const;
150-
151151
/**
152-
* @tparam T Enum, Struct, Union or TypeDef
153152
* @return true if the type will be printed.
154153
* Following types are not printed:
155154
* - Unused types from included headers
156155
* - Unused typedefs from main header if they reference an opaque
157156
* type (if such typedef is used then true is returned but error
158157
* message is printed when bindings are generated)
159158
*/
160-
template <typename T>
161-
bool shouldOutput(const std::shared_ptr<T> &type) const;
159+
bool shouldOutput(const std::shared_ptr<const LocatableType> &type) const;
162160

163161
/**
164162
* @tparam T Struct or Union

bindgen/ir/LiteralDefine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1111
return s;
1212
}
1313

14-
bool LiteralDefine::usesType(const std::shared_ptr<Type> &type,
14+
bool LiteralDefine::usesType(const std::shared_ptr<const Type> &type,
1515
bool stopOnTypeDefs) const {
1616
return *this->type == *type ||
1717
this->type.get()->usesType(type, stopOnTypeDefs);

bindgen/ir/LiteralDefine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class LiteralDefine : public Define {
1313
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1414
const LiteralDefine &literalDefine);
1515

16-
bool usesType(const std::shared_ptr<Type> &type, bool stopOnTypeDefs) const;
16+
bool usesType(const std::shared_ptr<const Type> &type,
17+
bool stopOnTypeDefs) const;
1718

1819
private:
1920
std::string literal;

bindgen/ir/LocatableType.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "LocatableType.h"
2+
3+
LocatableType::LocatableType(std::shared_ptr<Location> location)
4+
: location(std::move(location)) {}
5+
6+
std::shared_ptr<Location> LocatableType::getLocation() const {
7+
return location;
8+
}

0 commit comments

Comments
 (0)