blob: eb9328c7521f43bad01887e4782691f331a5cbd1 [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201//
202// Same error message for all places assignments don't work.
203//
Jamie Madill075edd82013-07-08 13:30:19 -0400204void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000206 std::stringstream extraInfoStream;
207 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
208 std::string extraInfo = extraInfoStream.str();
209 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210}
211
212//
213// Same error message for all places unary operations don't work.
214//
Jamie Madill075edd82013-07-08 13:30:19 -0400215void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000217 std::stringstream extraInfoStream;
218 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
219 << " (or there is no acceptable conversion)";
220 std::string extraInfo = extraInfoStream.str();
221 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222}
223
224//
225// Same error message for all binary operations don't work.
226//
Jamie Madill075edd82013-07-08 13:30:19 -0400227void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000229 std::stringstream extraInfoStream;
230 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
231 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
232 std::string extraInfo = extraInfoStream.str();
233 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
Jamie Madill075edd82013-07-08 13:30:19 -0400236bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000237 if (!checksPrecisionErrors)
238 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000239 switch( type ){
240 case EbtFloat:
241 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000242 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 return true;
244 }
245 break;
246 case EbtInt:
247 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000248 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000249 return true;
250 }
251 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000252 default:
253 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000254 }
255 return false;
256}
257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258//
259// Both test and if necessary, spit out an error, to see if the node is really
260// an l-value that can be operated on this way.
261//
262// Returns true if the was an error.
263//
Jamie Madill075edd82013-07-08 13:30:19 -0400264bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000266 TIntermSymbol* symNode = node->getAsSymbolNode();
267 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000269 if (binaryNode) {
270 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000272 switch(binaryNode->getOp()) {
273 case EOpIndexDirect:
274 case EOpIndexIndirect:
275 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000276 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 return lValueErrorCheck(line, op, binaryNode->getLeft());
278 case EOpVectorSwizzle:
279 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
280 if (!errorReturn) {
281 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000283 TIntermTyped* rightNode = binaryNode->getRight();
284 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700285
286 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
287 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000288 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700289 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000290 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000291 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 return true;
294 }
295 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700299 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 break;
301 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000302 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 return true;
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 const char* symbol = 0;
309 if (symNode != 0)
310 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* message = 0;
313 switch (node->getQualifier()) {
314 case EvqConst: message = "can't modify a const"; break;
315 case EvqConstReadOnly: message = "can't modify a const"; break;
316 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700317 case EvqFragmentIn: message = "can't modify an input"; break;
318 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000319 case EvqUniform: message = "can't modify a uniform"; break;
320 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000321 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
322 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
323 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
324 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000326 //
327 // Type that can't be written to?
328 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400329 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400331 }
332 if (IsSampler(node->getBasicType())) {
333 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 }
335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000338 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return true;
341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 //
345 // Everything else is okay, no error.
346 //
347 if (message == 0)
348 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000350 //
351 // If we get here, we have an error and a message.
352 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000353 if (symNode) {
354 std::stringstream extraInfoStream;
355 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
356 std::string extraInfo = extraInfoStream.str();
357 error(line, " l-value required", op, extraInfo.c_str());
358 }
359 else {
360 std::stringstream extraInfoStream;
361 extraInfoStream << "(" << message << ")";
362 std::string extraInfo = extraInfoStream.str();
363 error(line, " l-value required", op, extraInfo.c_str());
364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
369//
370// Both test, and if necessary spit out an error, to see if the node is really
371// a constant.
372//
373// Returns true if the was an error.
374//
375bool TParseContext::constErrorCheck(TIntermTyped* node)
376{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000377 if (node->getQualifier() == EvqConst)
378 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000380 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000382 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
384
385//
386// Both test, and if necessary spit out an error, to see if the node is really
387// an integer.
388//
389// Returns true if the was an error.
390//
391bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
392{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000393 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000394 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000396 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399}
400
401//
402// Both test, and if necessary spit out an error, to see if we are currently
403// globally scoped.
404//
405// Returns true if the was an error.
406//
Jamie Madill075edd82013-07-08 13:30:19 -0400407bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000409 if (global)
410 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000412 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
417//
418// For now, keep it simple: if it starts "gl_", it's reserved, independent
419// of scope. Except, if the symbol table is at the built-in push-level,
420// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000421// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
422// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423//
424// Returns true if there was an error.
425//
Jamie Madill075edd82013-07-08 13:30:19 -0400426bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000428 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000429 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000430 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000431 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 return true;
433 }
Jamie Madill5508f392014-02-20 13:31:36 -0500434 if (IsWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000435 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000437 return true;
438 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000439 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000440 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000441 return true;
442 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000443 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000444 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000445 return true;
446 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000447 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000450 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 }
452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
456
457//
458// Make sure there is enough data provided to the constructor to build
459// something of the type of the constructor. Also returns the type of
460// the constructor.
461//
462// Returns true if there was an error in construction.
463//
Jamie Madill075edd82013-07-08 13:30:19 -0400464bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000468 bool constructingMatrix = false;
469 switch(op) {
470 case EOpConstructMat2:
471 case EOpConstructMat3:
472 case EOpConstructMat4:
473 constructingMatrix = true;
474 break;
475 default:
476 break;
477 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000479 //
480 // Note: It's okay to have too many components available, but not okay to have unused
481 // arguments. 'full' will go to true when enough args have been seen. If we loop
482 // again, there is an extra argument, so 'overfull' will become true.
483 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
Jamie Madill94bf7f22013-07-08 13:31:15 -0400485 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 bool constType = true;
487 bool full = false;
488 bool overFull = false;
489 bool matrixInMatrix = false;
490 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000491 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000492 const TParameter& param = function.getParam(i);
493 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000495 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496 matrixInMatrix = true;
497 if (full)
498 overFull = true;
499 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
500 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000501 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000503 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 arrayArg = true;
505 }
506
507 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000508 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509
Olli Etuaho376f1b52015-04-13 13:23:41 +0300510 if (type->isArray())
511 {
512 if (type->isUnsizedArray())
513 {
514 type->setArraySize(function.getParamCount());
515 }
516 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
517 {
518 error(line, "array constructor needs one argument per array element", "constructor");
519 return true;
520 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000524 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 return true;
526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000529 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000530 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000531 return true;
532 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000536 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 return true;
538 }
539
Brendan Longeaa84062013-12-08 18:26:50 +0100540 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000541 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 return true;
543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000545 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000546 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
547 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000548 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000549 return true;
550 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000553 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000555 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 return true;
557 }
558 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000559 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 return true;
561 }
562 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000563 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 return true;
565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000567 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568}
569
570// This function checks to see if a void variable has been declared and raise an error message for such a case
571//
572// returns true in case of an error
573//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300574bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300576 if (type == EbtVoid)
577 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000578 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583}
584
585// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
586//
587// returns true in case of an error
588//
Jamie Madill075edd82013-07-08 13:30:19 -0400589bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000592 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return true;
594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
599// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
600//
601// returns true in case of an error
602//
Jamie Madill075edd82013-07-08 13:30:19 -0400603bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000605 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000606 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return true;
608 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611}
612
Jamie Madill075edd82013-07-08 13:30:19 -0400613bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 if (pType.type == EbtStruct) {
616 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000617 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618
619 return true;
620 }
621
622 return false;
623 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 return true;
627 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630}
631
Jamie Madill075edd82013-07-08 13:30:19 -0400632bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400633{
634 if (pType.layoutQualifier.location != -1)
635 {
636 error(line, "location must only be specified for a single input or output variable", "location");
637 return true;
638 }
639
640 return false;
641}
642
Jamie Madill075edd82013-07-08 13:30:19 -0400643bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
646 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000647 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return true;
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654bool TParseContext::containsSampler(TType& type)
655{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 if (IsSampler(type.getBasicType()))
657 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658
Jamie Madill98493dd2013-07-08 14:39:03 -0400659 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
660 const TFieldList& fields = type.getStruct()->fields();
661 for (unsigned int i = 0; i < fields.size(); ++i) {
662 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 return true;
664 }
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670//
671// Do size checking for an array type's size.
672//
673// Returns true if there was an error.
674//
Jamie Madill075edd82013-07-08 13:30:19 -0400675bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000678
Olli Etuahoe7847b02015-03-16 11:56:12 +0200679 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000680 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000681 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200682 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 return true;
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
Nicolas Capens906744a2014-06-06 15:18:07 -0400686 unsigned int unsignedSize = 0;
687
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000688 if (constant->getBasicType() == EbtUInt)
689 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400690 unsignedSize = constant->getUConst(0);
691 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000692 }
693 else
694 {
695 size = constant->getIConst(0);
696
Nicolas Capens906744a2014-06-06 15:18:07 -0400697 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000698 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400699 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000700 size = 1;
701 return true;
702 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400703
704 unsignedSize = static_cast<unsigned int>(size);
705 }
706
707 if (size == 0)
708 {
709 error(line, "array size must be greater than zero", "");
710 size = 1;
711 return true;
712 }
713
714 // The size of arrays is restricted here to prevent issues further down the
715 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
716 // 4096 registers so this should be reasonable even for aggressively optimizable code.
717 const unsigned int sizeLimit = 65536;
718
719 if (unsignedSize > sizeLimit)
720 {
721 error(line, "array size too large", "");
722 size = 1;
723 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729//
730// See if this qualifier can be an array.
731//
732// Returns true if there is an error.
733//
Olli Etuaho3739d232015-04-08 12:23:44 +0300734bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Olli Etuaho3739d232015-04-08 12:23:44 +0300736 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
737 (type.qualifier == EvqConst && shaderVersion < 300))
738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return true;
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
746//
747// See if this type can be an array.
748//
749// Returns true if there is an error.
750//
Jamie Madill075edd82013-07-08 13:30:19 -0400751bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 //
754 // Can the type be an array?
755 //
756 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000757 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return true;
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
764//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765// Enforce non-initializer type/qualifier rules.
766//
767// Returns true if there was an error.
768//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300769bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Olli Etuaho3739d232015-04-08 12:23:44 +0300771 ASSERT(type != nullptr);
772 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000773 {
774 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300775 type->qualifier = EvqTemporary;
776
777 // Generate informative error messages for ESSL1.
778 // In ESSL3 arrays and structures containing arrays can be constant.
779 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000780 {
781 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
782 }
783 else
784 {
785 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
786 }
787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000788 return true;
789 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300790 if (type->isUnsizedArray())
791 {
792 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
793 return true;
794 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
Olli Etuaho2935c582015-04-08 14:32:06 +0300798// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799// and update the symbol table.
800//
Olli Etuaho2935c582015-04-08 14:32:06 +0300801// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802//
Olli Etuaho2935c582015-04-08 14:32:06 +0300803bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
804 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805{
Olli Etuaho2935c582015-04-08 14:32:06 +0300806 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
Olli Etuaho2935c582015-04-08 14:32:06 +0300808 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
Olli Etuaho2935c582015-04-08 14:32:06 +0300810 // gl_LastFragData may be redeclared with a new precision qualifier
811 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
812 {
813 const TVariable *maxDrawBuffers =
814 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion));
815 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
816 {
817 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion))
818 {
819 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
820 }
821 }
822 else
823 {
824 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
825 return false;
826 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
Olli Etuaho2935c582015-04-08 14:32:06 +0300829 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
Olli Etuaho2935c582015-04-08 14:32:06 +0300832 (*variable) = new TVariable(&identifier, type);
833 if (!symbolTable.declare(*variable))
834 {
835 error(line, "redefinition", identifier.c_str());
836 delete (*variable);
837 (*variable) = nullptr;
838 return false;
839 }
840
841 if (voidErrorCheck(line, identifier, type.getBasicType()))
842 return false;
843
844 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
Jamie Madill075edd82013-07-08 13:30:19 -0400847bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000850 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 return true;
852 }
853 if (qualifier == EvqConst && paramQualifier != EvqIn) {
854 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
855 return true;
856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000859 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000861 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864}
865
Jamie Madill075edd82013-07-08 13:30:19 -0400866bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000867{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000868 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000869 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
870 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000871 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return true;
873 }
zmo@google.comf5450912011-09-09 01:37:19 +0000874 // In GLSL ES, an extension's default behavior is "disable".
875 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000876 error(line, "extension", extension.c_str(), "is disabled");
877 return true;
878 }
879 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000880 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000881 return false;
882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885}
886
Olli Etuahofa33d582015-04-09 14:33:12 +0300887// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
888// declaration.
889//
890bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400891{
Olli Etuahofa33d582015-04-09 14:33:12 +0300892 switch (publicType.qualifier)
893 {
894 case EvqVaryingIn:
895 case EvqVaryingOut:
896 case EvqAttribute:
897 case EvqVertexIn:
898 case EvqFragmentOut:
899 if (publicType.type == EbtStruct)
900 {
901 error(identifierLocation, "cannot be used with a structure",
902 getQualifierString(publicType.qualifier));
903 return true;
904 }
905
906 default: break;
907 }
908
909 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
910 "samplers must be uniform"))
911 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400912 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300913 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400914
915 // check for layout qualifier issues
916 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
917
918 if (layoutQualifier.matrixPacking != EmpUnspecified)
919 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300920 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
921 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400922 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400923 }
924
925 if (layoutQualifier.blockStorage != EbsUnspecified)
926 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300927 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
928 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400929 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400930 }
931
Olli Etuahofa33d582015-04-09 14:33:12 +0300932 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
933 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400934 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400935 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400936 }
937
938 return false;
939}
940
Jamie Madill075edd82013-07-08 13:30:19 -0400941bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400942{
943 if (layoutQualifier.location != -1)
944 {
945 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
946 return true;
947 }
948
949 return false;
950}
951
Olli Etuahob6e07a62015-02-16 12:22:10 +0200952bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
953{
954 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
955 {
956 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
957 if (qual == EvqOut || qual == EvqInOut)
958 {
959 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
960 if (lValueErrorCheck(node->getLine(), "assign", node))
961 {
962 error(node->getLine(),
963 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
964 recover();
965 return true;
966 }
967 }
968 }
969 return false;
970}
971
zmo@google.com09c323a2011-08-12 18:22:25 +0000972bool TParseContext::supportsExtension(const char* extension)
973{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000974 const TExtensionBehavior& extbehavior = extensionBehavior();
975 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
976 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000977}
978
Jamie Madill5d287f52013-07-12 15:38:19 -0400979bool TParseContext::isExtensionEnabled(const char* extension) const
980{
981 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400982 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400983
984 if (iter == extbehavior.end())
985 {
986 return false;
987 }
988
989 return (iter->second == EBhEnable || iter->second == EBhRequire);
990}
991
Jamie Madill075edd82013-07-08 13:30:19 -0400992void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
993{
994 pp::SourceLocation srcLoc;
995 srcLoc.file = loc.first_file;
996 srcLoc.line = loc.first_line;
997 directiveHandler.handleExtension(srcLoc, extName, behavior);
998}
999
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001000void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001001{
1002 pp::SourceLocation srcLoc;
1003 srcLoc.file = loc.first_file;
1004 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001005 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001006}
1007
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008/////////////////////////////////////////////////////////////////////////////////
1009//
1010// Non-Errors.
1011//
1012/////////////////////////////////////////////////////////////////////////////////
1013
Jamie Madill5c097022014-08-20 16:38:32 -04001014const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1015 const TString *name,
1016 const TSymbol *symbol)
1017{
1018 const TVariable *variable = NULL;
1019
1020 if (!symbol)
1021 {
1022 error(location, "undeclared identifier", name->c_str());
1023 recover();
1024 }
1025 else if (!symbol->isVariable())
1026 {
1027 error(location, "variable expected", name->c_str());
1028 recover();
1029 }
1030 else
1031 {
1032 variable = static_cast<const TVariable*>(symbol);
1033
1034 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1035 !variable->getExtension().empty() &&
1036 extensionErrorCheck(location, variable->getExtension()))
1037 {
1038 recover();
1039 }
1040 }
1041
1042 if (!variable)
1043 {
1044 TType type(EbtFloat, EbpUndefined);
1045 TVariable *fakeVariable = new TVariable(name, type);
1046 symbolTable.declare(fakeVariable);
1047 variable = fakeVariable;
1048 }
1049
1050 return variable;
1051}
1052
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053//
1054// Look up a function name in the symbol table, and make sure it is a function.
1055//
1056// Return the function symbol if found, otherwise 0.
1057//
Austin Kinross3ae64652015-01-26 15:51:39 -08001058const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001060 // First find by unmangled name to check whether the function name has been
1061 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001062 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001063 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001064 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001065 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
alokp@chromium.org0a576182010-08-09 17:16:27 +00001068 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001069 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001070 return 0;
1071 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
alokp@chromium.org0a576182010-08-09 17:16:27 +00001073 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 return 0;
1076 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001077
1078 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081//
1082// Initializers show up in several places in the grammar. Have one set of
1083// code to handle them here.
1084//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001085// Returns true on error, false if no error
1086//
Olli Etuaho2935c582015-04-08 14:32:06 +03001087bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001088 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001090 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
Olli Etuaho2935c582015-04-08 14:32:06 +03001093 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001094 if (type.isUnsizedArray())
1095 {
1096 type.setArraySize(initializer->getArraySize());
1097 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 if (!declareVariable(line, identifier, type, &variable))
1099 {
1100 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 //
1104 // identifier must be of type constant, a global, or a temporary
1105 //
1106 TQualifier qualifier = variable->getType().getQualifier();
1107 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001108 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001109 return true;
1110 }
1111 //
1112 // test for and propagate constant
1113 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001115 if (qualifier == EvqConst) {
1116 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001117 std::stringstream extraInfoStream;
1118 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1119 std::string extraInfo = extraInfoStream.str();
1120 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001121 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 return true;
1123 }
1124 if (type != initializer->getType()) {
1125 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001126 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001127 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001128 return true;
1129 }
1130 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001131 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001133 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001136 TConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 variable->shareConstPointer(constArray);
1138 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001139 std::stringstream extraInfoStream;
1140 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1141 std::string extraInfo = extraInfoStream.str();
1142 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001143 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001144 return true;
1145 }
1146 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001147
1148 if (qualifier != EvqConst)
1149 {
1150 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1151 variable->getType(), line);
1152 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1153 if (*intermNode == nullptr)
1154 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001155 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1156 return true;
1157 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001158 }
1159 else
1160 {
1161 *intermNode = nullptr;
1162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001164 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165}
1166
1167bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1168{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001169 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 if (!aggrNode->isConstructor())
1171 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001173 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 // check if all the child nodes are constants so that they can be inserted into
1176 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001177 TIntermSequence *sequence = aggrNode->getSequence() ;
1178 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001179 if (!(*p)->getAsTyped()->getAsConstantUnion())
1180 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001183 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
Jamie Madilla5efff92013-06-06 11:56:47 -04001186TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001187{
1188 TPublicType returnType = typeSpecifier;
1189 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001190 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001191
1192 if (typeSpecifier.array)
1193 {
1194 error(typeSpecifier.line, "not supported", "first-class array");
1195 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001196 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001197 }
1198
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001199 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001200 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001201 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1202 {
1203 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1204 recover();
1205 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001206
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001207 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1208 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1209 {
1210 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1211 recover();
1212 }
1213 }
1214 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001215 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001216 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001217 {
Jamie Madill19571812013-08-12 15:26:34 -07001218 case EvqSmoothIn:
1219 case EvqSmoothOut:
1220 case EvqVertexOut:
1221 case EvqFragmentIn:
1222 case EvqCentroidOut:
1223 case EvqCentroidIn:
1224 if (typeSpecifier.type == EbtBool)
1225 {
1226 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1227 recover();
1228 }
1229 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1230 {
1231 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1232 recover();
1233 }
1234 break;
1235
1236 case EvqVertexIn:
1237 case EvqFragmentOut:
1238 case EvqFlatIn:
1239 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001240 if (typeSpecifier.type == EbtBool)
1241 {
1242 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1243 recover();
1244 }
1245 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001246
Jamie Madillb120eac2013-06-12 14:08:13 -04001247 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001248 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001249 }
1250
1251 return returnType;
1252}
1253
Olli Etuahofa33d582015-04-09 14:33:12 +03001254TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1255 const TSourceLoc &identifierOrTypeLocation,
1256 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001257{
Olli Etuahofa33d582015-04-09 14:33:12 +03001258 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001259
Olli Etuahofa33d582015-04-09 14:33:12 +03001260 mDeferredSingleDeclarationErrorCheck = (identifier == "");
1261
1262 if (!mDeferredSingleDeclarationErrorCheck)
Jamie Madill60ed9812013-06-06 11:56:46 -04001263 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001264 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001265 recover();
1266
Olli Etuaho376f1b52015-04-13 13:23:41 +03001267 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001268 recover();
1269
Olli Etuaho2935c582015-04-08 14:32:06 +03001270 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001271 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001272 recover();
1273
1274 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001275 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001276 }
1277
Olli Etuahoe7847b02015-03-16 11:56:12 +02001278 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001279}
1280
Olli Etuahoe7847b02015-03-16 11:56:12 +02001281TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1282 const TSourceLoc &identifierLocation,
1283 const TString &identifier,
1284 const TSourceLoc &indexLocation,
1285 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001286{
Olli Etuahofa33d582015-04-09 14:33:12 +03001287 mDeferredSingleDeclarationErrorCheck = false;
1288
1289 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001290 recover();
1291
Olli Etuaho376f1b52015-04-13 13:23:41 +03001292 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001293 recover();
1294
1295 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1296 {
1297 recover();
1298 }
1299
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001300 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001301
1302 int size;
1303 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1304 {
1305 recover();
1306 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001307 // Make the type an array even if size check failed.
1308 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1309 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001310
Olli Etuaho2935c582015-04-08 14:32:06 +03001311 TVariable *variable = nullptr;
1312 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001313 recover();
1314
Olli Etuahoe7847b02015-03-16 11:56:12 +02001315 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001316 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001317 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001318
Olli Etuahoe7847b02015-03-16 11:56:12 +02001319 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001320}
1321
Olli Etuahoe7847b02015-03-16 11:56:12 +02001322TIntermAggregate *TParseContext::parseSingleInitDeclaration(TPublicType &publicType,
1323 const TSourceLoc &identifierLocation,
1324 const TString &identifier,
1325 const TSourceLoc &initLocation,
1326 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001327{
Olli Etuahofa33d582015-04-09 14:33:12 +03001328 mDeferredSingleDeclarationErrorCheck = false;
1329
1330 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001331 recover();
1332
Olli Etuahoe7847b02015-03-16 11:56:12 +02001333 TIntermNode *intermNode = nullptr;
1334 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001335 {
1336 //
1337 // Build intermediate representation
1338 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001339 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001340 }
1341 else
1342 {
1343 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001344 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001345 }
1346}
1347
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001348TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1349 const TSourceLoc &identifierLocation,
1350 const TString &identifier,
1351 const TSourceLoc &indexLocation,
1352 TIntermTyped *indexExpression,
1353 const TSourceLoc &initLocation,
1354 TIntermTyped *initializer)
1355{
1356 mDeferredSingleDeclarationErrorCheck = false;
1357
1358 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1359 recover();
1360
1361 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1362 {
1363 recover();
1364 }
1365
1366 TPublicType arrayType(publicType);
1367
Olli Etuaho376f1b52015-04-13 13:23:41 +03001368 int size = 0;
1369 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1370 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001371 {
1372 recover();
1373 }
1374 // Make the type an array even if size check failed.
1375 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1376 arrayType.setArraySize(size);
1377
1378 // initNode will correspond to the whole of "type b[n] = initializer".
1379 TIntermNode *initNode = nullptr;
1380 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1381 {
1382 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1383 }
1384 else
1385 {
1386 recover();
1387 return nullptr;
1388 }
1389}
1390
Olli Etuahoe7847b02015-03-16 11:56:12 +02001391TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001392 const TSourceLoc &identifierLoc,
1393 const TString *identifier,
1394 const TSymbol *symbol)
1395{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001396 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001397 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1398 {
1399 recover();
1400 }
1401
1402 if (!symbol)
1403 {
1404 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1405 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001406 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001407 }
1408 else
1409 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001410 const TString kGlFrontFacing("gl_FrontFacing");
1411 if (*identifier == kGlFrontFacing)
1412 {
1413 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1414 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001415 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001416 }
Jamie Madill2c433252014-12-03 12:36:54 -05001417 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001418 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1419 ASSERT(variable);
1420 const TType &type = variable->getType();
1421 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1422 *identifier, type, identifierLoc);
1423
1424 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1425 aggregate->setOp(EOpInvariantDeclaration);
1426 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001427 }
1428}
1429
Olli Etuahoe7847b02015-03-16 11:56:12 +02001430TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1431 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001432{
Olli Etuahofa33d582015-04-09 14:33:12 +03001433 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1434 if (mDeferredSingleDeclarationErrorCheck)
1435 {
1436 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1437 recover();
1438 mDeferredSingleDeclarationErrorCheck = false;
1439 }
1440
Jamie Madill0bd18df2013-06-20 11:55:52 -04001441 if (locationDeclaratorListCheck(identifierLocation, publicType))
1442 recover();
1443
Olli Etuaho376f1b52015-04-13 13:23:41 +03001444 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001445 recover();
1446
Olli Etuaho2935c582015-04-08 14:32:06 +03001447 TVariable *variable = nullptr;
1448 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001449 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001450
1451 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1452 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001453 symbol->setId(variable->getUniqueId());
1454
Olli Etuahoe7847b02015-03-16 11:56:12 +02001455 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001456}
1457
Olli Etuahoe7847b02015-03-16 11:56:12 +02001458TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1459 const TSourceLoc &identifierLocation, const TString &identifier,
1460 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001461{
Olli Etuahofa33d582015-04-09 14:33:12 +03001462 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1463 if (mDeferredSingleDeclarationErrorCheck)
1464 {
1465 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1466 recover();
1467 mDeferredSingleDeclarationErrorCheck = false;
1468 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001469
Jamie Madill0bd18df2013-06-20 11:55:52 -04001470 if (locationDeclaratorListCheck(identifierLocation, publicType))
1471 recover();
1472
Olli Etuaho376f1b52015-04-13 13:23:41 +03001473 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001474 recover();
1475
1476 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1477 {
1478 recover();
1479 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001480 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001481 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001482 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001483 int size;
1484 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001485 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001486 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001487 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001488 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001489
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001490 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001491 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001492 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001493
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001494 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1495 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001496 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001497
1498 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001499 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001500
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001501 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001502}
1503
Olli Etuahoe7847b02015-03-16 11:56:12 +02001504TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1505 const TSourceLoc &identifierLocation, const TString &identifier,
1506 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001507{
Olli Etuahofa33d582015-04-09 14:33:12 +03001508 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1509 if (mDeferredSingleDeclarationErrorCheck)
1510 {
1511 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1512 recover();
1513 mDeferredSingleDeclarationErrorCheck = false;
1514 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001515
Jamie Madill0bd18df2013-06-20 11:55:52 -04001516 if (locationDeclaratorListCheck(identifierLocation, publicType))
1517 recover();
1518
Olli Etuahoe7847b02015-03-16 11:56:12 +02001519 TIntermNode *intermNode = nullptr;
1520 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001521 {
1522 //
1523 // build the intermediate representation
1524 //
1525 if (intermNode)
1526 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001527 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001528 }
1529 else
1530 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001531 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001532 }
1533 }
1534 else
1535 {
1536 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001537 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001538 }
1539}
1540
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001541TIntermAggregate *TParseContext::parseArrayInitDeclarator(TPublicType &publicType,
1542 TIntermAggregate *aggregateDeclaration,
1543 const TSourceLoc& identifierLocation,
1544 const TString &identifier,
1545 const TSourceLoc& indexLocation,
1546 TIntermTyped *indexExpression,
1547 const TSourceLoc &initLocation, TIntermTyped *initializer)
1548{
1549 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1550 if (mDeferredSingleDeclarationErrorCheck)
1551 {
1552 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1553 recover();
1554 mDeferredSingleDeclarationErrorCheck = false;
1555 }
1556
1557 if (locationDeclaratorListCheck(identifierLocation, publicType))
1558 recover();
1559
1560 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1561 {
1562 recover();
1563 }
1564
1565 TPublicType arrayType(publicType);
1566
Olli Etuaho376f1b52015-04-13 13:23:41 +03001567 int size = 0;
1568 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1569 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001570 {
1571 recover();
1572 }
1573 // Make the type an array even if size check failed.
1574 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1575 arrayType.setArraySize(size);
1576
1577 // initNode will correspond to the whole of "b[n] = initializer".
1578 TIntermNode *initNode = nullptr;
1579 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1580 {
1581 if (initNode)
1582 {
1583 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1584 }
1585 else
1586 {
1587 return aggregateDeclaration;
1588 }
1589 }
1590 else
1591 {
1592 recover();
1593 return nullptr;
1594 }
1595}
1596
Jamie Madilla295edf2013-06-06 11:56:48 -04001597void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1598{
1599 if (typeQualifier.qualifier != EvqUniform)
1600 {
1601 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1602 recover();
1603 return;
1604 }
1605
1606 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1607 ASSERT(!layoutQualifier.isEmpty());
1608
1609 if (shaderVersion < 300)
1610 {
1611 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1612 recover();
1613 return;
1614 }
1615
1616 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1617 {
1618 recover();
1619 return;
1620 }
1621
Jamie Madill099c0f32013-06-20 11:55:52 -04001622 if (layoutQualifier.matrixPacking != EmpUnspecified)
1623 {
1624 defaultMatrixPacking = layoutQualifier.matrixPacking;
1625 }
1626
Geoff Langc6856732014-02-11 09:38:55 -05001627 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001628 {
1629 defaultBlockStorage = layoutQualifier.blockStorage;
1630 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001631}
1632
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001633TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1634{
1635 TOperator op = EOpNull;
1636 if (publicType.userDef)
1637 {
1638 op = EOpConstructStruct;
1639 }
1640 else
1641 {
1642 switch (publicType.type)
1643 {
1644 case EbtFloat:
1645 if (publicType.isMatrix())
1646 {
1647 // TODO: non-square matrices
1648 switch(publicType.getCols())
1649 {
Jamie Madill28b97422013-07-08 14:01:38 -04001650 case 2: op = EOpConstructMat2; break;
1651 case 3: op = EOpConstructMat3; break;
1652 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001653 }
1654 }
1655 else
1656 {
1657 switch(publicType.getNominalSize())
1658 {
Jamie Madill28b97422013-07-08 14:01:38 -04001659 case 1: op = EOpConstructFloat; break;
1660 case 2: op = EOpConstructVec2; break;
1661 case 3: op = EOpConstructVec3; break;
1662 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001663 }
1664 }
1665 break;
1666
1667 case EbtInt:
1668 switch(publicType.getNominalSize())
1669 {
Jamie Madill28b97422013-07-08 14:01:38 -04001670 case 1: op = EOpConstructInt; break;
1671 case 2: op = EOpConstructIVec2; break;
1672 case 3: op = EOpConstructIVec3; break;
1673 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001674 }
1675 break;
1676
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001677 case EbtUInt:
1678 switch(publicType.getNominalSize())
1679 {
Jamie Madill28b97422013-07-08 14:01:38 -04001680 case 1: op = EOpConstructUInt; break;
1681 case 2: op = EOpConstructUVec2; break;
1682 case 3: op = EOpConstructUVec3; break;
1683 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001684 }
1685 break;
1686
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001687 case EbtBool:
1688 switch(publicType.getNominalSize())
1689 {
Jamie Madill28b97422013-07-08 14:01:38 -04001690 case 1: op = EOpConstructBool; break;
1691 case 2: op = EOpConstructBVec2; break;
1692 case 3: op = EOpConstructBVec3; break;
1693 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001694 }
1695 break;
1696
1697 default: break;
1698 }
1699
1700 if (op == EOpNull)
1701 {
1702 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1703 recover();
1704 publicType.type = EbtFloat;
1705 op = EOpConstructFloat;
1706 }
1707 }
1708
1709 TString tempString;
1710 TType type(publicType);
1711 return new TFunction(&tempString, type, op);
1712}
1713
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714// This function is used to test for the correctness of the parameters passed to various constructor functions
1715// and also convert them to the right datatype if it is allowed and required.
1716//
1717// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1718//
Olli Etuaho21203702014-11-13 16:16:21 +02001719TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001720{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001721 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001723 if (!aggregateArguments)
1724 {
1725 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001726 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001727 }
1728
Olli Etuahof40319e2015-03-10 14:33:00 +02001729 if (type->isArray())
1730 {
1731 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1732 TIntermSequence *args = aggregateArguments->getSequence();
1733 for (size_t i = 0; i < args->size(); i++)
1734 {
1735 const TType &argType = (*args)[i]->getAsTyped()->getType();
1736 // It has already been checked that the argument is not an array.
1737 ASSERT(!argType.isArray());
1738 if (!argType.sameElementType(*type))
1739 {
1740 error(line, "Array constructor argument has an incorrect type", "Error");
1741 recover();
1742 return nullptr;
1743 }
1744 }
1745 }
1746 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001747 {
1748 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001749 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001751 for (size_t i = 0; i < fields.size(); i++)
1752 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001753 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001754 {
1755 error(line, "Structure constructor arguments do not match structure fields", "Error");
1756 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001758 return 0;
1759 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001760 }
1761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001763 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001764 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1765 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001766 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001767 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001768 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770
Olli Etuaho21203702014-11-13 16:16:21 +02001771 // Structs should not be precision qualified, the individual members may be.
1772 // Built-in types on the other hand should be precision qualified.
1773 if (op != EOpConstructStruct)
1774 {
1775 constructor->setPrecisionFromChildren();
1776 type->setPrecision(constructor->getPrecision());
1777 }
1778
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001779 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780}
1781
1782TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1783{
Olli Etuahof40319e2015-03-10 14:33:00 +02001784 // TODO: Add support for folding array constructors
1785 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 aggrNode->setType(type);
1787 if (canBeFolded) {
1788 bool returnVal = false;
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001789 TConstantUnion* unionArray = new TConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001790 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001791 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 }
1793 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001794 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001795 }
1796 if (returnVal)
1797 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001799 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803}
1804
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805//
1806// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1807// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1808// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1809// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1810// a constant matrix.
1811//
Jamie Madill075edd82013-07-08 13:30:19 -04001812TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001814 TIntermTyped* typedNode;
1815 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001817 TConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001818 if (tempConstantNode) {
1819 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001821 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001822 return node;
1823 }
1824 } 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 +00001825 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001828 return 0;
1829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001831 TConstantUnion* constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001833 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001834 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001835 std::stringstream extraInfoStream;
1836 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1837 std::string extraInfo = extraInfoStream.str();
1838 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 recover();
1840 fields.offsets[i] = 0;
1841 }
1842
1843 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 }
1846 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1847 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848}
1849
1850//
1851// This function returns the column being accessed from a constant matrix. The values are retrieved from
1852// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1853// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1854// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1855//
Jamie Madill075edd82013-07-08 13:30:19 -04001856TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001858 TIntermTyped* typedNode;
1859 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001861 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001862 std::stringstream extraInfoStream;
1863 extraInfoStream << "matrix field selection out of range '" << index << "'";
1864 std::string extraInfo = extraInfoStream.str();
1865 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001866 recover();
1867 index = 0;
1868 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001871 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001872 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001873 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1874 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001875 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001876 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001878 return 0;
1879 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882}
1883
1884
1885//
1886// This function returns an element of an array accessed from a constant array. The values are retrieved from
1887// the symbol table and parse-tree is built for the type of the element. The input
1888// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1889// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1890//
Jamie Madill075edd82013-07-08 13:30:19 -04001891TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001893 TIntermTyped* typedNode;
1894 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1895 TType arrayElementType = node->getType();
1896 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001898 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001899 std::stringstream extraInfoStream;
1900 extraInfoStream << "array field selection out of range '" << index << "'";
1901 std::string extraInfo = extraInfoStream.str();
1902 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001903 recover();
1904 index = 0;
1905 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001907 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001908 size_t arrayElementSize = arrayElementType.getObjectSize();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001909 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001910 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001911 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001912 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001913 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001915 return 0;
1916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001918 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919}
1920
1921
1922//
1923// This function returns the value of a particular field inside a constant structure from the symbol table.
1924// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1925// function and returns the parse-tree with the values of the embedded/nested struct.
1926//
Jamie Madill075edd82013-07-08 13:30:19 -04001927TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928{
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001930 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931
Jamie Madill98493dd2013-07-08 14:39:03 -04001932 for (size_t index = 0; index < fields.size(); ++index) {
1933 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001934 break;
1935 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001936 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001937 }
1938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Jamie Madill94bf7f22013-07-08 13:31:15 -04001940 TIntermTyped *typedNode;
1941 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001942 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001943 TConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001945 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1946 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001947 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001948 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 return 0;
1951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001953 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954}
1955
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001956//
1957// Interface/uniform blocks
1958//
Jamie Madill98493dd2013-07-08 14:39:03 -04001959TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1960 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001961{
1962 if (reservedErrorCheck(nameLine, blockName))
1963 recover();
1964
1965 if (typeQualifier.qualifier != EvqUniform)
1966 {
1967 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1968 recover();
1969 }
1970
Jamie Madill099c0f32013-06-20 11:55:52 -04001971 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1972 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001973 {
1974 recover();
1975 }
1976
Jamie Madill099c0f32013-06-20 11:55:52 -04001977 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1978 {
1979 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1980 }
1981
Jamie Madill1566ef72013-06-20 11:55:54 -04001982 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1983 {
1984 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1985 }
1986
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001987 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001988 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001989 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1990 recover();
1991 }
1992
Jamie Madill98493dd2013-07-08 14:39:03 -04001993 // check for sampler types and apply layout qualifiers
1994 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1995 TField* field = (*fieldList)[memberIndex];
1996 TType* fieldType = field->type();
1997 if (IsSampler(fieldType->getBasicType())) {
1998 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001999 recover();
2000 }
2001
Jamie Madill98493dd2013-07-08 14:39:03 -04002002 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002003 switch (qualifier)
2004 {
2005 case EvqGlobal:
2006 case EvqUniform:
2007 break;
2008 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002009 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002010 recover();
2011 break;
2012 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002013
2014 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002015 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2016 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002017 {
2018 recover();
2019 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002020
Jamie Madill98493dd2013-07-08 14:39:03 -04002021 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002022 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002023 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002024 recover();
2025 }
2026
Jamie Madill98493dd2013-07-08 14:39:03 -04002027 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002028 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002029 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002030 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002031 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002032 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002033 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002034 recover();
2035 }
2036
Jamie Madill98493dd2013-07-08 14:39:03 -04002037 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002038 }
2039
Jamie Madill98493dd2013-07-08 14:39:03 -04002040 // add array index
2041 int arraySize = 0;
2042 if (arrayIndex != NULL)
2043 {
2044 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2045 recover();
2046 }
2047
2048 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2049 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002050
2051 TString symbolName = "";
2052 int symbolId = 0;
2053
Jamie Madill98493dd2013-07-08 14:39:03 -04002054 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002055 {
2056 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002057 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2058 {
2059 TField* field = (*fieldList)[memberIndex];
2060 TType* fieldType = field->type();
2061
2062 // set parent pointer of the field variable
2063 fieldType->setInterfaceBlock(interfaceBlock);
2064
2065 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2066 fieldVariable->setQualifier(typeQualifier.qualifier);
2067
Nicolas Capensadfffe42014-06-17 02:13:36 -04002068 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002069 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002070 recover();
2071 }
2072 }
2073 }
2074 else
2075 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002076 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002077 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002078 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002079
Nicolas Capensadfffe42014-06-17 02:13:36 -04002080 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002081 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002082 recover();
2083 }
2084
2085 symbolId = instanceTypeDef->getUniqueId();
2086 symbolName = instanceTypeDef->getName();
2087 }
2088
Jamie Madill98493dd2013-07-08 14:39:03 -04002089 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002090 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002091
2092 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002093 return aggregate;
2094}
2095
Jamie Madill075edd82013-07-08 13:30:19 -04002096bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002097{
2098 ++structNestingLevel;
2099
2100 // Embedded structure definitions are not supported per GLSL ES spec.
2101 // They aren't allowed in GLSL either, but we need to detect this here
2102 // so we don't rely on the GLSL compiler to catch it.
2103 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002104 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002105 return true;
2106 }
2107
2108 return false;
2109}
2110
2111void TParseContext::exitStructDeclaration()
2112{
2113 --structNestingLevel;
2114}
2115
2116namespace {
2117
2118const int kWebGLMaxStructNesting = 4;
2119
2120} // namespace
2121
Jamie Madill98493dd2013-07-08 14:39:03 -04002122bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002123{
Jamie Madill5508f392014-02-20 13:31:36 -05002124 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002125 return false;
2126 }
2127
Jamie Madill98493dd2013-07-08 14:39:03 -04002128 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002129 return false;
2130 }
2131
2132 // We're already inside a structure definition at this point, so add
2133 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002134 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002135 std::stringstream reasonStream;
2136 reasonStream << "Reference of struct type "
2137 << field.type()->getStruct()->name().c_str()
2138 << " exceeds maximum allowed nesting level of "
2139 << kWebGLMaxStructNesting;
2140 std::string reason = reasonStream.str();
2141 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002142 return true;
2143 }
2144
2145 return false;
2146}
2147
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002148//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002149// Parse an array index expression
2150//
Jamie Madill075edd82013-07-08 13:30:19 -04002151TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002152{
2153 TIntermTyped *indexedExpression = NULL;
2154
2155 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2156 {
2157 if (baseExpression->getAsSymbolNode())
2158 {
2159 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2160 }
2161 else
2162 {
2163 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2164 }
2165 recover();
2166 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002167
Jamie Madill21c1e452014-12-29 11:33:41 -05002168 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2169
2170 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002171 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002172 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002173 if (index < 0)
2174 {
2175 std::stringstream infoStream;
2176 infoStream << index;
2177 std::string info = infoStream.str();
2178 error(location, "negative index", info.c_str());
2179 recover();
2180 index = 0;
2181 }
2182 if (baseExpression->getType().getQualifier() == EvqConst)
2183 {
2184 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002185 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002186 // constant folding for arrays
2187 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002188 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002189 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002190 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002191 // constant folding for vectors
2192 TVectorFields fields;
2193 fields.num = 1;
2194 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2195 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2196 }
2197 else if (baseExpression->isMatrix())
2198 {
2199 // constant folding for matrices
2200 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002201 }
2202 }
2203 else
2204 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002205 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002206 {
Jamie Madill18464b52013-07-08 14:01:55 -04002207 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002208 {
2209 std::stringstream extraInfoStream;
2210 extraInfoStream << "array index out of range '" << index << "'";
2211 std::string extraInfo = extraInfoStream.str();
2212 error(location, "", "[", extraInfo.c_str());
2213 recover();
2214 index = baseExpression->getType().getArraySize() - 1;
2215 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002216 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2217 {
2218 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2219 recover();
2220 index = 0;
2221 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002222 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002223 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002224 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002225 std::stringstream extraInfoStream;
2226 extraInfoStream << "field selection out of range '" << index << "'";
2227 std::string extraInfo = extraInfoStream.str();
2228 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002229 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002230 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002231 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002232
Jamie Madill21c1e452014-12-29 11:33:41 -05002233 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002234 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002235 }
2236 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002237 else
2238 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002239 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002240 {
Jamie Madill19571812013-08-12 15:26:34 -07002241 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002242 recover();
2243 }
Jamie Madill19571812013-08-12 15:26:34 -07002244 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002245 {
Jamie Madill19571812013-08-12 15:26:34 -07002246 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002247 recover();
2248 }
2249
2250 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2251 }
2252
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002253 if (indexedExpression == 0)
2254 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002255 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002256 unionArray->setFConst(0.0f);
2257 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2258 }
2259 else if (baseExpression->isArray())
2260 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002261 const TType &baseType = baseExpression->getType();
2262 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002263 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002264 TType copyOfType(baseType.getStruct());
2265 indexedExpression->setType(copyOfType);
2266 }
2267 else if (baseType.isInterfaceBlock())
2268 {
2269 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002270 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002271 }
2272 else
2273 {
Minmin Gong794e0002015-04-07 18:31:54 -07002274 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2275 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002276 }
2277
2278 if (baseExpression->getType().getQualifier() == EvqConst)
2279 {
2280 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2281 }
2282 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002283 else if (baseExpression->isMatrix())
2284 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002285 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002286 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002287 }
2288 else if (baseExpression->isVector())
2289 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002290 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2291 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002292 }
2293 else
2294 {
2295 indexedExpression->setType(baseExpression->getType());
2296 }
2297
2298 return indexedExpression;
2299}
2300
Jamie Madill075edd82013-07-08 13:30:19 -04002301TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002302{
2303 TIntermTyped *indexedExpression = NULL;
2304
2305 if (baseExpression->isArray())
2306 {
2307 error(fieldLocation, "cannot apply dot operator to an array", ".");
2308 recover();
2309 }
2310
2311 if (baseExpression->isVector())
2312 {
2313 TVectorFields fields;
2314 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2315 {
2316 fields.num = 1;
2317 fields.offsets[0] = 0;
2318 recover();
2319 }
2320
2321 if (baseExpression->getType().getQualifier() == EvqConst)
2322 {
2323 // constant folding for vector fields
2324 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2325 if (indexedExpression == 0)
2326 {
2327 recover();
2328 indexedExpression = baseExpression;
2329 }
2330 else
2331 {
Minmin Gong794e0002015-04-07 18:31:54 -07002332 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002333 }
2334 }
2335 else
2336 {
2337 TString vectorString = fieldString;
2338 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2339 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002340 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002341 }
2342 }
2343 else if (baseExpression->isMatrix())
2344 {
2345 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002346 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002347 {
2348 fields.wholeRow = false;
2349 fields.wholeCol = false;
2350 fields.row = 0;
2351 fields.col = 0;
2352 recover();
2353 }
2354
2355 if (fields.wholeRow || fields.wholeCol)
2356 {
2357 error(dotLocation, " non-scalar fields not implemented yet", ".");
2358 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002359 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002360 unionArray->setIConst(0);
2361 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2362 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002363 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2364 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002365 }
2366 else
2367 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002368 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002369 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2371 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2372 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2373 }
2374 }
2375 else if (baseExpression->getBasicType() == EbtStruct)
2376 {
2377 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002378 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2379 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 {
2381 error(dotLocation, "structure has no fields", "Internal Error");
2382 recover();
2383 indexedExpression = baseExpression;
2384 }
2385 else
2386 {
2387 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002388 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002389 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002390 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002391 {
2392 fieldFound = true;
2393 break;
2394 }
2395 }
2396 if (fieldFound)
2397 {
2398 if (baseExpression->getType().getQualifier() == EvqConst)
2399 {
2400 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2401 if (indexedExpression == 0)
2402 {
2403 recover();
2404 indexedExpression = baseExpression;
2405 }
2406 else
2407 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002408 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002409 // change the qualifier of the return type, not of the structure field
2410 // as the structure definition is shared between various structures.
2411 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2412 }
2413 }
2414 else
2415 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002416 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002417 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002418 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002419 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002420 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002421 }
2422 }
2423 else
2424 {
2425 error(dotLocation, " no such field in structure", fieldString.c_str());
2426 recover();
2427 indexedExpression = baseExpression;
2428 }
2429 }
2430 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002431 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002432 {
2433 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002434 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2435 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002436 {
2437 error(dotLocation, "interface block has no fields", "Internal Error");
2438 recover();
2439 indexedExpression = baseExpression;
2440 }
2441 else
2442 {
2443 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002444 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002445 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002446 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002447 {
2448 fieldFound = true;
2449 break;
2450 }
2451 }
2452 if (fieldFound)
2453 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002454 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002456 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002457 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002458 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002459 }
2460 else
2461 {
2462 error(dotLocation, " no such field in interface block", fieldString.c_str());
2463 recover();
2464 indexedExpression = baseExpression;
2465 }
2466 }
2467 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002468 else
2469 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002470 if (shaderVersion < 300)
2471 {
2472 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2473 }
2474 else
2475 {
2476 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2477 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002478 recover();
2479 indexedExpression = baseExpression;
2480 }
2481
2482 return indexedExpression;
2483}
2484
Jamie Madill075edd82013-07-08 13:30:19 -04002485TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002486{
Jamie Madilla5efff92013-06-06 11:56:47 -04002487 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002488
Jamie Madilla5efff92013-06-06 11:56:47 -04002489 qualifier.location = -1;
2490 qualifier.matrixPacking = EmpUnspecified;
2491 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002492
2493 if (qualifierType == "shared")
2494 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002495 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002496 }
2497 else if (qualifierType == "packed")
2498 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002499 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002500 }
2501 else if (qualifierType == "std140")
2502 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002503 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002504 }
2505 else if (qualifierType == "row_major")
2506 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002507 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002508 }
2509 else if (qualifierType == "column_major")
2510 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002511 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002512 }
2513 else if (qualifierType == "location")
2514 {
2515 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2516 recover();
2517 }
2518 else
2519 {
2520 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2521 recover();
2522 }
2523
Jamie Madilla5efff92013-06-06 11:56:47 -04002524 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002525}
2526
Jamie Madill075edd82013-07-08 13:30:19 -04002527TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002528{
Jamie Madilla5efff92013-06-06 11:56:47 -04002529 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002530
Jamie Madilla5efff92013-06-06 11:56:47 -04002531 qualifier.location = -1;
2532 qualifier.matrixPacking = EmpUnspecified;
2533 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002534
2535 if (qualifierType != "location")
2536 {
2537 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2538 recover();
2539 }
2540 else
2541 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002542 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002543 if (intValue < 0)
2544 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002545 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002546 recover();
2547 }
2548 else
2549 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002550 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002551 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002552 }
2553
Jamie Madilla5efff92013-06-06 11:56:47 -04002554 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002555}
2556
Jamie Madilla5efff92013-06-06 11:56:47 -04002557TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002558{
Jamie Madilla5efff92013-06-06 11:56:47 -04002559 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002560
Jamie Madilla5efff92013-06-06 11:56:47 -04002561 if (rightQualifier.location != -1)
2562 {
2563 joinedQualifier.location = rightQualifier.location;
2564 }
2565 if (rightQualifier.matrixPacking != EmpUnspecified)
2566 {
2567 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2568 }
2569 if (rightQualifier.blockStorage != EbsUnspecified)
2570 {
2571 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2572 }
2573
2574 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002575}
2576
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002577TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2578 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2579{
2580 TQualifier mergedQualifier = EvqSmoothIn;
2581
Jamie Madill19571812013-08-12 15:26:34 -07002582 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002583 if (interpolationQualifier == EvqSmooth)
2584 mergedQualifier = EvqSmoothIn;
2585 else if (interpolationQualifier == EvqFlat)
2586 mergedQualifier = EvqFlatIn;
2587 else UNREACHABLE();
2588 }
2589 else if (storageQualifier == EvqCentroidIn) {
2590 if (interpolationQualifier == EvqSmooth)
2591 mergedQualifier = EvqCentroidIn;
2592 else if (interpolationQualifier == EvqFlat)
2593 mergedQualifier = EvqFlatIn;
2594 else UNREACHABLE();
2595 }
Jamie Madill19571812013-08-12 15:26:34 -07002596 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002597 if (interpolationQualifier == EvqSmooth)
2598 mergedQualifier = EvqSmoothOut;
2599 else if (interpolationQualifier == EvqFlat)
2600 mergedQualifier = EvqFlatOut;
2601 else UNREACHABLE();
2602 }
2603 else if (storageQualifier == EvqCentroidOut) {
2604 if (interpolationQualifier == EvqSmooth)
2605 mergedQualifier = EvqCentroidOut;
2606 else if (interpolationQualifier == EvqFlat)
2607 mergedQualifier = EvqFlatOut;
2608 else UNREACHABLE();
2609 }
2610 else {
2611 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2612 recover();
2613
2614 mergedQualifier = storageQualifier;
2615 }
2616
2617 TPublicType type;
2618 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2619 return type;
2620}
2621
Jamie Madill98493dd2013-07-08 14:39:03 -04002622TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002623{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002624 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2625 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002626 recover();
2627 }
2628
Jamie Madill98493dd2013-07-08 14:39:03 -04002629 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002630 //
2631 // Careful not to replace already known aspects of type, like array-ness
2632 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002633 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002634 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002635 type->setPrimarySize(typeSpecifier.primarySize);
2636 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002637 type->setPrecision(typeSpecifier.precision);
2638 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002639 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002640
2641 // don't allow arrays of arrays
2642 if (type->isArray()) {
2643 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2644 recover();
2645 }
2646 if (typeSpecifier.array)
2647 type->setArraySize(typeSpecifier.arraySize);
2648 if (typeSpecifier.userDef) {
2649 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002650 }
2651
Jamie Madill98493dd2013-07-08 14:39:03 -04002652 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002653 recover();
2654 }
2655 }
2656
Jamie Madill98493dd2013-07-08 14:39:03 -04002657 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002658}
2659
Jamie Madill98493dd2013-07-08 14:39:03 -04002660TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002661{
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 TStructure* structure = new TStructure(structName, fieldList);
2663 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002664
Jamie Madill9b820842015-02-12 10:40:10 -05002665 // Store a bool in the struct if we're at global scope, to allow us to
2666 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002667 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002668 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002669
Jamie Madill98493dd2013-07-08 14:39:03 -04002670 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002671 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002672 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002673 {
2674 recover();
2675 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002676 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002677 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002678 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002679 recover();
2680 }
2681 }
2682
2683 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002684 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002685 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002686 const TField &field = *(*fieldList)[typeListIndex];
2687 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002688 switch (qualifier)
2689 {
2690 case EvqGlobal:
2691 case EvqTemporary:
2692 break;
2693 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002695 recover();
2696 break;
2697 }
2698 }
2699
2700 TPublicType publicType;
2701 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002702 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002703 exitStructDeclaration();
2704
2705 return publicType;
2706}
2707
Olli Etuahoa3a36662015-02-17 13:46:51 +02002708TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2709{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002710 TBasicType switchType = init->getBasicType();
2711 if ((switchType != EbtInt && switchType != EbtUInt) ||
2712 init->isMatrix() ||
2713 init->isArray() ||
2714 init->isVector())
2715 {
2716 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2717 recover();
2718 return nullptr;
2719 }
2720
Olli Etuahoac5274d2015-02-20 10:19:08 +02002721 if (statementList)
2722 {
2723 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2724 {
2725 recover();
2726 return nullptr;
2727 }
2728 }
2729
Olli Etuahoa3a36662015-02-17 13:46:51 +02002730 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2731 if (node == nullptr)
2732 {
2733 error(loc, "erroneous switch statement", "switch");
2734 recover();
2735 return nullptr;
2736 }
2737 return node;
2738}
2739
2740TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2741{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002742 if (mSwitchNestingLevel == 0)
2743 {
2744 error(loc, "case labels need to be inside switch statements", "case");
2745 recover();
2746 return nullptr;
2747 }
2748 if (condition == nullptr)
2749 {
2750 error(loc, "case label must have a condition", "case");
2751 recover();
2752 return nullptr;
2753 }
2754 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2755 condition->isMatrix() ||
2756 condition->isArray() ||
2757 condition->isVector())
2758 {
2759 error(condition->getLine(), "case label must be a scalar integer", "case");
2760 recover();
2761 }
2762 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2763 if (conditionConst == nullptr)
2764 {
2765 error(condition->getLine(), "case label must be constant", "case");
2766 recover();
2767 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002768 TIntermCase *node = intermediate.addCase(condition, loc);
2769 if (node == nullptr)
2770 {
2771 error(loc, "erroneous case statement", "case");
2772 recover();
2773 return nullptr;
2774 }
2775 return node;
2776}
2777
2778TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2779{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002780 if (mSwitchNestingLevel == 0)
2781 {
2782 error(loc, "default labels need to be inside switch statements", "default");
2783 recover();
2784 return nullptr;
2785 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002786 TIntermCase *node = intermediate.addCase(nullptr, loc);
2787 if (node == nullptr)
2788 {
2789 error(loc, "erroneous default statement", "default");
2790 recover();
2791 return nullptr;
2792 }
2793 return node;
2794}
2795
Olli Etuahof6c694b2015-03-26 14:50:53 +02002796TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2797 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002798{
2799 if (child == nullptr)
2800 {
2801 return nullptr;
2802 }
2803
2804 switch (op)
2805 {
2806 case EOpLogicalNot:
2807 if (child->getBasicType() != EbtBool ||
2808 child->isMatrix() ||
2809 child->isArray() ||
2810 child->isVector())
2811 {
2812 return nullptr;
2813 }
2814 break;
2815 case EOpBitwiseNot:
2816 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2817 child->isMatrix() ||
2818 child->isArray())
2819 {
2820 return nullptr;
2821 }
2822 break;
2823 case EOpPostIncrement:
2824 case EOpPreIncrement:
2825 case EOpPostDecrement:
2826 case EOpPreDecrement:
2827 case EOpNegative:
2828 case EOpPositive:
2829 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002830 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002831 child->isArray())
2832 {
2833 return nullptr;
2834 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002835 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002836 default:
2837 break;
2838 }
2839
Olli Etuahof6c694b2015-03-26 14:50:53 +02002840 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002841}
2842
Olli Etuaho09b22472015-02-11 11:47:26 +02002843TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2844{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002845 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002846 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002847 {
2848 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2849 recover();
2850 return child;
2851 }
2852 return node;
2853}
2854
2855TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2856{
2857 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2858 recover();
2859 return addUnaryMath(op, child, loc);
2860}
2861
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002862bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002863 const TSourceLoc &loc)
2864{
2865 if (left->isArray() || right->isArray())
2866 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002867 if (shaderVersion < 300)
2868 {
2869 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2870 return false;
2871 }
2872
2873 if (left->isArray() != right->isArray())
2874 {
2875 error(loc, "array / non-array mismatch", GetOperatorString(op));
2876 return false;
2877 }
2878
2879 switch (op)
2880 {
2881 case EOpEqual:
2882 case EOpNotEqual:
2883 case EOpAssign:
2884 case EOpInitialize:
2885 break;
2886 default:
2887 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2888 return false;
2889 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002890 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002891 if (left->getArraySize() != right->getArraySize())
2892 {
2893 error(loc, "array size mismatch", GetOperatorString(op));
2894 return false;
2895 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002896 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002897
2898 // Check ops which require integer / ivec parameters
2899 bool isBitShift = false;
2900 switch (op)
2901 {
2902 case EOpBitShiftLeft:
2903 case EOpBitShiftRight:
2904 case EOpBitShiftLeftAssign:
2905 case EOpBitShiftRightAssign:
2906 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2907 // check that the basic type is an integer type.
2908 isBitShift = true;
2909 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2910 {
2911 return false;
2912 }
2913 break;
2914 case EOpBitwiseAnd:
2915 case EOpBitwiseXor:
2916 case EOpBitwiseOr:
2917 case EOpBitwiseAndAssign:
2918 case EOpBitwiseXorAssign:
2919 case EOpBitwiseOrAssign:
2920 // It is enough to check the type of only one operand, since later it
2921 // is checked that the operand types match.
2922 if (!IsInteger(left->getBasicType()))
2923 {
2924 return false;
2925 }
2926 break;
2927 default:
2928 break;
2929 }
2930
2931 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2932 // So the basic type should usually match.
2933 if (!isBitShift && left->getBasicType() != right->getBasicType())
2934 {
2935 return false;
2936 }
2937
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002938 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002939 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002940 switch(op)
2941 {
2942 case EOpAssign:
2943 case EOpInitialize:
2944 case EOpEqual:
2945 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002946 // ESSL 1.00 sections 5.7, 5.8, 5.9
2947 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2948 {
2949 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2950 return false;
2951 }
Olli Etuahoff699002015-03-23 14:38:42 +02002952 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2953 // we interpret the spec so that this extends to structs containing samplers,
2954 // similarly to ESSL 1.00 spec.
2955 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2956 left->getType().isStructureContainingSamplers())
2957 {
2958 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2959 return false;
2960 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002961 case EOpLessThan:
2962 case EOpGreaterThan:
2963 case EOpLessThanEqual:
2964 case EOpGreaterThanEqual:
2965 if ((left->getNominalSize() != right->getNominalSize()) ||
2966 (left->getSecondarySize() != right->getSecondarySize()))
2967 {
2968 return false;
2969 }
2970 default:
2971 break;
2972 }
2973
Olli Etuahod6b14282015-03-17 14:31:35 +02002974 return true;
2975}
2976
Olli Etuahofc1806e2015-03-17 13:03:11 +02002977TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2978 const TSourceLoc &loc)
2979{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002980 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002981 return nullptr;
2982
Olli Etuahofc1806e2015-03-17 13:03:11 +02002983 switch (op)
2984 {
2985 case EOpEqual:
2986 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002987 break;
2988 case EOpLessThan:
2989 case EOpGreaterThan:
2990 case EOpLessThanEqual:
2991 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02002992 ASSERT(!left->isArray() && !right->isArray());
2993 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02002994 left->getBasicType() == EbtStruct)
2995 {
2996 return nullptr;
2997 }
2998 break;
2999 case EOpLogicalOr:
3000 case EOpLogicalXor:
3001 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003002 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003003 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003004 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003005 {
3006 return nullptr;
3007 }
3008 break;
3009 case EOpAdd:
3010 case EOpSub:
3011 case EOpDiv:
3012 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003013 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003014 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3015 {
3016 return nullptr;
3017 }
3018 break;
3019 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003020 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003021 // Note that this is only for the % operator, not for mod()
3022 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3023 {
3024 return nullptr;
3025 }
3026 break;
3027 // Note that for bitwise ops, type checking is done in promote() to
3028 // share code between ops and compound assignment
3029 default:
3030 break;
3031 }
3032
Olli Etuahofc1806e2015-03-17 13:03:11 +02003033 return intermediate.addBinaryMath(op, left, right, loc);
3034}
3035
Olli Etuaho09b22472015-02-11 11:47:26 +02003036TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3037 const TSourceLoc &loc)
3038{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003039 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003040 if (node == 0)
3041 {
3042 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3043 recover();
3044 return left;
3045 }
3046 return node;
3047}
3048
3049TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3050 const TSourceLoc &loc)
3051{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003052 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003053 if (node == 0)
3054 {
3055 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3056 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003057 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003058 unionArray->setBConst(false);
3059 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3060 }
3061 return node;
3062}
3063
Olli Etuahod6b14282015-03-17 14:31:35 +02003064TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3065 const TSourceLoc &loc)
3066{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003067 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003068 {
3069 return intermediate.addAssign(op, left, right, loc);
3070 }
3071 return nullptr;
3072}
3073
3074TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3075 const TSourceLoc &loc)
3076{
3077 TIntermTyped *node = createAssign(op, left, right, loc);
3078 if (node == nullptr)
3079 {
3080 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3081 recover();
3082 return left;
3083 }
3084 return node;
3085}
3086
Olli Etuaho49300862015-02-20 14:54:49 +02003087TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3088{
3089 switch (op)
3090 {
3091 case EOpContinue:
3092 if (mLoopNestingLevel <= 0)
3093 {
3094 error(loc, "continue statement only allowed in loops", "");
3095 recover();
3096 }
3097 break;
3098 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003099 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003100 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003101 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003102 recover();
3103 }
3104 break;
3105 case EOpReturn:
3106 if (currentFunctionType->getBasicType() != EbtVoid)
3107 {
3108 error(loc, "non-void function must return a value", "return");
3109 recover();
3110 }
3111 break;
3112 default:
3113 // No checks for discard
3114 break;
3115 }
3116 return intermediate.addBranch(op, loc);
3117}
3118
3119TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3120{
3121 ASSERT(op == EOpReturn);
3122 mFunctionReturnsValue = true;
3123 if (currentFunctionType->getBasicType() == EbtVoid)
3124 {
3125 error(loc, "void function cannot return a value", "return");
3126 recover();
3127 }
3128 else if (*currentFunctionType != returnValue->getType())
3129 {
3130 error(loc, "function return is not matching type:", "return");
3131 recover();
3132 }
3133 return intermediate.addBranch(op, returnValue, loc);
3134}
3135
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003136TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3137 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003138{
3139 *fatalError = false;
3140 TOperator op = fnCall->getBuiltInOp();
3141 TIntermTyped *callNode = nullptr;
3142
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003143 if (thisNode != nullptr)
3144 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003145 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003146 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003147 TIntermTyped *typedThis = thisNode->getAsTyped();
3148 if (fnCall->getName() != "length")
3149 {
3150 error(loc, "invalid method", fnCall->getName().c_str());
3151 recover();
3152 }
3153 else if (paramNode != nullptr)
3154 {
3155 error(loc, "method takes no parameters", "length");
3156 recover();
3157 }
3158 else if (typedThis == nullptr || !typedThis->isArray())
3159 {
3160 error(loc, "length can only be called on arrays", "length");
3161 recover();
3162 }
3163 else
3164 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003165 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003166 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003167 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003168 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003169 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003170 // (func()).length()
3171 // (int[3](0, 1, 2)).length()
3172 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3173 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3174 // which allows "An array, vector or matrix expression with the length method applied".
3175 error(loc, "length can only be called on array names, not on array expressions", "length");
3176 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003177 }
3178 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003179 unionArray->setIConst(arraySize);
3180 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003181 }
3182 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003183 {
3184 //
3185 // Then this should be a constructor.
3186 // Don't go through the symbol table for constructors.
3187 // Their parameters will be verified algorithmically.
3188 //
3189 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003190 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003191 {
3192 //
3193 // It's a constructor, of type 'type'.
3194 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003195 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003196 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003197
3198 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003199 {
3200 recover();
3201 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3202 }
3203 callNode->setType(type);
3204 }
3205 else
3206 {
3207 //
3208 // Not a constructor. Find it in the symbol table.
3209 //
3210 const TFunction* fnCandidate;
3211 bool builtIn;
3212 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3213 if (fnCandidate)
3214 {
3215 //
3216 // A declared function.
3217 //
3218 if (builtIn && !fnCandidate->getExtension().empty() &&
3219 extensionErrorCheck(loc, fnCandidate->getExtension()))
3220 {
3221 recover();
3222 }
3223 op = fnCandidate->getBuiltInOp();
3224 if (builtIn && op != EOpNull)
3225 {
3226 //
3227 // A function call mapped to a built-in operation.
3228 //
3229 if (fnCandidate->getParamCount() == 1)
3230 {
3231 //
3232 // Treat it like a built-in unary operator.
3233 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003234 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003235 if (callNode == nullptr)
3236 {
3237 std::stringstream extraInfoStream;
3238 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003239 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003240 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003241 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003242 *fatalError = true;
3243 return nullptr;
3244 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003245 }
3246 else
3247 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003248 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003249 aggregate->setType(fnCandidate->getReturnType());
3250 aggregate->setPrecisionFromChildren();
3251 callNode = aggregate;
3252
3253 // Some built-in functions have out parameters too.
3254 functionCallLValueErrorCheck(fnCandidate, aggregate);
3255 }
3256 }
3257 else
3258 {
3259 // This is a real function call
3260
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003261 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003262 aggregate->setType(fnCandidate->getReturnType());
3263
3264 // this is how we know whether the given function is a builtIn function or a user defined function
3265 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3266 // if builtIn == true, it's definitely a builtIn function with EOpNull
3267 if (!builtIn)
3268 aggregate->setUserDefined();
3269 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003270 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003271
3272 // This needs to happen after the name is set
3273 if (builtIn)
3274 aggregate->setBuiltInFunctionPrecision();
3275
3276 callNode = aggregate;
3277
3278 functionCallLValueErrorCheck(fnCandidate, aggregate);
3279 }
3280 }
3281 else
3282 {
3283 // error message was put out by findFunction()
3284 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003285 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003286 unionArray->setFConst(0.0f);
3287 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3288 recover();
3289 }
3290 }
3291 delete fnCall;
3292 return callNode;
3293}
3294
Olli Etuaho52901742015-04-15 13:42:45 +03003295TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3296 const TSourceLoc &loc)
3297{
3298 if (boolErrorCheck(loc, cond))
3299 recover();
3300
3301 if (trueBlock->getType() != falseBlock->getType())
3302 {
3303 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3304 recover();
3305 return falseBlock;
3306 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003307 // ESSL1 sections 5.2 and 5.7:
3308 // ESSL3 section 5.7:
3309 // Ternary operator is not among the operators allowed for structures/arrays.
3310 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3311 {
3312 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3313 recover();
3314 return falseBlock;
3315 }
Olli Etuaho52901742015-04-15 13:42:45 +03003316 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3317}
Olli Etuaho49300862015-02-20 14:54:49 +02003318
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003319//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003320// Parse an array of strings using yyparse.
3321//
3322// Returns 0 for success.
3323//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003324int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003325 TParseContext* context) {
3326 if ((count == 0) || (string == NULL))
3327 return 1;
3328
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003329 if (glslang_initialize(context))
3330 return 1;
3331
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003332 int error = glslang_scan(count, string, length, context);
3333 if (!error)
3334 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003335
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003336 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003337
alokp@chromium.org6b495712012-06-29 00:06:58 +00003338 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003339}
3340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003341
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003342