Skip to content

Commit 1d34fdb

Browse files
committed
Move object renaming logic to the main function
1 parent 9a25905 commit 1d34fdb

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

bindgen/Main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,21 @@ int main(int argc, char *argv[]) {
4242
return -1;
4343
}
4444

45+
auto objectName = libName;
46+
if (objectName == "native") {
47+
/* there are at most 3 objects in the file.
48+
* All of them will have distinct names. */
49+
objectName = "nativeLib";
50+
}
51+
4552
auto stdhead = StdHeaders.getValue();
4653
if (!stdhead.empty()) {
4754
headerMan.LoadConfig(stdhead);
4855
}
4956

5057
locations.clear();
5158

52-
IR ir(libName, Package.getValue());
59+
IR ir(libName, objectName, Package.getValue());
5360
ScalaFrontendActionFactory actionFactory(ir);
5461

5562
int result = Tool.run(&actionFactory);

bindgen/ir/IR.cpp

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

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

159
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
@@ -55,12 +49,12 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
5549
<< "import scala.scalanative.native._\n\n";
5650
}
5751

58-
std::string libObjName = handleReservedWords(ir.libObjectName);
52+
std::string objectName = handleReservedWords(ir.objectName);
5953

6054
if (!ir.libObjEmpty()) {
6155
s << "@native.link(\"" << ir.libName << "\")\n"
6256
<< "@native.extern\n"
63-
<< "object " << libObjName << " {\n";
57+
<< "object " << objectName << " {\n";
6458

6559
for (const auto &typeDef : ir.typeDefs) {
6660
s << typeDef;
@@ -74,7 +68,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
7468
}
7569

7670
if (!ir.enums.empty() || ir.hasHelperMethods()) {
77-
s << "import " << libObjName << "._\n\n";
71+
s << "import " << objectName << "._\n\n";
7872
}
7973

8074
if (!ir.enums.empty()) {

bindgen/ir/IR.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
*/
1212
class IR {
1313
public:
14-
explicit IR(std::string libName, std::string packageName);
14+
explicit IR(std::string libName, std::string objectName,
15+
std::string packageName);
1516

1617
void addFunction(std::string name, std::vector<Parameter> parameters,
1718
std::string, bool isVariadic);
@@ -104,7 +105,7 @@ class IR {
104105
bool existsFunctionWithName(std::string functionName);
105106

106107
std::string libName; // name of the library
107-
std::string libObjectName; // name of Scala object
108+
std::string objectName; // name of Scala object
108109
std::vector<Function> functions;
109110
std::vector<TypeDef> typeDefs;
110111
std::vector<Struct> structs;

0 commit comments

Comments
 (0)