From dd890abc87361c9923d4089727cf200048794ae0 Mon Sep 17 00:00:00 2001 From: jeaye Date: Fri, 22 Sep 2023 13:09:34 -0700 Subject: [PATCH] Munge C++ keywords, but keep fn names stable --- include/cpp/jank/analyze/expr/function.hpp | 2 +- src/cpp/jank/analyze/processor.cpp | 9 +++++-- src/cpp/jank/codegen/processor.cpp | 4 ++-- src/cpp/jank/runtime/util.cpp | 28 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/include/cpp/jank/analyze/expr/function.hpp b/include/cpp/jank/analyze/expr/function.hpp index 261ec32da..c8483e099 100644 --- a/include/cpp/jank/analyze/expr/function.hpp +++ b/include/cpp/jank/analyze/expr/function.hpp @@ -60,7 +60,7 @@ namespace jank::analyze::expr template struct function : expression_base { - option name; + native_string name; native_vector> arities; runtime::object_ptr to_runtime_data() const diff --git a/src/cpp/jank/analyze/processor.cpp b/src/cpp/jank/analyze/processor.cpp index ff5dd7ddf..396cfd9fb 100644 --- a/src/cpp/jank/analyze/processor.cpp +++ b/src/cpp/jank/analyze/processor.cpp @@ -336,15 +336,20 @@ namespace jank::analyze { return err(error{ "fn missing forms" }); } auto list(full_list); - option name; + native_string name; auto first_elem(list->data.rest().first().unwrap()); if(first_elem->type == runtime::object_type::symbol) { auto const s(runtime::expect_object(first_elem)); - name = s->name; + /* TODO: Remove the generated portion here once we support codegen for making all references + * to generated code use the fully qualified name. Right now, a jank fn named `min` will + * conflict with the RT `min` fn, for example. */ + name = runtime::context::unique_string(s->name); first_elem = list->data.rest().rest().first().unwrap(); list = make_box(list->data.rest()); } + else + { name = runtime::context::unique_string("fn"); } native_vector> arities; diff --git a/src/cpp/jank/codegen/processor.cpp b/src/cpp/jank/codegen/processor.cpp index c86422af4..9e3527b98 100644 --- a/src/cpp/jank/codegen/processor.cpp +++ b/src/cpp/jank/codegen/processor.cpp @@ -251,7 +251,7 @@ namespace jank::codegen : rt_ctx{ rt_ctx }, root_expr{ expr }, root_fn{ boost::get>(expr->data) }, - struct_name{ runtime::context::unique_string(root_fn.name.unwrap_or("fn")) } + struct_name{ root_fn.name } { assert(root_fn.frame.data); } processor::processor @@ -261,7 +261,7 @@ namespace jank::codegen ) : rt_ctx{ rt_ctx }, root_fn{ expr }, - struct_name{ runtime::context::unique_string(root_fn.name.unwrap_or("fn")) } + struct_name{ root_fn.name } { assert(root_fn.frame.data); } option processor::gen diff --git a/src/cpp/jank/runtime/util.cpp b/src/cpp/jank/runtime/util.cpp index 0c70e5995..0cf5204bb 100644 --- a/src/cpp/jank/runtime/util.cpp +++ b/src/cpp/jank/runtime/util.cpp @@ -64,6 +64,31 @@ namespace jank::runtime { '?', "_QMARK_" } }; + /* https://en.cppreference.com/w/cpp/keyword */ + static native_set const cpp_keywords + { + "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", "auto", + "bitand", "bitor", "bool", "break", + "case", "catch", "char", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval", "constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", + "decltype", "default", "delete", "do", "double", "dynamic_cast", + "else", "enum", "explicit", "export", "extern", + "false", "float", "for", "friend", + "goto", + "if", "inline", "int", + "long", + "mutable", + "namespace", "new", "noexcept", "not", "not_eq", "nullptr", + "operator", "or", "or_eq", + "private", "protected", "public", + "reflexpr", "register", "reinterpret_cast", "requires", "return", + "short", "signed", "sizeof", "static", "static_assert", "static_cast", "struct", "switch", "synchronized", + "template", "this", "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", + "union", "unsigned", "using", + "virtual", "void", "volatile", + "wchar_t", "while", + "xor", "xor_eq" + }; + native_string munge(native_string const &o) { native_string munged; @@ -76,6 +101,9 @@ namespace jank::runtime { munged.append(1, c); } } + if(cpp_keywords.contains(munged)) + { munged += "__"; } + return munged; }