blob: ac89e44fc45e5b5e90a140d6c9fa0741d055667b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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"
alokp@chromium.org8b851c62012-06-15 16:25:11 +000013#include "compiler/preprocessor/new/SourceLocation.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
alokp@chromium.org8b851c62012-06-15 16:25:11 +000015///////////////////////////////////////////////////////////////////////
16//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017// Sub- vector and matrix fields
18//
19////////////////////////////////////////////////////////////////////////
20
21//
22// Look at a '.' field selector string and change it into offsets
23// for a vector.
24//
25bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
26{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000027 fields.num = (int) compString.size();
28 if (fields.num > 4) {
29 error(line, "illegal vector field selection", compString.c_str(), "");
30 return false;
31 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 enum {
34 exyzw,
35 ergba,
36 estpq,
37 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
41 case 'x':
42 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
45 case 'r':
46 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
53 case 'y':
54 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
57 case 'g':
58 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
65 case 'z':
66 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
69 case 'b':
70 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
77
78 case 'w':
79 fields.offsets[i] = 3;
80 fieldSet[i] = exyzw;
81 break;
82 case 'a':
83 fields.offsets[i] = 3;
84 fieldSet[i] = ergba;
85 break;
86 case 'q':
87 fields.offsets[i] = 3;
88 fieldSet[i] = estpq;
89 break;
90 default:
91 error(line, "illegal vector field selection", compString.c_str(), "");
92 return false;
93 }
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000096 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
98 error(line, "vector field selection out of range", compString.c_str(), "");
99 return false;
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
104 error(line, "illegal - vector component fields not from the same set", compString.c_str(), "");
105 return false;
106 }
107 }
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
118bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
119{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 if (compString.size() != 2) {
126 error(line, "illegal length of matrix field selection", compString.c_str(), "");
127 return false;
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
132 error(line, "illegal matrix field selection", compString.c_str(), "");
133 return false;
134 }
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
139 error(line, "illegal matrix field selection", compString.c_str(), "");
140 return false;
141 }
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
144 } else {
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
147 error(line, "illegal matrix field selection", compString.c_str(), "");
148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000154 if (fields.row >= matSize || fields.col >= matSize) {
155 error(line, "matrix field selection out of range", compString.c_str(), "");
156 return false;
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000178void TParseContext::error(TSourceLoc loc,
179 const char* reason, const char* token,
180 const char* extraInfoFormat, ...)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000182 char extraInfo[512];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000183 va_list marker;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000184 va_start(marker, extraInfoFormat);
185 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000186
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000187 pp::SourceLocation srcLoc;
188 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
189 diagnostics.writeInfo(pp::Diagnostics::ERROR,
190 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000191
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000192 va_end(marker);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000193 ++numErrors;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194}
195
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000196void TParseContext::warning(TSourceLoc loc,
197 const char* reason, const char* token,
198 const char* extraInfoFormat, ...) {
199 char extraInfo[512];
200 va_list marker;
201 va_start(marker, extraInfoFormat);
202 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
203
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000204 pp::SourceLocation srcLoc;
205 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
206 diagnostics.writeInfo(pp::Diagnostics::WARNING,
207 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000208
209 va_end(marker);
210}
211
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000212void TParseContext::trace(const char* str)
213{
214 diagnostics.writeDebug(str);
215}
216
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217//
218// Same error message for all places assignments don't work.
219//
220void TParseContext::assignError(int line, const char* op, TString left, TString right)
221{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000222 error(line, "", op, "cannot convert from '%s' to '%s'",
223 right.c_str(), left.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224}
225
226//
227// Same error message for all places unary operations don't work.
228//
229void TParseContext::unaryOpError(int line, const char* op, TString operand)
230{
231 error(line, " wrong operand type", op,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000232 "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",
233 op, operand.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
236//
237// Same error message for all binary operations don't work.
238//
239void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
240{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000241 error(line, " wrong operand types ", op,
242 "no operation '%s' exists that takes a left-hand operand of type '%s' and "
243 "a right operand of type '%s' (or there is no acceptable conversion)",
244 op, left.c_str(), right.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245}
246
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000248 if (!checksPrecisionErrors)
249 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000250 switch( type ){
251 case EbtFloat:
252 if( precision == EbpUndefined ){
253 error( line, "No precision specified for (float)", "", "" );
254 return true;
255 }
256 break;
257 case EbtInt:
258 if( precision == EbpUndefined ){
259 error( line, "No precision specified (int)", "", "" );
260 return true;
261 }
262 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000263 default:
264 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000265 }
266 return false;
267}
268
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269//
270// Both test and if necessary, spit out an error, to see if the node is really
271// an l-value that can be operated on this way.
272//
273// Returns true if the was an error.
274//
275bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
276{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 TIntermSymbol* symNode = node->getAsSymbolNode();
278 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000280 if (binaryNode) {
281 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000283 switch(binaryNode->getOp()) {
284 case EOpIndexDirect:
285 case EOpIndexIndirect:
286 case EOpIndexDirectStruct:
287 return lValueErrorCheck(line, op, binaryNode->getLeft());
288 case EOpVectorSwizzle:
289 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
290 if (!errorReturn) {
291 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 TIntermTyped* rightNode = binaryNode->getRight();
294 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
295
296 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
297 p != aggrNode->getSequence().end(); p++) {
298 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
299 offset[value]++;
300 if (offset[value] > 1) {
301 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 return true;
304 }
305 }
306 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return errorReturn;
309 default:
310 break;
311 }
312 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 return true;
315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316
317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000318 const char* symbol = 0;
319 if (symNode != 0)
320 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 const char* message = 0;
323 switch (node->getQualifier()) {
324 case EvqConst: message = "can't modify a const"; break;
325 case EvqConstReadOnly: message = "can't modify a const"; break;
326 case EvqAttribute: message = "can't modify an attribute"; break;
327 case EvqUniform: message = "can't modify a uniform"; break;
328 case EvqVaryingIn: message = "can't modify a varying"; break;
329 case EvqInput: message = "can't modify an input"; break;
330 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
331 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
332 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
333 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000335 //
336 // Type that can't be written to?
337 //
338 switch (node->getBasicType()) {
339 case EbtSampler2D:
340 case EbtSamplerCube:
341 message = "can't modify a sampler";
342 break;
343 case EbtVoid:
344 message = "can't modify void";
345 break;
346 default:
347 break;
348 }
349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 if (message == 0 && binaryNode == 0 && symNode == 0) {
352 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 return true;
355 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
357
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000358 //
359 // Everything else is okay, no error.
360 //
361 if (message == 0)
362 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000364 //
365 // If we get here, we have an error and a message.
366 //
367 if (symNode)
368 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
369 else
370 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373}
374
375//
376// Both test, and if necessary spit out an error, to see if the node is really
377// a constant.
378//
379// Returns true if the was an error.
380//
381bool TParseContext::constErrorCheck(TIntermTyped* node)
382{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 if (node->getQualifier() == EvqConst)
384 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000386 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389}
390
391//
392// Both test, and if necessary spit out an error, to see if the node is really
393// an integer.
394//
395// Returns true if the was an error.
396//
397bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
398{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
400 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Both test, and if necessary spit out an error, to see if we are currently
409// globally scoped.
410//
411// Returns true if the was an error.
412//
413bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
414{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 if (global)
416 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423//
424// For now, keep it simple: if it starts "gl_", it's reserved, independent
425// of scope. Except, if the symbol table is at the built-in push-level,
426// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000427// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
428// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429//
430// Returns true if there was an error.
431//
432bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
433{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000434 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000436 if (identifier.compare(0, 3, "gl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000437 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000438 return true;
439 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000440 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000441 if (identifier.compare(0, 6, "webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442 error(line, reservedErrMsg, "webgl_", "");
443 return true;
444 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000445 if (identifier.compare(0, 7, "_webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000446 error(line, reservedErrMsg, "_webgl_", "");
447 return true;
448 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000449 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
450 error(line, reservedErrMsg, "css_", "");
451 return true;
452 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000453 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 if (identifier.find("__") != TString::npos) {
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000455 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str(), "", "");
456 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 }
458 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461}
462
463//
464// Make sure there is enough data provided to the constructor to build
465// something of the type of the constructor. Also returns the type of
466// the constructor.
467//
468// Returns true if there was an error in construction.
469//
470bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
471{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 bool constructingMatrix = false;
475 switch(op) {
476 case EOpConstructMat2:
477 case EOpConstructMat3:
478 case EOpConstructMat4:
479 constructingMatrix = true;
480 break;
481 default:
482 break;
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 //
486 // Note: It's okay to have too many components available, but not okay to have unused
487 // arguments. 'full' will go to true when enough args have been seen. If we loop
488 // again, there is an extra argument, so 'overfull' will become true.
489 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 int size = 0;
492 bool constType = true;
493 bool full = false;
494 bool overFull = false;
495 bool matrixInMatrix = false;
496 bool arrayArg = false;
497 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000498 const TParameter& param = function.getParam(i);
499 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000500
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000501 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 matrixInMatrix = true;
503 if (full)
504 overFull = true;
505 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
506 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000507 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000509 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 arrayArg = true;
511 }
512
513 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000514 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
517 error(line, "array constructor needs one argument per array element", "constructor", "");
518 return true;
519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 if (arrayArg && op != EOpConstructStruct) {
522 error(line, "constructing from a non-dereferenced array", "constructor", "");
523 return true;
524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000527 if (function.getParamCount() != 1) {
528 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
529 return true;
530 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 if (overFull) {
534 error(line, "too many arguments", "constructor", "");
535 return true;
536 }
537
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000538 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
540 return true;
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000543 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000544 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
545 (op == EOpConstructStruct && size < type->getObjectSize())) {
546 error(line, "not enough data provided for construction", "constructor", "");
547 return true;
548 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000551 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 if (typed == 0) {
553 error(line, "constructor argument does not have a type", "constructor", "");
554 return true;
555 }
556 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
557 error(line, "cannot convert a sampler", "constructor", "");
558 return true;
559 }
560 if (typed->getBasicType() == EbtVoid) {
561 error(line, "cannot convert a void", "constructor", "");
562 return true;
563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566}
567
568// This function checks to see if a void variable has been declared and raise an error message for such a case
569//
570// returns true in case of an error
571//
572bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
573{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 if (pubType.type == EbtVoid) {
575 error(line, "illegal use of type 'void'", identifier.c_str(), "");
576 return true;
577 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580}
581
582// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
583//
584// returns true in case of an error
585//
586bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
587{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
589 error(line, "boolean expression expected", "", "");
590 return true;
591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
596// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
597//
598// returns true in case of an error
599//
600bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
601{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
603 error(line, "boolean expression expected", "", "");
604 return true;
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
610bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
611{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 if (pType.type == EbtStruct) {
613 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000614 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615
616 return true;
617 }
618
619 return false;
620 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000621 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 return true;
624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
629bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
630{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
632 pType.type == EbtStruct) {
633 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
634
635 return true;
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
639 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
644bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
645{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
647 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
648 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
655bool TParseContext::containsSampler(TType& type)
656{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 if (IsSampler(type.getBasicType()))
658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 if (type.getBasicType() == EbtStruct) {
661 TTypeList& structure = *type.getStruct();
662 for (unsigned int i = 0; i < structure.size(); ++i) {
663 if (containsSampler(*structure[i].type))
664 return true;
665 }
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671//
672// Do size checking for an array type's size.
673//
674// Returns true if there was an error.
675//
676bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
677{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 TIntermConstantUnion* constant = expr->getAsConstantUnion();
679 if (constant == 0 || constant->getBasicType() != EbtInt) {
680 error(line, "array size must be a constant integer expression", "", "");
681 return true;
682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 if (size <= 0) {
687 error(line, "array size must be a positive integer", "", "");
688 size = 1;
689 return true;
690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000692 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693}
694
695//
696// See if this qualifier can be an array.
697//
698// Returns true if there is an error.
699//
700bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
701{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000702 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000703 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
704 return true;
705 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000707 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708}
709
710//
711// See if this type can be an array.
712//
713// Returns true if there is an error.
714//
715bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
716{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 //
718 // Can the type be an array?
719 //
720 if (type.array) {
721 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
722 return true;
723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726}
727
728//
729// Do all the semantic checking for declaring an array, with and
730// without a size, and make the right changes to the symbol table.
731//
732// size == 0 means no specified size.
733//
734// Returns true if there was an error.
735//
736bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
737{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 //
739 // Don't check for reserved word use until after we know it's not in the symbol table,
740 // because reserved arrays can be redeclared.
741 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 bool builtIn = false;
744 bool sameScope = false;
745 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
746 if (symbol == 0 || !sameScope) {
747 if (reservedErrorCheck(line, identifier))
748 return true;
749
750 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000752 if (type.arraySize)
753 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 if (! symbolTable.insert(*variable)) {
756 delete variable;
757 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
758 return true;
759 }
760 } else {
761 if (! symbol->isVariable()) {
762 error(line, "variable expected", identifier.c_str(), "");
763 return true;
764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000766 variable = static_cast<TVariable*>(symbol);
767 if (! variable->getType().isArray()) {
768 error(line, "redeclaring non-array as array", identifier.c_str(), "");
769 return true;
770 }
771 if (variable->getType().getArraySize() > 0) {
772 error(line, "redeclaration of array with size", identifier.c_str(), "");
773 return true;
774 }
775
776 if (! variable->getType().sameElementType(TType(type))) {
777 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
778 return true;
779 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000781 TType* t = variable->getArrayInformationType();
782 while (t != 0) {
783 if (t->getMaxArraySize() > type.arraySize) {
784 error(line, "higher index value already used for the array", identifier.c_str(), "");
785 return true;
786 }
787 t->setArraySize(type.arraySize);
788 t = t->getArrayInformationType();
789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 if (type.arraySize)
792 variable->getType().setArraySize(type.arraySize);
793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 if (voidErrorCheck(line, identifier, type))
796 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799}
800
801bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
802{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 bool builtIn = false;
804 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
805 if (symbol == 0) {
806 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
807 return true;
808 }
809 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 type->setArrayInformationType(variable->getArrayInformationType());
812 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
815 // its an error
816 if (node->getSymbol() == "gl_FragData") {
817 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000818 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
821 if (fragDataValue <= size) {
822 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
823 return true;
824 }
825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
828 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
829 if (!updateFlag)
830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 size++;
833 variable->getType().setMaxArraySize(size);
834 type->setMaxArraySize(size);
835 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 while(tt->getArrayInformationType() != 0) {
838 tt = tt->getArrayInformationType();
839 tt->setMaxArraySize(size);
840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000842 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
845//
846// Enforce non-initializer type/qualifier rules.
847//
848// Returns true if there was an error.
849//
850bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
851{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 //
853 // Make the qualifier make sense.
854 //
855 if (type.qualifier == EvqConst) {
856 type.qualifier = EvqTemporary;
857 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
858 return true;
859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864//
865// Do semantic checking for a variable declaration that has no initializer,
866// and update the symbol table.
867//
868// Returns true if there was an error.
869//
zmo@google.comfd747b82011-04-23 01:30:07 +0000870bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 if (reservedErrorCheck(line, identifier))
873 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
zmo@google.comfd747b82011-04-23 01:30:07 +0000875 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000877 if (! symbolTable.insert(*variable)) {
878 error(line, "redefinition", variable->getName().c_str(), "");
879 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000880 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 return true;
882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 if (voidErrorCheck(line, identifier, type))
885 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
890bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
891{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 if (qualifier != EvqConst && qualifier != EvqTemporary) {
893 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
894 return true;
895 }
896 if (qualifier == EvqConst && paramQualifier != EvqIn) {
897 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
898 return true;
899 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000901 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000902 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000903 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000904 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000909bool TParseContext::extensionErrorCheck(int line, const TString& extension)
910{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000911 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000912 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
913 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000914 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000915 return true;
916 }
zmo@google.comf5450912011-09-09 01:37:19 +0000917 // In GLSL ES, an extension's default behavior is "disable".
918 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000919 error(line, "extension", extension.c_str(), "is disabled");
920 return true;
921 }
922 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000923 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000924 return false;
925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000927 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
zmo@google.com09c323a2011-08-12 18:22:25 +0000930bool TParseContext::supportsExtension(const char* extension)
931{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000932 const TExtensionBehavior& extbehavior = extensionBehavior();
933 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
934 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000935}
936
937void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
938{
939 pp::SourceLocation loc;
940 DecodeSourceLoc(line, &loc.file, &loc.line);
941 directiveHandler.handleExtension(loc, extName, behavior);
942}
943
944void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
945{
946 pp::SourceLocation loc;
947 DecodeSourceLoc(line, &loc.file, &loc.line);
948 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000949}
950
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951/////////////////////////////////////////////////////////////////////////////////
952//
953// Non-Errors.
954//
955/////////////////////////////////////////////////////////////////////////////////
956
957//
958// Look up a function name in the symbol table, and make sure it is a function.
959//
960// Return the function symbol if found, otherwise 0.
961//
962const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
963{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000964 // First find by unmangled name to check whether the function name has been
965 // hidden by a variable name or struct typename.
966 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
967 if (symbol == 0) {
968 symbol = symbolTable.find(call->getMangledName(), builtIn);
969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
alokp@chromium.org0a576182010-08-09 17:16:27 +0000971 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 error(line, "no matching overloaded function found", call->getName().c_str(), "");
973 return 0;
974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
alokp@chromium.org0a576182010-08-09 17:16:27 +0000976 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000977 error(line, "function name expected", call->getName().c_str(), "");
978 return 0;
979 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000980
981 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982}
983
984//
985// Initializers show up in several places in the grammar. Have one set of
986// code to handle them here.
987//
988bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000991 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000993 if (variable == 0) {
994 if (reservedErrorCheck(line, identifier))
995 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000997 if (voidErrorCheck(line, identifier, pType))
998 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001000 //
1001 // add variable to symbol table
1002 //
1003 variable = new TVariable(&identifier, type);
1004 if (! symbolTable.insert(*variable)) {
1005 error(line, "redefinition", variable->getName().c_str(), "");
1006 return true;
1007 // don't delete variable, it's used by error recovery, and the pool
1008 // pop will take care of the memory
1009 }
1010 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001012 //
1013 // identifier must be of type constant, a global, or a temporary
1014 //
1015 TQualifier qualifier = variable->getType().getQualifier();
1016 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1017 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1018 return true;
1019 }
1020 //
1021 // test for and propagate constant
1022 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 if (qualifier == EvqConst) {
1025 if (qualifier != initializer->getType().getQualifier()) {
1026 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001027 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001028 return true;
1029 }
1030 if (type != initializer->getType()) {
1031 error(line, " non-matching types for const initializer ",
1032 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001033 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001034 return true;
1035 }
1036 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001037 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001039 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1040 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1041 } else {
1042 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1043 }
1044 } else if (initializer->getAsSymbolNode()) {
1045 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1046 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001048 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001049 variable->shareConstPointer(constArray);
1050 } else {
1051 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001052 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001053 return true;
1054 }
1055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001057 if (qualifier != EvqConst) {
1058 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1059 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1060 if (intermNode == 0) {
1061 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1062 return true;
1063 }
1064 } else
1065 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001066
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001067 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
1070bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1071{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001072 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 if (!aggrNode->isConstructor())
1074 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001076 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 // check if all the child nodes are constants so that they can be inserted into
1079 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001080 TIntermSequence &sequence = aggrNode->getSequence() ;
1081 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1082 if (!(*p)->getAsTyped()->getAsConstantUnion())
1083 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001086 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
1089// This function is used to test for the correctness of the parameters passed to various constructor functions
1090// and also convert them to the right datatype if it is allowed and required.
1091//
1092// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1093//
1094TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1095{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001096 if (node == 0)
1097 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 TIntermAggregate* aggrNode = node->getAsAggregate();
1100
alokp@chromium.org58e54292010-08-24 21:40:03 +00001101 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 if (op == EOpConstructStruct)
1103 memberTypes = type->getStruct()->begin();
1104
1105 TType elementType = *type;
1106 if (type->isArray())
1107 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001109 bool singleArg;
1110 if (aggrNode) {
1111 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1112 singleArg = true;
1113 else
1114 singleArg = false;
1115 } else
1116 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 TIntermTyped *newNode;
1119 if (singleArg) {
1120 // If structure constructor or array constructor is being called
1121 // for only one parameter inside the structure, we need to call constructStruct function once.
1122 if (type->isArray())
1123 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1124 else if (op == EOpConstructStruct)
1125 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1126 else
1127 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001129 if (newNode && newNode->getAsAggregate()) {
1130 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1131 if (constConstructor)
1132 return constConstructor;
1133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001135 return newNode;
1136 }
1137
1138 //
1139 // Handle list of arguments.
1140 //
1141 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1142 // if the structure constructor contains more than one parameter, then construct
1143 // each parameter
1144
1145 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1146
1147 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1148 // to the right type if possible (and allowed).
1149 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1150
1151 for (TIntermSequence::iterator p = sequenceVector.begin();
1152 p != sequenceVector.end(); p++, paramCount++) {
1153 if (type->isArray())
1154 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1155 else if (op == EOpConstructStruct)
1156 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1157 else
1158 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1159
1160 if (newNode) {
1161 *p = newNode;
1162 }
1163 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1166 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1167 if (constConstructor)
1168 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171}
1172
1173TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1174{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 bool canBeFolded = areAllChildConst(aggrNode);
1176 aggrNode->setType(type);
1177 if (canBeFolded) {
1178 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001179 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 if (aggrNode->getSequence().size() == 1) {
1181 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1182 }
1183 else {
1184 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1185 }
1186 if (returnVal)
1187 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001188
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001189 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001191
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001192 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
1195// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1196// for the parameter to the constructor (passed to this function). Essentially, it converts
1197// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1198// float, then float is converted to int.
1199//
1200// Returns 0 for an error or the constructed node.
1201//
1202TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1203{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001204 TIntermTyped* newNode;
1205 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001207 //
1208 // First, convert types as needed.
1209 //
1210 switch (op) {
1211 case EOpConstructVec2:
1212 case EOpConstructVec3:
1213 case EOpConstructVec4:
1214 case EOpConstructMat2:
1215 case EOpConstructMat3:
1216 case EOpConstructMat4:
1217 case EOpConstructFloat:
1218 basicOp = EOpConstructFloat;
1219 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001221 case EOpConstructIVec2:
1222 case EOpConstructIVec3:
1223 case EOpConstructIVec4:
1224 case EOpConstructInt:
1225 basicOp = EOpConstructInt;
1226 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 case EOpConstructBVec2:
1229 case EOpConstructBVec3:
1230 case EOpConstructBVec4:
1231 case EOpConstructBool:
1232 basicOp = EOpConstructBool;
1233 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001234
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001235 default:
1236 error(line, "unsupported construction", "", "");
1237 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001239 return 0;
1240 }
1241 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1242 if (newNode == 0) {
1243 error(line, "can't convert", "constructor", "");
1244 return 0;
1245 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001246
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001247 //
1248 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1249 //
1250
1251 // Otherwise, skip out early.
1252 if (subset || (newNode != node && newNode->getType() == *type))
1253 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001255 // setAggregateOperator will insert a new node for the constructor, as needed.
1256 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001257}
1258
1259// This function tests for the type of the parameters to the structures constructors. Raises
1260// an error message if the expected type does not match the parameter passed to the constructor.
1261//
1262// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1263//
1264TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1265{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001266 if (*type == node->getAsTyped()->getType()) {
1267 if (subset)
1268 return node->getAsTyped();
1269 else
1270 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1271 } else {
1272 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1273 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1274 recover();
1275 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001277 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001278}
1279
1280//
1281// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1282// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1283// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1284// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1285// a constant matrix.
1286//
1287TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1288{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001289 TIntermTyped* typedNode;
1290 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001292 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001293 if (tempConstantNode) {
1294 unionArray = tempConstantNode->getUnionArrayPointer();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001295 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001296
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001297 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 return node;
1299 }
1300 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1301 error(line, "Cannot offset into the vector", "Error", "");
1302 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 return 0;
1305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001307 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001309 for (int i = 0; i < fields.num; i++) {
1310 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1311 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1312 recover();
1313 fields.offsets[i] = 0;
1314 }
1315
1316 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001318 }
1319 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1320 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321}
1322
1323//
1324// This function returns the column being accessed from a constant matrix. The values are retrieved from
1325// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1326// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1327// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1328//
1329TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1330{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001331 TIntermTyped* typedNode;
1332 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001334 if (index >= node->getType().getNominalSize()) {
1335 error(line, "", "[", "matrix field selection out of range '%d'", index);
1336 recover();
1337 index = 0;
1338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001340 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001341 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 int size = tempConstantNode->getType().getNominalSize();
1343 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1344 } else {
1345 error(line, "Cannot offset into the matrix", "Error", "");
1346 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001348 return 0;
1349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001351 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001352}
1353
1354
1355//
1356// This function returns an element of an array accessed from a constant array. The values are retrieved from
1357// the symbol table and parse-tree is built for the type of the element. The input
1358// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1359// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1360//
1361TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1362{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 TIntermTyped* typedNode;
1364 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1365 TType arrayElementType = node->getType();
1366 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001368 if (index >= node->getType().getArraySize()) {
1369 error(line, "", "[", "array field selection out of range '%d'", index);
1370 recover();
1371 index = 0;
1372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001373
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001374 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001376 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001377 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1379 } else {
1380 error(line, "Cannot offset into the array", "Error", "");
1381 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001383 return 0;
1384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001386 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001387}
1388
1389
1390//
1391// This function returns the value of a particular field inside a constant structure from the symbol table.
1392// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1393// function and returns the parse-tree with the values of the embedded/nested struct.
1394//
1395TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1396{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001397 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001398 TIntermTyped *typedNode;
1399 int instanceSize = 0;
1400 unsigned int index = 0;
1401 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001403 for ( index = 0; index < fields->size(); ++index) {
1404 if ((*fields)[index].type->getFieldName() == identifier) {
1405 break;
1406 } else {
1407 instanceSize += (*fields)[index].type->getObjectSize();
1408 }
1409 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001411 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001412 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1415 } else {
1416 error(line, "Cannot offset into the structure", "Error", "");
1417 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001419 return 0;
1420 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001422 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001423}
1424
kbr@chromium.org476541f2011-10-27 21:14:51 +00001425bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1426{
1427 ++structNestingLevel;
1428
1429 // Embedded structure definitions are not supported per GLSL ES spec.
1430 // They aren't allowed in GLSL either, but we need to detect this here
1431 // so we don't rely on the GLSL compiler to catch it.
1432 if (structNestingLevel > 1) {
1433 error(line, "", "Embedded struct definitions are not allowed", "");
1434 return true;
1435 }
1436
1437 return false;
1438}
1439
1440void TParseContext::exitStructDeclaration()
1441{
1442 --structNestingLevel;
1443}
1444
1445namespace {
1446
1447const int kWebGLMaxStructNesting = 4;
1448
1449} // namespace
1450
1451bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1452{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001453 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001454 return false;
1455 }
1456
1457 if (fieldType.getBasicType() != EbtStruct) {
1458 return false;
1459 }
1460
1461 // We're already inside a structure definition at this point, so add
1462 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001463 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001464 error(line, "", "", "Reference of struct type %s exceeds maximum struct nesting of %d",
1465 fieldType.getTypeName().c_str(), kWebGLMaxStructNesting);
1466 return true;
1467 }
1468
1469 return false;
1470}
1471
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001472//
1473// Parse an array of strings using yyparse.
1474//
1475// Returns 0 for success.
1476//
1477int PaParseStrings(int count, const char* const string[], const int length[],
1478 TParseContext* context) {
1479 if ((count == 0) || (string == NULL))
1480 return 1;
1481
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001482 if (glslang_initialize(context))
1483 return 1;
1484
alokp@chromium.org408c45e2012-04-05 15:54:43 +00001485 int error = glslang_scan(count, string, length, context);
1486 if (!error)
1487 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001488
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001489 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001490
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001491 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1492}
1493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001495