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