blob: f8b346061d326bce594275bbc88cad98340d9b8a [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
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 {
Brian Osman1298bc42020-06-30 13:39:35 -040026 static SampleUsage GetSampleUsage(const Program& program, const Variable& fp);
Michael Ludwig8f3a8362020-06-29 17:27:00 -040027
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
46class ProgramVisitor {
47public:
48 virtual ~ProgramVisitor() { SkASSERT(!fProgram); }
49
50 bool visit(const Program&);
51
52protected:
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
62private:
63 const Program* fProgram;
64};
65
66}
67
68#endif