blob: 014a9b213b423f20b951042a3d8419ff033a944c [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 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
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
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
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020013#include "compiler/translator/glslang.h"
14#include "compiler/translator/ValidateSwitch.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
alokp@chromium.org8b851c62012-06-15 16:25:11 +000016///////////////////////////////////////////////////////////////////////
17//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018// Sub- vector and matrix fields
19//
20////////////////////////////////////////////////////////////////////////
21
22//
23// Look at a '.' field selector string and change it into offsets
24// for a vector.
25//
Jamie Madill075edd82013-07-08 13:30:19 -040026bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000028 fields.num = (int) compString.size();
29 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000030 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000031 return false;
32 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000034 enum {
35 exyzw,
36 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000037 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000040 for (int i = 0; i < fields.num; ++i) {
41 switch (compString[i]) {
42 case 'x':
43 fields.offsets[i] = 0;
44 fieldSet[i] = exyzw;
45 break;
46 case 'r':
47 fields.offsets[i] = 0;
48 fieldSet[i] = ergba;
49 break;
50 case 's':
51 fields.offsets[i] = 0;
52 fieldSet[i] = estpq;
53 break;
54 case 'y':
55 fields.offsets[i] = 1;
56 fieldSet[i] = exyzw;
57 break;
58 case 'g':
59 fields.offsets[i] = 1;
60 fieldSet[i] = ergba;
61 break;
62 case 't':
63 fields.offsets[i] = 1;
64 fieldSet[i] = estpq;
65 break;
66 case 'z':
67 fields.offsets[i] = 2;
68 fieldSet[i] = exyzw;
69 break;
70 case 'b':
71 fields.offsets[i] = 2;
72 fieldSet[i] = ergba;
73 break;
74 case 'p':
75 fields.offsets[i] = 2;
76 fieldSet[i] = estpq;
77 break;
78
79 case 'w':
80 fields.offsets[i] = 3;
81 fieldSet[i] = exyzw;
82 break;
83 case 'a':
84 fields.offsets[i] = 3;
85 fieldSet[i] = ergba;
86 break;
87 case 'q':
88 fields.offsets[i] = 3;
89 fieldSet[i] = estpq;
90 break;
91 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000092 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000093 return false;
94 }
95 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000097 for (int i = 0; i < fields.num; ++i) {
98 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000099 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000100 return false;
101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000103 if (i > 0) {
104 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000105 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000106 return false;
107 }
108 }
109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000111 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112}
113
114
115//
116// Look at a '.' field selector string and change it into offsets
117// for a matrix.
118//
Jamie Madill075edd82013-07-08 13:30:19 -0400119bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 fields.wholeRow = false;
122 fields.wholeCol = false;
123 fields.row = -1;
124 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000127 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 return false;
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000131 if (compString[0] == '_') {
132 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000133 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000134 return false;
135 }
136 fields.wholeCol = true;
137 fields.col = compString[1] - '0';
138 } else if (compString[1] == '_') {
139 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000140 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000141 return false;
142 }
143 fields.wholeRow = true;
144 fields.row = compString[0] - '0';
145 } else {
146 if (compString[0] < '0' || compString[0] > '3' ||
147 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000148 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000149 return false;
150 }
151 fields.row = compString[0] - '0';
152 fields.col = compString[1] - '0';
153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000155 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000156 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000157 return false;
158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000160 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161}
162
163///////////////////////////////////////////////////////////////////////
164//
165// Errors
166//
167////////////////////////////////////////////////////////////////////////
168
169//
170// Track whether errors have occurred.
171//
172void TParseContext::recover()
173{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176//
177// Used by flex/bison to output all syntax and parsing errors.
178//
Jamie Madill075edd82013-07-08 13:30:19 -0400179void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000180 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000181 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000183 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400184 srcLoc.file = loc.first_file;
185 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500186 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000187 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
Jamie Madill075edd82013-07-08 13:30:19 -0400191void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000192 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000193 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000194 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400195 srcLoc.file = loc.first_file;
196 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500197 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000198 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000199}
200
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000201void TParseContext::trace(const char* str)
202{
203 diagnostics.writeDebug(str);
204}
205
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206//
207// Same error message for all places assignments don't work.
208//
Jamie Madill075edd82013-07-08 13:30:19 -0400209void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000211 std::stringstream extraInfoStream;
212 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
213 std::string extraInfo = extraInfoStream.str();
214 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215}
216
217//
218// Same error message for all places unary operations don't work.
219//
Jamie Madill075edd82013-07-08 13:30:19 -0400220void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000222 std::stringstream extraInfoStream;
223 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
224 << " (or there is no acceptable conversion)";
225 std::string extraInfo = extraInfoStream.str();
226 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227}
228
229//
230// Same error message for all binary operations don't work.
231//
Jamie Madill075edd82013-07-08 13:30:19 -0400232void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000234 std::stringstream extraInfoStream;
235 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
236 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
237 std::string extraInfo = extraInfoStream.str();
238 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239}
240
Jamie Madill075edd82013-07-08 13:30:19 -0400241bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000242 if (!checksPrecisionErrors)
243 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000244 switch( type ){
245 case EbtFloat:
246 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000247 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000248 return true;
249 }
250 break;
251 case EbtInt:
252 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000253 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000254 return true;
255 }
256 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000257 default:
258 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000259 }
260 return false;
261}
262
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263//
264// Both test and if necessary, spit out an error, to see if the node is really
265// an l-value that can be operated on this way.
266//
267// Returns true if the was an error.
268//
Jamie Madill075edd82013-07-08 13:30:19 -0400269bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000271 TIntermSymbol* symNode = node->getAsSymbolNode();
272 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000274 if (binaryNode) {
275 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 switch(binaryNode->getOp()) {
278 case EOpIndexDirect:
279 case EOpIndexIndirect:
280 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000281 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000282 return lValueErrorCheck(line, op, binaryNode->getLeft());
283 case EOpVectorSwizzle:
284 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
285 if (!errorReturn) {
286 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000288 TIntermTyped* rightNode = binaryNode->getRight();
289 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700290
291 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
292 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000293 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700294 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000296 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 return true;
299 }
300 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700304 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000305 break;
306 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000307 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 return true;
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 const char* symbol = 0;
314 if (symNode != 0)
315 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 const char* message = 0;
318 switch (node->getQualifier()) {
319 case EvqConst: message = "can't modify a const"; break;
320 case EvqConstReadOnly: message = "can't modify a const"; break;
321 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700322 case EvqFragmentIn: message = "can't modify an input"; break;
323 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 case EvqUniform: message = "can't modify a uniform"; break;
325 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000326 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
327 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
328 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
329 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 //
332 // Type that can't be written to?
333 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400334 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000335 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400336 }
337 if (IsSampler(node->getBasicType())) {
338 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000339 }
340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000343 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347
348
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000349 //
350 // Everything else is okay, no error.
351 //
352 if (message == 0)
353 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000355 //
356 // If we get here, we have an error and a message.
357 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 if (symNode) {
359 std::stringstream extraInfoStream;
360 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
361 std::string extraInfo = extraInfoStream.str();
362 error(line, " l-value required", op, extraInfo.c_str());
363 }
364 else {
365 std::stringstream extraInfoStream;
366 extraInfoStream << "(" << message << ")";
367 std::string extraInfo = extraInfoStream.str();
368 error(line, " l-value required", op, extraInfo.c_str());
369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000371 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
374//
375// Both test, and if necessary spit out an error, to see if the node is really
376// a constant.
377//
378// Returns true if the was an error.
379//
380bool TParseContext::constErrorCheck(TIntermTyped* node)
381{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000382 if (node->getQualifier() == EvqConst)
383 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000385 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// an integer.
393//
394// Returns true if the was an error.
395//
396bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
397{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000398 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if we are currently
408// globally scoped.
409//
410// Returns true if the was an error.
411//
Jamie Madill075edd82013-07-08 13:30:19 -0400412bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 if (global)
415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// For now, keep it simple: if it starts "gl_", it's reserved, independent
424// of scope. Except, if the symbol table is at the built-in push-level,
425// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000426// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
427// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428//
429// Returns true if there was an error.
430//
Jamie Madill075edd82013-07-08 13:30:19 -0400431bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000433 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000435 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000437 return true;
438 }
Jamie Madill5508f392014-02-20 13:31:36 -0500439 if (IsWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000440 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000441 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442 return true;
443 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000444 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000445 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000446 return true;
447 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000448 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000450 return true;
451 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000452 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000455 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 }
457 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460}
461
462//
463// Make sure there is enough data provided to the constructor to build
464// something of the type of the constructor. Also returns the type of
465// the constructor.
466//
467// Returns true if there was an error in construction.
468//
Jamie Madill075edd82013-07-08 13:30:19 -0400469bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000473 bool constructingMatrix = false;
474 switch(op) {
475 case EOpConstructMat2:
476 case EOpConstructMat3:
477 case EOpConstructMat4:
478 constructingMatrix = true;
479 break;
480 default:
481 break;
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000484 //
485 // Note: It's okay to have too many components available, but not okay to have unused
486 // arguments. 'full' will go to true when enough args have been seen. If we loop
487 // again, there is an extra argument, so 'overfull' will become true.
488 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489
Jamie Madill94bf7f22013-07-08 13:31:15 -0400490 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 bool constType = true;
492 bool full = false;
493 bool overFull = false;
494 bool matrixInMatrix = false;
495 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000496 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000497 const TParameter& param = function.getParam(i);
498 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000499
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000500 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 matrixInMatrix = true;
502 if (full)
503 overFull = true;
504 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
505 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000506 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000508 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 arrayArg = true;
510 }
511
512 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000513 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514
Olli Etuaho376f1b52015-04-13 13:23:41 +0300515 if (type->isArray())
516 {
517 if (type->isUnsizedArray())
518 {
519 type->setArraySize(function.getParamCount());
520 }
521 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
522 {
523 error(line, "array constructor needs one argument per array element", "constructor");
524 return true;
525 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000529 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 return true;
531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000534 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000536 return true;
537 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000541 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 return true;
543 }
544
Brendan Longeaa84062013-12-08 18:26:50 +0100545 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000546 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 return true;
548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000550 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000551 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
552 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000553 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000554 return true;
555 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000558 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000560 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 return true;
562 }
563 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000564 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 return true;
566 }
567 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573}
574
575// This function checks to see if a void variable has been declared and raise an error message for such a case
576//
577// returns true in case of an error
578//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300579bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300581 if (type == EbtVoid)
582 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000583 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300585 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000586
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588}
589
590// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
591//
592// returns true in case of an error
593//
Jamie Madill075edd82013-07-08 13:30:19 -0400594bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000597 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 return true;
599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602}
603
604// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
605//
606// returns true in case of an error
607//
Jamie Madill075edd82013-07-08 13:30:19 -0400608bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000610 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000611 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 return true;
613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616}
617
Jamie Madill075edd82013-07-08 13:30:19 -0400618bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000620 if (pType.type == EbtStruct) {
621 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000622 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623
624 return true;
625 }
626
627 return false;
628 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000629 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 return true;
632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635}
636
Jamie Madill075edd82013-07-08 13:30:19 -0400637bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400638{
639 if (pType.layoutQualifier.location != -1)
640 {
641 error(line, "location must only be specified for a single input or output variable", "location");
642 return true;
643 }
644
645 return false;
646}
647
Jamie Madill075edd82013-07-08 13:30:19 -0400648bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
651 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000652 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 return true;
654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657}
658
659bool TParseContext::containsSampler(TType& type)
660{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 if (IsSampler(type.getBasicType()))
662 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
Jamie Madill98493dd2013-07-08 14:39:03 -0400664 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
665 const TFieldList& fields = type.getStruct()->fields();
666 for (unsigned int i = 0; i < fields.size(); ++i) {
667 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return true;
669 }
670 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673}
674
675//
676// Do size checking for an array type's size.
677//
678// Returns true if there was an error.
679//
Jamie Madill075edd82013-07-08 13:30:19 -0400680bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000683
Olli Etuahoe7847b02015-03-16 11:56:12 +0200684 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000685 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000686 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200687 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 return true;
689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690
Nicolas Capens906744a2014-06-06 15:18:07 -0400691 unsigned int unsignedSize = 0;
692
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000693 if (constant->getBasicType() == EbtUInt)
694 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400695 unsignedSize = constant->getUConst(0);
696 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000697 }
698 else
699 {
700 size = constant->getIConst(0);
701
Nicolas Capens906744a2014-06-06 15:18:07 -0400702 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000703 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400704 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000705 size = 1;
706 return true;
707 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400708
709 unsignedSize = static_cast<unsigned int>(size);
710 }
711
712 if (size == 0)
713 {
714 error(line, "array size must be greater than zero", "");
715 size = 1;
716 return true;
717 }
718
719 // The size of arrays is restricted here to prevent issues further down the
720 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
721 // 4096 registers so this should be reasonable even for aggressively optimizable code.
722 const unsigned int sizeLimit = 65536;
723
724 if (unsignedSize > sizeLimit)
725 {
726 error(line, "array size too large", "");
727 size = 1;
728 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
734//
735// See if this qualifier can be an array.
736//
737// Returns true if there is an error.
738//
Olli Etuaho3739d232015-04-08 12:23:44 +0300739bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740{
Olli Etuaho3739d232015-04-08 12:23:44 +0300741 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
742 (type.qualifier == EvqConst && shaderVersion < 300))
743 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000744 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749}
750
751//
752// See if this type can be an array.
753//
754// Returns true if there is an error.
755//
Jamie Madill075edd82013-07-08 13:30:19 -0400756bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 //
759 // Can the type be an array?
760 //
761 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000762 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000763 return true;
764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000766 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767}
768
769//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770// Enforce non-initializer type/qualifier rules.
771//
772// Returns true if there was an error.
773//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300774bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775{
Olli Etuaho3739d232015-04-08 12:23:44 +0300776 ASSERT(type != nullptr);
777 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000778 {
779 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300780 type->qualifier = EvqTemporary;
781
782 // Generate informative error messages for ESSL1.
783 // In ESSL3 arrays and structures containing arrays can be constant.
784 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000785 {
786 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
787 }
788 else
789 {
790 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
791 }
792
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000793 return true;
794 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300795 if (type->isUnsizedArray())
796 {
797 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
798 return true;
799 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
Olli Etuaho2935c582015-04-08 14:32:06 +0300803// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804// and update the symbol table.
805//
Olli Etuaho2935c582015-04-08 14:32:06 +0300806// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807//
Olli Etuaho2935c582015-04-08 14:32:06 +0300808bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
809 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810{
Olli Etuaho2935c582015-04-08 14:32:06 +0300811 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
Olli Etuaho2935c582015-04-08 14:32:06 +0300813 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
Olli Etuaho2935c582015-04-08 14:32:06 +0300815 // gl_LastFragData may be redeclared with a new precision qualifier
816 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
817 {
818 const TVariable *maxDrawBuffers =
819 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion));
820 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
821 {
822 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion))
823 {
824 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
825 }
826 }
827 else
828 {
829 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
830 return false;
831 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
Olli Etuaho2935c582015-04-08 14:32:06 +0300834 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
835 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
Olli Etuaho2935c582015-04-08 14:32:06 +0300837 (*variable) = new TVariable(&identifier, type);
838 if (!symbolTable.declare(*variable))
839 {
840 error(line, "redefinition", identifier.c_str());
841 delete (*variable);
842 (*variable) = nullptr;
843 return false;
844 }
845
846 if (voidErrorCheck(line, identifier, type.getBasicType()))
847 return false;
848
849 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850}
851
Jamie Madill075edd82013-07-08 13:30:19 -0400852bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000855 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 return true;
857 }
858 if (qualifier == EvqConst && paramQualifier != EvqIn) {
859 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
860 return true;
861 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000864 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000865 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000866 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869}
870
Jamie Madill075edd82013-07-08 13:30:19 -0400871bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000872{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000873 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000874 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
875 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000876 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000877 return true;
878 }
zmo@google.comf5450912011-09-09 01:37:19 +0000879 // In GLSL ES, an extension's default behavior is "disable".
880 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000881 error(line, "extension", extension.c_str(), "is disabled");
882 return true;
883 }
884 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000885 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000886 return false;
887 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
Olli Etuahofa33d582015-04-09 14:33:12 +0300892// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
893// declaration.
894//
895bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400896{
Olli Etuahofa33d582015-04-09 14:33:12 +0300897 switch (publicType.qualifier)
898 {
899 case EvqVaryingIn:
900 case EvqVaryingOut:
901 case EvqAttribute:
902 case EvqVertexIn:
903 case EvqFragmentOut:
904 if (publicType.type == EbtStruct)
905 {
906 error(identifierLocation, "cannot be used with a structure",
907 getQualifierString(publicType.qualifier));
908 return true;
909 }
910
911 default: break;
912 }
913
914 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
915 "samplers must be uniform"))
916 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400917 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300918 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400919
920 // check for layout qualifier issues
921 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
922
923 if (layoutQualifier.matrixPacking != EmpUnspecified)
924 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300925 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
926 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400927 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400928 }
929
930 if (layoutQualifier.blockStorage != EbsUnspecified)
931 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300932 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
933 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400934 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400935 }
936
Olli Etuahofa33d582015-04-09 14:33:12 +0300937 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
938 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400939 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400940 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400941 }
942
943 return false;
944}
945
Jamie Madill075edd82013-07-08 13:30:19 -0400946bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400947{
948 if (layoutQualifier.location != -1)
949 {
950 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
951 return true;
952 }
953
954 return false;
955}
956
Olli Etuahob6e07a62015-02-16 12:22:10 +0200957bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
958{
959 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
960 {
961 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
962 if (qual == EvqOut || qual == EvqInOut)
963 {
964 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
965 if (lValueErrorCheck(node->getLine(), "assign", node))
966 {
967 error(node->getLine(),
968 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
969 recover();
970 return true;
971 }
972 }
973 }
974 return false;
975}
976
zmo@google.com09c323a2011-08-12 18:22:25 +0000977bool TParseContext::supportsExtension(const char* extension)
978{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000979 const TExtensionBehavior& extbehavior = extensionBehavior();
980 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
981 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000982}
983
Jamie Madill5d287f52013-07-12 15:38:19 -0400984bool TParseContext::isExtensionEnabled(const char* extension) const
985{
986 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400987 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400988
989 if (iter == extbehavior.end())
990 {
991 return false;
992 }
993
994 return (iter->second == EBhEnable || iter->second == EBhRequire);
995}
996
Jamie Madill075edd82013-07-08 13:30:19 -0400997void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
998{
999 pp::SourceLocation srcLoc;
1000 srcLoc.file = loc.first_file;
1001 srcLoc.line = loc.first_line;
1002 directiveHandler.handleExtension(srcLoc, extName, behavior);
1003}
1004
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001005void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001006{
1007 pp::SourceLocation srcLoc;
1008 srcLoc.file = loc.first_file;
1009 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001010 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001011}
1012
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013/////////////////////////////////////////////////////////////////////////////////
1014//
1015// Non-Errors.
1016//
1017/////////////////////////////////////////////////////////////////////////////////
1018
Jamie Madill5c097022014-08-20 16:38:32 -04001019const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1020 const TString *name,
1021 const TSymbol *symbol)
1022{
1023 const TVariable *variable = NULL;
1024
1025 if (!symbol)
1026 {
1027 error(location, "undeclared identifier", name->c_str());
1028 recover();
1029 }
1030 else if (!symbol->isVariable())
1031 {
1032 error(location, "variable expected", name->c_str());
1033 recover();
1034 }
1035 else
1036 {
1037 variable = static_cast<const TVariable*>(symbol);
1038
1039 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1040 !variable->getExtension().empty() &&
1041 extensionErrorCheck(location, variable->getExtension()))
1042 {
1043 recover();
1044 }
1045 }
1046
1047 if (!variable)
1048 {
1049 TType type(EbtFloat, EbpUndefined);
1050 TVariable *fakeVariable = new TVariable(name, type);
1051 symbolTable.declare(fakeVariable);
1052 variable = fakeVariable;
1053 }
1054
1055 return variable;
1056}
1057
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058//
1059// Look up a function name in the symbol table, and make sure it is a function.
1060//
1061// Return the function symbol if found, otherwise 0.
1062//
Austin Kinross3ae64652015-01-26 15:51:39 -08001063const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001065 // First find by unmangled name to check whether the function name has been
1066 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001067 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001068 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001069 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001070 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001071 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
alokp@chromium.org0a576182010-08-09 17:16:27 +00001073 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 return 0;
1076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
alokp@chromium.org0a576182010-08-09 17:16:27 +00001078 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001079 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 return 0;
1081 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001082
1083 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
1086//
1087// Initializers show up in several places in the grammar. Have one set of
1088// code to handle them here.
1089//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001090// Returns true on error, false if no error
1091//
Olli Etuaho2935c582015-04-08 14:32:06 +03001092bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001093 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001095 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001096 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001099 if (type.isUnsizedArray())
1100 {
1101 type.setArraySize(initializer->getArraySize());
1102 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 if (!declareVariable(line, identifier, type, &variable))
1104 {
1105 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 //
1109 // identifier must be of type constant, a global, or a temporary
1110 //
1111 TQualifier qualifier = variable->getType().getQualifier();
1112 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001113 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 return true;
1115 }
1116 //
1117 // test for and propagate constant
1118 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 if (qualifier == EvqConst) {
1121 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001122 std::stringstream extraInfoStream;
1123 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1124 std::string extraInfo = extraInfoStream.str();
1125 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001126 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001127 return true;
1128 }
1129 if (type != initializer->getType()) {
1130 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001131 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001132 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001133 return true;
1134 }
1135 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001136 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001138 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001139 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001141 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001142 variable->shareConstPointer(constArray);
1143 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001144 std::stringstream extraInfoStream;
1145 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1146 std::string extraInfo = extraInfoStream.str();
1147 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001148 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001149 return true;
1150 }
1151 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001152
1153 if (qualifier != EvqConst)
1154 {
1155 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1156 variable->getType(), line);
1157 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1158 if (*intermNode == nullptr)
1159 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1161 return true;
1162 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001163 }
1164 else
1165 {
1166 *intermNode = nullptr;
1167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170}
1171
1172bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1173{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001174 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 if (!aggrNode->isConstructor())
1176 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001178 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 // check if all the child nodes are constants so that they can be inserted into
1181 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001182 TIntermSequence *sequence = aggrNode->getSequence() ;
1183 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001184 if (!(*p)->getAsTyped()->getAsConstantUnion())
1185 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001188 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189}
1190
Jamie Madilla5efff92013-06-06 11:56:47 -04001191TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001192{
1193 TPublicType returnType = typeSpecifier;
1194 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001195 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001196
1197 if (typeSpecifier.array)
1198 {
1199 error(typeSpecifier.line, "not supported", "first-class array");
1200 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001201 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001202 }
1203
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001204 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001205 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001206 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1207 {
1208 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1209 recover();
1210 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001211
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001212 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1213 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1214 {
1215 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1216 recover();
1217 }
1218 }
1219 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001220 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001221 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001222 {
Jamie Madill19571812013-08-12 15:26:34 -07001223 case EvqSmoothIn:
1224 case EvqSmoothOut:
1225 case EvqVertexOut:
1226 case EvqFragmentIn:
1227 case EvqCentroidOut:
1228 case EvqCentroidIn:
1229 if (typeSpecifier.type == EbtBool)
1230 {
1231 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1232 recover();
1233 }
1234 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1235 {
1236 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1237 recover();
1238 }
1239 break;
1240
1241 case EvqVertexIn:
1242 case EvqFragmentOut:
1243 case EvqFlatIn:
1244 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001245 if (typeSpecifier.type == EbtBool)
1246 {
1247 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1248 recover();
1249 }
1250 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001251
Jamie Madillb120eac2013-06-12 14:08:13 -04001252 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001253 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001254 }
1255
1256 return returnType;
1257}
1258
Olli Etuahofa33d582015-04-09 14:33:12 +03001259TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1260 const TSourceLoc &identifierOrTypeLocation,
1261 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001262{
Olli Etuahofa33d582015-04-09 14:33:12 +03001263 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001264
Olli Etuahofa33d582015-04-09 14:33:12 +03001265 mDeferredSingleDeclarationErrorCheck = (identifier == "");
1266
1267 if (!mDeferredSingleDeclarationErrorCheck)
Jamie Madill60ed9812013-06-06 11:56:46 -04001268 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001269 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001270 recover();
1271
Olli Etuaho376f1b52015-04-13 13:23:41 +03001272 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001273 recover();
1274
Olli Etuaho2935c582015-04-08 14:32:06 +03001275 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001276 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001277 recover();
1278
1279 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001280 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001281 }
1282
Olli Etuahoe7847b02015-03-16 11:56:12 +02001283 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001284}
1285
Olli Etuahoe7847b02015-03-16 11:56:12 +02001286TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1287 const TSourceLoc &identifierLocation,
1288 const TString &identifier,
1289 const TSourceLoc &indexLocation,
1290 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001291{
Olli Etuahofa33d582015-04-09 14:33:12 +03001292 mDeferredSingleDeclarationErrorCheck = false;
1293
1294 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001295 recover();
1296
Olli Etuaho376f1b52015-04-13 13:23:41 +03001297 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001298 recover();
1299
1300 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1301 {
1302 recover();
1303 }
1304
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001305 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001306
1307 int size;
1308 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1309 {
1310 recover();
1311 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001312 // Make the type an array even if size check failed.
1313 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1314 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001315
Olli Etuaho2935c582015-04-08 14:32:06 +03001316 TVariable *variable = nullptr;
1317 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001318 recover();
1319
Olli Etuahoe7847b02015-03-16 11:56:12 +02001320 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001321 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001322 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001323
Olli Etuahoe7847b02015-03-16 11:56:12 +02001324 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001325}
1326
Olli Etuahoe7847b02015-03-16 11:56:12 +02001327TIntermAggregate *TParseContext::parseSingleInitDeclaration(TPublicType &publicType,
1328 const TSourceLoc &identifierLocation,
1329 const TString &identifier,
1330 const TSourceLoc &initLocation,
1331 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001332{
Olli Etuahofa33d582015-04-09 14:33:12 +03001333 mDeferredSingleDeclarationErrorCheck = false;
1334
1335 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001336 recover();
1337
Olli Etuahoe7847b02015-03-16 11:56:12 +02001338 TIntermNode *intermNode = nullptr;
1339 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001340 {
1341 //
1342 // Build intermediate representation
1343 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001344 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001345 }
1346 else
1347 {
1348 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001349 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001350 }
1351}
1352
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001353TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1354 const TSourceLoc &identifierLocation,
1355 const TString &identifier,
1356 const TSourceLoc &indexLocation,
1357 TIntermTyped *indexExpression,
1358 const TSourceLoc &initLocation,
1359 TIntermTyped *initializer)
1360{
1361 mDeferredSingleDeclarationErrorCheck = false;
1362
1363 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1364 recover();
1365
1366 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1367 {
1368 recover();
1369 }
1370
1371 TPublicType arrayType(publicType);
1372
Olli Etuaho376f1b52015-04-13 13:23:41 +03001373 int size = 0;
1374 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1375 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001376 {
1377 recover();
1378 }
1379 // Make the type an array even if size check failed.
1380 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1381 arrayType.setArraySize(size);
1382
1383 // initNode will correspond to the whole of "type b[n] = initializer".
1384 TIntermNode *initNode = nullptr;
1385 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1386 {
1387 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1388 }
1389 else
1390 {
1391 recover();
1392 return nullptr;
1393 }
1394}
1395
Olli Etuahoe7847b02015-03-16 11:56:12 +02001396TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001397 const TSourceLoc &identifierLoc,
1398 const TString *identifier,
1399 const TSymbol *symbol)
1400{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001401 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001402 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1403 {
1404 recover();
1405 }
1406
1407 if (!symbol)
1408 {
1409 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1410 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001411 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001412 }
1413 else
1414 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001415 const TString kGlFrontFacing("gl_FrontFacing");
1416 if (*identifier == kGlFrontFacing)
1417 {
1418 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1419 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001420 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001421 }
Jamie Madill2c433252014-12-03 12:36:54 -05001422 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001423 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1424 ASSERT(variable);
1425 const TType &type = variable->getType();
1426 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1427 *identifier, type, identifierLoc);
1428
1429 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1430 aggregate->setOp(EOpInvariantDeclaration);
1431 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001432 }
1433}
1434
Olli Etuahoe7847b02015-03-16 11:56:12 +02001435TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1436 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001437{
Olli Etuahofa33d582015-04-09 14:33:12 +03001438 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1439 if (mDeferredSingleDeclarationErrorCheck)
1440 {
1441 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1442 recover();
1443 mDeferredSingleDeclarationErrorCheck = false;
1444 }
1445
Jamie Madill0bd18df2013-06-20 11:55:52 -04001446 if (locationDeclaratorListCheck(identifierLocation, publicType))
1447 recover();
1448
Olli Etuaho376f1b52015-04-13 13:23:41 +03001449 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001450 recover();
1451
Olli Etuaho2935c582015-04-08 14:32:06 +03001452 TVariable *variable = nullptr;
1453 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001454 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001455
1456 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1457 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001458 symbol->setId(variable->getUniqueId());
1459
Olli Etuahoe7847b02015-03-16 11:56:12 +02001460 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001461}
1462
Olli Etuahoe7847b02015-03-16 11:56:12 +02001463TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1464 const TSourceLoc &identifierLocation, const TString &identifier,
1465 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001466{
Olli Etuahofa33d582015-04-09 14:33:12 +03001467 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1468 if (mDeferredSingleDeclarationErrorCheck)
1469 {
1470 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1471 recover();
1472 mDeferredSingleDeclarationErrorCheck = false;
1473 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001474
Jamie Madill0bd18df2013-06-20 11:55:52 -04001475 if (locationDeclaratorListCheck(identifierLocation, publicType))
1476 recover();
1477
Olli Etuaho376f1b52015-04-13 13:23:41 +03001478 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001479 recover();
1480
1481 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1482 {
1483 recover();
1484 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001485 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001486 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001487 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001488 int size;
1489 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001490 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001491 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001492 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001493 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001494
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001495 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001496 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001497 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001498
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001499 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1500 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001501 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001502
1503 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001504 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001505
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001506 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001507}
1508
Olli Etuahoe7847b02015-03-16 11:56:12 +02001509TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1510 const TSourceLoc &identifierLocation, const TString &identifier,
1511 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001512{
Olli Etuahofa33d582015-04-09 14:33:12 +03001513 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1514 if (mDeferredSingleDeclarationErrorCheck)
1515 {
1516 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1517 recover();
1518 mDeferredSingleDeclarationErrorCheck = false;
1519 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001520
Jamie Madill0bd18df2013-06-20 11:55:52 -04001521 if (locationDeclaratorListCheck(identifierLocation, publicType))
1522 recover();
1523
Olli Etuahoe7847b02015-03-16 11:56:12 +02001524 TIntermNode *intermNode = nullptr;
1525 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001526 {
1527 //
1528 // build the intermediate representation
1529 //
1530 if (intermNode)
1531 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001532 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001533 }
1534 else
1535 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001536 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001537 }
1538 }
1539 else
1540 {
1541 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001542 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001543 }
1544}
1545
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001546TIntermAggregate *TParseContext::parseArrayInitDeclarator(TPublicType &publicType,
1547 TIntermAggregate *aggregateDeclaration,
1548 const TSourceLoc& identifierLocation,
1549 const TString &identifier,
1550 const TSourceLoc& indexLocation,
1551 TIntermTyped *indexExpression,
1552 const TSourceLoc &initLocation, TIntermTyped *initializer)
1553{
1554 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1555 if (mDeferredSingleDeclarationErrorCheck)
1556 {
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1558 recover();
1559 mDeferredSingleDeclarationErrorCheck = false;
1560 }
1561
1562 if (locationDeclaratorListCheck(identifierLocation, publicType))
1563 recover();
1564
1565 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1566 {
1567 recover();
1568 }
1569
1570 TPublicType arrayType(publicType);
1571
Olli Etuaho376f1b52015-04-13 13:23:41 +03001572 int size = 0;
1573 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1574 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001575 {
1576 recover();
1577 }
1578 // Make the type an array even if size check failed.
1579 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1580 arrayType.setArraySize(size);
1581
1582 // initNode will correspond to the whole of "b[n] = initializer".
1583 TIntermNode *initNode = nullptr;
1584 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1585 {
1586 if (initNode)
1587 {
1588 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1589 }
1590 else
1591 {
1592 return aggregateDeclaration;
1593 }
1594 }
1595 else
1596 {
1597 recover();
1598 return nullptr;
1599 }
1600}
1601
Jamie Madilla295edf2013-06-06 11:56:48 -04001602void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1603{
1604 if (typeQualifier.qualifier != EvqUniform)
1605 {
1606 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1607 recover();
1608 return;
1609 }
1610
1611 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1612 ASSERT(!layoutQualifier.isEmpty());
1613
1614 if (shaderVersion < 300)
1615 {
1616 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1617 recover();
1618 return;
1619 }
1620
1621 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1622 {
1623 recover();
1624 return;
1625 }
1626
Jamie Madill099c0f32013-06-20 11:55:52 -04001627 if (layoutQualifier.matrixPacking != EmpUnspecified)
1628 {
1629 defaultMatrixPacking = layoutQualifier.matrixPacking;
1630 }
1631
Geoff Langc6856732014-02-11 09:38:55 -05001632 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001633 {
1634 defaultBlockStorage = layoutQualifier.blockStorage;
1635 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001636}
1637
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001638TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1639{
1640 TOperator op = EOpNull;
1641 if (publicType.userDef)
1642 {
1643 op = EOpConstructStruct;
1644 }
1645 else
1646 {
1647 switch (publicType.type)
1648 {
1649 case EbtFloat:
1650 if (publicType.isMatrix())
1651 {
1652 // TODO: non-square matrices
1653 switch(publicType.getCols())
1654 {
Jamie Madill28b97422013-07-08 14:01:38 -04001655 case 2: op = EOpConstructMat2; break;
1656 case 3: op = EOpConstructMat3; break;
1657 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001658 }
1659 }
1660 else
1661 {
1662 switch(publicType.getNominalSize())
1663 {
Jamie Madill28b97422013-07-08 14:01:38 -04001664 case 1: op = EOpConstructFloat; break;
1665 case 2: op = EOpConstructVec2; break;
1666 case 3: op = EOpConstructVec3; break;
1667 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001668 }
1669 }
1670 break;
1671
1672 case EbtInt:
1673 switch(publicType.getNominalSize())
1674 {
Jamie Madill28b97422013-07-08 14:01:38 -04001675 case 1: op = EOpConstructInt; break;
1676 case 2: op = EOpConstructIVec2; break;
1677 case 3: op = EOpConstructIVec3; break;
1678 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001679 }
1680 break;
1681
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001682 case EbtUInt:
1683 switch(publicType.getNominalSize())
1684 {
Jamie Madill28b97422013-07-08 14:01:38 -04001685 case 1: op = EOpConstructUInt; break;
1686 case 2: op = EOpConstructUVec2; break;
1687 case 3: op = EOpConstructUVec3; break;
1688 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001689 }
1690 break;
1691
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001692 case EbtBool:
1693 switch(publicType.getNominalSize())
1694 {
Jamie Madill28b97422013-07-08 14:01:38 -04001695 case 1: op = EOpConstructBool; break;
1696 case 2: op = EOpConstructBVec2; break;
1697 case 3: op = EOpConstructBVec3; break;
1698 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001699 }
1700 break;
1701
1702 default: break;
1703 }
1704
1705 if (op == EOpNull)
1706 {
1707 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1708 recover();
1709 publicType.type = EbtFloat;
1710 op = EOpConstructFloat;
1711 }
1712 }
1713
1714 TString tempString;
1715 TType type(publicType);
1716 return new TFunction(&tempString, type, op);
1717}
1718
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719// This function is used to test for the correctness of the parameters passed to various constructor functions
1720// and also convert them to the right datatype if it is allowed and required.
1721//
1722// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1723//
Olli Etuaho21203702014-11-13 16:16:21 +02001724TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001726 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001728 if (!aggregateArguments)
1729 {
1730 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001731 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001732 }
1733
Olli Etuahof40319e2015-03-10 14:33:00 +02001734 if (type->isArray())
1735 {
1736 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1737 TIntermSequence *args = aggregateArguments->getSequence();
1738 for (size_t i = 0; i < args->size(); i++)
1739 {
1740 const TType &argType = (*args)[i]->getAsTyped()->getType();
1741 // It has already been checked that the argument is not an array.
1742 ASSERT(!argType.isArray());
1743 if (!argType.sameElementType(*type))
1744 {
1745 error(line, "Array constructor argument has an incorrect type", "Error");
1746 recover();
1747 return nullptr;
1748 }
1749 }
1750 }
1751 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001752 {
1753 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001754 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001756 for (size_t i = 0; i < fields.size(); i++)
1757 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001758 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001759 {
1760 error(line, "Structure constructor arguments do not match structure fields", "Error");
1761 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001763 return 0;
1764 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 }
1766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001768 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001769 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1770 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001771 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001772 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001773 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775
Olli Etuaho21203702014-11-13 16:16:21 +02001776 // Structs should not be precision qualified, the individual members may be.
1777 // Built-in types on the other hand should be precision qualified.
1778 if (op != EOpConstructStruct)
1779 {
1780 constructor->setPrecisionFromChildren();
1781 type->setPrecision(constructor->getPrecision());
1782 }
1783
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785}
1786
1787TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1788{
Olli Etuahof40319e2015-03-10 14:33:00 +02001789 // TODO: Add support for folding array constructors
1790 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001791 aggrNode->setType(type);
1792 if (canBeFolded) {
1793 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001794 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001795 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001796 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001797 }
1798 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001799 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 }
1801 if (returnVal)
1802 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001807 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808}
1809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810//
1811// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1812// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1813// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1814// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1815// a constant matrix.
1816//
Jamie Madill075edd82013-07-08 13:30:19 -04001817TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 TIntermTyped* typedNode;
1820 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001822 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 if (tempConstantNode) {
1824 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001826 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 return node;
1828 }
1829 } 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 +00001830 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001833 return 0;
1834 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001836 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001838 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001839 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001840 std::stringstream extraInfoStream;
1841 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1842 std::string extraInfo = extraInfoStream.str();
1843 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 recover();
1845 fields.offsets[i] = 0;
1846 }
1847
1848 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001850 }
1851 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1852 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853}
1854
1855//
1856// This function returns the column being accessed from a constant matrix. The values are retrieved from
1857// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1858// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1859// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1860//
Jamie Madill075edd82013-07-08 13:30:19 -04001861TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001863 TIntermTyped* typedNode;
1864 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001866 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001867 std::stringstream extraInfoStream;
1868 extraInfoStream << "matrix field selection out of range '" << index << "'";
1869 std::string extraInfo = extraInfoStream.str();
1870 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001871 recover();
1872 index = 0;
1873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001875 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001876 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001877 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001878 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1879 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001880 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001883 return 0;
1884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001886 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887}
1888
1889
1890//
1891// This function returns an element of an array accessed from a constant array. The values are retrieved from
1892// the symbol table and parse-tree is built for the type of the element. The input
1893// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1894// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1895//
Jamie Madill075edd82013-07-08 13:30:19 -04001896TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001898 TIntermTyped* typedNode;
1899 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1900 TType arrayElementType = node->getType();
1901 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001903 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001904 std::stringstream extraInfoStream;
1905 extraInfoStream << "array field selection out of range '" << index << "'";
1906 std::string extraInfo = extraInfoStream.str();
1907 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001908 recover();
1909 index = 0;
1910 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001912 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001913 size_t arrayElementSize = arrayElementType.getObjectSize();
1914 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1915 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001916 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001917 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001918 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001920 return 0;
1921 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001923 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924}
1925
1926
1927//
1928// This function returns the value of a particular field inside a constant structure from the symbol table.
1929// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1930// function and returns the parse-tree with the values of the embedded/nested struct.
1931//
Jamie Madill075edd82013-07-08 13:30:19 -04001932TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933{
Jamie Madill98493dd2013-07-08 14:39:03 -04001934 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001935 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936
Jamie Madill98493dd2013-07-08 14:39:03 -04001937 for (size_t index = 0; index < fields.size(); ++index) {
1938 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 break;
1940 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001941 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001942 }
1943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
Jamie Madill94bf7f22013-07-08 13:31:15 -04001945 TIntermTyped *typedNode;
1946 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001947 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001948 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1951 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001952 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001953 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001955 return 0;
1956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959}
1960
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001961//
1962// Interface/uniform blocks
1963//
Jamie Madill98493dd2013-07-08 14:39:03 -04001964TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1965 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001966{
1967 if (reservedErrorCheck(nameLine, blockName))
1968 recover();
1969
1970 if (typeQualifier.qualifier != EvqUniform)
1971 {
1972 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1973 recover();
1974 }
1975
Jamie Madill099c0f32013-06-20 11:55:52 -04001976 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1977 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001978 {
1979 recover();
1980 }
1981
Jamie Madill099c0f32013-06-20 11:55:52 -04001982 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1983 {
1984 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1985 }
1986
Jamie Madill1566ef72013-06-20 11:55:54 -04001987 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1988 {
1989 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1990 }
1991
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001992 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001993 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001994 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1995 recover();
1996 }
1997
Jamie Madill98493dd2013-07-08 14:39:03 -04001998 // check for sampler types and apply layout qualifiers
1999 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2000 TField* field = (*fieldList)[memberIndex];
2001 TType* fieldType = field->type();
2002 if (IsSampler(fieldType->getBasicType())) {
2003 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002004 recover();
2005 }
2006
Jamie Madill98493dd2013-07-08 14:39:03 -04002007 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002008 switch (qualifier)
2009 {
2010 case EvqGlobal:
2011 case EvqUniform:
2012 break;
2013 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002014 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002015 recover();
2016 break;
2017 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002018
2019 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002020 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2021 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002022 {
2023 recover();
2024 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002025
Jamie Madill98493dd2013-07-08 14:39:03 -04002026 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002027 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002028 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002029 recover();
2030 }
2031
Jamie Madill98493dd2013-07-08 14:39:03 -04002032 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002033 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002034 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002035 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002036 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002037 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002038 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002039 recover();
2040 }
2041
Jamie Madill98493dd2013-07-08 14:39:03 -04002042 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002043 }
2044
Jamie Madill98493dd2013-07-08 14:39:03 -04002045 // add array index
2046 int arraySize = 0;
2047 if (arrayIndex != NULL)
2048 {
2049 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2050 recover();
2051 }
2052
2053 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2054 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002055
2056 TString symbolName = "";
2057 int symbolId = 0;
2058
Jamie Madill98493dd2013-07-08 14:39:03 -04002059 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002060 {
2061 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002062 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2063 {
2064 TField* field = (*fieldList)[memberIndex];
2065 TType* fieldType = field->type();
2066
2067 // set parent pointer of the field variable
2068 fieldType->setInterfaceBlock(interfaceBlock);
2069
2070 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2071 fieldVariable->setQualifier(typeQualifier.qualifier);
2072
Nicolas Capensadfffe42014-06-17 02:13:36 -04002073 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002074 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002075 recover();
2076 }
2077 }
2078 }
2079 else
2080 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002081 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002082 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002083 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002084
Nicolas Capensadfffe42014-06-17 02:13:36 -04002085 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002086 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002087 recover();
2088 }
2089
2090 symbolId = instanceTypeDef->getUniqueId();
2091 symbolName = instanceTypeDef->getName();
2092 }
2093
Jamie Madill98493dd2013-07-08 14:39:03 -04002094 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002095 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002096
2097 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002098 return aggregate;
2099}
2100
Jamie Madill075edd82013-07-08 13:30:19 -04002101bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002102{
2103 ++structNestingLevel;
2104
2105 // Embedded structure definitions are not supported per GLSL ES spec.
2106 // They aren't allowed in GLSL either, but we need to detect this here
2107 // so we don't rely on the GLSL compiler to catch it.
2108 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002109 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002110 return true;
2111 }
2112
2113 return false;
2114}
2115
2116void TParseContext::exitStructDeclaration()
2117{
2118 --structNestingLevel;
2119}
2120
2121namespace {
2122
2123const int kWebGLMaxStructNesting = 4;
2124
2125} // namespace
2126
Jamie Madill98493dd2013-07-08 14:39:03 -04002127bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002128{
Jamie Madill5508f392014-02-20 13:31:36 -05002129 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002130 return false;
2131 }
2132
Jamie Madill98493dd2013-07-08 14:39:03 -04002133 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002134 return false;
2135 }
2136
2137 // We're already inside a structure definition at this point, so add
2138 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002139 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002140 std::stringstream reasonStream;
2141 reasonStream << "Reference of struct type "
2142 << field.type()->getStruct()->name().c_str()
2143 << " exceeds maximum allowed nesting level of "
2144 << kWebGLMaxStructNesting;
2145 std::string reason = reasonStream.str();
2146 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002147 return true;
2148 }
2149
2150 return false;
2151}
2152
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002153//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002154// Parse an array index expression
2155//
Jamie Madill075edd82013-07-08 13:30:19 -04002156TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002157{
2158 TIntermTyped *indexedExpression = NULL;
2159
2160 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2161 {
2162 if (baseExpression->getAsSymbolNode())
2163 {
2164 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2165 }
2166 else
2167 {
2168 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2169 }
2170 recover();
2171 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002172
Jamie Madill21c1e452014-12-29 11:33:41 -05002173 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2174
2175 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002176 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002177 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002178 if (index < 0)
2179 {
2180 std::stringstream infoStream;
2181 infoStream << index;
2182 std::string info = infoStream.str();
2183 error(location, "negative index", info.c_str());
2184 recover();
2185 index = 0;
2186 }
2187 if (baseExpression->getType().getQualifier() == EvqConst)
2188 {
2189 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002190 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002191 // constant folding for arrays
2192 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002193 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002194 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002195 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002196 // constant folding for vectors
2197 TVectorFields fields;
2198 fields.num = 1;
2199 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2200 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2201 }
2202 else if (baseExpression->isMatrix())
2203 {
2204 // constant folding for matrices
2205 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002206 }
2207 }
2208 else
2209 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002210 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002211 {
Jamie Madill18464b52013-07-08 14:01:55 -04002212 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002213 {
2214 std::stringstream extraInfoStream;
2215 extraInfoStream << "array index out of range '" << index << "'";
2216 std::string extraInfo = extraInfoStream.str();
2217 error(location, "", "[", extraInfo.c_str());
2218 recover();
2219 index = baseExpression->getType().getArraySize() - 1;
2220 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002221 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2222 {
2223 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2224 recover();
2225 index = 0;
2226 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002227 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002228 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002229 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002230 std::stringstream extraInfoStream;
2231 extraInfoStream << "field selection out of range '" << index << "'";
2232 std::string extraInfo = extraInfoStream.str();
2233 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002234 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002235 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002236 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002237
Jamie Madill21c1e452014-12-29 11:33:41 -05002238 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002239 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002240 }
2241 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002242 else
2243 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002244 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002245 {
Jamie Madill19571812013-08-12 15:26:34 -07002246 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002247 recover();
2248 }
Jamie Madill19571812013-08-12 15:26:34 -07002249 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002250 {
Jamie Madill19571812013-08-12 15:26:34 -07002251 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002252 recover();
2253 }
2254
2255 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2256 }
2257
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002258 if (indexedExpression == 0)
2259 {
2260 ConstantUnion *unionArray = new ConstantUnion[1];
2261 unionArray->setFConst(0.0f);
2262 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2263 }
2264 else if (baseExpression->isArray())
2265 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002266 const TType &baseType = baseExpression->getType();
2267 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002268 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002269 TType copyOfType(baseType.getStruct());
2270 indexedExpression->setType(copyOfType);
2271 }
2272 else if (baseType.isInterfaceBlock())
2273 {
2274 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002275 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002276 }
2277 else
2278 {
Minmin Gong794e0002015-04-07 18:31:54 -07002279 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2280 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002281 }
2282
2283 if (baseExpression->getType().getQualifier() == EvqConst)
2284 {
2285 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2286 }
2287 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002288 else if (baseExpression->isMatrix())
2289 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002290 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002291 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002292 }
2293 else if (baseExpression->isVector())
2294 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002295 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2296 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002297 }
2298 else
2299 {
2300 indexedExpression->setType(baseExpression->getType());
2301 }
2302
2303 return indexedExpression;
2304}
2305
Jamie Madill075edd82013-07-08 13:30:19 -04002306TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002307{
2308 TIntermTyped *indexedExpression = NULL;
2309
2310 if (baseExpression->isArray())
2311 {
2312 error(fieldLocation, "cannot apply dot operator to an array", ".");
2313 recover();
2314 }
2315
2316 if (baseExpression->isVector())
2317 {
2318 TVectorFields fields;
2319 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2320 {
2321 fields.num = 1;
2322 fields.offsets[0] = 0;
2323 recover();
2324 }
2325
2326 if (baseExpression->getType().getQualifier() == EvqConst)
2327 {
2328 // constant folding for vector fields
2329 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2330 if (indexedExpression == 0)
2331 {
2332 recover();
2333 indexedExpression = baseExpression;
2334 }
2335 else
2336 {
Minmin Gong794e0002015-04-07 18:31:54 -07002337 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002338 }
2339 }
2340 else
2341 {
2342 TString vectorString = fieldString;
2343 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2344 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002345 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002346 }
2347 }
2348 else if (baseExpression->isMatrix())
2349 {
2350 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002351 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002352 {
2353 fields.wholeRow = false;
2354 fields.wholeCol = false;
2355 fields.row = 0;
2356 fields.col = 0;
2357 recover();
2358 }
2359
2360 if (fields.wholeRow || fields.wholeCol)
2361 {
2362 error(dotLocation, " non-scalar fields not implemented yet", ".");
2363 recover();
2364 ConstantUnion *unionArray = new ConstantUnion[1];
2365 unionArray->setIConst(0);
2366 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2367 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002368 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2369 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 }
2371 else
2372 {
2373 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002374 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002375 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2376 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2377 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2378 }
2379 }
2380 else if (baseExpression->getBasicType() == EbtStruct)
2381 {
2382 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002383 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2384 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002385 {
2386 error(dotLocation, "structure has no fields", "Internal Error");
2387 recover();
2388 indexedExpression = baseExpression;
2389 }
2390 else
2391 {
2392 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002393 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002394 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002395 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002396 {
2397 fieldFound = true;
2398 break;
2399 }
2400 }
2401 if (fieldFound)
2402 {
2403 if (baseExpression->getType().getQualifier() == EvqConst)
2404 {
2405 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2406 if (indexedExpression == 0)
2407 {
2408 recover();
2409 indexedExpression = baseExpression;
2410 }
2411 else
2412 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002413 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002414 // change the qualifier of the return type, not of the structure field
2415 // as the structure definition is shared between various structures.
2416 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2417 }
2418 }
2419 else
2420 {
2421 ConstantUnion *unionArray = new ConstantUnion[1];
2422 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002423 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002424 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002425 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002426 }
2427 }
2428 else
2429 {
2430 error(dotLocation, " no such field in structure", fieldString.c_str());
2431 recover();
2432 indexedExpression = baseExpression;
2433 }
2434 }
2435 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002436 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002437 {
2438 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002439 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2440 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002441 {
2442 error(dotLocation, "interface block has no fields", "Internal Error");
2443 recover();
2444 indexedExpression = baseExpression;
2445 }
2446 else
2447 {
2448 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002449 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002450 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002451 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002452 {
2453 fieldFound = true;
2454 break;
2455 }
2456 }
2457 if (fieldFound)
2458 {
2459 ConstantUnion *unionArray = new ConstantUnion[1];
2460 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002461 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002462 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002463 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002464 }
2465 else
2466 {
2467 error(dotLocation, " no such field in interface block", fieldString.c_str());
2468 recover();
2469 indexedExpression = baseExpression;
2470 }
2471 }
2472 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002473 else
2474 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002475 if (shaderVersion < 300)
2476 {
2477 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2478 }
2479 else
2480 {
2481 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2482 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002483 recover();
2484 indexedExpression = baseExpression;
2485 }
2486
2487 return indexedExpression;
2488}
2489
Jamie Madill075edd82013-07-08 13:30:19 -04002490TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002491{
Jamie Madilla5efff92013-06-06 11:56:47 -04002492 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002493
Jamie Madilla5efff92013-06-06 11:56:47 -04002494 qualifier.location = -1;
2495 qualifier.matrixPacking = EmpUnspecified;
2496 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002497
2498 if (qualifierType == "shared")
2499 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002500 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002501 }
2502 else if (qualifierType == "packed")
2503 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002504 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002505 }
2506 else if (qualifierType == "std140")
2507 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002508 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002509 }
2510 else if (qualifierType == "row_major")
2511 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002512 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002513 }
2514 else if (qualifierType == "column_major")
2515 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002516 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002517 }
2518 else if (qualifierType == "location")
2519 {
2520 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2521 recover();
2522 }
2523 else
2524 {
2525 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2526 recover();
2527 }
2528
Jamie Madilla5efff92013-06-06 11:56:47 -04002529 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002530}
2531
Jamie Madill075edd82013-07-08 13:30:19 -04002532TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002533{
Jamie Madilla5efff92013-06-06 11:56:47 -04002534 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002535
Jamie Madilla5efff92013-06-06 11:56:47 -04002536 qualifier.location = -1;
2537 qualifier.matrixPacking = EmpUnspecified;
2538 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002539
2540 if (qualifierType != "location")
2541 {
2542 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2543 recover();
2544 }
2545 else
2546 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002547 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002548 if (intValue < 0)
2549 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002550 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002551 recover();
2552 }
2553 else
2554 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002555 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002556 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002557 }
2558
Jamie Madilla5efff92013-06-06 11:56:47 -04002559 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002560}
2561
Jamie Madilla5efff92013-06-06 11:56:47 -04002562TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002563{
Jamie Madilla5efff92013-06-06 11:56:47 -04002564 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002565
Jamie Madilla5efff92013-06-06 11:56:47 -04002566 if (rightQualifier.location != -1)
2567 {
2568 joinedQualifier.location = rightQualifier.location;
2569 }
2570 if (rightQualifier.matrixPacking != EmpUnspecified)
2571 {
2572 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2573 }
2574 if (rightQualifier.blockStorage != EbsUnspecified)
2575 {
2576 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2577 }
2578
2579 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002580}
2581
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002582TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2583 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2584{
2585 TQualifier mergedQualifier = EvqSmoothIn;
2586
Jamie Madill19571812013-08-12 15:26:34 -07002587 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002588 if (interpolationQualifier == EvqSmooth)
2589 mergedQualifier = EvqSmoothIn;
2590 else if (interpolationQualifier == EvqFlat)
2591 mergedQualifier = EvqFlatIn;
2592 else UNREACHABLE();
2593 }
2594 else if (storageQualifier == EvqCentroidIn) {
2595 if (interpolationQualifier == EvqSmooth)
2596 mergedQualifier = EvqCentroidIn;
2597 else if (interpolationQualifier == EvqFlat)
2598 mergedQualifier = EvqFlatIn;
2599 else UNREACHABLE();
2600 }
Jamie Madill19571812013-08-12 15:26:34 -07002601 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002602 if (interpolationQualifier == EvqSmooth)
2603 mergedQualifier = EvqSmoothOut;
2604 else if (interpolationQualifier == EvqFlat)
2605 mergedQualifier = EvqFlatOut;
2606 else UNREACHABLE();
2607 }
2608 else if (storageQualifier == EvqCentroidOut) {
2609 if (interpolationQualifier == EvqSmooth)
2610 mergedQualifier = EvqCentroidOut;
2611 else if (interpolationQualifier == EvqFlat)
2612 mergedQualifier = EvqFlatOut;
2613 else UNREACHABLE();
2614 }
2615 else {
2616 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2617 recover();
2618
2619 mergedQualifier = storageQualifier;
2620 }
2621
2622 TPublicType type;
2623 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2624 return type;
2625}
2626
Jamie Madill98493dd2013-07-08 14:39:03 -04002627TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002628{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002629 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2630 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002631 recover();
2632 }
2633
Jamie Madill98493dd2013-07-08 14:39:03 -04002634 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002635 //
2636 // Careful not to replace already known aspects of type, like array-ness
2637 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002638 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002639 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002640 type->setPrimarySize(typeSpecifier.primarySize);
2641 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002642 type->setPrecision(typeSpecifier.precision);
2643 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002644 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002645
2646 // don't allow arrays of arrays
2647 if (type->isArray()) {
2648 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2649 recover();
2650 }
2651 if (typeSpecifier.array)
2652 type->setArraySize(typeSpecifier.arraySize);
2653 if (typeSpecifier.userDef) {
2654 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002655 }
2656
Jamie Madill98493dd2013-07-08 14:39:03 -04002657 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002658 recover();
2659 }
2660 }
2661
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002663}
2664
Jamie Madill98493dd2013-07-08 14:39:03 -04002665TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002666{
Jamie Madill98493dd2013-07-08 14:39:03 -04002667 TStructure* structure = new TStructure(structName, fieldList);
2668 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002669
Jamie Madill9b820842015-02-12 10:40:10 -05002670 // Store a bool in the struct if we're at global scope, to allow us to
2671 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002672 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002673 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002674
Jamie Madill98493dd2013-07-08 14:39:03 -04002675 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002676 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002677 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002678 {
2679 recover();
2680 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002681 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002682 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002683 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002684 recover();
2685 }
2686 }
2687
2688 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002689 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002690 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002691 const TField &field = *(*fieldList)[typeListIndex];
2692 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002693 switch (qualifier)
2694 {
2695 case EvqGlobal:
2696 case EvqTemporary:
2697 break;
2698 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002699 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002700 recover();
2701 break;
2702 }
2703 }
2704
2705 TPublicType publicType;
2706 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002707 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708 exitStructDeclaration();
2709
2710 return publicType;
2711}
2712
Olli Etuahoa3a36662015-02-17 13:46:51 +02002713TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2714{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002715 TBasicType switchType = init->getBasicType();
2716 if ((switchType != EbtInt && switchType != EbtUInt) ||
2717 init->isMatrix() ||
2718 init->isArray() ||
2719 init->isVector())
2720 {
2721 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2722 recover();
2723 return nullptr;
2724 }
2725
Olli Etuahoac5274d2015-02-20 10:19:08 +02002726 if (statementList)
2727 {
2728 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2729 {
2730 recover();
2731 return nullptr;
2732 }
2733 }
2734
Olli Etuahoa3a36662015-02-17 13:46:51 +02002735 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2736 if (node == nullptr)
2737 {
2738 error(loc, "erroneous switch statement", "switch");
2739 recover();
2740 return nullptr;
2741 }
2742 return node;
2743}
2744
2745TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2746{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002747 if (mSwitchNestingLevel == 0)
2748 {
2749 error(loc, "case labels need to be inside switch statements", "case");
2750 recover();
2751 return nullptr;
2752 }
2753 if (condition == nullptr)
2754 {
2755 error(loc, "case label must have a condition", "case");
2756 recover();
2757 return nullptr;
2758 }
2759 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2760 condition->isMatrix() ||
2761 condition->isArray() ||
2762 condition->isVector())
2763 {
2764 error(condition->getLine(), "case label must be a scalar integer", "case");
2765 recover();
2766 }
2767 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2768 if (conditionConst == nullptr)
2769 {
2770 error(condition->getLine(), "case label must be constant", "case");
2771 recover();
2772 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002773 TIntermCase *node = intermediate.addCase(condition, loc);
2774 if (node == nullptr)
2775 {
2776 error(loc, "erroneous case statement", "case");
2777 recover();
2778 return nullptr;
2779 }
2780 return node;
2781}
2782
2783TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2784{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002785 if (mSwitchNestingLevel == 0)
2786 {
2787 error(loc, "default labels need to be inside switch statements", "default");
2788 recover();
2789 return nullptr;
2790 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002791 TIntermCase *node = intermediate.addCase(nullptr, loc);
2792 if (node == nullptr)
2793 {
2794 error(loc, "erroneous default statement", "default");
2795 recover();
2796 return nullptr;
2797 }
2798 return node;
2799}
2800
Olli Etuahof6c694b2015-03-26 14:50:53 +02002801TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2802 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002803{
2804 if (child == nullptr)
2805 {
2806 return nullptr;
2807 }
2808
2809 switch (op)
2810 {
2811 case EOpLogicalNot:
2812 if (child->getBasicType() != EbtBool ||
2813 child->isMatrix() ||
2814 child->isArray() ||
2815 child->isVector())
2816 {
2817 return nullptr;
2818 }
2819 break;
2820 case EOpBitwiseNot:
2821 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2822 child->isMatrix() ||
2823 child->isArray())
2824 {
2825 return nullptr;
2826 }
2827 break;
2828 case EOpPostIncrement:
2829 case EOpPreIncrement:
2830 case EOpPostDecrement:
2831 case EOpPreDecrement:
2832 case EOpNegative:
2833 case EOpPositive:
2834 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002835 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002836 child->isArray())
2837 {
2838 return nullptr;
2839 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002840 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002841 default:
2842 break;
2843 }
2844
Olli Etuahof6c694b2015-03-26 14:50:53 +02002845 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002846}
2847
Olli Etuaho09b22472015-02-11 11:47:26 +02002848TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2849{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002850 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002851 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002852 {
2853 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2854 recover();
2855 return child;
2856 }
2857 return node;
2858}
2859
2860TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2861{
2862 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2863 recover();
2864 return addUnaryMath(op, child, loc);
2865}
2866
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002867bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002868 const TSourceLoc &loc)
2869{
2870 if (left->isArray() || right->isArray())
2871 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002872 if (shaderVersion < 300)
2873 {
2874 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2875 return false;
2876 }
2877
2878 if (left->isArray() != right->isArray())
2879 {
2880 error(loc, "array / non-array mismatch", GetOperatorString(op));
2881 return false;
2882 }
2883
2884 switch (op)
2885 {
2886 case EOpEqual:
2887 case EOpNotEqual:
2888 case EOpAssign:
2889 case EOpInitialize:
2890 break;
2891 default:
2892 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2893 return false;
2894 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002895 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002896 if (left->getArraySize() != right->getArraySize())
2897 {
2898 error(loc, "array size mismatch", GetOperatorString(op));
2899 return false;
2900 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002901 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002902
2903 // Check ops which require integer / ivec parameters
2904 bool isBitShift = false;
2905 switch (op)
2906 {
2907 case EOpBitShiftLeft:
2908 case EOpBitShiftRight:
2909 case EOpBitShiftLeftAssign:
2910 case EOpBitShiftRightAssign:
2911 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2912 // check that the basic type is an integer type.
2913 isBitShift = true;
2914 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2915 {
2916 return false;
2917 }
2918 break;
2919 case EOpBitwiseAnd:
2920 case EOpBitwiseXor:
2921 case EOpBitwiseOr:
2922 case EOpBitwiseAndAssign:
2923 case EOpBitwiseXorAssign:
2924 case EOpBitwiseOrAssign:
2925 // It is enough to check the type of only one operand, since later it
2926 // is checked that the operand types match.
2927 if (!IsInteger(left->getBasicType()))
2928 {
2929 return false;
2930 }
2931 break;
2932 default:
2933 break;
2934 }
2935
2936 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2937 // So the basic type should usually match.
2938 if (!isBitShift && left->getBasicType() != right->getBasicType())
2939 {
2940 return false;
2941 }
2942
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002943 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002944 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002945 switch(op)
2946 {
2947 case EOpAssign:
2948 case EOpInitialize:
2949 case EOpEqual:
2950 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002951 // ESSL 1.00 sections 5.7, 5.8, 5.9
2952 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2953 {
2954 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2955 return false;
2956 }
Olli Etuahoff699002015-03-23 14:38:42 +02002957 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2958 // we interpret the spec so that this extends to structs containing samplers,
2959 // similarly to ESSL 1.00 spec.
2960 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2961 left->getType().isStructureContainingSamplers())
2962 {
2963 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2964 return false;
2965 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002966 case EOpLessThan:
2967 case EOpGreaterThan:
2968 case EOpLessThanEqual:
2969 case EOpGreaterThanEqual:
2970 if ((left->getNominalSize() != right->getNominalSize()) ||
2971 (left->getSecondarySize() != right->getSecondarySize()))
2972 {
2973 return false;
2974 }
2975 default:
2976 break;
2977 }
2978
Olli Etuahod6b14282015-03-17 14:31:35 +02002979 return true;
2980}
2981
Olli Etuahofc1806e2015-03-17 13:03:11 +02002982TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2983 const TSourceLoc &loc)
2984{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002985 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002986 return nullptr;
2987
Olli Etuahofc1806e2015-03-17 13:03:11 +02002988 switch (op)
2989 {
2990 case EOpEqual:
2991 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002992 break;
2993 case EOpLessThan:
2994 case EOpGreaterThan:
2995 case EOpLessThanEqual:
2996 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002997 ASSERT(!left->isArray() && !right->isArray());
2998 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002999 left->getBasicType() == EbtStruct)
3000 {
3001 return nullptr;
3002 }
3003 break;
3004 case EOpLogicalOr:
3005 case EOpLogicalXor:
3006 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003007 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003008 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003009 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003010 {
3011 return nullptr;
3012 }
3013 break;
3014 case EOpAdd:
3015 case EOpSub:
3016 case EOpDiv:
3017 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003018 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003019 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3020 {
3021 return nullptr;
3022 }
3023 break;
3024 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003025 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003026 // Note that this is only for the % operator, not for mod()
3027 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3028 {
3029 return nullptr;
3030 }
3031 break;
3032 // Note that for bitwise ops, type checking is done in promote() to
3033 // share code between ops and compound assignment
3034 default:
3035 break;
3036 }
3037
Olli Etuahofc1806e2015-03-17 13:03:11 +02003038 return intermediate.addBinaryMath(op, left, right, loc);
3039}
3040
Olli Etuaho09b22472015-02-11 11:47:26 +02003041TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3042 const TSourceLoc &loc)
3043{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003044 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003045 if (node == 0)
3046 {
3047 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3048 recover();
3049 return left;
3050 }
3051 return node;
3052}
3053
3054TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3055 const TSourceLoc &loc)
3056{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003057 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003058 if (node == 0)
3059 {
3060 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3061 recover();
3062 ConstantUnion *unionArray = new ConstantUnion[1];
3063 unionArray->setBConst(false);
3064 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3065 }
3066 return node;
3067}
3068
Olli Etuahod6b14282015-03-17 14:31:35 +02003069TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3070 const TSourceLoc &loc)
3071{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003072 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003073 {
3074 return intermediate.addAssign(op, left, right, loc);
3075 }
3076 return nullptr;
3077}
3078
3079TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3080 const TSourceLoc &loc)
3081{
3082 TIntermTyped *node = createAssign(op, left, right, loc);
3083 if (node == nullptr)
3084 {
3085 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3086 recover();
3087 return left;
3088 }
3089 return node;
3090}
3091
Olli Etuaho49300862015-02-20 14:54:49 +02003092TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3093{
3094 switch (op)
3095 {
3096 case EOpContinue:
3097 if (mLoopNestingLevel <= 0)
3098 {
3099 error(loc, "continue statement only allowed in loops", "");
3100 recover();
3101 }
3102 break;
3103 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003104 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003105 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003106 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003107 recover();
3108 }
3109 break;
3110 case EOpReturn:
3111 if (currentFunctionType->getBasicType() != EbtVoid)
3112 {
3113 error(loc, "non-void function must return a value", "return");
3114 recover();
3115 }
3116 break;
3117 default:
3118 // No checks for discard
3119 break;
3120 }
3121 return intermediate.addBranch(op, loc);
3122}
3123
3124TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3125{
3126 ASSERT(op == EOpReturn);
3127 mFunctionReturnsValue = true;
3128 if (currentFunctionType->getBasicType() == EbtVoid)
3129 {
3130 error(loc, "void function cannot return a value", "return");
3131 recover();
3132 }
3133 else if (*currentFunctionType != returnValue->getType())
3134 {
3135 error(loc, "function return is not matching type:", "return");
3136 recover();
3137 }
3138 return intermediate.addBranch(op, returnValue, loc);
3139}
3140
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003141TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3142 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003143{
3144 *fatalError = false;
3145 TOperator op = fnCall->getBuiltInOp();
3146 TIntermTyped *callNode = nullptr;
3147
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003148 if (thisNode != nullptr)
3149 {
3150 ConstantUnion *unionArray = new ConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003151 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003152 TIntermTyped *typedThis = thisNode->getAsTyped();
3153 if (fnCall->getName() != "length")
3154 {
3155 error(loc, "invalid method", fnCall->getName().c_str());
3156 recover();
3157 }
3158 else if (paramNode != nullptr)
3159 {
3160 error(loc, "method takes no parameters", "length");
3161 recover();
3162 }
3163 else if (typedThis == nullptr || !typedThis->isArray())
3164 {
3165 error(loc, "length can only be called on arrays", "length");
3166 recover();
3167 }
3168 else
3169 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003170 arraySize = typedThis->getArraySize();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003171 if (typedThis->hasSideEffects())
3172 {
3173 // This code path can be hit with an expression like this:
3174 // (a = b).length()
3175 // where a and b are both arrays.
3176 UNIMPLEMENTED();
3177 }
3178 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003179 unionArray->setIConst(arraySize);
3180 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003181 }
3182 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003183 {
3184 //
3185 // Then this should be a constructor.
3186 // Don't go through the symbol table for constructors.
3187 // Their parameters will be verified algorithmically.
3188 //
3189 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003190 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003191 {
3192 //
3193 // It's a constructor, of type 'type'.
3194 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003195 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003196 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003197
3198 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003199 {
3200 recover();
3201 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3202 }
3203 callNode->setType(type);
3204 }
3205 else
3206 {
3207 //
3208 // Not a constructor. Find it in the symbol table.
3209 //
3210 const TFunction* fnCandidate;
3211 bool builtIn;
3212 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3213 if (fnCandidate)
3214 {
3215 //
3216 // A declared function.
3217 //
3218 if (builtIn && !fnCandidate->getExtension().empty() &&
3219 extensionErrorCheck(loc, fnCandidate->getExtension()))
3220 {
3221 recover();
3222 }
3223 op = fnCandidate->getBuiltInOp();
3224 if (builtIn && op != EOpNull)
3225 {
3226 //
3227 // A function call mapped to a built-in operation.
3228 //
3229 if (fnCandidate->getParamCount() == 1)
3230 {
3231 //
3232 // Treat it like a built-in unary operator.
3233 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003234 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003235 if (callNode == nullptr)
3236 {
3237 std::stringstream extraInfoStream;
3238 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003239 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003240 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003241 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003242 *fatalError = true;
3243 return nullptr;
3244 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003245 }
3246 else
3247 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003248 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003249 aggregate->setType(fnCandidate->getReturnType());
3250 aggregate->setPrecisionFromChildren();
3251 callNode = aggregate;
3252
3253 // Some built-in functions have out parameters too.
3254 functionCallLValueErrorCheck(fnCandidate, aggregate);
3255 }
3256 }
3257 else
3258 {
3259 // This is a real function call
3260
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003261 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003262 aggregate->setType(fnCandidate->getReturnType());
3263
3264 // this is how we know whether the given function is a builtIn function or a user defined function
3265 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3266 // if builtIn == true, it's definitely a builtIn function with EOpNull
3267 if (!builtIn)
3268 aggregate->setUserDefined();
3269 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003270 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003271
3272 // This needs to happen after the name is set
3273 if (builtIn)
3274 aggregate->setBuiltInFunctionPrecision();
3275
3276 callNode = aggregate;
3277
3278 functionCallLValueErrorCheck(fnCandidate, aggregate);
3279 }
3280 }
3281 else
3282 {
3283 // error message was put out by findFunction()
3284 // Put on a dummy node for error recovery
3285 ConstantUnion *unionArray = new ConstantUnion[1];
3286 unionArray->setFConst(0.0f);
3287 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3288 recover();
3289 }
3290 }
3291 delete fnCall;
3292 return callNode;
3293}
3294
Olli Etuaho52901742015-04-15 13:42:45 +03003295TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3296 const TSourceLoc &loc)
3297{
3298 if (boolErrorCheck(loc, cond))
3299 recover();
3300
3301 if (trueBlock->getType() != falseBlock->getType())
3302 {
3303 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3304 recover();
3305 return falseBlock;
3306 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003307 // ESSL1 sections 5.2 and 5.7:
3308 // ESSL3 section 5.7:
3309 // Ternary operator is not among the operators allowed for structures/arrays.
3310 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3311 {
3312 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3313 recover();
3314 return falseBlock;
3315 }
Olli Etuaho52901742015-04-15 13:42:45 +03003316 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3317}
Olli Etuaho49300862015-02-20 14:54:49 +02003318
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003319//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003320// Parse an array of strings using yyparse.
3321//
3322// Returns 0 for success.
3323//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003324int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003325 TParseContext* context) {
3326 if ((count == 0) || (string == NULL))
3327 return 1;
3328
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003329 if (glslang_initialize(context))
3330 return 1;
3331
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003332 int error = glslang_scan(count, string, length, context);
3333 if (!error)
3334 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003335
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003336 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003337
alokp@chromium.org6b495712012-06-29 00:06:58 +00003338 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003339}
3340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003341
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003342