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
11#include "include/private/SkSLSampleUsage.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 SampleUsage GetSampleUsage(const Program& program, const Variable& fp);
27
28 static bool ReferencesBuiltin(const Program& program, int builtin);
29
30 static bool ReferencesSampleCoords(const Program& program);
31 static bool ReferencesFragCoords(const Program& program);
32};
33
34/**
35 * Utility class to visit every element, statement, and expression in an SkSL program IR.
36 * This is intended for simple analysis and accumulation, where custom visitation behavior is only
37 * needed for a limited set of expression kinds.
38 *
39 * Subclasses should override visitExpression/visitStatement/visitProgramElement as needed and
40 * intercept elements of interest. They can then invoke the base class's function to visit all
41 * sub expressions. They can also choose not to call the base function to arrest recursion, or
42 * implement custom recursion.
43 *
44 * The visit functions return a bool that determines how the default implementation recurses. Once
45 * any visit call returns true, the default behavior stops recursing and propagates true up the
46 * stack.
47 */
48
49class ProgramVisitor {
50public:
51 virtual ~ProgramVisitor() { SkASSERT(!fProgram); }
52
53 bool visit(const Program&);
54
55protected:
56 const Program& program() const {
57 SkASSERT(fProgram);
58 return *fProgram;
59 }
60
61 virtual bool visitExpression(const Expression&);
62 virtual bool visitStatement(const Statement&);
63 virtual bool visitProgramElement(const ProgramElement&);
64
65private:
66 const Program* fProgram;
67};
68
69} // namespace SkSL
70
71#endif
72