blob: 5e700e07de176533709fffa379e89ed04d0f8128 [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.org044a5cf2010-11-12 15:42:16 +000015extern "C" {
16extern int InitPreprocessor();
17extern int FinalizePreprocessor();
18extern void PredefineIntMacro(const char *name, int value);
19}
20
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000021static void DefineExtensionMacros(const TExtensionBehavior& extBehavior)
22{
23 for (TExtensionBehavior::const_iterator iter = extBehavior.begin();
24 iter != extBehavior.end(); ++iter) {
25 PredefineIntMacro(iter->first.c_str(), 1);
26 }
27}
28
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029///////////////////////////////////////////////////////////////////////
30//
alokp@chromium.org8b851c62012-06-15 16:25:11 +000031// Preprocessor
32//
33////////////////////////////////////////////////////////////////////////
34
35bool TParseContext::initPreprocessor()
36{
37 if (InitPreprocessor())
38 return false;
39
40 DefineExtensionMacros(directiveHandler.extBehavior());
41 return true;
42}
43
44void TParseContext::destroyPreprocessor()
45{
46 FinalizePreprocessor();
47}
48
49///////////////////////////////////////////////////////////////////////
50//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051// Sub- vector and matrix fields
52//
53////////////////////////////////////////////////////////////////////////
54
55//
56// Look at a '.' field selector string and change it into offsets
57// for a vector.
58//
59bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
60{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000061 fields.num = (int) compString.size();
62 if (fields.num > 4) {
63 error(line, "illegal vector field selection", compString.c_str(), "");
64 return false;
65 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000067 enum {
68 exyzw,
69 ergba,
70 estpq,
71 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000073 for (int i = 0; i < fields.num; ++i) {
74 switch (compString[i]) {
75 case 'x':
76 fields.offsets[i] = 0;
77 fieldSet[i] = exyzw;
78 break;
79 case 'r':
80 fields.offsets[i] = 0;
81 fieldSet[i] = ergba;
82 break;
83 case 's':
84 fields.offsets[i] = 0;
85 fieldSet[i] = estpq;
86 break;
87 case 'y':
88 fields.offsets[i] = 1;
89 fieldSet[i] = exyzw;
90 break;
91 case 'g':
92 fields.offsets[i] = 1;
93 fieldSet[i] = ergba;
94 break;
95 case 't':
96 fields.offsets[i] = 1;
97 fieldSet[i] = estpq;
98 break;
99 case 'z':
100 fields.offsets[i] = 2;
101 fieldSet[i] = exyzw;
102 break;
103 case 'b':
104 fields.offsets[i] = 2;
105 fieldSet[i] = ergba;
106 break;
107 case 'p':
108 fields.offsets[i] = 2;
109 fieldSet[i] = estpq;
110 break;
111
112 case 'w':
113 fields.offsets[i] = 3;
114 fieldSet[i] = exyzw;
115 break;
116 case 'a':
117 fields.offsets[i] = 3;
118 fieldSet[i] = ergba;
119 break;
120 case 'q':
121 fields.offsets[i] = 3;
122 fieldSet[i] = estpq;
123 break;
124 default:
125 error(line, "illegal vector field selection", compString.c_str(), "");
126 return false;
127 }
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 for (int i = 0; i < fields.num; ++i) {
131 if (fields.offsets[i] >= vecSize) {
132 error(line, "vector field selection out of range", compString.c_str(), "");
133 return false;
134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 if (i > 0) {
137 if (fieldSet[i] != fieldSet[i-1]) {
138 error(line, "illegal - vector component fields not from the same set", compString.c_str(), "");
139 return false;
140 }
141 }
142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000144 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145}
146
147
148//
149// Look at a '.' field selector string and change it into offsets
150// for a matrix.
151//
152bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
153{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000154 fields.wholeRow = false;
155 fields.wholeCol = false;
156 fields.row = -1;
157 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 if (compString.size() != 2) {
160 error(line, "illegal length of matrix field selection", compString.c_str(), "");
161 return false;
162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000164 if (compString[0] == '_') {
165 if (compString[1] < '0' || compString[1] > '3') {
166 error(line, "illegal matrix field selection", compString.c_str(), "");
167 return false;
168 }
169 fields.wholeCol = true;
170 fields.col = compString[1] - '0';
171 } else if (compString[1] == '_') {
172 if (compString[0] < '0' || compString[0] > '3') {
173 error(line, "illegal matrix field selection", compString.c_str(), "");
174 return false;
175 }
176 fields.wholeRow = true;
177 fields.row = compString[0] - '0';
178 } else {
179 if (compString[0] < '0' || compString[0] > '3' ||
180 compString[1] < '0' || compString[1] > '3') {
181 error(line, "illegal matrix field selection", compString.c_str(), "");
182 return false;
183 }
184 fields.row = compString[0] - '0';
185 fields.col = compString[1] - '0';
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000188 if (fields.row >= matSize || fields.col >= matSize) {
189 error(line, "matrix field selection out of range", compString.c_str(), "");
190 return false;
191 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000193 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194}
195
196///////////////////////////////////////////////////////////////////////
197//
198// Errors
199//
200////////////////////////////////////////////////////////////////////////
201
202//
203// Track whether errors have occurred.
204//
205void TParseContext::recover()
206{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207}
208
209//
210// Used by flex/bison to output all syntax and parsing errors.
211//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000212void TParseContext::error(TSourceLoc loc,
213 const char* reason, const char* token,
214 const char* extraInfoFormat, ...)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000216 char extraInfo[512];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000217 va_list marker;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000218 va_start(marker, extraInfoFormat);
219 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000220
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000221 pp::SourceLocation srcLoc;
222 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
223 diagnostics.writeInfo(pp::Diagnostics::ERROR,
224 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000225
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000226 va_end(marker);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000227 ++numErrors;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228}
229
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000230void TParseContext::warning(TSourceLoc loc,
231 const char* reason, const char* token,
232 const char* extraInfoFormat, ...) {
233 char extraInfo[512];
234 va_list marker;
235 va_start(marker, extraInfoFormat);
236 vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker);
237
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000238 pp::SourceLocation srcLoc;
239 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
240 diagnostics.writeInfo(pp::Diagnostics::WARNING,
241 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000242
243 va_end(marker);
244}
245
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000246void TParseContext::trace(const char* str)
247{
248 diagnostics.writeDebug(str);
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Same error message for all places assignments don't work.
253//
254void TParseContext::assignError(int line, const char* op, TString left, TString right)
255{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 error(line, "", op, "cannot convert from '%s' to '%s'",
257 right.c_str(), left.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258}
259
260//
261// Same error message for all places unary operations don't work.
262//
263void TParseContext::unaryOpError(int line, const char* op, TString operand)
264{
265 error(line, " wrong operand type", op,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000266 "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)",
267 op, operand.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268}
269
270//
271// Same error message for all binary operations don't work.
272//
273void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
274{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000275 error(line, " wrong operand types ", op,
276 "no operation '%s' exists that takes a left-hand operand of type '%s' and "
277 "a right operand of type '%s' (or there is no acceptable conversion)",
278 op, left.c_str(), right.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}
280
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000281bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000282 if (!checksPrecisionErrors)
283 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000284 switch( type ){
285 case EbtFloat:
286 if( precision == EbpUndefined ){
287 error( line, "No precision specified for (float)", "", "" );
288 return true;
289 }
290 break;
291 case EbtInt:
292 if( precision == EbpUndefined ){
293 error( line, "No precision specified (int)", "", "" );
294 return true;
295 }
296 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000297 default:
298 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000299 }
300 return false;
301}
302
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303//
304// Both test and if necessary, spit out an error, to see if the node is really
305// an l-value that can be operated on this way.
306//
307// Returns true if the was an error.
308//
309bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
310{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000311 TIntermSymbol* symNode = node->getAsSymbolNode();
312 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 if (binaryNode) {
315 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 switch(binaryNode->getOp()) {
318 case EOpIndexDirect:
319 case EOpIndexIndirect:
320 case EOpIndexDirectStruct:
321 return lValueErrorCheck(line, op, binaryNode->getLeft());
322 case EOpVectorSwizzle:
323 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
324 if (!errorReturn) {
325 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000327 TIntermTyped* rightNode = binaryNode->getRight();
328 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
329
330 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
331 p != aggrNode->getSequence().end(); p++) {
332 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
333 offset[value]++;
334 if (offset[value] > 1) {
335 error(line, " l-value of swizzle cannot have duplicate components", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 return true;
338 }
339 }
340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 return errorReturn;
343 default:
344 break;
345 }
346 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000348 return true;
349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
351
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000352 const char* symbol = 0;
353 if (symNode != 0)
354 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000356 const char* message = 0;
357 switch (node->getQualifier()) {
358 case EvqConst: message = "can't modify a const"; break;
359 case EvqConstReadOnly: message = "can't modify a const"; break;
360 case EvqAttribute: message = "can't modify an attribute"; break;
361 case EvqUniform: message = "can't modify a uniform"; break;
362 case EvqVaryingIn: message = "can't modify a varying"; break;
363 case EvqInput: message = "can't modify an input"; break;
364 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
365 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
366 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
367 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // Type that can't be written to?
371 //
372 switch (node->getBasicType()) {
373 case EbtSampler2D:
374 case EbtSamplerCube:
375 message = "can't modify a sampler";
376 break;
377 case EbtVoid:
378 message = "can't modify void";
379 break;
380 default:
381 break;
382 }
383 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000385 if (message == 0 && binaryNode == 0 && symNode == 0) {
386 error(line, " l-value required", op, "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 return true;
389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
391
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000392 //
393 // Everything else is okay, no error.
394 //
395 if (message == 0)
396 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 //
399 // If we get here, we have an error and a message.
400 //
401 if (symNode)
402 error(line, " l-value required", op, "\"%s\" (%s)", symbol, message);
403 else
404 error(line, " l-value required", op, "(%s)", message);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000406 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407}
408
409//
410// Both test, and if necessary spit out an error, to see if the node is really
411// a constant.
412//
413// Returns true if the was an error.
414//
415bool TParseContext::constErrorCheck(TIntermTyped* node)
416{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000417 if (node->getQualifier() == EvqConst)
418 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 error(node->getLine(), "constant expression required", "", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000422 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423}
424
425//
426// Both test, and if necessary spit out an error, to see if the node is really
427// an integer.
428//
429// Returns true if the was an error.
430//
431bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
432{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
434 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 error(node->getLine(), "integer expression required", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000438 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439}
440
441//
442// Both test, and if necessary spit out an error, to see if we are currently
443// globally scoped.
444//
445// Returns true if the was an error.
446//
447bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
448{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000449 if (global)
450 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 error(line, "only allowed at global scope", token, "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
456
457//
458// For now, keep it simple: if it starts "gl_", it's reserved, independent
459// of scope. Except, if the symbol table is at the built-in push-level,
460// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000461// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
462// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463//
464// Returns true if there was an error.
465//
466bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
467{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000468 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000470 if (identifier.compare(0, 3, "gl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000471 error(line, reservedErrMsg, "gl_", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 return true;
473 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000474 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000475 if (identifier.compare(0, 6, "webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000476 error(line, reservedErrMsg, "webgl_", "");
477 return true;
478 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000479 if (identifier.compare(0, 7, "_webgl_") == 0) {
alokp@chromium.org613ef312010-07-21 18:54:22 +0000480 error(line, reservedErrMsg, "_webgl_", "");
481 return true;
482 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000483 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
484 error(line, reservedErrMsg, "css_", "");
485 return true;
486 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000487 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000488 if (identifier.find("__") != TString::npos) {
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000489 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str(), "", "");
490 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 }
492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495}
496
497//
498// Make sure there is enough data provided to the constructor to build
499// something of the type of the constructor. Also returns the type of
500// the constructor.
501//
502// Returns true if there was an error in construction.
503//
504bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
505{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 bool constructingMatrix = false;
509 switch(op) {
510 case EOpConstructMat2:
511 case EOpConstructMat3:
512 case EOpConstructMat4:
513 constructingMatrix = true;
514 break;
515 default:
516 break;
517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 //
520 // Note: It's okay to have too many components available, but not okay to have unused
521 // arguments. 'full' will go to true when enough args have been seen. If we loop
522 // again, there is an extra argument, so 'overfull' will become true.
523 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 int size = 0;
526 bool constType = true;
527 bool full = false;
528 bool overFull = false;
529 bool matrixInMatrix = false;
530 bool arrayArg = false;
531 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000532 const TParameter& param = function.getParam(i);
533 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000535 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 matrixInMatrix = true;
537 if (full)
538 overFull = true;
539 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
540 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000541 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000543 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000544 arrayArg = true;
545 }
546
547 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000548 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
551 error(line, "array constructor needs one argument per array element", "constructor", "");
552 return true;
553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 if (arrayArg && op != EOpConstructStruct) {
556 error(line, "constructing from a non-dereferenced array", "constructor", "");
557 return true;
558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000561 if (function.getParamCount() != 1) {
562 error(line, "constructing matrix from matrix can only take one argument", "constructor", "");
563 return true;
564 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000567 if (overFull) {
568 error(line, "too many arguments", "constructor", "");
569 return true;
570 }
571
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000572 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", "");
574 return true;
575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000577 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000578 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
579 (op == EOpConstructStruct && size < type->getObjectSize())) {
580 error(line, "not enough data provided for construction", "constructor", "");
581 return true;
582 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000583 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000585 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 if (typed == 0) {
587 error(line, "constructor argument does not have a type", "constructor", "");
588 return true;
589 }
590 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
591 error(line, "cannot convert a sampler", "constructor", "");
592 return true;
593 }
594 if (typed->getBasicType() == EbtVoid) {
595 error(line, "cannot convert a void", "constructor", "");
596 return true;
597 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600}
601
602// This function checks to see if a void variable has been declared and raise an error message for such a case
603//
604// returns true in case of an error
605//
606bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
607{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 if (pubType.type == EbtVoid) {
609 error(line, "illegal use of type 'void'", identifier.c_str(), "");
610 return true;
611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614}
615
616// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
617//
618// returns true in case of an error
619//
620bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
621{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
623 error(line, "boolean expression expected", "", "");
624 return true;
625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628}
629
630// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
631//
632// returns true in case of an error
633//
634bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
635{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000636 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
637 error(line, "boolean expression expected", "", "");
638 return true;
639 }
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::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
645{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if (pType.type == EbtStruct) {
647 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000648 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649
650 return true;
651 }
652
653 return false;
654 } else if (IsSampler(pType.type)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000655 error(line, reason, getBasicString(pType.type), "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 return true;
658 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
663bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
664{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
666 pType.type == EbtStruct) {
667 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), "");
668
669 return true;
670 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
673 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676}
677
678bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
679{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
681 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
682 error(line, "samplers cannot be output parameters", type.getBasicString(), "");
683 return true;
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687}
688
689bool TParseContext::containsSampler(TType& type)
690{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 if (IsSampler(type.getBasicType()))
692 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 if (type.getBasicType() == EbtStruct) {
695 TTypeList& structure = *type.getStruct();
696 for (unsigned int i = 0; i < structure.size(); ++i) {
697 if (containsSampler(*structure[i].type))
698 return true;
699 }
700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703}
704
705//
706// Do size checking for an array type's size.
707//
708// Returns true if there was an error.
709//
710bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
711{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000712 TIntermConstantUnion* constant = expr->getAsConstantUnion();
713 if (constant == 0 || constant->getBasicType() != EbtInt) {
714 error(line, "array size must be a constant integer expression", "", "");
715 return true;
716 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000718 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000720 if (size <= 0) {
721 error(line, "array size must be a positive integer", "", "");
722 size = 1;
723 return true;
724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729//
730// See if this qualifier can be an array.
731//
732// Returns true if there is an error.
733//
734bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
735{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000736 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), "");
738 return true;
739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
744//
745// See if this type can be an array.
746//
747// Returns true if there is an error.
748//
749bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
750{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 //
752 // Can the type be an array?
753 //
754 if (type.array) {
755 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), "");
756 return true;
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
762//
763// Do all the semantic checking for declaring an array, with and
764// without a size, and make the right changes to the symbol table.
765//
766// size == 0 means no specified size.
767//
768// Returns true if there was an error.
769//
770bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
771{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 //
773 // Don't check for reserved word use until after we know it's not in the symbol table,
774 // because reserved arrays can be redeclared.
775 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 bool builtIn = false;
778 bool sameScope = false;
779 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
780 if (symbol == 0 || !sameScope) {
781 if (reservedErrorCheck(line, identifier))
782 return true;
783
784 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000786 if (type.arraySize)
787 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 if (! symbolTable.insert(*variable)) {
790 delete variable;
791 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), "");
792 return true;
793 }
794 } else {
795 if (! symbol->isVariable()) {
796 error(line, "variable expected", identifier.c_str(), "");
797 return true;
798 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 variable = static_cast<TVariable*>(symbol);
801 if (! variable->getType().isArray()) {
802 error(line, "redeclaring non-array as array", identifier.c_str(), "");
803 return true;
804 }
805 if (variable->getType().getArraySize() > 0) {
806 error(line, "redeclaration of array with size", identifier.c_str(), "");
807 return true;
808 }
809
810 if (! variable->getType().sameElementType(TType(type))) {
811 error(line, "redeclaration of array with a different type", identifier.c_str(), "");
812 return true;
813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 TType* t = variable->getArrayInformationType();
816 while (t != 0) {
817 if (t->getMaxArraySize() > type.arraySize) {
818 error(line, "higher index value already used for the array", identifier.c_str(), "");
819 return true;
820 }
821 t->setArraySize(type.arraySize);
822 t = t->getArrayInformationType();
823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000825 if (type.arraySize)
826 variable->getType().setArraySize(type.arraySize);
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 if (voidErrorCheck(line, identifier, type))
830 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
835bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
836{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 bool builtIn = false;
838 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
839 if (symbol == 0) {
840 error(line, " undeclared identifier", node->getSymbol().c_str(), "");
841 return true;
842 }
843 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 type->setArrayInformationType(variable->getArrayInformationType());
846 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000848 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
849 // its an error
850 if (node->getSymbol() == "gl_FragData") {
851 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000852 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
855 if (fragDataValue <= size) {
856 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", "");
857 return true;
858 }
859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
862 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
863 if (!updateFlag)
864 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000866 size++;
867 variable->getType().setMaxArraySize(size);
868 type->setMaxArraySize(size);
869 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 while(tt->getArrayInformationType() != 0) {
872 tt = tt->getArrayInformationType();
873 tt->setMaxArraySize(size);
874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
879//
880// Enforce non-initializer type/qualifier rules.
881//
882// Returns true if there was an error.
883//
884bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
885{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 //
887 // Make the qualifier make sense.
888 //
889 if (type.qualifier == EvqConst) {
890 type.qualifier = EvqTemporary;
891 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), "");
892 return true;
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000895 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896}
897
898//
899// Do semantic checking for a variable declaration that has no initializer,
900// and update the symbol table.
901//
902// Returns true if there was an error.
903//
zmo@google.comfd747b82011-04-23 01:30:07 +0000904bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 if (reservedErrorCheck(line, identifier))
907 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
zmo@google.comfd747b82011-04-23 01:30:07 +0000909 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 if (! symbolTable.insert(*variable)) {
912 error(line, "redefinition", variable->getName().c_str(), "");
913 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000914 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000915 return true;
916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 if (voidErrorCheck(line, identifier, type))
919 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922}
923
924bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
925{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 if (qualifier != EvqConst && qualifier != EvqTemporary) {
927 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), "");
928 return true;
929 }
930 if (qualifier == EvqConst && paramQualifier != EvqIn) {
931 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
932 return true;
933 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000935 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000936 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000937 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000938 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000943bool TParseContext::extensionErrorCheck(int line, const TString& extension)
944{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000945 const TExtensionBehavior& extBehavior = directiveHandler.extBehavior();
946 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
947 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000948 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000949 return true;
950 }
zmo@google.comf5450912011-09-09 01:37:19 +0000951 // In GLSL ES, an extension's default behavior is "disable".
952 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000953 error(line, "extension", extension.c_str(), "is disabled");
954 return true;
955 }
956 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000957 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000958 return false;
959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
963
zmo@google.com09c323a2011-08-12 18:22:25 +0000964bool TParseContext::supportsExtension(const char* extension)
965{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000966 const TExtensionBehavior& extBehavior = directiveHandler.extBehavior();
967 TExtensionBehavior::const_iterator iter = extBehavior.find(extension);
968 return (iter != extBehavior.end());
969}
970
971void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
972{
973 pp::SourceLocation loc;
974 DecodeSourceLoc(line, &loc.file, &loc.line);
975 directiveHandler.handleExtension(loc, extName, behavior);
976}
977
978void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
979{
980 pp::SourceLocation loc;
981 DecodeSourceLoc(line, &loc.file, &loc.line);
982 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000983}
984
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985/////////////////////////////////////////////////////////////////////////////////
986//
987// Non-Errors.
988//
989/////////////////////////////////////////////////////////////////////////////////
990
991//
992// Look up a function name in the symbol table, and make sure it is a function.
993//
994// Return the function symbol if found, otherwise 0.
995//
996const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
997{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000998 // First find by unmangled name to check whether the function name has been
999 // hidden by a variable name or struct typename.
1000 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
1001 if (symbol == 0) {
1002 symbol = symbolTable.find(call->getMangledName(), builtIn);
1003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
alokp@chromium.org0a576182010-08-09 17:16:27 +00001005 if (symbol == 0) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001006 error(line, "no matching overloaded function found", call->getName().c_str(), "");
1007 return 0;
1008 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
alokp@chromium.org0a576182010-08-09 17:16:27 +00001010 if (!symbol->isFunction()) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001011 error(line, "function name expected", call->getName().c_str(), "");
1012 return 0;
1013 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001014
1015 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018//
1019// Initializers show up in several places in the grammar. Have one set of
1020// code to handle them here.
1021//
1022bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001025 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001027 if (variable == 0) {
1028 if (reservedErrorCheck(line, identifier))
1029 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 if (voidErrorCheck(line, identifier, pType))
1032 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001034 //
1035 // add variable to symbol table
1036 //
1037 variable = new TVariable(&identifier, type);
1038 if (! symbolTable.insert(*variable)) {
1039 error(line, "redefinition", variable->getName().c_str(), "");
1040 return true;
1041 // don't delete variable, it's used by error recovery, and the pool
1042 // pop will take care of the memory
1043 }
1044 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 //
1047 // identifier must be of type constant, a global, or a temporary
1048 //
1049 TQualifier qualifier = variable->getType().getQualifier();
1050 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1051 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), "");
1052 return true;
1053 }
1054 //
1055 // test for and propagate constant
1056 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 if (qualifier == EvqConst) {
1059 if (qualifier != initializer->getType().getQualifier()) {
1060 error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001061 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 return true;
1063 }
1064 if (type != initializer->getType()) {
1065 error(line, " non-matching types for const initializer ",
1066 variable->getType().getQualifierString(), "");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001067 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 return true;
1069 }
1070 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001071 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1074 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1075 } else {
1076 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1077 }
1078 } else if (initializer->getAsSymbolNode()) {
1079 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1080 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001082 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001083 variable->shareConstPointer(constArray);
1084 } else {
1085 error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001086 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 return true;
1088 }
1089 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 if (qualifier != EvqConst) {
1092 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1093 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1094 if (intermNode == 0) {
1095 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1096 return true;
1097 }
1098 } else
1099 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102}
1103
1104bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1105{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001106 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001107 if (!aggrNode->isConstructor())
1108 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 // check if all the child nodes are constants so that they can be inserted into
1113 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001114 TIntermSequence &sequence = aggrNode->getSequence() ;
1115 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1116 if (!(*p)->getAsTyped()->getAsConstantUnion())
1117 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121}
1122
1123// This function is used to test for the correctness of the parameters passed to various constructor functions
1124// and also convert them to the right datatype if it is allowed and required.
1125//
1126// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1127//
1128TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1129{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 if (node == 0)
1131 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001133 TIntermAggregate* aggrNode = node->getAsAggregate();
1134
alokp@chromium.org58e54292010-08-24 21:40:03 +00001135 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001136 if (op == EOpConstructStruct)
1137 memberTypes = type->getStruct()->begin();
1138
1139 TType elementType = *type;
1140 if (type->isArray())
1141 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001143 bool singleArg;
1144 if (aggrNode) {
1145 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1146 singleArg = true;
1147 else
1148 singleArg = false;
1149 } else
1150 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001152 TIntermTyped *newNode;
1153 if (singleArg) {
1154 // If structure constructor or array constructor is being called
1155 // for only one parameter inside the structure, we need to call constructStruct function once.
1156 if (type->isArray())
1157 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1158 else if (op == EOpConstructStruct)
1159 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1160 else
1161 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001163 if (newNode && newNode->getAsAggregate()) {
1164 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1165 if (constConstructor)
1166 return constConstructor;
1167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 return newNode;
1170 }
1171
1172 //
1173 // Handle list of arguments.
1174 //
1175 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1176 // if the structure constructor contains more than one parameter, then construct
1177 // each parameter
1178
1179 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1180
1181 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1182 // to the right type if possible (and allowed).
1183 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1184
1185 for (TIntermSequence::iterator p = sequenceVector.begin();
1186 p != sequenceVector.end(); p++, paramCount++) {
1187 if (type->isArray())
1188 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1189 else if (op == EOpConstructStruct)
1190 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1191 else
1192 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1193
1194 if (newNode) {
1195 *p = newNode;
1196 }
1197 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1200 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1201 if (constConstructor)
1202 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001203
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001204 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001205}
1206
1207TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1208{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001209 bool canBeFolded = areAllChildConst(aggrNode);
1210 aggrNode->setType(type);
1211 if (canBeFolded) {
1212 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001213 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001214 if (aggrNode->getSequence().size() == 1) {
1215 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1216 }
1217 else {
1218 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1219 }
1220 if (returnVal)
1221 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001223 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1224 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001226 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227}
1228
1229// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1230// for the parameter to the constructor (passed to this function). Essentially, it converts
1231// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1232// float, then float is converted to int.
1233//
1234// Returns 0 for an error or the constructed node.
1235//
1236TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1237{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001238 TIntermTyped* newNode;
1239 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001240
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001241 //
1242 // First, convert types as needed.
1243 //
1244 switch (op) {
1245 case EOpConstructVec2:
1246 case EOpConstructVec3:
1247 case EOpConstructVec4:
1248 case EOpConstructMat2:
1249 case EOpConstructMat3:
1250 case EOpConstructMat4:
1251 case EOpConstructFloat:
1252 basicOp = EOpConstructFloat;
1253 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001255 case EOpConstructIVec2:
1256 case EOpConstructIVec3:
1257 case EOpConstructIVec4:
1258 case EOpConstructInt:
1259 basicOp = EOpConstructInt;
1260 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001262 case EOpConstructBVec2:
1263 case EOpConstructBVec3:
1264 case EOpConstructBVec4:
1265 case EOpConstructBool:
1266 basicOp = EOpConstructBool;
1267 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 default:
1270 error(line, "unsupported construction", "", "");
1271 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001273 return 0;
1274 }
1275 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1276 if (newNode == 0) {
1277 error(line, "can't convert", "constructor", "");
1278 return 0;
1279 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001281 //
1282 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1283 //
1284
1285 // Otherwise, skip out early.
1286 if (subset || (newNode != node && newNode->getType() == *type))
1287 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001289 // setAggregateOperator will insert a new node for the constructor, as needed.
1290 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001291}
1292
1293// This function tests for the type of the parameters to the structures constructors. Raises
1294// an error message if the expected type does not match the parameter passed to the constructor.
1295//
1296// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1297//
1298TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1299{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001300 if (*type == node->getAsTyped()->getType()) {
1301 if (subset)
1302 return node->getAsTyped();
1303 else
1304 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1305 } else {
1306 error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount,
1307 node->getAsTyped()->getType().getBasicString(), type->getBasicString());
1308 recover();
1309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001311 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312}
1313
1314//
1315// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1316// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1317// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1318// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1319// a constant matrix.
1320//
1321TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1322{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 TIntermTyped* typedNode;
1324 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001326 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001327 if (tempConstantNode) {
1328 unionArray = tempConstantNode->getUnionArrayPointer();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001329 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001331 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001332 return node;
1333 }
1334 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1335 error(line, "Cannot offset into the vector", "Error", "");
1336 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001337
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001338 return 0;
1339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001341 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 for (int i = 0; i < fields.num; i++) {
1344 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1345 error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]);
1346 recover();
1347 fields.offsets[i] = 0;
1348 }
1349
1350 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001351
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001352 }
1353 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1354 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355}
1356
1357//
1358// This function returns the column being accessed from a constant matrix. The values are retrieved from
1359// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1360// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1361// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1362//
1363TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1364{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001365 TIntermTyped* typedNode;
1366 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001368 if (index >= node->getType().getNominalSize()) {
1369 error(line, "", "[", "matrix 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 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001375 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001376 int size = tempConstantNode->getType().getNominalSize();
1377 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1378 } else {
1379 error(line, "Cannot offset into the matrix", "Error", "");
1380 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001382 return 0;
1383 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001384
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001385 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001386}
1387
1388
1389//
1390// This function returns an element of an array accessed from a constant array. The values are retrieved from
1391// the symbol table and parse-tree is built for the type of the element. The input
1392// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1393// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1394//
1395TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1396{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001397 TIntermTyped* typedNode;
1398 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1399 TType arrayElementType = node->getType();
1400 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001402 if (index >= node->getType().getArraySize()) {
1403 error(line, "", "[", "array field selection out of range '%d'", index);
1404 recover();
1405 index = 0;
1406 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001409
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001410 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001411 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001412 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1413 } else {
1414 error(line, "Cannot offset into the array", "Error", "");
1415 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001417 return 0;
1418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001420 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421}
1422
1423
1424//
1425// This function returns the value of a particular field inside a constant structure from the symbol table.
1426// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1427// function and returns the parse-tree with the values of the embedded/nested struct.
1428//
1429TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1430{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001431 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001432 TIntermTyped *typedNode;
1433 int instanceSize = 0;
1434 unsigned int index = 0;
1435 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001437 for ( index = 0; index < fields->size(); ++index) {
1438 if ((*fields)[index].type->getFieldName() == identifier) {
1439 break;
1440 } else {
1441 instanceSize += (*fields)[index].type->getObjectSize();
1442 }
1443 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001444
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001445 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001446 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001448 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1449 } else {
1450 error(line, "Cannot offset into the structure", "Error", "");
1451 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001453 return 0;
1454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001455
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001456 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457}
1458
kbr@chromium.org476541f2011-10-27 21:14:51 +00001459bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1460{
1461 ++structNestingLevel;
1462
1463 // Embedded structure definitions are not supported per GLSL ES spec.
1464 // They aren't allowed in GLSL either, but we need to detect this here
1465 // so we don't rely on the GLSL compiler to catch it.
1466 if (structNestingLevel > 1) {
1467 error(line, "", "Embedded struct definitions are not allowed", "");
1468 return true;
1469 }
1470
1471 return false;
1472}
1473
1474void TParseContext::exitStructDeclaration()
1475{
1476 --structNestingLevel;
1477}
1478
1479namespace {
1480
1481const int kWebGLMaxStructNesting = 4;
1482
1483} // namespace
1484
1485bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1486{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001487 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001488 return false;
1489 }
1490
1491 if (fieldType.getBasicType() != EbtStruct) {
1492 return false;
1493 }
1494
1495 // We're already inside a structure definition at this point, so add
1496 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001497 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001498 error(line, "", "", "Reference of struct type %s exceeds maximum struct nesting of %d",
1499 fieldType.getTypeName().c_str(), kWebGLMaxStructNesting);
1500 return true;
1501 }
1502
1503 return false;
1504}
1505
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001506//
1507// Parse an array of strings using yyparse.
1508//
1509// Returns 0 for success.
1510//
1511int PaParseStrings(int count, const char* const string[], const int length[],
1512 TParseContext* context) {
1513 if ((count == 0) || (string == NULL))
1514 return 1;
1515
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001516 if (!context->initPreprocessor())
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001517 return 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001518
1519 if (glslang_initialize(context))
1520 return 1;
1521
alokp@chromium.org408c45e2012-04-05 15:54:43 +00001522 int error = glslang_scan(count, string, length, context);
1523 if (!error)
1524 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001525
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001526 glslang_finalize(context);
1527 context->destroyPreprocessor();
1528
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001529 return (error == 0) && (context->numErrors == 0) ? 0 : 1;
1530}
1531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001533