Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to valid identifier before checking for collision #942

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3140,7 +3140,8 @@ pub fn is_arg_by_pointer(resolve: &Resolve, ty: &Type) -> bool {
}

pub fn to_c_ident(name: &str) -> String {
match name {
let ident = name.to_snake_case();
match ident.as_str() {
// Escape C and C++ keywords.
// Source: https://en.cppreference.com/w/cpp/keyword
"alignas" => "alignas_".into(),
Expand Down Expand Up @@ -3245,6 +3246,6 @@ pub fn to_c_ident(name: &str) -> String {
// variable names for option and result flattening.
"ret" => "ret_".into(),
"err" => "err_".into(),
s => s.to_snake_case(),
_ => ident,
}
}
35 changes: 21 additions & 14 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2584,20 +2584,27 @@ trait ToCSharpIdent: ToOwned {

impl ToCSharpIdent for str {
fn to_csharp_ident(&self) -> String {
// Escape C# keywords
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

//TODO: Repace with actual keywords
match self {
"abstract" | "continue" | "for" | "new" | "switch" | "assert" | "default" | "goto"
| "namespace" | "synchronized" | "boolean" | "do" | "if" | "private" | "this"
| "break" | "double" | "implements" | "protected" | "throw" | "byte" | "else"
| "import" | "public" | "throws" | "case" | "enum" | "instanceof" | "return"
| "transient" | "catch" | "extends" | "int" | "short" | "try" | "char" | "final"
| "interface" | "static" | "void" | "class" | "finally" | "long" | "strictfp"
| "volatile" | "const" | "float" | "super" | "while" | "extern" | "sizeof" | "type"
| "struct" => format!("@{self}"),
_ => self.to_lower_camel_case(),
let ident = self.to_lower_camel_case();
match ident.as_str() {
// Escape C# keywords
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
"abstract" | "as" | "base" | "bool" | "break" | "byte" | "case" | "catch" | "char"
| "checked" | "class" | "const" | "continue" | "decimal" | "default" | "delegate"
| "do" | "double" | "else" | "enum" | "event" | "explicit" | "extern" | "false"
| "finally" | "fixed" | "float" | "for" | "foreach" | "goto" | "if" | "implicit"
| "in" | "int" | "interface" | "internal" | "is" | "lock" | "long" | "namespace"
| "new" | "null" | "object" | "operator" | "out" | "override" | "params"
| "private" | "protected" | "public" | "readonly" | "ref" | "return" | "sbyte"
| "sealed" | "short" | "sizeof" | "stackalloc" | "static" | "string" | "struct"
| "switch" | "this" | "throw" | "true" | "try" | "typeof" | "uint" | "ulong"
| "unchecked" | "unsafe" | "ushort" | "using" | "virtual" | "void" | "volatile"
| "while" | "add" | "and" | "alias" | "ascending" | "args" | "async" | "await"
| "by" | "descending" | "dynamic" | "equals" | "file" | "from" | "get" | "global"
| "group" | "init" | "into" | "join" | "let" | "managed" | "nameof" | "nint"
| "not" | "notnull" | "nuint" | "on" | "or" | "orderby" | "partial" | "record"
| "remove" | "required" | "scoped" | "select" | "set" | "unmanaged" | "value"
| "var" | "when" | "where" | "with" | "yield" => format!("@{ident}"),
_ => ident,
}
}
}
5 changes: 3 additions & 2 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,8 @@ struct FnSig {
}

pub fn to_rust_ident(name: &str) -> String {
match name {
let ident = name.to_snake_case();
match ident.as_str() {
// Escape Rust keywords.
// Source: https://doc.rust-lang.org/reference/keywords.html
"as" => "as_".into(),
Expand Down Expand Up @@ -1224,7 +1225,7 @@ pub fn to_rust_ident(name: &str) -> String {
"virtual" => "virtual_".into(),
"yield" => "yield_".into(),
"try" => "try_".into(),
s => s.to_snake_case(),
_ => ident,
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2273,17 +2273,18 @@ trait ToJavaIdent: ToOwned {

impl ToJavaIdent for str {
fn to_java_ident(&self) -> String {
// Escape Java keywords
// Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
match self {
let ident = self.to_lower_camel_case();
match ident.as_str() {
// Escape Java keywords
// Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
"abstract" | "continue" | "for" | "new" | "switch" | "assert" | "default" | "goto"
| "package" | "synchronized" | "boolean" | "do" | "if" | "private" | "this"
| "break" | "double" | "implements" | "protected" | "throw" | "byte" | "else"
| "import" | "public" | "throws" | "case" | "enum" | "instanceof" | "return"
| "transient" | "catch" | "extends" | "int" | "short" | "try" | "char" | "final"
| "interface" | "static" | "void" | "class" | "finally" | "long" | "strictfp"
| "volatile" | "const" | "float" | "native" | "super" | "while" => format!("{self}_"),
_ => self.to_lower_camel_case(),
| "volatile" | "const" | "float" | "native" | "super" | "while" => format!("{ident}_"),
_ => ident,
}
}
}
15 changes: 15 additions & 0 deletions tests/codegen/keywords-uppercase.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package foo:foo;

interface keywords-uppercase {
record CONST {
BREAK: u8,
ELSE: u8,
CONST-CAST: u8,
}
WHILE: func(ENUM: CONST) -> option<CONST>;
}

world the-world {
import keywords-uppercase;
export keywords-uppercase;
}
Loading