blob: 87f1e2dcd444fd1c0f6094e20c20d440010617ee [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capenscc863da2015-01-21 15:50:55 -05007#include "ParseHelper.h"
John Bauman66b8ab22014-05-06 15:57:45 -04008
9#include <stdarg.h>
10#include <stdio.h>
11
Nicolas Capenscc863da2015-01-21 15:50:55 -050012#include "glslang.h"
13#include "preprocessor/SourceLocation.h"
Alexis Hetue5246692015-06-18 12:34:52 -040014#include "ValidateGlobalInitializer.h"
Alexis Hetu76a343a2015-06-04 17:21:22 -040015#include "ValidateSwitch.h"
John Bauman66b8ab22014-05-06 15:57:45 -040016
17///////////////////////////////////////////////////////////////////////
18//
19// 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//
Alexis Hetufe1269e2015-06-16 12:43:32 -040027bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -040028{
29 fields.num = (int) compString.size();
30 if (fields.num > 4) {
31 error(line, "illegal vector field selection", compString.c_str());
32 return false;
33 }
34
35 enum {
36 exyzw,
37 ergba,
John Baumand4ae8632014-05-06 16:18:33 -040038 estpq
John Bauman66b8ab22014-05-06 15:57:45 -040039 } fieldSet[4];
40
41 for (int i = 0; i < fields.num; ++i) {
42 switch (compString[i]) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040043 case 'x':
John Bauman66b8ab22014-05-06 15:57:45 -040044 fields.offsets[i] = 0;
45 fieldSet[i] = exyzw;
46 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040047 case 'r':
John Bauman66b8ab22014-05-06 15:57:45 -040048 fields.offsets[i] = 0;
49 fieldSet[i] = ergba;
50 break;
51 case 's':
52 fields.offsets[i] = 0;
53 fieldSet[i] = estpq;
54 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040055 case 'y':
John Bauman66b8ab22014-05-06 15:57:45 -040056 fields.offsets[i] = 1;
57 fieldSet[i] = exyzw;
58 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040059 case 'g':
John Bauman66b8ab22014-05-06 15:57:45 -040060 fields.offsets[i] = 1;
61 fieldSet[i] = ergba;
62 break;
63 case 't':
64 fields.offsets[i] = 1;
65 fieldSet[i] = estpq;
66 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040067 case 'z':
John Bauman66b8ab22014-05-06 15:57:45 -040068 fields.offsets[i] = 2;
69 fieldSet[i] = exyzw;
70 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040071 case 'b':
John Bauman66b8ab22014-05-06 15:57:45 -040072 fields.offsets[i] = 2;
73 fieldSet[i] = ergba;
74 break;
75 case 'p':
76 fields.offsets[i] = 2;
77 fieldSet[i] = estpq;
78 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040079 case 'w':
John Bauman66b8ab22014-05-06 15:57:45 -040080 fields.offsets[i] = 3;
81 fieldSet[i] = exyzw;
82 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040083 case 'a':
John Bauman66b8ab22014-05-06 15:57:45 -040084 fields.offsets[i] = 3;
85 fieldSet[i] = ergba;
86 break;
87 case 'q':
88 fields.offsets[i] = 3;
89 fieldSet[i] = estpq;
90 break;
91 default:
92 error(line, "illegal vector field selection", compString.c_str());
93 return false;
94 }
95 }
96
97 for (int i = 0; i < fields.num; ++i) {
98 if (fields.offsets[i] >= vecSize) {
99 error(line, "vector field selection out of range", compString.c_str());
100 return false;
101 }
102
103 if (i > 0) {
104 if (fieldSet[i] != fieldSet[i-1]) {
105 error(line, "illegal - vector component fields not from the same set", compString.c_str());
106 return false;
107 }
108 }
109 }
110
111 return true;
112}
113
114
115//
116// Look at a '.' field selector string and change it into offsets
117// for a matrix.
118//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400119bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400120{
121 fields.wholeRow = false;
122 fields.wholeCol = false;
123 fields.row = -1;
124 fields.col = -1;
125
126 if (compString.size() != 2) {
127 error(line, "illegal length of matrix field selection", compString.c_str());
128 return false;
129 }
130
131 if (compString[0] == '_') {
132 if (compString[1] < '0' || compString[1] > '3') {
133 error(line, "illegal matrix field selection", compString.c_str());
134 return false;
135 }
136 fields.wholeCol = true;
137 fields.col = compString[1] - '0';
138 } else if (compString[1] == '_') {
139 if (compString[0] < '0' || compString[0] > '3') {
140 error(line, "illegal matrix field selection", compString.c_str());
141 return false;
142 }
143 fields.wholeRow = true;
144 fields.row = compString[0] - '0';
145 } else {
146 if (compString[0] < '0' || compString[0] > '3' ||
147 compString[1] < '0' || compString[1] > '3') {
148 error(line, "illegal matrix field selection", compString.c_str());
149 return false;
150 }
151 fields.row = compString[0] - '0';
152 fields.col = compString[1] - '0';
153 }
154
Alexis Hetu00106d42015-04-23 11:45:35 -0400155 if (fields.row >= matRows || fields.col >= matCols) {
John Bauman66b8ab22014-05-06 15:57:45 -0400156 error(line, "matrix field selection out of range", compString.c_str());
157 return false;
158 }
159
160 return true;
161}
162
163///////////////////////////////////////////////////////////////////////
164//
165// Errors
166//
167////////////////////////////////////////////////////////////////////////
168
169//
170// Track whether errors have occurred.
171//
172void TParseContext::recover()
173{
174}
175
176//
177// Used by flex/bison to output all syntax and parsing errors.
178//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400179void TParseContext::error(const TSourceLoc& loc,
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400180 const char* reason, const char* token,
John Bauman66b8ab22014-05-06 15:57:45 -0400181 const char* extraInfo)
182{
Alexis Hetu253fdd12015-07-07 15:12:46 -0400183 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -0400184 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
185 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400186
187}
188
Alexis Hetufe1269e2015-06-16 12:43:32 -0400189void TParseContext::warning(const TSourceLoc& loc,
John Bauman66b8ab22014-05-06 15:57:45 -0400190 const char* reason, const char* token,
191 const char* extraInfo) {
Alexis Hetu253fdd12015-07-07 15:12:46 -0400192 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -0400193 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
194 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400195}
196
197void TParseContext::trace(const char* str)
198{
Alexis Hetu0a655842015-06-22 16:52:11 -0400199 mDiagnostics.writeDebug(str);
John Bauman66b8ab22014-05-06 15:57:45 -0400200}
201
202//
203// Same error message for all places assignments don't work.
204//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400205void TParseContext::assignError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400206{
207 std::stringstream extraInfoStream;
208 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
209 std::string extraInfo = extraInfoStream.str();
210 error(line, "", op, extraInfo.c_str());
211}
212
213//
214// Same error message for all places unary operations don't work.
215//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400216void TParseContext::unaryOpError(const TSourceLoc &line, const char* op, TString operand)
John Bauman66b8ab22014-05-06 15:57:45 -0400217{
218 std::stringstream extraInfoStream;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400219 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
John Bauman66b8ab22014-05-06 15:57:45 -0400220 << " (or there is no acceptable conversion)";
221 std::string extraInfo = extraInfoStream.str();
222 error(line, " wrong operand type", op, extraInfo.c_str());
223}
224
225//
226// Same error message for all binary operations don't work.
227//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400228void TParseContext::binaryOpError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400229{
230 std::stringstream extraInfoStream;
Nicolas Capens0863f0d2016-04-10 00:30:02 -0400231 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
John Bauman66b8ab22014-05-06 15:57:45 -0400232 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
233 std::string extraInfo = extraInfoStream.str();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400234 error(line, " wrong operand types ", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400235}
236
Alexis Hetufe1269e2015-06-16 12:43:32 -0400237bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type){
Alexis Hetu0a655842015-06-22 16:52:11 -0400238 if (!mChecksPrecisionErrors)
John Bauman66b8ab22014-05-06 15:57:45 -0400239 return false;
240 switch( type ){
241 case EbtFloat:
242 if( precision == EbpUndefined ){
243 error( line, "No precision specified for (float)", "" );
244 return true;
245 }
246 break;
247 case EbtInt:
248 if( precision == EbpUndefined ){
249 error( line, "No precision specified (int)", "" );
250 return true;
251 }
252 break;
253 default:
254 return false;
255 }
256 return false;
257}
258
259//
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//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400265bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char* op, TIntermTyped* node)
John Bauman66b8ab22014-05-06 15:57:45 -0400266{
267 TIntermSymbol* symNode = node->getAsSymbolNode();
268 TIntermBinary* binaryNode = node->getAsBinaryNode();
269
270 if (binaryNode) {
271 bool errorReturn;
272
273 switch(binaryNode->getOp()) {
274 case EOpIndexDirect:
275 case EOpIndexIndirect:
276 case EOpIndexDirectStruct:
277 return lValueErrorCheck(line, op, binaryNode->getLeft());
278 case EOpVectorSwizzle:
279 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
280 if (!errorReturn) {
281 int offset[4] = {0,0,0,0};
282
283 TIntermTyped* rightNode = binaryNode->getRight();
284 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400285
286 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
John Bauman66b8ab22014-05-06 15:57:45 -0400287 p != aggrNode->getSequence().end(); p++) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500288 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400289 offset[value]++;
John Bauman66b8ab22014-05-06 15:57:45 -0400290 if (offset[value] > 1) {
291 error(line, " l-value of swizzle cannot have duplicate components", op);
292
293 return true;
294 }
295 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400296 }
John Bauman66b8ab22014-05-06 15:57:45 -0400297
298 return errorReturn;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400299 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400300 break;
301 }
302 error(line, " l-value required", op);
303
304 return true;
305 }
306
307
308 const char* symbol = 0;
309 if (symNode != 0)
310 symbol = symNode->getSymbol().c_str();
311
312 const char* message = 0;
313 switch (node->getQualifier()) {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500314 case EvqConstExpr: message = "can't modify a const"; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400315 case EvqConstReadOnly: message = "can't modify a const"; break;
316 case EvqAttribute: message = "can't modify an attribute"; break;
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400317 case EvqFragmentIn: message = "can't modify an input"; break;
318 case EvqVertexIn: message = "can't modify an input"; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400319 case EvqUniform: message = "can't modify a uniform"; break;
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400320 case EvqSmoothIn:
321 case EvqFlatIn:
322 case EvqCentroidIn:
John Bauman66b8ab22014-05-06 15:57:45 -0400323 case EvqVaryingIn: message = "can't modify a varying"; break;
324 case EvqInput: message = "can't modify an input"; break;
325 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
326 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
327 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
Alexis Hetu6743bbf2015-04-21 17:06:14 -0400328 case EvqInstanceID: message = "can't modify gl_InstanceID"; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400329 default:
330
331 //
332 // Type that can't be written to?
333 //
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400334 if(IsSampler(node->getBasicType()))
335 {
John Bauman66b8ab22014-05-06 15:57:45 -0400336 message = "can't modify a sampler";
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400337 }
338 else if(node->getBasicType() == EbtVoid)
339 {
John Bauman66b8ab22014-05-06 15:57:45 -0400340 message = "can't modify void";
John Bauman66b8ab22014-05-06 15:57:45 -0400341 }
342 }
343
344 if (message == 0 && binaryNode == 0 && symNode == 0) {
345 error(line, " l-value required", op);
346
347 return true;
348 }
349
350
351 //
352 // Everything else is okay, no error.
353 //
354 if (message == 0)
355 return false;
356
357 //
358 // If we get here, we have an error and a message.
359 //
360 if (symNode) {
361 std::stringstream extraInfoStream;
362 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
363 std::string extraInfo = extraInfoStream.str();
364 error(line, " l-value required", op, extraInfo.c_str());
365 }
366 else {
367 std::stringstream extraInfoStream;
368 extraInfoStream << "(" << message << ")";
369 std::string extraInfo = extraInfoStream.str();
370 error(line, " l-value required", op, extraInfo.c_str());
371 }
372
373 return true;
374}
375
376//
377// Both test, and if necessary spit out an error, to see if the node is really
378// a constant.
379//
380// Returns true if the was an error.
381//
382bool TParseContext::constErrorCheck(TIntermTyped* node)
383{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500384 if (node->getQualifier() == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400385 return false;
386
387 error(node->getLine(), "constant expression required", "");
388
389 return true;
390}
391
392//
393// Both test, and if necessary spit out an error, to see if the node is really
394// an integer.
395//
396// Returns true if the was an error.
397//
398bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
399{
Nicolas Capens3c20f802015-02-17 17:17:20 -0500400 if (node->isScalarInt())
John Bauman66b8ab22014-05-06 15:57:45 -0400401 return false;
402
403 error(node->getLine(), "integer expression required", token);
404
405 return true;
406}
407
408//
409// Both test, and if necessary spit out an error, to see if we are currently
410// globally scoped.
411//
412// Returns true if the was an error.
413//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400414bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char* token)
John Bauman66b8ab22014-05-06 15:57:45 -0400415{
416 if (global)
417 return false;
418
419 error(line, "only allowed at global scope", token);
420
421 return true;
422}
423
424//
425// For now, keep it simple: if it starts "gl_", it's reserved, independent
426// of scope. Except, if the symbol table is at the built-in push-level,
427// which is when we are parsing built-ins.
428// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
429// webgl shader.
430//
431// Returns true if there was an error.
432//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400433bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -0400434{
435 static const char* reservedErrMsg = "reserved built-in name";
436 if (!symbolTable.atBuiltInLevel()) {
437 if (identifier.compare(0, 3, "gl_") == 0) {
438 error(line, reservedErrMsg, "gl_");
439 return true;
440 }
John Bauman66b8ab22014-05-06 15:57:45 -0400441 if (identifier.find("__") != TString::npos) {
442 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
443 return true;
444 }
445 }
446
447 return false;
448}
449
450//
451// Make sure there is enough data provided to the constructor to build
452// something of the type of the constructor. Also returns the type of
453// the constructor.
454//
455// Returns true if there was an error in construction.
456//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400457bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400458{
459 *type = function.getReturnType();
460
461 bool constructingMatrix = false;
462 switch(op) {
463 case EOpConstructMat2:
Alexis Hetue5246692015-06-18 12:34:52 -0400464 case EOpConstructMat2x3:
465 case EOpConstructMat2x4:
466 case EOpConstructMat3x2:
John Bauman66b8ab22014-05-06 15:57:45 -0400467 case EOpConstructMat3:
Alexis Hetue5246692015-06-18 12:34:52 -0400468 case EOpConstructMat3x4:
469 case EOpConstructMat4x2:
470 case EOpConstructMat4x3:
John Bauman66b8ab22014-05-06 15:57:45 -0400471 case EOpConstructMat4:
472 constructingMatrix = true;
473 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400474 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400475 break;
476 }
477
478 //
479 // Note: It's okay to have too many components available, but not okay to have unused
480 // arguments. 'full' will go to true when enough args have been seen. If we loop
481 // again, there is an extra argument, so 'overfull' will become true.
482 //
483
Alexis Hetuab752792016-04-21 16:11:31 -0400484 size_t size = 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400485 bool full = false;
486 bool overFull = false;
487 bool matrixInMatrix = false;
488 bool arrayArg = false;
Alexis Hetua818c452015-06-11 13:06:58 -0400489 for (size_t i = 0; i < function.getParamCount(); ++i) {
John Bauman66b8ab22014-05-06 15:57:45 -0400490 const TParameter& param = function.getParam(i);
491 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400492
John Bauman66b8ab22014-05-06 15:57:45 -0400493 if (constructingMatrix && param.type->isMatrix())
494 matrixInMatrix = true;
495 if (full)
496 overFull = true;
497 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
498 full = true;
John Bauman66b8ab22014-05-06 15:57:45 -0400499 if (param.type->isArray())
500 arrayArg = true;
501 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400502
Alexis Hetue5246692015-06-18 12:34:52 -0400503 if(type->isArray()) {
504 if(type->getArraySize() == 0) {
505 type->setArraySize(function.getParamCount());
Nicolas Capens5d961882016-01-01 23:18:14 -0500506 } else if(type->getArraySize() != (int)function.getParamCount()) {
Alexis Hetue5246692015-06-18 12:34:52 -0400507 error(line, "array constructor needs one argument per array element", "constructor");
508 return true;
509 }
John Bauman66b8ab22014-05-06 15:57:45 -0400510 }
511
512 if (arrayArg && op != EOpConstructStruct) {
513 error(line, "constructing from a non-dereferenced array", "constructor");
514 return true;
515 }
516
517 if (matrixInMatrix && !type->isArray()) {
518 if (function.getParamCount() != 1) {
519 error(line, "constructing matrix from matrix can only take one argument", "constructor");
520 return true;
521 }
522 }
523
524 if (overFull) {
525 error(line, "too many arguments", "constructor");
526 return true;
527 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400528
Nicolas Capens5d961882016-01-01 23:18:14 -0500529 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
John Bauman66b8ab22014-05-06 15:57:45 -0400530 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
531 return true;
532 }
533
534 if (!type->isMatrix() || !matrixInMatrix) {
535 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
536 (op == EOpConstructStruct && size < type->getObjectSize())) {
537 error(line, "not enough data provided for construction", "constructor");
538 return true;
539 }
540 }
541
542 TIntermTyped *typed = node ? node->getAsTyped() : 0;
543 if (typed == 0) {
544 error(line, "constructor argument does not have a type", "constructor");
545 return true;
546 }
547 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
548 error(line, "cannot convert a sampler", "constructor");
549 return true;
550 }
551 if (typed->getBasicType() == EbtVoid) {
552 error(line, "cannot convert a void", "constructor");
553 return true;
554 }
555
556 return false;
557}
558
559// This function checks to see if a void variable has been declared and raise an error message for such a case
560//
561// returns true in case of an error
562//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400563bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString& identifier, const TBasicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400564{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400565 if(type == EbtVoid) {
John Bauman66b8ab22014-05-06 15:57:45 -0400566 error(line, "illegal use of type 'void'", identifier.c_str());
567 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400568 }
John Bauman66b8ab22014-05-06 15:57:45 -0400569
570 return false;
571}
572
573// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
574//
575// returns true in case of an error
576//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400577bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400578{
579 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
580 error(line, "boolean expression expected", "");
581 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400582 }
John Bauman66b8ab22014-05-06 15:57:45 -0400583
584 return false;
585}
586
587// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
588//
589// returns true in case of an error
590//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400591bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400592{
Alexis Hetub14178b2015-04-13 13:23:20 -0400593 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400594 error(line, "boolean expression expected", "");
595 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400596 }
John Bauman66b8ab22014-05-06 15:57:45 -0400597
598 return false;
599}
600
Alexis Hetufe1269e2015-06-16 12:43:32 -0400601bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType& pType, const char* reason)
John Bauman66b8ab22014-05-06 15:57:45 -0400602{
603 if (pType.type == EbtStruct) {
604 if (containsSampler(*pType.userDef)) {
605 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400606
John Bauman66b8ab22014-05-06 15:57:45 -0400607 return true;
608 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400609
John Bauman66b8ab22014-05-06 15:57:45 -0400610 return false;
611 } else if (IsSampler(pType.type)) {
612 error(line, reason, getBasicString(pType.type));
613
614 return true;
615 }
616
617 return false;
618}
619
Alexis Hetufe1269e2015-06-16 12:43:32 -0400620bool TParseContext::structQualifierErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400621{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400622 switch(pType.qualifier)
623 {
624 case EvqVaryingOut:
625 case EvqSmooth:
626 case EvqFlat:
627 case EvqCentroidOut:
628 case EvqVaryingIn:
629 case EvqSmoothIn:
630 case EvqFlatIn:
631 case EvqCentroidIn:
632 case EvqAttribute:
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400633 case EvqVertexIn:
634 case EvqFragmentOut:
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400635 if(pType.type == EbtStruct)
636 {
637 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400638
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400639 return true;
640 }
641 break;
642 default:
643 break;
644 }
John Bauman66b8ab22014-05-06 15:57:45 -0400645
646 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
647 return true;
648
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400649 // check for layout qualifier issues
650 const TLayoutQualifier layoutQualifier = pType.layoutQualifier;
651
652 if (pType.qualifier != EvqVertexIn && pType.qualifier != EvqFragmentOut &&
653 layoutLocationErrorCheck(line, pType.layoutQualifier))
654 {
655 return true;
656 }
657
John Bauman66b8ab22014-05-06 15:57:45 -0400658 return false;
659}
660
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400661// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
662// declaration.
663//
664bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
665{
666 switch(publicType.qualifier)
667 {
668 case EvqVaryingIn:
669 case EvqVaryingOut:
670 case EvqAttribute:
671 case EvqVertexIn:
672 case EvqFragmentOut:
673 if(publicType.type == EbtStruct)
674 {
675 error(identifierLocation, "cannot be used with a structure",
676 getQualifierString(publicType.qualifier));
677 return true;
678 }
679
680 default: break;
681 }
682
683 if(publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
684 "samplers must be uniform"))
685 {
686 return true;
687 }
688
689 // check for layout qualifier issues
690 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
691
692 if(layoutQualifier.matrixPacking != EmpUnspecified)
693 {
694 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
695 "only valid for interface blocks");
696 return true;
697 }
698
699 if(layoutQualifier.blockStorage != EbsUnspecified)
700 {
701 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
702 "only valid for interface blocks");
703 return true;
704 }
705
706 if(publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
707 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
708 {
709 return true;
710 }
711
712 return false;
713}
714
Nicolas Capens3713cd42015-06-22 10:41:54 -0400715bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
716{
717 if(layoutQualifier.location != -1)
718 {
719 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
720 return true;
721 }
722
723 return false;
724}
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400725
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400726bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
727{
728 if(pType.layoutQualifier.location != -1)
729 {
730 error(line, "location must only be specified for a single input or output variable", "location");
731 return true;
732 }
733
734 return false;
735}
736
Alexis Hetufe1269e2015-06-16 12:43:32 -0400737bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400738{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400739 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
John Bauman66b8ab22014-05-06 15:57:45 -0400740 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
741 error(line, "samplers cannot be output parameters", type.getBasicString());
742 return true;
743 }
744
745 return false;
746}
747
748bool TParseContext::containsSampler(TType& type)
749{
750 if (IsSampler(type.getBasicType()))
751 return true;
752
753 if (type.getBasicType() == EbtStruct) {
Alexis Hetua8b364b2015-06-10 11:48:40 -0400754 const TFieldList& fields = type.getStruct()->fields();
755 for(unsigned int i = 0; i < fields.size(); ++i) {
756 if (containsSampler(*fields[i]->type()))
John Bauman66b8ab22014-05-06 15:57:45 -0400757 return true;
758 }
759 }
760
761 return false;
762}
763
764//
765// Do size checking for an array type's size.
766//
767// Returns true if there was an error.
768//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400769bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped* expr, int& size)
John Bauman66b8ab22014-05-06 15:57:45 -0400770{
771 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500772
773 if (constant == 0 || !constant->isScalarInt())
774 {
John Bauman66b8ab22014-05-06 15:57:45 -0400775 error(line, "array size must be a constant integer expression", "");
776 return true;
777 }
778
Nicolas Capens3c20f802015-02-17 17:17:20 -0500779 if (constant->getBasicType() == EbtUInt)
780 {
781 unsigned int uintSize = constant->getUConst(0);
782 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
783 {
784 error(line, "array size too large", "");
785 size = 1;
786 return true;
787 }
John Bauman66b8ab22014-05-06 15:57:45 -0400788
Nicolas Capens3c20f802015-02-17 17:17:20 -0500789 size = static_cast<int>(uintSize);
790 }
791 else
792 {
793 size = constant->getIConst(0);
794
795 if (size <= 0)
796 {
797 error(line, "array size must be a positive integer", "");
798 size = 1;
799 return true;
800 }
John Bauman66b8ab22014-05-06 15:57:45 -0400801 }
802
803 return false;
804}
805
806//
807// See if this qualifier can be an array.
808//
809// Returns true if there is an error.
810//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400811bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400812{
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400813 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400814 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
815 return true;
816 }
817
818 return false;
819}
820
821//
822// See if this type can be an array.
823//
824// Returns true if there is an error.
825//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400826bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400827{
828 //
829 // Can the type be an array?
830 //
831 if (type.array) {
832 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
833 return true;
834 }
835
836 return false;
837}
838
Alexis Hetufe1269e2015-06-16 12:43:32 -0400839bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400840{
841 bool builtIn = false;
Alexis Hetu0a655842015-06-22 16:52:11 -0400842 TSymbol* symbol = symbolTable.find(node->getSymbol(), mShaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400843 if (symbol == 0) {
844 error(line, " undeclared identifier", node->getSymbol().c_str());
845 return true;
846 }
847 TVariable* variable = static_cast<TVariable*>(symbol);
848
849 type->setArrayInformationType(variable->getArrayInformationType());
850 variable->updateArrayInformationType(type);
851
852 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
853 // its an error
854 if (node->getSymbol() == "gl_FragData") {
Alexis Hetu0a655842015-06-22 16:52:11 -0400855 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", mShaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400856 ASSERT(fragData);
857
858 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
859 if (fragDataValue <= size) {
860 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
861 return true;
862 }
863 }
864
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400865 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
John Bauman66b8ab22014-05-06 15:57:45 -0400866 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
867 if (!updateFlag)
868 return false;
869
870 size++;
871 variable->getType().setMaxArraySize(size);
872 type->setMaxArraySize(size);
873 TType* tt = type;
874
875 while(tt->getArrayInformationType() != 0) {
876 tt = tt->getArrayInformationType();
877 tt->setMaxArraySize(size);
878 }
879
880 return false;
881}
882
883//
884// Enforce non-initializer type/qualifier rules.
885//
886// Returns true if there was an error.
887//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400888bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, TString& identifier, TPublicType& type, bool array)
John Bauman66b8ab22014-05-06 15:57:45 -0400889{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500890 if (type.qualifier == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400891 {
892 // Make the qualifier make sense.
893 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400894
John Bauman66b8ab22014-05-06 15:57:45 -0400895 if (array)
896 {
897 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
898 }
899 else if (type.isStructureContainingArrays())
900 {
901 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
902 }
903 else
904 {
905 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
906 }
907
908 return true;
909 }
910
911 return false;
912}
913
914//
915// Do semantic checking for a variable declaration that has no initializer,
916// and update the symbol table.
917//
918// Returns true if there was an error.
919//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400920bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString& identifier, TPublicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400921{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400922 if(type.qualifier == EvqConstExpr)
923 {
924 // Make the qualifier make sense.
925 type.qualifier = EvqTemporary;
John Bauman66b8ab22014-05-06 15:57:45 -0400926
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400927 // Generate informative error messages for ESSL1.
928 // In ESSL3 arrays and structures containing arrays can be constant.
Alexis Hetu0a655842015-06-22 16:52:11 -0400929 if(mShaderVersion < 300 && type.isStructureContainingArrays())
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400930 {
931 error(line,
932 "structures containing arrays may not be declared constant since they cannot be initialized",
933 identifier.c_str());
934 }
935 else
936 {
937 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
938 }
John Bauman66b8ab22014-05-06 15:57:45 -0400939
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400940 return true;
941 }
942 if(type.isUnsizedArray())
943 {
944 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
945 return true;
946 }
947 return false;
948}
John Bauman66b8ab22014-05-06 15:57:45 -0400949
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400950// Do some simple checks that are shared between all variable declarations,
951// and update the symbol table.
952//
953// Returns true if declaring the variable succeeded.
954//
955bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
956 TVariable **variable)
957{
958 ASSERT((*variable) == nullptr);
John Bauman66b8ab22014-05-06 15:57:45 -0400959
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400960 // gl_LastFragData may be redeclared with a new precision qualifier
961 if(type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
962 {
963 const TVariable *maxDrawBuffers =
Alexis Hetu0a655842015-06-22 16:52:11 -0400964 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400965 if(type.getArraySize() != maxDrawBuffers->getConstPointer()->getIConst())
966 {
967 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
968 return false;
969 }
970 }
971
972 if(reservedErrorCheck(line, identifier))
973 return false;
974
975 (*variable) = new TVariable(&identifier, type);
976 if(!symbolTable.declare(**variable))
977 {
978 error(line, "redefinition", identifier.c_str());
979 delete (*variable);
980 (*variable) = nullptr;
981 return false;
982 }
983
984 if(voidErrorCheck(line, identifier, type.getBasicType()))
985 return false;
986
987 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400988}
989
Alexis Hetufe1269e2015-06-16 12:43:32 -0400990bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400991{
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500992 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
John Bauman66b8ab22014-05-06 15:57:45 -0400993 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
994 return true;
995 }
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500996 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
John Bauman66b8ab22014-05-06 15:57:45 -0400997 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
998 return true;
999 }
1000
Nicolas Capensb1e911a2015-02-26 13:16:00 -05001001 if (qualifier == EvqConstReadOnly)
John Bauman66b8ab22014-05-06 15:57:45 -04001002 type->setQualifier(EvqConstReadOnly);
1003 else
1004 type->setQualifier(paramQualifier);
1005
1006 return false;
1007}
1008
Alexis Hetufe1269e2015-06-16 12:43:32 -04001009bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString& extension)
John Bauman66b8ab22014-05-06 15:57:45 -04001010{
1011 const TExtensionBehavior& extBehavior = extensionBehavior();
1012 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
1013 if (iter == extBehavior.end()) {
1014 error(line, "extension", extension.c_str(), "is not supported");
1015 return true;
1016 }
1017 // In GLSL ES, an extension's default behavior is "disable".
1018 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
1019 error(line, "extension", extension.c_str(), "is disabled");
1020 return true;
1021 }
1022 if (iter->second == EBhWarn) {
1023 warning(line, "extension", extension.c_str(), "is being used");
1024 return false;
1025 }
1026
1027 return false;
1028}
1029
Alexis Hetuad6b8752015-06-09 16:15:30 -04001030bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1031{
1032 for(size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1033 {
1034 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1035 if(qual == EvqOut || qual == EvqInOut)
1036 {
1037 TIntermTyped *node = (aggregate->getSequence())[i]->getAsTyped();
1038 if(lValueErrorCheck(node->getLine(), "assign", node))
1039 {
1040 error(node->getLine(),
1041 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1042 recover();
1043 return true;
1044 }
1045 }
1046 }
1047 return false;
1048}
1049
Alexis Hetuad527752015-07-07 13:31:44 -04001050void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1051{
1052 switch(qualifier)
1053 {
1054 case EvqVaryingOut:
1055 case EvqSmoothOut:
1056 case EvqFlatOut:
1057 case EvqCentroidOut:
1058 case EvqVertexOut:
1059 case EvqFragmentOut:
1060 break;
1061 default:
1062 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1063 recover();
1064 break;
1065 }
1066}
1067
John Bauman66b8ab22014-05-06 15:57:45 -04001068bool TParseContext::supportsExtension(const char* extension)
1069{
1070 const TExtensionBehavior& extbehavior = extensionBehavior();
1071 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1072 return (iter != extbehavior.end());
1073}
1074
Alexis Hetufe1269e2015-06-16 12:43:32 -04001075void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001076{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001077 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001078 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001079}
1080
Alexis Hetufe1269e2015-06-16 12:43:32 -04001081void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value)
John Bauman66b8ab22014-05-06 15:57:45 -04001082{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001083 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001084 mDirectiveHandler.handlePragma(loc, name, value);
John Bauman66b8ab22014-05-06 15:57:45 -04001085}
1086
1087/////////////////////////////////////////////////////////////////////////////////
1088//
1089// Non-Errors.
1090//
1091/////////////////////////////////////////////////////////////////////////////////
1092
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001093const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1094 const TString *name,
1095 const TSymbol *symbol)
1096{
1097 const TVariable *variable = NULL;
1098
1099 if(!symbol)
1100 {
1101 error(location, "undeclared identifier", name->c_str());
1102 recover();
1103 }
1104 else if(!symbol->isVariable())
1105 {
1106 error(location, "variable expected", name->c_str());
1107 recover();
1108 }
1109 else
1110 {
1111 variable = static_cast<const TVariable*>(symbol);
1112
Alexis Hetu0a655842015-06-22 16:52:11 -04001113 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001114 {
1115 recover();
1116 }
1117
1118 // Reject shaders using both gl_FragData and gl_FragColor
1119 TQualifier qualifier = variable->getType().getQualifier();
1120 if(qualifier == EvqFragData)
1121 {
1122 mUsesFragData = true;
1123 }
1124 else if(qualifier == EvqFragColor)
1125 {
1126 mUsesFragColor = true;
1127 }
1128
1129 // This validation is not quite correct - it's only an error to write to
1130 // both FragData and FragColor. For simplicity, and because users shouldn't
Nicolas Capens8b124c12016-04-18 14:09:37 -04001131 // be rewarded for reading from undefined variables, return an error
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001132 // if they are both referenced, rather than assigned.
1133 if(mUsesFragData && mUsesFragColor)
1134 {
1135 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1136 recover();
1137 }
1138 }
1139
1140 if(!variable)
1141 {
1142 TType type(EbtFloat, EbpUndefined);
1143 TVariable *fakeVariable = new TVariable(name, type);
1144 symbolTable.declare(*fakeVariable);
1145 variable = fakeVariable;
1146 }
1147
1148 return variable;
1149}
1150
John Bauman66b8ab22014-05-06 15:57:45 -04001151//
1152// Look up a function name in the symbol table, and make sure it is a function.
1153//
1154// Return the function symbol if found, otherwise 0.
1155//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001156const TFunction* TParseContext::findFunction(const TSourceLoc &line, TFunction* call, bool *builtIn)
John Bauman66b8ab22014-05-06 15:57:45 -04001157{
1158 // First find by unmangled name to check whether the function name has been
1159 // hidden by a variable name or struct typename.
Alexis Hetu0a655842015-06-22 16:52:11 -04001160 const TSymbol* symbol = symbolTable.find(call->getName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001161 if (symbol == 0) {
Alexis Hetu0a655842015-06-22 16:52:11 -04001162 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001163 }
1164
1165 if (symbol == 0) {
1166 error(line, "no matching overloaded function found", call->getName().c_str());
1167 return 0;
1168 }
1169
1170 if (!symbol->isFunction()) {
1171 error(line, "function name expected", call->getName().c_str());
1172 return 0;
1173 }
1174
1175 return static_cast<const TFunction*>(symbol);
1176}
1177
1178//
1179// Initializers show up in several places in the grammar. Have one set of
1180// code to handle them here.
1181//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001182bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, const TPublicType& pType,
Alexis Hetue5246692015-06-18 12:34:52 -04001183 TIntermTyped *initializer, TIntermNode **intermNode)
John Bauman66b8ab22014-05-06 15:57:45 -04001184{
Alexis Hetue5246692015-06-18 12:34:52 -04001185 ASSERT(intermNode != nullptr);
1186 TType type = TType(pType);
John Bauman66b8ab22014-05-06 15:57:45 -04001187
Alexis Hetue5246692015-06-18 12:34:52 -04001188 if(type.isArray() && (type.getArraySize() == 0))
1189 {
1190 type.setArraySize(initializer->getArraySize());
1191 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001192
1193 TVariable *variable = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001194 if(!declareVariable(line, identifier, type, &variable))
1195 {
1196 return true;
1197 }
John Bauman66b8ab22014-05-06 15:57:45 -04001198
Alexis Hetue5246692015-06-18 12:34:52 -04001199 bool globalInitWarning = false;
1200 if(symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1201 {
1202 // Error message does not completely match behavior with ESSL 1.00, but
1203 // we want to steer developers towards only using constant expressions.
1204 error(line, "global variable initializers must be constant expressions", "=");
1205 return true;
1206 }
1207 if(globalInitWarning)
1208 {
1209 warning(line, "global variable initializers should be constant expressions "
1210 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
John Bauman66b8ab22014-05-06 15:57:45 -04001211 }
1212
1213 //
1214 // identifier must be of type constant, a global, or a temporary
1215 //
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001216 TQualifier qualifier = type.getQualifier();
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001217 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001218 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1219 return true;
1220 }
1221 //
1222 // test for and propagate constant
1223 //
1224
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001225 if (qualifier == EvqConstExpr) {
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001226 if (qualifier != initializer->getQualifier()) {
John Bauman66b8ab22014-05-06 15:57:45 -04001227 std::stringstream extraInfoStream;
1228 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1229 std::string extraInfo = extraInfoStream.str();
1230 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1231 variable->getType().setQualifier(EvqTemporary);
1232 return true;
1233 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001234
John Bauman66b8ab22014-05-06 15:57:45 -04001235 if (type != initializer->getType()) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001236 error(line, " non-matching types for const initializer ",
John Bauman66b8ab22014-05-06 15:57:45 -04001237 variable->getType().getQualifierString());
1238 variable->getType().setQualifier(EvqTemporary);
1239 return true;
1240 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001241
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001242 if (initializer->getAsConstantUnion()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001243 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
John Bauman66b8ab22014-05-06 15:57:45 -04001244 } else if (initializer->getAsSymbolNode()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001245 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001246 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1247
1248 ConstantUnion* constArray = tVar->getConstPointer();
1249 variable->shareConstPointer(constArray);
John Bauman66b8ab22014-05-06 15:57:45 -04001250 }
1251 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001252
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001253 if (!variable->isConstant()) {
John Bauman66b8ab22014-05-06 15:57:45 -04001254 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
Alexis Hetue5246692015-06-18 12:34:52 -04001255 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1256 if(*intermNode == nullptr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001257 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1258 return true;
1259 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001260 } else
Alexis Hetue5246692015-06-18 12:34:52 -04001261 *intermNode = nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04001262
1263 return false;
1264}
1265
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001266TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1267{
1268 TPublicType returnType = typeSpecifier;
1269 returnType.qualifier = qualifier;
1270 returnType.invariant = invariant;
1271 returnType.layoutQualifier = layoutQualifier;
1272
1273 if(typeSpecifier.array)
1274 {
1275 error(typeSpecifier.line, "not supported", "first-class array");
1276 recover();
1277 returnType.clearArrayness();
1278 }
1279
Alexis Hetu0a655842015-06-22 16:52:11 -04001280 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001281 {
1282 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1283 {
1284 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1285 recover();
1286 }
1287
1288 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1289 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1290 {
1291 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1292 recover();
1293 }
1294 }
1295 else
1296 {
1297 switch(qualifier)
1298 {
1299 case EvqSmoothIn:
1300 case EvqSmoothOut:
1301 case EvqVertexOut:
1302 case EvqFragmentIn:
1303 case EvqCentroidOut:
1304 case EvqCentroidIn:
1305 if(typeSpecifier.type == EbtBool)
1306 {
1307 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1308 recover();
1309 }
1310 if(typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1311 {
1312 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1313 recover();
1314 }
1315 break;
1316
1317 case EvqVertexIn:
1318 case EvqFragmentOut:
1319 case EvqFlatIn:
1320 case EvqFlatOut:
1321 if(typeSpecifier.type == EbtBool)
1322 {
1323 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1324 recover();
1325 }
1326 break;
1327
1328 default: break;
1329 }
1330 }
1331
1332 return returnType;
1333}
1334
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001335TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1336 const TSourceLoc &identifierOrTypeLocation,
1337 const TString &identifier)
1338{
1339 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1340
1341 bool emptyDeclaration = (identifier == "");
1342
1343 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1344
1345 if(emptyDeclaration)
1346 {
1347 if(publicType.isUnsizedArray())
1348 {
1349 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1350 // It is assumed that this applies to empty declarations as well.
1351 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1352 }
1353 }
1354 else
1355 {
1356 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1357 recover();
1358
1359 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1360 recover();
1361
1362 TVariable *variable = nullptr;
1363 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1364 recover();
1365
1366 if(variable && symbol)
1367 symbol->setId(variable->getUniqueId());
1368 }
1369
1370 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1371}
1372
1373TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1374 const TSourceLoc &identifierLocation,
1375 const TString &identifier,
1376 const TSourceLoc &indexLocation,
1377 TIntermTyped *indexExpression)
1378{
1379 mDeferredSingleDeclarationErrorCheck = false;
1380
1381 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1382 recover();
1383
1384 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1385 recover();
1386
1387 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1388 {
1389 recover();
1390 }
1391
1392 TType arrayType(publicType);
1393
1394 int size;
1395 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1396 {
1397 recover();
1398 }
1399 // Make the type an array even if size check failed.
1400 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1401 arrayType.setArraySize(size);
1402
1403 TVariable *variable = nullptr;
1404 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1405 recover();
1406
1407 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1408 if(variable && symbol)
1409 symbol->setId(variable->getUniqueId());
1410
1411 return intermediate.makeAggregate(symbol, identifierLocation);
1412}
1413
1414TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1415 const TSourceLoc &identifierLocation,
1416 const TString &identifier,
1417 const TSourceLoc &initLocation,
1418 TIntermTyped *initializer)
1419{
1420 mDeferredSingleDeclarationErrorCheck = false;
1421
1422 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1423 recover();
1424
1425 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001426 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001427 {
1428 //
1429 // Build intermediate representation
1430 //
1431 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1432 }
1433 else
1434 {
1435 recover();
1436 return nullptr;
1437 }
1438}
1439
1440TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1441 const TSourceLoc &identifierLocation,
1442 const TString &identifier,
1443 const TSourceLoc &indexLocation,
1444 TIntermTyped *indexExpression,
1445 const TSourceLoc &initLocation,
1446 TIntermTyped *initializer)
1447{
1448 mDeferredSingleDeclarationErrorCheck = false;
1449
1450 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1451 recover();
1452
1453 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1454 {
1455 recover();
1456 }
1457
1458 TPublicType arrayType(publicType);
1459
1460 int size = 0;
1461 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1462 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1463 {
1464 recover();
1465 }
1466 // Make the type an array even if size check failed.
1467 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1468 arrayType.setArray(true, size);
1469
1470 // initNode will correspond to the whole of "type b[n] = initializer".
1471 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001472 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001473 {
1474 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1475 }
1476 else
1477 {
1478 recover();
1479 return nullptr;
1480 }
1481}
1482
1483TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1484 const TSourceLoc &identifierLoc,
1485 const TString *identifier,
1486 const TSymbol *symbol)
1487{
1488 // invariant declaration
1489 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1490 {
1491 recover();
1492 }
1493
1494 if(!symbol)
1495 {
1496 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1497 recover();
1498 return nullptr;
1499 }
1500 else
1501 {
1502 const TString kGlFrontFacing("gl_FrontFacing");
1503 if(*identifier == kGlFrontFacing)
1504 {
1505 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1506 recover();
1507 return nullptr;
1508 }
1509 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1510 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1511 ASSERT(variable);
1512 const TType &type = variable->getType();
1513 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1514 *identifier, type, identifierLoc);
1515
1516 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1517 aggregate->setOp(EOpInvariantDeclaration);
1518 return aggregate;
1519 }
1520}
1521
1522TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1523 const TSourceLoc &identifierLocation, const TString &identifier)
1524{
1525 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1526 if(mDeferredSingleDeclarationErrorCheck)
1527 {
1528 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1529 recover();
1530 mDeferredSingleDeclarationErrorCheck = false;
1531 }
1532
1533 if(locationDeclaratorListCheck(identifierLocation, publicType))
1534 recover();
1535
1536 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1537 recover();
1538
1539 TVariable *variable = nullptr;
1540 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1541 recover();
1542
1543 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1544 if(variable && symbol)
1545 symbol->setId(variable->getUniqueId());
1546
1547 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1548}
1549
1550TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1551 const TSourceLoc &identifierLocation, const TString &identifier,
1552 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1553{
1554 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1555 if(mDeferredSingleDeclarationErrorCheck)
1556 {
1557 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1558 recover();
1559 mDeferredSingleDeclarationErrorCheck = false;
1560 }
1561
1562 if(locationDeclaratorListCheck(identifierLocation, publicType))
1563 recover();
1564
1565 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1566 recover();
1567
1568 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1569 {
1570 recover();
1571 }
1572 else
1573 {
1574 TType arrayType = TType(publicType);
1575 int size;
1576 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1577 {
1578 recover();
1579 }
1580 arrayType.setArraySize(size);
1581
1582 TVariable *variable = nullptr;
1583 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1584 recover();
1585
1586 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1587 if(variable && symbol)
1588 symbol->setId(variable->getUniqueId());
1589
1590 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1591 }
1592
1593 return nullptr;
1594}
1595
1596TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1597 const TSourceLoc &identifierLocation, const TString &identifier,
1598 const TSourceLoc &initLocation, TIntermTyped *initializer)
1599{
1600 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1601 if(mDeferredSingleDeclarationErrorCheck)
1602 {
1603 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1604 recover();
1605 mDeferredSingleDeclarationErrorCheck = false;
1606 }
1607
1608 if(locationDeclaratorListCheck(identifierLocation, publicType))
1609 recover();
1610
1611 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001612 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001613 {
1614 //
1615 // build the intermediate representation
1616 //
1617 if(intermNode)
1618 {
1619 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1620 }
1621 else
1622 {
1623 return aggregateDeclaration;
1624 }
1625 }
1626 else
1627 {
1628 recover();
1629 return nullptr;
1630 }
1631}
1632
1633TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1634 TIntermAggregate *aggregateDeclaration,
1635 const TSourceLoc &identifierLocation,
1636 const TString &identifier,
1637 const TSourceLoc &indexLocation,
1638 TIntermTyped *indexExpression,
1639 const TSourceLoc &initLocation, TIntermTyped *initializer)
1640{
1641 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1642 if(mDeferredSingleDeclarationErrorCheck)
1643 {
1644 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1645 recover();
1646 mDeferredSingleDeclarationErrorCheck = false;
1647 }
1648
1649 if(locationDeclaratorListCheck(identifierLocation, publicType))
1650 recover();
1651
1652 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1653 {
1654 recover();
1655 }
1656
1657 TPublicType arrayType(publicType);
1658
1659 int size = 0;
1660 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1661 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1662 {
1663 recover();
1664 }
1665 // Make the type an array even if size check failed.
1666 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1667 arrayType.setArray(true, size);
1668
1669 // initNode will correspond to the whole of "b[n] = initializer".
1670 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001671 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001672 {
1673 if(initNode)
1674 {
1675 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1676 }
1677 else
1678 {
1679 return aggregateDeclaration;
1680 }
1681 }
1682 else
1683 {
1684 recover();
1685 return nullptr;
1686 }
1687}
1688
Alexis Hetua35d8232015-06-11 17:11:06 -04001689void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1690{
Alexis Hetu0a655842015-06-22 16:52:11 -04001691 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001692 {
1693 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1694 recover();
1695 return;
1696 }
1697
1698 if(typeQualifier.qualifier != EvqUniform)
1699 {
1700 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1701 recover();
1702 return;
1703 }
1704
1705 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1706 ASSERT(!layoutQualifier.isEmpty());
1707
1708 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1709 {
1710 recover();
1711 return;
1712 }
1713
1714 if(layoutQualifier.matrixPacking != EmpUnspecified)
1715 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001716 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001717 }
1718
1719 if(layoutQualifier.blockStorage != EbsUnspecified)
1720 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001721 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001722 }
1723}
1724
Alexis Hetu407813b2016-02-24 16:46:13 -05001725TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1726{
1727 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1728 // Either way the instance in the symbol table is used to track whether the function is declared
1729 // multiple times.
1730 TFunction *symbolTableFunction =
1731 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1732 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1733 {
1734 // ESSL 1.00.17 section 4.2.7.
1735 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1736 error(location, "duplicate function prototype declarations are not allowed", "function");
1737 recover();
1738 }
1739 symbolTableFunction->setHasPrototypeDeclaration();
1740
1741 TIntermAggregate *prototype = new TIntermAggregate;
1742 prototype->setType(function.getReturnType());
1743 prototype->setName(function.getMangledName());
1744
1745 for(size_t i = 0; i < function.getParamCount(); i++)
1746 {
1747 const TParameter &param = function.getParam(i);
1748 if(param.name != 0)
1749 {
1750 TVariable variable(param.name, *param.type);
1751
1752 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1753 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1754 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1755 }
1756 else
1757 {
1758 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1759 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1760 }
1761 }
1762
1763 prototype->setOp(EOpPrototype);
1764
1765 symbolTable.pop();
1766
1767 if(!symbolTable.atGlobalLevel())
1768 {
1769 // ESSL 3.00.4 section 4.2.4.
1770 error(location, "local function prototype declarations are not allowed", "function");
1771 recover();
1772 }
1773
1774 return prototype;
1775}
1776
1777TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1778{
1779 //?? Check that all paths return a value if return type != void ?
1780 // May be best done as post process phase on intermediate code
1781 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1782 {
1783 error(location, "function does not return a value:", "", function.getName().c_str());
1784 recover();
1785 }
1786
1787 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1788 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1789 aggregate->setName(function.getMangledName().c_str());
1790 aggregate->setType(function.getReturnType());
1791
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001792 // store the pragma information for debug and optimize and other vendor specific
1793 // information. This information can be queried from the parse tree
1794 aggregate->setOptimize(pragma().optimize);
Alexis Hetu407813b2016-02-24 16:46:13 -05001795 aggregate->setDebug(pragma().debug);
1796
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001797 if(functionBody && functionBody->getAsAggregate())
Alexis Hetu407813b2016-02-24 16:46:13 -05001798 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1799
1800 symbolTable.pop();
1801 return aggregate;
1802}
1803
1804void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1805{
1806 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1807
1808 if(builtIn)
1809 {
1810 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1811 recover();
1812 }
1813
1814 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1815 //
1816 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1817 // as it would have just been put in the symbol table. Otherwise, we're looking up
1818 // an earlier occurance.
1819 //
1820 if(prevDec->isDefined())
1821 {
1822 // Then this function already has a body.
1823 error(location, "function already has a body", function->getName().c_str());
1824 recover();
1825 }
1826 prevDec->setDefined();
1827 //
1828 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1829 // Eventually we will probably want to have only a single definition and just swap the
1830 // arguments to be the definition's arguments.
1831 //
1832 function->setUniqueId(prevDec->getUniqueId());
1833
1834 // Raise error message if main function takes any parameters or return anything other than void
1835 if(function->getName() == "main")
1836 {
1837 if(function->getParamCount() > 0)
1838 {
1839 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1840 recover();
1841 }
1842 if(function->getReturnType().getBasicType() != EbtVoid)
1843 {
1844 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1845 recover();
1846 }
1847 }
1848
1849 //
1850 // Remember the return type for later checking for RETURN statements.
1851 //
1852 mCurrentFunctionType = &(prevDec->getReturnType());
1853 mFunctionReturnsValue = false;
1854
1855 //
1856 // Insert parameters into the symbol table.
1857 // If the parameter has no name, it's not an error, just don't insert it
1858 // (could be used for unused args).
1859 //
1860 // Also, accumulate the list of parameters into the HIL, so lower level code
1861 // knows where to find parameters.
1862 //
1863 TIntermAggregate *paramNodes = new TIntermAggregate;
1864 for(size_t i = 0; i < function->getParamCount(); i++)
1865 {
1866 const TParameter &param = function->getParam(i);
1867 if(param.name != 0)
1868 {
1869 TVariable *variable = new TVariable(param.name, *param.type);
1870 //
1871 // Insert the parameters with name in the symbol table.
1872 //
1873 if(!symbolTable.declare(*variable))
1874 {
1875 error(location, "redefinition", variable->getName().c_str());
1876 recover();
1877 paramNodes = intermediate.growAggregate(
1878 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1879 continue;
1880 }
1881
1882 //
1883 // Add the parameter to the HIL
1884 //
1885 TIntermSymbol *symbol = intermediate.addSymbol(
1886 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1887
1888 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1889 }
1890 else
1891 {
1892 paramNodes = intermediate.growAggregate(
1893 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1894 }
1895 }
1896 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1897 *aggregateOut = paramNodes;
1898 setLoopNestingLevel(0);
1899}
1900
1901TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1902{
1903 //
1904 // We don't know at this point whether this is a function definition or a prototype.
1905 // The definition production code will check for redefinitions.
1906 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
1907 //
1908 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
1909 // here.
1910 //
1911 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1912 if(prevDec)
1913 {
1914 if(prevDec->getReturnType() != function->getReturnType())
1915 {
1916 error(location, "overloaded functions must have the same return type",
1917 function->getReturnType().getBasicString());
1918 recover();
1919 }
1920 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
1921 {
1922 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
1923 {
1924 error(location, "overloaded functions must have the same parameter qualifiers",
1925 function->getParam(i).type->getQualifierString());
1926 recover();
1927 }
1928 }
1929 }
1930
1931 //
1932 // Check for previously declared variables using the same name.
1933 //
1934 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
1935 if(prevSym)
1936 {
1937 if(!prevSym->isFunction())
1938 {
1939 error(location, "redefinition", function->getName().c_str(), "function");
1940 recover();
1941 }
1942 }
1943
1944 // We're at the inner scope level of the function's arguments and body statement.
1945 // Add the function prototype to the surrounding scope instead.
1946 symbolTable.getOuterLevel()->insert(*function);
1947
1948 //
1949 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
1950 // variable names from this one, and not the one that's
1951 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
1952 //
1953 return function;
1954}
1955
Alexis Hetue5246692015-06-18 12:34:52 -04001956TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
1957{
1958 TPublicType publicType = publicTypeIn;
1959 TOperator op = EOpNull;
1960 if(publicType.userDef)
1961 {
1962 op = EOpConstructStruct;
1963 }
1964 else
1965 {
1966 switch(publicType.type)
1967 {
Nicolas Capens5d961882016-01-01 23:18:14 -05001968 case EbtFloat:
1969 if(publicType.isMatrix())
1970 {
1971 switch(publicType.getCols())
1972 {
1973 case 2:
1974 switch(publicType.getRows())
1975 {
1976 case 2: op = EOpConstructMat2; break;
1977 case 3: op = EOpConstructMat2x3; break;
1978 case 4: op = EOpConstructMat2x4; break;
1979 }
1980 break;
1981 case 3:
1982 switch(publicType.getRows())
1983 {
1984 case 2: op = EOpConstructMat3x2; break;
1985 case 3: op = EOpConstructMat3; break;
1986 case 4: op = EOpConstructMat3x4; break;
1987 }
1988 break;
1989 case 4:
1990 switch(publicType.getRows())
1991 {
1992 case 2: op = EOpConstructMat4x2; break;
1993 case 3: op = EOpConstructMat4x3; break;
1994 case 4: op = EOpConstructMat4; break;
1995 }
1996 break;
1997 }
1998 }
1999 else
2000 {
2001 switch(publicType.getNominalSize())
2002 {
2003 case 1: op = EOpConstructFloat; break;
2004 case 2: op = EOpConstructVec2; break;
2005 case 3: op = EOpConstructVec3; break;
2006 case 4: op = EOpConstructVec4; break;
2007 }
2008 }
Alexis Hetue5246692015-06-18 12:34:52 -04002009 break;
2010
2011 case EbtInt:
2012 switch(publicType.getNominalSize())
2013 {
2014 case 1: op = EOpConstructInt; break;
2015 case 2: op = EOpConstructIVec2; break;
2016 case 3: op = EOpConstructIVec3; break;
2017 case 4: op = EOpConstructIVec4; break;
2018 }
2019 break;
2020
2021 case EbtUInt:
2022 switch(publicType.getNominalSize())
2023 {
2024 case 1: op = EOpConstructUInt; break;
2025 case 2: op = EOpConstructUVec2; break;
2026 case 3: op = EOpConstructUVec3; break;
2027 case 4: op = EOpConstructUVec4; break;
2028 }
2029 break;
2030
2031 case EbtBool:
2032 switch(publicType.getNominalSize())
2033 {
2034 case 1: op = EOpConstructBool; break;
2035 case 2: op = EOpConstructBVec2; break;
2036 case 3: op = EOpConstructBVec3; break;
2037 case 4: op = EOpConstructBVec4; break;
2038 }
2039 break;
2040
2041 default: break;
2042 }
2043
2044 if(op == EOpNull)
2045 {
2046 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2047 recover();
2048 publicType.type = EbtFloat;
2049 op = EOpConstructFloat;
2050 }
2051 }
2052
2053 TString tempString;
2054 TType type(publicType);
2055 return new TFunction(&tempString, type, op);
2056}
2057
John Bauman66b8ab22014-05-06 15:57:45 -04002058// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002059// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002060//
2061// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2062//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002063TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002064{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002065 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002066
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002067 if(!aggregateArguments)
2068 {
2069 aggregateArguments = new TIntermAggregate;
2070 aggregateArguments->getSequence().push_back(arguments);
John Bauman66b8ab22014-05-06 15:57:45 -04002071 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002072
2073 if(op == EOpConstructStruct)
2074 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002075 const TFieldList &fields = type->getStruct()->fields();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002076 TIntermSequence &args = aggregateArguments->getSequence();
2077
2078 for(size_t i = 0; i < fields.size(); i++)
2079 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002080 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002081 {
2082 error(line, "Structure constructor arguments do not match structure fields", "Error");
2083 recover();
2084
2085 return 0;
2086 }
John Bauman66b8ab22014-05-06 15:57:45 -04002087 }
2088 }
2089
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002090 // Turn the argument list itself into a constructor
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002091 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2092 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002093 if(constConstructor)
2094 {
John Bauman66b8ab22014-05-06 15:57:45 -04002095 return constConstructor;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002096 }
John Bauman66b8ab22014-05-06 15:57:45 -04002097
2098 return constructor;
2099}
2100
2101TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2102{
John Bauman66b8ab22014-05-06 15:57:45 -04002103 aggrNode->setType(type);
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002104 if (aggrNode->isConstantFoldable()) {
John Bauman66b8ab22014-05-06 15:57:45 -04002105 bool returnVal = false;
2106 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2107 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04002108 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04002109 }
2110 else {
John Baumand4ae8632014-05-06 16:18:33 -04002111 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04002112 }
2113 if (returnVal)
2114 return 0;
2115
2116 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2117 }
2118
2119 return 0;
2120}
2121
John Bauman66b8ab22014-05-06 15:57:45 -04002122//
2123// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2124// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2125// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002126// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
John Bauman66b8ab22014-05-06 15:57:45 -04002127// a constant matrix.
2128//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002129TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002130{
2131 TIntermTyped* typedNode;
2132 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2133
2134 ConstantUnion *unionArray;
2135 if (tempConstantNode) {
2136 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002137
2138 if (!unionArray) {
2139 return node;
2140 }
2141 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2142 error(line, "Cannot offset into the vector", "Error");
2143 recover();
2144
2145 return 0;
2146 }
2147
2148 ConstantUnion* constArray = new ConstantUnion[fields.num];
2149
2150 for (int i = 0; i < fields.num; i++) {
2151 if (fields.offsets[i] >= node->getType().getObjectSize()) {
2152 std::stringstream extraInfoStream;
2153 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2154 std::string extraInfo = extraInfoStream.str();
2155 error(line, "", "[", extraInfo.c_str());
2156 recover();
2157 fields.offsets[i] = 0;
2158 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002159
John Bauman66b8ab22014-05-06 15:57:45 -04002160 constArray[i] = unionArray[fields.offsets[i]];
2161
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002162 }
John Bauman66b8ab22014-05-06 15:57:45 -04002163 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2164 return typedNode;
2165}
2166
2167//
2168// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002169// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2170// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
John Bauman66b8ab22014-05-06 15:57:45 -04002171// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2172//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002173TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002174{
2175 TIntermTyped* typedNode;
2176 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2177
2178 if (index >= node->getType().getNominalSize()) {
2179 std::stringstream extraInfoStream;
2180 extraInfoStream << "matrix field selection out of range '" << index << "'";
2181 std::string extraInfo = extraInfoStream.str();
2182 error(line, "", "[", extraInfo.c_str());
2183 recover();
2184 index = 0;
2185 }
2186
2187 if (tempConstantNode) {
2188 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2189 int size = tempConstantNode->getType().getNominalSize();
2190 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2191 } else {
2192 error(line, "Cannot offset into the matrix", "Error");
2193 recover();
2194
2195 return 0;
2196 }
2197
2198 return typedNode;
2199}
2200
2201
2202//
2203// This function returns an element of an array accessed from a constant array. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002204// the symbol table and parse-tree is built for the type of the element. The input
2205// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
John Bauman66b8ab22014-05-06 15:57:45 -04002206// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2207//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002208TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002209{
2210 TIntermTyped* typedNode;
2211 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2212 TType arrayElementType = node->getType();
2213 arrayElementType.clearArrayness();
2214
2215 if (index >= node->getType().getArraySize()) {
2216 std::stringstream extraInfoStream;
2217 extraInfoStream << "array field selection out of range '" << index << "'";
2218 std::string extraInfo = extraInfoStream.str();
2219 error(line, "", "[", extraInfo.c_str());
2220 recover();
2221 index = 0;
2222 }
2223
Alexis Hetuab752792016-04-21 16:11:31 -04002224 size_t arrayElementSize = arrayElementType.getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002225
2226 if (tempConstantNode) {
2227 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2228 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2229 } else {
2230 error(line, "Cannot offset into the array", "Error");
2231 recover();
2232
2233 return 0;
2234 }
2235
2236 return typedNode;
2237}
2238
2239
2240//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002241// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002242// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2243// function and returns the parse-tree with the values of the embedded/nested struct.
2244//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002245TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002246{
Alexis Hetua8b364b2015-06-10 11:48:40 -04002247 const TFieldList &fields = node->getType().getStruct()->fields();
John Bauman66b8ab22014-05-06 15:57:45 -04002248 TIntermTyped *typedNode;
Alexis Hetuab752792016-04-21 16:11:31 -04002249 size_t instanceSize = 0;
John Bauman66b8ab22014-05-06 15:57:45 -04002250 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
2251
Alexis Hetuab752792016-04-21 16:11:31 -04002252 for(size_t index = 0; index < fields.size(); ++index) {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002253 if (fields[index]->name() == identifier) {
John Bauman66b8ab22014-05-06 15:57:45 -04002254 break;
2255 } else {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002256 instanceSize += fields[index]->type()->getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002257 }
2258 }
2259
2260 if (tempConstantNode) {
2261 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
2262
2263 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2264 } else {
2265 error(line, "Cannot offset into the structure", "Error");
2266 recover();
2267
2268 return 0;
2269 }
2270
2271 return typedNode;
2272}
2273
Alexis Hetuad6b8752015-06-09 16:15:30 -04002274//
Alexis Hetua35d8232015-06-11 17:11:06 -04002275// Interface/uniform blocks
2276//
2277TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
2278 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
2279{
2280 if(reservedErrorCheck(nameLine, blockName))
2281 recover();
2282
2283 if(typeQualifier.qualifier != EvqUniform)
2284 {
2285 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2286 recover();
2287 }
2288
2289 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2290 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2291 {
2292 recover();
2293 }
2294
2295 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2296 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002297 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002298 }
2299
2300 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2301 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002302 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002303 }
2304
2305 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2306 if(!symbolTable.declare(*blockNameSymbol)) {
2307 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2308 recover();
2309 }
2310
2311 // check for sampler types and apply layout qualifiers
2312 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2313 TField* field = (*fieldList)[memberIndex];
2314 TType* fieldType = field->type();
2315 if(IsSampler(fieldType->getBasicType())) {
2316 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2317 recover();
2318 }
2319
2320 const TQualifier qualifier = fieldType->getQualifier();
2321 switch(qualifier)
2322 {
2323 case EvqGlobal:
2324 case EvqUniform:
2325 break;
2326 default:
2327 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2328 recover();
2329 break;
2330 }
2331
2332 // check layout qualifiers
2333 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2334 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2335 {
2336 recover();
2337 }
2338
2339 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2340 {
2341 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2342 recover();
2343 }
2344
2345 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2346 {
2347 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2348 }
2349 else if(!fieldType->isMatrix())
2350 {
2351 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
2352 recover();
2353 }
2354
2355 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2356 }
2357
2358 // add array index
2359 int arraySize = 0;
2360 if(arrayIndex != NULL)
2361 {
2362 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2363 recover();
2364 }
2365
2366 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2367 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2368
2369 TString symbolName = "";
2370 int symbolId = 0;
2371
2372 if(!instanceName)
2373 {
2374 // define symbols for the members of the interface block
2375 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2376 {
2377 TField* field = (*fieldList)[memberIndex];
2378 TType* fieldType = field->type();
2379
2380 // set parent pointer of the field variable
2381 fieldType->setInterfaceBlock(interfaceBlock);
2382
2383 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2384 fieldVariable->setQualifier(typeQualifier.qualifier);
2385
2386 if(!symbolTable.declare(*fieldVariable)) {
2387 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2388 recover();
2389 }
2390 }
2391 }
2392 else
2393 {
2394 // add a symbol for this interface block
2395 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2396 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2397
2398 if(!symbolTable.declare(*instanceTypeDef)) {
2399 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2400 recover();
2401 }
2402
2403 symbolId = instanceTypeDef->getUniqueId();
2404 symbolName = instanceTypeDef->getName();
2405 }
2406
2407 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2408 aggregate->setOp(EOpDeclaration);
2409
2410 exitStructDeclaration();
2411 return aggregate;
2412}
2413
2414//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002415// Parse an array index expression
2416//
2417TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2418{
2419 TIntermTyped *indexedExpression = NULL;
2420
2421 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2422 {
2423 if(baseExpression->getAsSymbolNode())
2424 {
2425 error(location, " left of '[' is not of type array, matrix, or vector ",
2426 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2427 }
2428 else
2429 {
2430 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2431 }
2432 recover();
2433 }
2434
2435 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2436
2437 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2438 {
2439 int index = indexConstantUnion->getIConst(0);
2440 if(index < 0)
2441 {
2442 std::stringstream infoStream;
2443 infoStream << index;
2444 std::string info = infoStream.str();
2445 error(location, "negative index", info.c_str());
2446 recover();
2447 index = 0;
2448 }
2449 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2450 {
2451 if(baseExpression->isArray())
2452 {
2453 // constant folding for arrays
2454 indexedExpression = addConstArrayNode(index, baseExpression, location);
2455 }
2456 else if(baseExpression->isVector())
2457 {
2458 // constant folding for vectors
2459 TVectorFields fields;
2460 fields.num = 1;
2461 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2462 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2463 }
2464 else if(baseExpression->isMatrix())
2465 {
2466 // constant folding for matrices
2467 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2468 }
2469 }
2470 else
2471 {
2472 int safeIndex = -1;
2473
2474 if(baseExpression->isArray())
2475 {
2476 if(index >= baseExpression->getType().getArraySize())
2477 {
2478 std::stringstream extraInfoStream;
2479 extraInfoStream << "array index out of range '" << index << "'";
2480 std::string extraInfo = extraInfoStream.str();
2481 error(location, "", "[", extraInfo.c_str());
2482 recover();
2483 safeIndex = baseExpression->getType().getArraySize() - 1;
2484 }
2485 }
2486 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2487 baseExpression->getType().getNominalSize() <= index)
2488 {
2489 std::stringstream extraInfoStream;
2490 extraInfoStream << "field selection out of range '" << index << "'";
2491 std::string extraInfo = extraInfoStream.str();
2492 error(location, "", "[", extraInfo.c_str());
2493 recover();
2494 safeIndex = baseExpression->getType().getNominalSize() - 1;
2495 }
2496
2497 // Don't modify the data of the previous constant union, because it can point
2498 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2499 if(safeIndex != -1)
2500 {
2501 ConstantUnion *safeConstantUnion = new ConstantUnion();
2502 safeConstantUnion->setIConst(safeIndex);
2503 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2504 }
2505
2506 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2507 }
2508 }
2509 else
2510 {
2511 if(baseExpression->isInterfaceBlock())
2512 {
2513 error(location, "",
2514 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2515 recover();
2516 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002517 else if(baseExpression->getQualifier() == EvqFragmentOut)
2518 {
2519 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2520 recover();
2521 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002522
2523 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2524 }
2525
2526 if(indexedExpression == 0)
2527 {
2528 ConstantUnion *unionArray = new ConstantUnion[1];
2529 unionArray->setFConst(0.0f);
2530 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2531 }
2532 else if(baseExpression->isArray())
2533 {
2534 const TType &baseType = baseExpression->getType();
2535 if(baseType.getStruct())
2536 {
2537 TType copyOfType(baseType.getStruct());
2538 indexedExpression->setType(copyOfType);
2539 }
2540 else if(baseType.isInterfaceBlock())
2541 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002542 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002543 indexedExpression->setType(copyOfType);
2544 }
2545 else
2546 {
2547 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2548 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2549 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2550 }
2551
2552 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2553 {
2554 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2555 }
2556 }
2557 else if(baseExpression->isMatrix())
2558 {
2559 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2560 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2561 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2562 }
2563 else if(baseExpression->isVector())
2564 {
2565 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2566 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2567 }
2568 else
2569 {
2570 indexedExpression->setType(baseExpression->getType());
2571 }
2572
2573 return indexedExpression;
2574}
2575
2576TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2577 const TString &fieldString, const TSourceLoc &fieldLocation)
2578{
2579 TIntermTyped *indexedExpression = NULL;
2580
2581 if(baseExpression->isArray())
2582 {
2583 error(fieldLocation, "cannot apply dot operator to an array", ".");
2584 recover();
2585 }
2586
2587 if(baseExpression->isVector())
2588 {
2589 TVectorFields fields;
2590 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2591 {
2592 fields.num = 1;
2593 fields.offsets[0] = 0;
2594 recover();
2595 }
2596
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002597 if(baseExpression->getAsConstantUnion())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002598 {
2599 // constant folding for vector fields
2600 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2601 if(indexedExpression == 0)
2602 {
2603 recover();
2604 indexedExpression = baseExpression;
2605 }
2606 else
2607 {
2608 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2609 EvqConstExpr, (unsigned char)(fieldString).size()));
2610 }
2611 }
2612 else
2613 {
2614 TString vectorString = fieldString;
2615 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2616 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2617 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
Nicolas Capens341afbb2016-04-10 01:54:50 -04002618 baseExpression->getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary, (unsigned char)vectorString.size()));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002619 }
2620 }
2621 else if(baseExpression->isMatrix())
2622 {
2623 TMatrixFields fields;
2624 if(!parseMatrixFields(fieldString, baseExpression->getNominalSize(), baseExpression->getSecondarySize(), fields, fieldLocation))
2625 {
2626 fields.wholeRow = false;
2627 fields.wholeCol = false;
2628 fields.row = 0;
2629 fields.col = 0;
2630 recover();
2631 }
2632
2633 if(fields.wholeRow || fields.wholeCol)
2634 {
2635 error(dotLocation, " non-scalar fields not implemented yet", ".");
2636 recover();
2637 ConstantUnion *unionArray = new ConstantUnion[1];
2638 unionArray->setIConst(0);
2639 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2640 fieldLocation);
2641 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2642 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2643 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2644 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2645 }
2646 else
2647 {
2648 ConstantUnion *unionArray = new ConstantUnion[1];
2649 unionArray->setIConst(fields.col * baseExpression->getSecondarySize() + fields.row);
2650 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2651 fieldLocation);
2652 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2653 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2654 }
2655 }
2656 else if(baseExpression->getBasicType() == EbtStruct)
2657 {
2658 bool fieldFound = false;
2659 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2660 if(fields.empty())
2661 {
2662 error(dotLocation, "structure has no fields", "Internal Error");
2663 recover();
2664 indexedExpression = baseExpression;
2665 }
2666 else
2667 {
2668 unsigned int i;
2669 for(i = 0; i < fields.size(); ++i)
2670 {
2671 if(fields[i]->name() == fieldString)
2672 {
2673 fieldFound = true;
2674 break;
2675 }
2676 }
2677 if(fieldFound)
2678 {
2679 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2680 {
2681 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2682 if(indexedExpression == 0)
2683 {
2684 recover();
2685 indexedExpression = baseExpression;
2686 }
2687 else
2688 {
2689 indexedExpression->setType(*fields[i]->type());
2690 // change the qualifier of the return type, not of the structure field
2691 // as the structure definition is shared between various structures.
2692 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2693 }
2694 }
2695 else
2696 {
2697 ConstantUnion *unionArray = new ConstantUnion[1];
2698 unionArray->setIConst(i);
2699 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2700 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2701 indexedExpression->setType(*fields[i]->type());
2702 }
2703 }
2704 else
2705 {
2706 error(dotLocation, " no such field in structure", fieldString.c_str());
2707 recover();
2708 indexedExpression = baseExpression;
2709 }
2710 }
2711 }
2712 else if(baseExpression->isInterfaceBlock())
2713 {
2714 bool fieldFound = false;
2715 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2716 if(fields.empty())
2717 {
2718 error(dotLocation, "interface block has no fields", "Internal Error");
2719 recover();
2720 indexedExpression = baseExpression;
2721 }
2722 else
2723 {
2724 unsigned int i;
2725 for(i = 0; i < fields.size(); ++i)
2726 {
2727 if(fields[i]->name() == fieldString)
2728 {
2729 fieldFound = true;
2730 break;
2731 }
2732 }
2733 if(fieldFound)
2734 {
2735 ConstantUnion *unionArray = new ConstantUnion[1];
2736 unionArray->setIConst(i);
2737 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2738 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2739 dotLocation);
2740 indexedExpression->setType(*fields[i]->type());
2741 }
2742 else
2743 {
2744 error(dotLocation, " no such field in interface block", fieldString.c_str());
2745 recover();
2746 indexedExpression = baseExpression;
2747 }
2748 }
2749 }
2750 else
2751 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002752 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002753 {
2754 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2755 fieldString.c_str());
2756 }
2757 else
2758 {
2759 error(dotLocation,
2760 " field selection requires structure, vector, matrix, or interface block on left hand side",
2761 fieldString.c_str());
2762 }
2763 recover();
2764 indexedExpression = baseExpression;
2765 }
2766
2767 return indexedExpression;
2768}
2769
Nicolas Capens7d626792015-02-17 17:58:31 -05002770TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2771{
2772 TLayoutQualifier qualifier;
2773
2774 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002775 qualifier.matrixPacking = EmpUnspecified;
2776 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002777
Alexis Hetuad6b8752015-06-09 16:15:30 -04002778 if(qualifierType == "shared")
2779 {
2780 qualifier.blockStorage = EbsShared;
2781 }
2782 else if(qualifierType == "packed")
2783 {
2784 qualifier.blockStorage = EbsPacked;
2785 }
2786 else if(qualifierType == "std140")
2787 {
2788 qualifier.blockStorage = EbsStd140;
2789 }
2790 else if(qualifierType == "row_major")
2791 {
2792 qualifier.matrixPacking = EmpRowMajor;
2793 }
2794 else if(qualifierType == "column_major")
2795 {
2796 qualifier.matrixPacking = EmpColumnMajor;
2797 }
2798 else if(qualifierType == "location")
Nicolas Capens7d626792015-02-17 17:58:31 -05002799 {
2800 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2801 recover();
2802 }
2803 else
2804 {
2805 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2806 recover();
2807 }
2808
2809 return qualifier;
2810}
2811
2812TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2813{
2814 TLayoutQualifier qualifier;
2815
2816 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002817 qualifier.matrixPacking = EmpUnspecified;
2818 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002819
2820 if (qualifierType != "location")
2821 {
2822 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2823 recover();
2824 }
2825 else
2826 {
2827 // must check that location is non-negative
2828 if (intValue < 0)
2829 {
2830 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2831 recover();
2832 }
2833 else
2834 {
2835 qualifier.location = intValue;
2836 }
2837 }
2838
2839 return qualifier;
2840}
2841
2842TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2843{
2844 TLayoutQualifier joinedQualifier = leftQualifier;
2845
2846 if (rightQualifier.location != -1)
2847 {
2848 joinedQualifier.location = rightQualifier.location;
2849 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002850 if(rightQualifier.matrixPacking != EmpUnspecified)
2851 {
2852 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2853 }
2854 if(rightQualifier.blockStorage != EbsUnspecified)
2855 {
2856 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2857 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002858
2859 return joinedQualifier;
2860}
2861
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002862
2863TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2864 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2865{
2866 TQualifier mergedQualifier = EvqSmoothIn;
2867
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002868 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002869 if(interpolationQualifier == EvqSmooth)
2870 mergedQualifier = EvqSmoothIn;
2871 else if(interpolationQualifier == EvqFlat)
2872 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002873 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002874 }
2875 else if(storageQualifier == EvqCentroidIn) {
2876 if(interpolationQualifier == EvqSmooth)
2877 mergedQualifier = EvqCentroidIn;
2878 else if(interpolationQualifier == EvqFlat)
2879 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002880 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002881 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002882 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002883 if(interpolationQualifier == EvqSmooth)
2884 mergedQualifier = EvqSmoothOut;
2885 else if(interpolationQualifier == EvqFlat)
2886 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002887 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002888 }
2889 else if(storageQualifier == EvqCentroidOut) {
2890 if(interpolationQualifier == EvqSmooth)
2891 mergedQualifier = EvqCentroidOut;
2892 else if(interpolationQualifier == EvqFlat)
2893 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002894 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002895 }
2896 else {
2897 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2898 recover();
2899
2900 mergedQualifier = storageQualifier;
2901 }
2902
2903 TPublicType type;
2904 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2905 return type;
2906}
2907
Alexis Hetuad6b8752015-06-09 16:15:30 -04002908TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2909{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002910 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002911 {
2912 recover();
2913 }
2914
2915 for(unsigned int i = 0; i < fieldList->size(); ++i)
2916 {
2917 //
2918 // Careful not to replace already known aspects of type, like array-ness
2919 //
2920 TType *type = (*fieldList)[i]->type();
2921 type->setBasicType(typeSpecifier.type);
2922 type->setNominalSize(typeSpecifier.primarySize);
2923 type->setSecondarySize(typeSpecifier.secondarySize);
2924 type->setPrecision(typeSpecifier.precision);
2925 type->setQualifier(typeSpecifier.qualifier);
2926 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2927
2928 // don't allow arrays of arrays
2929 if(type->isArray())
2930 {
2931 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2932 recover();
2933 }
2934 if(typeSpecifier.array)
2935 type->setArraySize(typeSpecifier.arraySize);
2936 if(typeSpecifier.userDef)
2937 {
2938 type->setStruct(typeSpecifier.userDef->getStruct());
2939 }
2940
2941 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2942 {
2943 recover();
2944 }
2945 }
2946
2947 return fieldList;
2948}
2949
2950TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2951 const TString *structName, TFieldList *fieldList)
2952{
2953 TStructure *structure = new TStructure(structName, fieldList);
2954 TType *structureType = new TType(structure);
2955
2956 // Store a bool in the struct if we're at global scope, to allow us to
2957 // skip the local struct scoping workaround in HLSL.
2958 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2959 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2960
2961 if(!structName->empty())
2962 {
2963 if(reservedErrorCheck(nameLine, *structName))
2964 {
2965 recover();
2966 }
2967 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2968 if(!symbolTable.declare(*userTypeDef))
2969 {
2970 error(nameLine, "redefinition", structName->c_str(), "struct");
2971 recover();
2972 }
2973 }
2974
2975 // ensure we do not specify any storage qualifiers on the struct members
2976 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2977 {
2978 const TField &field = *(*fieldList)[typeListIndex];
2979 const TQualifier qualifier = field.type()->getQualifier();
2980 switch(qualifier)
2981 {
2982 case EvqGlobal:
2983 case EvqTemporary:
2984 break;
2985 default:
2986 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2987 recover();
2988 break;
2989 }
2990 }
2991
2992 TPublicType publicType;
2993 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2994 publicType.userDef = structureType;
2995 exitStructDeclaration();
2996
2997 return publicType;
2998}
2999
Alexis Hetufe1269e2015-06-16 12:43:32 -04003000bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04003001{
Alexis Hetu0a655842015-06-22 16:52:11 -04003002 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003003
3004 // Embedded structure definitions are not supported per GLSL ES spec.
3005 // They aren't allowed in GLSL either, but we need to detect this here
3006 // so we don't rely on the GLSL compiler to catch it.
Alexis Hetu0a655842015-06-22 16:52:11 -04003007 if (mStructNestingLevel > 1) {
John Bauman66b8ab22014-05-06 15:57:45 -04003008 error(line, "", "Embedded struct definitions are not allowed");
3009 return true;
3010 }
3011
3012 return false;
3013}
3014
3015void TParseContext::exitStructDeclaration()
3016{
Alexis Hetu0a655842015-06-22 16:52:11 -04003017 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003018}
3019
Alexis Hetuad6b8752015-06-09 16:15:30 -04003020bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3021{
3022 static const int kWebGLMaxStructNesting = 4;
3023
3024 if(field.type()->getBasicType() != EbtStruct)
3025 {
3026 return false;
3027 }
3028
3029 // We're already inside a structure definition at this point, so add
3030 // one to the field's struct nesting.
3031 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3032 {
3033 std::stringstream reasonStream;
3034 reasonStream << "Reference of struct type "
3035 << field.type()->getStruct()->name().c_str()
3036 << " exceeds maximum allowed nesting level of "
3037 << kWebGLMaxStructNesting;
3038 std::string reason = reasonStream.str();
3039 error(line, reason.c_str(), field.name().c_str(), "");
3040 return true;
3041 }
3042
3043 return false;
3044}
3045
3046TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3047{
3048 if(child == nullptr)
3049 {
3050 return nullptr;
3051 }
3052
3053 switch(op)
3054 {
3055 case EOpLogicalNot:
3056 if(child->getBasicType() != EbtBool ||
3057 child->isMatrix() ||
3058 child->isArray() ||
3059 child->isVector())
3060 {
3061 return nullptr;
3062 }
3063 break;
3064 case EOpBitwiseNot:
3065 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3066 child->isMatrix() ||
3067 child->isArray())
3068 {
3069 return nullptr;
3070 }
3071 break;
3072 case EOpPostIncrement:
3073 case EOpPreIncrement:
3074 case EOpPostDecrement:
3075 case EOpPreDecrement:
3076 case EOpNegative:
3077 if(child->getBasicType() == EbtStruct ||
3078 child->getBasicType() == EbtBool ||
3079 child->isArray())
3080 {
3081 return nullptr;
3082 }
3083 // Operators for built-ins are already type checked against their prototype.
3084 default:
3085 break;
3086 }
3087
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -04003088 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Alexis Hetuad6b8752015-06-09 16:15:30 -04003089}
3090
3091TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3092{
3093 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3094 if(node == nullptr)
3095 {
3096 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3097 recover();
3098 return child;
3099 }
3100 return node;
3101}
3102
3103TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3104{
3105 if(lValueErrorCheck(loc, getOperatorString(op), child))
3106 recover();
3107 return addUnaryMath(op, child, loc);
3108}
3109
3110bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3111{
3112 if(left->isArray() || right->isArray())
3113 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003114 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003115 {
3116 error(loc, "Invalid operation for arrays", getOperatorString(op));
3117 return false;
3118 }
3119
3120 if(left->isArray() != right->isArray())
3121 {
3122 error(loc, "array / non-array mismatch", getOperatorString(op));
3123 return false;
3124 }
3125
3126 switch(op)
3127 {
3128 case EOpEqual:
3129 case EOpNotEqual:
3130 case EOpAssign:
3131 case EOpInitialize:
3132 break;
3133 default:
3134 error(loc, "Invalid operation for arrays", getOperatorString(op));
3135 return false;
3136 }
3137 // At this point, size of implicitly sized arrays should be resolved.
3138 if(left->getArraySize() != right->getArraySize())
3139 {
3140 error(loc, "array size mismatch", getOperatorString(op));
3141 return false;
3142 }
3143 }
3144
3145 // Check ops which require integer / ivec parameters
3146 bool isBitShift = false;
3147 switch(op)
3148 {
3149 case EOpBitShiftLeft:
3150 case EOpBitShiftRight:
3151 case EOpBitShiftLeftAssign:
3152 case EOpBitShiftRightAssign:
3153 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3154 // check that the basic type is an integer type.
3155 isBitShift = true;
3156 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3157 {
3158 return false;
3159 }
3160 break;
3161 case EOpBitwiseAnd:
3162 case EOpBitwiseXor:
3163 case EOpBitwiseOr:
3164 case EOpBitwiseAndAssign:
3165 case EOpBitwiseXorAssign:
3166 case EOpBitwiseOrAssign:
3167 // It is enough to check the type of only one operand, since later it
3168 // is checked that the operand types match.
3169 if(!IsInteger(left->getBasicType()))
3170 {
3171 return false;
3172 }
3173 break;
3174 default:
3175 break;
3176 }
3177
3178 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3179 // So the basic type should usually match.
3180 if(!isBitShift && left->getBasicType() != right->getBasicType())
3181 {
3182 return false;
3183 }
3184
3185 // Check that type sizes match exactly on ops that require that.
3186 // Also check restrictions for structs that contain arrays or samplers.
3187 switch(op)
3188 {
3189 case EOpAssign:
3190 case EOpInitialize:
3191 case EOpEqual:
3192 case EOpNotEqual:
3193 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003194 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003195 {
3196 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3197 return false;
3198 }
3199 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3200 // we interpret the spec so that this extends to structs containing samplers,
3201 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003202 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003203 left->getType().isStructureContainingSamplers())
3204 {
3205 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3206 return false;
3207 }
3208 case EOpLessThan:
3209 case EOpGreaterThan:
3210 case EOpLessThanEqual:
3211 case EOpGreaterThanEqual:
3212 if((left->getNominalSize() != right->getNominalSize()) ||
3213 (left->getSecondarySize() != right->getSecondarySize()))
3214 {
3215 return false;
3216 }
3217 default:
3218 break;
3219 }
3220
3221 return true;
3222}
3223
Alexis Hetu76a343a2015-06-04 17:21:22 -04003224TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3225{
3226 TBasicType switchType = init->getBasicType();
3227 if((switchType != EbtInt && switchType != EbtUInt) ||
3228 init->isMatrix() ||
3229 init->isArray() ||
3230 init->isVector())
3231 {
3232 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3233 recover();
3234 return nullptr;
3235 }
3236
3237 if(statementList)
3238 {
3239 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3240 {
3241 recover();
3242 return nullptr;
3243 }
3244 }
3245
3246 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3247 if(node == nullptr)
3248 {
3249 error(loc, "erroneous switch statement", "switch");
3250 recover();
3251 return nullptr;
3252 }
3253 return node;
3254}
3255
3256TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3257{
Alexis Hetu0a655842015-06-22 16:52:11 -04003258 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003259 {
3260 error(loc, "case labels need to be inside switch statements", "case");
3261 recover();
3262 return nullptr;
3263 }
3264 if(condition == nullptr)
3265 {
3266 error(loc, "case label must have a condition", "case");
3267 recover();
3268 return nullptr;
3269 }
3270 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3271 condition->isMatrix() ||
3272 condition->isArray() ||
3273 condition->isVector())
3274 {
3275 error(condition->getLine(), "case label must be a scalar integer", "case");
3276 recover();
3277 }
3278 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3279 if(conditionConst == nullptr)
3280 {
3281 error(condition->getLine(), "case label must be constant", "case");
3282 recover();
3283 }
3284 TIntermCase *node = intermediate.addCase(condition, loc);
3285 if(node == nullptr)
3286 {
3287 error(loc, "erroneous case statement", "case");
3288 recover();
3289 return nullptr;
3290 }
3291 return node;
3292}
3293
3294TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3295{
Alexis Hetu0a655842015-06-22 16:52:11 -04003296 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003297 {
3298 error(loc, "default labels need to be inside switch statements", "default");
3299 recover();
3300 return nullptr;
3301 }
3302 TIntermCase *node = intermediate.addCase(nullptr, loc);
3303 if(node == nullptr)
3304 {
3305 error(loc, "erroneous default statement", "default");
3306 recover();
3307 return nullptr;
3308 }
3309 return node;
3310}
Alexis Hetue5246692015-06-18 12:34:52 -04003311TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3312{
3313 if(binaryOpCommonCheck(op, left, right, loc))
3314 {
3315 return intermediate.addAssign(op, left, right, loc);
3316 }
3317 return nullptr;
3318}
3319
3320TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3321{
3322 TIntermTyped *node = createAssign(op, left, right, loc);
3323 if(node == nullptr)
3324 {
3325 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3326 recover();
3327 return left;
3328 }
3329 return node;
3330}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003331
Alexis Hetub4769582015-06-16 12:19:50 -04003332TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3333 const TSourceLoc &loc)
3334{
3335 if(!binaryOpCommonCheck(op, left, right, loc))
3336 return nullptr;
3337
3338 switch(op)
3339 {
3340 case EOpEqual:
3341 case EOpNotEqual:
3342 break;
3343 case EOpLessThan:
3344 case EOpGreaterThan:
3345 case EOpLessThanEqual:
3346 case EOpGreaterThanEqual:
3347 ASSERT(!left->isArray() && !right->isArray());
3348 if(left->isMatrix() || left->isVector() ||
3349 left->getBasicType() == EbtStruct)
3350 {
3351 return nullptr;
3352 }
3353 break;
3354 case EOpLogicalOr:
3355 case EOpLogicalXor:
3356 case EOpLogicalAnd:
3357 ASSERT(!left->isArray() && !right->isArray());
3358 if(left->getBasicType() != EbtBool ||
3359 left->isMatrix() || left->isVector())
3360 {
3361 return nullptr;
3362 }
3363 break;
3364 case EOpAdd:
3365 case EOpSub:
3366 case EOpDiv:
3367 case EOpMul:
3368 ASSERT(!left->isArray() && !right->isArray());
3369 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3370 {
3371 return nullptr;
3372 }
3373 break;
3374 case EOpIMod:
3375 ASSERT(!left->isArray() && !right->isArray());
3376 // Note that this is only for the % operator, not for mod()
3377 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3378 {
3379 return nullptr;
3380 }
3381 break;
3382 // Note that for bitwise ops, type checking is done in promote() to
3383 // share code between ops and compound assignment
3384 default:
3385 break;
3386 }
3387
3388 return intermediate.addBinaryMath(op, left, right, loc);
3389}
3390
3391TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3392{
3393 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3394 if(node == 0)
3395 {
3396 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3397 recover();
3398 return left;
3399 }
3400 return node;
3401}
3402
3403TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3404{
3405 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3406 if(node == 0)
3407 {
3408 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3409 recover();
3410 ConstantUnion *unionArray = new ConstantUnion[1];
3411 unionArray->setBConst(false);
3412 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3413 }
3414 return node;
3415}
3416
Alexis Hetu76a343a2015-06-04 17:21:22 -04003417TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3418{
3419 switch(op)
3420 {
3421 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003422 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003423 {
3424 error(loc, "continue statement only allowed in loops", "");
3425 recover();
3426 }
3427 break;
3428 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003429 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003430 {
3431 error(loc, "break statement only allowed in loops and switch statements", "");
3432 recover();
3433 }
3434 break;
3435 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003436 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003437 {
3438 error(loc, "non-void function must return a value", "return");
3439 recover();
3440 }
3441 break;
3442 default:
3443 // No checks for discard
3444 break;
3445 }
3446 return intermediate.addBranch(op, loc);
3447}
3448
3449TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3450{
3451 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003452 mFunctionReturnsValue = true;
3453 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003454 {
3455 error(loc, "void function cannot return a value", "return");
3456 recover();
3457 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003458 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003459 {
3460 error(loc, "function return is not matching type:", "return");
3461 recover();
3462 }
3463 return intermediate.addBranch(op, returnValue, loc);
3464}
3465
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003466TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3467{
3468 *fatalError = false;
3469 TOperator op = fnCall->getBuiltInOp();
3470 TIntermTyped *callNode = nullptr;
3471
3472 if(thisNode != nullptr)
3473 {
3474 ConstantUnion *unionArray = new ConstantUnion[1];
3475 int arraySize = 0;
3476 TIntermTyped *typedThis = thisNode->getAsTyped();
3477 if(fnCall->getName() != "length")
3478 {
3479 error(loc, "invalid method", fnCall->getName().c_str());
3480 recover();
3481 }
3482 else if(paramNode != nullptr)
3483 {
3484 error(loc, "method takes no parameters", "length");
3485 recover();
3486 }
3487 else if(typedThis == nullptr || !typedThis->isArray())
3488 {
3489 error(loc, "length can only be called on arrays", "length");
3490 recover();
3491 }
3492 else
3493 {
3494 arraySize = typedThis->getArraySize();
3495 if(typedThis->getAsSymbolNode() == nullptr)
3496 {
3497 // This code path can be hit with expressions like these:
3498 // (a = b).length()
3499 // (func()).length()
3500 // (int[3](0, 1, 2)).length()
3501 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3502 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3503 // which allows "An array, vector or matrix expression with the length method applied".
3504 error(loc, "length can only be called on array names, not on array expressions", "length");
3505 recover();
3506 }
3507 }
3508 unionArray->setIConst(arraySize);
3509 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3510 }
3511 else if(op != EOpNull)
3512 {
3513 //
3514 // Then this should be a constructor.
3515 // Don't go through the symbol table for constructors.
3516 // Their parameters will be verified algorithmically.
3517 //
3518 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3519 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3520 {
3521 //
3522 // It's a constructor, of type 'type'.
3523 //
3524 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3525 }
3526
3527 if(callNode == nullptr)
3528 {
3529 recover();
3530 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3531 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003532 }
3533 else
3534 {
3535 //
3536 // Not a constructor. Find it in the symbol table.
3537 //
3538 const TFunction *fnCandidate;
3539 bool builtIn;
3540 fnCandidate = findFunction(loc, fnCall, &builtIn);
3541 if(fnCandidate)
3542 {
3543 //
3544 // A declared function.
3545 //
3546 if(builtIn && !fnCandidate->getExtension().empty() &&
3547 extensionErrorCheck(loc, fnCandidate->getExtension()))
3548 {
3549 recover();
3550 }
3551 op = fnCandidate->getBuiltInOp();
3552 if(builtIn && op != EOpNull)
3553 {
3554 //
3555 // A function call mapped to a built-in operation.
3556 //
3557 if(fnCandidate->getParamCount() == 1)
3558 {
3559 //
3560 // Treat it like a built-in unary operator.
3561 //
3562 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3563 if(callNode == nullptr)
3564 {
3565 std::stringstream extraInfoStream;
3566 extraInfoStream << "built in unary operator function. Type: "
3567 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3568 std::string extraInfo = extraInfoStream.str();
3569 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3570 *fatalError = true;
3571 return nullptr;
3572 }
3573 }
3574 else
3575 {
3576 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3577 aggregate->setType(fnCandidate->getReturnType());
3578
3579 // Some built-in functions have out parameters too.
3580 functionCallLValueErrorCheck(fnCandidate, aggregate);
3581
3582 callNode = aggregate;
Nicolas Capens91dfb972016-04-09 23:45:12 -04003583
3584 if(fnCandidate->getParamCount() == 2)
3585 {
3586 TIntermSequence &parameters = paramNode->getAsAggregate()->getSequence();
3587 TIntermTyped *left = parameters[0]->getAsTyped();
3588 TIntermTyped *right = parameters[1]->getAsTyped();
3589
3590 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
3591 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
3592 if (leftTempConstant && rightTempConstant)
3593 {
3594 TIntermTyped *typedReturnNode = leftTempConstant->fold(op, rightTempConstant, infoSink());
3595
3596 if(typedReturnNode)
3597 {
3598 callNode = typedReturnNode;
3599 }
3600 }
3601 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003602 }
3603 }
3604 else
3605 {
3606 // This is a real function call
3607
3608 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3609 aggregate->setType(fnCandidate->getReturnType());
3610
3611 // this is how we know whether the given function is a builtIn function or a user defined function
3612 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3613 // if builtIn == true, it's definitely a builtIn function with EOpNull
3614 if(!builtIn)
3615 aggregate->setUserDefined();
3616 aggregate->setName(fnCandidate->getMangledName());
3617
3618 callNode = aggregate;
3619
3620 functionCallLValueErrorCheck(fnCandidate, aggregate);
3621 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003622 }
3623 else
3624 {
3625 // error message was put out by findFunction()
3626 // Put on a dummy node for error recovery
3627 ConstantUnion *unionArray = new ConstantUnion[1];
3628 unionArray->setFConst(0.0f);
3629 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3630 recover();
3631 }
3632 }
3633 delete fnCall;
3634 return callNode;
3635}
3636
Alexis Hetueee212e2015-07-07 17:13:30 -04003637TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3638{
3639 if(boolErrorCheck(loc, cond))
3640 recover();
3641
3642 if(trueBlock->getType() != falseBlock->getType())
3643 {
3644 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3645 recover();
3646 return falseBlock;
3647 }
3648 // ESSL1 sections 5.2 and 5.7:
3649 // ESSL3 section 5.7:
3650 // Ternary operator is not among the operators allowed for structures/arrays.
3651 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3652 {
3653 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3654 recover();
3655 return falseBlock;
3656 }
3657 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3658}
3659
John Bauman66b8ab22014-05-06 15:57:45 -04003660//
3661// Parse an array of strings using yyparse.
3662//
3663// Returns 0 for success.
3664//
3665int PaParseStrings(int count, const char* const string[], const int length[],
3666 TParseContext* context) {
3667 if ((count == 0) || (string == NULL))
3668 return 1;
3669
3670 if (glslang_initialize(context))
3671 return 1;
3672
3673 int error = glslang_scan(count, string, length, context);
3674 if (!error)
3675 error = glslang_parse(context);
3676
3677 glslang_finalize(context);
3678
3679 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
3680}
3681
3682
3683