blob: eb41738cc733a92c25ebdea7c18df0ceb9073bf2 [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());
Nicolas Capens5d961882016-01-01 23:18:14 -0500512 } else if(type->getArraySize() != (int)function.getParamCount()) {
Alexis Hetue5246692015-06-18 12:34:52 -0400513 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
Nicolas Capens5d961882016-01-01 23:18:14 -0500535 if (op == EOpConstructStruct && !type->isArray() && 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
Alexis Hetuad527752015-07-07 13:31:44 -04001056void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1057{
1058 switch(qualifier)
1059 {
1060 case EvqVaryingOut:
1061 case EvqSmoothOut:
1062 case EvqFlatOut:
1063 case EvqCentroidOut:
1064 case EvqVertexOut:
1065 case EvqFragmentOut:
1066 break;
1067 default:
1068 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1069 recover();
1070 break;
1071 }
1072}
1073
John Bauman66b8ab22014-05-06 15:57:45 -04001074bool TParseContext::supportsExtension(const char* extension)
1075{
1076 const TExtensionBehavior& extbehavior = extensionBehavior();
1077 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1078 return (iter != extbehavior.end());
1079}
1080
Alexis Hetufe1269e2015-06-16 12:43:32 -04001081void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001082{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001083 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001084 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001085}
1086
Alexis Hetufe1269e2015-06-16 12:43:32 -04001087void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value)
John Bauman66b8ab22014-05-06 15:57:45 -04001088{
Alexis Hetu253fdd12015-07-07 15:12:46 -04001089 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetu0a655842015-06-22 16:52:11 -04001090 mDirectiveHandler.handlePragma(loc, name, value);
John Bauman66b8ab22014-05-06 15:57:45 -04001091}
1092
1093/////////////////////////////////////////////////////////////////////////////////
1094//
1095// Non-Errors.
1096//
1097/////////////////////////////////////////////////////////////////////////////////
1098
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001099const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1100 const TString *name,
1101 const TSymbol *symbol)
1102{
1103 const TVariable *variable = NULL;
1104
1105 if(!symbol)
1106 {
1107 error(location, "undeclared identifier", name->c_str());
1108 recover();
1109 }
1110 else if(!symbol->isVariable())
1111 {
1112 error(location, "variable expected", name->c_str());
1113 recover();
1114 }
1115 else
1116 {
1117 variable = static_cast<const TVariable*>(symbol);
1118
Alexis Hetu0a655842015-06-22 16:52:11 -04001119 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001120 {
1121 recover();
1122 }
1123
1124 // Reject shaders using both gl_FragData and gl_FragColor
1125 TQualifier qualifier = variable->getType().getQualifier();
1126 if(qualifier == EvqFragData)
1127 {
1128 mUsesFragData = true;
1129 }
1130 else if(qualifier == EvqFragColor)
1131 {
1132 mUsesFragColor = true;
1133 }
1134
1135 // This validation is not quite correct - it's only an error to write to
1136 // both FragData and FragColor. For simplicity, and because users shouldn't
1137 // be rewarded for reading from undefined varaibles, return an error
1138 // if they are both referenced, rather than assigned.
1139 if(mUsesFragData && mUsesFragColor)
1140 {
1141 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1142 recover();
1143 }
1144 }
1145
1146 if(!variable)
1147 {
1148 TType type(EbtFloat, EbpUndefined);
1149 TVariable *fakeVariable = new TVariable(name, type);
1150 symbolTable.declare(*fakeVariable);
1151 variable = fakeVariable;
1152 }
1153
1154 return variable;
1155}
1156
John Bauman66b8ab22014-05-06 15:57:45 -04001157//
1158// Look up a function name in the symbol table, and make sure it is a function.
1159//
1160// Return the function symbol if found, otherwise 0.
1161//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001162const TFunction* TParseContext::findFunction(const TSourceLoc &line, TFunction* call, bool *builtIn)
John Bauman66b8ab22014-05-06 15:57:45 -04001163{
1164 // First find by unmangled name to check whether the function name has been
1165 // hidden by a variable name or struct typename.
Alexis Hetu0a655842015-06-22 16:52:11 -04001166 const TSymbol* symbol = symbolTable.find(call->getName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001167 if (symbol == 0) {
Alexis Hetu0a655842015-06-22 16:52:11 -04001168 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -04001169 }
1170
1171 if (symbol == 0) {
1172 error(line, "no matching overloaded function found", call->getName().c_str());
1173 return 0;
1174 }
1175
1176 if (!symbol->isFunction()) {
1177 error(line, "function name expected", call->getName().c_str());
1178 return 0;
1179 }
1180
1181 return static_cast<const TFunction*>(symbol);
1182}
1183
1184//
1185// Initializers show up in several places in the grammar. Have one set of
1186// code to handle them here.
1187//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001188bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, const TPublicType& pType,
Alexis Hetue5246692015-06-18 12:34:52 -04001189 TIntermTyped *initializer, TIntermNode **intermNode)
John Bauman66b8ab22014-05-06 15:57:45 -04001190{
Alexis Hetue5246692015-06-18 12:34:52 -04001191 ASSERT(intermNode != nullptr);
1192 TType type = TType(pType);
John Bauman66b8ab22014-05-06 15:57:45 -04001193
Alexis Hetue5246692015-06-18 12:34:52 -04001194 TVariable *variable = nullptr;
1195 if(type.isArray() && (type.getArraySize() == 0))
1196 {
1197 type.setArraySize(initializer->getArraySize());
1198 }
1199 if(!declareVariable(line, identifier, type, &variable))
1200 {
1201 return true;
1202 }
John Bauman66b8ab22014-05-06 15:57:45 -04001203
Alexis Hetue5246692015-06-18 12:34:52 -04001204 bool globalInitWarning = false;
1205 if(symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1206 {
1207 // Error message does not completely match behavior with ESSL 1.00, but
1208 // we want to steer developers towards only using constant expressions.
1209 error(line, "global variable initializers must be constant expressions", "=");
1210 return true;
1211 }
1212 if(globalInitWarning)
1213 {
1214 warning(line, "global variable initializers should be constant expressions "
1215 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
John Bauman66b8ab22014-05-06 15:57:45 -04001216 }
1217
1218 //
1219 // identifier must be of type constant, a global, or a temporary
1220 //
1221 TQualifier qualifier = variable->getType().getQualifier();
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001222 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001223 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1224 return true;
1225 }
1226 //
1227 // test for and propagate constant
1228 //
1229
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001230 if (qualifier == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001231 if (qualifier != initializer->getType().getQualifier()) {
1232 std::stringstream extraInfoStream;
1233 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1234 std::string extraInfo = extraInfoStream.str();
1235 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1236 variable->getType().setQualifier(EvqTemporary);
1237 return true;
1238 }
1239 if (type != initializer->getType()) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001240 error(line, " non-matching types for const initializer ",
John Bauman66b8ab22014-05-06 15:57:45 -04001241 variable->getType().getQualifierString());
1242 variable->getType().setQualifier(EvqTemporary);
1243 return true;
1244 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001245 if (initializer->getAsConstantUnion()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001246 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
John Bauman66b8ab22014-05-06 15:57:45 -04001247 } else if (initializer->getAsSymbolNode()) {
Alexis Hetue5246692015-06-18 12:34:52 -04001248 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001249 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1250
1251 ConstantUnion* constArray = tVar->getConstPointer();
1252 variable->shareConstPointer(constArray);
1253 } else {
1254 std::stringstream extraInfoStream;
1255 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1256 std::string extraInfo = extraInfoStream.str();
1257 error(line, " cannot assign to", "=", extraInfo.c_str());
1258 variable->getType().setQualifier(EvqTemporary);
1259 return true;
1260 }
1261 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001262
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001263 if (qualifier != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001264 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
Alexis Hetue5246692015-06-18 12:34:52 -04001265 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1266 if(*intermNode == nullptr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001267 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1268 return true;
1269 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001270 } else
Alexis Hetue5246692015-06-18 12:34:52 -04001271 *intermNode = nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04001272
1273 return false;
1274}
1275
1276bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1277{
1278 ASSERT(aggrNode != NULL);
1279 if (!aggrNode->isConstructor())
1280 return false;
1281
1282 bool allConstant = true;
1283
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001284 // check if all the child nodes are constants so that they can be inserted into
John Bauman66b8ab22014-05-06 15:57:45 -04001285 // the parent node
1286 TIntermSequence &sequence = aggrNode->getSequence() ;
1287 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1288 if (!(*p)->getAsTyped()->getAsConstantUnion())
1289 return false;
1290 }
1291
1292 return allConstant;
1293}
1294
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001295TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1296{
1297 TPublicType returnType = typeSpecifier;
1298 returnType.qualifier = qualifier;
1299 returnType.invariant = invariant;
1300 returnType.layoutQualifier = layoutQualifier;
1301
1302 if(typeSpecifier.array)
1303 {
1304 error(typeSpecifier.line, "not supported", "first-class array");
1305 recover();
1306 returnType.clearArrayness();
1307 }
1308
Alexis Hetu0a655842015-06-22 16:52:11 -04001309 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001310 {
1311 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1312 {
1313 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1314 recover();
1315 }
1316
1317 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1318 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1319 {
1320 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1321 recover();
1322 }
1323 }
1324 else
1325 {
1326 switch(qualifier)
1327 {
1328 case EvqSmoothIn:
1329 case EvqSmoothOut:
1330 case EvqVertexOut:
1331 case EvqFragmentIn:
1332 case EvqCentroidOut:
1333 case EvqCentroidIn:
1334 if(typeSpecifier.type == EbtBool)
1335 {
1336 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1337 recover();
1338 }
1339 if(typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1340 {
1341 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1342 recover();
1343 }
1344 break;
1345
1346 case EvqVertexIn:
1347 case EvqFragmentOut:
1348 case EvqFlatIn:
1349 case EvqFlatOut:
1350 if(typeSpecifier.type == EbtBool)
1351 {
1352 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1353 recover();
1354 }
1355 break;
1356
1357 default: break;
1358 }
1359 }
1360
1361 return returnType;
1362}
1363
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001364TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1365 const TSourceLoc &identifierOrTypeLocation,
1366 const TString &identifier)
1367{
1368 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1369
1370 bool emptyDeclaration = (identifier == "");
1371
1372 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1373
1374 if(emptyDeclaration)
1375 {
1376 if(publicType.isUnsizedArray())
1377 {
1378 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1379 // It is assumed that this applies to empty declarations as well.
1380 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1381 }
1382 }
1383 else
1384 {
1385 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1386 recover();
1387
1388 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1389 recover();
1390
1391 TVariable *variable = nullptr;
1392 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1393 recover();
1394
1395 if(variable && symbol)
1396 symbol->setId(variable->getUniqueId());
1397 }
1398
1399 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1400}
1401
1402TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1403 const TSourceLoc &identifierLocation,
1404 const TString &identifier,
1405 const TSourceLoc &indexLocation,
1406 TIntermTyped *indexExpression)
1407{
1408 mDeferredSingleDeclarationErrorCheck = false;
1409
1410 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1411 recover();
1412
1413 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1414 recover();
1415
1416 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1417 {
1418 recover();
1419 }
1420
1421 TType arrayType(publicType);
1422
1423 int size;
1424 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1425 {
1426 recover();
1427 }
1428 // Make the type an array even if size check failed.
1429 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1430 arrayType.setArraySize(size);
1431
1432 TVariable *variable = nullptr;
1433 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1434 recover();
1435
1436 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1437 if(variable && symbol)
1438 symbol->setId(variable->getUniqueId());
1439
1440 return intermediate.makeAggregate(symbol, identifierLocation);
1441}
1442
1443TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1444 const TSourceLoc &identifierLocation,
1445 const TString &identifier,
1446 const TSourceLoc &initLocation,
1447 TIntermTyped *initializer)
1448{
1449 mDeferredSingleDeclarationErrorCheck = false;
1450
1451 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1452 recover();
1453
1454 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001455 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001456 {
1457 //
1458 // Build intermediate representation
1459 //
1460 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1461 }
1462 else
1463 {
1464 recover();
1465 return nullptr;
1466 }
1467}
1468
1469TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1470 const TSourceLoc &identifierLocation,
1471 const TString &identifier,
1472 const TSourceLoc &indexLocation,
1473 TIntermTyped *indexExpression,
1474 const TSourceLoc &initLocation,
1475 TIntermTyped *initializer)
1476{
1477 mDeferredSingleDeclarationErrorCheck = false;
1478
1479 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1480 recover();
1481
1482 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1483 {
1484 recover();
1485 }
1486
1487 TPublicType arrayType(publicType);
1488
1489 int size = 0;
1490 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1491 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1492 {
1493 recover();
1494 }
1495 // Make the type an array even if size check failed.
1496 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1497 arrayType.setArray(true, size);
1498
1499 // initNode will correspond to the whole of "type b[n] = initializer".
1500 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001501 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001502 {
1503 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1504 }
1505 else
1506 {
1507 recover();
1508 return nullptr;
1509 }
1510}
1511
1512TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1513 const TSourceLoc &identifierLoc,
1514 const TString *identifier,
1515 const TSymbol *symbol)
1516{
1517 // invariant declaration
1518 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1519 {
1520 recover();
1521 }
1522
1523 if(!symbol)
1524 {
1525 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1526 recover();
1527 return nullptr;
1528 }
1529 else
1530 {
1531 const TString kGlFrontFacing("gl_FrontFacing");
1532 if(*identifier == kGlFrontFacing)
1533 {
1534 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1535 recover();
1536 return nullptr;
1537 }
1538 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1539 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1540 ASSERT(variable);
1541 const TType &type = variable->getType();
1542 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1543 *identifier, type, identifierLoc);
1544
1545 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1546 aggregate->setOp(EOpInvariantDeclaration);
1547 return aggregate;
1548 }
1549}
1550
1551TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1552 const TSourceLoc &identifierLocation, const TString &identifier)
1553{
1554 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1555 if(mDeferredSingleDeclarationErrorCheck)
1556 {
1557 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1558 recover();
1559 mDeferredSingleDeclarationErrorCheck = false;
1560 }
1561
1562 if(locationDeclaratorListCheck(identifierLocation, publicType))
1563 recover();
1564
1565 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1566 recover();
1567
1568 TVariable *variable = nullptr;
1569 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1570 recover();
1571
1572 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1573 if(variable && symbol)
1574 symbol->setId(variable->getUniqueId());
1575
1576 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1577}
1578
1579TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1580 const TSourceLoc &identifierLocation, const TString &identifier,
1581 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1582{
1583 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1584 if(mDeferredSingleDeclarationErrorCheck)
1585 {
1586 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1587 recover();
1588 mDeferredSingleDeclarationErrorCheck = false;
1589 }
1590
1591 if(locationDeclaratorListCheck(identifierLocation, publicType))
1592 recover();
1593
1594 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1595 recover();
1596
1597 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1598 {
1599 recover();
1600 }
1601 else
1602 {
1603 TType arrayType = TType(publicType);
1604 int size;
1605 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1606 {
1607 recover();
1608 }
1609 arrayType.setArraySize(size);
1610
1611 TVariable *variable = nullptr;
1612 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1613 recover();
1614
1615 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1616 if(variable && symbol)
1617 symbol->setId(variable->getUniqueId());
1618
1619 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1620 }
1621
1622 return nullptr;
1623}
1624
1625TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1626 const TSourceLoc &identifierLocation, const TString &identifier,
1627 const TSourceLoc &initLocation, TIntermTyped *initializer)
1628{
1629 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1630 if(mDeferredSingleDeclarationErrorCheck)
1631 {
1632 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1633 recover();
1634 mDeferredSingleDeclarationErrorCheck = false;
1635 }
1636
1637 if(locationDeclaratorListCheck(identifierLocation, publicType))
1638 recover();
1639
1640 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001641 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001642 {
1643 //
1644 // build the intermediate representation
1645 //
1646 if(intermNode)
1647 {
1648 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1649 }
1650 else
1651 {
1652 return aggregateDeclaration;
1653 }
1654 }
1655 else
1656 {
1657 recover();
1658 return nullptr;
1659 }
1660}
1661
1662TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1663 TIntermAggregate *aggregateDeclaration,
1664 const TSourceLoc &identifierLocation,
1665 const TString &identifier,
1666 const TSourceLoc &indexLocation,
1667 TIntermTyped *indexExpression,
1668 const TSourceLoc &initLocation, TIntermTyped *initializer)
1669{
1670 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1671 if(mDeferredSingleDeclarationErrorCheck)
1672 {
1673 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1674 recover();
1675 mDeferredSingleDeclarationErrorCheck = false;
1676 }
1677
1678 if(locationDeclaratorListCheck(identifierLocation, publicType))
1679 recover();
1680
1681 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1682 {
1683 recover();
1684 }
1685
1686 TPublicType arrayType(publicType);
1687
1688 int size = 0;
1689 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1690 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1691 {
1692 recover();
1693 }
1694 // Make the type an array even if size check failed.
1695 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1696 arrayType.setArray(true, size);
1697
1698 // initNode will correspond to the whole of "b[n] = initializer".
1699 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001700 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001701 {
1702 if(initNode)
1703 {
1704 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1705 }
1706 else
1707 {
1708 return aggregateDeclaration;
1709 }
1710 }
1711 else
1712 {
1713 recover();
1714 return nullptr;
1715 }
1716}
1717
Alexis Hetua35d8232015-06-11 17:11:06 -04001718void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1719{
Alexis Hetu0a655842015-06-22 16:52:11 -04001720 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001721 {
1722 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1723 recover();
1724 return;
1725 }
1726
1727 if(typeQualifier.qualifier != EvqUniform)
1728 {
1729 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1730 recover();
1731 return;
1732 }
1733
1734 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1735 ASSERT(!layoutQualifier.isEmpty());
1736
1737 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1738 {
1739 recover();
1740 return;
1741 }
1742
1743 if(layoutQualifier.matrixPacking != EmpUnspecified)
1744 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001745 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001746 }
1747
1748 if(layoutQualifier.blockStorage != EbsUnspecified)
1749 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001750 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001751 }
1752}
1753
Alexis Hetu407813b2016-02-24 16:46:13 -05001754TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1755{
1756 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1757 // Either way the instance in the symbol table is used to track whether the function is declared
1758 // multiple times.
1759 TFunction *symbolTableFunction =
1760 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1761 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1762 {
1763 // ESSL 1.00.17 section 4.2.7.
1764 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1765 error(location, "duplicate function prototype declarations are not allowed", "function");
1766 recover();
1767 }
1768 symbolTableFunction->setHasPrototypeDeclaration();
1769
1770 TIntermAggregate *prototype = new TIntermAggregate;
1771 prototype->setType(function.getReturnType());
1772 prototype->setName(function.getMangledName());
1773
1774 for(size_t i = 0; i < function.getParamCount(); i++)
1775 {
1776 const TParameter &param = function.getParam(i);
1777 if(param.name != 0)
1778 {
1779 TVariable variable(param.name, *param.type);
1780
1781 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1782 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1783 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1784 }
1785 else
1786 {
1787 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1788 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1789 }
1790 }
1791
1792 prototype->setOp(EOpPrototype);
1793
1794 symbolTable.pop();
1795
1796 if(!symbolTable.atGlobalLevel())
1797 {
1798 // ESSL 3.00.4 section 4.2.4.
1799 error(location, "local function prototype declarations are not allowed", "function");
1800 recover();
1801 }
1802
1803 return prototype;
1804}
1805
1806TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1807{
1808 //?? Check that all paths return a value if return type != void ?
1809 // May be best done as post process phase on intermediate code
1810 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1811 {
1812 error(location, "function does not return a value:", "", function.getName().c_str());
1813 recover();
1814 }
1815
1816 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1817 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1818 aggregate->setName(function.getMangledName().c_str());
1819 aggregate->setType(function.getReturnType());
1820
1821 // store the pragma information for debug and optimize and other vendor specific
1822 // information. This information can be queried from the parse tree
1823 aggregate->setOptimize(pragma().optimize);
1824 aggregate->setDebug(pragma().debug);
1825
1826 if(functionBody && functionBody->getAsAggregate())
1827 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1828
1829 symbolTable.pop();
1830 return aggregate;
1831}
1832
1833void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1834{
1835 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1836
1837 if(builtIn)
1838 {
1839 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1840 recover();
1841 }
1842
1843 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1844 //
1845 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1846 // as it would have just been put in the symbol table. Otherwise, we're looking up
1847 // an earlier occurance.
1848 //
1849 if(prevDec->isDefined())
1850 {
1851 // Then this function already has a body.
1852 error(location, "function already has a body", function->getName().c_str());
1853 recover();
1854 }
1855 prevDec->setDefined();
1856 //
1857 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1858 // Eventually we will probably want to have only a single definition and just swap the
1859 // arguments to be the definition's arguments.
1860 //
1861 function->setUniqueId(prevDec->getUniqueId());
1862
1863 // Raise error message if main function takes any parameters or return anything other than void
1864 if(function->getName() == "main")
1865 {
1866 if(function->getParamCount() > 0)
1867 {
1868 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1869 recover();
1870 }
1871 if(function->getReturnType().getBasicType() != EbtVoid)
1872 {
1873 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1874 recover();
1875 }
1876 }
1877
1878 //
1879 // Remember the return type for later checking for RETURN statements.
1880 //
1881 mCurrentFunctionType = &(prevDec->getReturnType());
1882 mFunctionReturnsValue = false;
1883
1884 //
1885 // Insert parameters into the symbol table.
1886 // If the parameter has no name, it's not an error, just don't insert it
1887 // (could be used for unused args).
1888 //
1889 // Also, accumulate the list of parameters into the HIL, so lower level code
1890 // knows where to find parameters.
1891 //
1892 TIntermAggregate *paramNodes = new TIntermAggregate;
1893 for(size_t i = 0; i < function->getParamCount(); i++)
1894 {
1895 const TParameter &param = function->getParam(i);
1896 if(param.name != 0)
1897 {
1898 TVariable *variable = new TVariable(param.name, *param.type);
1899 //
1900 // Insert the parameters with name in the symbol table.
1901 //
1902 if(!symbolTable.declare(*variable))
1903 {
1904 error(location, "redefinition", variable->getName().c_str());
1905 recover();
1906 paramNodes = intermediate.growAggregate(
1907 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1908 continue;
1909 }
1910
1911 //
1912 // Add the parameter to the HIL
1913 //
1914 TIntermSymbol *symbol = intermediate.addSymbol(
1915 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1916
1917 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1918 }
1919 else
1920 {
1921 paramNodes = intermediate.growAggregate(
1922 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1923 }
1924 }
1925 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1926 *aggregateOut = paramNodes;
1927 setLoopNestingLevel(0);
1928}
1929
1930TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1931{
1932 //
1933 // We don't know at this point whether this is a function definition or a prototype.
1934 // The definition production code will check for redefinitions.
1935 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
1936 //
1937 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
1938 // here.
1939 //
1940 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1941 if(prevDec)
1942 {
1943 if(prevDec->getReturnType() != function->getReturnType())
1944 {
1945 error(location, "overloaded functions must have the same return type",
1946 function->getReturnType().getBasicString());
1947 recover();
1948 }
1949 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
1950 {
1951 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
1952 {
1953 error(location, "overloaded functions must have the same parameter qualifiers",
1954 function->getParam(i).type->getQualifierString());
1955 recover();
1956 }
1957 }
1958 }
1959
1960 //
1961 // Check for previously declared variables using the same name.
1962 //
1963 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
1964 if(prevSym)
1965 {
1966 if(!prevSym->isFunction())
1967 {
1968 error(location, "redefinition", function->getName().c_str(), "function");
1969 recover();
1970 }
1971 }
1972
1973 // We're at the inner scope level of the function's arguments and body statement.
1974 // Add the function prototype to the surrounding scope instead.
1975 symbolTable.getOuterLevel()->insert(*function);
1976
1977 //
1978 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
1979 // variable names from this one, and not the one that's
1980 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
1981 //
1982 return function;
1983}
1984
Alexis Hetue5246692015-06-18 12:34:52 -04001985TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
1986{
1987 TPublicType publicType = publicTypeIn;
1988 TOperator op = EOpNull;
1989 if(publicType.userDef)
1990 {
1991 op = EOpConstructStruct;
1992 }
1993 else
1994 {
1995 switch(publicType.type)
1996 {
Nicolas Capens5d961882016-01-01 23:18:14 -05001997 case EbtFloat:
1998 if(publicType.isMatrix())
1999 {
2000 switch(publicType.getCols())
2001 {
2002 case 2:
2003 switch(publicType.getRows())
2004 {
2005 case 2: op = EOpConstructMat2; break;
2006 case 3: op = EOpConstructMat2x3; break;
2007 case 4: op = EOpConstructMat2x4; break;
2008 }
2009 break;
2010 case 3:
2011 switch(publicType.getRows())
2012 {
2013 case 2: op = EOpConstructMat3x2; break;
2014 case 3: op = EOpConstructMat3; break;
2015 case 4: op = EOpConstructMat3x4; break;
2016 }
2017 break;
2018 case 4:
2019 switch(publicType.getRows())
2020 {
2021 case 2: op = EOpConstructMat4x2; break;
2022 case 3: op = EOpConstructMat4x3; break;
2023 case 4: op = EOpConstructMat4; break;
2024 }
2025 break;
2026 }
2027 }
2028 else
2029 {
2030 switch(publicType.getNominalSize())
2031 {
2032 case 1: op = EOpConstructFloat; break;
2033 case 2: op = EOpConstructVec2; break;
2034 case 3: op = EOpConstructVec3; break;
2035 case 4: op = EOpConstructVec4; break;
2036 }
2037 }
Alexis Hetue5246692015-06-18 12:34:52 -04002038 break;
2039
2040 case EbtInt:
2041 switch(publicType.getNominalSize())
2042 {
2043 case 1: op = EOpConstructInt; break;
2044 case 2: op = EOpConstructIVec2; break;
2045 case 3: op = EOpConstructIVec3; break;
2046 case 4: op = EOpConstructIVec4; break;
2047 }
2048 break;
2049
2050 case EbtUInt:
2051 switch(publicType.getNominalSize())
2052 {
2053 case 1: op = EOpConstructUInt; break;
2054 case 2: op = EOpConstructUVec2; break;
2055 case 3: op = EOpConstructUVec3; break;
2056 case 4: op = EOpConstructUVec4; break;
2057 }
2058 break;
2059
2060 case EbtBool:
2061 switch(publicType.getNominalSize())
2062 {
2063 case 1: op = EOpConstructBool; break;
2064 case 2: op = EOpConstructBVec2; break;
2065 case 3: op = EOpConstructBVec3; break;
2066 case 4: op = EOpConstructBVec4; break;
2067 }
2068 break;
2069
2070 default: break;
2071 }
2072
2073 if(op == EOpNull)
2074 {
2075 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2076 recover();
2077 publicType.type = EbtFloat;
2078 op = EOpConstructFloat;
2079 }
2080 }
2081
2082 TString tempString;
2083 TType type(publicType);
2084 return new TFunction(&tempString, type, op);
2085}
2086
John Bauman66b8ab22014-05-06 15:57:45 -04002087// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002088// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002089//
2090// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2091//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002092TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002093{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002094 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002095
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002096 if(!aggregateArguments)
2097 {
2098 aggregateArguments = new TIntermAggregate;
2099 aggregateArguments->getSequence().push_back(arguments);
John Bauman66b8ab22014-05-06 15:57:45 -04002100 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002101
2102 if(op == EOpConstructStruct)
2103 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002104 const TFieldList &fields = type->getStruct()->fields();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002105 TIntermSequence &args = aggregateArguments->getSequence();
2106
2107 for(size_t i = 0; i < fields.size(); i++)
2108 {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002109 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002110 {
2111 error(line, "Structure constructor arguments do not match structure fields", "Error");
2112 recover();
2113
2114 return 0;
2115 }
John Bauman66b8ab22014-05-06 15:57:45 -04002116 }
2117 }
2118
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002119 // Turn the argument list itself into a constructor
2120 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2121 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
2122 if(constConstructor)
2123 {
John Bauman66b8ab22014-05-06 15:57:45 -04002124 return constConstructor;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002125 }
John Bauman66b8ab22014-05-06 15:57:45 -04002126
2127 return constructor;
2128}
2129
2130TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2131{
2132 bool canBeFolded = areAllChildConst(aggrNode);
2133 aggrNode->setType(type);
2134 if (canBeFolded) {
2135 bool returnVal = false;
2136 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2137 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04002138 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04002139 }
2140 else {
John Baumand4ae8632014-05-06 16:18:33 -04002141 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04002142 }
2143 if (returnVal)
2144 return 0;
2145
2146 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2147 }
2148
2149 return 0;
2150}
2151
John Bauman66b8ab22014-05-06 15:57:45 -04002152//
2153// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2154// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2155// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
2156// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
2157// a constant matrix.
2158//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002159TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002160{
2161 TIntermTyped* typedNode;
2162 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2163
2164 ConstantUnion *unionArray;
2165 if (tempConstantNode) {
2166 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002167
2168 if (!unionArray) {
2169 return node;
2170 }
2171 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2172 error(line, "Cannot offset into the vector", "Error");
2173 recover();
2174
2175 return 0;
2176 }
2177
2178 ConstantUnion* constArray = new ConstantUnion[fields.num];
2179
2180 for (int i = 0; i < fields.num; i++) {
2181 if (fields.offsets[i] >= node->getType().getObjectSize()) {
2182 std::stringstream extraInfoStream;
2183 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2184 std::string extraInfo = extraInfoStream.str();
2185 error(line, "", "[", extraInfo.c_str());
2186 recover();
2187 fields.offsets[i] = 0;
2188 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002189
John Bauman66b8ab22014-05-06 15:57:45 -04002190 constArray[i] = unionArray[fields.offsets[i]];
2191
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002192 }
John Bauman66b8ab22014-05-06 15:57:45 -04002193 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2194 return typedNode;
2195}
2196
2197//
2198// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002199// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2200// 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 -04002201// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2202//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002203TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002204{
2205 TIntermTyped* typedNode;
2206 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2207
2208 if (index >= node->getType().getNominalSize()) {
2209 std::stringstream extraInfoStream;
2210 extraInfoStream << "matrix field selection out of range '" << index << "'";
2211 std::string extraInfo = extraInfoStream.str();
2212 error(line, "", "[", extraInfo.c_str());
2213 recover();
2214 index = 0;
2215 }
2216
2217 if (tempConstantNode) {
2218 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2219 int size = tempConstantNode->getType().getNominalSize();
2220 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2221 } else {
2222 error(line, "Cannot offset into the matrix", "Error");
2223 recover();
2224
2225 return 0;
2226 }
2227
2228 return typedNode;
2229}
2230
2231
2232//
2233// 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 -04002234// the symbol table and parse-tree is built for the type of the element. The input
2235// 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 -04002236// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2237//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002238TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002239{
2240 TIntermTyped* typedNode;
2241 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2242 TType arrayElementType = node->getType();
2243 arrayElementType.clearArrayness();
2244
2245 if (index >= node->getType().getArraySize()) {
2246 std::stringstream extraInfoStream;
2247 extraInfoStream << "array field selection out of range '" << index << "'";
2248 std::string extraInfo = extraInfoStream.str();
2249 error(line, "", "[", extraInfo.c_str());
2250 recover();
2251 index = 0;
2252 }
2253
2254 int arrayElementSize = arrayElementType.getObjectSize();
2255
2256 if (tempConstantNode) {
2257 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2258 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2259 } else {
2260 error(line, "Cannot offset into the array", "Error");
2261 recover();
2262
2263 return 0;
2264 }
2265
2266 return typedNode;
2267}
2268
2269
2270//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002271// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002272// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2273// function and returns the parse-tree with the values of the embedded/nested struct.
2274//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002275TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002276{
Alexis Hetua8b364b2015-06-10 11:48:40 -04002277 const TFieldList &fields = node->getType().getStruct()->fields();
John Bauman66b8ab22014-05-06 15:57:45 -04002278 TIntermTyped *typedNode;
2279 int instanceSize = 0;
2280 unsigned int index = 0;
2281 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
2282
Alexis Hetua8b364b2015-06-10 11:48:40 -04002283 for ( index = 0; index < fields.size(); ++index) {
2284 if (fields[index]->name() == identifier) {
John Bauman66b8ab22014-05-06 15:57:45 -04002285 break;
2286 } else {
Alexis Hetua8b364b2015-06-10 11:48:40 -04002287 instanceSize += fields[index]->type()->getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002288 }
2289 }
2290
2291 if (tempConstantNode) {
2292 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
2293
2294 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2295 } else {
2296 error(line, "Cannot offset into the structure", "Error");
2297 recover();
2298
2299 return 0;
2300 }
2301
2302 return typedNode;
2303}
2304
Alexis Hetuad6b8752015-06-09 16:15:30 -04002305//
Alexis Hetua35d8232015-06-11 17:11:06 -04002306// Interface/uniform blocks
2307//
2308TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
2309 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
2310{
2311 if(reservedErrorCheck(nameLine, blockName))
2312 recover();
2313
2314 if(typeQualifier.qualifier != EvqUniform)
2315 {
2316 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2317 recover();
2318 }
2319
2320 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2321 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2322 {
2323 recover();
2324 }
2325
2326 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2327 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002328 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002329 }
2330
2331 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2332 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002333 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002334 }
2335
2336 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2337 if(!symbolTable.declare(*blockNameSymbol)) {
2338 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2339 recover();
2340 }
2341
2342 // check for sampler types and apply layout qualifiers
2343 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2344 TField* field = (*fieldList)[memberIndex];
2345 TType* fieldType = field->type();
2346 if(IsSampler(fieldType->getBasicType())) {
2347 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2348 recover();
2349 }
2350
2351 const TQualifier qualifier = fieldType->getQualifier();
2352 switch(qualifier)
2353 {
2354 case EvqGlobal:
2355 case EvqUniform:
2356 break;
2357 default:
2358 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2359 recover();
2360 break;
2361 }
2362
2363 // check layout qualifiers
2364 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2365 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2366 {
2367 recover();
2368 }
2369
2370 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2371 {
2372 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2373 recover();
2374 }
2375
2376 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2377 {
2378 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2379 }
2380 else if(!fieldType->isMatrix())
2381 {
2382 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
2383 recover();
2384 }
2385
2386 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2387 }
2388
2389 // add array index
2390 int arraySize = 0;
2391 if(arrayIndex != NULL)
2392 {
2393 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2394 recover();
2395 }
2396
2397 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2398 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2399
2400 TString symbolName = "";
2401 int symbolId = 0;
2402
2403 if(!instanceName)
2404 {
2405 // define symbols for the members of the interface block
2406 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2407 {
2408 TField* field = (*fieldList)[memberIndex];
2409 TType* fieldType = field->type();
2410
2411 // set parent pointer of the field variable
2412 fieldType->setInterfaceBlock(interfaceBlock);
2413
2414 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2415 fieldVariable->setQualifier(typeQualifier.qualifier);
2416
2417 if(!symbolTable.declare(*fieldVariable)) {
2418 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2419 recover();
2420 }
2421 }
2422 }
2423 else
2424 {
2425 // add a symbol for this interface block
2426 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2427 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2428
2429 if(!symbolTable.declare(*instanceTypeDef)) {
2430 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2431 recover();
2432 }
2433
2434 symbolId = instanceTypeDef->getUniqueId();
2435 symbolName = instanceTypeDef->getName();
2436 }
2437
2438 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2439 aggregate->setOp(EOpDeclaration);
2440
2441 exitStructDeclaration();
2442 return aggregate;
2443}
2444
2445//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002446// Parse an array index expression
2447//
2448TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2449{
2450 TIntermTyped *indexedExpression = NULL;
2451
2452 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2453 {
2454 if(baseExpression->getAsSymbolNode())
2455 {
2456 error(location, " left of '[' is not of type array, matrix, or vector ",
2457 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2458 }
2459 else
2460 {
2461 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2462 }
2463 recover();
2464 }
2465
2466 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2467
2468 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2469 {
2470 int index = indexConstantUnion->getIConst(0);
2471 if(index < 0)
2472 {
2473 std::stringstream infoStream;
2474 infoStream << index;
2475 std::string info = infoStream.str();
2476 error(location, "negative index", info.c_str());
2477 recover();
2478 index = 0;
2479 }
2480 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2481 {
2482 if(baseExpression->isArray())
2483 {
2484 // constant folding for arrays
2485 indexedExpression = addConstArrayNode(index, baseExpression, location);
2486 }
2487 else if(baseExpression->isVector())
2488 {
2489 // constant folding for vectors
2490 TVectorFields fields;
2491 fields.num = 1;
2492 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2493 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2494 }
2495 else if(baseExpression->isMatrix())
2496 {
2497 // constant folding for matrices
2498 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2499 }
2500 }
2501 else
2502 {
2503 int safeIndex = -1;
2504
2505 if(baseExpression->isArray())
2506 {
2507 if(index >= baseExpression->getType().getArraySize())
2508 {
2509 std::stringstream extraInfoStream;
2510 extraInfoStream << "array index out of range '" << index << "'";
2511 std::string extraInfo = extraInfoStream.str();
2512 error(location, "", "[", extraInfo.c_str());
2513 recover();
2514 safeIndex = baseExpression->getType().getArraySize() - 1;
2515 }
2516 }
2517 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2518 baseExpression->getType().getNominalSize() <= index)
2519 {
2520 std::stringstream extraInfoStream;
2521 extraInfoStream << "field selection out of range '" << index << "'";
2522 std::string extraInfo = extraInfoStream.str();
2523 error(location, "", "[", extraInfo.c_str());
2524 recover();
2525 safeIndex = baseExpression->getType().getNominalSize() - 1;
2526 }
2527
2528 // Don't modify the data of the previous constant union, because it can point
2529 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2530 if(safeIndex != -1)
2531 {
2532 ConstantUnion *safeConstantUnion = new ConstantUnion();
2533 safeConstantUnion->setIConst(safeIndex);
2534 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2535 }
2536
2537 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2538 }
2539 }
2540 else
2541 {
2542 if(baseExpression->isInterfaceBlock())
2543 {
2544 error(location, "",
2545 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2546 recover();
2547 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002548 else if(baseExpression->getQualifier() == EvqFragmentOut)
2549 {
2550 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2551 recover();
2552 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002553
2554 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2555 }
2556
2557 if(indexedExpression == 0)
2558 {
2559 ConstantUnion *unionArray = new ConstantUnion[1];
2560 unionArray->setFConst(0.0f);
2561 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2562 }
2563 else if(baseExpression->isArray())
2564 {
2565 const TType &baseType = baseExpression->getType();
2566 if(baseType.getStruct())
2567 {
2568 TType copyOfType(baseType.getStruct());
2569 indexedExpression->setType(copyOfType);
2570 }
2571 else if(baseType.isInterfaceBlock())
2572 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002573 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002574 indexedExpression->setType(copyOfType);
2575 }
2576 else
2577 {
2578 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2579 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2580 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2581 }
2582
2583 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2584 {
2585 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2586 }
2587 }
2588 else if(baseExpression->isMatrix())
2589 {
2590 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2591 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2592 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2593 }
2594 else if(baseExpression->isVector())
2595 {
2596 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2597 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2598 }
2599 else
2600 {
2601 indexedExpression->setType(baseExpression->getType());
2602 }
2603
2604 return indexedExpression;
2605}
2606
2607TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2608 const TString &fieldString, const TSourceLoc &fieldLocation)
2609{
2610 TIntermTyped *indexedExpression = NULL;
2611
2612 if(baseExpression->isArray())
2613 {
2614 error(fieldLocation, "cannot apply dot operator to an array", ".");
2615 recover();
2616 }
2617
2618 if(baseExpression->isVector())
2619 {
2620 TVectorFields fields;
2621 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2622 {
2623 fields.num = 1;
2624 fields.offsets[0] = 0;
2625 recover();
2626 }
2627
2628 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2629 {
2630 // constant folding for vector fields
2631 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2632 if(indexedExpression == 0)
2633 {
2634 recover();
2635 indexedExpression = baseExpression;
2636 }
2637 else
2638 {
2639 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2640 EvqConstExpr, (unsigned char)(fieldString).size()));
2641 }
2642 }
2643 else
2644 {
2645 TString vectorString = fieldString;
2646 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2647 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2648 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2649 EvqTemporary, (unsigned char)vectorString.size()));
2650 }
2651 }
2652 else if(baseExpression->isMatrix())
2653 {
2654 TMatrixFields fields;
2655 if(!parseMatrixFields(fieldString, baseExpression->getNominalSize(), baseExpression->getSecondarySize(), fields, fieldLocation))
2656 {
2657 fields.wholeRow = false;
2658 fields.wholeCol = false;
2659 fields.row = 0;
2660 fields.col = 0;
2661 recover();
2662 }
2663
2664 if(fields.wholeRow || fields.wholeCol)
2665 {
2666 error(dotLocation, " non-scalar fields not implemented yet", ".");
2667 recover();
2668 ConstantUnion *unionArray = new ConstantUnion[1];
2669 unionArray->setIConst(0);
2670 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2671 fieldLocation);
2672 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2673 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2674 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2675 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2676 }
2677 else
2678 {
2679 ConstantUnion *unionArray = new ConstantUnion[1];
2680 unionArray->setIConst(fields.col * baseExpression->getSecondarySize() + fields.row);
2681 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr),
2682 fieldLocation);
2683 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2684 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2685 }
2686 }
2687 else if(baseExpression->getBasicType() == EbtStruct)
2688 {
2689 bool fieldFound = false;
2690 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2691 if(fields.empty())
2692 {
2693 error(dotLocation, "structure has no fields", "Internal Error");
2694 recover();
2695 indexedExpression = baseExpression;
2696 }
2697 else
2698 {
2699 unsigned int i;
2700 for(i = 0; i < fields.size(); ++i)
2701 {
2702 if(fields[i]->name() == fieldString)
2703 {
2704 fieldFound = true;
2705 break;
2706 }
2707 }
2708 if(fieldFound)
2709 {
2710 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2711 {
2712 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2713 if(indexedExpression == 0)
2714 {
2715 recover();
2716 indexedExpression = baseExpression;
2717 }
2718 else
2719 {
2720 indexedExpression->setType(*fields[i]->type());
2721 // change the qualifier of the return type, not of the structure field
2722 // as the structure definition is shared between various structures.
2723 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2724 }
2725 }
2726 else
2727 {
2728 ConstantUnion *unionArray = new ConstantUnion[1];
2729 unionArray->setIConst(i);
2730 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2731 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2732 indexedExpression->setType(*fields[i]->type());
2733 }
2734 }
2735 else
2736 {
2737 error(dotLocation, " no such field in structure", fieldString.c_str());
2738 recover();
2739 indexedExpression = baseExpression;
2740 }
2741 }
2742 }
2743 else if(baseExpression->isInterfaceBlock())
2744 {
2745 bool fieldFound = false;
2746 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2747 if(fields.empty())
2748 {
2749 error(dotLocation, "interface block has no fields", "Internal Error");
2750 recover();
2751 indexedExpression = baseExpression;
2752 }
2753 else
2754 {
2755 unsigned int i;
2756 for(i = 0; i < fields.size(); ++i)
2757 {
2758 if(fields[i]->name() == fieldString)
2759 {
2760 fieldFound = true;
2761 break;
2762 }
2763 }
2764 if(fieldFound)
2765 {
2766 ConstantUnion *unionArray = new ConstantUnion[1];
2767 unionArray->setIConst(i);
2768 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2769 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2770 dotLocation);
2771 indexedExpression->setType(*fields[i]->type());
2772 }
2773 else
2774 {
2775 error(dotLocation, " no such field in interface block", fieldString.c_str());
2776 recover();
2777 indexedExpression = baseExpression;
2778 }
2779 }
2780 }
2781 else
2782 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002783 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002784 {
2785 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2786 fieldString.c_str());
2787 }
2788 else
2789 {
2790 error(dotLocation,
2791 " field selection requires structure, vector, matrix, or interface block on left hand side",
2792 fieldString.c_str());
2793 }
2794 recover();
2795 indexedExpression = baseExpression;
2796 }
2797
2798 return indexedExpression;
2799}
2800
Nicolas Capens7d626792015-02-17 17:58:31 -05002801TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2802{
2803 TLayoutQualifier qualifier;
2804
2805 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002806 qualifier.matrixPacking = EmpUnspecified;
2807 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002808
Alexis Hetuad6b8752015-06-09 16:15:30 -04002809 if(qualifierType == "shared")
2810 {
2811 qualifier.blockStorage = EbsShared;
2812 }
2813 else if(qualifierType == "packed")
2814 {
2815 qualifier.blockStorage = EbsPacked;
2816 }
2817 else if(qualifierType == "std140")
2818 {
2819 qualifier.blockStorage = EbsStd140;
2820 }
2821 else if(qualifierType == "row_major")
2822 {
2823 qualifier.matrixPacking = EmpRowMajor;
2824 }
2825 else if(qualifierType == "column_major")
2826 {
2827 qualifier.matrixPacking = EmpColumnMajor;
2828 }
2829 else if(qualifierType == "location")
Nicolas Capens7d626792015-02-17 17:58:31 -05002830 {
2831 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2832 recover();
2833 }
2834 else
2835 {
2836 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2837 recover();
2838 }
2839
2840 return qualifier;
2841}
2842
2843TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2844{
2845 TLayoutQualifier qualifier;
2846
2847 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002848 qualifier.matrixPacking = EmpUnspecified;
2849 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002850
2851 if (qualifierType != "location")
2852 {
2853 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2854 recover();
2855 }
2856 else
2857 {
2858 // must check that location is non-negative
2859 if (intValue < 0)
2860 {
2861 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2862 recover();
2863 }
2864 else
2865 {
2866 qualifier.location = intValue;
2867 }
2868 }
2869
2870 return qualifier;
2871}
2872
2873TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2874{
2875 TLayoutQualifier joinedQualifier = leftQualifier;
2876
2877 if (rightQualifier.location != -1)
2878 {
2879 joinedQualifier.location = rightQualifier.location;
2880 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002881 if(rightQualifier.matrixPacking != EmpUnspecified)
2882 {
2883 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2884 }
2885 if(rightQualifier.blockStorage != EbsUnspecified)
2886 {
2887 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2888 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002889
2890 return joinedQualifier;
2891}
2892
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002893
2894TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2895 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2896{
2897 TQualifier mergedQualifier = EvqSmoothIn;
2898
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002899 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002900 if(interpolationQualifier == EvqSmooth)
2901 mergedQualifier = EvqSmoothIn;
2902 else if(interpolationQualifier == EvqFlat)
2903 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002904 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002905 }
2906 else if(storageQualifier == EvqCentroidIn) {
2907 if(interpolationQualifier == EvqSmooth)
2908 mergedQualifier = EvqCentroidIn;
2909 else if(interpolationQualifier == EvqFlat)
2910 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002911 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002912 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002913 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002914 if(interpolationQualifier == EvqSmooth)
2915 mergedQualifier = EvqSmoothOut;
2916 else if(interpolationQualifier == EvqFlat)
2917 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002918 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002919 }
2920 else if(storageQualifier == EvqCentroidOut) {
2921 if(interpolationQualifier == EvqSmooth)
2922 mergedQualifier = EvqCentroidOut;
2923 else if(interpolationQualifier == EvqFlat)
2924 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002925 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002926 }
2927 else {
2928 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2929 recover();
2930
2931 mergedQualifier = storageQualifier;
2932 }
2933
2934 TPublicType type;
2935 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2936 return type;
2937}
2938
Alexis Hetuad6b8752015-06-09 16:15:30 -04002939TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2940{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002941 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002942 {
2943 recover();
2944 }
2945
2946 for(unsigned int i = 0; i < fieldList->size(); ++i)
2947 {
2948 //
2949 // Careful not to replace already known aspects of type, like array-ness
2950 //
2951 TType *type = (*fieldList)[i]->type();
2952 type->setBasicType(typeSpecifier.type);
2953 type->setNominalSize(typeSpecifier.primarySize);
2954 type->setSecondarySize(typeSpecifier.secondarySize);
2955 type->setPrecision(typeSpecifier.precision);
2956 type->setQualifier(typeSpecifier.qualifier);
2957 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2958
2959 // don't allow arrays of arrays
2960 if(type->isArray())
2961 {
2962 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2963 recover();
2964 }
2965 if(typeSpecifier.array)
2966 type->setArraySize(typeSpecifier.arraySize);
2967 if(typeSpecifier.userDef)
2968 {
2969 type->setStruct(typeSpecifier.userDef->getStruct());
2970 }
2971
2972 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2973 {
2974 recover();
2975 }
2976 }
2977
2978 return fieldList;
2979}
2980
2981TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2982 const TString *structName, TFieldList *fieldList)
2983{
2984 TStructure *structure = new TStructure(structName, fieldList);
2985 TType *structureType = new TType(structure);
2986
2987 // Store a bool in the struct if we're at global scope, to allow us to
2988 // skip the local struct scoping workaround in HLSL.
2989 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2990 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2991
2992 if(!structName->empty())
2993 {
2994 if(reservedErrorCheck(nameLine, *structName))
2995 {
2996 recover();
2997 }
2998 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2999 if(!symbolTable.declare(*userTypeDef))
3000 {
3001 error(nameLine, "redefinition", structName->c_str(), "struct");
3002 recover();
3003 }
3004 }
3005
3006 // ensure we do not specify any storage qualifiers on the struct members
3007 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
3008 {
3009 const TField &field = *(*fieldList)[typeListIndex];
3010 const TQualifier qualifier = field.type()->getQualifier();
3011 switch(qualifier)
3012 {
3013 case EvqGlobal:
3014 case EvqTemporary:
3015 break;
3016 default:
3017 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
3018 recover();
3019 break;
3020 }
3021 }
3022
3023 TPublicType publicType;
3024 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
3025 publicType.userDef = structureType;
3026 exitStructDeclaration();
3027
3028 return publicType;
3029}
3030
Alexis Hetufe1269e2015-06-16 12:43:32 -04003031bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04003032{
Alexis Hetu0a655842015-06-22 16:52:11 -04003033 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003034
3035 // Embedded structure definitions are not supported per GLSL ES spec.
3036 // They aren't allowed in GLSL either, but we need to detect this here
3037 // so we don't rely on the GLSL compiler to catch it.
Alexis Hetu0a655842015-06-22 16:52:11 -04003038 if (mStructNestingLevel > 1) {
John Bauman66b8ab22014-05-06 15:57:45 -04003039 error(line, "", "Embedded struct definitions are not allowed");
3040 return true;
3041 }
3042
3043 return false;
3044}
3045
3046void TParseContext::exitStructDeclaration()
3047{
Alexis Hetu0a655842015-06-22 16:52:11 -04003048 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003049}
3050
Alexis Hetuad6b8752015-06-09 16:15:30 -04003051bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3052{
3053 static const int kWebGLMaxStructNesting = 4;
3054
3055 if(field.type()->getBasicType() != EbtStruct)
3056 {
3057 return false;
3058 }
3059
3060 // We're already inside a structure definition at this point, so add
3061 // one to the field's struct nesting.
3062 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3063 {
3064 std::stringstream reasonStream;
3065 reasonStream << "Reference of struct type "
3066 << field.type()->getStruct()->name().c_str()
3067 << " exceeds maximum allowed nesting level of "
3068 << kWebGLMaxStructNesting;
3069 std::string reason = reasonStream.str();
3070 error(line, reason.c_str(), field.name().c_str(), "");
3071 return true;
3072 }
3073
3074 return false;
3075}
3076
3077TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3078{
3079 if(child == nullptr)
3080 {
3081 return nullptr;
3082 }
3083
3084 switch(op)
3085 {
3086 case EOpLogicalNot:
3087 if(child->getBasicType() != EbtBool ||
3088 child->isMatrix() ||
3089 child->isArray() ||
3090 child->isVector())
3091 {
3092 return nullptr;
3093 }
3094 break;
3095 case EOpBitwiseNot:
3096 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3097 child->isMatrix() ||
3098 child->isArray())
3099 {
3100 return nullptr;
3101 }
3102 break;
3103 case EOpPostIncrement:
3104 case EOpPreIncrement:
3105 case EOpPostDecrement:
3106 case EOpPreDecrement:
3107 case EOpNegative:
3108 if(child->getBasicType() == EbtStruct ||
3109 child->getBasicType() == EbtBool ||
3110 child->isArray())
3111 {
3112 return nullptr;
3113 }
3114 // Operators for built-ins are already type checked against their prototype.
3115 default:
3116 break;
3117 }
3118
3119 return intermediate.addUnaryMath(op, child, loc); // FIXME , funcReturnType);
3120}
3121
3122TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3123{
3124 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3125 if(node == nullptr)
3126 {
3127 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3128 recover();
3129 return child;
3130 }
3131 return node;
3132}
3133
3134TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3135{
3136 if(lValueErrorCheck(loc, getOperatorString(op), child))
3137 recover();
3138 return addUnaryMath(op, child, loc);
3139}
3140
3141bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3142{
3143 if(left->isArray() || right->isArray())
3144 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003145 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003146 {
3147 error(loc, "Invalid operation for arrays", getOperatorString(op));
3148 return false;
3149 }
3150
3151 if(left->isArray() != right->isArray())
3152 {
3153 error(loc, "array / non-array mismatch", getOperatorString(op));
3154 return false;
3155 }
3156
3157 switch(op)
3158 {
3159 case EOpEqual:
3160 case EOpNotEqual:
3161 case EOpAssign:
3162 case EOpInitialize:
3163 break;
3164 default:
3165 error(loc, "Invalid operation for arrays", getOperatorString(op));
3166 return false;
3167 }
3168 // At this point, size of implicitly sized arrays should be resolved.
3169 if(left->getArraySize() != right->getArraySize())
3170 {
3171 error(loc, "array size mismatch", getOperatorString(op));
3172 return false;
3173 }
3174 }
3175
3176 // Check ops which require integer / ivec parameters
3177 bool isBitShift = false;
3178 switch(op)
3179 {
3180 case EOpBitShiftLeft:
3181 case EOpBitShiftRight:
3182 case EOpBitShiftLeftAssign:
3183 case EOpBitShiftRightAssign:
3184 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3185 // check that the basic type is an integer type.
3186 isBitShift = true;
3187 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3188 {
3189 return false;
3190 }
3191 break;
3192 case EOpBitwiseAnd:
3193 case EOpBitwiseXor:
3194 case EOpBitwiseOr:
3195 case EOpBitwiseAndAssign:
3196 case EOpBitwiseXorAssign:
3197 case EOpBitwiseOrAssign:
3198 // It is enough to check the type of only one operand, since later it
3199 // is checked that the operand types match.
3200 if(!IsInteger(left->getBasicType()))
3201 {
3202 return false;
3203 }
3204 break;
3205 default:
3206 break;
3207 }
3208
3209 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3210 // So the basic type should usually match.
3211 if(!isBitShift && left->getBasicType() != right->getBasicType())
3212 {
3213 return false;
3214 }
3215
3216 // Check that type sizes match exactly on ops that require that.
3217 // Also check restrictions for structs that contain arrays or samplers.
3218 switch(op)
3219 {
3220 case EOpAssign:
3221 case EOpInitialize:
3222 case EOpEqual:
3223 case EOpNotEqual:
3224 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003225 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003226 {
3227 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3228 return false;
3229 }
3230 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3231 // we interpret the spec so that this extends to structs containing samplers,
3232 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003233 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003234 left->getType().isStructureContainingSamplers())
3235 {
3236 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3237 return false;
3238 }
3239 case EOpLessThan:
3240 case EOpGreaterThan:
3241 case EOpLessThanEqual:
3242 case EOpGreaterThanEqual:
3243 if((left->getNominalSize() != right->getNominalSize()) ||
3244 (left->getSecondarySize() != right->getSecondarySize()))
3245 {
3246 return false;
3247 }
3248 default:
3249 break;
3250 }
3251
3252 return true;
3253}
3254
Alexis Hetu76a343a2015-06-04 17:21:22 -04003255TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3256{
3257 TBasicType switchType = init->getBasicType();
3258 if((switchType != EbtInt && switchType != EbtUInt) ||
3259 init->isMatrix() ||
3260 init->isArray() ||
3261 init->isVector())
3262 {
3263 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3264 recover();
3265 return nullptr;
3266 }
3267
3268 if(statementList)
3269 {
3270 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3271 {
3272 recover();
3273 return nullptr;
3274 }
3275 }
3276
3277 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3278 if(node == nullptr)
3279 {
3280 error(loc, "erroneous switch statement", "switch");
3281 recover();
3282 return nullptr;
3283 }
3284 return node;
3285}
3286
3287TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3288{
Alexis Hetu0a655842015-06-22 16:52:11 -04003289 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003290 {
3291 error(loc, "case labels need to be inside switch statements", "case");
3292 recover();
3293 return nullptr;
3294 }
3295 if(condition == nullptr)
3296 {
3297 error(loc, "case label must have a condition", "case");
3298 recover();
3299 return nullptr;
3300 }
3301 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3302 condition->isMatrix() ||
3303 condition->isArray() ||
3304 condition->isVector())
3305 {
3306 error(condition->getLine(), "case label must be a scalar integer", "case");
3307 recover();
3308 }
3309 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3310 if(conditionConst == nullptr)
3311 {
3312 error(condition->getLine(), "case label must be constant", "case");
3313 recover();
3314 }
3315 TIntermCase *node = intermediate.addCase(condition, loc);
3316 if(node == nullptr)
3317 {
3318 error(loc, "erroneous case statement", "case");
3319 recover();
3320 return nullptr;
3321 }
3322 return node;
3323}
3324
3325TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3326{
Alexis Hetu0a655842015-06-22 16:52:11 -04003327 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003328 {
3329 error(loc, "default labels need to be inside switch statements", "default");
3330 recover();
3331 return nullptr;
3332 }
3333 TIntermCase *node = intermediate.addCase(nullptr, loc);
3334 if(node == nullptr)
3335 {
3336 error(loc, "erroneous default statement", "default");
3337 recover();
3338 return nullptr;
3339 }
3340 return node;
3341}
Alexis Hetue5246692015-06-18 12:34:52 -04003342TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3343{
3344 if(binaryOpCommonCheck(op, left, right, loc))
3345 {
3346 return intermediate.addAssign(op, left, right, loc);
3347 }
3348 return nullptr;
3349}
3350
3351TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3352{
3353 TIntermTyped *node = createAssign(op, left, right, loc);
3354 if(node == nullptr)
3355 {
3356 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3357 recover();
3358 return left;
3359 }
3360 return node;
3361}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003362
Alexis Hetub4769582015-06-16 12:19:50 -04003363TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3364 const TSourceLoc &loc)
3365{
3366 if(!binaryOpCommonCheck(op, left, right, loc))
3367 return nullptr;
3368
3369 switch(op)
3370 {
3371 case EOpEqual:
3372 case EOpNotEqual:
3373 break;
3374 case EOpLessThan:
3375 case EOpGreaterThan:
3376 case EOpLessThanEqual:
3377 case EOpGreaterThanEqual:
3378 ASSERT(!left->isArray() && !right->isArray());
3379 if(left->isMatrix() || left->isVector() ||
3380 left->getBasicType() == EbtStruct)
3381 {
3382 return nullptr;
3383 }
3384 break;
3385 case EOpLogicalOr:
3386 case EOpLogicalXor:
3387 case EOpLogicalAnd:
3388 ASSERT(!left->isArray() && !right->isArray());
3389 if(left->getBasicType() != EbtBool ||
3390 left->isMatrix() || left->isVector())
3391 {
3392 return nullptr;
3393 }
3394 break;
3395 case EOpAdd:
3396 case EOpSub:
3397 case EOpDiv:
3398 case EOpMul:
3399 ASSERT(!left->isArray() && !right->isArray());
3400 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3401 {
3402 return nullptr;
3403 }
3404 break;
3405 case EOpIMod:
3406 ASSERT(!left->isArray() && !right->isArray());
3407 // Note that this is only for the % operator, not for mod()
3408 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3409 {
3410 return nullptr;
3411 }
3412 break;
3413 // Note that for bitwise ops, type checking is done in promote() to
3414 // share code between ops and compound assignment
3415 default:
3416 break;
3417 }
3418
3419 return intermediate.addBinaryMath(op, left, right, loc);
3420}
3421
3422TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3423{
3424 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3425 if(node == 0)
3426 {
3427 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3428 recover();
3429 return left;
3430 }
3431 return node;
3432}
3433
3434TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3435{
3436 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3437 if(node == 0)
3438 {
3439 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3440 recover();
3441 ConstantUnion *unionArray = new ConstantUnion[1];
3442 unionArray->setBConst(false);
3443 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3444 }
3445 return node;
3446}
3447
Alexis Hetu76a343a2015-06-04 17:21:22 -04003448TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3449{
3450 switch(op)
3451 {
3452 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003453 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003454 {
3455 error(loc, "continue statement only allowed in loops", "");
3456 recover();
3457 }
3458 break;
3459 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003460 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003461 {
3462 error(loc, "break statement only allowed in loops and switch statements", "");
3463 recover();
3464 }
3465 break;
3466 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003467 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003468 {
3469 error(loc, "non-void function must return a value", "return");
3470 recover();
3471 }
3472 break;
3473 default:
3474 // No checks for discard
3475 break;
3476 }
3477 return intermediate.addBranch(op, loc);
3478}
3479
3480TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3481{
3482 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003483 mFunctionReturnsValue = true;
3484 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003485 {
3486 error(loc, "void function cannot return a value", "return");
3487 recover();
3488 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003489 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003490 {
3491 error(loc, "function return is not matching type:", "return");
3492 recover();
3493 }
3494 return intermediate.addBranch(op, returnValue, loc);
3495}
3496
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003497TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3498{
3499 *fatalError = false;
3500 TOperator op = fnCall->getBuiltInOp();
3501 TIntermTyped *callNode = nullptr;
3502
3503 if(thisNode != nullptr)
3504 {
3505 ConstantUnion *unionArray = new ConstantUnion[1];
3506 int arraySize = 0;
3507 TIntermTyped *typedThis = thisNode->getAsTyped();
3508 if(fnCall->getName() != "length")
3509 {
3510 error(loc, "invalid method", fnCall->getName().c_str());
3511 recover();
3512 }
3513 else if(paramNode != nullptr)
3514 {
3515 error(loc, "method takes no parameters", "length");
3516 recover();
3517 }
3518 else if(typedThis == nullptr || !typedThis->isArray())
3519 {
3520 error(loc, "length can only be called on arrays", "length");
3521 recover();
3522 }
3523 else
3524 {
3525 arraySize = typedThis->getArraySize();
3526 if(typedThis->getAsSymbolNode() == nullptr)
3527 {
3528 // This code path can be hit with expressions like these:
3529 // (a = b).length()
3530 // (func()).length()
3531 // (int[3](0, 1, 2)).length()
3532 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3533 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3534 // which allows "An array, vector or matrix expression with the length method applied".
3535 error(loc, "length can only be called on array names, not on array expressions", "length");
3536 recover();
3537 }
3538 }
3539 unionArray->setIConst(arraySize);
3540 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3541 }
3542 else if(op != EOpNull)
3543 {
3544 //
3545 // Then this should be a constructor.
3546 // Don't go through the symbol table for constructors.
3547 // Their parameters will be verified algorithmically.
3548 //
3549 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3550 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3551 {
3552 //
3553 // It's a constructor, of type 'type'.
3554 //
3555 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3556 }
3557
3558 if(callNode == nullptr)
3559 {
3560 recover();
3561 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3562 }
3563 callNode->setType(type);
3564 }
3565 else
3566 {
3567 //
3568 // Not a constructor. Find it in the symbol table.
3569 //
3570 const TFunction *fnCandidate;
3571 bool builtIn;
3572 fnCandidate = findFunction(loc, fnCall, &builtIn);
3573 if(fnCandidate)
3574 {
3575 //
3576 // A declared function.
3577 //
3578 if(builtIn && !fnCandidate->getExtension().empty() &&
3579 extensionErrorCheck(loc, fnCandidate->getExtension()))
3580 {
3581 recover();
3582 }
3583 op = fnCandidate->getBuiltInOp();
3584 if(builtIn && op != EOpNull)
3585 {
3586 //
3587 // A function call mapped to a built-in operation.
3588 //
3589 if(fnCandidate->getParamCount() == 1)
3590 {
3591 //
3592 // Treat it like a built-in unary operator.
3593 //
3594 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3595 if(callNode == nullptr)
3596 {
3597 std::stringstream extraInfoStream;
3598 extraInfoStream << "built in unary operator function. Type: "
3599 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3600 std::string extraInfo = extraInfoStream.str();
3601 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3602 *fatalError = true;
3603 return nullptr;
3604 }
3605 }
3606 else
3607 {
3608 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3609 aggregate->setType(fnCandidate->getReturnType());
3610
3611 // Some built-in functions have out parameters too.
3612 functionCallLValueErrorCheck(fnCandidate, aggregate);
3613
3614 callNode = aggregate;
3615 }
3616 }
3617 else
3618 {
3619 // This is a real function call
3620
3621 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3622 aggregate->setType(fnCandidate->getReturnType());
3623
3624 // this is how we know whether the given function is a builtIn function or a user defined function
3625 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3626 // if builtIn == true, it's definitely a builtIn function with EOpNull
3627 if(!builtIn)
3628 aggregate->setUserDefined();
3629 aggregate->setName(fnCandidate->getMangledName());
3630
3631 callNode = aggregate;
3632
3633 functionCallLValueErrorCheck(fnCandidate, aggregate);
3634 }
3635 callNode->setType(fnCandidate->getReturnType());
3636 }
3637 else
3638 {
3639 // error message was put out by findFunction()
3640 // Put on a dummy node for error recovery
3641 ConstantUnion *unionArray = new ConstantUnion[1];
3642 unionArray->setFConst(0.0f);
3643 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3644 recover();
3645 }
3646 }
3647 delete fnCall;
3648 return callNode;
3649}
3650
Alexis Hetueee212e2015-07-07 17:13:30 -04003651TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3652{
3653 if(boolErrorCheck(loc, cond))
3654 recover();
3655
3656 if(trueBlock->getType() != falseBlock->getType())
3657 {
3658 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3659 recover();
3660 return falseBlock;
3661 }
3662 // ESSL1 sections 5.2 and 5.7:
3663 // ESSL3 section 5.7:
3664 // Ternary operator is not among the operators allowed for structures/arrays.
3665 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3666 {
3667 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3668 recover();
3669 return falseBlock;
3670 }
3671 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3672}
3673
John Bauman66b8ab22014-05-06 15:57:45 -04003674//
3675// Parse an array of strings using yyparse.
3676//
3677// Returns 0 for success.
3678//
3679int PaParseStrings(int count, const char* const string[], const int length[],
3680 TParseContext* context) {
3681 if ((count == 0) || (string == NULL))
3682 return 1;
3683
3684 if (glslang_initialize(context))
3685 return 1;
3686
3687 int error = glslang_scan(count, string, length, context);
3688 if (!error)
3689 error = glslang_parse(context);
3690
3691 glslang_finalize(context);
3692
3693 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
3694}
3695
3696
3697