Simplify/localize the LayoutToken hash map

Uses the initializer_list constructor to avoid generating a bunch of
insert calls, and then moves everything right into layout().

Change-Id: I09f5538e20ea6f7d3b6117020302a41e18b66295
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/539822
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/sksl/BUILD.bazel b/src/sksl/BUILD.bazel
index 863dc22..98b7e61 100644
--- a/src/sksl/BUILD.bazel
+++ b/src/sksl/BUILD.bazel
@@ -395,7 +395,6 @@
         "//include/private:SkSLDefines_hdr",
         "//include/private:SkSLProgramKind_hdr",
         "//include/private:SkTArray_hdr",
-        "//include/private:SkTHash_hdr",
         "//include/sksl:DSLCore_hdr",
         "//include/sksl:DSLExpression_hdr",
         "//include/sksl:DSLLayout_hdr",
@@ -423,6 +422,7 @@
         "//include/private:SkSLModifiers_hdr",
         "//include/private:SkSLProgramElement_hdr",
         "//include/private:SkSLString_hdr",
+        "//include/private:SkTHash_hdr",
         "//include/sksl:DSLBlock_hdr",
         "//include/sksl:DSLCase_hdr",
         "//include/sksl:DSLFunction_hdr",
diff --git a/src/sksl/SkSLDSLParser.cpp b/src/sksl/SkSLDSLParser.cpp
index e02a9d2..19e4df8 100644
--- a/src/sksl/SkSLDSLParser.cpp
+++ b/src/sksl/SkSLDSLParser.cpp
@@ -11,6 +11,7 @@
 #include "include/private/SkSLModifiers.h"
 #include "include/private/SkSLProgramElement.h"
 #include "include/private/SkSLString.h"
+#include "include/private/SkTHash.h"
 #include "include/sksl/DSL.h"
 #include "include/sksl/DSLBlock.h"
 #include "include/sksl/DSLCase.h"
@@ -29,6 +30,7 @@
 #include "src/sksl/ir/SkSLProgram.h"
 
 #include <algorithm>
+#include <initializer_list>
 #include <memory>
 #include <type_traits>
 #include <unordered_map>
@@ -99,25 +101,6 @@
     }
 };
 
-SkTHashMap<std::string_view, DSLParser::LayoutToken>* DSLParser::sLayoutTokens;
-
-void DSLParser::InitLayoutMap() {
-    sLayoutTokens = new SkTHashMap<std::string_view, LayoutToken>;
-    #define TOKEN(name, text) sLayoutTokens->set(text, LayoutToken::name)
-    TOKEN(LOCATION,                     "location");
-    TOKEN(OFFSET,                       "offset");
-    TOKEN(BINDING,                      "binding");
-    TOKEN(INDEX,                        "index");
-    TOKEN(SET,                          "set");
-    TOKEN(BUILTIN,                      "builtin");
-    TOKEN(INPUT_ATTACHMENT_INDEX,       "input_attachment_index");
-    TOKEN(ORIGIN_UPPER_LEFT,            "origin_upper_left");
-    TOKEN(BLEND_SUPPORT_ALL_EQUATIONS,  "blend_support_all_equations");
-    TOKEN(PUSH_CONSTANT,                "push_constant");
-    TOKEN(COLOR,                        "color");
-    #undef TOKEN
-}
-
 DSLParser::DSLParser(Compiler* compiler, const ProgramSettings& settings, ProgramKind kind,
                      std::string text)
     : fCompiler(*compiler)
@@ -131,8 +114,6 @@
     // We manage our symbol tables manually, so no need for name mangling
     fSettings.fDSLMangling = false;
     fLexer.start(*fText);
-    static const bool layoutMapInitialized = []{ InitLayoutMap(); return true; }();
-    (void) layoutMapInitialized;
 }
 
 Token DSLParser::nextRawToken() {
@@ -796,6 +777,35 @@
 
 /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */
 DSLLayout DSLParser::layout() {
+    enum class LayoutToken {
+        LOCATION,
+        OFFSET,
+        BINDING,
+        INDEX,
+        SET,
+        BUILTIN,
+        INPUT_ATTACHMENT_INDEX,
+        ORIGIN_UPPER_LEFT,
+        BLEND_SUPPORT_ALL_EQUATIONS,
+        PUSH_CONSTANT,
+        COLOR,
+    };
+
+    using LayoutMap = SkTHashMap<std::string_view, LayoutToken>;
+    static LayoutMap* sLayoutTokens = new LayoutMap{
+            {"location",                    LayoutToken::LOCATION},
+            {"offset",                      LayoutToken::OFFSET},
+            {"binding",                     LayoutToken::BINDING},
+            {"index",                       LayoutToken::INDEX},
+            {"set",                         LayoutToken::SET},
+            {"builtin",                     LayoutToken::BUILTIN},
+            {"input_attachment_index",      LayoutToken::INPUT_ATTACHMENT_INDEX},
+            {"origin_upper_left",           LayoutToken::ORIGIN_UPPER_LEFT},
+            {"blend_support_all_equations", LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS},
+            {"push_constant",               LayoutToken::PUSH_CONSTANT},
+            {"color",                       LayoutToken::COLOR},
+    };
+
     DSLLayout result;
     if (this->checkNext(Token::Kind::TK_LAYOUT)) {
         if (!this->expect(Token::Kind::TK_LPAREN, "'('")) {
diff --git a/src/sksl/SkSLDSLParser.h b/src/sksl/SkSLDSLParser.h
index 70be0b0..e742ddb 100644
--- a/src/sksl/SkSLDSLParser.h
+++ b/src/sksl/SkSLDSLParser.h
@@ -12,7 +12,6 @@
 #include "include/private/SkSLDefines.h"
 #include "include/private/SkSLProgramKind.h"
 #include "include/private/SkTArray.h"
-#include "include/private/SkTHash.h"
 #include "include/sksl/DSLCore.h"
 #include "include/sksl/DSLExpression.h"
 #include "include/sksl/DSLLayout.h"
@@ -52,20 +51,6 @@
  */
 class DSLParser {
 public:
-    enum class LayoutToken {
-        LOCATION,
-        OFFSET,
-        BINDING,
-        INDEX,
-        SET,
-        BUILTIN,
-        INPUT_ATTACHMENT_INDEX,
-        ORIGIN_UPPER_LEFT,
-        BLEND_SUPPORT_ALL_EQUATIONS,
-        PUSH_CONSTANT,
-        COLOR,
-    };
-
     DSLParser(Compiler* compiler, const ProgramSettings& settings, ProgramKind kind,
               std::string text);
 
@@ -78,8 +63,6 @@
     Position position(Token token);
 
 private:
-    static void InitLayoutMap();
-
     /**
      * Return the next token, including whitespace tokens, from the parse stream.
      */
@@ -355,8 +338,6 @@
         bool fOldEncounteredFatalError;
     };
 
-    static SkTHashMap<std::string_view, LayoutToken>* sLayoutTokens;
-
     Compiler& fCompiler;
     ProgramSettings fSettings;
     ErrorReporter* fErrorReporter;