Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 SkSLAnalysis_DEFINED |
| 9 | #define SkSLAnalysis_DEFINED |
| 10 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame^] | 11 | #include "include/private/SkSLSampleUsage.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLDefines.h" |
| 13 | |
| 14 | namespace SkSL { |
| 15 | |
| 16 | struct Expression; |
| 17 | struct Program; |
| 18 | struct ProgramElement; |
| 19 | struct Statement; |
| 20 | struct Variable; |
| 21 | |
| 22 | /** |
| 23 | * Provides utilities for analyzing SkSL statically before it's composed into a full program. |
| 24 | */ |
| 25 | struct Analysis { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame^] | 26 | static SampleUsage GetSampleUsage(const Program& program, const Variable& fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 27 | |
| 28 | static bool ReferencesSampleCoords(const Program& program); |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * Utility class to visit every element, statement, and expression in an SkSL program IR. |
| 33 | * This is intended for simple analysis and accumulation, where custom visitation behavior is only |
| 34 | * needed for a limited set of expression kinds. |
| 35 | * |
| 36 | * Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and |
| 37 | * intercept elements of interest. They can then invoke the base class's function to visit all |
| 38 | * sub expressions. They can also choose not to call the base function to arrest recursion, or |
| 39 | * implement custom recursion. |
| 40 | * |
| 41 | * The visit functions return a bool that determines how the default implementation recurses. Once |
| 42 | * any visit call returns true, the default behavior stops recursing and propagates true up the |
| 43 | * stack. |
| 44 | */ |
| 45 | |
| 46 | class ProgramVisitor { |
| 47 | public: |
| 48 | virtual ~ProgramVisitor() { SkASSERT(!fProgram); } |
| 49 | |
| 50 | bool visit(const Program&); |
| 51 | |
| 52 | protected: |
| 53 | const Program& program() const { |
| 54 | SkASSERT(fProgram); |
| 55 | return *fProgram; |
| 56 | } |
| 57 | |
| 58 | virtual bool visitExpression(const Expression&); |
| 59 | virtual bool visitStatement(const Statement&); |
| 60 | virtual bool visitProgramElement(const ProgramElement&); |
| 61 | |
| 62 | private: |
| 63 | const Program* fProgram; |
| 64 | }; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | #endif |