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