From b428bf4600774b49de780e1dae7ff00f6564e72f Mon Sep 17 00:00:00 2001 From: Emily Schmidt Date: Tue, 27 Aug 2024 13:10:34 +0100 Subject: [PATCH] functional backends: identifiers in c++/smtlib may not start with digits --- backends/functional/cxx.cc | 4 ++-- backends/functional/smtlib.cc | 4 ++-- kernel/functional.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backends/functional/cxx.cc b/backends/functional/cxx.cc index 282eb8f67..de7737a42 100644 --- a/backends/functional/cxx.cc +++ b/backends/functional/cxx.cc @@ -48,8 +48,8 @@ template struct CxxScope : public Functional::Scope { for(const char **p = reserved_keywords; *p != nullptr; p++) this->reserve(*p); } - bool is_character_legal(char c) override { - return isascii(c) && (isalnum(c) || c == '_' || c == '$'); + bool is_character_legal(char c, int index) override { + return isascii(c) && (isalpha(c) || (isdigit(c) && index > 0) || c == '_' || c == '$'); } }; diff --git a/backends/functional/smtlib.cc b/backends/functional/smtlib.cc index 4e176c2fd..ae6ae40ca 100644 --- a/backends/functional/smtlib.cc +++ b/backends/functional/smtlib.cc @@ -48,8 +48,8 @@ struct SmtScope : public Functional::Scope { for(const char **p = reserved_keywords; *p != nullptr; p++) reserve(*p); } - bool is_character_legal(char c) override { - return isascii(c) && (isalnum(c) || strchr("~!@$%^&*_-+=<>.?/", c)); + bool is_character_legal(char c, int index) override { + return isascii(c) && (isalpha(c) || (isdigit(c) && index > 0) || strchr("~!@$%^&*_-+=<>.?/", c)); } }; diff --git a/kernel/functional.h b/kernel/functional.h index bc7b86ac1..2afdd7fc3 100644 --- a/kernel/functional.h +++ b/kernel/functional.h @@ -576,7 +576,7 @@ namespace Functional { template class Scope { protected: char substitution_character = '_'; - virtual bool is_character_legal(char) = 0; + virtual bool is_character_legal(char, int) = 0; private: pool _used_names; dict _by_id; @@ -587,7 +587,7 @@ namespace Functional { std::string unique_name(IdString suggestion) { std::string str = RTLIL::unescape_id(suggestion); for(size_t i = 0; i < str.size(); i++) - if(!is_character_legal(str[i])) + if(!is_character_legal(str[i], i)) str[i] = substitution_character; if(_used_names.count(str) == 0) { _used_names.insert(str);