Use SymbolTableStackBuilder in the inliner.

This lets us consolidate the logic for symbol-table reassembly in
one place.

Change-Id: Ic005774eaab208431661e3a68a2039a5fd6aef2f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/598085
Reviewed-by: Arman Uguray <armansito@google.com>
Commit-Queue: Arman Uguray <armansito@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLAnalysis.h b/src/sksl/SkSLAnalysis.h
index eddd7a8..5c48ac5 100644
--- a/src/sksl/SkSLAnalysis.h
+++ b/src/sksl/SkSLAnalysis.h
@@ -222,7 +222,7 @@
     SymbolTableStackBuilder(const Statement* stmt,
                             std::vector<std::shared_ptr<SymbolTable>>* stack);
 
-    // If a symbol table was added to the stack earlier, removes it here.
+    // If a symbol table was added to the stack earlier, removes it from the stack.
     ~SymbolTableStackBuilder();
 
 private:
diff --git a/src/sksl/SkSLInliner.cpp b/src/sksl/SkSLInliner.cpp
index 5404f50..721180d 100644
--- a/src/sksl/SkSLInliner.cpp
+++ b/src/sksl/SkSLInliner.cpp
@@ -796,8 +796,8 @@
             return;
         }
 
+        Analysis::SymbolTableStackBuilder scopedStackBuilder(stmt->get(), &fSymbolTableStack);
         size_t oldEnclosingStmtStackSize = fEnclosingStmtStack.size();
-        size_t oldSymbolStackSize = fSymbolTableStack.size();
 
         if (isViableAsEnclosingStatement) {
             fEnclosingStmtStack.push_back(stmt);
@@ -812,10 +812,6 @@
 
             case Statement::Kind::kBlock: {
                 Block& block = (*stmt)->as<Block>();
-                if (block.symbolTable()) {
-                    fSymbolTableStack.push_back(block.symbolTable());
-                }
-
                 for (std::unique_ptr<Statement>& blockStmt : block.children()) {
                     this->visitStatement(&blockStmt);
                 }
@@ -843,10 +839,6 @@
             }
             case Statement::Kind::kFor: {
                 ForStatement& forStmt = (*stmt)->as<ForStatement>();
-                if (forStmt.symbols()) {
-                    fSymbolTableStack.push_back(forStmt.symbols());
-                }
-
                 // The initializer and loop body are candidates for inlining.
                 this->visitStatement(&forStmt.initializer(),
                                      /*isViableAsEnclosingStatement=*/false);
@@ -882,10 +874,6 @@
             }
             case Statement::Kind::kSwitch: {
                 SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>();
-                if (switchStmt.symbols()) {
-                    fSymbolTableStack.push_back(switchStmt.symbols());
-                }
-
                 this->visitExpression(&switchStmt.value());
                 for (const std::unique_ptr<Statement>& switchCase : switchStmt.cases()) {
                     // The switch-case's fValue cannot be a FunctionCall; skip it.
@@ -904,7 +892,6 @@
         }
 
         // Pop our symbol and enclosing-statement stacks.
-        fSymbolTableStack.resize(oldSymbolStackSize);
         fEnclosingStmtStack.resize(oldEnclosingStmtStackSize);
     }
 
diff --git a/src/sksl/analysis/SkSLSymbolTableStackBuilder.cpp b/src/sksl/analysis/SkSLSymbolTableStackBuilder.cpp
index 8bfc0cb..ada31aa 100644
--- a/src/sksl/analysis/SkSLSymbolTableStackBuilder.cpp
+++ b/src/sksl/analysis/SkSLSymbolTableStackBuilder.cpp
@@ -13,6 +13,7 @@
 #include "src/sksl/ir/SkSLSwitchStatement.h"
 
 #include <memory>
+#include <utility>
 #include <vector>
 
 namespace SkSL {
@@ -27,21 +28,21 @@
         switch (stmt->kind()) {
             case Statement::Kind::kBlock:
                 if (std::shared_ptr<SymbolTable> symbols = stmt->as<Block>().symbolTable()) {
-                    stack->push_back(symbols);
+                    stack->push_back(std::move(symbols));
                     fStackToPop = stack;
                 }
                 break;
 
             case Statement::Kind::kFor:
                 if (std::shared_ptr<SymbolTable> symbols = stmt->as<ForStatement>().symbols()) {
-                    stack->push_back(symbols);
+                    stack->push_back(std::move(symbols));
                     fStackToPop = stack;
                 }
                 break;
 
             case Statement::Kind::kSwitch:
                 if (std::shared_ptr<SymbolTable> symbols = stmt->as<SwitchStatement>().symbols()) {
-                    stack->push_back(symbols);
+                    stack->push_back(std::move(symbols));
                     fStackToPop = stack;
                 }
                 break;