Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLProgramVisitor.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkSLProgramVisitor_DEFINED
9#define SkSLProgramVisitor_DEFINED
10
11#include <memory>
12
13namespace SkSL {
14
15struct Program;
16class Expression;
17class Statement;
18class ProgramElement;
19
20/**
21 * Utility class to visit every element, statement, and expression in an SkSL program IR.
22 * This is intended for simple analysis and accumulation, where custom visitation behavior is only
23 * needed for a limited set of expression kinds.
24 *
25 * Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and
26 * intercept elements of interest. They can then invoke the base class's function to visit all
27 * sub expressions. They can also choose not to call the base function to arrest recursion, or
28 * implement custom recursion.
29 *
30 * The visit functions return a bool that determines how the default implementation recurses. Once
31 * any visit call returns true, the default behavior stops recursing and propagates true up the
32 * stack.
33 */
34template <typename T>
36public:
37 virtual ~TProgramVisitor() = default;
38
39protected:
40 virtual bool visitExpression(typename T::Expression& expression);
41 virtual bool visitStatement(typename T::Statement& statement);
42 virtual bool visitProgramElement(typename T::ProgramElement& programElement);
43
44 virtual bool visitExpressionPtr(typename T::UniquePtrExpression& expr) = 0;
45 virtual bool visitStatementPtr(typename T::UniquePtrStatement& stmt) = 0;
46};
47
48// ProgramVisitors take const types; ProgramWriters do not.
50 using Program = const SkSL::Program;
54 using UniquePtrExpression = const std::unique_ptr<SkSL::Expression>;
55 using UniquePtrStatement = const std::unique_ptr<SkSL::Statement>;
56};
57
58extern template class TProgramVisitor<ProgramVisitorTypes>;
59
61public:
62 bool visit(const Program& program);
63
64private:
65 // ProgramVisitors shouldn't need access to unique_ptrs, and marking these as final should help
66 // these accessors inline away. Use ProgramWriter if you need the unique_ptrs.
67 bool visitExpressionPtr(const std::unique_ptr<Expression>& e) final {
68 return this->visitExpression(*e);
69 }
70 bool visitStatementPtr(const std::unique_ptr<Statement>& s) final {
71 return this->visitStatement(*s);
72 }
73};
74
75} // namespace SkSL
76
77#endif
bool visitExpressionPtr(const std::unique_ptr< Expression > &e) final
bool visitStatementPtr(const std::unique_ptr< Statement > &s) final
virtual bool visitStatementPtr(typename T::UniquePtrStatement &stmt)=0
virtual bool visitStatement(typename T::Statement &statement)
virtual ~TProgramVisitor()=default
virtual bool visitExpressionPtr(typename T::UniquePtrExpression &expr)=0
virtual bool visitExpression(typename T::Expression &expression)
virtual bool visitProgramElement(typename T::ProgramElement &programElement)
struct MyStruct s
const std::unique_ptr< SkSL::Statement > UniquePtrStatement
const std::unique_ptr< SkSL::Expression > UniquePtrExpression