blob: 38748439136138c635989541d7a464dd8027ea50 [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//
zmo@google.comfd747b82011-04-23 01:30:07 +0000885bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 if (reservedErrorCheck(line, identifier))
888 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889
zmo@google.comfd747b82011-04-23 01:30:07 +0000890 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;
zmo@google.comfd747b82011-04-23 01:30:07 +0000895 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return true;
897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 if (voidErrorCheck(line, identifier, type))
900 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
905bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
906{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 if (qualifier != EvqConst && qualifier != EvqTemporary) {
908 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
909 return true;
910 }
911 if (qualifier == EvqConst && paramQualifier != EvqIn) {
912 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
913 return true;
914 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000917 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000919 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922}
923
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000924bool TParseContext::extensionErrorCheck(int line, const TString& extension)
925{
926 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
927 if (iter == extensionBehavior.end()) {
928 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 return true;
930 }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000931 if (iter->second == EBhDisable) {
932 error(line, "extension", extension.c_str(), "is disabled");
933 return true;
934 }
935 if (iter->second == EBhWarn) {
936 TString msg = "extension " + extension + " is being used";
937 infoSink.info.message(EPrefixWarning, msg.c_str(), line);
938 return false;
939 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
944/////////////////////////////////////////////////////////////////////////////////
945//
946// Non-Errors.
947//
948/////////////////////////////////////////////////////////////////////////////////
949
950//
951// Look up a function name in the symbol table, and make sure it is a function.
952//
953// Return the function symbol if found, otherwise 0.
954//
955const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
956{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000957 // First find by unmangled name to check whether the function name has been
958 // hidden by a variable name or struct typename.
959 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
960 if (symbol == 0) {
961 symbol = symbolTable.find(call->getMangledName(), builtIn);
962 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
alokp@chromium.org0a576182010-08-09 17:16:27 +0000964 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000965 error(line, "no matching overloaded function found", call->getName().c_str(), "");
966 return 0;
967 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
alokp@chromium.org0a576182010-08-09 17:16:27 +0000969 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 error(line, "function name expected", call->getName().c_str(), "");
971 return 0;
972 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000973
974 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975}
976
977//
978// Initializers show up in several places in the grammar. Have one set of
979// code to handle them here.
980//
981bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 if (variable == 0) {
987 if (reservedErrorCheck(line, identifier))
988 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 if (voidErrorCheck(line, identifier, pType))
991 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000993 //
994 // add variable to symbol table
995 //
996 variable = new TVariable(&identifier, type);
997 if (! symbolTable.insert(*variable)) {
998 error(line, "redefinition", variable->getName().c_str(), "");
999 return true;
1000 // don't delete variable, it's used by error recovery, and the pool
1001 // pop will take care of the memory
1002 }
1003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 //
1006 // identifier must be of type constant, a global, or a temporary
1007 //
1008 TQualifier qualifier = variable->getType().getQualifier();
1009 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1010 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1011 return true;
1012 }
1013 //
1014 // test for and propagate constant
1015 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001017 if (qualifier == EvqConst) {
1018 if (qualifier != initializer->getType().getQualifier()) {
1019 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001020 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001021 return true;
1022 }
1023 if (type != initializer->getType()) {
1024 error(line, " non-matching types for const initializer ",
1025 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001026 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001027 return true;
1028 }
1029 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001030 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001032 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1033 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1034 } else {
1035 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1036 }
1037 } else if (initializer->getAsSymbolNode()) {
1038 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1039 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001041 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001042 variable->shareConstPointer(constArray);
1043 } else {
1044 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001045 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 return true;
1047 }
1048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 if (qualifier != EvqConst) {
1051 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1052 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1053 if (intermNode == 0) {
1054 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1055 return true;
1056 }
1057 } else
1058 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061}
1062
1063bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1064{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001065 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 if (!aggrNode->isConstructor())
1067 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001071 // check if all the child nodes are constants so that they can be inserted into
1072 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001073 TIntermSequence &sequence = aggrNode->getSequence() ;
1074 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1075 if (!(*p)->getAsTyped()->getAsConstantUnion())
1076 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080}
1081
1082// This function is used to test for the correctness of the parameters passed to various constructor functions
1083// and also convert them to the right datatype if it is allowed and required.
1084//
1085// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1086//
1087TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1088{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 if (node == 0)
1090 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001092 TIntermAggregate* aggrNode = node->getAsAggregate();
1093
alokp@chromium.org58e54292010-08-24 21:40:03 +00001094 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001095 if (op == EOpConstructStruct)
1096 memberTypes = type->getStruct()->begin();
1097
1098 TType elementType = *type;
1099 if (type->isArray())
1100 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 bool singleArg;
1103 if (aggrNode) {
1104 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1105 singleArg = true;
1106 else
1107 singleArg = false;
1108 } else
1109 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 TIntermTyped *newNode;
1112 if (singleArg) {
1113 // If structure constructor or array constructor is being called
1114 // for only one parameter inside the structure, we need to call constructStruct function once.
1115 if (type->isArray())
1116 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1117 else if (op == EOpConstructStruct)
1118 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1119 else
1120 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 if (newNode && newNode->getAsAggregate()) {
1123 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1124 if (constConstructor)
1125 return constConstructor;
1126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001128 return newNode;
1129 }
1130
1131 //
1132 // Handle list of arguments.
1133 //
1134 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1135 // if the structure constructor contains more than one parameter, then construct
1136 // each parameter
1137
1138 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1139
1140 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1141 // to the right type if possible (and allowed).
1142 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1143
1144 for (TIntermSequence::iterator p = sequenceVector.begin();
1145 p != sequenceVector.end(); p++, paramCount++) {
1146 if (type->isArray())
1147 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1148 else if (op == EOpConstructStruct)
1149 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1150 else
1151 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1152
1153 if (newNode) {
1154 *p = newNode;
1155 }
1156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1159 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1160 if (constConstructor)
1161 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001163 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164}
1165
1166TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1167{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001168 bool canBeFolded = areAllChildConst(aggrNode);
1169 aggrNode->setType(type);
1170 if (canBeFolded) {
1171 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001172 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001173 if (aggrNode->getSequence().size() == 1) {
1174 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1175 }
1176 else {
1177 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1178 }
1179 if (returnVal)
1180 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001182 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186}
1187
1188// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1189// for the parameter to the constructor (passed to this function). Essentially, it converts
1190// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1191// float, then float is converted to int.
1192//
1193// Returns 0 for an error or the constructed node.
1194//
1195TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1196{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001197 TIntermTyped* newNode;
1198 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001199
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001200 //
1201 // First, convert types as needed.
1202 //
1203 switch (op) {
1204 case EOpConstructVec2:
1205 case EOpConstructVec3:
1206 case EOpConstructVec4:
1207 case EOpConstructMat2:
1208 case EOpConstructMat3:
1209 case EOpConstructMat4:
1210 case EOpConstructFloat:
1211 basicOp = EOpConstructFloat;
1212 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001213
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001214 case EOpConstructIVec2:
1215 case EOpConstructIVec3:
1216 case EOpConstructIVec4:
1217 case EOpConstructInt:
1218 basicOp = EOpConstructInt;
1219 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001221 case EOpConstructBVec2:
1222 case EOpConstructBVec3:
1223 case EOpConstructBVec4:
1224 case EOpConstructBool:
1225 basicOp = EOpConstructBool;
1226 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 default:
1229 error(line, "unsupported construction", "", "");
1230 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001231
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001232 return 0;
1233 }
1234 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1235 if (newNode == 0) {
1236 error(line, "can't convert", "constructor", "");
1237 return 0;
1238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001240 //
1241 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1242 //
1243
1244 // Otherwise, skip out early.
1245 if (subset || (newNode != node && newNode->getType() == *type))
1246 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001248 // setAggregateOperator will insert a new node for the constructor, as needed.
1249 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250}
1251
1252// This function tests for the type of the parameters to the structures constructors. Raises
1253// an error message if the expected type does not match the parameter passed to the constructor.
1254//
1255// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1256//
1257TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1258{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001259 if (*type == node->getAsTyped()->getType()) {
1260 if (subset)
1261 return node->getAsTyped();
1262 else
1263 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1264 } else {
1265 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1266 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1267 recover();
1268 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001269
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001270 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271}
1272
1273//
1274// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1275// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1276// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1277// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1278// a constant matrix.
1279//
1280TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1281{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001282 TIntermTyped* typedNode;
1283 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001285 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 if (tempConstantNode) {
1287 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001289 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001290 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001291 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001293 return node;
1294 }
1295 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1296 error(line, "Cannot offset into the vector", "Error", "");
1297 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001299 return 0;
1300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001301
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001302 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 for (int i = 0; i < fields.num; i++) {
1305 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1306 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1307 recover();
1308 fields.offsets[i] = 0;
1309 }
1310
1311 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001313 }
1314 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1315 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316}
1317
1318//
1319// This function returns the column being accessed from a constant matrix. The values are retrieved from
1320// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1321// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1322// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1323//
1324TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1325{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001326 TIntermTyped* typedNode;
1327 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 if (index >= node->getType().getNominalSize()) {
1330 error(line, "", "[", "matrix field selection out of range '%d'", index);
1331 recover();
1332 index = 0;
1333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001335 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001336 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 int size = tempConstantNode->getType().getNominalSize();
1338 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1339 } else {
1340 error(line, "Cannot offset into the matrix", "Error", "");
1341 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 return 0;
1344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001345
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001346 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001347}
1348
1349
1350//
1351// This function returns an element of an array accessed from a constant array. The values are retrieved from
1352// the symbol table and parse-tree is built for the type of the element. The input
1353// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1354// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1355//
1356TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1357{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001358 TIntermTyped* typedNode;
1359 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1360 TType arrayElementType = node->getType();
1361 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 if (index >= node->getType().getArraySize()) {
1364 error(line, "", "[", "array field selection out of range '%d'", index);
1365 recover();
1366 index = 0;
1367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001369 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001371 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001372 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001373 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1374 } else {
1375 error(line, "Cannot offset into the array", "Error", "");
1376 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 return 0;
1379 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001380
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001381 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382}
1383
1384
1385//
1386// This function returns the value of a particular field inside a constant structure from the symbol table.
1387// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1388// function and returns the parse-tree with the values of the embedded/nested struct.
1389//
1390TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1391{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001392 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001393 TIntermTyped *typedNode;
1394 int instanceSize = 0;
1395 unsigned int index = 0;
1396 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001398 for ( index = 0; index < fields->size(); ++index) {
1399 if ((*fields)[index].type->getFieldName() == identifier) {
1400 break;
1401 } else {
1402 instanceSize += (*fields)[index].type->getObjectSize();
1403 }
1404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001407 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001409 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1410 } else {
1411 error(line, "Cannot offset into the structure", "Error", "");
1412 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 return 0;
1415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001417 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418}
1419
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001420//
1421// Parse an array of strings using yyparse.
1422//
1423// Returns 0 for success.
1424//
1425int PaParseStrings(int count, const char* const string[], const int length[],
1426 TParseContext* context) {
1427 if ((count == 0) || (string == NULL))
1428 return 1;
1429
1430 // setup preprocessor.
1431 if (InitPreprocessor())
1432 return 1;
1433 DefineExtensionMacros(context->extensionBehavior);
1434
1435 if (glslang_initialize(context))
1436 return 1;
1437
1438 glslang_scan(count, string, length, context);
1439 int error = glslang_parse(context);
1440
1441 glslang_finalize(context);
1442 FinalizePreprocessor();
1443 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1444}
1445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001446OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1447
1448bool InitializeParseContextIndex()
1449{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001450 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1451 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1452 return false;
1453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001454
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001455 //
1456 // Allocate a TLS index.
1457 //
1458 GlobalParseContextIndex = OS_AllocTLSIndex();
1459
1460 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1461 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1462 return false;
1463 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001464
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001465 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466}
1467
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001468bool FreeParseContextIndex()
1469{
1470 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
1471
1472 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1473 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1474 return false;
1475 }
1476
1477 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1478
1479 return OS_FreeTLSIndex(tlsiIndex);
1480}
1481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001482bool InitializeGlobalParseContext()
1483{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001484 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1485 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1486 return false;
1487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001488
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001489 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1490 if (lpParseContext != 0) {
1491 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1492 return false;
1493 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001495 TThreadParseContext *lpThreadData = new TThreadParseContext();
1496 if (lpThreadData == 0) {
1497 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1498 return false;
1499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001501 lpThreadData->lpGlobalParseContext = 0;
1502 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001504 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505}
1506
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001507bool FreeParseContext()
1508{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001509 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1510 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1511 return false;
1512 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001514 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1515 if (lpParseContext)
1516 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001518 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519}
1520
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001521TParseContextPointer& GetGlobalParseContext()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001523 //
1524 // Minimal error checking for speed
1525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001526
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001527 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001528
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001529 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001531