1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QSSG_GLSLTYPES_H
5#define QSSG_GLSLTYPES_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick3DGlslParser/private/glsltype_p.h>
19#include <QtQuick3DGlslParser/private/glslsymbol_p.h>
20#include <QVector>
21#include <QHash>
22#include <QString>
23#include <QStringList>
24
25QT_BEGIN_NAMESPACE
26
27namespace GLSL {
28
29class Q_QUICK3DGLSLPARSER_EXPORT ScalarType: public Type
30{
31public:
32 const ScalarType *asScalarType() const override { return this; }
33};
34
35class Q_QUICK3DGLSLPARSER_EXPORT UndefinedType: public Type
36{
37public:
38 QString toString() const override { return QLatin1String("undefined"); }
39 const UndefinedType *asUndefinedType() const override { return this; }
40 bool isEqualTo(const Type *other) const override;
41 bool isLessThan(const Type *other) const override;
42};
43
44class Q_QUICK3DGLSLPARSER_EXPORT VoidType: public Type
45{
46public:
47 QString toString() const override { return QLatin1String("void"); }
48 const VoidType *asVoidType() const override { return this; }
49 bool isEqualTo(const Type *other) const override;
50 bool isLessThan(const Type *other) const override;
51};
52
53class Q_QUICK3DGLSLPARSER_EXPORT BoolType: public ScalarType
54{
55public:
56 QString toString() const override { return QLatin1String("bool"); }
57 const BoolType *asBoolType() const override { return this; }
58 bool isEqualTo(const Type *other) const override;
59 bool isLessThan(const Type *other) const override;
60};
61
62class Q_QUICK3DGLSLPARSER_EXPORT IntType: public ScalarType
63{
64public:
65 QString toString() const override { return QLatin1String("int"); }
66 const IntType *asIntType() const override { return this; }
67 bool isEqualTo(const Type *other) const override;
68 bool isLessThan(const Type *other) const override;
69};
70
71class Q_QUICK3DGLSLPARSER_EXPORT UIntType: public ScalarType
72{
73public:
74 QString toString() const override { return QLatin1String("uint"); }
75 const UIntType *asUIntType() const override { return this; }
76 bool isEqualTo(const Type *other) const override;
77 bool isLessThan(const Type *other) const override;
78};
79
80class Q_QUICK3DGLSLPARSER_EXPORT FloatType: public ScalarType
81{
82public:
83 QString toString() const override { return QLatin1String("float"); }
84 const FloatType *asFloatType() const override { return this; }
85 bool isEqualTo(const Type *other) const override;
86 bool isLessThan(const Type *other) const override;
87};
88
89class Q_QUICK3DGLSLPARSER_EXPORT DoubleType: public ScalarType
90{
91public:
92 QString toString() const override { return QLatin1String("double"); }
93 const DoubleType *asDoubleType() const override { return this; }
94 bool isEqualTo(const Type *other) const override;
95 bool isLessThan(const Type *other) const override;
96};
97
98// Type that can be indexed with the [] operator.
99class Q_QUICK3DGLSLPARSER_EXPORT IndexType: public Type
100{
101public:
102 IndexType(const Type *indexElementType) : _indexElementType(indexElementType) {}
103
104 const Type *indexElementType() const { return _indexElementType; }
105
106 const IndexType *asIndexType() const override { return this; }
107
108private:
109 const Type *_indexElementType;
110};
111
112class Q_QUICK3DGLSLPARSER_EXPORT VectorType: public IndexType, public Scope
113{
114public:
115 VectorType(const Type *elementType, int dimension)
116 : IndexType(elementType), _dimension(dimension) {}
117
118 QString toString() const override;
119 const Type *elementType() const { return indexElementType(); }
120 int dimension() const { return _dimension; }
121
122 QList<Symbol *> members() const override { return _members.values(); }
123
124 void add(Symbol *symbol) override;
125 Symbol *find(const QString &name) const override;
126 const Type *type() const override { return this; }
127
128 const VectorType *asVectorType() const override { return this; }
129 bool isEqualTo(const Type *other) const override;
130 bool isLessThan(const Type *other) const override;
131
132private:
133 int _dimension;
134 QHash<QString, Symbol *> _members;
135
136 friend class Engine;
137
138 void populateMembers(Engine *engine);
139 void populateMembers(Engine *engine, const char *components);
140};
141
142class Q_QUICK3DGLSLPARSER_EXPORT MatrixType: public IndexType
143{
144public:
145 MatrixType(const Type *elementType, int columns, int rows, const Type *columnType)
146 : IndexType(columnType), _elementType(elementType), _columns(columns), _rows(rows) {}
147
148 const Type *elementType() const { return _elementType; }
149 const Type *columnType() const { return indexElementType(); }
150 int columns() const { return _columns; }
151 int rows() const { return _rows; }
152
153 QString toString() const override;
154 const MatrixType *asMatrixType() const override { return this; }
155 bool isEqualTo(const Type *other) const override;
156 bool isLessThan(const Type *other) const override;
157
158private:
159 const Type *_elementType;
160 int _columns;
161 int _rows;
162};
163
164class Q_QUICK3DGLSLPARSER_EXPORT ArrayType: public IndexType
165{
166public:
167 explicit ArrayType(const Type *elementType)
168 : IndexType(elementType) {}
169
170 const Type *elementType() const { return indexElementType(); }
171
172 QString toString() const override;
173 const ArrayType *asArrayType() const override { return this; }
174 bool isEqualTo(const Type *other) const override;
175 bool isLessThan(const Type *other) const override;
176};
177
178class Q_QUICK3DGLSLPARSER_EXPORT Struct: public Type, public Scope
179{
180public:
181 Struct(Scope *scope = nullptr)
182 : Scope(scope) {}
183
184 QList<Symbol *> members() const override;
185 void add(Symbol *member) override;
186 Symbol *find(const QString &name) const override;
187
188 // as Type
189 QString toString() const override { return name(); }
190 const Struct *asStructType() const override { return this; }
191 bool isEqualTo(const Type *other) const override;
192 bool isLessThan(const Type *other) const override;
193
194 // as Symbol
195 Struct *asStruct() override { return this; } // as Symbol
196 const Type *type() const override { return this; }
197
198private:
199 QVector<Symbol *> _members;
200};
201
202class Q_QUICK3DGLSLPARSER_EXPORT Function: public Type, public Scope
203{
204public:
205 Function(Scope *scope = nullptr)
206 : Scope(scope) {}
207
208 const Type *returnType() const;
209 void setReturnType(const Type *returnType);
210
211 QVector<Argument *> arguments() const;
212 void addArgument(Argument *arg);
213 int argumentCount() const;
214 Argument *argumentAt(int index) const;
215
216 // as Type
217 QString prettyPrint() const;
218 QString toString() const override;
219 const Function *asFunctionType() const override { return this; }
220 bool isEqualTo(const Type *other) const override;
221 bool isLessThan(const Type *other) const override;
222
223 // as Symbol
224 Function *asFunction() override { return this; }
225 const Type *type() const override { return this; }
226
227 Symbol *find(const QString &name) const override;
228
229 QList<Symbol *> members() const override;
230 void add(Symbol *symbol) override {
231 if (! symbol)
232 return;
233 else if (Argument *arg = symbol->asArgument())
234 addArgument(arg);
235 }
236
237private:
238 const Type *_returnType;
239 QVector<Argument *> _arguments;
240};
241
242class Q_QUICK3DGLSLPARSER_EXPORT SamplerType: public Type
243{
244public:
245 explicit SamplerType(int kind) : _kind(kind) {}
246
247 // Kind of sampler as a token code; e.g. T_SAMPLER2D.
248 int kind() const { return _kind; }
249
250 QString toString() const override;
251 const SamplerType *asSamplerType() const override { return this; }
252 bool isEqualTo(const Type *other) const override;
253 bool isLessThan(const Type *other) const override;
254
255private:
256 int _kind;
257};
258
259class Q_QUICK3DGLSLPARSER_EXPORT OverloadSet: public Type, public Scope
260{
261public:
262 OverloadSet(Scope *enclosingScope = nullptr);
263
264 QVector<Function *> functions() const;
265 void addFunction(Function *function);
266
267 // as symbol
268 OverloadSet *asOverloadSet() override { return this; }
269 const Type *type() const override;
270 Symbol *find(const QString &name) const override;
271 void add(Symbol *symbol) override;
272
273 // as type
274 QString toString() const override { return QLatin1String("overload"); }
275 const OverloadSet *asOverloadSetType() const override { return this; }
276 bool isEqualTo(const Type *other) const override;
277 bool isLessThan(const Type *other) const override;
278
279private:
280 QVector<Function *> _functions;
281};
282
283} // namespace GLSL
284
285QT_END_NAMESPACE
286
287#endif // QSSG_GLSLTYPES_H
288

source code of qtquick3d/src/glslparser/glsltypes_p.h