Skip to content

Commit

Permalink
Munge C++ keywords, but keep fn names stable
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaye committed Sep 22, 2023
1 parent 70a3b84 commit dd890ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/cpp/jank/analyze/expr/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace jank::analyze::expr
template <typename E>
struct function : expression_base
{
option<native_string> name;
native_string name;
native_vector<function_arity<E>> arities;

runtime::object_ptr to_runtime_data() const
Expand Down
9 changes: 7 additions & 2 deletions src/cpp/jank/analyze/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,20 @@ namespace jank::analyze
{ return err(error{ "fn missing forms" }); }
auto list(full_list);

option<native_string> 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<runtime::obj::symbol>(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<expr::function_arity<expression>> arities;

Expand Down
4 changes: 2 additions & 2 deletions src/cpp/jank/codegen/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace jank::codegen
: rt_ctx{ rt_ctx },
root_expr{ expr },
root_fn{ boost::get<analyze::expr::function<analyze::expression>>(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
Expand All @@ -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<handle> processor::gen
Expand Down
28 changes: 28 additions & 0 deletions src/cpp/jank/runtime/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ namespace jank::runtime
{ '?', "_QMARK_" }
};

/* https://en.cppreference.com/w/cpp/keyword */
static native_set<native_string_view> 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;
Expand All @@ -76,6 +101,9 @@ namespace jank::runtime
{ munged.append(1, c); }
}

if(cpp_keywords.contains(munged))
{ munged += "__"; }

return munged;
}

Expand Down

0 comments on commit dd890ab

Please sign in to comment.