moved SkSL InlineMarker and UnresolvedFunction data into IRNode

Change-Id: I05b940c69b7756d41277626fc3eef06003d133c1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/324886
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/SkSLDehydrator.cpp b/src/sksl/SkSLDehydrator.cpp
index a4af947..e2f635a 100644
--- a/src/sksl/SkSLDehydrator.cpp
+++ b/src/sksl/SkSLDehydrator.cpp
@@ -170,8 +170,8 @@
             const UnresolvedFunction& f = s.as<UnresolvedFunction>();
             this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
             this->writeId(&f);
-            this->writeU8(f.fFunctions.size());
-            for (const FunctionDeclaration* funcDecl : f.fFunctions) {
+            this->writeU8(f.functions().size());
+            for (const FunctionDeclaration* funcDecl : f.functions()) {
                 this->write(*funcDecl);
             }
             break;
@@ -451,7 +451,7 @@
             case Statement::Kind::kInlineMarker: {
                 const InlineMarker& i = s->as<InlineMarker>();
                 this->writeCommand(Rehydrator::kInlineMarker_Command);
-                this->writeId(i.fFuncDecl);
+                this->writeId(&i.function());
                 break;
             }
             case Statement::Kind::kNop:
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index 2937a9f..0d423e7 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -976,7 +976,7 @@
         std::vector<const FunctionDeclaration*> functions;
         switch (entry->kind()) {
             case Symbol::Kind::kUnresolvedFunction:
-                functions = entry->as<UnresolvedFunction>().fFunctions;
+                functions = entry->as<UnresolvedFunction>().functions();
                 break;
             case Symbol::Kind::kFunctionDeclaration:
                 functions.push_back(&entry->as<FunctionDeclaration>());
@@ -1347,7 +1347,8 @@
         }
         case Symbol::Kind::kUnresolvedFunction: {
             const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
-            return std::make_unique<FunctionReference>(fContext, identifier.fOffset, f->fFunctions);
+            return std::make_unique<FunctionReference>(fContext, identifier.fOffset,
+                                                       f->functions());
         }
         case Symbol::Kind::kVariable: {
             const Variable* var = &result->as<Variable>();
diff --git a/src/sksl/SkSLInliner.cpp b/src/sksl/SkSLInliner.cpp
index 42b86bb..babd1e8 100644
--- a/src/sksl/SkSLInliner.cpp
+++ b/src/sksl/SkSLInliner.cpp
@@ -178,7 +178,8 @@
         }
 
         bool visitStatement(const Statement& stmt) override {
-            if (stmt.is<InlineMarker>() && stmt.as<InlineMarker>().fFuncDecl->matches(*fFuncDecl)) {
+            if (stmt.is<InlineMarker>() &&
+                stmt.as<InlineMarker>().function().matches(*fFuncDecl)) {
                 return true;
             }
             return INHERITED::visitStatement(stmt);
@@ -609,7 +610,7 @@
                                    arguments.size() + // Function arguments (copy out-params back)
                                    1);                // Inlined code (Block or do-while loop)
 
-    inlinedBody.children().push_back(std::make_unique<InlineMarker>(call->function()));
+    inlinedBody.children().push_back(std::make_unique<InlineMarker>(&call->function()));
 
     auto makeInlineVar =
             [&](const String& baseName, const Type* type, Modifiers modifiers,
diff --git a/src/sksl/SkSLRehydrator.cpp b/src/sksl/SkSLRehydrator.cpp
index 55d2132..0ca045f 100644
--- a/src/sksl/SkSLRehydrator.cpp
+++ b/src/sksl/SkSLRehydrator.cpp
@@ -386,7 +386,7 @@
         case Rehydrator::kInlineMarker_Command: {
             const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
                                                           Symbol::Kind::kFunctionDeclaration);
-            return std::make_unique<InlineMarker>(*funcDecl);
+            return std::make_unique<InlineMarker>(funcDecl);
         }
         case Rehydrator::kReturn_Command: {
             std::unique_ptr<Expression> expr = this->expression();
diff --git a/src/sksl/ir/SkSLIRNode.cpp b/src/sksl/ir/SkSLIRNode.cpp
index b44ea18..bec217f 100644
--- a/src/sksl/ir/SkSLIRNode.cpp
+++ b/src/sksl/ir/SkSLIRNode.cpp
@@ -68,6 +68,11 @@
 , fKind(kind)
 , fData(data) {}
 
+IRNode::IRNode(int offset, int kind, const InlineMarkerData& data)
+: fOffset(offset)
+, fKind(kind)
+, fData(data) {}
+
 IRNode::IRNode(int offset, int kind, const IntLiteralData& data)
 : fOffset(offset)
 , fKind(kind)
@@ -103,6 +108,11 @@
 , fKind(kind)
 , fData(data) {}
 
+IRNode::IRNode(int offset, int kind, const UnresolvedFunctionData& data)
+: fOffset(offset)
+, fKind(kind)
+, fData(data) {}
+
 IRNode::IRNode(int offset, int kind, const VariableData& data)
 : fOffset(offset)
 , fKind(kind)
diff --git a/src/sksl/ir/SkSLIRNode.h b/src/sksl/ir/SkSLIRNode.h
index 1f9d798..0a22046 100644
--- a/src/sksl/ir/SkSLIRNode.h
+++ b/src/sksl/ir/SkSLIRNode.h
@@ -143,6 +143,10 @@
         int64_t fValue;
     };
 
+    struct InlineMarkerData {
+        const FunctionDeclaration* fFunction;
+    };
+
     struct SettingData {
         String fName;
         const Type* fType;
@@ -163,6 +167,12 @@
         Token::Kind fToken;
     };
 
+    struct UnresolvedFunctionData {
+        // FIXME move this into the child vector after killing fExpressionChildren /
+        // fStatementChildren
+        std::vector<const FunctionDeclaration*> fFunctions;
+    };
+
     struct VariableData {
         StringFragment fName;
         const Type* fType;
@@ -196,6 +206,7 @@
             kFunctionCall,
             kFunctionDeclaration,
             kIfStatement,
+            kInlineMarker,
             kIntLiteral,
             kSetting,
             kString,
@@ -203,6 +214,7 @@
             kSymbolAlias,
             kType,
             kTypeToken,
+            kUnresolvedFunction,
             kVariable,
             kVariableReference,
         } fKind = Kind::kType;
@@ -220,6 +232,7 @@
             FunctionCallData fFunctionCall;
             FunctionDeclarationData fFunctionDeclaration;
             IfStatementData fIfStatement;
+            InlineMarkerData fInlineMarker;
             IntLiteralData fIntLiteral;
             SettingData fSetting;
             String fString;
@@ -227,6 +240,7 @@
             SymbolAliasData fSymbolAlias;
             const Type* fType;
             TypeTokenData fTypeToken;
+            UnresolvedFunctionData fUnresolvedFunction;
             VariableData fVariable;
             VariableReferenceData fVariableReference;
 
@@ -290,6 +304,11 @@
             *(new(&fContents) IfStatementData) = data;
         }
 
+        NodeData(InlineMarkerData data)
+            : fKind(Kind::kInlineMarker) {
+            *(new(&fContents) InlineMarkerData) = data;
+        }
+
         NodeData(IntLiteralData data)
             : fKind(Kind::kIntLiteral) {
             *(new(&fContents) IntLiteralData) = data;
@@ -325,6 +344,11 @@
             *(new(&fContents) TypeTokenData) = data;
         }
 
+        NodeData(const UnresolvedFunctionData& data)
+            : fKind(Kind::kUnresolvedFunction) {
+            *(new(&fContents) UnresolvedFunctionData) = data;
+        }
+
         NodeData(const VariableData& data)
             : fKind(Kind::kVariable) {
             *(new(&fContents) VariableData) = data;
@@ -377,6 +401,9 @@
                 case Kind::kIfStatement:
                     *(new(&fContents) IfStatementData) = other.fContents.fIfStatement;
                     break;
+                case Kind::kInlineMarker:
+                    *(new(&fContents) InlineMarkerData) = other.fContents.fInlineMarker;
+                    break;
                 case Kind::kIntLiteral:
                     *(new(&fContents) IntLiteralData) = other.fContents.fIntLiteral;
                     break;
@@ -398,6 +425,9 @@
                 case Kind::kTypeToken:
                     *(new(&fContents) TypeTokenData) = other.fContents.fTypeToken;
                     break;
+                case Kind::kUnresolvedFunction:
+                    *(new(&fContents) UnresolvedFunctionData) = other.fContents.fUnresolvedFunction;
+                    break;
                 case Kind::kVariable:
                     *(new(&fContents) VariableData) = other.fContents.fVariable;
                     break;
@@ -442,11 +472,14 @@
                 case Kind::kFunctionCall:
                     fContents.fFunctionCall.~FunctionCallData();
                     break;
+                case Kind::kFunctionDeclaration:
+                    fContents.fFunctionDeclaration.~FunctionDeclarationData();
+                    break;
                 case Kind::kIfStatement:
                     fContents.fIfStatement.~IfStatementData();
                     break;
-                case Kind::kFunctionDeclaration:
-                    fContents.fFunctionDeclaration.~FunctionDeclarationData();
+                case Kind::kInlineMarker:
+                    fContents.fInlineMarker.~InlineMarkerData();
                     break;
                 case Kind::kIntLiteral:
                     fContents.fIntLiteral.~IntLiteralData();
@@ -468,6 +501,9 @@
                 case Kind::kTypeToken:
                     fContents.fTypeToken.~TypeTokenData();
                     break;
+                case Kind::kUnresolvedFunction:
+                    fContents.fUnresolvedFunction.~UnresolvedFunctionData();
+                    break;
                 case Kind::kVariable:
                     fContents.fVariable.~VariableData();
                     break;
@@ -497,9 +533,11 @@
 
     IRNode(int offset, int kind, const FunctionCallData& data);
 
+    IRNode(int offset, int kind, const FunctionDeclarationData& data);
+
     IRNode(int offset, int kind, const IfStatementData& data);
 
-    IRNode(int offset, int kind, const FunctionDeclarationData& data);
+    IRNode(int offset, int kind, const InlineMarkerData& data);
 
     IRNode(int offset, int kind, const IntLiteralData& data);
 
@@ -515,6 +553,8 @@
 
     IRNode(int offset, int kind, const TypeTokenData& data);
 
+    IRNode(int offset, int kind, const UnresolvedFunctionData& data);
+
     IRNode(int offset, int kind, const VariableData& data);
 
     IRNode(int offset, int kind, const VariableReferenceData& data);
@@ -613,14 +653,19 @@
         return fData.fContents.fFunctionDeclaration;
     }
 
+    const FunctionDeclarationData& functionDeclarationData() const {
+        SkASSERT(fData.fKind == NodeData::Kind::kFunctionDeclaration);
+        return fData.fContents.fFunctionDeclaration;
+    }
+
     const IfStatementData& ifStatementData() const {
         SkASSERT(fData.fKind == NodeData::Kind::kIfStatement);
         return fData.fContents.fIfStatement;
     }
 
-    const FunctionDeclarationData& functionDeclarationData() const {
-        SkASSERT(fData.fKind == NodeData::Kind::kFunctionDeclaration);
-        return fData.fContents.fFunctionDeclaration;
+    const InlineMarkerData& inlineMarkerData() const {
+        SkASSERT(fData.fKind == NodeData::Kind::kInlineMarker);
+        return fData.fContents.fInlineMarker;
     }
 
     const IntLiteralData& intLiteralData() const {
@@ -663,6 +708,11 @@
         return fData.fContents.fTypeToken;
     }
 
+    const UnresolvedFunctionData& unresolvedFunctionData() const {
+        SkASSERT(fData.fKind == NodeData::Kind::kUnresolvedFunction);
+        return fData.fContents.fUnresolvedFunction;
+    }
+
     VariableData& variableData() {
         SkASSERT(fData.fKind == NodeData::Kind::kVariable);
         return fData.fContents.fVariable;
diff --git a/src/sksl/ir/SkSLInlineMarker.h b/src/sksl/ir/SkSLInlineMarker.h
index 725fbab..9b01c9ce 100644
--- a/src/sksl/ir/SkSLInlineMarker.h
+++ b/src/sksl/ir/SkSLInlineMarker.h
@@ -18,25 +18,30 @@
  * A no-op statement that indicates that a function was inlined here. This is necessary to detect
  * and prevent runaway infinite recursion. This node doesn't directly generate code.
  */
-struct InlineMarker : public Statement {
+class InlineMarker : public Statement {
+public:
     static constexpr Kind kStatementKind = Kind::kInlineMarker;
 
-    InlineMarker(const FunctionDeclaration& funcDecl)
-            : INHERITED(-1, kStatementKind), fFuncDecl(&funcDecl) {}
+    InlineMarker(const FunctionDeclaration* function)
+            : INHERITED(-1, InlineMarkerData{function}) {}
+
+    const FunctionDeclaration& function() const {
+        return *this->inlineMarkerData().fFunction;
+    }
 
     bool isEmpty() const override {
         return true;
     }
 
     String description() const override {
-        return String("/* inlined: ") + fFuncDecl->name() + String(" */");
+        return String("/* inlined: ") + this->function().name() + String(" */");
     }
 
     std::unique_ptr<Statement> clone() const override {
-        return std::make_unique<InlineMarker>(*fFuncDecl);
+        return std::make_unique<InlineMarker>(&this->function());
     }
 
-    const FunctionDeclaration* fFuncDecl;
+private:
     using INHERITED = Statement;
 };
 
diff --git a/src/sksl/ir/SkSLStatement.h b/src/sksl/ir/SkSLStatement.h
index 03165eb..7cc10a6 100644
--- a/src/sksl/ir/SkSLStatement.h
+++ b/src/sksl/ir/SkSLStatement.h
@@ -49,11 +49,14 @@
         SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
     }
 
+    Statement(int offset, const ForStatementData& data)
+    : INHERITED(offset, (int) Kind::kFor, data) {}
+
     Statement(int offset, const IfStatementData& data)
     : INHERITED(offset, (int) Kind::kIf, data) {}
 
-    Statement(int offset, const ForStatementData& data)
-    : INHERITED(offset, (int) Kind::kFor, data) {}
+    Statement(int offset, const InlineMarkerData& data)
+    : INHERITED(offset, (int) Kind::kInlineMarker, data) {}
 
     Kind kind() const {
         return (Kind) fKind;
diff --git a/src/sksl/ir/SkSLSymbol.h b/src/sksl/ir/SkSLSymbol.h
index 0d2dded..f54e8f9 100644
--- a/src/sksl/ir/SkSLSymbol.h
+++ b/src/sksl/ir/SkSLSymbol.h
@@ -45,6 +45,9 @@
     Symbol(int offset, const SymbolAliasData& data)
     : INHERITED(offset, (int) Kind::kSymbolAlias, data) {}
 
+    Symbol(int offset, const UnresolvedFunctionData& data)
+    : INHERITED(offset, (int) Kind::kUnresolvedFunction, data) {}
+
     Symbol(int offset, const VariableData& data)
     : INHERITED(offset, (int) Kind::kVariable, data) {}
 
diff --git a/src/sksl/ir/SkSLSymbolTable.cpp b/src/sksl/ir/SkSLSymbolTable.cpp
index 7f8f45f..d81e608 100644
--- a/src/sksl/ir/SkSLSymbolTable.cpp
+++ b/src/sksl/ir/SkSLSymbolTable.cpp
@@ -17,7 +17,7 @@
         case Symbol::Kind::kFunctionDeclaration:
             return { &s.as<FunctionDeclaration>() };
         case Symbol::Kind::kUnresolvedFunction:
-            return s.as<UnresolvedFunction>().fFunctions;
+            return s.as<UnresolvedFunction>().functions();
         default:
             return std::vector<const FunctionDeclaration*>();
     }
@@ -103,7 +103,7 @@
         refInSymbolTable = this->takeOwnershipOfSymbol(
                 std::make_unique<UnresolvedFunction>(std::move(functions)));
     } else if (refInSymbolTable->is<UnresolvedFunction>()) {
-        functions = refInSymbolTable->as<UnresolvedFunction>().fFunctions;
+        functions = refInSymbolTable->as<UnresolvedFunction>().functions();
         functions.push_back(&symbol->as<FunctionDeclaration>());
 
         refInSymbolTable = this->takeOwnershipOfSymbol(
diff --git a/src/sksl/ir/SkSLUnresolvedFunction.h b/src/sksl/ir/SkSLUnresolvedFunction.h
index 53c0a38..2e123eb 100644
--- a/src/sksl/ir/SkSLUnresolvedFunction.h
+++ b/src/sksl/ir/SkSLUnresolvedFunction.h
@@ -15,25 +15,32 @@
 /**
  * A symbol representing multiple functions with the same name.
  */
-struct UnresolvedFunction : public Symbol {
+class UnresolvedFunction : public Symbol {
+public:
     static constexpr Kind kSymbolKind = Kind::kUnresolvedFunction;
 
     UnresolvedFunction(std::vector<const FunctionDeclaration*> funcs)
-    : INHERITED(-1, kSymbolKind, funcs[0]->name())
-    , fFunctions(std::move(funcs)) {
+    : INHERITED(-1, UnresolvedFunctionData{std::move(funcs)}) {
 #ifdef SK_DEBUG
-        for (auto func : funcs) {
+        for (auto func : this->functions()) {
             SkASSERT(func->name() == name());
         }
 #endif
     }
 
+    StringFragment name() const override {
+        return this->functions()[0]->name();
+    }
+
+    const std::vector<const FunctionDeclaration*>& functions() const {
+        return this->unresolvedFunctionData().fFunctions;
+    }
+
     String description() const override {
         return this->name();
     }
 
-    const std::vector<const FunctionDeclaration*> fFunctions;
-
+private:
     using INHERITED = Symbol;
 };