Skip to content

Commit e3ba2a8

Browse files
committed
Add a test for reserved words
Fix case where reserved word inside `native.Ptr[...]` was not quoted.
1 parent 5323f41 commit e3ba2a8

File tree

5 files changed

+92
-13
lines changed

5 files changed

+92
-13
lines changed

TypeTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ std::string TypeTranslator::TranslatePointer(const clang::QualType& pte, const s
8181

8282

8383

84-
return std::string("native.Ptr[") + Translate(pte, avoid) + std::string("]");
84+
return std::string("native.Ptr[") + handleReservedWords(Translate(pte, avoid)) + std::string("]");
8585
}
8686

8787
std::string TypeTranslator::TranslateStructOrUnion(const clang::QualType& qtpe){

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/samples/ReservedWords.h

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

tests/samples/ReservedWords.scala

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

0 commit comments

Comments
 (0)