Skip to content

Commit 762e66e

Browse files
committed
Save variable in IR
1 parent 4545bd5 commit 762e66e

File tree

10 files changed

+97
-34
lines changed

10 files changed

+97
-34
lines changed

bindgen/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ add_executable(bindgen
5050
ir/LiteralDefine.h
5151
ir/VarDefine.cpp
5252
ir/VarDefine.h
53+
ir/Variable.cpp
54+
ir/Variable.h
55+
ir/PossibleVarDefine.cpp
56+
ir/PossibleVarDefine.h
5357
)
5458

5559
set_target_properties(bindgen

bindgen/ir/IR.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ void IR::addLiteralDefine(std::string name, std::string literal,
4040

4141
void IR::addPossibleVarDefine(const std::string &macroName,
4242
const std::string &varName) {
43-
possibleVarDefines.emplace_back(macroName, varName, "", false);
43+
possibleVarDefines.emplace_back(macroName, varName);
4444
}
4545

46-
void IR::addVarDefine(const std::string &macroName, const std::string &varName,
47-
const std::string &type, bool isConst) {
48-
varDefines.emplace_back(macroName, varName, type, isConst);
46+
void IR::addVarDefine(std::string name, Variable *variable) {
47+
varDefines.emplace_back(name, variable);
4948
}
5049

5150
bool IR::libObjEmpty() const {
@@ -284,9 +283,22 @@ void IR::filterByName(std::vector<T> &declarations, const std::string &name) {
284283

285284
std::string IR::getDefineForVar(const std::string &varName) const {
286285
for (const auto &varDefine : possibleVarDefines) {
287-
if (varDefine.getVarName() == varName) {
286+
if (varDefine.getVariableName() == varName) {
288287
return varDefine.getName();
289288
}
290289
}
291290
return "";
292291
}
292+
293+
Variable *IR::addVariable(const std::string &name, const std::string &type,
294+
bool isConst) {
295+
Variable *variable = new Variable(name, type, isConst);
296+
variables.push_back(variable);
297+
return variable;
298+
}
299+
300+
IR::~IR() {
301+
for (auto variable : variables) {
302+
std::free(variable);
303+
}
304+
}

bindgen/ir/IR.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Enum.h"
55
#include "Function.h"
66
#include "LiteralDefine.h"
7+
#include "PossibleVarDefine.h"
78
#include "Struct.h"
89
#include "TypeDef.h"
910
#include "VarDefine.h"
@@ -16,6 +17,8 @@ class IR {
1617
explicit IR(std::string libName, std::string linkName,
1718
std::string objectName, std::string packageName);
1819

20+
~IR();
21+
1922
void addFunction(std::string name, std::vector<Parameter> parameters,
2023
std::string, bool isVariadic);
2124

@@ -36,8 +39,10 @@ class IR {
3639
void addPossibleVarDefine(const std::string &macroName,
3740
const std::string &varName);
3841

39-
void addVarDefine(const std::string &macroName, const std::string &varName,
40-
const std::string &type, bool isConst);
42+
void addVarDefine(std::string name, Variable *variable);
43+
44+
Variable *addVariable(const std::string &name, const std::string &type,
45+
bool isConst);
4146

4247
/**
4348
* @return true if there are no functions, types,
@@ -134,8 +139,9 @@ class IR {
134139
std::vector<Union> unions;
135140
std::vector<Enum> enums;
136141
std::vector<LiteralDefine> literalDefines;
137-
std::vector<VarDefine> possibleVarDefines;
142+
std::vector<PossibleVarDefine> possibleVarDefines;
138143
std::vector<VarDefine> varDefines;
144+
std::vector<Variable *> variables;
139145
bool generated = false; // generate type defs only once
140146
std::string packageName;
141147
};

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: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
#include "VarDefine.h"
22

3-
VarDefine::VarDefine(std::string name, std::string varName, std::string type,
4-
bool isConst)
5-
: Define(std::move(name)), varName(std::move(varName)),
6-
type(std::move(type)), isConst(isConst) {}
7-
8-
std::string VarDefine::getVarName() const { return varName; }
9-
10-
std::string VarDefine::getType() const { return type; }
3+
VarDefine::VarDefine(std::string name, Variable *variable)
4+
: Define(std::move(name)), variable(variable) {}
115

126
llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
137
const VarDefine &varDefine) {
14-
s << " @name(\"" << varDefine.getVarName() << "\")\n";
15-
if (varDefine.isConst) {
8+
s << " @name(\"" << varDefine.variable->getName() << "\")\n";
9+
if (varDefine.variable->getIsConst()) {
1610
s << " val ";
1711
} else {
1812
s << " def ";
1913
}
20-
s << varDefine.getName() << ": " << varDefine.getType()
14+
s << varDefine.getName() << ": " << varDefine.variable->getType()
2115
<< " = native.extern\n";
2216
return s;
2317
}

bindgen/ir/VarDefine.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22
#define SCALA_NATIVE_BINDGEN_VARDEFINE_H
33

44
#include "Define.h"
5+
#include "Variable.h"
56
#include <llvm/Support/raw_ostream.h>
67

8+
/**
9+
* Stores a pointer to Variable instance from IR
10+
*/
711
class VarDefine : public Define {
812
public:
9-
VarDefine(std::string name, std::string varName, std::string type,
10-
bool isConst);
11-
12-
std::string getVarName() const;
13-
14-
std::string getType() const;
13+
VarDefine(std::string name, Variable *variable);
1514

1615
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1716
const VarDefine &varDefine);
1817

1918
private:
20-
std::string varName;
21-
std::string type;
22-
bool isConst;
19+
Variable *variable;
2320
};
2421

2522
#endif // SCALA_NATIVE_BINDGEN_VARDEFINE_H

bindgen/ir/Variable.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "Variable.h"
2+
3+
Variable::Variable(const std::string &name, const std::string &type,
4+
bool isConst)
5+
: TypeAndName(name, type), isConst(isConst) {}
6+
7+
bool Variable::getIsConst() { return isConst; }

bindgen/ir/Variable.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef SCALA_NATIVE_BINDGEN_VARIABLE_H
2+
#define SCALA_NATIVE_BINDGEN_VARIABLE_H
3+
4+
#include "TypeAndName.h"
5+
6+
class Variable : public TypeAndName {
7+
public:
8+
Variable(const std::string &name, const std::string &type, bool isConst);
9+
10+
bool getIsConst();
11+
12+
private:
13+
bool isConst;
14+
};
15+
16+
#endif // SCALA_NATIVE_BINDGEN_VARIABLE_H

bindgen/visitor/TreeVisitor.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,16 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
162162

163163
bool TreeVisitor::VisitVarDecl(clang::VarDecl *varDecl) {
164164
if (!varDecl->isThisDeclarationADefinition()) {
165+
std::string variableName = varDecl->getName().str();
166+
std::string type =
167+
handleReservedWords(typeTranslator.Translate(varDecl->getType()));
168+
Variable *variable = ir.addVariable(
169+
variableName, type, varDecl->getType().isConstQualified());
165170
/* check if there is a macro for the variable.
166171
* Macros were saved by DefineFinder */
167-
std::string varName = varDecl->getName().str();
168-
std::string macroName = ir.getDefineForVar(varName);
172+
std::string macroName = ir.getDefineForVar(variableName);
169173
if (!macroName.empty()) {
170-
std::string type = handleReservedWords(
171-
typeTranslator.Translate(varDecl->getType()));
172-
ir.addVarDefine(macroName, varName, type,
173-
varDecl->getType().isConstQualified());
174+
ir.addVarDefine(macroName, variable);
174175
}
175176
}
176177
return true;

0 commit comments

Comments
 (0)