Skip to content

Commit 85a51bd

Browse files
committed
Add new helpers
1 parent 851172f commit 85a51bd

File tree

8 files changed

+191
-11
lines changed

8 files changed

+191
-11
lines changed

HeaderManager.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#include "HeaderManager.h"
2+
#include "Utils.h"
23

34
#include "clang/Tooling/Tooling.h"
45

6+
#include <iostream>
57
#include <fstream>
68

79
HeaderManager::HeaderManager(): headers() {
810

911
}
1012

11-
void HeaderManager::LoadConfig(const std::string& path){
13+
void HeaderManager::LoadConfig(std::string path){
14+
trim(path);
1215
std::ifstream input(path);
1316

1417
for(std::string line; getline( input, line );){
@@ -22,7 +25,8 @@ void HeaderManager::LoadConfig(const std::string& path){
2225

2326
}
2427

25-
bool HeaderManager::IsStandard(const std::string& path){
28+
bool HeaderManager::IsStandard(std::string path){
29+
trim(path);
2630
return !!headers.count(path);
2731
}
2832

HeaderManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class HeaderManager {
99

1010
public:
1111
HeaderManager();
12-
void LoadConfig(const std::string& path);
13-
bool IsStandard(const std::string& path);
12+
void LoadConfig(std::string path);
13+
bool IsStandard(std::string path);
1414
std::string* GetImport(const std::string& include);
1515

1616
};

Main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#define CATCH_CONFIG_RUNNER
55
#include "catch/catch.hpp"
66

7+
#include <sys/types.h>
8+
79
static llvm::cl::OptionCategory Category("Binding Generator");
810
static llvm::cl::extrahelp CommonHelp(clang::tooling::CommonOptionsParser::HelpMessage);
911
static llvm::cl::extrahelp MoreHelp("\nProduce Bindings for scala native. Please specify lib name wit parameter name\n");

TreeConsumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class TreeConsumer : public clang::ASTConsumer {
2424
for (clang::DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; i++) {
2525
clang::Decl *D = *i;
2626
std::string fpath = smanager.getFilename(D->getLocation()).str();
27-
locations.insert(basename(fpath) + "\n");
2827

2928
if(!headerMan.IsStandard(basename(fpath)) && fpath != ""){
29+
locations.insert(basename(fpath) + "\n");
3030
visitor->TraverseDecl(D);
3131
}
3232

TreeVisitor.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
3737
}
3838

3939
//Note the C Iso require at least one argument in a variadic function, so the comma is fine
40-
std::string variad = func->isVariadic() ? ", varArgs: native.CVararg*" : "";
40+
std::string variad = func->isVariadic() ? ", varArgs: native.CVararg" : "";
4141

4242
declarations += "\tdef " + funcName + "(" + params + variad + "): " + retType + " = native.extern\n";
4343
return true;
@@ -89,17 +89,30 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
8989
if(record->isUnion() && !record->isAnonymousStructOrUnion() && name != ""){
9090

9191
//Replace "union x" with union_x in scala
92-
typeTranslator.AddTranslation("union " + name, "union" + name);
92+
typeTranslator.AddTranslation("union " + name, "union_" + name);
9393

9494
uint64_t maxSize = 0;
95+
std::string helpersFunc = "";
9596

9697
for(const clang::FieldDecl* field : record->fields()){
9798
maxSize = std::max(maxSize, astContext->getTypeSize(field->getType()));
99+
std::string fname = handleReservedWords(field->getNameAsString());
100+
std::string ftype = typeTranslator.Translate(field->getType(), &name);
101+
102+
if(fname != ""){
103+
helpersFunc += "\t\tdef " + fname + ": native.Ptr[" + ftype + "] = p.cast[native.Ptr[" + ftype + "]]\n";
104+
helpersFunc += "\t\tdef " + fname + "_=(value: " + ftype + "): Unit = !(p.cast[native.Ptr[" + ftype + "]]) = value\n";
105+
}
98106
}
99107

100-
declarations += "\ttype union_" + name + " = native.CArray[Byte, " + intToScalaNat(maxSize) + "]\n";
108+
auto usize = intToScalaNat(maxSize);
109+
declarations += "\ttype union_" + name + " = native.CArray[Byte, " + usize + "]\n";
101110

102-
return true;
111+
helpers += "\timplicit class union_" + name +"_pos(val p: native.Ptr[native.CArray[Byte, " + usize + "]]) extends AnyVal {\n";
112+
helpers += helpersFunc;
113+
helpers += "\t}\n\n";
114+
115+
return true;
103116

104117
} else if (record->isStruct() && record->isThisDeclarationADefinition() && !record->isAnonymousStructOrUnion() && name != ""){
105118

@@ -119,7 +132,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
119132
fields += ftype + ", ";
120133
if(name != ""){
121134
helpersFunc += "\t\tdef " + fname + ": " + ftype + " = !p._" + std::to_string(fieldCnt + 1) + "\n";
122-
helpersFunc += "\t\tdef " + fname +"_=(value: " + ftype + "):Unit = !p._" + std::to_string(fieldCnt + 1) + " = value\n";
135+
helpersFunc += "\t\tdef " + fname + "_=(value: " + ftype + "):Unit = !p._" + std::to_string(fieldCnt + 1) + " = value\n";
123136
}
124137
fieldCnt++;
125138
}

TypeTranslator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
4949
counter++;
5050
}
5151

52-
std::string variad = fc->isVariadic() ? "varArgs: native.CVararg*, " : "";
52+
std::string variad = "";
53+
54+
if(fc->isVariadic()){
55+
counter ++;
56+
variad = "native.CVararg, ";
57+
}
5358

5459
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + params + variad + ret + "]";
5560

Utils.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include <string>
77
#include <cinttypes>
8+
#include <algorithm>
9+
#include <cctype>
10+
#include <locale>
811

912
inline std::string basename(const std::string& pathname) {
1013
return {std::find_if(pathname.rbegin(), pathname.rend(),
@@ -74,4 +77,25 @@ inline std::string handleReservedWords(std::string name){
7477
}
7578
}
7679

80+
// trim from start (in place)
81+
static inline void ltrim(std::string &s) {
82+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
83+
return !std::isspace(ch);
84+
}));
85+
}
86+
87+
// trim from end (in place)
88+
static inline void rtrim(std::string &s) {
89+
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
90+
return !std::isspace(ch);
91+
}).base(), s.end());
92+
}
93+
94+
// trim from both ends (in place)
95+
static inline void trim(std::string &s) {
96+
ltrim(s);
97+
rtrim(s);
98+
}
99+
100+
77101
#endif // UTILS_H

stdHeaders.txt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
assert.h=
3+
complex.h=scala.scalanative.native.complex
4+
ctype.h=scala.scalanative.native.ctype
5+
errno.h=scala.scalanative.native.errno
6+
fenv.h=
7+
float.h=
8+
inttypes.h=
9+
iso646.h=
10+
limits.h=
11+
locale.h=
12+
math.h=scala.scalanative.native.math
13+
setjmp.h=
14+
signal.h=scala.scalanative.native.signal
15+
stdalign.h=
16+
stdarg.h=
17+
stdatomic.h=
18+
stdbool.h=
19+
stddef.h=
20+
stdint.h=
21+
stdio.h=scala.scalanative.native.stdio
22+
stdlib.h=scala.scalanative.native.stdlib
23+
stdnoreturn.h=
24+
string.h=scala.scalanative.native.string
25+
tgmath.h=
26+
threads.h=
27+
time.h=
28+
uchar.h=
29+
wchar.h=
30+
wctype.h=
31+
32+
33+
aio.h=
34+
inet.h=scala.scalanative.posix.arpa.inet
35+
assert.h=
36+
complex.h=scala.scalanative.native.complex
37+
cpio.h=scala.scalanative.posix.cpio
38+
ctype.h=
39+
dirent.h=scala.scalanative.posix.dirent
40+
dlfcn.h=
41+
errno.h=scala.scalanative.posix.errno
42+
fcntl.h=scala.scalanative.posix.fcntl
43+
fenv.h=
44+
float.h=
45+
fmtmsg.h=
46+
fnmatch.h=
47+
ftw.h=
48+
glob.h=
49+
grp.h=scala.scalanative.posix.grp
50+
iconv.h=
51+
inttypes.h=scala.scalanative.posix.inttypes
52+
iso646.h=
53+
langinfo.h=
54+
libgen.h=
55+
limits.h=scala.scalanative.posix.limits
56+
locale.h=
57+
math.h=
58+
monetary.h=
59+
mqueue.h=
60+
ndbm.h=
61+
if.h=
62+
netdb.h=
63+
in.h=scala.scalanative.posix.netinet.in
64+
tcp.h=
65+
nl_types.h=
66+
poll.h=
67+
pthread.h=
68+
pwd.h=scala.scalanative.posix.pwd
69+
regex.h=scala.scalanative.posix.regex
70+
sched.h=
71+
search.h=
72+
semaphore.h=
73+
setjmp.h=
74+
signal.h=
75+
spawn.h=
76+
stdarg.h=
77+
stdbool.h=
78+
stddef.h=
79+
stdint.h=
80+
stdio.h=
81+
stdlib.h=scala.scalanative.posix.stdlib
82+
string.h=
83+
strings.h=
84+
stropts.h=
85+
ipc.h=
86+
mman.h=
87+
msg.h=
88+
resource.h=
89+
select.h=
90+
sem.h=
91+
shm.h=
92+
socket.h=scala.scalanative.posix.sys.socket
93+
stat.h=scala.scalanative.posix.sys.stat
94+
statvfs.h=
95+
time.h=scala.scalanative.posix.sys.time
96+
times.h=
97+
types.h=
98+
uio.h=scala.scalanative.posix.sys.uio
99+
un.h=
100+
utsname.h=
101+
wait.h=
102+
syslog.h=scala.scalanative.posix.syslog
103+
tar.h=
104+
termios.h=scala.scalanative.posix.termios
105+
tgmath.h=
106+
time.h=
107+
trace.h=
108+
ulimit.h=
109+
unistd.h=scala.scalanative.posix.unistd
110+
utime.h=scala.scalanative.posix.utime
111+
utmpx.h=
112+
wchar.h=
113+
wctype.h=
114+
wordexp.h=
115+
116+
siginfo.h=
117+
sigset.h=
118+
sigaction.h=
119+
sigcontext.h=
120+
sigthread.h=
121+
ucontext.h=
122+
pthreadtypes.h=
123+
libio.h=
124+
__stddef_max_align_t.h=
125+
_G_config.h=
126+
sys_errlist.h"=
127+
sysmacros.h=
128+
xlocale.h=
129+
socket_type.h=
130+
sockaddr.h=
131+
sigstack.h=
132+
sys_errlist.h=

0 commit comments

Comments
 (0)