1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "glslastvisitor.h"
35 #include "glslparsertable_p.h"
39 void AST::accept(Visitor *visitor)
41 if (visitor->preVisit(this))
43 visitor->postVisit(this);
46 void AST::accept(AST *ast, Visitor *visitor)
52 void TranslationUnitAST::accept0(Visitor *visitor)
54 if (visitor->visit(this)) {
55 accept(declarations, visitor);
57 visitor->endVisit(this);
60 void IdentifierExpressionAST::accept0(Visitor *visitor)
63 visitor->endVisit(this);
66 void LiteralExpressionAST::accept0(Visitor *visitor)
69 visitor->endVisit(this);
72 void BinaryExpressionAST::accept0(Visitor *visitor)
74 if (visitor->visit(this)) {
75 accept(left, visitor);
76 accept(right, visitor);
78 visitor->endVisit(this);
81 void UnaryExpressionAST::accept0(Visitor *visitor)
83 if (visitor->visit(this))
84 accept(expr, visitor);
85 visitor->endVisit(this);
88 void TernaryExpressionAST::accept0(Visitor *visitor)
90 if (visitor->visit(this)) {
91 accept(first, visitor);
92 accept(second, visitor);
93 accept(third, visitor);
95 visitor->endVisit(this);
98 void AssignmentExpressionAST::accept0(Visitor *visitor)
100 if (visitor->visit(this)) {
101 accept(variable, visitor);
102 accept(value, visitor);
104 visitor->endVisit(this);
107 void MemberAccessExpressionAST::accept0(Visitor *visitor)
109 if (visitor->visit(this))
110 accept(expr, visitor);
111 visitor->endVisit(this);
114 void FunctionCallExpressionAST::accept0(Visitor *visitor)
116 if (visitor->visit(this)) {
117 accept(expr, visitor);
119 accept(arguments, visitor);
121 visitor->endVisit(this);
124 void FunctionIdentifierAST::accept0(Visitor *visitor)
126 if (visitor->visit(this))
127 accept(type, visitor);
128 visitor->endVisit(this);
131 void DeclarationExpressionAST::accept0(Visitor *visitor)
133 if (visitor->visit(this)) {
134 accept(type, visitor);
135 accept(initializer, visitor);
137 visitor->endVisit(this);
140 void ExpressionStatementAST::accept0(Visitor *visitor)
142 if (visitor->visit(this))
143 accept(expr, visitor);
144 visitor->endVisit(this);
147 void CompoundStatementAST::accept0(Visitor *visitor)
149 if (visitor->visit(this))
150 accept(statements, visitor);
151 visitor->endVisit(this);
154 void IfStatementAST::accept0(Visitor *visitor)
156 if (visitor->visit(this)) {
157 accept(condition, visitor);
158 accept(thenClause, visitor);
159 accept(elseClause, visitor);
161 visitor->endVisit(this);
164 void WhileStatementAST::accept0(Visitor *visitor)
166 if (visitor->visit(this)) {
167 accept(condition, visitor);
168 accept(body, visitor);
170 visitor->endVisit(this);
173 void DoStatementAST::accept0(Visitor *visitor)
175 if (visitor->visit(this)) {
176 accept(body, visitor);
177 accept(condition, visitor);
179 visitor->endVisit(this);
182 void ForStatementAST::accept0(Visitor *visitor)
184 if (visitor->visit(this)) {
185 accept(init, visitor);
186 accept(condition, visitor);
187 accept(increment, visitor);
188 accept(body, visitor);
190 visitor->endVisit(this);
193 void JumpStatementAST::accept0(Visitor *visitor)
195 visitor->visit(this);
196 visitor->endVisit(this);
199 void ReturnStatementAST::accept0(Visitor *visitor)
201 if (visitor->visit(this))
202 accept(expr, visitor);
203 visitor->endVisit(this);
206 void SwitchStatementAST::accept0(Visitor *visitor)
208 if (visitor->visit(this)) {
209 accept(expr, visitor);
210 accept(body, visitor);
212 visitor->endVisit(this);
215 void CaseLabelStatementAST::accept0(Visitor *visitor)
217 if (visitor->visit(this))
218 accept(expr, visitor);
219 visitor->endVisit(this);
222 void DeclarationStatementAST::accept0(Visitor *visitor)
224 if (visitor->visit(this))
225 accept(decl, visitor);
226 visitor->endVisit(this);
229 BasicTypeAST::BasicTypeAST(int _token, const char *_name)
230 : TypeAST(Kind_BasicType), token(_token), name(_name)
233 case GLSLParserTable::T_VOID:
234 case GLSLParserTable::T_BOOL:
235 case GLSLParserTable::T_BVEC2:
236 case GLSLParserTable::T_BVEC3:
237 case GLSLParserTable::T_BVEC4:
241 prec = PrecUnspecified;
246 void BasicTypeAST::accept0(Visitor *visitor)
248 visitor->visit(this);
249 visitor->endVisit(this);
252 TypeAST::Precision BasicTypeAST::precision() const
257 bool BasicTypeAST::setPrecision(Precision precision)
259 if (prec == PrecNotValid)
265 void NamedTypeAST::accept0(Visitor *visitor)
267 visitor->visit(this);
268 visitor->endVisit(this);
271 TypeAST::Precision NamedTypeAST::precision() const
273 // Named types are typically structs, which cannot have their precision set.
277 bool NamedTypeAST::setPrecision(Precision)
282 void ArrayTypeAST::accept0(Visitor *visitor)
284 if (visitor->visit(this)) {
285 accept(elementType, visitor);
286 accept(size, visitor);
288 visitor->endVisit(this);
291 TypeAST::Precision ArrayTypeAST::precision() const
293 return elementType ? elementType->precision() : PrecNotValid;
296 bool ArrayTypeAST::setPrecision(Precision precision)
299 return elementType->setPrecision(precision);
304 void StructTypeAST::accept0(Visitor *visitor)
306 if (visitor->visit(this))
307 accept(fields, visitor);
308 visitor->endVisit(this);
311 TypeAST::Precision StructTypeAST::precision() const
316 bool StructTypeAST::setPrecision(Precision)
318 // Structs cannot have a precision set.
322 void StructTypeAST::Field::accept0(Visitor *visitor)
324 if (visitor->visit(this))
325 accept(type, visitor);
326 visitor->endVisit(this);
329 void StructTypeAST::Field::setInnerType(TypeAST *innerType)
333 TypeAST **parent = &type;
334 TypeAST *inner = type;
336 ArrayTypeAST *array = inner->asArrayType();
339 parent = &(array->elementType);
340 inner = array->elementType;
345 List<StructTypeAST::Field *> *StructTypeAST::fixInnerTypes(TypeAST *innerType, List<Field *> *fields)
349 List<Field *> *head = fields->next;
350 List<Field *> *current = head;
352 current->value->setInnerType(innerType);
353 current = current->next;
354 } while (current && current != head);
358 void QualifiedTypeAST::accept0(Visitor *visitor)
360 if (visitor->visit(this))
361 accept(type, visitor);
362 visitor->endVisit(this);
365 void PrecisionDeclarationAST::accept0(Visitor *visitor)
367 if (visitor->visit(this))
368 accept(type, visitor);
369 visitor->endVisit(this);
372 void ParameterDeclarationAST::accept0(Visitor *visitor)
374 if (visitor->visit(this))
375 accept(type, visitor);
376 visitor->endVisit(this);
379 void VariableDeclarationAST::accept0(Visitor *visitor)
381 if (visitor->visit(this)) {
382 accept(type, visitor);
383 accept(initializer, visitor);
385 visitor->endVisit(this);
388 TypeAST *VariableDeclarationAST::declarationType(List<DeclarationAST *> *decls)
390 VariableDeclarationAST *var = decls->value->asVariableDeclaration();
391 return var ? var->type : 0;
394 void TypeDeclarationAST::accept0(Visitor *visitor)
396 if (visitor->visit(this))
397 accept(type, visitor);
398 visitor->endVisit(this);
401 void TypeAndVariableDeclarationAST::accept0(Visitor *visitor)
403 if (visitor->visit(this)) {
404 accept(typeDecl, visitor);
405 accept(varDecl, visitor);
407 visitor->endVisit(this);
410 void InvariantDeclarationAST::accept0(Visitor *visitor)
412 visitor->visit(this);
413 visitor->endVisit(this);
416 void InitDeclarationAST::accept0(Visitor *visitor)
418 if (visitor->visit(this))
419 accept(decls, visitor);
420 visitor->endVisit(this);
423 void FunctionDeclarationAST::accept0(Visitor *visitor)
425 if (visitor->visit(this)) {
426 accept(returnType, visitor);
427 accept(params, visitor);
428 accept(body, visitor);
430 visitor->endVisit(this);