blob: cd7ffc45b6214a59a6e22239da0d927d8aca87ef [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 Madillb11e2482015-05-04 14:21:22 -04001817 const 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 Madillb11e2482015-05-04 14:21:22 -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 Madillb11e2482015-05-04 14:21:22 -04002205 int safeIndex = -1;
2206
Jamie Madill7164cf42013-07-08 13:30:59 -04002207 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002208 {
Jamie Madill18464b52013-07-08 14:01:55 -04002209 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002210 {
2211 std::stringstream extraInfoStream;
2212 extraInfoStream << "array index out of range '" << index << "'";
2213 std::string extraInfo = extraInfoStream.str();
2214 error(location, "", "[", extraInfo.c_str());
2215 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002216 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002217 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002218 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2219 {
2220 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2221 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002222 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002223 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002224 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002225 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002226 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002227 std::stringstream extraInfoStream;
2228 extraInfoStream << "field selection out of range '" << index << "'";
2229 std::string extraInfo = extraInfoStream.str();
2230 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002231 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002232 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002233 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002234
Jamie Madillb11e2482015-05-04 14:21:22 -04002235 // Don't modify the data of the previous constant union, because it can point
2236 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2237 if (safeIndex != -1)
2238 {
2239 TConstantUnion *safeConstantUnion = new TConstantUnion();
2240 safeConstantUnion->setIConst(safeIndex);
2241 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2242 }
2243
Jamie Madill7164cf42013-07-08 13:30:59 -04002244 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002245 }
2246 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002247 else
2248 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002249 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002250 {
Jamie Madill19571812013-08-12 15:26:34 -07002251 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002252 recover();
2253 }
Jamie Madill19571812013-08-12 15:26:34 -07002254 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002255 {
Jamie Madill19571812013-08-12 15:26:34 -07002256 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002257 recover();
2258 }
2259
2260 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2261 }
2262
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002263 if (indexedExpression == 0)
2264 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002265 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002266 unionArray->setFConst(0.0f);
2267 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2268 }
2269 else if (baseExpression->isArray())
2270 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002271 const TType &baseType = baseExpression->getType();
2272 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002273 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002274 TType copyOfType(baseType.getStruct());
2275 indexedExpression->setType(copyOfType);
2276 }
2277 else if (baseType.isInterfaceBlock())
2278 {
2279 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002280 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002281 }
2282 else
2283 {
Minmin Gong794e0002015-04-07 18:31:54 -07002284 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2285 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002286 }
2287
2288 if (baseExpression->getType().getQualifier() == EvqConst)
2289 {
2290 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2291 }
2292 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002293 else if (baseExpression->isMatrix())
2294 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002295 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002296 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002297 }
2298 else if (baseExpression->isVector())
2299 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002300 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2301 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002302 }
2303 else
2304 {
2305 indexedExpression->setType(baseExpression->getType());
2306 }
2307
2308 return indexedExpression;
2309}
2310
Jamie Madill075edd82013-07-08 13:30:19 -04002311TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002312{
2313 TIntermTyped *indexedExpression = NULL;
2314
2315 if (baseExpression->isArray())
2316 {
2317 error(fieldLocation, "cannot apply dot operator to an array", ".");
2318 recover();
2319 }
2320
2321 if (baseExpression->isVector())
2322 {
2323 TVectorFields fields;
2324 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2325 {
2326 fields.num = 1;
2327 fields.offsets[0] = 0;
2328 recover();
2329 }
2330
2331 if (baseExpression->getType().getQualifier() == EvqConst)
2332 {
2333 // constant folding for vector fields
2334 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2335 if (indexedExpression == 0)
2336 {
2337 recover();
2338 indexedExpression = baseExpression;
2339 }
2340 else
2341 {
Minmin Gong794e0002015-04-07 18:31:54 -07002342 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002343 }
2344 }
2345 else
2346 {
2347 TString vectorString = fieldString;
2348 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2349 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002350 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002351 }
2352 }
2353 else if (baseExpression->isMatrix())
2354 {
2355 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002356 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002357 {
2358 fields.wholeRow = false;
2359 fields.wholeCol = false;
2360 fields.row = 0;
2361 fields.col = 0;
2362 recover();
2363 }
2364
2365 if (fields.wholeRow || fields.wholeCol)
2366 {
2367 error(dotLocation, " non-scalar fields not implemented yet", ".");
2368 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002369 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 unionArray->setIConst(0);
2371 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2372 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002373 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2374 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002375 }
2376 else
2377 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002378 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002379 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2381 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2382 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2383 }
2384 }
2385 else if (baseExpression->getBasicType() == EbtStruct)
2386 {
2387 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002388 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2389 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002390 {
2391 error(dotLocation, "structure has no fields", "Internal Error");
2392 recover();
2393 indexedExpression = baseExpression;
2394 }
2395 else
2396 {
2397 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002398 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002399 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002400 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002401 {
2402 fieldFound = true;
2403 break;
2404 }
2405 }
2406 if (fieldFound)
2407 {
2408 if (baseExpression->getType().getQualifier() == EvqConst)
2409 {
2410 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2411 if (indexedExpression == 0)
2412 {
2413 recover();
2414 indexedExpression = baseExpression;
2415 }
2416 else
2417 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002418 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002419 // change the qualifier of the return type, not of the structure field
2420 // as the structure definition is shared between various structures.
2421 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2422 }
2423 }
2424 else
2425 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002426 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002427 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002428 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002429 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002430 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002431 }
2432 }
2433 else
2434 {
2435 error(dotLocation, " no such field in structure", fieldString.c_str());
2436 recover();
2437 indexedExpression = baseExpression;
2438 }
2439 }
2440 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002441 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002442 {
2443 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002444 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2445 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002446 {
2447 error(dotLocation, "interface block has no fields", "Internal Error");
2448 recover();
2449 indexedExpression = baseExpression;
2450 }
2451 else
2452 {
2453 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002454 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002456 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002457 {
2458 fieldFound = true;
2459 break;
2460 }
2461 }
2462 if (fieldFound)
2463 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002464 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002465 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002466 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002467 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002468 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002469 }
2470 else
2471 {
2472 error(dotLocation, " no such field in interface block", fieldString.c_str());
2473 recover();
2474 indexedExpression = baseExpression;
2475 }
2476 }
2477 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002478 else
2479 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002480 if (shaderVersion < 300)
2481 {
2482 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2483 }
2484 else
2485 {
2486 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2487 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002488 recover();
2489 indexedExpression = baseExpression;
2490 }
2491
2492 return indexedExpression;
2493}
2494
Jamie Madill075edd82013-07-08 13:30:19 -04002495TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002496{
Jamie Madilla5efff92013-06-06 11:56:47 -04002497 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002498
Jamie Madilla5efff92013-06-06 11:56:47 -04002499 qualifier.location = -1;
2500 qualifier.matrixPacking = EmpUnspecified;
2501 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002502
2503 if (qualifierType == "shared")
2504 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002505 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002506 }
2507 else if (qualifierType == "packed")
2508 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002509 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002510 }
2511 else if (qualifierType == "std140")
2512 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002513 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002514 }
2515 else if (qualifierType == "row_major")
2516 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002517 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002518 }
2519 else if (qualifierType == "column_major")
2520 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002521 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002522 }
2523 else if (qualifierType == "location")
2524 {
2525 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2526 recover();
2527 }
2528 else
2529 {
2530 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2531 recover();
2532 }
2533
Jamie Madilla5efff92013-06-06 11:56:47 -04002534 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002535}
2536
Jamie Madill075edd82013-07-08 13:30:19 -04002537TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002538{
Jamie Madilla5efff92013-06-06 11:56:47 -04002539 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002540
Jamie Madilla5efff92013-06-06 11:56:47 -04002541 qualifier.location = -1;
2542 qualifier.matrixPacking = EmpUnspecified;
2543 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002544
2545 if (qualifierType != "location")
2546 {
2547 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2548 recover();
2549 }
2550 else
2551 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002552 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002553 if (intValue < 0)
2554 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002555 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002556 recover();
2557 }
2558 else
2559 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002560 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002561 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002562 }
2563
Jamie Madilla5efff92013-06-06 11:56:47 -04002564 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002565}
2566
Jamie Madilla5efff92013-06-06 11:56:47 -04002567TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002568{
Jamie Madilla5efff92013-06-06 11:56:47 -04002569 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002570
Jamie Madilla5efff92013-06-06 11:56:47 -04002571 if (rightQualifier.location != -1)
2572 {
2573 joinedQualifier.location = rightQualifier.location;
2574 }
2575 if (rightQualifier.matrixPacking != EmpUnspecified)
2576 {
2577 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2578 }
2579 if (rightQualifier.blockStorage != EbsUnspecified)
2580 {
2581 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2582 }
2583
2584 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002585}
2586
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002587TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2588 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2589{
2590 TQualifier mergedQualifier = EvqSmoothIn;
2591
Jamie Madill19571812013-08-12 15:26:34 -07002592 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002593 if (interpolationQualifier == EvqSmooth)
2594 mergedQualifier = EvqSmoothIn;
2595 else if (interpolationQualifier == EvqFlat)
2596 mergedQualifier = EvqFlatIn;
2597 else UNREACHABLE();
2598 }
2599 else if (storageQualifier == EvqCentroidIn) {
2600 if (interpolationQualifier == EvqSmooth)
2601 mergedQualifier = EvqCentroidIn;
2602 else if (interpolationQualifier == EvqFlat)
2603 mergedQualifier = EvqFlatIn;
2604 else UNREACHABLE();
2605 }
Jamie Madill19571812013-08-12 15:26:34 -07002606 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002607 if (interpolationQualifier == EvqSmooth)
2608 mergedQualifier = EvqSmoothOut;
2609 else if (interpolationQualifier == EvqFlat)
2610 mergedQualifier = EvqFlatOut;
2611 else UNREACHABLE();
2612 }
2613 else if (storageQualifier == EvqCentroidOut) {
2614 if (interpolationQualifier == EvqSmooth)
2615 mergedQualifier = EvqCentroidOut;
2616 else if (interpolationQualifier == EvqFlat)
2617 mergedQualifier = EvqFlatOut;
2618 else UNREACHABLE();
2619 }
2620 else {
2621 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2622 recover();
2623
2624 mergedQualifier = storageQualifier;
2625 }
2626
2627 TPublicType type;
2628 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2629 return type;
2630}
2631
Jamie Madill98493dd2013-07-08 14:39:03 -04002632TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002633{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002634 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2635 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002636 recover();
2637 }
2638
Jamie Madill98493dd2013-07-08 14:39:03 -04002639 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002640 //
2641 // Careful not to replace already known aspects of type, like array-ness
2642 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002643 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002644 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002645 type->setPrimarySize(typeSpecifier.primarySize);
2646 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002647 type->setPrecision(typeSpecifier.precision);
2648 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002649 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002650
2651 // don't allow arrays of arrays
2652 if (type->isArray()) {
2653 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2654 recover();
2655 }
2656 if (typeSpecifier.array)
2657 type->setArraySize(typeSpecifier.arraySize);
2658 if (typeSpecifier.userDef) {
2659 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002660 }
2661
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002663 recover();
2664 }
2665 }
2666
Jamie Madill98493dd2013-07-08 14:39:03 -04002667 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002668}
2669
Jamie Madill98493dd2013-07-08 14:39:03 -04002670TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002671{
Jamie Madill98493dd2013-07-08 14:39:03 -04002672 TStructure* structure = new TStructure(structName, fieldList);
2673 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002674
Jamie Madill9b820842015-02-12 10:40:10 -05002675 // Store a bool in the struct if we're at global scope, to allow us to
2676 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002677 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002678 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002679
Jamie Madill98493dd2013-07-08 14:39:03 -04002680 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002681 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002682 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002683 {
2684 recover();
2685 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002686 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002687 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002688 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689 recover();
2690 }
2691 }
2692
2693 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002695 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002696 const TField &field = *(*fieldList)[typeListIndex];
2697 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002698 switch (qualifier)
2699 {
2700 case EvqGlobal:
2701 case EvqTemporary:
2702 break;
2703 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002704 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002705 recover();
2706 break;
2707 }
2708 }
2709
2710 TPublicType publicType;
2711 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002712 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002713 exitStructDeclaration();
2714
2715 return publicType;
2716}
2717
Olli Etuahoa3a36662015-02-17 13:46:51 +02002718TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2719{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002720 TBasicType switchType = init->getBasicType();
2721 if ((switchType != EbtInt && switchType != EbtUInt) ||
2722 init->isMatrix() ||
2723 init->isArray() ||
2724 init->isVector())
2725 {
2726 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2727 recover();
2728 return nullptr;
2729 }
2730
Olli Etuahoac5274d2015-02-20 10:19:08 +02002731 if (statementList)
2732 {
2733 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2734 {
2735 recover();
2736 return nullptr;
2737 }
2738 }
2739
Olli Etuahoa3a36662015-02-17 13:46:51 +02002740 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2741 if (node == nullptr)
2742 {
2743 error(loc, "erroneous switch statement", "switch");
2744 recover();
2745 return nullptr;
2746 }
2747 return node;
2748}
2749
2750TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2751{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002752 if (mSwitchNestingLevel == 0)
2753 {
2754 error(loc, "case labels need to be inside switch statements", "case");
2755 recover();
2756 return nullptr;
2757 }
2758 if (condition == nullptr)
2759 {
2760 error(loc, "case label must have a condition", "case");
2761 recover();
2762 return nullptr;
2763 }
2764 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2765 condition->isMatrix() ||
2766 condition->isArray() ||
2767 condition->isVector())
2768 {
2769 error(condition->getLine(), "case label must be a scalar integer", "case");
2770 recover();
2771 }
2772 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2773 if (conditionConst == nullptr)
2774 {
2775 error(condition->getLine(), "case label must be constant", "case");
2776 recover();
2777 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002778 TIntermCase *node = intermediate.addCase(condition, loc);
2779 if (node == nullptr)
2780 {
2781 error(loc, "erroneous case statement", "case");
2782 recover();
2783 return nullptr;
2784 }
2785 return node;
2786}
2787
2788TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2789{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002790 if (mSwitchNestingLevel == 0)
2791 {
2792 error(loc, "default labels need to be inside switch statements", "default");
2793 recover();
2794 return nullptr;
2795 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002796 TIntermCase *node = intermediate.addCase(nullptr, loc);
2797 if (node == nullptr)
2798 {
2799 error(loc, "erroneous default statement", "default");
2800 recover();
2801 return nullptr;
2802 }
2803 return node;
2804}
2805
Olli Etuahof6c694b2015-03-26 14:50:53 +02002806TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2807 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002808{
2809 if (child == nullptr)
2810 {
2811 return nullptr;
2812 }
2813
2814 switch (op)
2815 {
2816 case EOpLogicalNot:
2817 if (child->getBasicType() != EbtBool ||
2818 child->isMatrix() ||
2819 child->isArray() ||
2820 child->isVector())
2821 {
2822 return nullptr;
2823 }
2824 break;
2825 case EOpBitwiseNot:
2826 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2827 child->isMatrix() ||
2828 child->isArray())
2829 {
2830 return nullptr;
2831 }
2832 break;
2833 case EOpPostIncrement:
2834 case EOpPreIncrement:
2835 case EOpPostDecrement:
2836 case EOpPreDecrement:
2837 case EOpNegative:
2838 case EOpPositive:
2839 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002840 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002841 child->isArray())
2842 {
2843 return nullptr;
2844 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002845 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002846 default:
2847 break;
2848 }
2849
Olli Etuahof6c694b2015-03-26 14:50:53 +02002850 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002851}
2852
Olli Etuaho09b22472015-02-11 11:47:26 +02002853TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2854{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002855 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002856 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002857 {
2858 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2859 recover();
2860 return child;
2861 }
2862 return node;
2863}
2864
2865TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2866{
2867 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2868 recover();
2869 return addUnaryMath(op, child, loc);
2870}
2871
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002872bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002873 const TSourceLoc &loc)
2874{
2875 if (left->isArray() || right->isArray())
2876 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002877 if (shaderVersion < 300)
2878 {
2879 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2880 return false;
2881 }
2882
2883 if (left->isArray() != right->isArray())
2884 {
2885 error(loc, "array / non-array mismatch", GetOperatorString(op));
2886 return false;
2887 }
2888
2889 switch (op)
2890 {
2891 case EOpEqual:
2892 case EOpNotEqual:
2893 case EOpAssign:
2894 case EOpInitialize:
2895 break;
2896 default:
2897 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2898 return false;
2899 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002900 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002901 if (left->getArraySize() != right->getArraySize())
2902 {
2903 error(loc, "array size mismatch", GetOperatorString(op));
2904 return false;
2905 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002906 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002907
2908 // Check ops which require integer / ivec parameters
2909 bool isBitShift = false;
2910 switch (op)
2911 {
2912 case EOpBitShiftLeft:
2913 case EOpBitShiftRight:
2914 case EOpBitShiftLeftAssign:
2915 case EOpBitShiftRightAssign:
2916 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2917 // check that the basic type is an integer type.
2918 isBitShift = true;
2919 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2920 {
2921 return false;
2922 }
2923 break;
2924 case EOpBitwiseAnd:
2925 case EOpBitwiseXor:
2926 case EOpBitwiseOr:
2927 case EOpBitwiseAndAssign:
2928 case EOpBitwiseXorAssign:
2929 case EOpBitwiseOrAssign:
2930 // It is enough to check the type of only one operand, since later it
2931 // is checked that the operand types match.
2932 if (!IsInteger(left->getBasicType()))
2933 {
2934 return false;
2935 }
2936 break;
2937 default:
2938 break;
2939 }
2940
2941 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2942 // So the basic type should usually match.
2943 if (!isBitShift && left->getBasicType() != right->getBasicType())
2944 {
2945 return false;
2946 }
2947
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002948 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002949 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002950 switch(op)
2951 {
2952 case EOpAssign:
2953 case EOpInitialize:
2954 case EOpEqual:
2955 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002956 // ESSL 1.00 sections 5.7, 5.8, 5.9
2957 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2958 {
2959 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2960 return false;
2961 }
Olli Etuahoff699002015-03-23 14:38:42 +02002962 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2963 // we interpret the spec so that this extends to structs containing samplers,
2964 // similarly to ESSL 1.00 spec.
2965 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2966 left->getType().isStructureContainingSamplers())
2967 {
2968 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2969 return false;
2970 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002971 case EOpLessThan:
2972 case EOpGreaterThan:
2973 case EOpLessThanEqual:
2974 case EOpGreaterThanEqual:
2975 if ((left->getNominalSize() != right->getNominalSize()) ||
2976 (left->getSecondarySize() != right->getSecondarySize()))
2977 {
2978 return false;
2979 }
2980 default:
2981 break;
2982 }
2983
Olli Etuahod6b14282015-03-17 14:31:35 +02002984 return true;
2985}
2986
Olli Etuahofc1806e2015-03-17 13:03:11 +02002987TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
2988 const TSourceLoc &loc)
2989{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002990 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02002991 return nullptr;
2992
Olli Etuahofc1806e2015-03-17 13:03:11 +02002993 switch (op)
2994 {
2995 case EOpEqual:
2996 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02002997 break;
2998 case EOpLessThan:
2999 case EOpGreaterThan:
3000 case EOpLessThanEqual:
3001 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003002 ASSERT(!left->isArray() && !right->isArray());
3003 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003004 left->getBasicType() == EbtStruct)
3005 {
3006 return nullptr;
3007 }
3008 break;
3009 case EOpLogicalOr:
3010 case EOpLogicalXor:
3011 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003012 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003013 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003014 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003015 {
3016 return nullptr;
3017 }
3018 break;
3019 case EOpAdd:
3020 case EOpSub:
3021 case EOpDiv:
3022 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003023 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003024 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3025 {
3026 return nullptr;
3027 }
3028 break;
3029 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003030 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003031 // Note that this is only for the % operator, not for mod()
3032 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3033 {
3034 return nullptr;
3035 }
3036 break;
3037 // Note that for bitwise ops, type checking is done in promote() to
3038 // share code between ops and compound assignment
3039 default:
3040 break;
3041 }
3042
Olli Etuahofc1806e2015-03-17 13:03:11 +02003043 return intermediate.addBinaryMath(op, left, right, loc);
3044}
3045
Olli Etuaho09b22472015-02-11 11:47:26 +02003046TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3047 const TSourceLoc &loc)
3048{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003049 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003050 if (node == 0)
3051 {
3052 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3053 recover();
3054 return left;
3055 }
3056 return node;
3057}
3058
3059TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3060 const TSourceLoc &loc)
3061{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003062 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003063 if (node == 0)
3064 {
3065 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3066 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003067 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003068 unionArray->setBConst(false);
3069 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3070 }
3071 return node;
3072}
3073
Olli Etuahod6b14282015-03-17 14:31:35 +02003074TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3075 const TSourceLoc &loc)
3076{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003077 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003078 {
3079 return intermediate.addAssign(op, left, right, loc);
3080 }
3081 return nullptr;
3082}
3083
3084TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3085 const TSourceLoc &loc)
3086{
3087 TIntermTyped *node = createAssign(op, left, right, loc);
3088 if (node == nullptr)
3089 {
3090 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3091 recover();
3092 return left;
3093 }
3094 return node;
3095}
3096
Olli Etuaho49300862015-02-20 14:54:49 +02003097TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3098{
3099 switch (op)
3100 {
3101 case EOpContinue:
3102 if (mLoopNestingLevel <= 0)
3103 {
3104 error(loc, "continue statement only allowed in loops", "");
3105 recover();
3106 }
3107 break;
3108 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003109 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003110 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003111 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003112 recover();
3113 }
3114 break;
3115 case EOpReturn:
3116 if (currentFunctionType->getBasicType() != EbtVoid)
3117 {
3118 error(loc, "non-void function must return a value", "return");
3119 recover();
3120 }
3121 break;
3122 default:
3123 // No checks for discard
3124 break;
3125 }
3126 return intermediate.addBranch(op, loc);
3127}
3128
3129TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3130{
3131 ASSERT(op == EOpReturn);
3132 mFunctionReturnsValue = true;
3133 if (currentFunctionType->getBasicType() == EbtVoid)
3134 {
3135 error(loc, "void function cannot return a value", "return");
3136 recover();
3137 }
3138 else if (*currentFunctionType != returnValue->getType())
3139 {
3140 error(loc, "function return is not matching type:", "return");
3141 recover();
3142 }
3143 return intermediate.addBranch(op, returnValue, loc);
3144}
3145
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003146TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3147 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003148{
3149 *fatalError = false;
3150 TOperator op = fnCall->getBuiltInOp();
3151 TIntermTyped *callNode = nullptr;
3152
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003153 if (thisNode != nullptr)
3154 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003155 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003156 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003157 TIntermTyped *typedThis = thisNode->getAsTyped();
3158 if (fnCall->getName() != "length")
3159 {
3160 error(loc, "invalid method", fnCall->getName().c_str());
3161 recover();
3162 }
3163 else if (paramNode != nullptr)
3164 {
3165 error(loc, "method takes no parameters", "length");
3166 recover();
3167 }
3168 else if (typedThis == nullptr || !typedThis->isArray())
3169 {
3170 error(loc, "length can only be called on arrays", "length");
3171 recover();
3172 }
3173 else
3174 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003175 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003176 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003177 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003178 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003179 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003180 // (func()).length()
3181 // (int[3](0, 1, 2)).length()
3182 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3183 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3184 // which allows "An array, vector or matrix expression with the length method applied".
3185 error(loc, "length can only be called on array names, not on array expressions", "length");
3186 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003187 }
3188 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003189 unionArray->setIConst(arraySize);
3190 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003191 }
3192 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003193 {
3194 //
3195 // Then this should be a constructor.
3196 // Don't go through the symbol table for constructors.
3197 // Their parameters will be verified algorithmically.
3198 //
3199 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003200 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003201 {
3202 //
3203 // It's a constructor, of type 'type'.
3204 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003205 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003206 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003207
3208 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003209 {
3210 recover();
3211 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3212 }
3213 callNode->setType(type);
3214 }
3215 else
3216 {
3217 //
3218 // Not a constructor. Find it in the symbol table.
3219 //
3220 const TFunction* fnCandidate;
3221 bool builtIn;
3222 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3223 if (fnCandidate)
3224 {
3225 //
3226 // A declared function.
3227 //
3228 if (builtIn && !fnCandidate->getExtension().empty() &&
3229 extensionErrorCheck(loc, fnCandidate->getExtension()))
3230 {
3231 recover();
3232 }
3233 op = fnCandidate->getBuiltInOp();
3234 if (builtIn && op != EOpNull)
3235 {
3236 //
3237 // A function call mapped to a built-in operation.
3238 //
3239 if (fnCandidate->getParamCount() == 1)
3240 {
3241 //
3242 // Treat it like a built-in unary operator.
3243 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003244 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003245 if (callNode == nullptr)
3246 {
3247 std::stringstream extraInfoStream;
3248 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003249 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003250 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003251 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003252 *fatalError = true;
3253 return nullptr;
3254 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003255 }
3256 else
3257 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003258 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003259 aggregate->setType(fnCandidate->getReturnType());
3260 aggregate->setPrecisionFromChildren();
3261 callNode = aggregate;
3262
3263 // Some built-in functions have out parameters too.
3264 functionCallLValueErrorCheck(fnCandidate, aggregate);
3265 }
3266 }
3267 else
3268 {
3269 // This is a real function call
3270
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003271 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003272 aggregate->setType(fnCandidate->getReturnType());
3273
3274 // this is how we know whether the given function is a builtIn function or a user defined function
3275 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3276 // if builtIn == true, it's definitely a builtIn function with EOpNull
3277 if (!builtIn)
3278 aggregate->setUserDefined();
3279 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003280 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003281
3282 // This needs to happen after the name is set
3283 if (builtIn)
3284 aggregate->setBuiltInFunctionPrecision();
3285
3286 callNode = aggregate;
3287
3288 functionCallLValueErrorCheck(fnCandidate, aggregate);
3289 }
3290 }
3291 else
3292 {
3293 // error message was put out by findFunction()
3294 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003295 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003296 unionArray->setFConst(0.0f);
3297 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3298 recover();
3299 }
3300 }
3301 delete fnCall;
3302 return callNode;
3303}
3304
Olli Etuaho52901742015-04-15 13:42:45 +03003305TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3306 const TSourceLoc &loc)
3307{
3308 if (boolErrorCheck(loc, cond))
3309 recover();
3310
3311 if (trueBlock->getType() != falseBlock->getType())
3312 {
3313 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3314 recover();
3315 return falseBlock;
3316 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003317 // ESSL1 sections 5.2 and 5.7:
3318 // ESSL3 section 5.7:
3319 // Ternary operator is not among the operators allowed for structures/arrays.
3320 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3321 {
3322 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3323 recover();
3324 return falseBlock;
3325 }
Olli Etuaho52901742015-04-15 13:42:45 +03003326 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3327}
Olli Etuaho49300862015-02-20 14:54:49 +02003328
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003329//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003330// Parse an array of strings using yyparse.
3331//
3332// Returns 0 for success.
3333//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003334int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003335 TParseContext* context) {
3336 if ((count == 0) || (string == NULL))
3337 return 1;
3338
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003339 if (glslang_initialize(context))
3340 return 1;
3341
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003342 int error = glslang_scan(count, string, length, context);
3343 if (!error)
3344 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003345
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003346 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003347
alokp@chromium.org6b495712012-06-29 00:06:58 +00003348 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003349}
3350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003351
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003352