OSDN Git Service

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