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