1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "glslengine_p.h" |
5 | #include "glslsymbols_p.h" |
6 | #include "glsltypes_p.h" |
7 | #include "glslparser_p.h" |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | using namespace GLSL; |
12 | |
13 | DiagnosticMessage::DiagnosticMessage() |
14 | : _kind(Error), _line(0) |
15 | { |
16 | } |
17 | |
18 | DiagnosticMessage::Kind DiagnosticMessage::kind() const |
19 | { |
20 | return _kind; |
21 | } |
22 | |
23 | void DiagnosticMessage::setKind(Kind kind) |
24 | { |
25 | _kind = kind; |
26 | } |
27 | |
28 | QString DiagnosticMessage::fileName() const |
29 | { |
30 | return _fileName; |
31 | } |
32 | |
33 | void DiagnosticMessage::setFileName(const QString &fileName) |
34 | { |
35 | _fileName = fileName; |
36 | } |
37 | |
38 | int DiagnosticMessage::line() const |
39 | { |
40 | return _line; |
41 | } |
42 | |
43 | void DiagnosticMessage::setLine(int line) |
44 | { |
45 | _line = line; |
46 | } |
47 | |
48 | QString DiagnosticMessage::message() const |
49 | { |
50 | return _message; |
51 | } |
52 | |
53 | void DiagnosticMessage::setMessage(const QString &message) |
54 | { |
55 | _message = message; |
56 | } |
57 | |
58 | Engine::Engine() |
59 | : _blockDiagnosticMessages(false) |
60 | { |
61 | } |
62 | |
63 | Engine::~Engine() |
64 | { |
65 | qDeleteAll(c: _symbols); |
66 | } |
67 | |
68 | const QString *Engine::identifier(const QString &s) |
69 | { |
70 | return &(*_identifiers.insert(x: s).first); |
71 | } |
72 | |
73 | const QString *Engine::identifier(const char *s, int n) |
74 | { |
75 | return &(*_identifiers.insert(x: QString::fromLatin1(str: s, size: n)).first); |
76 | } |
77 | |
78 | std::unordered_set<QString> Engine::identifiers() const |
79 | { |
80 | return _identifiers; |
81 | } |
82 | |
83 | const QString *Engine::number(const QString &s) |
84 | { |
85 | return &(*_numbers.insert(x: s).first); |
86 | } |
87 | |
88 | const QString *Engine::number(const char *s, int n) |
89 | { |
90 | return &(*_numbers.insert(x: QString::fromLatin1(str: s, size: n)).first); |
91 | } |
92 | |
93 | std::unordered_set<QString> Engine::numbers() const |
94 | { |
95 | return _numbers; |
96 | } |
97 | |
98 | MemoryPool *Engine::pool() |
99 | { |
100 | return &_pool; |
101 | } |
102 | |
103 | const UndefinedType *Engine::undefinedType() |
104 | { |
105 | static UndefinedType t; |
106 | return &t; |
107 | } |
108 | |
109 | const VoidType *Engine::voidType() |
110 | { |
111 | static VoidType t; |
112 | return &t; |
113 | } |
114 | |
115 | const BoolType *Engine::boolType() |
116 | { |
117 | static BoolType t; |
118 | return &t; |
119 | } |
120 | |
121 | const IntType *Engine::intType() |
122 | { |
123 | static IntType t; |
124 | return &t; |
125 | } |
126 | |
127 | const UIntType *Engine::uintType() |
128 | { |
129 | static UIntType t; |
130 | return &t; |
131 | } |
132 | |
133 | const FloatType *Engine::floatType() |
134 | { |
135 | static FloatType t; |
136 | return &t; |
137 | } |
138 | |
139 | const DoubleType *Engine::doubleType() |
140 | { |
141 | static DoubleType t; |
142 | return &t; |
143 | } |
144 | |
145 | const SamplerType *Engine::samplerType(int kind) |
146 | { |
147 | return _samplerTypes.intern(ty: SamplerType(kind)); |
148 | } |
149 | |
150 | const VectorType *Engine::vectorType(const Type *elementType, int dimension) |
151 | { |
152 | VectorType *type = const_cast<VectorType *> |
153 | (_vectorTypes.intern(ty: VectorType(elementType, dimension))); |
154 | type->populateMembers(engine: this); |
155 | return type; |
156 | } |
157 | |
158 | const MatrixType *Engine::matrixType(const Type *elementType, int columns, int rows) |
159 | { |
160 | return _matrixTypes.intern(ty: MatrixType(elementType, columns, rows, |
161 | vectorType(elementType, dimension: rows))); |
162 | } |
163 | |
164 | const ArrayType *Engine::arrayType(const Type *elementType) |
165 | { |
166 | return _arrayTypes.intern(ty: ArrayType(elementType)); |
167 | } |
168 | |
169 | |
170 | QList<DiagnosticMessage> Engine::diagnosticMessages() const |
171 | { |
172 | return _diagnosticMessages; |
173 | } |
174 | |
175 | void Engine::clearDiagnosticMessages() |
176 | { |
177 | _diagnosticMessages.clear(); |
178 | } |
179 | |
180 | void Engine::addDiagnosticMessage(const DiagnosticMessage &m) |
181 | { |
182 | if (! _blockDiagnosticMessages) |
183 | _diagnosticMessages.append(t: m); |
184 | } |
185 | |
186 | void Engine::warning(int line, const QString &message) |
187 | { |
188 | DiagnosticMessage m; |
189 | m.setKind(DiagnosticMessage::Warning); |
190 | m.setLine(line); |
191 | m.setMessage(message); |
192 | addDiagnosticMessage(m); |
193 | } |
194 | |
195 | void Engine::error(int line, const QString &message) |
196 | { |
197 | DiagnosticMessage m; |
198 | m.setKind(DiagnosticMessage::Error); |
199 | m.setLine(line); |
200 | m.setMessage(message); |
201 | addDiagnosticMessage(m); |
202 | } |
203 | |
204 | bool DiagnosticMessage::isError() const |
205 | { |
206 | return _kind == Error; |
207 | } |
208 | |
209 | bool DiagnosticMessage::isWarning() const |
210 | { |
211 | return _kind == Warning; |
212 | } |
213 | |
214 | Namespace *Engine::newNamespace() |
215 | { |
216 | Namespace *s = new Namespace(); |
217 | _symbols.append(t: s); |
218 | return s; |
219 | } |
220 | |
221 | Struct *Engine::newStruct(Scope *scope) |
222 | { |
223 | Struct *s = new Struct(scope); |
224 | _symbols.append(t: s); |
225 | return s; |
226 | } |
227 | |
228 | Block *Engine::newBlock(Scope *scope) |
229 | { |
230 | Block *s = new Block(scope); |
231 | _symbols.append(t: s); |
232 | return s; |
233 | } |
234 | |
235 | Function *Engine::newFunction(Scope *scope) |
236 | { |
237 | Function *s = new Function(scope); |
238 | _symbols.append(t: s); |
239 | return s; |
240 | } |
241 | |
242 | Argument *Engine::newArgument(Function *function, const QString &name, const Type *type) |
243 | { |
244 | Argument *a = new Argument(function); |
245 | a->setName(name); |
246 | a->setType(type); |
247 | _symbols.append(t: a); |
248 | return a; |
249 | } |
250 | |
251 | Variable *Engine::newVariable(Scope *scope, const QString &name, const Type *type, int qualifiers) |
252 | { |
253 | Variable *var = new Variable(scope); |
254 | var->setName(name); |
255 | var->setType(type); |
256 | var->setQualifiers(qualifiers); |
257 | _symbols.append(t: var); |
258 | return var; |
259 | } |
260 | |
261 | bool Engine::blockDiagnosticMessages(bool block) |
262 | { |
263 | bool previous = _blockDiagnosticMessages; |
264 | _blockDiagnosticMessages = block; |
265 | return previous; |
266 | } |
267 | |
268 | QT_END_NAMESPACE |
269 | |
270 | |