Skip to content

Commit 01751cc

Browse files
committed
Handle Scala reserved name
1 parent 4a463c4 commit 01751cc

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

TreeVisitor.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
1616

1717
for (const clang::ParmVarDecl* parm : func->parameters()){
1818
//Handle default values
19-
std::string pname = parm->getNameAsString();
19+
std::string pname = handleReservedWords(parm->getNameAsString());
2020
if(pname == ""){
2121
pname = "anonymous";
2222
}
23+
2324
params += pname;
2425
params += ": ";
2526
params += typeTranslator.Translate(parm->getType());
@@ -58,10 +59,12 @@ bool TreeVisitor::VisitEnumDecl(clang::EnumDecl *enumdecl){
5859

5960
int i = 0;
6061
for (const clang::EnumConstantDecl* en : enumdecl->enumerators()){
62+
std::string ename = handleReservedWords(en->getNameAsString());
63+
6164
if(name != ""){
62-
enums += "\tfinal val enum_" + name + "_" + en->getNameAsString() + " = " + std::to_string(i++) + "\n";
65+
enums += "\tfinal val enum_" + name + "_" + ename + " = " + std::to_string(i++) + "\n";
6366
} else {
64-
enums += "\tfinal val enum_" + en->getNameAsString() + " = " + std::to_string(i++) + "\n";
67+
enums += "\tfinal val enum_" + ename + " = " + std::to_string(i++) + "\n";
6568
}
6669
}
6770

@@ -103,7 +106,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
103106
std::string helpersFunc = "";
104107

105108
for(const clang::FieldDecl* field : record->fields()){
106-
std::string fname = field->getNameAsString();
109+
std::string fname = handleReservedWords(field->getNameAsString());
107110
std::string ftype = typeTranslator.Translate(field->getType(), &name);
108111

109112
fields += ftype + ", ";

Utils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@ inline bool typeEquals(const clang::Type* tpe1, const std::string* tpe2){
5959
return false;
6060
}
6161

62+
static std::array<std::string, 39> reserved_words = {"abstract", "case", "catch", "class", "def", "do", "else", "extends",
63+
"false", "final", "finally", "for", "forSome", "if", "implicit", "import",
64+
"lazy", "match", "new", "null", "object", "override", "package", "private",
65+
"protected", "return", "sealed", "super", "this", "throw", "trait", "try",
66+
"true", "type", "val", "var", "while", "with", "yield"};
67+
68+
inline std::string handleReservedWords(std::string name){
69+
auto found = std::find(reserved_words.begin(), reserved_words.end(), name);
70+
if(found != reserved_words.end()){
71+
return "`" + name + "`";
72+
} else{
73+
return name;
74+
}
75+
}
76+
6277
#endif // UTILS_H

0 commit comments

Comments
 (0)