blob: 85c167f903925a8e3b43abd691a436d3d14dbb76 [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//
Jamie Madill075edd82013-07-08 13:30:19 -040025bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026{
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//
Jamie Madill075edd82013-07-08 13:30:19 -0400118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& 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//
Jamie Madill075edd82013-07-08 13:30:19 -0400178void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179 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;
Jamie Madill075edd82013-07-08 13:30:19 -0400183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000185 diagnostics.writeInfo(pp::Diagnostics::ERROR,
186 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madill075edd82013-07-08 13:30:19 -0400190void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000191 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000192 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000193 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000196 diagnostics.writeInfo(pp::Diagnostics::WARNING,
197 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000198}
199
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000200void TParseContext::trace(const char* str)
201{
202 diagnostics.writeDebug(str);
203}
204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205//
206// Same error message for all places assignments don't work.
207//
Jamie Madill075edd82013-07-08 13:30:19 -0400208void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
216//
217// Same error message for all places unary operations don't work.
218//
Jamie Madill075edd82013-07-08 13:30:19 -0400219void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Same error message for all binary operations don't work.
230//
Jamie Madill075edd82013-07-08 13:30:19 -0400231void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
Jamie Madill075edd82013-07-08 13:30:19 -0400240bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000241 if (!checksPrecisionErrors)
242 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 switch( type ){
244 case EbtFloat:
245 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000246 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 return true;
248 }
249 break;
250 case EbtInt:
251 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253 return true;
254 }
255 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000256 default:
257 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000258 }
259 return false;
260}
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262//
263// Both test and if necessary, spit out an error, to see if the node is really
264// an l-value that can be operated on this way.
265//
266// Returns true if the was an error.
267//
Jamie Madill075edd82013-07-08 13:30:19 -0400268bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 if (binaryNode) {
274 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 switch(binaryNode->getOp()) {
277 case EOpIndexDirect:
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000280 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
284 if (!errorReturn) {
285 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
289
290 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
291 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 offset[value]++;
294 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 return true;
298 }
299 }
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return errorReturn;
303 default:
304 break;
305 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000306 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return true;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* symbol = 0;
313 if (symNode != 0)
314 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madillb120eac2013-06-12 14:08:13 -0400321 case EvqVertexInput: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 case EvqUniform: message = "can't modify a uniform"; break;
323 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
325 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
326 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
327 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000329 //
330 // Type that can't be written to?
331 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400332 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000333 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400334 }
335 if (IsSampler(node->getBasicType())) {
336 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 }
338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000341 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000343 return true;
344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 //
348 // Everything else is okay, no error.
349 //
350 if (message == 0)
351 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000353 //
354 // If we get here, we have an error and a message.
355 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000356 if (symNode) {
357 std::stringstream extraInfoStream;
358 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
359 std::string extraInfo = extraInfoStream.str();
360 error(line, " l-value required", op, extraInfo.c_str());
361 }
362 else {
363 std::stringstream extraInfoStream;
364 extraInfoStream << "(" << message << ")";
365 std::string extraInfo = extraInfoStream.str();
366 error(line, " l-value required", op, extraInfo.c_str());
367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370}
371
372//
373// Both test, and if necessary spit out an error, to see if the node is really
374// a constant.
375//
376// Returns true if the was an error.
377//
378bool TParseContext::constErrorCheck(TIntermTyped* node)
379{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000380 if (node->getQualifier() == EvqConst)
381 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000383 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000385 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386}
387
388//
389// Both test, and if necessary spit out an error, to see if the node is really
390// an integer.
391//
392// Returns true if the was an error.
393//
394bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
395{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000396 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000397 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000399 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402}
403
404//
405// Both test, and if necessary spit out an error, to see if we are currently
406// globally scoped.
407//
408// Returns true if the was an error.
409//
Jamie Madill075edd82013-07-08 13:30:19 -0400410bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000412 if (global)
413 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000415 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000417 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418}
419
420//
421// For now, keep it simple: if it starts "gl_", it's reserved, independent
422// of scope. Except, if the symbol table is at the built-in push-level,
423// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000424// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
425// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426//
427// Returns true if there was an error.
428//
Jamie Madill075edd82013-07-08 13:30:19 -0400429bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000431 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000433 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000434 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
436 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000437 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000438 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000439 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000440 return true;
441 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000442 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000443 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000444 return true;
445 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000446 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000447 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000448 return true;
449 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000450 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000453 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 }
455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458}
459
460//
461// Make sure there is enough data provided to the constructor to build
462// something of the type of the constructor. Also returns the type of
463// the constructor.
464//
465// Returns true if there was an error in construction.
466//
Jamie Madill075edd82013-07-08 13:30:19 -0400467bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 bool constructingMatrix = false;
472 switch(op) {
473 case EOpConstructMat2:
474 case EOpConstructMat3:
475 case EOpConstructMat4:
476 constructingMatrix = true;
477 break;
478 default:
479 break;
480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 //
483 // Note: It's okay to have too many components available, but not okay to have unused
484 // arguments. 'full' will go to true when enough args have been seen. If we loop
485 // again, there is an extra argument, so 'overfull' will become true.
486 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
Jamie Madill94bf7f22013-07-08 13:31:15 -0400488 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 bool constType = true;
490 bool full = false;
491 bool overFull = false;
492 bool matrixInMatrix = false;
493 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000494 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000495 const TParameter& param = function.getParam(i);
496 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000498 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000499 matrixInMatrix = true;
500 if (full)
501 overFull = true;
502 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
503 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000504 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000506 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 arrayArg = true;
508 }
509
510 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000511 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000513 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000514 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000515 return true;
516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000519 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 return true;
521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000524 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000525 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000526 return true;
527 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000531 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 return true;
533 }
534
Jamie Madill98493dd2013-07-08 14:39:03 -0400535 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->fields().size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000536 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 return true;
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000540 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000541 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
542 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000543 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000544 return true;
545 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000548 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000550 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 return true;
552 }
553 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000554 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 return true;
556 }
557 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 return true;
560 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563}
564
565// This function checks to see if a void variable has been declared and raise an error message for such a case
566//
567// returns true in case of an error
568//
Jamie Madill075edd82013-07-08 13:30:19 -0400569bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000572 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 return true;
574 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577}
578
579// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
580//
581// returns true in case of an error
582//
Jamie Madill075edd82013-07-08 13:30:19 -0400583bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000586 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 return true;
588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591}
592
593// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
594//
595// returns true in case of an error
596//
Jamie Madill075edd82013-07-08 13:30:19 -0400597bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000599 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000600 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 return true;
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605}
606
Jamie Madill075edd82013-07-08 13:30:19 -0400607bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 if (pType.type == EbtStruct) {
610 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000611 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612
613 return true;
614 }
615
616 return false;
617 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000618 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000620 return true;
621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
Jamie Madill075edd82013-07-08 13:30:19 -0400626bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627{
Jamie Madillb120eac2013-06-12 14:08:13 -0400628 switch (pType.qualifier)
629 {
630 case EvqVaryingIn:
631 case EvqVaryingOut:
632 case EvqAttribute:
633 case EvqVertexInput:
634 case EvqFragmentOutput:
635 if (pType.type == EbtStruct)
636 {
637 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
638 return true;
639 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
643 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646}
647
Jamie Madill075edd82013-07-08 13:30:19 -0400648bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400649{
650 if (pType.layoutQualifier.location != -1)
651 {
652 error(line, "location must only be specified for a single input or output variable", "location");
653 return true;
654 }
655
656 return false;
657}
658
Jamie Madill075edd82013-07-08 13:30:19 -0400659bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
662 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000663 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return true;
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670bool TParseContext::containsSampler(TType& type)
671{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 if (IsSampler(type.getBasicType()))
673 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674
Jamie Madill98493dd2013-07-08 14:39:03 -0400675 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
676 const TFieldList& fields = type.getStruct()->fields();
677 for (unsigned int i = 0; i < fields.size(); ++i) {
678 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 return true;
680 }
681 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684}
685
686//
687// Do size checking for an array type's size.
688//
689// Returns true if there was an error.
690//
Jamie Madill075edd82013-07-08 13:30:19 -0400691bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000694
695 if (constant == 0 || !constant->isScalarInt())
696 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000697 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 return true;
699 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000701 if (constant->getBasicType() == EbtUInt)
702 {
703 unsigned int uintSize = constant->getUConst(0);
704 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
705 {
706 error(line, "array size too large", "");
707 size = 1;
708 return true;
709 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000711 size = static_cast<int>(uintSize);
712 }
713 else
714 {
715 size = constant->getIConst(0);
716
717 if (size <= 0)
718 {
719 error(line, "array size must be a positive integer", "");
720 size = 1;
721 return true;
722 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726}
727
728//
729// See if this qualifier can be an array.
730//
731// Returns true if there is an error.
732//
Jamie Madill075edd82013-07-08 13:30:19 -0400733bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734{
Jamie Madillb120eac2013-06-12 14:08:13 -0400735 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000736 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 return true;
738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
743//
744// See if this type can be an array.
745//
746// Returns true if there is an error.
747//
Jamie Madill075edd82013-07-08 13:30:19 -0400748bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 //
751 // Can the type be an array?
752 //
753 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000754 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 return true;
756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759}
760
761//
762// Do all the semantic checking for declaring an array, with and
763// without a size, and make the right changes to the symbol table.
764//
765// size == 0 means no specified size.
766//
767// Returns true if there was an error.
768//
Jamie Madill075edd82013-07-08 13:30:19 -0400769bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000771 //
772 // Don't check for reserved word use until after we know it's not in the symbol table,
773 // because reserved arrays can be redeclared.
774 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 bool builtIn = false;
777 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000778 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000779 if (symbol == 0 || !sameScope) {
780 if (reservedErrorCheck(line, identifier))
781 return true;
782
783 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000785 if (type.arraySize)
786 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000788 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000790 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 return true;
792 }
793 } else {
794 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000795 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 return true;
797 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 variable = static_cast<TVariable*>(symbol);
800 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000801 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000802 return true;
803 }
804 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000805 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 return true;
807 }
808
809 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000810 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return true;
812 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 if (type.arraySize)
815 variable->getType().setArraySize(type.arraySize);
816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 if (voidErrorCheck(line, identifier, type))
819 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822}
823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824//
825// Enforce non-initializer type/qualifier rules.
826//
827// Returns true if there was an error.
828//
Jamie Madill075edd82013-07-08 13:30:19 -0400829bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000831 if (type.qualifier == EvqConst)
832 {
833 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000835
836 if (array)
837 {
838 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
839 }
840 else if (type.isStructureContainingArrays())
841 {
842 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
843 }
844 else
845 {
846 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
847 }
848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 return true;
850 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853}
854
855//
856// Do semantic checking for a variable declaration that has no initializer,
857// and update the symbol table.
858//
859// Returns true if there was an error.
860//
Jamie Madill075edd82013-07-08 13:30:19 -0400861bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 if (reservedErrorCheck(line, identifier))
864 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
zmo@google.comfd747b82011-04-23 01:30:07 +0000866 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000868 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000869 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000871 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return true;
873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 if (voidErrorCheck(line, identifier, type))
876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
Jamie Madill075edd82013-07-08 13:30:19 -0400881bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000884 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return true;
886 }
887 if (qualifier == EvqConst && paramQualifier != EvqIn) {
888 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
889 return true;
890 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000893 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000895 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
Jamie Madill075edd82013-07-08 13:30:19 -0400900bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000901{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000902 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000903 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
904 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000905 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 return true;
907 }
zmo@google.comf5450912011-09-09 01:37:19 +0000908 // In GLSL ES, an extension's default behavior is "disable".
909 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000910 error(line, "extension", extension.c_str(), "is disabled");
911 return true;
912 }
913 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000914 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000915 return false;
916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
Jamie Madill075edd82013-07-08 13:30:19 -0400921bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400922{
923 if (structQualifierErrorCheck(identifierLocation, publicType))
924 return true;
925
926 // check for layout qualifier issues
927 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
928
929 if (layoutQualifier.matrixPacking != EmpUnspecified)
930 {
931 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400932 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400933 }
934
935 if (layoutQualifier.blockStorage != EbsUnspecified)
936 {
937 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400938 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400939 }
940
941 if (publicType.qualifier != EvqVertexInput && publicType.qualifier != EvqFragmentOutput && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
942 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400943 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400944 }
945
946 return false;
947}
948
Jamie Madill075edd82013-07-08 13:30:19 -0400949bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400950{
951 if (layoutQualifier.location != -1)
952 {
953 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
954 return true;
955 }
956
957 return false;
958}
959
zmo@google.com09c323a2011-08-12 18:22:25 +0000960bool TParseContext::supportsExtension(const char* extension)
961{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000962 const TExtensionBehavior& extbehavior = extensionBehavior();
963 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
964 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000965}
966
Jamie Madill5d287f52013-07-12 15:38:19 -0400967bool TParseContext::isExtensionEnabled(const char* extension) const
968{
969 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400970 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400971
972 if (iter == extbehavior.end())
973 {
974 return false;
975 }
976
977 return (iter->second == EBhEnable || iter->second == EBhRequire);
978}
979
Jamie Madill075edd82013-07-08 13:30:19 -0400980void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
981{
982 pp::SourceLocation srcLoc;
983 srcLoc.file = loc.first_file;
984 srcLoc.line = loc.first_line;
985 directiveHandler.handleExtension(srcLoc, extName, behavior);
986}
987
988void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
989{
990 pp::SourceLocation srcLoc;
991 srcLoc.file = loc.first_file;
992 srcLoc.line = loc.first_line;
993 directiveHandler.handlePragma(srcLoc, name, value);
994}
995
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996/////////////////////////////////////////////////////////////////////////////////
997//
998// Non-Errors.
999//
1000/////////////////////////////////////////////////////////////////////////////////
1001
1002//
1003// Look up a function name in the symbol table, and make sure it is a function.
1004//
1005// Return the function symbol if found, otherwise 0.
1006//
Jamie Madill075edd82013-07-08 13:30:19 -04001007const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001009 // First find by unmangled name to check whether the function name has been
1010 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001011 // If a function is found, check for one with a matching argument list.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001012 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001013 if (symbol == 0 || symbol->isFunction()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001014 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016
alokp@chromium.org0a576182010-08-09 17:16:27 +00001017 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001018 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001019 return 0;
1020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021
alokp@chromium.org0a576182010-08-09 17:16:27 +00001022 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001023 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 return 0;
1025 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001026
1027 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
1030//
1031// Initializers show up in several places in the grammar. Have one set of
1032// code to handle them here.
1033//
Jamie Madill075edd82013-07-08 13:30:19 -04001034bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001039 if (variable == 0) {
1040 if (reservedErrorCheck(line, identifier))
1041 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001043 if (voidErrorCheck(line, identifier, pType))
1044 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 //
1047 // add variable to symbol table
1048 //
1049 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001050 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001051 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001052 return true;
1053 // don't delete variable, it's used by error recovery, and the pool
1054 // pop will take care of the memory
1055 }
1056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 //
1059 // identifier must be of type constant, a global, or a temporary
1060 //
1061 TQualifier qualifier = variable->getType().getQualifier();
1062 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001063 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001064 return true;
1065 }
1066 //
1067 // test for and propagate constant
1068 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001070 if (qualifier == EvqConst) {
1071 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001072 std::stringstream extraInfoStream;
1073 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1074 std::string extraInfo = extraInfoStream.str();
1075 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001076 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 return true;
1078 }
1079 if (type != initializer->getType()) {
1080 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001081 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001082 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001083 return true;
1084 }
1085 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001086 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001088 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001091 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001092 variable->shareConstPointer(constArray);
1093 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001094 std::stringstream extraInfoStream;
1095 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1096 std::string extraInfo = extraInfoStream.str();
1097 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001098 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 return true;
1100 }
1101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 if (qualifier != EvqConst) {
1104 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1105 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1106 if (intermNode == 0) {
1107 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1108 return true;
1109 }
1110 } else
1111 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
1116bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1117{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001118 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 if (!aggrNode->isConstructor())
1120 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 // check if all the child nodes are constants so that they can be inserted into
1125 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001126 TIntermSequence &sequence = aggrNode->getSequence() ;
1127 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1128 if (!(*p)->getAsTyped()->getAsConstantUnion())
1129 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133}
1134
Jamie Madilla5efff92013-06-06 11:56:47 -04001135TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001136{
1137 TPublicType returnType = typeSpecifier;
1138 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001139 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001140
1141 if (typeSpecifier.array)
1142 {
1143 error(typeSpecifier.line, "not supported", "first-class array");
1144 recover();
1145 returnType.setArray(false);
1146 }
1147
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001148 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001149 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001150 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1151 {
1152 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1153 recover();
1154 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001155
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001156 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1157 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1158 {
1159 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1160 recover();
1161 }
1162 }
1163 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001164 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001165 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001166 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001167 case EvqVertexInput:
1168 case EvqFragmentOutput:
1169 case EvqVaryingIn:
1170 case EvqVaryingOut:
1171 if (typeSpecifier.type == EbtBool)
1172 {
1173 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1174 recover();
1175 }
1176 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001177
Jamie Madillb120eac2013-06-12 14:08:13 -04001178 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001179 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001180 }
1181
1182 return returnType;
1183}
1184
Jamie Madill075edd82013-07-08 13:30:19 -04001185TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001186{
1187 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1188 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1189
1190 if (identifier != "")
1191 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001192 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001193 recover();
1194
1195 // this error check can mutate the type
1196 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1197 recover();
1198
1199 TVariable* variable = 0;
1200
1201 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1202 recover();
1203
1204 if (variable && symbol)
1205 {
1206 symbol->setId(variable->getUniqueId());
1207 }
1208 }
1209
1210 return aggregate;
1211}
1212
Jamie Madill075edd82013-07-08 13:30:19 -04001213TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001214{
Jamie Madill51a53c72013-06-19 09:24:43 -04001215 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001216 recover();
1217
1218 // this error check can mutate the type
1219 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1220 recover();
1221
1222 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1223 {
1224 recover();
1225 }
1226
1227 TPublicType arrayType = publicType;
1228
1229 int size;
1230 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1231 {
1232 recover();
1233 }
1234 else
1235 {
1236 arrayType.setArray(true, size);
1237 }
1238
1239 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1240 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1241 TVariable* variable = 0;
1242
1243 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1244 recover();
1245
1246 if (variable && symbol)
1247 {
1248 symbol->setId(variable->getUniqueId());
1249 }
1250
1251 return aggregate;
1252}
1253
Jamie Madill075edd82013-07-08 13:30:19 -04001254TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001255{
Jamie Madilla5efff92013-06-06 11:56:47 -04001256 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001257 recover();
1258
1259 TIntermNode* intermNode;
1260 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1261 {
1262 //
1263 // Build intermediate representation
1264 //
1265 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1266 }
1267 else
1268 {
1269 recover();
1270 return NULL;
1271 }
1272}
1273
Jamie Madill075edd82013-07-08 13:30:19 -04001274TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001275{
1276 if (publicType.type == EbtInvariant && !identifierSymbol)
1277 {
1278 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1279 recover();
1280 }
1281
1282 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1283 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1284
1285 if (structQualifierErrorCheck(identifierLocation, publicType))
1286 recover();
1287
Jamie Madill0bd18df2013-06-20 11:55:52 -04001288 if (locationDeclaratorListCheck(identifierLocation, publicType))
1289 recover();
1290
Jamie Madill502d66f2013-06-20 11:55:52 -04001291 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1292 recover();
1293
1294 TVariable* variable = 0;
1295 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1296 recover();
1297 if (symbol && variable)
1298 symbol->setId(variable->getUniqueId());
1299
1300 return intermAggregate;
1301}
1302
Jamie Madill075edd82013-07-08 13:30:19 -04001303TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001304{
1305 if (structQualifierErrorCheck(identifierLocation, publicType))
1306 recover();
1307
Jamie Madill0bd18df2013-06-20 11:55:52 -04001308 if (locationDeclaratorListCheck(identifierLocation, publicType))
1309 recover();
1310
Jamie Madill502d66f2013-06-20 11:55:52 -04001311 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1312 recover();
1313
1314 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1315 {
1316 recover();
1317 }
1318 else if (indexExpression)
1319 {
1320 int size;
1321 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1322 recover();
1323 TPublicType arrayType(publicType);
1324 arrayType.setArray(true, size);
1325 TVariable* variable = NULL;
1326 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1327 recover();
1328 TType type = TType(arrayType);
1329 type.setArraySize(size);
1330
1331 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1332 }
1333 else
1334 {
1335 TPublicType arrayType(publicType);
1336 arrayType.setArray(true);
1337 TVariable* variable = NULL;
1338 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1339 recover();
1340 }
1341
1342 return NULL;
1343}
1344
Jamie Madill075edd82013-07-08 13:30:19 -04001345TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001346{
1347 if (structQualifierErrorCheck(identifierLocation, publicType))
1348 recover();
1349
Jamie Madill0bd18df2013-06-20 11:55:52 -04001350 if (locationDeclaratorListCheck(identifierLocation, publicType))
1351 recover();
1352
Jamie Madill502d66f2013-06-20 11:55:52 -04001353 TIntermNode* intermNode;
1354 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1355 {
1356 //
1357 // build the intermediate representation
1358 //
1359 if (intermNode)
1360 {
1361 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1362 }
1363 else
1364 {
1365 return declaratorList;
1366 }
1367 }
1368 else
1369 {
1370 recover();
1371 return NULL;
1372 }
1373}
1374
Jamie Madilla295edf2013-06-06 11:56:48 -04001375void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1376{
1377 if (typeQualifier.qualifier != EvqUniform)
1378 {
1379 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1380 recover();
1381 return;
1382 }
1383
1384 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1385 ASSERT(!layoutQualifier.isEmpty());
1386
1387 if (shaderVersion < 300)
1388 {
1389 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1390 recover();
1391 return;
1392 }
1393
1394 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1395 {
1396 recover();
1397 return;
1398 }
1399
Jamie Madill099c0f32013-06-20 11:55:52 -04001400 if (layoutQualifier.matrixPacking != EmpUnspecified)
1401 {
1402 defaultMatrixPacking = layoutQualifier.matrixPacking;
1403 }
1404
Jamie Madill1566ef72013-06-20 11:55:54 -04001405 if (layoutQualifier.blockStorage != EmpUnspecified)
1406 {
1407 defaultBlockStorage = layoutQualifier.blockStorage;
1408 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001409}
1410
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001411TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1412{
1413 TOperator op = EOpNull;
1414 if (publicType.userDef)
1415 {
1416 op = EOpConstructStruct;
1417 }
1418 else
1419 {
1420 switch (publicType.type)
1421 {
1422 case EbtFloat:
1423 if (publicType.isMatrix())
1424 {
1425 // TODO: non-square matrices
1426 switch(publicType.getCols())
1427 {
Jamie Madill28b97422013-07-08 14:01:38 -04001428 case 2: op = EOpConstructMat2; break;
1429 case 3: op = EOpConstructMat3; break;
1430 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001431 }
1432 }
1433 else
1434 {
1435 switch(publicType.getNominalSize())
1436 {
Jamie Madill28b97422013-07-08 14:01:38 -04001437 case 1: op = EOpConstructFloat; break;
1438 case 2: op = EOpConstructVec2; break;
1439 case 3: op = EOpConstructVec3; break;
1440 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001441 }
1442 }
1443 break;
1444
1445 case EbtInt:
1446 switch(publicType.getNominalSize())
1447 {
Jamie Madill28b97422013-07-08 14:01:38 -04001448 case 1: op = EOpConstructInt; break;
1449 case 2: op = EOpConstructIVec2; break;
1450 case 3: op = EOpConstructIVec3; break;
1451 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001452 }
1453 break;
1454
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001455 case EbtUInt:
1456 switch(publicType.getNominalSize())
1457 {
Jamie Madill28b97422013-07-08 14:01:38 -04001458 case 1: op = EOpConstructUInt; break;
1459 case 2: op = EOpConstructUVec2; break;
1460 case 3: op = EOpConstructUVec3; break;
1461 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001462 }
1463 break;
1464
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001465 case EbtBool:
1466 switch(publicType.getNominalSize())
1467 {
Jamie Madill28b97422013-07-08 14:01:38 -04001468 case 1: op = EOpConstructBool; break;
1469 case 2: op = EOpConstructBVec2; break;
1470 case 3: op = EOpConstructBVec3; break;
1471 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001472 }
1473 break;
1474
1475 default: break;
1476 }
1477
1478 if (op == EOpNull)
1479 {
1480 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1481 recover();
1482 publicType.type = EbtFloat;
1483 op = EOpConstructFloat;
1484 }
1485 }
1486
1487 TString tempString;
1488 TType type(publicType);
1489 return new TFunction(&tempString, type, op);
1490}
1491
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492// This function is used to test for the correctness of the parameters passed to various constructor functions
1493// and also convert them to the right datatype if it is allowed and required.
1494//
1495// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1496//
Jamie Madill075edd82013-07-08 13:30:19 -04001497TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001499 if (node == 0)
1500 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001501
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001502 TIntermAggregate* aggrNode = node->getAsAggregate();
1503
Jamie Madill98493dd2013-07-08 14:39:03 -04001504 TFieldList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001505 if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001506 memberTypes = type->getStruct()->fields().begin();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001507
1508 TType elementType = *type;
1509 if (type->isArray())
1510 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001512 bool singleArg;
1513 if (aggrNode) {
1514 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1515 singleArg = true;
1516 else
1517 singleArg = false;
1518 } else
1519 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001521 TIntermTyped *newNode;
1522 if (singleArg) {
1523 // If structure constructor or array constructor is being called
1524 // for only one parameter inside the structure, we need to call constructStruct function once.
1525 if (type->isArray())
1526 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1527 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001528 newNode = constructStruct(node, (*memberTypes)->type(), 1, node->getLine(), false);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001529 else
1530 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001532 if (newNode && newNode->getAsAggregate()) {
1533 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1534 if (constConstructor)
1535 return constConstructor;
1536 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001538 return newNode;
1539 }
1540
1541 //
1542 // Handle list of arguments.
1543 //
1544 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1545 // if the structure constructor contains more than one parameter, then construct
1546 // each parameter
1547
1548 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1549
1550 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1551 // to the right type if possible (and allowed).
1552 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1553
1554 for (TIntermSequence::iterator p = sequenceVector.begin();
1555 p != sequenceVector.end(); p++, paramCount++) {
1556 if (type->isArray())
1557 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1558 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001559 newNode = constructStruct(*p, (memberTypes[paramCount])->type(), paramCount+1, node->getLine(), true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001560 else
1561 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1562
1563 if (newNode) {
1564 *p = newNode;
1565 }
1566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001568 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1569 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1570 if (constConstructor)
1571 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001573 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574}
1575
1576TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1577{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001578 bool canBeFolded = areAllChildConst(aggrNode);
1579 aggrNode->setType(type);
1580 if (canBeFolded) {
1581 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001582 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001583 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001584 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001585 }
1586 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001587 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001588 }
1589 if (returnVal)
1590 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001592 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001595 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596}
1597
1598// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1599// for the parameter to the constructor (passed to this function). Essentially, it converts
1600// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1601// float, then float is converted to int.
1602//
1603// Returns 0 for an error or the constructed node.
1604//
Jamie Madill075edd82013-07-08 13:30:19 -04001605TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001607 TIntermTyped* newNode;
1608 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001610 //
1611 // First, convert types as needed.
1612 //
1613 switch (op) {
1614 case EOpConstructVec2:
1615 case EOpConstructVec3:
1616 case EOpConstructVec4:
1617 case EOpConstructMat2:
1618 case EOpConstructMat3:
1619 case EOpConstructMat4:
1620 case EOpConstructFloat:
1621 basicOp = EOpConstructFloat;
1622 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001624 case EOpConstructIVec2:
1625 case EOpConstructIVec3:
1626 case EOpConstructIVec4:
1627 case EOpConstructInt:
1628 basicOp = EOpConstructInt;
1629 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001631 case EOpConstructUVec2:
1632 case EOpConstructUVec3:
1633 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001634 case EOpConstructUInt:
1635 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001636 break;
1637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001638 case EOpConstructBVec2:
1639 case EOpConstructBVec3:
1640 case EOpConstructBVec4:
1641 case EOpConstructBool:
1642 basicOp = EOpConstructBool;
1643 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001645 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001646 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001647 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001649 return 0;
1650 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001651 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001652 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001653 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001654 return 0;
1655 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001657 //
1658 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1659 //
1660
1661 // Otherwise, skip out early.
1662 if (subset || (newNode != node && newNode->getType() == *type))
1663 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001665 // setAggregateOperator will insert a new node for the constructor, as needed.
1666 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667}
1668
1669// This function tests for the type of the parameters to the structures constructors. Raises
1670// an error message if the expected type does not match the parameter passed to the constructor.
1671//
1672// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1673//
Jamie Madill075edd82013-07-08 13:30:19 -04001674TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001676 if (*type == node->getAsTyped()->getType()) {
1677 if (subset)
1678 return node->getAsTyped();
1679 else
1680 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1681 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001682 std::stringstream extraInfoStream;
1683 extraInfoStream << "cannot convert parameter " << paramCount
1684 << " from '" << node->getAsTyped()->getType().getBasicString()
1685 << "' to '" << type->getBasicString() << "'";
1686 std::string extraInfo = extraInfoStream.str();
1687 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001688 recover();
1689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001691 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692}
1693
1694//
1695// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1696// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1697// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1698// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1699// a constant matrix.
1700//
Jamie Madill075edd82013-07-08 13:30:19 -04001701TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001703 TIntermTyped* typedNode;
1704 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001706 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001707 if (tempConstantNode) {
1708 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001710 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001711 return node;
1712 }
1713 } 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 +00001714 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001715 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 return 0;
1718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001720 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001722 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001723 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001724 std::stringstream extraInfoStream;
1725 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1726 std::string extraInfo = extraInfoStream.str();
1727 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001728 recover();
1729 fields.offsets[i] = 0;
1730 }
1731
1732 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 }
1735 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1736 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737}
1738
1739//
1740// This function returns the column being accessed from a constant matrix. The values are retrieved from
1741// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1742// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1743// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1744//
Jamie Madill075edd82013-07-08 13:30:19 -04001745TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001747 TIntermTyped* typedNode;
1748 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001750 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001751 std::stringstream extraInfoStream;
1752 extraInfoStream << "matrix field selection out of range '" << index << "'";
1753 std::string extraInfo = extraInfoStream.str();
1754 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 recover();
1756 index = 0;
1757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001759 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001760 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001761 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001762 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1763 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001764 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 return 0;
1768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771}
1772
1773
1774//
1775// This function returns an element of an array accessed from a constant array. The values are retrieved from
1776// the symbol table and parse-tree is built for the type of the element. The input
1777// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1778// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1779//
Jamie Madill075edd82013-07-08 13:30:19 -04001780TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 TIntermTyped* typedNode;
1783 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1784 TType arrayElementType = node->getType();
1785 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001787 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001788 std::stringstream extraInfoStream;
1789 extraInfoStream << "array field selection out of range '" << index << "'";
1790 std::string extraInfo = extraInfoStream.str();
1791 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 recover();
1793 index = 0;
1794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001796 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001797 size_t arrayElementSize = arrayElementType.getObjectSize();
1798 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1799 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001801 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 return 0;
1805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001807 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808}
1809
1810
1811//
1812// This function returns the value of a particular field inside a constant structure from the symbol table.
1813// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1814// function and returns the parse-tree with the values of the embedded/nested struct.
1815//
Jamie Madill075edd82013-07-08 13:30:19 -04001816TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817{
Jamie Madill98493dd2013-07-08 14:39:03 -04001818 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001819 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 for (size_t index = 0; index < fields.size(); ++index) {
1822 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 break;
1824 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001825 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 }
1827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
Jamie Madill94bf7f22013-07-08 13:31:15 -04001829 TIntermTyped *typedNode;
1830 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001832 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1835 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001836 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 return 0;
1840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843}
1844
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001845//
1846// Interface/uniform blocks
1847//
Jamie Madill98493dd2013-07-08 14:39:03 -04001848TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1849 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001850{
1851 if (reservedErrorCheck(nameLine, blockName))
1852 recover();
1853
1854 if (typeQualifier.qualifier != EvqUniform)
1855 {
1856 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1857 recover();
1858 }
1859
Jamie Madill099c0f32013-06-20 11:55:52 -04001860 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1861 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001862 {
1863 recover();
1864 }
1865
Jamie Madill099c0f32013-06-20 11:55:52 -04001866 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1867 {
1868 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1869 }
1870
Jamie Madill1566ef72013-06-20 11:55:54 -04001871 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1872 {
1873 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1874 }
1875
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001876 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1877 if (!symbolTable.declare(*blockNameSymbol)) {
1878 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1879 recover();
1880 }
1881
Jamie Madill98493dd2013-07-08 14:39:03 -04001882 // check for sampler types and apply layout qualifiers
1883 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1884 TField* field = (*fieldList)[memberIndex];
1885 TType* fieldType = field->type();
1886 if (IsSampler(fieldType->getBasicType())) {
1887 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001888 recover();
1889 }
1890
Jamie Madill98493dd2013-07-08 14:39:03 -04001891 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001892 switch (qualifier)
1893 {
1894 case EvqGlobal:
1895 case EvqUniform:
1896 break;
1897 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001898 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001899 recover();
1900 break;
1901 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001902
1903 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001904 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1905 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001906 {
1907 recover();
1908 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001909
Jamie Madill98493dd2013-07-08 14:39:03 -04001910 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001911 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001913 recover();
1914 }
1915
Jamie Madill98493dd2013-07-08 14:39:03 -04001916 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001917 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001920 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001921 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001923 recover();
1924 }
1925
Jamie Madill98493dd2013-07-08 14:39:03 -04001926 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001927 }
1928
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 // add array index
1930 int arraySize = 0;
1931 if (arrayIndex != NULL)
1932 {
1933 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1934 recover();
1935 }
1936
1937 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1938 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001939
1940 TString symbolName = "";
1941 int symbolId = 0;
1942
Jamie Madill98493dd2013-07-08 14:39:03 -04001943 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001944 {
1945 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001946 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1947 {
1948 TField* field = (*fieldList)[memberIndex];
1949 TType* fieldType = field->type();
1950
1951 // set parent pointer of the field variable
1952 fieldType->setInterfaceBlock(interfaceBlock);
1953
1954 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1955 fieldVariable->setQualifier(typeQualifier.qualifier);
1956
1957 if (!symbolTable.declare(*fieldVariable)) {
1958 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001959 recover();
1960 }
1961 }
1962 }
1963 else
1964 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001965 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001966 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001967 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001968
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001969 if (!symbolTable.declare(*instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001970 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001971 recover();
1972 }
1973
1974 symbolId = instanceTypeDef->getUniqueId();
1975 symbolName = instanceTypeDef->getName();
1976 }
1977
Jamie Madill98493dd2013-07-08 14:39:03 -04001978 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001979 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001980
1981 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001982 return aggregate;
1983}
1984
Jamie Madill075edd82013-07-08 13:30:19 -04001985bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001986{
1987 ++structNestingLevel;
1988
1989 // Embedded structure definitions are not supported per GLSL ES spec.
1990 // They aren't allowed in GLSL either, but we need to detect this here
1991 // so we don't rely on the GLSL compiler to catch it.
1992 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001993 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001994 return true;
1995 }
1996
1997 return false;
1998}
1999
2000void TParseContext::exitStructDeclaration()
2001{
2002 --structNestingLevel;
2003}
2004
2005namespace {
2006
2007const int kWebGLMaxStructNesting = 4;
2008
2009} // namespace
2010
Jamie Madill98493dd2013-07-08 14:39:03 -04002011bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002012{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002013 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014 return false;
2015 }
2016
Jamie Madill98493dd2013-07-08 14:39:03 -04002017 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002018 return false;
2019 }
2020
2021 // We're already inside a structure definition at this point, so add
2022 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002023 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002024 std::stringstream extraInfoStream;
Jamie Madill98493dd2013-07-08 14:39:03 -04002025 extraInfoStream << "Reference of struct type " << field.type()->getStruct()->name()
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002026 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2027 std::string extraInfo = extraInfoStream.str();
2028 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002029 return true;
2030 }
2031
2032 return false;
2033}
2034
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002035//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002036// Parse an array index expression
2037//
Jamie Madill075edd82013-07-08 13:30:19 -04002038TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002039{
2040 TIntermTyped *indexedExpression = NULL;
2041
2042 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2043 {
2044 if (baseExpression->getAsSymbolNode())
2045 {
2046 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2047 }
2048 else
2049 {
2050 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2051 }
2052 recover();
2053 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002054
Jamie Madill7164cf42013-07-08 13:30:59 -04002055 if (indexExpression->getQualifier() == EvqConst)
2056 {
2057 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2058 if (index < 0)
2059 {
2060 std::stringstream infoStream;
2061 infoStream << index;
2062 std::string info = infoStream.str();
2063 error(location, "negative index", info.c_str());
2064 recover();
2065 index = 0;
2066 }
2067 if (baseExpression->getType().getQualifier() == EvqConst)
2068 {
2069 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002070 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002071 // constant folding for arrays
2072 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002073 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002074 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002075 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002076 // constant folding for vectors
2077 TVectorFields fields;
2078 fields.num = 1;
2079 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2080 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2081 }
2082 else if (baseExpression->isMatrix())
2083 {
2084 // constant folding for matrices
2085 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002086 }
2087 }
2088 else
2089 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002090 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002091 {
Jamie Madill18464b52013-07-08 14:01:55 -04002092 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002093 {
2094 std::stringstream extraInfoStream;
2095 extraInfoStream << "array index out of range '" << index << "'";
2096 std::string extraInfo = extraInfoStream.str();
2097 error(location, "", "[", extraInfo.c_str());
2098 recover();
2099 index = baseExpression->getType().getArraySize() - 1;
2100 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002101 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2102 {
2103 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2104 recover();
2105 index = 0;
2106 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002107 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002108 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002109 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002110 std::stringstream extraInfoStream;
2111 extraInfoStream << "field selection out of range '" << index << "'";
2112 std::string extraInfo = extraInfoStream.str();
2113 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002114 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002115 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002116 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002117
Jamie Madill7164cf42013-07-08 13:30:59 -04002118 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2119 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002120 }
2121 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002122 else
2123 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002124 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002125 {
2126 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2127 recover();
2128 }
2129 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2130 {
2131 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2132 recover();
2133 }
2134
2135 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2136 }
2137
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002138 if (indexedExpression == 0)
2139 {
2140 ConstantUnion *unionArray = new ConstantUnion[1];
2141 unionArray->setFConst(0.0f);
2142 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2143 }
2144 else if (baseExpression->isArray())
2145 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002146 const TType &baseType = baseExpression->getType();
2147 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002148 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002149 TType copyOfType(baseType.getStruct());
2150 indexedExpression->setType(copyOfType);
2151 }
2152 else if (baseType.isInterfaceBlock())
2153 {
2154 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002155 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002156 }
2157 else
2158 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002159 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002160 }
2161
2162 if (baseExpression->getType().getQualifier() == EvqConst)
2163 {
2164 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2165 }
2166 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002167 else if (baseExpression->isMatrix())
2168 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002169 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2170 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002171 }
2172 else if (baseExpression->isVector())
2173 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002174 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2175 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002176 }
2177 else
2178 {
2179 indexedExpression->setType(baseExpression->getType());
2180 }
2181
2182 return indexedExpression;
2183}
2184
Jamie Madill075edd82013-07-08 13:30:19 -04002185TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002186{
2187 TIntermTyped *indexedExpression = NULL;
2188
2189 if (baseExpression->isArray())
2190 {
2191 error(fieldLocation, "cannot apply dot operator to an array", ".");
2192 recover();
2193 }
2194
2195 if (baseExpression->isVector())
2196 {
2197 TVectorFields fields;
2198 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2199 {
2200 fields.num = 1;
2201 fields.offsets[0] = 0;
2202 recover();
2203 }
2204
2205 if (baseExpression->getType().getQualifier() == EvqConst)
2206 {
2207 // constant folding for vector fields
2208 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2209 if (indexedExpression == 0)
2210 {
2211 recover();
2212 indexedExpression = baseExpression;
2213 }
2214 else
2215 {
2216 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2217 }
2218 }
2219 else
2220 {
2221 TString vectorString = fieldString;
2222 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2223 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2224 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2225 }
2226 }
2227 else if (baseExpression->isMatrix())
2228 {
2229 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002230 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002231 {
2232 fields.wholeRow = false;
2233 fields.wholeCol = false;
2234 fields.row = 0;
2235 fields.col = 0;
2236 recover();
2237 }
2238
2239 if (fields.wholeRow || fields.wholeCol)
2240 {
2241 error(dotLocation, " non-scalar fields not implemented yet", ".");
2242 recover();
2243 ConstantUnion *unionArray = new ConstantUnion[1];
2244 unionArray->setIConst(0);
2245 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2246 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002247 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002248 }
2249 else
2250 {
2251 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002252 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002253 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2254 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2255 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2256 }
2257 }
2258 else if (baseExpression->getBasicType() == EbtStruct)
2259 {
2260 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002261 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2262 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002263 {
2264 error(dotLocation, "structure has no fields", "Internal Error");
2265 recover();
2266 indexedExpression = baseExpression;
2267 }
2268 else
2269 {
2270 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002271 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002272 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002273 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002274 {
2275 fieldFound = true;
2276 break;
2277 }
2278 }
2279 if (fieldFound)
2280 {
2281 if (baseExpression->getType().getQualifier() == EvqConst)
2282 {
2283 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2284 if (indexedExpression == 0)
2285 {
2286 recover();
2287 indexedExpression = baseExpression;
2288 }
2289 else
2290 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002291 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002292 // change the qualifier of the return type, not of the structure field
2293 // as the structure definition is shared between various structures.
2294 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2295 }
2296 }
2297 else
2298 {
2299 ConstantUnion *unionArray = new ConstantUnion[1];
2300 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002301 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002302 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002303 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002304 }
2305 }
2306 else
2307 {
2308 error(dotLocation, " no such field in structure", fieldString.c_str());
2309 recover();
2310 indexedExpression = baseExpression;
2311 }
2312 }
2313 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002314 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002315 {
2316 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002317 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2318 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002319 {
2320 error(dotLocation, "interface block has no fields", "Internal Error");
2321 recover();
2322 indexedExpression = baseExpression;
2323 }
2324 else
2325 {
2326 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002327 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002328 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002329 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002330 {
2331 fieldFound = true;
2332 break;
2333 }
2334 }
2335 if (fieldFound)
2336 {
2337 ConstantUnion *unionArray = new ConstantUnion[1];
2338 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002339 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002340 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002341 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002342 }
2343 else
2344 {
2345 error(dotLocation, " no such field in interface block", fieldString.c_str());
2346 recover();
2347 indexedExpression = baseExpression;
2348 }
2349 }
2350 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002351 else
2352 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002353 if (shaderVersion < 300)
2354 {
2355 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2356 }
2357 else
2358 {
2359 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2360 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002361 recover();
2362 indexedExpression = baseExpression;
2363 }
2364
2365 return indexedExpression;
2366}
2367
Jamie Madill075edd82013-07-08 13:30:19 -04002368TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002369{
Jamie Madilla5efff92013-06-06 11:56:47 -04002370 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002371
Jamie Madilla5efff92013-06-06 11:56:47 -04002372 qualifier.location = -1;
2373 qualifier.matrixPacking = EmpUnspecified;
2374 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002375
2376 if (qualifierType == "shared")
2377 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002378 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002379 }
2380 else if (qualifierType == "packed")
2381 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002382 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002383 }
2384 else if (qualifierType == "std140")
2385 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002386 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002387 }
2388 else if (qualifierType == "row_major")
2389 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002390 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002391 }
2392 else if (qualifierType == "column_major")
2393 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002394 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002395 }
2396 else if (qualifierType == "location")
2397 {
2398 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2399 recover();
2400 }
2401 else
2402 {
2403 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2404 recover();
2405 }
2406
Jamie Madilla5efff92013-06-06 11:56:47 -04002407 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002408}
2409
Jamie Madill075edd82013-07-08 13:30:19 -04002410TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002411{
Jamie Madilla5efff92013-06-06 11:56:47 -04002412 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002413
Jamie Madilla5efff92013-06-06 11:56:47 -04002414 qualifier.location = -1;
2415 qualifier.matrixPacking = EmpUnspecified;
2416 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002417
2418 if (qualifierType != "location")
2419 {
2420 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2421 recover();
2422 }
2423 else
2424 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002425 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002426 if (intValue < 0)
2427 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002428 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002429 recover();
2430 }
2431 else
2432 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002434 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002435 }
2436
Jamie Madilla5efff92013-06-06 11:56:47 -04002437 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002438}
2439
Jamie Madilla5efff92013-06-06 11:56:47 -04002440TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002441{
Jamie Madilla5efff92013-06-06 11:56:47 -04002442 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002443
Jamie Madilla5efff92013-06-06 11:56:47 -04002444 if (rightQualifier.location != -1)
2445 {
2446 joinedQualifier.location = rightQualifier.location;
2447 }
2448 if (rightQualifier.matrixPacking != EmpUnspecified)
2449 {
2450 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2451 }
2452 if (rightQualifier.blockStorage != EbsUnspecified)
2453 {
2454 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2455 }
2456
2457 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002458}
2459
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002460TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2461 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2462{
2463 TQualifier mergedQualifier = EvqSmoothIn;
2464
2465 if (storageQualifier == EvqSmoothIn) {
2466 if (interpolationQualifier == EvqSmooth)
2467 mergedQualifier = EvqSmoothIn;
2468 else if (interpolationQualifier == EvqFlat)
2469 mergedQualifier = EvqFlatIn;
2470 else UNREACHABLE();
2471 }
2472 else if (storageQualifier == EvqCentroidIn) {
2473 if (interpolationQualifier == EvqSmooth)
2474 mergedQualifier = EvqCentroidIn;
2475 else if (interpolationQualifier == EvqFlat)
2476 mergedQualifier = EvqFlatIn;
2477 else UNREACHABLE();
2478 }
2479 else if (storageQualifier == EvqSmoothOut) {
2480 if (interpolationQualifier == EvqSmooth)
2481 mergedQualifier = EvqSmoothOut;
2482 else if (interpolationQualifier == EvqFlat)
2483 mergedQualifier = EvqFlatOut;
2484 else UNREACHABLE();
2485 }
2486 else if (storageQualifier == EvqCentroidOut) {
2487 if (interpolationQualifier == EvqSmooth)
2488 mergedQualifier = EvqCentroidOut;
2489 else if (interpolationQualifier == EvqFlat)
2490 mergedQualifier = EvqFlatOut;
2491 else UNREACHABLE();
2492 }
2493 else {
2494 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2495 recover();
2496
2497 mergedQualifier = storageQualifier;
2498 }
2499
2500 TPublicType type;
2501 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2502 return type;
2503}
2504
Jamie Madill98493dd2013-07-08 14:39:03 -04002505TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002506{
Jamie Madill98493dd2013-07-08 14:39:03 -04002507 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002508 recover();
2509 }
2510
Jamie Madill98493dd2013-07-08 14:39:03 -04002511 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002512 //
2513 // Careful not to replace already known aspects of type, like array-ness
2514 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002515 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002516 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002517 type->setPrimarySize(typeSpecifier.primarySize);
2518 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002519 type->setPrecision(typeSpecifier.precision);
2520 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002521 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002522
2523 // don't allow arrays of arrays
2524 if (type->isArray()) {
2525 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2526 recover();
2527 }
2528 if (typeSpecifier.array)
2529 type->setArraySize(typeSpecifier.arraySize);
2530 if (typeSpecifier.userDef) {
2531 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002532 }
2533
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002535 recover();
2536 }
2537 }
2538
Jamie Madill98493dd2013-07-08 14:39:03 -04002539 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002540}
2541
Jamie Madill98493dd2013-07-08 14:39:03 -04002542TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002543{
Jamie Madill98493dd2013-07-08 14:39:03 -04002544 TStructure* structure = new TStructure(structName, fieldList);
2545 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002546
Jamie Madill98493dd2013-07-08 14:39:03 -04002547 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002548 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002549 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002550 {
2551 recover();
2552 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002554 if (!symbolTable.declare(*userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002556 recover();
2557 }
2558 }
2559
2560 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002561 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002563 const TField &field = *(*fieldList)[typeListIndex];
2564 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002565 switch (qualifier)
2566 {
2567 case EvqGlobal:
2568 case EvqTemporary:
2569 break;
2570 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002571 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002572 recover();
2573 break;
2574 }
2575 }
2576
2577 TPublicType publicType;
2578 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002579 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002580 exitStructDeclaration();
2581
2582 return publicType;
2583}
2584
2585//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002586// Parse an array of strings using yyparse.
2587//
2588// Returns 0 for success.
2589//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002590int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002591 TParseContext* context) {
2592 if ((count == 0) || (string == NULL))
2593 return 1;
2594
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002595 if (glslang_initialize(context))
2596 return 1;
2597
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002598 int error = glslang_scan(count, string, length, context);
2599 if (!error)
2600 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002601
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002602 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002603
alokp@chromium.org6b495712012-06-29 00:06:58 +00002604 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002605}
2606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002608