From f0b4ddf0b4261782f6ddc497a40a1bf5ca76719f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20T=C3=B4rres?= Date: Sun, 26 May 2024 07:56:26 -0700 Subject: [PATCH] fix: method argument name collision (#57) --- types/objc/type_encoding.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/types/objc/type_encoding.go b/types/objc/type_encoding.go index 56d0dae..9efaecb 100644 --- a/types/objc/type_encoding.go +++ b/types/objc/type_encoding.go @@ -4,7 +4,6 @@ import ( "fmt" "regexp" "strings" - "unicode" ) // References: @@ -145,6 +144,17 @@ func getLastCapitalizedPart(s string) string { return strings.ToLower(s[start:]) } +func isReserved(s string) bool { + switch s { + case "alignas", "alignof", "auto", "bool", "break", "case", "char", "const", "constexpr", "continue", "default", "do", "double", "else", "enum", "extern", "false", "float", "for", "goto", "if", "inline", "int", "long", "nullptr", "register", "restrict", "return", "short", "signed", "sizeof", "static", "static_assert", "struct", "switch", "thread_local", "true", "typedef", "typeof", "typeof_unqual", "union", "unsigned", "void", "volatile", "while": + return true // C Keywords + case "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", "bitand", "bitor", "catch", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "consteval", "constinit", "const_cast", "co_await", "co_return", "co_yield", "decltype", "delete", "dynamic_cast", "explicit", "export", "friend", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "operator", "or", "or_eq", "private", "protected", "public", "reflexpr", "reinterpret_cast", "requires", "static_cast", "synchronized", "template", "this", "throw", "try", "typeid", "typename", "using", "virtual", "wchar_t", "xor", "xor_eq": + return true // C++ Keywords + default: + return false + } +} + func getMethodWithArgs(method, returnType string, args []string) string { if len(args) <= 2 { return fmt.Sprintf("(%s)%s;", returnType, method) @@ -157,6 +167,9 @@ func getMethodWithArgs(method, returnType string, args []string) string { if len(parts) > 1 { // method has arguments based on SEL having ':' for idx, part := range parts { argName := getLastCapitalizedPart(part) + if isReserved(argName) { + argName = "_" + argName + } if len(part) == 0 || idx >= len(args) { break }