Broke ProgramVisitor and ProgramWriter out into separate files

Change-Id: I46d3718431539100f6787674910ba10f40865e1f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/454857
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2021-10-01 09:42:36 -04:00 committed by SkCQ
parent e8566129ba
commit 178075524b
8 changed files with 139 additions and 88 deletions

View File

@ -16,8 +16,10 @@
#include "src/core/SkSafeMath.h"
#include "src/sksl/SkSLCompiler.h"
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/analysis/SkSLProgramVisitor.h"
#include "src/sksl/ir/SkSLExpression.h"
#include "src/sksl/ir/SkSLProgram.h"
#include "src/sksl/transform/SkSLProgramWriter.h"
// ProgramElements
#include "src/sksl/ir/SkSLExtension.h"

View File

@ -184,91 +184,6 @@ struct Analysis {
ProgramUsage* usage = nullptr);
};
/**
* Utility class to visit every element, statement, and expression in an SkSL program IR.
* This is intended for simple analysis and accumulation, where custom visitation behavior is only
* needed for a limited set of expression kinds.
*
* Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and
* intercept elements of interest. They can then invoke the base class's function to visit all
* sub expressions. They can also choose not to call the base function to arrest recursion, or
* implement custom recursion.
*
* The visit functions return a bool that determines how the default implementation recurses. Once
* any visit call returns true, the default behavior stops recursing and propagates true up the
* stack.
*/
template <typename T>
class TProgramVisitor {
public:
virtual ~TProgramVisitor() = default;
protected:
virtual bool visitExpression(typename T::Expression& expression);
virtual bool visitStatement(typename T::Statement& statement);
virtual bool visitProgramElement(typename T::ProgramElement& programElement);
virtual bool visitExpressionPtr(typename T::UniquePtrExpression& expr) = 0;
virtual bool visitStatementPtr(typename T::UniquePtrStatement& stmt) = 0;
};
// ProgramVisitors take const types; ProgramWriters do not.
struct ProgramVisitorTypes {
using Program = const SkSL::Program;
using Expression = const SkSL::Expression;
using Statement = const SkSL::Statement;
using ProgramElement = const SkSL::ProgramElement;
using UniquePtrExpression = const std::unique_ptr<SkSL::Expression>;
using UniquePtrStatement = const std::unique_ptr<SkSL::Statement>;
};
struct ProgramWriterTypes {
using Program = SkSL::Program;
using Expression = SkSL::Expression;
using Statement = SkSL::Statement;
using ProgramElement = SkSL::ProgramElement;
using UniquePtrExpression = std::unique_ptr<SkSL::Expression>;
using UniquePtrStatement = std::unique_ptr<SkSL::Statement>;
};
// Squelch bogus Clang warning about template vtables: https://bugs.llvm.org/show_bug.cgi?id=18733
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-template-vtables"
#endif
extern template class TProgramVisitor<ProgramVisitorTypes>;
extern template class TProgramVisitor<ProgramWriterTypes>;
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
class ProgramVisitor : public TProgramVisitor<ProgramVisitorTypes> {
public:
bool visit(const Program& program);
private:
// ProgramVisitors shouldn't need access to unique_ptrs, and marking these as final should help
// these accessors inline away. Use ProgramWriter if you need the unique_ptrs.
bool visitExpressionPtr(const std::unique_ptr<Expression>& e) final {
return this->visitExpression(*e);
}
bool visitStatementPtr(const std::unique_ptr<Statement>& s) final {
return this->visitStatement(*s);
}
};
class ProgramWriter : public TProgramVisitor<ProgramWriterTypes> {
public:
// Subclass these methods if you want access to the unique_ptrs of IRNodes in a program.
// This will allow statements or expressions to be replaced during a visit.
bool visitExpressionPtr(std::unique_ptr<Expression>& e) override {
return this->visitExpression(*e);
}
bool visitStatementPtr(std::unique_ptr<Statement>& s) override {
return this->visitStatement(*s);
}
};
} // namespace SkSL
#endif

View File

@ -12,7 +12,6 @@
#include "include/sksl/DSLCore.h"
#include "src/core/SkTraceEvent.h"
#include "src/sksl/SkSLAnalysis.h"
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/SkSLDSLParser.h"
#include "src/sksl/SkSLIRGenerator.h"
@ -36,6 +35,7 @@
#include "src/sksl/ir/SkSLTernaryExpression.h"
#include "src/sksl/ir/SkSLUnresolvedFunction.h"
#include "src/sksl/ir/SkSLVarDeclarations.h"
#include "src/sksl/transform/SkSLProgramWriter.h"
#include "src/utils/SkBitSet.h"
#include <fstream>

View File

@ -12,7 +12,7 @@
#include <unordered_set>
#include "include/private/SkSLLayout.h"
#include "src/sksl/SkSLAnalysis.h"
#include "src/sksl/analysis/SkSLProgramVisitor.h"
#include "src/sksl/ir/SkSLBinaryExpression.h"
#include "src/sksl/ir/SkSLBreakStatement.h"
#include "src/sksl/ir/SkSLChildCall.h"

View File

@ -0,0 +1,85 @@
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSLProgramVisitor_DEFINED
#define SkSLProgramVisitor_DEFINED
#include <memory>
namespace SkSL {
struct Program;
class Expression;
class Statement;
class ProgramElement;
/**
* Utility class to visit every element, statement, and expression in an SkSL program IR.
* This is intended for simple analysis and accumulation, where custom visitation behavior is only
* needed for a limited set of expression kinds.
*
* Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and
* intercept elements of interest. They can then invoke the base class's function to visit all
* sub expressions. They can also choose not to call the base function to arrest recursion, or
* implement custom recursion.
*
* The visit functions return a bool that determines how the default implementation recurses. Once
* any visit call returns true, the default behavior stops recursing and propagates true up the
* stack.
*/
template <typename T>
class TProgramVisitor {
public:
virtual ~TProgramVisitor() = default;
protected:
virtual bool visitExpression(typename T::Expression& expression);
virtual bool visitStatement(typename T::Statement& statement);
virtual bool visitProgramElement(typename T::ProgramElement& programElement);
virtual bool visitExpressionPtr(typename T::UniquePtrExpression& expr) = 0;
virtual bool visitStatementPtr(typename T::UniquePtrStatement& stmt) = 0;
};
// ProgramVisitors take const types; ProgramWriters do not.
struct ProgramVisitorTypes {
using Program = const SkSL::Program;
using Expression = const SkSL::Expression;
using Statement = const SkSL::Statement;
using ProgramElement = const SkSL::ProgramElement;
using UniquePtrExpression = const std::unique_ptr<SkSL::Expression>;
using UniquePtrStatement = const std::unique_ptr<SkSL::Statement>;
};
// Squelch bogus Clang warning about template vtables: https://bugs.llvm.org/show_bug.cgi?id=18733
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-template-vtables"
#endif
extern template class TProgramVisitor<ProgramVisitorTypes>;
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
class ProgramVisitor : public TProgramVisitor<ProgramVisitorTypes> {
public:
bool visit(const Program& program);
private:
// ProgramVisitors shouldn't need access to unique_ptrs, and marking these as final should help
// these accessors inline away. Use ProgramWriter if you need the unique_ptrs.
bool visitExpressionPtr(const std::unique_ptr<Expression>& e) final {
return this->visitExpression(*e);
}
bool visitStatementPtr(const std::unique_ptr<Statement>& s) final {
return this->visitStatement(*s);
}
};
} // namespace SkSL
#endif

View File

@ -11,6 +11,7 @@
#include "src/sksl/ir/SkSLFunctionCall.h"
#include "src/sksl/ir/SkSLFunctionDefinition.h"
#include "src/sksl/ir/SkSLReturnStatement.h"
#include "src/sksl/transform/SkSLProgramWriter.h"
#include <forward_list>

View File

@ -7,10 +7,10 @@
#include "src/sksl/transform/SkSLTransform.h"
#include "src/sksl/SkSLAnalysis.h"
#include "src/sksl/SkSLCompiler.h"
#include "src/sksl/SkSLContext.h"
#include "src/sksl/SkSLIntrinsicMap.h"
#include "src/sksl/analysis/SkSLProgramVisitor.h"
#include "src/sksl/dsl/priv/DSLWriter.h"
#include "src/sksl/ir/SkSLFunctionDefinition.h"
#include "src/sksl/ir/SkSLInterfaceBlock.h"

View File

@ -0,0 +1,48 @@
/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSLProgramWriter_DEFINED
#define SkSLProgramWriter_DEFINED
#include "src/sksl/analysis/SkSLProgramVisitor.h"
namespace SkSL {
struct ProgramWriterTypes {
using Program = SkSL::Program;
using Expression = SkSL::Expression;
using Statement = SkSL::Statement;
using ProgramElement = SkSL::ProgramElement;
using UniquePtrExpression = std::unique_ptr<SkSL::Expression>;
using UniquePtrStatement = std::unique_ptr<SkSL::Statement>;
};
// Squelch bogus Clang warning about template vtables: https://bugs.llvm.org/show_bug.cgi?id=18733
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wweak-template-vtables"
#endif
extern template class TProgramVisitor<ProgramWriterTypes>;
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
class ProgramWriter : public TProgramVisitor<ProgramWriterTypes> {
public:
// Subclass these methods if you want access to the unique_ptrs of IRNodes in a program.
// This will allow statements or expressions to be replaced during a visit.
bool visitExpressionPtr(std::unique_ptr<Expression>& e) override {
return this->visitExpression(*e);
}
bool visitStatementPtr(std::unique_ptr<Statement>& s) override {
return this->visitStatement(*s);
}
};
} // namespace SkSL
#endif