@@ -18,6 +18,7 @@ HeaderManager headerMan;
18
18
19
19
std::string declarations;
20
20
std::string enums;
21
+ std::string helpers;
21
22
22
23
bool TreeVisitor::VisitFunctionDecl (clang::FunctionDecl *func) {
23
24
std::string funcName = func->getNameInfo ().getName ().getAsString ();
@@ -106,28 +107,43 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
106
107
// Replace "struct x" with struct_x in scala
107
108
typeTranslator.AddTranslation (" struct " + name, " struct_" +name);
108
109
109
- int counter = 0 ;
110
+ int fieldCnt = 0 ;
110
111
std::string fields = " " ;
112
+ std::string helpersFunc = " " ;
111
113
112
114
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\t def " + fname + " : " + ftype + " = !p._" + std::to_string (fieldCnt + 1 ) + " \n " ;
121
+ helpersFunc += " \t\t def " + fname +" _=(value: " + ftype + " ):Unit = !p._" + std::to_string (fieldCnt + 1 ) + " = value\n " ;
122
+ }
123
+ fieldCnt++;
115
124
}
116
125
117
126
// remove last ,
118
127
if (fields != " " ){
119
128
fields = fields.substr (0 , fields.size ()-2 );
120
129
}
121
130
122
- if (counter < SCALA_NATIVE_MAX_STRUCT_FIELDS){
123
- declarations += " \t type struct_" + name + " = " + " native.CStruct" + std::to_string (counter ) + " [" + fields + " ]\n " ;
131
+ if (fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
132
+ declarations += " \t type struct_" + name + " = " + " native.CStruct" + std::to_string (fieldCnt ) + " [" + fields + " ]\n " ;
124
133
} else {
125
134
// There is no easy way to represent it as a struct in scala native, have to represent it as an array and then
126
135
// Add helpers to help with it's manipulation
127
136
uint64_t size = astContext->getTypeSize (record->getTypeForDecl ());
128
137
declarations += " \t type struct_" + name + " = " + " native.CArray[Byte, " + uint64ToScalaNat (size) + " ]\n " ;
129
138
}
130
139
140
+ // Create helpers in an implicit class
141
+ if (fieldCnt > 0 && fieldCnt < SCALA_NATIVE_MAX_STRUCT_FIELDS){
142
+ helpers += " \t implicit class struct_" + name + " _ops(val p: native.Ptr[struct_" + name + " ]) extends AnyVal {\n " ;
143
+ helpers += helpersFunc;
144
+ helpers += " \t }\n\n " ;
145
+ }
146
+
131
147
return true ;
132
148
}
133
149
return false ;
@@ -158,7 +174,7 @@ int main(int argc, char *argv[]) {
158
174
159
175
declarations = " " ;
160
176
enums = " " ;
161
-
177
+ helpers = " " ;
162
178
163
179
int result = Tool.run (clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get ());
164
180
@@ -170,13 +186,20 @@ int main(int argc, char *argv[]) {
170
186
<< " @native.extern\n "
171
187
<< " object " << lib << " {\n "
172
188
<< declarations
173
- << " }\n\n " ;
189
+ << " }\n\n "
190
+ << " import " + lib + " ._\n\n " ;
174
191
}
175
192
176
193
if (enums != " " ){
177
194
llvm::outs () << " object " << lib << " Enums {\n "
178
195
<< enums
179
- << " }\n " ;
196
+ << " }\n\n " ;
197
+ }
198
+
199
+ if (helpers != " " ){
200
+ llvm::outs () << " object " << lib << " Helpers {\n "
201
+ << helpers
202
+ << " }\n\n " ;
180
203
}
181
204
182
205
return result;
0 commit comments