blob: d5f4e3c41f34ce4034249a23bcd97a5f1732ec57 [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;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000277 default:
278 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000279 }
280 return false;
281}
282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283//
284// Both test and if necessary, spit out an error, to see if the node is really
285// an l-value that can be operated on this way.
286//
287// Returns true if the was an error.
288//
289bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
290{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000291 TIntermSymbol* symNode = node->getAsSymbolNode();
292 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 if (binaryNode) {
295 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 switch(binaryNode->getOp()) {
298 case EOpIndexDirect:
299 case EOpIndexIndirect:
300 case EOpIndexDirectStruct:
301 return lValueErrorCheck(line, op, binaryNode->getLeft());
302 case EOpVectorSwizzle:
303 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
304 if (!errorReturn) {
305 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000307 TIntermTyped* rightNode = binaryNode->getRight();
308 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
309
310 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
311 p != aggrNode->getSequence().end(); p++) {
312 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
313 offset[value]++;
314 if (offset[value] > 1) {
315 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 return true;
318 }
319 }
320 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 return errorReturn;
323 default:
324 break;
325 }
326 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 return true;
329 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
331
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000332 const char* symbol = 0;
333 if (symNode != 0)
334 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000336 const char* message = 0;
337 switch (node->getQualifier()) {
338 case EvqConst: message = "can't modify a const"; break;
339 case EvqConstReadOnly: message = "can't modify a const"; break;
340 case EvqAttribute: message = "can't modify an attribute"; break;
341 case EvqUniform: message = "can't modify a uniform"; break;
342 case EvqVaryingIn: message = "can't modify a varying"; break;
343 case EvqInput: message = "can't modify an input"; break;
344 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
345 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
346 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
347 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000349 //
350 // Type that can't be written to?
351 //
352 switch (node->getBasicType()) {
353 case EbtSampler2D:
354 case EbtSamplerCube:
355 message = "can't modify a sampler";
356 break;
357 case EbtVoid:
358 message = "can't modify void";
359 break;
360 default:
361 break;
362 }
363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000365 if (message == 0 && binaryNode == 0 && symNode == 0) {
366 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 return true;
369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
371
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 //
373 // Everything else is okay, no error.
374 //
375 if (message == 0)
376 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000378 //
379 // If we get here, we have an error and a message.
380 //
381 if (symNode)
382 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
383 else
384 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000386 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389//
390// Both test, and if necessary spit out an error, to see if the node is really
391// a constant.
392//
393// Returns true if the was an error.
394//
395bool TParseContext::constErrorCheck(TIntermTyped* node)
396{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000397 if (node->getQualifier() == EvqConst)
398 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000400 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Both test, and if necessary spit out an error, to see if the node is really
407// an integer.
408//
409// Returns true if the was an error.
410//
411bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
412{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000413 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
414 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
421//
422// Both test, and if necessary spit out an error, to see if we are currently
423// globally scoped.
424//
425// Returns true if the was an error.
426//
427bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
428{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000429 if (global)
430 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435}
436
437//
438// For now, keep it simple: if it starts "gl_", it's reserved, independent
439// of scope. Except, if the symbol table is at the built-in push-level,
440// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000441// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
442// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443//
444// Returns true if there was an error.
445//
446bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
447{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000448 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000449 if (!symbolTable.atBuiltInLevel()) {
450 if (identifier.substr(0, 3) == TString("gl_")) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000451 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 return true;
453 }
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000454 if (shaderSpec == SH_WEBGL_SPEC) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000455 if (identifier.substr(0, 6) == TString("webgl_")) {
456 error(line, reservedErrMsg, "webgl_", "");
457 return true;
458 }
459 if (identifier.substr(0, 7) == TString("_webgl_")) {
460 error(line, reservedErrMsg, "_webgl_", "");
461 return true;
462 }
463 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000464 if (identifier.find("__") != TString::npos) {
465 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
466 //return true;
467 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
468 return false;
469 }
470 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473}
474
475//
476// Make sure there is enough data provided to the constructor to build
477// something of the type of the constructor. Also returns the type of
478// the constructor.
479//
480// Returns true if there was an error in construction.
481//
482bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
483{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000484 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 bool constructingMatrix = false;
487 switch(op) {
488 case EOpConstructMat2:
489 case EOpConstructMat3:
490 case EOpConstructMat4:
491 constructingMatrix = true;
492 break;
493 default:
494 break;
495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497 //
498 // Note: It's okay to have too many components available, but not okay to have unused
499 // arguments. 'full' will go to true when enough args have been seen. If we loop
500 // again, there is an extra argument, so 'overfull' will become true.
501 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 int size = 0;
504 bool constType = true;
505 bool full = false;
506 bool overFull = false;
507 bool matrixInMatrix = false;
508 bool arrayArg = false;
509 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000510 const TParameter& param = function.getParam(i);
511 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000512
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000513 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514 matrixInMatrix = true;
515 if (full)
516 overFull = true;
517 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
518 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000519 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000521 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 arrayArg = true;
523 }
524
525 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000526 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
529 error(line, "array constructor needs one argument per array element", "constructor", "");
530 return true;
531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 if (arrayArg && op != EOpConstructStruct) {
534 error(line, "constructing from a non-dereferenced array", "constructor", "");
535 return true;
536 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000539 if (function.getParamCount() != 1) {
540 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
541 return true;
542 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 if (overFull) {
546 error(line, "too many arguments", "constructor", "");
547 return true;
548 }
549
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000550 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
552 return true;
553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000555 if (!type->isMatrix()) {
556 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
557 (op == EOpConstructStruct && size < type->getObjectSize())) {
558 error(line, "not enough data provided for construction", "constructor", "");
559 return true;
560 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000563 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 if (typed == 0) {
565 error(line, "constructor argument does not have a type", "constructor", "");
566 return true;
567 }
568 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
569 error(line, "cannot convert a sampler", "constructor", "");
570 return true;
571 }
572 if (typed->getBasicType() == EbtVoid) {
573 error(line, "cannot convert a void", "constructor", "");
574 return true;
575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578}
579
580// This function checks to see if a void variable has been declared and raise an error message for such a case
581//
582// returns true in case of an error
583//
584bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
585{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 if (pubType.type == EbtVoid) {
587 error(line, "illegal use of type 'void'", identifier.c_str(), "");
588 return true;
589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592}
593
594// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
595//
596// returns true in case of an error
597//
598bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
599{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
601 error(line, "boolean expression expected", "", "");
602 return true;
603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606}
607
608// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
609//
610// returns true in case of an error
611//
612bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
613{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000614 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
615 error(line, "boolean expression expected", "", "");
616 return true;
617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620}
621
622bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
623{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 if (pType.type == EbtStruct) {
625 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000626 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627
628 return true;
629 }
630
631 return false;
632 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000633 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000635 return true;
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639}
640
641bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
642{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
644 pType.type == EbtStruct) {
645 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
646
647 return true;
648 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
651 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
656bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
657{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000658 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
659 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
660 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
661 return true;
662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
667bool TParseContext::containsSampler(TType& type)
668{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000669 if (IsSampler(type.getBasicType()))
670 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 if (type.getBasicType() == EbtStruct) {
673 TTypeList& structure = *type.getStruct();
674 for (unsigned int i = 0; i < structure.size(); ++i) {
675 if (containsSampler(*structure[i].type))
676 return true;
677 }
678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
683//
684// Do size checking for an array type's size.
685//
686// Returns true if there was an error.
687//
688bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
689{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000690 TIntermConstantUnion* constant = expr->getAsConstantUnion();
691 if (constant == 0 || constant->getBasicType() != EbtInt) {
692 error(line, "array size must be a constant integer expression", "", "");
693 return true;
694 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 if (size <= 0) {
699 error(line, "array size must be a positive integer", "", "");
700 size = 1;
701 return true;
702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000704 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705}
706
707//
708// See if this qualifier can be an array.
709//
710// Returns true if there is an error.
711//
712bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
713{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000714 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000715 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
716 return true;
717 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000719 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720}
721
722//
723// See if this type can be an array.
724//
725// Returns true if there is an error.
726//
727bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
728{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000729 //
730 // Can the type be an array?
731 //
732 if (type.array) {
733 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
734 return true;
735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738}
739
740//
741// Do all the semantic checking for declaring an array, with and
742// without a size, and make the right changes to the symbol table.
743//
744// size == 0 means no specified size.
745//
746// Returns true if there was an error.
747//
748bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 //
751 // Don't check for reserved word use until after we know it's not in the symbol table,
752 // because reserved arrays can be redeclared.
753 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 bool builtIn = false;
756 bool sameScope = false;
757 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
758 if (symbol == 0 || !sameScope) {
759 if (reservedErrorCheck(line, identifier))
760 return true;
761
762 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000764 if (type.arraySize)
765 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000767 if (! symbolTable.insert(*variable)) {
768 delete variable;
769 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
770 return true;
771 }
772 } else {
773 if (! symbol->isVariable()) {
774 error(line, "variable expected", identifier.c_str(), "");
775 return true;
776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 variable = static_cast<TVariable*>(symbol);
779 if (! variable->getType().isArray()) {
780 error(line, "redeclaring non-array as array", identifier.c_str(), "");
781 return true;
782 }
783 if (variable->getType().getArraySize() > 0) {
784 error(line, "redeclaration of array with size", identifier.c_str(), "");
785 return true;
786 }
787
788 if (! variable->getType().sameElementType(TType(type))) {
789 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
790 return true;
791 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000793 TType* t = variable->getArrayInformationType();
794 while (t != 0) {
795 if (t->getMaxArraySize() > type.arraySize) {
796 error(line, "higher index value already used for the array", identifier.c_str(), "");
797 return true;
798 }
799 t->setArraySize(type.arraySize);
800 t = t->getArrayInformationType();
801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 if (type.arraySize)
804 variable->getType().setArraySize(type.arraySize);
805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 if (voidErrorCheck(line, identifier, type))
808 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
813bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
814{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 bool builtIn = false;
816 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
817 if (symbol == 0) {
818 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
819 return true;
820 }
821 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 type->setArrayInformationType(variable->getArrayInformationType());
824 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
827 // its an error
828 if (node->getSymbol() == "gl_FragData") {
829 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
830 if (fragData == 0) {
831 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
832 return true;
833 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
836 if (fragDataValue <= size) {
837 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
838 return true;
839 }
840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000842 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
843 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
844 if (!updateFlag)
845 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 size++;
848 variable->getType().setMaxArraySize(size);
849 type->setMaxArraySize(size);
850 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 while(tt->getArrayInformationType() != 0) {
853 tt = tt->getArrayInformationType();
854 tt->setMaxArraySize(size);
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
865bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
866{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000867 //
868 // Make the qualifier make sense.
869 //
870 if (type.qualifier == EvqConst) {
871 type.qualifier = EvqTemporary;
872 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
873 return true;
874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
879//
880// Do semantic checking for a variable declaration that has no initializer,
881// and update the symbol table.
882//
883// Returns true if there was an error.
884//
885bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type)
886{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 if (reservedErrorCheck(line, identifier))
888 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 TVariable* variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 if (! symbolTable.insert(*variable)) {
893 error(line, "redefinition", variable->getName().c_str(), "");
894 delete variable;
895 return true;
896 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 if (voidErrorCheck(line, identifier, type))
899 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000901 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
904bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
905{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 if (qualifier != EvqConst && qualifier != EvqTemporary) {
907 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
908 return true;
909 }
910 if (qualifier == EvqConst && paramQualifier != EvqIn) {
911 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
912 return true;
913 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000915 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000916 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000918 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000920 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921}
922
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000923bool TParseContext::extensionErrorCheck(int line, const TString& extension)
924{
925 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
926 if (iter == extensionBehavior.end()) {
927 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 return true;
929 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000930 if (iter->second == EBhDisable) {
931 error(line, "extension", extension.c_str(), "is disabled");
932 return true;
933 }
934 if (iter->second == EBhWarn) {
935 TString msg = "extension " + extension + " is being used";
936 infoSink.info.message(EPrefixWarning, msg.c_str(), line);
937 return false;
938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943/////////////////////////////////////////////////////////////////////////////////
944//
945// Non-Errors.
946//
947/////////////////////////////////////////////////////////////////////////////////
948
949//
950// Look up a function name in the symbol table, and make sure it is a function.
951//
952// Return the function symbol if found, otherwise 0.
953//
954const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
955{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000956 // First find by unmangled name to check whether the function name has been
957 // hidden by a variable name or struct typename.
958 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
959 if (symbol == 0) {
960 symbol = symbolTable.find(call->getMangledName(), builtIn);
961 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
alokp@chromium.org0a576182010-08-09 17:16:27 +0000963 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 error(line, "no matching overloaded function found", call->getName().c_str(), "");
965 return 0;
966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967
alokp@chromium.org0a576182010-08-09 17:16:27 +0000968 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 error(line, "function name expected", call->getName().c_str(), "");
970 return 0;
971 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000972
973 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
976//
977// Initializers show up in several places in the grammar. Have one set of
978// code to handle them here.
979//
980bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000985 if (variable == 0) {
986 if (reservedErrorCheck(line, identifier))
987 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 if (voidErrorCheck(line, identifier, pType))
990 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 //
993 // add variable to symbol table
994 //
995 variable = new TVariable(&identifier, type);
996 if (! symbolTable.insert(*variable)) {
997 error(line, "redefinition", variable->getName().c_str(), "");
998 return true;
999 // don't delete variable, it's used by error recovery, and the pool
1000 // pop will take care of the memory
1001 }
1002 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001004 //
1005 // identifier must be of type constant, a global, or a temporary
1006 //
1007 TQualifier qualifier = variable->getType().getQualifier();
1008 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1009 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1010 return true;
1011 }
1012 //
1013 // test for and propagate constant
1014 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001016 if (qualifier == EvqConst) {
1017 if (qualifier != initializer->getType().getQualifier()) {
1018 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001019 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001020 return true;
1021 }
1022 if (type != initializer->getType()) {
1023 error(line, " non-matching types for const initializer ",
1024 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001025 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001026 return true;
1027 }
1028 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001029 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1032 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1033 } else {
1034 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1035 }
1036 } else if (initializer->getAsSymbolNode()) {
1037 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1038 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001040 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001041 variable->shareConstPointer(constArray);
1042 } else {
1043 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001044 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001045 return true;
1046 }
1047 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001049 if (qualifier != EvqConst) {
1050 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1051 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1052 if (intermNode == 0) {
1053 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1054 return true;
1055 }
1056 } else
1057 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001059 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060}
1061
1062bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1063{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001064 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001065 if (!aggrNode->isConstructor())
1066 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001070 // check if all the child nodes are constants so that they can be inserted into
1071 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001072 TIntermSequence &sequence = aggrNode->getSequence() ;
1073 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1074 if (!(*p)->getAsTyped()->getAsConstantUnion())
1075 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081// This function is used to test for the correctness of the parameters passed to various constructor functions
1082// and also convert them to the right datatype if it is allowed and required.
1083//
1084// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1085//
1086TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1087{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001088 if (node == 0)
1089 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 TIntermAggregate* aggrNode = node->getAsAggregate();
1092
alokp@chromium.org58e54292010-08-24 21:40:03 +00001093 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 if (op == EOpConstructStruct)
1095 memberTypes = type->getStruct()->begin();
1096
1097 TType elementType = *type;
1098 if (type->isArray())
1099 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 bool singleArg;
1102 if (aggrNode) {
1103 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1104 singleArg = true;
1105 else
1106 singleArg = false;
1107 } else
1108 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 TIntermTyped *newNode;
1111 if (singleArg) {
1112 // If structure constructor or array constructor is being called
1113 // for only one parameter inside the structure, we need to call constructStruct function once.
1114 if (type->isArray())
1115 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1116 else if (op == EOpConstructStruct)
1117 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1118 else
1119 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001121 if (newNode && newNode->getAsAggregate()) {
1122 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1123 if (constConstructor)
1124 return constConstructor;
1125 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001127 return newNode;
1128 }
1129
1130 //
1131 // Handle list of arguments.
1132 //
1133 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1134 // if the structure constructor contains more than one parameter, then construct
1135 // each parameter
1136
1137 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1138
1139 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1140 // to the right type if possible (and allowed).
1141 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1142
1143 for (TIntermSequence::iterator p = sequenceVector.begin();
1144 p != sequenceVector.end(); p++, paramCount++) {
1145 if (type->isArray())
1146 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1147 else if (op == EOpConstructStruct)
1148 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1149 else
1150 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1151
1152 if (newNode) {
1153 *p = newNode;
1154 }
1155 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001157 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1158 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1159 if (constConstructor)
1160 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001162 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163}
1164
1165TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1166{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001167 bool canBeFolded = areAllChildConst(aggrNode);
1168 aggrNode->setType(type);
1169 if (canBeFolded) {
1170 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001171 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001172 if (aggrNode->getSequence().size() == 1) {
1173 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1174 }
1175 else {
1176 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1177 }
1178 if (returnVal)
1179 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001180
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001184 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185}
1186
1187// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1188// for the parameter to the constructor (passed to this function). Essentially, it converts
1189// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1190// float, then float is converted to int.
1191//
1192// Returns 0 for an error or the constructed node.
1193//
1194TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1195{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001196 TIntermTyped* newNode;
1197 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 //
1200 // First, convert types as needed.
1201 //
1202 switch (op) {
1203 case EOpConstructVec2:
1204 case EOpConstructVec3:
1205 case EOpConstructVec4:
1206 case EOpConstructMat2:
1207 case EOpConstructMat3:
1208 case EOpConstructMat4:
1209 case EOpConstructFloat:
1210 basicOp = EOpConstructFloat;
1211 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001212
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001213 case EOpConstructIVec2:
1214 case EOpConstructIVec3:
1215 case EOpConstructIVec4:
1216 case EOpConstructInt:
1217 basicOp = EOpConstructInt;
1218 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001220 case EOpConstructBVec2:
1221 case EOpConstructBVec3:
1222 case EOpConstructBVec4:
1223 case EOpConstructBool:
1224 basicOp = EOpConstructBool;
1225 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001227 default:
1228 error(line, "unsupported construction", "", "");
1229 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001230
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001231 return 0;
1232 }
1233 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1234 if (newNode == 0) {
1235 error(line, "can't convert", "constructor", "");
1236 return 0;
1237 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001239 //
1240 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1241 //
1242
1243 // Otherwise, skip out early.
1244 if (subset || (newNode != node && newNode->getType() == *type))
1245 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001246
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001247 // setAggregateOperator will insert a new node for the constructor, as needed.
1248 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249}
1250
1251// This function tests for the type of the parameters to the structures constructors. Raises
1252// an error message if the expected type does not match the parameter passed to the constructor.
1253//
1254// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1255//
1256TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1257{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001258 if (*type == node->getAsTyped()->getType()) {
1259 if (subset)
1260 return node->getAsTyped();
1261 else
1262 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1263 } else {
1264 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1265 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1266 recover();
1267 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270}
1271
1272//
1273// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1274// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1275// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1276// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1277// a constant matrix.
1278//
1279TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1280{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001281 TIntermTyped* typedNode;
1282 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001283
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001284 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001285 if (tempConstantNode) {
1286 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001288 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001289 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001290 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001292 return node;
1293 }
1294 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1295 error(line, "Cannot offset into the vector", "Error", "");
1296 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 return 0;
1299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001301 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001303 for (int i = 0; i < fields.num; i++) {
1304 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1305 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1306 recover();
1307 fields.offsets[i] = 0;
1308 }
1309
1310 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001312 }
1313 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1314 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315}
1316
1317//
1318// This function returns the column being accessed from a constant matrix. The values are retrieved from
1319// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1320// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1321// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1322//
1323TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1324{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001325 TIntermTyped* typedNode;
1326 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001328 if (index >= node->getType().getNominalSize()) {
1329 error(line, "", "[", "matrix field selection out of range '%d'", index);
1330 recover();
1331 index = 0;
1332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001334 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001335 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 int size = tempConstantNode->getType().getNominalSize();
1337 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1338 } else {
1339 error(line, "Cannot offset into the matrix", "Error", "");
1340 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 return 0;
1343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001345 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001346}
1347
1348
1349//
1350// This function returns an element of an array accessed from a constant array. The values are retrieved from
1351// the symbol table and parse-tree is built for the type of the element. The input
1352// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1353// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1354//
1355TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1356{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001357 TIntermTyped* typedNode;
1358 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1359 TType arrayElementType = node->getType();
1360 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001361
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001362 if (index >= node->getType().getArraySize()) {
1363 error(line, "", "[", "array field selection out of range '%d'", index);
1364 recover();
1365 index = 0;
1366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001368 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001370 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001371 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001372 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1373 } else {
1374 error(line, "Cannot offset into the array", "Error", "");
1375 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001377 return 0;
1378 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001380 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381}
1382
1383
1384//
1385// This function returns the value of a particular field inside a constant structure from the symbol table.
1386// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1387// function and returns the parse-tree with the values of the embedded/nested struct.
1388//
1389TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1390{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001391 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001392 TIntermTyped *typedNode;
1393 int instanceSize = 0;
1394 unsigned int index = 0;
1395 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001396
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001397 for ( index = 0; index < fields->size(); ++index) {
1398 if ((*fields)[index].type->getFieldName() == identifier) {
1399 break;
1400 } else {
1401 instanceSize += (*fields)[index].type->getObjectSize();
1402 }
1403 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001405 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001406 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1409 } else {
1410 error(line, "Cannot offset into the structure", "Error", "");
1411 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001413 return 0;
1414 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001416 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001417}
1418
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001419//
1420// Parse an array of strings using yyparse.
1421//
1422// Returns 0 for success.
1423//
1424int PaParseStrings(int count, const char* const string[], const int length[],
1425 TParseContext* context) {
1426 if ((count == 0) || (string == NULL))
1427 return 1;
1428
1429 // setup preprocessor.
1430 if (InitPreprocessor())
1431 return 1;
1432 DefineExtensionMacros(context->extensionBehavior);
1433
1434 if (glslang_initialize(context))
1435 return 1;
1436
1437 glslang_scan(count, string, length, context);
1438 int error = glslang_parse(context);
1439
1440 glslang_finalize(context);
1441 FinalizePreprocessor();
1442 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1443}
1444
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001445OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1446
1447bool InitializeParseContextIndex()
1448{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001449 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1450 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1451 return false;
1452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001454 //
1455 // Allocate a TLS index.
1456 //
1457 GlobalParseContextIndex = OS_AllocTLSIndex();
1458
1459 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1460 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1461 return false;
1462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001463
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001464 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001465}
1466
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001467bool FreeParseContextIndex()
1468{
1469 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
1470
1471 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1472 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1473 return false;
1474 }
1475
1476 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1477
1478 return OS_FreeTLSIndex(tlsiIndex);
1479}
1480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481bool InitializeGlobalParseContext()
1482{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001483 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1484 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1485 return false;
1486 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001488 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1489 if (lpParseContext != 0) {
1490 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1491 return false;
1492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001493
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001494 TThreadParseContext *lpThreadData = new TThreadParseContext();
1495 if (lpThreadData == 0) {
1496 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1497 return false;
1498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001500 lpThreadData->lpGlobalParseContext = 0;
1501 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001503 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504}
1505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506bool FreeParseContext()
1507{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001508 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1509 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1510 return false;
1511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001512
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001513 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1514 if (lpParseContext)
1515 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001517 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518}
1519
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001520TParseContextPointer& GetGlobalParseContext()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001521{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001522 //
1523 // Minimal error checking for speed
1524 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001526 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001528 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001530