Skip to content

Commit 4545bd5

Browse files
committed
Use val for const variables and def otherwise
1 parent 582d04b commit 4545bd5

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

bindgen/ir/IR.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ 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, "");
43+
possibleVarDefines.emplace_back(macroName, varName, "", false);
4444
}
4545

4646
void IR::addVarDefine(const std::string &macroName, const std::string &varName,
47-
const std::string &type) {
48-
varDefines.emplace_back(macroName, varName, type);
47+
const std::string &type, bool isConst) {
48+
varDefines.emplace_back(macroName, varName, type, isConst);
4949
}
5050

5151
bool IR::libObjEmpty() const {

bindgen/ir/IR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class IR {
3737
const std::string &varName);
3838

3939
void addVarDefine(const std::string &macroName, const std::string &varName,
40-
const std::string &type);
40+
const std::string &type, bool isConst);
4141

4242
/**
4343
* @return true if there are no functions, types,

bindgen/ir/VarDefine.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
#include "VarDefine.h"
22

3-
VarDefine::VarDefine(std::string name, std::string varName, std::string type)
3+
VarDefine::VarDefine(std::string name, std::string varName, std::string type,
4+
bool isConst)
45
: Define(std::move(name)), varName(std::move(varName)),
5-
type(std::move(type)) {}
6+
type(std::move(type)), isConst(isConst) {}
67

78
std::string VarDefine::getVarName() const { return varName; }
89

910
std::string VarDefine::getType() const { return type; }
1011

1112
llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1213
const VarDefine &varDefine) {
13-
s << " @name(\"" << varDefine.getVarName() << "\")\n"
14-
<< " val " << varDefine.getName() << ": " << varDefine.getType()
14+
s << " @name(\"" << varDefine.getVarName() << "\")\n";
15+
if (varDefine.isConst) {
16+
s << " val ";
17+
} else {
18+
s << " def ";
19+
}
20+
s << varDefine.getName() << ": " << varDefine.getType()
1521
<< " = native.extern\n";
1622
return s;
1723
}

bindgen/ir/VarDefine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
class VarDefine : public Define {
88
public:
9-
VarDefine(std::string name, std::string varName, std::string type);
9+
VarDefine(std::string name, std::string varName, std::string type,
10+
bool isConst);
1011

1112
std::string getVarName() const;
1213

@@ -18,6 +19,7 @@ class VarDefine : public Define {
1819
private:
1920
std::string varName;
2021
std::string type;
22+
bool isConst;
2123
};
2224

2325
#endif // SCALA_NATIVE_BINDGEN_VARDEFINE_H

bindgen/visitor/TreeVisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ bool TreeVisitor::VisitVarDecl(clang::VarDecl *varDecl) {
169169
if (!macroName.empty()) {
170170
std::string type = handleReservedWords(
171171
typeTranslator.Translate(varDecl->getType()));
172-
ir.addVarDefine(macroName, varName, type);
172+
ir.addVarDefine(macroName, varName, type,
173+
varDecl->getType().isConstQualified());
173174
}
174175
}
175176
return true;

tests/samples/VarDefine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
extern int a;
22
#define A a
33

4+
extern const int constInt;
5+
#define CONST_INT constInt
6+
7+
extern const int *constIntPointer;
8+
#define CONST_INT_POINTER constIntPointer
9+
410
#define B b // ignored because there is no such variable
511

612
extern int c;

tests/samples/VarDefine.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ import scala.scalanative.native._
77
@native.extern
88
object VarDefine {
99
@name("a")
10-
val A: native.CInt = native.extern
10+
def A: native.CInt = native.extern
11+
@name("constInt")
12+
val CONST_INT: native.CInt = native.extern
13+
@name("constIntPointer")
14+
def CONST_INT_POINTER: native.Ptr[native.CInt] = native.extern
1115
}

0 commit comments

Comments
 (0)