blob: 8e977b444944cebaa35558eecb7a107505baff0e [file] [log] [blame]
Michael Ludwig8f3a8362020-06-29 17:27:00 -04001/*
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 Osman1298bc42020-06-30 13:39:35 -040011#include "include/private/SkSLSampleUsage.h"
Michael Ludwig8f3a8362020-06-29 17:27:00 -040012#include "src/sksl/SkSLDefines.h"
13
Brian Osman010ce6a2020-10-19 16:34:10 -040014#include <memory>
15
Michael Ludwig8f3a8362020-06-29 17:27:00 -040016namespace SkSL {
17
John Stilesdce4d3e2020-09-25 14:35:13 -040018class ErrorReporter;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040019class Expression;
Brian Osman2e25ff42020-10-15 10:32:04 -040020class FunctionDeclaration;
Ethan Nicholas6f87de72020-10-26 15:06:46 -040021class FunctionDefinition;
Michael Ludwig8f3a8362020-06-29 17:27:00 -040022struct Program;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040023class ProgramElement;
Brian Osman010ce6a2020-10-19 16:34:10 -040024class ProgramUsage;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040025class Statement;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040026class Variable;
Ethan Nicholas78686922020-10-08 06:46:27 -040027class VariableReference;
Michael Ludwig8f3a8362020-06-29 17:27:00 -040028
29/**
30 * Provides utilities for analyzing SkSL statically before it's composed into a full program.
31 */
32struct Analysis {
Brian Osman1298bc42020-06-30 13:39:35 -040033 static SampleUsage GetSampleUsage(const Program& program, const Variable& fp);
Michael Ludwig8f3a8362020-06-29 17:27:00 -040034
Brian Osman92aac1e2020-08-05 16:48:58 -040035 static bool ReferencesBuiltin(const Program& program, int builtin);
36
Michael Ludwig8f3a8362020-06-29 17:27:00 -040037 static bool ReferencesSampleCoords(const Program& program);
Brian Osman92aac1e2020-08-05 16:48:58 -040038 static bool ReferencesFragCoords(const Program& program);
Ethan Nicholas6e0fa402020-08-20 14:08:23 -040039
John Stiles2c1e4922020-10-01 09:14:14 -040040 static bool NodeCountExceeds(const FunctionDefinition& function, int limit);
Ethan Nicholas765d2fe2020-08-26 08:29:55 -040041
Brian Osman010ce6a2020-10-19 16:34:10 -040042 static std::unique_ptr<ProgramUsage> GetUsage(const Program& program);
Brian Osman2e25ff42020-10-15 10:32:04 -040043
Ethan Nicholas765d2fe2020-08-26 08:29:55 -040044 static bool StatementWritesToVariable(const Statement& stmt, const Variable& var);
John Stilesa976da72020-09-25 23:06:26 -040045 static bool IsAssignable(Expression& expr, VariableReference** assignableVar,
46 ErrorReporter* errors = nullptr);
Michael Ludwig8f3a8362020-06-29 17:27:00 -040047};
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 Stiles70b82422020-09-30 10:55:12 -040064template <typename PROG, typename EXPR, typename STMT, typename ELEM>
65class TProgramVisitor {
Michael Ludwig8f3a8362020-06-29 17:27:00 -040066public:
John Stiles70b82422020-09-30 10:55:12 -040067 virtual ~TProgramVisitor() = default;
Michael Ludwig8f3a8362020-06-29 17:27:00 -040068
John Stiles70b82422020-09-30 10:55:12 -040069 bool visit(PROG program);
Michael Ludwig8f3a8362020-06-29 17:27:00 -040070
71protected:
John Stiles70b82422020-09-30 10:55:12 -040072 virtual bool visitExpression(EXPR expression);
73 virtual bool visitStatement(STMT statement);
74 virtual bool visitProgramElement(ELEM programElement);
Michael Ludwig8f3a8362020-06-29 17:27:00 -040075};
76
John Stiles70b82422020-09-30 10:55:12 -040077// 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
82extern template class TProgramVisitor<const Program&, const Expression&,
83 const Statement&, const ProgramElement&>;
84extern template class TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>;
85#if defined(__clang__)
86#pragma clang diagnostic pop
87#endif
88
89using ProgramVisitor = TProgramVisitor<const Program&, const Expression&,
90 const Statement&, const ProgramElement&>;
91using ProgramWriter = TProgramVisitor<Program&, Expression&, Statement&, ProgramElement&>;
92
John Stilesa6841be2020-08-06 14:11:56 -040093} // namespace SkSL
Michael Ludwig8f3a8362020-06-29 17:27:00 -040094
95#endif