Skip to content

Commit a436cc7

Browse files
authored
Merge pull request #4 from kornilova-l/struct-alloc-helper
Add struct alloc helper
2 parents 28972c2 + e7a5e8d commit a436cc7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

SimpleTypeTests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ TEST_CASE("struct pointer", "[Type]"){
6666
REQUIRE(answ == Translate(code));
6767
}
6868

69+
TEST_CASE("struct helper methods") {
70+
std::string code = "struct listing { int a; int b; };";
71+
Translate(code);
72+
std::string answ = "\timplicit class struct_listing_ops(val p: native.Ptr[struct_listing]) extends AnyVal {\n"
73+
"\t\tdef a: native.CInt = !p._1\n"
74+
"\t\tdef a_=(value: native.CInt):Unit = !p._1 = value\n"
75+
"\t\tdef b: native.CInt = !p._2\n"
76+
"\t\tdef b_=(value: native.CInt):Unit = !p._2 = value\n"
77+
"\t}\n\n"
78+
"\tdef struct_listing()(implicit z: native.Zone): native.Ptr[struct_listing] = native.alloc[struct_listing]\n\n";
79+
REQUIRE(answ == helpers);
80+
}
81+
6982
TEST_CASE("func no args", "[Func]"){
7083
std::string code = "int foo();";
7184
std::string answ = "\tdef foo(): native.CInt = native.extern\n";

TreeVisitor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
112112
helpers += helpersFunc;
113113
helpers += "\t}\n\n";
114114

115-
return true;
115+
return true;
116116

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

@@ -165,6 +165,8 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
165165
helpers += "\timplicit class " + newName + "_ops(val p: native.Ptr[struct_" + name + "]) extends AnyVal {\n";
166166
helpers += helpersFunc;
167167
helpers += "\t}\n\n";
168+
169+
helpers += "\tdef " + newName + "()(implicit z: native.Zone): native.Ptr[" + newName + "] = native.alloc[" + newName + "]\n\n";
168170
}
169171

170172
return true;

0 commit comments

Comments
 (0)