blob: 1bab3ce4984ed49b0bb6d515c6b7fd471f7f5bf9 [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 Madill075edd82013-07-08 13:30:19 -0400967void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
968{
969 pp::SourceLocation srcLoc;
970 srcLoc.file = loc.first_file;
971 srcLoc.line = loc.first_line;
972 directiveHandler.handleExtension(srcLoc, extName, behavior);
973}
974
975void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
976{
977 pp::SourceLocation srcLoc;
978 srcLoc.file = loc.first_file;
979 srcLoc.line = loc.first_line;
980 directiveHandler.handlePragma(srcLoc, name, value);
981}
982
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983/////////////////////////////////////////////////////////////////////////////////
984//
985// Non-Errors.
986//
987/////////////////////////////////////////////////////////////////////////////////
988
989//
990// Look up a function name in the symbol table, and make sure it is a function.
991//
992// Return the function symbol if found, otherwise 0.
993//
Jamie Madill075edd82013-07-08 13:30:19 -0400994const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000996 // First find by unmangled name to check whether the function name has been
997 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000998 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +0000999 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001000 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001001 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002
alokp@chromium.org0a576182010-08-09 17:16:27 +00001003 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001004 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 return 0;
1006 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
alokp@chromium.org0a576182010-08-09 17:16:27 +00001008 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001009 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 return 0;
1011 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001012
1013 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014}
1015
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001016bool TParseContext::isVariableBuiltIn(const TVariable* var)
1017{
1018 // First find by unmangled name to check whether the function name has been
1019 // hidden by a variable name or struct typename.
1020 const TSymbol* symbol = symbolTable.findBuiltIn(var->getName(), shaderVersion);
1021 if (symbol == 0) {
1022 symbol = symbolTable.findBuiltIn(var->getMangledName(), shaderVersion);
1023 }
1024
1025 if (symbol == 0) {
1026 return false;
1027 }
1028
1029 return symbol->isVariable();
1030}
1031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032//
1033// Initializers show up in several places in the grammar. Have one set of
1034// code to handle them here.
1035//
Jamie Madill075edd82013-07-08 13:30:19 -04001036bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001039 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001041 if (variable == 0) {
1042 if (reservedErrorCheck(line, identifier))
1043 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001045 if (voidErrorCheck(line, identifier, pType))
1046 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 //
1049 // add variable to symbol table
1050 //
1051 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001052 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001053 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 return true;
1055 // don't delete variable, it's used by error recovery, and the pool
1056 // pop will take care of the memory
1057 }
1058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 //
1061 // identifier must be of type constant, a global, or a temporary
1062 //
1063 TQualifier qualifier = variable->getType().getQualifier();
1064 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001065 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 return true;
1067 }
1068 //
1069 // test for and propagate constant
1070 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001072 if (qualifier == EvqConst) {
1073 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 std::stringstream extraInfoStream;
1075 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1076 std::string extraInfo = extraInfoStream.str();
1077 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001078 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 return true;
1080 }
1081 if (type != initializer->getType()) {
1082 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001083 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001084 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 return true;
1086 }
1087 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001088 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001090 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001093 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 variable->shareConstPointer(constArray);
1095 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001096 std::stringstream extraInfoStream;
1097 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1098 std::string extraInfo = extraInfoStream.str();
1099 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001100 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 return true;
1102 }
1103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001105 if (qualifier != EvqConst) {
1106 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1107 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1108 if (intermNode == 0) {
1109 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1110 return true;
1111 }
1112 } else
1113 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001115 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
1118bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1119{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001120 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001121 if (!aggrNode->isConstructor())
1122 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001126 // check if all the child nodes are constants so that they can be inserted into
1127 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001128 TIntermSequence &sequence = aggrNode->getSequence() ;
1129 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1130 if (!(*p)->getAsTyped()->getAsConstantUnion())
1131 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135}
1136
Jamie Madilla5efff92013-06-06 11:56:47 -04001137TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001138{
1139 TPublicType returnType = typeSpecifier;
1140 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001141 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001142
1143 if (typeSpecifier.array)
1144 {
1145 error(typeSpecifier.line, "not supported", "first-class array");
1146 recover();
1147 returnType.setArray(false);
1148 }
1149
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001150 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001151 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001152 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1153 {
1154 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1155 recover();
1156 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001157
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001158 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1159 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1160 {
1161 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1162 recover();
1163 }
1164 }
1165 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001166 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001167 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001168 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001169 case EvqVertexInput:
1170 case EvqFragmentOutput:
1171 case EvqVaryingIn:
1172 case EvqVaryingOut:
1173 if (typeSpecifier.type == EbtBool)
1174 {
1175 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1176 recover();
1177 }
1178 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001179
Jamie Madillb120eac2013-06-12 14:08:13 -04001180 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001181 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001182 }
1183
1184 return returnType;
1185}
1186
Jamie Madill075edd82013-07-08 13:30:19 -04001187TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001188{
1189 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1190 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1191
1192 if (identifier != "")
1193 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001194 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001195 recover();
1196
1197 // this error check can mutate the type
1198 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1199 recover();
1200
1201 TVariable* variable = 0;
1202
1203 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1204 recover();
1205
1206 if (variable && symbol)
1207 {
1208 symbol->setId(variable->getUniqueId());
1209 }
1210 }
1211
1212 return aggregate;
1213}
1214
Jamie Madill075edd82013-07-08 13:30:19 -04001215TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001216{
Jamie Madill51a53c72013-06-19 09:24:43 -04001217 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001218 recover();
1219
1220 // this error check can mutate the type
1221 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1222 recover();
1223
1224 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1225 {
1226 recover();
1227 }
1228
1229 TPublicType arrayType = publicType;
1230
1231 int size;
1232 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1233 {
1234 recover();
1235 }
1236 else
1237 {
1238 arrayType.setArray(true, size);
1239 }
1240
1241 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1242 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1243 TVariable* variable = 0;
1244
1245 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1246 recover();
1247
1248 if (variable && symbol)
1249 {
1250 symbol->setId(variable->getUniqueId());
1251 }
1252
1253 return aggregate;
1254}
1255
Jamie Madill075edd82013-07-08 13:30:19 -04001256TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001257{
Jamie Madilla5efff92013-06-06 11:56:47 -04001258 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001259 recover();
1260
1261 TIntermNode* intermNode;
1262 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1263 {
1264 //
1265 // Build intermediate representation
1266 //
1267 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1268 }
1269 else
1270 {
1271 recover();
1272 return NULL;
1273 }
1274}
1275
Jamie Madill075edd82013-07-08 13:30:19 -04001276TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001277{
1278 if (publicType.type == EbtInvariant && !identifierSymbol)
1279 {
1280 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1281 recover();
1282 }
1283
1284 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1285 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1286
1287 if (structQualifierErrorCheck(identifierLocation, publicType))
1288 recover();
1289
Jamie Madill0bd18df2013-06-20 11:55:52 -04001290 if (locationDeclaratorListCheck(identifierLocation, publicType))
1291 recover();
1292
Jamie Madill502d66f2013-06-20 11:55:52 -04001293 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1294 recover();
1295
1296 TVariable* variable = 0;
1297 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1298 recover();
1299 if (symbol && variable)
1300 symbol->setId(variable->getUniqueId());
1301
1302 return intermAggregate;
1303}
1304
Jamie Madill075edd82013-07-08 13:30:19 -04001305TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001306{
1307 if (structQualifierErrorCheck(identifierLocation, publicType))
1308 recover();
1309
Jamie Madill0bd18df2013-06-20 11:55:52 -04001310 if (locationDeclaratorListCheck(identifierLocation, publicType))
1311 recover();
1312
Jamie Madill502d66f2013-06-20 11:55:52 -04001313 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1314 recover();
1315
1316 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1317 {
1318 recover();
1319 }
1320 else if (indexExpression)
1321 {
1322 int size;
1323 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1324 recover();
1325 TPublicType arrayType(publicType);
1326 arrayType.setArray(true, size);
1327 TVariable* variable = NULL;
1328 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1329 recover();
1330 TType type = TType(arrayType);
1331 type.setArraySize(size);
1332
1333 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1334 }
1335 else
1336 {
1337 TPublicType arrayType(publicType);
1338 arrayType.setArray(true);
1339 TVariable* variable = NULL;
1340 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1341 recover();
1342 }
1343
1344 return NULL;
1345}
1346
Jamie Madill075edd82013-07-08 13:30:19 -04001347TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001348{
1349 if (structQualifierErrorCheck(identifierLocation, publicType))
1350 recover();
1351
Jamie Madill0bd18df2013-06-20 11:55:52 -04001352 if (locationDeclaratorListCheck(identifierLocation, publicType))
1353 recover();
1354
Jamie Madill502d66f2013-06-20 11:55:52 -04001355 TIntermNode* intermNode;
1356 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1357 {
1358 //
1359 // build the intermediate representation
1360 //
1361 if (intermNode)
1362 {
1363 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1364 }
1365 else
1366 {
1367 return declaratorList;
1368 }
1369 }
1370 else
1371 {
1372 recover();
1373 return NULL;
1374 }
1375}
1376
Jamie Madilla295edf2013-06-06 11:56:48 -04001377void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1378{
1379 if (typeQualifier.qualifier != EvqUniform)
1380 {
1381 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1382 recover();
1383 return;
1384 }
1385
1386 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1387 ASSERT(!layoutQualifier.isEmpty());
1388
1389 if (shaderVersion < 300)
1390 {
1391 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1392 recover();
1393 return;
1394 }
1395
1396 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1397 {
1398 recover();
1399 return;
1400 }
1401
Jamie Madill099c0f32013-06-20 11:55:52 -04001402 if (layoutQualifier.matrixPacking != EmpUnspecified)
1403 {
1404 defaultMatrixPacking = layoutQualifier.matrixPacking;
1405 }
1406
Jamie Madill1566ef72013-06-20 11:55:54 -04001407 if (layoutQualifier.blockStorage != EmpUnspecified)
1408 {
1409 defaultBlockStorage = layoutQualifier.blockStorage;
1410 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001411}
1412
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001413TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1414{
1415 TOperator op = EOpNull;
1416 if (publicType.userDef)
1417 {
1418 op = EOpConstructStruct;
1419 }
1420 else
1421 {
1422 switch (publicType.type)
1423 {
1424 case EbtFloat:
1425 if (publicType.isMatrix())
1426 {
1427 // TODO: non-square matrices
1428 switch(publicType.getCols())
1429 {
Jamie Madill28b97422013-07-08 14:01:38 -04001430 case 2: op = EOpConstructMat2; break;
1431 case 3: op = EOpConstructMat3; break;
1432 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001433 }
1434 }
1435 else
1436 {
1437 switch(publicType.getNominalSize())
1438 {
Jamie Madill28b97422013-07-08 14:01:38 -04001439 case 1: op = EOpConstructFloat; break;
1440 case 2: op = EOpConstructVec2; break;
1441 case 3: op = EOpConstructVec3; break;
1442 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001443 }
1444 }
1445 break;
1446
1447 case EbtInt:
1448 switch(publicType.getNominalSize())
1449 {
Jamie Madill28b97422013-07-08 14:01:38 -04001450 case 1: op = EOpConstructInt; break;
1451 case 2: op = EOpConstructIVec2; break;
1452 case 3: op = EOpConstructIVec3; break;
1453 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001454 }
1455 break;
1456
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001457 case EbtUInt:
1458 switch(publicType.getNominalSize())
1459 {
Jamie Madill28b97422013-07-08 14:01:38 -04001460 case 1: op = EOpConstructUInt; break;
1461 case 2: op = EOpConstructUVec2; break;
1462 case 3: op = EOpConstructUVec3; break;
1463 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001464 }
1465 break;
1466
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001467 case EbtBool:
1468 switch(publicType.getNominalSize())
1469 {
Jamie Madill28b97422013-07-08 14:01:38 -04001470 case 1: op = EOpConstructBool; break;
1471 case 2: op = EOpConstructBVec2; break;
1472 case 3: op = EOpConstructBVec3; break;
1473 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001474 }
1475 break;
1476
1477 default: break;
1478 }
1479
1480 if (op == EOpNull)
1481 {
1482 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1483 recover();
1484 publicType.type = EbtFloat;
1485 op = EOpConstructFloat;
1486 }
1487 }
1488
1489 TString tempString;
1490 TType type(publicType);
1491 return new TFunction(&tempString, type, op);
1492}
1493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494// This function is used to test for the correctness of the parameters passed to various constructor functions
1495// and also convert them to the right datatype if it is allowed and required.
1496//
1497// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1498//
Jamie Madill075edd82013-07-08 13:30:19 -04001499TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001501 if (node == 0)
1502 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001504 TIntermAggregate* aggrNode = node->getAsAggregate();
1505
Jamie Madill98493dd2013-07-08 14:39:03 -04001506 TFieldList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001507 if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001508 memberTypes = type->getStruct()->fields().begin();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001509
1510 TType elementType = *type;
1511 if (type->isArray())
1512 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001514 bool singleArg;
1515 if (aggrNode) {
1516 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1517 singleArg = true;
1518 else
1519 singleArg = false;
1520 } else
1521 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001523 TIntermTyped *newNode;
1524 if (singleArg) {
1525 // If structure constructor or array constructor is being called
1526 // for only one parameter inside the structure, we need to call constructStruct function once.
1527 if (type->isArray())
1528 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1529 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001530 newNode = constructStruct(node, (*memberTypes)->type(), 1, node->getLine(), false);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001531 else
1532 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001534 if (newNode && newNode->getAsAggregate()) {
1535 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1536 if (constConstructor)
1537 return constConstructor;
1538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001540 return newNode;
1541 }
1542
1543 //
1544 // Handle list of arguments.
1545 //
1546 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1547 // if the structure constructor contains more than one parameter, then construct
1548 // each parameter
1549
1550 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1551
1552 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1553 // to the right type if possible (and allowed).
1554 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1555
1556 for (TIntermSequence::iterator p = sequenceVector.begin();
1557 p != sequenceVector.end(); p++, paramCount++) {
1558 if (type->isArray())
1559 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1560 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001561 newNode = constructStruct(*p, (memberTypes[paramCount])->type(), paramCount+1, node->getLine(), true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001562 else
1563 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1564
1565 if (newNode) {
1566 *p = newNode;
1567 }
1568 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001570 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1571 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1572 if (constConstructor)
1573 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001575 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576}
1577
1578TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1579{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001580 bool canBeFolded = areAllChildConst(aggrNode);
1581 aggrNode->setType(type);
1582 if (canBeFolded) {
1583 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001584 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001585 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001586 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001587 }
1588 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001589 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001590 }
1591 if (returnVal)
1592 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001594 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001597 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598}
1599
1600// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1601// for the parameter to the constructor (passed to this function). Essentially, it converts
1602// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1603// float, then float is converted to int.
1604//
1605// Returns 0 for an error or the constructed node.
1606//
Jamie Madill075edd82013-07-08 13:30:19 -04001607TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001609 TIntermTyped* newNode;
1610 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001612 //
1613 // First, convert types as needed.
1614 //
1615 switch (op) {
1616 case EOpConstructVec2:
1617 case EOpConstructVec3:
1618 case EOpConstructVec4:
1619 case EOpConstructMat2:
1620 case EOpConstructMat3:
1621 case EOpConstructMat4:
1622 case EOpConstructFloat:
1623 basicOp = EOpConstructFloat;
1624 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 case EOpConstructIVec2:
1627 case EOpConstructIVec3:
1628 case EOpConstructIVec4:
1629 case EOpConstructInt:
1630 basicOp = EOpConstructInt;
1631 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001633 case EOpConstructUVec2:
1634 case EOpConstructUVec3:
1635 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001636 case EOpConstructUInt:
1637 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001638 break;
1639
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001640 case EOpConstructBVec2:
1641 case EOpConstructBVec3:
1642 case EOpConstructBVec4:
1643 case EOpConstructBool:
1644 basicOp = EOpConstructBool;
1645 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001647 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001648 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001649 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001651 return 0;
1652 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001653 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001654 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001655 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001656 return 0;
1657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001659 //
1660 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1661 //
1662
1663 // Otherwise, skip out early.
1664 if (subset || (newNode != node && newNode->getType() == *type))
1665 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001667 // setAggregateOperator will insert a new node for the constructor, as needed.
1668 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669}
1670
1671// This function tests for the type of the parameters to the structures constructors. Raises
1672// an error message if the expected type does not match the parameter passed to the constructor.
1673//
1674// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1675//
Jamie Madill075edd82013-07-08 13:30:19 -04001676TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 if (*type == node->getAsTyped()->getType()) {
1679 if (subset)
1680 return node->getAsTyped();
1681 else
1682 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1683 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001684 std::stringstream extraInfoStream;
1685 extraInfoStream << "cannot convert parameter " << paramCount
1686 << " from '" << node->getAsTyped()->getType().getBasicString()
1687 << "' to '" << type->getBasicString() << "'";
1688 std::string extraInfo = extraInfoStream.str();
1689 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001690 recover();
1691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694}
1695
1696//
1697// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1698// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1699// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1700// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1701// a constant matrix.
1702//
Jamie Madill075edd82013-07-08 13:30:19 -04001703TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001705 TIntermTyped* typedNode;
1706 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001708 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001709 if (tempConstantNode) {
1710 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001712 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001713 return node;
1714 }
1715 } 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 +00001716 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001719 return 0;
1720 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001722 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001725 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001726 std::stringstream extraInfoStream;
1727 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1728 std::string extraInfo = extraInfoStream.str();
1729 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001730 recover();
1731 fields.offsets[i] = 0;
1732 }
1733
1734 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 }
1737 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1738 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739}
1740
1741//
1742// This function returns the column being accessed from a constant matrix. The values are retrieved from
1743// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1744// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1745// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1746//
Jamie Madill075edd82013-07-08 13:30:19 -04001747TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001749 TIntermTyped* typedNode;
1750 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001752 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001753 std::stringstream extraInfoStream;
1754 extraInfoStream << "matrix field selection out of range '" << index << "'";
1755 std::string extraInfo = extraInfoStream.str();
1756 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001757 recover();
1758 index = 0;
1759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001761 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001762 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001763 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1765 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001766 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001769 return 0;
1770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001772 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773}
1774
1775
1776//
1777// This function returns an element of an array accessed from a constant array. The values are retrieved from
1778// the symbol table and parse-tree is built for the type of the element. The input
1779// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1780// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1781//
Jamie Madill075edd82013-07-08 13:30:19 -04001782TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 TIntermTyped* typedNode;
1785 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1786 TType arrayElementType = node->getType();
1787 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001789 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001790 std::stringstream extraInfoStream;
1791 extraInfoStream << "array field selection out of range '" << index << "'";
1792 std::string extraInfo = extraInfoStream.str();
1793 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001794 recover();
1795 index = 0;
1796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001798 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001799 size_t arrayElementSize = arrayElementType.getObjectSize();
1800 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1801 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001803 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 return 0;
1807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001809 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810}
1811
1812
1813//
1814// This function returns the value of a particular field inside a constant structure from the symbol table.
1815// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1816// function and returns the parse-tree with the values of the embedded/nested struct.
1817//
Jamie Madill075edd82013-07-08 13:30:19 -04001818TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819{
Jamie Madill98493dd2013-07-08 14:39:03 -04001820 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001821 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822
Jamie Madill98493dd2013-07-08 14:39:03 -04001823 for (size_t index = 0; index < fields.size(); ++index) {
1824 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 break;
1826 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001827 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001828 }
1829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830
Jamie Madill94bf7f22013-07-08 13:31:15 -04001831 TIntermTyped *typedNode;
1832 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001833 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001834 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001836 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1837 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001838 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 return 0;
1842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845}
1846
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001847//
1848// Interface/uniform blocks
1849//
Jamie Madill98493dd2013-07-08 14:39:03 -04001850TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1851 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001852{
1853 if (reservedErrorCheck(nameLine, blockName))
1854 recover();
1855
1856 if (typeQualifier.qualifier != EvqUniform)
1857 {
1858 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1859 recover();
1860 }
1861
Jamie Madill099c0f32013-06-20 11:55:52 -04001862 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1863 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001864 {
1865 recover();
1866 }
1867
Jamie Madill099c0f32013-06-20 11:55:52 -04001868 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1869 {
1870 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1871 }
1872
Jamie Madill1566ef72013-06-20 11:55:54 -04001873 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1874 {
1875 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1876 }
1877
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001878 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1879 if (!symbolTable.declare(*blockNameSymbol)) {
1880 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1881 recover();
1882 }
1883
Jamie Madill98493dd2013-07-08 14:39:03 -04001884 // check for sampler types and apply layout qualifiers
1885 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1886 TField* field = (*fieldList)[memberIndex];
1887 TType* fieldType = field->type();
1888 if (IsSampler(fieldType->getBasicType())) {
1889 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001890 recover();
1891 }
1892
Jamie Madill98493dd2013-07-08 14:39:03 -04001893 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001894 switch (qualifier)
1895 {
1896 case EvqGlobal:
1897 case EvqUniform:
1898 break;
1899 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001900 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001901 recover();
1902 break;
1903 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001904
1905 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001906 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1907 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001908 {
1909 recover();
1910 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001911
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001913 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001914 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001915 recover();
1916 }
1917
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001920 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001921 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001923 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001924 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001925 recover();
1926 }
1927
Jamie Madill98493dd2013-07-08 14:39:03 -04001928 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001929 }
1930
Jamie Madill98493dd2013-07-08 14:39:03 -04001931 // add array index
1932 int arraySize = 0;
1933 if (arrayIndex != NULL)
1934 {
1935 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1936 recover();
1937 }
1938
1939 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1940 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001941
1942 TString symbolName = "";
1943 int symbolId = 0;
1944
Jamie Madill98493dd2013-07-08 14:39:03 -04001945 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001946 {
1947 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001948 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1949 {
1950 TField* field = (*fieldList)[memberIndex];
1951 TType* fieldType = field->type();
1952
1953 // set parent pointer of the field variable
1954 fieldType->setInterfaceBlock(interfaceBlock);
1955
1956 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1957 fieldVariable->setQualifier(typeQualifier.qualifier);
1958
1959 if (!symbolTable.declare(*fieldVariable)) {
1960 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001961 recover();
1962 }
1963 }
1964 }
1965 else
1966 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001967 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001968 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001969 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001970
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001971 if (!symbolTable.declare(*instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001972 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001973 recover();
1974 }
1975
1976 symbolId = instanceTypeDef->getUniqueId();
1977 symbolName = instanceTypeDef->getName();
1978 }
1979
Jamie Madill98493dd2013-07-08 14:39:03 -04001980 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001981 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001982
1983 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001984 return aggregate;
1985}
1986
Jamie Madill075edd82013-07-08 13:30:19 -04001987bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001988{
1989 ++structNestingLevel;
1990
1991 // Embedded structure definitions are not supported per GLSL ES spec.
1992 // They aren't allowed in GLSL either, but we need to detect this here
1993 // so we don't rely on the GLSL compiler to catch it.
1994 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001995 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001996 return true;
1997 }
1998
1999 return false;
2000}
2001
2002void TParseContext::exitStructDeclaration()
2003{
2004 --structNestingLevel;
2005}
2006
2007namespace {
2008
2009const int kWebGLMaxStructNesting = 4;
2010
2011} // namespace
2012
Jamie Madill98493dd2013-07-08 14:39:03 -04002013bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002015 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002016 return false;
2017 }
2018
Jamie Madill98493dd2013-07-08 14:39:03 -04002019 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002020 return false;
2021 }
2022
2023 // We're already inside a structure definition at this point, so add
2024 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002025 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002026 std::stringstream extraInfoStream;
Jamie Madill98493dd2013-07-08 14:39:03 -04002027 extraInfoStream << "Reference of struct type " << field.type()->getStruct()->name()
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002028 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2029 std::string extraInfo = extraInfoStream.str();
2030 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002031 return true;
2032 }
2033
2034 return false;
2035}
2036
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002037//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002038// Parse an array index expression
2039//
Jamie Madill075edd82013-07-08 13:30:19 -04002040TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002041{
2042 TIntermTyped *indexedExpression = NULL;
2043
2044 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2045 {
2046 if (baseExpression->getAsSymbolNode())
2047 {
2048 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2049 }
2050 else
2051 {
2052 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2053 }
2054 recover();
2055 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002056
Jamie Madill7164cf42013-07-08 13:30:59 -04002057 if (indexExpression->getQualifier() == EvqConst)
2058 {
2059 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2060 if (index < 0)
2061 {
2062 std::stringstream infoStream;
2063 infoStream << index;
2064 std::string info = infoStream.str();
2065 error(location, "negative index", info.c_str());
2066 recover();
2067 index = 0;
2068 }
2069 if (baseExpression->getType().getQualifier() == EvqConst)
2070 {
2071 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002072 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002073 // constant folding for arrays
2074 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002075 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002076 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002077 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002078 // constant folding for vectors
2079 TVectorFields fields;
2080 fields.num = 1;
2081 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2082 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2083 }
2084 else if (baseExpression->isMatrix())
2085 {
2086 // constant folding for matrices
2087 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002088 }
2089 }
2090 else
2091 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002092 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002093 {
Jamie Madill18464b52013-07-08 14:01:55 -04002094 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002095 {
2096 std::stringstream extraInfoStream;
2097 extraInfoStream << "array index out of range '" << index << "'";
2098 std::string extraInfo = extraInfoStream.str();
2099 error(location, "", "[", extraInfo.c_str());
2100 recover();
2101 index = baseExpression->getType().getArraySize() - 1;
2102 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002103 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002104 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002105 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002106 std::stringstream extraInfoStream;
2107 extraInfoStream << "field selection out of range '" << index << "'";
2108 std::string extraInfo = extraInfoStream.str();
2109 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002110 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002111 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002112 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002113
Jamie Madill7164cf42013-07-08 13:30:59 -04002114 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2115 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002116 }
2117 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002118 else
2119 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002120 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002121 {
2122 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2123 recover();
2124 }
2125 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2126 {
2127 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2128 recover();
2129 }
2130
2131 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2132 }
2133
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002134 if (indexedExpression == 0)
2135 {
2136 ConstantUnion *unionArray = new ConstantUnion[1];
2137 unionArray->setFConst(0.0f);
2138 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2139 }
2140 else if (baseExpression->isArray())
2141 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002142 const TType &baseType = baseExpression->getType();
2143 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002144 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002145 TType copyOfType(baseType.getStruct());
2146 indexedExpression->setType(copyOfType);
2147 }
2148 else if (baseType.isInterfaceBlock())
2149 {
2150 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002151 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002152 }
2153 else
2154 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002155 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002156 }
2157
2158 if (baseExpression->getType().getQualifier() == EvqConst)
2159 {
2160 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2161 }
2162 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002163 else if (baseExpression->isMatrix())
2164 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002165 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2166 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002167 }
2168 else if (baseExpression->isVector())
2169 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002170 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2171 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002172 }
2173 else
2174 {
2175 indexedExpression->setType(baseExpression->getType());
2176 }
2177
2178 return indexedExpression;
2179}
2180
Jamie Madill075edd82013-07-08 13:30:19 -04002181TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002182{
2183 TIntermTyped *indexedExpression = NULL;
2184
2185 if (baseExpression->isArray())
2186 {
2187 error(fieldLocation, "cannot apply dot operator to an array", ".");
2188 recover();
2189 }
2190
2191 if (baseExpression->isVector())
2192 {
2193 TVectorFields fields;
2194 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2195 {
2196 fields.num = 1;
2197 fields.offsets[0] = 0;
2198 recover();
2199 }
2200
2201 if (baseExpression->getType().getQualifier() == EvqConst)
2202 {
2203 // constant folding for vector fields
2204 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2205 if (indexedExpression == 0)
2206 {
2207 recover();
2208 indexedExpression = baseExpression;
2209 }
2210 else
2211 {
2212 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2213 }
2214 }
2215 else
2216 {
2217 TString vectorString = fieldString;
2218 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2219 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2220 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2221 }
2222 }
2223 else if (baseExpression->isMatrix())
2224 {
2225 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002226 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002227 {
2228 fields.wholeRow = false;
2229 fields.wholeCol = false;
2230 fields.row = 0;
2231 fields.col = 0;
2232 recover();
2233 }
2234
2235 if (fields.wholeRow || fields.wholeCol)
2236 {
2237 error(dotLocation, " non-scalar fields not implemented yet", ".");
2238 recover();
2239 ConstantUnion *unionArray = new ConstantUnion[1];
2240 unionArray->setIConst(0);
2241 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2242 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002243 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002244 }
2245 else
2246 {
2247 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002248 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002249 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2250 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2251 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2252 }
2253 }
2254 else if (baseExpression->getBasicType() == EbtStruct)
2255 {
2256 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002257 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2258 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002259 {
2260 error(dotLocation, "structure has no fields", "Internal Error");
2261 recover();
2262 indexedExpression = baseExpression;
2263 }
2264 else
2265 {
2266 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002267 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002268 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002269 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002270 {
2271 fieldFound = true;
2272 break;
2273 }
2274 }
2275 if (fieldFound)
2276 {
2277 if (baseExpression->getType().getQualifier() == EvqConst)
2278 {
2279 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2280 if (indexedExpression == 0)
2281 {
2282 recover();
2283 indexedExpression = baseExpression;
2284 }
2285 else
2286 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002287 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002288 // change the qualifier of the return type, not of the structure field
2289 // as the structure definition is shared between various structures.
2290 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2291 }
2292 }
2293 else
2294 {
2295 ConstantUnion *unionArray = new ConstantUnion[1];
2296 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002297 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002298 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002299 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002300 }
2301 }
2302 else
2303 {
2304 error(dotLocation, " no such field in structure", fieldString.c_str());
2305 recover();
2306 indexedExpression = baseExpression;
2307 }
2308 }
2309 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002310 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002311 {
2312 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002313 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2314 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002315 {
2316 error(dotLocation, "interface block has no fields", "Internal Error");
2317 recover();
2318 indexedExpression = baseExpression;
2319 }
2320 else
2321 {
2322 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002323 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002324 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002325 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002326 {
2327 fieldFound = true;
2328 break;
2329 }
2330 }
2331 if (fieldFound)
2332 {
2333 ConstantUnion *unionArray = new ConstantUnion[1];
2334 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002335 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002336 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002337 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002338 }
2339 else
2340 {
2341 error(dotLocation, " no such field in interface block", fieldString.c_str());
2342 recover();
2343 indexedExpression = baseExpression;
2344 }
2345 }
2346 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002347 else
2348 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002349 if (shaderVersion < 300)
2350 {
2351 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2352 }
2353 else
2354 {
2355 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2356 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002357 recover();
2358 indexedExpression = baseExpression;
2359 }
2360
2361 return indexedExpression;
2362}
2363
Jamie Madill075edd82013-07-08 13:30:19 -04002364TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002365{
Jamie Madilla5efff92013-06-06 11:56:47 -04002366 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002367
Jamie Madilla5efff92013-06-06 11:56:47 -04002368 qualifier.location = -1;
2369 qualifier.matrixPacking = EmpUnspecified;
2370 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002371
2372 if (qualifierType == "shared")
2373 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002374 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002375 }
2376 else if (qualifierType == "packed")
2377 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002378 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002379 }
2380 else if (qualifierType == "std140")
2381 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002382 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002383 }
2384 else if (qualifierType == "row_major")
2385 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002386 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002387 }
2388 else if (qualifierType == "column_major")
2389 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002390 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002391 }
2392 else if (qualifierType == "location")
2393 {
2394 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2395 recover();
2396 }
2397 else
2398 {
2399 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2400 recover();
2401 }
2402
Jamie Madilla5efff92013-06-06 11:56:47 -04002403 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002404}
2405
Jamie Madill075edd82013-07-08 13:30:19 -04002406TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002407{
Jamie Madilla5efff92013-06-06 11:56:47 -04002408 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002409
Jamie Madilla5efff92013-06-06 11:56:47 -04002410 qualifier.location = -1;
2411 qualifier.matrixPacking = EmpUnspecified;
2412 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002413
2414 if (qualifierType != "location")
2415 {
2416 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2417 recover();
2418 }
2419 else
2420 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002421 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002422 if (intValue < 0)
2423 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002424 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002425 recover();
2426 }
2427 else
2428 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002429 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002430 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002431 }
2432
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002434}
2435
Jamie Madilla5efff92013-06-06 11:56:47 -04002436TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002437{
Jamie Madilla5efff92013-06-06 11:56:47 -04002438 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002439
Jamie Madilla5efff92013-06-06 11:56:47 -04002440 if (rightQualifier.location != -1)
2441 {
2442 joinedQualifier.location = rightQualifier.location;
2443 }
2444 if (rightQualifier.matrixPacking != EmpUnspecified)
2445 {
2446 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2447 }
2448 if (rightQualifier.blockStorage != EbsUnspecified)
2449 {
2450 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2451 }
2452
2453 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002454}
2455
Jamie Madill98493dd2013-07-08 14:39:03 -04002456TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002457{
Jamie Madill98493dd2013-07-08 14:39:03 -04002458 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002459 recover();
2460 }
2461
Jamie Madill98493dd2013-07-08 14:39:03 -04002462 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002463 //
2464 // Careful not to replace already known aspects of type, like array-ness
2465 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002466 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002467 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002468 type->setPrimarySize(typeSpecifier.primarySize);
2469 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002470 type->setPrecision(typeSpecifier.precision);
2471 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002472 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002473
2474 // don't allow arrays of arrays
2475 if (type->isArray()) {
2476 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2477 recover();
2478 }
2479 if (typeSpecifier.array)
2480 type->setArraySize(typeSpecifier.arraySize);
2481 if (typeSpecifier.userDef) {
2482 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002483 }
2484
Jamie Madill98493dd2013-07-08 14:39:03 -04002485 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002486 recover();
2487 }
2488 }
2489
Jamie Madill98493dd2013-07-08 14:39:03 -04002490 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002491}
2492
Jamie Madill98493dd2013-07-08 14:39:03 -04002493TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002494{
Jamie Madill98493dd2013-07-08 14:39:03 -04002495 TStructure* structure = new TStructure(structName, fieldList);
2496 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002497
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002499 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002500 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002501 {
2502 recover();
2503 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002504 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002505 if (!symbolTable.declare(*userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002506 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002507 recover();
2508 }
2509 }
2510
2511 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002512 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002513 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002514 const TField &field = *(*fieldList)[typeListIndex];
2515 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002516 switch (qualifier)
2517 {
2518 case EvqGlobal:
2519 case EvqTemporary:
2520 break;
2521 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002522 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 recover();
2524 break;
2525 }
2526 }
2527
2528 TPublicType publicType;
2529 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002530 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002531 exitStructDeclaration();
2532
2533 return publicType;
2534}
2535
2536//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002537// Parse an array of strings using yyparse.
2538//
2539// Returns 0 for success.
2540//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002541int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002542 TParseContext* context) {
2543 if ((count == 0) || (string == NULL))
2544 return 1;
2545
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002546 if (glslang_initialize(context))
2547 return 1;
2548
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002549 int error = glslang_scan(count, string, length, context);
2550 if (!error)
2551 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002552
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002553 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002554
alokp@chromium.org6b495712012-06-29 00:06:58 +00002555 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002556}
2557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002559