Skip to content

Commit 8db4611

Browse files
committed
Rename SimpleType and remove unused imports
1 parent 494e4d9 commit 8db4611

21 files changed

+90
-118
lines changed

bindgen/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ add_executable(bindgen
5656
ir/PossibleVarDefine.h
5757
ir/types/Type.cpp
5858
ir/types/Type.h
59-
ir/types/SimpleType.cpp
60-
ir/types/SimpleType.h
59+
ir/types/PrimitiveType.cpp
60+
ir/types/PrimitiveType.h
6161
ir/types/PointerType.cpp
6262
ir/types/PointerType.h
6363
ir/types/FunctionPointerType.cpp

bindgen/TypeTranslator.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include "TypeTranslator.h"
22
#include "Utils.h"
3-
#include "ir/types/ArrayType.h"
43
#include "ir/types/FunctionPointerType.h"
54
#include "ir/types/PointerType.h"
6-
#include "ir/types/SimpleType.h"
75

86
TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
97
: ctx(ctx_), ir(ir), typeMap() {
@@ -67,13 +65,13 @@ Type *TypeTranslator::TranslatePointer(const clang::QualType &pte,
6765

6866
// Take care of void*
6967
if (as->getKind() == clang::BuiltinType::Void) {
70-
return new PointerType(new SimpleType("Byte"));
68+
return new PointerType(new PrimitiveType("Byte"));
7169
}
7270

7371
// Take care of char*
7472
if (as->getKind() == clang::BuiltinType::Char_S ||
7573
as->getKind() == clang::BuiltinType::SChar) {
76-
return new SimpleType("native.CString");
74+
return new PrimitiveType("native.CString");
7775
}
7876
}
7977

@@ -84,15 +82,15 @@ Type *TypeTranslator::translateStruct(const clang::QualType &qtpe) {
8482
if (qtpe->hasUnnamedOrLocalType()) {
8583
// TODO: Verify that the local part is not a problem
8684
uint64_t size = ctx->getTypeSize(qtpe);
87-
return new ArrayType(new SimpleType("Byte"), size);
85+
return new ArrayType(new PrimitiveType("Byte"), size);
8886
}
8987

9088
std::string name = qtpe.getUnqualifiedType().getAsString();
9189

9290
// TODO: do it properly
9391
size_t f = name.find(std::string("struct __dirstream"));
9492
if (f != std::string::npos) {
95-
return new ArrayType(new SimpleType("Byte"), 320);
93+
return new ArrayType(new PrimitiveType("Byte"), 320);
9694
}
9795

9896
auto it = aliasesMap.find(name);
@@ -109,7 +107,7 @@ Type *TypeTranslator::translateUnion(const clang::QualType &qtpe) {
109107
if (qtpe->hasUnnamedOrLocalType()) {
110108
// TODO: Verify that the local part is not a problem
111109
uint64_t size = ctx->getTypeSize(qtpe);
112-
return new ArrayType(new SimpleType("Byte"), size);
110+
return new ArrayType(new PrimitiveType("Byte"), size);
113111
}
114112

115113
std::string name = qtpe.getUnqualifiedType().getAsString();
@@ -152,7 +150,7 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
152150
// This is a type that we want to avoid the usage.
153151
// Êxample: A struct that has a pointer to itself
154152
uint64_t size = ctx->getTypeSize(tpe);
155-
return new ArrayType(new SimpleType("Byte"), size);
153+
return new ArrayType(new PrimitiveType("Byte"), size);
156154
}
157155

158156
if (tpe->isFunctionPointerType()) {
@@ -180,7 +178,7 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
180178

181179
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
182180
if (found != typeMap.end()) {
183-
return new SimpleType(found->second);
181+
return new PrimitiveType(found->second);
184182
} else {
185183
return ir.getTypeDefWithName(
186184
qtpe.getUnqualifiedType().getAsString());

bindgen/TypeTranslator.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#pragma once
22

33
#include "ir/IR.h"
4-
#include "ir/types/Type.h"
5-
#include <clang/AST/AST.h>
6-
#include <clang/AST/ASTContext.h>
74
#include <clang/Tooling/Tooling.h>
85

96
class TypeTranslator {
@@ -26,8 +23,15 @@ class TypeTranslator {
2623
private:
2724
clang::ASTContext *ctx;
2825
IR &ir;
26+
27+
/**
28+
* Primitive types
29+
*/
2930
std::map<std::string, std::string> typeMap;
3031

32+
/**
33+
* Maps C struct, union or enum name to Type alias
34+
*/
3135
std::map<std::string, Type *> aliasesMap;
3236

3337
Type *translateEnum(const clang::QualType &qtpe);

bindgen/defines/DefineFinder.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "DefineFinder.h"
2-
#include "../ir/types/SimpleType.h"
2+
#include "../ir/types/PrimitiveType.h"
33

44
#include <llvm/ADT/APInt.h>
55
#include <sstream>
@@ -42,7 +42,8 @@ void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
4242
clang::Token stringToken = (*tokens)[0];
4343
std::string literal(stringToken.getLiteralData(),
4444
stringToken.getLength());
45-
ir.addLiteralDefine(macroName, "c" + literal, new SimpleType("native.CString"));
45+
ir.addLiteralDefine(macroName, "c" + literal,
46+
new PrimitiveType("native.CString"));
4647
} else if (tokens->size() == 1 &&
4748
(*tokens)[0].getKind() == clang::tok::identifier) {
4849
// token might be a variable
@@ -116,19 +117,19 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,
116117
if (parser.isLongLong) {
117118
/* literal has `LL` ending. `long long` is represented as `Long`
118119
* in Scala Native */
119-
type = new SimpleType("native.CLongLong");
120+
type = new PrimitiveType("native.CLongLong");
120121

121122
/* must fit into Scala integer type */
122-
if (!integerFitsIntoType<long, ulong>(parser, positive)) {
123+
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
123124
std::free(type);
124125
type = nullptr;
125126
}
126127
} else if (parser.isLong) {
127128
/* literal has `L` ending */
128-
type = new SimpleType("native.CLong");
129+
type = new PrimitiveType("native.CLong");
129130

130131
/* must fit into Scala integer type */
131-
if (!integerFitsIntoType<long, ulong>(parser, positive)) {
132+
if (!integerFitsIntoType<long, unsigned long>(parser, positive)) {
132133
std::free(type);
133134
type = nullptr;
134135
}
@@ -145,7 +146,7 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,
145146
}
146147
} else if (parser.isFloatingLiteral()) {
147148
if (fitsIntoDouble(parser)) {
148-
type = new SimpleType("native.CDouble");
149+
type = new PrimitiveType("native.CDouble");
149150
scalaLiteral = getDoubleLiteral(parser);
150151
}
151152
}
@@ -164,9 +165,9 @@ DefineFinder::getTypeOfIntegerLiteral(const clang::NumericLiteralParser &parser,
164165
bool positive) {
165166

166167
if (integerFitsIntoType<int, uint>(parser, positive)) {
167-
return new SimpleType("native.CInt");
168+
return new PrimitiveType("native.CInt");
168169
} else if (integerFitsIntoType<long, unsigned long>(parser, positive)) {
169-
return new SimpleType("native.CLong");
170+
return new PrimitiveType("native.CLong");
170171
} else {
171172
llvm::errs() << "Warning: integer value does not fit into 8 bytes: "
172173
<< literal << "\n";

bindgen/ir/Enum.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "Enum.h"
2-
#include <sstream>
3-
#include <utility>
42

53
Enumerator::Enumerator(std::string name, int64_t value)
64
: name(std::move(name)), value(value) {}
@@ -11,7 +9,7 @@ int64_t Enumerator::getValue() { return value; }
119

1210
Enum::Enum(std::string name, std::string type,
1311
std::vector<Enumerator> enumerators)
14-
: SimpleType(std::move(type)), name(std::move(name)),
12+
: PrimitiveType(std::move(type)), name(std::move(name)),
1513
enumerators(std::move(enumerators)) {}
1614

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

bindgen/ir/Enum.h

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

44
#include "TypeDef.h"
5-
#include "types/SimpleType.h"
6-
#include <llvm/Support/raw_ostream.h>
7-
#include <string>
5+
#include "types/PrimitiveType.h"
86
#include <vector>
97

108
class Enumerator {
@@ -20,7 +18,7 @@ class Enumerator {
2018
int64_t value;
2119
};
2220

23-
class Enum : public SimpleType {
21+
class Enum : public PrimitiveType {
2422
public:
2523
Enum(std::string name, std::string type,
2624
std::vector<Enumerator> enumerators);

bindgen/ir/IR.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ IR::IR(std::string libName, std::string linkName, std::string objectName,
88

99
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
1010
Type *retType, bool isVariadic) {
11-
functions.push_back(new Function(name, std::move(parameters),
12-
retType, isVariadic));
11+
functions.push_back(
12+
new Function(name, std::move(parameters), retType, isVariadic));
1313
}
1414

1515
void IR::addTypeDef(std::string name, Type *type) {
@@ -50,7 +50,7 @@ void IR::addLiteralDefine(std::string name, std::string literal, Type *type) {
5050

5151
void IR::addPossibleVarDefine(const std::string &macroName,
5252
const std::string &varName) {
53-
possibleVarDefines.emplace_back(macroName, varName);
53+
possibleVarDefines.push_back(new PossibleVarDefine(macroName, varName));
5454
}
5555

5656
void IR::addVarDefine(std::string name, Variable *variable) {
@@ -165,8 +165,6 @@ bool IR::hasHelperMethods() const {
165165
return false;
166166
}
167167

168-
bool IR::hasEnums() const { return !enums.empty(); }
169-
170168
void IR::filterDeclarations(const std::string &excludePrefix) {
171169
if (excludePrefix.empty()) {
172170
return;
@@ -283,30 +281,12 @@ std::string IR::getDefineForVar(const std::string &varName) const {
283281
return "";
284282
}
285283

286-
Variable *IR::addVariable(const std::string &name, const std::string &type) {
284+
Variable *IR::addVariable(const std::string &name, Type *type) {
287285
Variable *variable = new Variable(name, type);
288286
variables.push_back(variable);
289287
return variable;
290288
}
291289

292-
IR::~IR() {
293-
for (auto variable : variables) {
294-
std::free(variable);
295-
}
296-
}
297-
298-
Enum *IR::getEnumWithName(const std::string &name) {
299-
return getDeclarationWithName(enums, name);
300-
}
301-
302-
Struct *IR::getStructWithName(const std::string &name) {
303-
return getDeclarationWithName(structs, name);
304-
}
305-
306-
Union *IR::getUnionWithName(const std::string &name) {
307-
return getDeclarationWithName(unions, name);
308-
}
309-
310290
TypeDef *IR::getTypeDefWithName(const std::string &name) {
311291
return getDeclarationWithName(typeDefs, name);
312292
}
@@ -323,3 +303,21 @@ T IR::getDeclarationWithName(std::vector<T> &declarations,
323303
}
324304
return nullptr;
325305
}
306+
307+
IR::~IR() {
308+
clearVector(functions);
309+
clearVector(typeDefs);
310+
clearVector(structs);
311+
clearVector(unions);
312+
clearVector(enums);
313+
clearVector(literalDefines);
314+
clearVector(possibleVarDefines);
315+
clearVector(variables);
316+
clearVector(varDefines);
317+
}
318+
319+
template <typename T> void IR::clearVector(std::vector<T> v) {
320+
for (auto e : v) {
321+
std::free(e);
322+
}
323+
}

bindgen/ir/IR.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515
class IR {
1616
public:
17-
explicit IR(std::string libName, std::string linkName,
18-
std::string objectName, std::string packageName);
17+
IR(std::string libName, std::string linkName, std::string objectName,
18+
std::string packageName);
1919

2020
~IR();
2121

@@ -49,16 +49,14 @@ class IR {
4949

5050
void addVarDefine(std::string name, Variable *variable);
5151

52-
Variable *addVariable(const std::string &name, const std::string &type);
52+
Variable *addVariable(const std::string &name, Type *type);
5353

5454
/**
5555
* @return true if there are no functions, types,
5656
* structs and unions
5757
*/
5858
bool libObjEmpty() const;
5959

60-
bool hasEnums() const;
61-
6260
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir);
6361

6462
void generate(const std::string &excludePrefix);
@@ -71,20 +69,9 @@ class IR {
7169
*/
7270
std::string getDefineForVar(const std::string &varName) const;
7371

74-
Enum *getEnumWithName(const std::string &name);
75-
76-
Struct *getStructWithName(const std::string &name);
77-
78-
Union *getUnionWithName(const std::string &name);
79-
8072
TypeDef *getTypeDefWithName(const std::string &name);
8173

8274
private:
83-
/**
84-
* Generates type defs for enums, structs and unions
85-
*/
86-
void generateTypeDefs();
87-
8875
/**
8976
* @return true if helper methods will be generated for this library
9077
*/
@@ -147,10 +134,11 @@ class IR {
147134
T getDeclarationWithName(std::vector<T> &declarations,
148135
const std::string &name);
149136

137+
template <typename T> void clearVector(std::vector<T> v);
138+
150139
std::string libName; // name of the library
151140
std::string linkName; // name of the library to link with
152141
std::string objectName; // name of Scala object
153-
// TODO: free memory
154142
std::vector<Function *> functions;
155143
std::vector<TypeDef *> typeDefs;
156144
std::vector<Struct *> structs;

bindgen/ir/LiteralDefine.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "LiteralDefine.h"
2-
#include "types/Type.h"
32

43
LiteralDefine::LiteralDefine(std::string name, std::string literal, Type *type)
54
: Define(std::move(name)), literal(std::move(literal)), type(type) {}

bindgen/ir/Struct.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "Struct.h"
22
#include "../Utils.h"
33
#include "types/ArrayType.h"
4-
#include "types/SimpleType.h"
4+
#include "types/PrimitiveType.h"
55
#include <sstream>
66
#include <utility>
77

@@ -24,7 +24,7 @@ TypeDef *Struct::generateTypeDef() {
2424
// have to represent it as an array and then Add helpers to help with
2525
// its manipulation
2626
return new TypeDef(getAliasType(),
27-
new ArrayType(new SimpleType("Byte"), typeSize));
27+
new ArrayType(new PrimitiveType("Byte"), typeSize));
2828
}
2929
}
3030

@@ -96,7 +96,7 @@ bool Struct::usesType(Type *type) const {
9696

9797
Union::Union(std::string name, std::vector<Field> fields, uint64_t maxSize)
9898
: StructOrUnion(std::move(name), std::move(fields)),
99-
ArrayType(new SimpleType("Byte"), maxSize) {}
99+
ArrayType(new PrimitiveType("Byte"), maxSize) {}
100100

101101
TypeDef *Union::generateTypeDef() { return new TypeDef(getTypeAlias(), this); }
102102

0 commit comments

Comments
 (0)