blob: 3fb8612bbf55f434cff382a80e54be5ed32b1108 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
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 SKSL_DSLWRITER
9#define SKSL_DSLWRITER
10
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050011#include "src/sksl/SkSLMangler.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050012#include "src/sksl/dsl/DSLExpression.h"
13#include "src/sksl/ir/SkSLExpressionStatement.h"
14#include "src/sksl/ir/SkSLProgram.h"
15#include "src/sksl/ir/SkSLStatement.h"
16#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
17#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
19
20#include <stack>
21
22class AutoDSLContext;
23
24namespace SkSL {
25
26class Compiler;
27class Context;
28class IRGenerator;
29class SymbolTable;
30class Type;
31
32namespace dsl {
33
34class ErrorHandler;
35
36/**
37 * Thread-safe class that tracks per-thread state associated with DSL output. This class is for
38 * internal use only.
39 */
40class DSLWriter {
41public:
42 DSLWriter(SkSL::Compiler* compiler);
43
44 /**
45 * Returns the Compiler used by DSL operations in the current thread.
46 */
47 static SkSL::Compiler& Compiler() {
48 return *Instance().fCompiler;
49 }
50
51 /**
52 * Returns the IRGenerator used by DSL operations in the current thread.
53 */
54 static SkSL::IRGenerator& IRGenerator();
55
56 /**
57 * Returns the Context used by DSL operations in the current thread.
58 */
59 static const SkSL::Context& Context();
60
61 /**
62 * Returns the SymbolTable of the current thread's IRGenerator.
63 */
64 static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
65
66 /**
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050067 * Returns the final pointer to a pooled Modifiers object that should be used to represent the
68 * given modifiers.
69 */
70 static const SkSL::Modifiers* Modifiers(SkSL::Modifiers modifiers);
71
72 /**
73 * Returns the (possibly mangled) final name that should be used for an entity with the given
74 * raw name.
75 */
76 static const char* Name(const char* name);
77
78 /**
Ethan Nicholas95046142021-01-07 10:57:27 -050079 * Reports an error if the argument is null. Returns its argument unmodified.
80 */
81 static std::unique_ptr<SkSL::Expression> Check(std::unique_ptr<SkSL::Expression> expr);
82
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050083 static DSLExpression Construct(const SkSL::Type& type, std::vector<DSLExpression> rawArgs);
84
Ethan Nicholas95046142021-01-07 10:57:27 -050085 /**
86 * Sets the ErrorHandler associated with the current thread. This object will be notified when
87 * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to
88 * stderr and a fatal exception will be generated.
89 */
90 static void SetErrorHandler(ErrorHandler* errorHandler) {
91 Instance().fErrorHandler = errorHandler;
92 }
93
94 /**
95 * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler
96 * (the default), any errors will be dumped to stderr and a fatal exception will be generated.
97 */
98 static void ReportError(const char* msg);
99
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500100 /**
101 * Returns whether name mangling is enabled. This should always be enabled outside of tests.
102 */
103 static bool ManglingEnabled() {
104 return Instance().fMangle;
105 }
106
Ethan Nicholas95046142021-01-07 10:57:27 -0500107 static DSLWriter& Instance();
108
109 static void SetInstance(std::unique_ptr<DSLWriter> instance);
110
111private:
112 SkSL::Program::Settings fSettings;
113 SkSL::Compiler* fCompiler;
114 ErrorHandler* fErrorHandler = nullptr;
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500115 bool fMangle = true;
116 Mangler fMangler;
Ethan Nicholas95046142021-01-07 10:57:27 -0500117
118 friend class DSLCore;
119 friend class ::AutoDSLContext;
120};
121
122} // namespace dsl
123
124} // namespace SkSL
125
126#endif