blob: 63632a7c694aea9d59130960f927c4f7c2aca18d [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 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400331 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000332 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400333 }
334 if (IsSampler(node->getBasicType())) {
335 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000336 }
337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000339 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000340 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 return true;
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
345
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000346 //
347 // Everything else is okay, no error.
348 //
349 if (message == 0)
350 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000352 //
353 // If we get here, we have an error and a message.
354 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000355 if (symNode) {
356 std::stringstream extraInfoStream;
357 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
358 std::string extraInfo = extraInfoStream.str();
359 error(line, " l-value required", op, extraInfo.c_str());
360 }
361 else {
362 std::stringstream extraInfoStream;
363 extraInfoStream << "(" << message << ")";
364 std::string extraInfo = extraInfoStream.str();
365 error(line, " l-value required", op, extraInfo.c_str());
366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
371//
372// Both test, and if necessary spit out an error, to see if the node is really
373// a constant.
374//
375// Returns true if the was an error.
376//
377bool TParseContext::constErrorCheck(TIntermTyped* node)
378{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 if (node->getQualifier() == EvqConst)
380 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000382 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385}
386
387//
388// Both test, and if necessary spit out an error, to see if the node is really
389// an integer.
390//
391// Returns true if the was an error.
392//
393bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
394{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000395 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000396 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000398 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000400 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401}
402
403//
404// Both test, and if necessary spit out an error, to see if we are currently
405// globally scoped.
406//
407// Returns true if the was an error.
408//
409bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
410{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000411 if (global)
412 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000414 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417}
418
419//
420// For now, keep it simple: if it starts "gl_", it's reserved, independent
421// of scope. Except, if the symbol table is at the built-in push-level,
422// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000423// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
424// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425//
426// Returns true if there was an error.
427//
428bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
429{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000430 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000432 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 return true;
435 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000436 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000437 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000438 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000439 return true;
440 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000441 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000442 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443 return true;
444 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000445 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000446 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000447 return true;
448 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000449 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000451 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000452 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 }
454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457}
458
459//
460// Make sure there is enough data provided to the constructor to build
461// something of the type of the constructor. Also returns the type of
462// the constructor.
463//
464// Returns true if there was an error in construction.
465//
466bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
467{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000468 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 bool constructingMatrix = false;
471 switch(op) {
472 case EOpConstructMat2:
473 case EOpConstructMat3:
474 case EOpConstructMat4:
475 constructingMatrix = true;
476 break;
477 default:
478 break;
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000481 //
482 // Note: It's okay to have too many components available, but not okay to have unused
483 // arguments. 'full' will go to true when enough args have been seen. If we loop
484 // again, there is an extra argument, so 'overfull' will become true.
485 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 int size = 0;
488 bool constType = true;
489 bool full = false;
490 bool overFull = false;
491 bool matrixInMatrix = false;
492 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000493 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000494 const TParameter& param = function.getParam(i);
495 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000497 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498 matrixInMatrix = true;
499 if (full)
500 overFull = true;
501 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
502 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000503 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000505 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 arrayArg = true;
507 }
508
509 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000510 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000512 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000513 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514 return true;
515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000518 error(line, "constructing from a non-dereferenced array", "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 (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000523 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000524 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000525 return true;
526 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000530 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 return true;
532 }
533
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000534 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 return true;
537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000539 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000540 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
541 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000542 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000543 return true;
544 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000547 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000549 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 return true;
551 }
552 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000553 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 return true;
555 }
556 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000557 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000558 return true;
559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562}
563
564// This function checks to see if a void variable has been declared and raise an error message for such a case
565//
566// returns true in case of an error
567//
568bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
569{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000571 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 return true;
573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576}
577
578// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
579//
580// returns true in case of an error
581//
582bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
583{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000585 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 return true;
587 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590}
591
592// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
593//
594// returns true in case of an error
595//
596bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
597{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000598 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000599 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 return true;
601 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604}
605
606bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
607{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 if (pType.type == EbtStruct) {
609 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000610 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611
612 return true;
613 }
614
615 return false;
616 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000617 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 return true;
620 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623}
624
625bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
626{
Jamie Madillb120eac2013-06-12 14:08:13 -0400627 switch (pType.qualifier)
628 {
629 case EvqVaryingIn:
630 case EvqVaryingOut:
631 case EvqAttribute:
632 case EvqVertexInput:
633 case EvqFragmentOutput:
634 if (pType.type == EbtStruct)
635 {
636 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
637 return true;
638 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
642 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645}
646
Jamie Madill0bd18df2013-06-20 11:55:52 -0400647bool TParseContext::locationDeclaratorListCheck(int line, const TPublicType &pType)
648{
649 if (pType.layoutQualifier.location != -1)
650 {
651 error(line, "location must only be specified for a single input or output variable", "location");
652 return true;
653 }
654
655 return false;
656}
657
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
659{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
661 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000662 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 return true;
664 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667}
668
669bool TParseContext::containsSampler(TType& type)
670{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 if (IsSampler(type.getBasicType()))
672 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000674 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 TTypeList& structure = *type.getStruct();
676 for (unsigned int i = 0; i < structure.size(); ++i) {
677 if (containsSampler(*structure[i].type))
678 return true;
679 }
680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683}
684
685//
686// Do size checking for an array type's size.
687//
688// Returns true if there was an error.
689//
690bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
691{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000692 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000693
694 if (constant == 0 || !constant->isScalarInt())
695 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000696 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 return true;
698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000700 if (constant->getBasicType() == EbtUInt)
701 {
702 unsigned int uintSize = constant->getUConst(0);
703 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
704 {
705 error(line, "array size too large", "");
706 size = 1;
707 return true;
708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000710 size = static_cast<int>(uintSize);
711 }
712 else
713 {
714 size = constant->getIConst(0);
715
716 if (size <= 0)
717 {
718 error(line, "array size must be a positive integer", "");
719 size = 1;
720 return true;
721 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725}
726
727//
728// See if this qualifier can be an array.
729//
730// Returns true if there is an error.
731//
732bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
733{
Jamie Madillb120eac2013-06-12 14:08:13 -0400734 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000735 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 return true;
737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000739 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740}
741
742//
743// See if this type can be an array.
744//
745// Returns true if there is an error.
746//
747bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
748{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 //
750 // Can the type be an array?
751 //
752 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000753 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000754 return true;
755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758}
759
760//
761// Do all the semantic checking for declaring an array, with and
762// without a size, and make the right changes to the symbol table.
763//
764// size == 0 means no specified size.
765//
766// Returns true if there was an error.
767//
Jamie Madill60ed9812013-06-06 11:56:46 -0400768bool TParseContext::arrayErrorCheck(int line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000770 //
771 // Don't check for reserved word use until after we know it's not in the symbol table,
772 // because reserved arrays can be redeclared.
773 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 bool builtIn = false;
776 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000777 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 if (symbol == 0 || !sameScope) {
779 if (reservedErrorCheck(line, identifier))
780 return true;
781
782 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000784 if (type.arraySize)
785 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000787 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000788 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000789 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 return true;
791 }
792 } else {
793 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000794 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 return true;
796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 variable = static_cast<TVariable*>(symbol);
799 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000800 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000801 return true;
802 }
803 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000804 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 return true;
806 }
807
808 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000809 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 return true;
811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000813 TType* t = variable->getArrayInformationType();
814 while (t != 0) {
815 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000816 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 return true;
818 }
819 t->setArraySize(type.arraySize);
820 t = t->getArrayInformationType();
821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 if (type.arraySize)
824 variable->getType().setArraySize(type.arraySize);
825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 if (voidErrorCheck(line, identifier, type))
828 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
833bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
834{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 bool builtIn = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000836 TSymbol* symbol = symbolTable.find(node->getSymbol(), 0, &builtIn);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000838 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 return true;
840 }
841 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000843 type->setArrayInformationType(variable->getArrayInformationType());
844 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
847 // its an error
848 if (node->getSymbol() == "gl_FragData") {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000849 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", 0, &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000850 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
853 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000854 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return true;
856 }
857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000859 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
860 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
861 if (!updateFlag)
862 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 size++;
865 variable->getType().setMaxArraySize(size);
866 type->setMaxArraySize(size);
867 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 while(tt->getArrayInformationType() != 0) {
870 tt = tt->getArrayInformationType();
871 tt->setMaxArraySize(size);
872 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000874 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875}
876
877//
878// Enforce non-initializer type/qualifier rules.
879//
880// Returns true if there was an error.
881//
Jamie Madill60ed9812013-06-06 11:56:46 -0400882bool TParseContext::nonInitConstErrorCheck(int line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000884 if (type.qualifier == EvqConst)
885 {
886 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000888
889 if (array)
890 {
891 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
892 }
893 else if (type.isStructureContainingArrays())
894 {
895 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
896 }
897 else
898 {
899 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
900 }
901
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 return true;
903 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000905 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906}
907
908//
909// Do semantic checking for a variable declaration that has no initializer,
910// and update the symbol table.
911//
912// Returns true if there was an error.
913//
Jamie Madill60ed9812013-06-06 11:56:46 -0400914bool TParseContext::nonInitErrorCheck(int line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 if (reservedErrorCheck(line, identifier))
917 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
zmo@google.comfd747b82011-04-23 01:30:07 +0000919 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000921 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000922 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000923 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000924 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 return true;
926 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 if (voidErrorCheck(line, identifier, type))
929 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
933
934bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
935{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000936 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000937 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 return true;
939 }
940 if (qualifier == EvqConst && paramQualifier != EvqIn) {
941 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
942 return true;
943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000945 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000946 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000947 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000948 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000950 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951}
952
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000953bool TParseContext::extensionErrorCheck(int line, const TString& extension)
954{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000955 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000956 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
957 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000958 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 return true;
960 }
zmo@google.comf5450912011-09-09 01:37:19 +0000961 // In GLSL ES, an extension's default behavior is "disable".
962 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000963 error(line, "extension", extension.c_str(), "is disabled");
964 return true;
965 }
966 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000967 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000968 return false;
969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
Jamie Madilla5efff92013-06-06 11:56:47 -0400974bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier)
975{
976 if (structQualifierErrorCheck(identifierLocation, publicType))
977 return true;
978
979 // check for layout qualifier issues
980 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
981
982 if (layoutQualifier.matrixPacking != EmpUnspecified)
983 {
984 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400985 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400986 }
987
988 if (layoutQualifier.blockStorage != EbsUnspecified)
989 {
990 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400991 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400992 }
993
994 if (publicType.qualifier != EvqVertexInput && publicType.qualifier != EvqFragmentOutput && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
995 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400996 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400997 }
998
999 return false;
1000}
1001
1002bool TParseContext::layoutLocationErrorCheck(TSourceLoc location, const TLayoutQualifier &layoutQualifier)
1003{
1004 if (layoutQualifier.location != -1)
1005 {
1006 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1007 return true;
1008 }
1009
1010 return false;
1011}
1012
zmo@google.com09c323a2011-08-12 18:22:25 +00001013bool TParseContext::supportsExtension(const char* extension)
1014{
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001015 const TExtensionBehavior& extbehavior = extensionBehavior();
1016 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1017 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001018}
1019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020/////////////////////////////////////////////////////////////////////////////////
1021//
1022// Non-Errors.
1023//
1024/////////////////////////////////////////////////////////////////////////////////
1025
1026//
1027// Look up a function name in the symbol table, and make sure it is a function.
1028//
1029// Return the function symbol if found, otherwise 0.
1030//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001031const TFunction* TParseContext::findFunction(int line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001033 // First find by unmangled name to check whether the function name has been
1034 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001035 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001036 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001037 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001038 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
alokp@chromium.org0a576182010-08-09 17:16:27 +00001040 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001041 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001042 return 0;
1043 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044
alokp@chromium.org0a576182010-08-09 17:16:27 +00001045 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001046 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001047 return 0;
1048 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001049
1050 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051}
1052
1053//
1054// Initializers show up in several places in the grammar. Have one set of
1055// code to handle them here.
1056//
Jamie Madill60ed9812013-06-06 11:56:46 -04001057bool TParseContext::executeInitializer(TSourceLoc line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 if (variable == 0) {
1063 if (reservedErrorCheck(line, identifier))
1064 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 if (voidErrorCheck(line, identifier, pType))
1067 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 //
1070 // add variable to symbol table
1071 //
1072 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001073 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 return true;
1076 // don't delete variable, it's used by error recovery, and the pool
1077 // pop will take care of the memory
1078 }
1079 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001081 //
1082 // identifier must be of type constant, a global, or a temporary
1083 //
1084 TQualifier qualifier = variable->getType().getQualifier();
1085 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001086 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 return true;
1088 }
1089 //
1090 // test for and propagate constant
1091 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001093 if (qualifier == EvqConst) {
1094 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001095 std::stringstream extraInfoStream;
1096 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1097 std::string extraInfo = extraInfoStream.str();
1098 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001099 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 return true;
1101 }
1102 if (type != initializer->getType()) {
1103 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001104 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001105 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001106 return true;
1107 }
1108 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001109 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1112 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1113 } else {
1114 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1115 }
1116 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001117 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001120 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001121 variable->shareConstPointer(constArray);
1122 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001123 std::stringstream extraInfoStream;
1124 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1125 std::string extraInfo = extraInfoStream.str();
1126 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001127 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001128 return true;
1129 }
1130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 if (qualifier != EvqConst) {
1133 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1134 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1135 if (intermNode == 0) {
1136 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1137 return true;
1138 }
1139 } else
1140 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001142 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143}
1144
1145bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1146{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001147 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 if (!aggrNode->isConstructor())
1149 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001151 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001153 // check if all the child nodes are constants so that they can be inserted into
1154 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001155 TIntermSequence &sequence = aggrNode->getSequence() ;
1156 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1157 if (!(*p)->getAsTyped()->getAsConstantUnion())
1158 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001161 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162}
1163
Jamie Madilla5efff92013-06-06 11:56:47 -04001164TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001165{
1166 TPublicType returnType = typeSpecifier;
1167 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001168 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001169
1170 if (typeSpecifier.array)
1171 {
1172 error(typeSpecifier.line, "not supported", "first-class array");
1173 recover();
1174 returnType.setArray(false);
1175 }
1176
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001177 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001178 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001179 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1180 {
1181 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1182 recover();
1183 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001184
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001185 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1186 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1187 {
1188 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1189 recover();
1190 }
1191 }
1192 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001193 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001194 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001195 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001196 case EvqVertexInput:
1197 case EvqFragmentOutput:
1198 case EvqVaryingIn:
1199 case EvqVaryingOut:
1200 if (typeSpecifier.type == EbtBool)
1201 {
1202 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1203 recover();
1204 }
1205 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001206
Jamie Madillb120eac2013-06-12 14:08:13 -04001207 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001208 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001209 }
1210
1211 return returnType;
1212}
1213
Jamie Madill60ed9812013-06-06 11:56:46 -04001214TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier)
1215{
1216 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1217 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1218
1219 if (identifier != "")
1220 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001221 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001222 recover();
1223
1224 // this error check can mutate the type
1225 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1226 recover();
1227
1228 TVariable* variable = 0;
1229
1230 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1231 recover();
1232
1233 if (variable && symbol)
1234 {
1235 symbol->setId(variable->getUniqueId());
1236 }
1237 }
1238
1239 return aggregate;
1240}
1241
1242TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc indexLocation, TIntermTyped *indexExpression)
1243{
Jamie Madill51a53c72013-06-19 09:24:43 -04001244 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001245 recover();
1246
1247 // this error check can mutate the type
1248 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1249 recover();
1250
1251 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1252 {
1253 recover();
1254 }
1255
1256 TPublicType arrayType = publicType;
1257
1258 int size;
1259 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1260 {
1261 recover();
1262 }
1263 else
1264 {
1265 arrayType.setArray(true, size);
1266 }
1267
1268 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1269 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1270 TVariable* variable = 0;
1271
1272 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1273 recover();
1274
1275 if (variable && symbol)
1276 {
1277 symbol->setId(variable->getUniqueId());
1278 }
1279
1280 return aggregate;
1281}
1282
1283TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc initLocation, TIntermTyped *initializer)
1284{
Jamie Madilla5efff92013-06-06 11:56:47 -04001285 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001286 recover();
1287
1288 TIntermNode* intermNode;
1289 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1290 {
1291 //
1292 // Build intermediate representation
1293 //
1294 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1295 }
1296 else
1297 {
1298 recover();
1299 return NULL;
1300 }
1301}
1302
Jamie Madill502d66f2013-06-20 11:55:52 -04001303TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, TSourceLoc identifierLocation, const TString &identifier)
1304{
1305 if (publicType.type == EbtInvariant && !identifierSymbol)
1306 {
1307 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1308 recover();
1309 }
1310
1311 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1312 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1313
1314 if (structQualifierErrorCheck(identifierLocation, publicType))
1315 recover();
1316
Jamie Madill0bd18df2013-06-20 11:55:52 -04001317 if (locationDeclaratorListCheck(identifierLocation, publicType))
1318 recover();
1319
Jamie Madill502d66f2013-06-20 11:55:52 -04001320 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1321 recover();
1322
1323 TVariable* variable = 0;
1324 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1325 recover();
1326 if (symbol && variable)
1327 symbol->setId(variable->getUniqueId());
1328
1329 return intermAggregate;
1330}
1331
1332TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
1333{
1334 if (structQualifierErrorCheck(identifierLocation, publicType))
1335 recover();
1336
Jamie Madill0bd18df2013-06-20 11:55:52 -04001337 if (locationDeclaratorListCheck(identifierLocation, publicType))
1338 recover();
1339
Jamie Madill502d66f2013-06-20 11:55:52 -04001340 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1341 recover();
1342
1343 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1344 {
1345 recover();
1346 }
1347 else if (indexExpression)
1348 {
1349 int size;
1350 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1351 recover();
1352 TPublicType arrayType(publicType);
1353 arrayType.setArray(true, size);
1354 TVariable* variable = NULL;
1355 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1356 recover();
1357 TType type = TType(arrayType);
1358 type.setArraySize(size);
1359
1360 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1361 }
1362 else
1363 {
1364 TPublicType arrayType(publicType);
1365 arrayType.setArray(true);
1366 TVariable* variable = NULL;
1367 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1368 recover();
1369 }
1370
1371 return NULL;
1372}
1373
1374TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc initLocation, TIntermTyped *initializer)
1375{
1376 if (structQualifierErrorCheck(identifierLocation, publicType))
1377 recover();
1378
Jamie Madill0bd18df2013-06-20 11:55:52 -04001379 if (locationDeclaratorListCheck(identifierLocation, publicType))
1380 recover();
1381
Jamie Madill502d66f2013-06-20 11:55:52 -04001382 TIntermNode* intermNode;
1383 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1384 {
1385 //
1386 // build the intermediate representation
1387 //
1388 if (intermNode)
1389 {
1390 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1391 }
1392 else
1393 {
1394 return declaratorList;
1395 }
1396 }
1397 else
1398 {
1399 recover();
1400 return NULL;
1401 }
1402}
1403
Jamie Madilla295edf2013-06-06 11:56:48 -04001404void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1405{
1406 if (typeQualifier.qualifier != EvqUniform)
1407 {
1408 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1409 recover();
1410 return;
1411 }
1412
1413 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1414 ASSERT(!layoutQualifier.isEmpty());
1415
1416 if (shaderVersion < 300)
1417 {
1418 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1419 recover();
1420 return;
1421 }
1422
1423 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1424 {
1425 recover();
1426 return;
1427 }
1428
Jamie Madill099c0f32013-06-20 11:55:52 -04001429 if (layoutQualifier.matrixPacking != EmpUnspecified)
1430 {
1431 defaultMatrixPacking = layoutQualifier.matrixPacking;
1432 }
1433
Jamie Madill1566ef72013-06-20 11:55:54 -04001434 if (layoutQualifier.blockStorage != EmpUnspecified)
1435 {
1436 defaultBlockStorage = layoutQualifier.blockStorage;
1437 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001438}
1439
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001440TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1441{
1442 TOperator op = EOpNull;
1443 if (publicType.userDef)
1444 {
1445 op = EOpConstructStruct;
1446 }
1447 else
1448 {
1449 switch (publicType.type)
1450 {
1451 case EbtFloat:
1452 if (publicType.isMatrix())
1453 {
1454 // TODO: non-square matrices
1455 switch(publicType.getCols())
1456 {
1457 case 2: op = EOpConstructMat2; break;
1458 case 3: op = EOpConstructMat3; break;
1459 case 4: op = EOpConstructMat4; break;
1460 }
1461 }
1462 else
1463 {
1464 switch(publicType.getNominalSize())
1465 {
1466 case 1: op = EOpConstructFloat; break;
1467 case 2: op = EOpConstructVec2; break;
1468 case 3: op = EOpConstructVec3; break;
1469 case 4: op = EOpConstructVec4; break;
1470 }
1471 }
1472 break;
1473
1474 case EbtInt:
1475 switch(publicType.getNominalSize())
1476 {
1477 case 1: op = EOpConstructInt; break;
1478 case 2: op = EOpConstructIVec2; break;
1479 case 3: op = EOpConstructIVec3; break;
1480 case 4: op = EOpConstructIVec4; break;
1481 }
1482 break;
1483
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001484 case EbtUInt:
1485 switch(publicType.getNominalSize())
1486 {
Nicolas Capensab60b932013-06-05 10:31:21 -04001487 case 1: op = EOpConstructUInt; break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001488 case 2: op = EOpConstructUVec2; break;
1489 case 3: op = EOpConstructUVec3; break;
1490 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001491 }
1492 break;
1493
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001494 case EbtBool:
1495 switch(publicType.getNominalSize())
1496 {
1497 case 1: op = EOpConstructBool; break;
1498 case 2: op = EOpConstructBVec2; break;
1499 case 3: op = EOpConstructBVec3; break;
1500 case 4: op = EOpConstructBVec4; break;
1501 }
1502 break;
1503
1504 default: break;
1505 }
1506
1507 if (op == EOpNull)
1508 {
1509 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1510 recover();
1511 publicType.type = EbtFloat;
1512 op = EOpConstructFloat;
1513 }
1514 }
1515
1516 TString tempString;
1517 TType type(publicType);
1518 return new TFunction(&tempString, type, op);
1519}
1520
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001521// This function is used to test for the correctness of the parameters passed to various constructor functions
1522// and also convert them to the right datatype if it is allowed and required.
1523//
1524// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1525//
1526TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1527{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001528 if (node == 0)
1529 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001531 TIntermAggregate* aggrNode = node->getAsAggregate();
1532
alokp@chromium.org58e54292010-08-24 21:40:03 +00001533 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001534 if (op == EOpConstructStruct)
1535 memberTypes = type->getStruct()->begin();
1536
1537 TType elementType = *type;
1538 if (type->isArray())
1539 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001540
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001541 bool singleArg;
1542 if (aggrNode) {
1543 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1544 singleArg = true;
1545 else
1546 singleArg = false;
1547 } else
1548 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001550 TIntermTyped *newNode;
1551 if (singleArg) {
1552 // If structure constructor or array constructor is being called
1553 // for only one parameter inside the structure, we need to call constructStruct function once.
1554 if (type->isArray())
1555 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1556 else if (op == EOpConstructStruct)
1557 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1558 else
1559 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001561 if (newNode && newNode->getAsAggregate()) {
1562 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1563 if (constConstructor)
1564 return constConstructor;
1565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001566
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001567 return newNode;
1568 }
1569
1570 //
1571 // Handle list of arguments.
1572 //
1573 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1574 // if the structure constructor contains more than one parameter, then construct
1575 // each parameter
1576
1577 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1578
1579 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1580 // to the right type if possible (and allowed).
1581 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1582
1583 for (TIntermSequence::iterator p = sequenceVector.begin();
1584 p != sequenceVector.end(); p++, paramCount++) {
1585 if (type->isArray())
1586 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1587 else if (op == EOpConstructStruct)
1588 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1589 else
1590 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1591
1592 if (newNode) {
1593 *p = newNode;
1594 }
1595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001597 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1598 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1599 if (constConstructor)
1600 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001602 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603}
1604
1605TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1606{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001607 bool canBeFolded = areAllChildConst(aggrNode);
1608 aggrNode->setType(type);
1609 if (canBeFolded) {
1610 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001611 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001612 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001613 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001614 }
1615 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001616 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001617 }
1618 if (returnVal)
1619 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001621 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001624 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625}
1626
1627// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1628// for the parameter to the constructor (passed to this function). Essentially, it converts
1629// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1630// float, then float is converted to int.
1631//
1632// Returns 0 for an error or the constructed node.
1633//
1634TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1635{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001636 TIntermTyped* newNode;
1637 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001639 //
1640 // First, convert types as needed.
1641 //
1642 switch (op) {
1643 case EOpConstructVec2:
1644 case EOpConstructVec3:
1645 case EOpConstructVec4:
1646 case EOpConstructMat2:
1647 case EOpConstructMat3:
1648 case EOpConstructMat4:
1649 case EOpConstructFloat:
1650 basicOp = EOpConstructFloat;
1651 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001653 case EOpConstructIVec2:
1654 case EOpConstructIVec3:
1655 case EOpConstructIVec4:
1656 case EOpConstructInt:
1657 basicOp = EOpConstructInt;
1658 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001660 case EOpConstructUVec2:
1661 case EOpConstructUVec3:
1662 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001663 case EOpConstructUInt:
1664 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001665 break;
1666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001667 case EOpConstructBVec2:
1668 case EOpConstructBVec3:
1669 case EOpConstructBVec4:
1670 case EOpConstructBool:
1671 basicOp = EOpConstructBool;
1672 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001674 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001675 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001676 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 return 0;
1679 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001680 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001681 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001682 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001683 return 0;
1684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001686 //
1687 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1688 //
1689
1690 // Otherwise, skip out early.
1691 if (subset || (newNode != node && newNode->getType() == *type))
1692 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001694 // setAggregateOperator will insert a new node for the constructor, as needed.
1695 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696}
1697
1698// This function tests for the type of the parameters to the structures constructors. Raises
1699// an error message if the expected type does not match the parameter passed to the constructor.
1700//
1701// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1702//
1703TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1704{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001705 if (*type == node->getAsTyped()->getType()) {
1706 if (subset)
1707 return node->getAsTyped();
1708 else
1709 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1710 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001711 std::stringstream extraInfoStream;
1712 extraInfoStream << "cannot convert parameter " << paramCount
1713 << " from '" << node->getAsTyped()->getType().getBasicString()
1714 << "' to '" << type->getBasicString() << "'";
1715 std::string extraInfo = extraInfoStream.str();
1716 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 recover();
1718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721}
1722
1723//
1724// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1725// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1726// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1727// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1728// a constant matrix.
1729//
1730TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1731{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001732 TIntermTyped* typedNode;
1733 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001735 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 if (tempConstantNode) {
1737 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001739 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001740 return node;
1741 }
1742 } 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 +00001743 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001744 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001746 return 0;
1747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001749 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 for (int i = 0; i < fields.num; i++) {
1752 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001753 std::stringstream extraInfoStream;
1754 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1755 std::string extraInfo = extraInfoStream.str();
1756 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001757 recover();
1758 fields.offsets[i] = 0;
1759 }
1760
1761 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001763 }
1764 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1765 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766}
1767
1768//
1769// This function returns the column being accessed from a constant matrix. The values are retrieved from
1770// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1771// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1772// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1773//
1774TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1775{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001776 TIntermTyped* typedNode;
1777 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001779 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001780 std::stringstream extraInfoStream;
1781 extraInfoStream << "matrix field selection out of range '" << index << "'";
1782 std::string extraInfo = extraInfoStream.str();
1783 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 recover();
1785 index = 0;
1786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001788 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001789 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001790 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001791 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1792 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001793 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001794 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001796 return 0;
1797 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001799 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800}
1801
1802
1803//
1804// This function returns an element of an array accessed from a constant array. The values are retrieved from
1805// the symbol table and parse-tree is built for the type of the element. The input
1806// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1807// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1808//
1809TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1810{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001811 TIntermTyped* typedNode;
1812 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1813 TType arrayElementType = node->getType();
1814 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001816 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001817 std::stringstream extraInfoStream;
1818 extraInfoStream << "array field selection out of range '" << index << "'";
1819 std::string extraInfo = extraInfoStream.str();
1820 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 recover();
1822 index = 0;
1823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001828 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001829 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1830 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001831 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001832 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 return 0;
1835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838}
1839
1840
1841//
1842// This function returns the value of a particular field inside a constant structure from the symbol table.
1843// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1844// function and returns the parse-tree with the values of the embedded/nested struct.
1845//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001846TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, TSourceLoc line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001847{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001848 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001849 TIntermTyped *typedNode;
1850 int instanceSize = 0;
1851 unsigned int index = 0;
1852 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001854 for ( index = 0; index < fields->size(); ++index) {
1855 if ((*fields)[index].type->getFieldName() == identifier) {
1856 break;
1857 } else {
1858 instanceSize += (*fields)[index].type->getObjectSize();
1859 }
1860 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001863 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001865 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1866 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001867 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001868 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 return 0;
1871 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001873 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874}
1875
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001876//
1877// Interface/uniform blocks
1878//
1879TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, TSourceLoc nameLine, const TString& blockName, TTypeList* typeList,
1880 const TString& instanceName, TSourceLoc instanceLine, TIntermTyped* arrayIndex, TSourceLoc arrayIndexLine)
1881{
1882 if (reservedErrorCheck(nameLine, blockName))
1883 recover();
1884
1885 if (typeQualifier.qualifier != EvqUniform)
1886 {
1887 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1888 recover();
1889 }
1890
Jamie Madill099c0f32013-06-20 11:55:52 -04001891 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1892 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001893 {
1894 recover();
1895 }
1896
Jamie Madill099c0f32013-06-20 11:55:52 -04001897 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1898 {
1899 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1900 }
1901
Jamie Madill1566ef72013-06-20 11:55:54 -04001902 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1903 {
1904 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1905 }
1906
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001907 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1908 if (!symbolTable.declare(*blockNameSymbol)) {
1909 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1910 recover();
1911 }
1912
1913 // check for sampler types
1914 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1915 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1916 TType* memberType = memberTypeLine.type;
1917 if (IsSampler(memberType->getBasicType())) {
1918 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1919 recover();
1920 }
1921
1922 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1923 switch (qualifier)
1924 {
1925 case EvqGlobal:
1926 case EvqUniform:
1927 break;
1928 default:
1929 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1930 recover();
1931 break;
1932 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001933
1934 // check layout qualifiers
Jamie Madill099c0f32013-06-20 11:55:52 -04001935 TLayoutQualifier memberLayoutQualifier = memberType->getLayoutQualifier();
1936 if (layoutLocationErrorCheck(memberTypeLine.line, memberLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001937 {
1938 recover();
1939 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001940
Jamie Madill1566ef72013-06-20 11:55:54 -04001941 if (memberLayoutQualifier.blockStorage != EbsUnspecified)
1942 {
1943 error(memberTypeLine.line, "invalid layout qualifier:", getBlockStorageString(memberLayoutQualifier.blockStorage), "cannot be used here");
1944 recover();
1945 }
1946
Jamie Madill099c0f32013-06-20 11:55:52 -04001947 if (memberLayoutQualifier.matrixPacking == EmpUnspecified)
1948 {
1949 memberLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1950 }
1951 else if (!memberType->isMatrix())
1952 {
1953 error(memberTypeLine.line, "invalid layout qualifier:", getMatrixPackingString(memberLayoutQualifier.matrixPacking), "can only be used on matrix types");
1954 recover();
1955 }
1956
1957 memberType->setLayoutQualifier(memberLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001958 }
1959
1960 TType* interfaceBlock = new TType(typeList, blockName);
1961 interfaceBlock->setBasicType(EbtInterfaceBlock);
1962 interfaceBlock->setQualifier(typeQualifier.qualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04001963 interfaceBlock->setLayoutQualifier(blockLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001964
1965 TString symbolName = "";
1966 int symbolId = 0;
1967
1968 if (instanceName == "")
1969 {
1970 // define symbols for the members of the interface block
1971 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1972 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1973 TType* memberType = memberTypeLine.type;
1974 memberType->setInterfaceBlockType(interfaceBlock);
1975 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1976 memberVariable->setQualifier(typeQualifier.qualifier);
1977 if (!symbolTable.declare(*memberVariable)) {
1978 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1979 recover();
1980 }
1981 }
1982 }
1983 else
1984 {
1985 interfaceBlock->setInstanceName(instanceName);
1986
1987 // add array index
1988 if (arrayIndex != NULL)
1989 {
1990 int size;
1991 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
1992 recover();
1993 interfaceBlock->setArraySize(size);
1994 }
1995
1996 // add a symbol for this interface block
1997 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
1998 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1999 if (!symbolTable.declare(*instanceTypeDef)) {
2000 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
2001 recover();
2002 }
2003
2004 symbolId = instanceTypeDef->getUniqueId();
2005 symbolName = instanceTypeDef->getName();
2006 }
2007
2008 exitStructDeclaration();
2009 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
2010 aggregate->setOp(EOpDeclaration);
2011 return aggregate;
2012}
2013
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
2015{
2016 ++structNestingLevel;
2017
2018 // Embedded structure definitions are not supported per GLSL ES spec.
2019 // They aren't allowed in GLSL either, but we need to detect this here
2020 // so we don't rely on the GLSL compiler to catch it.
2021 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002022 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002023 return true;
2024 }
2025
2026 return false;
2027}
2028
2029void TParseContext::exitStructDeclaration()
2030{
2031 --structNestingLevel;
2032}
2033
2034namespace {
2035
2036const int kWebGLMaxStructNesting = 4;
2037
2038} // namespace
2039
2040bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
2041{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002042 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002043 return false;
2044 }
2045
2046 if (fieldType.getBasicType() != EbtStruct) {
2047 return false;
2048 }
2049
2050 // We're already inside a structure definition at this point, so add
2051 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00002052 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002053 std::stringstream extraInfoStream;
2054 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
2055 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2056 std::string extraInfo = extraInfoStream.str();
2057 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002058 return true;
2059 }
2060
2061 return false;
2062}
2063
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002064//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002065// Parse an array index expression
2066//
2067TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, TSourceLoc location, TIntermTyped *indexExpression)
2068{
2069 TIntermTyped *indexedExpression = NULL;
2070
2071 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2072 {
2073 if (baseExpression->getAsSymbolNode())
2074 {
2075 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2076 }
2077 else
2078 {
2079 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2080 }
2081 recover();
2082 }
2083 if (baseExpression->getType().getQualifier() == EvqConst && indexExpression->getQualifier() == EvqConst)
2084 {
2085 if (baseExpression->isArray())
2086 {
2087 // constant folding for arrays
2088 indexedExpression = addConstArrayNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2089 }
2090 else if (baseExpression->isVector())
2091 {
2092 // constant folding for vectors
2093 TVectorFields fields;
2094 fields.num = 1;
2095 fields.offsets[0] = indexExpression->getAsConstantUnion()->getIConst(0); // need to do it this way because v.xy sends fields integer array
2096 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2097 }
2098 else if (baseExpression->isMatrix())
2099 {
2100 // constant folding for matrices
2101 indexedExpression = addConstMatrixNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2102 }
2103 }
2104 else
2105 {
2106 if (indexExpression->getQualifier() == EvqConst)
2107 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002108 const bool isMatrixOrVector = (baseExpression->isVector() || baseExpression->isMatrix());
2109 const bool indexOOR = (isMatrixOrVector && baseExpression->getType().getNominalSize() <= indexExpression->getAsConstantUnion()->getIConst(0));
2110
2111 // check for index out-of-range
2112 if (indexOOR && !baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002113 {
2114 std::stringstream extraInfoStream;
2115 extraInfoStream << "field selection out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2116 std::string extraInfo = extraInfoStream.str();
2117 error(location, "", "[", extraInfo.c_str());
2118 recover();
2119 }
2120 else
2121 {
2122 if (baseExpression->isArray())
2123 {
2124 if (baseExpression->getType().getArraySize() == 0)
2125 {
2126 if (baseExpression->getType().getMaxArraySize() <= indexExpression->getAsConstantUnion()->getIConst(0))
2127 {
2128 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), indexExpression->getAsConstantUnion()->getIConst(0), true, location))
2129 recover();
2130 }
2131 else
2132 {
2133 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), 0, false, location))
2134 recover();
2135 }
2136 }
2137 else if ( indexExpression->getAsConstantUnion()->getIConst(0) >= baseExpression->getType().getArraySize())
2138 {
2139 std::stringstream extraInfoStream;
2140 extraInfoStream << "array index out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2141 std::string extraInfo = extraInfoStream.str();
2142 error(location, "", "[", extraInfo.c_str());
2143 recover();
2144 }
2145 }
2146 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2147 }
2148 }
2149 else
2150 {
2151 if (baseExpression->isArray() && baseExpression->getType().getArraySize() == 0)
2152 {
2153 error(location, "", "[", "array must be redeclared with a size before being indexed with a variable");
2154 recover();
2155 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002156 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2157 {
2158 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2159 recover();
2160 }
Jamie Madill46131a32013-06-20 11:55:50 -04002161 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2162 {
2163 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2164 recover();
2165 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002166
2167 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2168 }
2169 }
2170 if (indexedExpression == 0)
2171 {
2172 ConstantUnion *unionArray = new ConstantUnion[1];
2173 unionArray->setFConst(0.0f);
2174 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2175 }
2176 else if (baseExpression->isArray())
2177 {
2178 if (baseExpression->getType().getStruct())
2179 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002180 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
2181 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
2182 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002183 }
2184 else
2185 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002186 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002187 }
2188
2189 if (baseExpression->getType().getQualifier() == EvqConst)
2190 {
2191 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2192 }
2193 }
2194 else if (baseExpression->isMatrix() && baseExpression->getType().getQualifier() == EvqConst)
2195 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002196 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002197 }
2198 else if (baseExpression->isMatrix())
2199 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002200 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002201 }
2202 else if (baseExpression->isVector() && baseExpression->getType().getQualifier() == EvqConst)
2203 {
2204 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst));
2205 }
2206 else if (baseExpression->isVector())
2207 {
2208 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
2209 }
2210 else
2211 {
2212 indexedExpression->setType(baseExpression->getType());
2213 }
2214
2215 return indexedExpression;
2216}
2217
2218TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, TSourceLoc dotLocation, const TString &fieldString, TSourceLoc fieldLocation)
2219{
2220 TIntermTyped *indexedExpression = NULL;
2221
2222 if (baseExpression->isArray())
2223 {
2224 error(fieldLocation, "cannot apply dot operator to an array", ".");
2225 recover();
2226 }
2227
2228 if (baseExpression->isVector())
2229 {
2230 TVectorFields fields;
2231 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2232 {
2233 fields.num = 1;
2234 fields.offsets[0] = 0;
2235 recover();
2236 }
2237
2238 if (baseExpression->getType().getQualifier() == EvqConst)
2239 {
2240 // constant folding for vector fields
2241 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2242 if (indexedExpression == 0)
2243 {
2244 recover();
2245 indexedExpression = baseExpression;
2246 }
2247 else
2248 {
2249 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2250 }
2251 }
2252 else
2253 {
2254 TString vectorString = fieldString;
2255 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2256 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2257 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2258 }
2259 }
2260 else if (baseExpression->isMatrix())
2261 {
2262 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002263 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002264 {
2265 fields.wholeRow = false;
2266 fields.wholeCol = false;
2267 fields.row = 0;
2268 fields.col = 0;
2269 recover();
2270 }
2271
2272 if (fields.wholeRow || fields.wholeCol)
2273 {
2274 error(dotLocation, " non-scalar fields not implemented yet", ".");
2275 recover();
2276 ConstantUnion *unionArray = new ConstantUnion[1];
2277 unionArray->setIConst(0);
2278 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2279 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002280 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002281 }
2282 else
2283 {
2284 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002285 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002286 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2287 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2288 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2289 }
2290 }
2291 else if (baseExpression->getBasicType() == EbtStruct)
2292 {
2293 bool fieldFound = false;
2294 const TTypeList* fields = baseExpression->getType().getStruct();
2295 if (fields == 0)
2296 {
2297 error(dotLocation, "structure has no fields", "Internal Error");
2298 recover();
2299 indexedExpression = baseExpression;
2300 }
2301 else
2302 {
2303 unsigned int i;
2304 for (i = 0; i < fields->size(); ++i)
2305 {
2306 if ((*fields)[i].type->getFieldName() == fieldString)
2307 {
2308 fieldFound = true;
2309 break;
2310 }
2311 }
2312 if (fieldFound)
2313 {
2314 if (baseExpression->getType().getQualifier() == EvqConst)
2315 {
2316 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2317 if (indexedExpression == 0)
2318 {
2319 recover();
2320 indexedExpression = baseExpression;
2321 }
2322 else
2323 {
2324 indexedExpression->setType(*(*fields)[i].type);
2325 // change the qualifier of the return type, not of the structure field
2326 // as the structure definition is shared between various structures.
2327 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2328 }
2329 }
2330 else
2331 {
2332 ConstantUnion *unionArray = new ConstantUnion[1];
2333 unionArray->setIConst(i);
2334 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2335 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2336 indexedExpression->setType(*(*fields)[i].type);
2337 }
2338 }
2339 else
2340 {
2341 error(dotLocation, " no such field in structure", fieldString.c_str());
2342 recover();
2343 indexedExpression = baseExpression;
2344 }
2345 }
2346 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002347 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2348 {
2349 bool fieldFound = false;
2350 const TTypeList* fields = baseExpression->getType().getStruct();
2351 if (fields == 0)
2352 {
2353 error(dotLocation, "interface block has no fields", "Internal Error");
2354 recover();
2355 indexedExpression = baseExpression;
2356 }
2357 else
2358 {
2359 unsigned int i;
2360 for (i = 0; i < fields->size(); ++i)
2361 {
2362 if ((*fields)[i].type->getFieldName() == fieldString)
2363 {
2364 fieldFound = true;
2365 break;
2366 }
2367 }
2368 if (fieldFound)
2369 {
2370 ConstantUnion *unionArray = new ConstantUnion[1];
2371 unionArray->setIConst(i);
2372 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2373 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2374 indexedExpression->setType(*(*fields)[i].type);
2375 }
2376 else
2377 {
2378 error(dotLocation, " no such field in interface block", fieldString.c_str());
2379 recover();
2380 indexedExpression = baseExpression;
2381 }
2382 }
2383 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002384 else
2385 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002386 if (shaderVersion < 300)
2387 {
2388 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2389 }
2390 else
2391 {
2392 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2393 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002394 recover();
2395 indexedExpression = baseExpression;
2396 }
2397
2398 return indexedExpression;
2399}
2400
Jamie Madilla5efff92013-06-06 11:56:47 -04002401TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, TSourceLoc qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002402{
Jamie Madilla5efff92013-06-06 11:56:47 -04002403 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002404
Jamie Madilla5efff92013-06-06 11:56:47 -04002405 qualifier.location = -1;
2406 qualifier.matrixPacking = EmpUnspecified;
2407 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002408
2409 if (qualifierType == "shared")
2410 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002411 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002412 }
2413 else if (qualifierType == "packed")
2414 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002415 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002416 }
2417 else if (qualifierType == "std140")
2418 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002419 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002420 }
2421 else if (qualifierType == "row_major")
2422 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002423 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002424 }
2425 else if (qualifierType == "column_major")
2426 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002427 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002428 }
2429 else if (qualifierType == "location")
2430 {
2431 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2432 recover();
2433 }
2434 else
2435 {
2436 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2437 recover();
2438 }
2439
Jamie Madilla5efff92013-06-06 11:56:47 -04002440 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002441}
2442
Jamie Madilla5efff92013-06-06 11:56:47 -04002443TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, TSourceLoc qualifierTypeLine, const TString &intValueString, int intValue, TSourceLoc intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002444{
Jamie Madilla5efff92013-06-06 11:56:47 -04002445 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002446
Jamie Madilla5efff92013-06-06 11:56:47 -04002447 qualifier.location = -1;
2448 qualifier.matrixPacking = EmpUnspecified;
2449 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002450
2451 if (qualifierType != "location")
2452 {
2453 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2454 recover();
2455 }
2456 else
2457 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002458 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002459 if (intValue < 0)
2460 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002461 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002462 recover();
2463 }
2464 else
2465 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002466 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002467 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002468 }
2469
Jamie Madilla5efff92013-06-06 11:56:47 -04002470 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002471}
2472
Jamie Madilla5efff92013-06-06 11:56:47 -04002473TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002474{
Jamie Madilla5efff92013-06-06 11:56:47 -04002475 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002476
Jamie Madilla5efff92013-06-06 11:56:47 -04002477 if (rightQualifier.location != -1)
2478 {
2479 joinedQualifier.location = rightQualifier.location;
2480 }
2481 if (rightQualifier.matrixPacking != EmpUnspecified)
2482 {
2483 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2484 }
2485 if (rightQualifier.blockStorage != EbsUnspecified)
2486 {
2487 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2488 }
2489
2490 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002491}
2492
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002493TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2494{
2495 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2496 recover();
2497 }
2498
2499 for (unsigned int i = 0; i < typeList->size(); ++i) {
2500 //
2501 // Careful not to replace already known aspects of type, like array-ness
2502 //
2503 TType* type = (*typeList)[i].type;
2504 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002505 type->setPrimarySize(typeSpecifier.primarySize);
2506 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002507 type->setPrecision(typeSpecifier.precision);
2508 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002509 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002510
2511 // don't allow arrays of arrays
2512 if (type->isArray()) {
2513 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2514 recover();
2515 }
2516 if (typeSpecifier.array)
2517 type->setArraySize(typeSpecifier.arraySize);
2518 if (typeSpecifier.userDef) {
2519 type->setStruct(typeSpecifier.userDef->getStruct());
2520 type->setTypeName(typeSpecifier.userDef->getTypeName());
2521 }
2522
2523 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2524 recover();
2525 }
2526 }
2527
2528 return typeList;
2529}
2530
2531TPublicType TParseContext::addStructure(TSourceLoc structLine, TSourceLoc nameLine, const TString &structName, TTypeList* typeList)
2532{
2533 TType* structure = new TType(typeList, structName);
2534
2535 if (!structName.empty())
2536 {
2537 if (reservedErrorCheck(nameLine, structName))
2538 {
2539 recover();
2540 }
2541 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2542 if (!symbolTable.declare(*userTypeDef)) {
2543 error(nameLine, "redefinition", structName.c_str(), "struct");
2544 recover();
2545 }
2546 }
2547
2548 // ensure we do not specify any storage qualifiers on the struct members
2549 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2550 {
2551 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2552 const TQualifier qualifier = typeLine.type->getQualifier();
2553 switch (qualifier)
2554 {
2555 case EvqGlobal:
2556 case EvqTemporary:
2557 break;
2558 default:
2559 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2560 recover();
2561 break;
2562 }
2563 }
2564
2565 TPublicType publicType;
2566 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2567 publicType.userDef = structure;
2568 exitStructDeclaration();
2569
2570 return publicType;
2571}
2572
2573//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002574// Parse an array of strings using yyparse.
2575//
2576// Returns 0 for success.
2577//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002578int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002579 TParseContext* context) {
2580 if ((count == 0) || (string == NULL))
2581 return 1;
2582
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002583 if (glslang_initialize(context))
2584 return 1;
2585
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002586 int error = glslang_scan(count, string, length, context);
2587 if (!error)
2588 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002589
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002590 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002591
alokp@chromium.org6b495712012-06-29 00:06:58 +00002592 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002593}
2594
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002596