Skip to content

Commit 288a06b

Browse files
committed
Add helpers for structs
1 parent 7861005 commit 288a06b

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

ScalaBindgen.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ HeaderManager headerMan;
1818

1919
std::string declarations;
2020
std::string enums;
21+
std::string helpers;
2122

2223
bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
2324
std::string funcName = func->getNameInfo().getName().getAsString();
@@ -106,28 +107,43 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
106107
//Replace "struct x" with struct_x in scala
107108
typeTranslator.AddTranslation("struct " + name, "struct_"+name);
108109

109-
int counter = 0;
110+
int fieldCnt = 0;
110111
std::string fields = "";
112+
std::string helpersFunc = "";
111113

112114
for(const clang::FieldDecl* field : record->fields()){
113-
fields += typeTranslator.Translate(field->getType(), &name) + ", ";
114-
counter++;
115+
std::string fname = field->getNameAsString();
116+
std::string ftype = typeTranslator.Translate(field->getType(), &name);
117+
118+
fields += ftype + ", ";
119+
if(name != ""){
120+
helpersFunc += "\t\tdef " + fname + ": " + ftype + " = !p._" + std::to_string(fieldCnt + 1) + "\n";
121+
helpersFunc += "\t\tdef " + fname +"_=(value: " + ftype + "):Unit = !p._" + std::to_string(fieldCnt + 1) + " = value\n";
122+
}
123+
fieldCnt++;
115124
}
116125

117126
//remove last ,
118127
if(fields != ""){
119128
fields = fields.substr(0, fields.size()-2);
120129
}
121130

122-
if(counter < SCALA_NATIVE_MAX_STRUCT_FIELDS){
123-
declarations += "\ttype struct_" + name + " = " + "native.CStruct" + std::to_string(counter) + "[" + fields + "]\n";
131+
if(fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
132+
declarations += "\ttype struct_" + name + " = " + "native.CStruct" + std::to_string(fieldCnt) + "[" + fields + "]\n";
124133
} else {
125134
//There is no easy way to represent it as a struct in scala native, have to represent it as an array and then
126135
//Add helpers to help with it's manipulation
127136
uint64_t size = astContext->getTypeSize(record->getTypeForDecl());
128137
declarations += "\ttype struct_" + name + " = " + "native.CArray[Byte, " + uint64ToScalaNat(size) + "]\n";
129138
}
130139

140+
//Create helpers in an implicit class
141+
if(fieldCnt > 0 && fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
142+
helpers += "\timplicit class struct_" + name + "_ops(val p: native.Ptr[struct_" + name + "]) extends AnyVal {\n";
143+
helpers += helpersFunc;
144+
helpers += "\t}\n\n";
145+
}
146+
131147
return true;
132148
}
133149
return false;
@@ -158,7 +174,7 @@ int main(int argc, char *argv[]) {
158174

159175
declarations = "";
160176
enums = "";
161-
177+
helpers = "";
162178

163179
int result = Tool.run(clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get());
164180

@@ -170,13 +186,20 @@ int main(int argc, char *argv[]) {
170186
<< "@native.extern\n"
171187
<< "object " << lib << " {\n"
172188
<< declarations
173-
<< "}\n\n";
189+
<< "}\n\n"
190+
<< "import " + lib + "._\n\n";
174191
}
175192

176193
if(enums != ""){
177194
llvm::outs() << "object " << lib << "Enums {\n"
178195
<< enums
179-
<< "}\n";
196+
<< "}\n\n";
197+
}
198+
199+
if(helpers != ""){
200+
llvm::outs() << "object " << lib << "Helpers {\n"
201+
<< helpers
202+
<< "}\n\n";
180203
}
181204

182205
return result;

ScalaBindgen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
extern std::string declarations;
1919
extern std::string enums;
20+
extern std::string helpers;
2021

2122
class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
2223
private:

SimpleTypeTests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
std::string Translate(std::string code){
1010
declarations = "";
1111
enums = "";
12+
helpers = "";
1213
auto* action = new ExampleFrontendAction;
1314
clang::tooling::runToolOnCode(action, code, "input.h");
1415
return declarations;

0 commit comments

Comments
 (0)