Skip to content

Commit 8b63ced

Browse files
authored
Merge pull request #62 from kornilova-l/defines-for-extern-variables
Generate bindings for defines for variables
2 parents 6408927 + 7455ee5 commit 8b63ced

File tree

19 files changed

+234
-17
lines changed

19 files changed

+234
-17
lines changed

bindgen/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ add_executable(bindgen
4848
ir/Define.cpp
4949
ir/LiteralDefine.cpp
5050
ir/LiteralDefine.h
51+
ir/VarDefine.cpp
52+
ir/VarDefine.h
53+
ir/Variable.cpp
54+
ir/Variable.h
55+
ir/PossibleVarDefine.cpp
56+
ir/PossibleVarDefine.h
5157
)
5258

5359
set_target_properties(bindgen

bindgen/defines/DefineFinder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
4242
std::string literal(stringToken.getLiteralData(),
4343
stringToken.getLength());
4444
ir.addLiteralDefine(macroName, "c" + literal, "native.CString");
45+
} else if (tokens->size() == 1 &&
46+
(*tokens)[0].getKind() == clang::tok::identifier) {
47+
// token might be a variable
48+
std::string varName = (*tokens)[0].getIdentifierInfo()->getName();
49+
ir.addPossibleVarDefine(macroName, varName);
4550
}
4651
std::free(tokens);
4752
}

bindgen/ir/Define.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
Define::Define(std::string name) : name(std::move(name)) {}
44

5-
std::string Define::getName() { return name; }
5+
std::string Define::getName() const { return name; }

bindgen/ir/Define.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Define {
77
public:
88
explicit Define(std::string name);
99

10-
std::string getName();
10+
std::string getName() const;
1111

1212
protected:
1313
std::string name;

bindgen/ir/IR.cpp

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ void IR::addLiteralDefine(std::string name, std::string literal,
3838
std::move(type));
3939
}
4040

41+
void IR::addPossibleVarDefine(const std::string &macroName,
42+
const std::string &varName) {
43+
possibleVarDefines.emplace_back(macroName, varName);
44+
}
45+
46+
void IR::addVarDefine(std::string name, Variable *variable) {
47+
varDefines.emplace_back(name, variable);
48+
}
49+
4150
bool IR::libObjEmpty() const {
4251
return functions.empty() && typeDefs.empty() && structs.empty() &&
43-
unions.empty();
52+
unions.empty() && varDefines.empty();
4453
}
4554

4655
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
@@ -69,6 +78,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
6978
s << typeDef;
7079
}
7180

81+
for (const auto &varDefine : ir.varDefines) {
82+
s << varDefine;
83+
}
84+
7285
for (const auto &func : ir.functions) {
7386
s << func;
7487
}
@@ -166,9 +179,11 @@ void IR::filterDeclarations(const std::string &excludePrefix) {
166179

167180
filterTypeDefs(excludePrefix);
168181

169-
filter(functions, excludePrefix);
182+
filterByPrefix(functions, excludePrefix);
183+
184+
filterByPrefix(literalDefines, excludePrefix);
170185

171-
filter(literalDefines, excludePrefix);
186+
filterByPrefix(varDefines, excludePrefix);
172187
}
173188

174189
void IR::filterTypeDefs(const std::string &excludePrefix) {
@@ -236,24 +251,53 @@ bool IR::existsFunctionWithName(std::string functionName) {
236251
}
237252

238253
void IR::removeDefine(const std::string &name) {
239-
for (auto it = literalDefines.begin(), end = literalDefines.end();
240-
it != end; ++it) {
241-
if ((*it).getName() == name) {
242-
literalDefines.erase(it);
243-
return;
254+
filterByName(literalDefines, name);
255+
filterByName(possibleVarDefines, name);
256+
filterByName(varDefines, name);
257+
}
258+
259+
template <typename T>
260+
void IR::filterByPrefix(std::vector<T> &declarations,
261+
const std::string &excludePrefix) {
262+
for (auto it = declarations.begin(); it != declarations.end();) {
263+
auto &declaration = *it;
264+
if (startsWith(declaration.getName(), excludePrefix)) {
265+
it = declarations.erase(it);
266+
} else {
267+
it++;
244268
}
245269
}
246270
}
247271

248272
template <typename T>
249-
void IR::filter(std::vector<T> &declarations,
250-
const std::string &excludePrefix) {
273+
void IR::filterByName(std::vector<T> &declarations, const std::string &name) {
251274
for (auto it = declarations.begin(); it != declarations.end();) {
252275
auto &declaration = *it;
253-
if (startsWith(declaration.getName(), excludePrefix)) {
276+
if (declaration.getName() == name) {
254277
it = declarations.erase(it);
255278
} else {
256279
it++;
257280
}
258281
}
259282
}
283+
284+
std::string IR::getDefineForVar(const std::string &varName) const {
285+
for (const auto &varDefine : possibleVarDefines) {
286+
if (varDefine.getVariableName() == varName) {
287+
return varDefine.getName();
288+
}
289+
}
290+
return "";
291+
}
292+
293+
Variable *IR::addVariable(const std::string &name, const std::string &type) {
294+
Variable *variable = new Variable(name, type);
295+
variables.push_back(variable);
296+
return variable;
297+
}
298+
299+
IR::~IR() {
300+
for (auto variable : variables) {
301+
std::free(variable);
302+
}
303+
}

bindgen/ir/IR.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include "Enum.h"
55
#include "Function.h"
66
#include "LiteralDefine.h"
7+
#include "PossibleVarDefine.h"
78
#include "Struct.h"
89
#include "TypeDef.h"
10+
#include "VarDefine.h"
911

1012
/**
1113
* Intermediate representation
@@ -15,6 +17,8 @@ class IR {
1517
explicit IR(std::string libName, std::string linkName,
1618
std::string objectName, std::string packageName);
1719

20+
~IR();
21+
1822
void addFunction(std::string name, std::vector<Parameter> parameters,
1923
std::string, bool isVariadic);
2024

@@ -32,6 +36,13 @@ class IR {
3236
void addLiteralDefine(std::string name, std::string literal,
3337
std::string type);
3438

39+
void addPossibleVarDefine(const std::string &macroName,
40+
const std::string &varName);
41+
42+
void addVarDefine(std::string name, Variable *variable);
43+
44+
Variable *addVariable(const std::string &name, const std::string &type);
45+
3546
/**
3647
* @return true if there are no functions, types,
3748
* structs and unions
@@ -46,6 +57,12 @@ class IR {
4657

4758
void removeDefine(const std::string &name);
4859

60+
/**
61+
* @return macro name if there is a macro for the variable.
62+
* otherwise return empty string
63+
*/
64+
std::string getDefineForVar(const std::string &varName) const;
65+
4966
private:
5067
/**
5168
* Generates type defs for enums, structs and unions
@@ -106,7 +123,11 @@ class IR {
106123
bool existsFunctionWithName(std::string functionName);
107124

108125
template <typename T>
109-
void filter(std::vector<T> &declarations, const std::string &excludePrefix);
126+
void filterByPrefix(std::vector<T> &declarations,
127+
const std::string &excludePrefix);
128+
129+
template <typename T>
130+
void filterByName(std::vector<T> &declarations, const std::string &name);
110131

111132
std::string libName; // name of the library
112133
std::string linkName; // name of the library to link with
@@ -117,6 +138,9 @@ class IR {
117138
std::vector<Union> unions;
118139
std::vector<Enum> enums;
119140
std::vector<LiteralDefine> literalDefines;
141+
std::vector<PossibleVarDefine> possibleVarDefines;
142+
std::vector<VarDefine> varDefines;
143+
std::vector<Variable *> variables;
120144
bool generated = false; // generate type defs only once
121145
std::string packageName;
122146
};

bindgen/ir/PossibleVarDefine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "PossibleVarDefine.h"
2+
3+
PossibleVarDefine::PossibleVarDefine(const std::string &name,
4+
const std::string &variableName)
5+
: Define(name), variableName(variableName) {}
6+
7+
std::string PossibleVarDefine::getVariableName() const { return variableName; }

bindgen/ir/PossibleVarDefine.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_POSSIBLEVARDEFINE_H
2+
#define SCALA_NATIVE_BINDGEN_POSSIBLEVARDEFINE_H
3+
4+
#include "Define.h"
5+
6+
/**
7+
* Stores name of possible variable.
8+
*/
9+
class PossibleVarDefine : public Define {
10+
public:
11+
PossibleVarDefine(const std::string &name, const std::string &variableName);
12+
13+
std::string getVariableName() const;
14+
15+
private:
16+
std::string variableName;
17+
};
18+
19+
#endif // SCALA_NATIVE_BINDGEN_POSSIBLEVARDEFINE_H

bindgen/ir/VarDefine.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "VarDefine.h"
2+
3+
VarDefine::VarDefine(std::string name, Variable *variable)
4+
: Define(std::move(name)), variable(variable) {}
5+
6+
llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
7+
const VarDefine &varDefine) {
8+
s << " @name(\"" << varDefine.variable->getName() << "\")\n"
9+
<< " val " << varDefine.getName() << ": "
10+
<< varDefine.variable->getType() << " = native.extern\n";
11+
return s;
12+
}

bindgen/ir/VarDefine.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_VARDEFINE_H
2+
#define SCALA_NATIVE_BINDGEN_VARDEFINE_H
3+
4+
#include "Define.h"
5+
#include "Variable.h"
6+
#include <llvm/Support/raw_ostream.h>
7+
8+
/**
9+
* Stores a pointer to Variable instance from IR
10+
*/
11+
class VarDefine : public Define {
12+
public:
13+
VarDefine(std::string name, Variable *variable);
14+
15+
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
16+
const VarDefine &varDefine);
17+
18+
private:
19+
Variable *variable;
20+
};
21+
22+
#endif // SCALA_NATIVE_BINDGEN_VARDEFINE_H

0 commit comments

Comments
 (0)