blob: 3f0836d752df21aa7321fa70273e404b9214edfe [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){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000264 if (!checksPrecisionErrors)
265 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000266 switch( type ){
267 case EbtFloat:
268 if( precision == EbpUndefined ){
269 error( line, "No precision specified for (float)", "", "" );
270 return true;
271 }
272 break;
273 case EbtInt:
274 if( precision == EbpUndefined ){
275 error( line, "No precision specified (int)", "", "" );
276 return true;
277 }
278 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000279 default:
280 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000281 }
282 return false;
283}
284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285//
286// Both test and if necessary, spit out an error, to see if the node is really
287// an l-value that can be operated on this way.
288//
289// Returns true if the was an error.
290//
291bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
292{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 TIntermSymbol* symNode = node->getAsSymbolNode();
294 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000296 if (binaryNode) {
297 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 switch(binaryNode->getOp()) {
300 case EOpIndexDirect:
301 case EOpIndexIndirect:
302 case EOpIndexDirectStruct:
303 return lValueErrorCheck(line, op, binaryNode->getLeft());
304 case EOpVectorSwizzle:
305 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
306 if (!errorReturn) {
307 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 TIntermTyped* rightNode = binaryNode->getRight();
310 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
311
312 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
313 p != aggrNode->getSequence().end(); p++) {
314 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
315 offset[value]++;
316 if (offset[value] > 1) {
317 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000319 return true;
320 }
321 }
322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 return errorReturn;
325 default:
326 break;
327 }
328 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return true;
331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332
333
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 const char* symbol = 0;
335 if (symNode != 0)
336 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 const char* message = 0;
339 switch (node->getQualifier()) {
340 case EvqConst: message = "can't modify a const"; break;
341 case EvqConstReadOnly: message = "can't modify a const"; break;
342 case EvqAttribute: message = "can't modify an attribute"; break;
343 case EvqUniform: message = "can't modify a uniform"; break;
344 case EvqVaryingIn: message = "can't modify a varying"; break;
345 case EvqInput: message = "can't modify an input"; break;
346 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
347 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
348 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
349 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 //
352 // Type that can't be written to?
353 //
354 switch (node->getBasicType()) {
355 case EbtSampler2D:
356 case EbtSamplerCube:
357 message = "can't modify a sampler";
358 break;
359 case EbtVoid:
360 message = "can't modify void";
361 break;
362 default:
363 break;
364 }
365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000367 if (message == 0 && binaryNode == 0 && symNode == 0) {
368 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 return true;
371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
373
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000374 //
375 // Everything else is okay, no error.
376 //
377 if (message == 0)
378 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000380 //
381 // If we get here, we have an error and a message.
382 //
383 if (symNode)
384 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
385 else
386 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389}
390
391//
392// Both test, and if necessary spit out an error, to see if the node is really
393// a constant.
394//
395// Returns true if the was an error.
396//
397bool TParseContext::constErrorCheck(TIntermTyped* node)
398{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 if (node->getQualifier() == EvqConst)
400 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Both test, and if necessary spit out an error, to see if the node is really
409// an integer.
410//
411// Returns true if the was an error.
412//
413bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
414{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
416 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423//
424// Both test, and if necessary spit out an error, to see if we are currently
425// globally scoped.
426//
427// Returns true if the was an error.
428//
429bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
430{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 if (global)
432 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439//
440// For now, keep it simple: if it starts "gl_", it's reserved, independent
441// of scope. Except, if the symbol table is at the built-in push-level,
442// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
444// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445//
446// Returns true if there was an error.
447//
448bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
449{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000450 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 if (!symbolTable.atBuiltInLevel()) {
452 if (identifier.substr(0, 3) == TString("gl_")) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000453 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 return true;
455 }
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000456 if (shaderSpec == SH_WEBGL_SPEC) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000457 if (identifier.substr(0, 6) == TString("webgl_")) {
458 error(line, reservedErrMsg, "webgl_", "");
459 return true;
460 }
461 if (identifier.substr(0, 7) == TString("_webgl_")) {
462 error(line, reservedErrMsg, "_webgl_", "");
463 return true;
464 }
465 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 if (identifier.find("__") != TString::npos) {
467 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
468 //return true;
469 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
470 return false;
471 }
472 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475}
476
477//
478// Make sure there is enough data provided to the constructor to build
479// something of the type of the constructor. Also returns the type of
480// the constructor.
481//
482// Returns true if there was an error in construction.
483//
484bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
485{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000488 bool constructingMatrix = false;
489 switch(op) {
490 case EOpConstructMat2:
491 case EOpConstructMat3:
492 case EOpConstructMat4:
493 constructingMatrix = true;
494 break;
495 default:
496 break;
497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000499 //
500 // Note: It's okay to have too many components available, but not okay to have unused
501 // arguments. 'full' will go to true when enough args have been seen. If we loop
502 // again, there is an extra argument, so 'overfull' will become true.
503 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 int size = 0;
506 bool constType = true;
507 bool full = false;
508 bool overFull = false;
509 bool matrixInMatrix = false;
510 bool arrayArg = false;
511 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000512 const TParameter& param = function.getParam(i);
513 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000515 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 matrixInMatrix = true;
517 if (full)
518 overFull = true;
519 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
520 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000521 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000523 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 arrayArg = true;
525 }
526
527 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000528 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
531 error(line, "array constructor needs one argument per array element", "constructor", "");
532 return true;
533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 if (arrayArg && op != EOpConstructStruct) {
536 error(line, "constructing from a non-dereferenced array", "constructor", "");
537 return true;
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000541 if (function.getParamCount() != 1) {
542 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
543 return true;
544 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 if (overFull) {
548 error(line, "too many arguments", "constructor", "");
549 return true;
550 }
551
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000552 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
554 return true;
555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000557 if (!type->isMatrix()) {
558 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
559 (op == EOpConstructStruct && size < type->getObjectSize())) {
560 error(line, "not enough data provided for construction", "constructor", "");
561 return true;
562 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000565 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 if (typed == 0) {
567 error(line, "constructor argument does not have a type", "constructor", "");
568 return true;
569 }
570 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
571 error(line, "cannot convert a sampler", "constructor", "");
572 return true;
573 }
574 if (typed->getBasicType() == EbtVoid) {
575 error(line, "cannot convert a void", "constructor", "");
576 return true;
577 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580}
581
582// This function checks to see if a void variable has been declared and raise an error message for such a case
583//
584// returns true in case of an error
585//
586bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
587{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 if (pubType.type == EbtVoid) {
589 error(line, "illegal use of type 'void'", identifier.c_str(), "");
590 return true;
591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
596// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
597//
598// returns true in case of an error
599//
600bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
601{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
603 error(line, "boolean expression expected", "", "");
604 return true;
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
610// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
611//
612// returns true in case of an error
613//
614bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
615{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
617 error(line, "boolean expression expected", "", "");
618 return true;
619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622}
623
624bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
625{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 if (pType.type == EbtStruct) {
627 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000628 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629
630 return true;
631 }
632
633 return false;
634 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000635 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000637 return true;
638 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641}
642
643bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
644{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
646 pType.type == EbtStruct) {
647 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
648
649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
653 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000655 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
658bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
659{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
661 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
662 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
663 return true;
664 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667}
668
669bool TParseContext::containsSampler(TType& type)
670{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 if (IsSampler(type.getBasicType()))
672 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 if (type.getBasicType() == EbtStruct) {
675 TTypeList& structure = *type.getStruct();
676 for (unsigned int i = 0; i < structure.size(); ++i) {
677 if (containsSampler(*structure[i].type))
678 return true;
679 }
680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683}
684
685//
686// Do size checking for an array type's size.
687//
688// Returns true if there was an error.
689//
690bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
691{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000692 TIntermConstantUnion* constant = expr->getAsConstantUnion();
693 if (constant == 0 || constant->getBasicType() != EbtInt) {
694 error(line, "array size must be a constant integer expression", "", "");
695 return true;
696 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000700 if (size <= 0) {
701 error(line, "array size must be a positive integer", "", "");
702 size = 1;
703 return true;
704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000706 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707}
708
709//
710// See if this qualifier can be an array.
711//
712// Returns true if there is an error.
713//
714bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
715{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000716 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
718 return true;
719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000721 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722}
723
724//
725// See if this type can be an array.
726//
727// Returns true if there is an error.
728//
729bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
730{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 //
732 // Can the type be an array?
733 //
734 if (type.array) {
735 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
736 return true;
737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000739 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740}
741
742//
743// Do all the semantic checking for declaring an array, with and
744// without a size, and make the right changes to the symbol table.
745//
746// size == 0 means no specified size.
747//
748// Returns true if there was an error.
749//
750bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
751{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000752 //
753 // Don't check for reserved word use until after we know it's not in the symbol table,
754 // because reserved arrays can be redeclared.
755 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 bool builtIn = false;
758 bool sameScope = false;
759 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
760 if (symbol == 0 || !sameScope) {
761 if (reservedErrorCheck(line, identifier))
762 return true;
763
764 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000766 if (type.arraySize)
767 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 if (! symbolTable.insert(*variable)) {
770 delete variable;
771 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
772 return true;
773 }
774 } else {
775 if (! symbol->isVariable()) {
776 error(line, "variable expected", identifier.c_str(), "");
777 return true;
778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 variable = static_cast<TVariable*>(symbol);
781 if (! variable->getType().isArray()) {
782 error(line, "redeclaring non-array as array", identifier.c_str(), "");
783 return true;
784 }
785 if (variable->getType().getArraySize() > 0) {
786 error(line, "redeclaration of array with size", identifier.c_str(), "");
787 return true;
788 }
789
790 if (! variable->getType().sameElementType(TType(type))) {
791 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
792 return true;
793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 TType* t = variable->getArrayInformationType();
796 while (t != 0) {
797 if (t->getMaxArraySize() > type.arraySize) {
798 error(line, "higher index value already used for the array", identifier.c_str(), "");
799 return true;
800 }
801 t->setArraySize(type.arraySize);
802 t = t->getArrayInformationType();
803 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 if (type.arraySize)
806 variable->getType().setArraySize(type.arraySize);
807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 if (voidErrorCheck(line, identifier, type))
810 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
815bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
816{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 bool builtIn = false;
818 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
819 if (symbol == 0) {
820 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
821 return true;
822 }
823 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000825 type->setArrayInformationType(variable->getArrayInformationType());
826 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
829 // its an error
830 if (node->getSymbol() == "gl_FragData") {
831 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
832 if (fragData == 0) {
833 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
834 return true;
835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
838 if (fragDataValue <= size) {
839 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
840 return true;
841 }
842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
845 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
846 if (!updateFlag)
847 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 size++;
850 variable->getType().setMaxArraySize(size);
851 type->setMaxArraySize(size);
852 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 while(tt->getArrayInformationType() != 0) {
855 tt = tt->getArrayInformationType();
856 tt->setMaxArraySize(size);
857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000859 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862//
863// Enforce non-initializer type/qualifier rules.
864//
865// Returns true if there was an error.
866//
867bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
868{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 //
870 // Make the qualifier make sense.
871 //
872 if (type.qualifier == EvqConst) {
873 type.qualifier = EvqTemporary;
874 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
875 return true;
876 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
881//
882// Do semantic checking for a variable declaration that has no initializer,
883// and update the symbol table.
884//
885// Returns true if there was an error.
886//
zmo@google.comfd747b82011-04-23 01:30:07 +0000887bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 if (reservedErrorCheck(line, identifier))
890 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891
zmo@google.comfd747b82011-04-23 01:30:07 +0000892 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 if (! symbolTable.insert(*variable)) {
895 error(line, "redefinition", variable->getName().c_str(), "");
896 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000897 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 return true;
899 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000901 if (voidErrorCheck(line, identifier, type))
902 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
906
907bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
908{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 if (qualifier != EvqConst && qualifier != EvqTemporary) {
910 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
911 return true;
912 }
913 if (qualifier == EvqConst && paramQualifier != EvqIn) {
914 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
915 return true;
916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000919 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000920 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000921 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000923 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924}
925
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000926bool TParseContext::extensionErrorCheck(int line, const TString& extension)
927{
928 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
929 if (iter == extensionBehavior.end()) {
930 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 return true;
932 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000933 if (iter->second == EBhDisable) {
934 error(line, "extension", extension.c_str(), "is disabled");
935 return true;
936 }
937 if (iter->second == EBhWarn) {
938 TString msg = "extension " + extension + " is being used";
939 infoSink.info.message(EPrefixWarning, msg.c_str(), line);
940 return false;
941 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000943 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
946/////////////////////////////////////////////////////////////////////////////////
947//
948// Non-Errors.
949//
950/////////////////////////////////////////////////////////////////////////////////
951
952//
953// Look up a function name in the symbol table, and make sure it is a function.
954//
955// Return the function symbol if found, otherwise 0.
956//
957const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
958{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000959 // First find by unmangled name to check whether the function name has been
960 // hidden by a variable name or struct typename.
961 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
962 if (symbol == 0) {
963 symbol = symbolTable.find(call->getMangledName(), builtIn);
964 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
alokp@chromium.org0a576182010-08-09 17:16:27 +0000966 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 error(line, "no matching overloaded function found", call->getName().c_str(), "");
968 return 0;
969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
alokp@chromium.org0a576182010-08-09 17:16:27 +0000971 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 error(line, "function name expected", call->getName().c_str(), "");
973 return 0;
974 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000975
976 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979//
980// Initializers show up in several places in the grammar. Have one set of
981// code to handle them here.
982//
983bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000988 if (variable == 0) {
989 if (reservedErrorCheck(line, identifier))
990 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 if (voidErrorCheck(line, identifier, pType))
993 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000995 //
996 // add variable to symbol table
997 //
998 variable = new TVariable(&identifier, type);
999 if (! symbolTable.insert(*variable)) {
1000 error(line, "redefinition", variable->getName().c_str(), "");
1001 return true;
1002 // don't delete variable, it's used by error recovery, and the pool
1003 // pop will take care of the memory
1004 }
1005 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 //
1008 // identifier must be of type constant, a global, or a temporary
1009 //
1010 TQualifier qualifier = variable->getType().getQualifier();
1011 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1012 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1013 return true;
1014 }
1015 //
1016 // test for and propagate constant
1017 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001019 if (qualifier == EvqConst) {
1020 if (qualifier != initializer->getType().getQualifier()) {
1021 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001022 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 return true;
1024 }
1025 if (type != initializer->getType()) {
1026 error(line, " non-matching types for const initializer ",
1027 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001028 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 return true;
1030 }
1031 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001032 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001034 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1035 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1036 } else {
1037 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1038 }
1039 } else if (initializer->getAsSymbolNode()) {
1040 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1041 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001043 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001044 variable->shareConstPointer(constArray);
1045 } else {
1046 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001047 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 return true;
1049 }
1050 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001052 if (qualifier != EvqConst) {
1053 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1054 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1055 if (intermNode == 0) {
1056 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1057 return true;
1058 }
1059 } else
1060 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063}
1064
1065bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1066{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001067 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 if (!aggrNode->isConstructor())
1069 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001071 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 // check if all the child nodes are constants so that they can be inserted into
1074 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001075 TIntermSequence &sequence = aggrNode->getSequence() ;
1076 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1077 if (!(*p)->getAsTyped()->getAsConstantUnion())
1078 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001081 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082}
1083
1084// This function is used to test for the correctness of the parameters passed to various constructor functions
1085// and also convert them to the right datatype if it is allowed and required.
1086//
1087// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1088//
1089TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1090{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 if (node == 0)
1092 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 TIntermAggregate* aggrNode = node->getAsAggregate();
1095
alokp@chromium.org58e54292010-08-24 21:40:03 +00001096 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001097 if (op == EOpConstructStruct)
1098 memberTypes = type->getStruct()->begin();
1099
1100 TType elementType = *type;
1101 if (type->isArray())
1102 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 bool singleArg;
1105 if (aggrNode) {
1106 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1107 singleArg = true;
1108 else
1109 singleArg = false;
1110 } else
1111 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 TIntermTyped *newNode;
1114 if (singleArg) {
1115 // If structure constructor or array constructor is being called
1116 // for only one parameter inside the structure, we need to call constructStruct function once.
1117 if (type->isArray())
1118 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1119 else if (op == EOpConstructStruct)
1120 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1121 else
1122 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 if (newNode && newNode->getAsAggregate()) {
1125 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1126 if (constConstructor)
1127 return constConstructor;
1128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 return newNode;
1131 }
1132
1133 //
1134 // Handle list of arguments.
1135 //
1136 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1137 // if the structure constructor contains more than one parameter, then construct
1138 // each parameter
1139
1140 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1141
1142 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1143 // to the right type if possible (and allowed).
1144 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1145
1146 for (TIntermSequence::iterator p = sequenceVector.begin();
1147 p != sequenceVector.end(); p++, paramCount++) {
1148 if (type->isArray())
1149 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1150 else if (op == EOpConstructStruct)
1151 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1152 else
1153 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1154
1155 if (newNode) {
1156 *p = newNode;
1157 }
1158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1161 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1162 if (constConstructor)
1163 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001166}
1167
1168TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1169{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 bool canBeFolded = areAllChildConst(aggrNode);
1171 aggrNode->setType(type);
1172 if (canBeFolded) {
1173 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001174 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 if (aggrNode->getSequence().size() == 1) {
1176 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1177 }
1178 else {
1179 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1180 }
1181 if (returnVal)
1182 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001184 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001187 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001188}
1189
1190// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1191// for the parameter to the constructor (passed to this function). Essentially, it converts
1192// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1193// float, then float is converted to int.
1194//
1195// Returns 0 for an error or the constructed node.
1196//
1197TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1198{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 TIntermTyped* newNode;
1200 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001201
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001202 //
1203 // First, convert types as needed.
1204 //
1205 switch (op) {
1206 case EOpConstructVec2:
1207 case EOpConstructVec3:
1208 case EOpConstructVec4:
1209 case EOpConstructMat2:
1210 case EOpConstructMat3:
1211 case EOpConstructMat4:
1212 case EOpConstructFloat:
1213 basicOp = EOpConstructFloat;
1214 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001215
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001216 case EOpConstructIVec2:
1217 case EOpConstructIVec3:
1218 case EOpConstructIVec4:
1219 case EOpConstructInt:
1220 basicOp = EOpConstructInt;
1221 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001223 case EOpConstructBVec2:
1224 case EOpConstructBVec3:
1225 case EOpConstructBVec4:
1226 case EOpConstructBool:
1227 basicOp = EOpConstructBool;
1228 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001230 default:
1231 error(line, "unsupported construction", "", "");
1232 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001233
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001234 return 0;
1235 }
1236 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1237 if (newNode == 0) {
1238 error(line, "can't convert", "constructor", "");
1239 return 0;
1240 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001241
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001242 //
1243 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1244 //
1245
1246 // Otherwise, skip out early.
1247 if (subset || (newNode != node && newNode->getType() == *type))
1248 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001250 // setAggregateOperator will insert a new node for the constructor, as needed.
1251 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001252}
1253
1254// This function tests for the type of the parameters to the structures constructors. Raises
1255// an error message if the expected type does not match the parameter passed to the constructor.
1256//
1257// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1258//
1259TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1260{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001261 if (*type == node->getAsTyped()->getType()) {
1262 if (subset)
1263 return node->getAsTyped();
1264 else
1265 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1266 } else {
1267 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1268 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1269 recover();
1270 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001273}
1274
1275//
1276// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1277// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1278// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1279// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1280// a constant matrix.
1281//
1282TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1283{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001284 TIntermTyped* typedNode;
1285 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001287 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001288 if (tempConstantNode) {
1289 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001290
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001291 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001292 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001293 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001295 return node;
1296 }
1297 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1298 error(line, "Cannot offset into the vector", "Error", "");
1299 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001301 return 0;
1302 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001304 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 for (int i = 0; i < fields.num; i++) {
1307 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1308 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1309 recover();
1310 fields.offsets[i] = 0;
1311 }
1312
1313 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001314
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 }
1316 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1317 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318}
1319
1320//
1321// This function returns the column being accessed from a constant matrix. The values are retrieved from
1322// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1323// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1324// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1325//
1326TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1327{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001328 TIntermTyped* typedNode;
1329 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001331 if (index >= node->getType().getNominalSize()) {
1332 error(line, "", "[", "matrix field selection out of range '%d'", index);
1333 recover();
1334 index = 0;
1335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001338 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001339 int size = tempConstantNode->getType().getNominalSize();
1340 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1341 } else {
1342 error(line, "Cannot offset into the matrix", "Error", "");
1343 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001345 return 0;
1346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001348 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001349}
1350
1351
1352//
1353// This function returns an element of an array accessed from a constant array. The values are retrieved from
1354// the symbol table and parse-tree is built for the type of the element. The input
1355// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1356// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1357//
1358TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1359{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001360 TIntermTyped* typedNode;
1361 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1362 TType arrayElementType = node->getType();
1363 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001364
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001365 if (index >= node->getType().getArraySize()) {
1366 error(line, "", "[", "array field selection out of range '%d'", index);
1367 recover();
1368 index = 0;
1369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001371 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001372
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001373 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001374 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1376 } else {
1377 error(line, "Cannot offset into the array", "Error", "");
1378 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001380 return 0;
1381 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001383 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001384}
1385
1386
1387//
1388// This function returns the value of a particular field inside a constant structure from the symbol table.
1389// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1390// function and returns the parse-tree with the values of the embedded/nested struct.
1391//
1392TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1393{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001394 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001395 TIntermTyped *typedNode;
1396 int instanceSize = 0;
1397 unsigned int index = 0;
1398 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001400 for ( index = 0; index < fields->size(); ++index) {
1401 if ((*fields)[index].type->getFieldName() == identifier) {
1402 break;
1403 } else {
1404 instanceSize += (*fields)[index].type->getObjectSize();
1405 }
1406 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001409 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001411 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1412 } else {
1413 error(line, "Cannot offset into the structure", "Error", "");
1414 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001416 return 0;
1417 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001419 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420}
1421
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001422//
1423// Parse an array of strings using yyparse.
1424//
1425// Returns 0 for success.
1426//
1427int PaParseStrings(int count, const char* const string[], const int length[],
1428 TParseContext* context) {
1429 if ((count == 0) || (string == NULL))
1430 return 1;
1431
1432 // setup preprocessor.
1433 if (InitPreprocessor())
1434 return 1;
1435 DefineExtensionMacros(context->extensionBehavior);
1436
1437 if (glslang_initialize(context))
1438 return 1;
1439
1440 glslang_scan(count, string, length, context);
1441 int error = glslang_parse(context);
1442
1443 glslang_finalize(context);
1444 FinalizePreprocessor();
1445 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1446}
1447
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001448OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1449
1450bool InitializeParseContextIndex()
1451{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001452 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1453 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1454 return false;
1455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001457 //
1458 // Allocate a TLS index.
1459 //
1460 GlobalParseContextIndex = OS_AllocTLSIndex();
1461
1462 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1463 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1464 return false;
1465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001467 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001468}
1469
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001470bool FreeParseContextIndex()
1471{
1472 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
1473
1474 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1475 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1476 return false;
1477 }
1478
1479 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1480
1481 return OS_FreeTLSIndex(tlsiIndex);
1482}
1483
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484bool InitializeGlobalParseContext()
1485{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001486 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1487 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1488 return false;
1489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001491 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1492 if (lpParseContext != 0) {
1493 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1494 return false;
1495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001496
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001497 TThreadParseContext *lpThreadData = new TThreadParseContext();
1498 if (lpThreadData == 0) {
1499 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1500 return false;
1501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001503 lpThreadData->lpGlobalParseContext = 0;
1504 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001506 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001507}
1508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509bool FreeParseContext()
1510{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001511 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1512 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1513 return false;
1514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001516 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1517 if (lpParseContext)
1518 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001520 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001521}
1522
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001523TParseContextPointer& GetGlobalParseContext()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001525 //
1526 // Minimal error checking for speed
1527 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001528
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001529 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001531 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001533