blob: 5a6b60afbdef0d66cc348b24af1cca5b3a8b7786 [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 Etuahoe7847b02015-03-16 11:56:12 +02001336TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001337 const TSourceLoc &identifierLoc,
1338 const TString *identifier,
1339 const TSymbol *symbol)
1340{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001341 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001342 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1343 {
1344 recover();
1345 }
1346
1347 if (!symbol)
1348 {
1349 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1350 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001351 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001352 }
1353 else
1354 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001355 const TString kGlFrontFacing("gl_FrontFacing");
1356 if (*identifier == kGlFrontFacing)
1357 {
1358 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1359 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001360 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001361 }
Jamie Madill2c433252014-12-03 12:36:54 -05001362 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001363 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1364 ASSERT(variable);
1365 const TType &type = variable->getType();
1366 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1367 *identifier, type, identifierLoc);
1368
1369 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1370 aggregate->setOp(EOpInvariantDeclaration);
1371 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001372 }
1373}
1374
Olli Etuahoe7847b02015-03-16 11:56:12 +02001375TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1376 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001377{
Olli Etuahofa33d582015-04-09 14:33:12 +03001378 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1379 if (mDeferredSingleDeclarationErrorCheck)
1380 {
1381 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1382 recover();
1383 mDeferredSingleDeclarationErrorCheck = false;
1384 }
1385
Jamie Madill0bd18df2013-06-20 11:55:52 -04001386 if (locationDeclaratorListCheck(identifierLocation, publicType))
1387 recover();
1388
Olli Etuaho3739d232015-04-08 12:23:44 +03001389 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001390 recover();
1391
Olli Etuaho2935c582015-04-08 14:32:06 +03001392 TVariable *variable = nullptr;
1393 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001394 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001395
1396 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1397 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001398 symbol->setId(variable->getUniqueId());
1399
Olli Etuahoe7847b02015-03-16 11:56:12 +02001400 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001401}
1402
Olli Etuahoe7847b02015-03-16 11:56:12 +02001403TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1404 const TSourceLoc &identifierLocation, const TString &identifier,
1405 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001406{
Olli Etuahofa33d582015-04-09 14:33:12 +03001407 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1408 if (mDeferredSingleDeclarationErrorCheck)
1409 {
1410 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1411 recover();
1412 mDeferredSingleDeclarationErrorCheck = false;
1413 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001414
Jamie Madill0bd18df2013-06-20 11:55:52 -04001415 if (locationDeclaratorListCheck(identifierLocation, publicType))
1416 recover();
1417
Olli Etuaho3739d232015-04-08 12:23:44 +03001418 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001419 recover();
1420
1421 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1422 {
1423 recover();
1424 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001425 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001426 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001427 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001428 int size;
1429 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001430 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001431 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001432 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001433 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001434
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001435 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001436 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001437 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001438
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001439 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1440 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001441 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001442
1443 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001444 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001445
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001446 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001447}
1448
Olli Etuahoe7847b02015-03-16 11:56:12 +02001449TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1450 const TSourceLoc &identifierLocation, const TString &identifier,
1451 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001452{
Olli Etuahofa33d582015-04-09 14:33:12 +03001453 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1454 if (mDeferredSingleDeclarationErrorCheck)
1455 {
1456 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1457 recover();
1458 mDeferredSingleDeclarationErrorCheck = false;
1459 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001460
Jamie Madill0bd18df2013-06-20 11:55:52 -04001461 if (locationDeclaratorListCheck(identifierLocation, publicType))
1462 recover();
1463
Olli Etuahoe7847b02015-03-16 11:56:12 +02001464 TIntermNode *intermNode = nullptr;
1465 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001466 {
1467 //
1468 // build the intermediate representation
1469 //
1470 if (intermNode)
1471 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001472 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001473 }
1474 else
1475 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001476 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001477 }
1478 }
1479 else
1480 {
1481 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001482 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001483 }
1484}
1485
Jamie Madilla295edf2013-06-06 11:56:48 -04001486void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1487{
1488 if (typeQualifier.qualifier != EvqUniform)
1489 {
1490 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1491 recover();
1492 return;
1493 }
1494
1495 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1496 ASSERT(!layoutQualifier.isEmpty());
1497
1498 if (shaderVersion < 300)
1499 {
1500 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1501 recover();
1502 return;
1503 }
1504
1505 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1506 {
1507 recover();
1508 return;
1509 }
1510
Jamie Madill099c0f32013-06-20 11:55:52 -04001511 if (layoutQualifier.matrixPacking != EmpUnspecified)
1512 {
1513 defaultMatrixPacking = layoutQualifier.matrixPacking;
1514 }
1515
Geoff Langc6856732014-02-11 09:38:55 -05001516 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001517 {
1518 defaultBlockStorage = layoutQualifier.blockStorage;
1519 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001520}
1521
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001522TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1523{
1524 TOperator op = EOpNull;
1525 if (publicType.userDef)
1526 {
1527 op = EOpConstructStruct;
1528 }
1529 else
1530 {
1531 switch (publicType.type)
1532 {
1533 case EbtFloat:
1534 if (publicType.isMatrix())
1535 {
1536 // TODO: non-square matrices
1537 switch(publicType.getCols())
1538 {
Jamie Madill28b97422013-07-08 14:01:38 -04001539 case 2: op = EOpConstructMat2; break;
1540 case 3: op = EOpConstructMat3; break;
1541 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001542 }
1543 }
1544 else
1545 {
1546 switch(publicType.getNominalSize())
1547 {
Jamie Madill28b97422013-07-08 14:01:38 -04001548 case 1: op = EOpConstructFloat; break;
1549 case 2: op = EOpConstructVec2; break;
1550 case 3: op = EOpConstructVec3; break;
1551 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001552 }
1553 }
1554 break;
1555
1556 case EbtInt:
1557 switch(publicType.getNominalSize())
1558 {
Jamie Madill28b97422013-07-08 14:01:38 -04001559 case 1: op = EOpConstructInt; break;
1560 case 2: op = EOpConstructIVec2; break;
1561 case 3: op = EOpConstructIVec3; break;
1562 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001563 }
1564 break;
1565
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001566 case EbtUInt:
1567 switch(publicType.getNominalSize())
1568 {
Jamie Madill28b97422013-07-08 14:01:38 -04001569 case 1: op = EOpConstructUInt; break;
1570 case 2: op = EOpConstructUVec2; break;
1571 case 3: op = EOpConstructUVec3; break;
1572 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001573 }
1574 break;
1575
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001576 case EbtBool:
1577 switch(publicType.getNominalSize())
1578 {
Jamie Madill28b97422013-07-08 14:01:38 -04001579 case 1: op = EOpConstructBool; break;
1580 case 2: op = EOpConstructBVec2; break;
1581 case 3: op = EOpConstructBVec3; break;
1582 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001583 }
1584 break;
1585
1586 default: break;
1587 }
1588
1589 if (op == EOpNull)
1590 {
1591 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1592 recover();
1593 publicType.type = EbtFloat;
1594 op = EOpConstructFloat;
1595 }
1596 }
1597
1598 TString tempString;
1599 TType type(publicType);
1600 return new TFunction(&tempString, type, op);
1601}
1602
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603// This function is used to test for the correctness of the parameters passed to various constructor functions
1604// and also convert them to the right datatype if it is allowed and required.
1605//
1606// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1607//
Olli Etuaho21203702014-11-13 16:16:21 +02001608TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001610 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001612 if (!aggregateArguments)
1613 {
1614 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001615 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001616 }
1617
Olli Etuahof40319e2015-03-10 14:33:00 +02001618 if (type->isArray())
1619 {
1620 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1621 TIntermSequence *args = aggregateArguments->getSequence();
1622 for (size_t i = 0; i < args->size(); i++)
1623 {
1624 const TType &argType = (*args)[i]->getAsTyped()->getType();
1625 // It has already been checked that the argument is not an array.
1626 ASSERT(!argType.isArray());
1627 if (!argType.sameElementType(*type))
1628 {
1629 error(line, "Array constructor argument has an incorrect type", "Error");
1630 recover();
1631 return nullptr;
1632 }
1633 }
1634 }
1635 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001636 {
1637 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001638 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001640 for (size_t i = 0; i < fields.size(); i++)
1641 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001642 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001643 {
1644 error(line, "Structure constructor arguments do not match structure fields", "Error");
1645 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001647 return 0;
1648 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001649 }
1650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001651
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001652 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001653 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1654 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001655 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001656 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001657 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001658 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659
Olli Etuaho21203702014-11-13 16:16:21 +02001660 // Structs should not be precision qualified, the individual members may be.
1661 // Built-in types on the other hand should be precision qualified.
1662 if (op != EOpConstructStruct)
1663 {
1664 constructor->setPrecisionFromChildren();
1665 type->setPrecision(constructor->getPrecision());
1666 }
1667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001668 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669}
1670
1671TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1672{
Olli Etuahof40319e2015-03-10 14:33:00 +02001673 // TODO: Add support for folding array constructors
1674 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001675 aggrNode->setType(type);
1676 if (canBeFolded) {
1677 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001678 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001679 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001680 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001681 }
1682 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001683 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001684 }
1685 if (returnVal)
1686 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001688 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001691 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692}
1693
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694//
1695// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1696// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1697// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1698// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1699// a constant matrix.
1700//
Jamie Madill075edd82013-07-08 13:30:19 -04001701TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001703 TIntermTyped* typedNode;
1704 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001706 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001707 if (tempConstantNode) {
1708 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001710 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001711 return node;
1712 }
1713 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001714 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001715 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 return 0;
1718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001720 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001722 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001723 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001724 std::stringstream extraInfoStream;
1725 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1726 std::string extraInfo = extraInfoStream.str();
1727 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001728 recover();
1729 fields.offsets[i] = 0;
1730 }
1731
1732 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 }
1735 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1736 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737}
1738
1739//
1740// This function returns the column being accessed from a constant matrix. The values are retrieved from
1741// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1742// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1743// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1744//
Jamie Madill075edd82013-07-08 13:30:19 -04001745TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001747 TIntermTyped* typedNode;
1748 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001750 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001751 std::stringstream extraInfoStream;
1752 extraInfoStream << "matrix field selection out of range '" << index << "'";
1753 std::string extraInfo = extraInfoStream.str();
1754 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 recover();
1756 index = 0;
1757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001759 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001760 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001761 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001762 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1763 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001764 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 return 0;
1768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771}
1772
1773
1774//
1775// This function returns an element of an array accessed from a constant array. The values are retrieved from
1776// the symbol table and parse-tree is built for the type of the element. The input
1777// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1778// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1779//
Jamie Madill075edd82013-07-08 13:30:19 -04001780TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 TIntermTyped* typedNode;
1783 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1784 TType arrayElementType = node->getType();
1785 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001787 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001788 std::stringstream extraInfoStream;
1789 extraInfoStream << "array field selection out of range '" << index << "'";
1790 std::string extraInfo = extraInfoStream.str();
1791 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 recover();
1793 index = 0;
1794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001796 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001797 size_t arrayElementSize = arrayElementType.getObjectSize();
1798 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1799 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001801 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 return 0;
1805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001807 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808}
1809
1810
1811//
1812// This function returns the value of a particular field inside a constant structure from the symbol table.
1813// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1814// function and returns the parse-tree with the values of the embedded/nested struct.
1815//
Jamie Madill075edd82013-07-08 13:30:19 -04001816TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817{
Jamie Madill98493dd2013-07-08 14:39:03 -04001818 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001819 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 for (size_t index = 0; index < fields.size(); ++index) {
1822 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 break;
1824 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001825 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 }
1827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
Jamie Madill94bf7f22013-07-08 13:31:15 -04001829 TIntermTyped *typedNode;
1830 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001832 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1835 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001836 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 return 0;
1840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843}
1844
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001845//
1846// Interface/uniform blocks
1847//
Jamie Madill98493dd2013-07-08 14:39:03 -04001848TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1849 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001850{
1851 if (reservedErrorCheck(nameLine, blockName))
1852 recover();
1853
1854 if (typeQualifier.qualifier != EvqUniform)
1855 {
1856 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1857 recover();
1858 }
1859
Jamie Madill099c0f32013-06-20 11:55:52 -04001860 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1861 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001862 {
1863 recover();
1864 }
1865
Jamie Madill099c0f32013-06-20 11:55:52 -04001866 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1867 {
1868 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1869 }
1870
Jamie Madill1566ef72013-06-20 11:55:54 -04001871 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1872 {
1873 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1874 }
1875
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001876 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001877 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001878 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1879 recover();
1880 }
1881
Jamie Madill98493dd2013-07-08 14:39:03 -04001882 // check for sampler types and apply layout qualifiers
1883 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1884 TField* field = (*fieldList)[memberIndex];
1885 TType* fieldType = field->type();
1886 if (IsSampler(fieldType->getBasicType())) {
1887 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001888 recover();
1889 }
1890
Jamie Madill98493dd2013-07-08 14:39:03 -04001891 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001892 switch (qualifier)
1893 {
1894 case EvqGlobal:
1895 case EvqUniform:
1896 break;
1897 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001898 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001899 recover();
1900 break;
1901 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001902
1903 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001904 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1905 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001906 {
1907 recover();
1908 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001909
Jamie Madill98493dd2013-07-08 14:39:03 -04001910 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001911 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001913 recover();
1914 }
1915
Jamie Madill98493dd2013-07-08 14:39:03 -04001916 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001917 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001920 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001921 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001923 recover();
1924 }
1925
Jamie Madill98493dd2013-07-08 14:39:03 -04001926 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001927 }
1928
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 // add array index
1930 int arraySize = 0;
1931 if (arrayIndex != NULL)
1932 {
1933 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1934 recover();
1935 }
1936
1937 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1938 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001939
1940 TString symbolName = "";
1941 int symbolId = 0;
1942
Jamie Madill98493dd2013-07-08 14:39:03 -04001943 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001944 {
1945 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001946 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1947 {
1948 TField* field = (*fieldList)[memberIndex];
1949 TType* fieldType = field->type();
1950
1951 // set parent pointer of the field variable
1952 fieldType->setInterfaceBlock(interfaceBlock);
1953
1954 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1955 fieldVariable->setQualifier(typeQualifier.qualifier);
1956
Nicolas Capensadfffe42014-06-17 02:13:36 -04001957 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001958 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001959 recover();
1960 }
1961 }
1962 }
1963 else
1964 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001965 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001966 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001967 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001968
Nicolas Capensadfffe42014-06-17 02:13:36 -04001969 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001970 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001971 recover();
1972 }
1973
1974 symbolId = instanceTypeDef->getUniqueId();
1975 symbolName = instanceTypeDef->getName();
1976 }
1977
Jamie Madill98493dd2013-07-08 14:39:03 -04001978 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001979 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001980
1981 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001982 return aggregate;
1983}
1984
Jamie Madill075edd82013-07-08 13:30:19 -04001985bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001986{
1987 ++structNestingLevel;
1988
1989 // Embedded structure definitions are not supported per GLSL ES spec.
1990 // They aren't allowed in GLSL either, but we need to detect this here
1991 // so we don't rely on the GLSL compiler to catch it.
1992 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001993 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001994 return true;
1995 }
1996
1997 return false;
1998}
1999
2000void TParseContext::exitStructDeclaration()
2001{
2002 --structNestingLevel;
2003}
2004
2005namespace {
2006
2007const int kWebGLMaxStructNesting = 4;
2008
2009} // namespace
2010
Jamie Madill98493dd2013-07-08 14:39:03 -04002011bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002012{
Jamie Madill5508f392014-02-20 13:31:36 -05002013 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014 return false;
2015 }
2016
Jamie Madill98493dd2013-07-08 14:39:03 -04002017 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002018 return false;
2019 }
2020
2021 // We're already inside a structure definition at this point, so add
2022 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002023 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002024 std::stringstream reasonStream;
2025 reasonStream << "Reference of struct type "
2026 << field.type()->getStruct()->name().c_str()
2027 << " exceeds maximum allowed nesting level of "
2028 << kWebGLMaxStructNesting;
2029 std::string reason = reasonStream.str();
2030 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002031 return true;
2032 }
2033
2034 return false;
2035}
2036
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002037//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002038// Parse an array index expression
2039//
Jamie Madill075edd82013-07-08 13:30:19 -04002040TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002041{
2042 TIntermTyped *indexedExpression = NULL;
2043
2044 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2045 {
2046 if (baseExpression->getAsSymbolNode())
2047 {
2048 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2049 }
2050 else
2051 {
2052 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2053 }
2054 recover();
2055 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002056
Jamie Madill21c1e452014-12-29 11:33:41 -05002057 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2058
2059 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002060 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002061 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002062 if (index < 0)
2063 {
2064 std::stringstream infoStream;
2065 infoStream << index;
2066 std::string info = infoStream.str();
2067 error(location, "negative index", info.c_str());
2068 recover();
2069 index = 0;
2070 }
2071 if (baseExpression->getType().getQualifier() == EvqConst)
2072 {
2073 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002074 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002075 // constant folding for arrays
2076 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002077 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002078 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002079 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002080 // constant folding for vectors
2081 TVectorFields fields;
2082 fields.num = 1;
2083 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2084 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2085 }
2086 else if (baseExpression->isMatrix())
2087 {
2088 // constant folding for matrices
2089 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002090 }
2091 }
2092 else
2093 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002094 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002095 {
Jamie Madill18464b52013-07-08 14:01:55 -04002096 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002097 {
2098 std::stringstream extraInfoStream;
2099 extraInfoStream << "array index out of range '" << index << "'";
2100 std::string extraInfo = extraInfoStream.str();
2101 error(location, "", "[", extraInfo.c_str());
2102 recover();
2103 index = baseExpression->getType().getArraySize() - 1;
2104 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002105 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2106 {
2107 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2108 recover();
2109 index = 0;
2110 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002111 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002112 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002113 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002114 std::stringstream extraInfoStream;
2115 extraInfoStream << "field selection out of range '" << index << "'";
2116 std::string extraInfo = extraInfoStream.str();
2117 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002118 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002119 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002120 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002121
Jamie Madill21c1e452014-12-29 11:33:41 -05002122 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002123 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002124 }
2125 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002126 else
2127 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002128 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002129 {
Jamie Madill19571812013-08-12 15:26:34 -07002130 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002131 recover();
2132 }
Jamie Madill19571812013-08-12 15:26:34 -07002133 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002134 {
Jamie Madill19571812013-08-12 15:26:34 -07002135 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002136 recover();
2137 }
2138
2139 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2140 }
2141
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002142 if (indexedExpression == 0)
2143 {
2144 ConstantUnion *unionArray = new ConstantUnion[1];
2145 unionArray->setFConst(0.0f);
2146 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2147 }
2148 else if (baseExpression->isArray())
2149 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002150 const TType &baseType = baseExpression->getType();
2151 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002152 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002153 TType copyOfType(baseType.getStruct());
2154 indexedExpression->setType(copyOfType);
2155 }
2156 else if (baseType.isInterfaceBlock())
2157 {
2158 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002159 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002160 }
2161 else
2162 {
Minmin Gong794e0002015-04-07 18:31:54 -07002163 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2164 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002165 }
2166
2167 if (baseExpression->getType().getQualifier() == EvqConst)
2168 {
2169 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2170 }
2171 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002172 else if (baseExpression->isMatrix())
2173 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002174 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002175 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002176 }
2177 else if (baseExpression->isVector())
2178 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002179 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2180 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002181 }
2182 else
2183 {
2184 indexedExpression->setType(baseExpression->getType());
2185 }
2186
2187 return indexedExpression;
2188}
2189
Jamie Madill075edd82013-07-08 13:30:19 -04002190TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002191{
2192 TIntermTyped *indexedExpression = NULL;
2193
2194 if (baseExpression->isArray())
2195 {
2196 error(fieldLocation, "cannot apply dot operator to an array", ".");
2197 recover();
2198 }
2199
2200 if (baseExpression->isVector())
2201 {
2202 TVectorFields fields;
2203 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2204 {
2205 fields.num = 1;
2206 fields.offsets[0] = 0;
2207 recover();
2208 }
2209
2210 if (baseExpression->getType().getQualifier() == EvqConst)
2211 {
2212 // constant folding for vector fields
2213 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2214 if (indexedExpression == 0)
2215 {
2216 recover();
2217 indexedExpression = baseExpression;
2218 }
2219 else
2220 {
Minmin Gong794e0002015-04-07 18:31:54 -07002221 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002222 }
2223 }
2224 else
2225 {
2226 TString vectorString = fieldString;
2227 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2228 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002229 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002230 }
2231 }
2232 else if (baseExpression->isMatrix())
2233 {
2234 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002235 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002236 {
2237 fields.wholeRow = false;
2238 fields.wholeCol = false;
2239 fields.row = 0;
2240 fields.col = 0;
2241 recover();
2242 }
2243
2244 if (fields.wholeRow || fields.wholeCol)
2245 {
2246 error(dotLocation, " non-scalar fields not implemented yet", ".");
2247 recover();
2248 ConstantUnion *unionArray = new ConstantUnion[1];
2249 unionArray->setIConst(0);
2250 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2251 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002252 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2253 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002254 }
2255 else
2256 {
2257 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002258 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002259 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2260 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2261 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2262 }
2263 }
2264 else if (baseExpression->getBasicType() == EbtStruct)
2265 {
2266 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002267 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2268 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002269 {
2270 error(dotLocation, "structure has no fields", "Internal Error");
2271 recover();
2272 indexedExpression = baseExpression;
2273 }
2274 else
2275 {
2276 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002277 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002278 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002279 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002280 {
2281 fieldFound = true;
2282 break;
2283 }
2284 }
2285 if (fieldFound)
2286 {
2287 if (baseExpression->getType().getQualifier() == EvqConst)
2288 {
2289 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2290 if (indexedExpression == 0)
2291 {
2292 recover();
2293 indexedExpression = baseExpression;
2294 }
2295 else
2296 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002297 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002298 // change the qualifier of the return type, not of the structure field
2299 // as the structure definition is shared between various structures.
2300 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2301 }
2302 }
2303 else
2304 {
2305 ConstantUnion *unionArray = new ConstantUnion[1];
2306 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002307 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002308 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002309 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002310 }
2311 }
2312 else
2313 {
2314 error(dotLocation, " no such field in structure", fieldString.c_str());
2315 recover();
2316 indexedExpression = baseExpression;
2317 }
2318 }
2319 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002320 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002321 {
2322 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002323 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2324 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002325 {
2326 error(dotLocation, "interface block has no fields", "Internal Error");
2327 recover();
2328 indexedExpression = baseExpression;
2329 }
2330 else
2331 {
2332 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002333 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002334 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002335 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002336 {
2337 fieldFound = true;
2338 break;
2339 }
2340 }
2341 if (fieldFound)
2342 {
2343 ConstantUnion *unionArray = new ConstantUnion[1];
2344 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002345 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002346 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002347 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002348 }
2349 else
2350 {
2351 error(dotLocation, " no such field in interface block", fieldString.c_str());
2352 recover();
2353 indexedExpression = baseExpression;
2354 }
2355 }
2356 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002357 else
2358 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002359 if (shaderVersion < 300)
2360 {
2361 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2362 }
2363 else
2364 {
2365 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2366 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002367 recover();
2368 indexedExpression = baseExpression;
2369 }
2370
2371 return indexedExpression;
2372}
2373
Jamie Madill075edd82013-07-08 13:30:19 -04002374TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002375{
Jamie Madilla5efff92013-06-06 11:56:47 -04002376 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002377
Jamie Madilla5efff92013-06-06 11:56:47 -04002378 qualifier.location = -1;
2379 qualifier.matrixPacking = EmpUnspecified;
2380 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002381
2382 if (qualifierType == "shared")
2383 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002384 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002385 }
2386 else if (qualifierType == "packed")
2387 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002388 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002389 }
2390 else if (qualifierType == "std140")
2391 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002392 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002393 }
2394 else if (qualifierType == "row_major")
2395 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002396 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002397 }
2398 else if (qualifierType == "column_major")
2399 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002400 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002401 }
2402 else if (qualifierType == "location")
2403 {
2404 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2405 recover();
2406 }
2407 else
2408 {
2409 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2410 recover();
2411 }
2412
Jamie Madilla5efff92013-06-06 11:56:47 -04002413 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002414}
2415
Jamie Madill075edd82013-07-08 13:30:19 -04002416TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002417{
Jamie Madilla5efff92013-06-06 11:56:47 -04002418 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002419
Jamie Madilla5efff92013-06-06 11:56:47 -04002420 qualifier.location = -1;
2421 qualifier.matrixPacking = EmpUnspecified;
2422 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002423
2424 if (qualifierType != "location")
2425 {
2426 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2427 recover();
2428 }
2429 else
2430 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002431 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002432 if (intValue < 0)
2433 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002434 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002435 recover();
2436 }
2437 else
2438 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002439 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002440 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002441 }
2442
Jamie Madilla5efff92013-06-06 11:56:47 -04002443 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002444}
2445
Jamie Madilla5efff92013-06-06 11:56:47 -04002446TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002447{
Jamie Madilla5efff92013-06-06 11:56:47 -04002448 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002449
Jamie Madilla5efff92013-06-06 11:56:47 -04002450 if (rightQualifier.location != -1)
2451 {
2452 joinedQualifier.location = rightQualifier.location;
2453 }
2454 if (rightQualifier.matrixPacking != EmpUnspecified)
2455 {
2456 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2457 }
2458 if (rightQualifier.blockStorage != EbsUnspecified)
2459 {
2460 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2461 }
2462
2463 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002464}
2465
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002466TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2467 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2468{
2469 TQualifier mergedQualifier = EvqSmoothIn;
2470
Jamie Madill19571812013-08-12 15:26:34 -07002471 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002472 if (interpolationQualifier == EvqSmooth)
2473 mergedQualifier = EvqSmoothIn;
2474 else if (interpolationQualifier == EvqFlat)
2475 mergedQualifier = EvqFlatIn;
2476 else UNREACHABLE();
2477 }
2478 else if (storageQualifier == EvqCentroidIn) {
2479 if (interpolationQualifier == EvqSmooth)
2480 mergedQualifier = EvqCentroidIn;
2481 else if (interpolationQualifier == EvqFlat)
2482 mergedQualifier = EvqFlatIn;
2483 else UNREACHABLE();
2484 }
Jamie Madill19571812013-08-12 15:26:34 -07002485 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002486 if (interpolationQualifier == EvqSmooth)
2487 mergedQualifier = EvqSmoothOut;
2488 else if (interpolationQualifier == EvqFlat)
2489 mergedQualifier = EvqFlatOut;
2490 else UNREACHABLE();
2491 }
2492 else if (storageQualifier == EvqCentroidOut) {
2493 if (interpolationQualifier == EvqSmooth)
2494 mergedQualifier = EvqCentroidOut;
2495 else if (interpolationQualifier == EvqFlat)
2496 mergedQualifier = EvqFlatOut;
2497 else UNREACHABLE();
2498 }
2499 else {
2500 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2501 recover();
2502
2503 mergedQualifier = storageQualifier;
2504 }
2505
2506 TPublicType type;
2507 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2508 return type;
2509}
2510
Jamie Madill98493dd2013-07-08 14:39:03 -04002511TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002512{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002513 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2514 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002515 recover();
2516 }
2517
Jamie Madill98493dd2013-07-08 14:39:03 -04002518 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002519 //
2520 // Careful not to replace already known aspects of type, like array-ness
2521 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002522 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002524 type->setPrimarySize(typeSpecifier.primarySize);
2525 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002526 type->setPrecision(typeSpecifier.precision);
2527 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002528 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002529
2530 // don't allow arrays of arrays
2531 if (type->isArray()) {
2532 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2533 recover();
2534 }
2535 if (typeSpecifier.array)
2536 type->setArraySize(typeSpecifier.arraySize);
2537 if (typeSpecifier.userDef) {
2538 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002539 }
2540
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002542 recover();
2543 }
2544 }
2545
Jamie Madill98493dd2013-07-08 14:39:03 -04002546 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002547}
2548
Jamie Madill98493dd2013-07-08 14:39:03 -04002549TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002550{
Jamie Madill98493dd2013-07-08 14:39:03 -04002551 TStructure* structure = new TStructure(structName, fieldList);
2552 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002553
Jamie Madill9b820842015-02-12 10:40:10 -05002554 // Store a bool in the struct if we're at global scope, to allow us to
2555 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002556 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002557 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002558
Jamie Madill98493dd2013-07-08 14:39:03 -04002559 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002560 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002561 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562 {
2563 recover();
2564 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002565 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002566 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002567 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002568 recover();
2569 }
2570 }
2571
2572 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002573 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002574 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002575 const TField &field = *(*fieldList)[typeListIndex];
2576 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002577 switch (qualifier)
2578 {
2579 case EvqGlobal:
2580 case EvqTemporary:
2581 break;
2582 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002584 recover();
2585 break;
2586 }
2587 }
2588
2589 TPublicType publicType;
2590 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002591 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002592 exitStructDeclaration();
2593
2594 return publicType;
2595}
2596
Olli Etuahoa3a36662015-02-17 13:46:51 +02002597TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2598{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002599 TBasicType switchType = init->getBasicType();
2600 if ((switchType != EbtInt && switchType != EbtUInt) ||
2601 init->isMatrix() ||
2602 init->isArray() ||
2603 init->isVector())
2604 {
2605 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2606 recover();
2607 return nullptr;
2608 }
2609
Olli Etuahoac5274d2015-02-20 10:19:08 +02002610 if (statementList)
2611 {
2612 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2613 {
2614 recover();
2615 return nullptr;
2616 }
2617 }
2618
Olli Etuahoa3a36662015-02-17 13:46:51 +02002619 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2620 if (node == nullptr)
2621 {
2622 error(loc, "erroneous switch statement", "switch");
2623 recover();
2624 return nullptr;
2625 }
2626 return node;
2627}
2628
2629TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2630{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002631 if (mSwitchNestingLevel == 0)
2632 {
2633 error(loc, "case labels need to be inside switch statements", "case");
2634 recover();
2635 return nullptr;
2636 }
2637 if (condition == nullptr)
2638 {
2639 error(loc, "case label must have a condition", "case");
2640 recover();
2641 return nullptr;
2642 }
2643 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2644 condition->isMatrix() ||
2645 condition->isArray() ||
2646 condition->isVector())
2647 {
2648 error(condition->getLine(), "case label must be a scalar integer", "case");
2649 recover();
2650 }
2651 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2652 if (conditionConst == nullptr)
2653 {
2654 error(condition->getLine(), "case label must be constant", "case");
2655 recover();
2656 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002657 TIntermCase *node = intermediate.addCase(condition, loc);
2658 if (node == nullptr)
2659 {
2660 error(loc, "erroneous case statement", "case");
2661 recover();
2662 return nullptr;
2663 }
2664 return node;
2665}
2666
2667TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2668{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002669 if (mSwitchNestingLevel == 0)
2670 {
2671 error(loc, "default labels need to be inside switch statements", "default");
2672 recover();
2673 return nullptr;
2674 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002675 TIntermCase *node = intermediate.addCase(nullptr, loc);
2676 if (node == nullptr)
2677 {
2678 error(loc, "erroneous default statement", "default");
2679 recover();
2680 return nullptr;
2681 }
2682 return node;
2683}
2684
Olli Etuahof6c694b2015-03-26 14:50:53 +02002685TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2686 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002687{
2688 if (child == nullptr)
2689 {
2690 return nullptr;
2691 }
2692
2693 switch (op)
2694 {
2695 case EOpLogicalNot:
2696 if (child->getBasicType() != EbtBool ||
2697 child->isMatrix() ||
2698 child->isArray() ||
2699 child->isVector())
2700 {
2701 return nullptr;
2702 }
2703 break;
2704 case EOpBitwiseNot:
2705 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2706 child->isMatrix() ||
2707 child->isArray())
2708 {
2709 return nullptr;
2710 }
2711 break;
2712 case EOpPostIncrement:
2713 case EOpPreIncrement:
2714 case EOpPostDecrement:
2715 case EOpPreDecrement:
2716 case EOpNegative:
2717 case EOpPositive:
2718 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002719 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002720 child->isArray())
2721 {
2722 return nullptr;
2723 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002724 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002725 default:
2726 break;
2727 }
2728
Olli Etuahof6c694b2015-03-26 14:50:53 +02002729 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002730}
2731
Olli Etuaho09b22472015-02-11 11:47:26 +02002732TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2733{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002734 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002735 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002736 {
2737 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2738 recover();
2739 return child;
2740 }
2741 return node;
2742}
2743
2744TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2745{
2746 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2747 recover();
2748 return addUnaryMath(op, child, loc);
2749}
2750
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002751bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002752 const TSourceLoc &loc)
2753{
2754 if (left->isArray() || right->isArray())
2755 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002756 if (shaderVersion < 300)
2757 {
2758 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2759 return false;
2760 }
2761
2762 if (left->isArray() != right->isArray())
2763 {
2764 error(loc, "array / non-array mismatch", GetOperatorString(op));
2765 return false;
2766 }
2767
2768 switch (op)
2769 {
2770 case EOpEqual:
2771 case EOpNotEqual:
2772 case EOpAssign:
2773 case EOpInitialize:
2774 break;
2775 default:
2776 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2777 return false;
2778 }
2779 if (left->getArraySize() != right->getArraySize())
2780 {
2781 error(loc, "array size mismatch", GetOperatorString(op));
2782 return false;
2783 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002784 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002785
2786 // Check ops which require integer / ivec parameters
2787 bool isBitShift = false;
2788 switch (op)
2789 {
2790 case EOpBitShiftLeft:
2791 case EOpBitShiftRight:
2792 case EOpBitShiftLeftAssign:
2793 case EOpBitShiftRightAssign:
2794 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2795 // check that the basic type is an integer type.
2796 isBitShift = true;
2797 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2798 {
2799 return false;
2800 }
2801 break;
2802 case EOpBitwiseAnd:
2803 case EOpBitwiseXor:
2804 case EOpBitwiseOr:
2805 case EOpBitwiseAndAssign:
2806 case EOpBitwiseXorAssign:
2807 case EOpBitwiseOrAssign:
2808 // It is enough to check the type of only one operand, since later it
2809 // is checked that the operand types match.
2810 if (!IsInteger(left->getBasicType()))
2811 {
2812 return false;
2813 }
2814 break;
2815 default:
2816 break;
2817 }
2818
2819 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2820 // So the basic type should usually match.
2821 if (!isBitShift && left->getBasicType() != right->getBasicType())
2822 {
2823 return false;
2824 }
2825
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002826 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002827 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002828 switch(op)
2829 {
2830 case EOpAssign:
2831 case EOpInitialize:
2832 case EOpEqual:
2833 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002834 // ESSL 1.00 sections 5.7, 5.8, 5.9
2835 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2836 {
2837 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2838 return false;
2839 }
Olli Etuahoff699002015-03-23 14:38:42 +02002840 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2841 // we interpret the spec so that this extends to structs containing samplers,
2842 // similarly to ESSL 1.00 spec.
2843 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2844 left->getType().isStructureContainingSamplers())
2845 {
2846 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2847 return false;
2848 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002849 case EOpLessThan:
2850 case EOpGreaterThan:
2851 case EOpLessThanEqual:
2852 case EOpGreaterThanEqual:
2853 if ((left->getNominalSize() != right->getNominalSize()) ||
2854 (left->getSecondarySize() != right->getSecondarySize()))
2855 {
2856 return false;
2857 }
2858 default:
2859 break;
2860 }
2861
Olli Etuahod6b14282015-03-17 14:31:35 +02002862 return true;
2863}
2864
Olli Etuahofc1806e2015-03-17 13:03:11 +02002865TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2866 const TSourceLoc &loc)
2867{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002868 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002869 return nullptr;
2870
Olli Etuahofc1806e2015-03-17 13:03:11 +02002871 switch (op)
2872 {
2873 case EOpEqual:
2874 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002875 break;
2876 case EOpLessThan:
2877 case EOpGreaterThan:
2878 case EOpLessThanEqual:
2879 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002880 ASSERT(!left->isArray() && !right->isArray());
2881 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002882 left->getBasicType() == EbtStruct)
2883 {
2884 return nullptr;
2885 }
2886 break;
2887 case EOpLogicalOr:
2888 case EOpLogicalXor:
2889 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02002890 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002891 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02002892 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02002893 {
2894 return nullptr;
2895 }
2896 break;
2897 case EOpAdd:
2898 case EOpSub:
2899 case EOpDiv:
2900 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02002901 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002902 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
2903 {
2904 return nullptr;
2905 }
2906 break;
2907 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02002908 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002909 // Note that this is only for the % operator, not for mod()
2910 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
2911 {
2912 return nullptr;
2913 }
2914 break;
2915 // Note that for bitwise ops, type checking is done in promote() to
2916 // share code between ops and compound assignment
2917 default:
2918 break;
2919 }
2920
Olli Etuahofc1806e2015-03-17 13:03:11 +02002921 return intermediate.addBinaryMath(op, left, right, loc);
2922}
2923
Olli Etuaho09b22472015-02-11 11:47:26 +02002924TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
2925 const TSourceLoc &loc)
2926{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002927 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002928 if (node == 0)
2929 {
2930 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2931 recover();
2932 return left;
2933 }
2934 return node;
2935}
2936
2937TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
2938 const TSourceLoc &loc)
2939{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002940 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002941 if (node == 0)
2942 {
2943 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2944 recover();
2945 ConstantUnion *unionArray = new ConstantUnion[1];
2946 unionArray->setBConst(false);
2947 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
2948 }
2949 return node;
2950}
2951
Olli Etuahod6b14282015-03-17 14:31:35 +02002952TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2953 const TSourceLoc &loc)
2954{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002955 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002956 {
2957 return intermediate.addAssign(op, left, right, loc);
2958 }
2959 return nullptr;
2960}
2961
2962TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2963 const TSourceLoc &loc)
2964{
2965 TIntermTyped *node = createAssign(op, left, right, loc);
2966 if (node == nullptr)
2967 {
2968 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
2969 recover();
2970 return left;
2971 }
2972 return node;
2973}
2974
Olli Etuaho49300862015-02-20 14:54:49 +02002975TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
2976{
2977 switch (op)
2978 {
2979 case EOpContinue:
2980 if (mLoopNestingLevel <= 0)
2981 {
2982 error(loc, "continue statement only allowed in loops", "");
2983 recover();
2984 }
2985 break;
2986 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02002987 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02002988 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02002989 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02002990 recover();
2991 }
2992 break;
2993 case EOpReturn:
2994 if (currentFunctionType->getBasicType() != EbtVoid)
2995 {
2996 error(loc, "non-void function must return a value", "return");
2997 recover();
2998 }
2999 break;
3000 default:
3001 // No checks for discard
3002 break;
3003 }
3004 return intermediate.addBranch(op, loc);
3005}
3006
3007TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3008{
3009 ASSERT(op == EOpReturn);
3010 mFunctionReturnsValue = true;
3011 if (currentFunctionType->getBasicType() == EbtVoid)
3012 {
3013 error(loc, "void function cannot return a value", "return");
3014 recover();
3015 }
3016 else if (*currentFunctionType != returnValue->getType())
3017 {
3018 error(loc, "function return is not matching type:", "return");
3019 recover();
3020 }
3021 return intermediate.addBranch(op, returnValue, loc);
3022}
3023
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003024TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *node,
3025 const TSourceLoc &loc, bool *fatalError)
3026{
3027 *fatalError = false;
3028 TOperator op = fnCall->getBuiltInOp();
3029 TIntermTyped *callNode = nullptr;
3030
3031 if (op != EOpNull)
3032 {
3033 //
3034 // Then this should be a constructor.
3035 // Don't go through the symbol table for constructors.
3036 // Their parameters will be verified algorithmically.
3037 //
3038 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3039 if (!constructorErrorCheck(loc, node, *fnCall, op, &type))
3040 {
3041 //
3042 // It's a constructor, of type 'type'.
3043 //
3044 callNode = addConstructor(node, &type, op, fnCall, loc);
3045 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003046
3047 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003048 {
3049 recover();
3050 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3051 }
3052 callNode->setType(type);
3053 }
3054 else
3055 {
3056 //
3057 // Not a constructor. Find it in the symbol table.
3058 //
3059 const TFunction* fnCandidate;
3060 bool builtIn;
3061 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3062 if (fnCandidate)
3063 {
3064 //
3065 // A declared function.
3066 //
3067 if (builtIn && !fnCandidate->getExtension().empty() &&
3068 extensionErrorCheck(loc, fnCandidate->getExtension()))
3069 {
3070 recover();
3071 }
3072 op = fnCandidate->getBuiltInOp();
3073 if (builtIn && op != EOpNull)
3074 {
3075 //
3076 // A function call mapped to a built-in operation.
3077 //
3078 if (fnCandidate->getParamCount() == 1)
3079 {
3080 //
3081 // Treat it like a built-in unary operator.
3082 //
Olli Etuahof6c694b2015-03-26 14:50:53 +02003083 callNode = createUnaryMath(op, node->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003084 if (callNode == nullptr)
3085 {
3086 std::stringstream extraInfoStream;
3087 extraInfoStream << "built in unary operator function. Type: "
3088 << static_cast<TIntermTyped*>(node)->getCompleteString();
3089 std::string extraInfo = extraInfoStream.str();
3090 error(node->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3091 *fatalError = true;
3092 return nullptr;
3093 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003094 }
3095 else
3096 {
3097 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, op, loc);
3098 aggregate->setType(fnCandidate->getReturnType());
3099 aggregate->setPrecisionFromChildren();
3100 callNode = aggregate;
3101
3102 // Some built-in functions have out parameters too.
3103 functionCallLValueErrorCheck(fnCandidate, aggregate);
3104 }
3105 }
3106 else
3107 {
3108 // This is a real function call
3109
3110 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, EOpFunctionCall, loc);
3111 aggregate->setType(fnCandidate->getReturnType());
3112
3113 // this is how we know whether the given function is a builtIn function or a user defined function
3114 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3115 // if builtIn == true, it's definitely a builtIn function with EOpNull
3116 if (!builtIn)
3117 aggregate->setUserDefined();
3118 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003119 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003120
3121 // This needs to happen after the name is set
3122 if (builtIn)
3123 aggregate->setBuiltInFunctionPrecision();
3124
3125 callNode = aggregate;
3126
3127 functionCallLValueErrorCheck(fnCandidate, aggregate);
3128 }
3129 }
3130 else
3131 {
3132 // error message was put out by findFunction()
3133 // Put on a dummy node for error recovery
3134 ConstantUnion *unionArray = new ConstantUnion[1];
3135 unionArray->setFConst(0.0f);
3136 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3137 recover();
3138 }
3139 }
3140 delete fnCall;
3141 return callNode;
3142}
3143
Olli Etuaho49300862015-02-20 14:54:49 +02003144
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003145//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003146// Parse an array of strings using yyparse.
3147//
3148// Returns 0 for success.
3149//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003150int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003151 TParseContext* context) {
3152 if ((count == 0) || (string == NULL))
3153 return 1;
3154
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003155 if (glslang_initialize(context))
3156 return 1;
3157
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003158 int error = glslang_scan(count, string, length, context);
3159 if (!error)
3160 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003161
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003162 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003163
alokp@chromium.org6b495712012-06-29 00:06:58 +00003164 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003165}
3166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003168