Move symbol-table parameter setup inside FunctionDeclaration.

Previously, this was a group effort shared by the Parser and
DSLFunction.

Change-Id: Ibe84f3584887f288b5515057f5be9df73605d8b3
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/698841
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLParser.cpp b/src/sksl/SkSLParser.cpp
index 466b26a..7fe6063 100644
--- a/src/sksl/SkSLParser.cpp
+++ b/src/sksl/SkSLParser.cpp
@@ -545,11 +545,7 @@
     const bool hasFunctionBody = !this->checkNext(Token::Kind::TK_SEMICOLON);
     if (hasFunctionBody) {
         AutoSymbolTable symbols(this);
-        for (DSLParameter* var : parameterPointers) {
-            if (!var->fName.empty()) {
-                this->addToSymbolTable(*var);
-            }
-        }
+        result.addParametersToSymbolTable(fCompiler.context());
         Token bodyStart = this->peek();
         std::optional<DSLStatement> body = this->block();
         if (!body) {
diff --git a/src/sksl/dsl/DSLFunction.cpp b/src/sksl/dsl/DSLFunction.cpp
index 215421d..fb23f3c 100644
--- a/src/sksl/dsl/DSLFunction.cpp
+++ b/src/sksl/dsl/DSLFunction.cpp
@@ -30,7 +30,6 @@
 #include "src/sksl/ir/SkSLStatement.h"
 #include "src/sksl/ir/SkSLVariable.h"
 
-#include <cstddef>
 #include <memory>
 #include <string>
 #include <utility>
@@ -76,12 +75,6 @@
                                                std::move(paramVars),
                                                pos,
                                                &returnType.skslType());
-    if (fDecl) {
-        for (size_t i = 0; i < params.size(); ++i) {
-            params[i]->fVar = fDecl->parameters()[i];
-            params[i]->fInitialized = true;
-        }
-    }
 }
 
 void DSLFunction::prototype() {
@@ -135,4 +128,10 @@
     return DSLExpression(std::move(result), pos);
 }
 
+void DSLFunction::addParametersToSymbolTable(const Context& context) {
+    if (fDecl) {
+        fDecl->addParametersToSymbolTable(context);
+    }
+}
+
 }  // namespace SkSL::dsl
diff --git a/src/sksl/dsl/DSLFunction.h b/src/sksl/dsl/DSLFunction.h
index 761abc9..617a9e4 100644
--- a/src/sksl/dsl/DSLFunction.h
+++ b/src/sksl/dsl/DSLFunction.h
@@ -17,6 +17,7 @@
 
 namespace SkSL {
 
+class Context;
 class ExpressionArray;
 class FunctionDeclaration;
 
@@ -40,6 +41,8 @@
 
     void prototype();
 
+    void addParametersToSymbolTable(const Context& context);
+
     /**
      * Invokes the function with the given arguments.
      */
diff --git a/src/sksl/ir/SkSLFunctionDeclaration.cpp b/src/sksl/ir/SkSLFunctionDeclaration.cpp
index abf79a5..8196213 100644
--- a/src/sksl/ir/SkSLFunctionDeclaration.cpp
+++ b/src/sksl/ir/SkSLFunctionDeclaration.cpp
@@ -494,13 +494,19 @@
     if (decl) {
         return decl;
     }
-    auto result = std::make_unique<FunctionDeclaration>(pos,
-                                                        context.fModifiersPool->add(*modifiers),
-                                                        name,
-                                                        std::move(finalParameters),
-                                                        returnType,
-                                                        context.fConfig->fIsBuiltinCode);
-    return context.fSymbolTable->add(std::move(result));
+    return context.fSymbolTable->add(
+            std::make_unique<FunctionDeclaration>(pos,
+                                                  context.fModifiersPool->add(*modifiers),
+                                                  name,
+                                                  std::move(finalParameters),
+                                                  returnType,
+                                                  context.fConfig->fIsBuiltinCode));
+}
+
+void FunctionDeclaration::addParametersToSymbolTable(const Context& context) {
+    for (Variable* param : fParameters) {
+        context.fSymbolTable->addWithoutOwnership(param);
+    }
 }
 
 std::string FunctionDeclaration::mangledName() const {
diff --git a/src/sksl/ir/SkSLFunctionDeclaration.h b/src/sksl/ir/SkSLFunctionDeclaration.h
index 5bb1b77..b04168e 100644
--- a/src/sksl/ir/SkSLFunctionDeclaration.h
+++ b/src/sksl/ir/SkSLFunctionDeclaration.h
@@ -53,6 +53,8 @@
                                         Position returnTypePos,
                                         const Type* returnType);
 
+    void addParametersToSymbolTable(const Context& context);
+
     const Modifiers& modifiers() const {
         return *fModifiers;
     }
diff --git a/src/sksl/ir/SkSLSymbolTable.cpp b/src/sksl/ir/SkSLSymbolTable.cpp
index e8771c9..c7a9572 100644
--- a/src/sksl/ir/SkSLSymbolTable.cpp
+++ b/src/sksl/ir/SkSLSymbolTable.cpp
@@ -64,6 +64,11 @@
 }
 
 void SymbolTable::addWithoutOwnership(Symbol* symbol) {
+    if (symbol->name().empty()) {
+        // We have legitimate use cases of nameless symbols, such as anonymous function parameters.
+        // If we find one here, we don't need to add its name to the symbol table.
+        return;
+    }
     auto key = MakeSymbolKey(symbol->name());
 
     // If this is a function declaration, we need to keep the overload chain in sync.