1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QSSG_GLSLAST_H |
5 | #define QSSG_GLSLAST_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/glsl_p.h> |
19 | #include <QtQuick3DGlslParser/private/glslmemorypool_p.h> |
20 | #include <qstring.h> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | namespace GLSL { |
25 | |
26 | class AST; |
27 | class TranslationUnitAST; |
28 | class ExpressionAST; |
29 | class IdentifierExpressionAST; |
30 | class LiteralExpressionAST; |
31 | class BinaryExpressionAST; |
32 | class UnaryExpressionAST; |
33 | class TernaryExpressionAST; |
34 | class AssignmentExpressionAST; |
35 | class MemberAccessExpressionAST; |
36 | class FunctionCallExpressionAST; |
37 | class FunctionIdentifierAST; |
38 | class DeclarationExpressionAST; |
39 | class StatementAST; |
40 | class ExpressionStatementAST; |
41 | class CompoundStatementAST; |
42 | class IfStatementAST; |
43 | class WhileStatementAST; |
44 | class DoStatementAST; |
45 | class ForStatementAST; |
46 | class JumpStatementAST; |
47 | class ReturnStatementAST; |
48 | class SwitchStatementAST; |
49 | class CaseLabelStatementAST; |
50 | class DeclarationStatementAST; |
51 | class TypeAST; |
52 | class BasicTypeAST; |
53 | class NamedTypeAST; |
54 | class ArrayTypeAST; |
55 | class StructTypeAST; |
56 | class LayoutQualifierAST; |
57 | class QualifiedTypeAST; |
58 | class DeclarationAST; |
59 | class PrecisionDeclarationAST; |
60 | class ParameterDeclarationAST; |
61 | class VariableDeclarationAST; |
62 | class TypeDeclarationAST; |
63 | class TypeAndVariableDeclarationAST; |
64 | class InvariantDeclarationAST; |
65 | class InitDeclarationAST; |
66 | class FunctionDeclarationAST; |
67 | class Visitor; |
68 | |
69 | template <typename T> |
70 | class Q_QUICK3DGLSLPARSER_EXPORT List: public Managed |
71 | { |
72 | public: |
73 | List(const T &value_) |
74 | : value(value_), next(this), lineno(0) {} |
75 | |
76 | List(List *previous, const T &value_) |
77 | : value(value_), lineno(0) |
78 | { |
79 | next = previous->next; |
80 | previous->next = this; |
81 | } |
82 | |
83 | List *finish() |
84 | { |
85 | List *head = next; |
86 | next = nullptr; |
87 | return head; |
88 | } |
89 | |
90 | T value; |
91 | List *next; |
92 | int lineno; |
93 | }; |
94 | |
95 | // Append two lists, which are assumed to still be circular, pre-finish. |
96 | template <typename T> |
97 | List<T> *appendLists(List<T> *first, List<T> *second) |
98 | { |
99 | if (!first) |
100 | return second; |
101 | else if (!second) |
102 | return first; |
103 | List<T> *firstHead = first->next; |
104 | List<T> *secondHead = second->next; |
105 | first->next = secondHead; |
106 | second->next = firstHead; |
107 | return second; |
108 | } |
109 | |
110 | class Q_QUICK3DGLSLPARSER_EXPORT AST: public Managed |
111 | { |
112 | public: |
113 | enum Kind { |
114 | Kind_Undefined, |
115 | |
116 | // Translation unit |
117 | Kind_TranslationUnit, |
118 | |
119 | // Primary expressions |
120 | Kind_Identifier, |
121 | Kind_Literal, |
122 | |
123 | // Unary expressions |
124 | Kind_PreIncrement, |
125 | Kind_PostIncrement, |
126 | Kind_PreDecrement, |
127 | Kind_PostDecrement, |
128 | Kind_UnaryPlus, |
129 | Kind_UnaryMinus, |
130 | Kind_LogicalNot, |
131 | Kind_BitwiseNot, |
132 | |
133 | // Binary expressions |
134 | Kind_Plus, |
135 | Kind_Minus, |
136 | Kind_Multiply, |
137 | Kind_Divide, |
138 | Kind_Modulus, |
139 | Kind_ShiftLeft, |
140 | Kind_ShiftRight, |
141 | Kind_Equal, |
142 | Kind_NotEqual, |
143 | Kind_LessThan, |
144 | Kind_LessEqual, |
145 | Kind_GreaterThan, |
146 | Kind_GreaterEqual, |
147 | Kind_LogicalAnd, |
148 | Kind_LogicalOr, |
149 | Kind_LogicalXor, |
150 | Kind_BitwiseAnd, |
151 | Kind_BitwiseOr, |
152 | Kind_BitwiseXor, |
153 | Kind_Comma, |
154 | Kind_ArrayAccess, |
155 | |
156 | // Other expressions |
157 | Kind_Conditional, |
158 | Kind_MemberAccess, |
159 | Kind_FunctionCall, |
160 | Kind_MemberFunctionCall, |
161 | Kind_FunctionIdentifier, |
162 | Kind_DeclarationExpression, |
163 | |
164 | // Assignment expressions |
165 | Kind_Assign, |
166 | Kind_AssignPlus, |
167 | Kind_AssignMinus, |
168 | Kind_AssignMultiply, |
169 | Kind_AssignDivide, |
170 | Kind_AssignModulus, |
171 | Kind_AssignShiftLeft, |
172 | Kind_AssignShiftRight, |
173 | Kind_AssignAnd, |
174 | Kind_AssignOr, |
175 | Kind_AssignXor, |
176 | |
177 | // Statements |
178 | Kind_ExpressionStatement, |
179 | Kind_CompoundStatement, |
180 | Kind_If, |
181 | Kind_While, |
182 | Kind_Do, |
183 | Kind_For, |
184 | Kind_Break, |
185 | Kind_Continue, |
186 | Kind_Discard, |
187 | Kind_Return, |
188 | Kind_ReturnExpression, |
189 | Kind_Switch, |
190 | Kind_CaseLabel, |
191 | Kind_DefaultLabel, |
192 | Kind_DeclarationStatement, |
193 | |
194 | // Types |
195 | Kind_BasicType, |
196 | Kind_NamedType, |
197 | Kind_ArrayType, |
198 | Kind_OpenArrayType, |
199 | Kind_StructType, |
200 | Kind_AnonymousStructType, |
201 | Kind_StructField, |
202 | Kind_LayoutQualifier, |
203 | Kind_QualifiedType, |
204 | |
205 | // Declarations |
206 | Kind_PrecisionDeclaration, |
207 | Kind_ParameterDeclaration, |
208 | Kind_VariableDeclaration, |
209 | Kind_TypeDeclaration, |
210 | Kind_TypeAndVariableDeclaration, |
211 | Kind_InvariantDeclaration, |
212 | Kind_InitDeclaration, |
213 | Kind_FunctionDeclaration |
214 | }; |
215 | |
216 | virtual TranslationUnitAST *asTranslationUnit() { return nullptr; } |
217 | |
218 | virtual ExpressionAST *asExpression() { return nullptr; } |
219 | virtual IdentifierExpressionAST *asIdentifierExpression() { return nullptr; } |
220 | virtual LiteralExpressionAST *asLiteralExpression() { return nullptr; } |
221 | virtual BinaryExpressionAST *asBinaryExpression() { return nullptr; } |
222 | virtual UnaryExpressionAST *asUnaryExpression() { return nullptr; } |
223 | virtual TernaryExpressionAST *asTernaryExpression() { return nullptr; } |
224 | virtual AssignmentExpressionAST *asAssignmentExpression() { return nullptr; } |
225 | virtual MemberAccessExpressionAST *asMemberAccessExpression() { return nullptr; } |
226 | virtual FunctionCallExpressionAST *asFunctionCallExpression() { return nullptr; } |
227 | virtual FunctionIdentifierAST *asFunctionIdentifier() { return nullptr; } |
228 | virtual DeclarationExpressionAST *asDeclarationExpression() { return nullptr; } |
229 | |
230 | virtual StatementAST *asStatement() { return nullptr; } |
231 | virtual ExpressionStatementAST *asExpressionStatement() { return nullptr; } |
232 | virtual CompoundStatementAST *asCompoundStatement() { return nullptr; } |
233 | virtual IfStatementAST *asIfStatement() { return nullptr; } |
234 | virtual WhileStatementAST *asWhileStatement() { return nullptr; } |
235 | virtual DoStatementAST *asDoStatement() { return nullptr; } |
236 | virtual ForStatementAST *asForStatement() { return nullptr; } |
237 | virtual JumpStatementAST *asJumpStatement() { return nullptr; } |
238 | virtual ReturnStatementAST *asReturnStatement() { return nullptr; } |
239 | virtual SwitchStatementAST *asSwitchStatement() { return nullptr; } |
240 | virtual CaseLabelStatementAST *asCaseLabelStatement() { return nullptr; } |
241 | virtual DeclarationStatementAST *asDeclarationStatement() { return nullptr; } |
242 | |
243 | virtual TypeAST *asType() { return nullptr; } |
244 | virtual BasicTypeAST *asBasicType() { return nullptr; } |
245 | virtual NamedTypeAST *asNamedType() { return nullptr; } |
246 | virtual ArrayTypeAST *asArrayType() { return nullptr; } |
247 | virtual StructTypeAST *asStructType() { return nullptr; } |
248 | virtual QualifiedTypeAST *asQualifiedType() { return nullptr; } |
249 | virtual LayoutQualifierAST *asLayoutQualifier() { return nullptr; } |
250 | |
251 | virtual DeclarationAST *asDeclaration() { return nullptr; } |
252 | virtual PrecisionDeclarationAST *asPrecisionDeclaration() { return nullptr; } |
253 | virtual ParameterDeclarationAST *asParameterDeclaration() { return nullptr; } |
254 | virtual VariableDeclarationAST *asVariableDeclaration() { return nullptr; } |
255 | virtual TypeDeclarationAST *asTypeDeclaration() { return nullptr; } |
256 | virtual TypeAndVariableDeclarationAST *asTypeAndVariableDeclaration() { return nullptr; } |
257 | virtual InvariantDeclarationAST *asInvariantDeclaration() { return nullptr; } |
258 | virtual InitDeclarationAST *asInitDeclaration() { return nullptr; } |
259 | virtual FunctionDeclarationAST *asFunctionDeclaration() { return nullptr; } |
260 | |
261 | void accept(Visitor *visitor); |
262 | static void accept(AST *ast, Visitor *visitor); |
263 | |
264 | template <typename T> |
265 | static void accept(List<T> *it, Visitor *visitor) |
266 | { |
267 | for (; it; it = it->next) |
268 | accept(it->value, visitor); |
269 | } |
270 | |
271 | virtual void accept0(Visitor *visitor) = 0; |
272 | |
273 | protected: |
274 | AST(Kind _kind) : kind(_kind), lineno(0) {} |
275 | |
276 | template <typename T> |
277 | static List<T> *finish(List<T> *list) |
278 | { |
279 | if (! list) |
280 | return nullptr; |
281 | return list->finish(); // convert the circular list with a linked list. |
282 | } |
283 | |
284 | public: // attributes |
285 | int kind; |
286 | int lineno; |
287 | |
288 | protected: |
289 | ~AST() override {} // Managed types cannot be deleted. |
290 | }; |
291 | |
292 | class Q_QUICK3DGLSLPARSER_EXPORT TranslationUnitAST: public AST |
293 | { |
294 | public: |
295 | TranslationUnitAST(List<DeclarationAST *> *declarations_) |
296 | : AST(Kind_TranslationUnit), declarations(finish(list: declarations_)) {} |
297 | |
298 | TranslationUnitAST *asTranslationUnit() override { return this; } |
299 | |
300 | void accept0(Visitor *visitor) override; |
301 | |
302 | public: // attributes |
303 | List<DeclarationAST *> *declarations; |
304 | }; |
305 | |
306 | class Q_QUICK3DGLSLPARSER_EXPORT ExpressionAST: public AST |
307 | { |
308 | protected: |
309 | ExpressionAST(Kind _kind) : AST(_kind) {} |
310 | |
311 | public: |
312 | ExpressionAST *asExpression() override { return this; } |
313 | }; |
314 | |
315 | class Q_QUICK3DGLSLPARSER_EXPORT IdentifierExpressionAST: public ExpressionAST |
316 | { |
317 | public: |
318 | IdentifierExpressionAST(const QString *_name) |
319 | : ExpressionAST(Kind_Identifier), name(_name) {} |
320 | |
321 | IdentifierExpressionAST *asIdentifierExpression() override { return this; } |
322 | |
323 | void accept0(Visitor *visitor) override; |
324 | |
325 | public: // attributes |
326 | const QString *name; |
327 | }; |
328 | |
329 | class Q_QUICK3DGLSLPARSER_EXPORT LiteralExpressionAST: public ExpressionAST |
330 | { |
331 | public: |
332 | LiteralExpressionAST(const QString *_value) |
333 | : ExpressionAST(Kind_Literal), value(_value) {} |
334 | |
335 | LiteralExpressionAST *asLiteralExpression() override { return this; } |
336 | |
337 | void accept0(Visitor *visitor) override; |
338 | |
339 | public: // attributes |
340 | const QString *value; |
341 | }; |
342 | |
343 | class Q_QUICK3DGLSLPARSER_EXPORT BinaryExpressionAST: public ExpressionAST |
344 | { |
345 | public: |
346 | BinaryExpressionAST(Kind _kind, ExpressionAST *_left, ExpressionAST *_right) |
347 | : ExpressionAST(_kind), left(_left), right(_right) {} |
348 | |
349 | BinaryExpressionAST *asBinaryExpression() override { return this; } |
350 | |
351 | void accept0(Visitor *visitor) override; |
352 | |
353 | public: // attributes |
354 | ExpressionAST *left; |
355 | ExpressionAST *right; |
356 | }; |
357 | |
358 | class Q_QUICK3DGLSLPARSER_EXPORT UnaryExpressionAST: public ExpressionAST |
359 | { |
360 | public: |
361 | UnaryExpressionAST(Kind _kind, ExpressionAST *_expr) |
362 | : ExpressionAST(_kind), expr(_expr) {} |
363 | |
364 | UnaryExpressionAST *asUnaryExpression() override { return this; } |
365 | |
366 | void accept0(Visitor *visitor) override; |
367 | |
368 | public: // attributes |
369 | ExpressionAST *expr; |
370 | }; |
371 | |
372 | class Q_QUICK3DGLSLPARSER_EXPORT TernaryExpressionAST: public ExpressionAST |
373 | { |
374 | public: |
375 | TernaryExpressionAST(Kind _kind, ExpressionAST *_first, ExpressionAST *_second, ExpressionAST *_third) |
376 | : ExpressionAST(_kind), first(_first), second(_second), third(_third) {} |
377 | |
378 | TernaryExpressionAST *asTernaryExpression() override { return this; } |
379 | |
380 | void accept0(Visitor *visitor) override; |
381 | |
382 | public: // attributes |
383 | ExpressionAST *first; |
384 | ExpressionAST *second; |
385 | ExpressionAST *third; |
386 | }; |
387 | |
388 | class Q_QUICK3DGLSLPARSER_EXPORT AssignmentExpressionAST: public ExpressionAST |
389 | { |
390 | public: |
391 | AssignmentExpressionAST(Kind _kind, ExpressionAST *_variable, ExpressionAST *_value) |
392 | : ExpressionAST(_kind), variable(_variable), value(_value) {} |
393 | |
394 | AssignmentExpressionAST *asAssignmentExpression() override { return this; } |
395 | |
396 | void accept0(Visitor *visitor) override; |
397 | |
398 | public: // attributes |
399 | ExpressionAST *variable; |
400 | ExpressionAST *value; |
401 | }; |
402 | |
403 | class Q_QUICK3DGLSLPARSER_EXPORT MemberAccessExpressionAST: public ExpressionAST |
404 | { |
405 | public: |
406 | MemberAccessExpressionAST(ExpressionAST *_expr, const QString *_field) |
407 | : ExpressionAST(Kind_MemberAccess), expr(_expr), field(_field) {} |
408 | |
409 | MemberAccessExpressionAST *asMemberAccessExpression() override { return this; } |
410 | |
411 | void accept0(Visitor *visitor) override; |
412 | |
413 | public: // attributes |
414 | ExpressionAST *expr; |
415 | const QString *field; |
416 | }; |
417 | |
418 | class Q_QUICK3DGLSLPARSER_EXPORT FunctionCallExpressionAST: public ExpressionAST |
419 | { |
420 | public: |
421 | FunctionCallExpressionAST(FunctionIdentifierAST *_id, |
422 | List<ExpressionAST *> *_arguments) |
423 | : ExpressionAST(Kind_FunctionCall), expr(nullptr), id(_id) |
424 | , arguments(finish(list: _arguments)) {} |
425 | FunctionCallExpressionAST(ExpressionAST *_expr, FunctionIdentifierAST *_id, |
426 | List<ExpressionAST *> *_arguments) |
427 | : ExpressionAST(Kind_MemberFunctionCall), expr(_expr), id(_id) |
428 | , arguments(finish(list: _arguments)) {} |
429 | |
430 | FunctionCallExpressionAST *asFunctionCallExpression() override { return this; } |
431 | |
432 | void accept0(Visitor *visitor) override; |
433 | |
434 | public: // attributes |
435 | ExpressionAST *expr; |
436 | FunctionIdentifierAST *id; |
437 | List<ExpressionAST *> *arguments; |
438 | }; |
439 | |
440 | class Q_QUICK3DGLSLPARSER_EXPORT FunctionIdentifierAST: public AST |
441 | { |
442 | public: |
443 | FunctionIdentifierAST(const QString *_name) |
444 | : AST(Kind_FunctionIdentifier), name(_name), type(nullptr) {} |
445 | FunctionIdentifierAST(TypeAST *_type) |
446 | : AST(Kind_FunctionIdentifier), name(nullptr), type(_type) {} |
447 | |
448 | FunctionIdentifierAST *asFunctionIdentifier() override { return this; } |
449 | |
450 | void accept0(Visitor *visitor) override; |
451 | |
452 | public: // attributes |
453 | const QString *name; |
454 | TypeAST *type; |
455 | }; |
456 | |
457 | class Q_QUICK3DGLSLPARSER_EXPORT DeclarationExpressionAST: public ExpressionAST |
458 | { |
459 | public: |
460 | DeclarationExpressionAST(TypeAST *_type, const QString *_name, |
461 | ExpressionAST *_initializer) |
462 | : ExpressionAST(Kind_DeclarationExpression), type(_type) |
463 | , name(_name), initializer(_initializer) {} |
464 | |
465 | DeclarationExpressionAST *asDeclarationExpression() override { return this; } |
466 | |
467 | void accept0(Visitor *visitor) override; |
468 | |
469 | public: // attributes |
470 | TypeAST *type; |
471 | const QString *name; |
472 | ExpressionAST *initializer; |
473 | }; |
474 | |
475 | class Q_QUICK3DGLSLPARSER_EXPORT StatementAST: public AST |
476 | { |
477 | protected: |
478 | StatementAST(Kind _kind) : AST(_kind) {} |
479 | |
480 | public: |
481 | StatementAST *asStatement() override { return this; } |
482 | }; |
483 | |
484 | class Q_QUICK3DGLSLPARSER_EXPORT ExpressionStatementAST: public StatementAST |
485 | { |
486 | public: |
487 | ExpressionStatementAST(ExpressionAST *_expr) |
488 | : StatementAST(Kind_ExpressionStatement), expr(_expr) {} |
489 | |
490 | ExpressionStatementAST *asExpressionStatement() override { return this; } |
491 | |
492 | void accept0(Visitor *visitor) override; |
493 | |
494 | public: // attributes |
495 | ExpressionAST *expr; |
496 | }; |
497 | |
498 | class Q_QUICK3DGLSLPARSER_EXPORT CompoundStatementAST: public StatementAST |
499 | { |
500 | public: |
501 | CompoundStatementAST() |
502 | : StatementAST(Kind_CompoundStatement), statements(nullptr) |
503 | , start(0), end(0), symbol(nullptr) {} |
504 | CompoundStatementAST(List<StatementAST *> *_statements) |
505 | : StatementAST(Kind_CompoundStatement), statements(finish(list: _statements)) |
506 | , start(0), end(0), symbol(nullptr) {} |
507 | |
508 | CompoundStatementAST *asCompoundStatement() override { return this; } |
509 | |
510 | void accept0(Visitor *visitor) override; |
511 | |
512 | public: // attributes |
513 | List<StatementAST *> *statements; |
514 | int start; |
515 | int end; |
516 | Block *symbol; // decoration |
517 | }; |
518 | |
519 | class Q_QUICK3DGLSLPARSER_EXPORT IfStatementAST: public StatementAST |
520 | { |
521 | public: |
522 | IfStatementAST(ExpressionAST *_condition, StatementAST *_thenClause, StatementAST *_elseClause) |
523 | : StatementAST(Kind_If), condition(_condition) |
524 | , thenClause(_thenClause), elseClause(_elseClause) {} |
525 | |
526 | IfStatementAST *asIfStatement() override { return this; } |
527 | |
528 | void accept0(Visitor *visitor) override; |
529 | |
530 | public: // attributes |
531 | ExpressionAST *condition; |
532 | StatementAST *thenClause; |
533 | StatementAST *elseClause; |
534 | }; |
535 | |
536 | class Q_QUICK3DGLSLPARSER_EXPORT WhileStatementAST: public StatementAST |
537 | { |
538 | public: |
539 | WhileStatementAST(ExpressionAST *_condition, StatementAST *_body) |
540 | : StatementAST(Kind_While), condition(_condition), body(_body) {} |
541 | |
542 | WhileStatementAST *asWhileStatement() override { return this; } |
543 | |
544 | void accept0(Visitor *visitor) override; |
545 | |
546 | public: // attributes |
547 | ExpressionAST *condition; |
548 | StatementAST *body; |
549 | }; |
550 | |
551 | class Q_QUICK3DGLSLPARSER_EXPORT DoStatementAST: public StatementAST |
552 | { |
553 | public: |
554 | DoStatementAST(StatementAST *_body, ExpressionAST *_condition) |
555 | : StatementAST(Kind_Do), body(_body), condition(_condition) {} |
556 | |
557 | DoStatementAST *asDoStatement() override { return this; } |
558 | |
559 | void accept0(Visitor *visitor) override; |
560 | |
561 | public: // attributes |
562 | StatementAST *body; |
563 | ExpressionAST *condition; |
564 | }; |
565 | |
566 | class Q_QUICK3DGLSLPARSER_EXPORT ForStatementAST: public StatementAST |
567 | { |
568 | public: |
569 | ForStatementAST(StatementAST *_init, ExpressionAST *_condition, ExpressionAST *_increment, StatementAST *_body) |
570 | : StatementAST(Kind_For), init(_init), condition(_condition), increment(_increment), body(_body) {} |
571 | |
572 | ForStatementAST *asForStatement() override { return this; } |
573 | |
574 | void accept0(Visitor *visitor) override; |
575 | |
576 | public: // attributes |
577 | StatementAST *init; |
578 | ExpressionAST *condition; |
579 | ExpressionAST *increment; |
580 | StatementAST *body; |
581 | }; |
582 | |
583 | class Q_QUICK3DGLSLPARSER_EXPORT JumpStatementAST: public StatementAST |
584 | { |
585 | public: |
586 | JumpStatementAST(Kind _kind) : StatementAST(_kind) {} |
587 | |
588 | JumpStatementAST *asJumpStatement() override { return this; } |
589 | |
590 | void accept0(Visitor *visitor) override; |
591 | }; |
592 | |
593 | class Q_QUICK3DGLSLPARSER_EXPORT ReturnStatementAST: public StatementAST |
594 | { |
595 | public: |
596 | ReturnStatementAST() : StatementAST(Kind_Return), expr(nullptr) {} |
597 | ReturnStatementAST(ExpressionAST *_expr) |
598 | : StatementAST(Kind_ReturnExpression), expr(_expr) {} |
599 | |
600 | ReturnStatementAST *asReturnStatement() override { return this; } |
601 | |
602 | void accept0(Visitor *visitor) override; |
603 | |
604 | public: // attributes |
605 | ExpressionAST *expr; |
606 | }; |
607 | |
608 | class Q_QUICK3DGLSLPARSER_EXPORT SwitchStatementAST: public StatementAST |
609 | { |
610 | public: |
611 | SwitchStatementAST(ExpressionAST *_expr, StatementAST *_body) |
612 | : StatementAST(Kind_Switch), expr(_expr), body(_body) {} |
613 | |
614 | SwitchStatementAST *asSwitchStatement() override { return this; } |
615 | |
616 | void accept0(Visitor *visitor) override; |
617 | |
618 | public: // attributes |
619 | ExpressionAST *expr; |
620 | StatementAST *body; |
621 | }; |
622 | |
623 | class Q_QUICK3DGLSLPARSER_EXPORT CaseLabelStatementAST: public StatementAST |
624 | { |
625 | public: |
626 | CaseLabelStatementAST() : StatementAST(Kind_DefaultLabel), expr(nullptr) {} |
627 | CaseLabelStatementAST(ExpressionAST *_expr) |
628 | : StatementAST(Kind_CaseLabel), expr(_expr) {} |
629 | |
630 | CaseLabelStatementAST *asCaseLabelStatement() override { return this; } |
631 | |
632 | void accept0(Visitor *visitor) override; |
633 | |
634 | public: // attributes |
635 | ExpressionAST *expr; |
636 | }; |
637 | |
638 | class Q_QUICK3DGLSLPARSER_EXPORT DeclarationStatementAST: public StatementAST |
639 | { |
640 | public: |
641 | DeclarationStatementAST(DeclarationAST *_decl) |
642 | : StatementAST(Kind_DeclarationStatement), decl(_decl) {} |
643 | |
644 | DeclarationStatementAST *asDeclarationStatement() override { return this; } |
645 | |
646 | void accept0(Visitor *visitor) override; |
647 | |
648 | public: // attributes |
649 | DeclarationAST *decl; |
650 | }; |
651 | |
652 | class Q_QUICK3DGLSLPARSER_EXPORT TypeAST: public AST |
653 | { |
654 | protected: |
655 | TypeAST(Kind _kind) : AST(_kind) {} |
656 | |
657 | public: |
658 | enum Precision |
659 | { |
660 | PrecNotValid, // Precision not valid (e.g. structs). |
661 | PrecUnspecified, // Precision not known, but can be validly set. |
662 | Lowp, |
663 | Mediump, |
664 | Highp |
665 | }; |
666 | |
667 | TypeAST *asType() override { return this; } |
668 | |
669 | virtual Precision precision() const = 0; |
670 | |
671 | // Set the precision for the innermost basic type. Returns false if it |
672 | // is not valid to set a precision (e.g. structs). |
673 | virtual bool setPrecision(Precision precision) = 0; |
674 | }; |
675 | |
676 | class Q_QUICK3DGLSLPARSER_EXPORT BasicTypeAST: public TypeAST |
677 | { |
678 | public: |
679 | // Pass the parser's token code: T_VOID, T_VEC4, etc. |
680 | BasicTypeAST(int _token, const char *_name); |
681 | |
682 | BasicTypeAST *asBasicType() override { return this; } |
683 | |
684 | void accept0(Visitor *visitor) override; |
685 | |
686 | Precision precision() const override; |
687 | bool setPrecision(Precision precision) override; |
688 | |
689 | public: // attributes |
690 | Precision prec; |
691 | int token; |
692 | const char *name; |
693 | }; |
694 | |
695 | class Q_QUICK3DGLSLPARSER_EXPORT NamedTypeAST: public TypeAST |
696 | { |
697 | public: |
698 | NamedTypeAST(const QString *_name) : TypeAST(Kind_NamedType), name(_name) {} |
699 | |
700 | NamedTypeAST *asNamedType() override { return this; } |
701 | |
702 | void accept0(Visitor *visitor) override; |
703 | |
704 | Precision precision() const override; |
705 | bool setPrecision(Precision precision) override; |
706 | |
707 | public: // attributes |
708 | const QString *name; |
709 | }; |
710 | |
711 | class Q_QUICK3DGLSLPARSER_EXPORT ArrayTypeAST: public TypeAST |
712 | { |
713 | public: |
714 | ArrayTypeAST(TypeAST *_elementType) |
715 | : TypeAST(Kind_OpenArrayType), elementType(_elementType), size(nullptr) {} |
716 | ArrayTypeAST(TypeAST *_elementType, ExpressionAST *_size) |
717 | : TypeAST(Kind_ArrayType), elementType(_elementType), size(_size) {} |
718 | |
719 | ArrayTypeAST *asArrayType() override { return this; } |
720 | |
721 | void accept0(Visitor *visitor) override; |
722 | |
723 | Precision precision() const override; |
724 | bool setPrecision(Precision precision) override; |
725 | |
726 | public: // attributes |
727 | TypeAST *elementType; |
728 | ExpressionAST *size; |
729 | }; |
730 | |
731 | class Q_QUICK3DGLSLPARSER_EXPORT StructTypeAST: public TypeAST |
732 | { |
733 | public: |
734 | class Field: public AST |
735 | { |
736 | public: |
737 | Field(const QString *_name) |
738 | : AST(Kind_StructField), name(_name), type(nullptr) {} |
739 | |
740 | // Takes the outer shell of an array type with the innermost |
741 | // element type set to null. The fixInnerTypes() function will |
742 | // set the innermost element type to a meaningful value. |
743 | Field(const QString *_name, TypeAST *_type) |
744 | : AST(Kind_StructField), name(_name), type(_type) {} |
745 | |
746 | void accept0(Visitor *visitor) override; |
747 | |
748 | void setInnerType(TypeAST *innerType); |
749 | |
750 | const QString *name; |
751 | TypeAST *type; |
752 | }; |
753 | |
754 | StructTypeAST(List<Field *> *_fields) |
755 | : TypeAST(Kind_AnonymousStructType), name(nullptr), fields(finish(list: _fields)) {} |
756 | StructTypeAST(const QString *_name, List<Field *> *_fields) |
757 | : TypeAST(Kind_StructType), name(_name), fields(finish(list: _fields)) {} |
758 | |
759 | StructTypeAST *asStructType() override { return this; } |
760 | |
761 | void accept0(Visitor *visitor) override; |
762 | |
763 | Precision precision() const override; |
764 | bool setPrecision(Precision precision) override; |
765 | |
766 | // Fix the inner types of a field list. The "innerType" will |
767 | // be copied into the "array holes" of all fields. |
768 | static List<Field *> *fixInnerTypes(TypeAST *innerType, List<Field *> *fields); |
769 | |
770 | public: // attributes |
771 | const QString *name; |
772 | List<Field *> *fields; |
773 | }; |
774 | |
775 | class Q_QUICK3DGLSLPARSER_EXPORT LayoutQualifierAST: public AST |
776 | { |
777 | public: |
778 | LayoutQualifierAST(const QString *_name, const QString *_number) |
779 | : AST(Kind_LayoutQualifier), name(_name), number(_number) {} |
780 | |
781 | LayoutQualifierAST *asLayoutQualifier() override { return this; } |
782 | void accept0(Visitor *visitor) override; |
783 | |
784 | public: // attributes |
785 | const QString *name; |
786 | const QString *number; |
787 | }; |
788 | |
789 | class Q_QUICK3DGLSLPARSER_EXPORT QualifiedTypeAST: public TypeAST |
790 | { |
791 | public: |
792 | QualifiedTypeAST(int _qualifiers, TypeAST *_type, List<LayoutQualifierAST *> *_layout_list) |
793 | : TypeAST(Kind_QualifiedType), qualifiers(_qualifiers), type(_type) |
794 | , layout_list(finish(list: _layout_list)) {} |
795 | |
796 | enum |
797 | { |
798 | StorageMask = 0x000000FF, |
799 | NoStorage = 0x00000000, |
800 | Const = 0x00000001, |
801 | Attribute = 0x00000002, |
802 | Varying = 0x00000003, |
803 | CentroidVarying = 0x00000004, |
804 | In = 0x00000005, |
805 | Out = 0x00000006, |
806 | CentroidIn = 0x00000007, |
807 | CentroidOut = 0x00000008, |
808 | PatchIn = 0x00000009, |
809 | PatchOut = 0x0000000A, |
810 | SampleIn = 0x0000000B, |
811 | SampleOut = 0x0000000C, |
812 | Uniform = 0x0000000D, |
813 | InterpolationMask = 0x00000F00, |
814 | NoInterpolation = 0x00000000, |
815 | Smooth = 0x00000100, |
816 | Flat = 0x00000200, |
817 | NoPerspective = 0x00000300, |
818 | Invariant = 0x00010000, |
819 | Struct = 0x00020000 |
820 | }; |
821 | |
822 | QualifiedTypeAST *asQualifiedType() override { return this; } |
823 | |
824 | void accept0(Visitor *visitor) override; |
825 | |
826 | Precision precision() const override { return type->precision(); } |
827 | bool setPrecision(Precision precision) override { return type->setPrecision(precision); } |
828 | |
829 | public: // attributes |
830 | int qualifiers; |
831 | TypeAST *type; |
832 | List<LayoutQualifierAST *> *layout_list; |
833 | }; |
834 | |
835 | class Q_QUICK3DGLSLPARSER_EXPORT DeclarationAST: public AST |
836 | { |
837 | protected: |
838 | DeclarationAST(Kind _kind) : AST(_kind) {} |
839 | |
840 | public: |
841 | DeclarationAST *asDeclaration() override { return this; } |
842 | }; |
843 | |
844 | class Q_QUICK3DGLSLPARSER_EXPORT PrecisionDeclarationAST: public DeclarationAST |
845 | { |
846 | public: |
847 | PrecisionDeclarationAST(TypeAST::Precision _precision, TypeAST *_type) |
848 | : DeclarationAST(Kind_PrecisionDeclaration) |
849 | , precision(_precision), type(_type) {} |
850 | |
851 | PrecisionDeclarationAST *asPrecisionDeclaration() override { return this; } |
852 | |
853 | void accept0(Visitor *visitor) override; |
854 | |
855 | public: // attributes |
856 | TypeAST::Precision precision; |
857 | TypeAST *type; |
858 | }; |
859 | |
860 | class Q_QUICK3DGLSLPARSER_EXPORT ParameterDeclarationAST: public DeclarationAST |
861 | { |
862 | public: |
863 | enum Qualifier |
864 | { |
865 | In, |
866 | Out, |
867 | InOut |
868 | }; |
869 | ParameterDeclarationAST(TypeAST *_type, Qualifier _qualifier, |
870 | const QString *_name) |
871 | : DeclarationAST(Kind_ParameterDeclaration), type(_type) |
872 | , qualifier(_qualifier), name(_name) {} |
873 | |
874 | ParameterDeclarationAST *asParameterDeclaration() override { return this; } |
875 | |
876 | void accept0(Visitor *visitor) override; |
877 | |
878 | public: // attributes |
879 | TypeAST *type; |
880 | Qualifier qualifier; |
881 | const QString *name; |
882 | }; |
883 | |
884 | class Q_QUICK3DGLSLPARSER_EXPORT VariableDeclarationAST: public DeclarationAST |
885 | { |
886 | public: |
887 | VariableDeclarationAST(TypeAST *_type, const QString *_name, |
888 | ExpressionAST *_initializer = nullptr) |
889 | : DeclarationAST(Kind_VariableDeclaration), type(_type) |
890 | , name(_name), initializer(_initializer) {} |
891 | |
892 | VariableDeclarationAST *asVariableDeclaration() override { return this; } |
893 | |
894 | void accept0(Visitor *visitor) override; |
895 | |
896 | static TypeAST *declarationType(List<DeclarationAST *> *decls); |
897 | |
898 | public: // attributes |
899 | TypeAST *type; |
900 | const QString *name; |
901 | ExpressionAST *initializer; |
902 | }; |
903 | |
904 | class Q_QUICK3DGLSLPARSER_EXPORT TypeDeclarationAST: public DeclarationAST |
905 | { |
906 | public: |
907 | TypeDeclarationAST(TypeAST *_type) |
908 | : DeclarationAST(Kind_TypeDeclaration), type(_type) {} |
909 | |
910 | TypeDeclarationAST *asTypeDeclaration() override { return this; } |
911 | |
912 | void accept0(Visitor *visitor) override; |
913 | |
914 | public: // attributes |
915 | TypeAST *type; |
916 | }; |
917 | |
918 | class Q_QUICK3DGLSLPARSER_EXPORT TypeAndVariableDeclarationAST: public DeclarationAST |
919 | { |
920 | public: |
921 | TypeAndVariableDeclarationAST(TypeDeclarationAST *_typeDecl, |
922 | VariableDeclarationAST *_varDecl) |
923 | : DeclarationAST(Kind_TypeAndVariableDeclaration) |
924 | , typeDecl(_typeDecl), varDecl(_varDecl) {} |
925 | |
926 | TypeAndVariableDeclarationAST *asTypeAndVariableDeclaration() override { return this; } |
927 | |
928 | void accept0(Visitor *visitor) override; |
929 | |
930 | public: // attributes |
931 | TypeDeclarationAST *typeDecl; |
932 | VariableDeclarationAST *varDecl; |
933 | }; |
934 | |
935 | class Q_QUICK3DGLSLPARSER_EXPORT InvariantDeclarationAST: public DeclarationAST |
936 | { |
937 | public: |
938 | InvariantDeclarationAST(const QString *_name) |
939 | : DeclarationAST(Kind_InvariantDeclaration), name(_name) {} |
940 | |
941 | InvariantDeclarationAST *asInvariantDeclaration() override { return this; } |
942 | |
943 | void accept0(Visitor *visitor) override; |
944 | |
945 | public: // attributes |
946 | const QString *name; |
947 | }; |
948 | |
949 | class Q_QUICK3DGLSLPARSER_EXPORT InitDeclarationAST: public DeclarationAST |
950 | { |
951 | public: |
952 | InitDeclarationAST(List<DeclarationAST *> *_decls) |
953 | : DeclarationAST(Kind_InitDeclaration), decls(finish(list: _decls)) {} |
954 | |
955 | InitDeclarationAST *asInitDeclaration() override { return this; } |
956 | |
957 | void accept0(Visitor *visitor) override; |
958 | |
959 | public: // attributes |
960 | List<DeclarationAST *> *decls; |
961 | }; |
962 | |
963 | class Q_QUICK3DGLSLPARSER_EXPORT FunctionDeclarationAST : public DeclarationAST |
964 | { |
965 | public: |
966 | FunctionDeclarationAST(TypeAST *_returnType, const QString *_name) |
967 | : DeclarationAST(Kind_FunctionDeclaration), returnType(_returnType) |
968 | , name(_name), params(nullptr), body(nullptr) {} |
969 | |
970 | FunctionDeclarationAST *asFunctionDeclaration() override { return this; } |
971 | |
972 | void accept0(Visitor *visitor) override; |
973 | |
974 | void finishParams() { params = finish(list: params); } |
975 | |
976 | bool isPrototype() const { return body == nullptr; } |
977 | |
978 | public: // attributes |
979 | TypeAST *returnType; |
980 | const QString *name; |
981 | List<ParameterDeclarationAST *> *params; |
982 | StatementAST *body; |
983 | }; |
984 | |
985 | } // namespace GLSL |
986 | |
987 | QT_END_NAMESPACE |
988 | |
989 | #endif // QSSG_GLSLAST_H |
990 | |