Skip to content

Commit e80c15a

Browse files
committed
Rename lib object and functions with name "native"
1 parent 03d78cc commit e80c15a

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

bindgen/ir/Function.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ Parameter::Parameter(std::string name, std::string type)
66

77
Function::Function(std::string name, std::vector<Parameter> parameters,
88
std::string retType, bool isVariadic)
9-
: name(std::move(name)), parameters(std::move(parameters)),
9+
: name(name), scalaName(name), parameters(std::move(parameters)),
1010
retType(std::move(retType)), isVariadic(isVariadic) {}
1111

1212
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
13-
s << " def " << handleReservedWords(func.name) << "(";
13+
if (func.scalaName != func.name) {
14+
s << " @native.link(\"" << func.name << "\")\n";
15+
}
16+
s << " def " << handleReservedWords(func.scalaName) << "(";
1417
std::string sep = "";
1518
for (const auto &param : func.parameters) {
1619
s << sep << handleReservedWords(param.getName()) << ": "
@@ -43,17 +46,21 @@ std::string Function::getName() const { return name; }
4346
std::string Function::getVarargsParameterName() const {
4447
std::string parameterName = "varArgs";
4548
int i = 0;
46-
while (existParameterWithName(parameterName)) {
49+
while (existsParameterWithName(parameterName)) {
4750
parameterName = "varArgs" + std::to_string(i++);
4851
}
4952
return parameterName;
5053
}
5154

52-
bool Function::existParameterWithName(const std::string &parameterName) const {
55+
bool Function::existsParameterWithName(const std::string &parameterName) const {
5356
for (const auto &parameter : parameters) {
5457
if (parameter.getName() == parameterName) {
5558
return true;
5659
}
5760
}
5861
return false;
5962
}
63+
64+
void Function::setScalaName(std::string scalaName) {
65+
this->scalaName = std::move(scalaName);
66+
}

bindgen/ir/Function.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ class Function {
2323

2424
std::string getName() const;
2525

26+
void setScalaName(std::string scalaName);
27+
2628
private:
2729
std::string getVarargsParameterName() const;
2830

29-
bool existParameterWithName(const std::string &parameterName) const;
31+
bool existsParameterWithName(const std::string &parameterName) const;
3032

31-
std::string name;
33+
std::string name; // real name of the function
34+
std::string scalaName; // not empty
3235
std::vector<Parameter> parameters;
3336
std::string retType;
3437
bool isVariadic;

bindgen/ir/IR.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#include "IR.h"
22
#include "../Utils.h"
33

4-
IR::IR(std::string libName) : libName(std::move(libName)) {}
4+
IR::IR(std::string libName) : libName(std::move(libName)) {
5+
if (this->libName == "native") {
6+
/* there are at most 3 objects in the file.
7+
* All of them will have distinct names. */
8+
libObjectName = "nativeLib";
9+
} else {
10+
libObjectName = this->libName;
11+
}
12+
}
513

614
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
715
std::string retType, bool isVariadic) {
@@ -46,7 +54,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
4654
<< "import scala.scalanative.native._\n\n";
4755
}
4856

49-
std::string libObjName = handleReservedWords(ir.libName);
57+
std::string libObjName = handleReservedWords(ir.libObjectName);
5058

5159
if (!ir.libObjEmpty()) {
5260
s << "@native.link(\"" << ir.libName << "\")\n"
@@ -116,6 +124,7 @@ void IR::generateTypeDefs() {
116124

117125
void IR::generate(const std::string &excludePrefix) {
118126
if (!generated) {
127+
setScalaNames();
119128
filterDeclarations(excludePrefix);
120129
generateTypeDefs();
121130
generated = true;
@@ -199,5 +208,30 @@ bool IR::typeIsUsedOnlyInTypeDefs(std::string type) {
199208
}
200209

201210
void IR::setPackageName(std::string packageName) {
202-
this->packageName = packageName;
211+
this->packageName = std::move(packageName);
212+
}
213+
214+
void IR::setScalaNames() {
215+
/* Renaming according to Scala naming conventions
216+
* should happen here */
217+
218+
for (auto &function : functions) {
219+
if (function.getName() == "native") {
220+
std::string scalaName = "nativeFunc";
221+
int i = 0;
222+
while (existsFunctionWithName(scalaName)) {
223+
scalaName = "nativeFunc" + std::to_string(i++);
224+
}
225+
function.setScalaName(scalaName);
226+
}
227+
}
228+
}
229+
230+
bool IR::existsFunctionWithName(std::string functionName) {
231+
for (const auto &function : functions) {
232+
if (function.getName() == functionName) {
233+
return true;
234+
}
235+
}
236+
return false;
203237
}

bindgen/ir/IR.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ class IR {
101101
bool isTypeUsed(const std::vector<T> &declarations,
102102
const std::string &type);
103103

104-
std::string libName;
104+
void setScalaNames();
105+
106+
bool existsFunctionWithName(std::string functionName);
107+
108+
std::string libName; // name of the library
109+
std::string libObjectName; // name of Scala object
105110
std::vector<Function> functions;
106111
std::vector<TypeDef> typeDefs;
107112
std::vector<Struct> structs;

tests/samples/native.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
void native(int);
2+
void nativeFunc(float);
3+
4+
typedef int nativeFunc0;

tests/samples/native.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.scalanative._
2+
import scala.scalanative.native._
3+
4+
@native.link("native")
5+
@native.extern
6+
object nativeLib {
7+
type nativeFunc0 = native.CInt
8+
@native.link("native")
9+
def nativeFunc0(anonymous0: native.CInt): Unit = native.extern
10+
def nativeFunc(anonymous0: native.CFloat): Unit = native.extern
11+
}

0 commit comments

Comments
 (0)