Skip to content
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,
}
}
}
1 change: 1 addition & 0 deletions crates/go/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use heck::*;
macro_rules! codegen_test {
(issue668 $name:tt $test:tt) => {};
(multiversion $name:tt $test:tt) => {};
(keywords_uppercase $name:tt $test:tt) => {};
($id:ident $name:tt $test:tt) => {
#[test]
fn $id() {
Expand Down
61 changes: 9 additions & 52 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,60 +1171,17 @@ 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(),
"break" => "break_".into(),
"const" => "const_".into(),
"continue" => "continue_".into(),
"crate" => "crate_".into(),
"else" => "else_".into(),
"enum" => "enum_".into(),
"extern" => "extern_".into(),
"false" => "false_".into(),
"fn" => "fn_".into(),
"for" => "for_".into(),
"if" => "if_".into(),
"impl" => "impl_".into(),
"in" => "in_".into(),
"let" => "let_".into(),
"loop" => "loop_".into(),
"match" => "match_".into(),
"mod" => "mod_".into(),
"move" => "move_".into(),
"mut" => "mut_".into(),
"pub" => "pub_".into(),
"ref" => "ref_".into(),
"return" => "return_".into(),
"self" => "self_".into(),
"static" => "static_".into(),
"struct" => "struct_".into(),
"super" => "super_".into(),
"trait" => "trait_".into(),
"true" => "true_".into(),
"type" => "type_".into(),
"unsafe" => "unsafe_".into(),
"use" => "use_".into(),
"where" => "where_".into(),
"while" => "while_".into(),
"async" => "async_".into(),
"await" => "await_".into(),
"dyn" => "dyn_".into(),
"abstract" => "abstract_".into(),
"become" => "become_".into(),
"box" => "box_".into(),
"do" => "do_".into(),
"final" => "final_".into(),
"macro" => "macro_".into(),
"override" => "override_".into(),
"priv" => "priv_".into(),
"typeof" => "typeof_".into(),
"unsized" => "unsized_".into(),
"virtual" => "virtual_".into(),
"yield" => "yield_".into(),
"try" => "try_".into(),
s => s.to_snake_case(),
"as" | "break" | "const" | "continue" | "crate" | "else" | "enum" | "extern" | "false"
| "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | "match" | "mod" | "move"
| "mut" | "pub" | "ref" | "return" | "self" | "static" | "struct" | "super" | "trait"
| "true" | "type" | "unsafe" | "use" | "where" | "while" | "async" | "await" | "dyn"
| "abstract" | "become" | "box" | "do" | "final" | "macro" | "override" | "priv"
| "typeof" | "unsized" | "virtual" | "yield" | "try" => format!("{ident}_"),
_ => 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;
}