]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Change keyword set to a map
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 8 Nov 2023 13:54:51 +0000 (14:54 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:13:13 +0000 (19:13 +0100)
Some part of the code requires the token id behind a given keyword, a map
keep the "set" aspect whilst providing this additional feature.

gcc/rust/ChangeLog:

* lex/rust-lex.cc (RS_TOKEN): Remove local map.
(RS_TOKEN_KEYWORD): Likewise.
(Lexer::classify_keyword): Change call to utils.
* util/rust-keyword-values.cc (get_keywords): Add init function.
(RS_TOKEN_KEYWORD): Call to X macro.
* util/rust-keyword-values.h: Change from set to a map.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/lex/rust-lex.cc
gcc/rust/util/rust-keyword-values.cc
gcc/rust/util/rust-keyword-values.h

index 2cfc9cec5d41c1066433fbe383dcc7d54984964d..cab2abbba5af009cd8acf1e977d11e6d0f3aca21 100644 (file)
@@ -24,6 +24,7 @@
 #include "rust-session-manager.h"
 #include "safe-ctype.h"
 #include "cpplib.h"
+#include "rust-keyword-values.h"
 
 namespace Rust {
 // TODO: move to separate compilation unit?
@@ -254,25 +255,12 @@ Lexer::replace_current_token (TokenPtr replacement)
   rust_debug ("called 'replace_current_token' - this is deprecated");
 }
 
-/* shitty anonymous namespace that can only be accessed inside the compilation
- * unit - used for classify_keyword binary search in sorted array of keywords
- * created with x-macros. */
-namespace {
-// TODO: make constexpr when update to c++20
-const std::map<std::string, TokenId> keywords = {
-#define RS_TOKEN(x, y)
-#define RS_TOKEN_KEYWORD(tok, key) {key, tok},
-  RS_TOKEN_LIST
-#undef RS_TOKEN_KEYWORD
-#undef RS_TOKEN
-};
-} // namespace
-
 /* Determines whether the string passed in is a keyword or not. If it is, it
  * returns the keyword name.  */
 TokenId
 Lexer::classify_keyword (const std::string &str)
 {
+  auto &keywords = Rust::Values::Keywords::keywords;
   auto keyword = keywords.find (str);
 
   if (keyword == keywords.end ())
index 29bc65eba76669c89f78743f73b4846e4072aea0..58a404dc2aae980075e45d98065f6736b801974a 100644 (file)
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-keyword-values.h"
+#include "rust-token.h"
 
 namespace Rust {
 namespace Values {
 
-const std::set<std::string> Keywords::keywords = {
+// TODO: Can't we do this inline ?
+static std::map<std::string, TokenId>
+get_keywords ()
+{
+  std::map<std::string, TokenId> m = {
 #define RS_TOKEN(x, y)
-#define RS_TOKEN_KEYWORD(tok, key) key,
-  RS_TOKEN_LIST
+#define RS_TOKEN_KEYWORD(tok, key) {key, tok},
+    RS_TOKEN_LIST
 #undef RS_TOKEN_KEYWORD
 #undef RS_TOKEN
-};
+  };
+  return m;
+}
+
+const std::map<std::string, TokenId> Keywords::keywords = get_keywords ();
 
 } // namespace Values
 } // namespace Rust
index cf507909d20443568195e71aa8e719f441dd69ec..3edae55c76eb76a1d5447941c02e15b749854f8b 100644 (file)
@@ -28,7 +28,7 @@ namespace Values {
 class Keywords
 {
 public:
-  const static std::set<std::string> keywords;
+  const static std::map<std::string, TokenId> keywords;
 
   // Rust keyword values
 public: