blob: 784032d0a241c9b1f212231bb243ab20445c0a2b [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"
Olli Etuahob0c645e2015-05-12 14:25:36 +030015#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030016#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017
alokp@chromium.org8b851c62012-06-15 16:25:11 +000018///////////////////////////////////////////////////////////////////////
19//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020// Sub- vector and matrix fields
21//
22////////////////////////////////////////////////////////////////////////
23
24//
25// Look at a '.' field selector string and change it into offsets
26// for a vector.
27//
Jamie Madill075edd82013-07-08 13:30:19 -040028bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 fields.num = (int) compString.size();
31 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000032 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 return false;
34 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000036 enum {
37 exyzw,
38 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000039 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000040 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000042 for (int i = 0; i < fields.num; ++i) {
43 switch (compString[i]) {
44 case 'x':
45 fields.offsets[i] = 0;
46 fieldSet[i] = exyzw;
47 break;
48 case 'r':
49 fields.offsets[i] = 0;
50 fieldSet[i] = ergba;
51 break;
52 case 's':
53 fields.offsets[i] = 0;
54 fieldSet[i] = estpq;
55 break;
56 case 'y':
57 fields.offsets[i] = 1;
58 fieldSet[i] = exyzw;
59 break;
60 case 'g':
61 fields.offsets[i] = 1;
62 fieldSet[i] = ergba;
63 break;
64 case 't':
65 fields.offsets[i] = 1;
66 fieldSet[i] = estpq;
67 break;
68 case 'z':
69 fields.offsets[i] = 2;
70 fieldSet[i] = exyzw;
71 break;
72 case 'b':
73 fields.offsets[i] = 2;
74 fieldSet[i] = ergba;
75 break;
76 case 'p':
77 fields.offsets[i] = 2;
78 fieldSet[i] = estpq;
79 break;
80
81 case 'w':
82 fields.offsets[i] = 3;
83 fieldSet[i] = exyzw;
84 break;
85 case 'a':
86 fields.offsets[i] = 3;
87 fieldSet[i] = ergba;
88 break;
89 case 'q':
90 fields.offsets[i] = 3;
91 fieldSet[i] = estpq;
92 break;
93 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000094 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000095 return false;
96 }
97 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 for (int i = 0; i < fields.num; ++i) {
100 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000101 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 return false;
103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 if (i > 0) {
106 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000107 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000108 return false;
109 }
110 }
111 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000113 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114}
115
116
117//
118// Look at a '.' field selector string and change it into offsets
119// for a matrix.
120//
Jamie Madill075edd82013-07-08 13:30:19 -0400121bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000123 fields.wholeRow = false;
124 fields.wholeCol = false;
125 fields.row = -1;
126 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000129 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 return false;
131 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 if (compString[0] == '_') {
134 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000135 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 return false;
137 }
138 fields.wholeCol = true;
139 fields.col = compString[1] - '0';
140 } else if (compString[1] == '_') {
141 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000142 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000143 return false;
144 }
145 fields.wholeRow = true;
146 fields.row = compString[0] - '0';
147 } else {
148 if (compString[0] < '0' || compString[0] > '3' ||
149 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000150 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000151 return false;
152 }
153 fields.row = compString[0] - '0';
154 fields.col = compString[1] - '0';
155 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000157 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000158 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return false;
160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000162 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163}
164
165///////////////////////////////////////////////////////////////////////
166//
167// Errors
168//
169////////////////////////////////////////////////////////////////////////
170
171//
172// Track whether errors have occurred.
173//
174void TParseContext::recover()
175{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176}
177
178//
179// Used by flex/bison to output all syntax and parsing errors.
180//
Jamie Madill075edd82013-07-08 13:30:19 -0400181void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000182 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000183 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000185 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400186 srcLoc.file = loc.first_file;
187 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400188 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
189 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000190
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191}
192
Jamie Madill075edd82013-07-08 13:30:19 -0400193void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000194 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000195 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000196 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400197 srcLoc.file = loc.first_file;
198 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400199 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
200 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000201}
202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203//
204// Same error message for all places assignments don't work.
205//
Jamie Madill075edd82013-07-08 13:30:19 -0400206void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000208 std::stringstream extraInfoStream;
209 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
210 std::string extraInfo = extraInfoStream.str();
211 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212}
213
214//
215// Same error message for all places unary operations don't work.
216//
Jamie Madill075edd82013-07-08 13:30:19 -0400217void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::stringstream extraInfoStream;
220 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
221 << " (or there is no acceptable conversion)";
222 std::string extraInfo = extraInfoStream.str();
223 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224}
225
226//
227// Same error message for all binary operations don't work.
228//
Jamie Madill075edd82013-07-08 13:30:19 -0400229void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000231 std::stringstream extraInfoStream;
232 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
233 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
234 std::string extraInfo = extraInfoStream.str();
235 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
Jamie Madill075edd82013-07-08 13:30:19 -0400238bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400239 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000240 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 switch( type ){
242 case EbtFloat:
243 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000244 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000245 return true;
246 }
247 break;
248 case EbtInt:
249 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000250 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000251 return true;
252 }
253 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000254 default:
255 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000256 }
257 return false;
258}
259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260//
261// Both test and if necessary, spit out an error, to see if the node is really
262// an l-value that can be operated on this way.
263//
264// Returns true if the was an error.
265//
Jamie Madill075edd82013-07-08 13:30:19 -0400266bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000268 TIntermSymbol* symNode = node->getAsSymbolNode();
269 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000271 if (binaryNode) {
272 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000274 switch(binaryNode->getOp()) {
275 case EOpIndexDirect:
276 case EOpIndexIndirect:
277 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000278 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000279 return lValueErrorCheck(line, op, binaryNode->getLeft());
280 case EOpVectorSwizzle:
281 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
282 if (!errorReturn) {
283 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000285 TIntermTyped* rightNode = binaryNode->getRight();
286 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700287
288 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
289 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000290 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700291 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000292 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000293 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 return true;
296 }
297 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700298 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700301 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 break;
303 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000304 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 return true;
307 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
309
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000310 const char* symbol = 0;
311 if (symNode != 0)
312 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 const char* message = 0;
315 switch (node->getQualifier()) {
316 case EvqConst: message = "can't modify a const"; break;
317 case EvqConstReadOnly: message = "can't modify a const"; break;
318 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700319 case EvqFragmentIn: message = "can't modify an input"; break;
320 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000321 case EvqUniform: message = "can't modify a uniform"; break;
322 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
324 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
325 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
326 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 //
329 // Type that can't be written to?
330 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400331 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000332 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400333 }
334 if (IsSampler(node->getBasicType())) {
335 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000336 }
337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000339 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000340 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 return true;
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
345
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000346 //
347 // Everything else is okay, no error.
348 //
349 if (message == 0)
350 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000352 //
353 // If we get here, we have an error and a message.
354 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000355 if (symNode) {
356 std::stringstream extraInfoStream;
357 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
358 std::string extraInfo = extraInfoStream.str();
359 error(line, " l-value required", op, extraInfo.c_str());
360 }
361 else {
362 std::stringstream extraInfoStream;
363 extraInfoStream << "(" << message << ")";
364 std::string extraInfo = extraInfoStream.str();
365 error(line, " l-value required", op, extraInfo.c_str());
366 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
371//
372// Both test, and if necessary spit out an error, to see if the node is really
373// a constant.
374//
375// Returns true if the was an error.
376//
377bool TParseContext::constErrorCheck(TIntermTyped* node)
378{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 if (node->getQualifier() == EvqConst)
380 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000382 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385}
386
387//
388// Both test, and if necessary spit out an error, to see if the node is really
389// an integer.
390//
391// Returns true if the was an error.
392//
393bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
394{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000395 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000396 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000398 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000400 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401}
402
403//
404// Both test, and if necessary spit out an error, to see if we are currently
405// globally scoped.
406//
407// Returns true if the was an error.
408//
Jamie Madill075edd82013-07-08 13:30:19 -0400409bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000411 if (global)
412 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000414 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417}
418
419//
420// For now, keep it simple: if it starts "gl_", it's reserved, independent
421// of scope. Except, if the symbol table is at the built-in push-level,
422// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000423// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
424// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425//
426// Returns true if there was an error.
427//
Jamie Madill075edd82013-07-08 13:30:19 -0400428bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000430 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000432 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 return true;
435 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400436 if (IsWebGLBasedSpec(mShaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000437 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000438 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000439 return true;
440 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000441 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000442 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443 return true;
444 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400445 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000446 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000447 return true;
448 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000449 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000451 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000452 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 }
454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457}
458
459//
460// Make sure there is enough data provided to the constructor to build
461// something of the type of the constructor. Also returns the type of
462// the constructor.
463//
464// Returns true if there was an error in construction.
465//
Jamie Madill075edd82013-07-08 13:30:19 -0400466bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000468 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 bool constructingMatrix = false;
471 switch(op) {
472 case EOpConstructMat2:
473 case EOpConstructMat3:
474 case EOpConstructMat4:
475 constructingMatrix = true;
476 break;
477 default:
478 break;
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000481 //
482 // Note: It's okay to have too many components available, but not okay to have unused
483 // arguments. 'full' will go to true when enough args have been seen. If we loop
484 // again, there is an extra argument, so 'overfull' will become true.
485 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
Jamie Madill94bf7f22013-07-08 13:31:15 -0400487 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000488 bool constType = true;
489 bool full = false;
490 bool overFull = false;
491 bool matrixInMatrix = false;
492 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000493 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000494 const TParameter& param = function.getParam(i);
495 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000497 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498 matrixInMatrix = true;
499 if (full)
500 overFull = true;
501 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
502 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000503 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000505 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 arrayArg = true;
507 }
508
509 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000510 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
Olli Etuaho376f1b52015-04-13 13:23:41 +0300512 if (type->isArray())
513 {
514 if (type->isUnsizedArray())
515 {
516 type->setArraySize(function.getParamCount());
517 }
518 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
519 {
520 error(line, "array constructor needs one argument per array element", "constructor");
521 return true;
522 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000526 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 return true;
528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000531 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000532 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000533 return true;
534 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000536
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000538 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 return true;
540 }
541
Brendan Longeaa84062013-12-08 18:26:50 +0100542 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000543 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000544 return true;
545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000547 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000548 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
549 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000550 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000551 return true;
552 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000555 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000557 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000558 return true;
559 }
560 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000561 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 return true;
563 }
564 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000565 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 return true;
567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570}
571
572// This function checks to see if a void variable has been declared and raise an error message for such a case
573//
574// returns true in case of an error
575//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300576bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300578 if (type == EbtVoid)
579 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000580 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300582 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585}
586
587// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
588//
589// returns true in case of an error
590//
Jamie Madill075edd82013-07-08 13:30:19 -0400591bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000594 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 return true;
596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599}
600
601// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
602//
603// returns true in case of an error
604//
Jamie Madill075edd82013-07-08 13:30:19 -0400605bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000607 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000608 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 return true;
610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000613}
614
Jamie Madill075edd82013-07-08 13:30:19 -0400615bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000617 if (pType.type == EbtStruct) {
618 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000619 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000620
621 return true;
622 }
623
624 return false;
625 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000626 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 return true;
629 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632}
633
Jamie Madill075edd82013-07-08 13:30:19 -0400634bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400635{
636 if (pType.layoutQualifier.location != -1)
637 {
638 error(line, "location must only be specified for a single input or output variable", "location");
639 return true;
640 }
641
642 return false;
643}
644
Jamie Madill075edd82013-07-08 13:30:19 -0400645bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000647 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
648 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000649 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 return true;
651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000653 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
Jamie Madill06145232015-05-13 13:10:01 -0400656bool TParseContext::containsSampler(const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000658 if (IsSampler(type.getBasicType()))
659 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660
Jamie Madill98493dd2013-07-08 14:39:03 -0400661 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
662 const TFieldList& fields = type.getStruct()->fields();
663 for (unsigned int i = 0; i < fields.size(); ++i) {
664 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return true;
666 }
667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000669 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670}
671
672//
673// Do size checking for an array type's size.
674//
675// Returns true if there was an error.
676//
Jamie Madill075edd82013-07-08 13:30:19 -0400677bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000680
Olli Etuahoe7847b02015-03-16 11:56:12 +0200681 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000682 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000683 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200684 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 return true;
686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687
Nicolas Capens906744a2014-06-06 15:18:07 -0400688 unsigned int unsignedSize = 0;
689
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000690 if (constant->getBasicType() == EbtUInt)
691 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400692 unsignedSize = constant->getUConst(0);
693 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000694 }
695 else
696 {
697 size = constant->getIConst(0);
698
Nicolas Capens906744a2014-06-06 15:18:07 -0400699 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000700 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400701 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000702 size = 1;
703 return true;
704 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400705
706 unsignedSize = static_cast<unsigned int>(size);
707 }
708
709 if (size == 0)
710 {
711 error(line, "array size must be greater than zero", "");
712 size = 1;
713 return true;
714 }
715
716 // The size of arrays is restricted here to prevent issues further down the
717 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
718 // 4096 registers so this should be reasonable even for aggressively optimizable code.
719 const unsigned int sizeLimit = 65536;
720
721 if (unsignedSize > sizeLimit)
722 {
723 error(line, "array size too large", "");
724 size = 1;
725 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729}
730
731//
732// See if this qualifier can be an array.
733//
734// Returns true if there is an error.
735//
Olli Etuaho3739d232015-04-08 12:23:44 +0300736bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737{
Olli Etuaho3739d232015-04-08 12:23:44 +0300738 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400739 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300740 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000741 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 return true;
743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746}
747
748//
749// See if this type can be an array.
750//
751// Returns true if there is an error.
752//
Jamie Madill06145232015-05-13 13:10:01 -0400753bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 //
756 // Can the type be an array?
757 //
Jamie Madill06145232015-05-13 13:10:01 -0400758 if (type.array)
759 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000760 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return true;
762 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000764 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765}
766
767//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768// Enforce non-initializer type/qualifier rules.
769//
770// Returns true if there was an error.
771//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300772bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773{
Olli Etuaho3739d232015-04-08 12:23:44 +0300774 ASSERT(type != nullptr);
775 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000776 {
777 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300778 type->qualifier = EvqTemporary;
779
780 // Generate informative error messages for ESSL1.
781 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400782 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000783 {
784 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
785 }
786 else
787 {
788 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
789 }
790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 return true;
792 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300793 if (type->isUnsizedArray())
794 {
795 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
796 return true;
797 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799}
800
Olli Etuaho2935c582015-04-08 14:32:06 +0300801// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802// and update the symbol table.
803//
Olli Etuaho2935c582015-04-08 14:32:06 +0300804// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805//
Olli Etuaho2935c582015-04-08 14:32:06 +0300806bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
807 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808{
Olli Etuaho2935c582015-04-08 14:32:06 +0300809 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
Olli Etuaho2935c582015-04-08 14:32:06 +0300811 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
Olli Etuaho2935c582015-04-08 14:32:06 +0300813 // gl_LastFragData may be redeclared with a new precision qualifier
814 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
815 {
816 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400817 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300818 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
819 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400820 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300821 {
822 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
823 }
824 }
825 else
826 {
827 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
828 return false;
829 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
Olli Etuaho2935c582015-04-08 14:32:06 +0300832 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
833 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
Olli Etuaho2935c582015-04-08 14:32:06 +0300835 (*variable) = new TVariable(&identifier, type);
836 if (!symbolTable.declare(*variable))
837 {
838 error(line, "redefinition", identifier.c_str());
839 delete (*variable);
840 (*variable) = nullptr;
841 return false;
842 }
843
844 if (voidErrorCheck(line, identifier, type.getBasicType()))
845 return false;
846
847 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
Jamie Madill075edd82013-07-08 13:30:19 -0400850bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000853 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 return true;
855 }
856 if (qualifier == EvqConst && paramQualifier != EvqIn) {
857 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
858 return true;
859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000862 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000864 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000866 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867}
868
Jamie Madill075edd82013-07-08 13:30:19 -0400869bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000870{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000871 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000872 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
873 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000874 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 return true;
876 }
zmo@google.comf5450912011-09-09 01:37:19 +0000877 // In GLSL ES, an extension's default behavior is "disable".
878 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000879 error(line, "extension", extension.c_str(), "is disabled");
880 return true;
881 }
882 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000883 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000884 return false;
885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000887 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
Olli Etuahofa33d582015-04-09 14:33:12 +0300890// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
891// declaration.
892//
Jamie Madill06145232015-05-13 13:10:01 -0400893bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400894{
Olli Etuahofa33d582015-04-09 14:33:12 +0300895 switch (publicType.qualifier)
896 {
897 case EvqVaryingIn:
898 case EvqVaryingOut:
899 case EvqAttribute:
900 case EvqVertexIn:
901 case EvqFragmentOut:
902 if (publicType.type == EbtStruct)
903 {
904 error(identifierLocation, "cannot be used with a structure",
905 getQualifierString(publicType.qualifier));
906 return true;
907 }
908
909 default: break;
910 }
911
912 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
913 "samplers must be uniform"))
914 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400915 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300916 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400917
918 // check for layout qualifier issues
919 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
920
921 if (layoutQualifier.matrixPacking != EmpUnspecified)
922 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300923 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
924 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400925 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400926 }
927
928 if (layoutQualifier.blockStorage != EbsUnspecified)
929 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300930 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
931 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400932 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400933 }
934
Olli Etuahofa33d582015-04-09 14:33:12 +0300935 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
936 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400937 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400938 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400939 }
940
941 return false;
942}
943
Jamie Madill075edd82013-07-08 13:30:19 -0400944bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400945{
946 if (layoutQualifier.location != -1)
947 {
948 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
949 return true;
950 }
951
952 return false;
953}
954
Olli Etuahob6e07a62015-02-16 12:22:10 +0200955bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
956{
957 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
958 {
959 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
960 if (qual == EvqOut || qual == EvqInOut)
961 {
962 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
963 if (lValueErrorCheck(node->getLine(), "assign", node))
964 {
965 error(node->getLine(),
966 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
967 recover();
968 return true;
969 }
970 }
971 }
972 return false;
973}
974
Olli Etuaho37ad4742015-04-27 13:18:50 +0300975void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
976{
977 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
978 {
979 error(invariantLocation, "Only out variables can be invariant.", "invariant");
980 recover();
981 }
982}
983
zmo@google.com09c323a2011-08-12 18:22:25 +0000984bool TParseContext::supportsExtension(const char* extension)
985{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000986 const TExtensionBehavior& extbehavior = extensionBehavior();
987 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
988 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000989}
990
Jamie Madill5d287f52013-07-12 15:38:19 -0400991bool TParseContext::isExtensionEnabled(const char* extension) const
992{
993 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400994 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400995
996 if (iter == extbehavior.end())
997 {
998 return false;
999 }
1000
1001 return (iter->second == EBhEnable || iter->second == EBhRequire);
1002}
1003
Jamie Madill075edd82013-07-08 13:30:19 -04001004void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1005{
1006 pp::SourceLocation srcLoc;
1007 srcLoc.file = loc.first_file;
1008 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001009 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001010}
1011
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001012void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001013{
1014 pp::SourceLocation srcLoc;
1015 srcLoc.file = loc.first_file;
1016 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001017 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001018}
1019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020/////////////////////////////////////////////////////////////////////////////////
1021//
1022// Non-Errors.
1023//
1024/////////////////////////////////////////////////////////////////////////////////
1025
Jamie Madill5c097022014-08-20 16:38:32 -04001026const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1027 const TString *name,
1028 const TSymbol *symbol)
1029{
1030 const TVariable *variable = NULL;
1031
1032 if (!symbol)
1033 {
1034 error(location, "undeclared identifier", name->c_str());
1035 recover();
1036 }
1037 else if (!symbol->isVariable())
1038 {
1039 error(location, "variable expected", name->c_str());
1040 recover();
1041 }
1042 else
1043 {
1044 variable = static_cast<const TVariable*>(symbol);
1045
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001046 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001047 !variable->getExtension().empty() &&
1048 extensionErrorCheck(location, variable->getExtension()))
1049 {
1050 recover();
1051 }
1052 }
1053
1054 if (!variable)
1055 {
1056 TType type(EbtFloat, EbpUndefined);
1057 TVariable *fakeVariable = new TVariable(name, type);
1058 symbolTable.declare(fakeVariable);
1059 variable = fakeVariable;
1060 }
1061
1062 return variable;
1063}
1064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065//
1066// Look up a function name in the symbol table, and make sure it is a function.
1067//
1068// Return the function symbol if found, otherwise 0.
1069//
Austin Kinross3ae64652015-01-26 15:51:39 -08001070const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001072 // First find by unmangled name to check whether the function name has been
1073 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001074 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001075 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001076 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001077 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
alokp@chromium.org0a576182010-08-09 17:16:27 +00001080 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001081 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001082 return 0;
1083 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
alokp@chromium.org0a576182010-08-09 17:16:27 +00001085 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001086 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 return 0;
1088 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001089
1090 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091}
1092
1093//
1094// Initializers show up in several places in the grammar. Have one set of
1095// code to handle them here.
1096//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001097// Returns true on error, false if no error
1098//
Jamie Madill06145232015-05-13 13:10:01 -04001099bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001100 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001102 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001106 if (type.isUnsizedArray())
1107 {
1108 type.setArraySize(initializer->getArraySize());
1109 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 if (!declareVariable(line, identifier, type, &variable))
1111 {
1112 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
Olli Etuahob0c645e2015-05-12 14:25:36 +03001115 bool globalInitWarning = false;
1116 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1117 {
1118 // Error message does not completely match behavior with ESSL 1.00, but
1119 // we want to steer developers towards only using constant expressions.
1120 error(line, "global variable initializers must be constant expressions", "=");
1121 return true;
1122 }
1123 if (globalInitWarning)
1124 {
1125 warning(line, "global variable initializers should be constant expressions "
1126 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1127 }
1128
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001129 //
1130 // identifier must be of type constant, a global, or a temporary
1131 //
1132 TQualifier qualifier = variable->getType().getQualifier();
1133 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001134 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001135 return true;
1136 }
1137 //
1138 // test for and propagate constant
1139 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001141 if (qualifier == EvqConst) {
1142 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001143 std::stringstream extraInfoStream;
1144 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1145 std::string extraInfo = extraInfoStream.str();
1146 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001147 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 return true;
1149 }
1150 if (type != initializer->getType()) {
1151 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001152 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001153 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001154 return true;
1155 }
1156 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001157 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001159 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001162 TConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001163 variable->shareConstPointer(constArray);
1164 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001165 std::stringstream extraInfoStream;
1166 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1167 std::string extraInfo = extraInfoStream.str();
1168 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001169 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 return true;
1171 }
1172 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001173
1174 if (qualifier != EvqConst)
1175 {
1176 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1177 variable->getType(), line);
1178 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1179 if (*intermNode == nullptr)
1180 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1182 return true;
1183 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001184 }
1185 else
1186 {
1187 *intermNode = nullptr;
1188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001190 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001191}
1192
1193bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1194{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001195 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001196 if (!aggrNode->isConstructor())
1197 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001200
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001201 // check if all the child nodes are constants so that they can be inserted into
1202 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001203 TIntermSequence *sequence = aggrNode->getSequence() ;
1204 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001205 if (!(*p)->getAsTyped()->getAsConstantUnion())
1206 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001207 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001208
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001209 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210}
1211
Olli Etuaho214c2d82015-04-27 14:49:13 +03001212TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
1213 const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001214{
1215 TPublicType returnType = typeSpecifier;
1216 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001217 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001218 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001219
1220 if (typeSpecifier.array)
1221 {
1222 error(typeSpecifier.line, "not supported", "first-class array");
1223 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001224 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001225 }
1226
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001227 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001228 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001229 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1230 {
1231 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1232 recover();
1233 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001234
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001235 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1236 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1237 {
1238 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1239 recover();
1240 }
1241 }
1242 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001243 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001244 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001245 {
Jamie Madill19571812013-08-12 15:26:34 -07001246 case EvqSmoothIn:
1247 case EvqSmoothOut:
1248 case EvqVertexOut:
1249 case EvqFragmentIn:
1250 case EvqCentroidOut:
1251 case EvqCentroidIn:
1252 if (typeSpecifier.type == EbtBool)
1253 {
1254 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1255 recover();
1256 }
1257 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1258 {
1259 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1260 recover();
1261 }
1262 break;
1263
1264 case EvqVertexIn:
1265 case EvqFragmentOut:
1266 case EvqFlatIn:
1267 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001268 if (typeSpecifier.type == EbtBool)
1269 {
1270 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1271 recover();
1272 }
1273 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001274
Jamie Madillb120eac2013-06-12 14:08:13 -04001275 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001276 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001277 }
1278
1279 return returnType;
1280}
1281
Olli Etuahofa33d582015-04-09 14:33:12 +03001282TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1283 const TSourceLoc &identifierOrTypeLocation,
1284 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001285{
Olli Etuahofa33d582015-04-09 14:33:12 +03001286 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001287
Olli Etuahobab4c082015-04-24 16:38:49 +03001288 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001289
Olli Etuahobab4c082015-04-24 16:38:49 +03001290 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1291
1292 if (emptyDeclaration)
1293 {
1294 if (publicType.isUnsizedArray())
1295 {
1296 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1297 // It is assumed that this applies to empty declarations as well.
1298 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1299 }
1300 }
1301 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001302 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001303 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001304 recover();
1305
Olli Etuaho376f1b52015-04-13 13:23:41 +03001306 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001307 recover();
1308
Olli Etuaho2935c582015-04-08 14:32:06 +03001309 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001310 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001311 recover();
1312
1313 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001314 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001315 }
1316
Olli Etuahoe7847b02015-03-16 11:56:12 +02001317 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001318}
1319
Olli Etuahoe7847b02015-03-16 11:56:12 +02001320TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1321 const TSourceLoc &identifierLocation,
1322 const TString &identifier,
1323 const TSourceLoc &indexLocation,
1324 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001325{
Olli Etuahofa33d582015-04-09 14:33:12 +03001326 mDeferredSingleDeclarationErrorCheck = false;
1327
1328 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001329 recover();
1330
Olli Etuaho376f1b52015-04-13 13:23:41 +03001331 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001332 recover();
1333
1334 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1335 {
1336 recover();
1337 }
1338
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001339 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001340
1341 int size;
1342 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1343 {
1344 recover();
1345 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001346 // Make the type an array even if size check failed.
1347 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1348 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001349
Olli Etuaho2935c582015-04-08 14:32:06 +03001350 TVariable *variable = nullptr;
1351 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001352 recover();
1353
Olli Etuahoe7847b02015-03-16 11:56:12 +02001354 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001355 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001356 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001357
Olli Etuahoe7847b02015-03-16 11:56:12 +02001358 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001359}
1360
Jamie Madill06145232015-05-13 13:10:01 -04001361TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001362 const TSourceLoc &identifierLocation,
1363 const TString &identifier,
1364 const TSourceLoc &initLocation,
1365 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001366{
Olli Etuahofa33d582015-04-09 14:33:12 +03001367 mDeferredSingleDeclarationErrorCheck = false;
1368
1369 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001370 recover();
1371
Olli Etuahoe7847b02015-03-16 11:56:12 +02001372 TIntermNode *intermNode = nullptr;
1373 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001374 {
1375 //
1376 // Build intermediate representation
1377 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001378 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001379 }
1380 else
1381 {
1382 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001383 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001384 }
1385}
1386
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001387TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1388 const TSourceLoc &identifierLocation,
1389 const TString &identifier,
1390 const TSourceLoc &indexLocation,
1391 TIntermTyped *indexExpression,
1392 const TSourceLoc &initLocation,
1393 TIntermTyped *initializer)
1394{
1395 mDeferredSingleDeclarationErrorCheck = false;
1396
1397 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1398 recover();
1399
1400 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1401 {
1402 recover();
1403 }
1404
1405 TPublicType arrayType(publicType);
1406
Olli Etuaho376f1b52015-04-13 13:23:41 +03001407 int size = 0;
1408 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1409 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001410 {
1411 recover();
1412 }
1413 // Make the type an array even if size check failed.
1414 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1415 arrayType.setArraySize(size);
1416
1417 // initNode will correspond to the whole of "type b[n] = initializer".
1418 TIntermNode *initNode = nullptr;
1419 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1420 {
1421 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1422 }
1423 else
1424 {
1425 recover();
1426 return nullptr;
1427 }
1428}
1429
Olli Etuahoe7847b02015-03-16 11:56:12 +02001430TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001431 const TSourceLoc &identifierLoc,
1432 const TString *identifier,
1433 const TSymbol *symbol)
1434{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001435 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001436 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1437 {
1438 recover();
1439 }
1440
1441 if (!symbol)
1442 {
1443 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1444 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001445 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001446 }
1447 else
1448 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001449 const TString kGlFrontFacing("gl_FrontFacing");
1450 if (*identifier == kGlFrontFacing)
1451 {
1452 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1453 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001454 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001455 }
Jamie Madill2c433252014-12-03 12:36:54 -05001456 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001457 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1458 ASSERT(variable);
1459 const TType &type = variable->getType();
1460 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1461 *identifier, type, identifierLoc);
1462
1463 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1464 aggregate->setOp(EOpInvariantDeclaration);
1465 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001466 }
1467}
1468
Olli Etuahoe7847b02015-03-16 11:56:12 +02001469TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1470 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001471{
Olli Etuahofa33d582015-04-09 14:33:12 +03001472 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1473 if (mDeferredSingleDeclarationErrorCheck)
1474 {
1475 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1476 recover();
1477 mDeferredSingleDeclarationErrorCheck = false;
1478 }
1479
Jamie Madill0bd18df2013-06-20 11:55:52 -04001480 if (locationDeclaratorListCheck(identifierLocation, publicType))
1481 recover();
1482
Olli Etuaho376f1b52015-04-13 13:23:41 +03001483 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001484 recover();
1485
Olli Etuaho2935c582015-04-08 14:32:06 +03001486 TVariable *variable = nullptr;
1487 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001488 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001489
1490 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1491 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001492 symbol->setId(variable->getUniqueId());
1493
Olli Etuahoe7847b02015-03-16 11:56:12 +02001494 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001495}
1496
Olli Etuahoe7847b02015-03-16 11:56:12 +02001497TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1498 const TSourceLoc &identifierLocation, const TString &identifier,
1499 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001500{
Olli Etuahofa33d582015-04-09 14:33:12 +03001501 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1502 if (mDeferredSingleDeclarationErrorCheck)
1503 {
1504 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1505 recover();
1506 mDeferredSingleDeclarationErrorCheck = false;
1507 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001508
Jamie Madill0bd18df2013-06-20 11:55:52 -04001509 if (locationDeclaratorListCheck(identifierLocation, publicType))
1510 recover();
1511
Olli Etuaho376f1b52015-04-13 13:23:41 +03001512 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001513 recover();
1514
1515 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1516 {
1517 recover();
1518 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001519 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001520 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001521 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001522 int size;
1523 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001524 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001525 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001526 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001527 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001528
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001529 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001530 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001531 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001532
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001533 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1534 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001535 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001536
1537 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001538 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001539
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001540 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001541}
1542
Jamie Madill06145232015-05-13 13:10:01 -04001543TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001544 const TSourceLoc &identifierLocation, const TString &identifier,
1545 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001546{
Olli Etuahofa33d582015-04-09 14:33:12 +03001547 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1548 if (mDeferredSingleDeclarationErrorCheck)
1549 {
1550 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1551 recover();
1552 mDeferredSingleDeclarationErrorCheck = false;
1553 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001554
Jamie Madill0bd18df2013-06-20 11:55:52 -04001555 if (locationDeclaratorListCheck(identifierLocation, publicType))
1556 recover();
1557
Olli Etuahoe7847b02015-03-16 11:56:12 +02001558 TIntermNode *intermNode = nullptr;
1559 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001560 {
1561 //
1562 // build the intermediate representation
1563 //
1564 if (intermNode)
1565 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001566 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001567 }
1568 else
1569 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001570 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001571 }
1572 }
1573 else
1574 {
1575 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001577 }
1578}
1579
Jamie Madill06145232015-05-13 13:10:01 -04001580TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001581 TIntermAggregate *aggregateDeclaration,
1582 const TSourceLoc& identifierLocation,
1583 const TString &identifier,
1584 const TSourceLoc& indexLocation,
1585 TIntermTyped *indexExpression,
1586 const TSourceLoc &initLocation, TIntermTyped *initializer)
1587{
1588 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1589 if (mDeferredSingleDeclarationErrorCheck)
1590 {
1591 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1592 recover();
1593 mDeferredSingleDeclarationErrorCheck = false;
1594 }
1595
1596 if (locationDeclaratorListCheck(identifierLocation, publicType))
1597 recover();
1598
1599 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1600 {
1601 recover();
1602 }
1603
1604 TPublicType arrayType(publicType);
1605
Olli Etuaho376f1b52015-04-13 13:23:41 +03001606 int size = 0;
1607 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1608 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001609 {
1610 recover();
1611 }
1612 // Make the type an array even if size check failed.
1613 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1614 arrayType.setArraySize(size);
1615
1616 // initNode will correspond to the whole of "b[n] = initializer".
1617 TIntermNode *initNode = nullptr;
1618 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1619 {
1620 if (initNode)
1621 {
1622 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1623 }
1624 else
1625 {
1626 return aggregateDeclaration;
1627 }
1628 }
1629 else
1630 {
1631 recover();
1632 return nullptr;
1633 }
1634}
1635
Jamie Madilla295edf2013-06-06 11:56:48 -04001636void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1637{
1638 if (typeQualifier.qualifier != EvqUniform)
1639 {
1640 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1641 recover();
1642 return;
1643 }
1644
1645 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1646 ASSERT(!layoutQualifier.isEmpty());
1647
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001648 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001649 {
1650 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1651 recover();
1652 return;
1653 }
1654
1655 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1656 {
1657 recover();
1658 return;
1659 }
1660
Jamie Madill099c0f32013-06-20 11:55:52 -04001661 if (layoutQualifier.matrixPacking != EmpUnspecified)
1662 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001663 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001664 }
1665
Geoff Langc6856732014-02-11 09:38:55 -05001666 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001667 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001668 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001669 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001670}
1671
Jamie Madill06145232015-05-13 13:10:01 -04001672TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001673{
Jamie Madill06145232015-05-13 13:10:01 -04001674 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001675 TOperator op = EOpNull;
1676 if (publicType.userDef)
1677 {
1678 op = EOpConstructStruct;
1679 }
1680 else
1681 {
1682 switch (publicType.type)
1683 {
1684 case EbtFloat:
1685 if (publicType.isMatrix())
1686 {
1687 // TODO: non-square matrices
1688 switch(publicType.getCols())
1689 {
Jamie Madill28b97422013-07-08 14:01:38 -04001690 case 2: op = EOpConstructMat2; break;
1691 case 3: op = EOpConstructMat3; break;
1692 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001693 }
1694 }
1695 else
1696 {
1697 switch(publicType.getNominalSize())
1698 {
Jamie Madill28b97422013-07-08 14:01:38 -04001699 case 1: op = EOpConstructFloat; break;
1700 case 2: op = EOpConstructVec2; break;
1701 case 3: op = EOpConstructVec3; break;
1702 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001703 }
1704 }
1705 break;
1706
1707 case EbtInt:
1708 switch(publicType.getNominalSize())
1709 {
Jamie Madill28b97422013-07-08 14:01:38 -04001710 case 1: op = EOpConstructInt; break;
1711 case 2: op = EOpConstructIVec2; break;
1712 case 3: op = EOpConstructIVec3; break;
1713 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001714 }
1715 break;
1716
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001717 case EbtUInt:
1718 switch(publicType.getNominalSize())
1719 {
Jamie Madill28b97422013-07-08 14:01:38 -04001720 case 1: op = EOpConstructUInt; break;
1721 case 2: op = EOpConstructUVec2; break;
1722 case 3: op = EOpConstructUVec3; break;
1723 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001724 }
1725 break;
1726
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001727 case EbtBool:
1728 switch(publicType.getNominalSize())
1729 {
Jamie Madill28b97422013-07-08 14:01:38 -04001730 case 1: op = EOpConstructBool; break;
1731 case 2: op = EOpConstructBVec2; break;
1732 case 3: op = EOpConstructBVec3; break;
1733 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001734 }
1735 break;
1736
1737 default: break;
1738 }
1739
1740 if (op == EOpNull)
1741 {
1742 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1743 recover();
1744 publicType.type = EbtFloat;
1745 op = EOpConstructFloat;
1746 }
1747 }
1748
1749 TString tempString;
1750 TType type(publicType);
1751 return new TFunction(&tempString, type, op);
1752}
1753
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754// This function is used to test for the correctness of the parameters passed to various constructor functions
1755// and also convert them to the right datatype if it is allowed and required.
1756//
1757// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1758//
Olli Etuaho21203702014-11-13 16:16:21 +02001759TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001761 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001763 if (!aggregateArguments)
1764 {
1765 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001766 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001767 }
1768
Olli Etuahof40319e2015-03-10 14:33:00 +02001769 if (type->isArray())
1770 {
1771 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1772 TIntermSequence *args = aggregateArguments->getSequence();
1773 for (size_t i = 0; i < args->size(); i++)
1774 {
1775 const TType &argType = (*args)[i]->getAsTyped()->getType();
1776 // It has already been checked that the argument is not an array.
1777 ASSERT(!argType.isArray());
1778 if (!argType.sameElementType(*type))
1779 {
1780 error(line, "Array constructor argument has an incorrect type", "Error");
1781 recover();
1782 return nullptr;
1783 }
1784 }
1785 }
1786 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001787 {
1788 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001789 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001791 for (size_t i = 0; i < fields.size(); i++)
1792 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001793 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001794 {
1795 error(line, "Structure constructor arguments do not match structure fields", "Error");
1796 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001798 return 0;
1799 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 }
1801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001803 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001804 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1805 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001807 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001808 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810
Olli Etuaho21203702014-11-13 16:16:21 +02001811 // Structs should not be precision qualified, the individual members may be.
1812 // Built-in types on the other hand should be precision qualified.
1813 if (op != EOpConstructStruct)
1814 {
1815 constructor->setPrecisionFromChildren();
1816 type->setPrecision(constructor->getPrecision());
1817 }
1818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820}
1821
1822TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1823{
Olli Etuahof40319e2015-03-10 14:33:00 +02001824 // TODO: Add support for folding array constructors
1825 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 aggrNode->setType(type);
1827 if (canBeFolded) {
1828 bool returnVal = false;
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001829 TConstantUnion* unionArray = new TConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001830 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001831 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001832 }
1833 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001834 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001835 }
1836 if (returnVal)
1837 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843}
1844
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845//
1846// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1847// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1848// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1849// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1850// a constant matrix.
1851//
Jamie Madill075edd82013-07-08 13:30:19 -04001852TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001854 TIntermTyped* typedNode;
1855 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Jamie Madillb11e2482015-05-04 14:21:22 -04001857 const TConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001858 if (tempConstantNode) {
1859 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001861 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 return node;
1863 }
1864 } 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 +00001865 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001866 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001868 return 0;
1869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001871 TConstantUnion* constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001873 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001874 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001875 std::stringstream extraInfoStream;
1876 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1877 std::string extraInfo = extraInfoStream.str();
1878 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001879 recover();
1880 fields.offsets[i] = 0;
1881 }
1882
1883 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001885 }
1886 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1887 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888}
1889
1890//
1891// This function returns the column being accessed from a constant matrix. The values are retrieved from
1892// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1893// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1894// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1895//
Jamie Madill075edd82013-07-08 13:30:19 -04001896TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001898 TIntermTyped* typedNode;
1899 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001901 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001902 std::stringstream extraInfoStream;
1903 extraInfoStream << "matrix field selection out of range '" << index << "'";
1904 std::string extraInfo = extraInfoStream.str();
1905 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001906 recover();
1907 index = 0;
1908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001910 if (tempConstantNode) {
Jamie Madillb11e2482015-05-04 14:21:22 -04001911 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001912 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001913 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1914 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001915 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001916 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001918 return 0;
1919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001921 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922}
1923
1924
1925//
1926// This function returns an element of an array accessed from a constant array. The values are retrieved from
1927// the symbol table and parse-tree is built for the type of the element. The input
1928// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1929// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1930//
Jamie Madill075edd82013-07-08 13:30:19 -04001931TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001933 TIntermTyped* typedNode;
1934 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1935 TType arrayElementType = node->getType();
1936 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001939 std::stringstream extraInfoStream;
1940 extraInfoStream << "array field selection out of range '" << index << "'";
1941 std::string extraInfo = extraInfoStream.str();
1942 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001943 recover();
1944 index = 0;
1945 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001947 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001948 size_t arrayElementSize = arrayElementType.getObjectSize();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001949 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001950 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001951 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001952 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001953 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001955 return 0;
1956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959}
1960
1961
1962//
1963// This function returns the value of a particular field inside a constant structure from the symbol table.
1964// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1965// function and returns the parse-tree with the values of the embedded/nested struct.
1966//
Jamie Madill075edd82013-07-08 13:30:19 -04001967TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968{
Jamie Madill98493dd2013-07-08 14:39:03 -04001969 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001970 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Jamie Madill98493dd2013-07-08 14:39:03 -04001972 for (size_t index = 0; index < fields.size(); ++index) {
1973 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001974 break;
1975 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001976 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 }
1978 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979
Jamie Madill94bf7f22013-07-08 13:31:15 -04001980 TIntermTyped *typedNode;
1981 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001982 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001983 TConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001984
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001985 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1986 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001987 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001988 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001990 return 0;
1991 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001993 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994}
1995
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001996//
1997// Interface/uniform blocks
1998//
Jamie Madill98493dd2013-07-08 14:39:03 -04001999TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
2000 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002001{
2002 if (reservedErrorCheck(nameLine, blockName))
2003 recover();
2004
2005 if (typeQualifier.qualifier != EvqUniform)
2006 {
2007 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2008 recover();
2009 }
2010
Jamie Madill099c0f32013-06-20 11:55:52 -04002011 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2012 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002013 {
2014 recover();
2015 }
2016
Jamie Madill099c0f32013-06-20 11:55:52 -04002017 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2018 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002019 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002020 }
2021
Jamie Madill1566ef72013-06-20 11:55:54 -04002022 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2023 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002024 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002025 }
2026
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002027 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002028 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002029 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2030 recover();
2031 }
2032
Jamie Madill98493dd2013-07-08 14:39:03 -04002033 // check for sampler types and apply layout qualifiers
2034 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2035 TField* field = (*fieldList)[memberIndex];
2036 TType* fieldType = field->type();
2037 if (IsSampler(fieldType->getBasicType())) {
2038 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002039 recover();
2040 }
2041
Jamie Madill98493dd2013-07-08 14:39:03 -04002042 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002043 switch (qualifier)
2044 {
2045 case EvqGlobal:
2046 case EvqUniform:
2047 break;
2048 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002049 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002050 recover();
2051 break;
2052 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002053
2054 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002055 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2056 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002057 {
2058 recover();
2059 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002060
Jamie Madill98493dd2013-07-08 14:39:03 -04002061 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002062 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002064 recover();
2065 }
2066
Jamie Madill98493dd2013-07-08 14:39:03 -04002067 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002068 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002069 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002070 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002071 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002072 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002073 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002074 recover();
2075 }
2076
Jamie Madill98493dd2013-07-08 14:39:03 -04002077 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002078 }
2079
Jamie Madill98493dd2013-07-08 14:39:03 -04002080 // add array index
2081 int arraySize = 0;
2082 if (arrayIndex != NULL)
2083 {
2084 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2085 recover();
2086 }
2087
2088 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2089 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002090
2091 TString symbolName = "";
2092 int symbolId = 0;
2093
Jamie Madill98493dd2013-07-08 14:39:03 -04002094 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002095 {
2096 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002097 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2098 {
2099 TField* field = (*fieldList)[memberIndex];
2100 TType* fieldType = field->type();
2101
2102 // set parent pointer of the field variable
2103 fieldType->setInterfaceBlock(interfaceBlock);
2104
2105 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2106 fieldVariable->setQualifier(typeQualifier.qualifier);
2107
Nicolas Capensadfffe42014-06-17 02:13:36 -04002108 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002109 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002110 recover();
2111 }
2112 }
2113 }
2114 else
2115 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002116 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002117 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002118 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002119
Nicolas Capensadfffe42014-06-17 02:13:36 -04002120 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002121 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002122 recover();
2123 }
2124
2125 symbolId = instanceTypeDef->getUniqueId();
2126 symbolName = instanceTypeDef->getName();
2127 }
2128
Jamie Madill98493dd2013-07-08 14:39:03 -04002129 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002130 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002131
2132 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002133 return aggregate;
2134}
2135
Jamie Madill075edd82013-07-08 13:30:19 -04002136bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002137{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002138 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002139
2140 // Embedded structure definitions are not supported per GLSL ES spec.
2141 // They aren't allowed in GLSL either, but we need to detect this here
2142 // so we don't rely on the GLSL compiler to catch it.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002143 if (mStructNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002144 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002145 return true;
2146 }
2147
2148 return false;
2149}
2150
2151void TParseContext::exitStructDeclaration()
2152{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002153 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002154}
2155
2156namespace {
2157
2158const int kWebGLMaxStructNesting = 4;
2159
2160} // namespace
2161
Jamie Madill98493dd2013-07-08 14:39:03 -04002162bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002163{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002164 if (!IsWebGLBasedSpec(mShaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002165 return false;
2166 }
2167
Jamie Madill98493dd2013-07-08 14:39:03 -04002168 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002169 return false;
2170 }
2171
2172 // We're already inside a structure definition at this point, so add
2173 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002174 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002175 std::stringstream reasonStream;
2176 reasonStream << "Reference of struct type "
2177 << field.type()->getStruct()->name().c_str()
2178 << " exceeds maximum allowed nesting level of "
2179 << kWebGLMaxStructNesting;
2180 std::string reason = reasonStream.str();
2181 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002182 return true;
2183 }
2184
2185 return false;
2186}
2187
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002188//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002189// Parse an array index expression
2190//
Jamie Madill075edd82013-07-08 13:30:19 -04002191TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002192{
2193 TIntermTyped *indexedExpression = NULL;
2194
2195 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2196 {
2197 if (baseExpression->getAsSymbolNode())
2198 {
2199 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2200 }
2201 else
2202 {
2203 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2204 }
2205 recover();
2206 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002207
Jamie Madill21c1e452014-12-29 11:33:41 -05002208 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2209
2210 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002211 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002212 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002213 if (index < 0)
2214 {
2215 std::stringstream infoStream;
2216 infoStream << index;
2217 std::string info = infoStream.str();
2218 error(location, "negative index", info.c_str());
2219 recover();
2220 index = 0;
2221 }
2222 if (baseExpression->getType().getQualifier() == EvqConst)
2223 {
2224 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002225 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002226 // constant folding for arrays
2227 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002228 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002229 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002230 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002231 // constant folding for vectors
2232 TVectorFields fields;
2233 fields.num = 1;
2234 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2235 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2236 }
2237 else if (baseExpression->isMatrix())
2238 {
2239 // constant folding for matrices
2240 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002241 }
2242 }
2243 else
2244 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002245 int safeIndex = -1;
2246
Jamie Madill7164cf42013-07-08 13:30:59 -04002247 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002248 {
Jamie Madill18464b52013-07-08 14:01:55 -04002249 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002250 {
2251 std::stringstream extraInfoStream;
2252 extraInfoStream << "array index out of range '" << index << "'";
2253 std::string extraInfo = extraInfoStream.str();
2254 error(location, "", "[", extraInfo.c_str());
2255 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002256 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002257 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002258 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2259 {
2260 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2261 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002262 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002263 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002264 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002265 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002266 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002267 std::stringstream extraInfoStream;
2268 extraInfoStream << "field selection out of range '" << index << "'";
2269 std::string extraInfo = extraInfoStream.str();
2270 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002271 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002272 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002273 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002274
Jamie Madillb11e2482015-05-04 14:21:22 -04002275 // Don't modify the data of the previous constant union, because it can point
2276 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2277 if (safeIndex != -1)
2278 {
2279 TConstantUnion *safeConstantUnion = new TConstantUnion();
2280 safeConstantUnion->setIConst(safeIndex);
2281 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2282 }
2283
Jamie Madill7164cf42013-07-08 13:30:59 -04002284 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002285 }
2286 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002287 else
2288 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002289 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002290 {
Jamie Madill19571812013-08-12 15:26:34 -07002291 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002292 recover();
2293 }
Jamie Madill19571812013-08-12 15:26:34 -07002294 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002295 {
Jamie Madill19571812013-08-12 15:26:34 -07002296 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002297 recover();
2298 }
2299
2300 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2301 }
2302
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002303 if (indexedExpression == 0)
2304 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002305 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002306 unionArray->setFConst(0.0f);
2307 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2308 }
2309 else if (baseExpression->isArray())
2310 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002311 const TType &baseType = baseExpression->getType();
2312 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002313 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002314 TType copyOfType(baseType.getStruct());
2315 indexedExpression->setType(copyOfType);
2316 }
2317 else if (baseType.isInterfaceBlock())
2318 {
2319 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002320 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002321 }
2322 else
2323 {
Minmin Gong794e0002015-04-07 18:31:54 -07002324 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2325 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002326 }
2327
2328 if (baseExpression->getType().getQualifier() == EvqConst)
2329 {
2330 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2331 }
2332 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002333 else if (baseExpression->isMatrix())
2334 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002335 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002336 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002337 }
2338 else if (baseExpression->isVector())
2339 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002340 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2341 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002342 }
2343 else
2344 {
2345 indexedExpression->setType(baseExpression->getType());
2346 }
2347
2348 return indexedExpression;
2349}
2350
Jamie Madill075edd82013-07-08 13:30:19 -04002351TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002352{
2353 TIntermTyped *indexedExpression = NULL;
2354
2355 if (baseExpression->isArray())
2356 {
2357 error(fieldLocation, "cannot apply dot operator to an array", ".");
2358 recover();
2359 }
2360
2361 if (baseExpression->isVector())
2362 {
2363 TVectorFields fields;
2364 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2365 {
2366 fields.num = 1;
2367 fields.offsets[0] = 0;
2368 recover();
2369 }
2370
2371 if (baseExpression->getType().getQualifier() == EvqConst)
2372 {
2373 // constant folding for vector fields
2374 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2375 if (indexedExpression == 0)
2376 {
2377 recover();
2378 indexedExpression = baseExpression;
2379 }
2380 else
2381 {
Minmin Gong794e0002015-04-07 18:31:54 -07002382 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002383 }
2384 }
2385 else
2386 {
2387 TString vectorString = fieldString;
2388 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2389 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002390 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002391 }
2392 }
2393 else if (baseExpression->isMatrix())
2394 {
2395 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002396 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002397 {
2398 fields.wholeRow = false;
2399 fields.wholeCol = false;
2400 fields.row = 0;
2401 fields.col = 0;
2402 recover();
2403 }
2404
2405 if (fields.wholeRow || fields.wholeCol)
2406 {
2407 error(dotLocation, " non-scalar fields not implemented yet", ".");
2408 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002409 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002410 unionArray->setIConst(0);
2411 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2412 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002413 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2414 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002415 }
2416 else
2417 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002418 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002419 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002420 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2421 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2422 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2423 }
2424 }
2425 else if (baseExpression->getBasicType() == EbtStruct)
2426 {
2427 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002428 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2429 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002430 {
2431 error(dotLocation, "structure has no fields", "Internal Error");
2432 recover();
2433 indexedExpression = baseExpression;
2434 }
2435 else
2436 {
2437 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002438 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002439 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002440 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002441 {
2442 fieldFound = true;
2443 break;
2444 }
2445 }
2446 if (fieldFound)
2447 {
2448 if (baseExpression->getType().getQualifier() == EvqConst)
2449 {
2450 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2451 if (indexedExpression == 0)
2452 {
2453 recover();
2454 indexedExpression = baseExpression;
2455 }
2456 else
2457 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002458 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002459 // change the qualifier of the return type, not of the structure field
2460 // as the structure definition is shared between various structures.
2461 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2462 }
2463 }
2464 else
2465 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002466 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002467 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002468 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002469 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002470 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002471 }
2472 }
2473 else
2474 {
2475 error(dotLocation, " no such field in structure", fieldString.c_str());
2476 recover();
2477 indexedExpression = baseExpression;
2478 }
2479 }
2480 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002481 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002482 {
2483 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002484 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2485 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002486 {
2487 error(dotLocation, "interface block has no fields", "Internal Error");
2488 recover();
2489 indexedExpression = baseExpression;
2490 }
2491 else
2492 {
2493 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002494 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002495 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002496 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002497 {
2498 fieldFound = true;
2499 break;
2500 }
2501 }
2502 if (fieldFound)
2503 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002504 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002506 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002507 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002508 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002509 }
2510 else
2511 {
2512 error(dotLocation, " no such field in interface block", fieldString.c_str());
2513 recover();
2514 indexedExpression = baseExpression;
2515 }
2516 }
2517 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002518 else
2519 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002520 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002521 {
2522 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2523 }
2524 else
2525 {
2526 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2527 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002528 recover();
2529 indexedExpression = baseExpression;
2530 }
2531
2532 return indexedExpression;
2533}
2534
Jamie Madill075edd82013-07-08 13:30:19 -04002535TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002536{
Jamie Madilla5efff92013-06-06 11:56:47 -04002537 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002538
Jamie Madilla5efff92013-06-06 11:56:47 -04002539 qualifier.location = -1;
2540 qualifier.matrixPacking = EmpUnspecified;
2541 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002542
2543 if (qualifierType == "shared")
2544 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002545 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002546 }
2547 else if (qualifierType == "packed")
2548 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002549 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002550 }
2551 else if (qualifierType == "std140")
2552 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002553 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002554 }
2555 else if (qualifierType == "row_major")
2556 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002557 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002558 }
2559 else if (qualifierType == "column_major")
2560 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002561 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002562 }
2563 else if (qualifierType == "location")
2564 {
2565 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2566 recover();
2567 }
2568 else
2569 {
2570 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2571 recover();
2572 }
2573
Jamie Madilla5efff92013-06-06 11:56:47 -04002574 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002575}
2576
Jamie Madill075edd82013-07-08 13:30:19 -04002577TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002578{
Jamie Madilla5efff92013-06-06 11:56:47 -04002579 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002580
Jamie Madilla5efff92013-06-06 11:56:47 -04002581 qualifier.location = -1;
2582 qualifier.matrixPacking = EmpUnspecified;
2583 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002584
2585 if (qualifierType != "location")
2586 {
2587 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2588 recover();
2589 }
2590 else
2591 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002592 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002593 if (intValue < 0)
2594 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002595 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002596 recover();
2597 }
2598 else
2599 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002600 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002601 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002602 }
2603
Jamie Madilla5efff92013-06-06 11:56:47 -04002604 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002605}
2606
Jamie Madilla5efff92013-06-06 11:56:47 -04002607TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002608{
Jamie Madilla5efff92013-06-06 11:56:47 -04002609 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002610
Jamie Madilla5efff92013-06-06 11:56:47 -04002611 if (rightQualifier.location != -1)
2612 {
2613 joinedQualifier.location = rightQualifier.location;
2614 }
2615 if (rightQualifier.matrixPacking != EmpUnspecified)
2616 {
2617 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2618 }
2619 if (rightQualifier.blockStorage != EbsUnspecified)
2620 {
2621 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2622 }
2623
2624 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002625}
2626
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002627TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2628 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2629{
2630 TQualifier mergedQualifier = EvqSmoothIn;
2631
Jamie Madill19571812013-08-12 15:26:34 -07002632 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002633 if (interpolationQualifier == EvqSmooth)
2634 mergedQualifier = EvqSmoothIn;
2635 else if (interpolationQualifier == EvqFlat)
2636 mergedQualifier = EvqFlatIn;
2637 else UNREACHABLE();
2638 }
2639 else if (storageQualifier == EvqCentroidIn) {
2640 if (interpolationQualifier == EvqSmooth)
2641 mergedQualifier = EvqCentroidIn;
2642 else if (interpolationQualifier == EvqFlat)
2643 mergedQualifier = EvqFlatIn;
2644 else UNREACHABLE();
2645 }
Jamie Madill19571812013-08-12 15:26:34 -07002646 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002647 if (interpolationQualifier == EvqSmooth)
2648 mergedQualifier = EvqSmoothOut;
2649 else if (interpolationQualifier == EvqFlat)
2650 mergedQualifier = EvqFlatOut;
2651 else UNREACHABLE();
2652 }
2653 else if (storageQualifier == EvqCentroidOut) {
2654 if (interpolationQualifier == EvqSmooth)
2655 mergedQualifier = EvqCentroidOut;
2656 else if (interpolationQualifier == EvqFlat)
2657 mergedQualifier = EvqFlatOut;
2658 else UNREACHABLE();
2659 }
2660 else {
2661 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2662 recover();
2663
2664 mergedQualifier = storageQualifier;
2665 }
2666
2667 TPublicType type;
2668 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2669 return type;
2670}
2671
Jamie Madill98493dd2013-07-08 14:39:03 -04002672TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002673{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002674 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2675 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002676 recover();
2677 }
2678
Jamie Madill98493dd2013-07-08 14:39:03 -04002679 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002680 //
2681 // Careful not to replace already known aspects of type, like array-ness
2682 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002683 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002684 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002685 type->setPrimarySize(typeSpecifier.primarySize);
2686 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002687 type->setPrecision(typeSpecifier.precision);
2688 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002689 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002690
2691 // don't allow arrays of arrays
2692 if (type->isArray()) {
2693 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2694 recover();
2695 }
2696 if (typeSpecifier.array)
2697 type->setArraySize(typeSpecifier.arraySize);
2698 if (typeSpecifier.userDef) {
2699 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002700 }
2701
Jamie Madill98493dd2013-07-08 14:39:03 -04002702 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002703 recover();
2704 }
2705 }
2706
Jamie Madill98493dd2013-07-08 14:39:03 -04002707 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708}
2709
Jamie Madill98493dd2013-07-08 14:39:03 -04002710TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002711{
Jamie Madill98493dd2013-07-08 14:39:03 -04002712 TStructure* structure = new TStructure(structName, fieldList);
2713 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002714
Jamie Madill9b820842015-02-12 10:40:10 -05002715 // Store a bool in the struct if we're at global scope, to allow us to
2716 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002717 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002718 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002719
Jamie Madill98493dd2013-07-08 14:39:03 -04002720 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002721 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002722 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002723 {
2724 recover();
2725 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002726 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002727 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002728 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002729 recover();
2730 }
2731 }
2732
2733 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002734 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002735 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002736 const TField &field = *(*fieldList)[typeListIndex];
2737 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002738 switch (qualifier)
2739 {
2740 case EvqGlobal:
2741 case EvqTemporary:
2742 break;
2743 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002744 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002745 recover();
2746 break;
2747 }
2748 }
2749
2750 TPublicType publicType;
2751 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002752 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002753 exitStructDeclaration();
2754
2755 return publicType;
2756}
2757
Olli Etuahoa3a36662015-02-17 13:46:51 +02002758TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2759{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002760 TBasicType switchType = init->getBasicType();
2761 if ((switchType != EbtInt && switchType != EbtUInt) ||
2762 init->isMatrix() ||
2763 init->isArray() ||
2764 init->isVector())
2765 {
2766 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2767 recover();
2768 return nullptr;
2769 }
2770
Olli Etuahoac5274d2015-02-20 10:19:08 +02002771 if (statementList)
2772 {
2773 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2774 {
2775 recover();
2776 return nullptr;
2777 }
2778 }
2779
Olli Etuahoa3a36662015-02-17 13:46:51 +02002780 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2781 if (node == nullptr)
2782 {
2783 error(loc, "erroneous switch statement", "switch");
2784 recover();
2785 return nullptr;
2786 }
2787 return node;
2788}
2789
2790TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2791{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002792 if (mSwitchNestingLevel == 0)
2793 {
2794 error(loc, "case labels need to be inside switch statements", "case");
2795 recover();
2796 return nullptr;
2797 }
2798 if (condition == nullptr)
2799 {
2800 error(loc, "case label must have a condition", "case");
2801 recover();
2802 return nullptr;
2803 }
2804 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2805 condition->isMatrix() ||
2806 condition->isArray() ||
2807 condition->isVector())
2808 {
2809 error(condition->getLine(), "case label must be a scalar integer", "case");
2810 recover();
2811 }
2812 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2813 if (conditionConst == nullptr)
2814 {
2815 error(condition->getLine(), "case label must be constant", "case");
2816 recover();
2817 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002818 TIntermCase *node = intermediate.addCase(condition, loc);
2819 if (node == nullptr)
2820 {
2821 error(loc, "erroneous case statement", "case");
2822 recover();
2823 return nullptr;
2824 }
2825 return node;
2826}
2827
2828TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2829{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002830 if (mSwitchNestingLevel == 0)
2831 {
2832 error(loc, "default labels need to be inside switch statements", "default");
2833 recover();
2834 return nullptr;
2835 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002836 TIntermCase *node = intermediate.addCase(nullptr, loc);
2837 if (node == nullptr)
2838 {
2839 error(loc, "erroneous default statement", "default");
2840 recover();
2841 return nullptr;
2842 }
2843 return node;
2844}
2845
Olli Etuahof6c694b2015-03-26 14:50:53 +02002846TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2847 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002848{
2849 if (child == nullptr)
2850 {
2851 return nullptr;
2852 }
2853
2854 switch (op)
2855 {
2856 case EOpLogicalNot:
2857 if (child->getBasicType() != EbtBool ||
2858 child->isMatrix() ||
2859 child->isArray() ||
2860 child->isVector())
2861 {
2862 return nullptr;
2863 }
2864 break;
2865 case EOpBitwiseNot:
2866 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2867 child->isMatrix() ||
2868 child->isArray())
2869 {
2870 return nullptr;
2871 }
2872 break;
2873 case EOpPostIncrement:
2874 case EOpPreIncrement:
2875 case EOpPostDecrement:
2876 case EOpPreDecrement:
2877 case EOpNegative:
2878 case EOpPositive:
2879 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002880 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002881 child->isArray())
2882 {
2883 return nullptr;
2884 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002885 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002886 default:
2887 break;
2888 }
2889
Olli Etuahof6c694b2015-03-26 14:50:53 +02002890 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002891}
2892
Olli Etuaho09b22472015-02-11 11:47:26 +02002893TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2894{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002895 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002896 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002897 {
2898 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2899 recover();
2900 return child;
2901 }
2902 return node;
2903}
2904
2905TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2906{
2907 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2908 recover();
2909 return addUnaryMath(op, child, loc);
2910}
2911
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002912bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002913 const TSourceLoc &loc)
2914{
2915 if (left->isArray() || right->isArray())
2916 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002917 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02002918 {
2919 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2920 return false;
2921 }
2922
2923 if (left->isArray() != right->isArray())
2924 {
2925 error(loc, "array / non-array mismatch", GetOperatorString(op));
2926 return false;
2927 }
2928
2929 switch (op)
2930 {
2931 case EOpEqual:
2932 case EOpNotEqual:
2933 case EOpAssign:
2934 case EOpInitialize:
2935 break;
2936 default:
2937 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2938 return false;
2939 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002940 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002941 if (left->getArraySize() != right->getArraySize())
2942 {
2943 error(loc, "array size mismatch", GetOperatorString(op));
2944 return false;
2945 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002946 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002947
2948 // Check ops which require integer / ivec parameters
2949 bool isBitShift = false;
2950 switch (op)
2951 {
2952 case EOpBitShiftLeft:
2953 case EOpBitShiftRight:
2954 case EOpBitShiftLeftAssign:
2955 case EOpBitShiftRightAssign:
2956 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2957 // check that the basic type is an integer type.
2958 isBitShift = true;
2959 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2960 {
2961 return false;
2962 }
2963 break;
2964 case EOpBitwiseAnd:
2965 case EOpBitwiseXor:
2966 case EOpBitwiseOr:
2967 case EOpBitwiseAndAssign:
2968 case EOpBitwiseXorAssign:
2969 case EOpBitwiseOrAssign:
2970 // It is enough to check the type of only one operand, since later it
2971 // is checked that the operand types match.
2972 if (!IsInteger(left->getBasicType()))
2973 {
2974 return false;
2975 }
2976 break;
2977 default:
2978 break;
2979 }
2980
2981 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2982 // So the basic type should usually match.
2983 if (!isBitShift && left->getBasicType() != right->getBasicType())
2984 {
2985 return false;
2986 }
2987
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002988 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002989 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002990 switch(op)
2991 {
2992 case EOpAssign:
2993 case EOpInitialize:
2994 case EOpEqual:
2995 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002996 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002997 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002998 {
2999 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3000 return false;
3001 }
Olli Etuahoff699002015-03-23 14:38:42 +02003002 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3003 // we interpret the spec so that this extends to structs containing samplers,
3004 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003005 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003006 left->getType().isStructureContainingSamplers())
3007 {
3008 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3009 return false;
3010 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003011 case EOpLessThan:
3012 case EOpGreaterThan:
3013 case EOpLessThanEqual:
3014 case EOpGreaterThanEqual:
3015 if ((left->getNominalSize() != right->getNominalSize()) ||
3016 (left->getSecondarySize() != right->getSecondarySize()))
3017 {
3018 return false;
3019 }
3020 default:
3021 break;
3022 }
3023
Olli Etuahod6b14282015-03-17 14:31:35 +02003024 return true;
3025}
3026
Olli Etuahofc1806e2015-03-17 13:03:11 +02003027TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3028 const TSourceLoc &loc)
3029{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003030 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003031 return nullptr;
3032
Olli Etuahofc1806e2015-03-17 13:03:11 +02003033 switch (op)
3034 {
3035 case EOpEqual:
3036 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003037 break;
3038 case EOpLessThan:
3039 case EOpGreaterThan:
3040 case EOpLessThanEqual:
3041 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003042 ASSERT(!left->isArray() && !right->isArray());
3043 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003044 left->getBasicType() == EbtStruct)
3045 {
3046 return nullptr;
3047 }
3048 break;
3049 case EOpLogicalOr:
3050 case EOpLogicalXor:
3051 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003052 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003053 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003054 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003055 {
3056 return nullptr;
3057 }
3058 break;
3059 case EOpAdd:
3060 case EOpSub:
3061 case EOpDiv:
3062 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003063 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003064 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3065 {
3066 return nullptr;
3067 }
3068 break;
3069 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003070 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003071 // Note that this is only for the % operator, not for mod()
3072 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3073 {
3074 return nullptr;
3075 }
3076 break;
3077 // Note that for bitwise ops, type checking is done in promote() to
3078 // share code between ops and compound assignment
3079 default:
3080 break;
3081 }
3082
Olli Etuahofc1806e2015-03-17 13:03:11 +02003083 return intermediate.addBinaryMath(op, left, right, loc);
3084}
3085
Olli Etuaho09b22472015-02-11 11:47:26 +02003086TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3087 const TSourceLoc &loc)
3088{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003089 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003090 if (node == 0)
3091 {
3092 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3093 recover();
3094 return left;
3095 }
3096 return node;
3097}
3098
3099TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3100 const TSourceLoc &loc)
3101{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003102 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003103 if (node == 0)
3104 {
3105 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3106 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003107 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003108 unionArray->setBConst(false);
3109 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3110 }
3111 return node;
3112}
3113
Olli Etuahod6b14282015-03-17 14:31:35 +02003114TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3115 const TSourceLoc &loc)
3116{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003117 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003118 {
3119 return intermediate.addAssign(op, left, right, loc);
3120 }
3121 return nullptr;
3122}
3123
3124TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3125 const TSourceLoc &loc)
3126{
3127 TIntermTyped *node = createAssign(op, left, right, loc);
3128 if (node == nullptr)
3129 {
3130 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3131 recover();
3132 return left;
3133 }
3134 return node;
3135}
3136
Olli Etuaho49300862015-02-20 14:54:49 +02003137TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3138{
3139 switch (op)
3140 {
3141 case EOpContinue:
3142 if (mLoopNestingLevel <= 0)
3143 {
3144 error(loc, "continue statement only allowed in loops", "");
3145 recover();
3146 }
3147 break;
3148 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003149 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003150 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003151 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003152 recover();
3153 }
3154 break;
3155 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003156 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003157 {
3158 error(loc, "non-void function must return a value", "return");
3159 recover();
3160 }
3161 break;
3162 default:
3163 // No checks for discard
3164 break;
3165 }
3166 return intermediate.addBranch(op, loc);
3167}
3168
3169TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3170{
3171 ASSERT(op == EOpReturn);
3172 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003173 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003174 {
3175 error(loc, "void function cannot return a value", "return");
3176 recover();
3177 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003178 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003179 {
3180 error(loc, "function return is not matching type:", "return");
3181 recover();
3182 }
3183 return intermediate.addBranch(op, returnValue, loc);
3184}
3185
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003186TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3187 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003188{
3189 *fatalError = false;
3190 TOperator op = fnCall->getBuiltInOp();
3191 TIntermTyped *callNode = nullptr;
3192
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003193 if (thisNode != nullptr)
3194 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003195 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003196 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003197 TIntermTyped *typedThis = thisNode->getAsTyped();
3198 if (fnCall->getName() != "length")
3199 {
3200 error(loc, "invalid method", fnCall->getName().c_str());
3201 recover();
3202 }
3203 else if (paramNode != nullptr)
3204 {
3205 error(loc, "method takes no parameters", "length");
3206 recover();
3207 }
3208 else if (typedThis == nullptr || !typedThis->isArray())
3209 {
3210 error(loc, "length can only be called on arrays", "length");
3211 recover();
3212 }
3213 else
3214 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003215 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003216 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003217 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003218 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003219 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003220 // (func()).length()
3221 // (int[3](0, 1, 2)).length()
3222 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3223 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3224 // which allows "An array, vector or matrix expression with the length method applied".
3225 error(loc, "length can only be called on array names, not on array expressions", "length");
3226 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003227 }
3228 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003229 unionArray->setIConst(arraySize);
3230 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003231 }
3232 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003233 {
3234 //
3235 // Then this should be a constructor.
3236 // Don't go through the symbol table for constructors.
3237 // Their parameters will be verified algorithmically.
3238 //
3239 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003240 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003241 {
3242 //
3243 // It's a constructor, of type 'type'.
3244 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003245 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003246 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003247
3248 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003249 {
3250 recover();
3251 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3252 }
3253 callNode->setType(type);
3254 }
3255 else
3256 {
3257 //
3258 // Not a constructor. Find it in the symbol table.
3259 //
3260 const TFunction* fnCandidate;
3261 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003262 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003263 if (fnCandidate)
3264 {
3265 //
3266 // A declared function.
3267 //
3268 if (builtIn && !fnCandidate->getExtension().empty() &&
3269 extensionErrorCheck(loc, fnCandidate->getExtension()))
3270 {
3271 recover();
3272 }
3273 op = fnCandidate->getBuiltInOp();
3274 if (builtIn && op != EOpNull)
3275 {
3276 //
3277 // A function call mapped to a built-in operation.
3278 //
3279 if (fnCandidate->getParamCount() == 1)
3280 {
3281 //
3282 // Treat it like a built-in unary operator.
3283 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003284 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003285 if (callNode == nullptr)
3286 {
3287 std::stringstream extraInfoStream;
3288 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003289 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003290 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003291 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003292 *fatalError = true;
3293 return nullptr;
3294 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003295 }
3296 else
3297 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003298 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003299 aggregate->setType(fnCandidate->getReturnType());
3300 aggregate->setPrecisionFromChildren();
3301 callNode = aggregate;
3302
3303 // Some built-in functions have out parameters too.
3304 functionCallLValueErrorCheck(fnCandidate, aggregate);
3305 }
3306 }
3307 else
3308 {
3309 // This is a real function call
3310
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003311 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003312 aggregate->setType(fnCandidate->getReturnType());
3313
3314 // this is how we know whether the given function is a builtIn function or a user defined function
3315 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3316 // if builtIn == true, it's definitely a builtIn function with EOpNull
3317 if (!builtIn)
3318 aggregate->setUserDefined();
3319 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003320 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003321
3322 // This needs to happen after the name is set
3323 if (builtIn)
3324 aggregate->setBuiltInFunctionPrecision();
3325
3326 callNode = aggregate;
3327
3328 functionCallLValueErrorCheck(fnCandidate, aggregate);
3329 }
3330 }
3331 else
3332 {
3333 // error message was put out by findFunction()
3334 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003335 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003336 unionArray->setFConst(0.0f);
3337 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3338 recover();
3339 }
3340 }
3341 delete fnCall;
3342 return callNode;
3343}
3344
Olli Etuaho52901742015-04-15 13:42:45 +03003345TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3346 const TSourceLoc &loc)
3347{
3348 if (boolErrorCheck(loc, cond))
3349 recover();
3350
3351 if (trueBlock->getType() != falseBlock->getType())
3352 {
3353 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3354 recover();
3355 return falseBlock;
3356 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003357 // ESSL1 sections 5.2 and 5.7:
3358 // ESSL3 section 5.7:
3359 // Ternary operator is not among the operators allowed for structures/arrays.
3360 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3361 {
3362 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3363 recover();
3364 return falseBlock;
3365 }
Olli Etuaho52901742015-04-15 13:42:45 +03003366 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3367}
Olli Etuaho49300862015-02-20 14:54:49 +02003368
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003369//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003370// Parse an array of strings using yyparse.
3371//
3372// Returns 0 for success.
3373//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003374int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003375 TParseContext* context) {
3376 if ((count == 0) || (string == NULL))
3377 return 1;
3378
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003379 if (glslang_initialize(context))
3380 return 1;
3381
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003382 int error = glslang_scan(count, string, length, context);
3383 if (!error)
3384 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003385
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003386 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003387
alokp@chromium.org6b495712012-06-29 00:06:58 +00003388 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003389}
3390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003391
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003392