blob: 431f8d1ce20d1b6be6aabb9f49f9bfe085b00c66 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/ParseHelper.h"
8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000012#include "compiler/glslang.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "compiler/osinclude.h"
14#include "compiler/InitializeParseContext.h"
15
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000016extern "C" {
17extern int InitPreprocessor();
18extern int FinalizePreprocessor();
19extern void PredefineIntMacro(const char *name, int value);
20}
21
22static void ReportInfo(TInfoSinkBase& sink,
23 TPrefixType type, TSourceLoc loc,
24 const char* reason, const char* token,
25 const char* extraInfo)
26{
27 /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
28 sink.prefix(type);
29 sink.location(loc);
30 sink << "'" << token << "' : " << reason << " " << extraInfo << "\n";
31}
32
33static void DefineExtensionMacros(const TExtensionBehavior& extBehavior)
34{
35 for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
36 iter != extBehavior.end(); ++iter) {
37 PredefineIntMacro(iter->first.c_str(), 1);
38 }
39}
40
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041///////////////////////////////////////////////////////////////////////
42//
43// Sub- vector and matrix fields
44//
45////////////////////////////////////////////////////////////////////////
46
47//
48// Look at a '.' field selector string and change it into offsets
49// for a vector.
50//
51bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
52{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000053 fields.num = (int) compString.size();
54 if (fields.num > 4) {
55 error(line, "illegal vector field selection", compString.c_str(), "");
56 return false;
57 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000059 enum {
60 exyzw,
61 ergba,
62 estpq,
63 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000065 for (int i = 0; i < fields.num; ++i) {
66 switch (compString[i]) {
67 case 'x':
68 fields.offsets[i] = 0;
69 fieldSet[i] = exyzw;
70 break;
71 case 'r':
72 fields.offsets[i] = 0;
73 fieldSet[i] = ergba;
74 break;
75 case 's':
76 fields.offsets[i] = 0;
77 fieldSet[i] = estpq;
78 break;
79 case 'y':
80 fields.offsets[i] = 1;
81 fieldSet[i] = exyzw;
82 break;
83 case 'g':
84 fields.offsets[i] = 1;
85 fieldSet[i] = ergba;
86 break;
87 case 't':
88 fields.offsets[i] = 1;
89 fieldSet[i] = estpq;
90 break;
91 case 'z':
92 fields.offsets[i] = 2;
93 fieldSet[i] = exyzw;
94 break;
95 case 'b':
96 fields.offsets[i] = 2;
97 fieldSet[i] = ergba;
98 break;
99 case 'p':
100 fields.offsets[i] = 2;
101 fieldSet[i] = estpq;
102 break;
103
104 case 'w':
105 fields.offsets[i] = 3;
106 fieldSet[i] = exyzw;
107 break;
108 case 'a':
109 fields.offsets[i] = 3;
110 fieldSet[i] = ergba;
111 break;
112 case 'q':
113 fields.offsets[i] = 3;
114 fieldSet[i] = estpq;
115 break;
116 default:
117 error(line, "illegal vector field selection", compString.c_str(), "");
118 return false;
119 }
120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000122 for (int i = 0; i < fields.num; ++i) {
123 if (fields.offsets[i] >= vecSize) {
124 error(line, "vector field selection out of range", compString.c_str(), "");
125 return false;
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 if (i > 0) {
129 if (fieldSet[i] != fieldSet[i-1]) {
130 error(line, "illegal - vector component fields not from the same set", compString.c_str(), "");
131 return false;
132 }
133 }
134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137}
138
139
140//
141// Look at a '.' field selector string and change it into offsets
142// for a matrix.
143//
144bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
145{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000146 fields.wholeRow = false;
147 fields.wholeCol = false;
148 fields.row = -1;
149 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000151 if (compString.size() != 2) {
152 error(line, "illegal length of matrix field selection", compString.c_str(), "");
153 return false;
154 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 if (compString[0] == '_') {
157 if (compString[1] < '0' || compString[1] > '3') {
158 error(line, "illegal matrix field selection", compString.c_str(), "");
159 return false;
160 }
161 fields.wholeCol = true;
162 fields.col = compString[1] - '0';
163 } else if (compString[1] == '_') {
164 if (compString[0] < '0' || compString[0] > '3') {
165 error(line, "illegal matrix field selection", compString.c_str(), "");
166 return false;
167 }
168 fields.wholeRow = true;
169 fields.row = compString[0] - '0';
170 } else {
171 if (compString[0] < '0' || compString[0] > '3' ||
172 compString[1] < '0' || compString[1] > '3') {
173 error(line, "illegal matrix field selection", compString.c_str(), "");
174 return false;
175 }
176 fields.row = compString[0] - '0';
177 fields.col = compString[1] - '0';
178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000180 if (fields.row >= matSize || fields.col >= matSize) {
181 error(line, "matrix field selection out of range", compString.c_str(), "");
182 return false;
183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000185 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186}
187
188///////////////////////////////////////////////////////////////////////
189//
190// Errors
191//
192////////////////////////////////////////////////////////////////////////
193
194//
195// Track whether errors have occurred.
196//
197void TParseContext::recover()
198{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199}
200
201//
202// Used by flex/bison to output all syntax and parsing errors.
203//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000204void TParseContext::error(TSourceLoc loc,
205 const char* reason, const char* token,
206 const char* extraInfoFormat, ...)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000208 char extraInfo[512];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000209 va_list marker;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000210 va_start(marker, extraInfoFormat);
211 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000212
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000213 ReportInfo(infoSink.info, EPrefixError, loc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000214
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000215 va_end(marker);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000216 ++numErrors;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217}
218
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000219void TParseContext::warning(TSourceLoc loc,
220 const char* reason, const char* token,
221 const char* extraInfoFormat, ...) {
222 char extraInfo[512];
223 va_list marker;
224 va_start(marker, extraInfoFormat);
225 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
226
227 ReportInfo(infoSink.info, EPrefixWarning, loc, reason, token, extraInfo);
228
229 va_end(marker);
230}
231
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232//
233// Same error message for all places assignments don't work.
234//
235void TParseContext::assignError(int line, const char* op, TString left, TString right)
236{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000237 error(line, "", op, "cannot convert from '%s' to '%s'",
238 right.c_str(), left.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239}
240
241//
242// Same error message for all places unary operations don't work.
243//
244void TParseContext::unaryOpError(int line, const char* op, TString operand)
245{
246 error(line, " wrong operand type", op,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000247 "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",
248 op, operand.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249}
250
251//
252// Same error message for all binary operations don't work.
253//
254void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
255{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 error(line, " wrong operand types ", op,
257 "no operation '%s' exists that takes a left-hand operand of type '%s' and "
258 "a right operand of type '%s' (or there is no acceptable conversion)",
259 op, left.c_str(), right.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260}
261
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000262bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000263 if (!checksPrecisionErrors)
264 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000265 switch( type ){
266 case EbtFloat:
267 if( precision == EbpUndefined ){
268 error( line, "No precision specified for (float)", "", "" );
269 return true;
270 }
271 break;
272 case EbtInt:
273 if( precision == EbpUndefined ){
274 error( line, "No precision specified (int)", "", "" );
275 return true;
276 }
277 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000278 default:
279 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000280 }
281 return false;
282}
283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284//
285// Both test and if necessary, spit out an error, to see if the node is really
286// an l-value that can be operated on this way.
287//
288// Returns true if the was an error.
289//
290bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
291{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000292 TIntermSymbol* symNode = node->getAsSymbolNode();
293 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 if (binaryNode) {
296 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 switch(binaryNode->getOp()) {
299 case EOpIndexDirect:
300 case EOpIndexIndirect:
301 case EOpIndexDirectStruct:
302 return lValueErrorCheck(line, op, binaryNode->getLeft());
303 case EOpVectorSwizzle:
304 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
305 if (!errorReturn) {
306 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 TIntermTyped* rightNode = binaryNode->getRight();
309 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
310
311 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
312 p != aggrNode->getSequence().end(); p++) {
313 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
314 offset[value]++;
315 if (offset[value] > 1) {
316 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000318 return true;
319 }
320 }
321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 return errorReturn;
324 default:
325 break;
326 }
327 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000329 return true;
330 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331
332
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000333 const char* symbol = 0;
334 if (symNode != 0)
335 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 const char* message = 0;
338 switch (node->getQualifier()) {
339 case EvqConst: message = "can't modify a const"; break;
340 case EvqConstReadOnly: message = "can't modify a const"; break;
341 case EvqAttribute: message = "can't modify an attribute"; break;
342 case EvqUniform: message = "can't modify a uniform"; break;
343 case EvqVaryingIn: message = "can't modify a varying"; break;
344 case EvqInput: message = "can't modify an input"; break;
345 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
346 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
347 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
348 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000350 //
351 // Type that can't be written to?
352 //
353 switch (node->getBasicType()) {
354 case EbtSampler2D:
355 case EbtSamplerCube:
356 message = "can't modify a sampler";
357 break;
358 case EbtVoid:
359 message = "can't modify void";
360 break;
361 default:
362 break;
363 }
364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 if (message == 0 && binaryNode == 0 && symNode == 0) {
367 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 return true;
370 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371
372
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000373 //
374 // Everything else is okay, no error.
375 //
376 if (message == 0)
377 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 //
380 // If we get here, we have an error and a message.
381 //
382 if (symNode)
383 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
384 else
385 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
396bool TParseContext::constErrorCheck(TIntermTyped* node)
397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
412bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
413{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000417 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
428bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
447bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
448{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000449 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000451 if (identifier.compare(0, 3, "gl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000452 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 return true;
454 }
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000455 if (shaderSpec == SH_WEBGL_SPEC) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000456 if (identifier.compare(0, 6, "webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000457 error(line, reservedErrMsg, "webgl_", "");
458 return true;
459 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000460 if (identifier.compare(0, 7, "_webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000461 error(line, reservedErrMsg, "_webgl_", "");
462 return true;
463 }
464 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000465 if (identifier.find("__") != TString::npos) {
466 //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", "");
467 //return true;
468 infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line);
469 return false;
470 }
471 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000473 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474}
475
476//
477// Make sure there is enough data provided to the constructor to build
478// something of the type of the constructor. Also returns the type of
479// the constructor.
480//
481// Returns true if there was an error in construction.
482//
483bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
484{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 bool constructingMatrix = false;
488 switch(op) {
489 case EOpConstructMat2:
490 case EOpConstructMat3:
491 case EOpConstructMat4:
492 constructingMatrix = true;
493 break;
494 default:
495 break;
496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498 //
499 // Note: It's okay to have too many components available, but not okay to have unused
500 // arguments. 'full' will go to true when enough args have been seen. If we loop
501 // again, there is an extra argument, so 'overfull' will become true.
502 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 int size = 0;
505 bool constType = true;
506 bool full = false;
507 bool overFull = false;
508 bool matrixInMatrix = false;
509 bool arrayArg = false;
510 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000511 const TParameter& param = function.getParam(i);
512 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000513
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000514 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000515 matrixInMatrix = true;
516 if (full)
517 overFull = true;
518 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
519 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000520 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000522 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 arrayArg = true;
524 }
525
526 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000527 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
530 error(line, "array constructor needs one argument per array element", "constructor", "");
531 return true;
532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 if (arrayArg && op != EOpConstructStruct) {
535 error(line, "constructing from a non-dereferenced array", "constructor", "");
536 return true;
537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000540 if (function.getParamCount() != 1) {
541 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
542 return true;
543 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 if (overFull) {
547 error(line, "too many arguments", "constructor", "");
548 return true;
549 }
550
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000551 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
553 return true;
554 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000556 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000557 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
558 (op == EOpConstructStruct && size < type->getObjectSize())) {
559 error(line, "not enough data provided for construction", "constructor", "");
560 return true;
561 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000564 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 if (typed == 0) {
566 error(line, "constructor argument does not have a type", "constructor", "");
567 return true;
568 }
569 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
570 error(line, "cannot convert a sampler", "constructor", "");
571 return true;
572 }
573 if (typed->getBasicType() == EbtVoid) {
574 error(line, "cannot convert a void", "constructor", "");
575 return true;
576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579}
580
581// This function checks to see if a void variable has been declared and raise an error message for such a case
582//
583// returns true in case of an error
584//
585bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
586{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 if (pubType.type == EbtVoid) {
588 error(line, "illegal use of type 'void'", identifier.c_str(), "");
589 return true;
590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593}
594
595// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
596//
597// returns true in case of an error
598//
599bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
600{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
602 error(line, "boolean expression expected", "", "");
603 return true;
604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000606 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
609// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
610//
611// returns true in case of an error
612//
613bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
614{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
616 error(line, "boolean expression expected", "", "");
617 return true;
618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000620 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621}
622
623bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
624{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 if (pType.type == EbtStruct) {
626 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000627 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628
629 return true;
630 }
631
632 return false;
633 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000634 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000636 return true;
637 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640}
641
642bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
643{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
645 pType.type == EbtStruct) {
646 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
647
648 return true;
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
652 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000654 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655}
656
657bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
658{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000659 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
660 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
661 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
662 return true;
663 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666}
667
668bool TParseContext::containsSampler(TType& type)
669{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000670 if (IsSampler(type.getBasicType()))
671 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 if (type.getBasicType() == EbtStruct) {
674 TTypeList& structure = *type.getStruct();
675 for (unsigned int i = 0; i < structure.size(); ++i) {
676 if (containsSampler(*structure[i].type))
677 return true;
678 }
679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682}
683
684//
685// Do size checking for an array type's size.
686//
687// Returns true if there was an error.
688//
689bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
690{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 TIntermConstantUnion* constant = expr->getAsConstantUnion();
692 if (constant == 0 || constant->getBasicType() != EbtInt) {
693 error(line, "array size must be a constant integer expression", "", "");
694 return true;
695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000699 if (size <= 0) {
700 error(line, "array size must be a positive integer", "", "");
701 size = 1;
702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
708//
709// See if this qualifier can be an array.
710//
711// Returns true if there is an error.
712//
713bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
714{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000715 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
717 return true;
718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000720 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721}
722
723//
724// See if this type can be an array.
725//
726// Returns true if there is an error.
727//
728bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
729{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000730 //
731 // Can the type be an array?
732 //
733 if (type.array) {
734 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
735 return true;
736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739}
740
741//
742// Do all the semantic checking for declaring an array, with and
743// without a size, and make the right changes to the symbol table.
744//
745// size == 0 means no specified size.
746//
747// Returns true if there was an error.
748//
749bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
750{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 //
752 // Don't check for reserved word use until after we know it's not in the symbol table,
753 // because reserved arrays can be redeclared.
754 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 bool builtIn = false;
757 bool sameScope = false;
758 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
759 if (symbol == 0 || !sameScope) {
760 if (reservedErrorCheck(line, identifier))
761 return true;
762
763 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000765 if (type.arraySize)
766 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 if (! symbolTable.insert(*variable)) {
769 delete variable;
770 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
771 return true;
772 }
773 } else {
774 if (! symbol->isVariable()) {
775 error(line, "variable expected", identifier.c_str(), "");
776 return true;
777 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000779 variable = static_cast<TVariable*>(symbol);
780 if (! variable->getType().isArray()) {
781 error(line, "redeclaring non-array as array", identifier.c_str(), "");
782 return true;
783 }
784 if (variable->getType().getArraySize() > 0) {
785 error(line, "redeclaration of array with size", identifier.c_str(), "");
786 return true;
787 }
788
789 if (! variable->getType().sameElementType(TType(type))) {
790 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
791 return true;
792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000794 TType* t = variable->getArrayInformationType();
795 while (t != 0) {
796 if (t->getMaxArraySize() > type.arraySize) {
797 error(line, "higher index value already used for the array", identifier.c_str(), "");
798 return true;
799 }
800 t->setArraySize(type.arraySize);
801 t = t->getArrayInformationType();
802 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 if (type.arraySize)
805 variable->getType().setArraySize(type.arraySize);
806 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000808 if (voidErrorCheck(line, identifier, type))
809 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
815{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000816 bool builtIn = false;
817 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
818 if (symbol == 0) {
819 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
820 return true;
821 }
822 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 type->setArrayInformationType(variable->getArrayInformationType());
825 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
828 // its an error
829 if (node->getSymbol() == "gl_FragData") {
830 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
831 if (fragData == 0) {
832 infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line);
833 return true;
834 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000836 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
837 if (fragDataValue <= size) {
838 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
839 return true;
840 }
841 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000843 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
844 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
845 if (!updateFlag)
846 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000848 size++;
849 variable->getType().setMaxArraySize(size);
850 type->setMaxArraySize(size);
851 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 while(tt->getArrayInformationType() != 0) {
854 tt = tt->getArrayInformationType();
855 tt->setMaxArraySize(size);
856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
861//
862// Enforce non-initializer type/qualifier rules.
863//
864// Returns true if there was an error.
865//
866bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
867{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 //
869 // Make the qualifier make sense.
870 //
871 if (type.qualifier == EvqConst) {
872 type.qualifier = EvqTemporary;
873 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
874 return true;
875 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000877 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878}
879
880//
881// Do semantic checking for a variable declaration that has no initializer,
882// and update the symbol table.
883//
884// Returns true if there was an error.
885//
zmo@google.comfd747b82011-04-23 01:30:07 +0000886bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000888 if (reservedErrorCheck(line, identifier))
889 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890
zmo@google.comfd747b82011-04-23 01:30:07 +0000891 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 if (! symbolTable.insert(*variable)) {
894 error(line, "redefinition", variable->getName().c_str(), "");
895 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000896 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return true;
898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000900 if (voidErrorCheck(line, identifier, type))
901 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000903 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904}
905
906bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
907{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 if (qualifier != EvqConst && qualifier != EvqTemporary) {
909 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
910 return true;
911 }
912 if (qualifier == EvqConst && paramQualifier != EvqIn) {
913 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
914 return true;
915 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000918 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000920 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923}
924
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000925bool TParseContext::extensionErrorCheck(int line, const TString& extension)
926{
927 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
928 if (iter == extensionBehavior.end()) {
929 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 return true;
931 }
zmo@google.comf5450912011-09-09 01:37:19 +0000932 // In GLSL ES, an extension's default behavior is "disable".
933 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000934 error(line, "extension", extension.c_str(), "is disabled");
935 return true;
936 }
937 if (iter->second == EBhWarn) {
938 TString msg = "extension " + extension + " is being used";
939 infoSink.info.message(EPrefixWarning, msg.c_str(), line);
940 return false;
941 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000943 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
zmo@google.com09c323a2011-08-12 18:22:25 +0000946bool TParseContext::supportsExtension(const char* extension)
947{
948 TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension);
949 return (iter != extensionBehavior.end());
950}
951
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952/////////////////////////////////////////////////////////////////////////////////
953//
954// Non-Errors.
955//
956/////////////////////////////////////////////////////////////////////////////////
957
958//
959// Look up a function name in the symbol table, and make sure it is a function.
960//
961// Return the function symbol if found, otherwise 0.
962//
963const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
964{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000965 // First find by unmangled name to check whether the function name has been
966 // hidden by a variable name or struct typename.
967 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
968 if (symbol == 0) {
969 symbol = symbolTable.find(call->getMangledName(), builtIn);
970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
alokp@chromium.org0a576182010-08-09 17:16:27 +0000972 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 error(line, "no matching overloaded function found", call->getName().c_str(), "");
974 return 0;
975 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976
alokp@chromium.org0a576182010-08-09 17:16:27 +0000977 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000978 error(line, "function name expected", call->getName().c_str(), "");
979 return 0;
980 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000981
982 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
985//
986// Initializers show up in several places in the grammar. Have one set of
987// code to handle them here.
988//
989bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000994 if (variable == 0) {
995 if (reservedErrorCheck(line, identifier))
996 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000998 if (voidErrorCheck(line, identifier, pType))
999 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001001 //
1002 // add variable to symbol table
1003 //
1004 variable = new TVariable(&identifier, type);
1005 if (! symbolTable.insert(*variable)) {
1006 error(line, "redefinition", variable->getName().c_str(), "");
1007 return true;
1008 // don't delete variable, it's used by error recovery, and the pool
1009 // pop will take care of the memory
1010 }
1011 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001013 //
1014 // identifier must be of type constant, a global, or a temporary
1015 //
1016 TQualifier qualifier = variable->getType().getQualifier();
1017 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1018 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1019 return true;
1020 }
1021 //
1022 // test for and propagate constant
1023 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001025 if (qualifier == EvqConst) {
1026 if (qualifier != initializer->getType().getQualifier()) {
1027 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001028 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 return true;
1030 }
1031 if (type != initializer->getType()) {
1032 error(line, " non-matching types for const initializer ",
1033 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001034 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 return true;
1036 }
1037 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001038 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1041 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1042 } else {
1043 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1044 }
1045 } else if (initializer->getAsSymbolNode()) {
1046 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1047 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001049 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 variable->shareConstPointer(constArray);
1051 } else {
1052 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001053 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 return true;
1055 }
1056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 if (qualifier != EvqConst) {
1059 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1060 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1061 if (intermNode == 0) {
1062 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1063 return true;
1064 }
1065 } else
1066 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
1071bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1072{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001073 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001074 if (!aggrNode->isConstructor())
1075 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 // check if all the child nodes are constants so that they can be inserted into
1080 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001081 TIntermSequence &sequence = aggrNode->getSequence() ;
1082 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1083 if (!(*p)->getAsTyped()->getAsConstantUnion())
1084 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
1090// This function is used to test for the correctness of the parameters passed to various constructor functions
1091// and also convert them to the right datatype if it is allowed and required.
1092//
1093// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1094//
1095TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1096{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001097 if (node == 0)
1098 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 TIntermAggregate* aggrNode = node->getAsAggregate();
1101
alokp@chromium.org58e54292010-08-24 21:40:03 +00001102 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 if (op == EOpConstructStruct)
1104 memberTypes = type->getStruct()->begin();
1105
1106 TType elementType = *type;
1107 if (type->isArray())
1108 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 bool singleArg;
1111 if (aggrNode) {
1112 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1113 singleArg = true;
1114 else
1115 singleArg = false;
1116 } else
1117 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 TIntermTyped *newNode;
1120 if (singleArg) {
1121 // If structure constructor or array constructor is being called
1122 // for only one parameter inside the structure, we need to call constructStruct function once.
1123 if (type->isArray())
1124 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1125 else if (op == EOpConstructStruct)
1126 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1127 else
1128 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 if (newNode && newNode->getAsAggregate()) {
1131 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1132 if (constConstructor)
1133 return constConstructor;
1134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001136 return newNode;
1137 }
1138
1139 //
1140 // Handle list of arguments.
1141 //
1142 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1143 // if the structure constructor contains more than one parameter, then construct
1144 // each parameter
1145
1146 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1147
1148 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1149 // to the right type if possible (and allowed).
1150 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1151
1152 for (TIntermSequence::iterator p = sequenceVector.begin();
1153 p != sequenceVector.end(); p++, paramCount++) {
1154 if (type->isArray())
1155 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1156 else if (op == EOpConstructStruct)
1157 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1158 else
1159 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1160
1161 if (newNode) {
1162 *p = newNode;
1163 }
1164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001166 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1167 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1168 if (constConstructor)
1169 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001171 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172}
1173
1174TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1175{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 bool canBeFolded = areAllChildConst(aggrNode);
1177 aggrNode->setType(type);
1178 if (canBeFolded) {
1179 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001180 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 if (aggrNode->getSequence().size() == 1) {
1182 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1183 }
1184 else {
1185 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1186 }
1187 if (returnVal)
1188 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001190 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1191 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001192
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001193 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194}
1195
1196// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1197// for the parameter to the constructor (passed to this function). Essentially, it converts
1198// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1199// float, then float is converted to int.
1200//
1201// Returns 0 for an error or the constructed node.
1202//
1203TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1204{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001205 TIntermTyped* newNode;
1206 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001208 //
1209 // First, convert types as needed.
1210 //
1211 switch (op) {
1212 case EOpConstructVec2:
1213 case EOpConstructVec3:
1214 case EOpConstructVec4:
1215 case EOpConstructMat2:
1216 case EOpConstructMat3:
1217 case EOpConstructMat4:
1218 case EOpConstructFloat:
1219 basicOp = EOpConstructFloat;
1220 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001221
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001222 case EOpConstructIVec2:
1223 case EOpConstructIVec3:
1224 case EOpConstructIVec4:
1225 case EOpConstructInt:
1226 basicOp = EOpConstructInt;
1227 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001229 case EOpConstructBVec2:
1230 case EOpConstructBVec3:
1231 case EOpConstructBVec4:
1232 case EOpConstructBool:
1233 basicOp = EOpConstructBool;
1234 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001235
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001236 default:
1237 error(line, "unsupported construction", "", "");
1238 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001240 return 0;
1241 }
1242 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1243 if (newNode == 0) {
1244 error(line, "can't convert", "constructor", "");
1245 return 0;
1246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001248 //
1249 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1250 //
1251
1252 // Otherwise, skip out early.
1253 if (subset || (newNode != node && newNode->getType() == *type))
1254 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001256 // setAggregateOperator will insert a new node for the constructor, as needed.
1257 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
1260// This function tests for the type of the parameters to the structures constructors. Raises
1261// an error message if the expected type does not match the parameter passed to the constructor.
1262//
1263// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1264//
1265TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1266{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001267 if (*type == node->getAsTyped()->getType()) {
1268 if (subset)
1269 return node->getAsTyped();
1270 else
1271 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1272 } else {
1273 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1274 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1275 recover();
1276 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001278 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001279}
1280
1281//
1282// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1283// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1284// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1285// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1286// a constant matrix.
1287//
1288TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1289{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001290 TIntermTyped* typedNode;
1291 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001293 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001294 if (tempConstantNode) {
1295 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001297 if (!unionArray) { // this error message should never be raised
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001298 infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001299 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001301 return node;
1302 }
1303 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1304 error(line, "Cannot offset into the vector", "Error", "");
1305 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 return 0;
1308 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001310 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001312 for (int i = 0; i < fields.num; i++) {
1313 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1314 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1315 recover();
1316 fields.offsets[i] = 0;
1317 }
1318
1319 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001320
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001321 }
1322 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1323 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324}
1325
1326//
1327// This function returns the column being accessed from a constant matrix. The values are retrieved from
1328// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1329// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1330// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1331//
1332TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1333{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001334 TIntermTyped* typedNode;
1335 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 if (index >= node->getType().getNominalSize()) {
1338 error(line, "", "[", "matrix field selection out of range '%d'", index);
1339 recover();
1340 index = 0;
1341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001344 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001345 int size = tempConstantNode->getType().getNominalSize();
1346 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1347 } else {
1348 error(line, "Cannot offset into the matrix", "Error", "");
1349 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001351 return 0;
1352 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001353
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001354 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355}
1356
1357
1358//
1359// This function returns an element of an array accessed from a constant array. The values are retrieved from
1360// the symbol table and parse-tree is built for the type of the element. The input
1361// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1362// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1363//
1364TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1365{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001366 TIntermTyped* typedNode;
1367 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1368 TType arrayElementType = node->getType();
1369 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001371 if (index >= node->getType().getArraySize()) {
1372 error(line, "", "[", "array field selection out of range '%d'", index);
1373 recover();
1374 index = 0;
1375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001377 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001379 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001380 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001381 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1382 } else {
1383 error(line, "Cannot offset into the array", "Error", "");
1384 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001386 return 0;
1387 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001389 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001390}
1391
1392
1393//
1394// This function returns the value of a particular field inside a constant structure from the symbol table.
1395// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1396// function and returns the parse-tree with the values of the embedded/nested struct.
1397//
1398TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1399{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001400 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001401 TIntermTyped *typedNode;
1402 int instanceSize = 0;
1403 unsigned int index = 0;
1404 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 for ( index = 0; index < fields->size(); ++index) {
1407 if ((*fields)[index].type->getFieldName() == identifier) {
1408 break;
1409 } else {
1410 instanceSize += (*fields)[index].type->getObjectSize();
1411 }
1412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001415 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001417 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1418 } else {
1419 error(line, "Cannot offset into the structure", "Error", "");
1420 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001422 return 0;
1423 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001424
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001425 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001426}
1427
kbr@chromium.org476541f2011-10-27 21:14:51 +00001428bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1429{
1430 ++structNestingLevel;
1431
1432 // Embedded structure definitions are not supported per GLSL ES spec.
1433 // They aren't allowed in GLSL either, but we need to detect this here
1434 // so we don't rely on the GLSL compiler to catch it.
1435 if (structNestingLevel > 1) {
1436 error(line, "", "Embedded struct definitions are not allowed", "");
1437 return true;
1438 }
1439
1440 return false;
1441}
1442
1443void TParseContext::exitStructDeclaration()
1444{
1445 --structNestingLevel;
1446}
1447
1448namespace {
1449
1450const int kWebGLMaxStructNesting = 4;
1451
1452} // namespace
1453
1454bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1455{
1456 if (shaderSpec != SH_WEBGL_SPEC) {
1457 return false;
1458 }
1459
1460 if (fieldType.getBasicType() != EbtStruct) {
1461 return false;
1462 }
1463
1464 // We're already inside a structure definition at this point, so add
1465 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001466 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001467 error(line, "", "", "Reference of struct type %s exceeds maximum struct nesting of %d",
1468 fieldType.getTypeName().c_str(), kWebGLMaxStructNesting);
1469 return true;
1470 }
1471
1472 return false;
1473}
1474
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001475//
1476// Parse an array of strings using yyparse.
1477//
1478// Returns 0 for success.
1479//
1480int PaParseStrings(int count, const char* const string[], const int length[],
1481 TParseContext* context) {
1482 if ((count == 0) || (string == NULL))
1483 return 1;
1484
1485 // setup preprocessor.
1486 if (InitPreprocessor())
1487 return 1;
1488 DefineExtensionMacros(context->extensionBehavior);
1489
1490 if (glslang_initialize(context))
1491 return 1;
1492
1493 glslang_scan(count, string, length, context);
1494 int error = glslang_parse(context);
1495
1496 glslang_finalize(context);
1497 FinalizePreprocessor();
1498 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1499}
1500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001501OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1502
1503bool InitializeParseContextIndex()
1504{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001505 if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) {
1506 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1507 return false;
1508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001510 //
1511 // Allocate a TLS index.
1512 //
1513 GlobalParseContextIndex = OS_AllocTLSIndex();
1514
1515 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1516 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1517 return false;
1518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001520 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001521}
1522
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001523bool FreeParseContextIndex()
1524{
1525 OS_TLSIndex tlsiIndex = GlobalParseContextIndex;
1526
1527 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1528 assert(0 && "FreeParseContextIndex(): Parse Context index not initalised");
1529 return false;
1530 }
1531
1532 GlobalParseContextIndex = OS_INVALID_TLS_INDEX;
1533
1534 return OS_FreeTLSIndex(tlsiIndex);
1535}
1536
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537bool InitializeGlobalParseContext()
1538{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001539 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1540 assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised");
1541 return false;
1542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001544 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1545 if (lpParseContext != 0) {
1546 assert(0 && "InitializeParseContextIndex(): Parse Context already initalised");
1547 return false;
1548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001550 TThreadParseContext *lpThreadData = new TThreadParseContext();
1551 if (lpThreadData == 0) {
1552 assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context");
1553 return false;
1554 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001555
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001556 lpThreadData->lpGlobalParseContext = 0;
1557 OS_SetTLSValue(GlobalParseContextIndex, lpThreadData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001559 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560}
1561
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562bool FreeParseContext()
1563{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001564 if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) {
1565 assert(0 && "FreeParseContext(): Parse Context index not initalised");
1566 return false;
1567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
1570 if (lpParseContext)
1571 delete lpParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001573 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574}
1575
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001576TParseContextPointer& GetGlobalParseContext()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577{
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001578 //
1579 // Minimal error checking for speed
1580 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001582 TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001584 return lpParseContext->lpGlobalParseContext;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001586