blob: 27eed39ce1f9a77a977352d3aec45a00f9be1800 [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
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000515 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000516 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 return true;
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000521 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 return true;
523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000526 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000527 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000528 return true;
529 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000533 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 return true;
535 }
536
Brendan Longeaa84062013-12-08 18:26:50 +0100537 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000538 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 return true;
540 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000542 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000543 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
544 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000545 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000546 return true;
547 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000550 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000552 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 return true;
554 }
555 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000556 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000557 return true;
558 }
559 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000560 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 return true;
562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
567// This function checks to see if a void variable has been declared and raise an error message for such a case
568//
569// returns true in case of an error
570//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300571bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300573 if (type == EbtVoid)
574 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000575 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300577 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580}
581
582// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
583//
584// returns true in case of an error
585//
Jamie Madill075edd82013-07-08 13:30:19 -0400586bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000587{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000589 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 return true;
591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
596// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
597//
598// returns true in case of an error
599//
Jamie Madill075edd82013-07-08 13:30:19 -0400600bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000602 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000603 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 return true;
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
Jamie Madill075edd82013-07-08 13:30:19 -0400610bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 if (pType.type == EbtStruct) {
613 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000614 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615
616 return true;
617 }
618
619 return false;
620 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 return true;
624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
Jamie Madill075edd82013-07-08 13:30:19 -0400629bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400630{
631 if (pType.layoutQualifier.location != -1)
632 {
633 error(line, "location must only be specified for a single input or output variable", "location");
634 return true;
635 }
636
637 return false;
638}
639
Jamie Madill075edd82013-07-08 13:30:19 -0400640bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
643 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
651bool TParseContext::containsSampler(TType& type)
652{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 if (IsSampler(type.getBasicType()))
654 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655
Jamie Madill98493dd2013-07-08 14:39:03 -0400656 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
657 const TFieldList& fields = type.getStruct()->fields();
658 for (unsigned int i = 0; i < fields.size(); ++i) {
659 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 return true;
661 }
662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
667//
668// Do size checking for an array type's size.
669//
670// Returns true if there was an error.
671//
Jamie Madill075edd82013-07-08 13:30:19 -0400672bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000675
Olli Etuahoe7847b02015-03-16 11:56:12 +0200676 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000677 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000678 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200679 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return true;
681 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682
Nicolas Capens906744a2014-06-06 15:18:07 -0400683 unsigned int unsignedSize = 0;
684
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000685 if (constant->getBasicType() == EbtUInt)
686 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400687 unsignedSize = constant->getUConst(0);
688 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000689 }
690 else
691 {
692 size = constant->getIConst(0);
693
Nicolas Capens906744a2014-06-06 15:18:07 -0400694 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000695 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400696 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000697 size = 1;
698 return true;
699 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400700
701 unsignedSize = static_cast<unsigned int>(size);
702 }
703
704 if (size == 0)
705 {
706 error(line, "array size must be greater than zero", "");
707 size = 1;
708 return true;
709 }
710
711 // The size of arrays is restricted here to prevent issues further down the
712 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
713 // 4096 registers so this should be reasonable even for aggressively optimizable code.
714 const unsigned int sizeLimit = 65536;
715
716 if (unsignedSize > sizeLimit)
717 {
718 error(line, "array size too large", "");
719 size = 1;
720 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000721 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000723 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724}
725
726//
727// See if this qualifier can be an array.
728//
729// Returns true if there is an error.
730//
Olli Etuaho3739d232015-04-08 12:23:44 +0300731bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732{
Olli Etuaho3739d232015-04-08 12:23:44 +0300733 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
734 (type.qualifier == EvqConst && shaderVersion < 300))
735 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000736 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 return true;
738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
743//
744// See if this type can be an array.
745//
746// Returns true if there is an error.
747//
Jamie Madill075edd82013-07-08 13:30:19 -0400748bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 //
751 // Can the type be an array?
752 //
753 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000754 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 return true;
756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759}
760
761//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762// Enforce non-initializer type/qualifier rules.
763//
764// Returns true if there was an error.
765//
Olli Etuaho3739d232015-04-08 12:23:44 +0300766bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767{
Olli Etuaho3739d232015-04-08 12:23:44 +0300768 ASSERT(type != nullptr);
769 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000770 {
771 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300772 type->qualifier = EvqTemporary;
773
774 // Generate informative error messages for ESSL1.
775 // In ESSL3 arrays and structures containing arrays can be constant.
776 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000777 {
778 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
779 }
780 else
781 {
782 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
783 }
784
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000785 return true;
786 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000787 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788}
789
Olli Etuaho2935c582015-04-08 14:32:06 +0300790// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791// and update the symbol table.
792//
Olli Etuaho2935c582015-04-08 14:32:06 +0300793// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794//
Olli Etuaho2935c582015-04-08 14:32:06 +0300795bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
796 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797{
Olli Etuaho2935c582015-04-08 14:32:06 +0300798 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
Olli Etuaho2935c582015-04-08 14:32:06 +0300800 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801
Olli Etuaho2935c582015-04-08 14:32:06 +0300802 // gl_LastFragData may be redeclared with a new precision qualifier
803 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
804 {
805 const TVariable *maxDrawBuffers =
806 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion));
807 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
808 {
809 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion))
810 {
811 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
812 }
813 }
814 else
815 {
816 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
817 return false;
818 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
Olli Etuaho2935c582015-04-08 14:32:06 +0300821 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
822 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
Olli Etuaho2935c582015-04-08 14:32:06 +0300824 (*variable) = new TVariable(&identifier, type);
825 if (!symbolTable.declare(*variable))
826 {
827 error(line, "redefinition", identifier.c_str());
828 delete (*variable);
829 (*variable) = nullptr;
830 return false;
831 }
832
833 if (voidErrorCheck(line, identifier, type.getBasicType()))
834 return false;
835
836 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837}
838
Jamie Madill075edd82013-07-08 13:30:19 -0400839bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000841 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000842 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000843 return true;
844 }
845 if (qualifier == EvqConst && paramQualifier != EvqIn) {
846 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
847 return true;
848 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000851 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000853 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
Jamie Madill075edd82013-07-08 13:30:19 -0400858bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000859{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000860 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000861 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
862 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000863 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 return true;
865 }
zmo@google.comf5450912011-09-09 01:37:19 +0000866 // In GLSL ES, an extension's default behavior is "disable".
867 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000868 error(line, "extension", extension.c_str(), "is disabled");
869 return true;
870 }
871 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000872 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000873 return false;
874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
Olli Etuahofa33d582015-04-09 14:33:12 +0300879// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
880// declaration.
881//
882bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400883{
Olli Etuahofa33d582015-04-09 14:33:12 +0300884 switch (publicType.qualifier)
885 {
886 case EvqVaryingIn:
887 case EvqVaryingOut:
888 case EvqAttribute:
889 case EvqVertexIn:
890 case EvqFragmentOut:
891 if (publicType.type == EbtStruct)
892 {
893 error(identifierLocation, "cannot be used with a structure",
894 getQualifierString(publicType.qualifier));
895 return true;
896 }
897
898 default: break;
899 }
900
901 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
902 "samplers must be uniform"))
903 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400904 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300905 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400906
907 // check for layout qualifier issues
908 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
909
910 if (layoutQualifier.matrixPacking != EmpUnspecified)
911 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300912 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
913 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400914 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400915 }
916
917 if (layoutQualifier.blockStorage != EbsUnspecified)
918 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300919 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
920 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400921 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400922 }
923
Olli Etuahofa33d582015-04-09 14:33:12 +0300924 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
925 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400926 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400927 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400928 }
929
930 return false;
931}
932
Jamie Madill075edd82013-07-08 13:30:19 -0400933bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400934{
935 if (layoutQualifier.location != -1)
936 {
937 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
938 return true;
939 }
940
941 return false;
942}
943
Olli Etuahob6e07a62015-02-16 12:22:10 +0200944bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
945{
946 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
947 {
948 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
949 if (qual == EvqOut || qual == EvqInOut)
950 {
951 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
952 if (lValueErrorCheck(node->getLine(), "assign", node))
953 {
954 error(node->getLine(),
955 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
956 recover();
957 return true;
958 }
959 }
960 }
961 return false;
962}
963
zmo@google.com09c323a2011-08-12 18:22:25 +0000964bool TParseContext::supportsExtension(const char* extension)
965{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000966 const TExtensionBehavior& extbehavior = extensionBehavior();
967 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
968 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000969}
970
Jamie Madill5d287f52013-07-12 15:38:19 -0400971bool TParseContext::isExtensionEnabled(const char* extension) const
972{
973 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400974 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400975
976 if (iter == extbehavior.end())
977 {
978 return false;
979 }
980
981 return (iter->second == EBhEnable || iter->second == EBhRequire);
982}
983
Jamie Madill075edd82013-07-08 13:30:19 -0400984void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
985{
986 pp::SourceLocation srcLoc;
987 srcLoc.file = loc.first_file;
988 srcLoc.line = loc.first_line;
989 directiveHandler.handleExtension(srcLoc, extName, behavior);
990}
991
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700992void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -0400993{
994 pp::SourceLocation srcLoc;
995 srcLoc.file = loc.first_file;
996 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700997 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -0400998}
999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000/////////////////////////////////////////////////////////////////////////////////
1001//
1002// Non-Errors.
1003//
1004/////////////////////////////////////////////////////////////////////////////////
1005
Jamie Madill5c097022014-08-20 16:38:32 -04001006const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1007 const TString *name,
1008 const TSymbol *symbol)
1009{
1010 const TVariable *variable = NULL;
1011
1012 if (!symbol)
1013 {
1014 error(location, "undeclared identifier", name->c_str());
1015 recover();
1016 }
1017 else if (!symbol->isVariable())
1018 {
1019 error(location, "variable expected", name->c_str());
1020 recover();
1021 }
1022 else
1023 {
1024 variable = static_cast<const TVariable*>(symbol);
1025
1026 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1027 !variable->getExtension().empty() &&
1028 extensionErrorCheck(location, variable->getExtension()))
1029 {
1030 recover();
1031 }
1032 }
1033
1034 if (!variable)
1035 {
1036 TType type(EbtFloat, EbpUndefined);
1037 TVariable *fakeVariable = new TVariable(name, type);
1038 symbolTable.declare(fakeVariable);
1039 variable = fakeVariable;
1040 }
1041
1042 return variable;
1043}
1044
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045//
1046// Look up a function name in the symbol table, and make sure it is a function.
1047//
1048// Return the function symbol if found, otherwise 0.
1049//
Austin Kinross3ae64652015-01-26 15:51:39 -08001050const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001052 // First find by unmangled name to check whether the function name has been
1053 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001054 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001055 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001056 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001057 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059
alokp@chromium.org0a576182010-08-09 17:16:27 +00001060 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001061 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 return 0;
1063 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064
alokp@chromium.org0a576182010-08-09 17:16:27 +00001065 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001066 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001067 return 0;
1068 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001069
1070 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073//
1074// Initializers show up in several places in the grammar. Have one set of
1075// code to handle them here.
1076//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001077// Returns true on error, false if no error
1078//
Olli Etuaho2935c582015-04-08 14:32:06 +03001079bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001080 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001082 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001083 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
Olli Etuaho2935c582015-04-08 14:32:06 +03001085 TVariable *variable = nullptr;
1086 if (!declareVariable(line, identifier, type, &variable))
1087 {
1088 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 //
1092 // identifier must be of type constant, a global, or a temporary
1093 //
1094 TQualifier qualifier = variable->getType().getQualifier();
1095 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001096 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001097 return true;
1098 }
1099 //
1100 // test for and propagate constant
1101 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 if (qualifier == EvqConst) {
1104 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001105 std::stringstream extraInfoStream;
1106 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1107 std::string extraInfo = extraInfoStream.str();
1108 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001109 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 return true;
1111 }
1112 if (type != initializer->getType()) {
1113 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001114 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001115 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001116 return true;
1117 }
1118 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001119 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001121 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001124 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 variable->shareConstPointer(constArray);
1126 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001127 std::stringstream extraInfoStream;
1128 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1129 std::string extraInfo = extraInfoStream.str();
1130 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001131 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 return true;
1133 }
1134 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001135
1136 if (qualifier != EvqConst)
1137 {
1138 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1139 variable->getType(), line);
1140 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1141 if (*intermNode == nullptr)
1142 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001143 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1144 return true;
1145 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001146 }
1147 else
1148 {
1149 *intermNode = nullptr;
1150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001152 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153}
1154
1155bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1156{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001157 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 if (!aggrNode->isConstructor())
1159 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001161 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001163 // check if all the child nodes are constants so that they can be inserted into
1164 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001165 TIntermSequence *sequence = aggrNode->getSequence() ;
1166 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001167 if (!(*p)->getAsTyped()->getAsConstantUnion())
1168 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001171 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172}
1173
Jamie Madilla5efff92013-06-06 11:56:47 -04001174TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001175{
1176 TPublicType returnType = typeSpecifier;
1177 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001178 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001179
1180 if (typeSpecifier.array)
1181 {
1182 error(typeSpecifier.line, "not supported", "first-class array");
1183 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001184 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001185 }
1186
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001187 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001188 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001189 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1190 {
1191 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1192 recover();
1193 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001194
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001195 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1196 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1197 {
1198 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1199 recover();
1200 }
1201 }
1202 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001203 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001204 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001205 {
Jamie Madill19571812013-08-12 15:26:34 -07001206 case EvqSmoothIn:
1207 case EvqSmoothOut:
1208 case EvqVertexOut:
1209 case EvqFragmentIn:
1210 case EvqCentroidOut:
1211 case EvqCentroidIn:
1212 if (typeSpecifier.type == EbtBool)
1213 {
1214 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1215 recover();
1216 }
1217 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1218 {
1219 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1220 recover();
1221 }
1222 break;
1223
1224 case EvqVertexIn:
1225 case EvqFragmentOut:
1226 case EvqFlatIn:
1227 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001228 if (typeSpecifier.type == EbtBool)
1229 {
1230 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1231 recover();
1232 }
1233 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001234
Jamie Madillb120eac2013-06-12 14:08:13 -04001235 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001236 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001237 }
1238
1239 return returnType;
1240}
1241
Olli Etuahofa33d582015-04-09 14:33:12 +03001242TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1243 const TSourceLoc &identifierOrTypeLocation,
1244 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001245{
Olli Etuahofa33d582015-04-09 14:33:12 +03001246 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001247
Olli Etuahofa33d582015-04-09 14:33:12 +03001248 mDeferredSingleDeclarationErrorCheck = (identifier == "");
1249
1250 if (!mDeferredSingleDeclarationErrorCheck)
Jamie Madill60ed9812013-06-06 11:56:46 -04001251 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001252 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001253 recover();
1254
Olli Etuahofa33d582015-04-09 14:33:12 +03001255 if (nonInitConstErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001256 recover();
1257
Olli Etuaho2935c582015-04-08 14:32:06 +03001258 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001259 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001260 recover();
1261
1262 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001263 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001264 }
1265
Olli Etuahoe7847b02015-03-16 11:56:12 +02001266 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001267}
1268
Olli Etuahoe7847b02015-03-16 11:56:12 +02001269TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1270 const TSourceLoc &identifierLocation,
1271 const TString &identifier,
1272 const TSourceLoc &indexLocation,
1273 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001274{
Olli Etuahofa33d582015-04-09 14:33:12 +03001275 mDeferredSingleDeclarationErrorCheck = false;
1276
1277 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001278 recover();
1279
Olli Etuaho3739d232015-04-08 12:23:44 +03001280 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001281 recover();
1282
1283 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1284 {
1285 recover();
1286 }
1287
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001288 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001289
1290 int size;
1291 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1292 {
1293 recover();
1294 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001295 // Make the type an array even if size check failed.
1296 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1297 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001298
Olli Etuaho2935c582015-04-08 14:32:06 +03001299 TVariable *variable = nullptr;
1300 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001301 recover();
1302
Olli Etuahoe7847b02015-03-16 11:56:12 +02001303 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001304 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001305 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001306
Olli Etuahoe7847b02015-03-16 11:56:12 +02001307 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001308}
1309
Olli Etuahoe7847b02015-03-16 11:56:12 +02001310TIntermAggregate *TParseContext::parseSingleInitDeclaration(TPublicType &publicType,
1311 const TSourceLoc &identifierLocation,
1312 const TString &identifier,
1313 const TSourceLoc &initLocation,
1314 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001315{
Olli Etuahofa33d582015-04-09 14:33:12 +03001316 mDeferredSingleDeclarationErrorCheck = false;
1317
1318 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001319 recover();
1320
Olli Etuahoe7847b02015-03-16 11:56:12 +02001321 TIntermNode *intermNode = nullptr;
1322 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001323 {
1324 //
1325 // Build intermediate representation
1326 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001327 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001328 }
1329 else
1330 {
1331 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001332 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001333 }
1334}
1335
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001336TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1337 const TSourceLoc &identifierLocation,
1338 const TString &identifier,
1339 const TSourceLoc &indexLocation,
1340 TIntermTyped *indexExpression,
1341 const TSourceLoc &initLocation,
1342 TIntermTyped *initializer)
1343{
1344 mDeferredSingleDeclarationErrorCheck = false;
1345
1346 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1347 recover();
1348
1349 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1350 {
1351 recover();
1352 }
1353
1354 TPublicType arrayType(publicType);
1355
1356 int size;
1357 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1358 {
1359 recover();
1360 }
1361 // Make the type an array even if size check failed.
1362 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1363 arrayType.setArraySize(size);
1364
1365 // initNode will correspond to the whole of "type b[n] = initializer".
1366 TIntermNode *initNode = nullptr;
1367 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1368 {
1369 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1370 }
1371 else
1372 {
1373 recover();
1374 return nullptr;
1375 }
1376}
1377
Olli Etuahoe7847b02015-03-16 11:56:12 +02001378TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001379 const TSourceLoc &identifierLoc,
1380 const TString *identifier,
1381 const TSymbol *symbol)
1382{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001383 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001384 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1385 {
1386 recover();
1387 }
1388
1389 if (!symbol)
1390 {
1391 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1392 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001393 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001394 }
1395 else
1396 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001397 const TString kGlFrontFacing("gl_FrontFacing");
1398 if (*identifier == kGlFrontFacing)
1399 {
1400 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1401 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001402 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001403 }
Jamie Madill2c433252014-12-03 12:36:54 -05001404 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001405 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1406 ASSERT(variable);
1407 const TType &type = variable->getType();
1408 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1409 *identifier, type, identifierLoc);
1410
1411 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1412 aggregate->setOp(EOpInvariantDeclaration);
1413 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001414 }
1415}
1416
Olli Etuahoe7847b02015-03-16 11:56:12 +02001417TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1418 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001419{
Olli Etuahofa33d582015-04-09 14:33:12 +03001420 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1421 if (mDeferredSingleDeclarationErrorCheck)
1422 {
1423 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1424 recover();
1425 mDeferredSingleDeclarationErrorCheck = false;
1426 }
1427
Jamie Madill0bd18df2013-06-20 11:55:52 -04001428 if (locationDeclaratorListCheck(identifierLocation, publicType))
1429 recover();
1430
Olli Etuaho3739d232015-04-08 12:23:44 +03001431 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001432 recover();
1433
Olli Etuaho2935c582015-04-08 14:32:06 +03001434 TVariable *variable = nullptr;
1435 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001436 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001437
1438 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1439 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001440 symbol->setId(variable->getUniqueId());
1441
Olli Etuahoe7847b02015-03-16 11:56:12 +02001442 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001443}
1444
Olli Etuahoe7847b02015-03-16 11:56:12 +02001445TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1446 const TSourceLoc &identifierLocation, const TString &identifier,
1447 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001448{
Olli Etuahofa33d582015-04-09 14:33:12 +03001449 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1450 if (mDeferredSingleDeclarationErrorCheck)
1451 {
1452 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1453 recover();
1454 mDeferredSingleDeclarationErrorCheck = false;
1455 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001456
Jamie Madill0bd18df2013-06-20 11:55:52 -04001457 if (locationDeclaratorListCheck(identifierLocation, publicType))
1458 recover();
1459
Olli Etuaho3739d232015-04-08 12:23:44 +03001460 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001461 recover();
1462
1463 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1464 {
1465 recover();
1466 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001467 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001468 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001469 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001470 int size;
1471 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001472 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001473 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001474 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001475 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001476
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001477 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001478 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001479 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001480
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001481 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1482 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001483 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001484
1485 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001486 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001487
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001488 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001489}
1490
Olli Etuahoe7847b02015-03-16 11:56:12 +02001491TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1492 const TSourceLoc &identifierLocation, const TString &identifier,
1493 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001494{
Olli Etuahofa33d582015-04-09 14:33:12 +03001495 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1496 if (mDeferredSingleDeclarationErrorCheck)
1497 {
1498 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1499 recover();
1500 mDeferredSingleDeclarationErrorCheck = false;
1501 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001502
Jamie Madill0bd18df2013-06-20 11:55:52 -04001503 if (locationDeclaratorListCheck(identifierLocation, publicType))
1504 recover();
1505
Olli Etuahoe7847b02015-03-16 11:56:12 +02001506 TIntermNode *intermNode = nullptr;
1507 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001508 {
1509 //
1510 // build the intermediate representation
1511 //
1512 if (intermNode)
1513 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001514 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001515 }
1516 else
1517 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001518 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001519 }
1520 }
1521 else
1522 {
1523 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001524 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001525 }
1526}
1527
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001528TIntermAggregate *TParseContext::parseArrayInitDeclarator(TPublicType &publicType,
1529 TIntermAggregate *aggregateDeclaration,
1530 const TSourceLoc& identifierLocation,
1531 const TString &identifier,
1532 const TSourceLoc& indexLocation,
1533 TIntermTyped *indexExpression,
1534 const TSourceLoc &initLocation, TIntermTyped *initializer)
1535{
1536 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1537 if (mDeferredSingleDeclarationErrorCheck)
1538 {
1539 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1540 recover();
1541 mDeferredSingleDeclarationErrorCheck = false;
1542 }
1543
1544 if (locationDeclaratorListCheck(identifierLocation, publicType))
1545 recover();
1546
1547 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1548 {
1549 recover();
1550 }
1551
1552 TPublicType arrayType(publicType);
1553
1554 int size;
1555 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1556 {
1557 recover();
1558 }
1559 // Make the type an array even if size check failed.
1560 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1561 arrayType.setArraySize(size);
1562
1563 // initNode will correspond to the whole of "b[n] = initializer".
1564 TIntermNode *initNode = nullptr;
1565 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1566 {
1567 if (initNode)
1568 {
1569 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1570 }
1571 else
1572 {
1573 return aggregateDeclaration;
1574 }
1575 }
1576 else
1577 {
1578 recover();
1579 return nullptr;
1580 }
1581}
1582
Jamie Madilla295edf2013-06-06 11:56:48 -04001583void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1584{
1585 if (typeQualifier.qualifier != EvqUniform)
1586 {
1587 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1588 recover();
1589 return;
1590 }
1591
1592 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1593 ASSERT(!layoutQualifier.isEmpty());
1594
1595 if (shaderVersion < 300)
1596 {
1597 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1598 recover();
1599 return;
1600 }
1601
1602 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1603 {
1604 recover();
1605 return;
1606 }
1607
Jamie Madill099c0f32013-06-20 11:55:52 -04001608 if (layoutQualifier.matrixPacking != EmpUnspecified)
1609 {
1610 defaultMatrixPacking = layoutQualifier.matrixPacking;
1611 }
1612
Geoff Langc6856732014-02-11 09:38:55 -05001613 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001614 {
1615 defaultBlockStorage = layoutQualifier.blockStorage;
1616 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001617}
1618
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001619TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1620{
1621 TOperator op = EOpNull;
1622 if (publicType.userDef)
1623 {
1624 op = EOpConstructStruct;
1625 }
1626 else
1627 {
1628 switch (publicType.type)
1629 {
1630 case EbtFloat:
1631 if (publicType.isMatrix())
1632 {
1633 // TODO: non-square matrices
1634 switch(publicType.getCols())
1635 {
Jamie Madill28b97422013-07-08 14:01:38 -04001636 case 2: op = EOpConstructMat2; break;
1637 case 3: op = EOpConstructMat3; break;
1638 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001639 }
1640 }
1641 else
1642 {
1643 switch(publicType.getNominalSize())
1644 {
Jamie Madill28b97422013-07-08 14:01:38 -04001645 case 1: op = EOpConstructFloat; break;
1646 case 2: op = EOpConstructVec2; break;
1647 case 3: op = EOpConstructVec3; break;
1648 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001649 }
1650 }
1651 break;
1652
1653 case EbtInt:
1654 switch(publicType.getNominalSize())
1655 {
Jamie Madill28b97422013-07-08 14:01:38 -04001656 case 1: op = EOpConstructInt; break;
1657 case 2: op = EOpConstructIVec2; break;
1658 case 3: op = EOpConstructIVec3; break;
1659 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001660 }
1661 break;
1662
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001663 case EbtUInt:
1664 switch(publicType.getNominalSize())
1665 {
Jamie Madill28b97422013-07-08 14:01:38 -04001666 case 1: op = EOpConstructUInt; break;
1667 case 2: op = EOpConstructUVec2; break;
1668 case 3: op = EOpConstructUVec3; break;
1669 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001670 }
1671 break;
1672
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001673 case EbtBool:
1674 switch(publicType.getNominalSize())
1675 {
Jamie Madill28b97422013-07-08 14:01:38 -04001676 case 1: op = EOpConstructBool; break;
1677 case 2: op = EOpConstructBVec2; break;
1678 case 3: op = EOpConstructBVec3; break;
1679 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001680 }
1681 break;
1682
1683 default: break;
1684 }
1685
1686 if (op == EOpNull)
1687 {
1688 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1689 recover();
1690 publicType.type = EbtFloat;
1691 op = EOpConstructFloat;
1692 }
1693 }
1694
1695 TString tempString;
1696 TType type(publicType);
1697 return new TFunction(&tempString, type, op);
1698}
1699
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700// This function is used to test for the correctness of the parameters passed to various constructor functions
1701// and also convert them to the right datatype if it is allowed and required.
1702//
1703// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1704//
Olli Etuaho21203702014-11-13 16:16:21 +02001705TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001706{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001707 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001709 if (!aggregateArguments)
1710 {
1711 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001712 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001713 }
1714
Olli Etuahof40319e2015-03-10 14:33:00 +02001715 if (type->isArray())
1716 {
1717 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1718 TIntermSequence *args = aggregateArguments->getSequence();
1719 for (size_t i = 0; i < args->size(); i++)
1720 {
1721 const TType &argType = (*args)[i]->getAsTyped()->getType();
1722 // It has already been checked that the argument is not an array.
1723 ASSERT(!argType.isArray());
1724 if (!argType.sameElementType(*type))
1725 {
1726 error(line, "Array constructor argument has an incorrect type", "Error");
1727 recover();
1728 return nullptr;
1729 }
1730 }
1731 }
1732 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001733 {
1734 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001735 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001737 for (size_t i = 0; i < fields.size(); i++)
1738 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001739 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001740 {
1741 error(line, "Structure constructor arguments do not match structure fields", "Error");
1742 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001744 return 0;
1745 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001746 }
1747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001749 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001750 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1751 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001752 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001753 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001754 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756
Olli Etuaho21203702014-11-13 16:16:21 +02001757 // Structs should not be precision qualified, the individual members may be.
1758 // Built-in types on the other hand should be precision qualified.
1759 if (op != EOpConstructStruct)
1760 {
1761 constructor->setPrecisionFromChildren();
1762 type->setPrecision(constructor->getPrecision());
1763 }
1764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766}
1767
1768TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1769{
Olli Etuahof40319e2015-03-10 14:33:00 +02001770 // TODO: Add support for folding array constructors
1771 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001772 aggrNode->setType(type);
1773 if (canBeFolded) {
1774 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001775 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001776 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001777 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001778 }
1779 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001780 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001781 }
1782 if (returnVal)
1783 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001785 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001788 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789}
1790
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791//
1792// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1793// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1794// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1795// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1796// a constant matrix.
1797//
Jamie Madill075edd82013-07-08 13:30:19 -04001798TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 TIntermTyped* typedNode;
1801 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001803 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 if (tempConstantNode) {
1805 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001807 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001808 return node;
1809 }
1810 } 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 +00001811 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001812 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001814 return 0;
1815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001817 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001820 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001821 std::stringstream extraInfoStream;
1822 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1823 std::string extraInfo = extraInfoStream.str();
1824 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 recover();
1826 fields.offsets[i] = 0;
1827 }
1828
1829 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 }
1832 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1833 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834}
1835
1836//
1837// This function returns the column being accessed from a constant matrix. The values are retrieved from
1838// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1839// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1840// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1841//
Jamie Madill075edd82013-07-08 13:30:19 -04001842TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 TIntermTyped* typedNode;
1845 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001847 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001848 std::stringstream extraInfoStream;
1849 extraInfoStream << "matrix field selection out of range '" << index << "'";
1850 std::string extraInfo = extraInfoStream.str();
1851 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001852 recover();
1853 index = 0;
1854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001857 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001858 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001859 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1860 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001861 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001864 return 0;
1865 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001867 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868}
1869
1870
1871//
1872// This function returns an element of an array accessed from a constant array. The values are retrieved from
1873// the symbol table and parse-tree is built for the type of the element. The input
1874// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1875// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1876//
Jamie Madill075edd82013-07-08 13:30:19 -04001877TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001878{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001879 TIntermTyped* typedNode;
1880 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1881 TType arrayElementType = node->getType();
1882 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001884 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001885 std::stringstream extraInfoStream;
1886 extraInfoStream << "array field selection out of range '" << index << "'";
1887 std::string extraInfo = extraInfoStream.str();
1888 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001889 recover();
1890 index = 0;
1891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001893 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001894 size_t arrayElementSize = arrayElementType.getObjectSize();
1895 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1896 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001897 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001898 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001899 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001901 return 0;
1902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001904 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905}
1906
1907
1908//
1909// This function returns the value of a particular field inside a constant structure from the symbol table.
1910// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1911// function and returns the parse-tree with the values of the embedded/nested struct.
1912//
Jamie Madill075edd82013-07-08 13:30:19 -04001913TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001916 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 for (size_t index = 0; index < fields.size(); ++index) {
1919 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001920 break;
1921 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001923 }
1924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925
Jamie Madill94bf7f22013-07-08 13:31:15 -04001926 TIntermTyped *typedNode;
1927 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001928 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001929 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001931 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1932 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001933 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001934 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001936 return 0;
1937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940}
1941
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001942//
1943// Interface/uniform blocks
1944//
Jamie Madill98493dd2013-07-08 14:39:03 -04001945TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1946 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001947{
1948 if (reservedErrorCheck(nameLine, blockName))
1949 recover();
1950
1951 if (typeQualifier.qualifier != EvqUniform)
1952 {
1953 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1954 recover();
1955 }
1956
Jamie Madill099c0f32013-06-20 11:55:52 -04001957 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1958 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001959 {
1960 recover();
1961 }
1962
Jamie Madill099c0f32013-06-20 11:55:52 -04001963 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1964 {
1965 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1966 }
1967
Jamie Madill1566ef72013-06-20 11:55:54 -04001968 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1969 {
1970 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1971 }
1972
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001973 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001974 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001975 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1976 recover();
1977 }
1978
Jamie Madill98493dd2013-07-08 14:39:03 -04001979 // check for sampler types and apply layout qualifiers
1980 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1981 TField* field = (*fieldList)[memberIndex];
1982 TType* fieldType = field->type();
1983 if (IsSampler(fieldType->getBasicType())) {
1984 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001985 recover();
1986 }
1987
Jamie Madill98493dd2013-07-08 14:39:03 -04001988 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001989 switch (qualifier)
1990 {
1991 case EvqGlobal:
1992 case EvqUniform:
1993 break;
1994 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001995 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001996 recover();
1997 break;
1998 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001999
2000 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002001 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2002 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002003 {
2004 recover();
2005 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002006
Jamie Madill98493dd2013-07-08 14:39:03 -04002007 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002008 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002009 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002010 recover();
2011 }
2012
Jamie Madill98493dd2013-07-08 14:39:03 -04002013 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002014 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002015 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002016 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002017 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002018 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002019 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002020 recover();
2021 }
2022
Jamie Madill98493dd2013-07-08 14:39:03 -04002023 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002024 }
2025
Jamie Madill98493dd2013-07-08 14:39:03 -04002026 // add array index
2027 int arraySize = 0;
2028 if (arrayIndex != NULL)
2029 {
2030 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2031 recover();
2032 }
2033
2034 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2035 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002036
2037 TString symbolName = "";
2038 int symbolId = 0;
2039
Jamie Madill98493dd2013-07-08 14:39:03 -04002040 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002041 {
2042 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002043 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2044 {
2045 TField* field = (*fieldList)[memberIndex];
2046 TType* fieldType = field->type();
2047
2048 // set parent pointer of the field variable
2049 fieldType->setInterfaceBlock(interfaceBlock);
2050
2051 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2052 fieldVariable->setQualifier(typeQualifier.qualifier);
2053
Nicolas Capensadfffe42014-06-17 02:13:36 -04002054 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002055 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002056 recover();
2057 }
2058 }
2059 }
2060 else
2061 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002062 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002064 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002065
Nicolas Capensadfffe42014-06-17 02:13:36 -04002066 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002067 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002068 recover();
2069 }
2070
2071 symbolId = instanceTypeDef->getUniqueId();
2072 symbolName = instanceTypeDef->getName();
2073 }
2074
Jamie Madill98493dd2013-07-08 14:39:03 -04002075 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002076 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002077
2078 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002079 return aggregate;
2080}
2081
Jamie Madill075edd82013-07-08 13:30:19 -04002082bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002083{
2084 ++structNestingLevel;
2085
2086 // Embedded structure definitions are not supported per GLSL ES spec.
2087 // They aren't allowed in GLSL either, but we need to detect this here
2088 // so we don't rely on the GLSL compiler to catch it.
2089 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002090 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002091 return true;
2092 }
2093
2094 return false;
2095}
2096
2097void TParseContext::exitStructDeclaration()
2098{
2099 --structNestingLevel;
2100}
2101
2102namespace {
2103
2104const int kWebGLMaxStructNesting = 4;
2105
2106} // namespace
2107
Jamie Madill98493dd2013-07-08 14:39:03 -04002108bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002109{
Jamie Madill5508f392014-02-20 13:31:36 -05002110 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002111 return false;
2112 }
2113
Jamie Madill98493dd2013-07-08 14:39:03 -04002114 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002115 return false;
2116 }
2117
2118 // We're already inside a structure definition at this point, so add
2119 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002120 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002121 std::stringstream reasonStream;
2122 reasonStream << "Reference of struct type "
2123 << field.type()->getStruct()->name().c_str()
2124 << " exceeds maximum allowed nesting level of "
2125 << kWebGLMaxStructNesting;
2126 std::string reason = reasonStream.str();
2127 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002128 return true;
2129 }
2130
2131 return false;
2132}
2133
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002134//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002135// Parse an array index expression
2136//
Jamie Madill075edd82013-07-08 13:30:19 -04002137TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002138{
2139 TIntermTyped *indexedExpression = NULL;
2140
2141 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2142 {
2143 if (baseExpression->getAsSymbolNode())
2144 {
2145 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2146 }
2147 else
2148 {
2149 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2150 }
2151 recover();
2152 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002153
Jamie Madill21c1e452014-12-29 11:33:41 -05002154 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2155
2156 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002157 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002158 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002159 if (index < 0)
2160 {
2161 std::stringstream infoStream;
2162 infoStream << index;
2163 std::string info = infoStream.str();
2164 error(location, "negative index", info.c_str());
2165 recover();
2166 index = 0;
2167 }
2168 if (baseExpression->getType().getQualifier() == EvqConst)
2169 {
2170 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002171 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002172 // constant folding for arrays
2173 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002174 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002175 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002176 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002177 // constant folding for vectors
2178 TVectorFields fields;
2179 fields.num = 1;
2180 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2181 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2182 }
2183 else if (baseExpression->isMatrix())
2184 {
2185 // constant folding for matrices
2186 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002187 }
2188 }
2189 else
2190 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002191 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002192 {
Jamie Madill18464b52013-07-08 14:01:55 -04002193 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002194 {
2195 std::stringstream extraInfoStream;
2196 extraInfoStream << "array index out of range '" << index << "'";
2197 std::string extraInfo = extraInfoStream.str();
2198 error(location, "", "[", extraInfo.c_str());
2199 recover();
2200 index = baseExpression->getType().getArraySize() - 1;
2201 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002202 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2203 {
2204 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2205 recover();
2206 index = 0;
2207 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002208 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002209 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002210 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002211 std::stringstream extraInfoStream;
2212 extraInfoStream << "field selection out of range '" << index << "'";
2213 std::string extraInfo = extraInfoStream.str();
2214 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002215 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002216 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002217 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002218
Jamie Madill21c1e452014-12-29 11:33:41 -05002219 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002220 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002221 }
2222 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002223 else
2224 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002225 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002226 {
Jamie Madill19571812013-08-12 15:26:34 -07002227 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002228 recover();
2229 }
Jamie Madill19571812013-08-12 15:26:34 -07002230 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002231 {
Jamie Madill19571812013-08-12 15:26:34 -07002232 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002233 recover();
2234 }
2235
2236 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2237 }
2238
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002239 if (indexedExpression == 0)
2240 {
2241 ConstantUnion *unionArray = new ConstantUnion[1];
2242 unionArray->setFConst(0.0f);
2243 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2244 }
2245 else if (baseExpression->isArray())
2246 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002247 const TType &baseType = baseExpression->getType();
2248 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002249 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002250 TType copyOfType(baseType.getStruct());
2251 indexedExpression->setType(copyOfType);
2252 }
2253 else if (baseType.isInterfaceBlock())
2254 {
2255 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002256 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002257 }
2258 else
2259 {
Minmin Gong794e0002015-04-07 18:31:54 -07002260 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2261 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002262 }
2263
2264 if (baseExpression->getType().getQualifier() == EvqConst)
2265 {
2266 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2267 }
2268 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002269 else if (baseExpression->isMatrix())
2270 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002271 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002272 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002273 }
2274 else if (baseExpression->isVector())
2275 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002276 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2277 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002278 }
2279 else
2280 {
2281 indexedExpression->setType(baseExpression->getType());
2282 }
2283
2284 return indexedExpression;
2285}
2286
Jamie Madill075edd82013-07-08 13:30:19 -04002287TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002288{
2289 TIntermTyped *indexedExpression = NULL;
2290
2291 if (baseExpression->isArray())
2292 {
2293 error(fieldLocation, "cannot apply dot operator to an array", ".");
2294 recover();
2295 }
2296
2297 if (baseExpression->isVector())
2298 {
2299 TVectorFields fields;
2300 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2301 {
2302 fields.num = 1;
2303 fields.offsets[0] = 0;
2304 recover();
2305 }
2306
2307 if (baseExpression->getType().getQualifier() == EvqConst)
2308 {
2309 // constant folding for vector fields
2310 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2311 if (indexedExpression == 0)
2312 {
2313 recover();
2314 indexedExpression = baseExpression;
2315 }
2316 else
2317 {
Minmin Gong794e0002015-04-07 18:31:54 -07002318 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002319 }
2320 }
2321 else
2322 {
2323 TString vectorString = fieldString;
2324 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2325 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002326 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002327 }
2328 }
2329 else if (baseExpression->isMatrix())
2330 {
2331 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002332 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002333 {
2334 fields.wholeRow = false;
2335 fields.wholeCol = false;
2336 fields.row = 0;
2337 fields.col = 0;
2338 recover();
2339 }
2340
2341 if (fields.wholeRow || fields.wholeCol)
2342 {
2343 error(dotLocation, " non-scalar fields not implemented yet", ".");
2344 recover();
2345 ConstantUnion *unionArray = new ConstantUnion[1];
2346 unionArray->setIConst(0);
2347 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2348 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002349 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2350 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002351 }
2352 else
2353 {
2354 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002355 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002356 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2357 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2358 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2359 }
2360 }
2361 else if (baseExpression->getBasicType() == EbtStruct)
2362 {
2363 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002364 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2365 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002366 {
2367 error(dotLocation, "structure has no fields", "Internal Error");
2368 recover();
2369 indexedExpression = baseExpression;
2370 }
2371 else
2372 {
2373 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002374 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002375 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002376 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002377 {
2378 fieldFound = true;
2379 break;
2380 }
2381 }
2382 if (fieldFound)
2383 {
2384 if (baseExpression->getType().getQualifier() == EvqConst)
2385 {
2386 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2387 if (indexedExpression == 0)
2388 {
2389 recover();
2390 indexedExpression = baseExpression;
2391 }
2392 else
2393 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002394 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002395 // change the qualifier of the return type, not of the structure field
2396 // as the structure definition is shared between various structures.
2397 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2398 }
2399 }
2400 else
2401 {
2402 ConstantUnion *unionArray = new ConstantUnion[1];
2403 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002404 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002405 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002406 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002407 }
2408 }
2409 else
2410 {
2411 error(dotLocation, " no such field in structure", fieldString.c_str());
2412 recover();
2413 indexedExpression = baseExpression;
2414 }
2415 }
2416 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002417 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002418 {
2419 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002420 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2421 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002422 {
2423 error(dotLocation, "interface block has no fields", "Internal Error");
2424 recover();
2425 indexedExpression = baseExpression;
2426 }
2427 else
2428 {
2429 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002430 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002431 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002432 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002433 {
2434 fieldFound = true;
2435 break;
2436 }
2437 }
2438 if (fieldFound)
2439 {
2440 ConstantUnion *unionArray = new ConstantUnion[1];
2441 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002442 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002443 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002444 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002445 }
2446 else
2447 {
2448 error(dotLocation, " no such field in interface block", fieldString.c_str());
2449 recover();
2450 indexedExpression = baseExpression;
2451 }
2452 }
2453 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002454 else
2455 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002456 if (shaderVersion < 300)
2457 {
2458 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2459 }
2460 else
2461 {
2462 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2463 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002464 recover();
2465 indexedExpression = baseExpression;
2466 }
2467
2468 return indexedExpression;
2469}
2470
Jamie Madill075edd82013-07-08 13:30:19 -04002471TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002472{
Jamie Madilla5efff92013-06-06 11:56:47 -04002473 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002474
Jamie Madilla5efff92013-06-06 11:56:47 -04002475 qualifier.location = -1;
2476 qualifier.matrixPacking = EmpUnspecified;
2477 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002478
2479 if (qualifierType == "shared")
2480 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002481 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002482 }
2483 else if (qualifierType == "packed")
2484 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002485 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002486 }
2487 else if (qualifierType == "std140")
2488 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002489 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002490 }
2491 else if (qualifierType == "row_major")
2492 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002493 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002494 }
2495 else if (qualifierType == "column_major")
2496 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002497 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002498 }
2499 else if (qualifierType == "location")
2500 {
2501 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2502 recover();
2503 }
2504 else
2505 {
2506 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2507 recover();
2508 }
2509
Jamie Madilla5efff92013-06-06 11:56:47 -04002510 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002511}
2512
Jamie Madill075edd82013-07-08 13:30:19 -04002513TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002514{
Jamie Madilla5efff92013-06-06 11:56:47 -04002515 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002516
Jamie Madilla5efff92013-06-06 11:56:47 -04002517 qualifier.location = -1;
2518 qualifier.matrixPacking = EmpUnspecified;
2519 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002520
2521 if (qualifierType != "location")
2522 {
2523 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2524 recover();
2525 }
2526 else
2527 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002528 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002529 if (intValue < 0)
2530 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002531 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002532 recover();
2533 }
2534 else
2535 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002536 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002537 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002538 }
2539
Jamie Madilla5efff92013-06-06 11:56:47 -04002540 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002541}
2542
Jamie Madilla5efff92013-06-06 11:56:47 -04002543TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002544{
Jamie Madilla5efff92013-06-06 11:56:47 -04002545 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002546
Jamie Madilla5efff92013-06-06 11:56:47 -04002547 if (rightQualifier.location != -1)
2548 {
2549 joinedQualifier.location = rightQualifier.location;
2550 }
2551 if (rightQualifier.matrixPacking != EmpUnspecified)
2552 {
2553 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2554 }
2555 if (rightQualifier.blockStorage != EbsUnspecified)
2556 {
2557 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2558 }
2559
2560 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002561}
2562
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002563TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2564 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2565{
2566 TQualifier mergedQualifier = EvqSmoothIn;
2567
Jamie Madill19571812013-08-12 15:26:34 -07002568 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002569 if (interpolationQualifier == EvqSmooth)
2570 mergedQualifier = EvqSmoothIn;
2571 else if (interpolationQualifier == EvqFlat)
2572 mergedQualifier = EvqFlatIn;
2573 else UNREACHABLE();
2574 }
2575 else if (storageQualifier == EvqCentroidIn) {
2576 if (interpolationQualifier == EvqSmooth)
2577 mergedQualifier = EvqCentroidIn;
2578 else if (interpolationQualifier == EvqFlat)
2579 mergedQualifier = EvqFlatIn;
2580 else UNREACHABLE();
2581 }
Jamie Madill19571812013-08-12 15:26:34 -07002582 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002583 if (interpolationQualifier == EvqSmooth)
2584 mergedQualifier = EvqSmoothOut;
2585 else if (interpolationQualifier == EvqFlat)
2586 mergedQualifier = EvqFlatOut;
2587 else UNREACHABLE();
2588 }
2589 else if (storageQualifier == EvqCentroidOut) {
2590 if (interpolationQualifier == EvqSmooth)
2591 mergedQualifier = EvqCentroidOut;
2592 else if (interpolationQualifier == EvqFlat)
2593 mergedQualifier = EvqFlatOut;
2594 else UNREACHABLE();
2595 }
2596 else {
2597 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2598 recover();
2599
2600 mergedQualifier = storageQualifier;
2601 }
2602
2603 TPublicType type;
2604 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2605 return type;
2606}
2607
Jamie Madill98493dd2013-07-08 14:39:03 -04002608TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002609{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002610 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2611 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002612 recover();
2613 }
2614
Jamie Madill98493dd2013-07-08 14:39:03 -04002615 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002616 //
2617 // Careful not to replace already known aspects of type, like array-ness
2618 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002619 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002620 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002621 type->setPrimarySize(typeSpecifier.primarySize);
2622 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002623 type->setPrecision(typeSpecifier.precision);
2624 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002625 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002626
2627 // don't allow arrays of arrays
2628 if (type->isArray()) {
2629 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2630 recover();
2631 }
2632 if (typeSpecifier.array)
2633 type->setArraySize(typeSpecifier.arraySize);
2634 if (typeSpecifier.userDef) {
2635 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002636 }
2637
Jamie Madill98493dd2013-07-08 14:39:03 -04002638 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002639 recover();
2640 }
2641 }
2642
Jamie Madill98493dd2013-07-08 14:39:03 -04002643 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002644}
2645
Jamie Madill98493dd2013-07-08 14:39:03 -04002646TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002647{
Jamie Madill98493dd2013-07-08 14:39:03 -04002648 TStructure* structure = new TStructure(structName, fieldList);
2649 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002650
Jamie Madill9b820842015-02-12 10:40:10 -05002651 // Store a bool in the struct if we're at global scope, to allow us to
2652 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002653 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002654 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002655
Jamie Madill98493dd2013-07-08 14:39:03 -04002656 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002657 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002658 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002659 {
2660 recover();
2661 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002663 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002664 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002665 recover();
2666 }
2667 }
2668
2669 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002670 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002671 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002672 const TField &field = *(*fieldList)[typeListIndex];
2673 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002674 switch (qualifier)
2675 {
2676 case EvqGlobal:
2677 case EvqTemporary:
2678 break;
2679 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002680 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002681 recover();
2682 break;
2683 }
2684 }
2685
2686 TPublicType publicType;
2687 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002688 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689 exitStructDeclaration();
2690
2691 return publicType;
2692}
2693
Olli Etuahoa3a36662015-02-17 13:46:51 +02002694TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2695{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002696 TBasicType switchType = init->getBasicType();
2697 if ((switchType != EbtInt && switchType != EbtUInt) ||
2698 init->isMatrix() ||
2699 init->isArray() ||
2700 init->isVector())
2701 {
2702 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2703 recover();
2704 return nullptr;
2705 }
2706
Olli Etuahoac5274d2015-02-20 10:19:08 +02002707 if (statementList)
2708 {
2709 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2710 {
2711 recover();
2712 return nullptr;
2713 }
2714 }
2715
Olli Etuahoa3a36662015-02-17 13:46:51 +02002716 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2717 if (node == nullptr)
2718 {
2719 error(loc, "erroneous switch statement", "switch");
2720 recover();
2721 return nullptr;
2722 }
2723 return node;
2724}
2725
2726TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2727{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002728 if (mSwitchNestingLevel == 0)
2729 {
2730 error(loc, "case labels need to be inside switch statements", "case");
2731 recover();
2732 return nullptr;
2733 }
2734 if (condition == nullptr)
2735 {
2736 error(loc, "case label must have a condition", "case");
2737 recover();
2738 return nullptr;
2739 }
2740 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2741 condition->isMatrix() ||
2742 condition->isArray() ||
2743 condition->isVector())
2744 {
2745 error(condition->getLine(), "case label must be a scalar integer", "case");
2746 recover();
2747 }
2748 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2749 if (conditionConst == nullptr)
2750 {
2751 error(condition->getLine(), "case label must be constant", "case");
2752 recover();
2753 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002754 TIntermCase *node = intermediate.addCase(condition, loc);
2755 if (node == nullptr)
2756 {
2757 error(loc, "erroneous case statement", "case");
2758 recover();
2759 return nullptr;
2760 }
2761 return node;
2762}
2763
2764TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2765{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002766 if (mSwitchNestingLevel == 0)
2767 {
2768 error(loc, "default labels need to be inside switch statements", "default");
2769 recover();
2770 return nullptr;
2771 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002772 TIntermCase *node = intermediate.addCase(nullptr, loc);
2773 if (node == nullptr)
2774 {
2775 error(loc, "erroneous default statement", "default");
2776 recover();
2777 return nullptr;
2778 }
2779 return node;
2780}
2781
Olli Etuahof6c694b2015-03-26 14:50:53 +02002782TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2783 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002784{
2785 if (child == nullptr)
2786 {
2787 return nullptr;
2788 }
2789
2790 switch (op)
2791 {
2792 case EOpLogicalNot:
2793 if (child->getBasicType() != EbtBool ||
2794 child->isMatrix() ||
2795 child->isArray() ||
2796 child->isVector())
2797 {
2798 return nullptr;
2799 }
2800 break;
2801 case EOpBitwiseNot:
2802 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2803 child->isMatrix() ||
2804 child->isArray())
2805 {
2806 return nullptr;
2807 }
2808 break;
2809 case EOpPostIncrement:
2810 case EOpPreIncrement:
2811 case EOpPostDecrement:
2812 case EOpPreDecrement:
2813 case EOpNegative:
2814 case EOpPositive:
2815 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002816 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002817 child->isArray())
2818 {
2819 return nullptr;
2820 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002821 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002822 default:
2823 break;
2824 }
2825
Olli Etuahof6c694b2015-03-26 14:50:53 +02002826 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002827}
2828
Olli Etuaho09b22472015-02-11 11:47:26 +02002829TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2830{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002831 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002832 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002833 {
2834 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2835 recover();
2836 return child;
2837 }
2838 return node;
2839}
2840
2841TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2842{
2843 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2844 recover();
2845 return addUnaryMath(op, child, loc);
2846}
2847
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002848bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002849 const TSourceLoc &loc)
2850{
2851 if (left->isArray() || right->isArray())
2852 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002853 if (shaderVersion < 300)
2854 {
2855 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2856 return false;
2857 }
2858
2859 if (left->isArray() != right->isArray())
2860 {
2861 error(loc, "array / non-array mismatch", GetOperatorString(op));
2862 return false;
2863 }
2864
2865 switch (op)
2866 {
2867 case EOpEqual:
2868 case EOpNotEqual:
2869 case EOpAssign:
2870 case EOpInitialize:
2871 break;
2872 default:
2873 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2874 return false;
2875 }
2876 if (left->getArraySize() != right->getArraySize())
2877 {
2878 error(loc, "array size mismatch", GetOperatorString(op));
2879 return false;
2880 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002881 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002882
2883 // Check ops which require integer / ivec parameters
2884 bool isBitShift = false;
2885 switch (op)
2886 {
2887 case EOpBitShiftLeft:
2888 case EOpBitShiftRight:
2889 case EOpBitShiftLeftAssign:
2890 case EOpBitShiftRightAssign:
2891 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2892 // check that the basic type is an integer type.
2893 isBitShift = true;
2894 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2895 {
2896 return false;
2897 }
2898 break;
2899 case EOpBitwiseAnd:
2900 case EOpBitwiseXor:
2901 case EOpBitwiseOr:
2902 case EOpBitwiseAndAssign:
2903 case EOpBitwiseXorAssign:
2904 case EOpBitwiseOrAssign:
2905 // It is enough to check the type of only one operand, since later it
2906 // is checked that the operand types match.
2907 if (!IsInteger(left->getBasicType()))
2908 {
2909 return false;
2910 }
2911 break;
2912 default:
2913 break;
2914 }
2915
2916 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2917 // So the basic type should usually match.
2918 if (!isBitShift && left->getBasicType() != right->getBasicType())
2919 {
2920 return false;
2921 }
2922
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002923 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002924 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002925 switch(op)
2926 {
2927 case EOpAssign:
2928 case EOpInitialize:
2929 case EOpEqual:
2930 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002931 // ESSL 1.00 sections 5.7, 5.8, 5.9
2932 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2933 {
2934 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2935 return false;
2936 }
Olli Etuahoff699002015-03-23 14:38:42 +02002937 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2938 // we interpret the spec so that this extends to structs containing samplers,
2939 // similarly to ESSL 1.00 spec.
2940 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2941 left->getType().isStructureContainingSamplers())
2942 {
2943 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2944 return false;
2945 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002946 case EOpLessThan:
2947 case EOpGreaterThan:
2948 case EOpLessThanEqual:
2949 case EOpGreaterThanEqual:
2950 if ((left->getNominalSize() != right->getNominalSize()) ||
2951 (left->getSecondarySize() != right->getSecondarySize()))
2952 {
2953 return false;
2954 }
2955 default:
2956 break;
2957 }
2958
Olli Etuahod6b14282015-03-17 14:31:35 +02002959 return true;
2960}
2961
Olli Etuahofc1806e2015-03-17 13:03:11 +02002962TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2963 const TSourceLoc &loc)
2964{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002965 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002966 return nullptr;
2967
Olli Etuahofc1806e2015-03-17 13:03:11 +02002968 switch (op)
2969 {
2970 case EOpEqual:
2971 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002972 break;
2973 case EOpLessThan:
2974 case EOpGreaterThan:
2975 case EOpLessThanEqual:
2976 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002977 ASSERT(!left->isArray() && !right->isArray());
2978 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002979 left->getBasicType() == EbtStruct)
2980 {
2981 return nullptr;
2982 }
2983 break;
2984 case EOpLogicalOr:
2985 case EOpLogicalXor:
2986 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02002987 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002988 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02002989 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02002990 {
2991 return nullptr;
2992 }
2993 break;
2994 case EOpAdd:
2995 case EOpSub:
2996 case EOpDiv:
2997 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02002998 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002999 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3000 {
3001 return nullptr;
3002 }
3003 break;
3004 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003005 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003006 // Note that this is only for the % operator, not for mod()
3007 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3008 {
3009 return nullptr;
3010 }
3011 break;
3012 // Note that for bitwise ops, type checking is done in promote() to
3013 // share code between ops and compound assignment
3014 default:
3015 break;
3016 }
3017
Olli Etuahofc1806e2015-03-17 13:03:11 +02003018 return intermediate.addBinaryMath(op, left, right, loc);
3019}
3020
Olli Etuaho09b22472015-02-11 11:47:26 +02003021TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3022 const TSourceLoc &loc)
3023{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003024 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003025 if (node == 0)
3026 {
3027 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3028 recover();
3029 return left;
3030 }
3031 return node;
3032}
3033
3034TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3035 const TSourceLoc &loc)
3036{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003037 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003038 if (node == 0)
3039 {
3040 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3041 recover();
3042 ConstantUnion *unionArray = new ConstantUnion[1];
3043 unionArray->setBConst(false);
3044 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3045 }
3046 return node;
3047}
3048
Olli Etuahod6b14282015-03-17 14:31:35 +02003049TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3050 const TSourceLoc &loc)
3051{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003052 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003053 {
3054 return intermediate.addAssign(op, left, right, loc);
3055 }
3056 return nullptr;
3057}
3058
3059TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3060 const TSourceLoc &loc)
3061{
3062 TIntermTyped *node = createAssign(op, left, right, loc);
3063 if (node == nullptr)
3064 {
3065 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3066 recover();
3067 return left;
3068 }
3069 return node;
3070}
3071
Olli Etuaho49300862015-02-20 14:54:49 +02003072TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3073{
3074 switch (op)
3075 {
3076 case EOpContinue:
3077 if (mLoopNestingLevel <= 0)
3078 {
3079 error(loc, "continue statement only allowed in loops", "");
3080 recover();
3081 }
3082 break;
3083 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003084 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003085 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003086 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003087 recover();
3088 }
3089 break;
3090 case EOpReturn:
3091 if (currentFunctionType->getBasicType() != EbtVoid)
3092 {
3093 error(loc, "non-void function must return a value", "return");
3094 recover();
3095 }
3096 break;
3097 default:
3098 // No checks for discard
3099 break;
3100 }
3101 return intermediate.addBranch(op, loc);
3102}
3103
3104TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3105{
3106 ASSERT(op == EOpReturn);
3107 mFunctionReturnsValue = true;
3108 if (currentFunctionType->getBasicType() == EbtVoid)
3109 {
3110 error(loc, "void function cannot return a value", "return");
3111 recover();
3112 }
3113 else if (*currentFunctionType != returnValue->getType())
3114 {
3115 error(loc, "function return is not matching type:", "return");
3116 recover();
3117 }
3118 return intermediate.addBranch(op, returnValue, loc);
3119}
3120
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003121TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *node,
3122 const TSourceLoc &loc, bool *fatalError)
3123{
3124 *fatalError = false;
3125 TOperator op = fnCall->getBuiltInOp();
3126 TIntermTyped *callNode = nullptr;
3127
3128 if (op != EOpNull)
3129 {
3130 //
3131 // Then this should be a constructor.
3132 // Don't go through the symbol table for constructors.
3133 // Their parameters will be verified algorithmically.
3134 //
3135 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3136 if (!constructorErrorCheck(loc, node, *fnCall, op, &type))
3137 {
3138 //
3139 // It's a constructor, of type 'type'.
3140 //
3141 callNode = addConstructor(node, &type, op, fnCall, loc);
3142 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003143
3144 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003145 {
3146 recover();
3147 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3148 }
3149 callNode->setType(type);
3150 }
3151 else
3152 {
3153 //
3154 // Not a constructor. Find it in the symbol table.
3155 //
3156 const TFunction* fnCandidate;
3157 bool builtIn;
3158 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3159 if (fnCandidate)
3160 {
3161 //
3162 // A declared function.
3163 //
3164 if (builtIn && !fnCandidate->getExtension().empty() &&
3165 extensionErrorCheck(loc, fnCandidate->getExtension()))
3166 {
3167 recover();
3168 }
3169 op = fnCandidate->getBuiltInOp();
3170 if (builtIn && op != EOpNull)
3171 {
3172 //
3173 // A function call mapped to a built-in operation.
3174 //
3175 if (fnCandidate->getParamCount() == 1)
3176 {
3177 //
3178 // Treat it like a built-in unary operator.
3179 //
Olli Etuahof6c694b2015-03-26 14:50:53 +02003180 callNode = createUnaryMath(op, node->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003181 if (callNode == nullptr)
3182 {
3183 std::stringstream extraInfoStream;
3184 extraInfoStream << "built in unary operator function. Type: "
3185 << static_cast<TIntermTyped*>(node)->getCompleteString();
3186 std::string extraInfo = extraInfoStream.str();
3187 error(node->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3188 *fatalError = true;
3189 return nullptr;
3190 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003191 }
3192 else
3193 {
3194 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, op, loc);
3195 aggregate->setType(fnCandidate->getReturnType());
3196 aggregate->setPrecisionFromChildren();
3197 callNode = aggregate;
3198
3199 // Some built-in functions have out parameters too.
3200 functionCallLValueErrorCheck(fnCandidate, aggregate);
3201 }
3202 }
3203 else
3204 {
3205 // This is a real function call
3206
3207 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, EOpFunctionCall, loc);
3208 aggregate->setType(fnCandidate->getReturnType());
3209
3210 // this is how we know whether the given function is a builtIn function or a user defined function
3211 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3212 // if builtIn == true, it's definitely a builtIn function with EOpNull
3213 if (!builtIn)
3214 aggregate->setUserDefined();
3215 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003216 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003217
3218 // This needs to happen after the name is set
3219 if (builtIn)
3220 aggregate->setBuiltInFunctionPrecision();
3221
3222 callNode = aggregate;
3223
3224 functionCallLValueErrorCheck(fnCandidate, aggregate);
3225 }
3226 }
3227 else
3228 {
3229 // error message was put out by findFunction()
3230 // Put on a dummy node for error recovery
3231 ConstantUnion *unionArray = new ConstantUnion[1];
3232 unionArray->setFConst(0.0f);
3233 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3234 recover();
3235 }
3236 }
3237 delete fnCall;
3238 return callNode;
3239}
3240
Olli Etuaho49300862015-02-20 14:54:49 +02003241
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003243// Parse an array of strings using yyparse.
3244//
3245// Returns 0 for success.
3246//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003247int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003248 TParseContext* context) {
3249 if ((count == 0) || (string == NULL))
3250 return 1;
3251
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003252 if (glslang_initialize(context))
3253 return 1;
3254
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003255 int error = glslang_scan(count, string, length, context);
3256 if (!error)
3257 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003258
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003259 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003260
alokp@chromium.org6b495712012-06-29 00:06:58 +00003261 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003262}
3263
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003264
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003265