Skip to content

Commit 53309dc

Browse files
authored
Merge pull request #154 from kornilova-l/move-implicits-to-main-object
Move implicits to main object
2 parents d386e65 + 322765d commit 53309dc

18 files changed

+412
-479
lines changed

bindgen/ir/IR.cpp

+28-24
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
171171
}
172172
}
173173

174+
if (ir.hasHelperMethods()) {
175+
s << "\n object implicits {\n" << ir.getHelperMethods() << " }\n";
176+
}
177+
174178
if (!isLibObjectEmpty) {
175179
s << "}\n\n";
176180
}
@@ -183,30 +187,6 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
183187
s << "}\n\n";
184188
}
185189

186-
if (ir.hasHelperMethods()) {
187-
s << "import " << objectName << "._\n\n";
188-
}
189-
190-
if (ir.hasHelperMethods()) {
191-
s << "object " << ir.libName << "Helpers {\n";
192-
193-
for (const auto &st : ir.structs) {
194-
visitedTypes.clear();
195-
if (ir.shouldOutput(st, visitedTypes) && st->hasHelperMethods()) {
196-
s << "\n" << st->generateHelperClass(ir.locationManager);
197-
}
198-
}
199-
200-
for (const auto &u : ir.unions) {
201-
visitedTypes.clear();
202-
if (ir.shouldOutput(u, visitedTypes) && u->hasHelperMethods()) {
203-
s << "\n" << u->generateHelperClass(ir.locationManager);
204-
}
205-
}
206-
207-
s << "}\n\n";
208-
}
209-
210190
return s;
211191
}
212192

@@ -507,3 +487,27 @@ bool IR::shouldOutputTypeDef(
507487
/* remove unused types from included files */
508488
return locationManager.inMainFile(*typeDef->getLocation());
509489
}
490+
491+
std::string IR::getHelperMethods() const {
492+
std::stringstream s;
493+
std::vector<std::shared_ptr<const Type>> visitedTypes;
494+
495+
std::string sep = "";
496+
497+
for (const auto &st : structs) {
498+
visitedTypes.clear();
499+
if (shouldOutput(st, visitedTypes) && st->hasHelperMethods()) {
500+
s << sep << st->generateHelperClass(locationManager);
501+
sep = "\n";
502+
}
503+
}
504+
505+
for (const auto &u : unions) {
506+
visitedTypes.clear();
507+
if (shouldOutput(u, visitedTypes) && u->hasHelperMethods()) {
508+
s << sep << u->generateHelperClass(locationManager);
509+
}
510+
sep = "\n";
511+
}
512+
return s.str();
513+
}

bindgen/ir/IR.h

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class IR {
178178
bool
179179
shouldOutputType(const std::vector<std::shared_ptr<T>> &declarations) const;
180180

181+
std::string getHelperMethods() const;
182+
181183
std::string libName; // name of the library
182184
std::string linkName; // name of the library to link with
183185
std::string objectName; // name of Scala object

bindgen/ir/Struct.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ Struct::generateHelperClass(const LocationManager &locationManager) const {
3434
assert(hasHelperMethods());
3535
std::stringstream s;
3636
std::string type = replaceChar(getTypeName(), " ", "_");
37-
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
37+
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
3838
<< "])"
3939
<< " extends AnyVal {\n";
4040
if (isRepresentedAsStruct()) {
4141
s << generateHelperClassMethodsForStructRepresentation(locationManager);
4242
} else {
4343
s << generateHelperClassMethodsForArrayRepresentation(locationManager);
4444
}
45-
s << " }\n\n";
45+
s << " }\n";
4646

4747
/* makes struct instantiation easier */
48-
s << " def "
48+
s << " def "
4949
<< type + "()(implicit z: native.Zone): native.Ptr[" + type + "]"
5050
<< " = native.alloc[" + type + "]\n";
5151

@@ -147,7 +147,7 @@ std::string Struct::generateSetterForStructRepresentation(
147147
value = "!" + value;
148148
}
149149
std::stringstream s;
150-
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
150+
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
151151
<< std::to_string(fieldIndex + 1) << " = " << value << "\n";
152152
return s.str();
153153
}
@@ -169,7 +169,7 @@ std::string Struct::generateGetterForStructRepresentation(
169169
methodBody = "!" + methodBody;
170170
}
171171
std::stringstream s;
172-
s << " def " << getter << ": " << returnType << " = " << methodBody
172+
s << " def " << getter << ": " << returnType << " = " << methodBody
173173
<< "\n";
174174
return s.str();
175175
}
@@ -207,7 +207,7 @@ std::string Struct::generateSetterForArrayRepresentation(
207207
value = "!" + value;
208208
}
209209
std::stringstream s;
210-
s << " def " << setter
210+
s << " def " << setter
211211
<< "(value: " + parameterType + "): Unit = " << castedField << " = "
212212
<< value << "\n";
213213
return s.str();
@@ -243,7 +243,7 @@ std::string Struct::generateGetterForArrayRepresentation(
243243
returnType = field->getType()->str(locationManager);
244244
}
245245
std::stringstream s;
246-
s << " def " << getter << ": " << returnType << " = " << methodBody
246+
s << " def " << getter << ": " << returnType << " = " << methodBody
247247
<< "\n";
248248
return s.str();
249249
}

bindgen/ir/Union.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Union::generateHelperClass(const LocationManager &locationManager) const {
2222
assert(hasHelperMethods());
2323
std::stringstream s;
2424
std::string type = replaceChar(getTypeName(), " ", "_");
25-
s << " implicit class " << type << "_pos"
25+
s << " implicit class " << type << "_pos"
2626
<< "(val p: native.Ptr[" << type << "]) extends AnyVal {\n";
2727
for (const auto &field : fields) {
2828
if (!field->getName().empty()) {
2929
s << generateGetter(field, locationManager);
3030
s << generateSetter(field, locationManager);
3131
}
3232
}
33-
s << " }\n";
33+
s << " }\n";
3434
return s.str();
3535
}
3636

@@ -71,7 +71,7 @@ Union::generateGetter(const std::shared_ptr<Field> &field,
7171
const LocationManager &locationManager) const {
7272
std::string getter = handleReservedWords(field->getName());
7373
std::string ftype = field->getType()->str(locationManager);
74-
return " def " + getter + ": native.Ptr[" + ftype +
74+
return " def " + getter + ": native.Ptr[" + ftype +
7575
"] = p.cast[native.Ptr[" + ftype + "]]\n";
7676
}
7777

@@ -82,9 +82,9 @@ Union::generateSetter(const std::shared_ptr<Field> &field,
8282
std::string ftype = field->getType()->str(locationManager);
8383
if (isAliasForType<ArrayType>(field->getType().get()) ||
8484
isAliasForType<Struct>(field->getType().get())) {
85-
return " def " + setter + "(value: native.Ptr[" + ftype +
85+
return " def " + setter + "(value: native.Ptr[" + ftype +
8686
"]): Unit = !p.cast[native.Ptr[" + ftype + "]] = !value\n";
8787
}
88-
return " def " + setter + "(value: " + ftype +
88+
return " def " + setter + "(value: " + ftype +
8989
"): Unit = !p.cast[native.Ptr[" + ftype + "]] = value\n";
9090
}

tests/samples/AnonymousTypes.scala

+31-38
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,37 @@ object AnonymousTypes {
1313
type struct_anonymous_2 = native.CStruct1[native.CInt]
1414
def foo(s: native.Ptr[struct_anonymous_0]): Unit = native.extern
1515
def bar(): native.Ptr[struct_anonymous_2] = native.extern
16-
}
17-
18-
import AnonymousTypes._
19-
20-
object AnonymousTypesHelpers {
21-
22-
implicit class struct_anonymous_0_ops(val p: native.Ptr[struct_anonymous_0]) extends AnyVal {
23-
def a: native.CChar = !p._1
24-
def a_=(value: native.CChar): Unit = !p._1 = value
25-
}
26-
27-
def struct_anonymous_0()(implicit z: native.Zone): native.Ptr[struct_anonymous_0] = native.alloc[struct_anonymous_0]
28-
29-
implicit class struct_anonymous_1_ops(val p: native.Ptr[struct_anonymous_1]) extends AnyVal {
30-
def innerUnion: native.Ptr[union_anonymous_0] = !p._1
31-
def innerUnion_=(value: native.Ptr[union_anonymous_0]): Unit = !p._1 = value
32-
}
33-
34-
def struct_anonymous_1()(implicit z: native.Zone): native.Ptr[struct_anonymous_1] = native.alloc[struct_anonymous_1]
35-
36-
implicit class struct_StructWithAnonymousStruct_ops(val p: native.Ptr[struct_StructWithAnonymousStruct]) extends AnyVal {
37-
def innerStruct: native.Ptr[struct_anonymous_1] = !p._1
38-
def innerStruct_=(value: native.Ptr[struct_anonymous_1]): Unit = !p._1 = value
39-
def innerEnum: native.CUnsignedInt = !p._2
40-
def innerEnum_=(value: native.CUnsignedInt): Unit = !p._2 = value
41-
}
42-
43-
def struct_StructWithAnonymousStruct()(implicit z: native.Zone): native.Ptr[struct_StructWithAnonymousStruct] = native.alloc[struct_StructWithAnonymousStruct]
44-
45-
implicit class struct_anonymous_2_ops(val p: native.Ptr[struct_anonymous_2]) extends AnyVal {
46-
def result: native.CInt = !p._1
47-
def result_=(value: native.CInt): Unit = !p._1 = value
48-
}
49-
50-
def struct_anonymous_2()(implicit z: native.Zone): native.Ptr[struct_anonymous_2] = native.alloc[struct_anonymous_2]
5116

52-
implicit class union_anonymous_0_pos(val p: native.Ptr[union_anonymous_0]) extends AnyVal {
53-
def a: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
54-
def a_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
17+
object implicits {
18+
implicit class struct_anonymous_0_ops(val p: native.Ptr[struct_anonymous_0]) extends AnyVal {
19+
def a: native.CChar = !p._1
20+
def a_=(value: native.CChar): Unit = !p._1 = value
21+
}
22+
def struct_anonymous_0()(implicit z: native.Zone): native.Ptr[struct_anonymous_0] = native.alloc[struct_anonymous_0]
23+
24+
implicit class struct_anonymous_1_ops(val p: native.Ptr[struct_anonymous_1]) extends AnyVal {
25+
def innerUnion: native.Ptr[union_anonymous_0] = !p._1
26+
def innerUnion_=(value: native.Ptr[union_anonymous_0]): Unit = !p._1 = value
27+
}
28+
def struct_anonymous_1()(implicit z: native.Zone): native.Ptr[struct_anonymous_1] = native.alloc[struct_anonymous_1]
29+
30+
implicit class struct_StructWithAnonymousStruct_ops(val p: native.Ptr[struct_StructWithAnonymousStruct]) extends AnyVal {
31+
def innerStruct: native.Ptr[struct_anonymous_1] = !p._1
32+
def innerStruct_=(value: native.Ptr[struct_anonymous_1]): Unit = !p._1 = value
33+
def innerEnum: native.CUnsignedInt = !p._2
34+
def innerEnum_=(value: native.CUnsignedInt): Unit = !p._2 = value
35+
}
36+
def struct_StructWithAnonymousStruct()(implicit z: native.Zone): native.Ptr[struct_StructWithAnonymousStruct] = native.alloc[struct_StructWithAnonymousStruct]
37+
38+
implicit class struct_anonymous_2_ops(val p: native.Ptr[struct_anonymous_2]) extends AnyVal {
39+
def result: native.CInt = !p._1
40+
def result_=(value: native.CInt): Unit = !p._1 = value
41+
}
42+
def struct_anonymous_2()(implicit z: native.Zone): native.Ptr[struct_anonymous_2] = native.alloc[struct_anonymous_2]
43+
44+
implicit class union_anonymous_0_pos(val p: native.Ptr[union_anonymous_0]) extends AnyVal {
45+
def a: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
46+
def a_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
47+
}
5548
}
5649
}

0 commit comments

Comments
 (0)