blob: cb513deb0acbd7c5060f7027a5ec6e86e6e87553 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman66b8ab22014-05-06 15:57:45 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman66b8ab22014-05-06 15:57:45 -040014
Nicolas Capenscc863da2015-01-21 15:50:55 -050015#include "ParseHelper.h"
John Bauman66b8ab22014-05-06 15:57:45 -040016
17#include <stdarg.h>
18#include <stdio.h>
19
Nicolas Capenscc863da2015-01-21 15:50:55 -050020#include "glslang.h"
21#include "preprocessor/SourceLocation.h"
Alexis Hetu76a343a2015-06-04 17:21:22 -040022#include "ValidateSwitch.h"
John Bauman66b8ab22014-05-06 15:57:45 -040023
24///////////////////////////////////////////////////////////////////////
25//
26// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Alexis Hetuec93b1d2016-12-09 16:01:29 -050030namespace
31{
32 bool IsVaryingOut(TQualifier qualifier)
33 {
34 switch(qualifier)
35 {
36 case EvqVaryingOut:
37 case EvqSmoothOut:
38 case EvqFlatOut:
39 case EvqCentroidOut:
40 case EvqVertexOut:
41 return true;
42
43 default: break;
44 }
45
46 return false;
47 }
48
49 bool IsVaryingIn(TQualifier qualifier)
50 {
51 switch(qualifier)
52 {
53 case EvqVaryingIn:
54 case EvqSmoothIn:
55 case EvqFlatIn:
56 case EvqCentroidIn:
57 case EvqFragmentIn:
58 return true;
59
60 default: break;
61 }
62
63 return false;
64 }
65
66 bool IsVarying(TQualifier qualifier)
67 {
68 return IsVaryingIn(qualifier) || IsVaryingOut(qualifier);
69 }
70
71 bool IsAssignment(TOperator op)
72 {
73 switch(op)
74 {
75 case EOpPostIncrement:
76 case EOpPostDecrement:
77 case EOpPreIncrement:
78 case EOpPreDecrement:
79 case EOpAssign:
80 case EOpAddAssign:
81 case EOpSubAssign:
82 case EOpMulAssign:
83 case EOpVectorTimesMatrixAssign:
84 case EOpVectorTimesScalarAssign:
85 case EOpMatrixTimesScalarAssign:
86 case EOpMatrixTimesMatrixAssign:
87 case EOpDivAssign:
88 case EOpIModAssign:
89 case EOpBitShiftLeftAssign:
90 case EOpBitShiftRightAssign:
91 case EOpBitwiseAndAssign:
92 case EOpBitwiseXorAssign:
93 case EOpBitwiseOrAssign:
94 return true;
95 default:
96 return false;
97 }
98 }
99}
100
John Bauman66b8ab22014-05-06 15:57:45 -0400101//
102// Look at a '.' field selector string and change it into offsets
103// for a vector.
104//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400105bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400106{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400107 fields.num = (int) compString.size();
108 if (fields.num > 4) {
109 error(line, "illegal vector field selection", compString.c_str());
110 return false;
111 }
John Bauman66b8ab22014-05-06 15:57:45 -0400112
Nicolas Capens0bac2852016-05-07 06:09:58 -0400113 enum {
114 exyzw,
115 ergba,
116 estpq
117 } fieldSet[4];
John Bauman66b8ab22014-05-06 15:57:45 -0400118
Nicolas Capens0bac2852016-05-07 06:09:58 -0400119 for (int i = 0; i < fields.num; ++i) {
120 switch (compString[i]) {
121 case 'x':
122 fields.offsets[i] = 0;
123 fieldSet[i] = exyzw;
124 break;
125 case 'r':
126 fields.offsets[i] = 0;
127 fieldSet[i] = ergba;
128 break;
129 case 's':
130 fields.offsets[i] = 0;
131 fieldSet[i] = estpq;
132 break;
133 case 'y':
134 fields.offsets[i] = 1;
135 fieldSet[i] = exyzw;
136 break;
137 case 'g':
138 fields.offsets[i] = 1;
139 fieldSet[i] = ergba;
140 break;
141 case 't':
142 fields.offsets[i] = 1;
143 fieldSet[i] = estpq;
144 break;
145 case 'z':
146 fields.offsets[i] = 2;
147 fieldSet[i] = exyzw;
148 break;
149 case 'b':
150 fields.offsets[i] = 2;
151 fieldSet[i] = ergba;
152 break;
153 case 'p':
154 fields.offsets[i] = 2;
155 fieldSet[i] = estpq;
156 break;
157 case 'w':
158 fields.offsets[i] = 3;
159 fieldSet[i] = exyzw;
160 break;
161 case 'a':
162 fields.offsets[i] = 3;
163 fieldSet[i] = ergba;
164 break;
165 case 'q':
166 fields.offsets[i] = 3;
167 fieldSet[i] = estpq;
168 break;
169 default:
170 error(line, "illegal vector field selection", compString.c_str());
171 return false;
172 }
173 }
John Bauman66b8ab22014-05-06 15:57:45 -0400174
Nicolas Capens0bac2852016-05-07 06:09:58 -0400175 for (int i = 0; i < fields.num; ++i) {
176 if (fields.offsets[i] >= vecSize) {
177 error(line, "vector field selection out of range", compString.c_str());
178 return false;
179 }
John Bauman66b8ab22014-05-06 15:57:45 -0400180
Nicolas Capens0bac2852016-05-07 06:09:58 -0400181 if (i > 0) {
182 if (fieldSet[i] != fieldSet[i-1]) {
183 error(line, "illegal - vector component fields not from the same set", compString.c_str());
184 return false;
185 }
186 }
187 }
John Bauman66b8ab22014-05-06 15:57:45 -0400188
Nicolas Capens0bac2852016-05-07 06:09:58 -0400189 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400190}
191
John Bauman66b8ab22014-05-06 15:57:45 -0400192///////////////////////////////////////////////////////////////////////
193//
194// Errors
195//
196////////////////////////////////////////////////////////////////////////
197
198//
199// Track whether errors have occurred.
200//
201void TParseContext::recover()
202{
203}
204
205//
206// Used by flex/bison to output all syntax and parsing errors.
207//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400208void TParseContext::error(const TSourceLoc& loc,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400209 const char* reason, const char* token,
210 const char* extraInfo)
John Bauman66b8ab22014-05-06 15:57:45 -0400211{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400212 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
213 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
214 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400215
216}
217
Alexis Hetufe1269e2015-06-16 12:43:32 -0400218void TParseContext::warning(const TSourceLoc& loc,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400219 const char* reason, const char* token,
220 const char* extraInfo) {
221 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
222 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
223 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400224}
225
226void TParseContext::trace(const char* str)
227{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400228 mDiagnostics.writeDebug(str);
John Bauman66b8ab22014-05-06 15:57:45 -0400229}
230
231//
232// Same error message for all places assignments don't work.
233//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400234void TParseContext::assignError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400235{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400236 std::stringstream extraInfoStream;
237 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
238 std::string extraInfo = extraInfoStream.str();
239 error(line, "", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400240}
241
242//
243// Same error message for all places unary operations don't work.
244//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400245void TParseContext::unaryOpError(const TSourceLoc &line, const char* op, TString operand)
John Bauman66b8ab22014-05-06 15:57:45 -0400246{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400247 std::stringstream extraInfoStream;
248 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
249 << " (or there is no acceptable conversion)";
250 std::string extraInfo = extraInfoStream.str();
251 error(line, " wrong operand type", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400252}
253
254//
255// Same error message for all binary operations don't work.
256//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400257void TParseContext::binaryOpError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400258{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400259 std::stringstream extraInfoStream;
260 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
261 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
262 std::string extraInfo = extraInfoStream.str();
263 error(line, " wrong operand types ", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400264}
265
Alexis Hetufe1269e2015-06-16 12:43:32 -0400266bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type){
Nicolas Capens0bac2852016-05-07 06:09:58 -0400267 if (!mChecksPrecisionErrors)
268 return false;
269 switch( type ){
270 case EbtFloat:
271 if( precision == EbpUndefined ){
272 error( line, "No precision specified for (float)", "" );
273 return true;
274 }
275 break;
276 case EbtInt:
277 if( precision == EbpUndefined ){
278 error( line, "No precision specified (int)", "" );
279 return true;
280 }
281 break;
282 default:
283 return false;
284 }
285 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400286}
287
288//
289// Both test and if necessary, spit out an error, to see if the node is really
290// an l-value that can be operated on this way.
291//
292// Returns true if the was an error.
293//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400294bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char* op, TIntermTyped* node)
John Bauman66b8ab22014-05-06 15:57:45 -0400295{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400296 TIntermSymbol* symNode = node->getAsSymbolNode();
297 TIntermBinary* binaryNode = node->getAsBinaryNode();
John Bauman66b8ab22014-05-06 15:57:45 -0400298
Nicolas Capens0bac2852016-05-07 06:09:58 -0400299 if (binaryNode) {
300 bool errorReturn;
John Bauman66b8ab22014-05-06 15:57:45 -0400301
Nicolas Capens0bac2852016-05-07 06:09:58 -0400302 switch(binaryNode->getOp()) {
303 case EOpIndexDirect:
304 case EOpIndexIndirect:
305 case EOpIndexDirectStruct:
306 return lValueErrorCheck(line, op, binaryNode->getLeft());
307 case EOpVectorSwizzle:
308 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
309 if (!errorReturn) {
310 int offset[4] = {0,0,0,0};
John Bauman66b8ab22014-05-06 15:57:45 -0400311
Nicolas Capens0bac2852016-05-07 06:09:58 -0400312 TIntermTyped* rightNode = binaryNode->getRight();
313 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400314
Nicolas Capens0bac2852016-05-07 06:09:58 -0400315 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
316 p != aggrNode->getSequence().end(); p++) {
317 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
318 offset[value]++;
319 if (offset[value] > 1) {
320 error(line, " l-value of swizzle cannot have duplicate components", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400321
Nicolas Capens0bac2852016-05-07 06:09:58 -0400322 return true;
323 }
324 }
325 }
John Bauman66b8ab22014-05-06 15:57:45 -0400326
Nicolas Capens0bac2852016-05-07 06:09:58 -0400327 return errorReturn;
Nicolas Capens91b425b2017-11-15 14:39:15 -0500328 case EOpIndexDirectInterfaceBlock:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400329 default:
330 break;
331 }
332 error(line, " l-value required", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400333
Nicolas Capens0bac2852016-05-07 06:09:58 -0400334 return true;
335 }
John Bauman66b8ab22014-05-06 15:57:45 -0400336
337
Nicolas Capens0bac2852016-05-07 06:09:58 -0400338 const char* symbol = 0;
339 if (symNode != 0)
340 symbol = symNode->getSymbol().c_str();
John Bauman66b8ab22014-05-06 15:57:45 -0400341
Nicolas Capens0bac2852016-05-07 06:09:58 -0400342 const char* message = 0;
343 switch (node->getQualifier()) {
344 case EvqConstExpr: message = "can't modify a const"; break;
345 case EvqConstReadOnly: message = "can't modify a const"; break;
346 case EvqAttribute: message = "can't modify an attribute"; break;
347 case EvqFragmentIn: message = "can't modify an input"; break;
348 case EvqVertexIn: message = "can't modify an input"; break;
349 case EvqUniform: message = "can't modify a uniform"; break;
350 case EvqSmoothIn:
351 case EvqFlatIn:
352 case EvqCentroidIn:
353 case EvqVaryingIn: message = "can't modify a varying"; break;
354 case EvqInput: message = "can't modify an input"; break;
355 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
356 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
357 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
358 case EvqInstanceID: message = "can't modify gl_InstanceID"; break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400359 case EvqVertexID: message = "can't modify gl_VertexID"; break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400360 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400361
Nicolas Capens0bac2852016-05-07 06:09:58 -0400362 //
363 // Type that can't be written to?
364 //
365 if(IsSampler(node->getBasicType()))
366 {
367 message = "can't modify a sampler";
368 }
369 else if(node->getBasicType() == EbtVoid)
370 {
371 message = "can't modify void";
372 }
373 }
John Bauman66b8ab22014-05-06 15:57:45 -0400374
Nicolas Capens0bac2852016-05-07 06:09:58 -0400375 if (message == 0 && binaryNode == 0 && symNode == 0) {
376 error(line, " l-value required", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400377
Nicolas Capens0bac2852016-05-07 06:09:58 -0400378 return true;
379 }
John Bauman66b8ab22014-05-06 15:57:45 -0400380
381
Nicolas Capens0bac2852016-05-07 06:09:58 -0400382 //
383 // Everything else is okay, no error.
384 //
385 if (message == 0)
386 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400387
Nicolas Capens0bac2852016-05-07 06:09:58 -0400388 //
389 // If we get here, we have an error and a message.
390 //
391 if (symNode) {
392 std::stringstream extraInfoStream;
393 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
394 std::string extraInfo = extraInfoStream.str();
395 error(line, " l-value required", op, extraInfo.c_str());
396 }
397 else {
398 std::stringstream extraInfoStream;
399 extraInfoStream << "(" << message << ")";
400 std::string extraInfo = extraInfoStream.str();
401 error(line, " l-value required", op, extraInfo.c_str());
402 }
John Bauman66b8ab22014-05-06 15:57:45 -0400403
Nicolas Capens0bac2852016-05-07 06:09:58 -0400404 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400405}
406
407//
408// Both test, and if necessary spit out an error, to see if the node is really
409// a constant.
410//
411// Returns true if the was an error.
412//
413bool TParseContext::constErrorCheck(TIntermTyped* node)
414{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400415 if (node->getQualifier() == EvqConstExpr)
416 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400417
Nicolas Capens0bac2852016-05-07 06:09:58 -0400418 error(node->getLine(), "constant expression required", "");
John Bauman66b8ab22014-05-06 15:57:45 -0400419
Nicolas Capens0bac2852016-05-07 06:09:58 -0400420 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400421}
422
423//
424// Both test, and if necessary spit out an error, to see if the node is really
425// an integer.
426//
427// Returns true if the was an error.
428//
429bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
430{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400431 if (node->isScalarInt())
432 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400433
Nicolas Capens0bac2852016-05-07 06:09:58 -0400434 error(node->getLine(), "integer expression required", token);
John Bauman66b8ab22014-05-06 15:57:45 -0400435
Nicolas Capens0bac2852016-05-07 06:09:58 -0400436 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400437}
438
439//
440// Both test, and if necessary spit out an error, to see if we are currently
441// globally scoped.
442//
443// Returns true if the was an error.
444//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400445bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char* token)
John Bauman66b8ab22014-05-06 15:57:45 -0400446{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400447 if (global)
448 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400449
Nicolas Capens0bac2852016-05-07 06:09:58 -0400450 error(line, "only allowed at global scope", token);
John Bauman66b8ab22014-05-06 15:57:45 -0400451
Nicolas Capens0bac2852016-05-07 06:09:58 -0400452 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400453}
454
455//
456// For now, keep it simple: if it starts "gl_", it's reserved, independent
457// of scope. Except, if the symbol table is at the built-in push-level,
458// which is when we are parsing built-ins.
459// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
460// webgl shader.
461//
462// Returns true if there was an error.
463//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400464bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -0400465{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400466 static const char* reservedErrMsg = "reserved built-in name";
467 if (!symbolTable.atBuiltInLevel()) {
468 if (identifier.compare(0, 3, "gl_") == 0) {
469 error(line, reservedErrMsg, "gl_");
470 return true;
471 }
472 if (identifier.find("__") != TString::npos) {
473 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
474 return true;
475 }
476 }
John Bauman66b8ab22014-05-06 15:57:45 -0400477
Nicolas Capens0bac2852016-05-07 06:09:58 -0400478 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400479}
480
481//
482// Make sure there is enough data provided to the constructor to build
483// something of the type of the constructor. Also returns the type of
484// the constructor.
485//
486// Returns true if there was an error in construction.
487//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400488bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400489{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400490 *type = function.getReturnType();
John Bauman66b8ab22014-05-06 15:57:45 -0400491
Nicolas Capens0bac2852016-05-07 06:09:58 -0400492 bool constructingMatrix = false;
493 switch(op) {
494 case EOpConstructMat2:
495 case EOpConstructMat2x3:
496 case EOpConstructMat2x4:
497 case EOpConstructMat3x2:
498 case EOpConstructMat3:
499 case EOpConstructMat3x4:
500 case EOpConstructMat4x2:
501 case EOpConstructMat4x3:
502 case EOpConstructMat4:
503 constructingMatrix = true;
504 break;
505 default:
506 break;
507 }
John Bauman66b8ab22014-05-06 15:57:45 -0400508
Nicolas Capens0bac2852016-05-07 06:09:58 -0400509 //
510 // Note: It's okay to have too many components available, but not okay to have unused
511 // arguments. 'full' will go to true when enough args have been seen. If we loop
512 // again, there is an extra argument, so 'overfull' will become true.
513 //
John Bauman66b8ab22014-05-06 15:57:45 -0400514
Nicolas Capens0bac2852016-05-07 06:09:58 -0400515 size_t size = 0;
516 bool full = false;
517 bool overFull = false;
518 bool matrixInMatrix = false;
519 bool arrayArg = false;
520 for (size_t i = 0; i < function.getParamCount(); ++i) {
521 const TParameter& param = function.getParam(i);
522 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400523
Nicolas Capens0bac2852016-05-07 06:09:58 -0400524 if (constructingMatrix && param.type->isMatrix())
525 matrixInMatrix = true;
526 if (full)
527 overFull = true;
528 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
529 full = true;
530 if (param.type->isArray())
531 arrayArg = true;
532 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400533
Nicolas Capens0bac2852016-05-07 06:09:58 -0400534 if(type->isArray()) {
535 if(type->getArraySize() == 0) {
536 type->setArraySize(function.getParamCount());
537 } else if(type->getArraySize() != (int)function.getParamCount()) {
538 error(line, "array constructor needs one argument per array element", "constructor");
539 return true;
540 }
541 }
John Bauman66b8ab22014-05-06 15:57:45 -0400542
Nicolas Capens0bac2852016-05-07 06:09:58 -0400543 if (arrayArg && op != EOpConstructStruct) {
544 error(line, "constructing from a non-dereferenced array", "constructor");
545 return true;
546 }
John Bauman66b8ab22014-05-06 15:57:45 -0400547
Nicolas Capens0bac2852016-05-07 06:09:58 -0400548 if (matrixInMatrix && !type->isArray()) {
549 if (function.getParamCount() != 1) {
550 error(line, "constructing matrix from matrix can only take one argument", "constructor");
551 return true;
552 }
553 }
John Bauman66b8ab22014-05-06 15:57:45 -0400554
Nicolas Capens0bac2852016-05-07 06:09:58 -0400555 if (overFull) {
556 error(line, "too many arguments", "constructor");
557 return true;
558 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400559
Nicolas Capens0bac2852016-05-07 06:09:58 -0400560 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
561 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
562 return true;
563 }
John Bauman66b8ab22014-05-06 15:57:45 -0400564
Nicolas Capens0bac2852016-05-07 06:09:58 -0400565 if (!type->isMatrix() || !matrixInMatrix) {
566 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
567 (op == EOpConstructStruct && size < type->getObjectSize())) {
568 error(line, "not enough data provided for construction", "constructor");
569 return true;
570 }
571 }
John Bauman66b8ab22014-05-06 15:57:45 -0400572
Nicolas Capens0bac2852016-05-07 06:09:58 -0400573 TIntermTyped *typed = node ? node->getAsTyped() : 0;
574 if (typed == 0) {
575 error(line, "constructor argument does not have a type", "constructor");
576 return true;
577 }
578 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
579 error(line, "cannot convert a sampler", "constructor");
580 return true;
581 }
582 if (typed->getBasicType() == EbtVoid) {
583 error(line, "cannot convert a void", "constructor");
584 return true;
585 }
John Bauman66b8ab22014-05-06 15:57:45 -0400586
Nicolas Capens0bac2852016-05-07 06:09:58 -0400587 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400588}
589
590// This function checks to see if a void variable has been declared and raise an error message for such a case
591//
592// returns true in case of an error
593//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400594bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString& identifier, const TBasicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400595{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400596 if(type == EbtVoid) {
597 error(line, "illegal use of type 'void'", identifier.c_str());
598 return true;
599 }
John Bauman66b8ab22014-05-06 15:57:45 -0400600
Nicolas Capens0bac2852016-05-07 06:09:58 -0400601 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400602}
603
604// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
605//
606// returns true in case of an error
607//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400608bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400609{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400610 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
611 error(line, "boolean expression expected", "");
612 return true;
613 }
John Bauman66b8ab22014-05-06 15:57:45 -0400614
Nicolas Capens0bac2852016-05-07 06:09:58 -0400615 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400616}
617
618// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
619//
620// returns true in case of an error
621//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400622bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400623{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400624 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
625 error(line, "boolean expression expected", "");
626 return true;
627 }
John Bauman66b8ab22014-05-06 15:57:45 -0400628
Nicolas Capens0bac2852016-05-07 06:09:58 -0400629 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400630}
631
Alexis Hetufe1269e2015-06-16 12:43:32 -0400632bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType& pType, const char* reason)
John Bauman66b8ab22014-05-06 15:57:45 -0400633{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400634 if (pType.type == EbtStruct) {
635 if (containsSampler(*pType.userDef)) {
636 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400637
Nicolas Capens0bac2852016-05-07 06:09:58 -0400638 return true;
639 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400640
Nicolas Capens0bac2852016-05-07 06:09:58 -0400641 return false;
642 } else if (IsSampler(pType.type)) {
643 error(line, reason, getBasicString(pType.type));
John Bauman66b8ab22014-05-06 15:57:45 -0400644
Nicolas Capens0bac2852016-05-07 06:09:58 -0400645 return true;
646 }
John Bauman66b8ab22014-05-06 15:57:45 -0400647
Nicolas Capens0bac2852016-05-07 06:09:58 -0400648 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400649}
650
Alexis Hetufe1269e2015-06-16 12:43:32 -0400651bool TParseContext::structQualifierErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400652{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400653 switch(pType.qualifier)
654 {
655 case EvqVaryingOut:
656 case EvqSmooth:
657 case EvqFlat:
658 case EvqCentroidOut:
659 case EvqVaryingIn:
660 case EvqSmoothIn:
661 case EvqFlatIn:
662 case EvqCentroidIn:
663 case EvqAttribute:
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400664 case EvqVertexIn:
665 case EvqFragmentOut:
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400666 if(pType.type == EbtStruct)
667 {
668 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400669
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400670 return true;
671 }
672 break;
673 default:
674 break;
675 }
John Bauman66b8ab22014-05-06 15:57:45 -0400676
Nicolas Capens0bac2852016-05-07 06:09:58 -0400677 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
678 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400679
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400680 // check for layout qualifier issues
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400681 if (pType.qualifier != EvqVertexIn && pType.qualifier != EvqFragmentOut &&
Nicolas Capens0bac2852016-05-07 06:09:58 -0400682 layoutLocationErrorCheck(line, pType.layoutQualifier))
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400683 {
684 return true;
685 }
686
Nicolas Capens0bac2852016-05-07 06:09:58 -0400687 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400688}
689
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400690// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
691// declaration.
692//
693bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
694{
695 switch(publicType.qualifier)
696 {
697 case EvqVaryingIn:
698 case EvqVaryingOut:
699 case EvqAttribute:
700 case EvqVertexIn:
701 case EvqFragmentOut:
702 if(publicType.type == EbtStruct)
703 {
704 error(identifierLocation, "cannot be used with a structure",
705 getQualifierString(publicType.qualifier));
706 return true;
707 }
708
709 default: break;
710 }
711
712 if(publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
713 "samplers must be uniform"))
714 {
715 return true;
716 }
717
718 // check for layout qualifier issues
719 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
720
721 if(layoutQualifier.matrixPacking != EmpUnspecified)
722 {
723 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
724 "only valid for interface blocks");
725 return true;
726 }
727
728 if(layoutQualifier.blockStorage != EbsUnspecified)
729 {
730 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
731 "only valid for interface blocks");
732 return true;
733 }
734
735 if(publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
736 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
737 {
738 return true;
739 }
740
741 return false;
742}
743
Nicolas Capens3713cd42015-06-22 10:41:54 -0400744bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
745{
746 if(layoutQualifier.location != -1)
747 {
748 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
749 return true;
750 }
751
752 return false;
753}
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400754
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400755bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
756{
757 if(pType.layoutQualifier.location != -1)
758 {
759 error(line, "location must only be specified for a single input or output variable", "location");
760 return true;
761 }
762
763 return false;
764}
765
Alexis Hetufe1269e2015-06-16 12:43:32 -0400766bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400767{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400768 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
769 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
770 error(line, "samplers cannot be output parameters", type.getBasicString());
771 return true;
772 }
John Bauman66b8ab22014-05-06 15:57:45 -0400773
Nicolas Capens0bac2852016-05-07 06:09:58 -0400774 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400775}
776
777bool TParseContext::containsSampler(TType& type)
778{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400779 if (IsSampler(type.getBasicType()))
780 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400781
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500782 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400783 const TFieldList& fields = type.getStruct()->fields();
784 for(unsigned int i = 0; i < fields.size(); ++i) {
785 if (containsSampler(*fields[i]->type()))
786 return true;
787 }
788 }
John Bauman66b8ab22014-05-06 15:57:45 -0400789
Nicolas Capens0bac2852016-05-07 06:09:58 -0400790 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400791}
792
793//
794// Do size checking for an array type's size.
795//
796// Returns true if there was an error.
797//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400798bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped* expr, int& size)
John Bauman66b8ab22014-05-06 15:57:45 -0400799{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400800 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500801
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500802 if (expr->getQualifier() != EvqConstExpr || constant == 0 || !constant->isScalarInt())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400803 {
804 error(line, "array size must be a constant integer expression", "");
805 return true;
806 }
John Bauman66b8ab22014-05-06 15:57:45 -0400807
Nicolas Capens0bac2852016-05-07 06:09:58 -0400808 if (constant->getBasicType() == EbtUInt)
809 {
810 unsigned int uintSize = constant->getUConst(0);
811 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
812 {
813 error(line, "array size too large", "");
814 size = 1;
815 return true;
816 }
John Bauman66b8ab22014-05-06 15:57:45 -0400817
Nicolas Capens0bac2852016-05-07 06:09:58 -0400818 size = static_cast<int>(uintSize);
819 }
820 else
821 {
822 size = constant->getIConst(0);
Nicolas Capens3c20f802015-02-17 17:17:20 -0500823
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500824 if (size < 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400825 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500826 error(line, "array size must be non-negative", "");
Nicolas Capens0bac2852016-05-07 06:09:58 -0400827 size = 1;
828 return true;
829 }
830 }
John Bauman66b8ab22014-05-06 15:57:45 -0400831
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500832 if(size == 0)
833 {
834 error(line, "array size must be greater than zero", "");
835 return true;
836 }
837
Nicolas Capens0bac2852016-05-07 06:09:58 -0400838 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400839}
840
841//
842// See if this qualifier can be an array.
843//
844// Returns true if there is an error.
845//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400846bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400847{
Alexis Hetud4e2ba52016-12-01 17:02:33 -0500848 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConstExpr && mShaderVersion < 300)) {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400849 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
850 return true;
851 }
John Bauman66b8ab22014-05-06 15:57:45 -0400852
Nicolas Capens0bac2852016-05-07 06:09:58 -0400853 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400854}
855
856//
857// See if this type can be an array.
858//
859// Returns true if there is an error.
860//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400861bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400862{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400863 //
864 // Can the type be an array?
865 //
866 if (type.array) {
867 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
868 return true;
869 }
John Bauman66b8ab22014-05-06 15:57:45 -0400870
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500871 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
872 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section 4.3.4).
873 if(mShaderVersion >= 300 && type.type == EbtStruct && IsVarying(type.qualifier))
874 {
875 error(line, "cannot declare arrays of structs of this qualifier",
876 TType(type).getCompleteString().c_str());
877 return true;
878 }
879
Nicolas Capens0bac2852016-05-07 06:09:58 -0400880 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400881}
882
Alexis Hetufe1269e2015-06-16 12:43:32 -0400883bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400884{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400885 bool builtIn = false;
886 TSymbol* symbol = symbolTable.find(node->getSymbol(), mShaderVersion, &builtIn);
887 if (symbol == 0) {
888 error(line, " undeclared identifier", node->getSymbol().c_str());
889 return true;
890 }
891 TVariable* variable = static_cast<TVariable*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400892
Nicolas Capens0bac2852016-05-07 06:09:58 -0400893 type->setArrayInformationType(variable->getArrayInformationType());
894 variable->updateArrayInformationType(type);
John Bauman66b8ab22014-05-06 15:57:45 -0400895
Nicolas Capens0bac2852016-05-07 06:09:58 -0400896 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
897 // its an error
898 if (node->getSymbol() == "gl_FragData") {
899 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", mShaderVersion, &builtIn);
900 ASSERT(fragData);
John Bauman66b8ab22014-05-06 15:57:45 -0400901
Nicolas Capens0bac2852016-05-07 06:09:58 -0400902 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
903 if (fragDataValue <= size) {
904 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
905 return true;
906 }
907 }
John Bauman66b8ab22014-05-06 15:57:45 -0400908
Nicolas Capens0bac2852016-05-07 06:09:58 -0400909 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
910 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
911 if (!updateFlag)
912 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400913
Nicolas Capens0bac2852016-05-07 06:09:58 -0400914 size++;
915 variable->getType().setMaxArraySize(size);
916 type->setMaxArraySize(size);
917 TType* tt = type;
John Bauman66b8ab22014-05-06 15:57:45 -0400918
Nicolas Capens0bac2852016-05-07 06:09:58 -0400919 while(tt->getArrayInformationType() != 0) {
920 tt = tt->getArrayInformationType();
921 tt->setMaxArraySize(size);
922 }
John Bauman66b8ab22014-05-06 15:57:45 -0400923
Nicolas Capens0bac2852016-05-07 06:09:58 -0400924 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400925}
926
927//
928// Enforce non-initializer type/qualifier rules.
929//
930// Returns true if there was an error.
931//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400932bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, TString& identifier, TPublicType& type, bool array)
John Bauman66b8ab22014-05-06 15:57:45 -0400933{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400934 if (type.qualifier == EvqConstExpr)
935 {
936 // Make the qualifier make sense.
937 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400938
Nicolas Capens0bac2852016-05-07 06:09:58 -0400939 if (array)
940 {
941 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
942 }
943 else if (type.isStructureContainingArrays())
944 {
945 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
946 }
947 else
948 {
949 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
950 }
John Bauman66b8ab22014-05-06 15:57:45 -0400951
Nicolas Capens0bac2852016-05-07 06:09:58 -0400952 return true;
953 }
John Bauman66b8ab22014-05-06 15:57:45 -0400954
Nicolas Capens0bac2852016-05-07 06:09:58 -0400955 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400956}
957
958//
959// Do semantic checking for a variable declaration that has no initializer,
960// and update the symbol table.
961//
962// Returns true if there was an error.
963//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400964bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString& identifier, TPublicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400965{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400966 if(type.qualifier == EvqConstExpr)
967 {
968 // Make the qualifier make sense.
969 type.qualifier = EvqTemporary;
John Bauman66b8ab22014-05-06 15:57:45 -0400970
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400971 // Generate informative error messages for ESSL1.
972 // In ESSL3 arrays and structures containing arrays can be constant.
Alexis Hetu0a655842015-06-22 16:52:11 -0400973 if(mShaderVersion < 300 && type.isStructureContainingArrays())
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400974 {
975 error(line,
976 "structures containing arrays may not be declared constant since they cannot be initialized",
977 identifier.c_str());
978 }
979 else
980 {
981 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
982 }
John Bauman66b8ab22014-05-06 15:57:45 -0400983
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400984 return true;
985 }
986 if(type.isUnsizedArray())
987 {
988 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
989 return true;
990 }
991 return false;
992}
John Bauman66b8ab22014-05-06 15:57:45 -0400993
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400994// Do some simple checks that are shared between all variable declarations,
995// and update the symbol table.
996//
997// Returns true if declaring the variable succeeded.
998//
999bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
1000 TVariable **variable)
1001{
1002 ASSERT((*variable) == nullptr);
John Bauman66b8ab22014-05-06 15:57:45 -04001003
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001004 // gl_LastFragData may be redeclared with a new precision qualifier
1005 if(type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1006 {
1007 const TVariable *maxDrawBuffers =
Alexis Hetu0a655842015-06-22 16:52:11 -04001008 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001009 if(type.getArraySize() != maxDrawBuffers->getConstPointer()->getIConst())
1010 {
1011 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
1012 return false;
1013 }
1014 }
1015
1016 if(reservedErrorCheck(line, identifier))
1017 return false;
1018
1019 (*variable) = new TVariable(&identifier, type);
1020 if(!symbolTable.declare(**variable))
1021 {
1022 error(line, "redefinition", identifier.c_str());
1023 delete (*variable);
1024 (*variable) = nullptr;
1025 return false;
1026 }
1027
1028 if(voidErrorCheck(line, identifier, type.getBasicType()))
1029 return false;
1030
1031 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001032}
1033
Alexis Hetufe1269e2015-06-16 12:43:32 -04001034bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001035{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001036 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
1037 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
1038 return true;
1039 }
1040 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
1041 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
1042 return true;
1043 }
John Bauman66b8ab22014-05-06 15:57:45 -04001044
Nicolas Capens0bac2852016-05-07 06:09:58 -04001045 if (qualifier == EvqConstReadOnly)
1046 type->setQualifier(EvqConstReadOnly);
1047 else
1048 type->setQualifier(paramQualifier);
John Bauman66b8ab22014-05-06 15:57:45 -04001049
Nicolas Capens0bac2852016-05-07 06:09:58 -04001050 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001051}
1052
Alexis Hetufe1269e2015-06-16 12:43:32 -04001053bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString& extension)
John Bauman66b8ab22014-05-06 15:57:45 -04001054{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001055 const TExtensionBehavior& extBehavior = extensionBehavior();
1056 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
1057 if (iter == extBehavior.end()) {
1058 error(line, "extension", extension.c_str(), "is not supported");
1059 return true;
1060 }
1061 // In GLSL ES, an extension's default behavior is "disable".
1062 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
1063 error(line, "extension", extension.c_str(), "is disabled");
1064 return true;
1065 }
1066 if (iter->second == EBhWarn) {
1067 warning(line, "extension", extension.c_str(), "is being used");
1068 return false;
1069 }
John Bauman66b8ab22014-05-06 15:57:45 -04001070
Nicolas Capens0bac2852016-05-07 06:09:58 -04001071 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001072}
1073
Alexis Hetuad6b8752015-06-09 16:15:30 -04001074bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1075{
1076 for(size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1077 {
1078 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1079 if(qual == EvqOut || qual == EvqInOut)
1080 {
1081 TIntermTyped *node = (aggregate->getSequence())[i]->getAsTyped();
1082 if(lValueErrorCheck(node->getLine(), "assign", node))
1083 {
1084 error(node->getLine(),
1085 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1086 recover();
1087 return true;
1088 }
1089 }
1090 }
1091 return false;
1092}
1093
Alexis Hetuad527752015-07-07 13:31:44 -04001094void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1095{
1096 switch(qualifier)
1097 {
1098 case EvqVaryingOut:
1099 case EvqSmoothOut:
1100 case EvqFlatOut:
1101 case EvqCentroidOut:
1102 case EvqVertexOut:
1103 case EvqFragmentOut:
1104 break;
1105 default:
1106 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1107 recover();
1108 break;
1109 }
1110}
1111
John Bauman66b8ab22014-05-06 15:57:45 -04001112bool TParseContext::supportsExtension(const char* extension)
1113{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001114 const TExtensionBehavior& extbehavior = extensionBehavior();
1115 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1116 return (iter != extbehavior.end());
John Bauman66b8ab22014-05-06 15:57:45 -04001117}
1118
Alexis Hetufe1269e2015-06-16 12:43:32 -04001119void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001120{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001121 pp::SourceLocation loc(line.first_file, line.first_line);
1122 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001123}
1124
Alexis Hetue13238e2017-12-15 18:01:07 -05001125void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value, bool stdgl)
John Bauman66b8ab22014-05-06 15:57:45 -04001126{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001127 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetue13238e2017-12-15 18:01:07 -05001128 mDirectiveHandler.handlePragma(loc, name, value, stdgl);
John Bauman66b8ab22014-05-06 15:57:45 -04001129}
1130
1131/////////////////////////////////////////////////////////////////////////////////
1132//
1133// Non-Errors.
1134//
1135/////////////////////////////////////////////////////////////////////////////////
1136
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001137const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1138 const TString *name,
1139 const TSymbol *symbol)
1140{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001141 const TVariable *variable = nullptr;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001142
1143 if(!symbol)
1144 {
1145 error(location, "undeclared identifier", name->c_str());
1146 recover();
1147 }
1148 else if(!symbol->isVariable())
1149 {
1150 error(location, "variable expected", name->c_str());
1151 recover();
1152 }
1153 else
1154 {
1155 variable = static_cast<const TVariable*>(symbol);
1156
Alexis Hetu0a655842015-06-22 16:52:11 -04001157 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001158 {
1159 recover();
1160 }
1161
1162 // Reject shaders using both gl_FragData and gl_FragColor
1163 TQualifier qualifier = variable->getType().getQualifier();
1164 if(qualifier == EvqFragData)
1165 {
1166 mUsesFragData = true;
1167 }
1168 else if(qualifier == EvqFragColor)
1169 {
1170 mUsesFragColor = true;
1171 }
1172
1173 // This validation is not quite correct - it's only an error to write to
1174 // both FragData and FragColor. For simplicity, and because users shouldn't
Nicolas Capens8b124c12016-04-18 14:09:37 -04001175 // be rewarded for reading from undefined variables, return an error
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001176 // if they are both referenced, rather than assigned.
1177 if(mUsesFragData && mUsesFragColor)
1178 {
1179 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1180 recover();
1181 }
1182 }
1183
1184 if(!variable)
1185 {
1186 TType type(EbtFloat, EbpUndefined);
1187 TVariable *fakeVariable = new TVariable(name, type);
1188 symbolTable.declare(*fakeVariable);
1189 variable = fakeVariable;
1190 }
1191
1192 return variable;
1193}
1194
John Bauman66b8ab22014-05-06 15:57:45 -04001195//
1196// Look up a function name in the symbol table, and make sure it is a function.
1197//
1198// Return the function symbol if found, otherwise 0.
1199//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001200const TFunction* TParseContext::findFunction(const TSourceLoc &line, TFunction* call, bool *builtIn)
John Bauman66b8ab22014-05-06 15:57:45 -04001201{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001202 // First find by unmangled name to check whether the function name has been
1203 // hidden by a variable name or struct typename.
1204 const TSymbol* symbol = symbolTable.find(call->getName(), mShaderVersion, builtIn);
1205 if (symbol == 0) {
1206 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
1207 }
John Bauman66b8ab22014-05-06 15:57:45 -04001208
Nicolas Capens0bac2852016-05-07 06:09:58 -04001209 if (symbol == 0) {
1210 error(line, "no matching overloaded function found", call->getName().c_str());
Alexis Hetuf0005a12016-09-28 15:52:21 -04001211 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001212 }
John Bauman66b8ab22014-05-06 15:57:45 -04001213
Nicolas Capens0bac2852016-05-07 06:09:58 -04001214 if (!symbol->isFunction()) {
1215 error(line, "function name expected", call->getName().c_str());
Alexis Hetuf0005a12016-09-28 15:52:21 -04001216 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001217 }
John Bauman66b8ab22014-05-06 15:57:45 -04001218
Nicolas Capens0bac2852016-05-07 06:09:58 -04001219 return static_cast<const TFunction*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -04001220}
1221
1222//
1223// Initializers show up in several places in the grammar. Have one set of
1224// code to handle them here.
1225//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001226bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, const TPublicType& pType,
Nicolas Capens0bac2852016-05-07 06:09:58 -04001227 TIntermTyped *initializer, TIntermNode **intermNode)
John Bauman66b8ab22014-05-06 15:57:45 -04001228{
Alexis Hetue5246692015-06-18 12:34:52 -04001229 ASSERT(intermNode != nullptr);
1230 TType type = TType(pType);
John Bauman66b8ab22014-05-06 15:57:45 -04001231
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001232 if(type.isUnsizedArray())
Alexis Hetue5246692015-06-18 12:34:52 -04001233 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001234 // We have not checked yet whether the initializer actually is an array or not.
1235 if(initializer->isArray())
1236 {
1237 type.setArraySize(initializer->getArraySize());
1238 }
1239 else
1240 {
1241 // Having a non-array initializer for an unsized array will result in an error later,
1242 // so we don't generate an error message here.
1243 type.setArraySize(1u);
1244 }
Alexis Hetue5246692015-06-18 12:34:52 -04001245 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001246
1247 TVariable *variable = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001248 if(!declareVariable(line, identifier, type, &variable))
1249 {
1250 return true;
1251 }
John Bauman66b8ab22014-05-06 15:57:45 -04001252
Nicolas Capensbf1307b2016-04-07 01:03:14 -04001253 if(symbolTable.atGlobalLevel() && initializer->getQualifier() != EvqConstExpr)
Alexis Hetue5246692015-06-18 12:34:52 -04001254 {
Alexis Hetue5246692015-06-18 12:34:52 -04001255 error(line, "global variable initializers must be constant expressions", "=");
1256 return true;
1257 }
John Bauman66b8ab22014-05-06 15:57:45 -04001258
Nicolas Capens0bac2852016-05-07 06:09:58 -04001259 //
1260 // identifier must be of type constant, a global, or a temporary
1261 //
1262 TQualifier qualifier = type.getQualifier();
1263 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
1264 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1265 return true;
1266 }
1267 //
1268 // test for and propagate constant
1269 //
John Bauman66b8ab22014-05-06 15:57:45 -04001270
Nicolas Capens0bac2852016-05-07 06:09:58 -04001271 if (qualifier == EvqConstExpr) {
1272 if (qualifier != initializer->getQualifier()) {
1273 std::stringstream extraInfoStream;
1274 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1275 std::string extraInfo = extraInfoStream.str();
1276 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1277 variable->getType().setQualifier(EvqTemporary);
1278 return true;
1279 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001280
Nicolas Capens0bac2852016-05-07 06:09:58 -04001281 if (type != initializer->getType()) {
1282 error(line, " non-matching types for const initializer ",
1283 variable->getType().getQualifierString());
1284 variable->getType().setQualifier(EvqTemporary);
1285 return true;
1286 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001287
Nicolas Capens0bac2852016-05-07 06:09:58 -04001288 if (initializer->getAsConstantUnion()) {
1289 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1290 } else if (initializer->getAsSymbolNode()) {
1291 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1292 const TVariable* tVar = static_cast<const TVariable*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -04001293
Nicolas Capens0bac2852016-05-07 06:09:58 -04001294 ConstantUnion* constArray = tVar->getConstPointer();
1295 variable->shareConstPointer(constArray);
1296 }
1297 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001298
Nicolas Capens0bac2852016-05-07 06:09:58 -04001299 if (!variable->isConstant()) {
1300 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1301 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1302 if(*intermNode == nullptr) {
1303 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1304 return true;
1305 }
1306 } else
1307 *intermNode = nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04001308
Nicolas Capens0bac2852016-05-07 06:09:58 -04001309 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001310}
1311
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001312TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1313{
1314 TPublicType returnType = typeSpecifier;
1315 returnType.qualifier = qualifier;
1316 returnType.invariant = invariant;
1317 returnType.layoutQualifier = layoutQualifier;
1318
Alexis Hetu0a655842015-06-22 16:52:11 -04001319 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001320 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001321 if(typeSpecifier.array)
1322 {
1323 error(typeSpecifier.line, "not supported", "first-class array");
1324 returnType.clearArrayness();
1325 }
1326
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001327 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1328 {
1329 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1330 recover();
1331 }
1332
1333 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1334 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1335 {
1336 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1337 recover();
1338 }
1339 }
1340 else
1341 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001342 if(!returnType.layoutQualifier.isEmpty())
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001343 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001344 globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout");
1345 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001346
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001347 if(IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn || returnType.qualifier == EvqFragmentOut)
1348 {
1349 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier, typeSpecifier.line);
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001350 }
1351 }
1352
1353 return returnType;
1354}
1355
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001356void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1357 const TPublicType &type,
1358 const TSourceLoc &qualifierLocation)
1359{
1360 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1361 if(type.type == EbtBool)
1362 {
1363 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1364 }
1365
1366 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1367 switch(qualifier)
1368 {
1369 case EvqVertexIn:
1370 // ESSL 3.00 section 4.3.4
1371 if(type.array)
1372 {
1373 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1374 }
1375 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1376 return;
1377 case EvqFragmentOut:
1378 // ESSL 3.00 section 4.3.6
1379 if(type.isMatrix())
1380 {
1381 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1382 }
1383 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1384 return;
1385 default:
1386 break;
1387 }
1388
1389 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1390 // restrictions.
1391 bool typeContainsIntegers = (type.type == EbtInt || type.type == EbtUInt ||
1392 type.isStructureContainingType(EbtInt) ||
1393 type.isStructureContainingType(EbtUInt));
1394 if(typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1395 {
1396 error(qualifierLocation, "must use 'flat' interpolation here", getQualifierString(qualifier));
1397 }
1398
1399 if(type.type == EbtStruct)
1400 {
1401 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1402 // These restrictions are only implied by the ESSL 3.00 spec, but
1403 // the ESSL 3.10 spec lists these restrictions explicitly.
1404 if(type.array)
1405 {
1406 error(qualifierLocation, "cannot be an array of structures", getQualifierString(qualifier));
1407 }
1408 if(type.isStructureContainingArrays())
1409 {
1410 error(qualifierLocation, "cannot be a structure containing an array", getQualifierString(qualifier));
1411 }
1412 if(type.isStructureContainingType(EbtStruct))
1413 {
1414 error(qualifierLocation, "cannot be a structure containing a structure", getQualifierString(qualifier));
1415 }
1416 if(type.isStructureContainingType(EbtBool))
1417 {
1418 error(qualifierLocation, "cannot be a structure containing a bool", getQualifierString(qualifier));
1419 }
1420 }
1421}
1422
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001423TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1424 const TSourceLoc &identifierOrTypeLocation,
1425 const TString &identifier)
1426{
1427 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1428
1429 bool emptyDeclaration = (identifier == "");
1430
1431 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1432
1433 if(emptyDeclaration)
1434 {
1435 if(publicType.isUnsizedArray())
1436 {
1437 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1438 // It is assumed that this applies to empty declarations as well.
1439 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1440 }
1441 }
1442 else
1443 {
1444 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1445 recover();
1446
1447 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1448 recover();
1449
1450 TVariable *variable = nullptr;
1451 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1452 recover();
1453
1454 if(variable && symbol)
1455 symbol->setId(variable->getUniqueId());
1456 }
1457
1458 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1459}
1460
1461TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1462 const TSourceLoc &identifierLocation,
1463 const TString &identifier,
1464 const TSourceLoc &indexLocation,
1465 TIntermTyped *indexExpression)
1466{
1467 mDeferredSingleDeclarationErrorCheck = false;
1468
1469 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1470 recover();
1471
1472 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1473 recover();
1474
1475 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1476 {
1477 recover();
1478 }
1479
1480 TType arrayType(publicType);
1481
1482 int size;
1483 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1484 {
1485 recover();
1486 }
1487 // Make the type an array even if size check failed.
1488 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1489 arrayType.setArraySize(size);
1490
1491 TVariable *variable = nullptr;
1492 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1493 recover();
1494
1495 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1496 if(variable && symbol)
1497 symbol->setId(variable->getUniqueId());
1498
1499 return intermediate.makeAggregate(symbol, identifierLocation);
1500}
1501
1502TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1503 const TSourceLoc &identifierLocation,
1504 const TString &identifier,
1505 const TSourceLoc &initLocation,
1506 TIntermTyped *initializer)
1507{
1508 mDeferredSingleDeclarationErrorCheck = false;
1509
1510 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1511 recover();
1512
1513 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001514 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001515 {
1516 //
1517 // Build intermediate representation
1518 //
1519 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1520 }
1521 else
1522 {
1523 recover();
1524 return nullptr;
1525 }
1526}
1527
1528TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1529 const TSourceLoc &identifierLocation,
1530 const TString &identifier,
1531 const TSourceLoc &indexLocation,
1532 TIntermTyped *indexExpression,
1533 const TSourceLoc &initLocation,
1534 TIntermTyped *initializer)
1535{
1536 mDeferredSingleDeclarationErrorCheck = false;
1537
1538 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1539 recover();
1540
1541 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1542 {
1543 recover();
1544 }
1545
1546 TPublicType arrayType(publicType);
1547
1548 int size = 0;
1549 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1550 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1551 {
1552 recover();
1553 }
1554 // Make the type an array even if size check failed.
1555 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1556 arrayType.setArray(true, size);
1557
1558 // initNode will correspond to the whole of "type b[n] = initializer".
1559 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001560 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001561 {
1562 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1563 }
1564 else
1565 {
1566 recover();
1567 return nullptr;
1568 }
1569}
1570
1571TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1572 const TSourceLoc &identifierLoc,
1573 const TString *identifier,
1574 const TSymbol *symbol)
1575{
1576 // invariant declaration
1577 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1578 {
1579 recover();
1580 }
1581
1582 if(!symbol)
1583 {
1584 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1585 recover();
1586 return nullptr;
1587 }
1588 else
1589 {
1590 const TString kGlFrontFacing("gl_FrontFacing");
1591 if(*identifier == kGlFrontFacing)
1592 {
1593 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1594 recover();
1595 return nullptr;
1596 }
1597 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1598 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1599 ASSERT(variable);
1600 const TType &type = variable->getType();
1601 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1602 *identifier, type, identifierLoc);
1603
1604 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1605 aggregate->setOp(EOpInvariantDeclaration);
1606 return aggregate;
1607 }
1608}
1609
1610TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1611 const TSourceLoc &identifierLocation, const TString &identifier)
1612{
1613 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1614 if(mDeferredSingleDeclarationErrorCheck)
1615 {
1616 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1617 recover();
1618 mDeferredSingleDeclarationErrorCheck = false;
1619 }
1620
1621 if(locationDeclaratorListCheck(identifierLocation, publicType))
1622 recover();
1623
1624 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1625 recover();
1626
1627 TVariable *variable = nullptr;
1628 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1629 recover();
1630
1631 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1632 if(variable && symbol)
1633 symbol->setId(variable->getUniqueId());
1634
1635 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1636}
1637
1638TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1639 const TSourceLoc &identifierLocation, const TString &identifier,
1640 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1641{
1642 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1643 if(mDeferredSingleDeclarationErrorCheck)
1644 {
1645 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1646 recover();
1647 mDeferredSingleDeclarationErrorCheck = false;
1648 }
1649
1650 if(locationDeclaratorListCheck(identifierLocation, publicType))
1651 recover();
1652
1653 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1654 recover();
1655
1656 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1657 {
1658 recover();
1659 }
1660 else
1661 {
1662 TType arrayType = TType(publicType);
1663 int size;
1664 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1665 {
1666 recover();
1667 }
1668 arrayType.setArraySize(size);
1669
1670 TVariable *variable = nullptr;
1671 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1672 recover();
1673
1674 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1675 if(variable && symbol)
1676 symbol->setId(variable->getUniqueId());
1677
1678 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1679 }
1680
1681 return nullptr;
1682}
1683
1684TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1685 const TSourceLoc &identifierLocation, const TString &identifier,
1686 const TSourceLoc &initLocation, TIntermTyped *initializer)
1687{
1688 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1689 if(mDeferredSingleDeclarationErrorCheck)
1690 {
1691 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1692 recover();
1693 mDeferredSingleDeclarationErrorCheck = false;
1694 }
1695
1696 if(locationDeclaratorListCheck(identifierLocation, publicType))
1697 recover();
1698
1699 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001700 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001701 {
1702 //
1703 // build the intermediate representation
1704 //
1705 if(intermNode)
1706 {
1707 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1708 }
1709 else
1710 {
1711 return aggregateDeclaration;
1712 }
1713 }
1714 else
1715 {
1716 recover();
1717 return nullptr;
1718 }
1719}
1720
1721TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1722 TIntermAggregate *aggregateDeclaration,
1723 const TSourceLoc &identifierLocation,
1724 const TString &identifier,
1725 const TSourceLoc &indexLocation,
1726 TIntermTyped *indexExpression,
1727 const TSourceLoc &initLocation, TIntermTyped *initializer)
1728{
1729 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1730 if(mDeferredSingleDeclarationErrorCheck)
1731 {
1732 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1733 recover();
1734 mDeferredSingleDeclarationErrorCheck = false;
1735 }
1736
1737 if(locationDeclaratorListCheck(identifierLocation, publicType))
1738 recover();
1739
1740 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1741 {
1742 recover();
1743 }
1744
1745 TPublicType arrayType(publicType);
1746
1747 int size = 0;
1748 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1749 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1750 {
1751 recover();
1752 }
1753 // Make the type an array even if size check failed.
1754 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1755 arrayType.setArray(true, size);
1756
1757 // initNode will correspond to the whole of "b[n] = initializer".
1758 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001759 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001760 {
1761 if(initNode)
1762 {
1763 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1764 }
1765 else
1766 {
1767 return aggregateDeclaration;
1768 }
1769 }
1770 else
1771 {
1772 recover();
1773 return nullptr;
1774 }
1775}
1776
Alexis Hetua35d8232015-06-11 17:11:06 -04001777void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1778{
Alexis Hetu0a655842015-06-22 16:52:11 -04001779 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001780 {
1781 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1782 recover();
1783 return;
1784 }
1785
1786 if(typeQualifier.qualifier != EvqUniform)
1787 {
1788 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1789 recover();
1790 return;
1791 }
1792
1793 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1794 ASSERT(!layoutQualifier.isEmpty());
1795
1796 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1797 {
1798 recover();
1799 return;
1800 }
1801
1802 if(layoutQualifier.matrixPacking != EmpUnspecified)
1803 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001804 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001805 }
1806
1807 if(layoutQualifier.blockStorage != EbsUnspecified)
1808 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001809 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001810 }
1811}
1812
Alexis Hetu407813b2016-02-24 16:46:13 -05001813TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1814{
1815 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1816 // Either way the instance in the symbol table is used to track whether the function is declared
1817 // multiple times.
1818 TFunction *symbolTableFunction =
1819 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1820 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1821 {
1822 // ESSL 1.00.17 section 4.2.7.
1823 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1824 error(location, "duplicate function prototype declarations are not allowed", "function");
1825 recover();
1826 }
1827 symbolTableFunction->setHasPrototypeDeclaration();
1828
1829 TIntermAggregate *prototype = new TIntermAggregate;
1830 prototype->setType(function.getReturnType());
1831 prototype->setName(function.getMangledName());
1832
1833 for(size_t i = 0; i < function.getParamCount(); i++)
1834 {
1835 const TParameter &param = function.getParam(i);
1836 if(param.name != 0)
1837 {
1838 TVariable variable(param.name, *param.type);
1839
1840 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1841 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1842 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1843 }
1844 else
1845 {
1846 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1847 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1848 }
1849 }
1850
1851 prototype->setOp(EOpPrototype);
1852
1853 symbolTable.pop();
1854
1855 if(!symbolTable.atGlobalLevel())
1856 {
1857 // ESSL 3.00.4 section 4.2.4.
1858 error(location, "local function prototype declarations are not allowed", "function");
1859 recover();
1860 }
1861
1862 return prototype;
1863}
1864
1865TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1866{
1867 //?? Check that all paths return a value if return type != void ?
1868 // May be best done as post process phase on intermediate code
1869 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1870 {
1871 error(location, "function does not return a value:", "", function.getName().c_str());
1872 recover();
1873 }
1874
1875 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1876 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1877 aggregate->setName(function.getMangledName().c_str());
1878 aggregate->setType(function.getReturnType());
1879
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001880 // store the pragma information for debug and optimize and other vendor specific
1881 // information. This information can be queried from the parse tree
1882 aggregate->setOptimize(pragma().optimize);
Alexis Hetu407813b2016-02-24 16:46:13 -05001883 aggregate->setDebug(pragma().debug);
1884
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001885 if(functionBody && functionBody->getAsAggregate())
Alexis Hetu407813b2016-02-24 16:46:13 -05001886 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1887
1888 symbolTable.pop();
1889 return aggregate;
1890}
1891
1892void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1893{
1894 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1895
1896 if(builtIn)
1897 {
1898 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1899 recover();
1900 }
1901
1902 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1903 //
1904 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1905 // as it would have just been put in the symbol table. Otherwise, we're looking up
1906 // an earlier occurance.
1907 //
1908 if(prevDec->isDefined())
1909 {
1910 // Then this function already has a body.
1911 error(location, "function already has a body", function->getName().c_str());
1912 recover();
1913 }
1914 prevDec->setDefined();
1915 //
1916 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1917 // Eventually we will probably want to have only a single definition and just swap the
1918 // arguments to be the definition's arguments.
1919 //
1920 function->setUniqueId(prevDec->getUniqueId());
1921
1922 // Raise error message if main function takes any parameters or return anything other than void
1923 if(function->getName() == "main")
1924 {
1925 if(function->getParamCount() > 0)
1926 {
1927 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1928 recover();
1929 }
1930 if(function->getReturnType().getBasicType() != EbtVoid)
1931 {
1932 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1933 recover();
1934 }
1935 }
1936
1937 //
1938 // Remember the return type for later checking for RETURN statements.
1939 //
1940 mCurrentFunctionType = &(prevDec->getReturnType());
1941 mFunctionReturnsValue = false;
1942
1943 //
1944 // Insert parameters into the symbol table.
1945 // If the parameter has no name, it's not an error, just don't insert it
1946 // (could be used for unused args).
1947 //
1948 // Also, accumulate the list of parameters into the HIL, so lower level code
1949 // knows where to find parameters.
1950 //
1951 TIntermAggregate *paramNodes = new TIntermAggregate;
1952 for(size_t i = 0; i < function->getParamCount(); i++)
1953 {
1954 const TParameter &param = function->getParam(i);
1955 if(param.name != 0)
1956 {
1957 TVariable *variable = new TVariable(param.name, *param.type);
1958 //
1959 // Insert the parameters with name in the symbol table.
1960 //
1961 if(!symbolTable.declare(*variable))
1962 {
1963 error(location, "redefinition", variable->getName().c_str());
1964 recover();
1965 paramNodes = intermediate.growAggregate(
1966 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1967 continue;
1968 }
1969
1970 //
1971 // Add the parameter to the HIL
1972 //
1973 TIntermSymbol *symbol = intermediate.addSymbol(
1974 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1975
1976 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1977 }
1978 else
1979 {
1980 paramNodes = intermediate.growAggregate(
1981 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1982 }
1983 }
1984 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1985 *aggregateOut = paramNodes;
1986 setLoopNestingLevel(0);
1987}
1988
1989TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1990{
1991 //
1992 // We don't know at this point whether this is a function definition or a prototype.
1993 // The definition production code will check for redefinitions.
1994 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
1995 //
1996 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
1997 // here.
1998 //
1999 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002000 if(getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2001 {
2002 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2003 // Therefore overloading or redefining builtin functions is an error.
2004 error(location, "Name of a built-in function cannot be redeclared as function", function->getName().c_str());
2005 }
2006 else if(prevDec)
Alexis Hetu407813b2016-02-24 16:46:13 -05002007 {
2008 if(prevDec->getReturnType() != function->getReturnType())
2009 {
2010 error(location, "overloaded functions must have the same return type",
2011 function->getReturnType().getBasicString());
2012 recover();
2013 }
2014 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
2015 {
2016 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
2017 {
2018 error(location, "overloaded functions must have the same parameter qualifiers",
2019 function->getParam(i).type->getQualifierString());
2020 recover();
2021 }
2022 }
2023 }
2024
2025 //
2026 // Check for previously declared variables using the same name.
2027 //
2028 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
2029 if(prevSym)
2030 {
2031 if(!prevSym->isFunction())
2032 {
2033 error(location, "redefinition", function->getName().c_str(), "function");
2034 recover();
2035 }
2036 }
2037
2038 // We're at the inner scope level of the function's arguments and body statement.
2039 // Add the function prototype to the surrounding scope instead.
2040 symbolTable.getOuterLevel()->insert(*function);
2041
2042 //
2043 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2044 // variable names from this one, and not the one that's
2045 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2046 //
2047 return function;
2048}
2049
Alexis Hetue5246692015-06-18 12:34:52 -04002050TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
2051{
2052 TPublicType publicType = publicTypeIn;
2053 TOperator op = EOpNull;
2054 if(publicType.userDef)
2055 {
2056 op = EOpConstructStruct;
2057 }
2058 else
2059 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002060 op = TypeToConstructorOperator(TType(publicType));
Alexis Hetue5246692015-06-18 12:34:52 -04002061 if(op == EOpNull)
2062 {
2063 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2064 recover();
2065 publicType.type = EbtFloat;
2066 op = EOpConstructFloat;
2067 }
2068 }
2069
2070 TString tempString;
2071 TType type(publicType);
2072 return new TFunction(&tempString, type, op);
2073}
2074
John Bauman66b8ab22014-05-06 15:57:45 -04002075// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002076// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002077//
2078// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2079//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002080TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002081{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002082 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002083
Nicolas Capens0bac2852016-05-07 06:09:58 -04002084 if(!aggregateArguments)
2085 {
2086 aggregateArguments = new TIntermAggregate;
2087 aggregateArguments->getSequence().push_back(arguments);
2088 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002089
Alexis Hetu2a198552016-09-27 20:50:45 -04002090 if(type->isArray())
2091 {
2092 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2093 // the array.
2094 for(TIntermNode *&argNode : aggregateArguments->getSequence())
2095 {
2096 const TType &argType = argNode->getAsTyped()->getType();
2097 // It has already been checked that the argument is not an array.
2098 ASSERT(!argType.isArray());
2099 if(!argType.sameElementType(*type))
2100 {
2101 error(line, "Array constructor argument has an incorrect type", "Error");
Alexis Hetuf0005a12016-09-28 15:52:21 -04002102 return nullptr;
Alexis Hetu2a198552016-09-27 20:50:45 -04002103 }
2104 }
2105 }
2106 else if(op == EOpConstructStruct)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002107 {
2108 const TFieldList &fields = type->getStruct()->fields();
2109 TIntermSequence &args = aggregateArguments->getSequence();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002110
Nicolas Capens0bac2852016-05-07 06:09:58 -04002111 for(size_t i = 0; i < fields.size(); i++)
2112 {
2113 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
2114 {
2115 error(line, "Structure constructor arguments do not match structure fields", "Error");
2116 recover();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002117
Alexis Hetuf0005a12016-09-28 15:52:21 -04002118 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002119 }
2120 }
2121 }
John Bauman66b8ab22014-05-06 15:57:45 -04002122
Nicolas Capens0bac2852016-05-07 06:09:58 -04002123 // Turn the argument list itself into a constructor
2124 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2125 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
2126 if(constConstructor)
2127 {
2128 return constConstructor;
2129 }
John Bauman66b8ab22014-05-06 15:57:45 -04002130
Nicolas Capens0bac2852016-05-07 06:09:58 -04002131 return constructor;
John Bauman66b8ab22014-05-06 15:57:45 -04002132}
2133
2134TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2135{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002136 aggrNode->setType(type);
2137 if (aggrNode->isConstantFoldable()) {
2138 bool returnVal = false;
2139 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2140 if (aggrNode->getSequence().size() == 1) {
2141 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
2142 }
2143 else {
2144 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
2145 }
2146 if (returnVal)
Alexis Hetuf0005a12016-09-28 15:52:21 -04002147 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002148
Nicolas Capens0bac2852016-05-07 06:09:58 -04002149 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2150 }
John Bauman66b8ab22014-05-06 15:57:45 -04002151
Alexis Hetuf0005a12016-09-28 15:52:21 -04002152 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002153}
2154
John Bauman66b8ab22014-05-06 15:57:45 -04002155//
2156// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2157// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2158// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002159// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
John Bauman66b8ab22014-05-06 15:57:45 -04002160// a constant matrix.
2161//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002162TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002163{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002164 TIntermTyped* typedNode;
2165 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002166
Nicolas Capens0bac2852016-05-07 06:09:58 -04002167 ConstantUnion *unionArray;
2168 if (tempConstantNode) {
2169 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002170
Nicolas Capens0bac2852016-05-07 06:09:58 -04002171 if (!unionArray) {
2172 return node;
2173 }
2174 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2175 error(line, "Cannot offset into the vector", "Error");
2176 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002177
Alexis Hetuf0005a12016-09-28 15:52:21 -04002178 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002179 }
John Bauman66b8ab22014-05-06 15:57:45 -04002180
Nicolas Capens0bac2852016-05-07 06:09:58 -04002181 ConstantUnion* constArray = new ConstantUnion[fields.num];
John Bauman66b8ab22014-05-06 15:57:45 -04002182
Alexis Hetub34591a2016-06-28 15:48:35 -04002183 int objSize = static_cast<int>(node->getType().getObjectSize());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002184 for (int i = 0; i < fields.num; i++) {
Alexis Hetub34591a2016-06-28 15:48:35 -04002185 if (fields.offsets[i] >= objSize) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002186 std::stringstream extraInfoStream;
2187 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2188 std::string extraInfo = extraInfoStream.str();
2189 error(line, "", "[", extraInfo.c_str());
2190 recover();
2191 fields.offsets[i] = 0;
2192 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002193
Nicolas Capens0bac2852016-05-07 06:09:58 -04002194 constArray[i] = unionArray[fields.offsets[i]];
John Bauman66b8ab22014-05-06 15:57:45 -04002195
Nicolas Capens0bac2852016-05-07 06:09:58 -04002196 }
2197 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2198 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002199}
2200
2201//
2202// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002203// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2204// 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 -04002205// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2206//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002207TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002208{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002209 TIntermTyped* typedNode;
2210 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002211
Nicolas Capens0bac2852016-05-07 06:09:58 -04002212 if (index >= node->getType().getNominalSize()) {
2213 std::stringstream extraInfoStream;
2214 extraInfoStream << "matrix field selection out of range '" << index << "'";
2215 std::string extraInfo = extraInfoStream.str();
2216 error(line, "", "[", extraInfo.c_str());
2217 recover();
2218 index = 0;
2219 }
John Bauman66b8ab22014-05-06 15:57:45 -04002220
Nicolas Capens0bac2852016-05-07 06:09:58 -04002221 if (tempConstantNode) {
2222 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2223 int size = tempConstantNode->getType().getNominalSize();
2224 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2225 } else {
2226 error(line, "Cannot offset into the matrix", "Error");
2227 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002228
Alexis Hetuf0005a12016-09-28 15:52:21 -04002229 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002230 }
John Bauman66b8ab22014-05-06 15:57:45 -04002231
Nicolas Capens0bac2852016-05-07 06:09:58 -04002232 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002233}
2234
2235
2236//
2237// 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 -04002238// the symbol table and parse-tree is built for the type of the element. The input
2239// 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 -04002240// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2241//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002242TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002243{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002244 TIntermTyped* typedNode;
2245 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2246 TType arrayElementType = node->getType();
2247 arrayElementType.clearArrayness();
John Bauman66b8ab22014-05-06 15:57:45 -04002248
Nicolas Capens0bac2852016-05-07 06:09:58 -04002249 if (index >= node->getType().getArraySize()) {
2250 std::stringstream extraInfoStream;
2251 extraInfoStream << "array field selection out of range '" << index << "'";
2252 std::string extraInfo = extraInfoStream.str();
2253 error(line, "", "[", extraInfo.c_str());
2254 recover();
2255 index = 0;
2256 }
John Bauman66b8ab22014-05-06 15:57:45 -04002257
Nicolas Capens0bac2852016-05-07 06:09:58 -04002258 size_t arrayElementSize = arrayElementType.getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002259
Nicolas Capens0bac2852016-05-07 06:09:58 -04002260 if (tempConstantNode) {
2261 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2262 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2263 } else {
2264 error(line, "Cannot offset into the array", "Error");
2265 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002266
Alexis Hetuf0005a12016-09-28 15:52:21 -04002267 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002268 }
John Bauman66b8ab22014-05-06 15:57:45 -04002269
Nicolas Capens0bac2852016-05-07 06:09:58 -04002270 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002271}
2272
2273
2274//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002275// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002276// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2277// function and returns the parse-tree with the values of the embedded/nested struct.
2278//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002279TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002280{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002281 const TFieldList &fields = node->getType().getStruct()->fields();
2282 TIntermTyped *typedNode;
2283 size_t instanceSize = 0;
2284 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002285
Nicolas Capens0bac2852016-05-07 06:09:58 -04002286 for(size_t index = 0; index < fields.size(); ++index) {
2287 if (fields[index]->name() == identifier) {
2288 break;
2289 } else {
2290 instanceSize += fields[index]->type()->getObjectSize();
2291 }
2292 }
John Bauman66b8ab22014-05-06 15:57:45 -04002293
Nicolas Capens0bac2852016-05-07 06:09:58 -04002294 if (tempConstantNode) {
2295 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002296
Nicolas Capens0bac2852016-05-07 06:09:58 -04002297 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2298 } else {
2299 error(line, "Cannot offset into the structure", "Error");
2300 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002301
Alexis Hetuf0005a12016-09-28 15:52:21 -04002302 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002303 }
John Bauman66b8ab22014-05-06 15:57:45 -04002304
Nicolas Capens0bac2852016-05-07 06:09:58 -04002305 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002306}
2307
Alexis Hetuad6b8752015-06-09 16:15:30 -04002308//
Alexis Hetua35d8232015-06-11 17:11:06 -04002309// Interface/uniform blocks
2310//
2311TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
Nicolas Capens0bac2852016-05-07 06:09:58 -04002312 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
Alexis Hetua35d8232015-06-11 17:11:06 -04002313{
2314 if(reservedErrorCheck(nameLine, blockName))
2315 recover();
2316
2317 if(typeQualifier.qualifier != EvqUniform)
2318 {
2319 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2320 recover();
2321 }
2322
2323 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2324 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2325 {
2326 recover();
2327 }
2328
2329 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2330 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002331 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002332 }
2333
2334 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2335 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002336 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002337 }
2338
2339 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2340 if(!symbolTable.declare(*blockNameSymbol)) {
2341 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2342 recover();
2343 }
2344
2345 // check for sampler types and apply layout qualifiers
2346 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2347 TField* field = (*fieldList)[memberIndex];
2348 TType* fieldType = field->type();
2349 if(IsSampler(fieldType->getBasicType())) {
2350 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2351 recover();
2352 }
2353
2354 const TQualifier qualifier = fieldType->getQualifier();
2355 switch(qualifier)
2356 {
2357 case EvqGlobal:
2358 case EvqUniform:
2359 break;
2360 default:
2361 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2362 recover();
2363 break;
2364 }
2365
2366 // check layout qualifiers
2367 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2368 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2369 {
2370 recover();
2371 }
2372
2373 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2374 {
2375 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2376 recover();
2377 }
2378
2379 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2380 {
2381 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2382 }
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002383 else if(!fieldType->isMatrix() && (fieldType->getBasicType() != EbtStruct))
Alexis Hetua35d8232015-06-11 17:11:06 -04002384 {
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002385 warning(field->line(), "extraneous layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Alexis Hetua35d8232015-06-11 17:11:06 -04002386 }
2387
2388 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2389 }
2390
2391 // add array index
2392 int arraySize = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002393 if(arrayIndex)
Alexis Hetua35d8232015-06-11 17:11:06 -04002394 {
2395 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2396 recover();
2397 }
2398
2399 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2400 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2401
2402 TString symbolName = "";
2403 int symbolId = 0;
2404
2405 if(!instanceName)
2406 {
2407 // define symbols for the members of the interface block
2408 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2409 {
2410 TField* field = (*fieldList)[memberIndex];
2411 TType* fieldType = field->type();
2412
2413 // set parent pointer of the field variable
2414 fieldType->setInterfaceBlock(interfaceBlock);
2415
2416 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2417 fieldVariable->setQualifier(typeQualifier.qualifier);
2418
2419 if(!symbolTable.declare(*fieldVariable)) {
2420 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2421 recover();
2422 }
2423 }
2424 }
2425 else
2426 {
2427 // add a symbol for this interface block
2428 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2429 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2430
2431 if(!symbolTable.declare(*instanceTypeDef)) {
2432 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2433 recover();
2434 }
2435
2436 symbolId = instanceTypeDef->getUniqueId();
2437 symbolName = instanceTypeDef->getName();
2438 }
2439
2440 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2441 aggregate->setOp(EOpDeclaration);
2442
2443 exitStructDeclaration();
2444 return aggregate;
2445}
2446
2447//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002448// Parse an array index expression
2449//
2450TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2451{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002452 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002453
2454 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2455 {
2456 if(baseExpression->getAsSymbolNode())
2457 {
2458 error(location, " left of '[' is not of type array, matrix, or vector ",
2459 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2460 }
2461 else
2462 {
2463 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2464 }
2465 recover();
2466 }
2467
2468 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2469
2470 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2471 {
2472 int index = indexConstantUnion->getIConst(0);
2473 if(index < 0)
2474 {
2475 std::stringstream infoStream;
2476 infoStream << index;
2477 std::string info = infoStream.str();
2478 error(location, "negative index", info.c_str());
2479 recover();
2480 index = 0;
2481 }
2482 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2483 {
2484 if(baseExpression->isArray())
2485 {
2486 // constant folding for arrays
2487 indexedExpression = addConstArrayNode(index, baseExpression, location);
2488 }
2489 else if(baseExpression->isVector())
2490 {
2491 // constant folding for vectors
2492 TVectorFields fields;
2493 fields.num = 1;
2494 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2495 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2496 }
2497 else if(baseExpression->isMatrix())
2498 {
2499 // constant folding for matrices
2500 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2501 }
2502 }
2503 else
2504 {
2505 int safeIndex = -1;
2506
2507 if(baseExpression->isArray())
2508 {
2509 if(index >= baseExpression->getType().getArraySize())
2510 {
2511 std::stringstream extraInfoStream;
2512 extraInfoStream << "array index out of range '" << index << "'";
2513 std::string extraInfo = extraInfoStream.str();
2514 error(location, "", "[", extraInfo.c_str());
2515 recover();
2516 safeIndex = baseExpression->getType().getArraySize() - 1;
2517 }
2518 }
2519 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2520 baseExpression->getType().getNominalSize() <= index)
2521 {
2522 std::stringstream extraInfoStream;
2523 extraInfoStream << "field selection out of range '" << index << "'";
2524 std::string extraInfo = extraInfoStream.str();
2525 error(location, "", "[", extraInfo.c_str());
2526 recover();
2527 safeIndex = baseExpression->getType().getNominalSize() - 1;
2528 }
2529
2530 // Don't modify the data of the previous constant union, because it can point
2531 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2532 if(safeIndex != -1)
2533 {
2534 ConstantUnion *safeConstantUnion = new ConstantUnion();
2535 safeConstantUnion->setIConst(safeIndex);
2536 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2537 }
2538
2539 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2540 }
2541 }
2542 else
2543 {
2544 if(baseExpression->isInterfaceBlock())
2545 {
2546 error(location, "",
2547 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2548 recover();
2549 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002550 else if(baseExpression->getQualifier() == EvqFragmentOut)
2551 {
2552 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2553 recover();
2554 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002555
2556 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2557 }
2558
2559 if(indexedExpression == 0)
2560 {
2561 ConstantUnion *unionArray = new ConstantUnion[1];
2562 unionArray->setFConst(0.0f);
2563 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2564 }
2565 else if(baseExpression->isArray())
2566 {
2567 const TType &baseType = baseExpression->getType();
2568 if(baseType.getStruct())
2569 {
2570 TType copyOfType(baseType.getStruct());
2571 indexedExpression->setType(copyOfType);
2572 }
2573 else if(baseType.isInterfaceBlock())
2574 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002575 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002576 indexedExpression->setType(copyOfType);
2577 }
2578 else
2579 {
2580 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2581 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2582 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2583 }
2584
2585 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2586 {
2587 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2588 }
2589 }
2590 else if(baseExpression->isMatrix())
2591 {
2592 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2593 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2594 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2595 }
2596 else if(baseExpression->isVector())
2597 {
2598 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2599 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2600 }
2601 else
2602 {
2603 indexedExpression->setType(baseExpression->getType());
2604 }
2605
2606 return indexedExpression;
2607}
2608
2609TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2610 const TString &fieldString, const TSourceLoc &fieldLocation)
2611{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002612 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002613
2614 if(baseExpression->isArray())
2615 {
2616 error(fieldLocation, "cannot apply dot operator to an array", ".");
2617 recover();
2618 }
2619
2620 if(baseExpression->isVector())
2621 {
2622 TVectorFields fields;
2623 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2624 {
2625 fields.num = 1;
2626 fields.offsets[0] = 0;
2627 recover();
2628 }
2629
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002630 if(baseExpression->getAsConstantUnion())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002631 {
2632 // constant folding for vector fields
2633 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2634 if(indexedExpression == 0)
2635 {
2636 recover();
2637 indexedExpression = baseExpression;
2638 }
2639 else
2640 {
2641 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2642 EvqConstExpr, (unsigned char)(fieldString).size()));
2643 }
2644 }
2645 else
2646 {
2647 TString vectorString = fieldString;
2648 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2649 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2650 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
Nicolas Capens341afbb2016-04-10 01:54:50 -04002651 baseExpression->getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary, (unsigned char)vectorString.size()));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002652 }
2653 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002654 else if(baseExpression->getBasicType() == EbtStruct)
2655 {
2656 bool fieldFound = false;
2657 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2658 if(fields.empty())
2659 {
2660 error(dotLocation, "structure has no fields", "Internal Error");
2661 recover();
2662 indexedExpression = baseExpression;
2663 }
2664 else
2665 {
2666 unsigned int i;
2667 for(i = 0; i < fields.size(); ++i)
2668 {
2669 if(fields[i]->name() == fieldString)
2670 {
2671 fieldFound = true;
2672 break;
2673 }
2674 }
2675 if(fieldFound)
2676 {
2677 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2678 {
2679 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2680 if(indexedExpression == 0)
2681 {
2682 recover();
2683 indexedExpression = baseExpression;
2684 }
2685 else
2686 {
2687 indexedExpression->setType(*fields[i]->type());
2688 // change the qualifier of the return type, not of the structure field
2689 // as the structure definition is shared between various structures.
2690 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2691 }
2692 }
2693 else
2694 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002695 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2696 index->setLine(fieldLocation);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002697 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2698 indexedExpression->setType(*fields[i]->type());
2699 }
2700 }
2701 else
2702 {
2703 error(dotLocation, " no such field in structure", fieldString.c_str());
2704 recover();
2705 indexedExpression = baseExpression;
2706 }
2707 }
2708 }
2709 else if(baseExpression->isInterfaceBlock())
2710 {
2711 bool fieldFound = false;
2712 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2713 if(fields.empty())
2714 {
2715 error(dotLocation, "interface block has no fields", "Internal Error");
2716 recover();
2717 indexedExpression = baseExpression;
2718 }
2719 else
2720 {
2721 unsigned int i;
2722 for(i = 0; i < fields.size(); ++i)
2723 {
2724 if(fields[i]->name() == fieldString)
2725 {
2726 fieldFound = true;
2727 break;
2728 }
2729 }
2730 if(fieldFound)
2731 {
2732 ConstantUnion *unionArray = new ConstantUnion[1];
2733 unionArray->setIConst(i);
2734 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2735 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2736 dotLocation);
2737 indexedExpression->setType(*fields[i]->type());
2738 }
2739 else
2740 {
2741 error(dotLocation, " no such field in interface block", fieldString.c_str());
2742 recover();
2743 indexedExpression = baseExpression;
2744 }
2745 }
2746 }
2747 else
2748 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002749 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002750 {
Nicolas Capensc09073f2017-11-09 09:49:03 -05002751 error(dotLocation, " field selection requires structure or vector on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002752 fieldString.c_str());
2753 }
2754 else
2755 {
2756 error(dotLocation,
Nicolas Capensc09073f2017-11-09 09:49:03 -05002757 " field selection requires structure, vector, or interface block on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002758 fieldString.c_str());
2759 }
2760 recover();
2761 indexedExpression = baseExpression;
2762 }
2763
2764 return indexedExpression;
2765}
2766
Nicolas Capens7d626792015-02-17 17:58:31 -05002767TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2768{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002769 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002770
Nicolas Capens0bac2852016-05-07 06:09:58 -04002771 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002772 qualifier.matrixPacking = EmpUnspecified;
2773 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002774
Alexis Hetuad6b8752015-06-09 16:15:30 -04002775 if(qualifierType == "shared")
2776 {
2777 qualifier.blockStorage = EbsShared;
2778 }
2779 else if(qualifierType == "packed")
2780 {
2781 qualifier.blockStorage = EbsPacked;
2782 }
2783 else if(qualifierType == "std140")
2784 {
2785 qualifier.blockStorage = EbsStd140;
2786 }
2787 else if(qualifierType == "row_major")
2788 {
2789 qualifier.matrixPacking = EmpRowMajor;
2790 }
2791 else if(qualifierType == "column_major")
2792 {
2793 qualifier.matrixPacking = EmpColumnMajor;
2794 }
2795 else if(qualifierType == "location")
Nicolas Capens0bac2852016-05-07 06:09:58 -04002796 {
2797 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2798 recover();
2799 }
2800 else
2801 {
2802 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2803 recover();
2804 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002805
Nicolas Capens0bac2852016-05-07 06:09:58 -04002806 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002807}
2808
2809TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2810{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002811 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002812
Nicolas Capens0bac2852016-05-07 06:09:58 -04002813 qualifier.location = -1;
2814 qualifier.matrixPacking = EmpUnspecified;
2815 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002816
Nicolas Capens0bac2852016-05-07 06:09:58 -04002817 if (qualifierType != "location")
2818 {
2819 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2820 recover();
2821 }
2822 else
2823 {
2824 // must check that location is non-negative
2825 if (intValue < 0)
2826 {
2827 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2828 recover();
2829 }
2830 else
2831 {
2832 qualifier.location = intValue;
2833 }
2834 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002835
Nicolas Capens0bac2852016-05-07 06:09:58 -04002836 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002837}
2838
2839TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2840{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002841 TLayoutQualifier joinedQualifier = leftQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002842
Nicolas Capens0bac2852016-05-07 06:09:58 -04002843 if (rightQualifier.location != -1)
2844 {
2845 joinedQualifier.location = rightQualifier.location;
2846 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002847 if(rightQualifier.matrixPacking != EmpUnspecified)
2848 {
2849 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2850 }
2851 if(rightQualifier.blockStorage != EbsUnspecified)
2852 {
2853 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2854 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002855
Nicolas Capens0bac2852016-05-07 06:09:58 -04002856 return joinedQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002857}
2858
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002859
2860TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2861 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2862{
2863 TQualifier mergedQualifier = EvqSmoothIn;
2864
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002865 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002866 if(interpolationQualifier == EvqSmooth)
2867 mergedQualifier = EvqSmoothIn;
2868 else if(interpolationQualifier == EvqFlat)
2869 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002870 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002871 }
2872 else if(storageQualifier == EvqCentroidIn) {
2873 if(interpolationQualifier == EvqSmooth)
2874 mergedQualifier = EvqCentroidIn;
2875 else if(interpolationQualifier == EvqFlat)
2876 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002877 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002878 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002879 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002880 if(interpolationQualifier == EvqSmooth)
2881 mergedQualifier = EvqSmoothOut;
2882 else if(interpolationQualifier == EvqFlat)
2883 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002884 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002885 }
2886 else if(storageQualifier == EvqCentroidOut) {
2887 if(interpolationQualifier == EvqSmooth)
2888 mergedQualifier = EvqCentroidOut;
2889 else if(interpolationQualifier == EvqFlat)
2890 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002891 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002892 }
2893 else {
2894 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2895 recover();
2896
2897 mergedQualifier = storageQualifier;
2898 }
2899
2900 TPublicType type;
2901 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2902 return type;
2903}
2904
Alexis Hetuad6b8752015-06-09 16:15:30 -04002905TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2906{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002907 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002908 {
2909 recover();
2910 }
2911
2912 for(unsigned int i = 0; i < fieldList->size(); ++i)
2913 {
2914 //
2915 // Careful not to replace already known aspects of type, like array-ness
2916 //
2917 TType *type = (*fieldList)[i]->type();
2918 type->setBasicType(typeSpecifier.type);
2919 type->setNominalSize(typeSpecifier.primarySize);
2920 type->setSecondarySize(typeSpecifier.secondarySize);
2921 type->setPrecision(typeSpecifier.precision);
2922 type->setQualifier(typeSpecifier.qualifier);
2923 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2924
2925 // don't allow arrays of arrays
2926 if(type->isArray())
2927 {
2928 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2929 recover();
2930 }
2931 if(typeSpecifier.array)
2932 type->setArraySize(typeSpecifier.arraySize);
2933 if(typeSpecifier.userDef)
2934 {
2935 type->setStruct(typeSpecifier.userDef->getStruct());
2936 }
2937
2938 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2939 {
2940 recover();
2941 }
2942 }
2943
2944 return fieldList;
2945}
2946
2947TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2948 const TString *structName, TFieldList *fieldList)
2949{
2950 TStructure *structure = new TStructure(structName, fieldList);
2951 TType *structureType = new TType(structure);
2952
2953 // Store a bool in the struct if we're at global scope, to allow us to
2954 // skip the local struct scoping workaround in HLSL.
2955 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2956 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2957
2958 if(!structName->empty())
2959 {
2960 if(reservedErrorCheck(nameLine, *structName))
2961 {
2962 recover();
2963 }
2964 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2965 if(!symbolTable.declare(*userTypeDef))
2966 {
2967 error(nameLine, "redefinition", structName->c_str(), "struct");
2968 recover();
2969 }
2970 }
2971
2972 // ensure we do not specify any storage qualifiers on the struct members
2973 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2974 {
2975 const TField &field = *(*fieldList)[typeListIndex];
2976 const TQualifier qualifier = field.type()->getQualifier();
2977 switch(qualifier)
2978 {
2979 case EvqGlobal:
2980 case EvqTemporary:
2981 break;
2982 default:
2983 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2984 recover();
2985 break;
2986 }
2987 }
2988
2989 TPublicType publicType;
2990 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2991 publicType.userDef = structureType;
2992 exitStructDeclaration();
2993
2994 return publicType;
2995}
2996
Alexis Hetufe1269e2015-06-16 12:43:32 -04002997bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04002998{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002999 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003000
Nicolas Capens0bac2852016-05-07 06:09:58 -04003001 // Embedded structure definitions are not supported per GLSL ES spec.
3002 // They aren't allowed in GLSL either, but we need to detect this here
3003 // so we don't rely on the GLSL compiler to catch it.
3004 if (mStructNestingLevel > 1) {
3005 error(line, "", "Embedded struct definitions are not allowed");
3006 return true;
3007 }
John Bauman66b8ab22014-05-06 15:57:45 -04003008
Nicolas Capens0bac2852016-05-07 06:09:58 -04003009 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04003010}
3011
3012void TParseContext::exitStructDeclaration()
3013{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003014 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003015}
3016
Alexis Hetuad6b8752015-06-09 16:15:30 -04003017bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3018{
3019 static const int kWebGLMaxStructNesting = 4;
3020
3021 if(field.type()->getBasicType() != EbtStruct)
3022 {
3023 return false;
3024 }
3025
3026 // We're already inside a structure definition at this point, so add
3027 // one to the field's struct nesting.
3028 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3029 {
3030 std::stringstream reasonStream;
3031 reasonStream << "Reference of struct type "
3032 << field.type()->getStruct()->name().c_str()
3033 << " exceeds maximum allowed nesting level of "
3034 << kWebGLMaxStructNesting;
3035 std::string reason = reasonStream.str();
3036 error(line, reason.c_str(), field.name().c_str(), "");
3037 return true;
3038 }
3039
3040 return false;
3041}
3042
3043TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3044{
3045 if(child == nullptr)
3046 {
3047 return nullptr;
3048 }
3049
3050 switch(op)
3051 {
3052 case EOpLogicalNot:
3053 if(child->getBasicType() != EbtBool ||
3054 child->isMatrix() ||
3055 child->isArray() ||
3056 child->isVector())
3057 {
3058 return nullptr;
3059 }
3060 break;
3061 case EOpBitwiseNot:
3062 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3063 child->isMatrix() ||
3064 child->isArray())
3065 {
3066 return nullptr;
3067 }
3068 break;
3069 case EOpPostIncrement:
3070 case EOpPreIncrement:
3071 case EOpPostDecrement:
3072 case EOpPreDecrement:
3073 case EOpNegative:
3074 if(child->getBasicType() == EbtStruct ||
3075 child->getBasicType() == EbtBool ||
3076 child->isArray())
3077 {
3078 return nullptr;
3079 }
3080 // Operators for built-ins are already type checked against their prototype.
3081 default:
3082 break;
3083 }
3084
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -04003085 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Alexis Hetuad6b8752015-06-09 16:15:30 -04003086}
3087
3088TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3089{
3090 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3091 if(node == nullptr)
3092 {
3093 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3094 recover();
3095 return child;
3096 }
3097 return node;
3098}
3099
3100TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3101{
3102 if(lValueErrorCheck(loc, getOperatorString(op), child))
3103 recover();
3104 return addUnaryMath(op, child, loc);
3105}
3106
3107bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3108{
3109 if(left->isArray() || right->isArray())
3110 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003111 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003112 {
3113 error(loc, "Invalid operation for arrays", getOperatorString(op));
3114 return false;
3115 }
3116
3117 if(left->isArray() != right->isArray())
3118 {
3119 error(loc, "array / non-array mismatch", getOperatorString(op));
3120 return false;
3121 }
3122
3123 switch(op)
3124 {
3125 case EOpEqual:
3126 case EOpNotEqual:
3127 case EOpAssign:
3128 case EOpInitialize:
3129 break;
3130 default:
3131 error(loc, "Invalid operation for arrays", getOperatorString(op));
3132 return false;
3133 }
3134 // At this point, size of implicitly sized arrays should be resolved.
3135 if(left->getArraySize() != right->getArraySize())
3136 {
3137 error(loc, "array size mismatch", getOperatorString(op));
3138 return false;
3139 }
3140 }
3141
3142 // Check ops which require integer / ivec parameters
3143 bool isBitShift = false;
3144 switch(op)
3145 {
3146 case EOpBitShiftLeft:
3147 case EOpBitShiftRight:
3148 case EOpBitShiftLeftAssign:
3149 case EOpBitShiftRightAssign:
3150 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3151 // check that the basic type is an integer type.
3152 isBitShift = true;
3153 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3154 {
3155 return false;
3156 }
3157 break;
3158 case EOpBitwiseAnd:
3159 case EOpBitwiseXor:
3160 case EOpBitwiseOr:
3161 case EOpBitwiseAndAssign:
3162 case EOpBitwiseXorAssign:
3163 case EOpBitwiseOrAssign:
3164 // It is enough to check the type of only one operand, since later it
3165 // is checked that the operand types match.
3166 if(!IsInteger(left->getBasicType()))
3167 {
3168 return false;
3169 }
3170 break;
3171 default:
3172 break;
3173 }
3174
3175 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3176 // So the basic type should usually match.
3177 if(!isBitShift && left->getBasicType() != right->getBasicType())
3178 {
3179 return false;
3180 }
3181
3182 // Check that type sizes match exactly on ops that require that.
3183 // Also check restrictions for structs that contain arrays or samplers.
3184 switch(op)
3185 {
3186 case EOpAssign:
3187 case EOpInitialize:
3188 case EOpEqual:
3189 case EOpNotEqual:
3190 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003191 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003192 {
3193 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3194 return false;
3195 }
3196 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3197 // we interpret the spec so that this extends to structs containing samplers,
3198 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003199 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003200 left->getType().isStructureContainingSamplers())
3201 {
3202 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3203 return false;
3204 }
3205 case EOpLessThan:
3206 case EOpGreaterThan:
3207 case EOpLessThanEqual:
3208 case EOpGreaterThanEqual:
3209 if((left->getNominalSize() != right->getNominalSize()) ||
3210 (left->getSecondarySize() != right->getSecondarySize()))
3211 {
3212 return false;
3213 }
Alexis Hetuec93b1d2016-12-09 16:01:29 -05003214 break;
3215 case EOpAdd:
3216 case EOpSub:
3217 case EOpDiv:
3218 case EOpIMod:
3219 case EOpBitShiftLeft:
3220 case EOpBitShiftRight:
3221 case EOpBitwiseAnd:
3222 case EOpBitwiseXor:
3223 case EOpBitwiseOr:
3224 case EOpAddAssign:
3225 case EOpSubAssign:
3226 case EOpDivAssign:
3227 case EOpIModAssign:
3228 case EOpBitShiftLeftAssign:
3229 case EOpBitShiftRightAssign:
3230 case EOpBitwiseAndAssign:
3231 case EOpBitwiseXorAssign:
3232 case EOpBitwiseOrAssign:
3233 if((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3234 {
3235 return false;
3236 }
3237
3238 // Are the sizes compatible?
3239 if(left->getNominalSize() != right->getNominalSize() || left->getSecondarySize() != right->getSecondarySize())
3240 {
3241 // If the nominal sizes of operands do not match:
3242 // One of them must be a scalar.
3243 if(!left->isScalar() && !right->isScalar())
3244 return false;
3245
3246 // In the case of compound assignment other than multiply-assign,
3247 // the right side needs to be a scalar. Otherwise a vector/matrix
3248 // would be assigned to a scalar. A scalar can't be shifted by a
3249 // vector either.
3250 if(!right->isScalar() && (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3251 return false;
3252 }
3253 break;
Alexis Hetuad6b8752015-06-09 16:15:30 -04003254 default:
3255 break;
3256 }
3257
3258 return true;
3259}
3260
Alexis Hetu76a343a2015-06-04 17:21:22 -04003261TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3262{
3263 TBasicType switchType = init->getBasicType();
3264 if((switchType != EbtInt && switchType != EbtUInt) ||
3265 init->isMatrix() ||
3266 init->isArray() ||
3267 init->isVector())
3268 {
3269 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3270 recover();
3271 return nullptr;
3272 }
3273
3274 if(statementList)
3275 {
3276 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3277 {
3278 recover();
3279 return nullptr;
3280 }
3281 }
3282
3283 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3284 if(node == nullptr)
3285 {
3286 error(loc, "erroneous switch statement", "switch");
3287 recover();
3288 return nullptr;
3289 }
3290 return node;
3291}
3292
3293TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3294{
Alexis Hetu0a655842015-06-22 16:52:11 -04003295 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003296 {
3297 error(loc, "case labels need to be inside switch statements", "case");
3298 recover();
3299 return nullptr;
3300 }
3301 if(condition == nullptr)
3302 {
3303 error(loc, "case label must have a condition", "case");
3304 recover();
3305 return nullptr;
3306 }
3307 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3308 condition->isMatrix() ||
3309 condition->isArray() ||
3310 condition->isVector())
3311 {
3312 error(condition->getLine(), "case label must be a scalar integer", "case");
3313 recover();
3314 }
3315 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3316 if(conditionConst == nullptr)
3317 {
3318 error(condition->getLine(), "case label must be constant", "case");
3319 recover();
3320 }
3321 TIntermCase *node = intermediate.addCase(condition, loc);
3322 if(node == nullptr)
3323 {
3324 error(loc, "erroneous case statement", "case");
3325 recover();
3326 return nullptr;
3327 }
3328 return node;
3329}
3330
3331TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3332{
Alexis Hetu0a655842015-06-22 16:52:11 -04003333 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003334 {
3335 error(loc, "default labels need to be inside switch statements", "default");
3336 recover();
3337 return nullptr;
3338 }
3339 TIntermCase *node = intermediate.addCase(nullptr, loc);
3340 if(node == nullptr)
3341 {
3342 error(loc, "erroneous default statement", "default");
3343 recover();
3344 return nullptr;
3345 }
3346 return node;
3347}
Alexis Hetue5246692015-06-18 12:34:52 -04003348TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3349{
3350 if(binaryOpCommonCheck(op, left, right, loc))
3351 {
3352 return intermediate.addAssign(op, left, right, loc);
3353 }
3354 return nullptr;
3355}
3356
3357TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3358{
3359 TIntermTyped *node = createAssign(op, left, right, loc);
3360 if(node == nullptr)
3361 {
3362 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3363 recover();
3364 return left;
3365 }
3366 return node;
3367}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003368
Alexis Hetub4769582015-06-16 12:19:50 -04003369TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3370 const TSourceLoc &loc)
3371{
3372 if(!binaryOpCommonCheck(op, left, right, loc))
3373 return nullptr;
3374
3375 switch(op)
3376 {
3377 case EOpEqual:
3378 case EOpNotEqual:
3379 break;
3380 case EOpLessThan:
3381 case EOpGreaterThan:
3382 case EOpLessThanEqual:
3383 case EOpGreaterThanEqual:
3384 ASSERT(!left->isArray() && !right->isArray());
3385 if(left->isMatrix() || left->isVector() ||
3386 left->getBasicType() == EbtStruct)
3387 {
3388 return nullptr;
3389 }
3390 break;
3391 case EOpLogicalOr:
3392 case EOpLogicalXor:
3393 case EOpLogicalAnd:
3394 ASSERT(!left->isArray() && !right->isArray());
3395 if(left->getBasicType() != EbtBool ||
3396 left->isMatrix() || left->isVector())
3397 {
3398 return nullptr;
3399 }
3400 break;
3401 case EOpAdd:
3402 case EOpSub:
3403 case EOpDiv:
3404 case EOpMul:
3405 ASSERT(!left->isArray() && !right->isArray());
3406 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3407 {
3408 return nullptr;
3409 }
3410 break;
3411 case EOpIMod:
3412 ASSERT(!left->isArray() && !right->isArray());
3413 // Note that this is only for the % operator, not for mod()
3414 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3415 {
3416 return nullptr;
3417 }
3418 break;
3419 // Note that for bitwise ops, type checking is done in promote() to
3420 // share code between ops and compound assignment
3421 default:
3422 break;
3423 }
3424
3425 return intermediate.addBinaryMath(op, left, right, loc);
3426}
3427
3428TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3429{
3430 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3431 if(node == 0)
3432 {
3433 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3434 recover();
3435 return left;
3436 }
3437 return node;
3438}
3439
3440TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3441{
3442 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3443 if(node == 0)
3444 {
3445 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3446 recover();
3447 ConstantUnion *unionArray = new ConstantUnion[1];
3448 unionArray->setBConst(false);
3449 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3450 }
3451 return node;
3452}
3453
Alexis Hetu76a343a2015-06-04 17:21:22 -04003454TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3455{
3456 switch(op)
3457 {
3458 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003459 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003460 {
3461 error(loc, "continue statement only allowed in loops", "");
3462 recover();
3463 }
3464 break;
3465 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003466 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003467 {
3468 error(loc, "break statement only allowed in loops and switch statements", "");
3469 recover();
3470 }
3471 break;
3472 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003473 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003474 {
3475 error(loc, "non-void function must return a value", "return");
3476 recover();
3477 }
3478 break;
3479 default:
3480 // No checks for discard
3481 break;
3482 }
3483 return intermediate.addBranch(op, loc);
3484}
3485
3486TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3487{
3488 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003489 mFunctionReturnsValue = true;
3490 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003491 {
3492 error(loc, "void function cannot return a value", "return");
3493 recover();
3494 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003495 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003496 {
3497 error(loc, "function return is not matching type:", "return");
3498 recover();
3499 }
3500 return intermediate.addBranch(op, returnValue, loc);
3501}
3502
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003503TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3504{
3505 *fatalError = false;
3506 TOperator op = fnCall->getBuiltInOp();
3507 TIntermTyped *callNode = nullptr;
3508
3509 if(thisNode != nullptr)
3510 {
3511 ConstantUnion *unionArray = new ConstantUnion[1];
3512 int arraySize = 0;
3513 TIntermTyped *typedThis = thisNode->getAsTyped();
3514 if(fnCall->getName() != "length")
3515 {
3516 error(loc, "invalid method", fnCall->getName().c_str());
3517 recover();
3518 }
3519 else if(paramNode != nullptr)
3520 {
3521 error(loc, "method takes no parameters", "length");
3522 recover();
3523 }
3524 else if(typedThis == nullptr || !typedThis->isArray())
3525 {
3526 error(loc, "length can only be called on arrays", "length");
3527 recover();
3528 }
3529 else
3530 {
3531 arraySize = typedThis->getArraySize();
3532 if(typedThis->getAsSymbolNode() == nullptr)
3533 {
3534 // This code path can be hit with expressions like these:
3535 // (a = b).length()
3536 // (func()).length()
3537 // (int[3](0, 1, 2)).length()
3538 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3539 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3540 // which allows "An array, vector or matrix expression with the length method applied".
3541 error(loc, "length can only be called on array names, not on array expressions", "length");
3542 recover();
3543 }
3544 }
3545 unionArray->setIConst(arraySize);
3546 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3547 }
3548 else if(op != EOpNull)
3549 {
3550 //
3551 // Then this should be a constructor.
3552 // Don't go through the symbol table for constructors.
3553 // Their parameters will be verified algorithmically.
3554 //
3555 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3556 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3557 {
3558 //
3559 // It's a constructor, of type 'type'.
3560 //
3561 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3562 }
3563
3564 if(callNode == nullptr)
3565 {
3566 recover();
3567 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3568 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003569 }
3570 else
3571 {
3572 //
3573 // Not a constructor. Find it in the symbol table.
3574 //
3575 const TFunction *fnCandidate;
3576 bool builtIn;
3577 fnCandidate = findFunction(loc, fnCall, &builtIn);
3578 if(fnCandidate)
3579 {
3580 //
3581 // A declared function.
3582 //
3583 if(builtIn && !fnCandidate->getExtension().empty() &&
3584 extensionErrorCheck(loc, fnCandidate->getExtension()))
3585 {
3586 recover();
3587 }
3588 op = fnCandidate->getBuiltInOp();
3589 if(builtIn && op != EOpNull)
3590 {
3591 //
3592 // A function call mapped to a built-in operation.
3593 //
3594 if(fnCandidate->getParamCount() == 1)
3595 {
3596 //
3597 // Treat it like a built-in unary operator.
3598 //
3599 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3600 if(callNode == nullptr)
3601 {
3602 std::stringstream extraInfoStream;
3603 extraInfoStream << "built in unary operator function. Type: "
3604 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3605 std::string extraInfo = extraInfoStream.str();
3606 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3607 *fatalError = true;
3608 return nullptr;
3609 }
3610 }
3611 else
3612 {
3613 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3614 aggregate->setType(fnCandidate->getReturnType());
3615
3616 // Some built-in functions have out parameters too.
3617 functionCallLValueErrorCheck(fnCandidate, aggregate);
3618
3619 callNode = aggregate;
Nicolas Capens91dfb972016-04-09 23:45:12 -04003620
3621 if(fnCandidate->getParamCount() == 2)
3622 {
3623 TIntermSequence &parameters = paramNode->getAsAggregate()->getSequence();
3624 TIntermTyped *left = parameters[0]->getAsTyped();
3625 TIntermTyped *right = parameters[1]->getAsTyped();
3626
3627 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
3628 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
3629 if (leftTempConstant && rightTempConstant)
3630 {
3631 TIntermTyped *typedReturnNode = leftTempConstant->fold(op, rightTempConstant, infoSink());
3632
3633 if(typedReturnNode)
3634 {
3635 callNode = typedReturnNode;
3636 }
3637 }
3638 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003639 }
3640 }
3641 else
3642 {
3643 // This is a real function call
3644
3645 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3646 aggregate->setType(fnCandidate->getReturnType());
3647
3648 // this is how we know whether the given function is a builtIn function or a user defined function
3649 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3650 // if builtIn == true, it's definitely a builtIn function with EOpNull
3651 if(!builtIn)
3652 aggregate->setUserDefined();
3653 aggregate->setName(fnCandidate->getMangledName());
3654
3655 callNode = aggregate;
3656
3657 functionCallLValueErrorCheck(fnCandidate, aggregate);
3658 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003659 }
3660 else
3661 {
3662 // error message was put out by findFunction()
3663 // Put on a dummy node for error recovery
3664 ConstantUnion *unionArray = new ConstantUnion[1];
3665 unionArray->setFConst(0.0f);
3666 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3667 recover();
3668 }
3669 }
3670 delete fnCall;
3671 return callNode;
3672}
3673
Alexis Hetueee212e2015-07-07 17:13:30 -04003674TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3675{
3676 if(boolErrorCheck(loc, cond))
3677 recover();
3678
3679 if(trueBlock->getType() != falseBlock->getType())
3680 {
3681 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3682 recover();
3683 return falseBlock;
3684 }
3685 // ESSL1 sections 5.2 and 5.7:
3686 // ESSL3 section 5.7:
3687 // Ternary operator is not among the operators allowed for structures/arrays.
3688 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3689 {
3690 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3691 recover();
3692 return falseBlock;
3693 }
3694 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3695}
3696
John Bauman66b8ab22014-05-06 15:57:45 -04003697//
3698// Parse an array of strings using yyparse.
3699//
3700// Returns 0 for success.
3701//
3702int PaParseStrings(int count, const char* const string[], const int length[],
Nicolas Capens0bac2852016-05-07 06:09:58 -04003703 TParseContext* context) {
3704 if ((count == 0) || !string)
3705 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003706
Nicolas Capens0bac2852016-05-07 06:09:58 -04003707 if (glslang_initialize(context))
3708 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003709
Nicolas Capens0bac2852016-05-07 06:09:58 -04003710 int error = glslang_scan(count, string, length, context);
3711 if (!error)
3712 error = glslang_parse(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003713
Nicolas Capens0bac2852016-05-07 06:09:58 -04003714 glslang_finalize(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003715
Nicolas Capens0bac2852016-05-07 06:09:58 -04003716 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003717}
3718
3719
3720