blob: f27486ea77f88826940aa3953dc588e5290bc946 [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;
231 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
232 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
233 std::string extraInfo = extraInfoStream.str();
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
484 int size = 0;
485 bool constType = true;
486 bool full = false;
487 bool overFull = false;
488 bool matrixInMatrix = false;
489 bool arrayArg = false;
Alexis Hetua818c452015-06-11 13:06:58 -0400490 for (size_t i = 0; i < function.getParamCount(); ++i) {
John Bauman66b8ab22014-05-06 15:57:45 -0400491 const TParameter& param = function.getParam(i);
492 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400493
John Bauman66b8ab22014-05-06 15:57:45 -0400494 if (constructingMatrix && param.type->isMatrix())
495 matrixInMatrix = true;
496 if (full)
497 overFull = true;
498 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
499 full = true;
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500500 if (param.type->getQualifier() != EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400501 constType = false;
502 if (param.type->isArray())
503 arrayArg = true;
504 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400505
John Bauman66b8ab22014-05-06 15:57:45 -0400506 if (constType)
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500507 type->setQualifier(EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -0400508
Alexis Hetue5246692015-06-18 12:34:52 -0400509 if(type->isArray()) {
510 if(type->getArraySize() == 0) {
511 type->setArraySize(function.getParamCount());
512 } else if(type->getArraySize() != function.getParamCount()) {
513 error(line, "array constructor needs one argument per array element", "constructor");
514 return true;
515 }
John Bauman66b8ab22014-05-06 15:57:45 -0400516 }
517
518 if (arrayArg && op != EOpConstructStruct) {
519 error(line, "constructing from a non-dereferenced array", "constructor");
520 return true;
521 }
522
523 if (matrixInMatrix && !type->isArray()) {
524 if (function.getParamCount() != 1) {
525 error(line, "constructing matrix from matrix can only take one argument", "constructor");
526 return true;
527 }
528 }
529
530 if (overFull) {
531 error(line, "too many arguments", "constructor");
532 return true;
533 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400534
Alexis Hetua8b364b2015-06-10 11:48:40 -0400535 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->fields().size()) != function.getParamCount()) {
John Bauman66b8ab22014-05-06 15:57:45 -0400536 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
537 return true;
538 }
539
540 if (!type->isMatrix() || !matrixInMatrix) {
541 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
542 (op == EOpConstructStruct && size < type->getObjectSize())) {
543 error(line, "not enough data provided for construction", "constructor");
544 return true;
545 }
546 }
547
548 TIntermTyped *typed = node ? node->getAsTyped() : 0;
549 if (typed == 0) {
550 error(line, "constructor argument does not have a type", "constructor");
551 return true;
552 }
553 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
554 error(line, "cannot convert a sampler", "constructor");
555 return true;
556 }
557 if (typed->getBasicType() == EbtVoid) {
558 error(line, "cannot convert a void", "constructor");
559 return true;
560 }
561
562 return false;
563}
564
565// This function checks to see if a void variable has been declared and raise an error message for such a case
566//
567// returns true in case of an error
568//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400569bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString& identifier, const TBasicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400570{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400571 if(type == EbtVoid) {
John Bauman66b8ab22014-05-06 15:57:45 -0400572 error(line, "illegal use of type 'void'", identifier.c_str());
573 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400574 }
John Bauman66b8ab22014-05-06 15:57:45 -0400575
576 return false;
577}
578
579// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
580//
581// returns true in case of an error
582//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400583bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400584{
585 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
586 error(line, "boolean expression expected", "");
587 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400588 }
John Bauman66b8ab22014-05-06 15:57:45 -0400589
590 return false;
591}
592
593// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
594//
595// returns true in case of an error
596//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400597bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400598{
Alexis Hetub14178b2015-04-13 13:23:20 -0400599 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400600 error(line, "boolean expression expected", "");
601 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400602 }
John Bauman66b8ab22014-05-06 15:57:45 -0400603
604 return false;
605}
606
Alexis Hetufe1269e2015-06-16 12:43:32 -0400607bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType& pType, const char* reason)
John Bauman66b8ab22014-05-06 15:57:45 -0400608{
609 if (pType.type == EbtStruct) {
610 if (containsSampler(*pType.userDef)) {
611 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400612
John Bauman66b8ab22014-05-06 15:57:45 -0400613 return true;
614 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400615
John Bauman66b8ab22014-05-06 15:57:45 -0400616 return false;
617 } else if (IsSampler(pType.type)) {
618 error(line, reason, getBasicString(pType.type));
619
620 return true;
621 }
622
623 return false;
624}
625
Alexis Hetufe1269e2015-06-16 12:43:32 -0400626bool TParseContext::structQualifierErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400627{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400628 switch(pType.qualifier)
629 {
630 case EvqVaryingOut:
631 case EvqSmooth:
632 case EvqFlat:
633 case EvqCentroidOut:
634 case EvqVaryingIn:
635 case EvqSmoothIn:
636 case EvqFlatIn:
637 case EvqCentroidIn:
638 case EvqAttribute:
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400639 case EvqVertexIn:
640 case EvqFragmentOut:
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400641 if(pType.type == EbtStruct)
642 {
643 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400644
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400645 return true;
646 }
647 break;
648 default:
649 break;
650 }
John Bauman66b8ab22014-05-06 15:57:45 -0400651
652 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
653 return true;
654
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400655 // check for layout qualifier issues
656 const TLayoutQualifier layoutQualifier = pType.layoutQualifier;
657
658 if (pType.qualifier != EvqVertexIn && pType.qualifier != EvqFragmentOut &&
659 layoutLocationErrorCheck(line, pType.layoutQualifier))
660 {
661 return true;
662 }
663
John Bauman66b8ab22014-05-06 15:57:45 -0400664 return false;
665}
666
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400667// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
668// declaration.
669//
670bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
671{
672 switch(publicType.qualifier)
673 {
674 case EvqVaryingIn:
675 case EvqVaryingOut:
676 case EvqAttribute:
677 case EvqVertexIn:
678 case EvqFragmentOut:
679 if(publicType.type == EbtStruct)
680 {
681 error(identifierLocation, "cannot be used with a structure",
682 getQualifierString(publicType.qualifier));
683 return true;
684 }
685
686 default: break;
687 }
688
689 if(publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
690 "samplers must be uniform"))
691 {
692 return true;
693 }
694
695 // check for layout qualifier issues
696 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
697
698 if(layoutQualifier.matrixPacking != EmpUnspecified)
699 {
700 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
701 "only valid for interface blocks");
702 return true;
703 }
704
705 if(layoutQualifier.blockStorage != EbsUnspecified)
706 {
707 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
708 "only valid for interface blocks");
709 return true;
710 }
711
712 if(publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
713 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
714 {
715 return true;
716 }
717
718 return false;
719}
720
Nicolas Capens3713cd42015-06-22 10:41:54 -0400721bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
722{
723 if(layoutQualifier.location != -1)
724 {
725 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
726 return true;
727 }
728
729 return false;
730}
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400731
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400732bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
733{
734 if(pType.layoutQualifier.location != -1)
735 {
736 error(line, "location must only be specified for a single input or output variable", "location");
737 return true;
738 }
739
740 return false;
741}
742
Alexis Hetufe1269e2015-06-16 12:43:32 -0400743bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400744{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400745 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
John Bauman66b8ab22014-05-06 15:57:45 -0400746 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
747 error(line, "samplers cannot be output parameters", type.getBasicString());
748 return true;
749 }
750
751 return false;
752}
753
754bool TParseContext::containsSampler(TType& type)
755{
756 if (IsSampler(type.getBasicType()))
757 return true;
758
759 if (type.getBasicType() == EbtStruct) {
Alexis Hetua8b364b2015-06-10 11:48:40 -0400760 const TFieldList& fields = type.getStruct()->fields();
761 for(unsigned int i = 0; i < fields.size(); ++i) {
762 if (containsSampler(*fields[i]->type()))
John Bauman66b8ab22014-05-06 15:57:45 -0400763 return true;
764 }
765 }
766
767 return false;
768}
769
770//
771// Do size checking for an array type's size.
772//
773// Returns true if there was an error.
774//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400775bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped* expr, int& size)
John Bauman66b8ab22014-05-06 15:57:45 -0400776{
777 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500778
779 if (constant == 0 || !constant->isScalarInt())
780 {
John Bauman66b8ab22014-05-06 15:57:45 -0400781 error(line, "array size must be a constant integer expression", "");
782 return true;
783 }
784
Nicolas Capens3c20f802015-02-17 17:17:20 -0500785 if (constant->getBasicType() == EbtUInt)
786 {
787 unsigned int uintSize = constant->getUConst(0);
788 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
789 {
790 error(line, "array size too large", "");
791 size = 1;
792 return true;
793 }
John Bauman66b8ab22014-05-06 15:57:45 -0400794
Nicolas Capens3c20f802015-02-17 17:17:20 -0500795 size = static_cast<int>(uintSize);
796 }
797 else
798 {
799 size = constant->getIConst(0);
800
801 if (size <= 0)
802 {
803 error(line, "array size must be a positive integer", "");
804 size = 1;
805 return true;
806 }
John Bauman66b8ab22014-05-06 15:57:45 -0400807 }
808
809 return false;
810}
811
812//
813// See if this qualifier can be an array.
814//
815// Returns true if there is an error.
816//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400817bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400818{
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400819 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400820 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
821 return true;
822 }
823
824 return false;
825}
826
827//
828// See if this type can be an array.
829//
830// Returns true if there is an error.
831//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400832bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400833{
834 //
835 // Can the type be an array?
836 //
837 if (type.array) {
838 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
839 return true;
840 }
841
842 return false;
843}
844
Alexis Hetufe1269e2015-06-16 12:43:32 -0400845bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400846{
847 bool builtIn = false;
Alexis Hetu0a655842015-06-22 16:52:11 -0400848 TSymbol* symbol = symbolTable.find(node->getSymbol(), mShaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400849 if (symbol == 0) {
850 error(line, " undeclared identifier", node->getSymbol().c_str());
851 return true;
852 }
853 TVariable* variable = static_cast<TVariable*>(symbol);
854
855 type->setArrayInformationType(variable->getArrayInformationType());
856 variable->updateArrayInformationType(type);
857
858 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
859 // its an error
860 if (node->getSymbol() == "gl_FragData") {
Alexis Hetu0a655842015-06-22 16:52:11 -0400861 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", mShaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400862 ASSERT(fragData);
863
864 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
865 if (fragDataValue <= size) {
866 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
867 return true;
868 }
869 }
870
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400871 // 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 -0400872 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
873 if (!updateFlag)
874 return false;
875
876 size++;
877 variable->getType().setMaxArraySize(size);
878 type->setMaxArraySize(size);
879 TType* tt = type;
880
881 while(tt->getArrayInformationType() != 0) {
882 tt = tt->getArrayInformationType();
883 tt->setMaxArraySize(size);
884 }
885
886 return false;
887}
888
889//
890// Enforce non-initializer type/qualifier rules.
891//
892// Returns true if there was an error.
893//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400894bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, TString& identifier, TPublicType& type, bool array)
John Bauman66b8ab22014-05-06 15:57:45 -0400895{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500896 if (type.qualifier == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400897 {
898 // Make the qualifier make sense.
899 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400900
John Bauman66b8ab22014-05-06 15:57:45 -0400901 if (array)
902 {
903 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
904 }
905 else if (type.isStructureContainingArrays())
906 {
907 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
908 }
909 else
910 {
911 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
912 }
913
914 return true;
915 }
916
917 return false;
918}
919
920//
921// Do semantic checking for a variable declaration that has no initializer,
922// and update the symbol table.
923//
924// Returns true if there was an error.
925//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400926bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString& identifier, TPublicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400927{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400928 if(type.qualifier == EvqConstExpr)
929 {
930 // Make the qualifier make sense.
931 type.qualifier = EvqTemporary;
John Bauman66b8ab22014-05-06 15:57:45 -0400932
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400933 // Generate informative error messages for ESSL1.
934 // In ESSL3 arrays and structures containing arrays can be constant.
Alexis Hetu0a655842015-06-22 16:52:11 -0400935 if(mShaderVersion < 300 && type.isStructureContainingArrays())
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400936 {
937 error(line,
938 "structures containing arrays may not be declared constant since they cannot be initialized",
939 identifier.c_str());
940 }
941 else
942 {
943 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
944 }
John Bauman66b8ab22014-05-06 15:57:45 -0400945
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400946 return true;
947 }
948 if(type.isUnsizedArray())
949 {
950 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
951 return true;
952 }
953 return false;
954}
John Bauman66b8ab22014-05-06 15:57:45 -0400955
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400956// Do some simple checks that are shared between all variable declarations,
957// and update the symbol table.
958//
959// Returns true if declaring the variable succeeded.
960//
961bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
962 TVariable **variable)
963{
964 ASSERT((*variable) == nullptr);
John Bauman66b8ab22014-05-06 15:57:45 -0400965
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400966 // gl_LastFragData may be redeclared with a new precision qualifier
967 if(type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
968 {
969 const TVariable *maxDrawBuffers =
Alexis Hetu0a655842015-06-22 16:52:11 -0400970 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400971 if(type.getArraySize() != maxDrawBuffers->getConstPointer()->getIConst())
972 {
973 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
974 return false;
975 }
976 }
977
978 if(reservedErrorCheck(line, identifier))
979 return false;
980
981 (*variable) = new TVariable(&identifier, type);
982 if(!symbolTable.declare(**variable))
983 {
984 error(line, "redefinition", identifier.c_str());
985 delete (*variable);
986 (*variable) = nullptr;
987 return false;
988 }
989
990 if(voidErrorCheck(line, identifier, type.getBasicType()))
991 return false;
992
993 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400994}
995
Alexis Hetufe1269e2015-06-16 12:43:32 -0400996bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400997{
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500998 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
John Bauman66b8ab22014-05-06 15:57:45 -0400999 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
1000 return true;
1001 }
Nicolas Capensb1e911a2015-02-26 13:16:00 -05001002 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
John Bauman66b8ab22014-05-06 15:57:45 -04001003 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
1004 return true;
1005 }
1006
Nicolas Capensb1e911a2015-02-26 13:16:00 -05001007 if (qualifier == EvqConstReadOnly)
John Bauman66b8ab22014-05-06 15:57:45 -04001008 type->setQualifier(EvqConstReadOnly);
1009 else
1010 type->setQualifier(paramQualifier);
1011
1012 return false;
1013}
1014
Alexis Hetufe1269e2015-06-16 12:43:32 -04001015bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString& extension)
John Bauman66b8ab22014-05-06 15:57:45 -04001016{
1017 const TExtensionBehavior& extBehavior = extensionBehavior();
1018 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
1019 if (iter == extBehavior.end()) {
1020 error(line, "extension", extension.c_str(), "is not supported");
1021 return true;
1022 }
1023 // In GLSL ES, an extension's default behavior is "disable".
1024 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
1025 error(line, "extension", extension.c_str(), "is disabled");
1026 return true;
1027 }
1028 if (iter->second == EBhWarn) {
1029 warning(line, "extension", extension.c_str(), "is being used");
1030 return false;
1031 }
1032
1033 return false;
1034}
1035
Alexis Hetuad6b8752015-06-09 16:15:30 -04001036bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1037{
1038 for(size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1039 {
1040 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1041 if(qual == EvqOut || qual == EvqInOut)
1042 {
1043 TIntermTyped *node = (aggregate->getSequence())[i]->getAsTyped();
1044 if(lValueErrorCheck(node->getLine(), "assign", node))
1045 {
1046 error(node->getLine(),
1047 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1048 recover();
1049 return true;
1050 }
1051 }
1052 }
1053 return false;
1054}
1055
John Bauman66b8ab22014-05-06 15:57:45 -04001056bool TParseContext::supportsExtension(const char* extension)
1057{
1058 const TExtensionBehavior& extbehavior = extensionBehavior();
1059 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1060 return (iter != extbehavior.end());
1061}
1062
Alexis Hetufe1269e2015-06-16 12:43:32 -04001063void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001064{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001065 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001066 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001067}
1068
Alexis Hetufe1269e2015-06-16 12:43:32 -04001069void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value)
John Bauman66b8ab22014-05-06 15:57:45 -04001070{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001071 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001072 mDirectiveHandler.handlePragma(loc, name, value);
John Bauman66b8ab22014-05-06 15:57:45 -04001073}
1074
1075/////////////////////////////////////////////////////////////////////////////////
1076//
1077// Non-Errors.
1078//
1079/////////////////////////////////////////////////////////////////////////////////
1080
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001081const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1082 const TString *name,
1083 const TSymbol *symbol)
1084{
1085 const TVariable *variable = NULL;
1086
1087 if(!symbol)
1088 {
1089 error(location, "undeclared identifier", name->c_str());
1090 recover();
1091 }
1092 else if(!symbol->isVariable())
1093 {
1094 error(location, "variable expected", name->c_str());
1095 recover();
1096 }
1097 else
1098 {
1099 variable = static_cast<const TVariable*>(symbol);
1100
Alexis Hetu0a655842015-06-22 16:52:11 -04001101 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001102 {
1103 recover();
1104 }
1105
1106 // Reject shaders using both gl_FragData and gl_FragColor
1107 TQualifier qualifier = variable->getType().getQualifier();
1108 if(qualifier == EvqFragData)
1109 {
1110 mUsesFragData = true;
1111 }
1112 else if(qualifier == EvqFragColor)
1113 {
1114 mUsesFragColor = true;
1115 }
1116
1117 // This validation is not quite correct - it's only an error to write to
1118 // both FragData and FragColor. For simplicity, and because users shouldn't
1119 // be rewarded for reading from undefined varaibles, return an error
1120 // if they are both referenced, rather than assigned.
1121 if(mUsesFragData && mUsesFragColor)
1122 {
1123 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1124 recover();
1125 }
1126 }
1127
1128 if(!variable)
1129 {
1130 TType type(EbtFloat, EbpUndefined);
1131 TVariable *fakeVariable = new TVariable(name, type);
1132 symbolTable.declare(*fakeVariable);
1133 variable = fakeVariable;
1134 }
1135
1136 return variable;
1137}
1138
John Bauman66b8ab22014-05-06 15:57:45 -04001139//
1140// Look up a function name in the symbol table, and make sure it is a function.
1141//
1142// Return the function symbol if found, otherwise 0.
1143//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001144const TFunction* TParseContext::findFunction(const TSourceLoc &line, TFunction* call, bool *builtIn)
John Bauman66b8ab22014-05-06 15:57:45 -04001145{
1146 // First find by unmangled name to check whether the function name has been
1147 // hidden by a variable name or struct typename.
Alexis Hetu0a655842015-06-22 16:52:11 -04001148 const TSymbol* symbol = symbolTable.find(call->getName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001149 if (symbol == 0) {
Alexis Hetu0a655842015-06-22 16:52:11 -04001150 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001151 }
1152
1153 if (symbol == 0) {
1154 error(line, "no matching overloaded function found", call->getName().c_str());
1155 return 0;
1156 }
1157
1158 if (!symbol->isFunction()) {
1159 error(line, "function name expected", call->getName().c_str());
1160 return 0;
1161 }
1162
1163 return static_cast<const TFunction*>(symbol);
1164}
1165
1166//
1167// Initializers show up in several places in the grammar. Have one set of
1168// code to handle them here.
1169//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001170bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, const TPublicType& pType,
Alexis Hetue5246692015-06-18 12:34:52 -04001171 TIntermTyped *initializer, TIntermNode **intermNode)
John Bauman66b8ab22014-05-06 15:57:45 -04001172{
Alexis Hetue5246692015-06-18 12:34:52 -04001173 ASSERT(intermNode != nullptr);
1174 TType type = TType(pType);
John Bauman66b8ab22014-05-06 15:57:45 -04001175
Alexis Hetue5246692015-06-18 12:34:52 -04001176 TVariable *variable = nullptr;
1177 if(type.isArray() && (type.getArraySize() == 0))
1178 {
1179 type.setArraySize(initializer->getArraySize());
1180 }
1181 if(!declareVariable(line, identifier, type, &variable))
1182 {
1183 return true;
1184 }
John Bauman66b8ab22014-05-06 15:57:45 -04001185
Alexis Hetue5246692015-06-18 12:34:52 -04001186 bool globalInitWarning = false;
1187 if(symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1188 {
1189 // Error message does not completely match behavior with ESSL 1.00, but
1190 // we want to steer developers towards only using constant expressions.
1191 error(line, "global variable initializers must be constant expressions", "=");
1192 return true;
1193 }
1194 if(globalInitWarning)
1195 {
1196 warning(line, "global variable initializers should be constant expressions "
1197 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
John Bauman66b8ab22014-05-06 15:57:45 -04001198 }
1199
1200 //
1201 // identifier must be of type constant, a global, or a temporary
1202 //
1203 TQualifier qualifier = variable->getType().getQualifier();
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001204 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001205 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1206 return true;
1207 }
1208 //
1209 // test for and propagate constant
1210 //
1211
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001212 if (qualifier == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001213 if (qualifier != initializer->getType().getQualifier()) {
1214 std::stringstream extraInfoStream;
1215 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1216 std::string extraInfo = extraInfoStream.str();
1217 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1218 variable->getType().setQualifier(EvqTemporary);
1219 return true;
1220 }
1221 if (type != initializer->getType()) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001222 error(line, " non-matching types for const initializer ",
John Bauman66b8ab22014-05-06 15:57:45 -04001223 variable->getType().getQualifierString());
1224 variable->getType().setQualifier(EvqTemporary);
1225 return true;
1226 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001227 if (initializer->getAsConstantUnion()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001228 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
John Bauman66b8ab22014-05-06 15:57:45 -04001229 } else if (initializer->getAsSymbolNode()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001230 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001231 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1232
1233 ConstantUnion* constArray = tVar->getConstPointer();
1234 variable->shareConstPointer(constArray);
1235 } else {
1236 std::stringstream extraInfoStream;
1237 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1238 std::string extraInfo = extraInfoStream.str();
1239 error(line, " cannot assign to", "=", extraInfo.c_str());
1240 variable->getType().setQualifier(EvqTemporary);
1241 return true;
1242 }
1243 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001244
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001245 if (qualifier != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001246 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
Alexis Hetue5246692015-06-18 12:34:52 -04001247 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1248 if(*intermNode == nullptr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001249 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1250 return true;
1251 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001252 } else
Alexis Hetue5246692015-06-18 12:34:52 -04001253 *intermNode = nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04001254
1255 return false;
1256}
1257
1258bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1259{
1260 ASSERT(aggrNode != NULL);
1261 if (!aggrNode->isConstructor())
1262 return false;
1263
1264 bool allConstant = true;
1265
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001266 // check if all the child nodes are constants so that they can be inserted into
John Bauman66b8ab22014-05-06 15:57:45 -04001267 // the parent node
1268 TIntermSequence &sequence = aggrNode->getSequence() ;
1269 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1270 if (!(*p)->getAsTyped()->getAsConstantUnion())
1271 return false;
1272 }
1273
1274 return allConstant;
1275}
1276
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001277TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1278{
1279 TPublicType returnType = typeSpecifier;
1280 returnType.qualifier = qualifier;
1281 returnType.invariant = invariant;
1282 returnType.layoutQualifier = layoutQualifier;
1283
1284 if(typeSpecifier.array)
1285 {
1286 error(typeSpecifier.line, "not supported", "first-class array");
1287 recover();
1288 returnType.clearArrayness();
1289 }
1290
Alexis Hetu0a655842015-06-22 16:52:11 -04001291 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001292 {
1293 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1294 {
1295 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1296 recover();
1297 }
1298
1299 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1300 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1301 {
1302 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1303 recover();
1304 }
1305 }
1306 else
1307 {
1308 switch(qualifier)
1309 {
1310 case EvqSmoothIn:
1311 case EvqSmoothOut:
1312 case EvqVertexOut:
1313 case EvqFragmentIn:
1314 case EvqCentroidOut:
1315 case EvqCentroidIn:
1316 if(typeSpecifier.type == EbtBool)
1317 {
1318 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1319 recover();
1320 }
1321 if(typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1322 {
1323 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1324 recover();
1325 }
1326 break;
1327
1328 case EvqVertexIn:
1329 case EvqFragmentOut:
1330 case EvqFlatIn:
1331 case EvqFlatOut:
1332 if(typeSpecifier.type == EbtBool)
1333 {
1334 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1335 recover();
1336 }
1337 break;
1338
1339 default: break;
1340 }
1341 }
1342
1343 return returnType;
1344}
1345
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001346TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1347 const TSourceLoc &identifierOrTypeLocation,
1348 const TString &identifier)
1349{
1350 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1351
1352 bool emptyDeclaration = (identifier == "");
1353
1354 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1355
1356 if(emptyDeclaration)
1357 {
1358 if(publicType.isUnsizedArray())
1359 {
1360 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1361 // It is assumed that this applies to empty declarations as well.
1362 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1363 }
1364 }
1365 else
1366 {
1367 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1368 recover();
1369
1370 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1371 recover();
1372
1373 TVariable *variable = nullptr;
1374 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1375 recover();
1376
1377 if(variable && symbol)
1378 symbol->setId(variable->getUniqueId());
1379 }
1380
1381 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1382}
1383
1384TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1385 const TSourceLoc &identifierLocation,
1386 const TString &identifier,
1387 const TSourceLoc &indexLocation,
1388 TIntermTyped *indexExpression)
1389{
1390 mDeferredSingleDeclarationErrorCheck = false;
1391
1392 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1393 recover();
1394
1395 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1396 recover();
1397
1398 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1399 {
1400 recover();
1401 }
1402
1403 TType arrayType(publicType);
1404
1405 int size;
1406 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1407 {
1408 recover();
1409 }
1410 // Make the type an array even if size check failed.
1411 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1412 arrayType.setArraySize(size);
1413
1414 TVariable *variable = nullptr;
1415 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1416 recover();
1417
1418 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1419 if(variable && symbol)
1420 symbol->setId(variable->getUniqueId());
1421
1422 return intermediate.makeAggregate(symbol, identifierLocation);
1423}
1424
1425TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1426 const TSourceLoc &identifierLocation,
1427 const TString &identifier,
1428 const TSourceLoc &initLocation,
1429 TIntermTyped *initializer)
1430{
1431 mDeferredSingleDeclarationErrorCheck = false;
1432
1433 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1434 recover();
1435
1436 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001437 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001438 {
1439 //
1440 // Build intermediate representation
1441 //
1442 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1443 }
1444 else
1445 {
1446 recover();
1447 return nullptr;
1448 }
1449}
1450
1451TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1452 const TSourceLoc &identifierLocation,
1453 const TString &identifier,
1454 const TSourceLoc &indexLocation,
1455 TIntermTyped *indexExpression,
1456 const TSourceLoc &initLocation,
1457 TIntermTyped *initializer)
1458{
1459 mDeferredSingleDeclarationErrorCheck = false;
1460
1461 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1462 recover();
1463
1464 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1465 {
1466 recover();
1467 }
1468
1469 TPublicType arrayType(publicType);
1470
1471 int size = 0;
1472 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1473 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1474 {
1475 recover();
1476 }
1477 // Make the type an array even if size check failed.
1478 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1479 arrayType.setArray(true, size);
1480
1481 // initNode will correspond to the whole of "type b[n] = initializer".
1482 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001483 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001484 {
1485 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1486 }
1487 else
1488 {
1489 recover();
1490 return nullptr;
1491 }
1492}
1493
1494TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1495 const TSourceLoc &identifierLoc,
1496 const TString *identifier,
1497 const TSymbol *symbol)
1498{
1499 // invariant declaration
1500 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1501 {
1502 recover();
1503 }
1504
1505 if(!symbol)
1506 {
1507 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1508 recover();
1509 return nullptr;
1510 }
1511 else
1512 {
1513 const TString kGlFrontFacing("gl_FrontFacing");
1514 if(*identifier == kGlFrontFacing)
1515 {
1516 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1517 recover();
1518 return nullptr;
1519 }
1520 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1521 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1522 ASSERT(variable);
1523 const TType &type = variable->getType();
1524 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1525 *identifier, type, identifierLoc);
1526
1527 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1528 aggregate->setOp(EOpInvariantDeclaration);
1529 return aggregate;
1530 }
1531}
1532
1533TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1534 const TSourceLoc &identifierLocation, const TString &identifier)
1535{
1536 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1537 if(mDeferredSingleDeclarationErrorCheck)
1538 {
1539 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1540 recover();
1541 mDeferredSingleDeclarationErrorCheck = false;
1542 }
1543
1544 if(locationDeclaratorListCheck(identifierLocation, publicType))
1545 recover();
1546
1547 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1548 recover();
1549
1550 TVariable *variable = nullptr;
1551 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1552 recover();
1553
1554 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1555 if(variable && symbol)
1556 symbol->setId(variable->getUniqueId());
1557
1558 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1559}
1560
1561TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1562 const TSourceLoc &identifierLocation, const TString &identifier,
1563 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1564{
1565 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1566 if(mDeferredSingleDeclarationErrorCheck)
1567 {
1568 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1569 recover();
1570 mDeferredSingleDeclarationErrorCheck = false;
1571 }
1572
1573 if(locationDeclaratorListCheck(identifierLocation, publicType))
1574 recover();
1575
1576 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1577 recover();
1578
1579 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1580 {
1581 recover();
1582 }
1583 else
1584 {
1585 TType arrayType = TType(publicType);
1586 int size;
1587 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1588 {
1589 recover();
1590 }
1591 arrayType.setArraySize(size);
1592
1593 TVariable *variable = nullptr;
1594 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1595 recover();
1596
1597 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1598 if(variable && symbol)
1599 symbol->setId(variable->getUniqueId());
1600
1601 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1602 }
1603
1604 return nullptr;
1605}
1606
1607TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1608 const TSourceLoc &identifierLocation, const TString &identifier,
1609 const TSourceLoc &initLocation, TIntermTyped *initializer)
1610{
1611 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1612 if(mDeferredSingleDeclarationErrorCheck)
1613 {
1614 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1615 recover();
1616 mDeferredSingleDeclarationErrorCheck = false;
1617 }
1618
1619 if(locationDeclaratorListCheck(identifierLocation, publicType))
1620 recover();
1621
1622 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001623 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001624 {
1625 //
1626 // build the intermediate representation
1627 //
1628 if(intermNode)
1629 {
1630 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1631 }
1632 else
1633 {
1634 return aggregateDeclaration;
1635 }
1636 }
1637 else
1638 {
1639 recover();
1640 return nullptr;
1641 }
1642}
1643
1644TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1645 TIntermAggregate *aggregateDeclaration,
1646 const TSourceLoc &identifierLocation,
1647 const TString &identifier,
1648 const TSourceLoc &indexLocation,
1649 TIntermTyped *indexExpression,
1650 const TSourceLoc &initLocation, TIntermTyped *initializer)
1651{
1652 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1653 if(mDeferredSingleDeclarationErrorCheck)
1654 {
1655 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1656 recover();
1657 mDeferredSingleDeclarationErrorCheck = false;
1658 }
1659
1660 if(locationDeclaratorListCheck(identifierLocation, publicType))
1661 recover();
1662
1663 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1664 {
1665 recover();
1666 }
1667
1668 TPublicType arrayType(publicType);
1669
1670 int size = 0;
1671 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1672 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1673 {
1674 recover();
1675 }
1676 // Make the type an array even if size check failed.
1677 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1678 arrayType.setArray(true, size);
1679
1680 // initNode will correspond to the whole of "b[n] = initializer".
1681 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001682 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001683 {
1684 if(initNode)
1685 {
1686 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1687 }
1688 else
1689 {
1690 return aggregateDeclaration;
1691 }
1692 }
1693 else
1694 {
1695 recover();
1696 return nullptr;
1697 }
1698}
1699
Alexis Hetua35d8232015-06-11 17:11:06 -04001700void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1701{
Alexis Hetu0a655842015-06-22 16:52:11 -04001702 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001703 {
1704 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1705 recover();
1706 return;
1707 }
1708
1709 if(typeQualifier.qualifier != EvqUniform)
1710 {
1711 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1712 recover();
1713 return;
1714 }
1715
1716 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1717 ASSERT(!layoutQualifier.isEmpty());
1718
1719 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1720 {
1721 recover();
1722 return;
1723 }
1724
1725 if(layoutQualifier.matrixPacking != EmpUnspecified)
1726 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001727 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001728 }
1729
1730 if(layoutQualifier.blockStorage != EbsUnspecified)
1731 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001732 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001733 }
1734}
1735
Alexis Hetue5246692015-06-18 12:34:52 -04001736TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
1737{
1738 TPublicType publicType = publicTypeIn;
1739 TOperator op = EOpNull;
1740 if(publicType.userDef)
1741 {
1742 op = EOpConstructStruct;
1743 }
1744 else
1745 {
1746 switch(publicType.type)
1747 {
1748 case EbtFloat:
1749 if(publicType.isMatrix())
1750 {
1751 switch(publicType.getCols())
1752 {
1753 case 2:
1754 switch(publicType.getRows())
1755 {
1756 case 2: op = EOpConstructMat2; break;
1757 case 3: op = EOpConstructMat2x3; break;
1758 case 4: op = EOpConstructMat2x4; break;
1759 }
1760 break;
1761 case 3:
1762 switch(publicType.getRows())
1763 {
1764 case 2: op = EOpConstructMat3x2; break;
1765 case 3: op = EOpConstructMat3; break;
1766 case 4: op = EOpConstructMat3x4; break;
1767 }
1768 break;
1769 case 4:
1770 switch(publicType.getRows())
1771 {
1772 case 2: op = EOpConstructMat4x2; break;
1773 case 3: op = EOpConstructMat4x3; break;
1774 case 4: op = EOpConstructMat4; break;
1775 }
1776 break;
1777 }
1778 }
1779 else
1780 {
1781 switch(publicType.getNominalSize())
1782 {
1783 case 1: op = EOpConstructFloat; break;
1784 case 2: op = EOpConstructVec2; break;
1785 case 3: op = EOpConstructVec3; break;
1786 case 4: op = EOpConstructVec4; break;
1787 }
1788 }
1789 break;
1790
1791 case EbtInt:
1792 switch(publicType.getNominalSize())
1793 {
1794 case 1: op = EOpConstructInt; break;
1795 case 2: op = EOpConstructIVec2; break;
1796 case 3: op = EOpConstructIVec3; break;
1797 case 4: op = EOpConstructIVec4; break;
1798 }
1799 break;
1800
1801 case EbtUInt:
1802 switch(publicType.getNominalSize())
1803 {
1804 case 1: op = EOpConstructUInt; break;
1805 case 2: op = EOpConstructUVec2; break;
1806 case 3: op = EOpConstructUVec3; break;
1807 case 4: op = EOpConstructUVec4; break;
1808 }
1809 break;
1810
1811 case EbtBool:
1812 switch(publicType.getNominalSize())
1813 {
1814 case 1: op = EOpConstructBool; break;
1815 case 2: op = EOpConstructBVec2; break;
1816 case 3: op = EOpConstructBVec3; break;
1817 case 4: op = EOpConstructBVec4; break;
1818 }
1819 break;
1820
1821 default: break;
1822 }
1823
1824 if(op == EOpNull)
1825 {
1826 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1827 recover();
1828 publicType.type = EbtFloat;
1829 op = EOpConstructFloat;
1830 }
1831 }
1832
1833 TString tempString;
1834 TType type(publicType);
1835 return new TFunction(&tempString, type, op);
1836}
1837
John Bauman66b8ab22014-05-06 15:57:45 -04001838// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001839// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04001840//
1841// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1842//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001843TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04001844{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001845 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04001846
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001847 if(!aggregateArguments)
1848 {
1849 aggregateArguments = new TIntermAggregate;
1850 aggregateArguments->getSequence().push_back(arguments);
John Bauman66b8ab22014-05-06 15:57:45 -04001851 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001852
1853 if(op == EOpConstructStruct)
1854 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04001855 const TFieldList &fields = type->getStruct()->fields();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001856 TIntermSequence &args = aggregateArguments->getSequence();
1857
1858 for(size_t i = 0; i < fields.size(); i++)
1859 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04001860 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001861 {
1862 error(line, "Structure constructor arguments do not match structure fields", "Error");
1863 recover();
1864
1865 return 0;
1866 }
John Bauman66b8ab22014-05-06 15:57:45 -04001867 }
1868 }
1869
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001870 // Turn the argument list itself into a constructor
1871 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1872 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1873 if(constConstructor)
1874 {
John Bauman66b8ab22014-05-06 15:57:45 -04001875 return constConstructor;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001876 }
John Bauman66b8ab22014-05-06 15:57:45 -04001877
1878 return constructor;
1879}
1880
1881TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1882{
1883 bool canBeFolded = areAllChildConst(aggrNode);
1884 aggrNode->setType(type);
1885 if (canBeFolded) {
1886 bool returnVal = false;
1887 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1888 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04001889 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04001890 }
1891 else {
John Baumand4ae8632014-05-06 16:18:33 -04001892 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04001893 }
1894 if (returnVal)
1895 return 0;
1896
1897 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1898 }
1899
1900 return 0;
1901}
1902
John Bauman66b8ab22014-05-06 15:57:45 -04001903//
1904// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1905// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1906// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1907// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1908// a constant matrix.
1909//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001910TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04001911{
1912 TIntermTyped* typedNode;
1913 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1914
1915 ConstantUnion *unionArray;
1916 if (tempConstantNode) {
1917 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04001918
1919 if (!unionArray) {
1920 return node;
1921 }
1922 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1923 error(line, "Cannot offset into the vector", "Error");
1924 recover();
1925
1926 return 0;
1927 }
1928
1929 ConstantUnion* constArray = new ConstantUnion[fields.num];
1930
1931 for (int i = 0; i < fields.num; i++) {
1932 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1933 std::stringstream extraInfoStream;
1934 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1935 std::string extraInfo = extraInfoStream.str();
1936 error(line, "", "[", extraInfo.c_str());
1937 recover();
1938 fields.offsets[i] = 0;
1939 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001940
John Bauman66b8ab22014-05-06 15:57:45 -04001941 constArray[i] = unionArray[fields.offsets[i]];
1942
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001943 }
John Bauman66b8ab22014-05-06 15:57:45 -04001944 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1945 return typedNode;
1946}
1947
1948//
1949// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001950// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1951// 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 -04001952// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1953//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001954TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04001955{
1956 TIntermTyped* typedNode;
1957 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1958
1959 if (index >= node->getType().getNominalSize()) {
1960 std::stringstream extraInfoStream;
1961 extraInfoStream << "matrix field selection out of range '" << index << "'";
1962 std::string extraInfo = extraInfoStream.str();
1963 error(line, "", "[", extraInfo.c_str());
1964 recover();
1965 index = 0;
1966 }
1967
1968 if (tempConstantNode) {
1969 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1970 int size = tempConstantNode->getType().getNominalSize();
1971 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1972 } else {
1973 error(line, "Cannot offset into the matrix", "Error");
1974 recover();
1975
1976 return 0;
1977 }
1978
1979 return typedNode;
1980}
1981
1982
1983//
1984// 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 -04001985// the symbol table and parse-tree is built for the type of the element. The input
1986// 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 -04001987// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1988//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001989TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04001990{
1991 TIntermTyped* typedNode;
1992 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1993 TType arrayElementType = node->getType();
1994 arrayElementType.clearArrayness();
1995
1996 if (index >= node->getType().getArraySize()) {
1997 std::stringstream extraInfoStream;
1998 extraInfoStream << "array field selection out of range '" << index << "'";
1999 std::string extraInfo = extraInfoStream.str();
2000 error(line, "", "[", extraInfo.c_str());
2001 recover();
2002 index = 0;
2003 }
2004
2005 int arrayElementSize = arrayElementType.getObjectSize();
2006
2007 if (tempConstantNode) {
2008 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2009 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2010 } else {
2011 error(line, "Cannot offset into the array", "Error");
2012 recover();
2013
2014 return 0;
2015 }
2016
2017 return typedNode;
2018}
2019
2020
2021//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002022// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002023// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2024// function and returns the parse-tree with the values of the embedded/nested struct.
2025//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002026TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002027{
Alexis Hetua8b364b2015-06-10 11:48:40 -04002028 const TFieldList &fields = node->getType().getStruct()->fields();
John Bauman66b8ab22014-05-06 15:57:45 -04002029 TIntermTyped *typedNode;
2030 int instanceSize = 0;
2031 unsigned int index = 0;
2032 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
2033
Alexis Hetua8b364b2015-06-10 11:48:40 -04002034 for ( index = 0; index < fields.size(); ++index) {
2035 if (fields[index]->name() == identifier) {
John Bauman66b8ab22014-05-06 15:57:45 -04002036 break;
2037 } else {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002038 instanceSize += fields[index]->type()->getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002039 }
2040 }
2041
2042 if (tempConstantNode) {
2043 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
2044
2045 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2046 } else {
2047 error(line, "Cannot offset into the structure", "Error");
2048 recover();
2049
2050 return 0;
2051 }
2052
2053 return typedNode;
2054}
2055
Alexis Hetuad6b8752015-06-09 16:15:30 -04002056//
Alexis Hetua35d8232015-06-11 17:11:06 -04002057// Interface/uniform blocks
2058//
2059TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
2060 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
2061{
2062 if(reservedErrorCheck(nameLine, blockName))
2063 recover();
2064
2065 if(typeQualifier.qualifier != EvqUniform)
2066 {
2067 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2068 recover();
2069 }
2070
2071 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2072 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2073 {
2074 recover();
2075 }
2076
2077 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2078 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002079 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002080 }
2081
2082 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2083 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002084 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002085 }
2086
2087 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2088 if(!symbolTable.declare(*blockNameSymbol)) {
2089 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2090 recover();
2091 }
2092
2093 // check for sampler types and apply layout qualifiers
2094 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2095 TField* field = (*fieldList)[memberIndex];
2096 TType* fieldType = field->type();
2097 if(IsSampler(fieldType->getBasicType())) {
2098 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2099 recover();
2100 }
2101
2102 const TQualifier qualifier = fieldType->getQualifier();
2103 switch(qualifier)
2104 {
2105 case EvqGlobal:
2106 case EvqUniform:
2107 break;
2108 default:
2109 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2110 recover();
2111 break;
2112 }
2113
2114 // check layout qualifiers
2115 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2116 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2117 {
2118 recover();
2119 }
2120
2121 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2122 {
2123 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2124 recover();
2125 }
2126
2127 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2128 {
2129 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2130 }
2131 else if(!fieldType->isMatrix())
2132 {
2133 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
2134 recover();
2135 }
2136
2137 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2138 }
2139
2140 // add array index
2141 int arraySize = 0;
2142 if(arrayIndex != NULL)
2143 {
2144 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2145 recover();
2146 }
2147
2148 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2149 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2150
2151 TString symbolName = "";
2152 int symbolId = 0;
2153
2154 if(!instanceName)
2155 {
2156 // define symbols for the members of the interface block
2157 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2158 {
2159 TField* field = (*fieldList)[memberIndex];
2160 TType* fieldType = field->type();
2161
2162 // set parent pointer of the field variable
2163 fieldType->setInterfaceBlock(interfaceBlock);
2164
2165 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2166 fieldVariable->setQualifier(typeQualifier.qualifier);
2167
2168 if(!symbolTable.declare(*fieldVariable)) {
2169 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2170 recover();
2171 }
2172 }
2173 }
2174 else
2175 {
2176 // add a symbol for this interface block
2177 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2178 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2179
2180 if(!symbolTable.declare(*instanceTypeDef)) {
2181 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2182 recover();
2183 }
2184
2185 symbolId = instanceTypeDef->getUniqueId();
2186 symbolName = instanceTypeDef->getName();
2187 }
2188
2189 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2190 aggregate->setOp(EOpDeclaration);
2191
2192 exitStructDeclaration();
2193 return aggregate;
2194}
2195
2196//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002197// Parse an array index expression
2198//
2199TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2200{
2201 TIntermTyped *indexedExpression = NULL;
2202
2203 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2204 {
2205 if(baseExpression->getAsSymbolNode())
2206 {
2207 error(location, " left of '[' is not of type array, matrix, or vector ",
2208 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2209 }
2210 else
2211 {
2212 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2213 }
2214 recover();
2215 }
2216
2217 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2218
2219 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2220 {
2221 int index = indexConstantUnion->getIConst(0);
2222 if(index < 0)
2223 {
2224 std::stringstream infoStream;
2225 infoStream << index;
2226 std::string info = infoStream.str();
2227 error(location, "negative index", info.c_str());
2228 recover();
2229 index = 0;
2230 }
2231 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2232 {
2233 if(baseExpression->isArray())
2234 {
2235 // constant folding for arrays
2236 indexedExpression = addConstArrayNode(index, baseExpression, location);
2237 }
2238 else if(baseExpression->isVector())
2239 {
2240 // constant folding for vectors
2241 TVectorFields fields;
2242 fields.num = 1;
2243 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2244 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2245 }
2246 else if(baseExpression->isMatrix())
2247 {
2248 // constant folding for matrices
2249 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2250 }
2251 }
2252 else
2253 {
2254 int safeIndex = -1;
2255
2256 if(baseExpression->isArray())
2257 {
2258 if(index >= baseExpression->getType().getArraySize())
2259 {
2260 std::stringstream extraInfoStream;
2261 extraInfoStream << "array index out of range '" << index << "'";
2262 std::string extraInfo = extraInfoStream.str();
2263 error(location, "", "[", extraInfo.c_str());
2264 recover();
2265 safeIndex = baseExpression->getType().getArraySize() - 1;
2266 }
2267 }
2268 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2269 baseExpression->getType().getNominalSize() <= index)
2270 {
2271 std::stringstream extraInfoStream;
2272 extraInfoStream << "field selection out of range '" << index << "'";
2273 std::string extraInfo = extraInfoStream.str();
2274 error(location, "", "[", extraInfo.c_str());
2275 recover();
2276 safeIndex = baseExpression->getType().getNominalSize() - 1;
2277 }
2278
2279 // Don't modify the data of the previous constant union, because it can point
2280 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2281 if(safeIndex != -1)
2282 {
2283 ConstantUnion *safeConstantUnion = new ConstantUnion();
2284 safeConstantUnion->setIConst(safeIndex);
2285 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2286 }
2287
2288 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2289 }
2290 }
2291 else
2292 {
2293 if(baseExpression->isInterfaceBlock())
2294 {
2295 error(location, "",
2296 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2297 recover();
2298 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002299 else if(baseExpression->getQualifier() == EvqFragmentOut)
2300 {
2301 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2302 recover();
2303 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002304
2305 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2306 }
2307
2308 if(indexedExpression == 0)
2309 {
2310 ConstantUnion *unionArray = new ConstantUnion[1];
2311 unionArray->setFConst(0.0f);
2312 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2313 }
2314 else if(baseExpression->isArray())
2315 {
2316 const TType &baseType = baseExpression->getType();
2317 if(baseType.getStruct())
2318 {
2319 TType copyOfType(baseType.getStruct());
2320 indexedExpression->setType(copyOfType);
2321 }
2322 else if(baseType.isInterfaceBlock())
2323 {
2324 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
2325 indexedExpression->setType(copyOfType);
2326 }
2327 else
2328 {
2329 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2330 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2331 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2332 }
2333
2334 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2335 {
2336 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2337 }
2338 }
2339 else if(baseExpression->isMatrix())
2340 {
2341 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2342 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2343 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2344 }
2345 else if(baseExpression->isVector())
2346 {
2347 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2348 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2349 }
2350 else
2351 {
2352 indexedExpression->setType(baseExpression->getType());
2353 }
2354
2355 return indexedExpression;
2356}
2357
2358TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2359 const TString &fieldString, const TSourceLoc &fieldLocation)
2360{
2361 TIntermTyped *indexedExpression = NULL;
2362
2363 if(baseExpression->isArray())
2364 {
2365 error(fieldLocation, "cannot apply dot operator to an array", ".");
2366 recover();
2367 }
2368
2369 if(baseExpression->isVector())
2370 {
2371 TVectorFields fields;
2372 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2373 {
2374 fields.num = 1;
2375 fields.offsets[0] = 0;
2376 recover();
2377 }
2378
2379 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2380 {
2381 // constant folding for vector fields
2382 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2383 if(indexedExpression == 0)
2384 {
2385 recover();
2386 indexedExpression = baseExpression;
2387 }
2388 else
2389 {
2390 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2391 EvqConstExpr, (unsigned char)(fieldString).size()));
2392 }
2393 }
2394 else
2395 {
2396 TString vectorString = fieldString;
2397 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2398 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2399 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2400 EvqTemporary, (unsigned char)vectorString.size()));
2401 }
2402 }
2403 else if(baseExpression->isMatrix())
2404 {
2405 TMatrixFields fields;
2406 if(!parseMatrixFields(fieldString, baseExpression->getNominalSize(), baseExpression->getSecondarySize(), fields, fieldLocation))
2407 {
2408 fields.wholeRow = false;
2409 fields.wholeCol = false;
2410 fields.row = 0;
2411 fields.col = 0;
2412 recover();
2413 }
2414
2415 if(fields.wholeRow || fields.wholeCol)
2416 {
2417 error(dotLocation, " non-scalar fields not implemented yet", ".");
2418 recover();
2419 ConstantUnion *unionArray = new ConstantUnion[1];
2420 unionArray->setIConst(0);
2421 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2422 fieldLocation);
2423 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2424 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2425 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2426 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2427 }
2428 else
2429 {
2430 ConstantUnion *unionArray = new ConstantUnion[1];
2431 unionArray->setIConst(fields.col * baseExpression->getSecondarySize() + fields.row);
2432 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2433 fieldLocation);
2434 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2435 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2436 }
2437 }
2438 else if(baseExpression->getBasicType() == EbtStruct)
2439 {
2440 bool fieldFound = false;
2441 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2442 if(fields.empty())
2443 {
2444 error(dotLocation, "structure has no fields", "Internal Error");
2445 recover();
2446 indexedExpression = baseExpression;
2447 }
2448 else
2449 {
2450 unsigned int i;
2451 for(i = 0; i < fields.size(); ++i)
2452 {
2453 if(fields[i]->name() == fieldString)
2454 {
2455 fieldFound = true;
2456 break;
2457 }
2458 }
2459 if(fieldFound)
2460 {
2461 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2462 {
2463 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2464 if(indexedExpression == 0)
2465 {
2466 recover();
2467 indexedExpression = baseExpression;
2468 }
2469 else
2470 {
2471 indexedExpression->setType(*fields[i]->type());
2472 // change the qualifier of the return type, not of the structure field
2473 // as the structure definition is shared between various structures.
2474 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2475 }
2476 }
2477 else
2478 {
2479 ConstantUnion *unionArray = new ConstantUnion[1];
2480 unionArray->setIConst(i);
2481 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2482 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2483 indexedExpression->setType(*fields[i]->type());
2484 }
2485 }
2486 else
2487 {
2488 error(dotLocation, " no such field in structure", fieldString.c_str());
2489 recover();
2490 indexedExpression = baseExpression;
2491 }
2492 }
2493 }
2494 else if(baseExpression->isInterfaceBlock())
2495 {
2496 bool fieldFound = false;
2497 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2498 if(fields.empty())
2499 {
2500 error(dotLocation, "interface block has no fields", "Internal Error");
2501 recover();
2502 indexedExpression = baseExpression;
2503 }
2504 else
2505 {
2506 unsigned int i;
2507 for(i = 0; i < fields.size(); ++i)
2508 {
2509 if(fields[i]->name() == fieldString)
2510 {
2511 fieldFound = true;
2512 break;
2513 }
2514 }
2515 if(fieldFound)
2516 {
2517 ConstantUnion *unionArray = new ConstantUnion[1];
2518 unionArray->setIConst(i);
2519 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2520 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2521 dotLocation);
2522 indexedExpression->setType(*fields[i]->type());
2523 }
2524 else
2525 {
2526 error(dotLocation, " no such field in interface block", fieldString.c_str());
2527 recover();
2528 indexedExpression = baseExpression;
2529 }
2530 }
2531 }
2532 else
2533 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002534 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002535 {
2536 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2537 fieldString.c_str());
2538 }
2539 else
2540 {
2541 error(dotLocation,
2542 " field selection requires structure, vector, matrix, or interface block on left hand side",
2543 fieldString.c_str());
2544 }
2545 recover();
2546 indexedExpression = baseExpression;
2547 }
2548
2549 return indexedExpression;
2550}
2551
Nicolas Capens7d626792015-02-17 17:58:31 -05002552TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2553{
2554 TLayoutQualifier qualifier;
2555
2556 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002557 qualifier.matrixPacking = EmpUnspecified;
2558 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002559
Alexis Hetuad6b8752015-06-09 16:15:30 -04002560 if(qualifierType == "shared")
2561 {
2562 qualifier.blockStorage = EbsShared;
2563 }
2564 else if(qualifierType == "packed")
2565 {
2566 qualifier.blockStorage = EbsPacked;
2567 }
2568 else if(qualifierType == "std140")
2569 {
2570 qualifier.blockStorage = EbsStd140;
2571 }
2572 else if(qualifierType == "row_major")
2573 {
2574 qualifier.matrixPacking = EmpRowMajor;
2575 }
2576 else if(qualifierType == "column_major")
2577 {
2578 qualifier.matrixPacking = EmpColumnMajor;
2579 }
2580 else if(qualifierType == "location")
Nicolas Capens7d626792015-02-17 17:58:31 -05002581 {
2582 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2583 recover();
2584 }
2585 else
2586 {
2587 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2588 recover();
2589 }
2590
2591 return qualifier;
2592}
2593
2594TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2595{
2596 TLayoutQualifier qualifier;
2597
2598 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002599 qualifier.matrixPacking = EmpUnspecified;
2600 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002601
2602 if (qualifierType != "location")
2603 {
2604 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2605 recover();
2606 }
2607 else
2608 {
2609 // must check that location is non-negative
2610 if (intValue < 0)
2611 {
2612 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2613 recover();
2614 }
2615 else
2616 {
2617 qualifier.location = intValue;
2618 }
2619 }
2620
2621 return qualifier;
2622}
2623
2624TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2625{
2626 TLayoutQualifier joinedQualifier = leftQualifier;
2627
2628 if (rightQualifier.location != -1)
2629 {
2630 joinedQualifier.location = rightQualifier.location;
2631 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002632 if(rightQualifier.matrixPacking != EmpUnspecified)
2633 {
2634 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2635 }
2636 if(rightQualifier.blockStorage != EbsUnspecified)
2637 {
2638 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2639 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002640
2641 return joinedQualifier;
2642}
2643
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002644
2645TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2646 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2647{
2648 TQualifier mergedQualifier = EvqSmoothIn;
2649
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002650 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002651 if(interpolationQualifier == EvqSmooth)
2652 mergedQualifier = EvqSmoothIn;
2653 else if(interpolationQualifier == EvqFlat)
2654 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002655 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002656 }
2657 else if(storageQualifier == EvqCentroidIn) {
2658 if(interpolationQualifier == EvqSmooth)
2659 mergedQualifier = EvqCentroidIn;
2660 else if(interpolationQualifier == EvqFlat)
2661 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002662 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002663 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002664 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002665 if(interpolationQualifier == EvqSmooth)
2666 mergedQualifier = EvqSmoothOut;
2667 else if(interpolationQualifier == EvqFlat)
2668 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002669 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002670 }
2671 else if(storageQualifier == EvqCentroidOut) {
2672 if(interpolationQualifier == EvqSmooth)
2673 mergedQualifier = EvqCentroidOut;
2674 else if(interpolationQualifier == EvqFlat)
2675 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002676 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002677 }
2678 else {
2679 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2680 recover();
2681
2682 mergedQualifier = storageQualifier;
2683 }
2684
2685 TPublicType type;
2686 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2687 return type;
2688}
2689
Alexis Hetuad6b8752015-06-09 16:15:30 -04002690TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2691{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002692 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002693 {
2694 recover();
2695 }
2696
2697 for(unsigned int i = 0; i < fieldList->size(); ++i)
2698 {
2699 //
2700 // Careful not to replace already known aspects of type, like array-ness
2701 //
2702 TType *type = (*fieldList)[i]->type();
2703 type->setBasicType(typeSpecifier.type);
2704 type->setNominalSize(typeSpecifier.primarySize);
2705 type->setSecondarySize(typeSpecifier.secondarySize);
2706 type->setPrecision(typeSpecifier.precision);
2707 type->setQualifier(typeSpecifier.qualifier);
2708 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2709
2710 // don't allow arrays of arrays
2711 if(type->isArray())
2712 {
2713 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2714 recover();
2715 }
2716 if(typeSpecifier.array)
2717 type->setArraySize(typeSpecifier.arraySize);
2718 if(typeSpecifier.userDef)
2719 {
2720 type->setStruct(typeSpecifier.userDef->getStruct());
2721 }
2722
2723 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2724 {
2725 recover();
2726 }
2727 }
2728
2729 return fieldList;
2730}
2731
2732TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2733 const TString *structName, TFieldList *fieldList)
2734{
2735 TStructure *structure = new TStructure(structName, fieldList);
2736 TType *structureType = new TType(structure);
2737
2738 // Store a bool in the struct if we're at global scope, to allow us to
2739 // skip the local struct scoping workaround in HLSL.
2740 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2741 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2742
2743 if(!structName->empty())
2744 {
2745 if(reservedErrorCheck(nameLine, *structName))
2746 {
2747 recover();
2748 }
2749 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2750 if(!symbolTable.declare(*userTypeDef))
2751 {
2752 error(nameLine, "redefinition", structName->c_str(), "struct");
2753 recover();
2754 }
2755 }
2756
2757 // ensure we do not specify any storage qualifiers on the struct members
2758 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2759 {
2760 const TField &field = *(*fieldList)[typeListIndex];
2761 const TQualifier qualifier = field.type()->getQualifier();
2762 switch(qualifier)
2763 {
2764 case EvqGlobal:
2765 case EvqTemporary:
2766 break;
2767 default:
2768 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2769 recover();
2770 break;
2771 }
2772 }
2773
2774 TPublicType publicType;
2775 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2776 publicType.userDef = structureType;
2777 exitStructDeclaration();
2778
2779 return publicType;
2780}
2781
Alexis Hetufe1269e2015-06-16 12:43:32 -04002782bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04002783{
Alexis Hetu0a655842015-06-22 16:52:11 -04002784 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04002785
2786 // Embedded structure definitions are not supported per GLSL ES spec.
2787 // They aren't allowed in GLSL either, but we need to detect this here
2788 // so we don't rely on the GLSL compiler to catch it.
Alexis Hetu0a655842015-06-22 16:52:11 -04002789 if (mStructNestingLevel > 1) {
John Bauman66b8ab22014-05-06 15:57:45 -04002790 error(line, "", "Embedded struct definitions are not allowed");
2791 return true;
2792 }
2793
2794 return false;
2795}
2796
2797void TParseContext::exitStructDeclaration()
2798{
Alexis Hetu0a655842015-06-22 16:52:11 -04002799 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04002800}
2801
Alexis Hetuad6b8752015-06-09 16:15:30 -04002802bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
2803{
2804 static const int kWebGLMaxStructNesting = 4;
2805
2806 if(field.type()->getBasicType() != EbtStruct)
2807 {
2808 return false;
2809 }
2810
2811 // We're already inside a structure definition at this point, so add
2812 // one to the field's struct nesting.
2813 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2814 {
2815 std::stringstream reasonStream;
2816 reasonStream << "Reference of struct type "
2817 << field.type()->getStruct()->name().c_str()
2818 << " exceeds maximum allowed nesting level of "
2819 << kWebGLMaxStructNesting;
2820 std::string reason = reasonStream.str();
2821 error(line, reason.c_str(), field.name().c_str(), "");
2822 return true;
2823 }
2824
2825 return false;
2826}
2827
2828TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
2829{
2830 if(child == nullptr)
2831 {
2832 return nullptr;
2833 }
2834
2835 switch(op)
2836 {
2837 case EOpLogicalNot:
2838 if(child->getBasicType() != EbtBool ||
2839 child->isMatrix() ||
2840 child->isArray() ||
2841 child->isVector())
2842 {
2843 return nullptr;
2844 }
2845 break;
2846 case EOpBitwiseNot:
2847 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2848 child->isMatrix() ||
2849 child->isArray())
2850 {
2851 return nullptr;
2852 }
2853 break;
2854 case EOpPostIncrement:
2855 case EOpPreIncrement:
2856 case EOpPostDecrement:
2857 case EOpPreDecrement:
2858 case EOpNegative:
2859 if(child->getBasicType() == EbtStruct ||
2860 child->getBasicType() == EbtBool ||
2861 child->isArray())
2862 {
2863 return nullptr;
2864 }
2865 // Operators for built-ins are already type checked against their prototype.
2866 default:
2867 break;
2868 }
2869
2870 return intermediate.addUnaryMath(op, child, loc); // FIXME , funcReturnType);
2871}
2872
2873TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2874{
2875 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
2876 if(node == nullptr)
2877 {
2878 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
2879 recover();
2880 return child;
2881 }
2882 return node;
2883}
2884
2885TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2886{
2887 if(lValueErrorCheck(loc, getOperatorString(op), child))
2888 recover();
2889 return addUnaryMath(op, child, loc);
2890}
2891
2892bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
2893{
2894 if(left->isArray() || right->isArray())
2895 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002896 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002897 {
2898 error(loc, "Invalid operation for arrays", getOperatorString(op));
2899 return false;
2900 }
2901
2902 if(left->isArray() != right->isArray())
2903 {
2904 error(loc, "array / non-array mismatch", getOperatorString(op));
2905 return false;
2906 }
2907
2908 switch(op)
2909 {
2910 case EOpEqual:
2911 case EOpNotEqual:
2912 case EOpAssign:
2913 case EOpInitialize:
2914 break;
2915 default:
2916 error(loc, "Invalid operation for arrays", getOperatorString(op));
2917 return false;
2918 }
2919 // At this point, size of implicitly sized arrays should be resolved.
2920 if(left->getArraySize() != right->getArraySize())
2921 {
2922 error(loc, "array size mismatch", getOperatorString(op));
2923 return false;
2924 }
2925 }
2926
2927 // Check ops which require integer / ivec parameters
2928 bool isBitShift = false;
2929 switch(op)
2930 {
2931 case EOpBitShiftLeft:
2932 case EOpBitShiftRight:
2933 case EOpBitShiftLeftAssign:
2934 case EOpBitShiftRightAssign:
2935 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2936 // check that the basic type is an integer type.
2937 isBitShift = true;
2938 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2939 {
2940 return false;
2941 }
2942 break;
2943 case EOpBitwiseAnd:
2944 case EOpBitwiseXor:
2945 case EOpBitwiseOr:
2946 case EOpBitwiseAndAssign:
2947 case EOpBitwiseXorAssign:
2948 case EOpBitwiseOrAssign:
2949 // It is enough to check the type of only one operand, since later it
2950 // is checked that the operand types match.
2951 if(!IsInteger(left->getBasicType()))
2952 {
2953 return false;
2954 }
2955 break;
2956 default:
2957 break;
2958 }
2959
2960 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2961 // So the basic type should usually match.
2962 if(!isBitShift && left->getBasicType() != right->getBasicType())
2963 {
2964 return false;
2965 }
2966
2967 // Check that type sizes match exactly on ops that require that.
2968 // Also check restrictions for structs that contain arrays or samplers.
2969 switch(op)
2970 {
2971 case EOpAssign:
2972 case EOpInitialize:
2973 case EOpEqual:
2974 case EOpNotEqual:
2975 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04002976 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002977 {
2978 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
2979 return false;
2980 }
2981 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2982 // we interpret the spec so that this extends to structs containing samplers,
2983 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04002984 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04002985 left->getType().isStructureContainingSamplers())
2986 {
2987 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
2988 return false;
2989 }
2990 case EOpLessThan:
2991 case EOpGreaterThan:
2992 case EOpLessThanEqual:
2993 case EOpGreaterThanEqual:
2994 if((left->getNominalSize() != right->getNominalSize()) ||
2995 (left->getSecondarySize() != right->getSecondarySize()))
2996 {
2997 return false;
2998 }
2999 default:
3000 break;
3001 }
3002
3003 return true;
3004}
3005
Alexis Hetu76a343a2015-06-04 17:21:22 -04003006TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3007{
3008 TBasicType switchType = init->getBasicType();
3009 if((switchType != EbtInt && switchType != EbtUInt) ||
3010 init->isMatrix() ||
3011 init->isArray() ||
3012 init->isVector())
3013 {
3014 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3015 recover();
3016 return nullptr;
3017 }
3018
3019 if(statementList)
3020 {
3021 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3022 {
3023 recover();
3024 return nullptr;
3025 }
3026 }
3027
3028 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3029 if(node == nullptr)
3030 {
3031 error(loc, "erroneous switch statement", "switch");
3032 recover();
3033 return nullptr;
3034 }
3035 return node;
3036}
3037
3038TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3039{
Alexis Hetu0a655842015-06-22 16:52:11 -04003040 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003041 {
3042 error(loc, "case labels need to be inside switch statements", "case");
3043 recover();
3044 return nullptr;
3045 }
3046 if(condition == nullptr)
3047 {
3048 error(loc, "case label must have a condition", "case");
3049 recover();
3050 return nullptr;
3051 }
3052 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3053 condition->isMatrix() ||
3054 condition->isArray() ||
3055 condition->isVector())
3056 {
3057 error(condition->getLine(), "case label must be a scalar integer", "case");
3058 recover();
3059 }
3060 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3061 if(conditionConst == nullptr)
3062 {
3063 error(condition->getLine(), "case label must be constant", "case");
3064 recover();
3065 }
3066 TIntermCase *node = intermediate.addCase(condition, loc);
3067 if(node == nullptr)
3068 {
3069 error(loc, "erroneous case statement", "case");
3070 recover();
3071 return nullptr;
3072 }
3073 return node;
3074}
3075
3076TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3077{
Alexis Hetu0a655842015-06-22 16:52:11 -04003078 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003079 {
3080 error(loc, "default labels need to be inside switch statements", "default");
3081 recover();
3082 return nullptr;
3083 }
3084 TIntermCase *node = intermediate.addCase(nullptr, loc);
3085 if(node == nullptr)
3086 {
3087 error(loc, "erroneous default statement", "default");
3088 recover();
3089 return nullptr;
3090 }
3091 return node;
3092}
Alexis Hetue5246692015-06-18 12:34:52 -04003093TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3094{
3095 if(binaryOpCommonCheck(op, left, right, loc))
3096 {
3097 return intermediate.addAssign(op, left, right, loc);
3098 }
3099 return nullptr;
3100}
3101
3102TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3103{
3104 TIntermTyped *node = createAssign(op, left, right, loc);
3105 if(node == nullptr)
3106 {
3107 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3108 recover();
3109 return left;
3110 }
3111 return node;
3112}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003113
Alexis Hetub4769582015-06-16 12:19:50 -04003114TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3115 const TSourceLoc &loc)
3116{
3117 if(!binaryOpCommonCheck(op, left, right, loc))
3118 return nullptr;
3119
3120 switch(op)
3121 {
3122 case EOpEqual:
3123 case EOpNotEqual:
3124 break;
3125 case EOpLessThan:
3126 case EOpGreaterThan:
3127 case EOpLessThanEqual:
3128 case EOpGreaterThanEqual:
3129 ASSERT(!left->isArray() && !right->isArray());
3130 if(left->isMatrix() || left->isVector() ||
3131 left->getBasicType() == EbtStruct)
3132 {
3133 return nullptr;
3134 }
3135 break;
3136 case EOpLogicalOr:
3137 case EOpLogicalXor:
3138 case EOpLogicalAnd:
3139 ASSERT(!left->isArray() && !right->isArray());
3140 if(left->getBasicType() != EbtBool ||
3141 left->isMatrix() || left->isVector())
3142 {
3143 return nullptr;
3144 }
3145 break;
3146 case EOpAdd:
3147 case EOpSub:
3148 case EOpDiv:
3149 case EOpMul:
3150 ASSERT(!left->isArray() && !right->isArray());
3151 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3152 {
3153 return nullptr;
3154 }
3155 break;
3156 case EOpIMod:
3157 ASSERT(!left->isArray() && !right->isArray());
3158 // Note that this is only for the % operator, not for mod()
3159 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3160 {
3161 return nullptr;
3162 }
3163 break;
3164 // Note that for bitwise ops, type checking is done in promote() to
3165 // share code between ops and compound assignment
3166 default:
3167 break;
3168 }
3169
3170 return intermediate.addBinaryMath(op, left, right, loc);
3171}
3172
3173TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3174{
3175 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3176 if(node == 0)
3177 {
3178 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3179 recover();
3180 return left;
3181 }
3182 return node;
3183}
3184
3185TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3186{
3187 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3188 if(node == 0)
3189 {
3190 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3191 recover();
3192 ConstantUnion *unionArray = new ConstantUnion[1];
3193 unionArray->setBConst(false);
3194 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3195 }
3196 return node;
3197}
3198
Alexis Hetu76a343a2015-06-04 17:21:22 -04003199TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3200{
3201 switch(op)
3202 {
3203 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003204 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003205 {
3206 error(loc, "continue statement only allowed in loops", "");
3207 recover();
3208 }
3209 break;
3210 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003211 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003212 {
3213 error(loc, "break statement only allowed in loops and switch statements", "");
3214 recover();
3215 }
3216 break;
3217 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003218 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003219 {
3220 error(loc, "non-void function must return a value", "return");
3221 recover();
3222 }
3223 break;
3224 default:
3225 // No checks for discard
3226 break;
3227 }
3228 return intermediate.addBranch(op, loc);
3229}
3230
3231TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3232{
3233 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003234 mFunctionReturnsValue = true;
3235 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003236 {
3237 error(loc, "void function cannot return a value", "return");
3238 recover();
3239 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003240 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003241 {
3242 error(loc, "function return is not matching type:", "return");
3243 recover();
3244 }
3245 return intermediate.addBranch(op, returnValue, loc);
3246}
3247
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003248TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3249{
3250 *fatalError = false;
3251 TOperator op = fnCall->getBuiltInOp();
3252 TIntermTyped *callNode = nullptr;
3253
3254 if(thisNode != nullptr)
3255 {
3256 ConstantUnion *unionArray = new ConstantUnion[1];
3257 int arraySize = 0;
3258 TIntermTyped *typedThis = thisNode->getAsTyped();
3259 if(fnCall->getName() != "length")
3260 {
3261 error(loc, "invalid method", fnCall->getName().c_str());
3262 recover();
3263 }
3264 else if(paramNode != nullptr)
3265 {
3266 error(loc, "method takes no parameters", "length");
3267 recover();
3268 }
3269 else if(typedThis == nullptr || !typedThis->isArray())
3270 {
3271 error(loc, "length can only be called on arrays", "length");
3272 recover();
3273 }
3274 else
3275 {
3276 arraySize = typedThis->getArraySize();
3277 if(typedThis->getAsSymbolNode() == nullptr)
3278 {
3279 // This code path can be hit with expressions like these:
3280 // (a = b).length()
3281 // (func()).length()
3282 // (int[3](0, 1, 2)).length()
3283 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3284 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3285 // which allows "An array, vector or matrix expression with the length method applied".
3286 error(loc, "length can only be called on array names, not on array expressions", "length");
3287 recover();
3288 }
3289 }
3290 unionArray->setIConst(arraySize);
3291 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3292 }
3293 else if(op != EOpNull)
3294 {
3295 //
3296 // Then this should be a constructor.
3297 // Don't go through the symbol table for constructors.
3298 // Their parameters will be verified algorithmically.
3299 //
3300 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3301 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3302 {
3303 //
3304 // It's a constructor, of type 'type'.
3305 //
3306 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3307 }
3308
3309 if(callNode == nullptr)
3310 {
3311 recover();
3312 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3313 }
3314 callNode->setType(type);
3315 }
3316 else
3317 {
3318 //
3319 // Not a constructor. Find it in the symbol table.
3320 //
3321 const TFunction *fnCandidate;
3322 bool builtIn;
3323 fnCandidate = findFunction(loc, fnCall, &builtIn);
3324 if(fnCandidate)
3325 {
3326 //
3327 // A declared function.
3328 //
3329 if(builtIn && !fnCandidate->getExtension().empty() &&
3330 extensionErrorCheck(loc, fnCandidate->getExtension()))
3331 {
3332 recover();
3333 }
3334 op = fnCandidate->getBuiltInOp();
3335 if(builtIn && op != EOpNull)
3336 {
3337 //
3338 // A function call mapped to a built-in operation.
3339 //
3340 if(fnCandidate->getParamCount() == 1)
3341 {
3342 //
3343 // Treat it like a built-in unary operator.
3344 //
3345 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3346 if(callNode == nullptr)
3347 {
3348 std::stringstream extraInfoStream;
3349 extraInfoStream << "built in unary operator function. Type: "
3350 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3351 std::string extraInfo = extraInfoStream.str();
3352 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3353 *fatalError = true;
3354 return nullptr;
3355 }
3356 }
3357 else
3358 {
3359 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3360 aggregate->setType(fnCandidate->getReturnType());
3361
3362 // Some built-in functions have out parameters too.
3363 functionCallLValueErrorCheck(fnCandidate, aggregate);
3364
3365 callNode = aggregate;
3366 }
3367 }
3368 else
3369 {
3370 // This is a real function call
3371
3372 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3373 aggregate->setType(fnCandidate->getReturnType());
3374
3375 // this is how we know whether the given function is a builtIn function or a user defined function
3376 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3377 // if builtIn == true, it's definitely a builtIn function with EOpNull
3378 if(!builtIn)
3379 aggregate->setUserDefined();
3380 aggregate->setName(fnCandidate->getMangledName());
3381
3382 callNode = aggregate;
3383
3384 functionCallLValueErrorCheck(fnCandidate, aggregate);
3385 }
3386 callNode->setType(fnCandidate->getReturnType());
3387 }
3388 else
3389 {
3390 // error message was put out by findFunction()
3391 // Put on a dummy node for error recovery
3392 ConstantUnion *unionArray = new ConstantUnion[1];
3393 unionArray->setFConst(0.0f);
3394 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3395 recover();
3396 }
3397 }
3398 delete fnCall;
3399 return callNode;
3400}
3401
Alexis Hetueee212e2015-07-07 17:13:30 -04003402TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3403{
3404 if(boolErrorCheck(loc, cond))
3405 recover();
3406
3407 if(trueBlock->getType() != falseBlock->getType())
3408 {
3409 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3410 recover();
3411 return falseBlock;
3412 }
3413 // ESSL1 sections 5.2 and 5.7:
3414 // ESSL3 section 5.7:
3415 // Ternary operator is not among the operators allowed for structures/arrays.
3416 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3417 {
3418 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3419 recover();
3420 return falseBlock;
3421 }
3422 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3423}
3424
John Bauman66b8ab22014-05-06 15:57:45 -04003425//
3426// Parse an array of strings using yyparse.
3427//
3428// Returns 0 for success.
3429//
3430int PaParseStrings(int count, const char* const string[], const int length[],
3431 TParseContext* context) {
3432 if ((count == 0) || (string == NULL))
3433 return 1;
3434
3435 if (glslang_initialize(context))
3436 return 1;
3437
3438 int error = glslang_scan(count, string, length, context);
3439 if (!error)
3440 error = glslang_parse(context);
3441
3442 glslang_finalize(context);
3443
3444 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
3445}
3446
3447
3448