blob: a9e951678c8654df6eb93e72b8235a16b72e54c8 [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 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001052
1053 // Reject shaders using both gl_FragData and gl_FragColor
1054 TQualifier qualifier = variable->getType().getQualifier();
1055 if (qualifier == EvqFragData)
1056 {
1057 mUsesFragData = true;
1058 }
1059 else if (qualifier == EvqFragColor)
1060 {
1061 mUsesFragColor = true;
1062 }
1063
1064 // This validation is not quite correct - it's only an error to write to
1065 // both FragData and FragColor. For simplicity, and because users shouldn't
1066 // be rewarded for reading from undefined varaibles, return an error
1067 // if they are both referenced, rather than assigned.
1068 if (mUsesFragData && mUsesFragColor)
1069 {
1070 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1071 recover();
1072 }
Jamie Madill5c097022014-08-20 16:38:32 -04001073 }
1074
1075 if (!variable)
1076 {
1077 TType type(EbtFloat, EbpUndefined);
1078 TVariable *fakeVariable = new TVariable(name, type);
1079 symbolTable.declare(fakeVariable);
1080 variable = fakeVariable;
1081 }
1082
1083 return variable;
1084}
1085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086//
1087// Look up a function name in the symbol table, and make sure it is a function.
1088//
1089// Return the function symbol if found, otherwise 0.
1090//
Austin Kinross3ae64652015-01-26 15:51:39 -08001091const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001093 // First find by unmangled name to check whether the function name has been
1094 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001095 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001096 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001097 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001098 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
alokp@chromium.org0a576182010-08-09 17:16:27 +00001101 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001102 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 return 0;
1104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
alokp@chromium.org0a576182010-08-09 17:16:27 +00001106 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001107 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 return 0;
1109 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001110
1111 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112}
1113
1114//
1115// Initializers show up in several places in the grammar. Have one set of
1116// code to handle them here.
1117//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001118// Returns true on error, false if no error
1119//
Jamie Madill06145232015-05-13 13:10:01 -04001120bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001121 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001123 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
Olli Etuaho2935c582015-04-08 14:32:06 +03001126 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001127 if (type.isUnsizedArray())
1128 {
1129 type.setArraySize(initializer->getArraySize());
1130 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001131 if (!declareVariable(line, identifier, type, &variable))
1132 {
1133 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
Olli Etuahob0c645e2015-05-12 14:25:36 +03001136 bool globalInitWarning = false;
1137 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1138 {
1139 // Error message does not completely match behavior with ESSL 1.00, but
1140 // we want to steer developers towards only using constant expressions.
1141 error(line, "global variable initializers must be constant expressions", "=");
1142 return true;
1143 }
1144 if (globalInitWarning)
1145 {
1146 warning(line, "global variable initializers should be constant expressions "
1147 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1148 }
1149
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001150 //
1151 // identifier must be of type constant, a global, or a temporary
1152 //
1153 TQualifier qualifier = variable->getType().getQualifier();
1154 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001155 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001156 return true;
1157 }
1158 //
1159 // test for and propagate constant
1160 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001162 if (qualifier == EvqConst) {
1163 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001164 std::stringstream extraInfoStream;
1165 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1166 std::string extraInfo = extraInfoStream.str();
1167 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001168 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 return true;
1170 }
1171 if (type != initializer->getType()) {
1172 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001173 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001174 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 return true;
1176 }
1177 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001178 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001180 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001183 TConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001184 variable->shareConstPointer(constArray);
1185 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001186 std::stringstream extraInfoStream;
1187 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1188 std::string extraInfo = extraInfoStream.str();
1189 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001190 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001191 return true;
1192 }
1193 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001194
1195 if (qualifier != EvqConst)
1196 {
1197 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1198 variable->getType(), line);
1199 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1200 if (*intermNode == nullptr)
1201 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001202 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1203 return true;
1204 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001205 }
1206 else
1207 {
1208 *intermNode = nullptr;
1209 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001211 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001212}
1213
1214bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1215{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001216 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001217 if (!aggrNode->isConstructor())
1218 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001220 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001221
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001222 // check if all the child nodes are constants so that they can be inserted into
1223 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001224 TIntermSequence *sequence = aggrNode->getSequence() ;
1225 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001226 if (!(*p)->getAsTyped()->getAsConstantUnion())
1227 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001230 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001231}
1232
Olli Etuaho214c2d82015-04-27 14:49:13 +03001233TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
1234 const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001235{
1236 TPublicType returnType = typeSpecifier;
1237 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001238 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001239 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001240
1241 if (typeSpecifier.array)
1242 {
1243 error(typeSpecifier.line, "not supported", "first-class array");
1244 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001245 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001246 }
1247
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001248 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001249 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001250 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1251 {
1252 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1253 recover();
1254 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001255
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001256 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1257 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1258 {
1259 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1260 recover();
1261 }
1262 }
1263 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001264 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001265 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001266 {
Jamie Madill19571812013-08-12 15:26:34 -07001267 case EvqSmoothIn:
1268 case EvqSmoothOut:
1269 case EvqVertexOut:
1270 case EvqFragmentIn:
1271 case EvqCentroidOut:
1272 case EvqCentroidIn:
1273 if (typeSpecifier.type == EbtBool)
1274 {
1275 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1276 recover();
1277 }
1278 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1279 {
1280 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1281 recover();
1282 }
1283 break;
1284
1285 case EvqVertexIn:
1286 case EvqFragmentOut:
1287 case EvqFlatIn:
1288 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001289 if (typeSpecifier.type == EbtBool)
1290 {
1291 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1292 recover();
1293 }
1294 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001295
Jamie Madillb120eac2013-06-12 14:08:13 -04001296 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001297 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001298 }
1299
1300 return returnType;
1301}
1302
Olli Etuahofa33d582015-04-09 14:33:12 +03001303TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1304 const TSourceLoc &identifierOrTypeLocation,
1305 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001306{
Olli Etuahofa33d582015-04-09 14:33:12 +03001307 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001308
Olli Etuahobab4c082015-04-24 16:38:49 +03001309 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001310
Olli Etuahobab4c082015-04-24 16:38:49 +03001311 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1312
1313 if (emptyDeclaration)
1314 {
1315 if (publicType.isUnsizedArray())
1316 {
1317 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1318 // It is assumed that this applies to empty declarations as well.
1319 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1320 }
1321 }
1322 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001323 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001324 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001325 recover();
1326
Olli Etuaho376f1b52015-04-13 13:23:41 +03001327 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001328 recover();
1329
Olli Etuaho2935c582015-04-08 14:32:06 +03001330 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001331 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001332 recover();
1333
1334 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001335 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001336 }
1337
Olli Etuahoe7847b02015-03-16 11:56:12 +02001338 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001339}
1340
Olli Etuahoe7847b02015-03-16 11:56:12 +02001341TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1342 const TSourceLoc &identifierLocation,
1343 const TString &identifier,
1344 const TSourceLoc &indexLocation,
1345 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001346{
Olli Etuahofa33d582015-04-09 14:33:12 +03001347 mDeferredSingleDeclarationErrorCheck = false;
1348
1349 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001350 recover();
1351
Olli Etuaho376f1b52015-04-13 13:23:41 +03001352 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001353 recover();
1354
1355 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1356 {
1357 recover();
1358 }
1359
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001360 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001361
1362 int size;
1363 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1364 {
1365 recover();
1366 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001367 // Make the type an array even if size check failed.
1368 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1369 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001370
Olli Etuaho2935c582015-04-08 14:32:06 +03001371 TVariable *variable = nullptr;
1372 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001373 recover();
1374
Olli Etuahoe7847b02015-03-16 11:56:12 +02001375 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001376 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001377 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001378
Olli Etuahoe7847b02015-03-16 11:56:12 +02001379 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001380}
1381
Jamie Madill06145232015-05-13 13:10:01 -04001382TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001383 const TSourceLoc &identifierLocation,
1384 const TString &identifier,
1385 const TSourceLoc &initLocation,
1386 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001387{
Olli Etuahofa33d582015-04-09 14:33:12 +03001388 mDeferredSingleDeclarationErrorCheck = false;
1389
1390 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001391 recover();
1392
Olli Etuahoe7847b02015-03-16 11:56:12 +02001393 TIntermNode *intermNode = nullptr;
1394 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001395 {
1396 //
1397 // Build intermediate representation
1398 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001399 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001400 }
1401 else
1402 {
1403 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001404 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001405 }
1406}
1407
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001408TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1409 const TSourceLoc &identifierLocation,
1410 const TString &identifier,
1411 const TSourceLoc &indexLocation,
1412 TIntermTyped *indexExpression,
1413 const TSourceLoc &initLocation,
1414 TIntermTyped *initializer)
1415{
1416 mDeferredSingleDeclarationErrorCheck = false;
1417
1418 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1419 recover();
1420
1421 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1422 {
1423 recover();
1424 }
1425
1426 TPublicType arrayType(publicType);
1427
Olli Etuaho376f1b52015-04-13 13:23:41 +03001428 int size = 0;
1429 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1430 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001431 {
1432 recover();
1433 }
1434 // Make the type an array even if size check failed.
1435 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1436 arrayType.setArraySize(size);
1437
1438 // initNode will correspond to the whole of "type b[n] = initializer".
1439 TIntermNode *initNode = nullptr;
1440 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1441 {
1442 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1443 }
1444 else
1445 {
1446 recover();
1447 return nullptr;
1448 }
1449}
1450
Olli Etuahoe7847b02015-03-16 11:56:12 +02001451TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001452 const TSourceLoc &identifierLoc,
1453 const TString *identifier,
1454 const TSymbol *symbol)
1455{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001456 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001457 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1458 {
1459 recover();
1460 }
1461
1462 if (!symbol)
1463 {
1464 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1465 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001466 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001467 }
1468 else
1469 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001470 const TString kGlFrontFacing("gl_FrontFacing");
1471 if (*identifier == kGlFrontFacing)
1472 {
1473 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1474 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001475 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001476 }
Jamie Madill2c433252014-12-03 12:36:54 -05001477 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001478 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1479 ASSERT(variable);
1480 const TType &type = variable->getType();
1481 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1482 *identifier, type, identifierLoc);
1483
1484 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1485 aggregate->setOp(EOpInvariantDeclaration);
1486 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001487 }
1488}
1489
Olli Etuahoe7847b02015-03-16 11:56:12 +02001490TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1491 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001492{
Olli Etuahofa33d582015-04-09 14:33:12 +03001493 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1494 if (mDeferredSingleDeclarationErrorCheck)
1495 {
1496 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1497 recover();
1498 mDeferredSingleDeclarationErrorCheck = false;
1499 }
1500
Jamie Madill0bd18df2013-06-20 11:55:52 -04001501 if (locationDeclaratorListCheck(identifierLocation, publicType))
1502 recover();
1503
Olli Etuaho376f1b52015-04-13 13:23:41 +03001504 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001505 recover();
1506
Olli Etuaho2935c582015-04-08 14:32:06 +03001507 TVariable *variable = nullptr;
1508 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001509 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001510
1511 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1512 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001513 symbol->setId(variable->getUniqueId());
1514
Olli Etuahoe7847b02015-03-16 11:56:12 +02001515 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001516}
1517
Olli Etuahoe7847b02015-03-16 11:56:12 +02001518TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1519 const TSourceLoc &identifierLocation, const TString &identifier,
1520 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001521{
Olli Etuahofa33d582015-04-09 14:33:12 +03001522 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1523 if (mDeferredSingleDeclarationErrorCheck)
1524 {
1525 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1526 recover();
1527 mDeferredSingleDeclarationErrorCheck = false;
1528 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001529
Jamie Madill0bd18df2013-06-20 11:55:52 -04001530 if (locationDeclaratorListCheck(identifierLocation, publicType))
1531 recover();
1532
Olli Etuaho376f1b52015-04-13 13:23:41 +03001533 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001534 recover();
1535
1536 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1537 {
1538 recover();
1539 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001540 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001541 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001542 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001543 int size;
1544 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001545 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001546 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001547 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001548 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001550 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001551 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001552 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001553
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001554 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1555 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001556 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001557
1558 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001559 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001560
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001561 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001562}
1563
Jamie Madill06145232015-05-13 13:10:01 -04001564TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001565 const TSourceLoc &identifierLocation, const TString &identifier,
1566 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001567{
Olli Etuahofa33d582015-04-09 14:33:12 +03001568 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1569 if (mDeferredSingleDeclarationErrorCheck)
1570 {
1571 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1572 recover();
1573 mDeferredSingleDeclarationErrorCheck = false;
1574 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001575
Jamie Madill0bd18df2013-06-20 11:55:52 -04001576 if (locationDeclaratorListCheck(identifierLocation, publicType))
1577 recover();
1578
Olli Etuahoe7847b02015-03-16 11:56:12 +02001579 TIntermNode *intermNode = nullptr;
1580 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001581 {
1582 //
1583 // build the intermediate representation
1584 //
1585 if (intermNode)
1586 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001587 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001588 }
1589 else
1590 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001591 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001592 }
1593 }
1594 else
1595 {
1596 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001597 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001598 }
1599}
1600
Jamie Madill06145232015-05-13 13:10:01 -04001601TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001602 TIntermAggregate *aggregateDeclaration,
1603 const TSourceLoc& identifierLocation,
1604 const TString &identifier,
1605 const TSourceLoc& indexLocation,
1606 TIntermTyped *indexExpression,
1607 const TSourceLoc &initLocation, TIntermTyped *initializer)
1608{
1609 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1610 if (mDeferredSingleDeclarationErrorCheck)
1611 {
1612 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1613 recover();
1614 mDeferredSingleDeclarationErrorCheck = false;
1615 }
1616
1617 if (locationDeclaratorListCheck(identifierLocation, publicType))
1618 recover();
1619
1620 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1621 {
1622 recover();
1623 }
1624
1625 TPublicType arrayType(publicType);
1626
Olli Etuaho376f1b52015-04-13 13:23:41 +03001627 int size = 0;
1628 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1629 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001630 {
1631 recover();
1632 }
1633 // Make the type an array even if size check failed.
1634 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1635 arrayType.setArraySize(size);
1636
1637 // initNode will correspond to the whole of "b[n] = initializer".
1638 TIntermNode *initNode = nullptr;
1639 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1640 {
1641 if (initNode)
1642 {
1643 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1644 }
1645 else
1646 {
1647 return aggregateDeclaration;
1648 }
1649 }
1650 else
1651 {
1652 recover();
1653 return nullptr;
1654 }
1655}
1656
Jamie Madilla295edf2013-06-06 11:56:48 -04001657void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1658{
1659 if (typeQualifier.qualifier != EvqUniform)
1660 {
1661 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1662 recover();
1663 return;
1664 }
1665
1666 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1667 ASSERT(!layoutQualifier.isEmpty());
1668
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001669 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001670 {
1671 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1672 recover();
1673 return;
1674 }
1675
1676 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1677 {
1678 recover();
1679 return;
1680 }
1681
Jamie Madill099c0f32013-06-20 11:55:52 -04001682 if (layoutQualifier.matrixPacking != EmpUnspecified)
1683 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001684 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001685 }
1686
Geoff Langc6856732014-02-11 09:38:55 -05001687 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001688 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001689 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001690 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001691}
1692
Jamie Madill06145232015-05-13 13:10:01 -04001693TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001694{
Jamie Madill06145232015-05-13 13:10:01 -04001695 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001696 TOperator op = EOpNull;
1697 if (publicType.userDef)
1698 {
1699 op = EOpConstructStruct;
1700 }
1701 else
1702 {
1703 switch (publicType.type)
1704 {
1705 case EbtFloat:
1706 if (publicType.isMatrix())
1707 {
1708 // TODO: non-square matrices
1709 switch(publicType.getCols())
1710 {
Jamie Madill28b97422013-07-08 14:01:38 -04001711 case 2: op = EOpConstructMat2; break;
1712 case 3: op = EOpConstructMat3; break;
1713 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001714 }
1715 }
1716 else
1717 {
1718 switch(publicType.getNominalSize())
1719 {
Jamie Madill28b97422013-07-08 14:01:38 -04001720 case 1: op = EOpConstructFloat; break;
1721 case 2: op = EOpConstructVec2; break;
1722 case 3: op = EOpConstructVec3; break;
1723 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001724 }
1725 }
1726 break;
1727
1728 case EbtInt:
1729 switch(publicType.getNominalSize())
1730 {
Jamie Madill28b97422013-07-08 14:01:38 -04001731 case 1: op = EOpConstructInt; break;
1732 case 2: op = EOpConstructIVec2; break;
1733 case 3: op = EOpConstructIVec3; break;
1734 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001735 }
1736 break;
1737
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001738 case EbtUInt:
1739 switch(publicType.getNominalSize())
1740 {
Jamie Madill28b97422013-07-08 14:01:38 -04001741 case 1: op = EOpConstructUInt; break;
1742 case 2: op = EOpConstructUVec2; break;
1743 case 3: op = EOpConstructUVec3; break;
1744 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001745 }
1746 break;
1747
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001748 case EbtBool:
1749 switch(publicType.getNominalSize())
1750 {
Jamie Madill28b97422013-07-08 14:01:38 -04001751 case 1: op = EOpConstructBool; break;
1752 case 2: op = EOpConstructBVec2; break;
1753 case 3: op = EOpConstructBVec3; break;
1754 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001755 }
1756 break;
1757
1758 default: break;
1759 }
1760
1761 if (op == EOpNull)
1762 {
1763 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1764 recover();
1765 publicType.type = EbtFloat;
1766 op = EOpConstructFloat;
1767 }
1768 }
1769
1770 TString tempString;
1771 TType type(publicType);
1772 return new TFunction(&tempString, type, op);
1773}
1774
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775// This function is used to test for the correctness of the parameters passed to various constructor functions
1776// and also convert them to the right datatype if it is allowed and required.
1777//
1778// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1779//
Olli Etuaho21203702014-11-13 16:16:21 +02001780TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001782 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001784 if (!aggregateArguments)
1785 {
1786 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001787 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001788 }
1789
Olli Etuahof40319e2015-03-10 14:33:00 +02001790 if (type->isArray())
1791 {
1792 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1793 TIntermSequence *args = aggregateArguments->getSequence();
1794 for (size_t i = 0; i < args->size(); i++)
1795 {
1796 const TType &argType = (*args)[i]->getAsTyped()->getType();
1797 // It has already been checked that the argument is not an array.
1798 ASSERT(!argType.isArray());
1799 if (!argType.sameElementType(*type))
1800 {
1801 error(line, "Array constructor argument has an incorrect type", "Error");
1802 recover();
1803 return nullptr;
1804 }
1805 }
1806 }
1807 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001808 {
1809 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001810 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001812 for (size_t i = 0; i < fields.size(); i++)
1813 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001814 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001815 {
1816 error(line, "Structure constructor arguments do not match structure fields", "Error");
1817 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001819 return 0;
1820 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 }
1822 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001824 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001825 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1826 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001828 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001829 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
Olli Etuaho21203702014-11-13 16:16:21 +02001832 // Structs should not be precision qualified, the individual members may be.
1833 // Built-in types on the other hand should be precision qualified.
1834 if (op != EOpConstructStruct)
1835 {
1836 constructor->setPrecisionFromChildren();
1837 type->setPrecision(constructor->getPrecision());
1838 }
1839
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841}
1842
1843TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1844{
Olli Etuahof40319e2015-03-10 14:33:00 +02001845 // TODO: Add support for folding array constructors
1846 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001847 aggrNode->setType(type);
1848 if (canBeFolded) {
1849 bool returnVal = false;
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001850 TConstantUnion* unionArray = new TConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001851 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001852 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001853 }
1854 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001855 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 }
1857 if (returnVal)
1858 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001859
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001860 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1861 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001863 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864}
1865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866//
1867// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1868// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1869// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1870// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1871// a constant matrix.
1872//
Jamie Madill075edd82013-07-08 13:30:19 -04001873TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001875 TIntermTyped* typedNode;
1876 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877
Jamie Madillb11e2482015-05-04 14:21:22 -04001878 const TConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001879 if (tempConstantNode) {
1880 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001882 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001883 return node;
1884 }
1885 } 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 +00001886 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001887 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001889 return 0;
1890 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001892 TConstantUnion* constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001894 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001895 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001896 std::stringstream extraInfoStream;
1897 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1898 std::string extraInfo = extraInfoStream.str();
1899 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001900 recover();
1901 fields.offsets[i] = 0;
1902 }
1903
1904 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001906 }
1907 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1908 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909}
1910
1911//
1912// This function returns the column being accessed from a constant matrix. The values are retrieved from
1913// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1914// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1915// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1916//
Jamie Madill075edd82013-07-08 13:30:19 -04001917TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001919 TIntermTyped* typedNode;
1920 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001922 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001923 std::stringstream extraInfoStream;
1924 extraInfoStream << "matrix field selection out of range '" << index << "'";
1925 std::string extraInfo = extraInfoStream.str();
1926 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001927 recover();
1928 index = 0;
1929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001931 if (tempConstantNode) {
Jamie Madillb11e2482015-05-04 14:21:22 -04001932 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001933 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001934 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1935 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001936 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001937 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 return 0;
1940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001942 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943}
1944
1945
1946//
1947// This function returns an element of an array accessed from a constant array. The values are retrieved from
1948// the symbol table and parse-tree is built for the type of the element. The input
1949// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1950// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1951//
Jamie Madill075edd82013-07-08 13:30:19 -04001952TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001954 TIntermTyped* typedNode;
1955 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1956 TType arrayElementType = node->getType();
1957 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001959 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001960 std::stringstream extraInfoStream;
1961 extraInfoStream << "array field selection out of range '" << index << "'";
1962 std::string extraInfo = extraInfoStream.str();
1963 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001964 recover();
1965 index = 0;
1966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001968 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001969 size_t arrayElementSize = arrayElementType.getObjectSize();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001970 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001971 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001972 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001973 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001974 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001976 return 0;
1977 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001979 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980}
1981
1982
1983//
1984// This function returns the value of a particular field inside a constant structure from the symbol table.
1985// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1986// function and returns the parse-tree with the values of the embedded/nested struct.
1987//
Jamie Madill075edd82013-07-08 13:30:19 -04001988TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989{
Jamie Madill98493dd2013-07-08 14:39:03 -04001990 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001991 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
Jamie Madill98493dd2013-07-08 14:39:03 -04001993 for (size_t index = 0; index < fields.size(); ++index) {
1994 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001995 break;
1996 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001997 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001998 }
1999 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000
Jamie Madill94bf7f22013-07-08 13:31:15 -04002001 TIntermTyped *typedNode;
2002 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002003 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002004 TConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002006 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2007 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002008 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002009 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002011 return 0;
2012 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002014 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015}
2016
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002017//
2018// Interface/uniform blocks
2019//
Jamie Madill98493dd2013-07-08 14:39:03 -04002020TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
2021 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002022{
2023 if (reservedErrorCheck(nameLine, blockName))
2024 recover();
2025
2026 if (typeQualifier.qualifier != EvqUniform)
2027 {
2028 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2029 recover();
2030 }
2031
Jamie Madill099c0f32013-06-20 11:55:52 -04002032 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2033 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002034 {
2035 recover();
2036 }
2037
Jamie Madill099c0f32013-06-20 11:55:52 -04002038 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2039 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002040 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002041 }
2042
Jamie Madill1566ef72013-06-20 11:55:54 -04002043 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2044 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002045 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002046 }
2047
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002048 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002049 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002050 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2051 recover();
2052 }
2053
Jamie Madill98493dd2013-07-08 14:39:03 -04002054 // check for sampler types and apply layout qualifiers
2055 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2056 TField* field = (*fieldList)[memberIndex];
2057 TType* fieldType = field->type();
2058 if (IsSampler(fieldType->getBasicType())) {
2059 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002060 recover();
2061 }
2062
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002064 switch (qualifier)
2065 {
2066 case EvqGlobal:
2067 case EvqUniform:
2068 break;
2069 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002070 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002071 recover();
2072 break;
2073 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002074
2075 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002076 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2077 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002078 {
2079 recover();
2080 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002081
Jamie Madill98493dd2013-07-08 14:39:03 -04002082 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002083 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002084 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002085 recover();
2086 }
2087
Jamie Madill98493dd2013-07-08 14:39:03 -04002088 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002089 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002090 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002091 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002092 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002093 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002094 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002095 recover();
2096 }
2097
Jamie Madill98493dd2013-07-08 14:39:03 -04002098 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002099 }
2100
Jamie Madill98493dd2013-07-08 14:39:03 -04002101 // add array index
2102 int arraySize = 0;
2103 if (arrayIndex != NULL)
2104 {
2105 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2106 recover();
2107 }
2108
2109 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2110 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002111
2112 TString symbolName = "";
2113 int symbolId = 0;
2114
Jamie Madill98493dd2013-07-08 14:39:03 -04002115 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002116 {
2117 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002118 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2119 {
2120 TField* field = (*fieldList)[memberIndex];
2121 TType* fieldType = field->type();
2122
2123 // set parent pointer of the field variable
2124 fieldType->setInterfaceBlock(interfaceBlock);
2125
2126 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2127 fieldVariable->setQualifier(typeQualifier.qualifier);
2128
Nicolas Capensadfffe42014-06-17 02:13:36 -04002129 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002130 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002131 recover();
2132 }
2133 }
2134 }
2135 else
2136 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002137 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002138 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002139 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002140
Nicolas Capensadfffe42014-06-17 02:13:36 -04002141 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002142 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002143 recover();
2144 }
2145
2146 symbolId = instanceTypeDef->getUniqueId();
2147 symbolName = instanceTypeDef->getName();
2148 }
2149
Jamie Madill98493dd2013-07-08 14:39:03 -04002150 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002151 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002152
2153 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002154 return aggregate;
2155}
2156
Jamie Madill075edd82013-07-08 13:30:19 -04002157bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002158{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002159 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002160
2161 // Embedded structure definitions are not supported per GLSL ES spec.
2162 // They aren't allowed in GLSL either, but we need to detect this here
2163 // so we don't rely on the GLSL compiler to catch it.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002164 if (mStructNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002165 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002166 return true;
2167 }
2168
2169 return false;
2170}
2171
2172void TParseContext::exitStructDeclaration()
2173{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002174 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002175}
2176
2177namespace {
2178
2179const int kWebGLMaxStructNesting = 4;
2180
2181} // namespace
2182
Jamie Madill98493dd2013-07-08 14:39:03 -04002183bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002184{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002185 if (!IsWebGLBasedSpec(mShaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002186 return false;
2187 }
2188
Jamie Madill98493dd2013-07-08 14:39:03 -04002189 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002190 return false;
2191 }
2192
2193 // We're already inside a structure definition at this point, so add
2194 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002195 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002196 std::stringstream reasonStream;
2197 reasonStream << "Reference of struct type "
2198 << field.type()->getStruct()->name().c_str()
2199 << " exceeds maximum allowed nesting level of "
2200 << kWebGLMaxStructNesting;
2201 std::string reason = reasonStream.str();
2202 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002203 return true;
2204 }
2205
2206 return false;
2207}
2208
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002209//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002210// Parse an array index expression
2211//
Jamie Madill075edd82013-07-08 13:30:19 -04002212TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002213{
2214 TIntermTyped *indexedExpression = NULL;
2215
2216 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2217 {
2218 if (baseExpression->getAsSymbolNode())
2219 {
2220 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2221 }
2222 else
2223 {
2224 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2225 }
2226 recover();
2227 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002228
Jamie Madill21c1e452014-12-29 11:33:41 -05002229 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2230
2231 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002232 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002233 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002234 if (index < 0)
2235 {
2236 std::stringstream infoStream;
2237 infoStream << index;
2238 std::string info = infoStream.str();
2239 error(location, "negative index", info.c_str());
2240 recover();
2241 index = 0;
2242 }
2243 if (baseExpression->getType().getQualifier() == EvqConst)
2244 {
2245 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002246 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002247 // constant folding for arrays
2248 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002249 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002250 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002251 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002252 // constant folding for vectors
2253 TVectorFields fields;
2254 fields.num = 1;
2255 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2256 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2257 }
2258 else if (baseExpression->isMatrix())
2259 {
2260 // constant folding for matrices
2261 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002262 }
2263 }
2264 else
2265 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002266 int safeIndex = -1;
2267
Jamie Madill7164cf42013-07-08 13:30:59 -04002268 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002269 {
Jamie Madill18464b52013-07-08 14:01:55 -04002270 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002271 {
2272 std::stringstream extraInfoStream;
2273 extraInfoStream << "array index out of range '" << index << "'";
2274 std::string extraInfo = extraInfoStream.str();
2275 error(location, "", "[", extraInfo.c_str());
2276 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002277 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002278 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002279 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2280 {
2281 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2282 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002283 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002284 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002285 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002286 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002287 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002288 std::stringstream extraInfoStream;
2289 extraInfoStream << "field selection out of range '" << index << "'";
2290 std::string extraInfo = extraInfoStream.str();
2291 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002292 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002293 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002294 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002295
Jamie Madillb11e2482015-05-04 14:21:22 -04002296 // Don't modify the data of the previous constant union, because it can point
2297 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2298 if (safeIndex != -1)
2299 {
2300 TConstantUnion *safeConstantUnion = new TConstantUnion();
2301 safeConstantUnion->setIConst(safeIndex);
2302 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2303 }
2304
Jamie Madill7164cf42013-07-08 13:30:59 -04002305 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002306 }
2307 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002308 else
2309 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002310 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002311 {
Jamie Madill19571812013-08-12 15:26:34 -07002312 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002313 recover();
2314 }
Jamie Madill19571812013-08-12 15:26:34 -07002315 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002316 {
Jamie Madill19571812013-08-12 15:26:34 -07002317 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002318 recover();
2319 }
2320
2321 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2322 }
2323
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002324 if (indexedExpression == 0)
2325 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002326 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002327 unionArray->setFConst(0.0f);
2328 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2329 }
2330 else if (baseExpression->isArray())
2331 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002332 const TType &baseType = baseExpression->getType();
2333 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002334 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002335 TType copyOfType(baseType.getStruct());
2336 indexedExpression->setType(copyOfType);
2337 }
2338 else if (baseType.isInterfaceBlock())
2339 {
2340 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002341 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002342 }
2343 else
2344 {
Minmin Gong794e0002015-04-07 18:31:54 -07002345 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2346 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002347 }
2348
2349 if (baseExpression->getType().getQualifier() == EvqConst)
2350 {
2351 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2352 }
2353 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002354 else if (baseExpression->isMatrix())
2355 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002356 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002357 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002358 }
2359 else if (baseExpression->isVector())
2360 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002361 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2362 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002363 }
2364 else
2365 {
2366 indexedExpression->setType(baseExpression->getType());
2367 }
2368
2369 return indexedExpression;
2370}
2371
Jamie Madill075edd82013-07-08 13:30:19 -04002372TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002373{
2374 TIntermTyped *indexedExpression = NULL;
2375
2376 if (baseExpression->isArray())
2377 {
2378 error(fieldLocation, "cannot apply dot operator to an array", ".");
2379 recover();
2380 }
2381
2382 if (baseExpression->isVector())
2383 {
2384 TVectorFields fields;
2385 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2386 {
2387 fields.num = 1;
2388 fields.offsets[0] = 0;
2389 recover();
2390 }
2391
2392 if (baseExpression->getType().getQualifier() == EvqConst)
2393 {
2394 // constant folding for vector fields
2395 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2396 if (indexedExpression == 0)
2397 {
2398 recover();
2399 indexedExpression = baseExpression;
2400 }
2401 else
2402 {
Minmin Gong794e0002015-04-07 18:31:54 -07002403 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002404 }
2405 }
2406 else
2407 {
2408 TString vectorString = fieldString;
2409 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2410 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002411 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002412 }
2413 }
2414 else if (baseExpression->isMatrix())
2415 {
2416 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002417 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002418 {
2419 fields.wholeRow = false;
2420 fields.wholeCol = false;
2421 fields.row = 0;
2422 fields.col = 0;
2423 recover();
2424 }
2425
2426 if (fields.wholeRow || fields.wholeCol)
2427 {
2428 error(dotLocation, " non-scalar fields not implemented yet", ".");
2429 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002430 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002431 unionArray->setIConst(0);
2432 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2433 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002434 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2435 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002436 }
2437 else
2438 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002439 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002440 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002441 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2442 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2443 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2444 }
2445 }
2446 else if (baseExpression->getBasicType() == EbtStruct)
2447 {
2448 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002449 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2450 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002451 {
2452 error(dotLocation, "structure has no fields", "Internal Error");
2453 recover();
2454 indexedExpression = baseExpression;
2455 }
2456 else
2457 {
2458 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002459 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002460 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002461 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002462 {
2463 fieldFound = true;
2464 break;
2465 }
2466 }
2467 if (fieldFound)
2468 {
2469 if (baseExpression->getType().getQualifier() == EvqConst)
2470 {
2471 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2472 if (indexedExpression == 0)
2473 {
2474 recover();
2475 indexedExpression = baseExpression;
2476 }
2477 else
2478 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002479 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002480 // change the qualifier of the return type, not of the structure field
2481 // as the structure definition is shared between various structures.
2482 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2483 }
2484 }
2485 else
2486 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002487 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002488 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002489 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002490 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002491 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002492 }
2493 }
2494 else
2495 {
2496 error(dotLocation, " no such field in structure", fieldString.c_str());
2497 recover();
2498 indexedExpression = baseExpression;
2499 }
2500 }
2501 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002502 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002503 {
2504 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002505 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2506 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002507 {
2508 error(dotLocation, "interface block has no fields", "Internal Error");
2509 recover();
2510 indexedExpression = baseExpression;
2511 }
2512 else
2513 {
2514 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002515 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002516 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002517 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002518 {
2519 fieldFound = true;
2520 break;
2521 }
2522 }
2523 if (fieldFound)
2524 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002525 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002526 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002527 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002528 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002529 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002530 }
2531 else
2532 {
2533 error(dotLocation, " no such field in interface block", fieldString.c_str());
2534 recover();
2535 indexedExpression = baseExpression;
2536 }
2537 }
2538 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002539 else
2540 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002541 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002542 {
2543 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2544 }
2545 else
2546 {
2547 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2548 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002549 recover();
2550 indexedExpression = baseExpression;
2551 }
2552
2553 return indexedExpression;
2554}
2555
Jamie Madill075edd82013-07-08 13:30:19 -04002556TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002557{
Jamie Madilla5efff92013-06-06 11:56:47 -04002558 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002559
Jamie Madilla5efff92013-06-06 11:56:47 -04002560 qualifier.location = -1;
2561 qualifier.matrixPacking = EmpUnspecified;
2562 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002563
2564 if (qualifierType == "shared")
2565 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002566 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002567 }
2568 else if (qualifierType == "packed")
2569 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002570 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002571 }
2572 else if (qualifierType == "std140")
2573 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002574 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002575 }
2576 else if (qualifierType == "row_major")
2577 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002578 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002579 }
2580 else if (qualifierType == "column_major")
2581 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002582 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002583 }
2584 else if (qualifierType == "location")
2585 {
2586 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2587 recover();
2588 }
2589 else
2590 {
2591 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2592 recover();
2593 }
2594
Jamie Madilla5efff92013-06-06 11:56:47 -04002595 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002596}
2597
Jamie Madill075edd82013-07-08 13:30:19 -04002598TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002599{
Jamie Madilla5efff92013-06-06 11:56:47 -04002600 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002601
Jamie Madilla5efff92013-06-06 11:56:47 -04002602 qualifier.location = -1;
2603 qualifier.matrixPacking = EmpUnspecified;
2604 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002605
2606 if (qualifierType != "location")
2607 {
2608 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2609 recover();
2610 }
2611 else
2612 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002613 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002614 if (intValue < 0)
2615 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002616 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002617 recover();
2618 }
2619 else
2620 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002621 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002622 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002623 }
2624
Jamie Madilla5efff92013-06-06 11:56:47 -04002625 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002626}
2627
Jamie Madilla5efff92013-06-06 11:56:47 -04002628TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002629{
Jamie Madilla5efff92013-06-06 11:56:47 -04002630 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002631
Jamie Madilla5efff92013-06-06 11:56:47 -04002632 if (rightQualifier.location != -1)
2633 {
2634 joinedQualifier.location = rightQualifier.location;
2635 }
2636 if (rightQualifier.matrixPacking != EmpUnspecified)
2637 {
2638 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2639 }
2640 if (rightQualifier.blockStorage != EbsUnspecified)
2641 {
2642 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2643 }
2644
2645 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002646}
2647
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002648TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2649 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2650{
2651 TQualifier mergedQualifier = EvqSmoothIn;
2652
Jamie Madill19571812013-08-12 15:26:34 -07002653 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002654 if (interpolationQualifier == EvqSmooth)
2655 mergedQualifier = EvqSmoothIn;
2656 else if (interpolationQualifier == EvqFlat)
2657 mergedQualifier = EvqFlatIn;
2658 else UNREACHABLE();
2659 }
2660 else if (storageQualifier == EvqCentroidIn) {
2661 if (interpolationQualifier == EvqSmooth)
2662 mergedQualifier = EvqCentroidIn;
2663 else if (interpolationQualifier == EvqFlat)
2664 mergedQualifier = EvqFlatIn;
2665 else UNREACHABLE();
2666 }
Jamie Madill19571812013-08-12 15:26:34 -07002667 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002668 if (interpolationQualifier == EvqSmooth)
2669 mergedQualifier = EvqSmoothOut;
2670 else if (interpolationQualifier == EvqFlat)
2671 mergedQualifier = EvqFlatOut;
2672 else UNREACHABLE();
2673 }
2674 else if (storageQualifier == EvqCentroidOut) {
2675 if (interpolationQualifier == EvqSmooth)
2676 mergedQualifier = EvqCentroidOut;
2677 else if (interpolationQualifier == EvqFlat)
2678 mergedQualifier = EvqFlatOut;
2679 else UNREACHABLE();
2680 }
2681 else {
2682 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2683 recover();
2684
2685 mergedQualifier = storageQualifier;
2686 }
2687
2688 TPublicType type;
2689 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2690 return type;
2691}
2692
Jamie Madill98493dd2013-07-08 14:39:03 -04002693TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002694{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002695 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2696 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002697 recover();
2698 }
2699
Jamie Madill98493dd2013-07-08 14:39:03 -04002700 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002701 //
2702 // Careful not to replace already known aspects of type, like array-ness
2703 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002704 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002705 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002706 type->setPrimarySize(typeSpecifier.primarySize);
2707 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708 type->setPrecision(typeSpecifier.precision);
2709 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002710 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002711
2712 // don't allow arrays of arrays
2713 if (type->isArray()) {
2714 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2715 recover();
2716 }
2717 if (typeSpecifier.array)
2718 type->setArraySize(typeSpecifier.arraySize);
2719 if (typeSpecifier.userDef) {
2720 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002721 }
2722
Jamie Madill98493dd2013-07-08 14:39:03 -04002723 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002724 recover();
2725 }
2726 }
2727
Jamie Madill98493dd2013-07-08 14:39:03 -04002728 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002729}
2730
Jamie Madill98493dd2013-07-08 14:39:03 -04002731TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002732{
Jamie Madill98493dd2013-07-08 14:39:03 -04002733 TStructure* structure = new TStructure(structName, fieldList);
2734 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002735
Jamie Madill9b820842015-02-12 10:40:10 -05002736 // Store a bool in the struct if we're at global scope, to allow us to
2737 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002738 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002739 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002740
Jamie Madill98493dd2013-07-08 14:39:03 -04002741 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002742 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002743 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002744 {
2745 recover();
2746 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002747 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002748 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002749 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002750 recover();
2751 }
2752 }
2753
2754 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002755 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002756 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002757 const TField &field = *(*fieldList)[typeListIndex];
2758 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002759 switch (qualifier)
2760 {
2761 case EvqGlobal:
2762 case EvqTemporary:
2763 break;
2764 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002765 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002766 recover();
2767 break;
2768 }
2769 }
2770
2771 TPublicType publicType;
2772 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002773 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002774 exitStructDeclaration();
2775
2776 return publicType;
2777}
2778
Olli Etuahoa3a36662015-02-17 13:46:51 +02002779TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2780{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002781 TBasicType switchType = init->getBasicType();
2782 if ((switchType != EbtInt && switchType != EbtUInt) ||
2783 init->isMatrix() ||
2784 init->isArray() ||
2785 init->isVector())
2786 {
2787 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2788 recover();
2789 return nullptr;
2790 }
2791
Olli Etuahoac5274d2015-02-20 10:19:08 +02002792 if (statementList)
2793 {
2794 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2795 {
2796 recover();
2797 return nullptr;
2798 }
2799 }
2800
Olli Etuahoa3a36662015-02-17 13:46:51 +02002801 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2802 if (node == nullptr)
2803 {
2804 error(loc, "erroneous switch statement", "switch");
2805 recover();
2806 return nullptr;
2807 }
2808 return node;
2809}
2810
2811TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2812{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002813 if (mSwitchNestingLevel == 0)
2814 {
2815 error(loc, "case labels need to be inside switch statements", "case");
2816 recover();
2817 return nullptr;
2818 }
2819 if (condition == nullptr)
2820 {
2821 error(loc, "case label must have a condition", "case");
2822 recover();
2823 return nullptr;
2824 }
2825 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2826 condition->isMatrix() ||
2827 condition->isArray() ||
2828 condition->isVector())
2829 {
2830 error(condition->getLine(), "case label must be a scalar integer", "case");
2831 recover();
2832 }
2833 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2834 if (conditionConst == nullptr)
2835 {
2836 error(condition->getLine(), "case label must be constant", "case");
2837 recover();
2838 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002839 TIntermCase *node = intermediate.addCase(condition, loc);
2840 if (node == nullptr)
2841 {
2842 error(loc, "erroneous case statement", "case");
2843 recover();
2844 return nullptr;
2845 }
2846 return node;
2847}
2848
2849TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2850{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002851 if (mSwitchNestingLevel == 0)
2852 {
2853 error(loc, "default labels need to be inside switch statements", "default");
2854 recover();
2855 return nullptr;
2856 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002857 TIntermCase *node = intermediate.addCase(nullptr, loc);
2858 if (node == nullptr)
2859 {
2860 error(loc, "erroneous default statement", "default");
2861 recover();
2862 return nullptr;
2863 }
2864 return node;
2865}
2866
Olli Etuahof6c694b2015-03-26 14:50:53 +02002867TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2868 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002869{
2870 if (child == nullptr)
2871 {
2872 return nullptr;
2873 }
2874
2875 switch (op)
2876 {
2877 case EOpLogicalNot:
2878 if (child->getBasicType() != EbtBool ||
2879 child->isMatrix() ||
2880 child->isArray() ||
2881 child->isVector())
2882 {
2883 return nullptr;
2884 }
2885 break;
2886 case EOpBitwiseNot:
2887 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2888 child->isMatrix() ||
2889 child->isArray())
2890 {
2891 return nullptr;
2892 }
2893 break;
2894 case EOpPostIncrement:
2895 case EOpPreIncrement:
2896 case EOpPostDecrement:
2897 case EOpPreDecrement:
2898 case EOpNegative:
2899 case EOpPositive:
2900 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002901 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002902 child->isArray())
2903 {
2904 return nullptr;
2905 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002906 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002907 default:
2908 break;
2909 }
2910
Olli Etuahof6c694b2015-03-26 14:50:53 +02002911 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002912}
2913
Olli Etuaho09b22472015-02-11 11:47:26 +02002914TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2915{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002916 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002917 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002918 {
2919 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2920 recover();
2921 return child;
2922 }
2923 return node;
2924}
2925
2926TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2927{
2928 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2929 recover();
2930 return addUnaryMath(op, child, loc);
2931}
2932
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002933bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002934 const TSourceLoc &loc)
2935{
2936 if (left->isArray() || right->isArray())
2937 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002938 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02002939 {
2940 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2941 return false;
2942 }
2943
2944 if (left->isArray() != right->isArray())
2945 {
2946 error(loc, "array / non-array mismatch", GetOperatorString(op));
2947 return false;
2948 }
2949
2950 switch (op)
2951 {
2952 case EOpEqual:
2953 case EOpNotEqual:
2954 case EOpAssign:
2955 case EOpInitialize:
2956 break;
2957 default:
2958 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2959 return false;
2960 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002961 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002962 if (left->getArraySize() != right->getArraySize())
2963 {
2964 error(loc, "array size mismatch", GetOperatorString(op));
2965 return false;
2966 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002967 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002968
2969 // Check ops which require integer / ivec parameters
2970 bool isBitShift = false;
2971 switch (op)
2972 {
2973 case EOpBitShiftLeft:
2974 case EOpBitShiftRight:
2975 case EOpBitShiftLeftAssign:
2976 case EOpBitShiftRightAssign:
2977 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2978 // check that the basic type is an integer type.
2979 isBitShift = true;
2980 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2981 {
2982 return false;
2983 }
2984 break;
2985 case EOpBitwiseAnd:
2986 case EOpBitwiseXor:
2987 case EOpBitwiseOr:
2988 case EOpBitwiseAndAssign:
2989 case EOpBitwiseXorAssign:
2990 case EOpBitwiseOrAssign:
2991 // It is enough to check the type of only one operand, since later it
2992 // is checked that the operand types match.
2993 if (!IsInteger(left->getBasicType()))
2994 {
2995 return false;
2996 }
2997 break;
2998 default:
2999 break;
3000 }
3001
3002 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3003 // So the basic type should usually match.
3004 if (!isBitShift && left->getBasicType() != right->getBasicType())
3005 {
3006 return false;
3007 }
3008
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003009 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003010 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003011 switch(op)
3012 {
3013 case EOpAssign:
3014 case EOpInitialize:
3015 case EOpEqual:
3016 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003017 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003018 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003019 {
3020 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3021 return false;
3022 }
Olli Etuahoff699002015-03-23 14:38:42 +02003023 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3024 // we interpret the spec so that this extends to structs containing samplers,
3025 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003026 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003027 left->getType().isStructureContainingSamplers())
3028 {
3029 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3030 return false;
3031 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003032 case EOpLessThan:
3033 case EOpGreaterThan:
3034 case EOpLessThanEqual:
3035 case EOpGreaterThanEqual:
3036 if ((left->getNominalSize() != right->getNominalSize()) ||
3037 (left->getSecondarySize() != right->getSecondarySize()))
3038 {
3039 return false;
3040 }
3041 default:
3042 break;
3043 }
3044
Olli Etuahod6b14282015-03-17 14:31:35 +02003045 return true;
3046}
3047
Olli Etuahofc1806e2015-03-17 13:03:11 +02003048TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3049 const TSourceLoc &loc)
3050{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003051 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003052 return nullptr;
3053
Olli Etuahofc1806e2015-03-17 13:03:11 +02003054 switch (op)
3055 {
3056 case EOpEqual:
3057 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003058 break;
3059 case EOpLessThan:
3060 case EOpGreaterThan:
3061 case EOpLessThanEqual:
3062 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003063 ASSERT(!left->isArray() && !right->isArray());
3064 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003065 left->getBasicType() == EbtStruct)
3066 {
3067 return nullptr;
3068 }
3069 break;
3070 case EOpLogicalOr:
3071 case EOpLogicalXor:
3072 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003073 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003074 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003075 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003076 {
3077 return nullptr;
3078 }
3079 break;
3080 case EOpAdd:
3081 case EOpSub:
3082 case EOpDiv:
3083 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003084 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003085 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3086 {
3087 return nullptr;
3088 }
3089 break;
3090 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003091 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003092 // Note that this is only for the % operator, not for mod()
3093 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3094 {
3095 return nullptr;
3096 }
3097 break;
3098 // Note that for bitwise ops, type checking is done in promote() to
3099 // share code between ops and compound assignment
3100 default:
3101 break;
3102 }
3103
Olli Etuahofc1806e2015-03-17 13:03:11 +02003104 return intermediate.addBinaryMath(op, left, right, loc);
3105}
3106
Olli Etuaho09b22472015-02-11 11:47:26 +02003107TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3108 const TSourceLoc &loc)
3109{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003110 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003111 if (node == 0)
3112 {
3113 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3114 recover();
3115 return left;
3116 }
3117 return node;
3118}
3119
3120TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3121 const TSourceLoc &loc)
3122{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003123 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003124 if (node == 0)
3125 {
3126 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3127 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003128 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003129 unionArray->setBConst(false);
3130 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3131 }
3132 return node;
3133}
3134
Olli Etuahod6b14282015-03-17 14:31:35 +02003135TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3136 const TSourceLoc &loc)
3137{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003138 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003139 {
3140 return intermediate.addAssign(op, left, right, loc);
3141 }
3142 return nullptr;
3143}
3144
3145TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3146 const TSourceLoc &loc)
3147{
3148 TIntermTyped *node = createAssign(op, left, right, loc);
3149 if (node == nullptr)
3150 {
3151 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3152 recover();
3153 return left;
3154 }
3155 return node;
3156}
3157
Olli Etuaho49300862015-02-20 14:54:49 +02003158TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3159{
3160 switch (op)
3161 {
3162 case EOpContinue:
3163 if (mLoopNestingLevel <= 0)
3164 {
3165 error(loc, "continue statement only allowed in loops", "");
3166 recover();
3167 }
3168 break;
3169 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003170 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003171 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003172 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003173 recover();
3174 }
3175 break;
3176 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003177 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003178 {
3179 error(loc, "non-void function must return a value", "return");
3180 recover();
3181 }
3182 break;
3183 default:
3184 // No checks for discard
3185 break;
3186 }
3187 return intermediate.addBranch(op, loc);
3188}
3189
3190TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3191{
3192 ASSERT(op == EOpReturn);
3193 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003194 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003195 {
3196 error(loc, "void function cannot return a value", "return");
3197 recover();
3198 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003199 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003200 {
3201 error(loc, "function return is not matching type:", "return");
3202 recover();
3203 }
3204 return intermediate.addBranch(op, returnValue, loc);
3205}
3206
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003207TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3208 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003209{
3210 *fatalError = false;
3211 TOperator op = fnCall->getBuiltInOp();
3212 TIntermTyped *callNode = nullptr;
3213
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003214 if (thisNode != nullptr)
3215 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003216 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003217 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003218 TIntermTyped *typedThis = thisNode->getAsTyped();
3219 if (fnCall->getName() != "length")
3220 {
3221 error(loc, "invalid method", fnCall->getName().c_str());
3222 recover();
3223 }
3224 else if (paramNode != nullptr)
3225 {
3226 error(loc, "method takes no parameters", "length");
3227 recover();
3228 }
3229 else if (typedThis == nullptr || !typedThis->isArray())
3230 {
3231 error(loc, "length can only be called on arrays", "length");
3232 recover();
3233 }
3234 else
3235 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003236 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003237 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003238 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003239 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003240 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003241 // (func()).length()
3242 // (int[3](0, 1, 2)).length()
3243 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3244 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3245 // which allows "An array, vector or matrix expression with the length method applied".
3246 error(loc, "length can only be called on array names, not on array expressions", "length");
3247 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003248 }
3249 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003250 unionArray->setIConst(arraySize);
3251 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003252 }
3253 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003254 {
3255 //
3256 // Then this should be a constructor.
3257 // Don't go through the symbol table for constructors.
3258 // Their parameters will be verified algorithmically.
3259 //
3260 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003261 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003262 {
3263 //
3264 // It's a constructor, of type 'type'.
3265 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003266 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003267 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003268
3269 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003270 {
3271 recover();
3272 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3273 }
3274 callNode->setType(type);
3275 }
3276 else
3277 {
3278 //
3279 // Not a constructor. Find it in the symbol table.
3280 //
3281 const TFunction* fnCandidate;
3282 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003283 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003284 if (fnCandidate)
3285 {
3286 //
3287 // A declared function.
3288 //
3289 if (builtIn && !fnCandidate->getExtension().empty() &&
3290 extensionErrorCheck(loc, fnCandidate->getExtension()))
3291 {
3292 recover();
3293 }
3294 op = fnCandidate->getBuiltInOp();
3295 if (builtIn && op != EOpNull)
3296 {
3297 //
3298 // A function call mapped to a built-in operation.
3299 //
3300 if (fnCandidate->getParamCount() == 1)
3301 {
3302 //
3303 // Treat it like a built-in unary operator.
3304 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003305 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003306 if (callNode == nullptr)
3307 {
3308 std::stringstream extraInfoStream;
3309 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003310 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003311 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003312 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003313 *fatalError = true;
3314 return nullptr;
3315 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003316 }
3317 else
3318 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003319 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003320 aggregate->setType(fnCandidate->getReturnType());
3321 aggregate->setPrecisionFromChildren();
3322 callNode = aggregate;
3323
3324 // Some built-in functions have out parameters too.
3325 functionCallLValueErrorCheck(fnCandidate, aggregate);
3326 }
3327 }
3328 else
3329 {
3330 // This is a real function call
3331
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003332 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003333 aggregate->setType(fnCandidate->getReturnType());
3334
3335 // this is how we know whether the given function is a builtIn function or a user defined function
3336 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3337 // if builtIn == true, it's definitely a builtIn function with EOpNull
3338 if (!builtIn)
3339 aggregate->setUserDefined();
3340 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003341 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003342
3343 // This needs to happen after the name is set
3344 if (builtIn)
3345 aggregate->setBuiltInFunctionPrecision();
3346
3347 callNode = aggregate;
3348
3349 functionCallLValueErrorCheck(fnCandidate, aggregate);
3350 }
3351 }
3352 else
3353 {
3354 // error message was put out by findFunction()
3355 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003356 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003357 unionArray->setFConst(0.0f);
3358 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3359 recover();
3360 }
3361 }
3362 delete fnCall;
3363 return callNode;
3364}
3365
Olli Etuaho52901742015-04-15 13:42:45 +03003366TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3367 const TSourceLoc &loc)
3368{
3369 if (boolErrorCheck(loc, cond))
3370 recover();
3371
3372 if (trueBlock->getType() != falseBlock->getType())
3373 {
3374 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3375 recover();
3376 return falseBlock;
3377 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003378 // ESSL1 sections 5.2 and 5.7:
3379 // ESSL3 section 5.7:
3380 // Ternary operator is not among the operators allowed for structures/arrays.
3381 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3382 {
3383 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3384 recover();
3385 return falseBlock;
3386 }
Olli Etuaho52901742015-04-15 13:42:45 +03003387 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3388}
Olli Etuaho49300862015-02-20 14:54:49 +02003389
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003390//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003391// Parse an array of strings using yyparse.
3392//
3393// Returns 0 for success.
3394//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003395int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003396 TParseContext* context) {
3397 if ((count == 0) || (string == NULL))
3398 return 1;
3399
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003400 if (glslang_initialize(context))
3401 return 1;
3402
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003403 int error = glslang_scan(count, string, length, context);
3404 if (!error)
3405 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003406
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003407 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003408
alokp@chromium.org6b495712012-06-29 00:06:58 +00003409 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003410}
3411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003412
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003413