blob: fbd0ba85c16fe57bdb7c9df208991e8e0ddc7f81 [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//
Jamie Madill075edd82013-07-08 13:30:19 -0400571bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000574 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 return true;
576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579}
580
581// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
582//
583// returns true in case of an error
584//
Jamie Madill075edd82013-07-08 13:30:19 -0400585bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000586{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000588 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return true;
590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593}
594
595// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
596//
597// returns true in case of an error
598//
Jamie Madill075edd82013-07-08 13:30:19 -0400599bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000601 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 return true;
604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000606 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
Jamie Madill075edd82013-07-08 13:30:19 -0400609bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 if (pType.type == EbtStruct) {
612 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000613 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000614
615 return true;
616 }
617
618 return false;
619 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 return true;
623 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
Jamie Madill075edd82013-07-08 13:30:19 -0400628bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Jamie Madillb120eac2013-06-12 14:08:13 -0400630 switch (pType.qualifier)
631 {
632 case EvqVaryingIn:
633 case EvqVaryingOut:
634 case EvqAttribute:
Jamie Madill19571812013-08-12 15:26:34 -0700635 case EvqVertexIn:
636 case EvqFragmentOut:
Jamie Madillb120eac2013-06-12 14:08:13 -0400637 if (pType.type == EbtStruct)
638 {
639 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
640 return true;
641 }
Jamie Madill10567262014-04-17 16:40:00 -0400642
643 default: break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
647 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
Jamie Madill075edd82013-07-08 13:30:19 -0400652bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400653{
654 if (pType.layoutQualifier.location != -1)
655 {
656 error(line, "location must only be specified for a single input or output variable", "location");
657 return true;
658 }
659
660 return false;
661}
662
Jamie Madill075edd82013-07-08 13:30:19 -0400663bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
666 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000667 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return true;
669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672}
673
674bool TParseContext::containsSampler(TType& type)
675{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 if (IsSampler(type.getBasicType()))
677 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678
Jamie Madill98493dd2013-07-08 14:39:03 -0400679 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
680 const TFieldList& fields = type.getStruct()->fields();
681 for (unsigned int i = 0; i < fields.size(); ++i) {
682 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 return true;
684 }
685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688}
689
690//
691// Do size checking for an array type's size.
692//
693// Returns true if there was an error.
694//
Jamie Madill075edd82013-07-08 13:30:19 -0400695bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000698
699 if (constant == 0 || !constant->isScalarInt())
700 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000701 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
Nicolas Capens906744a2014-06-06 15:18:07 -0400705 unsigned int unsignedSize = 0;
706
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000707 if (constant->getBasicType() == EbtUInt)
708 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400709 unsignedSize = constant->getUConst(0);
710 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000711 }
712 else
713 {
714 size = constant->getIConst(0);
715
Nicolas Capens906744a2014-06-06 15:18:07 -0400716 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000717 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400718 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000719 size = 1;
720 return true;
721 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400722
723 unsignedSize = static_cast<unsigned int>(size);
724 }
725
726 if (size == 0)
727 {
728 error(line, "array size must be greater than zero", "");
729 size = 1;
730 return true;
731 }
732
733 // The size of arrays is restricted here to prevent issues further down the
734 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
735 // 4096 registers so this should be reasonable even for aggressively optimizable code.
736 const unsigned int sizeLimit = 65536;
737
738 if (unsignedSize > sizeLimit)
739 {
740 error(line, "array size too large", "");
741 size = 1;
742 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746}
747
748//
749// See if this qualifier can be an array.
750//
751// Returns true if there is an error.
752//
Olli Etuaho3739d232015-04-08 12:23:44 +0300753bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754{
Olli Etuaho3739d232015-04-08 12:23:44 +0300755 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
756 (type.qualifier == EvqConst && shaderVersion < 300))
757 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000758 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return true;
760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
765//
766// See if this type can be an array.
767//
768// Returns true if there is an error.
769//
Jamie Madill075edd82013-07-08 13:30:19 -0400770bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 //
773 // Can the type be an array?
774 //
775 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000776 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return true;
778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781}
782
783//
Olli Etuaho47fadcb2015-04-08 14:13:53 +0300784// Do all the semantic checking for declaring an array,
785// and make the right changes to the symbol table.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786//
787// size == 0 means no specified size.
788//
789// Returns true if there was an error.
790//
Jamie Madill075edd82013-07-08 13:30:19 -0400791bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000793 bool builtIn = false;
794 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000795 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 if (symbol == 0 || !sameScope) {
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100797 bool needsReservedErrorCheck = true;
798
799 // gl_LastFragData may be redeclared with a new precision qualifier
800 if (identifier.compare(0, 15, "gl_LastFragData") == 0) {
801 if (type.arraySize == static_cast<const TVariable*>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion))->getConstPointer()->getIConst()) {
802 if (TSymbol* builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion)) {
803 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
804 }
805 } else {
806 error(line, "redeclaration of array with size != gl_MaxDrawBuffers", identifier.c_str());
807 return true;
808 }
809 }
810
811 if (needsReservedErrorCheck)
812 if (reservedErrorCheck(line, identifier))
813 return true;
814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 if (type.arraySize)
818 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
Nicolas Capensadfffe42014-06-17 02:13:36 -0400820 if (! symbolTable.declare(variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000822 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 return true;
824 }
Olli Etuaho47fadcb2015-04-08 14:13:53 +0300825 }
826 else
827 {
828 error(line, "redeclaration of an array", identifier.c_str());
829 return true;
830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 if (voidErrorCheck(line, identifier, type))
833 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838//
839// Enforce non-initializer type/qualifier rules.
840//
841// Returns true if there was an error.
842//
Olli Etuaho3739d232015-04-08 12:23:44 +0300843bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844{
Olli Etuaho3739d232015-04-08 12:23:44 +0300845 ASSERT(type != nullptr);
846 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000847 {
848 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300849 type->qualifier = EvqTemporary;
850
851 // Generate informative error messages for ESSL1.
852 // In ESSL3 arrays and structures containing arrays can be constant.
853 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000854 {
855 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
856 }
857 else
858 {
859 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
860 }
861
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 return true;
863 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
867//
868// Do semantic checking for a variable declaration that has no initializer,
869// and update the symbol table.
870//
871// Returns true if there was an error.
872//
Jamie Madill075edd82013-07-08 13:30:19 -0400873bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 if (reservedErrorCheck(line, identifier))
876 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
zmo@google.comfd747b82011-04-23 01:30:07 +0000878 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
Nicolas Capensadfffe42014-06-17 02:13:36 -0400880 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000881 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000882 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000883 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 return true;
885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 if (voidErrorCheck(line, identifier, type))
888 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
Jamie Madill075edd82013-07-08 13:30:19 -0400893bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000895 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000896 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return true;
898 }
899 if (qualifier == EvqConst && paramQualifier != EvqIn) {
900 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
901 return true;
902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000905 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000907 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
Jamie Madill075edd82013-07-08 13:30:19 -0400912bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000913{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000914 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000915 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
916 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000917 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 return true;
919 }
zmo@google.comf5450912011-09-09 01:37:19 +0000920 // In GLSL ES, an extension's default behavior is "disable".
921 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000922 error(line, "extension", extension.c_str(), "is disabled");
923 return true;
924 }
925 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000926 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000927 return false;
928 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931}
932
Jamie Madill075edd82013-07-08 13:30:19 -0400933bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400934{
935 if (structQualifierErrorCheck(identifierLocation, publicType))
936 return true;
937
938 // check for layout qualifier issues
939 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
940
941 if (layoutQualifier.matrixPacking != EmpUnspecified)
942 {
943 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400944 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400945 }
946
947 if (layoutQualifier.blockStorage != EbsUnspecified)
948 {
949 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400950 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400951 }
952
Jamie Madill19571812013-08-12 15:26:34 -0700953 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400954 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400955 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400956 }
957
958 return false;
959}
960
Jamie Madill075edd82013-07-08 13:30:19 -0400961bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400962{
963 if (layoutQualifier.location != -1)
964 {
965 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
966 return true;
967 }
968
969 return false;
970}
971
Olli Etuahob6e07a62015-02-16 12:22:10 +0200972bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
973{
974 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
975 {
976 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
977 if (qual == EvqOut || qual == EvqInOut)
978 {
979 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
980 if (lValueErrorCheck(node->getLine(), "assign", node))
981 {
982 error(node->getLine(),
983 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
984 recover();
985 return true;
986 }
987 }
988 }
989 return false;
990}
991
zmo@google.com09c323a2011-08-12 18:22:25 +0000992bool TParseContext::supportsExtension(const char* extension)
993{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000994 const TExtensionBehavior& extbehavior = extensionBehavior();
995 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
996 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000997}
998
Jamie Madill5d287f52013-07-12 15:38:19 -0400999bool TParseContext::isExtensionEnabled(const char* extension) const
1000{
1001 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001002 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001003
1004 if (iter == extbehavior.end())
1005 {
1006 return false;
1007 }
1008
1009 return (iter->second == EBhEnable || iter->second == EBhRequire);
1010}
1011
Jamie Madill075edd82013-07-08 13:30:19 -04001012void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1013{
1014 pp::SourceLocation srcLoc;
1015 srcLoc.file = loc.first_file;
1016 srcLoc.line = loc.first_line;
1017 directiveHandler.handleExtension(srcLoc, extName, behavior);
1018}
1019
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001020void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001021{
1022 pp::SourceLocation srcLoc;
1023 srcLoc.file = loc.first_file;
1024 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001025 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001026}
1027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028/////////////////////////////////////////////////////////////////////////////////
1029//
1030// Non-Errors.
1031//
1032/////////////////////////////////////////////////////////////////////////////////
1033
Jamie Madill5c097022014-08-20 16:38:32 -04001034const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1035 const TString *name,
1036 const TSymbol *symbol)
1037{
1038 const TVariable *variable = NULL;
1039
1040 if (!symbol)
1041 {
1042 error(location, "undeclared identifier", name->c_str());
1043 recover();
1044 }
1045 else if (!symbol->isVariable())
1046 {
1047 error(location, "variable expected", name->c_str());
1048 recover();
1049 }
1050 else
1051 {
1052 variable = static_cast<const TVariable*>(symbol);
1053
1054 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1055 !variable->getExtension().empty() &&
1056 extensionErrorCheck(location, variable->getExtension()))
1057 {
1058 recover();
1059 }
1060 }
1061
1062 if (!variable)
1063 {
1064 TType type(EbtFloat, EbpUndefined);
1065 TVariable *fakeVariable = new TVariable(name, type);
1066 symbolTable.declare(fakeVariable);
1067 variable = fakeVariable;
1068 }
1069
1070 return variable;
1071}
1072
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073//
1074// Look up a function name in the symbol table, and make sure it is a function.
1075//
1076// Return the function symbol if found, otherwise 0.
1077//
Austin Kinross3ae64652015-01-26 15:51:39 -08001078const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001080 // First find by unmangled name to check whether the function name has been
1081 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001082 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001083 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001084 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001085 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001086 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087
alokp@chromium.org0a576182010-08-09 17:16:27 +00001088 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001089 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 return 0;
1091 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
alokp@chromium.org0a576182010-08-09 17:16:27 +00001093 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001094 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001095 return 0;
1096 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001097
1098 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101//
1102// Initializers show up in several places in the grammar. Have one set of
1103// code to handle them here.
1104//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001105// Returns true on error, false if no error
1106//
Jamie Madill075edd82013-07-08 13:30:19 -04001107bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 if (variable == 0) {
1113 if (reservedErrorCheck(line, identifier))
1114 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001116 if (voidErrorCheck(line, identifier, pType))
1117 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 //
1120 // add variable to symbol table
1121 //
1122 variable = new TVariable(&identifier, type);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001123 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001124 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 return true;
1126 // don't delete variable, it's used by error recovery, and the pool
1127 // pop will take care of the memory
1128 }
1129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001131 //
1132 // identifier must be of type constant, a global, or a temporary
1133 //
1134 TQualifier qualifier = variable->getType().getQualifier();
1135 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001136 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 return true;
1138 }
1139 //
1140 // test for and propagate constant
1141 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001143 if (qualifier == EvqConst) {
1144 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001145 std::stringstream extraInfoStream;
1146 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1147 std::string extraInfo = extraInfoStream.str();
1148 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001149 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001150 return true;
1151 }
1152 if (type != initializer->getType()) {
1153 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001154 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001155 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001156 return true;
1157 }
1158 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001159 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001161 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001162 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001164 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 variable->shareConstPointer(constArray);
1166 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001167 std::stringstream extraInfoStream;
1168 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1169 std::string extraInfo = extraInfoStream.str();
1170 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001171 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001172 return true;
1173 }
1174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 if (qualifier != EvqConst) {
1177 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuahod6b14282015-03-17 14:31:35 +02001178 intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 if (intermNode == 0) {
1180 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1181 return true;
1182 }
1183 } else
1184 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001186 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187}
1188
1189bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1190{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001191 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001192 if (!aggrNode->isConstructor())
1193 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001195 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001197 // check if all the child nodes are constants so that they can be inserted into
1198 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001199 TIntermSequence *sequence = aggrNode->getSequence() ;
1200 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001201 if (!(*p)->getAsTyped()->getAsConstantUnion())
1202 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001203 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001204
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001205 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206}
1207
Jamie Madilla5efff92013-06-06 11:56:47 -04001208TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001209{
1210 TPublicType returnType = typeSpecifier;
1211 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001212 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001213
1214 if (typeSpecifier.array)
1215 {
1216 error(typeSpecifier.line, "not supported", "first-class array");
1217 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001218 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001219 }
1220
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001221 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001222 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001223 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1224 {
1225 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1226 recover();
1227 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001228
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001229 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1230 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1231 {
1232 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1233 recover();
1234 }
1235 }
1236 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001237 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001238 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001239 {
Jamie Madill19571812013-08-12 15:26:34 -07001240 case EvqSmoothIn:
1241 case EvqSmoothOut:
1242 case EvqVertexOut:
1243 case EvqFragmentIn:
1244 case EvqCentroidOut:
1245 case EvqCentroidIn:
1246 if (typeSpecifier.type == EbtBool)
1247 {
1248 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1249 recover();
1250 }
1251 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1252 {
1253 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1254 recover();
1255 }
1256 break;
1257
1258 case EvqVertexIn:
1259 case EvqFragmentOut:
1260 case EvqFlatIn:
1261 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001262 if (typeSpecifier.type == EbtBool)
1263 {
1264 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1265 recover();
1266 }
1267 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001268
Jamie Madillb120eac2013-06-12 14:08:13 -04001269 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001270 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001271 }
1272
1273 return returnType;
1274}
1275
Jamie Madill075edd82013-07-08 13:30:19 -04001276TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001277{
1278 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1279 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1280
1281 if (identifier != "")
1282 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001283 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001284 recover();
1285
Olli Etuaho3739d232015-04-08 12:23:44 +03001286 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001287 recover();
1288
1289 TVariable* variable = 0;
1290
1291 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1292 recover();
1293
1294 if (variable && symbol)
1295 {
1296 symbol->setId(variable->getUniqueId());
1297 }
1298 }
1299
1300 return aggregate;
1301}
1302
Jamie Madill075edd82013-07-08 13:30:19 -04001303TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001304{
Jamie Madill51a53c72013-06-19 09:24:43 -04001305 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001306 recover();
1307
Olli Etuaho3739d232015-04-08 12:23:44 +03001308 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001309 recover();
1310
1311 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1312 {
1313 recover();
1314 }
1315
1316 TPublicType arrayType = publicType;
1317
1318 int size;
1319 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1320 {
1321 recover();
1322 }
1323 else
1324 {
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001325 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001326 }
1327
1328 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1329 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1330 TVariable* variable = 0;
1331
1332 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1333 recover();
1334
1335 if (variable && symbol)
1336 {
1337 symbol->setId(variable->getUniqueId());
1338 }
1339
1340 return aggregate;
1341}
1342
Jamie Madill075edd82013-07-08 13:30:19 -04001343TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001344{
Jamie Madilla5efff92013-06-06 11:56:47 -04001345 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001346 recover();
1347
1348 TIntermNode* intermNode;
1349 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1350 {
1351 //
1352 // Build intermediate representation
1353 //
1354 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1355 }
1356 else
1357 {
1358 recover();
1359 return NULL;
1360 }
1361}
1362
Jamie Madill47e3ec02014-08-20 16:38:33 -04001363TIntermAggregate* TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1364 const TSourceLoc &identifierLoc,
1365 const TString *identifier,
1366 const TSymbol *symbol)
1367{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001368 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001369 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1370 {
1371 recover();
1372 }
1373
1374 if (!symbol)
1375 {
1376 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1377 recover();
Jamie Madill47e3ec02014-08-20 16:38:33 -04001378 return NULL;
1379 }
1380 else
1381 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001382 const TString kGlFrontFacing("gl_FrontFacing");
1383 if (*identifier == kGlFrontFacing)
1384 {
1385 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1386 recover();
1387 return NULL;
1388 }
Jamie Madill2c433252014-12-03 12:36:54 -05001389 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001390 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1391 ASSERT(variable);
1392 const TType &type = variable->getType();
1393 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1394 *identifier, type, identifierLoc);
1395
1396 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1397 aggregate->setOp(EOpInvariantDeclaration);
1398 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001399 }
1400}
1401
Jamie Madill075edd82013-07-08 13:30:19 -04001402TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001403{
Jamie Madill502d66f2013-06-20 11:55:52 -04001404 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1405 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1406
1407 if (structQualifierErrorCheck(identifierLocation, publicType))
1408 recover();
1409
Jamie Madill0bd18df2013-06-20 11:55:52 -04001410 if (locationDeclaratorListCheck(identifierLocation, publicType))
1411 recover();
1412
Olli Etuaho3739d232015-04-08 12:23:44 +03001413 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001414 recover();
1415
1416 TVariable* variable = 0;
1417 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1418 recover();
1419 if (symbol && variable)
1420 symbol->setId(variable->getUniqueId());
1421
1422 return intermAggregate;
1423}
1424
Jamie Madill075edd82013-07-08 13:30:19 -04001425TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001426{
1427 if (structQualifierErrorCheck(identifierLocation, publicType))
1428 recover();
1429
Jamie Madill0bd18df2013-06-20 11:55:52 -04001430 if (locationDeclaratorListCheck(identifierLocation, publicType))
1431 recover();
1432
Olli Etuaho3739d232015-04-08 12:23:44 +03001433 if (nonInitConstErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001434 recover();
1435
1436 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1437 {
1438 recover();
1439 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001440 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001441 {
1442 int size;
1443 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1444 recover();
1445 TPublicType arrayType(publicType);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001446 arrayType.setArraySize(size);
Jamie Madill502d66f2013-06-20 11:55:52 -04001447 TVariable* variable = NULL;
1448 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1449 recover();
1450 TType type = TType(arrayType);
1451 type.setArraySize(size);
1452
1453 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1454 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001455
1456 return NULL;
1457}
1458
Jamie Madill075edd82013-07-08 13:30:19 -04001459TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001460{
1461 if (structQualifierErrorCheck(identifierLocation, publicType))
1462 recover();
1463
Jamie Madill0bd18df2013-06-20 11:55:52 -04001464 if (locationDeclaratorListCheck(identifierLocation, publicType))
1465 recover();
1466
Jamie Madill502d66f2013-06-20 11:55:52 -04001467 TIntermNode* intermNode;
1468 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1469 {
1470 //
1471 // build the intermediate representation
1472 //
1473 if (intermNode)
1474 {
1475 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1476 }
1477 else
1478 {
1479 return declaratorList;
1480 }
1481 }
1482 else
1483 {
1484 recover();
1485 return NULL;
1486 }
1487}
1488
Jamie Madilla295edf2013-06-06 11:56:48 -04001489void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1490{
1491 if (typeQualifier.qualifier != EvqUniform)
1492 {
1493 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1494 recover();
1495 return;
1496 }
1497
1498 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1499 ASSERT(!layoutQualifier.isEmpty());
1500
1501 if (shaderVersion < 300)
1502 {
1503 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1504 recover();
1505 return;
1506 }
1507
1508 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1509 {
1510 recover();
1511 return;
1512 }
1513
Jamie Madill099c0f32013-06-20 11:55:52 -04001514 if (layoutQualifier.matrixPacking != EmpUnspecified)
1515 {
1516 defaultMatrixPacking = layoutQualifier.matrixPacking;
1517 }
1518
Geoff Langc6856732014-02-11 09:38:55 -05001519 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001520 {
1521 defaultBlockStorage = layoutQualifier.blockStorage;
1522 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001523}
1524
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001525TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1526{
1527 TOperator op = EOpNull;
1528 if (publicType.userDef)
1529 {
1530 op = EOpConstructStruct;
1531 }
1532 else
1533 {
1534 switch (publicType.type)
1535 {
1536 case EbtFloat:
1537 if (publicType.isMatrix())
1538 {
1539 // TODO: non-square matrices
1540 switch(publicType.getCols())
1541 {
Jamie Madill28b97422013-07-08 14:01:38 -04001542 case 2: op = EOpConstructMat2; break;
1543 case 3: op = EOpConstructMat3; break;
1544 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001545 }
1546 }
1547 else
1548 {
1549 switch(publicType.getNominalSize())
1550 {
Jamie Madill28b97422013-07-08 14:01:38 -04001551 case 1: op = EOpConstructFloat; break;
1552 case 2: op = EOpConstructVec2; break;
1553 case 3: op = EOpConstructVec3; break;
1554 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001555 }
1556 }
1557 break;
1558
1559 case EbtInt:
1560 switch(publicType.getNominalSize())
1561 {
Jamie Madill28b97422013-07-08 14:01:38 -04001562 case 1: op = EOpConstructInt; break;
1563 case 2: op = EOpConstructIVec2; break;
1564 case 3: op = EOpConstructIVec3; break;
1565 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001566 }
1567 break;
1568
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001569 case EbtUInt:
1570 switch(publicType.getNominalSize())
1571 {
Jamie Madill28b97422013-07-08 14:01:38 -04001572 case 1: op = EOpConstructUInt; break;
1573 case 2: op = EOpConstructUVec2; break;
1574 case 3: op = EOpConstructUVec3; break;
1575 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001576 }
1577 break;
1578
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001579 case EbtBool:
1580 switch(publicType.getNominalSize())
1581 {
Jamie Madill28b97422013-07-08 14:01:38 -04001582 case 1: op = EOpConstructBool; break;
1583 case 2: op = EOpConstructBVec2; break;
1584 case 3: op = EOpConstructBVec3; break;
1585 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001586 }
1587 break;
1588
1589 default: break;
1590 }
1591
1592 if (op == EOpNull)
1593 {
1594 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1595 recover();
1596 publicType.type = EbtFloat;
1597 op = EOpConstructFloat;
1598 }
1599 }
1600
1601 TString tempString;
1602 TType type(publicType);
1603 return new TFunction(&tempString, type, op);
1604}
1605
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606// This function is used to test for the correctness of the parameters passed to various constructor functions
1607// and also convert them to the right datatype if it is allowed and required.
1608//
1609// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1610//
Olli Etuaho21203702014-11-13 16:16:21 +02001611TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001613 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001615 if (!aggregateArguments)
1616 {
1617 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001618 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001619 }
1620
Olli Etuahof40319e2015-03-10 14:33:00 +02001621 if (type->isArray())
1622 {
1623 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1624 TIntermSequence *args = aggregateArguments->getSequence();
1625 for (size_t i = 0; i < args->size(); i++)
1626 {
1627 const TType &argType = (*args)[i]->getAsTyped()->getType();
1628 // It has already been checked that the argument is not an array.
1629 ASSERT(!argType.isArray());
1630 if (!argType.sameElementType(*type))
1631 {
1632 error(line, "Array constructor argument has an incorrect type", "Error");
1633 recover();
1634 return nullptr;
1635 }
1636 }
1637 }
1638 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001639 {
1640 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001641 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001643 for (size_t i = 0; i < fields.size(); i++)
1644 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001645 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001646 {
1647 error(line, "Structure constructor arguments do not match structure fields", "Error");
1648 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001650 return 0;
1651 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001652 }
1653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001655 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001656 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1657 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001658 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001659 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001660 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001661 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662
Olli Etuaho21203702014-11-13 16:16:21 +02001663 // Structs should not be precision qualified, the individual members may be.
1664 // Built-in types on the other hand should be precision qualified.
1665 if (op != EOpConstructStruct)
1666 {
1667 constructor->setPrecisionFromChildren();
1668 type->setPrecision(constructor->getPrecision());
1669 }
1670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001671 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672}
1673
1674TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1675{
Olli Etuahof40319e2015-03-10 14:33:00 +02001676 // TODO: Add support for folding array constructors
1677 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 aggrNode->setType(type);
1679 if (canBeFolded) {
1680 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001681 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001682 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001683 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001684 }
1685 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001686 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001687 }
1688 if (returnVal)
1689 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001691 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001694 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695}
1696
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697//
1698// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1699// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1700// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1701// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1702// a constant matrix.
1703//
Jamie Madill075edd82013-07-08 13:30:19 -04001704TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001706 TIntermTyped* typedNode;
1707 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001709 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001710 if (tempConstantNode) {
1711 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001713 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001714 return node;
1715 }
1716 } 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 +00001717 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001718 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 return 0;
1721 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001723 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001725 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001726 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001727 std::stringstream extraInfoStream;
1728 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1729 std::string extraInfo = extraInfoStream.str();
1730 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001731 recover();
1732 fields.offsets[i] = 0;
1733 }
1734
1735 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001737 }
1738 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1739 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740}
1741
1742//
1743// This function returns the column being accessed from a constant matrix. The values are retrieved from
1744// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1745// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1746// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1747//
Jamie Madill075edd82013-07-08 13:30:19 -04001748TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001750 TIntermTyped* typedNode;
1751 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001753 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001754 std::stringstream extraInfoStream;
1755 extraInfoStream << "matrix field selection out of range '" << index << "'";
1756 std::string extraInfo = extraInfoStream.str();
1757 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001758 recover();
1759 index = 0;
1760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001762 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001763 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001764 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001765 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1766 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001767 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001768 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 return 0;
1771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001773 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774}
1775
1776
1777//
1778// This function returns an element of an array accessed from a constant array. The values are retrieved from
1779// the symbol table and parse-tree is built for the type of the element. The input
1780// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1781// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1782//
Jamie Madill075edd82013-07-08 13:30:19 -04001783TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001785 TIntermTyped* typedNode;
1786 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1787 TType arrayElementType = node->getType();
1788 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001790 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001791 std::stringstream extraInfoStream;
1792 extraInfoStream << "array field selection out of range '" << index << "'";
1793 std::string extraInfo = extraInfoStream.str();
1794 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001795 recover();
1796 index = 0;
1797 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001799 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001800 size_t arrayElementSize = arrayElementType.getObjectSize();
1801 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1802 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001803 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001804 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001805 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001807 return 0;
1808 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001809
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001810 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811}
1812
1813
1814//
1815// This function returns the value of a particular field inside a constant structure from the symbol table.
1816// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1817// function and returns the parse-tree with the values of the embedded/nested struct.
1818//
Jamie Madill075edd82013-07-08 13:30:19 -04001819TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820{
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001822 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
Jamie Madill98493dd2013-07-08 14:39:03 -04001824 for (size_t index = 0; index < fields.size(); ++index) {
1825 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 break;
1827 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001828 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001829 }
1830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
Jamie Madill94bf7f22013-07-08 13:31:15 -04001832 TIntermTyped *typedNode;
1833 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001835 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1838 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001839 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 return 0;
1843 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846}
1847
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001848//
1849// Interface/uniform blocks
1850//
Jamie Madill98493dd2013-07-08 14:39:03 -04001851TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1852 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001853{
1854 if (reservedErrorCheck(nameLine, blockName))
1855 recover();
1856
1857 if (typeQualifier.qualifier != EvqUniform)
1858 {
1859 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1860 recover();
1861 }
1862
Jamie Madill099c0f32013-06-20 11:55:52 -04001863 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1864 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001865 {
1866 recover();
1867 }
1868
Jamie Madill099c0f32013-06-20 11:55:52 -04001869 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1870 {
1871 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1872 }
1873
Jamie Madill1566ef72013-06-20 11:55:54 -04001874 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1875 {
1876 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1877 }
1878
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001879 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001880 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001881 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1882 recover();
1883 }
1884
Jamie Madill98493dd2013-07-08 14:39:03 -04001885 // check for sampler types and apply layout qualifiers
1886 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1887 TField* field = (*fieldList)[memberIndex];
1888 TType* fieldType = field->type();
1889 if (IsSampler(fieldType->getBasicType())) {
1890 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001891 recover();
1892 }
1893
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001895 switch (qualifier)
1896 {
1897 case EvqGlobal:
1898 case EvqUniform:
1899 break;
1900 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001901 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001902 recover();
1903 break;
1904 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001905
1906 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001907 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1908 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001909 {
1910 recover();
1911 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001912
Jamie Madill98493dd2013-07-08 14:39:03 -04001913 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001914 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001916 recover();
1917 }
1918
Jamie Madill98493dd2013-07-08 14:39:03 -04001919 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001920 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001921 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001922 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001923 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001924 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001925 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001926 recover();
1927 }
1928
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001930 }
1931
Jamie Madill98493dd2013-07-08 14:39:03 -04001932 // add array index
1933 int arraySize = 0;
1934 if (arrayIndex != NULL)
1935 {
1936 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1937 recover();
1938 }
1939
1940 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1941 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001942
1943 TString symbolName = "";
1944 int symbolId = 0;
1945
Jamie Madill98493dd2013-07-08 14:39:03 -04001946 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001947 {
1948 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001949 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1950 {
1951 TField* field = (*fieldList)[memberIndex];
1952 TType* fieldType = field->type();
1953
1954 // set parent pointer of the field variable
1955 fieldType->setInterfaceBlock(interfaceBlock);
1956
1957 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1958 fieldVariable->setQualifier(typeQualifier.qualifier);
1959
Nicolas Capensadfffe42014-06-17 02:13:36 -04001960 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001961 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001962 recover();
1963 }
1964 }
1965 }
1966 else
1967 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001968 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001969 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001970 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001971
Nicolas Capensadfffe42014-06-17 02:13:36 -04001972 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001973 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001974 recover();
1975 }
1976
1977 symbolId = instanceTypeDef->getUniqueId();
1978 symbolName = instanceTypeDef->getName();
1979 }
1980
Jamie Madill98493dd2013-07-08 14:39:03 -04001981 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001982 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001983
1984 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001985 return aggregate;
1986}
1987
Jamie Madill075edd82013-07-08 13:30:19 -04001988bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001989{
1990 ++structNestingLevel;
1991
1992 // Embedded structure definitions are not supported per GLSL ES spec.
1993 // They aren't allowed in GLSL either, but we need to detect this here
1994 // so we don't rely on the GLSL compiler to catch it.
1995 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001996 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001997 return true;
1998 }
1999
2000 return false;
2001}
2002
2003void TParseContext::exitStructDeclaration()
2004{
2005 --structNestingLevel;
2006}
2007
2008namespace {
2009
2010const int kWebGLMaxStructNesting = 4;
2011
2012} // namespace
2013
Jamie Madill98493dd2013-07-08 14:39:03 -04002014bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002015{
Jamie Madill5508f392014-02-20 13:31:36 -05002016 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002017 return false;
2018 }
2019
Jamie Madill98493dd2013-07-08 14:39:03 -04002020 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002021 return false;
2022 }
2023
2024 // We're already inside a structure definition at this point, so add
2025 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002026 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002027 std::stringstream reasonStream;
2028 reasonStream << "Reference of struct type "
2029 << field.type()->getStruct()->name().c_str()
2030 << " exceeds maximum allowed nesting level of "
2031 << kWebGLMaxStructNesting;
2032 std::string reason = reasonStream.str();
2033 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002034 return true;
2035 }
2036
2037 return false;
2038}
2039
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002040//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002041// Parse an array index expression
2042//
Jamie Madill075edd82013-07-08 13:30:19 -04002043TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002044{
2045 TIntermTyped *indexedExpression = NULL;
2046
2047 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2048 {
2049 if (baseExpression->getAsSymbolNode())
2050 {
2051 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2052 }
2053 else
2054 {
2055 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2056 }
2057 recover();
2058 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002059
Jamie Madill21c1e452014-12-29 11:33:41 -05002060 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2061
2062 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002063 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002064 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002065 if (index < 0)
2066 {
2067 std::stringstream infoStream;
2068 infoStream << index;
2069 std::string info = infoStream.str();
2070 error(location, "negative index", info.c_str());
2071 recover();
2072 index = 0;
2073 }
2074 if (baseExpression->getType().getQualifier() == EvqConst)
2075 {
2076 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002077 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002078 // constant folding for arrays
2079 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002080 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002081 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002082 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002083 // constant folding for vectors
2084 TVectorFields fields;
2085 fields.num = 1;
2086 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2087 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2088 }
2089 else if (baseExpression->isMatrix())
2090 {
2091 // constant folding for matrices
2092 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002093 }
2094 }
2095 else
2096 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002097 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002098 {
Jamie Madill18464b52013-07-08 14:01:55 -04002099 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002100 {
2101 std::stringstream extraInfoStream;
2102 extraInfoStream << "array index out of range '" << index << "'";
2103 std::string extraInfo = extraInfoStream.str();
2104 error(location, "", "[", extraInfo.c_str());
2105 recover();
2106 index = baseExpression->getType().getArraySize() - 1;
2107 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002108 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2109 {
2110 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2111 recover();
2112 index = 0;
2113 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002114 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002115 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002116 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002117 std::stringstream extraInfoStream;
2118 extraInfoStream << "field selection out of range '" << index << "'";
2119 std::string extraInfo = extraInfoStream.str();
2120 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002121 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002122 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002123 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002124
Jamie Madill21c1e452014-12-29 11:33:41 -05002125 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002126 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002127 }
2128 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002129 else
2130 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002131 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002132 {
Jamie Madill19571812013-08-12 15:26:34 -07002133 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002134 recover();
2135 }
Jamie Madill19571812013-08-12 15:26:34 -07002136 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002137 {
Jamie Madill19571812013-08-12 15:26:34 -07002138 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002139 recover();
2140 }
2141
2142 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2143 }
2144
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002145 if (indexedExpression == 0)
2146 {
2147 ConstantUnion *unionArray = new ConstantUnion[1];
2148 unionArray->setFConst(0.0f);
2149 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2150 }
2151 else if (baseExpression->isArray())
2152 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002153 const TType &baseType = baseExpression->getType();
2154 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002155 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002156 TType copyOfType(baseType.getStruct());
2157 indexedExpression->setType(copyOfType);
2158 }
2159 else if (baseType.isInterfaceBlock())
2160 {
2161 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002162 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002163 }
2164 else
2165 {
Minmin Gong3b26e232015-04-07 18:31:54 -07002166 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2167 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002168 }
2169
2170 if (baseExpression->getType().getQualifier() == EvqConst)
2171 {
2172 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2173 }
2174 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002175 else if (baseExpression->isMatrix())
2176 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002177 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong3b26e232015-04-07 18:31:54 -07002178 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002179 }
2180 else if (baseExpression->isVector())
2181 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002182 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2183 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002184 }
2185 else
2186 {
2187 indexedExpression->setType(baseExpression->getType());
2188 }
2189
2190 return indexedExpression;
2191}
2192
Jamie Madill075edd82013-07-08 13:30:19 -04002193TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002194{
2195 TIntermTyped *indexedExpression = NULL;
2196
2197 if (baseExpression->isArray())
2198 {
2199 error(fieldLocation, "cannot apply dot operator to an array", ".");
2200 recover();
2201 }
2202
2203 if (baseExpression->isVector())
2204 {
2205 TVectorFields fields;
2206 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2207 {
2208 fields.num = 1;
2209 fields.offsets[0] = 0;
2210 recover();
2211 }
2212
2213 if (baseExpression->getType().getQualifier() == EvqConst)
2214 {
2215 // constant folding for vector fields
2216 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2217 if (indexedExpression == 0)
2218 {
2219 recover();
2220 indexedExpression = baseExpression;
2221 }
2222 else
2223 {
Minmin Gong3b26e232015-04-07 18:31:54 -07002224 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002225 }
2226 }
2227 else
2228 {
2229 TString vectorString = fieldString;
2230 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2231 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong3b26e232015-04-07 18:31:54 -07002232 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002233 }
2234 }
2235 else if (baseExpression->isMatrix())
2236 {
2237 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002238 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002239 {
2240 fields.wholeRow = false;
2241 fields.wholeCol = false;
2242 fields.row = 0;
2243 fields.col = 0;
2244 recover();
2245 }
2246
2247 if (fields.wholeRow || fields.wholeCol)
2248 {
2249 error(dotLocation, " non-scalar fields not implemented yet", ".");
2250 recover();
2251 ConstantUnion *unionArray = new ConstantUnion[1];
2252 unionArray->setIConst(0);
2253 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2254 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong3b26e232015-04-07 18:31:54 -07002255 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2256 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002257 }
2258 else
2259 {
2260 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002261 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002262 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2263 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2264 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2265 }
2266 }
2267 else if (baseExpression->getBasicType() == EbtStruct)
2268 {
2269 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002270 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2271 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002272 {
2273 error(dotLocation, "structure has no fields", "Internal Error");
2274 recover();
2275 indexedExpression = baseExpression;
2276 }
2277 else
2278 {
2279 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002280 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002281 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002282 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002283 {
2284 fieldFound = true;
2285 break;
2286 }
2287 }
2288 if (fieldFound)
2289 {
2290 if (baseExpression->getType().getQualifier() == EvqConst)
2291 {
2292 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2293 if (indexedExpression == 0)
2294 {
2295 recover();
2296 indexedExpression = baseExpression;
2297 }
2298 else
2299 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002300 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002301 // change the qualifier of the return type, not of the structure field
2302 // as the structure definition is shared between various structures.
2303 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2304 }
2305 }
2306 else
2307 {
2308 ConstantUnion *unionArray = new ConstantUnion[1];
2309 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002310 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002311 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002312 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002313 }
2314 }
2315 else
2316 {
2317 error(dotLocation, " no such field in structure", fieldString.c_str());
2318 recover();
2319 indexedExpression = baseExpression;
2320 }
2321 }
2322 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002323 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002324 {
2325 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002326 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2327 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002328 {
2329 error(dotLocation, "interface block has no fields", "Internal Error");
2330 recover();
2331 indexedExpression = baseExpression;
2332 }
2333 else
2334 {
2335 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002336 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002337 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002338 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002339 {
2340 fieldFound = true;
2341 break;
2342 }
2343 }
2344 if (fieldFound)
2345 {
2346 ConstantUnion *unionArray = new ConstantUnion[1];
2347 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002348 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002349 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002350 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002351 }
2352 else
2353 {
2354 error(dotLocation, " no such field in interface block", fieldString.c_str());
2355 recover();
2356 indexedExpression = baseExpression;
2357 }
2358 }
2359 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002360 else
2361 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002362 if (shaderVersion < 300)
2363 {
2364 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2365 }
2366 else
2367 {
2368 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2369 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 recover();
2371 indexedExpression = baseExpression;
2372 }
2373
2374 return indexedExpression;
2375}
2376
Jamie Madill075edd82013-07-08 13:30:19 -04002377TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002378{
Jamie Madilla5efff92013-06-06 11:56:47 -04002379 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002380
Jamie Madilla5efff92013-06-06 11:56:47 -04002381 qualifier.location = -1;
2382 qualifier.matrixPacking = EmpUnspecified;
2383 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002384
2385 if (qualifierType == "shared")
2386 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002387 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002388 }
2389 else if (qualifierType == "packed")
2390 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002391 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002392 }
2393 else if (qualifierType == "std140")
2394 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002395 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002396 }
2397 else if (qualifierType == "row_major")
2398 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002399 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002400 }
2401 else if (qualifierType == "column_major")
2402 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002403 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002404 }
2405 else if (qualifierType == "location")
2406 {
2407 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2408 recover();
2409 }
2410 else
2411 {
2412 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2413 recover();
2414 }
2415
Jamie Madilla5efff92013-06-06 11:56:47 -04002416 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002417}
2418
Jamie Madill075edd82013-07-08 13:30:19 -04002419TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002420{
Jamie Madilla5efff92013-06-06 11:56:47 -04002421 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002422
Jamie Madilla5efff92013-06-06 11:56:47 -04002423 qualifier.location = -1;
2424 qualifier.matrixPacking = EmpUnspecified;
2425 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002426
2427 if (qualifierType != "location")
2428 {
2429 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2430 recover();
2431 }
2432 else
2433 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002434 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002435 if (intValue < 0)
2436 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002437 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002438 recover();
2439 }
2440 else
2441 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002442 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002443 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002444 }
2445
Jamie Madilla5efff92013-06-06 11:56:47 -04002446 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002447}
2448
Jamie Madilla5efff92013-06-06 11:56:47 -04002449TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002450{
Jamie Madilla5efff92013-06-06 11:56:47 -04002451 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002452
Jamie Madilla5efff92013-06-06 11:56:47 -04002453 if (rightQualifier.location != -1)
2454 {
2455 joinedQualifier.location = rightQualifier.location;
2456 }
2457 if (rightQualifier.matrixPacking != EmpUnspecified)
2458 {
2459 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2460 }
2461 if (rightQualifier.blockStorage != EbsUnspecified)
2462 {
2463 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2464 }
2465
2466 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002467}
2468
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002469TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2470 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2471{
2472 TQualifier mergedQualifier = EvqSmoothIn;
2473
Jamie Madill19571812013-08-12 15:26:34 -07002474 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002475 if (interpolationQualifier == EvqSmooth)
2476 mergedQualifier = EvqSmoothIn;
2477 else if (interpolationQualifier == EvqFlat)
2478 mergedQualifier = EvqFlatIn;
2479 else UNREACHABLE();
2480 }
2481 else if (storageQualifier == EvqCentroidIn) {
2482 if (interpolationQualifier == EvqSmooth)
2483 mergedQualifier = EvqCentroidIn;
2484 else if (interpolationQualifier == EvqFlat)
2485 mergedQualifier = EvqFlatIn;
2486 else UNREACHABLE();
2487 }
Jamie Madill19571812013-08-12 15:26:34 -07002488 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002489 if (interpolationQualifier == EvqSmooth)
2490 mergedQualifier = EvqSmoothOut;
2491 else if (interpolationQualifier == EvqFlat)
2492 mergedQualifier = EvqFlatOut;
2493 else UNREACHABLE();
2494 }
2495 else if (storageQualifier == EvqCentroidOut) {
2496 if (interpolationQualifier == EvqSmooth)
2497 mergedQualifier = EvqCentroidOut;
2498 else if (interpolationQualifier == EvqFlat)
2499 mergedQualifier = EvqFlatOut;
2500 else UNREACHABLE();
2501 }
2502 else {
2503 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2504 recover();
2505
2506 mergedQualifier = storageQualifier;
2507 }
2508
2509 TPublicType type;
2510 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2511 return type;
2512}
2513
Jamie Madill98493dd2013-07-08 14:39:03 -04002514TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002515{
Jamie Madill98493dd2013-07-08 14:39:03 -04002516 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002517 recover();
2518 }
2519
Jamie Madill98493dd2013-07-08 14:39:03 -04002520 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002521 //
2522 // Careful not to replace already known aspects of type, like array-ness
2523 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002524 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002525 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002526 type->setPrimarySize(typeSpecifier.primarySize);
2527 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002528 type->setPrecision(typeSpecifier.precision);
2529 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002530 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002531
2532 // don't allow arrays of arrays
2533 if (type->isArray()) {
2534 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2535 recover();
2536 }
2537 if (typeSpecifier.array)
2538 type->setArraySize(typeSpecifier.arraySize);
2539 if (typeSpecifier.userDef) {
2540 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002541 }
2542
Jamie Madill98493dd2013-07-08 14:39:03 -04002543 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002544 recover();
2545 }
2546 }
2547
Jamie Madill98493dd2013-07-08 14:39:03 -04002548 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002549}
2550
Jamie Madill98493dd2013-07-08 14:39:03 -04002551TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002552{
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 TStructure* structure = new TStructure(structName, fieldList);
2554 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002555
Jamie Madill9b820842015-02-12 10:40:10 -05002556 // Store a bool in the struct if we're at global scope, to allow us to
2557 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002558 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002559 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002560
Jamie Madill98493dd2013-07-08 14:39:03 -04002561 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002563 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002564 {
2565 recover();
2566 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002567 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002568 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002569 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002570 recover();
2571 }
2572 }
2573
2574 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002575 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002576 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002577 const TField &field = *(*fieldList)[typeListIndex];
2578 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002579 switch (qualifier)
2580 {
2581 case EvqGlobal:
2582 case EvqTemporary:
2583 break;
2584 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002585 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002586 recover();
2587 break;
2588 }
2589 }
2590
2591 TPublicType publicType;
2592 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002593 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002594 exitStructDeclaration();
2595
2596 return publicType;
2597}
2598
Olli Etuahoa3a36662015-02-17 13:46:51 +02002599TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2600{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002601 TBasicType switchType = init->getBasicType();
2602 if ((switchType != EbtInt && switchType != EbtUInt) ||
2603 init->isMatrix() ||
2604 init->isArray() ||
2605 init->isVector())
2606 {
2607 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2608 recover();
2609 return nullptr;
2610 }
2611
Olli Etuahoac5274d2015-02-20 10:19:08 +02002612 if (statementList)
2613 {
2614 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2615 {
2616 recover();
2617 return nullptr;
2618 }
2619 }
2620
Olli Etuahoa3a36662015-02-17 13:46:51 +02002621 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2622 if (node == nullptr)
2623 {
2624 error(loc, "erroneous switch statement", "switch");
2625 recover();
2626 return nullptr;
2627 }
2628 return node;
2629}
2630
2631TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2632{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002633 if (mSwitchNestingLevel == 0)
2634 {
2635 error(loc, "case labels need to be inside switch statements", "case");
2636 recover();
2637 return nullptr;
2638 }
2639 if (condition == nullptr)
2640 {
2641 error(loc, "case label must have a condition", "case");
2642 recover();
2643 return nullptr;
2644 }
2645 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2646 condition->isMatrix() ||
2647 condition->isArray() ||
2648 condition->isVector())
2649 {
2650 error(condition->getLine(), "case label must be a scalar integer", "case");
2651 recover();
2652 }
2653 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2654 if (conditionConst == nullptr)
2655 {
2656 error(condition->getLine(), "case label must be constant", "case");
2657 recover();
2658 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002659 TIntermCase *node = intermediate.addCase(condition, loc);
2660 if (node == nullptr)
2661 {
2662 error(loc, "erroneous case statement", "case");
2663 recover();
2664 return nullptr;
2665 }
2666 return node;
2667}
2668
2669TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2670{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002671 if (mSwitchNestingLevel == 0)
2672 {
2673 error(loc, "default labels need to be inside switch statements", "default");
2674 recover();
2675 return nullptr;
2676 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002677 TIntermCase *node = intermediate.addCase(nullptr, loc);
2678 if (node == nullptr)
2679 {
2680 error(loc, "erroneous default statement", "default");
2681 recover();
2682 return nullptr;
2683 }
2684 return node;
2685}
2686
Olli Etuahof6c694b2015-03-26 14:50:53 +02002687TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2688 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002689{
2690 if (child == nullptr)
2691 {
2692 return nullptr;
2693 }
2694
2695 switch (op)
2696 {
2697 case EOpLogicalNot:
2698 if (child->getBasicType() != EbtBool ||
2699 child->isMatrix() ||
2700 child->isArray() ||
2701 child->isVector())
2702 {
2703 return nullptr;
2704 }
2705 break;
2706 case EOpBitwiseNot:
2707 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2708 child->isMatrix() ||
2709 child->isArray())
2710 {
2711 return nullptr;
2712 }
2713 break;
2714 case EOpPostIncrement:
2715 case EOpPreIncrement:
2716 case EOpPostDecrement:
2717 case EOpPreDecrement:
2718 case EOpNegative:
2719 case EOpPositive:
2720 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002721 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002722 child->isArray())
2723 {
2724 return nullptr;
2725 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002726 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002727 default:
2728 break;
2729 }
2730
Olli Etuahof6c694b2015-03-26 14:50:53 +02002731 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002732}
2733
Olli Etuaho09b22472015-02-11 11:47:26 +02002734TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2735{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002736 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002737 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002738 {
2739 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2740 recover();
2741 return child;
2742 }
2743 return node;
2744}
2745
2746TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2747{
2748 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2749 recover();
2750 return addUnaryMath(op, child, loc);
2751}
2752
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002753bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002754 const TSourceLoc &loc)
2755{
2756 if (left->isArray() || right->isArray())
2757 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002758 if (shaderVersion < 300)
2759 {
2760 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2761 return false;
2762 }
2763
2764 if (left->isArray() != right->isArray())
2765 {
2766 error(loc, "array / non-array mismatch", GetOperatorString(op));
2767 return false;
2768 }
2769
2770 switch (op)
2771 {
2772 case EOpEqual:
2773 case EOpNotEqual:
2774 case EOpAssign:
2775 case EOpInitialize:
2776 break;
2777 default:
2778 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2779 return false;
2780 }
2781 if (left->getArraySize() != right->getArraySize())
2782 {
2783 error(loc, "array size mismatch", GetOperatorString(op));
2784 return false;
2785 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002786 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002787
2788 // Check ops which require integer / ivec parameters
2789 bool isBitShift = false;
2790 switch (op)
2791 {
2792 case EOpBitShiftLeft:
2793 case EOpBitShiftRight:
2794 case EOpBitShiftLeftAssign:
2795 case EOpBitShiftRightAssign:
2796 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2797 // check that the basic type is an integer type.
2798 isBitShift = true;
2799 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2800 {
2801 return false;
2802 }
2803 break;
2804 case EOpBitwiseAnd:
2805 case EOpBitwiseXor:
2806 case EOpBitwiseOr:
2807 case EOpBitwiseAndAssign:
2808 case EOpBitwiseXorAssign:
2809 case EOpBitwiseOrAssign:
2810 // It is enough to check the type of only one operand, since later it
2811 // is checked that the operand types match.
2812 if (!IsInteger(left->getBasicType()))
2813 {
2814 return false;
2815 }
2816 break;
2817 default:
2818 break;
2819 }
2820
2821 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2822 // So the basic type should usually match.
2823 if (!isBitShift && left->getBasicType() != right->getBasicType())
2824 {
2825 return false;
2826 }
2827
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002828 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002829 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002830 switch(op)
2831 {
2832 case EOpAssign:
2833 case EOpInitialize:
2834 case EOpEqual:
2835 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002836 // ESSL 1.00 sections 5.7, 5.8, 5.9
2837 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2838 {
2839 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2840 return false;
2841 }
Olli Etuahoff699002015-03-23 14:38:42 +02002842 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2843 // we interpret the spec so that this extends to structs containing samplers,
2844 // similarly to ESSL 1.00 spec.
2845 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2846 left->getType().isStructureContainingSamplers())
2847 {
2848 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2849 return false;
2850 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002851 case EOpLessThan:
2852 case EOpGreaterThan:
2853 case EOpLessThanEqual:
2854 case EOpGreaterThanEqual:
2855 if ((left->getNominalSize() != right->getNominalSize()) ||
2856 (left->getSecondarySize() != right->getSecondarySize()))
2857 {
2858 return false;
2859 }
2860 default:
2861 break;
2862 }
2863
Olli Etuahod6b14282015-03-17 14:31:35 +02002864 return true;
2865}
2866
Olli Etuahofc1806e2015-03-17 13:03:11 +02002867TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2868 const TSourceLoc &loc)
2869{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002870 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002871 return nullptr;
2872
Olli Etuahofc1806e2015-03-17 13:03:11 +02002873 switch (op)
2874 {
2875 case EOpEqual:
2876 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002877 break;
2878 case EOpLessThan:
2879 case EOpGreaterThan:
2880 case EOpLessThanEqual:
2881 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002882 ASSERT(!left->isArray() && !right->isArray());
2883 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002884 left->getBasicType() == EbtStruct)
2885 {
2886 return nullptr;
2887 }
2888 break;
2889 case EOpLogicalOr:
2890 case EOpLogicalXor:
2891 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02002892 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002893 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02002894 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02002895 {
2896 return nullptr;
2897 }
2898 break;
2899 case EOpAdd:
2900 case EOpSub:
2901 case EOpDiv:
2902 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02002903 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002904 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
2905 {
2906 return nullptr;
2907 }
2908 break;
2909 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02002910 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002911 // Note that this is only for the % operator, not for mod()
2912 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
2913 {
2914 return nullptr;
2915 }
2916 break;
2917 // Note that for bitwise ops, type checking is done in promote() to
2918 // share code between ops and compound assignment
2919 default:
2920 break;
2921 }
2922
Olli Etuahofc1806e2015-03-17 13:03:11 +02002923 return intermediate.addBinaryMath(op, left, right, loc);
2924}
2925
Olli Etuaho09b22472015-02-11 11:47:26 +02002926TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
2927 const TSourceLoc &loc)
2928{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002929 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002930 if (node == 0)
2931 {
2932 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2933 recover();
2934 return left;
2935 }
2936 return node;
2937}
2938
2939TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
2940 const TSourceLoc &loc)
2941{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002942 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002943 if (node == 0)
2944 {
2945 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2946 recover();
2947 ConstantUnion *unionArray = new ConstantUnion[1];
2948 unionArray->setBConst(false);
2949 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
2950 }
2951 return node;
2952}
2953
Olli Etuahod6b14282015-03-17 14:31:35 +02002954TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2955 const TSourceLoc &loc)
2956{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002957 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002958 {
2959 return intermediate.addAssign(op, left, right, loc);
2960 }
2961 return nullptr;
2962}
2963
2964TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2965 const TSourceLoc &loc)
2966{
2967 TIntermTyped *node = createAssign(op, left, right, loc);
2968 if (node == nullptr)
2969 {
2970 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
2971 recover();
2972 return left;
2973 }
2974 return node;
2975}
2976
Olli Etuaho49300862015-02-20 14:54:49 +02002977TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
2978{
2979 switch (op)
2980 {
2981 case EOpContinue:
2982 if (mLoopNestingLevel <= 0)
2983 {
2984 error(loc, "continue statement only allowed in loops", "");
2985 recover();
2986 }
2987 break;
2988 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02002989 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02002990 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02002991 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02002992 recover();
2993 }
2994 break;
2995 case EOpReturn:
2996 if (currentFunctionType->getBasicType() != EbtVoid)
2997 {
2998 error(loc, "non-void function must return a value", "return");
2999 recover();
3000 }
3001 break;
3002 default:
3003 // No checks for discard
3004 break;
3005 }
3006 return intermediate.addBranch(op, loc);
3007}
3008
3009TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3010{
3011 ASSERT(op == EOpReturn);
3012 mFunctionReturnsValue = true;
3013 if (currentFunctionType->getBasicType() == EbtVoid)
3014 {
3015 error(loc, "void function cannot return a value", "return");
3016 recover();
3017 }
3018 else if (*currentFunctionType != returnValue->getType())
3019 {
3020 error(loc, "function return is not matching type:", "return");
3021 recover();
3022 }
3023 return intermediate.addBranch(op, returnValue, loc);
3024}
3025
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003026TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *node,
3027 const TSourceLoc &loc, bool *fatalError)
3028{
3029 *fatalError = false;
3030 TOperator op = fnCall->getBuiltInOp();
3031 TIntermTyped *callNode = nullptr;
3032
3033 if (op != EOpNull)
3034 {
3035 //
3036 // Then this should be a constructor.
3037 // Don't go through the symbol table for constructors.
3038 // Their parameters will be verified algorithmically.
3039 //
3040 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3041 if (!constructorErrorCheck(loc, node, *fnCall, op, &type))
3042 {
3043 //
3044 // It's a constructor, of type 'type'.
3045 //
3046 callNode = addConstructor(node, &type, op, fnCall, loc);
3047 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003048
3049 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003050 {
3051 recover();
3052 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3053 }
3054 callNode->setType(type);
3055 }
3056 else
3057 {
3058 //
3059 // Not a constructor. Find it in the symbol table.
3060 //
3061 const TFunction* fnCandidate;
3062 bool builtIn;
3063 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3064 if (fnCandidate)
3065 {
3066 //
3067 // A declared function.
3068 //
3069 if (builtIn && !fnCandidate->getExtension().empty() &&
3070 extensionErrorCheck(loc, fnCandidate->getExtension()))
3071 {
3072 recover();
3073 }
3074 op = fnCandidate->getBuiltInOp();
3075 if (builtIn && op != EOpNull)
3076 {
3077 //
3078 // A function call mapped to a built-in operation.
3079 //
3080 if (fnCandidate->getParamCount() == 1)
3081 {
3082 //
3083 // Treat it like a built-in unary operator.
3084 //
Olli Etuahof6c694b2015-03-26 14:50:53 +02003085 callNode = createUnaryMath(op, node->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003086 if (callNode == nullptr)
3087 {
3088 std::stringstream extraInfoStream;
3089 extraInfoStream << "built in unary operator function. Type: "
3090 << static_cast<TIntermTyped*>(node)->getCompleteString();
3091 std::string extraInfo = extraInfoStream.str();
3092 error(node->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3093 *fatalError = true;
3094 return nullptr;
3095 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003096 }
3097 else
3098 {
3099 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, op, loc);
3100 aggregate->setType(fnCandidate->getReturnType());
3101 aggregate->setPrecisionFromChildren();
3102 callNode = aggregate;
3103
3104 // Some built-in functions have out parameters too.
3105 functionCallLValueErrorCheck(fnCandidate, aggregate);
3106 }
3107 }
3108 else
3109 {
3110 // This is a real function call
3111
3112 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, EOpFunctionCall, loc);
3113 aggregate->setType(fnCandidate->getReturnType());
3114
3115 // this is how we know whether the given function is a builtIn function or a user defined function
3116 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3117 // if builtIn == true, it's definitely a builtIn function with EOpNull
3118 if (!builtIn)
3119 aggregate->setUserDefined();
3120 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003121 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003122
3123 // This needs to happen after the name is set
3124 if (builtIn)
3125 aggregate->setBuiltInFunctionPrecision();
3126
3127 callNode = aggregate;
3128
3129 functionCallLValueErrorCheck(fnCandidate, aggregate);
3130 }
3131 }
3132 else
3133 {
3134 // error message was put out by findFunction()
3135 // Put on a dummy node for error recovery
3136 ConstantUnion *unionArray = new ConstantUnion[1];
3137 unionArray->setFConst(0.0f);
3138 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3139 recover();
3140 }
3141 }
3142 delete fnCall;
3143 return callNode;
3144}
3145
Olli Etuaho49300862015-02-20 14:54:49 +02003146
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003147//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003148// Parse an array of strings using yyparse.
3149//
3150// Returns 0 for success.
3151//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003152int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003153 TParseContext* context) {
3154 if ((count == 0) || (string == NULL))
3155 return 1;
3156
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003157 if (glslang_initialize(context))
3158 return 1;
3159
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003160 int error = glslang_scan(count, string, length, context);
3161 if (!error)
3162 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003163
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003164 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003165
alokp@chromium.org6b495712012-06-29 00:06:58 +00003166 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003167}
3168
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003170