Skip to content

Commit 98a84c6

Browse files
committed
Implement intermediate representation
1 parent 1906b35 commit 98a84c6

30 files changed

+909
-336
lines changed

CMakeLists.txt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,31 @@ add_compile_options(-fexceptions -std=c++11)
1313

1414
add_library(scalaNativeBindgen
1515
STATIC
16-
ScalaFrontend.h
17-
TreeVisitor.h
18-
TreeVisitor.cpp
19-
TreeConsumer.h
16+
visitor/ScalaFrontendAction.h
17+
visitor/ScalaFrontendAction.cpp
18+
visitor/ScalaFrontendActionFactory.h
19+
visitor/ScalaFrontendActionFactory.cpp
20+
visitor/TreeVisitor.h
21+
visitor/TreeVisitor.cpp
22+
visitor/TreeConsumer.h
2023
TypeTranslator.h
2124
TypeTranslator.cpp
2225
HeaderManager.h
2326
HeaderManager.cpp
2427
CycleDetection.h
2528
Utils.h
29+
ir/IR.h
30+
ir/IR.cpp
31+
ir/Struct.cpp
32+
ir/Struct.h
33+
ir/Function.cpp
34+
ir/Function.h
35+
ir/TypeDef.cpp
36+
ir/TypeDef.h
37+
ir/Enum.cpp
38+
ir/Enum.h
39+
ir/TypeAndName.cpp
40+
ir/TypeAndName.h
2641
)
2742

2843
add_executable(scalaBindgen

HeaderManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "HeaderManager.h"
22
#include "Utils.h"
33

4-
#include "clang/Tooling/Tooling.h"
4+
#include <clang/Tooling/Tooling.h>
55

66
#include <iostream>
77
#include <fstream>

Main.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
#include "ScalaFrontend.h"
1+
#include "visitor/ScalaFrontendAction.h"
2+
#include <sstream>
23
#include "Utils.h"
4+
#include "visitor/ScalaFrontendActionFactory.h"
5+
#include <clang/Tooling/CommonOptionsParser.h>
36

47

58
static llvm::cl::OptionCategory Category("Binding Generator");
@@ -14,60 +17,36 @@ int main(int argc, char *argv[]) {
1417
clang::tooling::CommonOptionsParser op(argc, (const char**)argv, Category);
1518
clang::tooling::ClangTool Tool(op.getCompilations(), op.getSourcePathList());
1619

17-
auto lib = LibName.getValue();
18-
if(lib == ""){
19-
llvm::errs() << "Error: Please specify the lib name using -name paramter\n";
20+
auto libName = LibName.getValue();
21+
if(libName.empty()){
22+
llvm::errs() << "Error: Please specify the lib name using -name parameter\n";
2023
return -1;
2124
}
2225

2326
auto stdhead = StdHeaders.getValue();
24-
if(stdhead != ""){
27+
if(!stdhead.empty()){
2528
headerMan.LoadConfig(stdhead);
2629
}
2730

28-
declarations = "";
29-
enums = "";
30-
helpers = "";
3131
locations.clear();
3232

33-
int result = Tool.run(clang::tooling::newFrontendActionFactory<ScalaFrontendAction>().get());
33+
ScalaFrontendActionFactory actionFactory(libName);
3434

35-
auto printLoc = PrintHeadersLocation.getValue();
36-
37-
if(printLoc){
38-
for(const auto& location: locations){
39-
llvm::outs() << location;
40-
}
41-
42-
} else {
43-
if(declarations != "" || enums != "")
44-
llvm::outs() << "import scala.scalanative._\n"
45-
<< "import scala.scalanative.native._\n"
46-
<< "import scala.scalanative.native.Nat._\n\n";
35+
int result = Tool.run(&actionFactory);
4736

48-
if(declarations != ""){
49-
llvm::outs() << "@native.link(\"" << lib << "\")\n"
50-
<< "@native.extern\n"
51-
<< "object " << lib << " {\n"
52-
<< declarations
53-
<< "}\n\n";
54-
}
37+
IR ir = actionFactory.getIntermediateRepresentation();
5538

56-
if(enums != "" || helpers != ""){
57-
llvm::outs() << "import " + lib + "._\n\n";
58-
}
39+
auto printLoc = PrintHeadersLocation.getValue();
5940

60-
if(enums != ""){
61-
llvm::outs() << "object " << lib << "Enums {\n"
62-
<< enums
63-
<< "}\n\n";
64-
}
41+
std::ostringstream s;
6542

66-
if(helpers != ""){
67-
llvm::outs() << "object " << lib << "Helpers {\n"
68-
<< helpers
69-
<< "}\n\n";
43+
if(printLoc){
44+
for(const auto& location: locations) {
45+
s << location.c_str();
7046
}
47+
} else {
48+
s << ir.generate();
7149
}
50+
std::cout << s.str();
7251
return result;
7352
}

ScalaFrontend.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

TreeConsumer.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

TreeVisitor.cpp

Lines changed: 0 additions & 191 deletions
This file was deleted.

0 commit comments

Comments
 (0)