blob: 7141b0c696660e6fe9f663cc9720c240cc790007 [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;
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000183
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000184 va_start(marker, szExtraInfoFormat);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000185
186 vsnprintf(szExtraInfo, sizeof(szExtraInfo), szExtraInfoFormat, marker);
187
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000188 /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
189 infoSink.info.prefix(EPrefixError);
190 infoSink.info.location(nLine);
191 infoSink.info << "'" << szToken << "' : " << szReason << " " << szExtraInfo << "\n";
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000192
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000193 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()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000470 if (function.getParamCount() != 1) {
471 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
472 return true;
473 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000476 if (overFull) {
477 error(line, "too many arguments", "constructor", "");
478 return true;
479 }
480
481 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->size() != function.getParamCount()) {
482 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
483 return true;
484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000486 if (!type->isMatrix()) {
487 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
488 (op == EOpConstructStruct && size < type->getObjectSize())) {
489 error(line, "not enough data provided for construction", "constructor", "");
490 return true;
491 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494 TIntermTyped* typed = node->getAsTyped();
495 if (typed == 0) {
496 error(line, "constructor argument does not have a type", "constructor", "");
497 return true;
498 }
499 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
500 error(line, "cannot convert a sampler", "constructor", "");
501 return true;
502 }
503 if (typed->getBasicType() == EbtVoid) {
504 error(line, "cannot convert a void", "constructor", "");
505 return true;
506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509}
510
511// This function checks to see if a void variable has been declared and raise an error message for such a case
512//
513// returns true in case of an error
514//
515bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
516{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 if (pubType.type == EbtVoid) {
518 error(line, "illegal use of type 'void'", identifier.c_str(), "");
519 return true;
520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523}
524
525// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
526//
527// returns true in case of an error
528//
529bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
530{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
532 error(line, "boolean expression expected", "", "");
533 return true;
534 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537}
538
539// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
540//
541// returns true in case of an error
542//
543bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
544{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
546 error(line, "boolean expression expected", "", "");
547 return true;
548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551}
552
553bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
554{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 if (pType.type == EbtStruct) {
556 if (containsSampler(*pType.userDef)) {
557 error(line, reason, TType::getBasicString(pType.type), "(structure contains a sampler)");
558
559 return true;
560 }
561
562 return false;
563 } else if (IsSampler(pType.type)) {
564 error(line, reason, TType::getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 return true;
567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570}
571
572bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
573{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
575 pType.type == EbtStruct) {
576 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
577
578 return true;
579 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
582 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585}
586
587bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
588{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
590 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
591 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
592 return true;
593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596}
597
598bool TParseContext::containsSampler(TType& type)
599{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 if (IsSampler(type.getBasicType()))
601 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 if (type.getBasicType() == EbtStruct) {
604 TTypeList& structure = *type.getStruct();
605 for (unsigned int i = 0; i < structure.size(); ++i) {
606 if (containsSampler(*structure[i].type))
607 return true;
608 }
609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612}
613
614//
615// Do size checking for an array type's size.
616//
617// Returns true if there was an error.
618//
619bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
620{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 TIntermConstantUnion* constant = expr->getAsConstantUnion();
622 if (constant == 0 || constant->getBasicType() != EbtInt) {
623 error(line, "array size must be a constant integer expression", "", "");
624 return true;
625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 if (size <= 0) {
630 error(line, "array size must be a positive integer", "", "");
631 size = 1;
632 return true;
633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000635 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636}
637
638//
639// See if this qualifier can be an array.
640//
641// Returns true if there is an error.
642//
643bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
644{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if (type.qualifier == EvqAttribute) {
646 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
647 return true;
648 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 if (type.qualifier == EvqConst && extensionErrorCheck(line, "GL_3DL_array_objects"))
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
656//
657// See if this type can be an array.
658//
659// Returns true if there is an error.
660//
661bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
662{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 //
664 // Can the type be an array?
665 //
666 if (type.array) {
667 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
668 return true;
669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672}
673
674//
675// Do all the semantic checking for declaring an array, with and
676// without a size, and make the right changes to the symbol table.
677//
678// size == 0 means no specified size.
679//
680// Returns true if there was an error.
681//
682bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
683{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 //
685 // Don't check for reserved word use until after we know it's not in the symbol table,
686 // because reserved arrays can be redeclared.
687 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000689 bool builtIn = false;
690 bool sameScope = false;
691 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
692 if (symbol == 0 || !sameScope) {
693 if (reservedErrorCheck(line, identifier))
694 return true;
695
696 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 if (type.arraySize)
699 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 if (! symbolTable.insert(*variable)) {
702 delete variable;
703 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
704 return true;
705 }
706 } else {
707 if (! symbol->isVariable()) {
708 error(line, "variable expected", identifier.c_str(), "");
709 return true;
710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000712 variable = static_cast<TVariable*>(symbol);
713 if (! variable->getType().isArray()) {
714 error(line, "redeclaring non-array as array", identifier.c_str(), "");
715 return true;
716 }
717 if (variable->getType().getArraySize() > 0) {
718 error(line, "redeclaration of array with size", identifier.c_str(), "");
719 return true;
720 }
721
722 if (! variable->getType().sameElementType(TType(type))) {
723 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
724 return true;
725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000727 TType* t = variable->getArrayInformationType();
728 while (t != 0) {
729 if (t->getMaxArraySize() > type.arraySize) {
730 error(line, "higher index value already used for the array", identifier.c_str(), "");
731 return true;
732 }
733 t->setArraySize(type.arraySize);
734 t = t->getArrayInformationType();
735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 if (type.arraySize)
738 variable->getType().setArraySize(type.arraySize);
739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 if (voidErrorCheck(line, identifier, type))
742 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
747bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
748{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 bool builtIn = false;
750 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
751 if (symbol == 0) {
752 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
753 return true;
754 }
755 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 type->setArrayInformationType(variable->getArrayInformationType());
758 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000760 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
761 // its an error
762 if (node->getSymbol() == "gl_FragData") {
763 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
764 if (fragData == 0) {
765 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
766 return true;
767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
770 if (fragDataValue <= size) {
771 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
772 return true;
773 }
774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
777 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
778 if (!updateFlag)
779 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000781 size++;
782 variable->getType().setMaxArraySize(size);
783 type->setMaxArraySize(size);
784 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000786 while(tt->getArrayInformationType() != 0) {
787 tt = tt->getArrayInformationType();
788 tt->setMaxArraySize(size);
789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792}
793
794//
795// Enforce non-initializer type/qualifier rules.
796//
797// Returns true if there was an error.
798//
799bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
800{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000801 //
802 // Make the qualifier make sense.
803 //
804 if (type.qualifier == EvqConst) {
805 type.qualifier = EvqTemporary;
806 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
807 return true;
808 }
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
813//
814// Do semantic checking for a variable declaration that has no initializer,
815// and update the symbol table.
816//
817// Returns true if there was an error.
818//
819bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type)
820{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 if (reservedErrorCheck(line, identifier))
822 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 TVariable* variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 if (! symbolTable.insert(*variable)) {
827 error(line, "redefinition", variable->getName().c_str(), "");
828 delete variable;
829 return true;
830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 if (voidErrorCheck(line, identifier, type))
833 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
838bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
839{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 if (qualifier != EvqConst && qualifier != EvqTemporary) {
841 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
842 return true;
843 }
844 if (qualifier == EvqConst && paramQualifier != EvqIn) {
845 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
846 return true;
847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 if (qualifier == EvqConst)
850 type->changeQualifier(EvqConstReadOnly);
851 else
852 type->changeQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855}
856
857bool TParseContext::extensionErrorCheck(int line, const char* extension)
858{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000859 if (extensionBehavior[extension] == EBhWarn) {
860 infoSink.info.message(EPrefixWarning, ("extension " + TString(extension) + " is being used").c_str(), line);
861 return false;
862 }
863 if (extensionBehavior[extension] == EBhDisable) {
864 error(line, "extension", extension, "is disabled");
865 return true;
866 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869}
870
871/////////////////////////////////////////////////////////////////////////////////
872//
873// Non-Errors.
874//
875/////////////////////////////////////////////////////////////////////////////////
876
877//
878// Look up a function name in the symbol table, and make sure it is a function.
879//
880// Return the function symbol if found, otherwise 0.
881//
882const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
883{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 const TSymbol* symbol = symbolTable.find(call->getMangledName(), builtIn);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 if (symbol == 0) {
887 error(line, "no matching overloaded function found", call->getName().c_str(), "");
888 return 0;
889 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000891 if (! symbol->isFunction()) {
892 error(line, "function name expected", call->getName().c_str(), "");
893 return 0;
894 }
895
896 const TFunction* function = static_cast<const TFunction*>(symbol);
897
898 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
901//
902// Initializers show up in several places in the grammar. Have one set of
903// code to handle them here.
904//
905bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 if (variable == 0) {
911 if (reservedErrorCheck(line, identifier))
912 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000914 if (voidErrorCheck(line, identifier, pType))
915 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 //
918 // add variable to symbol table
919 //
920 variable = new TVariable(&identifier, type);
921 if (! symbolTable.insert(*variable)) {
922 error(line, "redefinition", variable->getName().c_str(), "");
923 return true;
924 // don't delete variable, it's used by error recovery, and the pool
925 // pop will take care of the memory
926 }
927 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 //
930 // identifier must be of type constant, a global, or a temporary
931 //
932 TQualifier qualifier = variable->getType().getQualifier();
933 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
934 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
935 return true;
936 }
937 //
938 // test for and propagate constant
939 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 if (qualifier == EvqConst) {
942 if (qualifier != initializer->getType().getQualifier()) {
943 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
944 variable->getType().changeQualifier(EvqTemporary);
945 return true;
946 }
947 if (type != initializer->getType()) {
948 error(line, " non-matching types for const initializer ",
949 variable->getType().getQualifierString(), "");
950 variable->getType().changeQualifier(EvqTemporary);
951 return true;
952 }
953 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000954 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
957 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
958 } else {
959 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
960 }
961 } else if (initializer->getAsSymbolNode()) {
962 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
963 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000965 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000966 variable->shareConstPointer(constArray);
967 } else {
968 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
969 variable->getType().changeQualifier(EvqTemporary);
970 return true;
971 }
972 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 if (qualifier != EvqConst) {
975 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
976 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
977 if (intermNode == 0) {
978 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
979 return true;
980 }
981 } else
982 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985}
986
987bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
988{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 if (!aggrNode->isConstructor())
990 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000994 // check if all the child nodes are constants so that they can be inserted into
995 // the parent node
996 if (aggrNode) {
997 TIntermSequence &childSequenceVector = aggrNode->getSequence() ;
998 for (TIntermSequence::iterator p = childSequenceVector.begin();
999 p != childSequenceVector.end(); p++) {
1000 if (!(*p)->getAsTyped()->getAsConstantUnion())
1001 return false;
1002 }
1003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
1008// This function is used to test for the correctness of the parameters passed to various constructor functions
1009// and also convert them to the right datatype if it is allowed and required.
1010//
1011// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1012//
1013TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1014{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001015 if (node == 0)
1016 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001018 TIntermAggregate* aggrNode = node->getAsAggregate();
1019
1020 TTypeList::iterator memberTypes;
1021 if (op == EOpConstructStruct)
1022 memberTypes = type->getStruct()->begin();
1023
1024 TType elementType = *type;
1025 if (type->isArray())
1026 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001028 bool singleArg;
1029 if (aggrNode) {
1030 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1031 singleArg = true;
1032 else
1033 singleArg = false;
1034 } else
1035 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 TIntermTyped *newNode;
1038 if (singleArg) {
1039 // If structure constructor or array constructor is being called
1040 // for only one parameter inside the structure, we need to call constructStruct function once.
1041 if (type->isArray())
1042 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1043 else if (op == EOpConstructStruct)
1044 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1045 else
1046 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 if (newNode && newNode->getAsAggregate()) {
1049 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1050 if (constConstructor)
1051 return constConstructor;
1052 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 return newNode;
1055 }
1056
1057 //
1058 // Handle list of arguments.
1059 //
1060 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1061 // if the structure constructor contains more than one parameter, then construct
1062 // each parameter
1063
1064 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1065
1066 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1067 // to the right type if possible (and allowed).
1068 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1069
1070 for (TIntermSequence::iterator p = sequenceVector.begin();
1071 p != sequenceVector.end(); p++, paramCount++) {
1072 if (type->isArray())
1073 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1074 else if (op == EOpConstructStruct)
1075 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1076 else
1077 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1078
1079 if (newNode) {
1080 *p = newNode;
1081 }
1082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1085 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1086 if (constConstructor)
1087 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090}
1091
1092TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1093{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 bool canBeFolded = areAllChildConst(aggrNode);
1095 aggrNode->setType(type);
1096 if (canBeFolded) {
1097 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001098 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 if (aggrNode->getSequence().size() == 1) {
1100 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1101 }
1102 else {
1103 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1104 }
1105 if (returnVal)
1106 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112}
1113
1114// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1115// for the parameter to the constructor (passed to this function). Essentially, it converts
1116// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1117// float, then float is converted to int.
1118//
1119// Returns 0 for an error or the constructed node.
1120//
1121TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1122{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001123 TIntermTyped* newNode;
1124 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001126 //
1127 // First, convert types as needed.
1128 //
1129 switch (op) {
1130 case EOpConstructVec2:
1131 case EOpConstructVec3:
1132 case EOpConstructVec4:
1133 case EOpConstructMat2:
1134 case EOpConstructMat3:
1135 case EOpConstructMat4:
1136 case EOpConstructFloat:
1137 basicOp = EOpConstructFloat;
1138 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 case EOpConstructIVec2:
1141 case EOpConstructIVec3:
1142 case EOpConstructIVec4:
1143 case EOpConstructInt:
1144 basicOp = EOpConstructInt;
1145 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001147 case EOpConstructBVec2:
1148 case EOpConstructBVec3:
1149 case EOpConstructBVec4:
1150 case EOpConstructBool:
1151 basicOp = EOpConstructBool;
1152 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001154 default:
1155 error(line, "unsupported construction", "", "");
1156 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 return 0;
1159 }
1160 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1161 if (newNode == 0) {
1162 error(line, "can't convert", "constructor", "");
1163 return 0;
1164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001166 //
1167 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1168 //
1169
1170 // Otherwise, skip out early.
1171 if (subset || (newNode != node && newNode->getType() == *type))
1172 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001174 // setAggregateOperator will insert a new node for the constructor, as needed.
1175 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176}
1177
1178// This function tests for the type of the parameters to the structures constructors. Raises
1179// an error message if the expected type does not match the parameter passed to the constructor.
1180//
1181// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1182//
1183TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1184{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 if (*type == node->getAsTyped()->getType()) {
1186 if (subset)
1187 return node->getAsTyped();
1188 else
1189 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1190 } else {
1191 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1192 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1193 recover();
1194 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001195
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001196 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001197}
1198
1199//
1200// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1201// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1202// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1203// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1204// a constant matrix.
1205//
1206TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1207{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001208 TIntermTyped* typedNode;
1209 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001211 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001212 if (tempConstantNode) {
1213 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001214
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001215 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001216 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001217 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001218
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001219 return node;
1220 }
1221 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1222 error(line, "Cannot offset into the vector", "Error", "");
1223 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001224
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001225 return 0;
1226 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001228 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001230 for (int i = 0; i < fields.num; i++) {
1231 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1232 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1233 recover();
1234 fields.offsets[i] = 0;
1235 }
1236
1237 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001239 }
1240 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1241 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001242}
1243
1244//
1245// This function returns the column being accessed from a constant matrix. The values are retrieved from
1246// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1247// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1248// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1249//
1250TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1251{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001252 TIntermTyped* typedNode;
1253 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001255 if (index >= node->getType().getNominalSize()) {
1256 error(line, "", "[", "matrix field selection out of range '%d'", index);
1257 recover();
1258 index = 0;
1259 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001261 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001262 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001263 int size = tempConstantNode->getType().getNominalSize();
1264 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1265 } else {
1266 error(line, "Cannot offset into the matrix", "Error", "");
1267 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 return 0;
1270 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001273}
1274
1275
1276//
1277// This function returns an element of an array accessed from a constant array. The values are retrieved from
1278// the symbol table and parse-tree is built for the type of the element. The input
1279// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1280// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1281//
1282TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1283{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001284 TIntermTyped* typedNode;
1285 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1286 TType arrayElementType = node->getType();
1287 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001289 if (index >= node->getType().getArraySize()) {
1290 error(line, "", "[", "array field selection out of range '%d'", index);
1291 recover();
1292 index = 0;
1293 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001295 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001297 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001298 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001299 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1300 } else {
1301 error(line, "Cannot offset into the array", "Error", "");
1302 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 return 0;
1305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308}
1309
1310
1311//
1312// This function returns the value of a particular field inside a constant structure from the symbol table.
1313// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1314// function and returns the parse-tree with the values of the embedded/nested struct.
1315//
1316TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1317{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001318 TTypeList* fields = node->getType().getStruct();
1319 TIntermTyped *typedNode;
1320 int instanceSize = 0;
1321 unsigned int index = 0;
1322 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 for ( index = 0; index < fields->size(); ++index) {
1325 if ((*fields)[index].type->getFieldName() == identifier) {
1326 break;
1327 } else {
1328 instanceSize += (*fields)[index].type->getObjectSize();
1329 }
1330 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001331
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001332 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001333 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001335 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1336 } else {
1337 error(line, "Cannot offset into the structure", "Error", "");
1338 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001340 return 0;
1341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344}
1345
1346//
1347// Initialize all supported extensions to disable
1348//
1349void TParseContext::initializeExtensionBehavior()
1350{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001351 //
1352 // example code: extensionBehavior["test"] = EBhDisable; // where "test" is the name of
1353 // supported extension
1354 //
1355 extensionBehavior["GL_ARB_texture_rectangle"] = EBhRequire;
1356 extensionBehavior["GL_3DL_array_objects"] = EBhDisable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357}
1358
1359OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1360
1361bool InitializeParseContextIndex()
1362{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1364 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1365 return false;
1366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001368 //
1369 // Allocate a TLS index.
1370 //
1371 GlobalParseContextIndex = OS_AllocTLSIndex();
1372
1373 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1374 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1375 return false;
1376 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379}
1380
1381bool InitializeGlobalParseContext()
1382{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001383 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1384 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1385 return false;
1386 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001388 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1389 if (lpParseContext != 0) {
1390 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1391 return false;
1392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001393
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001394 TThreadParseContext *lpThreadData = new TThreadParseContext();
1395 if (lpThreadData == 0) {
1396 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1397 return false;
1398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001400 lpThreadData->lpGlobalParseContext = 0;
1401 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404}
1405
1406TParseContextPointer& GetGlobalParseContext()
1407{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 //
1409 // Minimal error checking for speed
1410 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001412 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415}
1416
1417bool FreeParseContext()
1418{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001419 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1420 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1421 return false;
1422 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001423
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001424 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1425 if (lpParseContext)
1426 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001428 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429}
1430
1431bool FreeParseContextIndex()
1432{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001433 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001435 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1436 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1437 return false;
1438 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001439
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001440 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001442 return OS_FreeTLSIndex(tlsiIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443}