blob: 86eeb6d029cdf0fe937be901146a16dbe8016b8b [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//
Jamie Madill075edd82013-07-08 13:30:19 -0400753bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754{
Jamie Madill19571812013-08-12 15:26:34 -0700755 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000756 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 return true;
758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000760 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
763//
764// See if this type can be an array.
765//
766// Returns true if there is an error.
767//
Jamie Madill075edd82013-07-08 13:30:19 -0400768bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000770 //
771 // Can the type be an array?
772 //
773 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000774 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 return true;
776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779}
780
781//
782// Do all the semantic checking for declaring an array, with and
783// without a size, and make the right changes to the symbol table.
784//
785// size == 0 means no specified size.
786//
787// Returns true if there was an error.
788//
Jamie Madill075edd82013-07-08 13:30:19 -0400789bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 //
792 // Don't check for reserved word use until after we know it's not in the symbol table,
793 // because reserved arrays can be redeclared.
794 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 bool builtIn = false;
797 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000798 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 if (symbol == 0 || !sameScope) {
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100800 bool needsReservedErrorCheck = true;
801
802 // gl_LastFragData may be redeclared with a new precision qualifier
803 if (identifier.compare(0, 15, "gl_LastFragData") == 0) {
804 if (type.arraySize == static_cast<const TVariable*>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion))->getConstPointer()->getIConst()) {
805 if (TSymbol* builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion)) {
806 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
807 }
808 } else {
809 error(line, "redeclaration of array with size != gl_MaxDrawBuffers", identifier.c_str());
810 return true;
811 }
812 }
813
814 if (needsReservedErrorCheck)
815 if (reservedErrorCheck(line, identifier))
816 return true;
817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 if (type.arraySize)
821 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
Nicolas Capensadfffe42014-06-17 02:13:36 -0400823 if (! symbolTable.declare(variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000825 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
828 } else {
829 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000830 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000831 return true;
832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 variable = static_cast<TVariable*>(symbol);
835 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000836 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 return true;
838 }
839 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000840 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000841 return true;
842 }
843
844 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000845 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 return true;
847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 if (type.arraySize)
850 variable->getType().setArraySize(type.arraySize);
851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 if (voidErrorCheck(line, identifier, type))
854 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859//
860// Enforce non-initializer type/qualifier rules.
861//
862// Returns true if there was an error.
863//
Jamie Madill075edd82013-07-08 13:30:19 -0400864bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000866 if (type.qualifier == EvqConst)
867 {
868 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000870
871 if (array)
872 {
873 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
874 }
875 else if (type.isStructureContainingArrays())
876 {
877 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
878 }
879 else
880 {
881 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
882 }
883
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 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
890//
891// Do semantic checking for a variable declaration that has no initializer,
892// and update the symbol table.
893//
894// Returns true if there was an error.
895//
Jamie Madill075edd82013-07-08 13:30:19 -0400896bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 if (reservedErrorCheck(line, identifier))
899 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
zmo@google.comfd747b82011-04-23 01:30:07 +0000901 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
Nicolas Capensadfffe42014-06-17 02:13:36 -0400903 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000904 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000905 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000906 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 return true;
908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 if (voidErrorCheck(line, identifier, type))
911 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
Jamie Madill075edd82013-07-08 13:30:19 -0400916bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000919 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000920 return true;
921 }
922 if (qualifier == EvqConst && paramQualifier != EvqIn) {
923 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
924 return true;
925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000927 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000928 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000930 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
Jamie Madill075edd82013-07-08 13:30:19 -0400935bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000936{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000937 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000938 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
939 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000940 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 return true;
942 }
zmo@google.comf5450912011-09-09 01:37:19 +0000943 // In GLSL ES, an extension's default behavior is "disable".
944 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000945 error(line, "extension", extension.c_str(), "is disabled");
946 return true;
947 }
948 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000949 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000950 return false;
951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
Jamie Madill075edd82013-07-08 13:30:19 -0400956bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400957{
958 if (structQualifierErrorCheck(identifierLocation, publicType))
959 return true;
960
961 // check for layout qualifier issues
962 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
963
964 if (layoutQualifier.matrixPacking != EmpUnspecified)
965 {
966 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400967 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400968 }
969
970 if (layoutQualifier.blockStorage != EbsUnspecified)
971 {
972 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400973 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400974 }
975
Jamie Madill19571812013-08-12 15:26:34 -0700976 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400977 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400978 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400979 }
980
981 return false;
982}
983
Jamie Madill075edd82013-07-08 13:30:19 -0400984bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400985{
986 if (layoutQualifier.location != -1)
987 {
988 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
989 return true;
990 }
991
992 return false;
993}
994
Olli Etuahob6e07a62015-02-16 12:22:10 +0200995bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
996{
997 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
998 {
999 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1000 if (qual == EvqOut || qual == EvqInOut)
1001 {
1002 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1003 if (lValueErrorCheck(node->getLine(), "assign", node))
1004 {
1005 error(node->getLine(),
1006 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1007 recover();
1008 return true;
1009 }
1010 }
1011 }
1012 return false;
1013}
1014
zmo@google.com09c323a2011-08-12 18:22:25 +00001015bool TParseContext::supportsExtension(const char* extension)
1016{
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001017 const TExtensionBehavior& extbehavior = extensionBehavior();
1018 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1019 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001020}
1021
Jamie Madill5d287f52013-07-12 15:38:19 -04001022bool TParseContext::isExtensionEnabled(const char* extension) const
1023{
1024 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001025 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001026
1027 if (iter == extbehavior.end())
1028 {
1029 return false;
1030 }
1031
1032 return (iter->second == EBhEnable || iter->second == EBhRequire);
1033}
1034
Jamie Madill075edd82013-07-08 13:30:19 -04001035void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1036{
1037 pp::SourceLocation srcLoc;
1038 srcLoc.file = loc.first_file;
1039 srcLoc.line = loc.first_line;
1040 directiveHandler.handleExtension(srcLoc, extName, behavior);
1041}
1042
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001043void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001044{
1045 pp::SourceLocation srcLoc;
1046 srcLoc.file = loc.first_file;
1047 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001048 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001049}
1050
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051/////////////////////////////////////////////////////////////////////////////////
1052//
1053// Non-Errors.
1054//
1055/////////////////////////////////////////////////////////////////////////////////
1056
Jamie Madill5c097022014-08-20 16:38:32 -04001057const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1058 const TString *name,
1059 const TSymbol *symbol)
1060{
1061 const TVariable *variable = NULL;
1062
1063 if (!symbol)
1064 {
1065 error(location, "undeclared identifier", name->c_str());
1066 recover();
1067 }
1068 else if (!symbol->isVariable())
1069 {
1070 error(location, "variable expected", name->c_str());
1071 recover();
1072 }
1073 else
1074 {
1075 variable = static_cast<const TVariable*>(symbol);
1076
1077 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1078 !variable->getExtension().empty() &&
1079 extensionErrorCheck(location, variable->getExtension()))
1080 {
1081 recover();
1082 }
1083 }
1084
1085 if (!variable)
1086 {
1087 TType type(EbtFloat, EbpUndefined);
1088 TVariable *fakeVariable = new TVariable(name, type);
1089 symbolTable.declare(fakeVariable);
1090 variable = fakeVariable;
1091 }
1092
1093 return variable;
1094}
1095
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096//
1097// Look up a function name in the symbol table, and make sure it is a function.
1098//
1099// Return the function symbol if found, otherwise 0.
1100//
Austin Kinross3ae64652015-01-26 15:51:39 -08001101const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001103 // First find by unmangled name to check whether the function name has been
1104 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001105 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001106 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001107 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001108 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
alokp@chromium.org0a576182010-08-09 17:16:27 +00001111 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001112 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 return 0;
1114 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
alokp@chromium.org0a576182010-08-09 17:16:27 +00001116 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001117 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 return 0;
1119 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001120
1121 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122}
1123
1124//
1125// Initializers show up in several places in the grammar. Have one set of
1126// code to handle them here.
1127//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001128// Returns true on error, false if no error
1129//
Jamie Madill075edd82013-07-08 13:30:19 -04001130bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001131 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001133 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001135 if (variable == 0) {
1136 if (reservedErrorCheck(line, identifier))
1137 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001139 if (voidErrorCheck(line, identifier, pType))
1140 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001142 //
1143 // add variable to symbol table
1144 //
1145 variable = new TVariable(&identifier, type);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001146 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001147 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 return true;
1149 // don't delete variable, it's used by error recovery, and the pool
1150 // pop will take care of the memory
1151 }
1152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001153
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001154 //
1155 // identifier must be of type constant, a global, or a temporary
1156 //
1157 TQualifier qualifier = variable->getType().getQualifier();
1158 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001159 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 return true;
1161 }
1162 //
1163 // test for and propagate constant
1164 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001166 if (qualifier == EvqConst) {
1167 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001168 std::stringstream extraInfoStream;
1169 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1170 std::string extraInfo = extraInfoStream.str();
1171 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001172 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001173 return true;
1174 }
1175 if (type != initializer->getType()) {
1176 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001177 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001178 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 return true;
1180 }
1181 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001182 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001183 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001184 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001187 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001188 variable->shareConstPointer(constArray);
1189 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001190 std::stringstream extraInfoStream;
1191 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1192 std::string extraInfo = extraInfoStream.str();
1193 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001194 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001195 return true;
1196 }
1197 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 if (qualifier != EvqConst) {
1200 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuahod6b14282015-03-17 14:31:35 +02001201 intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001202 if (intermNode == 0) {
1203 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1204 return true;
1205 }
1206 } else
1207 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001208
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001209 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210}
1211
1212bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1213{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001214 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001215 if (!aggrNode->isConstructor())
1216 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001217
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001218 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001220 // check if all the child nodes are constants so that they can be inserted into
1221 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001222 TIntermSequence *sequence = aggrNode->getSequence() ;
1223 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001224 if (!(*p)->getAsTyped()->getAsConstantUnion())
1225 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001226 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001227
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229}
1230
Jamie Madilla5efff92013-06-06 11:56:47 -04001231TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001232{
1233 TPublicType returnType = typeSpecifier;
1234 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001235 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001236
1237 if (typeSpecifier.array)
1238 {
1239 error(typeSpecifier.line, "not supported", "first-class array");
1240 recover();
1241 returnType.setArray(false);
1242 }
1243
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001244 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001245 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001246 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1247 {
1248 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1249 recover();
1250 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001251
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001252 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1253 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1254 {
1255 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1256 recover();
1257 }
1258 }
1259 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001260 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001261 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001262 {
Jamie Madill19571812013-08-12 15:26:34 -07001263 case EvqSmoothIn:
1264 case EvqSmoothOut:
1265 case EvqVertexOut:
1266 case EvqFragmentIn:
1267 case EvqCentroidOut:
1268 case EvqCentroidIn:
1269 if (typeSpecifier.type == EbtBool)
1270 {
1271 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1272 recover();
1273 }
1274 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1275 {
1276 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1277 recover();
1278 }
1279 break;
1280
1281 case EvqVertexIn:
1282 case EvqFragmentOut:
1283 case EvqFlatIn:
1284 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001285 if (typeSpecifier.type == EbtBool)
1286 {
1287 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1288 recover();
1289 }
1290 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001291
Jamie Madillb120eac2013-06-12 14:08:13 -04001292 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001293 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001294 }
1295
1296 return returnType;
1297}
1298
Jamie Madill075edd82013-07-08 13:30:19 -04001299TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001300{
1301 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1302 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1303
1304 if (identifier != "")
1305 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001306 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001307 recover();
1308
1309 // this error check can mutate the type
1310 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1311 recover();
1312
1313 TVariable* variable = 0;
1314
1315 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1316 recover();
1317
1318 if (variable && symbol)
1319 {
1320 symbol->setId(variable->getUniqueId());
1321 }
1322 }
1323
1324 return aggregate;
1325}
1326
Jamie Madill075edd82013-07-08 13:30:19 -04001327TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001328{
Jamie Madill51a53c72013-06-19 09:24:43 -04001329 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001330 recover();
1331
1332 // this error check can mutate the type
1333 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1334 recover();
1335
1336 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1337 {
1338 recover();
1339 }
1340
1341 TPublicType arrayType = publicType;
1342
1343 int size;
1344 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1345 {
1346 recover();
1347 }
1348 else
1349 {
1350 arrayType.setArray(true, size);
1351 }
1352
1353 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1354 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1355 TVariable* variable = 0;
1356
1357 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1358 recover();
1359
1360 if (variable && symbol)
1361 {
1362 symbol->setId(variable->getUniqueId());
1363 }
1364
1365 return aggregate;
1366}
1367
Jamie Madill075edd82013-07-08 13:30:19 -04001368TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001369{
Jamie Madilla5efff92013-06-06 11:56:47 -04001370 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001371 recover();
1372
1373 TIntermNode* intermNode;
1374 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1375 {
1376 //
1377 // Build intermediate representation
1378 //
1379 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1380 }
1381 else
1382 {
1383 recover();
1384 return NULL;
1385 }
1386}
1387
Jamie Madill47e3ec02014-08-20 16:38:33 -04001388TIntermAggregate* TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1389 const TSourceLoc &identifierLoc,
1390 const TString *identifier,
1391 const TSymbol *symbol)
1392{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001393 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001394 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1395 {
1396 recover();
1397 }
1398
1399 if (!symbol)
1400 {
1401 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1402 recover();
Jamie Madill47e3ec02014-08-20 16:38:33 -04001403 return NULL;
1404 }
1405 else
1406 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001407 const TString kGlFrontFacing("gl_FrontFacing");
1408 if (*identifier == kGlFrontFacing)
1409 {
1410 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1411 recover();
1412 return NULL;
1413 }
Jamie Madill2c433252014-12-03 12:36:54 -05001414 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001415 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1416 ASSERT(variable);
1417 const TType &type = variable->getType();
1418 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1419 *identifier, type, identifierLoc);
1420
1421 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1422 aggregate->setOp(EOpInvariantDeclaration);
1423 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001424 }
1425}
1426
Jamie Madill075edd82013-07-08 13:30:19 -04001427TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001428{
Jamie Madill502d66f2013-06-20 11:55:52 -04001429 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1430 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1431
1432 if (structQualifierErrorCheck(identifierLocation, publicType))
1433 recover();
1434
Jamie Madill0bd18df2013-06-20 11:55:52 -04001435 if (locationDeclaratorListCheck(identifierLocation, publicType))
1436 recover();
1437
Jamie Madill502d66f2013-06-20 11:55:52 -04001438 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1439 recover();
1440
1441 TVariable* variable = 0;
1442 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1443 recover();
1444 if (symbol && variable)
1445 symbol->setId(variable->getUniqueId());
1446
1447 return intermAggregate;
1448}
1449
Jamie Madill075edd82013-07-08 13:30:19 -04001450TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001451{
1452 if (structQualifierErrorCheck(identifierLocation, publicType))
1453 recover();
1454
Jamie Madill0bd18df2013-06-20 11:55:52 -04001455 if (locationDeclaratorListCheck(identifierLocation, publicType))
1456 recover();
1457
Jamie Madill502d66f2013-06-20 11:55:52 -04001458 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1459 recover();
1460
1461 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1462 {
1463 recover();
1464 }
1465 else if (indexExpression)
1466 {
1467 int size;
1468 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1469 recover();
1470 TPublicType arrayType(publicType);
1471 arrayType.setArray(true, size);
1472 TVariable* variable = NULL;
1473 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1474 recover();
1475 TType type = TType(arrayType);
1476 type.setArraySize(size);
1477
1478 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1479 }
1480 else
1481 {
1482 TPublicType arrayType(publicType);
1483 arrayType.setArray(true);
1484 TVariable* variable = NULL;
1485 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1486 recover();
1487 }
1488
1489 return NULL;
1490}
1491
Jamie Madill075edd82013-07-08 13:30:19 -04001492TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001493{
1494 if (structQualifierErrorCheck(identifierLocation, publicType))
1495 recover();
1496
Jamie Madill0bd18df2013-06-20 11:55:52 -04001497 if (locationDeclaratorListCheck(identifierLocation, publicType))
1498 recover();
1499
Jamie Madill502d66f2013-06-20 11:55:52 -04001500 TIntermNode* intermNode;
1501 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1502 {
1503 //
1504 // build the intermediate representation
1505 //
1506 if (intermNode)
1507 {
1508 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1509 }
1510 else
1511 {
1512 return declaratorList;
1513 }
1514 }
1515 else
1516 {
1517 recover();
1518 return NULL;
1519 }
1520}
1521
Jamie Madilla295edf2013-06-06 11:56:48 -04001522void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1523{
1524 if (typeQualifier.qualifier != EvqUniform)
1525 {
1526 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1527 recover();
1528 return;
1529 }
1530
1531 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1532 ASSERT(!layoutQualifier.isEmpty());
1533
1534 if (shaderVersion < 300)
1535 {
1536 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1537 recover();
1538 return;
1539 }
1540
1541 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1542 {
1543 recover();
1544 return;
1545 }
1546
Jamie Madill099c0f32013-06-20 11:55:52 -04001547 if (layoutQualifier.matrixPacking != EmpUnspecified)
1548 {
1549 defaultMatrixPacking = layoutQualifier.matrixPacking;
1550 }
1551
Geoff Langc6856732014-02-11 09:38:55 -05001552 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001553 {
1554 defaultBlockStorage = layoutQualifier.blockStorage;
1555 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001556}
1557
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001558TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1559{
1560 TOperator op = EOpNull;
1561 if (publicType.userDef)
1562 {
1563 op = EOpConstructStruct;
1564 }
1565 else
1566 {
1567 switch (publicType.type)
1568 {
1569 case EbtFloat:
1570 if (publicType.isMatrix())
1571 {
1572 // TODO: non-square matrices
1573 switch(publicType.getCols())
1574 {
Jamie Madill28b97422013-07-08 14:01:38 -04001575 case 2: op = EOpConstructMat2; break;
1576 case 3: op = EOpConstructMat3; break;
1577 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001578 }
1579 }
1580 else
1581 {
1582 switch(publicType.getNominalSize())
1583 {
Jamie Madill28b97422013-07-08 14:01:38 -04001584 case 1: op = EOpConstructFloat; break;
1585 case 2: op = EOpConstructVec2; break;
1586 case 3: op = EOpConstructVec3; break;
1587 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001588 }
1589 }
1590 break;
1591
1592 case EbtInt:
1593 switch(publicType.getNominalSize())
1594 {
Jamie Madill28b97422013-07-08 14:01:38 -04001595 case 1: op = EOpConstructInt; break;
1596 case 2: op = EOpConstructIVec2; break;
1597 case 3: op = EOpConstructIVec3; break;
1598 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001599 }
1600 break;
1601
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001602 case EbtUInt:
1603 switch(publicType.getNominalSize())
1604 {
Jamie Madill28b97422013-07-08 14:01:38 -04001605 case 1: op = EOpConstructUInt; break;
1606 case 2: op = EOpConstructUVec2; break;
1607 case 3: op = EOpConstructUVec3; break;
1608 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001609 }
1610 break;
1611
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001612 case EbtBool:
1613 switch(publicType.getNominalSize())
1614 {
Jamie Madill28b97422013-07-08 14:01:38 -04001615 case 1: op = EOpConstructBool; break;
1616 case 2: op = EOpConstructBVec2; break;
1617 case 3: op = EOpConstructBVec3; break;
1618 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001619 }
1620 break;
1621
1622 default: break;
1623 }
1624
1625 if (op == EOpNull)
1626 {
1627 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1628 recover();
1629 publicType.type = EbtFloat;
1630 op = EOpConstructFloat;
1631 }
1632 }
1633
1634 TString tempString;
1635 TType type(publicType);
1636 return new TFunction(&tempString, type, op);
1637}
1638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639// This function is used to test for the correctness of the parameters passed to various constructor functions
1640// and also convert them to the right datatype if it is allowed and required.
1641//
1642// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1643//
Olli Etuaho21203702014-11-13 16:16:21 +02001644TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001646 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001648 if (!aggregateArguments)
1649 {
1650 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001651 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001652 }
1653
Olli Etuahof40319e2015-03-10 14:33:00 +02001654 if (type->isArray())
1655 {
1656 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1657 TIntermSequence *args = aggregateArguments->getSequence();
1658 for (size_t i = 0; i < args->size(); i++)
1659 {
1660 const TType &argType = (*args)[i]->getAsTyped()->getType();
1661 // It has already been checked that the argument is not an array.
1662 ASSERT(!argType.isArray());
1663 if (!argType.sameElementType(*type))
1664 {
1665 error(line, "Array constructor argument has an incorrect type", "Error");
1666 recover();
1667 return nullptr;
1668 }
1669 }
1670 }
1671 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001672 {
1673 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001674 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001676 for (size_t i = 0; i < fields.size(); i++)
1677 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001678 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001679 {
1680 error(line, "Structure constructor arguments do not match structure fields", "Error");
1681 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001683 return 0;
1684 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001685 }
1686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001688 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001689 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1690 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001691 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001692 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001694 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695
Olli Etuaho21203702014-11-13 16:16:21 +02001696 // Structs should not be precision qualified, the individual members may be.
1697 // Built-in types on the other hand should be precision qualified.
1698 if (op != EOpConstructStruct)
1699 {
1700 constructor->setPrecisionFromChildren();
1701 type->setPrecision(constructor->getPrecision());
1702 }
1703
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001704 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705}
1706
1707TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1708{
Olli Etuahof40319e2015-03-10 14:33:00 +02001709 // TODO: Add support for folding array constructors
1710 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001711 aggrNode->setType(type);
1712 if (canBeFolded) {
1713 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001714 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001715 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001716 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 }
1718 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001719 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 }
1721 if (returnVal)
1722 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001727 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728}
1729
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730//
1731// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1732// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1733// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1734// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1735// a constant matrix.
1736//
Jamie Madill075edd82013-07-08 13:30:19 -04001737TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001739 TIntermTyped* typedNode;
1740 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001742 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001743 if (tempConstantNode) {
1744 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001746 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001747 return node;
1748 }
1749 } 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 +00001750 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001753 return 0;
1754 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001756 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001758 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001759 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001760 std::stringstream extraInfoStream;
1761 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1762 std::string extraInfo = extraInfoStream.str();
1763 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 recover();
1765 fields.offsets[i] = 0;
1766 }
1767
1768 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 }
1771 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1772 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773}
1774
1775//
1776// This function returns the column being accessed from a constant matrix. The values are retrieved from
1777// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1778// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1779// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1780//
Jamie Madill075edd82013-07-08 13:30:19 -04001781TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001783 TIntermTyped* typedNode;
1784 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001786 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001787 std::stringstream extraInfoStream;
1788 extraInfoStream << "matrix field selection out of range '" << index << "'";
1789 std::string extraInfo = extraInfoStream.str();
1790 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001791 recover();
1792 index = 0;
1793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001795 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001796 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001797 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001798 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1799 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001800 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001801 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001803 return 0;
1804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807}
1808
1809
1810//
1811// This function returns an element of an array accessed from a constant array. The values are retrieved from
1812// the symbol table and parse-tree is built for the type of the element. The input
1813// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1814// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1815//
Jamie Madill075edd82013-07-08 13:30:19 -04001816TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001818 TIntermTyped* typedNode;
1819 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1820 TType arrayElementType = node->getType();
1821 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001824 std::stringstream extraInfoStream;
1825 extraInfoStream << "array field selection out of range '" << index << "'";
1826 std::string extraInfo = extraInfoStream.str();
1827 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001828 recover();
1829 index = 0;
1830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001832 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001833 size_t arrayElementSize = arrayElementType.getObjectSize();
1834 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1835 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001836 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001837 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001838 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 return 0;
1841 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001843 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844}
1845
1846
1847//
1848// This function returns the value of a particular field inside a constant structure from the symbol table.
1849// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1850// function and returns the parse-tree with the values of the embedded/nested struct.
1851//
Jamie Madill075edd82013-07-08 13:30:19 -04001852TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853{
Jamie Madill98493dd2013-07-08 14:39:03 -04001854 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001855 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Jamie Madill98493dd2013-07-08 14:39:03 -04001857 for (size_t index = 0; index < fields.size(); ++index) {
1858 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001859 break;
1860 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001861 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 }
1863 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
Jamie Madill94bf7f22013-07-08 13:31:15 -04001865 TIntermTyped *typedNode;
1866 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001867 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001868 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1871 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001872 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001873 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001875 return 0;
1876 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001878 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879}
1880
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001881//
1882// Interface/uniform blocks
1883//
Jamie Madill98493dd2013-07-08 14:39:03 -04001884TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1885 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001886{
1887 if (reservedErrorCheck(nameLine, blockName))
1888 recover();
1889
1890 if (typeQualifier.qualifier != EvqUniform)
1891 {
1892 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1893 recover();
1894 }
1895
Jamie Madill099c0f32013-06-20 11:55:52 -04001896 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1897 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001898 {
1899 recover();
1900 }
1901
Jamie Madill099c0f32013-06-20 11:55:52 -04001902 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1903 {
1904 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1905 }
1906
Jamie Madill1566ef72013-06-20 11:55:54 -04001907 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1908 {
1909 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1910 }
1911
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001912 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001913 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001914 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1915 recover();
1916 }
1917
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 // check for sampler types and apply layout qualifiers
1919 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1920 TField* field = (*fieldList)[memberIndex];
1921 TType* fieldType = field->type();
1922 if (IsSampler(fieldType->getBasicType())) {
1923 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001924 recover();
1925 }
1926
Jamie Madill98493dd2013-07-08 14:39:03 -04001927 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001928 switch (qualifier)
1929 {
1930 case EvqGlobal:
1931 case EvqUniform:
1932 break;
1933 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001934 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001935 recover();
1936 break;
1937 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001938
1939 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001940 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1941 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001942 {
1943 recover();
1944 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001945
Jamie Madill98493dd2013-07-08 14:39:03 -04001946 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001947 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001948 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001949 recover();
1950 }
1951
Jamie Madill98493dd2013-07-08 14:39:03 -04001952 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001953 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001954 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001955 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001956 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001957 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001958 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001959 recover();
1960 }
1961
Jamie Madill98493dd2013-07-08 14:39:03 -04001962 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001963 }
1964
Jamie Madill98493dd2013-07-08 14:39:03 -04001965 // add array index
1966 int arraySize = 0;
1967 if (arrayIndex != NULL)
1968 {
1969 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1970 recover();
1971 }
1972
1973 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1974 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001975
1976 TString symbolName = "";
1977 int symbolId = 0;
1978
Jamie Madill98493dd2013-07-08 14:39:03 -04001979 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001980 {
1981 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001982 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1983 {
1984 TField* field = (*fieldList)[memberIndex];
1985 TType* fieldType = field->type();
1986
1987 // set parent pointer of the field variable
1988 fieldType->setInterfaceBlock(interfaceBlock);
1989
1990 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1991 fieldVariable->setQualifier(typeQualifier.qualifier);
1992
Nicolas Capensadfffe42014-06-17 02:13:36 -04001993 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001994 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001995 recover();
1996 }
1997 }
1998 }
1999 else
2000 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002001 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002002 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002003 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002004
Nicolas Capensadfffe42014-06-17 02:13:36 -04002005 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002006 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002007 recover();
2008 }
2009
2010 symbolId = instanceTypeDef->getUniqueId();
2011 symbolName = instanceTypeDef->getName();
2012 }
2013
Jamie Madill98493dd2013-07-08 14:39:03 -04002014 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002015 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002016
2017 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002018 return aggregate;
2019}
2020
Jamie Madill075edd82013-07-08 13:30:19 -04002021bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002022{
2023 ++structNestingLevel;
2024
2025 // Embedded structure definitions are not supported per GLSL ES spec.
2026 // They aren't allowed in GLSL either, but we need to detect this here
2027 // so we don't rely on the GLSL compiler to catch it.
2028 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002029 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002030 return true;
2031 }
2032
2033 return false;
2034}
2035
2036void TParseContext::exitStructDeclaration()
2037{
2038 --structNestingLevel;
2039}
2040
2041namespace {
2042
2043const int kWebGLMaxStructNesting = 4;
2044
2045} // namespace
2046
Jamie Madill98493dd2013-07-08 14:39:03 -04002047bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002048{
Jamie Madill5508f392014-02-20 13:31:36 -05002049 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002050 return false;
2051 }
2052
Jamie Madill98493dd2013-07-08 14:39:03 -04002053 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002054 return false;
2055 }
2056
2057 // We're already inside a structure definition at this point, so add
2058 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002059 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002060 std::stringstream reasonStream;
2061 reasonStream << "Reference of struct type "
2062 << field.type()->getStruct()->name().c_str()
2063 << " exceeds maximum allowed nesting level of "
2064 << kWebGLMaxStructNesting;
2065 std::string reason = reasonStream.str();
2066 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002067 return true;
2068 }
2069
2070 return false;
2071}
2072
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002073//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002074// Parse an array index expression
2075//
Jamie Madill075edd82013-07-08 13:30:19 -04002076TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002077{
2078 TIntermTyped *indexedExpression = NULL;
2079
2080 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2081 {
2082 if (baseExpression->getAsSymbolNode())
2083 {
2084 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2085 }
2086 else
2087 {
2088 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2089 }
2090 recover();
2091 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002092
Jamie Madill21c1e452014-12-29 11:33:41 -05002093 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2094
2095 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002096 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002097 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002098 if (index < 0)
2099 {
2100 std::stringstream infoStream;
2101 infoStream << index;
2102 std::string info = infoStream.str();
2103 error(location, "negative index", info.c_str());
2104 recover();
2105 index = 0;
2106 }
2107 if (baseExpression->getType().getQualifier() == EvqConst)
2108 {
2109 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002110 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002111 // constant folding for arrays
2112 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002113 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002114 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002115 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002116 // constant folding for vectors
2117 TVectorFields fields;
2118 fields.num = 1;
2119 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2120 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2121 }
2122 else if (baseExpression->isMatrix())
2123 {
2124 // constant folding for matrices
2125 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002126 }
2127 }
2128 else
2129 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002130 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002131 {
Jamie Madill18464b52013-07-08 14:01:55 -04002132 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002133 {
2134 std::stringstream extraInfoStream;
2135 extraInfoStream << "array index out of range '" << index << "'";
2136 std::string extraInfo = extraInfoStream.str();
2137 error(location, "", "[", extraInfo.c_str());
2138 recover();
2139 index = baseExpression->getType().getArraySize() - 1;
2140 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002141 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2142 {
2143 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2144 recover();
2145 index = 0;
2146 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002147 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002148 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002149 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002150 std::stringstream extraInfoStream;
2151 extraInfoStream << "field selection out of range '" << index << "'";
2152 std::string extraInfo = extraInfoStream.str();
2153 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002154 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002155 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002156 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002157
Jamie Madill21c1e452014-12-29 11:33:41 -05002158 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002159 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002160 }
2161 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002162 else
2163 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002164 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002165 {
Jamie Madill19571812013-08-12 15:26:34 -07002166 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002167 recover();
2168 }
Jamie Madill19571812013-08-12 15:26:34 -07002169 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002170 {
Jamie Madill19571812013-08-12 15:26:34 -07002171 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002172 recover();
2173 }
2174
2175 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2176 }
2177
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002178 if (indexedExpression == 0)
2179 {
2180 ConstantUnion *unionArray = new ConstantUnion[1];
2181 unionArray->setFConst(0.0f);
2182 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2183 }
2184 else if (baseExpression->isArray())
2185 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002186 const TType &baseType = baseExpression->getType();
2187 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002188 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002189 TType copyOfType(baseType.getStruct());
2190 indexedExpression->setType(copyOfType);
2191 }
2192 else if (baseType.isInterfaceBlock())
2193 {
2194 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002195 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002196 }
2197 else
2198 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002199 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002200 }
2201
2202 if (baseExpression->getType().getQualifier() == EvqConst)
2203 {
2204 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2205 }
2206 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002207 else if (baseExpression->isMatrix())
2208 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002209 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2210 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002211 }
2212 else if (baseExpression->isVector())
2213 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002214 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2215 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002216 }
2217 else
2218 {
2219 indexedExpression->setType(baseExpression->getType());
2220 }
2221
2222 return indexedExpression;
2223}
2224
Jamie Madill075edd82013-07-08 13:30:19 -04002225TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002226{
2227 TIntermTyped *indexedExpression = NULL;
2228
2229 if (baseExpression->isArray())
2230 {
2231 error(fieldLocation, "cannot apply dot operator to an array", ".");
2232 recover();
2233 }
2234
2235 if (baseExpression->isVector())
2236 {
2237 TVectorFields fields;
2238 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2239 {
2240 fields.num = 1;
2241 fields.offsets[0] = 0;
2242 recover();
2243 }
2244
2245 if (baseExpression->getType().getQualifier() == EvqConst)
2246 {
2247 // constant folding for vector fields
2248 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2249 if (indexedExpression == 0)
2250 {
2251 recover();
2252 indexedExpression = baseExpression;
2253 }
2254 else
2255 {
2256 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2257 }
2258 }
2259 else
2260 {
2261 TString vectorString = fieldString;
2262 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2263 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2264 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2265 }
2266 }
2267 else if (baseExpression->isMatrix())
2268 {
2269 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002270 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002271 {
2272 fields.wholeRow = false;
2273 fields.wholeCol = false;
2274 fields.row = 0;
2275 fields.col = 0;
2276 recover();
2277 }
2278
2279 if (fields.wholeRow || fields.wholeCol)
2280 {
2281 error(dotLocation, " non-scalar fields not implemented yet", ".");
2282 recover();
2283 ConstantUnion *unionArray = new ConstantUnion[1];
2284 unionArray->setIConst(0);
2285 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2286 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002287 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002288 }
2289 else
2290 {
2291 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002292 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002293 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2294 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2295 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2296 }
2297 }
2298 else if (baseExpression->getBasicType() == EbtStruct)
2299 {
2300 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002301 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2302 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002303 {
2304 error(dotLocation, "structure has no fields", "Internal Error");
2305 recover();
2306 indexedExpression = baseExpression;
2307 }
2308 else
2309 {
2310 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002311 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002312 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002313 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002314 {
2315 fieldFound = true;
2316 break;
2317 }
2318 }
2319 if (fieldFound)
2320 {
2321 if (baseExpression->getType().getQualifier() == EvqConst)
2322 {
2323 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2324 if (indexedExpression == 0)
2325 {
2326 recover();
2327 indexedExpression = baseExpression;
2328 }
2329 else
2330 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002331 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002332 // change the qualifier of the return type, not of the structure field
2333 // as the structure definition is shared between various structures.
2334 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2335 }
2336 }
2337 else
2338 {
2339 ConstantUnion *unionArray = new ConstantUnion[1];
2340 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002341 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002342 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002343 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002344 }
2345 }
2346 else
2347 {
2348 error(dotLocation, " no such field in structure", fieldString.c_str());
2349 recover();
2350 indexedExpression = baseExpression;
2351 }
2352 }
2353 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002354 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002355 {
2356 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002357 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2358 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002359 {
2360 error(dotLocation, "interface block has no fields", "Internal Error");
2361 recover();
2362 indexedExpression = baseExpression;
2363 }
2364 else
2365 {
2366 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002367 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002368 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002369 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002370 {
2371 fieldFound = true;
2372 break;
2373 }
2374 }
2375 if (fieldFound)
2376 {
2377 ConstantUnion *unionArray = new ConstantUnion[1];
2378 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002379 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002380 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002381 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002382 }
2383 else
2384 {
2385 error(dotLocation, " no such field in interface block", fieldString.c_str());
2386 recover();
2387 indexedExpression = baseExpression;
2388 }
2389 }
2390 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002391 else
2392 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002393 if (shaderVersion < 300)
2394 {
2395 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2396 }
2397 else
2398 {
2399 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2400 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002401 recover();
2402 indexedExpression = baseExpression;
2403 }
2404
2405 return indexedExpression;
2406}
2407
Jamie Madill075edd82013-07-08 13:30:19 -04002408TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002409{
Jamie Madilla5efff92013-06-06 11:56:47 -04002410 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002411
Jamie Madilla5efff92013-06-06 11:56:47 -04002412 qualifier.location = -1;
2413 qualifier.matrixPacking = EmpUnspecified;
2414 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002415
2416 if (qualifierType == "shared")
2417 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002418 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002419 }
2420 else if (qualifierType == "packed")
2421 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002422 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002423 }
2424 else if (qualifierType == "std140")
2425 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002426 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002427 }
2428 else if (qualifierType == "row_major")
2429 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002430 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002431 }
2432 else if (qualifierType == "column_major")
2433 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002434 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002435 }
2436 else if (qualifierType == "location")
2437 {
2438 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2439 recover();
2440 }
2441 else
2442 {
2443 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2444 recover();
2445 }
2446
Jamie Madilla5efff92013-06-06 11:56:47 -04002447 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002448}
2449
Jamie Madill075edd82013-07-08 13:30:19 -04002450TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002451{
Jamie Madilla5efff92013-06-06 11:56:47 -04002452 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002453
Jamie Madilla5efff92013-06-06 11:56:47 -04002454 qualifier.location = -1;
2455 qualifier.matrixPacking = EmpUnspecified;
2456 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002457
2458 if (qualifierType != "location")
2459 {
2460 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2461 recover();
2462 }
2463 else
2464 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002465 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002466 if (intValue < 0)
2467 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002468 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002469 recover();
2470 }
2471 else
2472 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002473 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002474 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002475 }
2476
Jamie Madilla5efff92013-06-06 11:56:47 -04002477 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002478}
2479
Jamie Madilla5efff92013-06-06 11:56:47 -04002480TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002481{
Jamie Madilla5efff92013-06-06 11:56:47 -04002482 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002483
Jamie Madilla5efff92013-06-06 11:56:47 -04002484 if (rightQualifier.location != -1)
2485 {
2486 joinedQualifier.location = rightQualifier.location;
2487 }
2488 if (rightQualifier.matrixPacking != EmpUnspecified)
2489 {
2490 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2491 }
2492 if (rightQualifier.blockStorage != EbsUnspecified)
2493 {
2494 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2495 }
2496
2497 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002498}
2499
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002500TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2501 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2502{
2503 TQualifier mergedQualifier = EvqSmoothIn;
2504
Jamie Madill19571812013-08-12 15:26:34 -07002505 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002506 if (interpolationQualifier == EvqSmooth)
2507 mergedQualifier = EvqSmoothIn;
2508 else if (interpolationQualifier == EvqFlat)
2509 mergedQualifier = EvqFlatIn;
2510 else UNREACHABLE();
2511 }
2512 else if (storageQualifier == EvqCentroidIn) {
2513 if (interpolationQualifier == EvqSmooth)
2514 mergedQualifier = EvqCentroidIn;
2515 else if (interpolationQualifier == EvqFlat)
2516 mergedQualifier = EvqFlatIn;
2517 else UNREACHABLE();
2518 }
Jamie Madill19571812013-08-12 15:26:34 -07002519 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002520 if (interpolationQualifier == EvqSmooth)
2521 mergedQualifier = EvqSmoothOut;
2522 else if (interpolationQualifier == EvqFlat)
2523 mergedQualifier = EvqFlatOut;
2524 else UNREACHABLE();
2525 }
2526 else if (storageQualifier == EvqCentroidOut) {
2527 if (interpolationQualifier == EvqSmooth)
2528 mergedQualifier = EvqCentroidOut;
2529 else if (interpolationQualifier == EvqFlat)
2530 mergedQualifier = EvqFlatOut;
2531 else UNREACHABLE();
2532 }
2533 else {
2534 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2535 recover();
2536
2537 mergedQualifier = storageQualifier;
2538 }
2539
2540 TPublicType type;
2541 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2542 return type;
2543}
2544
Jamie Madill98493dd2013-07-08 14:39:03 -04002545TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002546{
Jamie Madill98493dd2013-07-08 14:39:03 -04002547 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002548 recover();
2549 }
2550
Jamie Madill98493dd2013-07-08 14:39:03 -04002551 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002552 //
2553 // Careful not to replace already known aspects of type, like array-ness
2554 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002556 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002557 type->setPrimarySize(typeSpecifier.primarySize);
2558 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002559 type->setPrecision(typeSpecifier.precision);
2560 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002561 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562
2563 // don't allow arrays of arrays
2564 if (type->isArray()) {
2565 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2566 recover();
2567 }
2568 if (typeSpecifier.array)
2569 type->setArraySize(typeSpecifier.arraySize);
2570 if (typeSpecifier.userDef) {
2571 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002572 }
2573
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002575 recover();
2576 }
2577 }
2578
Jamie Madill98493dd2013-07-08 14:39:03 -04002579 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002580}
2581
Jamie Madill98493dd2013-07-08 14:39:03 -04002582TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002583{
Jamie Madill98493dd2013-07-08 14:39:03 -04002584 TStructure* structure = new TStructure(structName, fieldList);
2585 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002586
Jamie Madill9b820842015-02-12 10:40:10 -05002587 // Store a bool in the struct if we're at global scope, to allow us to
2588 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002589 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002590 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002591
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002593 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002594 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002595 {
2596 recover();
2597 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002598 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002599 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002600 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002601 recover();
2602 }
2603 }
2604
2605 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002606 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002607 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002608 const TField &field = *(*fieldList)[typeListIndex];
2609 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002610 switch (qualifier)
2611 {
2612 case EvqGlobal:
2613 case EvqTemporary:
2614 break;
2615 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002616 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002617 recover();
2618 break;
2619 }
2620 }
2621
2622 TPublicType publicType;
2623 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002624 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002625 exitStructDeclaration();
2626
2627 return publicType;
2628}
2629
Olli Etuahoa3a36662015-02-17 13:46:51 +02002630TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2631{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002632 TBasicType switchType = init->getBasicType();
2633 if ((switchType != EbtInt && switchType != EbtUInt) ||
2634 init->isMatrix() ||
2635 init->isArray() ||
2636 init->isVector())
2637 {
2638 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2639 recover();
2640 return nullptr;
2641 }
2642
Olli Etuahoac5274d2015-02-20 10:19:08 +02002643 if (statementList)
2644 {
2645 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2646 {
2647 recover();
2648 return nullptr;
2649 }
2650 }
2651
Olli Etuahoa3a36662015-02-17 13:46:51 +02002652 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2653 if (node == nullptr)
2654 {
2655 error(loc, "erroneous switch statement", "switch");
2656 recover();
2657 return nullptr;
2658 }
2659 return node;
2660}
2661
2662TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2663{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002664 if (mSwitchNestingLevel == 0)
2665 {
2666 error(loc, "case labels need to be inside switch statements", "case");
2667 recover();
2668 return nullptr;
2669 }
2670 if (condition == nullptr)
2671 {
2672 error(loc, "case label must have a condition", "case");
2673 recover();
2674 return nullptr;
2675 }
2676 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2677 condition->isMatrix() ||
2678 condition->isArray() ||
2679 condition->isVector())
2680 {
2681 error(condition->getLine(), "case label must be a scalar integer", "case");
2682 recover();
2683 }
2684 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2685 if (conditionConst == nullptr)
2686 {
2687 error(condition->getLine(), "case label must be constant", "case");
2688 recover();
2689 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002690 TIntermCase *node = intermediate.addCase(condition, loc);
2691 if (node == nullptr)
2692 {
2693 error(loc, "erroneous case statement", "case");
2694 recover();
2695 return nullptr;
2696 }
2697 return node;
2698}
2699
2700TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2701{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002702 if (mSwitchNestingLevel == 0)
2703 {
2704 error(loc, "default labels need to be inside switch statements", "default");
2705 recover();
2706 return nullptr;
2707 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002708 TIntermCase *node = intermediate.addCase(nullptr, loc);
2709 if (node == nullptr)
2710 {
2711 error(loc, "erroneous default statement", "default");
2712 recover();
2713 return nullptr;
2714 }
2715 return node;
2716}
2717
Olli Etuahof6c694b2015-03-26 14:50:53 +02002718TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2719 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002720{
2721 if (child == nullptr)
2722 {
2723 return nullptr;
2724 }
2725
2726 switch (op)
2727 {
2728 case EOpLogicalNot:
2729 if (child->getBasicType() != EbtBool ||
2730 child->isMatrix() ||
2731 child->isArray() ||
2732 child->isVector())
2733 {
2734 return nullptr;
2735 }
2736 break;
2737 case EOpBitwiseNot:
2738 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2739 child->isMatrix() ||
2740 child->isArray())
2741 {
2742 return nullptr;
2743 }
2744 break;
2745 case EOpPostIncrement:
2746 case EOpPreIncrement:
2747 case EOpPostDecrement:
2748 case EOpPreDecrement:
2749 case EOpNegative:
2750 case EOpPositive:
2751 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002752 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002753 child->isArray())
2754 {
2755 return nullptr;
2756 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002757 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002758 default:
2759 break;
2760 }
2761
Olli Etuahof6c694b2015-03-26 14:50:53 +02002762 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002763}
2764
Olli Etuaho09b22472015-02-11 11:47:26 +02002765TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2766{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002767 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002768 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002769 {
2770 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2771 recover();
2772 return child;
2773 }
2774 return node;
2775}
2776
2777TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2778{
2779 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2780 recover();
2781 return addUnaryMath(op, child, loc);
2782}
2783
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002784bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002785 const TSourceLoc &loc)
2786{
2787 if (left->isArray() || right->isArray())
2788 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002789 if (shaderVersion < 300)
2790 {
2791 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2792 return false;
2793 }
2794
2795 if (left->isArray() != right->isArray())
2796 {
2797 error(loc, "array / non-array mismatch", GetOperatorString(op));
2798 return false;
2799 }
2800
2801 switch (op)
2802 {
2803 case EOpEqual:
2804 case EOpNotEqual:
2805 case EOpAssign:
2806 case EOpInitialize:
2807 break;
2808 default:
2809 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2810 return false;
2811 }
2812 if (left->getArraySize() != right->getArraySize())
2813 {
2814 error(loc, "array size mismatch", GetOperatorString(op));
2815 return false;
2816 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002817 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002818
2819 // Check ops which require integer / ivec parameters
2820 bool isBitShift = false;
2821 switch (op)
2822 {
2823 case EOpBitShiftLeft:
2824 case EOpBitShiftRight:
2825 case EOpBitShiftLeftAssign:
2826 case EOpBitShiftRightAssign:
2827 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2828 // check that the basic type is an integer type.
2829 isBitShift = true;
2830 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2831 {
2832 return false;
2833 }
2834 break;
2835 case EOpBitwiseAnd:
2836 case EOpBitwiseXor:
2837 case EOpBitwiseOr:
2838 case EOpBitwiseAndAssign:
2839 case EOpBitwiseXorAssign:
2840 case EOpBitwiseOrAssign:
2841 // It is enough to check the type of only one operand, since later it
2842 // is checked that the operand types match.
2843 if (!IsInteger(left->getBasicType()))
2844 {
2845 return false;
2846 }
2847 break;
2848 default:
2849 break;
2850 }
2851
2852 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2853 // So the basic type should usually match.
2854 if (!isBitShift && left->getBasicType() != right->getBasicType())
2855 {
2856 return false;
2857 }
2858
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002859 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002860 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002861 switch(op)
2862 {
2863 case EOpAssign:
2864 case EOpInitialize:
2865 case EOpEqual:
2866 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002867 // ESSL 1.00 sections 5.7, 5.8, 5.9
2868 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2869 {
2870 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2871 return false;
2872 }
Olli Etuahoff699002015-03-23 14:38:42 +02002873 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2874 // we interpret the spec so that this extends to structs containing samplers,
2875 // similarly to ESSL 1.00 spec.
2876 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2877 left->getType().isStructureContainingSamplers())
2878 {
2879 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2880 return false;
2881 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002882 case EOpLessThan:
2883 case EOpGreaterThan:
2884 case EOpLessThanEqual:
2885 case EOpGreaterThanEqual:
2886 if ((left->getNominalSize() != right->getNominalSize()) ||
2887 (left->getSecondarySize() != right->getSecondarySize()))
2888 {
2889 return false;
2890 }
2891 default:
2892 break;
2893 }
2894
Olli Etuahod6b14282015-03-17 14:31:35 +02002895 return true;
2896}
2897
Olli Etuahofc1806e2015-03-17 13:03:11 +02002898TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2899 const TSourceLoc &loc)
2900{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002901 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002902 return nullptr;
2903
Olli Etuahofc1806e2015-03-17 13:03:11 +02002904 switch (op)
2905 {
2906 case EOpEqual:
2907 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002908 break;
2909 case EOpLessThan:
2910 case EOpGreaterThan:
2911 case EOpLessThanEqual:
2912 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002913 ASSERT(!left->isArray() && !right->isArray());
2914 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002915 left->getBasicType() == EbtStruct)
2916 {
2917 return nullptr;
2918 }
2919 break;
2920 case EOpLogicalOr:
2921 case EOpLogicalXor:
2922 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02002923 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002924 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02002925 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02002926 {
2927 return nullptr;
2928 }
2929 break;
2930 case EOpAdd:
2931 case EOpSub:
2932 case EOpDiv:
2933 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02002934 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002935 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
2936 {
2937 return nullptr;
2938 }
2939 break;
2940 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02002941 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02002942 // Note that this is only for the % operator, not for mod()
2943 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
2944 {
2945 return nullptr;
2946 }
2947 break;
2948 // Note that for bitwise ops, type checking is done in promote() to
2949 // share code between ops and compound assignment
2950 default:
2951 break;
2952 }
2953
Olli Etuahofc1806e2015-03-17 13:03:11 +02002954 return intermediate.addBinaryMath(op, left, right, loc);
2955}
2956
Olli Etuaho09b22472015-02-11 11:47:26 +02002957TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
2958 const TSourceLoc &loc)
2959{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002960 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002961 if (node == 0)
2962 {
2963 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2964 recover();
2965 return left;
2966 }
2967 return node;
2968}
2969
2970TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
2971 const TSourceLoc &loc)
2972{
Olli Etuahofc1806e2015-03-17 13:03:11 +02002973 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02002974 if (node == 0)
2975 {
2976 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2977 recover();
2978 ConstantUnion *unionArray = new ConstantUnion[1];
2979 unionArray->setBConst(false);
2980 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
2981 }
2982 return node;
2983}
2984
Olli Etuahod6b14282015-03-17 14:31:35 +02002985TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2986 const TSourceLoc &loc)
2987{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002988 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002989 {
2990 return intermediate.addAssign(op, left, right, loc);
2991 }
2992 return nullptr;
2993}
2994
2995TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
2996 const TSourceLoc &loc)
2997{
2998 TIntermTyped *node = createAssign(op, left, right, loc);
2999 if (node == nullptr)
3000 {
3001 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3002 recover();
3003 return left;
3004 }
3005 return node;
3006}
3007
Olli Etuaho49300862015-02-20 14:54:49 +02003008TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3009{
3010 switch (op)
3011 {
3012 case EOpContinue:
3013 if (mLoopNestingLevel <= 0)
3014 {
3015 error(loc, "continue statement only allowed in loops", "");
3016 recover();
3017 }
3018 break;
3019 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003020 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003021 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003022 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003023 recover();
3024 }
3025 break;
3026 case EOpReturn:
3027 if (currentFunctionType->getBasicType() != EbtVoid)
3028 {
3029 error(loc, "non-void function must return a value", "return");
3030 recover();
3031 }
3032 break;
3033 default:
3034 // No checks for discard
3035 break;
3036 }
3037 return intermediate.addBranch(op, loc);
3038}
3039
3040TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3041{
3042 ASSERT(op == EOpReturn);
3043 mFunctionReturnsValue = true;
3044 if (currentFunctionType->getBasicType() == EbtVoid)
3045 {
3046 error(loc, "void function cannot return a value", "return");
3047 recover();
3048 }
3049 else if (*currentFunctionType != returnValue->getType())
3050 {
3051 error(loc, "function return is not matching type:", "return");
3052 recover();
3053 }
3054 return intermediate.addBranch(op, returnValue, loc);
3055}
3056
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003057TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *node,
3058 const TSourceLoc &loc, bool *fatalError)
3059{
3060 *fatalError = false;
3061 TOperator op = fnCall->getBuiltInOp();
3062 TIntermTyped *callNode = nullptr;
3063
3064 if (op != EOpNull)
3065 {
3066 //
3067 // Then this should be a constructor.
3068 // Don't go through the symbol table for constructors.
3069 // Their parameters will be verified algorithmically.
3070 //
3071 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3072 if (!constructorErrorCheck(loc, node, *fnCall, op, &type))
3073 {
3074 //
3075 // It's a constructor, of type 'type'.
3076 //
3077 callNode = addConstructor(node, &type, op, fnCall, loc);
3078 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003079
3080 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003081 {
3082 recover();
3083 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3084 }
3085 callNode->setType(type);
3086 }
3087 else
3088 {
3089 //
3090 // Not a constructor. Find it in the symbol table.
3091 //
3092 const TFunction* fnCandidate;
3093 bool builtIn;
3094 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3095 if (fnCandidate)
3096 {
3097 //
3098 // A declared function.
3099 //
3100 if (builtIn && !fnCandidate->getExtension().empty() &&
3101 extensionErrorCheck(loc, fnCandidate->getExtension()))
3102 {
3103 recover();
3104 }
3105 op = fnCandidate->getBuiltInOp();
3106 if (builtIn && op != EOpNull)
3107 {
3108 //
3109 // A function call mapped to a built-in operation.
3110 //
3111 if (fnCandidate->getParamCount() == 1)
3112 {
3113 //
3114 // Treat it like a built-in unary operator.
3115 //
Olli Etuahof6c694b2015-03-26 14:50:53 +02003116 callNode = createUnaryMath(op, node->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003117 if (callNode == nullptr)
3118 {
3119 std::stringstream extraInfoStream;
3120 extraInfoStream << "built in unary operator function. Type: "
3121 << static_cast<TIntermTyped*>(node)->getCompleteString();
3122 std::string extraInfo = extraInfoStream.str();
3123 error(node->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3124 *fatalError = true;
3125 return nullptr;
3126 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003127 }
3128 else
3129 {
3130 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, op, loc);
3131 aggregate->setType(fnCandidate->getReturnType());
3132 aggregate->setPrecisionFromChildren();
3133 callNode = aggregate;
3134
3135 // Some built-in functions have out parameters too.
3136 functionCallLValueErrorCheck(fnCandidate, aggregate);
3137 }
3138 }
3139 else
3140 {
3141 // This is a real function call
3142
3143 TIntermAggregate *aggregate = intermediate.setAggregateOperator(node, EOpFunctionCall, loc);
3144 aggregate->setType(fnCandidate->getReturnType());
3145
3146 // this is how we know whether the given function is a builtIn function or a user defined function
3147 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3148 // if builtIn == true, it's definitely a builtIn function with EOpNull
3149 if (!builtIn)
3150 aggregate->setUserDefined();
3151 aggregate->setName(fnCandidate->getMangledName());
3152
3153 // This needs to happen after the name is set
3154 if (builtIn)
3155 aggregate->setBuiltInFunctionPrecision();
3156
3157 callNode = aggregate;
3158
3159 functionCallLValueErrorCheck(fnCandidate, aggregate);
3160 }
3161 }
3162 else
3163 {
3164 // error message was put out by findFunction()
3165 // Put on a dummy node for error recovery
3166 ConstantUnion *unionArray = new ConstantUnion[1];
3167 unionArray->setFConst(0.0f);
3168 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3169 recover();
3170 }
3171 }
3172 delete fnCall;
3173 return callNode;
3174}
3175
Olli Etuaho49300862015-02-20 14:54:49 +02003176
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003177//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003178// Parse an array of strings using yyparse.
3179//
3180// Returns 0 for success.
3181//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003182int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003183 TParseContext* context) {
3184 if ((count == 0) || (string == NULL))
3185 return 1;
3186
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003187 if (glslang_initialize(context))
3188 return 1;
3189
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003190 int error = glslang_scan(count, string, length, context);
3191 if (!error)
3192 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003193
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003194 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003195
alokp@chromium.org6b495712012-06-29 00:06:58 +00003196 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003197}
3198
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003200