Skip to content

Commit 497c6b4

Browse files
authored
Merge pull request #28 from jonas/reserved-words
Add a test for reserved words
2 parents 5323f41 + f384d75 commit 497c6b4

File tree

6 files changed

+99
-16
lines changed

6 files changed

+99
-16
lines changed

TypeTranslator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
153153
return TranslatePointer(tpe->getAs<clang::PointerType>()->getPointeeType(), avoid);
154154

155155
} else if(qtpe->isStructureType() || qtpe->isUnionType()){
156-
return TranslateStructOrUnion(qtpe);
156+
return handleReservedWords(TranslateStructOrUnion(qtpe));
157157

158158
} else if(qtpe->isEnumeralType()){
159159
return TranslateEnum(qtpe);
@@ -166,10 +166,10 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
166166

167167
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
168168
if(found != typeMap.end()){
169-
return found->second;
169+
return handleReservedWords(found->second);
170170
} else {
171171
//TODO: Properly handle non-default types
172-
return qtpe.getUnqualifiedType().getAsString();
172+
return handleReservedWords(qtpe.getUnqualifiedType().getAsString());
173173
}
174174
}
175175

Utils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ static std::array<std::string, 39> reserved_words = {"abstract", "case", "catch"
6868
"protected", "return", "sealed", "super", "this", "throw", "trait", "try",
6969
"true", "type", "val", "var", "while", "with", "yield"};
7070

71-
inline std::string handleReservedWords(std::string name){
71+
inline std::string handleReservedWords(std::string name, std::string suffix = "") {
7272
auto found = std::find(reserved_words.begin(), reserved_words.end(), name);
73-
if(found != reserved_words.end()){
74-
return "`" + name + "`";
75-
} else{
76-
return name;
73+
if (found != reserved_words.end()) {
74+
return "`" + name + suffix + "`";
75+
} else {
76+
return name + suffix;
7777
}
7878
}
7979

ir/Struct.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ std::string Struct::generateHelperClass() const {
4646
int fieldIndex = 0;
4747
for (const auto &field : fields) {
4848
if (!field.getName().empty()) {
49-
std::string fname = handleReservedWords(field.getName());
49+
std::string getter = handleReservedWords(field.getName());
50+
std::string setter = handleReservedWords(field.getName(), "_=");
5051
std::string ftype = field.getType();
51-
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n"
52-
<< " def " << fname << "_=(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
52+
s << " def " << getter << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n"
53+
<< " def " << setter << "(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
5354
<< " = value\n";
5455
}
5556
fieldIndex++;
@@ -81,13 +82,14 @@ std::string Union::generateHelperClass() const {
8182
<< "(val p: native.Ptr[union_" << name << "]) extends AnyVal {\n";
8283
for (const auto &field : fields) {
8384
if (!field.getName().empty()) {
84-
std::string fname = handleReservedWords(field.getName());
85+
std::string getter = handleReservedWords(field.getName());
86+
std::string setter = handleReservedWords(field.getName(), "_=");
8587
std::string ftype = field.getType();
86-
s << " def " << fname
88+
s << " def " << getter
8789
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]\n";
8890

89-
s << " def " << fname
90-
<< "_=(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value\n";
91+
s << " def " << setter
92+
<< "(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value\n";
9193
}
9294
}
9395
s << " }\n";

tests/build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ inThisBuild(
1414

1515
val `scala-native-bindgen-tests` = project
1616
.in(file("."))
17+
.aggregate(samples)
1718
.settings(
1819
fork in Test := true,
1920
javaOptions in Test += "-Dbindgen.path=" + file("../target/scalaBindgen"),
@@ -25,5 +26,6 @@ val `scala-native-bindgen-tests` = project
2526
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
2627
)
2728

28-
val samples = project
29+
lazy val samples = project
2930
.enablePlugins(ScalaNativePlugin)
31+
.settings(test := (compile in Compile).value)

tests/samples/ReservedWords.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
typedef int match;
2+
typedef match var[5];
3+
4+
struct object {
5+
match yield;
6+
int val;
7+
};
8+
9+
typedef struct object object;
10+
typedef struct object type;
11+
12+
union lazy {
13+
object *instance;
14+
match forSome;
15+
struct {
16+
char def;
17+
type *super;
18+
} implicit;
19+
};
20+
21+
typedef union lazy lazy;
22+
23+
type with(match sealed, var implicit, lazy forSome);
24+
25+
typedef match def;
26+
typedef struct { def val; lazy finally; } finally;
27+
match implicit(finally type[12]);

tests/samples/ReservedWords.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import scala.scalanative._
2+
import scala.scalanative.native._
3+
import scala.scalanative.native.Nat._
4+
5+
@native.link("ReservedWords")
6+
@native.extern
7+
object ReservedWords {
8+
type `match` = native.CInt
9+
type `var` = native.CArray[`match`, _5]
10+
type `object` = struct_object
11+
type `type` = struct_object
12+
type `lazy` = union_lazy
13+
type `def` = `match`
14+
type `finally` = struct_finally
15+
type struct_object = native.CStruct2[`match`, native.CInt]
16+
type struct_finally = native.CStruct2[`def`, `lazy`]
17+
type union_lazy = native.CArray[Byte, Digit[_1, Digit[_2, _8]]]
18+
def `with`(`sealed`: `match`, `implicit`: native.Ptr[`match`], `forSome`: `lazy`): `type` = native.extern
19+
def `implicit`(`type`: native.Ptr[`finally`]): `match` = native.extern
20+
}
21+
22+
import ReservedWords._
23+
24+
object ReservedWordsHelpers {
25+
26+
implicit class struct_object_ops(val p: native.Ptr[struct_object]) extends AnyVal {
27+
def `yield`: `match` = !p._1
28+
def `yield_=`(value: `match`):Unit = !p._1 = value
29+
def `val`: native.CInt = !p._2
30+
def `val_=`(value: native.CInt):Unit = !p._2 = value
31+
}
32+
33+
def struct_object()(implicit z: native.Zone): native.Ptr[struct_object] = native.alloc[struct_object]
34+
35+
implicit class struct_finally_ops(val p: native.Ptr[struct_finally]) extends AnyVal {
36+
def `val`: `def` = !p._1
37+
def `val_=`(value: `def`):Unit = !p._1 = value
38+
def `finally`: `lazy` = !p._2
39+
def `finally_=`(value: `lazy`):Unit = !p._2 = value
40+
}
41+
42+
def struct_finally()(implicit z: native.Zone): native.Ptr[struct_finally] = native.alloc[struct_finally]
43+
44+
implicit class union_lazy_pos(val p: native.Ptr[union_lazy]) extends AnyVal {
45+
def instance: native.Ptr[native.Ptr[`object`]] = p.cast[native.Ptr[native.Ptr[`object`]]]
46+
def instance_=(value: native.Ptr[`object`]): Unit = !p.cast[native.Ptr[native.Ptr[`object`]]] = value
47+
def `forSome`: native.Ptr[`match`] = p.cast[native.Ptr[`match`]]
48+
def `forSome_=`(value: `match`): Unit = !p.cast[native.Ptr[`match`]] = value
49+
def `implicit`: native.Ptr[native.CArray[Byte, Digit[_1, Digit[_2, _8]]]] = p.cast[native.Ptr[native.CArray[Byte, Digit[_1, Digit[_2, _8]]]]]
50+
def `implicit_=`(value: native.CArray[Byte, Digit[_1, Digit[_2, _8]]]): Unit = !p.cast[native.Ptr[native.CArray[Byte, Digit[_1, Digit[_2, _8]]]]] = value
51+
}
52+
}

0 commit comments

Comments
 (0)