blob: 396e87aa77b6ec2c202e9864e9d4d2da01fa81cc [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
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000228bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
229 switch( type ){
230 case EbtFloat:
231 if( precision == EbpUndefined ){
232 error( line, "No precision specified for (float)", "", "" );
233 return true;
234 }
235 break;
236 case EbtInt:
237 if( precision == EbpUndefined ){
238 error( line, "No precision specified (int)", "", "" );
239 return true;
240 }
241 break;
242 }
243 return false;
244}
245
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246//
247// Both test and if necessary, spit out an error, to see if the node is really
248// an l-value that can be operated on this way.
249//
250// Returns true if the was an error.
251//
252bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
253{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000254 TIntermSymbol* symNode = node->getAsSymbolNode();
255 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000257 if (binaryNode) {
258 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000260 switch(binaryNode->getOp()) {
261 case EOpIndexDirect:
262 case EOpIndexIndirect:
263 case EOpIndexDirectStruct:
264 return lValueErrorCheck(line, op, binaryNode->getLeft());
265 case EOpVectorSwizzle:
266 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
267 if (!errorReturn) {
268 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermTyped* rightNode = binaryNode->getRight();
271 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
272
273 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
274 p != aggrNode->getSequence().end(); p++) {
275 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
276 offset[value]++;
277 if (offset[value] > 1) {
278 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000280 return true;
281 }
282 }
283 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000285 return errorReturn;
286 default:
287 break;
288 }
289 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000291 return true;
292 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 const char* symbol = 0;
296 if (symNode != 0)
297 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 const char* message = 0;
300 switch (node->getQualifier()) {
301 case EvqConst: message = "can't modify a const"; break;
302 case EvqConstReadOnly: message = "can't modify a const"; break;
303 case EvqAttribute: message = "can't modify an attribute"; break;
304 case EvqUniform: message = "can't modify a uniform"; break;
305 case EvqVaryingIn: message = "can't modify a varying"; break;
306 case EvqInput: message = "can't modify an input"; break;
307 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
308 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
309 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
310 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 //
313 // Type that can't be written to?
314 //
315 switch (node->getBasicType()) {
316 case EbtSampler2D:
317 case EbtSamplerCube:
318 message = "can't modify a sampler";
319 break;
320 case EbtVoid:
321 message = "can't modify void";
322 break;
323 default:
324 break;
325 }
326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 if (message == 0 && binaryNode == 0 && symNode == 0) {
329 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return true;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
334
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000335 //
336 // Everything else is okay, no error.
337 //
338 if (message == 0)
339 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 //
342 // If we get here, we have an error and a message.
343 //
344 if (symNode)
345 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
346 else
347 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000349 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350}
351
352//
353// Both test, and if necessary spit out an error, to see if the node is really
354// a constant.
355//
356// Returns true if the was an error.
357//
358bool TParseContext::constErrorCheck(TIntermTyped* node)
359{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 if (node->getQualifier() == EvqConst)
361 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000365 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366}
367
368//
369// Both test, and if necessary spit out an error, to see if the node is really
370// an integer.
371//
372// Returns true if the was an error.
373//
374bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
375{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000376 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
377 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000381 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382}
383
384//
385// Both test, and if necessary spit out an error, to see if we are currently
386// globally scoped.
387//
388// Returns true if the was an error.
389//
390bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
391{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000392 if (global)
393 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000395 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000397 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398}
399
400//
401// For now, keep it simple: if it starts "gl_", it's reserved, independent
402// of scope. Except, if the symbol table is at the built-in push-level,
403// which is when we are parsing built-ins.
404//
405// Returns true if there was an error.
406//
407bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
408{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000409 if (!symbolTable.atBuiltInLevel()) {
410 if (identifier.substr(0, 3) == TString("gl_")) {
411 error(line, "reserved built-in name", "gl_", "");
412 return true;
413 }
414 if (identifier.find("__") != TString::npos) {
415 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
416 //return true;
417 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
418 return false;
419 }
420 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000422 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423}
424
425//
426// Make sure there is enough data provided to the constructor to build
427// something of the type of the constructor. Also returns the type of
428// the constructor.
429//
430// Returns true if there was an error in construction.
431//
432bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
433{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 bool constructingMatrix = false;
437 switch(op) {
438 case EOpConstructMat2:
439 case EOpConstructMat3:
440 case EOpConstructMat4:
441 constructingMatrix = true;
442 break;
443 default:
444 break;
445 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000447 //
448 // Note: It's okay to have too many components available, but not okay to have unused
449 // arguments. 'full' will go to true when enough args have been seen. If we loop
450 // again, there is an extra argument, so 'overfull' will become true.
451 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 int size = 0;
454 bool constType = true;
455 bool full = false;
456 bool overFull = false;
457 bool matrixInMatrix = false;
458 bool arrayArg = false;
459 for (int i = 0; i < function.getParamCount(); ++i) {
460 size += function[i].type->getObjectSize();
461
462 if (constructingMatrix && function[i].type->isMatrix())
463 matrixInMatrix = true;
464 if (full)
465 overFull = true;
466 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
467 full = true;
468 if (function[i].type->getQualifier() != EvqConst)
469 constType = false;
470 if (function[i].type->isArray())
471 arrayArg = true;
472 }
473
474 if (constType)
475 type->changeQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000477 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
478 error(line, "array constructor needs one argument per array element", "constructor", "");
479 return true;
480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 if (arrayArg && op != EOpConstructStruct) {
483 error(line, "constructing from a non-dereferenced array", "constructor", "");
484 return true;
485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000488 if (function.getParamCount() != 1) {
489 error(line, "constructing matrix from matrix can only take one argument", "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 if (overFull) {
495 error(line, "too many arguments", "constructor", "");
496 return true;
497 }
498
499 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->size() != function.getParamCount()) {
500 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
501 return true;
502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000504 if (!type->isMatrix()) {
505 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
506 (op == EOpConstructStruct && size < type->getObjectSize())) {
507 error(line, "not enough data provided for construction", "constructor", "");
508 return true;
509 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000512 TIntermTyped* typed = node->getAsTyped();
513 if (typed == 0) {
514 error(line, "constructor argument does not have a type", "constructor", "");
515 return true;
516 }
517 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
518 error(line, "cannot convert a sampler", "constructor", "");
519 return true;
520 }
521 if (typed->getBasicType() == EbtVoid) {
522 error(line, "cannot convert a void", "constructor", "");
523 return true;
524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527}
528
529// This function checks to see if a void variable has been declared and raise an error message for such a case
530//
531// returns true in case of an error
532//
533bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
534{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 if (pubType.type == EbtVoid) {
536 error(line, "illegal use of type 'void'", identifier.c_str(), "");
537 return true;
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541}
542
543// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
544//
545// returns true in case of an error
546//
547bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
548{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
550 error(line, "boolean expression expected", "", "");
551 return true;
552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555}
556
557// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
558//
559// returns true in case of an error
560//
561bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
562{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
564 error(line, "boolean expression expected", "", "");
565 return true;
566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569}
570
571bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
572{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 if (pType.type == EbtStruct) {
574 if (containsSampler(*pType.userDef)) {
575 error(line, reason, TType::getBasicString(pType.type), "(structure contains a sampler)");
576
577 return true;
578 }
579
580 return false;
581 } else if (IsSampler(pType.type)) {
582 error(line, reason, TType::getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 return true;
585 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000586
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588}
589
590bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
591{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
593 pType.type == EbtStruct) {
594 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
595
596 return true;
597 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
600 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603}
604
605bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
606{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
608 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
609 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
610 return true;
611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614}
615
616bool TParseContext::containsSampler(TType& type)
617{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 if (IsSampler(type.getBasicType()))
619 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 if (type.getBasicType() == EbtStruct) {
622 TTypeList& structure = *type.getStruct();
623 for (unsigned int i = 0; i < structure.size(); ++i) {
624 if (containsSampler(*structure[i].type))
625 return true;
626 }
627 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630}
631
632//
633// Do size checking for an array type's size.
634//
635// Returns true if there was an error.
636//
637bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
638{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 TIntermConstantUnion* constant = expr->getAsConstantUnion();
640 if (constant == 0 || constant->getBasicType() != EbtInt) {
641 error(line, "array size must be a constant integer expression", "", "");
642 return true;
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000647 if (size <= 0) {
648 error(line, "array size must be a positive integer", "", "");
649 size = 1;
650 return true;
651 }
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 qualifier can be an array.
658//
659// Returns true if there is an error.
660//
661bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
662{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 if (type.qualifier == EvqAttribute) {
664 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
665 return true;
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 if (type.qualifier == EvqConst && extensionErrorCheck(line, "GL_3DL_array_objects"))
669 return true;
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// See if this type can be an array.
676//
677// Returns true if there is an error.
678//
679bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
680{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 //
682 // Can the type be an array?
683 //
684 if (type.array) {
685 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
686 return true;
687 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000689 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690}
691
692//
693// Do all the semantic checking for declaring an array, with and
694// without a size, and make the right changes to the symbol table.
695//
696// size == 0 means no specified size.
697//
698// Returns true if there was an error.
699//
700bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
701{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 //
703 // Don't check for reserved word use until after we know it's not in the symbol table,
704 // because reserved arrays can be redeclared.
705 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000707 bool builtIn = false;
708 bool sameScope = false;
709 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
710 if (symbol == 0 || !sameScope) {
711 if (reservedErrorCheck(line, identifier))
712 return true;
713
714 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 if (type.arraySize)
717 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000719 if (! symbolTable.insert(*variable)) {
720 delete variable;
721 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
722 return true;
723 }
724 } else {
725 if (! symbol->isVariable()) {
726 error(line, "variable expected", identifier.c_str(), "");
727 return true;
728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000730 variable = static_cast<TVariable*>(symbol);
731 if (! variable->getType().isArray()) {
732 error(line, "redeclaring non-array as array", identifier.c_str(), "");
733 return true;
734 }
735 if (variable->getType().getArraySize() > 0) {
736 error(line, "redeclaration of array with size", identifier.c_str(), "");
737 return true;
738 }
739
740 if (! variable->getType().sameElementType(TType(type))) {
741 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
742 return true;
743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 TType* t = variable->getArrayInformationType();
746 while (t != 0) {
747 if (t->getMaxArraySize() > type.arraySize) {
748 error(line, "higher index value already used for the array", identifier.c_str(), "");
749 return true;
750 }
751 t->setArraySize(type.arraySize);
752 t = t->getArrayInformationType();
753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 if (type.arraySize)
756 variable->getType().setArraySize(type.arraySize);
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 if (voidErrorCheck(line, identifier, type))
760 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
765bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
766{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000767 bool builtIn = false;
768 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
769 if (symbol == 0) {
770 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
771 return true;
772 }
773 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 type->setArrayInformationType(variable->getArrayInformationType());
776 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
779 // its an error
780 if (node->getSymbol() == "gl_FragData") {
781 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
782 if (fragData == 0) {
783 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
784 return true;
785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000787 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
788 if (fragDataValue <= size) {
789 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
790 return true;
791 }
792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000794 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
795 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
796 if (!updateFlag)
797 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 size++;
800 variable->getType().setMaxArraySize(size);
801 type->setMaxArraySize(size);
802 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 while(tt->getArrayInformationType() != 0) {
805 tt = tt->getArrayInformationType();
806 tt->setMaxArraySize(size);
807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810}
811
812//
813// Enforce non-initializer type/qualifier rules.
814//
815// Returns true if there was an error.
816//
817bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
818{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 //
820 // Make the qualifier make sense.
821 //
822 if (type.qualifier == EvqConst) {
823 type.qualifier = EvqTemporary;
824 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
825 return true;
826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829}
830
831//
832// Do semantic checking for a variable declaration that has no initializer,
833// and update the symbol table.
834//
835// Returns true if there was an error.
836//
837bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type)
838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 if (reservedErrorCheck(line, identifier))
840 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000842 TVariable* variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 if (! symbolTable.insert(*variable)) {
845 error(line, "redefinition", variable->getName().c_str(), "");
846 delete variable;
847 return true;
848 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 if (voidErrorCheck(line, identifier, type))
851 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
856bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
857{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 if (qualifier != EvqConst && qualifier != EvqTemporary) {
859 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
860 return true;
861 }
862 if (qualifier == EvqConst && paramQualifier != EvqIn) {
863 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
864 return true;
865 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000867 if (qualifier == EvqConst)
868 type->changeQualifier(EvqConstReadOnly);
869 else
870 type->changeQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
875bool TParseContext::extensionErrorCheck(int line, const char* extension)
876{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000877 if (extensionBehavior[extension] == EBhWarn) {
878 infoSink.info.message(EPrefixWarning, ("extension " + TString(extension) + " is being used").c_str(), line);
879 return false;
880 }
881 if (extensionBehavior[extension] == EBhDisable) {
882 error(line, "extension", extension, "is disabled");
883 return true;
884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889/////////////////////////////////////////////////////////////////////////////////
890//
891// Non-Errors.
892//
893/////////////////////////////////////////////////////////////////////////////////
894
895//
896// Look up a function name in the symbol table, and make sure it is a function.
897//
898// Return the function symbol if found, otherwise 0.
899//
900const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
901{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 const TSymbol* symbol = symbolTable.find(call->getMangledName(), builtIn);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 if (symbol == 0) {
905 error(line, "no matching overloaded function found", call->getName().c_str(), "");
906 return 0;
907 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 if (! symbol->isFunction()) {
910 error(line, "function name expected", call->getName().c_str(), "");
911 return 0;
912 }
913
914 const TFunction* function = static_cast<const TFunction*>(symbol);
915
916 return function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
919//
920// Initializers show up in several places in the grammar. Have one set of
921// code to handle them here.
922//
923bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 if (variable == 0) {
929 if (reservedErrorCheck(line, identifier))
930 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 if (voidErrorCheck(line, identifier, pType))
933 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000935 //
936 // add variable to symbol table
937 //
938 variable = new TVariable(&identifier, type);
939 if (! symbolTable.insert(*variable)) {
940 error(line, "redefinition", variable->getName().c_str(), "");
941 return true;
942 // don't delete variable, it's used by error recovery, and the pool
943 // pop will take care of the memory
944 }
945 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000947 //
948 // identifier must be of type constant, a global, or a temporary
949 //
950 TQualifier qualifier = variable->getType().getQualifier();
951 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
952 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
953 return true;
954 }
955 //
956 // test for and propagate constant
957 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 if (qualifier == EvqConst) {
960 if (qualifier != initializer->getType().getQualifier()) {
961 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
962 variable->getType().changeQualifier(EvqTemporary);
963 return true;
964 }
965 if (type != initializer->getType()) {
966 error(line, " non-matching types for const initializer ",
967 variable->getType().getQualifierString(), "");
968 variable->getType().changeQualifier(EvqTemporary);
969 return true;
970 }
971 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000972 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
975 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
976 } else {
977 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
978 }
979 } else if (initializer->getAsSymbolNode()) {
980 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
981 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000983 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 variable->shareConstPointer(constArray);
985 } else {
986 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
987 variable->getType().changeQualifier(EvqTemporary);
988 return true;
989 }
990 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 if (qualifier != EvqConst) {
993 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
994 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
995 if (intermNode == 0) {
996 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
997 return true;
998 }
999 } else
1000 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001002 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003}
1004
1005bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1006{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 if (!aggrNode->isConstructor())
1008 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001012 // check if all the child nodes are constants so that they can be inserted into
1013 // the parent node
1014 if (aggrNode) {
1015 TIntermSequence &childSequenceVector = aggrNode->getSequence() ;
1016 for (TIntermSequence::iterator p = childSequenceVector.begin();
1017 p != childSequenceVector.end(); p++) {
1018 if (!(*p)->getAsTyped()->getAsConstantUnion())
1019 return false;
1020 }
1021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024}
1025
1026// This function is used to test for the correctness of the parameters passed to various constructor functions
1027// and also convert them to the right datatype if it is allowed and required.
1028//
1029// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1030//
1031TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1032{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001033 if (node == 0)
1034 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 TIntermAggregate* aggrNode = node->getAsAggregate();
1037
1038 TTypeList::iterator memberTypes;
1039 if (op == EOpConstructStruct)
1040 memberTypes = type->getStruct()->begin();
1041
1042 TType elementType = *type;
1043 if (type->isArray())
1044 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 bool singleArg;
1047 if (aggrNode) {
1048 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1049 singleArg = true;
1050 else
1051 singleArg = false;
1052 } else
1053 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001055 TIntermTyped *newNode;
1056 if (singleArg) {
1057 // If structure constructor or array constructor is being called
1058 // for only one parameter inside the structure, we need to call constructStruct function once.
1059 if (type->isArray())
1060 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1061 else if (op == EOpConstructStruct)
1062 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1063 else
1064 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 if (newNode && newNode->getAsAggregate()) {
1067 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1068 if (constConstructor)
1069 return constConstructor;
1070 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001072 return newNode;
1073 }
1074
1075 //
1076 // Handle list of arguments.
1077 //
1078 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1079 // if the structure constructor contains more than one parameter, then construct
1080 // each parameter
1081
1082 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1083
1084 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1085 // to the right type if possible (and allowed).
1086 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1087
1088 for (TIntermSequence::iterator p = sequenceVector.begin();
1089 p != sequenceVector.end(); p++, paramCount++) {
1090 if (type->isArray())
1091 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1092 else if (op == EOpConstructStruct)
1093 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1094 else
1095 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1096
1097 if (newNode) {
1098 *p = newNode;
1099 }
1100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1103 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1104 if (constConstructor)
1105 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001107 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108}
1109
1110TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1111{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 bool canBeFolded = areAllChildConst(aggrNode);
1113 aggrNode->setType(type);
1114 if (canBeFolded) {
1115 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001116 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001117 if (aggrNode->getSequence().size() == 1) {
1118 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1119 }
1120 else {
1121 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1122 }
1123 if (returnVal)
1124 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001126 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001129 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130}
1131
1132// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1133// for the parameter to the constructor (passed to this function). Essentially, it converts
1134// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1135// float, then float is converted to int.
1136//
1137// Returns 0 for an error or the constructed node.
1138//
1139TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1140{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001141 TIntermTyped* newNode;
1142 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001144 //
1145 // First, convert types as needed.
1146 //
1147 switch (op) {
1148 case EOpConstructVec2:
1149 case EOpConstructVec3:
1150 case EOpConstructVec4:
1151 case EOpConstructMat2:
1152 case EOpConstructMat3:
1153 case EOpConstructMat4:
1154 case EOpConstructFloat:
1155 basicOp = EOpConstructFloat;
1156 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 case EOpConstructIVec2:
1159 case EOpConstructIVec3:
1160 case EOpConstructIVec4:
1161 case EOpConstructInt:
1162 basicOp = EOpConstructInt;
1163 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 case EOpConstructBVec2:
1166 case EOpConstructBVec3:
1167 case EOpConstructBVec4:
1168 case EOpConstructBool:
1169 basicOp = EOpConstructBool;
1170 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001172 default:
1173 error(line, "unsupported construction", "", "");
1174 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 return 0;
1177 }
1178 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1179 if (newNode == 0) {
1180 error(line, "can't convert", "constructor", "");
1181 return 0;
1182 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001184 //
1185 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1186 //
1187
1188 // Otherwise, skip out early.
1189 if (subset || (newNode != node && newNode->getType() == *type))
1190 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001191
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001192 // setAggregateOperator will insert a new node for the constructor, as needed.
1193 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194}
1195
1196// This function tests for the type of the parameters to the structures constructors. Raises
1197// an error message if the expected type does not match the parameter passed to the constructor.
1198//
1199// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1200//
1201TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1202{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001203 if (*type == node->getAsTyped()->getType()) {
1204 if (subset)
1205 return node->getAsTyped();
1206 else
1207 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1208 } else {
1209 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1210 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1211 recover();
1212 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001213
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001214 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001215}
1216
1217//
1218// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1219// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1220// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1221// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1222// a constant matrix.
1223//
1224TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1225{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001226 TIntermTyped* typedNode;
1227 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001229 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001230 if (tempConstantNode) {
1231 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001233 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001234 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001235 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001236
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001237 return node;
1238 }
1239 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1240 error(line, "Cannot offset into the vector", "Error", "");
1241 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001242
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001243 return 0;
1244 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001245
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001246 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001248 for (int i = 0; i < fields.num; i++) {
1249 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1250 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1251 recover();
1252 fields.offsets[i] = 0;
1253 }
1254
1255 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 }
1258 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1259 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260}
1261
1262//
1263// This function returns the column being accessed from a constant matrix. The values are retrieved from
1264// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1265// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1266// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1267//
1268TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001270 TIntermTyped* typedNode;
1271 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001273 if (index >= node->getType().getNominalSize()) {
1274 error(line, "", "[", "matrix field selection out of range '%d'", index);
1275 recover();
1276 index = 0;
1277 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001278
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001279 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001280 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001281 int size = tempConstantNode->getType().getNominalSize();
1282 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1283 } else {
1284 error(line, "Cannot offset into the matrix", "Error", "");
1285 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001287 return 0;
1288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001290 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291}
1292
1293
1294//
1295// This function returns an element of an array accessed from a constant array. The values are retrieved from
1296// the symbol table and parse-tree is built for the type of the element. The input
1297// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1298// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1299//
1300TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1301{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001302 TIntermTyped* typedNode;
1303 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1304 TType arrayElementType = node->getType();
1305 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 if (index >= node->getType().getArraySize()) {
1308 error(line, "", "[", "array field selection out of range '%d'", index);
1309 recover();
1310 index = 0;
1311 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001313 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001314
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001316 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001317 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1318 } else {
1319 error(line, "Cannot offset into the array", "Error", "");
1320 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001322 return 0;
1323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001325 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001326}
1327
1328
1329//
1330// This function returns the value of a particular field inside a constant structure from the symbol table.
1331// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1332// function and returns the parse-tree with the values of the embedded/nested struct.
1333//
1334TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1335{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 TTypeList* fields = node->getType().getStruct();
1337 TIntermTyped *typedNode;
1338 int instanceSize = 0;
1339 unsigned int index = 0;
1340 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 for ( index = 0; index < fields->size(); ++index) {
1343 if ((*fields)[index].type->getFieldName() == identifier) {
1344 break;
1345 } else {
1346 instanceSize += (*fields)[index].type->getObjectSize();
1347 }
1348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001350 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001351 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001352
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001353 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1354 } else {
1355 error(line, "Cannot offset into the structure", "Error", "");
1356 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001358 return 0;
1359 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001360
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001361 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001362}
1363
1364//
1365// Initialize all supported extensions to disable
1366//
1367void TParseContext::initializeExtensionBehavior()
1368{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001369 //
1370 // example code: extensionBehavior["test"] = EBhDisable; // where "test" is the name of
1371 // supported extension
1372 //
1373 extensionBehavior["GL_ARB_texture_rectangle"] = EBhRequire;
1374 extensionBehavior["GL_3DL_array_objects"] = EBhDisable;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375}
1376
1377OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1378
1379bool InitializeParseContextIndex()
1380{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001381 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1382 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1383 return false;
1384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001386 //
1387 // Allocate a TLS index.
1388 //
1389 GlobalParseContextIndex = OS_AllocTLSIndex();
1390
1391 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1392 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1393 return false;
1394 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001396 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397}
1398
1399bool InitializeGlobalParseContext()
1400{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001401 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1402 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1403 return false;
1404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1407 if (lpParseContext != 0) {
1408 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1409 return false;
1410 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001412 TThreadParseContext *lpThreadData = new TThreadParseContext();
1413 if (lpThreadData == 0) {
1414 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1415 return false;
1416 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001418 lpThreadData->lpGlobalParseContext = 0;
1419 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001421 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001422}
1423
1424TParseContextPointer& GetGlobalParseContext()
1425{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001426 //
1427 // Minimal error checking for speed
1428 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001430 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001432 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001433}
1434
1435bool FreeParseContext()
1436{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001437 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1438 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1439 return false;
1440 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001442 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1443 if (lpParseContext)
1444 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001445
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001446 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447}
1448
1449bool FreeParseContextIndex()
1450{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001451 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001453 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1454 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1455 return false;
1456 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001458 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001459
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001460 return OS_FreeTLSIndex(tlsiIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001461}