blob: 89ae6ff8a66f27a908c66b5eba353c5cd523d24f [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 Etuaho37ad4742015-04-27 13:18:50 +030015#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000016
alokp@chromium.org8b851c62012-06-15 16:25:11 +000017///////////////////////////////////////////////////////////////////////
18//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019// Sub- vector and matrix fields
20//
21////////////////////////////////////////////////////////////////////////
22
23//
24// Look at a '.' field selector string and change it into offsets
25// for a vector.
26//
Jamie Madill075edd82013-07-08 13:30:19 -040027bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000029 fields.num = (int) compString.size();
30 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000031 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000032 return false;
33 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000035 enum {
36 exyzw,
37 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000038 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000041 for (int i = 0; i < fields.num; ++i) {
42 switch (compString[i]) {
43 case 'x':
44 fields.offsets[i] = 0;
45 fieldSet[i] = exyzw;
46 break;
47 case 'r':
48 fields.offsets[i] = 0;
49 fieldSet[i] = ergba;
50 break;
51 case 's':
52 fields.offsets[i] = 0;
53 fieldSet[i] = estpq;
54 break;
55 case 'y':
56 fields.offsets[i] = 1;
57 fieldSet[i] = exyzw;
58 break;
59 case 'g':
60 fields.offsets[i] = 1;
61 fieldSet[i] = ergba;
62 break;
63 case 't':
64 fields.offsets[i] = 1;
65 fieldSet[i] = estpq;
66 break;
67 case 'z':
68 fields.offsets[i] = 2;
69 fieldSet[i] = exyzw;
70 break;
71 case 'b':
72 fields.offsets[i] = 2;
73 fieldSet[i] = ergba;
74 break;
75 case 'p':
76 fields.offsets[i] = 2;
77 fieldSet[i] = estpq;
78 break;
79
80 case 'w':
81 fields.offsets[i] = 3;
82 fieldSet[i] = exyzw;
83 break;
84 case 'a':
85 fields.offsets[i] = 3;
86 fieldSet[i] = ergba;
87 break;
88 case 'q':
89 fields.offsets[i] = 3;
90 fieldSet[i] = estpq;
91 break;
92 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000093 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000094 return false;
95 }
96 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000098 for (int i = 0; i < fields.num; ++i) {
99 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000100 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000101 return false;
102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 if (i > 0) {
105 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000106 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000107 return false;
108 }
109 }
110 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113}
114
115
116//
117// Look at a '.' field selector string and change it into offsets
118// for a matrix.
119//
Jamie Madill075edd82013-07-08 13:30:19 -0400120bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000122 fields.wholeRow = false;
123 fields.wholeCol = false;
124 fields.row = -1;
125 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000128 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000129 return false;
130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000132 if (compString[0] == '_') {
133 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000134 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000135 return false;
136 }
137 fields.wholeCol = true;
138 fields.col = compString[1] - '0';
139 } else if (compString[1] == '_') {
140 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000141 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000142 return false;
143 }
144 fields.wholeRow = true;
145 fields.row = compString[0] - '0';
146 } else {
147 if (compString[0] < '0' || compString[0] > '3' ||
148 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000149 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000150 return false;
151 }
152 fields.row = compString[0] - '0';
153 fields.col = compString[1] - '0';
154 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000156 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000157 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000158 return false;
159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000161 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162}
163
164///////////////////////////////////////////////////////////////////////
165//
166// Errors
167//
168////////////////////////////////////////////////////////////////////////
169
170//
171// Track whether errors have occurred.
172//
173void TParseContext::recover()
174{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175}
176
177//
178// Used by flex/bison to output all syntax and parsing errors.
179//
Jamie Madill075edd82013-07-08 13:30:19 -0400180void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000181 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000182 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000184 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400185 srcLoc.file = loc.first_file;
186 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500187 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000188 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190}
191
Jamie Madill075edd82013-07-08 13:30:19 -0400192void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000193 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000194 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000195 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400196 srcLoc.file = loc.first_file;
197 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500198 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000199 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000200}
201
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202//
203// Same error message for all places assignments don't work.
204//
Jamie Madill075edd82013-07-08 13:30:19 -0400205void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000207 std::stringstream extraInfoStream;
208 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
209 std::string extraInfo = extraInfoStream.str();
210 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
213//
214// Same error message for all places unary operations don't work.
215//
Jamie Madill075edd82013-07-08 13:30:19 -0400216void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000218 std::stringstream extraInfoStream;
219 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
220 << " (or there is no acceptable conversion)";
221 std::string extraInfo = extraInfoStream.str();
222 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223}
224
225//
226// Same error message for all binary operations don't work.
227//
Jamie Madill075edd82013-07-08 13:30:19 -0400228void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000230 std::stringstream extraInfoStream;
231 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
232 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
233 std::string extraInfo = extraInfoStream.str();
234 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
Jamie Madill075edd82013-07-08 13:30:19 -0400237bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000238 if (!checksPrecisionErrors)
239 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000240 switch( type ){
241 case EbtFloat:
242 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000243 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000244 return true;
245 }
246 break;
247 case EbtInt:
248 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000249 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000250 return true;
251 }
252 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000253 default:
254 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000255 }
256 return false;
257}
258
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259//
260// Both test and if necessary, spit out an error, to see if the node is really
261// an l-value that can be operated on this way.
262//
263// Returns true if the was an error.
264//
Jamie Madill075edd82013-07-08 13:30:19 -0400265bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000267 TIntermSymbol* symNode = node->getAsSymbolNode();
268 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 if (binaryNode) {
271 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 switch(binaryNode->getOp()) {
274 case EOpIndexDirect:
275 case EOpIndexIndirect:
276 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000277 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000278 return lValueErrorCheck(line, op, binaryNode->getLeft());
279 case EOpVectorSwizzle:
280 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
281 if (!errorReturn) {
282 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000284 TIntermTyped* rightNode = binaryNode->getRight();
285 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700286
287 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
288 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000289 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700290 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000291 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000292 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 return true;
295 }
296 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700300 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000301 break;
302 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000303 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000305 return true;
306 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
308
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 const char* symbol = 0;
310 if (symNode != 0)
311 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 const char* message = 0;
314 switch (node->getQualifier()) {
315 case EvqConst: message = "can't modify a const"; break;
316 case EvqConstReadOnly: message = "can't modify a const"; break;
317 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700318 case EvqFragmentIn: message = "can't modify an input"; break;
319 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000320 case EvqUniform: message = "can't modify a uniform"; break;
321 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
323 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
324 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
325 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000327 //
328 // Type that can't be written to?
329 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400330 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400332 }
333 if (IsSampler(node->getBasicType())) {
334 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000335 }
336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000339 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 return true;
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 //
346 // Everything else is okay, no error.
347 //
348 if (message == 0)
349 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 //
352 // If we get here, we have an error and a message.
353 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000354 if (symNode) {
355 std::stringstream extraInfoStream;
356 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
357 std::string extraInfo = extraInfoStream.str();
358 error(line, " l-value required", op, extraInfo.c_str());
359 }
360 else {
361 std::stringstream extraInfoStream;
362 extraInfoStream << "(" << message << ")";
363 std::string extraInfo = extraInfoStream.str();
364 error(line, " l-value required", op, extraInfo.c_str());
365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000367 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
370//
371// Both test, and if necessary spit out an error, to see if the node is really
372// a constant.
373//
374// Returns true if the was an error.
375//
376bool TParseContext::constErrorCheck(TIntermTyped* node)
377{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000378 if (node->getQualifier() == EvqConst)
379 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384}
385
386//
387// Both test, and if necessary spit out an error, to see if the node is really
388// an integer.
389//
390// Returns true if the was an error.
391//
392bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
393{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000394 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000395 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000397 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400}
401
402//
403// Both test, and if necessary spit out an error, to see if we are currently
404// globally scoped.
405//
406// Returns true if the was an error.
407//
Jamie Madill075edd82013-07-08 13:30:19 -0400408bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000410 if (global)
411 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000413 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
418//
419// For now, keep it simple: if it starts "gl_", it's reserved, independent
420// of scope. Except, if the symbol table is at the built-in push-level,
421// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000422// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
423// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424//
425// Returns true if there was an error.
426//
Jamie Madill075edd82013-07-08 13:30:19 -0400427bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000429 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000431 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000432 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 return true;
434 }
Jamie Madill5508f392014-02-20 13:31:36 -0500435 if (IsWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000436 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000437 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000438 return true;
439 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000440 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000441 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442 return true;
443 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000444 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000445 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000446 return true;
447 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000448 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000449 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000450 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000451 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 }
453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456}
457
458//
459// Make sure there is enough data provided to the constructor to build
460// something of the type of the constructor. Also returns the type of
461// the constructor.
462//
463// Returns true if there was an error in construction.
464//
Jamie Madill075edd82013-07-08 13:30:19 -0400465bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000467 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 bool constructingMatrix = false;
470 switch(op) {
471 case EOpConstructMat2:
472 case EOpConstructMat3:
473 case EOpConstructMat4:
474 constructingMatrix = true;
475 break;
476 default:
477 break;
478 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000480 //
481 // Note: It's okay to have too many components available, but not okay to have unused
482 // arguments. 'full' will go to true when enough args have been seen. If we loop
483 // again, there is an extra argument, so 'overfull' will become true.
484 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
Jamie Madill94bf7f22013-07-08 13:31:15 -0400486 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 bool constType = true;
488 bool full = false;
489 bool overFull = false;
490 bool matrixInMatrix = false;
491 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000492 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000493 const TParameter& param = function.getParam(i);
494 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000495
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000496 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497 matrixInMatrix = true;
498 if (full)
499 overFull = true;
500 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
501 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000502 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000504 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 arrayArg = true;
506 }
507
508 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000509 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510
Olli Etuaho376f1b52015-04-13 13:23:41 +0300511 if (type->isArray())
512 {
513 if (type->isUnsizedArray())
514 {
515 type->setArraySize(function.getParamCount());
516 }
517 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
518 {
519 error(line, "array constructor needs one argument per array element", "constructor");
520 return true;
521 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000525 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 return true;
527 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000530 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000531 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000532 return true;
533 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000537 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 return true;
539 }
540
Brendan Longeaa84062013-12-08 18:26:50 +0100541 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000542 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 return true;
544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000546 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000547 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
548 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000549 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000550 return true;
551 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000554 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000556 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000557 return true;
558 }
559 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000560 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 return true;
562 }
563 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000564 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 return true;
566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569}
570
571// This function checks to see if a void variable has been declared and raise an error message for such a case
572//
573// returns true in case of an error
574//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300575bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300577 if (type == EbtVoid)
578 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000579 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000583 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584}
585
586// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
587//
588// returns true in case of an error
589//
Jamie Madill075edd82013-07-08 13:30:19 -0400590bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000593 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000597 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598}
599
600// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
601//
602// returns true in case of an error
603//
Jamie Madill075edd82013-07-08 13:30:19 -0400604bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000606 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000607 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 return true;
609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612}
613
Jamie Madill075edd82013-07-08 13:30:19 -0400614bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 if (pType.type == EbtStruct) {
617 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000618 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619
620 return true;
621 }
622
623 return false;
624 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000625 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 return true;
628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madill075edd82013-07-08 13:30:19 -0400633bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400634{
635 if (pType.layoutQualifier.location != -1)
636 {
637 error(line, "location must only be specified for a single input or output variable", "location");
638 return true;
639 }
640
641 return false;
642}
643
Jamie Madill075edd82013-07-08 13:30:19 -0400644bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
647 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
655bool TParseContext::containsSampler(TType& type)
656{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 if (IsSampler(type.getBasicType()))
658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
Jamie Madill98493dd2013-07-08 14:39:03 -0400660 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
661 const TFieldList& fields = type.getStruct()->fields();
662 for (unsigned int i = 0; i < fields.size(); ++i) {
663 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return true;
665 }
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671//
672// Do size checking for an array type's size.
673//
674// Returns true if there was an error.
675//
Jamie Madill075edd82013-07-08 13:30:19 -0400676bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000679
Olli Etuahoe7847b02015-03-16 11:56:12 +0200680 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000681 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000682 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200683 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 return true;
685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
Nicolas Capens906744a2014-06-06 15:18:07 -0400687 unsigned int unsignedSize = 0;
688
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000689 if (constant->getBasicType() == EbtUInt)
690 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400691 unsignedSize = constant->getUConst(0);
692 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000693 }
694 else
695 {
696 size = constant->getIConst(0);
697
Nicolas Capens906744a2014-06-06 15:18:07 -0400698 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000699 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400700 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000701 size = 1;
702 return true;
703 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400704
705 unsignedSize = static_cast<unsigned int>(size);
706 }
707
708 if (size == 0)
709 {
710 error(line, "array size must be greater than zero", "");
711 size = 1;
712 return true;
713 }
714
715 // The size of arrays is restricted here to prevent issues further down the
716 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
717 // 4096 registers so this should be reasonable even for aggressively optimizable code.
718 const unsigned int sizeLimit = 65536;
719
720 if (unsignedSize > sizeLimit)
721 {
722 error(line, "array size too large", "");
723 size = 1;
724 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000727 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728}
729
730//
731// See if this qualifier can be an array.
732//
733// Returns true if there is an error.
734//
Olli Etuaho3739d232015-04-08 12:23:44 +0300735bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736{
Olli Etuaho3739d232015-04-08 12:23:44 +0300737 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
738 (type.qualifier == EvqConst && shaderVersion < 300))
739 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000740 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return true;
742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
747//
748// See if this type can be an array.
749//
750// Returns true if there is an error.
751//
Jamie Madill075edd82013-07-08 13:30:19 -0400752bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000754 //
755 // Can the type be an array?
756 //
757 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000758 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return true;
760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
765//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766// Enforce non-initializer type/qualifier rules.
767//
768// Returns true if there was an error.
769//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300770bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771{
Olli Etuaho3739d232015-04-08 12:23:44 +0300772 ASSERT(type != nullptr);
773 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000774 {
775 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300776 type->qualifier = EvqTemporary;
777
778 // Generate informative error messages for ESSL1.
779 // In ESSL3 arrays and structures containing arrays can be constant.
780 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000781 {
782 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
783 }
784 else
785 {
786 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
787 }
788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 return true;
790 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300791 if (type->isUnsizedArray())
792 {
793 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
794 return true;
795 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
Olli Etuaho2935c582015-04-08 14:32:06 +0300799// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800// and update the symbol table.
801//
Olli Etuaho2935c582015-04-08 14:32:06 +0300802// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803//
Olli Etuaho2935c582015-04-08 14:32:06 +0300804bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
805 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806{
Olli Etuaho2935c582015-04-08 14:32:06 +0300807 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
Olli Etuaho2935c582015-04-08 14:32:06 +0300809 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
Olli Etuaho2935c582015-04-08 14:32:06 +0300811 // gl_LastFragData may be redeclared with a new precision qualifier
812 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
813 {
814 const TVariable *maxDrawBuffers =
815 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion));
816 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
817 {
818 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion))
819 {
820 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
821 }
822 }
823 else
824 {
825 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
826 return false;
827 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829
Olli Etuaho2935c582015-04-08 14:32:06 +0300830 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
831 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
Olli Etuaho2935c582015-04-08 14:32:06 +0300833 (*variable) = new TVariable(&identifier, type);
834 if (!symbolTable.declare(*variable))
835 {
836 error(line, "redefinition", identifier.c_str());
837 delete (*variable);
838 (*variable) = nullptr;
839 return false;
840 }
841
842 if (voidErrorCheck(line, identifier, type.getBasicType()))
843 return false;
844
845 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
Jamie Madill075edd82013-07-08 13:30:19 -0400848bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000851 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 return true;
853 }
854 if (qualifier == EvqConst && paramQualifier != EvqIn) {
855 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
856 return true;
857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000859 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000860 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000862 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
Jamie Madill075edd82013-07-08 13:30:19 -0400867bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000868{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000869 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000870 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
871 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000872 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 return true;
874 }
zmo@google.comf5450912011-09-09 01:37:19 +0000875 // In GLSL ES, an extension's default behavior is "disable".
876 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000877 error(line, "extension", extension.c_str(), "is disabled");
878 return true;
879 }
880 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000881 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000882 return false;
883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
Olli Etuahofa33d582015-04-09 14:33:12 +0300888// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
889// declaration.
890//
891bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400892{
Olli Etuahofa33d582015-04-09 14:33:12 +0300893 switch (publicType.qualifier)
894 {
895 case EvqVaryingIn:
896 case EvqVaryingOut:
897 case EvqAttribute:
898 case EvqVertexIn:
899 case EvqFragmentOut:
900 if (publicType.type == EbtStruct)
901 {
902 error(identifierLocation, "cannot be used with a structure",
903 getQualifierString(publicType.qualifier));
904 return true;
905 }
906
907 default: break;
908 }
909
910 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
911 "samplers must be uniform"))
912 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400913 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300914 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400915
916 // check for layout qualifier issues
917 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
918
919 if (layoutQualifier.matrixPacking != EmpUnspecified)
920 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300921 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
922 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400923 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400924 }
925
926 if (layoutQualifier.blockStorage != EbsUnspecified)
927 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300928 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
929 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400930 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400931 }
932
Olli Etuahofa33d582015-04-09 14:33:12 +0300933 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
934 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400935 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400936 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400937 }
938
939 return false;
940}
941
Jamie Madill075edd82013-07-08 13:30:19 -0400942bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400943{
944 if (layoutQualifier.location != -1)
945 {
946 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
947 return true;
948 }
949
950 return false;
951}
952
Olli Etuahob6e07a62015-02-16 12:22:10 +0200953bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
954{
955 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
956 {
957 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
958 if (qual == EvqOut || qual == EvqInOut)
959 {
960 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
961 if (lValueErrorCheck(node->getLine(), "assign", node))
962 {
963 error(node->getLine(),
964 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
965 recover();
966 return true;
967 }
968 }
969 }
970 return false;
971}
972
Olli Etuaho37ad4742015-04-27 13:18:50 +0300973void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
974{
975 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
976 {
977 error(invariantLocation, "Only out variables can be invariant.", "invariant");
978 recover();
979 }
980}
981
zmo@google.com09c323a2011-08-12 18:22:25 +0000982bool TParseContext::supportsExtension(const char* extension)
983{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000984 const TExtensionBehavior& extbehavior = extensionBehavior();
985 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
986 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000987}
988
Jamie Madill5d287f52013-07-12 15:38:19 -0400989bool TParseContext::isExtensionEnabled(const char* extension) const
990{
991 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400992 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400993
994 if (iter == extbehavior.end())
995 {
996 return false;
997 }
998
999 return (iter->second == EBhEnable || iter->second == EBhRequire);
1000}
1001
Jamie Madill075edd82013-07-08 13:30:19 -04001002void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1003{
1004 pp::SourceLocation srcLoc;
1005 srcLoc.file = loc.first_file;
1006 srcLoc.line = loc.first_line;
1007 directiveHandler.handleExtension(srcLoc, extName, behavior);
1008}
1009
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001010void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001011{
1012 pp::SourceLocation srcLoc;
1013 srcLoc.file = loc.first_file;
1014 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001015 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001016}
1017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018/////////////////////////////////////////////////////////////////////////////////
1019//
1020// Non-Errors.
1021//
1022/////////////////////////////////////////////////////////////////////////////////
1023
Jamie Madill5c097022014-08-20 16:38:32 -04001024const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1025 const TString *name,
1026 const TSymbol *symbol)
1027{
1028 const TVariable *variable = NULL;
1029
1030 if (!symbol)
1031 {
1032 error(location, "undeclared identifier", name->c_str());
1033 recover();
1034 }
1035 else if (!symbol->isVariable())
1036 {
1037 error(location, "variable expected", name->c_str());
1038 recover();
1039 }
1040 else
1041 {
1042 variable = static_cast<const TVariable*>(symbol);
1043
1044 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1045 !variable->getExtension().empty() &&
1046 extensionErrorCheck(location, variable->getExtension()))
1047 {
1048 recover();
1049 }
1050 }
1051
1052 if (!variable)
1053 {
1054 TType type(EbtFloat, EbpUndefined);
1055 TVariable *fakeVariable = new TVariable(name, type);
1056 symbolTable.declare(fakeVariable);
1057 variable = fakeVariable;
1058 }
1059
1060 return variable;
1061}
1062
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063//
1064// Look up a function name in the symbol table, and make sure it is a function.
1065//
1066// Return the function symbol if found, otherwise 0.
1067//
Austin Kinross3ae64652015-01-26 15:51:39 -08001068const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001070 // First find by unmangled name to check whether the function name has been
1071 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001072 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001073 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001074 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001075 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
alokp@chromium.org0a576182010-08-09 17:16:27 +00001078 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001079 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 return 0;
1081 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082
alokp@chromium.org0a576182010-08-09 17:16:27 +00001083 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001084 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 return 0;
1086 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001087
1088 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089}
1090
1091//
1092// Initializers show up in several places in the grammar. Have one set of
1093// code to handle them here.
1094//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001095// Returns true on error, false if no error
1096//
Olli Etuaho2935c582015-04-08 14:32:06 +03001097bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001098 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001100 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001104 if (type.isUnsizedArray())
1105 {
1106 type.setArraySize(initializer->getArraySize());
1107 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001108 if (!declareVariable(line, identifier, type, &variable))
1109 {
1110 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 //
1114 // identifier must be of type constant, a global, or a temporary
1115 //
1116 TQualifier qualifier = variable->getType().getQualifier();
1117 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001118 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 return true;
1120 }
1121 //
1122 // test for and propagate constant
1123 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 if (qualifier == EvqConst) {
1126 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001127 std::stringstream extraInfoStream;
1128 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1129 std::string extraInfo = extraInfoStream.str();
1130 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001131 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 return true;
1133 }
1134 if (type != initializer->getType()) {
1135 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001136 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001137 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 return true;
1139 }
1140 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001141 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001142 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001143 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001144 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001145
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001146 TConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001147 variable->shareConstPointer(constArray);
1148 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001149 std::stringstream extraInfoStream;
1150 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1151 std::string extraInfo = extraInfoStream.str();
1152 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001153 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001154 return true;
1155 }
1156 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001157
1158 if (qualifier != EvqConst)
1159 {
1160 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1161 variable->getType(), line);
1162 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1163 if (*intermNode == nullptr)
1164 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1166 return true;
1167 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001168 }
1169 else
1170 {
1171 *intermNode = nullptr;
1172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001174 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175}
1176
1177bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1178{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001179 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 if (!aggrNode->isConstructor())
1181 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001183 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 // check if all the child nodes are constants so that they can be inserted into
1186 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001187 TIntermSequence *sequence = aggrNode->getSequence() ;
1188 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001189 if (!(*p)->getAsTyped()->getAsConstantUnion())
1190 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001191 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001192
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001193 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194}
1195
Olli Etuaho214c2d82015-04-27 14:49:13 +03001196TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
1197 const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001198{
1199 TPublicType returnType = typeSpecifier;
1200 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001201 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001202 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001203
1204 if (typeSpecifier.array)
1205 {
1206 error(typeSpecifier.line, "not supported", "first-class array");
1207 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001208 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001209 }
1210
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001211 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001212 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001213 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1214 {
1215 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1216 recover();
1217 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001218
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001219 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1220 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1221 {
1222 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1223 recover();
1224 }
1225 }
1226 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001227 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001228 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001229 {
Jamie Madill19571812013-08-12 15:26:34 -07001230 case EvqSmoothIn:
1231 case EvqSmoothOut:
1232 case EvqVertexOut:
1233 case EvqFragmentIn:
1234 case EvqCentroidOut:
1235 case EvqCentroidIn:
1236 if (typeSpecifier.type == EbtBool)
1237 {
1238 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1239 recover();
1240 }
1241 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1242 {
1243 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1244 recover();
1245 }
1246 break;
1247
1248 case EvqVertexIn:
1249 case EvqFragmentOut:
1250 case EvqFlatIn:
1251 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001252 if (typeSpecifier.type == EbtBool)
1253 {
1254 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1255 recover();
1256 }
1257 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001258
Jamie Madillb120eac2013-06-12 14:08:13 -04001259 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001260 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001261 }
1262
1263 return returnType;
1264}
1265
Olli Etuahofa33d582015-04-09 14:33:12 +03001266TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1267 const TSourceLoc &identifierOrTypeLocation,
1268 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001269{
Olli Etuahofa33d582015-04-09 14:33:12 +03001270 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001271
Olli Etuahobab4c082015-04-24 16:38:49 +03001272 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001273
Olli Etuahobab4c082015-04-24 16:38:49 +03001274 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1275
1276 if (emptyDeclaration)
1277 {
1278 if (publicType.isUnsizedArray())
1279 {
1280 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1281 // It is assumed that this applies to empty declarations as well.
1282 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1283 }
1284 }
1285 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001286 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001287 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001288 recover();
1289
Olli Etuaho376f1b52015-04-13 13:23:41 +03001290 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001291 recover();
1292
Olli Etuaho2935c582015-04-08 14:32:06 +03001293 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001294 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001295 recover();
1296
1297 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001298 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001299 }
1300
Olli Etuahoe7847b02015-03-16 11:56:12 +02001301 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001302}
1303
Olli Etuahoe7847b02015-03-16 11:56:12 +02001304TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1305 const TSourceLoc &identifierLocation,
1306 const TString &identifier,
1307 const TSourceLoc &indexLocation,
1308 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001309{
Olli Etuahofa33d582015-04-09 14:33:12 +03001310 mDeferredSingleDeclarationErrorCheck = false;
1311
1312 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001313 recover();
1314
Olli Etuaho376f1b52015-04-13 13:23:41 +03001315 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001316 recover();
1317
1318 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1319 {
1320 recover();
1321 }
1322
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001323 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001324
1325 int size;
1326 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1327 {
1328 recover();
1329 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001330 // Make the type an array even if size check failed.
1331 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1332 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001333
Olli Etuaho2935c582015-04-08 14:32:06 +03001334 TVariable *variable = nullptr;
1335 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001336 recover();
1337
Olli Etuahoe7847b02015-03-16 11:56:12 +02001338 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001339 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001340 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001341
Olli Etuahoe7847b02015-03-16 11:56:12 +02001342 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001343}
1344
Olli Etuahoe7847b02015-03-16 11:56:12 +02001345TIntermAggregate *TParseContext::parseSingleInitDeclaration(TPublicType &publicType,
1346 const TSourceLoc &identifierLocation,
1347 const TString &identifier,
1348 const TSourceLoc &initLocation,
1349 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001350{
Olli Etuahofa33d582015-04-09 14:33:12 +03001351 mDeferredSingleDeclarationErrorCheck = false;
1352
1353 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001354 recover();
1355
Olli Etuahoe7847b02015-03-16 11:56:12 +02001356 TIntermNode *intermNode = nullptr;
1357 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001358 {
1359 //
1360 // Build intermediate representation
1361 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001362 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001363 }
1364 else
1365 {
1366 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001367 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001368 }
1369}
1370
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001371TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1372 const TSourceLoc &identifierLocation,
1373 const TString &identifier,
1374 const TSourceLoc &indexLocation,
1375 TIntermTyped *indexExpression,
1376 const TSourceLoc &initLocation,
1377 TIntermTyped *initializer)
1378{
1379 mDeferredSingleDeclarationErrorCheck = false;
1380
1381 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1382 recover();
1383
1384 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1385 {
1386 recover();
1387 }
1388
1389 TPublicType arrayType(publicType);
1390
Olli Etuaho376f1b52015-04-13 13:23:41 +03001391 int size = 0;
1392 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1393 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001394 {
1395 recover();
1396 }
1397 // Make the type an array even if size check failed.
1398 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1399 arrayType.setArraySize(size);
1400
1401 // initNode will correspond to the whole of "type b[n] = initializer".
1402 TIntermNode *initNode = nullptr;
1403 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1404 {
1405 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1406 }
1407 else
1408 {
1409 recover();
1410 return nullptr;
1411 }
1412}
1413
Olli Etuahoe7847b02015-03-16 11:56:12 +02001414TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001415 const TSourceLoc &identifierLoc,
1416 const TString *identifier,
1417 const TSymbol *symbol)
1418{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001419 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001420 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1421 {
1422 recover();
1423 }
1424
1425 if (!symbol)
1426 {
1427 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1428 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001429 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001430 }
1431 else
1432 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001433 const TString kGlFrontFacing("gl_FrontFacing");
1434 if (*identifier == kGlFrontFacing)
1435 {
1436 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1437 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001438 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001439 }
Jamie Madill2c433252014-12-03 12:36:54 -05001440 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001441 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1442 ASSERT(variable);
1443 const TType &type = variable->getType();
1444 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1445 *identifier, type, identifierLoc);
1446
1447 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1448 aggregate->setOp(EOpInvariantDeclaration);
1449 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001450 }
1451}
1452
Olli Etuahoe7847b02015-03-16 11:56:12 +02001453TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1454 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001455{
Olli Etuahofa33d582015-04-09 14:33:12 +03001456 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1457 if (mDeferredSingleDeclarationErrorCheck)
1458 {
1459 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1460 recover();
1461 mDeferredSingleDeclarationErrorCheck = false;
1462 }
1463
Jamie Madill0bd18df2013-06-20 11:55:52 -04001464 if (locationDeclaratorListCheck(identifierLocation, publicType))
1465 recover();
1466
Olli Etuaho376f1b52015-04-13 13:23:41 +03001467 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001468 recover();
1469
Olli Etuaho2935c582015-04-08 14:32:06 +03001470 TVariable *variable = nullptr;
1471 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001472 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001473
1474 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1475 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001476 symbol->setId(variable->getUniqueId());
1477
Olli Etuahoe7847b02015-03-16 11:56:12 +02001478 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001479}
1480
Olli Etuahoe7847b02015-03-16 11:56:12 +02001481TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1482 const TSourceLoc &identifierLocation, const TString &identifier,
1483 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001484{
Olli Etuahofa33d582015-04-09 14:33:12 +03001485 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1486 if (mDeferredSingleDeclarationErrorCheck)
1487 {
1488 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1489 recover();
1490 mDeferredSingleDeclarationErrorCheck = false;
1491 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001492
Jamie Madill0bd18df2013-06-20 11:55:52 -04001493 if (locationDeclaratorListCheck(identifierLocation, publicType))
1494 recover();
1495
Olli Etuaho376f1b52015-04-13 13:23:41 +03001496 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001497 recover();
1498
1499 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1500 {
1501 recover();
1502 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001503 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001504 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001505 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001506 int size;
1507 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001508 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001509 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001510 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001511 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001512
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001513 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001514 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001515 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001516
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001517 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1518 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001519 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001520
1521 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001522 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001523
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001524 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001525}
1526
Olli Etuahoe7847b02015-03-16 11:56:12 +02001527TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1528 const TSourceLoc &identifierLocation, const TString &identifier,
1529 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001530{
Olli Etuahofa33d582015-04-09 14:33:12 +03001531 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1532 if (mDeferredSingleDeclarationErrorCheck)
1533 {
1534 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1535 recover();
1536 mDeferredSingleDeclarationErrorCheck = false;
1537 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001538
Jamie Madill0bd18df2013-06-20 11:55:52 -04001539 if (locationDeclaratorListCheck(identifierLocation, publicType))
1540 recover();
1541
Olli Etuahoe7847b02015-03-16 11:56:12 +02001542 TIntermNode *intermNode = nullptr;
1543 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001544 {
1545 //
1546 // build the intermediate representation
1547 //
1548 if (intermNode)
1549 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001550 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001551 }
1552 else
1553 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001554 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001555 }
1556 }
1557 else
1558 {
1559 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001560 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001561 }
1562}
1563
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001564TIntermAggregate *TParseContext::parseArrayInitDeclarator(TPublicType &publicType,
1565 TIntermAggregate *aggregateDeclaration,
1566 const TSourceLoc& identifierLocation,
1567 const TString &identifier,
1568 const TSourceLoc& indexLocation,
1569 TIntermTyped *indexExpression,
1570 const TSourceLoc &initLocation, TIntermTyped *initializer)
1571{
1572 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1573 if (mDeferredSingleDeclarationErrorCheck)
1574 {
1575 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1576 recover();
1577 mDeferredSingleDeclarationErrorCheck = false;
1578 }
1579
1580 if (locationDeclaratorListCheck(identifierLocation, publicType))
1581 recover();
1582
1583 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1584 {
1585 recover();
1586 }
1587
1588 TPublicType arrayType(publicType);
1589
Olli Etuaho376f1b52015-04-13 13:23:41 +03001590 int size = 0;
1591 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1592 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001593 {
1594 recover();
1595 }
1596 // Make the type an array even if size check failed.
1597 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1598 arrayType.setArraySize(size);
1599
1600 // initNode will correspond to the whole of "b[n] = initializer".
1601 TIntermNode *initNode = nullptr;
1602 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1603 {
1604 if (initNode)
1605 {
1606 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1607 }
1608 else
1609 {
1610 return aggregateDeclaration;
1611 }
1612 }
1613 else
1614 {
1615 recover();
1616 return nullptr;
1617 }
1618}
1619
Jamie Madilla295edf2013-06-06 11:56:48 -04001620void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1621{
1622 if (typeQualifier.qualifier != EvqUniform)
1623 {
1624 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1625 recover();
1626 return;
1627 }
1628
1629 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1630 ASSERT(!layoutQualifier.isEmpty());
1631
1632 if (shaderVersion < 300)
1633 {
1634 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1635 recover();
1636 return;
1637 }
1638
1639 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1640 {
1641 recover();
1642 return;
1643 }
1644
Jamie Madill099c0f32013-06-20 11:55:52 -04001645 if (layoutQualifier.matrixPacking != EmpUnspecified)
1646 {
1647 defaultMatrixPacking = layoutQualifier.matrixPacking;
1648 }
1649
Geoff Langc6856732014-02-11 09:38:55 -05001650 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001651 {
1652 defaultBlockStorage = layoutQualifier.blockStorage;
1653 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001654}
1655
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001656TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1657{
1658 TOperator op = EOpNull;
1659 if (publicType.userDef)
1660 {
1661 op = EOpConstructStruct;
1662 }
1663 else
1664 {
1665 switch (publicType.type)
1666 {
1667 case EbtFloat:
1668 if (publicType.isMatrix())
1669 {
1670 // TODO: non-square matrices
1671 switch(publicType.getCols())
1672 {
Jamie Madill28b97422013-07-08 14:01:38 -04001673 case 2: op = EOpConstructMat2; break;
1674 case 3: op = EOpConstructMat3; break;
1675 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001676 }
1677 }
1678 else
1679 {
1680 switch(publicType.getNominalSize())
1681 {
Jamie Madill28b97422013-07-08 14:01:38 -04001682 case 1: op = EOpConstructFloat; break;
1683 case 2: op = EOpConstructVec2; break;
1684 case 3: op = EOpConstructVec3; break;
1685 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001686 }
1687 }
1688 break;
1689
1690 case EbtInt:
1691 switch(publicType.getNominalSize())
1692 {
Jamie Madill28b97422013-07-08 14:01:38 -04001693 case 1: op = EOpConstructInt; break;
1694 case 2: op = EOpConstructIVec2; break;
1695 case 3: op = EOpConstructIVec3; break;
1696 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001697 }
1698 break;
1699
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001700 case EbtUInt:
1701 switch(publicType.getNominalSize())
1702 {
Jamie Madill28b97422013-07-08 14:01:38 -04001703 case 1: op = EOpConstructUInt; break;
1704 case 2: op = EOpConstructUVec2; break;
1705 case 3: op = EOpConstructUVec3; break;
1706 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001707 }
1708 break;
1709
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001710 case EbtBool:
1711 switch(publicType.getNominalSize())
1712 {
Jamie Madill28b97422013-07-08 14:01:38 -04001713 case 1: op = EOpConstructBool; break;
1714 case 2: op = EOpConstructBVec2; break;
1715 case 3: op = EOpConstructBVec3; break;
1716 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001717 }
1718 break;
1719
1720 default: break;
1721 }
1722
1723 if (op == EOpNull)
1724 {
1725 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1726 recover();
1727 publicType.type = EbtFloat;
1728 op = EOpConstructFloat;
1729 }
1730 }
1731
1732 TString tempString;
1733 TType type(publicType);
1734 return new TFunction(&tempString, type, op);
1735}
1736
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737// This function is used to test for the correctness of the parameters passed to various constructor functions
1738// and also convert them to the right datatype if it is allowed and required.
1739//
1740// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1741//
Olli Etuaho21203702014-11-13 16:16:21 +02001742TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001744 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001746 if (!aggregateArguments)
1747 {
1748 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001749 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001750 }
1751
Olli Etuahof40319e2015-03-10 14:33:00 +02001752 if (type->isArray())
1753 {
1754 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1755 TIntermSequence *args = aggregateArguments->getSequence();
1756 for (size_t i = 0; i < args->size(); i++)
1757 {
1758 const TType &argType = (*args)[i]->getAsTyped()->getType();
1759 // It has already been checked that the argument is not an array.
1760 ASSERT(!argType.isArray());
1761 if (!argType.sameElementType(*type))
1762 {
1763 error(line, "Array constructor argument has an incorrect type", "Error");
1764 recover();
1765 return nullptr;
1766 }
1767 }
1768 }
1769 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001770 {
1771 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001772 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001774 for (size_t i = 0; i < fields.size(); i++)
1775 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001776 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001777 {
1778 error(line, "Structure constructor arguments do not match structure fields", "Error");
1779 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001781 return 0;
1782 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001783 }
1784 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001786 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001787 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1788 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001789 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001790 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001791 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793
Olli Etuaho21203702014-11-13 16:16:21 +02001794 // Structs should not be precision qualified, the individual members may be.
1795 // Built-in types on the other hand should be precision qualified.
1796 if (op != EOpConstructStruct)
1797 {
1798 constructor->setPrecisionFromChildren();
1799 type->setPrecision(constructor->getPrecision());
1800 }
1801
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803}
1804
1805TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1806{
Olli Etuahof40319e2015-03-10 14:33:00 +02001807 // TODO: Add support for folding array constructors
1808 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001809 aggrNode->setType(type);
1810 if (canBeFolded) {
1811 bool returnVal = false;
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001812 TConstantUnion* unionArray = new TConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001813 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001814 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001815 }
1816 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001817 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001818 }
1819 if (returnVal)
1820 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001822 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826}
1827
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828//
1829// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1830// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1831// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1832// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1833// a constant matrix.
1834//
Jamie Madill075edd82013-07-08 13:30:19 -04001835TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001837 TIntermTyped* typedNode;
1838 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
Jamie Madillb11e2482015-05-04 14:21:22 -04001840 const TConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 if (tempConstantNode) {
1842 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001844 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 return node;
1846 }
1847 } 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 +00001848 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001849 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001851 return 0;
1852 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001854 TConstantUnion* constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001857 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001858 std::stringstream extraInfoStream;
1859 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1860 std::string extraInfo = extraInfoStream.str();
1861 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001862 recover();
1863 fields.offsets[i] = 0;
1864 }
1865
1866 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001868 }
1869 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1870 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871}
1872
1873//
1874// This function returns the column being accessed from a constant matrix. The values are retrieved from
1875// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1876// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1877// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1878//
Jamie Madill075edd82013-07-08 13:30:19 -04001879TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 TIntermTyped* typedNode;
1882 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001884 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001885 std::stringstream extraInfoStream;
1886 extraInfoStream << "matrix field selection out of range '" << index << "'";
1887 std::string extraInfo = extraInfoStream.str();
1888 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001889 recover();
1890 index = 0;
1891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001893 if (tempConstantNode) {
Jamie Madillb11e2482015-05-04 14:21:22 -04001894 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001895 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001896 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1897 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001898 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001899 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001901 return 0;
1902 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001904 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905}
1906
1907
1908//
1909// This function returns an element of an array accessed from a constant array. The values are retrieved from
1910// the symbol table and parse-tree is built for the type of the element. The input
1911// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1912// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1913//
Jamie Madill075edd82013-07-08 13:30:19 -04001914TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001916 TIntermTyped* typedNode;
1917 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1918 TType arrayElementType = node->getType();
1919 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001921 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001922 std::stringstream extraInfoStream;
1923 extraInfoStream << "array field selection out of range '" << index << "'";
1924 std::string extraInfo = extraInfoStream.str();
1925 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001926 recover();
1927 index = 0;
1928 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001930 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001931 size_t arrayElementSize = arrayElementType.getObjectSize();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001932 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001933 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001934 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001935 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001936 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 return 0;
1939 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001941 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942}
1943
1944
1945//
1946// This function returns the value of a particular field inside a constant structure from the symbol table.
1947// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1948// function and returns the parse-tree with the values of the embedded/nested struct.
1949//
Jamie Madill075edd82013-07-08 13:30:19 -04001950TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951{
Jamie Madill98493dd2013-07-08 14:39:03 -04001952 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001953 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
Jamie Madill98493dd2013-07-08 14:39:03 -04001955 for (size_t index = 0; index < fields.size(); ++index) {
1956 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001957 break;
1958 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001959 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001960 }
1961 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962
Jamie Madill94bf7f22013-07-08 13:31:15 -04001963 TIntermTyped *typedNode;
1964 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001966 TConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001968 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1969 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001970 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001971 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001973 return 0;
1974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001976 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977}
1978
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001979//
1980// Interface/uniform blocks
1981//
Jamie Madill98493dd2013-07-08 14:39:03 -04001982TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1983 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001984{
1985 if (reservedErrorCheck(nameLine, blockName))
1986 recover();
1987
1988 if (typeQualifier.qualifier != EvqUniform)
1989 {
1990 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1991 recover();
1992 }
1993
Jamie Madill099c0f32013-06-20 11:55:52 -04001994 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1995 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001996 {
1997 recover();
1998 }
1999
Jamie Madill099c0f32013-06-20 11:55:52 -04002000 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2001 {
2002 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
2003 }
2004
Jamie Madill1566ef72013-06-20 11:55:54 -04002005 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2006 {
2007 blockLayoutQualifier.blockStorage = defaultBlockStorage;
2008 }
2009
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002010 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002011 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002012 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2013 recover();
2014 }
2015
Jamie Madill98493dd2013-07-08 14:39:03 -04002016 // check for sampler types and apply layout qualifiers
2017 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2018 TField* field = (*fieldList)[memberIndex];
2019 TType* fieldType = field->type();
2020 if (IsSampler(fieldType->getBasicType())) {
2021 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002022 recover();
2023 }
2024
Jamie Madill98493dd2013-07-08 14:39:03 -04002025 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002026 switch (qualifier)
2027 {
2028 case EvqGlobal:
2029 case EvqUniform:
2030 break;
2031 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002032 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002033 recover();
2034 break;
2035 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002036
2037 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002038 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2039 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002040 {
2041 recover();
2042 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002043
Jamie Madill98493dd2013-07-08 14:39:03 -04002044 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002045 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002046 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002047 recover();
2048 }
2049
Jamie Madill98493dd2013-07-08 14:39:03 -04002050 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002051 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002052 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002053 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002054 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002055 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002056 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002057 recover();
2058 }
2059
Jamie Madill98493dd2013-07-08 14:39:03 -04002060 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002061 }
2062
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 // add array index
2064 int arraySize = 0;
2065 if (arrayIndex != NULL)
2066 {
2067 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2068 recover();
2069 }
2070
2071 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2072 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002073
2074 TString symbolName = "";
2075 int symbolId = 0;
2076
Jamie Madill98493dd2013-07-08 14:39:03 -04002077 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002078 {
2079 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002080 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2081 {
2082 TField* field = (*fieldList)[memberIndex];
2083 TType* fieldType = field->type();
2084
2085 // set parent pointer of the field variable
2086 fieldType->setInterfaceBlock(interfaceBlock);
2087
2088 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2089 fieldVariable->setQualifier(typeQualifier.qualifier);
2090
Nicolas Capensadfffe42014-06-17 02:13:36 -04002091 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002092 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002093 recover();
2094 }
2095 }
2096 }
2097 else
2098 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002099 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002100 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002101 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002102
Nicolas Capensadfffe42014-06-17 02:13:36 -04002103 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002104 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002105 recover();
2106 }
2107
2108 symbolId = instanceTypeDef->getUniqueId();
2109 symbolName = instanceTypeDef->getName();
2110 }
2111
Jamie Madill98493dd2013-07-08 14:39:03 -04002112 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002113 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002114
2115 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002116 return aggregate;
2117}
2118
Jamie Madill075edd82013-07-08 13:30:19 -04002119bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002120{
2121 ++structNestingLevel;
2122
2123 // Embedded structure definitions are not supported per GLSL ES spec.
2124 // They aren't allowed in GLSL either, but we need to detect this here
2125 // so we don't rely on the GLSL compiler to catch it.
2126 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002127 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002128 return true;
2129 }
2130
2131 return false;
2132}
2133
2134void TParseContext::exitStructDeclaration()
2135{
2136 --structNestingLevel;
2137}
2138
2139namespace {
2140
2141const int kWebGLMaxStructNesting = 4;
2142
2143} // namespace
2144
Jamie Madill98493dd2013-07-08 14:39:03 -04002145bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002146{
Jamie Madill5508f392014-02-20 13:31:36 -05002147 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002148 return false;
2149 }
2150
Jamie Madill98493dd2013-07-08 14:39:03 -04002151 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002152 return false;
2153 }
2154
2155 // We're already inside a structure definition at this point, so add
2156 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002157 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002158 std::stringstream reasonStream;
2159 reasonStream << "Reference of struct type "
2160 << field.type()->getStruct()->name().c_str()
2161 << " exceeds maximum allowed nesting level of "
2162 << kWebGLMaxStructNesting;
2163 std::string reason = reasonStream.str();
2164 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002165 return true;
2166 }
2167
2168 return false;
2169}
2170
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002171//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002172// Parse an array index expression
2173//
Jamie Madill075edd82013-07-08 13:30:19 -04002174TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002175{
2176 TIntermTyped *indexedExpression = NULL;
2177
2178 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2179 {
2180 if (baseExpression->getAsSymbolNode())
2181 {
2182 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2183 }
2184 else
2185 {
2186 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2187 }
2188 recover();
2189 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002190
Jamie Madill21c1e452014-12-29 11:33:41 -05002191 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2192
2193 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002194 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002195 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002196 if (index < 0)
2197 {
2198 std::stringstream infoStream;
2199 infoStream << index;
2200 std::string info = infoStream.str();
2201 error(location, "negative index", info.c_str());
2202 recover();
2203 index = 0;
2204 }
2205 if (baseExpression->getType().getQualifier() == EvqConst)
2206 {
2207 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002208 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002209 // constant folding for arrays
2210 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002211 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002212 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002213 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002214 // constant folding for vectors
2215 TVectorFields fields;
2216 fields.num = 1;
2217 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2218 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2219 }
2220 else if (baseExpression->isMatrix())
2221 {
2222 // constant folding for matrices
2223 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002224 }
2225 }
2226 else
2227 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002228 int safeIndex = -1;
2229
Jamie Madill7164cf42013-07-08 13:30:59 -04002230 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002231 {
Jamie Madill18464b52013-07-08 14:01:55 -04002232 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002233 {
2234 std::stringstream extraInfoStream;
2235 extraInfoStream << "array index out of range '" << index << "'";
2236 std::string extraInfo = extraInfoStream.str();
2237 error(location, "", "[", extraInfo.c_str());
2238 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002239 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002240 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002241 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2242 {
2243 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2244 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002245 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002246 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002247 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002248 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002249 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002250 std::stringstream extraInfoStream;
2251 extraInfoStream << "field selection out of range '" << index << "'";
2252 std::string extraInfo = extraInfoStream.str();
2253 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002254 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002255 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002256 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002257
Jamie Madillb11e2482015-05-04 14:21:22 -04002258 // Don't modify the data of the previous constant union, because it can point
2259 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2260 if (safeIndex != -1)
2261 {
2262 TConstantUnion *safeConstantUnion = new TConstantUnion();
2263 safeConstantUnion->setIConst(safeIndex);
2264 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2265 }
2266
Jamie Madill7164cf42013-07-08 13:30:59 -04002267 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002268 }
2269 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002270 else
2271 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002272 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002273 {
Jamie Madill19571812013-08-12 15:26:34 -07002274 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002275 recover();
2276 }
Jamie Madill19571812013-08-12 15:26:34 -07002277 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002278 {
Jamie Madill19571812013-08-12 15:26:34 -07002279 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002280 recover();
2281 }
2282
2283 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2284 }
2285
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002286 if (indexedExpression == 0)
2287 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002288 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002289 unionArray->setFConst(0.0f);
2290 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2291 }
2292 else if (baseExpression->isArray())
2293 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002294 const TType &baseType = baseExpression->getType();
2295 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002296 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002297 TType copyOfType(baseType.getStruct());
2298 indexedExpression->setType(copyOfType);
2299 }
2300 else if (baseType.isInterfaceBlock())
2301 {
2302 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002303 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002304 }
2305 else
2306 {
Minmin Gong794e0002015-04-07 18:31:54 -07002307 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2308 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002309 }
2310
2311 if (baseExpression->getType().getQualifier() == EvqConst)
2312 {
2313 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2314 }
2315 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002316 else if (baseExpression->isMatrix())
2317 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002318 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002319 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002320 }
2321 else if (baseExpression->isVector())
2322 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002323 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2324 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002325 }
2326 else
2327 {
2328 indexedExpression->setType(baseExpression->getType());
2329 }
2330
2331 return indexedExpression;
2332}
2333
Jamie Madill075edd82013-07-08 13:30:19 -04002334TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002335{
2336 TIntermTyped *indexedExpression = NULL;
2337
2338 if (baseExpression->isArray())
2339 {
2340 error(fieldLocation, "cannot apply dot operator to an array", ".");
2341 recover();
2342 }
2343
2344 if (baseExpression->isVector())
2345 {
2346 TVectorFields fields;
2347 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2348 {
2349 fields.num = 1;
2350 fields.offsets[0] = 0;
2351 recover();
2352 }
2353
2354 if (baseExpression->getType().getQualifier() == EvqConst)
2355 {
2356 // constant folding for vector fields
2357 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2358 if (indexedExpression == 0)
2359 {
2360 recover();
2361 indexedExpression = baseExpression;
2362 }
2363 else
2364 {
Minmin Gong794e0002015-04-07 18:31:54 -07002365 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002366 }
2367 }
2368 else
2369 {
2370 TString vectorString = fieldString;
2371 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2372 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002373 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002374 }
2375 }
2376 else if (baseExpression->isMatrix())
2377 {
2378 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002379 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 {
2381 fields.wholeRow = false;
2382 fields.wholeCol = false;
2383 fields.row = 0;
2384 fields.col = 0;
2385 recover();
2386 }
2387
2388 if (fields.wholeRow || fields.wholeCol)
2389 {
2390 error(dotLocation, " non-scalar fields not implemented yet", ".");
2391 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002392 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002393 unionArray->setIConst(0);
2394 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2395 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002396 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2397 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002398 }
2399 else
2400 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002401 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002402 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002403 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2404 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2405 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2406 }
2407 }
2408 else if (baseExpression->getBasicType() == EbtStruct)
2409 {
2410 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002411 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2412 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002413 {
2414 error(dotLocation, "structure has no fields", "Internal Error");
2415 recover();
2416 indexedExpression = baseExpression;
2417 }
2418 else
2419 {
2420 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002421 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002422 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002423 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002424 {
2425 fieldFound = true;
2426 break;
2427 }
2428 }
2429 if (fieldFound)
2430 {
2431 if (baseExpression->getType().getQualifier() == EvqConst)
2432 {
2433 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2434 if (indexedExpression == 0)
2435 {
2436 recover();
2437 indexedExpression = baseExpression;
2438 }
2439 else
2440 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002441 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002442 // change the qualifier of the return type, not of the structure field
2443 // as the structure definition is shared between various structures.
2444 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2445 }
2446 }
2447 else
2448 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002449 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002450 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002451 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002452 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002453 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002454 }
2455 }
2456 else
2457 {
2458 error(dotLocation, " no such field in structure", fieldString.c_str());
2459 recover();
2460 indexedExpression = baseExpression;
2461 }
2462 }
2463 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002464 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002465 {
2466 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002467 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2468 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002469 {
2470 error(dotLocation, "interface block has no fields", "Internal Error");
2471 recover();
2472 indexedExpression = baseExpression;
2473 }
2474 else
2475 {
2476 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002477 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002478 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002479 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002480 {
2481 fieldFound = true;
2482 break;
2483 }
2484 }
2485 if (fieldFound)
2486 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002487 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002488 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002489 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002490 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002491 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002492 }
2493 else
2494 {
2495 error(dotLocation, " no such field in interface block", fieldString.c_str());
2496 recover();
2497 indexedExpression = baseExpression;
2498 }
2499 }
2500 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002501 else
2502 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002503 if (shaderVersion < 300)
2504 {
2505 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2506 }
2507 else
2508 {
2509 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2510 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002511 recover();
2512 indexedExpression = baseExpression;
2513 }
2514
2515 return indexedExpression;
2516}
2517
Jamie Madill075edd82013-07-08 13:30:19 -04002518TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002519{
Jamie Madilla5efff92013-06-06 11:56:47 -04002520 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002521
Jamie Madilla5efff92013-06-06 11:56:47 -04002522 qualifier.location = -1;
2523 qualifier.matrixPacking = EmpUnspecified;
2524 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002525
2526 if (qualifierType == "shared")
2527 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002528 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002529 }
2530 else if (qualifierType == "packed")
2531 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002532 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002533 }
2534 else if (qualifierType == "std140")
2535 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002536 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002537 }
2538 else if (qualifierType == "row_major")
2539 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002540 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002541 }
2542 else if (qualifierType == "column_major")
2543 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002544 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002545 }
2546 else if (qualifierType == "location")
2547 {
2548 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2549 recover();
2550 }
2551 else
2552 {
2553 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2554 recover();
2555 }
2556
Jamie Madilla5efff92013-06-06 11:56:47 -04002557 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002558}
2559
Jamie Madill075edd82013-07-08 13:30:19 -04002560TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002561{
Jamie Madilla5efff92013-06-06 11:56:47 -04002562 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002563
Jamie Madilla5efff92013-06-06 11:56:47 -04002564 qualifier.location = -1;
2565 qualifier.matrixPacking = EmpUnspecified;
2566 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002567
2568 if (qualifierType != "location")
2569 {
2570 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2571 recover();
2572 }
2573 else
2574 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002575 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002576 if (intValue < 0)
2577 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002578 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002579 recover();
2580 }
2581 else
2582 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002583 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002584 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002585 }
2586
Jamie Madilla5efff92013-06-06 11:56:47 -04002587 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002588}
2589
Jamie Madilla5efff92013-06-06 11:56:47 -04002590TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002591{
Jamie Madilla5efff92013-06-06 11:56:47 -04002592 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002593
Jamie Madilla5efff92013-06-06 11:56:47 -04002594 if (rightQualifier.location != -1)
2595 {
2596 joinedQualifier.location = rightQualifier.location;
2597 }
2598 if (rightQualifier.matrixPacking != EmpUnspecified)
2599 {
2600 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2601 }
2602 if (rightQualifier.blockStorage != EbsUnspecified)
2603 {
2604 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2605 }
2606
2607 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002608}
2609
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002610TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2611 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2612{
2613 TQualifier mergedQualifier = EvqSmoothIn;
2614
Jamie Madill19571812013-08-12 15:26:34 -07002615 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002616 if (interpolationQualifier == EvqSmooth)
2617 mergedQualifier = EvqSmoothIn;
2618 else if (interpolationQualifier == EvqFlat)
2619 mergedQualifier = EvqFlatIn;
2620 else UNREACHABLE();
2621 }
2622 else if (storageQualifier == EvqCentroidIn) {
2623 if (interpolationQualifier == EvqSmooth)
2624 mergedQualifier = EvqCentroidIn;
2625 else if (interpolationQualifier == EvqFlat)
2626 mergedQualifier = EvqFlatIn;
2627 else UNREACHABLE();
2628 }
Jamie Madill19571812013-08-12 15:26:34 -07002629 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002630 if (interpolationQualifier == EvqSmooth)
2631 mergedQualifier = EvqSmoothOut;
2632 else if (interpolationQualifier == EvqFlat)
2633 mergedQualifier = EvqFlatOut;
2634 else UNREACHABLE();
2635 }
2636 else if (storageQualifier == EvqCentroidOut) {
2637 if (interpolationQualifier == EvqSmooth)
2638 mergedQualifier = EvqCentroidOut;
2639 else if (interpolationQualifier == EvqFlat)
2640 mergedQualifier = EvqFlatOut;
2641 else UNREACHABLE();
2642 }
2643 else {
2644 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2645 recover();
2646
2647 mergedQualifier = storageQualifier;
2648 }
2649
2650 TPublicType type;
2651 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2652 return type;
2653}
2654
Jamie Madill98493dd2013-07-08 14:39:03 -04002655TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002656{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002657 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2658 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002659 recover();
2660 }
2661
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002663 //
2664 // Careful not to replace already known aspects of type, like array-ness
2665 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002666 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002667 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002668 type->setPrimarySize(typeSpecifier.primarySize);
2669 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002670 type->setPrecision(typeSpecifier.precision);
2671 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002672 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002673
2674 // don't allow arrays of arrays
2675 if (type->isArray()) {
2676 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2677 recover();
2678 }
2679 if (typeSpecifier.array)
2680 type->setArraySize(typeSpecifier.arraySize);
2681 if (typeSpecifier.userDef) {
2682 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002683 }
2684
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002686 recover();
2687 }
2688 }
2689
Jamie Madill98493dd2013-07-08 14:39:03 -04002690 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002691}
2692
Jamie Madill98493dd2013-07-08 14:39:03 -04002693TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002694{
Jamie Madill98493dd2013-07-08 14:39:03 -04002695 TStructure* structure = new TStructure(structName, fieldList);
2696 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002697
Jamie Madill9b820842015-02-12 10:40:10 -05002698 // Store a bool in the struct if we're at global scope, to allow us to
2699 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002700 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002701 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002702
Jamie Madill98493dd2013-07-08 14:39:03 -04002703 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002704 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002705 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002706 {
2707 recover();
2708 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002709 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002710 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002711 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002712 recover();
2713 }
2714 }
2715
2716 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002717 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002718 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002719 const TField &field = *(*fieldList)[typeListIndex];
2720 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002721 switch (qualifier)
2722 {
2723 case EvqGlobal:
2724 case EvqTemporary:
2725 break;
2726 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002727 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002728 recover();
2729 break;
2730 }
2731 }
2732
2733 TPublicType publicType;
2734 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002735 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002736 exitStructDeclaration();
2737
2738 return publicType;
2739}
2740
Olli Etuahoa3a36662015-02-17 13:46:51 +02002741TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2742{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002743 TBasicType switchType = init->getBasicType();
2744 if ((switchType != EbtInt && switchType != EbtUInt) ||
2745 init->isMatrix() ||
2746 init->isArray() ||
2747 init->isVector())
2748 {
2749 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2750 recover();
2751 return nullptr;
2752 }
2753
Olli Etuahoac5274d2015-02-20 10:19:08 +02002754 if (statementList)
2755 {
2756 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2757 {
2758 recover();
2759 return nullptr;
2760 }
2761 }
2762
Olli Etuahoa3a36662015-02-17 13:46:51 +02002763 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2764 if (node == nullptr)
2765 {
2766 error(loc, "erroneous switch statement", "switch");
2767 recover();
2768 return nullptr;
2769 }
2770 return node;
2771}
2772
2773TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2774{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002775 if (mSwitchNestingLevel == 0)
2776 {
2777 error(loc, "case labels need to be inside switch statements", "case");
2778 recover();
2779 return nullptr;
2780 }
2781 if (condition == nullptr)
2782 {
2783 error(loc, "case label must have a condition", "case");
2784 recover();
2785 return nullptr;
2786 }
2787 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2788 condition->isMatrix() ||
2789 condition->isArray() ||
2790 condition->isVector())
2791 {
2792 error(condition->getLine(), "case label must be a scalar integer", "case");
2793 recover();
2794 }
2795 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2796 if (conditionConst == nullptr)
2797 {
2798 error(condition->getLine(), "case label must be constant", "case");
2799 recover();
2800 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002801 TIntermCase *node = intermediate.addCase(condition, loc);
2802 if (node == nullptr)
2803 {
2804 error(loc, "erroneous case statement", "case");
2805 recover();
2806 return nullptr;
2807 }
2808 return node;
2809}
2810
2811TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2812{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002813 if (mSwitchNestingLevel == 0)
2814 {
2815 error(loc, "default labels need to be inside switch statements", "default");
2816 recover();
2817 return nullptr;
2818 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002819 TIntermCase *node = intermediate.addCase(nullptr, loc);
2820 if (node == nullptr)
2821 {
2822 error(loc, "erroneous default statement", "default");
2823 recover();
2824 return nullptr;
2825 }
2826 return node;
2827}
2828
Olli Etuahof6c694b2015-03-26 14:50:53 +02002829TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2830 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002831{
2832 if (child == nullptr)
2833 {
2834 return nullptr;
2835 }
2836
2837 switch (op)
2838 {
2839 case EOpLogicalNot:
2840 if (child->getBasicType() != EbtBool ||
2841 child->isMatrix() ||
2842 child->isArray() ||
2843 child->isVector())
2844 {
2845 return nullptr;
2846 }
2847 break;
2848 case EOpBitwiseNot:
2849 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2850 child->isMatrix() ||
2851 child->isArray())
2852 {
2853 return nullptr;
2854 }
2855 break;
2856 case EOpPostIncrement:
2857 case EOpPreIncrement:
2858 case EOpPostDecrement:
2859 case EOpPreDecrement:
2860 case EOpNegative:
2861 case EOpPositive:
2862 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002863 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002864 child->isArray())
2865 {
2866 return nullptr;
2867 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002868 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002869 default:
2870 break;
2871 }
2872
Olli Etuahof6c694b2015-03-26 14:50:53 +02002873 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002874}
2875
Olli Etuaho09b22472015-02-11 11:47:26 +02002876TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2877{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002878 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002879 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002880 {
2881 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2882 recover();
2883 return child;
2884 }
2885 return node;
2886}
2887
2888TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2889{
2890 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2891 recover();
2892 return addUnaryMath(op, child, loc);
2893}
2894
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002895bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002896 const TSourceLoc &loc)
2897{
2898 if (left->isArray() || right->isArray())
2899 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002900 if (shaderVersion < 300)
2901 {
2902 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2903 return false;
2904 }
2905
2906 if (left->isArray() != right->isArray())
2907 {
2908 error(loc, "array / non-array mismatch", GetOperatorString(op));
2909 return false;
2910 }
2911
2912 switch (op)
2913 {
2914 case EOpEqual:
2915 case EOpNotEqual:
2916 case EOpAssign:
2917 case EOpInitialize:
2918 break;
2919 default:
2920 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2921 return false;
2922 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002923 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002924 if (left->getArraySize() != right->getArraySize())
2925 {
2926 error(loc, "array size mismatch", GetOperatorString(op));
2927 return false;
2928 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002929 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002930
2931 // Check ops which require integer / ivec parameters
2932 bool isBitShift = false;
2933 switch (op)
2934 {
2935 case EOpBitShiftLeft:
2936 case EOpBitShiftRight:
2937 case EOpBitShiftLeftAssign:
2938 case EOpBitShiftRightAssign:
2939 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2940 // check that the basic type is an integer type.
2941 isBitShift = true;
2942 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2943 {
2944 return false;
2945 }
2946 break;
2947 case EOpBitwiseAnd:
2948 case EOpBitwiseXor:
2949 case EOpBitwiseOr:
2950 case EOpBitwiseAndAssign:
2951 case EOpBitwiseXorAssign:
2952 case EOpBitwiseOrAssign:
2953 // It is enough to check the type of only one operand, since later it
2954 // is checked that the operand types match.
2955 if (!IsInteger(left->getBasicType()))
2956 {
2957 return false;
2958 }
2959 break;
2960 default:
2961 break;
2962 }
2963
2964 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2965 // So the basic type should usually match.
2966 if (!isBitShift && left->getBasicType() != right->getBasicType())
2967 {
2968 return false;
2969 }
2970
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002971 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002972 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002973 switch(op)
2974 {
2975 case EOpAssign:
2976 case EOpInitialize:
2977 case EOpEqual:
2978 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002979 // ESSL 1.00 sections 5.7, 5.8, 5.9
2980 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2981 {
2982 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2983 return false;
2984 }
Olli Etuahoff699002015-03-23 14:38:42 +02002985 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2986 // we interpret the spec so that this extends to structs containing samplers,
2987 // similarly to ESSL 1.00 spec.
2988 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2989 left->getType().isStructureContainingSamplers())
2990 {
2991 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2992 return false;
2993 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002994 case EOpLessThan:
2995 case EOpGreaterThan:
2996 case EOpLessThanEqual:
2997 case EOpGreaterThanEqual:
2998 if ((left->getNominalSize() != right->getNominalSize()) ||
2999 (left->getSecondarySize() != right->getSecondarySize()))
3000 {
3001 return false;
3002 }
3003 default:
3004 break;
3005 }
3006
Olli Etuahod6b14282015-03-17 14:31:35 +02003007 return true;
3008}
3009
Olli Etuahofc1806e2015-03-17 13:03:11 +02003010TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3011 const TSourceLoc &loc)
3012{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003013 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003014 return nullptr;
3015
Olli Etuahofc1806e2015-03-17 13:03:11 +02003016 switch (op)
3017 {
3018 case EOpEqual:
3019 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003020 break;
3021 case EOpLessThan:
3022 case EOpGreaterThan:
3023 case EOpLessThanEqual:
3024 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003025 ASSERT(!left->isArray() && !right->isArray());
3026 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003027 left->getBasicType() == EbtStruct)
3028 {
3029 return nullptr;
3030 }
3031 break;
3032 case EOpLogicalOr:
3033 case EOpLogicalXor:
3034 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003035 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003036 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003037 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003038 {
3039 return nullptr;
3040 }
3041 break;
3042 case EOpAdd:
3043 case EOpSub:
3044 case EOpDiv:
3045 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003046 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003047 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3048 {
3049 return nullptr;
3050 }
3051 break;
3052 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003053 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003054 // Note that this is only for the % operator, not for mod()
3055 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3056 {
3057 return nullptr;
3058 }
3059 break;
3060 // Note that for bitwise ops, type checking is done in promote() to
3061 // share code between ops and compound assignment
3062 default:
3063 break;
3064 }
3065
Olli Etuahofc1806e2015-03-17 13:03:11 +02003066 return intermediate.addBinaryMath(op, left, right, loc);
3067}
3068
Olli Etuaho09b22472015-02-11 11:47:26 +02003069TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3070 const TSourceLoc &loc)
3071{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003072 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003073 if (node == 0)
3074 {
3075 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3076 recover();
3077 return left;
3078 }
3079 return node;
3080}
3081
3082TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3083 const TSourceLoc &loc)
3084{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003085 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003086 if (node == 0)
3087 {
3088 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3089 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003090 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003091 unionArray->setBConst(false);
3092 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3093 }
3094 return node;
3095}
3096
Olli Etuahod6b14282015-03-17 14:31:35 +02003097TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3098 const TSourceLoc &loc)
3099{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003100 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003101 {
3102 return intermediate.addAssign(op, left, right, loc);
3103 }
3104 return nullptr;
3105}
3106
3107TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3108 const TSourceLoc &loc)
3109{
3110 TIntermTyped *node = createAssign(op, left, right, loc);
3111 if (node == nullptr)
3112 {
3113 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3114 recover();
3115 return left;
3116 }
3117 return node;
3118}
3119
Olli Etuaho49300862015-02-20 14:54:49 +02003120TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3121{
3122 switch (op)
3123 {
3124 case EOpContinue:
3125 if (mLoopNestingLevel <= 0)
3126 {
3127 error(loc, "continue statement only allowed in loops", "");
3128 recover();
3129 }
3130 break;
3131 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003132 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003133 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003134 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003135 recover();
3136 }
3137 break;
3138 case EOpReturn:
3139 if (currentFunctionType->getBasicType() != EbtVoid)
3140 {
3141 error(loc, "non-void function must return a value", "return");
3142 recover();
3143 }
3144 break;
3145 default:
3146 // No checks for discard
3147 break;
3148 }
3149 return intermediate.addBranch(op, loc);
3150}
3151
3152TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3153{
3154 ASSERT(op == EOpReturn);
3155 mFunctionReturnsValue = true;
3156 if (currentFunctionType->getBasicType() == EbtVoid)
3157 {
3158 error(loc, "void function cannot return a value", "return");
3159 recover();
3160 }
3161 else if (*currentFunctionType != returnValue->getType())
3162 {
3163 error(loc, "function return is not matching type:", "return");
3164 recover();
3165 }
3166 return intermediate.addBranch(op, returnValue, loc);
3167}
3168
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003169TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3170 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003171{
3172 *fatalError = false;
3173 TOperator op = fnCall->getBuiltInOp();
3174 TIntermTyped *callNode = nullptr;
3175
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003176 if (thisNode != nullptr)
3177 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003178 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003179 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003180 TIntermTyped *typedThis = thisNode->getAsTyped();
3181 if (fnCall->getName() != "length")
3182 {
3183 error(loc, "invalid method", fnCall->getName().c_str());
3184 recover();
3185 }
3186 else if (paramNode != nullptr)
3187 {
3188 error(loc, "method takes no parameters", "length");
3189 recover();
3190 }
3191 else if (typedThis == nullptr || !typedThis->isArray())
3192 {
3193 error(loc, "length can only be called on arrays", "length");
3194 recover();
3195 }
3196 else
3197 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003198 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003199 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003200 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003201 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003202 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003203 // (func()).length()
3204 // (int[3](0, 1, 2)).length()
3205 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3206 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3207 // which allows "An array, vector or matrix expression with the length method applied".
3208 error(loc, "length can only be called on array names, not on array expressions", "length");
3209 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003210 }
3211 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003212 unionArray->setIConst(arraySize);
3213 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003214 }
3215 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003216 {
3217 //
3218 // Then this should be a constructor.
3219 // Don't go through the symbol table for constructors.
3220 // Their parameters will be verified algorithmically.
3221 //
3222 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003223 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003224 {
3225 //
3226 // It's a constructor, of type 'type'.
3227 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003228 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003229 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003230
3231 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003232 {
3233 recover();
3234 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3235 }
3236 callNode->setType(type);
3237 }
3238 else
3239 {
3240 //
3241 // Not a constructor. Find it in the symbol table.
3242 //
3243 const TFunction* fnCandidate;
3244 bool builtIn;
3245 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3246 if (fnCandidate)
3247 {
3248 //
3249 // A declared function.
3250 //
3251 if (builtIn && !fnCandidate->getExtension().empty() &&
3252 extensionErrorCheck(loc, fnCandidate->getExtension()))
3253 {
3254 recover();
3255 }
3256 op = fnCandidate->getBuiltInOp();
3257 if (builtIn && op != EOpNull)
3258 {
3259 //
3260 // A function call mapped to a built-in operation.
3261 //
3262 if (fnCandidate->getParamCount() == 1)
3263 {
3264 //
3265 // Treat it like a built-in unary operator.
3266 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003267 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003268 if (callNode == nullptr)
3269 {
3270 std::stringstream extraInfoStream;
3271 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003272 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003273 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003274 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003275 *fatalError = true;
3276 return nullptr;
3277 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003278 }
3279 else
3280 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003281 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003282 aggregate->setType(fnCandidate->getReturnType());
3283 aggregate->setPrecisionFromChildren();
3284 callNode = aggregate;
3285
3286 // Some built-in functions have out parameters too.
3287 functionCallLValueErrorCheck(fnCandidate, aggregate);
3288 }
3289 }
3290 else
3291 {
3292 // This is a real function call
3293
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003294 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003295 aggregate->setType(fnCandidate->getReturnType());
3296
3297 // this is how we know whether the given function is a builtIn function or a user defined function
3298 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3299 // if builtIn == true, it's definitely a builtIn function with EOpNull
3300 if (!builtIn)
3301 aggregate->setUserDefined();
3302 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003303 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003304
3305 // This needs to happen after the name is set
3306 if (builtIn)
3307 aggregate->setBuiltInFunctionPrecision();
3308
3309 callNode = aggregate;
3310
3311 functionCallLValueErrorCheck(fnCandidate, aggregate);
3312 }
3313 }
3314 else
3315 {
3316 // error message was put out by findFunction()
3317 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003318 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003319 unionArray->setFConst(0.0f);
3320 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3321 recover();
3322 }
3323 }
3324 delete fnCall;
3325 return callNode;
3326}
3327
Olli Etuaho52901742015-04-15 13:42:45 +03003328TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3329 const TSourceLoc &loc)
3330{
3331 if (boolErrorCheck(loc, cond))
3332 recover();
3333
3334 if (trueBlock->getType() != falseBlock->getType())
3335 {
3336 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3337 recover();
3338 return falseBlock;
3339 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003340 // ESSL1 sections 5.2 and 5.7:
3341 // ESSL3 section 5.7:
3342 // Ternary operator is not among the operators allowed for structures/arrays.
3343 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3344 {
3345 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3346 recover();
3347 return falseBlock;
3348 }
Olli Etuaho52901742015-04-15 13:42:45 +03003349 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3350}
Olli Etuaho49300862015-02-20 14:54:49 +02003351
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003352//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003353// Parse an array of strings using yyparse.
3354//
3355// Returns 0 for success.
3356//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003357int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003358 TParseContext* context) {
3359 if ((count == 0) || (string == NULL))
3360 return 1;
3361
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003362 if (glslang_initialize(context))
3363 return 1;
3364
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003365 int error = glslang_scan(count, string, length, context);
3366 if (!error)
3367 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003368
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003369 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003370
alokp@chromium.org6b495712012-06-29 00:06:58 +00003371 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003372}
3373
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003374
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003375