blob: f126c8f96c4ebfbc4543ce95940c2e8a6d7f81d6 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002// Copyright (c) 2002-2013 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"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/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) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000029 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 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,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000036 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000037 } 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:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000091 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000092 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) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 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]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 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//
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, int line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119{
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) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000126 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 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') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000132 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 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') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 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') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 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
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000154 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000155 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 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,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000180 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000182 pp::SourceLocation srcLoc;
183 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
184 diagnostics.writeInfo(pp::Diagnostics::ERROR,
185 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000186
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187}
188
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000189void TParseContext::warning(TSourceLoc loc,
190 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000191 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000192 pp::SourceLocation srcLoc;
193 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
194 diagnostics.writeInfo(pp::Diagnostics::WARNING,
195 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000196}
197
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000198void TParseContext::trace(const char* str)
199{
200 diagnostics.writeDebug(str);
201}
202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203//
204// Same error message for all places assignments don't work.
205//
206void TParseContext::assignError(int line, const char* op, TString left, TString right)
207{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000208 std::stringstream extraInfoStream;
209 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
210 std::string extraInfo = extraInfoStream.str();
211 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212}
213
214//
215// Same error message for all places unary operations don't work.
216//
217void TParseContext::unaryOpError(int line, const char* op, TString operand)
218{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::stringstream extraInfoStream;
220 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
221 << " (or there is no acceptable conversion)";
222 std::string extraInfo = extraInfoStream.str();
223 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224}
225
226//
227// Same error message for all binary operations don't work.
228//
229void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
230{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000231 std::stringstream extraInfoStream;
232 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
233 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
234 std::string extraInfo = extraInfoStream.str();
235 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000238bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000239 if (!checksPrecisionErrors)
240 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 switch( type ){
242 case EbtFloat:
243 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000244 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000245 return true;
246 }
247 break;
248 case EbtInt:
249 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000250 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000251 return true;
252 }
253 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000254 default:
255 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000256 }
257 return false;
258}
259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260//
261// Both test and if necessary, spit out an error, to see if the node is really
262// an l-value that can be operated on this way.
263//
264// Returns true if the was an error.
265//
266bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
267{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000268 TIntermSymbol* symNode = node->getAsSymbolNode();
269 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000271 if (binaryNode) {
272 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000274 switch(binaryNode->getOp()) {
275 case EOpIndexDirect:
276 case EOpIndexIndirect:
277 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000278 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000279 return lValueErrorCheck(line, op, binaryNode->getLeft());
280 case EOpVectorSwizzle:
281 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
282 if (!errorReturn) {
283 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000285 TIntermTyped* rightNode = binaryNode->getRight();
286 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
287
288 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
289 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000290 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000291 offset[value]++;
292 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000293 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 return true;
296 }
297 }
298 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 return errorReturn;
301 default:
302 break;
303 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000304 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 return true;
307 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
309
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000310 const char* symbol = 0;
311 if (symNode != 0)
312 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 const char* message = 0;
315 switch (node->getQualifier()) {
316 case EvqConst: message = "can't modify a const"; break;
317 case EvqConstReadOnly: message = "can't modify a const"; break;
318 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madillb120eac2013-06-12 14:08:13 -0400319 case EvqVertexInput: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000320 case EvqUniform: message = "can't modify a uniform"; break;
321 case EvqVaryingIn: message = "can't modify a varying"; break;
322 case EvqInput: message = "can't modify an input"; break;
323 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
324 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
325 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
326 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 //
329 // Type that can't be written to?
330 //
331 switch (node->getBasicType()) {
332 case EbtSampler2D:
333 case EbtSamplerCube:
334 message = "can't modify a sampler";
335 break;
336 case EbtVoid:
337 message = "can't modify void";
338 break;
339 default:
340 break;
341 }
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000345 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 return true;
348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 //
352 // Everything else is okay, no error.
353 //
354 if (message == 0)
355 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000357 //
358 // If we get here, we have an error and a message.
359 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000360 if (symNode) {
361 std::stringstream extraInfoStream;
362 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
363 std::string extraInfo = extraInfoStream.str();
364 error(line, " l-value required", op, extraInfo.c_str());
365 }
366 else {
367 std::stringstream extraInfoStream;
368 extraInfoStream << "(" << message << ")";
369 std::string extraInfo = extraInfoStream.str();
370 error(line, " l-value required", op, extraInfo.c_str());
371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000373 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374}
375
376//
377// Both test, and if necessary spit out an error, to see if the node is really
378// a constant.
379//
380// Returns true if the was an error.
381//
382bool TParseContext::constErrorCheck(TIntermTyped* node)
383{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 if (node->getQualifier() == EvqConst)
385 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000387 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000389 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
392//
393// Both test, and if necessary spit out an error, to see if the node is really
394// an integer.
395//
396// Returns true if the was an error.
397//
398bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
399{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000400 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000403 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000405 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
408//
409// Both test, and if necessary spit out an error, to see if we are currently
410// globally scoped.
411//
412// Returns true if the was an error.
413//
414bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
415{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 if (global)
417 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000419 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000421 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
424//
425// For now, keep it simple: if it starts "gl_", it's reserved, independent
426// of scope. Except, if the symbol table is at the built-in push-level,
427// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000428// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
429// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430//
431// Returns true if there was an error.
432//
433bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
434{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000435 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000437 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000438 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000439 return true;
440 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000441 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000442 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000443 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000444 return true;
445 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000446 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000447 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000448 return true;
449 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000450 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000451 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000452 return true;
453 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000454 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000456 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000457 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 }
459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000461 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462}
463
464//
465// Make sure there is enough data provided to the constructor to build
466// something of the type of the constructor. Also returns the type of
467// the constructor.
468//
469// Returns true if there was an error in construction.
470//
471bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
472{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000473 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000475 bool constructingMatrix = false;
476 switch(op) {
477 case EOpConstructMat2:
478 case EOpConstructMat3:
479 case EOpConstructMat4:
480 constructingMatrix = true;
481 break;
482 default:
483 break;
484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 //
487 // Note: It's okay to have too many components available, but not okay to have unused
488 // arguments. 'full' will go to true when enough args have been seen. If we loop
489 // again, there is an extra argument, so 'overfull' will become true.
490 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000492 int size = 0;
493 bool constType = true;
494 bool full = false;
495 bool overFull = false;
496 bool matrixInMatrix = false;
497 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000498 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000499 const TParameter& param = function.getParam(i);
500 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000502 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 matrixInMatrix = true;
504 if (full)
505 overFull = true;
506 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
507 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000508 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000510 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000511 arrayArg = true;
512 }
513
514 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000515 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000517 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000518 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 return true;
520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000523 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 return true;
525 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000528 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000529 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000530 return true;
531 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 return true;
537 }
538
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000539 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 return true;
542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000544 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000545 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
546 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000547 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000548 return true;
549 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000552 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000554 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 return true;
556 }
557 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 return true;
560 }
561 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000562 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 return true;
564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567}
568
569// This function checks to see if a void variable has been declared and raise an error message for such a case
570//
571// returns true in case of an error
572//
573bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
574{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000576 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 return true;
578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581}
582
583// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
584//
585// returns true in case of an error
586//
587bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
588{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000590 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 return true;
592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595}
596
597// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
598//
599// returns true in case of an error
600//
601bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
602{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000603 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000604 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return true;
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609}
610
611bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
612{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 if (pType.type == EbtStruct) {
614 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000615 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616
617 return true;
618 }
619
620 return false;
621 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000622 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 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
630bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
631{
Jamie Madillb120eac2013-06-12 14:08:13 -0400632 switch (pType.qualifier)
633 {
634 case EvqVaryingIn:
635 case EvqVaryingOut:
636 case EvqAttribute:
637 case EvqVertexInput:
638 case EvqFragmentOutput:
639 if (pType.type == EbtStruct)
640 {
641 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
642 return true;
643 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
647 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
Jamie Madill0bd18df2013-06-20 11:55:52 -0400652bool TParseContext::locationDeclaratorListCheck(int line, const TPublicType &pType)
653{
654 if (pType.layoutQualifier.location != -1)
655 {
656 error(line, "location must only be specified for a single input or output variable", "location");
657 return true;
658 }
659
660 return false;
661}
662
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
664{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
666 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000667 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return true;
669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672}
673
674bool TParseContext::containsSampler(TType& type)
675{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 if (IsSampler(type.getBasicType()))
677 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000679 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 TTypeList& structure = *type.getStruct();
681 for (unsigned int i = 0; i < structure.size(); ++i) {
682 if (containsSampler(*structure[i].type))
683 return true;
684 }
685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688}
689
690//
691// Do size checking for an array type's size.
692//
693// Returns true if there was an error.
694//
695bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
696{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000698
699 if (constant == 0 || !constant->isScalarInt())
700 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000701 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000705 if (constant->getBasicType() == EbtUInt)
706 {
707 unsigned int uintSize = constant->getUConst(0);
708 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
709 {
710 error(line, "array size too large", "");
711 size = 1;
712 return true;
713 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000715 size = static_cast<int>(uintSize);
716 }
717 else
718 {
719 size = constant->getIConst(0);
720
721 if (size <= 0)
722 {
723 error(line, "array size must be a positive integer", "");
724 size = 1;
725 return true;
726 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000727 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000729 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730}
731
732//
733// See if this qualifier can be an array.
734//
735// Returns true if there is an error.
736//
737bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
738{
Jamie Madillb120eac2013-06-12 14:08:13 -0400739 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000740 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return true;
742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
747//
748// See if this type can be an array.
749//
750// Returns true if there is an error.
751//
752bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
753{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000754 //
755 // Can the type be an array?
756 //
757 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000758 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return true;
760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
765//
766// Do all the semantic checking for declaring an array, with and
767// without a size, and make the right changes to the symbol table.
768//
769// size == 0 means no specified size.
770//
771// Returns true if there was an error.
772//
Jamie Madill60ed9812013-06-06 11:56:46 -0400773bool TParseContext::arrayErrorCheck(int line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 //
776 // Don't check for reserved word use until after we know it's not in the symbol table,
777 // because reserved arrays can be redeclared.
778 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 bool builtIn = false;
781 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000782 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000783 if (symbol == 0 || !sameScope) {
784 if (reservedErrorCheck(line, identifier))
785 return true;
786
787 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 if (type.arraySize)
790 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000792 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000793 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000794 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 return true;
796 }
797 } else {
798 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000799 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 return true;
801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 variable = static_cast<TVariable*>(symbol);
804 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000805 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 return true;
807 }
808 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000809 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 return true;
811 }
812
813 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000814 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 return true;
816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 TType* t = variable->getArrayInformationType();
819 while (t != 0) {
820 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000821 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000822 return true;
823 }
824 t->setArraySize(type.arraySize);
825 t = t->getArrayInformationType();
826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 if (type.arraySize)
829 variable->getType().setArraySize(type.arraySize);
830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 if (voidErrorCheck(line, identifier, type))
833 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
838bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
839{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 bool builtIn = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000841 TSymbol* symbol = symbolTable.find(node->getSymbol(), 0, &builtIn);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000842 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 return true;
845 }
846 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000848 type->setArrayInformationType(variable->getArrayInformationType());
849 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
852 // its an error
853 if (node->getSymbol() == "gl_FragData") {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000854 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", 0, &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000855 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
858 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000859 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 return true;
861 }
862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
865 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
866 if (!updateFlag)
867 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 size++;
870 variable->getType().setMaxArraySize(size);
871 type->setMaxArraySize(size);
872 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000874 while(tt->getArrayInformationType() != 0) {
875 tt = tt->getArrayInformationType();
876 tt->setMaxArraySize(size);
877 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000879 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880}
881
882//
883// Enforce non-initializer type/qualifier rules.
884//
885// Returns true if there was an error.
886//
Jamie Madill60ed9812013-06-06 11:56:46 -0400887bool TParseContext::nonInitConstErrorCheck(int line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000889 if (type.qualifier == EvqConst)
890 {
891 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000893
894 if (array)
895 {
896 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
897 }
898 else if (type.isStructureContainingArrays())
899 {
900 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
901 }
902 else
903 {
904 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
905 }
906
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 return true;
908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
913//
914// Do semantic checking for a variable declaration that has no initializer,
915// and update the symbol table.
916//
917// Returns true if there was an error.
918//
Jamie Madill60ed9812013-06-06 11:56:46 -0400919bool TParseContext::nonInitErrorCheck(int line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 if (reservedErrorCheck(line, identifier))
922 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
zmo@google.comfd747b82011-04-23 01:30:07 +0000924 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000926 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000927 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000929 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 return true;
931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 if (voidErrorCheck(line, identifier, type))
934 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000936 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937}
938
939bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
940{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000942 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000943 return true;
944 }
945 if (qualifier == EvqConst && paramQualifier != EvqIn) {
946 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
947 return true;
948 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000950 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000951 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000953 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000955 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000958bool TParseContext::extensionErrorCheck(int line, const TString& extension)
959{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000960 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000961 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
962 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000963 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
zmo@google.comf5450912011-09-09 01:37:19 +0000966 // In GLSL ES, an extension's default behavior is "disable".
967 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000968 error(line, "extension", extension.c_str(), "is disabled");
969 return true;
970 }
971 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000972 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000973 return false;
974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000976 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
Jamie Madilla5efff92013-06-06 11:56:47 -0400979bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier)
980{
981 if (structQualifierErrorCheck(identifierLocation, publicType))
982 return true;
983
984 // check for layout qualifier issues
985 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
986
987 if (layoutQualifier.matrixPacking != EmpUnspecified)
988 {
989 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400990 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400991 }
992
993 if (layoutQualifier.blockStorage != EbsUnspecified)
994 {
995 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400996 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400997 }
998
999 if (publicType.qualifier != EvqVertexInput && publicType.qualifier != EvqFragmentOutput && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
1000 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001001 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001002 }
1003
1004 return false;
1005}
1006
1007bool TParseContext::layoutLocationErrorCheck(TSourceLoc location, const TLayoutQualifier &layoutQualifier)
1008{
1009 if (layoutQualifier.location != -1)
1010 {
1011 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1012 return true;
1013 }
1014
1015 return false;
1016}
1017
zmo@google.com09c323a2011-08-12 18:22:25 +00001018bool TParseContext::supportsExtension(const char* extension)
1019{
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001020 const TExtensionBehavior& extbehavior = extensionBehavior();
1021 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1022 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001023}
1024
1025void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
1026{
1027 pp::SourceLocation loc;
1028 DecodeSourceLoc(line, &loc.file, &loc.line);
1029 directiveHandler.handleExtension(loc, extName, behavior);
1030}
1031
1032void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
1033{
1034 pp::SourceLocation loc;
1035 DecodeSourceLoc(line, &loc.file, &loc.line);
1036 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +00001037}
1038
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039/////////////////////////////////////////////////////////////////////////////////
1040//
1041// Non-Errors.
1042//
1043/////////////////////////////////////////////////////////////////////////////////
1044
1045//
1046// Look up a function name in the symbol table, and make sure it is a function.
1047//
1048// Return the function symbol if found, otherwise 0.
1049//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001050const TFunction* TParseContext::findFunction(int line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001052 // First find by unmangled name to check whether the function name has been
1053 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001054 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001055 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001056 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058
alokp@chromium.org0a576182010-08-09 17:16:27 +00001059 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001060 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 return 0;
1062 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063
alokp@chromium.org0a576182010-08-09 17:16:27 +00001064 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001065 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 return 0;
1067 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001068
1069 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070}
1071
1072//
1073// Initializers show up in several places in the grammar. Have one set of
1074// code to handle them here.
1075//
Jamie Madill60ed9812013-06-06 11:56:46 -04001076bool TParseContext::executeInitializer(TSourceLoc line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001081 if (variable == 0) {
1082 if (reservedErrorCheck(line, identifier))
1083 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 if (voidErrorCheck(line, identifier, pType))
1086 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001088 //
1089 // add variable to symbol table
1090 //
1091 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001092 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001093 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 return true;
1095 // don't delete variable, it's used by error recovery, and the pool
1096 // pop will take care of the memory
1097 }
1098 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 //
1101 // identifier must be of type constant, a global, or a temporary
1102 //
1103 TQualifier qualifier = variable->getType().getQualifier();
1104 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001105 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001106 return true;
1107 }
1108 //
1109 // test for and propagate constant
1110 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 if (qualifier == EvqConst) {
1113 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001114 std::stringstream extraInfoStream;
1115 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1116 std::string extraInfo = extraInfoStream.str();
1117 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001118 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 return true;
1120 }
1121 if (type != initializer->getType()) {
1122 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001123 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001124 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 return true;
1126 }
1127 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001128 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1131 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1132 } else {
1133 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1134 }
1135 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001136 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001139 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 variable->shareConstPointer(constArray);
1141 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001142 std::stringstream extraInfoStream;
1143 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1144 std::string extraInfo = extraInfoStream.str();
1145 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001146 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001147 return true;
1148 }
1149 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001151 if (qualifier != EvqConst) {
1152 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1153 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1154 if (intermNode == 0) {
1155 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1156 return true;
1157 }
1158 } else
1159 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001161 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162}
1163
1164bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1165{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001166 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001167 if (!aggrNode->isConstructor())
1168 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001172 // check if all the child nodes are constants so that they can be inserted into
1173 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001174 TIntermSequence &sequence = aggrNode->getSequence() ;
1175 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1176 if (!(*p)->getAsTyped()->getAsConstantUnion())
1177 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181}
1182
Jamie Madilla5efff92013-06-06 11:56:47 -04001183TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001184{
1185 TPublicType returnType = typeSpecifier;
1186 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001187 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001188
1189 if (typeSpecifier.array)
1190 {
1191 error(typeSpecifier.line, "not supported", "first-class array");
1192 recover();
1193 returnType.setArray(false);
1194 }
1195
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001196 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001197 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001198 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1199 {
1200 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1201 recover();
1202 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001203
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001204 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1205 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1206 {
1207 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1208 recover();
1209 }
1210 }
1211 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001212 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001213 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001214 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001215 case EvqVertexInput:
1216 case EvqFragmentOutput:
1217 case EvqVaryingIn:
1218 case EvqVaryingOut:
1219 if (typeSpecifier.type == EbtBool)
1220 {
1221 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1222 recover();
1223 }
1224 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001225
Jamie Madillb120eac2013-06-12 14:08:13 -04001226 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001227 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001228 }
1229
1230 return returnType;
1231}
1232
Jamie Madill60ed9812013-06-06 11:56:46 -04001233TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier)
1234{
1235 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1236 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1237
1238 if (identifier != "")
1239 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001240 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001241 recover();
1242
1243 // this error check can mutate the type
1244 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1245 recover();
1246
1247 TVariable* variable = 0;
1248
1249 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1250 recover();
1251
1252 if (variable && symbol)
1253 {
1254 symbol->setId(variable->getUniqueId());
1255 }
1256 }
1257
1258 return aggregate;
1259}
1260
1261TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc indexLocation, TIntermTyped *indexExpression)
1262{
Jamie Madill51a53c72013-06-19 09:24:43 -04001263 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001264 recover();
1265
1266 // this error check can mutate the type
1267 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1268 recover();
1269
1270 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1271 {
1272 recover();
1273 }
1274
1275 TPublicType arrayType = publicType;
1276
1277 int size;
1278 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1279 {
1280 recover();
1281 }
1282 else
1283 {
1284 arrayType.setArray(true, size);
1285 }
1286
1287 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1288 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1289 TVariable* variable = 0;
1290
1291 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1292 recover();
1293
1294 if (variable && symbol)
1295 {
1296 symbol->setId(variable->getUniqueId());
1297 }
1298
1299 return aggregate;
1300}
1301
1302TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc initLocation, TIntermTyped *initializer)
1303{
Jamie Madilla5efff92013-06-06 11:56:47 -04001304 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001305 recover();
1306
1307 TIntermNode* intermNode;
1308 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1309 {
1310 //
1311 // Build intermediate representation
1312 //
1313 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1314 }
1315 else
1316 {
1317 recover();
1318 return NULL;
1319 }
1320}
1321
Jamie Madill502d66f2013-06-20 11:55:52 -04001322TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, TSourceLoc identifierLocation, const TString &identifier)
1323{
1324 if (publicType.type == EbtInvariant && !identifierSymbol)
1325 {
1326 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1327 recover();
1328 }
1329
1330 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1331 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1332
1333 if (structQualifierErrorCheck(identifierLocation, publicType))
1334 recover();
1335
Jamie Madill0bd18df2013-06-20 11:55:52 -04001336 if (locationDeclaratorListCheck(identifierLocation, publicType))
1337 recover();
1338
Jamie Madill502d66f2013-06-20 11:55:52 -04001339 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1340 recover();
1341
1342 TVariable* variable = 0;
1343 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1344 recover();
1345 if (symbol && variable)
1346 symbol->setId(variable->getUniqueId());
1347
1348 return intermAggregate;
1349}
1350
1351TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
1352{
1353 if (structQualifierErrorCheck(identifierLocation, publicType))
1354 recover();
1355
Jamie Madill0bd18df2013-06-20 11:55:52 -04001356 if (locationDeclaratorListCheck(identifierLocation, publicType))
1357 recover();
1358
Jamie Madill502d66f2013-06-20 11:55:52 -04001359 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1360 recover();
1361
1362 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1363 {
1364 recover();
1365 }
1366 else if (indexExpression)
1367 {
1368 int size;
1369 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1370 recover();
1371 TPublicType arrayType(publicType);
1372 arrayType.setArray(true, size);
1373 TVariable* variable = NULL;
1374 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1375 recover();
1376 TType type = TType(arrayType);
1377 type.setArraySize(size);
1378
1379 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1380 }
1381 else
1382 {
1383 TPublicType arrayType(publicType);
1384 arrayType.setArray(true);
1385 TVariable* variable = NULL;
1386 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1387 recover();
1388 }
1389
1390 return NULL;
1391}
1392
1393TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc initLocation, TIntermTyped *initializer)
1394{
1395 if (structQualifierErrorCheck(identifierLocation, publicType))
1396 recover();
1397
Jamie Madill0bd18df2013-06-20 11:55:52 -04001398 if (locationDeclaratorListCheck(identifierLocation, publicType))
1399 recover();
1400
Jamie Madill502d66f2013-06-20 11:55:52 -04001401 TIntermNode* intermNode;
1402 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1403 {
1404 //
1405 // build the intermediate representation
1406 //
1407 if (intermNode)
1408 {
1409 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1410 }
1411 else
1412 {
1413 return declaratorList;
1414 }
1415 }
1416 else
1417 {
1418 recover();
1419 return NULL;
1420 }
1421}
1422
Jamie Madilla295edf2013-06-06 11:56:48 -04001423void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1424{
1425 if (typeQualifier.qualifier != EvqUniform)
1426 {
1427 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1428 recover();
1429 return;
1430 }
1431
1432 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1433 ASSERT(!layoutQualifier.isEmpty());
1434
1435 if (shaderVersion < 300)
1436 {
1437 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1438 recover();
1439 return;
1440 }
1441
1442 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1443 {
1444 recover();
1445 return;
1446 }
1447
Jamie Madill099c0f32013-06-20 11:55:52 -04001448 if (layoutQualifier.matrixPacking != EmpUnspecified)
1449 {
1450 defaultMatrixPacking = layoutQualifier.matrixPacking;
1451 }
1452
Jamie Madill1566ef72013-06-20 11:55:54 -04001453 if (layoutQualifier.blockStorage != EmpUnspecified)
1454 {
1455 defaultBlockStorage = layoutQualifier.blockStorage;
1456 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001457}
1458
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001459TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1460{
1461 TOperator op = EOpNull;
1462 if (publicType.userDef)
1463 {
1464 op = EOpConstructStruct;
1465 }
1466 else
1467 {
1468 switch (publicType.type)
1469 {
1470 case EbtFloat:
1471 if (publicType.isMatrix())
1472 {
1473 // TODO: non-square matrices
1474 switch(publicType.getCols())
1475 {
1476 case 2: op = EOpConstructMat2; break;
1477 case 3: op = EOpConstructMat3; break;
1478 case 4: op = EOpConstructMat4; break;
1479 }
1480 }
1481 else
1482 {
1483 switch(publicType.getNominalSize())
1484 {
1485 case 1: op = EOpConstructFloat; break;
1486 case 2: op = EOpConstructVec2; break;
1487 case 3: op = EOpConstructVec3; break;
1488 case 4: op = EOpConstructVec4; break;
1489 }
1490 }
1491 break;
1492
1493 case EbtInt:
1494 switch(publicType.getNominalSize())
1495 {
1496 case 1: op = EOpConstructInt; break;
1497 case 2: op = EOpConstructIVec2; break;
1498 case 3: op = EOpConstructIVec3; break;
1499 case 4: op = EOpConstructIVec4; break;
1500 }
1501 break;
1502
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001503 case EbtUInt:
1504 switch(publicType.getNominalSize())
1505 {
Nicolas Capensab60b932013-06-05 10:31:21 -04001506 case 1: op = EOpConstructUInt; break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001507 case 2: op = EOpConstructUVec2; break;
1508 case 3: op = EOpConstructUVec3; break;
1509 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001510 }
1511 break;
1512
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001513 case EbtBool:
1514 switch(publicType.getNominalSize())
1515 {
1516 case 1: op = EOpConstructBool; break;
1517 case 2: op = EOpConstructBVec2; break;
1518 case 3: op = EOpConstructBVec3; break;
1519 case 4: op = EOpConstructBVec4; break;
1520 }
1521 break;
1522
1523 default: break;
1524 }
1525
1526 if (op == EOpNull)
1527 {
1528 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1529 recover();
1530 publicType.type = EbtFloat;
1531 op = EOpConstructFloat;
1532 }
1533 }
1534
1535 TString tempString;
1536 TType type(publicType);
1537 return new TFunction(&tempString, type, op);
1538}
1539
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001540// This function is used to test for the correctness of the parameters passed to various constructor functions
1541// and also convert them to the right datatype if it is allowed and required.
1542//
1543// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1544//
1545TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1546{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001547 if (node == 0)
1548 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001550 TIntermAggregate* aggrNode = node->getAsAggregate();
1551
alokp@chromium.org58e54292010-08-24 21:40:03 +00001552 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001553 if (op == EOpConstructStruct)
1554 memberTypes = type->getStruct()->begin();
1555
1556 TType elementType = *type;
1557 if (type->isArray())
1558 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001560 bool singleArg;
1561 if (aggrNode) {
1562 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1563 singleArg = true;
1564 else
1565 singleArg = false;
1566 } else
1567 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 TIntermTyped *newNode;
1570 if (singleArg) {
1571 // If structure constructor or array constructor is being called
1572 // for only one parameter inside the structure, we need to call constructStruct function once.
1573 if (type->isArray())
1574 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1575 else if (op == EOpConstructStruct)
1576 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1577 else
1578 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001580 if (newNode && newNode->getAsAggregate()) {
1581 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1582 if (constConstructor)
1583 return constConstructor;
1584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001586 return newNode;
1587 }
1588
1589 //
1590 // Handle list of arguments.
1591 //
1592 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1593 // if the structure constructor contains more than one parameter, then construct
1594 // each parameter
1595
1596 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1597
1598 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1599 // to the right type if possible (and allowed).
1600 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1601
1602 for (TIntermSequence::iterator p = sequenceVector.begin();
1603 p != sequenceVector.end(); p++, paramCount++) {
1604 if (type->isArray())
1605 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1606 else if (op == EOpConstructStruct)
1607 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1608 else
1609 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1610
1611 if (newNode) {
1612 *p = newNode;
1613 }
1614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001616 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1617 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1618 if (constConstructor)
1619 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001621 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622}
1623
1624TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1625{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 bool canBeFolded = areAllChildConst(aggrNode);
1627 aggrNode->setType(type);
1628 if (canBeFolded) {
1629 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001630 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001631 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001632 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001633 }
1634 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001635 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001636 }
1637 if (returnVal)
1638 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001640 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001643 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644}
1645
1646// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1647// for the parameter to the constructor (passed to this function). Essentially, it converts
1648// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1649// float, then float is converted to int.
1650//
1651// Returns 0 for an error or the constructed node.
1652//
1653TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1654{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001655 TIntermTyped* newNode;
1656 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001658 //
1659 // First, convert types as needed.
1660 //
1661 switch (op) {
1662 case EOpConstructVec2:
1663 case EOpConstructVec3:
1664 case EOpConstructVec4:
1665 case EOpConstructMat2:
1666 case EOpConstructMat3:
1667 case EOpConstructMat4:
1668 case EOpConstructFloat:
1669 basicOp = EOpConstructFloat;
1670 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001672 case EOpConstructIVec2:
1673 case EOpConstructIVec3:
1674 case EOpConstructIVec4:
1675 case EOpConstructInt:
1676 basicOp = EOpConstructInt;
1677 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001679 case EOpConstructUVec2:
1680 case EOpConstructUVec3:
1681 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001682 case EOpConstructUInt:
1683 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001684 break;
1685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001686 case EOpConstructBVec2:
1687 case EOpConstructBVec3:
1688 case EOpConstructBVec4:
1689 case EOpConstructBool:
1690 basicOp = EOpConstructBool;
1691 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001694 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001695 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001697 return 0;
1698 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001699 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001700 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001701 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001702 return 0;
1703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001705 //
1706 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1707 //
1708
1709 // Otherwise, skip out early.
1710 if (subset || (newNode != node && newNode->getType() == *type))
1711 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001713 // setAggregateOperator will insert a new node for the constructor, as needed.
1714 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715}
1716
1717// This function tests for the type of the parameters to the structures constructors. Raises
1718// an error message if the expected type does not match the parameter passed to the constructor.
1719//
1720// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1721//
1722TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1723{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 if (*type == node->getAsTyped()->getType()) {
1725 if (subset)
1726 return node->getAsTyped();
1727 else
1728 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1729 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001730 std::stringstream extraInfoStream;
1731 extraInfoStream << "cannot convert parameter " << paramCount
1732 << " from '" << node->getAsTyped()->getType().getBasicString()
1733 << "' to '" << type->getBasicString() << "'";
1734 std::string extraInfo = extraInfoStream.str();
1735 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 recover();
1737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001739 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740}
1741
1742//
1743// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1744// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1745// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1746// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1747// a constant matrix.
1748//
1749TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1750{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 TIntermTyped* typedNode;
1752 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001754 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 if (tempConstantNode) {
1756 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001758 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001759 return node;
1760 }
1761 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001762 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001763 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 return 0;
1766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001768 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 for (int i = 0; i < fields.num; i++) {
1771 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001772 std::stringstream extraInfoStream;
1773 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1774 std::string extraInfo = extraInfoStream.str();
1775 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001776 recover();
1777 fields.offsets[i] = 0;
1778 }
1779
1780 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 }
1783 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1784 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785}
1786
1787//
1788// This function returns the column being accessed from a constant matrix. The values are retrieved from
1789// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1790// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1791// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1792//
1793TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1794{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001795 TIntermTyped* typedNode;
1796 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001798 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001799 std::stringstream extraInfoStream;
1800 extraInfoStream << "matrix field selection out of range '" << index << "'";
1801 std::string extraInfo = extraInfoStream.str();
1802 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001803 recover();
1804 index = 0;
1805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001807 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001808 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001809 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001810 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1811 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001812 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001813 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001815 return 0;
1816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001818 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819}
1820
1821
1822//
1823// This function returns an element of an array accessed from a constant array. The values are retrieved from
1824// the symbol table and parse-tree is built for the type of the element. The input
1825// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1826// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1827//
1828TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1829{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001830 TIntermTyped* typedNode;
1831 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1832 TType arrayElementType = node->getType();
1833 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001835 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001836 std::stringstream extraInfoStream;
1837 extraInfoStream << "array field selection out of range '" << index << "'";
1838 std::string extraInfo = extraInfoStream.str();
1839 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 recover();
1841 index = 0;
1842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001846 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001847 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001848 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1849 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001850 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001851 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001853 return 0;
1854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857}
1858
1859
1860//
1861// This function returns the value of a particular field inside a constant structure from the symbol table.
1862// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1863// function and returns the parse-tree with the values of the embedded/nested struct.
1864//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001865TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, TSourceLoc line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001867 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001868 TIntermTyped *typedNode;
1869 int instanceSize = 0;
1870 unsigned int index = 0;
1871 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001873 for ( index = 0; index < fields->size(); ++index) {
1874 if ((*fields)[index].type->getFieldName() == identifier) {
1875 break;
1876 } else {
1877 instanceSize += (*fields)[index].type->getObjectSize();
1878 }
1879 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001882 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001884 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1885 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001886 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001887 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001889 return 0;
1890 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001892 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893}
1894
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001895//
1896// Interface/uniform blocks
1897//
1898TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, TSourceLoc nameLine, const TString& blockName, TTypeList* typeList,
1899 const TString& instanceName, TSourceLoc instanceLine, TIntermTyped* arrayIndex, TSourceLoc arrayIndexLine)
1900{
1901 if (reservedErrorCheck(nameLine, blockName))
1902 recover();
1903
1904 if (typeQualifier.qualifier != EvqUniform)
1905 {
1906 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1907 recover();
1908 }
1909
Jamie Madill099c0f32013-06-20 11:55:52 -04001910 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1911 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001912 {
1913 recover();
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1917 {
1918 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1919 }
1920
Jamie Madill1566ef72013-06-20 11:55:54 -04001921 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1922 {
1923 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1924 }
1925
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001926 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1927 if (!symbolTable.declare(*blockNameSymbol)) {
1928 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1929 recover();
1930 }
1931
1932 // check for sampler types
1933 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1934 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1935 TType* memberType = memberTypeLine.type;
1936 if (IsSampler(memberType->getBasicType())) {
1937 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1938 recover();
1939 }
1940
1941 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1942 switch (qualifier)
1943 {
1944 case EvqGlobal:
1945 case EvqUniform:
1946 break;
1947 default:
1948 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1949 recover();
1950 break;
1951 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001952
1953 // check layout qualifiers
Jamie Madill099c0f32013-06-20 11:55:52 -04001954 TLayoutQualifier memberLayoutQualifier = memberType->getLayoutQualifier();
1955 if (layoutLocationErrorCheck(memberTypeLine.line, memberLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001956 {
1957 recover();
1958 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001959
Jamie Madill1566ef72013-06-20 11:55:54 -04001960 if (memberLayoutQualifier.blockStorage != EbsUnspecified)
1961 {
1962 error(memberTypeLine.line, "invalid layout qualifier:", getBlockStorageString(memberLayoutQualifier.blockStorage), "cannot be used here");
1963 recover();
1964 }
1965
Jamie Madill099c0f32013-06-20 11:55:52 -04001966 if (memberLayoutQualifier.matrixPacking == EmpUnspecified)
1967 {
1968 memberLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1969 }
1970 else if (!memberType->isMatrix())
1971 {
1972 error(memberTypeLine.line, "invalid layout qualifier:", getMatrixPackingString(memberLayoutQualifier.matrixPacking), "can only be used on matrix types");
1973 recover();
1974 }
1975
1976 memberType->setLayoutQualifier(memberLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001977 }
1978
1979 TType* interfaceBlock = new TType(typeList, blockName);
1980 interfaceBlock->setBasicType(EbtInterfaceBlock);
1981 interfaceBlock->setQualifier(typeQualifier.qualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04001982 interfaceBlock->setLayoutQualifier(blockLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001983
1984 TString symbolName = "";
1985 int symbolId = 0;
1986
1987 if (instanceName == "")
1988 {
1989 // define symbols for the members of the interface block
1990 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1991 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1992 TType* memberType = memberTypeLine.type;
1993 memberType->setInterfaceBlockType(interfaceBlock);
1994 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1995 memberVariable->setQualifier(typeQualifier.qualifier);
1996 if (!symbolTable.declare(*memberVariable)) {
1997 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1998 recover();
1999 }
2000 }
2001 }
2002 else
2003 {
2004 interfaceBlock->setInstanceName(instanceName);
2005
2006 // add array index
2007 if (arrayIndex != NULL)
2008 {
2009 int size;
2010 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
2011 recover();
2012 interfaceBlock->setArraySize(size);
2013 }
2014
2015 // add a symbol for this interface block
2016 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
2017 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2018 if (!symbolTable.declare(*instanceTypeDef)) {
2019 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
2020 recover();
2021 }
2022
2023 symbolId = instanceTypeDef->getUniqueId();
2024 symbolName = instanceTypeDef->getName();
2025 }
2026
2027 exitStructDeclaration();
2028 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
2029 aggregate->setOp(EOpDeclaration);
2030 return aggregate;
2031}
2032
kbr@chromium.org476541f2011-10-27 21:14:51 +00002033bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
2034{
2035 ++structNestingLevel;
2036
2037 // Embedded structure definitions are not supported per GLSL ES spec.
2038 // They aren't allowed in GLSL either, but we need to detect this here
2039 // so we don't rely on the GLSL compiler to catch it.
2040 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002041 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002042 return true;
2043 }
2044
2045 return false;
2046}
2047
2048void TParseContext::exitStructDeclaration()
2049{
2050 --structNestingLevel;
2051}
2052
2053namespace {
2054
2055const int kWebGLMaxStructNesting = 4;
2056
2057} // namespace
2058
2059bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
2060{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002061 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002062 return false;
2063 }
2064
2065 if (fieldType.getBasicType() != EbtStruct) {
2066 return false;
2067 }
2068
2069 // We're already inside a structure definition at this point, so add
2070 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00002071 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002072 std::stringstream extraInfoStream;
2073 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
2074 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2075 std::string extraInfo = extraInfoStream.str();
2076 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002077 return true;
2078 }
2079
2080 return false;
2081}
2082
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002083//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002084// Parse an array index expression
2085//
2086TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, TSourceLoc location, TIntermTyped *indexExpression)
2087{
2088 TIntermTyped *indexedExpression = NULL;
2089
2090 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2091 {
2092 if (baseExpression->getAsSymbolNode())
2093 {
2094 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2095 }
2096 else
2097 {
2098 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2099 }
2100 recover();
2101 }
2102 if (baseExpression->getType().getQualifier() == EvqConst && indexExpression->getQualifier() == EvqConst)
2103 {
2104 if (baseExpression->isArray())
2105 {
2106 // constant folding for arrays
2107 indexedExpression = addConstArrayNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2108 }
2109 else if (baseExpression->isVector())
2110 {
2111 // constant folding for vectors
2112 TVectorFields fields;
2113 fields.num = 1;
2114 fields.offsets[0] = indexExpression->getAsConstantUnion()->getIConst(0); // need to do it this way because v.xy sends fields integer array
2115 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2116 }
2117 else if (baseExpression->isMatrix())
2118 {
2119 // constant folding for matrices
2120 indexedExpression = addConstMatrixNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2121 }
2122 }
2123 else
2124 {
2125 if (indexExpression->getQualifier() == EvqConst)
2126 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002127 const bool isMatrixOrVector = (baseExpression->isVector() || baseExpression->isMatrix());
2128 const bool indexOOR = (isMatrixOrVector && baseExpression->getType().getNominalSize() <= indexExpression->getAsConstantUnion()->getIConst(0));
2129
2130 // check for index out-of-range
2131 if (indexOOR && !baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002132 {
2133 std::stringstream extraInfoStream;
2134 extraInfoStream << "field selection out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2135 std::string extraInfo = extraInfoStream.str();
2136 error(location, "", "[", extraInfo.c_str());
2137 recover();
2138 }
2139 else
2140 {
2141 if (baseExpression->isArray())
2142 {
2143 if (baseExpression->getType().getArraySize() == 0)
2144 {
2145 if (baseExpression->getType().getMaxArraySize() <= indexExpression->getAsConstantUnion()->getIConst(0))
2146 {
2147 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), indexExpression->getAsConstantUnion()->getIConst(0), true, location))
2148 recover();
2149 }
2150 else
2151 {
2152 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), 0, false, location))
2153 recover();
2154 }
2155 }
2156 else if ( indexExpression->getAsConstantUnion()->getIConst(0) >= baseExpression->getType().getArraySize())
2157 {
2158 std::stringstream extraInfoStream;
2159 extraInfoStream << "array index out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2160 std::string extraInfo = extraInfoStream.str();
2161 error(location, "", "[", extraInfo.c_str());
2162 recover();
2163 }
2164 }
2165 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2166 }
2167 }
2168 else
2169 {
2170 if (baseExpression->isArray() && baseExpression->getType().getArraySize() == 0)
2171 {
2172 error(location, "", "[", "array must be redeclared with a size before being indexed with a variable");
2173 recover();
2174 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002175 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2176 {
2177 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2178 recover();
2179 }
Jamie Madill46131a32013-06-20 11:55:50 -04002180 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2181 {
2182 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2183 recover();
2184 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002185
2186 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2187 }
2188 }
2189 if (indexedExpression == 0)
2190 {
2191 ConstantUnion *unionArray = new ConstantUnion[1];
2192 unionArray->setFConst(0.0f);
2193 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2194 }
2195 else if (baseExpression->isArray())
2196 {
2197 if (baseExpression->getType().getStruct())
2198 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002199 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
2200 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
2201 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002202 }
2203 else
2204 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002205 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002206 }
2207
2208 if (baseExpression->getType().getQualifier() == EvqConst)
2209 {
2210 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2211 }
2212 }
2213 else if (baseExpression->isMatrix() && baseExpression->getType().getQualifier() == EvqConst)
2214 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002215 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002216 }
2217 else if (baseExpression->isMatrix())
2218 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002219 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002220 }
2221 else if (baseExpression->isVector() && baseExpression->getType().getQualifier() == EvqConst)
2222 {
2223 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst));
2224 }
2225 else if (baseExpression->isVector())
2226 {
2227 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
2228 }
2229 else
2230 {
2231 indexedExpression->setType(baseExpression->getType());
2232 }
2233
2234 return indexedExpression;
2235}
2236
2237TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, TSourceLoc dotLocation, const TString &fieldString, TSourceLoc fieldLocation)
2238{
2239 TIntermTyped *indexedExpression = NULL;
2240
2241 if (baseExpression->isArray())
2242 {
2243 error(fieldLocation, "cannot apply dot operator to an array", ".");
2244 recover();
2245 }
2246
2247 if (baseExpression->isVector())
2248 {
2249 TVectorFields fields;
2250 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2251 {
2252 fields.num = 1;
2253 fields.offsets[0] = 0;
2254 recover();
2255 }
2256
2257 if (baseExpression->getType().getQualifier() == EvqConst)
2258 {
2259 // constant folding for vector fields
2260 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2261 if (indexedExpression == 0)
2262 {
2263 recover();
2264 indexedExpression = baseExpression;
2265 }
2266 else
2267 {
2268 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2269 }
2270 }
2271 else
2272 {
2273 TString vectorString = fieldString;
2274 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2275 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2276 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2277 }
2278 }
2279 else if (baseExpression->isMatrix())
2280 {
2281 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002282 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002283 {
2284 fields.wholeRow = false;
2285 fields.wholeCol = false;
2286 fields.row = 0;
2287 fields.col = 0;
2288 recover();
2289 }
2290
2291 if (fields.wholeRow || fields.wholeCol)
2292 {
2293 error(dotLocation, " non-scalar fields not implemented yet", ".");
2294 recover();
2295 ConstantUnion *unionArray = new ConstantUnion[1];
2296 unionArray->setIConst(0);
2297 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2298 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002299 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002300 }
2301 else
2302 {
2303 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002304 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002305 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2306 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2307 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2308 }
2309 }
2310 else if (baseExpression->getBasicType() == EbtStruct)
2311 {
2312 bool fieldFound = false;
2313 const TTypeList* fields = baseExpression->getType().getStruct();
2314 if (fields == 0)
2315 {
2316 error(dotLocation, "structure has no fields", "Internal Error");
2317 recover();
2318 indexedExpression = baseExpression;
2319 }
2320 else
2321 {
2322 unsigned int i;
2323 for (i = 0; i < fields->size(); ++i)
2324 {
2325 if ((*fields)[i].type->getFieldName() == fieldString)
2326 {
2327 fieldFound = true;
2328 break;
2329 }
2330 }
2331 if (fieldFound)
2332 {
2333 if (baseExpression->getType().getQualifier() == EvqConst)
2334 {
2335 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2336 if (indexedExpression == 0)
2337 {
2338 recover();
2339 indexedExpression = baseExpression;
2340 }
2341 else
2342 {
2343 indexedExpression->setType(*(*fields)[i].type);
2344 // change the qualifier of the return type, not of the structure field
2345 // as the structure definition is shared between various structures.
2346 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2347 }
2348 }
2349 else
2350 {
2351 ConstantUnion *unionArray = new ConstantUnion[1];
2352 unionArray->setIConst(i);
2353 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2354 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2355 indexedExpression->setType(*(*fields)[i].type);
2356 }
2357 }
2358 else
2359 {
2360 error(dotLocation, " no such field in structure", fieldString.c_str());
2361 recover();
2362 indexedExpression = baseExpression;
2363 }
2364 }
2365 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002366 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2367 {
2368 bool fieldFound = false;
2369 const TTypeList* fields = baseExpression->getType().getStruct();
2370 if (fields == 0)
2371 {
2372 error(dotLocation, "interface block has no fields", "Internal Error");
2373 recover();
2374 indexedExpression = baseExpression;
2375 }
2376 else
2377 {
2378 unsigned int i;
2379 for (i = 0; i < fields->size(); ++i)
2380 {
2381 if ((*fields)[i].type->getFieldName() == fieldString)
2382 {
2383 fieldFound = true;
2384 break;
2385 }
2386 }
2387 if (fieldFound)
2388 {
2389 ConstantUnion *unionArray = new ConstantUnion[1];
2390 unionArray->setIConst(i);
2391 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2392 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2393 indexedExpression->setType(*(*fields)[i].type);
2394 }
2395 else
2396 {
2397 error(dotLocation, " no such field in interface block", fieldString.c_str());
2398 recover();
2399 indexedExpression = baseExpression;
2400 }
2401 }
2402 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002403 else
2404 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002405 if (shaderVersion < 300)
2406 {
2407 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2408 }
2409 else
2410 {
2411 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2412 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002413 recover();
2414 indexedExpression = baseExpression;
2415 }
2416
2417 return indexedExpression;
2418}
2419
Jamie Madilla5efff92013-06-06 11:56:47 -04002420TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, TSourceLoc qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002421{
Jamie Madilla5efff92013-06-06 11:56:47 -04002422 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002423
Jamie Madilla5efff92013-06-06 11:56:47 -04002424 qualifier.location = -1;
2425 qualifier.matrixPacking = EmpUnspecified;
2426 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002427
2428 if (qualifierType == "shared")
2429 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002430 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002431 }
2432 else if (qualifierType == "packed")
2433 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002434 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002435 }
2436 else if (qualifierType == "std140")
2437 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002438 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002439 }
2440 else if (qualifierType == "row_major")
2441 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002442 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002443 }
2444 else if (qualifierType == "column_major")
2445 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002446 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002447 }
2448 else if (qualifierType == "location")
2449 {
2450 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2451 recover();
2452 }
2453 else
2454 {
2455 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2456 recover();
2457 }
2458
Jamie Madilla5efff92013-06-06 11:56:47 -04002459 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002460}
2461
Jamie Madilla5efff92013-06-06 11:56:47 -04002462TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, TSourceLoc qualifierTypeLine, const TString &intValueString, int intValue, TSourceLoc intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002463{
Jamie Madilla5efff92013-06-06 11:56:47 -04002464 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002465
Jamie Madilla5efff92013-06-06 11:56:47 -04002466 qualifier.location = -1;
2467 qualifier.matrixPacking = EmpUnspecified;
2468 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002469
2470 if (qualifierType != "location")
2471 {
2472 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2473 recover();
2474 }
2475 else
2476 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002477 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002478 if (intValue < 0)
2479 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002480 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002481 recover();
2482 }
2483 else
2484 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002485 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002486 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002487 }
2488
Jamie Madilla5efff92013-06-06 11:56:47 -04002489 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002490}
2491
Jamie Madilla5efff92013-06-06 11:56:47 -04002492TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002493{
Jamie Madilla5efff92013-06-06 11:56:47 -04002494 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002495
Jamie Madilla5efff92013-06-06 11:56:47 -04002496 if (rightQualifier.location != -1)
2497 {
2498 joinedQualifier.location = rightQualifier.location;
2499 }
2500 if (rightQualifier.matrixPacking != EmpUnspecified)
2501 {
2502 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2503 }
2504 if (rightQualifier.blockStorage != EbsUnspecified)
2505 {
2506 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2507 }
2508
2509 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002510}
2511
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002512TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2513{
2514 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2515 recover();
2516 }
2517
2518 for (unsigned int i = 0; i < typeList->size(); ++i) {
2519 //
2520 // Careful not to replace already known aspects of type, like array-ness
2521 //
2522 TType* type = (*typeList)[i].type;
2523 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002524 type->setPrimarySize(typeSpecifier.primarySize);
2525 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002526 type->setPrecision(typeSpecifier.precision);
2527 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002528 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002529
2530 // don't allow arrays of arrays
2531 if (type->isArray()) {
2532 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2533 recover();
2534 }
2535 if (typeSpecifier.array)
2536 type->setArraySize(typeSpecifier.arraySize);
2537 if (typeSpecifier.userDef) {
2538 type->setStruct(typeSpecifier.userDef->getStruct());
2539 type->setTypeName(typeSpecifier.userDef->getTypeName());
2540 }
2541
2542 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2543 recover();
2544 }
2545 }
2546
2547 return typeList;
2548}
2549
2550TPublicType TParseContext::addStructure(TSourceLoc structLine, TSourceLoc nameLine, const TString &structName, TTypeList* typeList)
2551{
2552 TType* structure = new TType(typeList, structName);
2553
2554 if (!structName.empty())
2555 {
2556 if (reservedErrorCheck(nameLine, structName))
2557 {
2558 recover();
2559 }
2560 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2561 if (!symbolTable.declare(*userTypeDef)) {
2562 error(nameLine, "redefinition", structName.c_str(), "struct");
2563 recover();
2564 }
2565 }
2566
2567 // ensure we do not specify any storage qualifiers on the struct members
2568 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2569 {
2570 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2571 const TQualifier qualifier = typeLine.type->getQualifier();
2572 switch (qualifier)
2573 {
2574 case EvqGlobal:
2575 case EvqTemporary:
2576 break;
2577 default:
2578 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2579 recover();
2580 break;
2581 }
2582 }
2583
2584 TPublicType publicType;
2585 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2586 publicType.userDef = structure;
2587 exitStructDeclaration();
2588
2589 return publicType;
2590}
2591
2592//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002593// Parse an array of strings using yyparse.
2594//
2595// Returns 0 for success.
2596//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002597int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002598 TParseContext* context) {
2599 if ((count == 0) || (string == NULL))
2600 return 1;
2601
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002602 if (glslang_initialize(context))
2603 return 1;
2604
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002605 int error = glslang_scan(count, string, length, context);
2606 if (!error)
2607 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002608
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002609 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002610
alokp@chromium.org6b495712012-06-29 00:06:58 +00002611 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002612}
2613
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002615