1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "glslast_p.h"
5#include "glslastvisitor_p.h"
6#include "glslparsertable_p.h"
7
8QT_BEGIN_NAMESPACE
9
10using namespace GLSL;
11
12void AST::accept(Visitor *visitor)
13{
14 if (visitor->preVisit(this))
15 accept0(visitor);
16 visitor->postVisit(this);
17}
18
19void AST::accept(AST *ast, Visitor *visitor)
20{
21 if (ast)
22 ast->accept(visitor);
23}
24
25void TranslationUnitAST::accept0(Visitor *visitor)
26{
27 if (visitor->visit(this))
28 accept(it: declarations, visitor);
29 visitor->endVisit(this);
30}
31
32void IdentifierExpressionAST::accept0(Visitor *visitor)
33{
34 visitor->visit(this);
35 visitor->endVisit(this);
36}
37
38void LiteralExpressionAST::accept0(Visitor *visitor)
39{
40 visitor->visit(this);
41 visitor->endVisit(this);
42}
43
44void BinaryExpressionAST::accept0(Visitor *visitor)
45{
46 if (visitor->visit(this)) {
47 accept(ast: left, visitor);
48 accept(ast: right, visitor);
49 }
50 visitor->endVisit(this);
51}
52
53void UnaryExpressionAST::accept0(Visitor *visitor)
54{
55 if (visitor->visit(this))
56 accept(ast: expr, visitor);
57 visitor->endVisit(this);
58}
59
60void TernaryExpressionAST::accept0(Visitor *visitor)
61{
62 if (visitor->visit(this)) {
63 accept(ast: first, visitor);
64 accept(ast: second, visitor);
65 accept(ast: third, visitor);
66 }
67 visitor->endVisit(this);
68}
69
70void AssignmentExpressionAST::accept0(Visitor *visitor)
71{
72 if (visitor->visit(this)) {
73 accept(ast: variable, visitor);
74 accept(ast: value, visitor);
75 }
76 visitor->endVisit(this);
77}
78
79void MemberAccessExpressionAST::accept0(Visitor *visitor)
80{
81 if (visitor->visit(this))
82 accept(ast: expr, visitor);
83 visitor->endVisit(this);
84}
85
86void FunctionCallExpressionAST::accept0(Visitor *visitor)
87{
88 if (visitor->visit(this)) {
89 accept(ast: expr, visitor);
90 accept(ast: id, visitor);
91 accept(it: arguments, visitor);
92 }
93 visitor->endVisit(this);
94}
95
96void FunctionIdentifierAST::accept0(Visitor *visitor)
97{
98 if (visitor->visit(this))
99 accept(ast: type, visitor);
100 visitor->endVisit(this);
101}
102
103void DeclarationExpressionAST::accept0(Visitor *visitor)
104{
105 if (visitor->visit(this)) {
106 accept(ast: type, visitor);
107 accept(ast: initializer, visitor);
108 }
109 visitor->endVisit(this);
110}
111
112void ExpressionStatementAST::accept0(Visitor *visitor)
113{
114 if (visitor->visit(this))
115 accept(ast: expr, visitor);
116 visitor->endVisit(this);
117}
118
119void CompoundStatementAST::accept0(Visitor *visitor)
120{
121 if (visitor->visit(this))
122 accept(it: statements, visitor);
123 visitor->endVisit(this);
124}
125
126void IfStatementAST::accept0(Visitor *visitor)
127{
128 if (visitor->visit(this)) {
129 accept(ast: condition, visitor);
130 accept(ast: thenClause, visitor);
131 accept(ast: elseClause, visitor);
132 }
133 visitor->endVisit(this);
134}
135
136void WhileStatementAST::accept0(Visitor *visitor)
137{
138 if (visitor->visit(this)) {
139 accept(ast: condition, visitor);
140 accept(ast: body, visitor);
141 }
142 visitor->endVisit(this);
143}
144
145void DoStatementAST::accept0(Visitor *visitor)
146{
147 if (visitor->visit(this)) {
148 accept(ast: body, visitor);
149 accept(ast: condition, visitor);
150 }
151 visitor->endVisit(this);
152}
153
154void ForStatementAST::accept0(Visitor *visitor)
155{
156 if (visitor->visit(this)) {
157 accept(ast: init, visitor);
158 accept(ast: condition, visitor);
159 accept(ast: increment, visitor);
160 accept(ast: body, visitor);
161 }
162 visitor->endVisit(this);
163}
164
165void JumpStatementAST::accept0(Visitor *visitor)
166{
167 visitor->visit(this);
168 visitor->endVisit(this);
169}
170
171void ReturnStatementAST::accept0(Visitor *visitor)
172{
173 if (visitor->visit(this))
174 accept(ast: expr, visitor);
175 visitor->endVisit(this);
176}
177
178void SwitchStatementAST::accept0(Visitor *visitor)
179{
180 if (visitor->visit(this)) {
181 accept(ast: expr, visitor);
182 accept(ast: body, visitor);
183 }
184 visitor->endVisit(this);
185}
186
187void CaseLabelStatementAST::accept0(Visitor *visitor)
188{
189 if (visitor->visit(this))
190 accept(ast: expr, visitor);
191 visitor->endVisit(this);
192}
193
194void DeclarationStatementAST::accept0(Visitor *visitor)
195{
196 if (visitor->visit(this))
197 accept(ast: decl, visitor);
198 visitor->endVisit(this);
199}
200
201BasicTypeAST::BasicTypeAST(int _token, const char *_name)
202 : TypeAST(Kind_BasicType), token(_token), name(_name)
203{
204 switch (token) {
205 case GLSLParserTable::T_VOID:
206 case GLSLParserTable::T_BOOL:
207 case GLSLParserTable::T_BVEC2:
208 case GLSLParserTable::T_BVEC3:
209 case GLSLParserTable::T_BVEC4:
210 prec = PrecNotValid;
211 break;
212 default:
213 prec = PrecUnspecified;
214 break;
215 }
216}
217
218void BasicTypeAST::accept0(Visitor *visitor)
219{
220 visitor->visit(this);
221 visitor->endVisit(this);
222}
223
224TypeAST::Precision BasicTypeAST::precision() const
225{
226 return prec;
227}
228
229bool BasicTypeAST::setPrecision(Precision precision)
230{
231 if (prec == PrecNotValid)
232 return false;
233 prec = precision;
234 return true;
235}
236
237void NamedTypeAST::accept0(Visitor *visitor)
238{
239 visitor->visit(this);
240 visitor->endVisit(this);
241}
242
243TypeAST::Precision NamedTypeAST::precision() const
244{
245 // Named types are typically structs, which cannot have their precision set.
246 return PrecNotValid;
247}
248
249bool NamedTypeAST::setPrecision(Precision)
250{
251 return false;
252}
253
254void ArrayTypeAST::accept0(Visitor *visitor)
255{
256 if (visitor->visit(this)) {
257 accept(ast: elementType, visitor);
258 accept(ast: size, visitor);
259 }
260 visitor->endVisit(this);
261}
262
263TypeAST::Precision ArrayTypeAST::precision() const
264{
265 return elementType ? elementType->precision() : PrecNotValid;
266}
267
268bool ArrayTypeAST::setPrecision(Precision precision)
269{
270 if (elementType)
271 return elementType->setPrecision(precision);
272
273 return false;
274}
275
276void StructTypeAST::accept0(Visitor *visitor)
277{
278 if (visitor->visit(this))
279 accept(it: fields, visitor);
280 visitor->endVisit(this);
281}
282
283TypeAST::Precision StructTypeAST::precision() const
284{
285 return PrecNotValid;
286}
287
288bool StructTypeAST::setPrecision(Precision)
289{
290 // Structs cannot have a precision set.
291 return false;
292}
293
294void StructTypeAST::Field::accept0(Visitor *visitor)
295{
296 if (visitor->visit(this))
297 accept(ast: type, visitor);
298 visitor->endVisit(this);
299}
300
301void StructTypeAST::Field::setInnerType(TypeAST *innerType)
302{
303 if (!innerType)
304 return;
305 TypeAST **parent = &type;
306 TypeAST *inner = type;
307 while (inner != nullptr) {
308 ArrayTypeAST *array = inner->asArrayType();
309 if (!array)
310 break;
311 parent = &(array->elementType);
312 inner = array->elementType;
313 }
314 *parent = innerType;
315}
316
317List<StructTypeAST::Field *> *StructTypeAST::fixInnerTypes(TypeAST *innerType, List<Field *> *fields)
318{
319 if (!fields)
320 return fields;
321 List<Field *> *head = fields->next;
322 List<Field *> *current = head;
323 do {
324 current->value->setInnerType(innerType);
325 current = current->next;
326 } while (current && current != head);
327 return fields;
328}
329
330void LayoutQualifierAST::accept0(Visitor *visitor)
331{
332 if (visitor->visit(this)) {
333 }
334 visitor->endVisit(this);
335}
336
337void QualifiedTypeAST::accept0(Visitor *visitor)
338{
339 if (visitor->visit(this))
340 accept(ast: type, visitor);
341 visitor->endVisit(this);
342}
343
344void PrecisionDeclarationAST::accept0(Visitor *visitor)
345{
346 if (visitor->visit(this))
347 accept(ast: type, visitor);
348 visitor->endVisit(this);
349}
350
351void ParameterDeclarationAST::accept0(Visitor *visitor)
352{
353 if (visitor->visit(this))
354 accept(ast: type, visitor);
355 visitor->endVisit(this);
356}
357
358void VariableDeclarationAST::accept0(Visitor *visitor)
359{
360 if (visitor->visit(this)) {
361 accept(ast: type, visitor);
362 accept(ast: initializer, visitor);
363 }
364 visitor->endVisit(this);
365}
366
367TypeAST *VariableDeclarationAST::declarationType(List<DeclarationAST *> *decls)
368{
369 VariableDeclarationAST *var = decls->value->asVariableDeclaration();
370 return var ? var->type : nullptr;
371}
372
373void TypeDeclarationAST::accept0(Visitor *visitor)
374{
375 if (visitor->visit(this))
376 accept(ast: type, visitor);
377 visitor->endVisit(this);
378}
379
380void TypeAndVariableDeclarationAST::accept0(Visitor *visitor)
381{
382 if (visitor->visit(this)) {
383 accept(ast: typeDecl, visitor);
384 accept(ast: varDecl, visitor);
385 }
386 visitor->endVisit(this);
387}
388
389void InvariantDeclarationAST::accept0(Visitor *visitor)
390{
391 visitor->visit(this);
392 visitor->endVisit(this);
393}
394
395void InitDeclarationAST::accept0(Visitor *visitor)
396{
397 if (visitor->visit(this))
398 accept(it: decls, visitor);
399 visitor->endVisit(this);
400}
401
402void FunctionDeclarationAST::accept0(Visitor *visitor)
403{
404 if (visitor->visit(this)) {
405 accept(ast: returnType, visitor);
406 accept(it: params, visitor);
407 accept(ast: body, visitor);
408 }
409 visitor->endVisit(this);
410}
411
412QT_END_NAMESPACE
413

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