blob: 869fa915e0e1324265fe967258aa372e29b92336 [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>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010
11#include "compiler/osinclude.h"
12#include "compiler/InitializeParseContext.h"
13
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014///////////////////////////////////////////////////////////////////////
15//
16// Sub- vector and matrix fields
17//
18////////////////////////////////////////////////////////////////////////
19
20//
21// Look at a '.' field selector string and change it into offsets
22// for a vector.
23//
24bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
25{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000026 fields.num = (int) compString.size();
27 if (fields.num > 4) {
28 error(line, "illegal vector field selection", compString.c_str(), "");
29 return false;
30 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000032 enum {
33 exyzw,
34 ergba,
35 estpq,
36 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 for (int i = 0; i < fields.num; ++i) {
39 switch (compString[i]) {
40 case 'x':
41 fields.offsets[i] = 0;
42 fieldSet[i] = exyzw;
43 break;
44 case 'r':
45 fields.offsets[i] = 0;
46 fieldSet[i] = ergba;
47 break;
48 case 's':
49 fields.offsets[i] = 0;
50 fieldSet[i] = estpq;
51 break;
52 case 'y':
53 fields.offsets[i] = 1;
54 fieldSet[i] = exyzw;
55 break;
56 case 'g':
57 fields.offsets[i] = 1;
58 fieldSet[i] = ergba;
59 break;
60 case 't':
61 fields.offsets[i] = 1;
62 fieldSet[i] = estpq;
63 break;
64 case 'z':
65 fields.offsets[i] = 2;
66 fieldSet[i] = exyzw;
67 break;
68 case 'b':
69 fields.offsets[i] = 2;
70 fieldSet[i] = ergba;
71 break;
72 case 'p':
73 fields.offsets[i] = 2;
74 fieldSet[i] = estpq;
75 break;
76
77 case 'w':
78 fields.offsets[i] = 3;
79 fieldSet[i] = exyzw;
80 break;
81 case 'a':
82 fields.offsets[i] = 3;
83 fieldSet[i] = ergba;
84 break;
85 case 'q':
86 fields.offsets[i] = 3;
87 fieldSet[i] = estpq;
88 break;
89 default:
90 error(line, "illegal vector field selection", compString.c_str(), "");
91 return false;
92 }
93 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000095 for (int i = 0; i < fields.num; ++i) {
96 if (fields.offsets[i] >= vecSize) {
97 error(line, "vector field selection out of range", compString.c_str(), "");
98 return false;
99 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000101 if (i > 0) {
102 if (fieldSet[i] != fieldSet[i-1]) {
103 error(line, "illegal - vector component fields not from the same set", compString.c_str(), "");
104 return false;
105 }
106 }
107 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000109 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110}
111
112
113//
114// Look at a '.' field selector string and change it into offsets
115// for a matrix.
116//
117bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
118{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000119 fields.wholeRow = false;
120 fields.wholeCol = false;
121 fields.row = -1;
122 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000124 if (compString.size() != 2) {
125 error(line, "illegal length of matrix field selection", compString.c_str(), "");
126 return false;
127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000129 if (compString[0] == '_') {
130 if (compString[1] < '0' || compString[1] > '3') {
131 error(line, "illegal matrix field selection", compString.c_str(), "");
132 return false;
133 }
134 fields.wholeCol = true;
135 fields.col = compString[1] - '0';
136 } else if (compString[1] == '_') {
137 if (compString[0] < '0' || compString[0] > '3') {
138 error(line, "illegal matrix field selection", compString.c_str(), "");
139 return false;
140 }
141 fields.wholeRow = true;
142 fields.row = compString[0] - '0';
143 } else {
144 if (compString[0] < '0' || compString[0] > '3' ||
145 compString[1] < '0' || compString[1] > '3') {
146 error(line, "illegal matrix field selection", compString.c_str(), "");
147 return false;
148 }
149 fields.row = compString[0] - '0';
150 fields.col = compString[1] - '0';
151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000153 if (fields.row >= matSize || fields.col >= matSize) {
154 error(line, "matrix field selection out of range", compString.c_str(), "");
155 return false;
156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000158 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159}
160
161///////////////////////////////////////////////////////////////////////
162//
163// Errors
164//
165////////////////////////////////////////////////////////////////////////
166
167//
168// Track whether errors have occurred.
169//
170void TParseContext::recover()
171{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000172 recoveredFromError = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
178void C_DECL TParseContext::error(TSourceLoc nLine, const char *szReason, const char *szToken,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000179 const char *szExtraInfoFormat, ...)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000181 char szExtraInfo[400];
182 va_list marker;
183
184 va_start(marker, szExtraInfoFormat);
185
186 _vsnprintf(szExtraInfo, sizeof(szExtraInfo), szExtraInfoFormat, marker);
187
188 /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
189 infoSink.info.prefix(EPrefixError);
190 infoSink.info.location(nLine);
191 infoSink.info << "'" << szToken << "' : " << szReason << " " << szExtraInfo << "\n";
192
193 va_end(marker);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000195 ++numErrors;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196}
197
198//
199// Same error message for all places assignments don't work.
200//
201void TParseContext::assignError(int line, const char* op, TString left, TString right)
202{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000203 error(line, "", op, "cannot convert from '%s' to '%s'",
204 right.c_str(), left.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all places unary operations don't work.
209//
210void TParseContext::unaryOpError(int line, const char* op, TString operand)
211{
212 error(line, " wrong operand type", op,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000213 "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",
214 op, operand.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215}
216
217//
218// Same error message for all binary operations don't work.
219//
220void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
221{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000222 error(line, " wrong operand types ", op,
223 "no operation '%s' exists that takes a left-hand operand of type '%s' and "
224 "a right operand of type '%s' (or there is no acceptable conversion)",
225 op, left.c_str(), right.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Both test and if necessary, spit out an error, to see if the node is really
230// an l-value that can be operated on this way.
231//
232// Returns true if the was an error.
233//
234bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
235{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000236 TIntermSymbol* symNode = node->getAsSymbolNode();
237 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000239 if (binaryNode) {
240 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000242 switch(binaryNode->getOp()) {
243 case EOpIndexDirect:
244 case EOpIndexIndirect:
245 case EOpIndexDirectStruct:
246 return lValueErrorCheck(line, op, binaryNode->getLeft());
247 case EOpVectorSwizzle:
248 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
249 if (!errorReturn) {
250 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000252 TIntermTyped* rightNode = binaryNode->getRight();
253 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
254
255 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
256 p != aggrNode->getSequence().end(); p++) {
257 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
258 offset[value]++;
259 if (offset[value] > 1) {
260 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 return true;
263 }
264 }
265 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000267 return errorReturn;
268 default:
269 break;
270 }
271 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 return true;
274 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
276
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 const char* symbol = 0;
278 if (symNode != 0)
279 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 const char* message = 0;
282 switch (node->getQualifier()) {
283 case EvqConst: message = "can't modify a const"; break;
284 case EvqConstReadOnly: message = "can't modify a const"; break;
285 case EvqAttribute: message = "can't modify an attribute"; break;
286 case EvqUniform: message = "can't modify a uniform"; break;
287 case EvqVaryingIn: message = "can't modify a varying"; break;
288 case EvqInput: message = "can't modify an input"; break;
289 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
290 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
291 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
292 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 //
295 // Type that can't be written to?
296 //
297 switch (node->getBasicType()) {
298 case EbtSampler2D:
299 case EbtSamplerCube:
300 message = "can't modify a sampler";
301 break;
302 case EbtVoid:
303 message = "can't modify void";
304 break;
305 default:
306 break;
307 }
308 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000310 if (message == 0 && binaryNode == 0 && symNode == 0) {
311 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 return true;
314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 //
318 // Everything else is okay, no error.
319 //
320 if (message == 0)
321 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 //
324 // If we get here, we have an error and a message.
325 //
326 if (symNode)
327 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
328 else
329 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332}
333
334//
335// Both test, and if necessary spit out an error, to see if the node is really
336// a constant.
337//
338// Returns true if the was an error.
339//
340bool TParseContext::constErrorCheck(TIntermTyped* node)
341{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 if (node->getQualifier() == EvqConst)
343 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348}
349
350//
351// Both test, and if necessary spit out an error, to see if the node is really
352// an integer.
353//
354// Returns true if the was an error.
355//
356bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
357{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000358 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
359 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000361 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364}
365
366//
367// Both test, and if necessary spit out an error, to see if we are currently
368// globally scoped.
369//
370// Returns true if the was an error.
371//
372bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
373{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000374 if (global)
375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000377 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380}
381
382//
383// For now, keep it simple: if it starts "gl_", it's reserved, independent
384// of scope. Except, if the symbol table is at the built-in push-level,
385// which is when we are parsing built-ins.
386//
387// Returns true if there was an error.
388//
389bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
390{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000391 if (!symbolTable.atBuiltInLevel()) {
392 if (identifier.substr(0, 3) == TString("gl_")) {
393 error(line, "reserved built-in name", "gl_", "");
394 return true;
395 }
396 if (identifier.find("__") != TString::npos) {
397 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
398 //return true;
399 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
400 return false;
401 }
402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Make sure there is enough data provided to the constructor to build
409// something of the type of the constructor. Also returns the type of
410// the constructor.
411//
412// Returns true if there was an error in construction.
413//
414bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
415{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 bool constructingMatrix = false;
419 switch(op) {
420 case EOpConstructMat2:
421 case EOpConstructMat3:
422 case EOpConstructMat4:
423 constructingMatrix = true;
424 break;
425 default:
426 break;
427 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000429 //
430 // Note: It's okay to have too many components available, but not okay to have unused
431 // arguments. 'full' will go to true when enough args have been seen. If we loop
432 // again, there is an extra argument, so 'overfull' will become true.
433 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 int size = 0;
436 bool constType = true;
437 bool full = false;
438 bool overFull = false;
439 bool matrixInMatrix = false;
440 bool arrayArg = false;
441 for (int i = 0; i < function.getParamCount(); ++i) {
442 size += function[i].type->getObjectSize();
443
444 if (constructingMatrix && function[i].type->isMatrix())
445 matrixInMatrix = true;
446 if (full)
447 overFull = true;
448 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
449 full = true;
450 if (function[i].type->getQualifier() != EvqConst)
451 constType = false;
452 if (function[i].type->isArray())
453 arrayArg = true;
454 }
455
456 if (constType)
457 type->changeQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
460 error(line, "array constructor needs one argument per array element", "constructor", "");
461 return true;
462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000464 if (arrayArg && op != EOpConstructStruct) {
465 error(line, "constructing from a non-dereferenced array", "constructor", "");
466 return true;
467 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 if (matrixInMatrix && !type->isArray()) {
470 error(line, "constructing matrix from matrix", "constructor", "(reserved)");
471 return true;
472 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 if (overFull) {
475 error(line, "too many arguments", "constructor", "");
476 return true;
477 }
478
479 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->size() != function.getParamCount()) {
480 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
481 return true;
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000484 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
485 (op == EOpConstructStruct && size < type->getObjectSize())) {
486 error(line, "not enough data provided for construction", "constructor", "");
487 return true;
488 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 TIntermTyped* typed = node->getAsTyped();
491 if (typed == 0) {
492 error(line, "constructor argument does not have a type", "constructor", "");
493 return true;
494 }
495 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
496 error(line, "cannot convert a sampler", "constructor", "");
497 return true;
498 }
499 if (typed->getBasicType() == EbtVoid) {
500 error(line, "cannot convert a void", "constructor", "");
501 return true;
502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505}
506
507// This function checks to see if a void variable has been declared and raise an error message for such a case
508//
509// returns true in case of an error
510//
511bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
512{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000513 if (pubType.type == EbtVoid) {
514 error(line, "illegal use of type 'void'", identifier.c_str(), "");
515 return true;
516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519}
520
521// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
522//
523// returns true in case of an error
524//
525bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
526{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
528 error(line, "boolean expression expected", "", "");
529 return true;
530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533}
534
535// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
536//
537// returns true in case of an error
538//
539bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
540{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
542 error(line, "boolean expression expected", "", "");
543 return true;
544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547}
548
549bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
550{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 if (pType.type == EbtStruct) {
552 if (containsSampler(*pType.userDef)) {
553 error(line, reason, TType::getBasicString(pType.type), "(structure contains a sampler)");
554
555 return true;
556 }
557
558 return false;
559 } else if (IsSampler(pType.type)) {
560 error(line, reason, TType::getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 return true;
563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566}
567
568bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
569{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
571 pType.type == EbtStruct) {
572 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
573
574 return true;
575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
578 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581}
582
583bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
584{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
586 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
587 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
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
594bool TParseContext::containsSampler(TType& type)
595{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 if (IsSampler(type.getBasicType()))
597 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 if (type.getBasicType() == EbtStruct) {
600 TTypeList& structure = *type.getStruct();
601 for (unsigned int i = 0; i < structure.size(); ++i) {
602 if (containsSampler(*structure[i].type))
603 return true;
604 }
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
610//
611// Do size checking for an array type's size.
612//
613// Returns true if there was an error.
614//
615bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
616{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000617 TIntermConstantUnion* constant = expr->getAsConstantUnion();
618 if (constant == 0 || constant->getBasicType() != EbtInt) {
619 error(line, "array size must be a constant integer expression", "", "");
620 return true;
621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 if (size <= 0) {
626 error(line, "array size must be a positive integer", "", "");
627 size = 1;
628 return true;
629 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632}
633
634//
635// See if this qualifier can be an array.
636//
637// Returns true if there is an error.
638//
639bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
640{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 if (type.qualifier == EvqAttribute) {
642 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
643 return true;
644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if (type.qualifier == EvqConst && extensionErrorCheck(line, "GL_3DL_array_objects"))
647 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
652//
653// See if this type can be an array.
654//
655// Returns true if there is an error.
656//
657bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
658{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000659 //
660 // Can the type be an array?
661 //
662 if (type.array) {
663 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
664 return true;
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670//
671// Do all the semantic checking for declaring an array, with and
672// without a size, and make the right changes to the symbol table.
673//
674// size == 0 means no specified size.
675//
676// Returns true if there was an error.
677//
678bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
679{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 //
681 // Don't check for reserved word use until after we know it's not in the symbol table,
682 // because reserved arrays can be redeclared.
683 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 bool builtIn = false;
686 bool sameScope = false;
687 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
688 if (symbol == 0 || !sameScope) {
689 if (reservedErrorCheck(line, identifier))
690 return true;
691
692 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 if (type.arraySize)
695 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 if (! symbolTable.insert(*variable)) {
698 delete variable;
699 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
700 return true;
701 }
702 } else {
703 if (! symbol->isVariable()) {
704 error(line, "variable expected", identifier.c_str(), "");
705 return true;
706 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000708 variable = static_cast<TVariable*>(symbol);
709 if (! variable->getType().isArray()) {
710 error(line, "redeclaring non-array as array", identifier.c_str(), "");
711 return true;
712 }
713 if (variable->getType().getArraySize() > 0) {
714 error(line, "redeclaration of array with size", identifier.c_str(), "");
715 return true;
716 }
717
718 if (! variable->getType().sameElementType(TType(type))) {
719 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
720 return true;
721 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000723 TType* t = variable->getArrayInformationType();
724 while (t != 0) {
725 if (t->getMaxArraySize() > type.arraySize) {
726 error(line, "higher index value already used for the array", identifier.c_str(), "");
727 return true;
728 }
729 t->setArraySize(type.arraySize);
730 t = t->getArrayInformationType();
731 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000733 if (type.arraySize)
734 variable->getType().setArraySize(type.arraySize);
735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 if (voidErrorCheck(line, identifier, type))
738 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
743bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
744{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 bool builtIn = false;
746 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
747 if (symbol == 0) {
748 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
749 return true;
750 }
751 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 type->setArrayInformationType(variable->getArrayInformationType());
754 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
757 // its an error
758 if (node->getSymbol() == "gl_FragData") {
759 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
760 if (fragData == 0) {
761 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
762 return true;
763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000765 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
766 if (fragDataValue <= size) {
767 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
768 return true;
769 }
770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
773 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
774 if (!updateFlag)
775 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 size++;
778 variable->getType().setMaxArraySize(size);
779 type->setMaxArraySize(size);
780 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000782 while(tt->getArrayInformationType() != 0) {
783 tt = tt->getArrayInformationType();
784 tt->setMaxArraySize(size);
785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000787 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
790//
791// Enforce non-initializer type/qualifier rules.
792//
793// Returns true if there was an error.
794//
795bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
796{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000797 //
798 // Make the qualifier make sense.
799 //
800 if (type.qualifier == EvqConst) {
801 type.qualifier = EvqTemporary;
802 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
803 return true;
804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
809//
810// Do semantic checking for a variable declaration that has no initializer,
811// and update the symbol table.
812//
813// Returns true if there was an error.
814//
815bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type)
816{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 if (reservedErrorCheck(line, identifier))
818 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 TVariable* variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000822 if (! symbolTable.insert(*variable)) {
823 error(line, "redefinition", variable->getName().c_str(), "");
824 delete variable;
825 return true;
826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 if (voidErrorCheck(line, identifier, type))
829 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000831 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
834bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
835{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000836 if (qualifier != EvqConst && qualifier != EvqTemporary) {
837 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
838 return true;
839 }
840 if (qualifier == EvqConst && paramQualifier != EvqIn) {
841 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
842 return true;
843 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 if (qualifier == EvqConst)
846 type->changeQualifier(EvqConstReadOnly);
847 else
848 type->changeQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853bool TParseContext::extensionErrorCheck(int line, const char* extension)
854{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 if (extensionBehavior[extension] == EBhWarn) {
856 infoSink.info.message(EPrefixWarning, ("extension " + TString(extension) + " is being used").c_str(), line);
857 return false;
858 }
859 if (extensionBehavior[extension] == EBhDisable) {
860 error(line, "extension", extension, "is disabled");
861 return true;
862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867/////////////////////////////////////////////////////////////////////////////////
868//
869// Non-Errors.
870//
871/////////////////////////////////////////////////////////////////////////////////
872
873//
874// Look up a function name in the symbol table, and make sure it is a function.
875//
876// Return the function symbol if found, otherwise 0.
877//
878const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
879{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000880 const TSymbol* symbol = symbolTable.find(call->getMangledName(), builtIn);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000882 if (symbol == 0) {
883 error(line, "no matching overloaded function found", call->getName().c_str(), "");
884 return 0;
885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 if (! symbol->isFunction()) {
888 error(line, "function name expected", call->getName().c_str(), "");
889 return 0;
890 }
891
892 const TFunction* function = static_cast<const TFunction*>(symbol);
893
894 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895}
896
897//
898// Initializers show up in several places in the grammar. Have one set of
899// code to handle them here.
900//
901bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 if (variable == 0) {
907 if (reservedErrorCheck(line, identifier))
908 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 if (voidErrorCheck(line, identifier, pType))
911 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 //
914 // add variable to symbol table
915 //
916 variable = new TVariable(&identifier, type);
917 if (! symbolTable.insert(*variable)) {
918 error(line, "redefinition", variable->getName().c_str(), "");
919 return true;
920 // don't delete variable, it's used by error recovery, and the pool
921 // pop will take care of the memory
922 }
923 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 //
926 // identifier must be of type constant, a global, or a temporary
927 //
928 TQualifier qualifier = variable->getType().getQualifier();
929 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
930 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
931 return true;
932 }
933 //
934 // test for and propagate constant
935 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000937 if (qualifier == EvqConst) {
938 if (qualifier != initializer->getType().getQualifier()) {
939 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
940 variable->getType().changeQualifier(EvqTemporary);
941 return true;
942 }
943 if (type != initializer->getType()) {
944 error(line, " non-matching types for const initializer ",
945 variable->getType().getQualifierString(), "");
946 variable->getType().changeQualifier(EvqTemporary);
947 return true;
948 }
949 if (initializer->getAsConstantUnion()) {
950 constUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
953 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
954 } else {
955 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
956 }
957 } else if (initializer->getAsSymbolNode()) {
958 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
959 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 constUnion* constArray = tVar->getConstPointer();
962 variable->shareConstPointer(constArray);
963 } else {
964 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
965 variable->getType().changeQualifier(EvqTemporary);
966 return true;
967 }
968 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 if (qualifier != EvqConst) {
971 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
972 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
973 if (intermNode == 0) {
974 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
975 return true;
976 }
977 } else
978 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
983bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
984{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000985 if (!aggrNode->isConstructor())
986 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000988 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 // check if all the child nodes are constants so that they can be inserted into
991 // the parent node
992 if (aggrNode) {
993 TIntermSequence &childSequenceVector = aggrNode->getSequence() ;
994 for (TIntermSequence::iterator p = childSequenceVector.begin();
995 p != childSequenceVector.end(); p++) {
996 if (!(*p)->getAsTyped()->getAsConstantUnion())
997 return false;
998 }
999 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001001 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002}
1003
1004// This function is used to test for the correctness of the parameters passed to various constructor functions
1005// and also convert them to the right datatype if it is allowed and required.
1006//
1007// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1008//
1009TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1010{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001011 if (node == 0)
1012 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001014 TIntermAggregate* aggrNode = node->getAsAggregate();
1015
1016 TTypeList::iterator memberTypes;
1017 if (op == EOpConstructStruct)
1018 memberTypes = type->getStruct()->begin();
1019
1020 TType elementType = *type;
1021 if (type->isArray())
1022 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 bool singleArg;
1025 if (aggrNode) {
1026 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1027 singleArg = true;
1028 else
1029 singleArg = false;
1030 } else
1031 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001033 TIntermTyped *newNode;
1034 if (singleArg) {
1035 // If structure constructor or array constructor is being called
1036 // for only one parameter inside the structure, we need to call constructStruct function once.
1037 if (type->isArray())
1038 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1039 else if (op == EOpConstructStruct)
1040 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1041 else
1042 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001044 if (newNode && newNode->getAsAggregate()) {
1045 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1046 if (constConstructor)
1047 return constConstructor;
1048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 return newNode;
1051 }
1052
1053 //
1054 // Handle list of arguments.
1055 //
1056 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1057 // if the structure constructor contains more than one parameter, then construct
1058 // each parameter
1059
1060 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1061
1062 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1063 // to the right type if possible (and allowed).
1064 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1065
1066 for (TIntermSequence::iterator p = sequenceVector.begin();
1067 p != sequenceVector.end(); p++, paramCount++) {
1068 if (type->isArray())
1069 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1070 else if (op == EOpConstructStruct)
1071 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1072 else
1073 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1074
1075 if (newNode) {
1076 *p = newNode;
1077 }
1078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1081 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1082 if (constConstructor)
1083 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1089{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 bool canBeFolded = areAllChildConst(aggrNode);
1091 aggrNode->setType(type);
1092 if (canBeFolded) {
1093 bool returnVal = false;
1094 constUnion* unionArray = new constUnion[type.getObjectSize()];
1095 if (aggrNode->getSequence().size() == 1) {
1096 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1097 }
1098 else {
1099 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1100 }
1101 if (returnVal)
1102 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001107 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108}
1109
1110// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1111// for the parameter to the constructor (passed to this function). Essentially, it converts
1112// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1113// float, then float is converted to int.
1114//
1115// Returns 0 for an error or the constructed node.
1116//
1117TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1118{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 TIntermTyped* newNode;
1120 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 //
1123 // First, convert types as needed.
1124 //
1125 switch (op) {
1126 case EOpConstructVec2:
1127 case EOpConstructVec3:
1128 case EOpConstructVec4:
1129 case EOpConstructMat2:
1130 case EOpConstructMat3:
1131 case EOpConstructMat4:
1132 case EOpConstructFloat:
1133 basicOp = EOpConstructFloat;
1134 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001136 case EOpConstructIVec2:
1137 case EOpConstructIVec3:
1138 case EOpConstructIVec4:
1139 case EOpConstructInt:
1140 basicOp = EOpConstructInt;
1141 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001143 case EOpConstructBVec2:
1144 case EOpConstructBVec3:
1145 case EOpConstructBVec4:
1146 case EOpConstructBool:
1147 basicOp = EOpConstructBool;
1148 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001150 default:
1151 error(line, "unsupported construction", "", "");
1152 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001154 return 0;
1155 }
1156 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1157 if (newNode == 0) {
1158 error(line, "can't convert", "constructor", "");
1159 return 0;
1160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001162 //
1163 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1164 //
1165
1166 // Otherwise, skip out early.
1167 if (subset || (newNode != node && newNode->getType() == *type))
1168 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 // setAggregateOperator will insert a new node for the constructor, as needed.
1171 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172}
1173
1174// This function tests for the type of the parameters to the structures constructors. Raises
1175// an error message if the expected type does not match the parameter passed to the constructor.
1176//
1177// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1178//
1179TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1180{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 if (*type == node->getAsTyped()->getType()) {
1182 if (subset)
1183 return node->getAsTyped();
1184 else
1185 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1186 } else {
1187 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1188 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1189 recover();
1190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001191
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001192 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
1195//
1196// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1197// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1198// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1199// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1200// a constant matrix.
1201//
1202TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1203{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001204 TIntermTyped* typedNode;
1205 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001207 constUnion *unionArray;
1208 if (tempConstantNode) {
1209 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001211 if (!unionArray) { // this error message should never be raised
1212 infoSink.info.message(EPrefixInternalError, "constUnion not initialized in addConstVectorNode function", line);
1213 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001214
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001215 return node;
1216 }
1217 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1218 error(line, "Cannot offset into the vector", "Error", "");
1219 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001221 return 0;
1222 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001224 constUnion* constArray = new constUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001226 for (int i = 0; i < fields.num; i++) {
1227 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1228 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1229 recover();
1230 fields.offsets[i] = 0;
1231 }
1232
1233 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001234
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001235 }
1236 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1237 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238}
1239
1240//
1241// This function returns the column being accessed from a constant matrix. The values are retrieved from
1242// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1243// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1244// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1245//
1246TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1247{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001248 TIntermTyped* typedNode;
1249 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 if (index >= node->getType().getNominalSize()) {
1252 error(line, "", "[", "matrix field selection out of range '%d'", index);
1253 recover();
1254 index = 0;
1255 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 if (tempConstantNode) {
1258 constUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1259 int size = tempConstantNode->getType().getNominalSize();
1260 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1261 } else {
1262 error(line, "Cannot offset into the matrix", "Error", "");
1263 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001265 return 0;
1266 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001267
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001268 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001269}
1270
1271
1272//
1273// This function returns an element of an array accessed from a constant array. The values are retrieved from
1274// the symbol table and parse-tree is built for the type of the element. The input
1275// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1276// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1277//
1278TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1279{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001280 TIntermTyped* typedNode;
1281 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1282 TType arrayElementType = node->getType();
1283 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001285 if (index >= node->getType().getArraySize()) {
1286 error(line, "", "[", "array field selection out of range '%d'", index);
1287 recover();
1288 index = 0;
1289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001290
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001291 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001293 if (tempConstantNode) {
1294 constUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1295 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1296 } else {
1297 error(line, "Cannot offset into the array", "Error", "");
1298 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001299
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001300 return 0;
1301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001303 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001304}
1305
1306
1307//
1308// This function returns the value of a particular field inside a constant structure from the symbol table.
1309// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1310// function and returns the parse-tree with the values of the embedded/nested struct.
1311//
1312TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1313{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 TTypeList* fields = node->getType().getStruct();
1315 TIntermTyped *typedNode;
1316 int instanceSize = 0;
1317 unsigned int index = 0;
1318 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001320 for ( index = 0; index < fields->size(); ++index) {
1321 if ((*fields)[index].type->getFieldName() == identifier) {
1322 break;
1323 } else {
1324 instanceSize += (*fields)[index].type->getObjectSize();
1325 }
1326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001328 if (tempConstantNode) {
1329 constUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001331 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1332 } else {
1333 error(line, "Cannot offset into the structure", "Error", "");
1334 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001335
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return 0;
1337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001338
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001339 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340}
1341
1342//
1343// Initialize all supported extensions to disable
1344//
1345void TParseContext::initializeExtensionBehavior()
1346{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001347 //
1348 // example code: extensionBehavior["test"] = EBhDisable; // where "test" is the name of
1349 // supported extension
1350 //
1351 extensionBehavior["GL_ARB_texture_rectangle"] = EBhRequire;
1352 extensionBehavior["GL_3DL_array_objects"] = EBhDisable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001353}
1354
1355OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1356
1357bool InitializeParseContextIndex()
1358{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001359 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1360 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1361 return false;
1362 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001363
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001364 //
1365 // Allocate a TLS index.
1366 //
1367 GlobalParseContextIndex = OS_AllocTLSIndex();
1368
1369 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1370 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1371 return false;
1372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001373
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001374 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375}
1376
1377bool InitializeGlobalParseContext()
1378{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001379 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1380 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1381 return false;
1382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001384 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1385 if (lpParseContext != 0) {
1386 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1387 return false;
1388 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001389
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001390 TThreadParseContext *lpThreadData = new TThreadParseContext();
1391 if (lpThreadData == 0) {
1392 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1393 return false;
1394 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001396 lpThreadData->lpGlobalParseContext = 0;
1397 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001399 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001400}
1401
1402TParseContextPointer& GetGlobalParseContext()
1403{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001404 //
1405 // Minimal error checking for speed
1406 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001409
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001410 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411}
1412
1413bool FreeParseContext()
1414{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001415 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1416 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1417 return false;
1418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001420 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1421 if (lpParseContext)
1422 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001423
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001424 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425}
1426
1427bool FreeParseContextIndex()
1428{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001429 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001430
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001431 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1432 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1433 return false;
1434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001435
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001436 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001438 return OS_FreeTLSIndex(tlsiIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001439}