Skip to content

Commit 84cc1fb

Browse files
committed
Move from llvm outs to strings
1 parent 302f251 commit 84cc1fb

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

ScalaBindgen.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ static llvm::cl::opt<std::string> LibName("name", llvm::cl::cat(Category));
2626

2727
HeaderManager headerMan;
2828

29+
std::string declarations;
30+
std::string enums;
2931

3032
class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
3133
private:
@@ -57,15 +59,15 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
5759
params = params.substr(0, params.size()-2);
5860
}
5961

60-
llvm::outs() << "\tdef " << funcName << "(" << params << "): " + retType + " = native.extern\n";
62+
declarations += "\tdef " + funcName + "(" + params + "): " + retType + " = native.extern\n";
6163
return true;
6264
}
6365

6466
virtual bool VisitTypedefDecl(clang::TypedefDecl *tpdef){
6567
std::string name = tpdef->getName();
6668
std::string tpe = typeTranslator.Translate(tpdef->getUnderlyingType());
67-
llvm::outs() << "\ttype " << name << " = " << tpe << "\n";
68-
return true;
69+
declarations += "\ttype " + name + " = " + tpe + "\n";
70+
return true;
6971
}
7072

7173
virtual bool VisitEnumDecl(clang::EnumDecl *enumdecl){
@@ -79,15 +81,15 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
7981
}
8082

8183
if(name != ""){
82-
llvm::outs() << "\ttype enum_" << name << " = native.CInt\n";
84+
declarations += "\ttype enum_" + name + " = native.CInt\n";
8385
}
8486

8587
int i = 0;
8688
for (const clang::EnumConstantDecl* en : enumdecl->enumerators()){
8789
if(name != ""){
88-
llvm::outs() << "\tfinal val enum_" << name << "_" << en->getNameAsString() << " = " << i++ << "\n";
90+
declarations += "\tfinal val enum_" + name + "_" + en->getNameAsString() + " = " + std::to_string(i++) + "\n";
8991
} else {
90-
llvm::outs() << "\tfinal val enum_" << en->getNameAsString() << " = " << i++ << "\n";
92+
declarations += "\tfinal val enum_" + en->getNameAsString() + " = " + std::to_string(i++) + "\n";
9193
}
9294
}
9395

@@ -113,7 +115,7 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
113115
maxSize = std::max(maxSize, astContext->getTypeSize(field->getType()));
114116
}
115117

116-
llvm::outs() << "\ttype union_" << name << " = native.CArray[Byte, " << intToScalaNat(maxSize) << "]\n";
118+
declarations += "\ttype union_" + name + " = native.CArray[Byte, " + intToScalaNat(maxSize) + "]\n";
117119

118120
return true;
119121

@@ -136,12 +138,12 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
136138
}
137139

138140
if(counter < SCALA_NATIVE_MAX_STRUCT_FIELDS){
139-
llvm::outs() << "\ttype struct_" << name << " = " << "native.CStruct" << counter << "[" << fields << "]\n";
141+
declarations += "\ttype struct_" + name + " = " + "native.CStruct" + std::to_string(counter) + "[" + fields + "]\n";
140142
} else {
141143
//There is no easy way to represent it as a struct in scala native, have to represent it as an array and then
142144
//Add helpers to help with it's manipulation
143145
uint64_t size = astContext->getTypeSize(record->getTypeForDecl());
144-
llvm::outs() << "\ttype struct_" << name << " = " << "native.CArray[Byte, " << uint64ToScalaNat(size) << "]\n";
146+
declarations += "\ttype struct_" + name + " = " + "native.CArray[Byte, " + uint64ToScalaNat(size) + "]\n";
145147
}
146148

147149
return true;
@@ -245,13 +247,19 @@ int main(int argc, const char **argv) {
245247
return -1;
246248
}
247249

250+
declarations = "";
251+
enums = "";
252+
248253
headerMan.LoadConfig(std::string("../llvm/tools/clang/tools/extra/scala-bindgen/nativeHeaders.txt"));
249254

255+
256+
int result = Tool.run(clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get());
257+
250258
llvm::outs() << "import scala.scalanative._\n";
251259
llvm::outs() << "import scala.scalanative.native.Nat._\n\n";
252260
llvm::outs() << "@native.link(\"" << lib << "\")\n";
253261
llvm::outs() << "@native.extern\nobject " << lib<< " {\n";
254-
int result = Tool.run(clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get());
262+
llvm::outs() << declarations;
255263
llvm::outs() << "}\n";
256264

257265
return result;

0 commit comments

Comments
 (0)