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
9QT_BEGIN_NAMESPACE
10
11using namespace GLSL;
12
13DiagnosticMessage::DiagnosticMessage()
14 : _kind(Error), _line(0)
15{
16}
17
18DiagnosticMessage::Kind DiagnosticMessage::kind() const
19{
20 return _kind;
21}
22
23void DiagnosticMessage::setKind(Kind kind)
24{
25 _kind = kind;
26}
27
28QString DiagnosticMessage::fileName() const
29{
30 return _fileName;
31}
32
33void DiagnosticMessage::setFileName(const QString &fileName)
34{
35 _fileName = fileName;
36}
37
38int DiagnosticMessage::line() const
39{
40 return _line;
41}
42
43void DiagnosticMessage::setLine(int line)
44{
45 _line = line;
46}
47
48QString DiagnosticMessage::message() const
49{
50 return _message;
51}
52
53void DiagnosticMessage::setMessage(const QString &message)
54{
55 _message = message;
56}
57
58Engine::Engine()
59 : _blockDiagnosticMessages(false)
60{
61}
62
63Engine::~Engine()
64{
65 qDeleteAll(c: _symbols);
66}
67
68const QString *Engine::identifier(const QString &s)
69{
70 return &(*_identifiers.insert(x: s).first);
71}
72
73const QString *Engine::identifier(const char *s, int n)
74{
75 return &(*_identifiers.insert(x: QString::fromLatin1(str: s, size: n)).first);
76}
77
78std::unordered_set<QString> Engine::identifiers() const
79{
80 return _identifiers;
81}
82
83const QString *Engine::number(const QString &s)
84{
85 return &(*_numbers.insert(x: s).first);
86}
87
88const QString *Engine::number(const char *s, int n)
89{
90 return &(*_numbers.insert(x: QString::fromLatin1(str: s, size: n)).first);
91}
92
93std::unordered_set<QString> Engine::numbers() const
94{
95 return _numbers;
96}
97
98MemoryPool *Engine::pool()
99{
100 return &_pool;
101}
102
103const UndefinedType *Engine::undefinedType()
104{
105 static UndefinedType t;
106 return &t;
107}
108
109const VoidType *Engine::voidType()
110{
111 static VoidType t;
112 return &t;
113}
114
115const BoolType *Engine::boolType()
116{
117 static BoolType t;
118 return &t;
119}
120
121const IntType *Engine::intType()
122{
123 static IntType t;
124 return &t;
125}
126
127const UIntType *Engine::uintType()
128{
129 static UIntType t;
130 return &t;
131}
132
133const FloatType *Engine::floatType()
134{
135 static FloatType t;
136 return &t;
137}
138
139const DoubleType *Engine::doubleType()
140{
141 static DoubleType t;
142 return &t;
143}
144
145const SamplerType *Engine::samplerType(int kind)
146{
147 return _samplerTypes.intern(ty: SamplerType(kind));
148}
149
150const 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
158const 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
164const ArrayType *Engine::arrayType(const Type *elementType)
165{
166 return _arrayTypes.intern(ty: ArrayType(elementType));
167}
168
169
170QList<DiagnosticMessage> Engine::diagnosticMessages() const
171{
172 return _diagnosticMessages;
173}
174
175void Engine::clearDiagnosticMessages()
176{
177 _diagnosticMessages.clear();
178}
179
180void Engine::addDiagnosticMessage(const DiagnosticMessage &m)
181{
182 if (! _blockDiagnosticMessages)
183 _diagnosticMessages.append(t: m);
184}
185
186void 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
195void 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
204bool DiagnosticMessage::isError() const
205{
206 return _kind == Error;
207}
208
209bool DiagnosticMessage::isWarning() const
210{
211 return _kind == Warning;
212}
213
214Namespace *Engine::newNamespace()
215{
216 Namespace *s = new Namespace();
217 _symbols.append(t: s);
218 return s;
219}
220
221Struct *Engine::newStruct(Scope *scope)
222{
223 Struct *s = new Struct(scope);
224 _symbols.append(t: s);
225 return s;
226}
227
228Block *Engine::newBlock(Scope *scope)
229{
230 Block *s = new Block(scope);
231 _symbols.append(t: s);
232 return s;
233}
234
235Function *Engine::newFunction(Scope *scope)
236{
237 Function *s = new Function(scope);
238 _symbols.append(t: s);
239 return s;
240}
241
242Argument *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
251Variable *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
261bool Engine::blockDiagnosticMessages(bool block)
262{
263 bool previous = _blockDiagnosticMessages;
264 _blockDiagnosticMessages = block;
265 return previous;
266}
267
268QT_END_NAMESPACE
269
270

source code of qtquick3d/src/glslparser/glslengine.cpp