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 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 13 | #include "include/private/SkSLSampleUsage.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLDefines.h" |
| 15 | |
| 16 | namespace SkSL { |
| 17 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 18 | class ErrorReporter; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 19 | struct Expression; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 20 | struct FunctionDefinition; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 21 | struct Program; |
| 22 | struct ProgramElement; |
| 23 | struct Statement; |
| 24 | struct Variable; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 25 | struct VariableReference; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Provides utilities for analyzing SkSL statically before it's composed into a full program. |
| 29 | */ |
| 30 | struct Analysis { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 31 | static SampleUsage GetSampleUsage(const Program& program, const Variable& fp); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 32 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 33 | static bool ReferencesBuiltin(const Program& program, int builtin); |
| 34 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 35 | static bool ReferencesSampleCoords(const Program& program); |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 36 | static bool ReferencesFragCoords(const Program& program); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 37 | |
| 38 | static int NodeCount(const FunctionDefinition& function); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 39 | |
| 40 | static bool StatementWritesToVariable(const Statement& stmt, const Variable& var); |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 41 | static bool IsAssignable(Expression& expr, VariableReference** assignableVar, |
| 42 | ErrorReporter* errors = nullptr); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * Utility class to visit every element, statement, and expression in an SkSL program IR. |
| 47 | * This is intended for simple analysis and accumulation, where custom visitation behavior is only |
| 48 | * needed for a limited set of expression kinds. |
| 49 | * |
| 50 | * Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and |
| 51 | * intercept elements of interest. They can then invoke the base class's function to visit all |
| 52 | * sub expressions. They can also choose not to call the base function to arrest recursion, or |
| 53 | * implement custom recursion. |
| 54 | * |
| 55 | * The visit functions return a bool that determines how the default implementation recurses. Once |
| 56 | * any visit call returns true, the default behavior stops recursing and propagates true up the |
| 57 | * stack. |
| 58 | */ |
| 59 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 60 | template <typename PROG, typename EXPR, typename STMT, typename ELEM> |
| 61 | class TProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 62 | public: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 63 | virtual ~TProgramVisitor() = default; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 64 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 65 | bool visit(PROG program); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 66 | |
| 67 | protected: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 68 | virtual bool visitExpression(EXPR expression); |
| 69 | virtual bool visitStatement(STMT statement); |
| 70 | virtual bool visitProgramElement(ELEM programElement); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 73 | // Squelch bogus Clang warning about template vtables: https://bugs.llvm.org/show_bug.cgi?id=18733 |
| 74 | #if defined(__clang__) |
| 75 | #pragma clang diagnostic push |
| 76 | #pragma clang diagnostic ignored "-Wweak-template-vtables" |
| 77 | #endif |
| 78 | extern template class TProgramVisitor<const Program&, const Expression&, |
| 79 | const Statement&, const ProgramElement&>; |
| 80 | extern template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 81 | #if defined(__clang__) |
| 82 | #pragma clang diagnostic pop |
| 83 | #endif |
| 84 | |
| 85 | using ProgramVisitor = TProgramVisitor<const Program&, const Expression&, |
| 86 | const Statement&, const ProgramElement&>; |
| 87 | using ProgramWriter = TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>; |
| 88 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 89 | } // namespace SkSL |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 90 | |
| 91 | #endif |