blob: b3a711703f80b426eb48a59a4cf2e0fe30faef66 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/ParseHelper.h"
8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000012#include "compiler/glslang.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "compiler/osinclude.h"
14#include "compiler/InitializeParseContext.h"
15
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000016extern "C" {
17extern int InitPreprocessor();
18extern int FinalizePreprocessor();
19extern void PredefineIntMacro(const char *name, int value);
20}
21
22static void ReportInfo(TInfoSinkBase& sink,
23 TPrefixType type, TSourceLoc loc,
24 const char* reason, const char* token,
25 const char* extraInfo)
26{
27 /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
28 sink.prefix(type);
29 sink.location(loc);
30 sink << "'" << token << "' : " << reason << " " << extraInfo << "\n";
31}
32
33static void DefineExtensionMacros(const TExtensionBehavior& extBehavior)
34{
35 for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
36 iter != extBehavior.end(); ++iter) {
37 PredefineIntMacro(iter->first.c_str(), 1);
38 }
39}
40
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041///////////////////////////////////////////////////////////////////////
42//
43// Sub- vector and matrix fields
44//
45////////////////////////////////////////////////////////////////////////
46
47//
48// Look at a '.' field selector string and change it into offsets
49// for a vector.
50//
51bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
52{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000053 fields.num = (int) compString.size();
54 if (fields.num > 4) {
55 error(line, "illegal vector field selection", compString.c_str(), "");
56 return false;
57 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000059 enum {
60 exyzw,
61 ergba,
62 estpq,
63 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000065 for (int i = 0; i < fields.num; ++i) {
66 switch (compString[i]) {
67 case 'x':
68 fields.offsets[i] = 0;
69 fieldSet[i] = exyzw;
70 break;
71 case 'r':
72 fields.offsets[i] = 0;
73 fieldSet[i] = ergba;
74 break;
75 case 's':
76 fields.offsets[i] = 0;
77 fieldSet[i] = estpq;
78 break;
79 case 'y':
80 fields.offsets[i] = 1;
81 fieldSet[i] = exyzw;
82 break;
83 case 'g':
84 fields.offsets[i] = 1;
85 fieldSet[i] = ergba;
86 break;
87 case 't':
88 fields.offsets[i] = 1;
89 fieldSet[i] = estpq;
90 break;
91 case 'z':
92 fields.offsets[i] = 2;
93 fieldSet[i] = exyzw;
94 break;
95 case 'b':
96 fields.offsets[i] = 2;
97 fieldSet[i] = ergba;
98 break;
99 case 'p':
100 fields.offsets[i] = 2;
101 fieldSet[i] = estpq;
102 break;
103
104 case 'w':
105 fields.offsets[i] = 3;
106 fieldSet[i] = exyzw;
107 break;
108 case 'a':
109 fields.offsets[i] = 3;
110 fieldSet[i] = ergba;
111 break;
112 case 'q':
113 fields.offsets[i] = 3;
114 fieldSet[i] = estpq;
115 break;
116 default:
117 error(line, "illegal vector field selection", compString.c_str(), "");
118 return false;
119 }
120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000122 for (int i = 0; i < fields.num; ++i) {
123 if (fields.offsets[i] >= vecSize) {
124 error(line, "vector field selection out of range", compString.c_str(), "");
125 return false;
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 if (i > 0) {
129 if (fieldSet[i] != fieldSet[i-1]) {
130 error(line, "illegal - vector component fields not from the same set", compString.c_str(), "");
131 return false;
132 }
133 }
134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137}
138
139
140//
141// Look at a '.' field selector string and change it into offsets
142// for a matrix.
143//
144bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
145{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000146 fields.wholeRow = false;
147 fields.wholeCol = false;
148 fields.row = -1;
149 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000151 if (compString.size() != 2) {
152 error(line, "illegal length of matrix field selection", compString.c_str(), "");
153 return false;
154 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 if (compString[0] == '_') {
157 if (compString[1] < '0' || compString[1] > '3') {
158 error(line, "illegal matrix field selection", compString.c_str(), "");
159 return false;
160 }
161 fields.wholeCol = true;
162 fields.col = compString[1] - '0';
163 } else if (compString[1] == '_') {
164 if (compString[0] < '0' || compString[0] > '3') {
165 error(line, "illegal matrix field selection", compString.c_str(), "");
166 return false;
167 }
168 fields.wholeRow = true;
169 fields.row = compString[0] - '0';
170 } else {
171 if (compString[0] < '0' || compString[0] > '3' ||
172 compString[1] < '0' || compString[1] > '3') {
173 error(line, "illegal matrix field selection", compString.c_str(), "");
174 return false;
175 }
176 fields.row = compString[0] - '0';
177 fields.col = compString[1] - '0';
178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000180 if (fields.row >= matSize || fields.col >= matSize) {
181 error(line, "matrix field selection out of range", compString.c_str(), "");
182 return false;
183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000185 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186}
187
188///////////////////////////////////////////////////////////////////////
189//
190// Errors
191//
192////////////////////////////////////////////////////////////////////////
193
194//
195// Track whether errors have occurred.
196//
197void TParseContext::recover()
198{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000199 recoveredFromError = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200}
201
202//
203// Used by flex/bison to output all syntax and parsing errors.
204//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000205void TParseContext::error(TSourceLoc loc,
206 const char* reason, const char* token,
207 const char* extraInfoFormat, ...)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000209 char extraInfo[512];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000210 va_list marker;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000211 va_start(marker, extraInfoFormat);
212 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000213
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000214 ReportInfo(infoSink.info, EPrefixError, loc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000215
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000216 va_end(marker);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000217 ++numErrors;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218}
219
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000220void TParseContext::warning(TSourceLoc loc,
221 const char* reason, const char* token,
222 const char* extraInfoFormat, ...) {
223 char extraInfo[512];
224 va_list marker;
225 va_start(marker, extraInfoFormat);
226 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
227
228 ReportInfo(infoSink.info, EPrefixWarning, loc, reason, token, extraInfo);
229
230 va_end(marker);
231}
232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233//
234// Same error message for all places assignments don't work.
235//
236void TParseContext::assignError(int line, const char* op, TString left, TString right)
237{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000238 error(line, "", op, "cannot convert from '%s' to '%s'",
239 right.c_str(), left.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240}
241
242//
243// Same error message for all places unary operations don't work.
244//
245void TParseContext::unaryOpError(int line, const char* op, TString operand)
246{
247 error(line, " wrong operand type", op,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000248 "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",
249 op, operand.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252//
253// Same error message for all binary operations don't work.
254//
255void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
256{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000257 error(line, " wrong operand types ", op,
258 "no operation '%s' exists that takes a left-hand operand of type '%s' and "
259 "a right operand of type '%s' (or there is no acceptable conversion)",
260 op, left.c_str(), right.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261}
262
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000263bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
264 switch( type ){
265 case EbtFloat:
266 if( precision == EbpUndefined ){
267 error( line, "No precision specified for (float)", "", "" );
268 return true;
269 }
270 break;
271 case EbtInt:
272 if( precision == EbpUndefined ){
273 error( line, "No precision specified (int)", "", "" );
274 return true;
275 }
276 break;
277 }
278 return false;
279}
280
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281//
282// Both test and if necessary, spit out an error, to see if the node is really
283// an l-value that can be operated on this way.
284//
285// Returns true if the was an error.
286//
287bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
288{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000289 TIntermSymbol* symNode = node->getAsSymbolNode();
290 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000292 if (binaryNode) {
293 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 switch(binaryNode->getOp()) {
296 case EOpIndexDirect:
297 case EOpIndexIndirect:
298 case EOpIndexDirectStruct:
299 return lValueErrorCheck(line, op, binaryNode->getLeft());
300 case EOpVectorSwizzle:
301 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
302 if (!errorReturn) {
303 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000305 TIntermTyped* rightNode = binaryNode->getRight();
306 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
307
308 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
309 p != aggrNode->getSequence().end(); p++) {
310 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
311 offset[value]++;
312 if (offset[value] > 1) {
313 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000315 return true;
316 }
317 }
318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000320 return errorReturn;
321 default:
322 break;
323 }
324 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000326 return true;
327 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 const char* symbol = 0;
331 if (symNode != 0)
332 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 const char* message = 0;
335 switch (node->getQualifier()) {
336 case EvqConst: message = "can't modify a const"; break;
337 case EvqConstReadOnly: message = "can't modify a const"; break;
338 case EvqAttribute: message = "can't modify an attribute"; break;
339 case EvqUniform: message = "can't modify a uniform"; break;
340 case EvqVaryingIn: message = "can't modify a varying"; break;
341 case EvqInput: message = "can't modify an input"; break;
342 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
343 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
344 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
345 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 //
348 // Type that can't be written to?
349 //
350 switch (node->getBasicType()) {
351 case EbtSampler2D:
352 case EbtSamplerCube:
353 message = "can't modify a sampler";
354 break;
355 case EbtVoid:
356 message = "can't modify void";
357 break;
358 default:
359 break;
360 }
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 if (message == 0 && binaryNode == 0 && symNode == 0) {
364 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 return true;
367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 //
371 // Everything else is okay, no error.
372 //
373 if (message == 0)
374 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000376 //
377 // If we get here, we have an error and a message.
378 //
379 if (symNode)
380 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
381 else
382 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385}
386
387//
388// Both test, and if necessary spit out an error, to see if the node is really
389// a constant.
390//
391// Returns true if the was an error.
392//
393bool TParseContext::constErrorCheck(TIntermTyped* node)
394{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000395 if (node->getQualifier() == EvqConst)
396 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000400 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401}
402
403//
404// Both test, and if necessary spit out an error, to see if the node is really
405// an integer.
406//
407// Returns true if the was an error.
408//
409bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
410{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000411 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
412 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417}
418
419//
420// Both test, and if necessary spit out an error, to see if we are currently
421// globally scoped.
422//
423// Returns true if the was an error.
424//
425bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
426{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000427 if (global)
428 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433}
434
435//
436// For now, keep it simple: if it starts "gl_", it's reserved, independent
437// of scope. Except, if the symbol table is at the built-in push-level,
438// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000439// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
440// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000441//
442// Returns true if there was an error.
443//
444bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
445{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000446 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000447 if (!symbolTable.atBuiltInLevel()) {
448 if (identifier.substr(0, 3) == TString("gl_")) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000449 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 return true;
451 }
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000452 if (shaderSpec == SH_WEBGL_SPEC) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000453 if (identifier.substr(0, 6) == TString("webgl_")) {
454 error(line, reservedErrMsg, "webgl_", "");
455 return true;
456 }
457 if (identifier.substr(0, 7) == TString("_webgl_")) {
458 error(line, reservedErrMsg, "_webgl_", "");
459 return true;
460 }
461 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000462 if (identifier.find("__") != TString::npos) {
463 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
464 //return true;
465 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
466 return false;
467 }
468 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471}
472
473//
474// Make sure there is enough data provided to the constructor to build
475// something of the type of the constructor. Also returns the type of
476// the constructor.
477//
478// Returns true if there was an error in construction.
479//
480bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
481{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000484 bool constructingMatrix = false;
485 switch(op) {
486 case EOpConstructMat2:
487 case EOpConstructMat3:
488 case EOpConstructMat4:
489 constructingMatrix = true;
490 break;
491 default:
492 break;
493 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000495 //
496 // Note: It's okay to have too many components available, but not okay to have unused
497 // arguments. 'full' will go to true when enough args have been seen. If we loop
498 // again, there is an extra argument, so 'overfull' will become true.
499 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 int size = 0;
502 bool constType = true;
503 bool full = false;
504 bool overFull = false;
505 bool matrixInMatrix = false;
506 bool arrayArg = false;
507 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000508 const TParameter& param = function.getParam(i);
509 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000511 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000512 matrixInMatrix = true;
513 if (full)
514 overFull = true;
515 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
516 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000517 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000519 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 arrayArg = true;
521 }
522
523 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000524 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
527 error(line, "array constructor needs one argument per array element", "constructor", "");
528 return true;
529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 if (arrayArg && op != EOpConstructStruct) {
532 error(line, "constructing from a non-dereferenced array", "constructor", "");
533 return true;
534 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000537 if (function.getParamCount() != 1) {
538 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
539 return true;
540 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 if (overFull) {
544 error(line, "too many arguments", "constructor", "");
545 return true;
546 }
547
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000548 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
550 return true;
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000553 if (!type->isMatrix()) {
554 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
555 (op == EOpConstructStruct && size < type->getObjectSize())) {
556 error(line, "not enough data provided for construction", "constructor", "");
557 return true;
558 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000561 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 if (typed == 0) {
563 error(line, "constructor argument does not have a type", "constructor", "");
564 return true;
565 }
566 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
567 error(line, "cannot convert a sampler", "constructor", "");
568 return true;
569 }
570 if (typed->getBasicType() == EbtVoid) {
571 error(line, "cannot convert a void", "constructor", "");
572 return true;
573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576}
577
578// This function checks to see if a void variable has been declared and raise an error message for such a case
579//
580// returns true in case of an error
581//
582bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
583{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 if (pubType.type == EbtVoid) {
585 error(line, "illegal use of type 'void'", identifier.c_str(), "");
586 return true;
587 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590}
591
592// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
593//
594// returns true in case of an error
595//
596bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
597{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
599 error(line, "boolean expression expected", "", "");
600 return true;
601 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604}
605
606// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
607//
608// returns true in case of an error
609//
610bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
611{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
613 error(line, "boolean expression expected", "", "");
614 return true;
615 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000617 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618}
619
620bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
621{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 if (pType.type == EbtStruct) {
623 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000624 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625
626 return true;
627 }
628
629 return false;
630 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000631 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633 return true;
634 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000636 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
639bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
640{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
642 pType.type == EbtStruct) {
643 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
644
645 return true;
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
649 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
655{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
657 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
658 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
659 return true;
660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000662 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663}
664
665bool TParseContext::containsSampler(TType& type)
666{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 if (IsSampler(type.getBasicType()))
668 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000670 if (type.getBasicType() == EbtStruct) {
671 TTypeList& structure = *type.getStruct();
672 for (unsigned int i = 0; i < structure.size(); ++i) {
673 if (containsSampler(*structure[i].type))
674 return true;
675 }
676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
681//
682// Do size checking for an array type's size.
683//
684// Returns true if there was an error.
685//
686bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
687{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 TIntermConstantUnion* constant = expr->getAsConstantUnion();
689 if (constant == 0 || constant->getBasicType() != EbtInt) {
690 error(line, "array size must be a constant integer expression", "", "");
691 return true;
692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 if (size <= 0) {
697 error(line, "array size must be a positive integer", "", "");
698 size = 1;
699 return true;
700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703}
704
705//
706// See if this qualifier can be an array.
707//
708// Returns true if there is an error.
709//
710bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
711{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000712 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000713 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
714 return true;
715 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000716
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718}
719
720//
721// See if this type can be an array.
722//
723// Returns true if there is an error.
724//
725bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
726{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000727 //
728 // Can the type be an array?
729 //
730 if (type.array) {
731 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
732 return true;
733 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000735 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736}
737
738//
739// Do all the semantic checking for declaring an array, with and
740// without a size, and make the right changes to the symbol table.
741//
742// size == 0 means no specified size.
743//
744// Returns true if there was an error.
745//
746bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
747{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 //
749 // Don't check for reserved word use until after we know it's not in the symbol table,
750 // because reserved arrays can be redeclared.
751 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 bool builtIn = false;
754 bool sameScope = false;
755 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
756 if (symbol == 0 || !sameScope) {
757 if (reservedErrorCheck(line, identifier))
758 return true;
759
760 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 if (type.arraySize)
763 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000765 if (! symbolTable.insert(*variable)) {
766 delete variable;
767 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
768 return true;
769 }
770 } else {
771 if (! symbol->isVariable()) {
772 error(line, "variable expected", identifier.c_str(), "");
773 return true;
774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 variable = static_cast<TVariable*>(symbol);
777 if (! variable->getType().isArray()) {
778 error(line, "redeclaring non-array as array", identifier.c_str(), "");
779 return true;
780 }
781 if (variable->getType().getArraySize() > 0) {
782 error(line, "redeclaration of array with size", identifier.c_str(), "");
783 return true;
784 }
785
786 if (! variable->getType().sameElementType(TType(type))) {
787 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
788 return true;
789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 TType* t = variable->getArrayInformationType();
792 while (t != 0) {
793 if (t->getMaxArraySize() > type.arraySize) {
794 error(line, "higher index value already used for the array", identifier.c_str(), "");
795 return true;
796 }
797 t->setArraySize(type.arraySize);
798 t = t->getArrayInformationType();
799 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000801 if (type.arraySize)
802 variable->getType().setArraySize(type.arraySize);
803 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 if (voidErrorCheck(line, identifier, type))
806 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000808 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
811bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
812{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000813 bool builtIn = false;
814 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
815 if (symbol == 0) {
816 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
817 return true;
818 }
819 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 type->setArrayInformationType(variable->getArrayInformationType());
822 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
825 // its an error
826 if (node->getSymbol() == "gl_FragData") {
827 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
828 if (fragData == 0) {
829 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
830 return true;
831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
834 if (fragDataValue <= size) {
835 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
836 return true;
837 }
838 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
841 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
842 if (!updateFlag)
843 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 size++;
846 variable->getType().setMaxArraySize(size);
847 type->setMaxArraySize(size);
848 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 while(tt->getArrayInformationType() != 0) {
851 tt = tt->getArrayInformationType();
852 tt->setMaxArraySize(size);
853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858//
859// Enforce non-initializer type/qualifier rules.
860//
861// Returns true if there was an error.
862//
863bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
864{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000865 //
866 // Make the qualifier make sense.
867 //
868 if (type.qualifier == EvqConst) {
869 type.qualifier = EvqTemporary;
870 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
871 return true;
872 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000874 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875}
876
877//
878// Do semantic checking for a variable declaration that has no initializer,
879// and update the symbol table.
880//
881// Returns true if there was an error.
882//
883bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type)
884{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 if (reservedErrorCheck(line, identifier))
886 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000888 TVariable* variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 if (! symbolTable.insert(*variable)) {
891 error(line, "redefinition", variable->getName().c_str(), "");
892 delete variable;
893 return true;
894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 if (voidErrorCheck(line, identifier, type))
897 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900}
901
902bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
903{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 if (qualifier != EvqConst && qualifier != EvqTemporary) {
905 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
906 return true;
907 }
908 if (qualifier == EvqConst && paramQualifier != EvqIn) {
909 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
910 return true;
911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000914 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000915 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000916 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000921bool TParseContext::extensionErrorCheck(int line, const TString& extension)
922{
923 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
924 if (iter == extensionBehavior.end()) {
925 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 return true;
927 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000928 if (iter->second == EBhDisable) {
929 error(line, "extension", extension.c_str(), "is disabled");
930 return true;
931 }
932 if (iter->second == EBhWarn) {
933 TString msg = "extension " + extension + " is being used";
934 infoSink.info.message(EPrefixWarning, msg.c_str(), line);
935 return false;
936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939}
940
941/////////////////////////////////////////////////////////////////////////////////
942//
943// Non-Errors.
944//
945/////////////////////////////////////////////////////////////////////////////////
946
947//
948// Look up a function name in the symbol table, and make sure it is a function.
949//
950// Return the function symbol if found, otherwise 0.
951//
952const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
953{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000954 // First find by unmangled name to check whether the function name has been
955 // hidden by a variable name or struct typename.
956 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
957 if (symbol == 0) {
958 symbol = symbolTable.find(call->getMangledName(), builtIn);
959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
alokp@chromium.org0a576182010-08-09 17:16:27 +0000961 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000962 error(line, "no matching overloaded function found", call->getName().c_str(), "");
963 return 0;
964 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
alokp@chromium.org0a576182010-08-09 17:16:27 +0000966 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 error(line, "function name expected", call->getName().c_str(), "");
968 return 0;
969 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000970
971 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974//
975// Initializers show up in several places in the grammar. Have one set of
976// code to handle them here.
977//
978bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000979 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 if (variable == 0) {
984 if (reservedErrorCheck(line, identifier))
985 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000987 if (voidErrorCheck(line, identifier, pType))
988 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 //
991 // add variable to symbol table
992 //
993 variable = new TVariable(&identifier, type);
994 if (! symbolTable.insert(*variable)) {
995 error(line, "redefinition", variable->getName().c_str(), "");
996 return true;
997 // don't delete variable, it's used by error recovery, and the pool
998 // pop will take care of the memory
999 }
1000 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001002 //
1003 // identifier must be of type constant, a global, or a temporary
1004 //
1005 TQualifier qualifier = variable->getType().getQualifier();
1006 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1007 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1008 return true;
1009 }
1010 //
1011 // test for and propagate constant
1012 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001014 if (qualifier == EvqConst) {
1015 if (qualifier != initializer->getType().getQualifier()) {
1016 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001017 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001018 return true;
1019 }
1020 if (type != initializer->getType()) {
1021 error(line, " non-matching types for const initializer ",
1022 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001023 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 return true;
1025 }
1026 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001027 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1030 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1031 } else {
1032 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1033 }
1034 } else if (initializer->getAsSymbolNode()) {
1035 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1036 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001038 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001039 variable->shareConstPointer(constArray);
1040 } else {
1041 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001042 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001043 return true;
1044 }
1045 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001047 if (qualifier != EvqConst) {
1048 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1049 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1050 if (intermNode == 0) {
1051 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1052 return true;
1053 }
1054 } else
1055 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001057 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058}
1059
1060bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1061{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001062 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001063 if (!aggrNode->isConstructor())
1064 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 // check if all the child nodes are constants so that they can be inserted into
1069 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001070 TIntermSequence &sequence = aggrNode->getSequence() ;
1071 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1072 if (!(*p)->getAsTyped()->getAsConstantUnion())
1073 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001076 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
1079// This function is used to test for the correctness of the parameters passed to various constructor functions
1080// and also convert them to the right datatype if it is allowed and required.
1081//
1082// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1083//
1084TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1085{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001086 if (node == 0)
1087 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 TIntermAggregate* aggrNode = node->getAsAggregate();
1090
alokp@chromium.org58e54292010-08-24 21:40:03 +00001091 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001092 if (op == EOpConstructStruct)
1093 memberTypes = type->getStruct()->begin();
1094
1095 TType elementType = *type;
1096 if (type->isArray())
1097 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 bool singleArg;
1100 if (aggrNode) {
1101 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1102 singleArg = true;
1103 else
1104 singleArg = false;
1105 } else
1106 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 TIntermTyped *newNode;
1109 if (singleArg) {
1110 // If structure constructor or array constructor is being called
1111 // for only one parameter inside the structure, we need to call constructStruct function once.
1112 if (type->isArray())
1113 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1114 else if (op == EOpConstructStruct)
1115 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1116 else
1117 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 if (newNode && newNode->getAsAggregate()) {
1120 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1121 if (constConstructor)
1122 return constConstructor;
1123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 return newNode;
1126 }
1127
1128 //
1129 // Handle list of arguments.
1130 //
1131 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1132 // if the structure constructor contains more than one parameter, then construct
1133 // each parameter
1134
1135 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1136
1137 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1138 // to the right type if possible (and allowed).
1139 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1140
1141 for (TIntermSequence::iterator p = sequenceVector.begin();
1142 p != sequenceVector.end(); p++, paramCount++) {
1143 if (type->isArray())
1144 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1145 else if (op == EOpConstructStruct)
1146 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1147 else
1148 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1149
1150 if (newNode) {
1151 *p = newNode;
1152 }
1153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001155 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1156 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1157 if (constConstructor)
1158 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161}
1162
1163TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1164{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 bool canBeFolded = areAllChildConst(aggrNode);
1166 aggrNode->setType(type);
1167 if (canBeFolded) {
1168 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001169 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 if (aggrNode->getSequence().size() == 1) {
1171 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1172 }
1173 else {
1174 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1175 }
1176 if (returnVal)
1177 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001182 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183}
1184
1185// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1186// for the parameter to the constructor (passed to this function). Essentially, it converts
1187// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1188// float, then float is converted to int.
1189//
1190// Returns 0 for an error or the constructed node.
1191//
1192TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1193{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001194 TIntermTyped* newNode;
1195 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001197 //
1198 // First, convert types as needed.
1199 //
1200 switch (op) {
1201 case EOpConstructVec2:
1202 case EOpConstructVec3:
1203 case EOpConstructVec4:
1204 case EOpConstructMat2:
1205 case EOpConstructMat3:
1206 case EOpConstructMat4:
1207 case EOpConstructFloat:
1208 basicOp = EOpConstructFloat;
1209 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001211 case EOpConstructIVec2:
1212 case EOpConstructIVec3:
1213 case EOpConstructIVec4:
1214 case EOpConstructInt:
1215 basicOp = EOpConstructInt;
1216 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001217
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001218 case EOpConstructBVec2:
1219 case EOpConstructBVec3:
1220 case EOpConstructBVec4:
1221 case EOpConstructBool:
1222 basicOp = EOpConstructBool;
1223 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001224
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001225 default:
1226 error(line, "unsupported construction", "", "");
1227 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001229 return 0;
1230 }
1231 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1232 if (newNode == 0) {
1233 error(line, "can't convert", "constructor", "");
1234 return 0;
1235 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001237 //
1238 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1239 //
1240
1241 // Otherwise, skip out early.
1242 if (subset || (newNode != node && newNode->getType() == *type))
1243 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001245 // setAggregateOperator will insert a new node for the constructor, as needed.
1246 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247}
1248
1249// This function tests for the type of the parameters to the structures constructors. Raises
1250// an error message if the expected type does not match the parameter passed to the constructor.
1251//
1252// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1253//
1254TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1255{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001256 if (*type == node->getAsTyped()->getType()) {
1257 if (subset)
1258 return node->getAsTyped();
1259 else
1260 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1261 } else {
1262 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1263 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1264 recover();
1265 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001266
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001267 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268}
1269
1270//
1271// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1272// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1273// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1274// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1275// a constant matrix.
1276//
1277TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1278{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001279 TIntermTyped* typedNode;
1280 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001281
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001282 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001283 if (tempConstantNode) {
1284 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001285
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001287 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001288 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001290 return node;
1291 }
1292 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1293 error(line, "Cannot offset into the vector", "Error", "");
1294 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001296 return 0;
1297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001299 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001301 for (int i = 0; i < fields.num; i++) {
1302 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1303 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1304 recover();
1305 fields.offsets[i] = 0;
1306 }
1307
1308 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001310 }
1311 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1312 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313}
1314
1315//
1316// This function returns the column being accessed from a constant matrix. The values are retrieved from
1317// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1318// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1319// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1320//
1321TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1322{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 TIntermTyped* typedNode;
1324 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001326 if (index >= node->getType().getNominalSize()) {
1327 error(line, "", "[", "matrix field selection out of range '%d'", index);
1328 recover();
1329 index = 0;
1330 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001331
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001332 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001333 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001334 int size = tempConstantNode->getType().getNominalSize();
1335 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1336 } else {
1337 error(line, "Cannot offset into the matrix", "Error", "");
1338 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001340 return 0;
1341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344}
1345
1346
1347//
1348// This function returns an element of an array accessed from a constant array. The values are retrieved from
1349// the symbol table and parse-tree is built for the type of the element. The input
1350// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1351// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1352//
1353TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1354{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001355 TIntermTyped* typedNode;
1356 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1357 TType arrayElementType = node->getType();
1358 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001360 if (index >= node->getType().getArraySize()) {
1361 error(line, "", "[", "array field selection out of range '%d'", index);
1362 recover();
1363 index = 0;
1364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001366 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001368 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001369 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001370 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1371 } else {
1372 error(line, "Cannot offset into the array", "Error", "");
1373 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return 0;
1376 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379}
1380
1381
1382//
1383// This function returns the value of a particular field inside a constant structure from the symbol table.
1384// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1385// function and returns the parse-tree with the values of the embedded/nested struct.
1386//
1387TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1388{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001389 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001390 TIntermTyped *typedNode;
1391 int instanceSize = 0;
1392 unsigned int index = 0;
1393 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001394
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001395 for ( index = 0; index < fields->size(); ++index) {
1396 if ((*fields)[index].type->getFieldName() == identifier) {
1397 break;
1398 } else {
1399 instanceSize += (*fields)[index].type->getObjectSize();
1400 }
1401 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001403 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001404 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1407 } else {
1408 error(line, "Cannot offset into the structure", "Error", "");
1409 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001411 return 0;
1412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415}
1416
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001417//
1418// Parse an array of strings using yyparse.
1419//
1420// Returns 0 for success.
1421//
1422int PaParseStrings(int count, const char* const string[], const int length[],
1423 TParseContext* context) {
1424 if ((count == 0) || (string == NULL))
1425 return 1;
1426
1427 // setup preprocessor.
1428 if (InitPreprocessor())
1429 return 1;
1430 DefineExtensionMacros(context->extensionBehavior);
1431
1432 if (glslang_initialize(context))
1433 return 1;
1434
1435 glslang_scan(count, string, length, context);
1436 int error = glslang_parse(context);
1437
1438 glslang_finalize(context);
1439 FinalizePreprocessor();
1440 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1441}
1442
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1444
1445bool InitializeParseContextIndex()
1446{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001447 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1448 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1449 return false;
1450 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001451
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001452 //
1453 // Allocate a TLS index.
1454 //
1455 GlobalParseContextIndex = OS_AllocTLSIndex();
1456
1457 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1458 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1459 return false;
1460 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001461
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001462 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463}
1464
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001465bool FreeParseContextIndex()
1466{
1467 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
1468
1469 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1470 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1471 return false;
1472 }
1473
1474 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1475
1476 return OS_FreeTLSIndex(tlsiIndex);
1477}
1478
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001479bool InitializeGlobalParseContext()
1480{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001481 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1482 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1483 return false;
1484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001486 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1487 if (lpParseContext != 0) {
1488 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1489 return false;
1490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001491
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001492 TThreadParseContext *lpThreadData = new TThreadParseContext();
1493 if (lpThreadData == 0) {
1494 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1495 return false;
1496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001498 lpThreadData->lpGlobalParseContext = 0;
1499 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001501 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502}
1503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504bool FreeParseContext()
1505{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001506 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1507 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1508 return false;
1509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001511 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1512 if (lpParseContext)
1513 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001514
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001515 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516}
1517
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001518TParseContextPointer& GetGlobalParseContext()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001520 //
1521 // Minimal error checking for speed
1522 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001524 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001526 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001528