blob: 8d58c5f55f5bea3aab45edf3b96382850d8c2818 [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 Hetufe1269e2015-06-16 12:43:32 -04001125void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value)
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);
1128 mDirectiveHandler.handlePragma(loc, name, value);
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
1319 if(typeSpecifier.array)
1320 {
1321 error(typeSpecifier.line, "not supported", "first-class array");
1322 recover();
1323 returnType.clearArrayness();
1324 }
1325
Alexis Hetu0a655842015-06-22 16:52:11 -04001326 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001327 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001328 if(typeSpecifier.array)
1329 {
1330 error(typeSpecifier.line, "not supported", "first-class array");
1331 returnType.clearArrayness();
1332 }
1333
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001334 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1335 {
1336 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1337 recover();
1338 }
1339
1340 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1341 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1342 {
1343 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1344 recover();
1345 }
1346 }
1347 else
1348 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001349 if(!returnType.layoutQualifier.isEmpty())
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001350 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001351 globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout");
1352 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001353
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001354 if(IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn || returnType.qualifier == EvqFragmentOut)
1355 {
1356 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier, typeSpecifier.line);
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001357 }
1358 }
1359
1360 return returnType;
1361}
1362
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001363void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1364 const TPublicType &type,
1365 const TSourceLoc &qualifierLocation)
1366{
1367 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1368 if(type.type == EbtBool)
1369 {
1370 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1371 }
1372
1373 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1374 switch(qualifier)
1375 {
1376 case EvqVertexIn:
1377 // ESSL 3.00 section 4.3.4
1378 if(type.array)
1379 {
1380 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1381 }
1382 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1383 return;
1384 case EvqFragmentOut:
1385 // ESSL 3.00 section 4.3.6
1386 if(type.isMatrix())
1387 {
1388 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1389 }
1390 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1391 return;
1392 default:
1393 break;
1394 }
1395
1396 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1397 // restrictions.
1398 bool typeContainsIntegers = (type.type == EbtInt || type.type == EbtUInt ||
1399 type.isStructureContainingType(EbtInt) ||
1400 type.isStructureContainingType(EbtUInt));
1401 if(typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1402 {
1403 error(qualifierLocation, "must use 'flat' interpolation here", getQualifierString(qualifier));
1404 }
1405
1406 if(type.type == EbtStruct)
1407 {
1408 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1409 // These restrictions are only implied by the ESSL 3.00 spec, but
1410 // the ESSL 3.10 spec lists these restrictions explicitly.
1411 if(type.array)
1412 {
1413 error(qualifierLocation, "cannot be an array of structures", getQualifierString(qualifier));
1414 }
1415 if(type.isStructureContainingArrays())
1416 {
1417 error(qualifierLocation, "cannot be a structure containing an array", getQualifierString(qualifier));
1418 }
1419 if(type.isStructureContainingType(EbtStruct))
1420 {
1421 error(qualifierLocation, "cannot be a structure containing a structure", getQualifierString(qualifier));
1422 }
1423 if(type.isStructureContainingType(EbtBool))
1424 {
1425 error(qualifierLocation, "cannot be a structure containing a bool", getQualifierString(qualifier));
1426 }
1427 }
1428}
1429
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001430TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1431 const TSourceLoc &identifierOrTypeLocation,
1432 const TString &identifier)
1433{
1434 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1435
1436 bool emptyDeclaration = (identifier == "");
1437
1438 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1439
1440 if(emptyDeclaration)
1441 {
1442 if(publicType.isUnsizedArray())
1443 {
1444 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1445 // It is assumed that this applies to empty declarations as well.
1446 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1447 }
1448 }
1449 else
1450 {
1451 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1452 recover();
1453
1454 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1455 recover();
1456
1457 TVariable *variable = nullptr;
1458 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1459 recover();
1460
1461 if(variable && symbol)
1462 symbol->setId(variable->getUniqueId());
1463 }
1464
1465 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1466}
1467
1468TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1469 const TSourceLoc &identifierLocation,
1470 const TString &identifier,
1471 const TSourceLoc &indexLocation,
1472 TIntermTyped *indexExpression)
1473{
1474 mDeferredSingleDeclarationErrorCheck = false;
1475
1476 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1477 recover();
1478
1479 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1480 recover();
1481
1482 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1483 {
1484 recover();
1485 }
1486
1487 TType arrayType(publicType);
1488
1489 int size;
1490 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1491 {
1492 recover();
1493 }
1494 // Make the type an array even if size check failed.
1495 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1496 arrayType.setArraySize(size);
1497
1498 TVariable *variable = nullptr;
1499 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1500 recover();
1501
1502 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1503 if(variable && symbol)
1504 symbol->setId(variable->getUniqueId());
1505
1506 return intermediate.makeAggregate(symbol, identifierLocation);
1507}
1508
1509TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1510 const TSourceLoc &identifierLocation,
1511 const TString &identifier,
1512 const TSourceLoc &initLocation,
1513 TIntermTyped *initializer)
1514{
1515 mDeferredSingleDeclarationErrorCheck = false;
1516
1517 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1518 recover();
1519
1520 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001521 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001522 {
1523 //
1524 // Build intermediate representation
1525 //
1526 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1527 }
1528 else
1529 {
1530 recover();
1531 return nullptr;
1532 }
1533}
1534
1535TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1536 const TSourceLoc &identifierLocation,
1537 const TString &identifier,
1538 const TSourceLoc &indexLocation,
1539 TIntermTyped *indexExpression,
1540 const TSourceLoc &initLocation,
1541 TIntermTyped *initializer)
1542{
1543 mDeferredSingleDeclarationErrorCheck = false;
1544
1545 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1546 recover();
1547
1548 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1549 {
1550 recover();
1551 }
1552
1553 TPublicType arrayType(publicType);
1554
1555 int size = 0;
1556 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1557 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1558 {
1559 recover();
1560 }
1561 // Make the type an array even if size check failed.
1562 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1563 arrayType.setArray(true, size);
1564
1565 // initNode will correspond to the whole of "type b[n] = initializer".
1566 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001567 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001568 {
1569 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1570 }
1571 else
1572 {
1573 recover();
1574 return nullptr;
1575 }
1576}
1577
1578TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1579 const TSourceLoc &identifierLoc,
1580 const TString *identifier,
1581 const TSymbol *symbol)
1582{
1583 // invariant declaration
1584 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1585 {
1586 recover();
1587 }
1588
1589 if(!symbol)
1590 {
1591 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1592 recover();
1593 return nullptr;
1594 }
1595 else
1596 {
1597 const TString kGlFrontFacing("gl_FrontFacing");
1598 if(*identifier == kGlFrontFacing)
1599 {
1600 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1601 recover();
1602 return nullptr;
1603 }
1604 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1605 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1606 ASSERT(variable);
1607 const TType &type = variable->getType();
1608 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1609 *identifier, type, identifierLoc);
1610
1611 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1612 aggregate->setOp(EOpInvariantDeclaration);
1613 return aggregate;
1614 }
1615}
1616
1617TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1618 const TSourceLoc &identifierLocation, const TString &identifier)
1619{
1620 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1621 if(mDeferredSingleDeclarationErrorCheck)
1622 {
1623 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1624 recover();
1625 mDeferredSingleDeclarationErrorCheck = false;
1626 }
1627
1628 if(locationDeclaratorListCheck(identifierLocation, publicType))
1629 recover();
1630
1631 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1632 recover();
1633
1634 TVariable *variable = nullptr;
1635 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1636 recover();
1637
1638 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1639 if(variable && symbol)
1640 symbol->setId(variable->getUniqueId());
1641
1642 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1643}
1644
1645TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1646 const TSourceLoc &identifierLocation, const TString &identifier,
1647 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1648{
1649 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1650 if(mDeferredSingleDeclarationErrorCheck)
1651 {
1652 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1653 recover();
1654 mDeferredSingleDeclarationErrorCheck = false;
1655 }
1656
1657 if(locationDeclaratorListCheck(identifierLocation, publicType))
1658 recover();
1659
1660 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1661 recover();
1662
1663 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1664 {
1665 recover();
1666 }
1667 else
1668 {
1669 TType arrayType = TType(publicType);
1670 int size;
1671 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1672 {
1673 recover();
1674 }
1675 arrayType.setArraySize(size);
1676
1677 TVariable *variable = nullptr;
1678 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1679 recover();
1680
1681 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1682 if(variable && symbol)
1683 symbol->setId(variable->getUniqueId());
1684
1685 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1686 }
1687
1688 return nullptr;
1689}
1690
1691TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1692 const TSourceLoc &identifierLocation, const TString &identifier,
1693 const TSourceLoc &initLocation, TIntermTyped *initializer)
1694{
1695 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1696 if(mDeferredSingleDeclarationErrorCheck)
1697 {
1698 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1699 recover();
1700 mDeferredSingleDeclarationErrorCheck = false;
1701 }
1702
1703 if(locationDeclaratorListCheck(identifierLocation, publicType))
1704 recover();
1705
1706 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001707 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001708 {
1709 //
1710 // build the intermediate representation
1711 //
1712 if(intermNode)
1713 {
1714 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1715 }
1716 else
1717 {
1718 return aggregateDeclaration;
1719 }
1720 }
1721 else
1722 {
1723 recover();
1724 return nullptr;
1725 }
1726}
1727
1728TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1729 TIntermAggregate *aggregateDeclaration,
1730 const TSourceLoc &identifierLocation,
1731 const TString &identifier,
1732 const TSourceLoc &indexLocation,
1733 TIntermTyped *indexExpression,
1734 const TSourceLoc &initLocation, TIntermTyped *initializer)
1735{
1736 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1737 if(mDeferredSingleDeclarationErrorCheck)
1738 {
1739 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1740 recover();
1741 mDeferredSingleDeclarationErrorCheck = false;
1742 }
1743
1744 if(locationDeclaratorListCheck(identifierLocation, publicType))
1745 recover();
1746
1747 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1748 {
1749 recover();
1750 }
1751
1752 TPublicType arrayType(publicType);
1753
1754 int size = 0;
1755 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1756 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1757 {
1758 recover();
1759 }
1760 // Make the type an array even if size check failed.
1761 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1762 arrayType.setArray(true, size);
1763
1764 // initNode will correspond to the whole of "b[n] = initializer".
1765 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001766 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001767 {
1768 if(initNode)
1769 {
1770 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1771 }
1772 else
1773 {
1774 return aggregateDeclaration;
1775 }
1776 }
1777 else
1778 {
1779 recover();
1780 return nullptr;
1781 }
1782}
1783
Alexis Hetua35d8232015-06-11 17:11:06 -04001784void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1785{
Alexis Hetu0a655842015-06-22 16:52:11 -04001786 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001787 {
1788 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1789 recover();
1790 return;
1791 }
1792
1793 if(typeQualifier.qualifier != EvqUniform)
1794 {
1795 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1796 recover();
1797 return;
1798 }
1799
1800 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1801 ASSERT(!layoutQualifier.isEmpty());
1802
1803 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1804 {
1805 recover();
1806 return;
1807 }
1808
1809 if(layoutQualifier.matrixPacking != EmpUnspecified)
1810 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001811 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001812 }
1813
1814 if(layoutQualifier.blockStorage != EbsUnspecified)
1815 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001816 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001817 }
1818}
1819
Alexis Hetu407813b2016-02-24 16:46:13 -05001820TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1821{
1822 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1823 // Either way the instance in the symbol table is used to track whether the function is declared
1824 // multiple times.
1825 TFunction *symbolTableFunction =
1826 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1827 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1828 {
1829 // ESSL 1.00.17 section 4.2.7.
1830 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1831 error(location, "duplicate function prototype declarations are not allowed", "function");
1832 recover();
1833 }
1834 symbolTableFunction->setHasPrototypeDeclaration();
1835
1836 TIntermAggregate *prototype = new TIntermAggregate;
1837 prototype->setType(function.getReturnType());
1838 prototype->setName(function.getMangledName());
1839
1840 for(size_t i = 0; i < function.getParamCount(); i++)
1841 {
1842 const TParameter &param = function.getParam(i);
1843 if(param.name != 0)
1844 {
1845 TVariable variable(param.name, *param.type);
1846
1847 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1848 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1849 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1850 }
1851 else
1852 {
1853 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1854 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1855 }
1856 }
1857
1858 prototype->setOp(EOpPrototype);
1859
1860 symbolTable.pop();
1861
1862 if(!symbolTable.atGlobalLevel())
1863 {
1864 // ESSL 3.00.4 section 4.2.4.
1865 error(location, "local function prototype declarations are not allowed", "function");
1866 recover();
1867 }
1868
1869 return prototype;
1870}
1871
1872TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1873{
1874 //?? Check that all paths return a value if return type != void ?
1875 // May be best done as post process phase on intermediate code
1876 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1877 {
1878 error(location, "function does not return a value:", "", function.getName().c_str());
1879 recover();
1880 }
1881
1882 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1883 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1884 aggregate->setName(function.getMangledName().c_str());
1885 aggregate->setType(function.getReturnType());
1886
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001887 // store the pragma information for debug and optimize and other vendor specific
1888 // information. This information can be queried from the parse tree
1889 aggregate->setOptimize(pragma().optimize);
Alexis Hetu407813b2016-02-24 16:46:13 -05001890 aggregate->setDebug(pragma().debug);
1891
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001892 if(functionBody && functionBody->getAsAggregate())
Alexis Hetu407813b2016-02-24 16:46:13 -05001893 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1894
1895 symbolTable.pop();
1896 return aggregate;
1897}
1898
1899void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1900{
1901 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1902
1903 if(builtIn)
1904 {
1905 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1906 recover();
1907 }
1908
1909 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1910 //
1911 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1912 // as it would have just been put in the symbol table. Otherwise, we're looking up
1913 // an earlier occurance.
1914 //
1915 if(prevDec->isDefined())
1916 {
1917 // Then this function already has a body.
1918 error(location, "function already has a body", function->getName().c_str());
1919 recover();
1920 }
1921 prevDec->setDefined();
1922 //
1923 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1924 // Eventually we will probably want to have only a single definition and just swap the
1925 // arguments to be the definition's arguments.
1926 //
1927 function->setUniqueId(prevDec->getUniqueId());
1928
1929 // Raise error message if main function takes any parameters or return anything other than void
1930 if(function->getName() == "main")
1931 {
1932 if(function->getParamCount() > 0)
1933 {
1934 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1935 recover();
1936 }
1937 if(function->getReturnType().getBasicType() != EbtVoid)
1938 {
1939 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1940 recover();
1941 }
1942 }
1943
1944 //
1945 // Remember the return type for later checking for RETURN statements.
1946 //
1947 mCurrentFunctionType = &(prevDec->getReturnType());
1948 mFunctionReturnsValue = false;
1949
1950 //
1951 // Insert parameters into the symbol table.
1952 // If the parameter has no name, it's not an error, just don't insert it
1953 // (could be used for unused args).
1954 //
1955 // Also, accumulate the list of parameters into the HIL, so lower level code
1956 // knows where to find parameters.
1957 //
1958 TIntermAggregate *paramNodes = new TIntermAggregate;
1959 for(size_t i = 0; i < function->getParamCount(); i++)
1960 {
1961 const TParameter &param = function->getParam(i);
1962 if(param.name != 0)
1963 {
1964 TVariable *variable = new TVariable(param.name, *param.type);
1965 //
1966 // Insert the parameters with name in the symbol table.
1967 //
1968 if(!symbolTable.declare(*variable))
1969 {
1970 error(location, "redefinition", variable->getName().c_str());
1971 recover();
1972 paramNodes = intermediate.growAggregate(
1973 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1974 continue;
1975 }
1976
1977 //
1978 // Add the parameter to the HIL
1979 //
1980 TIntermSymbol *symbol = intermediate.addSymbol(
1981 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1982
1983 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1984 }
1985 else
1986 {
1987 paramNodes = intermediate.growAggregate(
1988 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1989 }
1990 }
1991 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1992 *aggregateOut = paramNodes;
1993 setLoopNestingLevel(0);
1994}
1995
1996TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1997{
1998 //
1999 // We don't know at this point whether this is a function definition or a prototype.
2000 // The definition production code will check for redefinitions.
2001 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
2002 //
2003 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2004 // here.
2005 //
2006 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002007 if(getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2008 {
2009 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2010 // Therefore overloading or redefining builtin functions is an error.
2011 error(location, "Name of a built-in function cannot be redeclared as function", function->getName().c_str());
2012 }
2013 else if(prevDec)
Alexis Hetu407813b2016-02-24 16:46:13 -05002014 {
2015 if(prevDec->getReturnType() != function->getReturnType())
2016 {
2017 error(location, "overloaded functions must have the same return type",
2018 function->getReturnType().getBasicString());
2019 recover();
2020 }
2021 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
2022 {
2023 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
2024 {
2025 error(location, "overloaded functions must have the same parameter qualifiers",
2026 function->getParam(i).type->getQualifierString());
2027 recover();
2028 }
2029 }
2030 }
2031
2032 //
2033 // Check for previously declared variables using the same name.
2034 //
2035 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
2036 if(prevSym)
2037 {
2038 if(!prevSym->isFunction())
2039 {
2040 error(location, "redefinition", function->getName().c_str(), "function");
2041 recover();
2042 }
2043 }
2044
2045 // We're at the inner scope level of the function's arguments and body statement.
2046 // Add the function prototype to the surrounding scope instead.
2047 symbolTable.getOuterLevel()->insert(*function);
2048
2049 //
2050 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2051 // variable names from this one, and not the one that's
2052 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2053 //
2054 return function;
2055}
2056
Alexis Hetue5246692015-06-18 12:34:52 -04002057TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
2058{
2059 TPublicType publicType = publicTypeIn;
2060 TOperator op = EOpNull;
2061 if(publicType.userDef)
2062 {
2063 op = EOpConstructStruct;
2064 }
2065 else
2066 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002067 op = TypeToConstructorOperator(TType(publicType));
Alexis Hetue5246692015-06-18 12:34:52 -04002068 if(op == EOpNull)
2069 {
2070 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2071 recover();
2072 publicType.type = EbtFloat;
2073 op = EOpConstructFloat;
2074 }
2075 }
2076
2077 TString tempString;
2078 TType type(publicType);
2079 return new TFunction(&tempString, type, op);
2080}
2081
John Bauman66b8ab22014-05-06 15:57:45 -04002082// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002083// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002084//
2085// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2086//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002087TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002088{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002089 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002090
Nicolas Capens0bac2852016-05-07 06:09:58 -04002091 if(!aggregateArguments)
2092 {
2093 aggregateArguments = new TIntermAggregate;
2094 aggregateArguments->getSequence().push_back(arguments);
2095 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002096
Alexis Hetu2a198552016-09-27 20:50:45 -04002097 if(type->isArray())
2098 {
2099 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2100 // the array.
2101 for(TIntermNode *&argNode : aggregateArguments->getSequence())
2102 {
2103 const TType &argType = argNode->getAsTyped()->getType();
2104 // It has already been checked that the argument is not an array.
2105 ASSERT(!argType.isArray());
2106 if(!argType.sameElementType(*type))
2107 {
2108 error(line, "Array constructor argument has an incorrect type", "Error");
Alexis Hetuf0005a12016-09-28 15:52:21 -04002109 return nullptr;
Alexis Hetu2a198552016-09-27 20:50:45 -04002110 }
2111 }
2112 }
2113 else if(op == EOpConstructStruct)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002114 {
2115 const TFieldList &fields = type->getStruct()->fields();
2116 TIntermSequence &args = aggregateArguments->getSequence();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002117
Nicolas Capens0bac2852016-05-07 06:09:58 -04002118 for(size_t i = 0; i < fields.size(); i++)
2119 {
2120 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
2121 {
2122 error(line, "Structure constructor arguments do not match structure fields", "Error");
2123 recover();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002124
Alexis Hetuf0005a12016-09-28 15:52:21 -04002125 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002126 }
2127 }
2128 }
John Bauman66b8ab22014-05-06 15:57:45 -04002129
Nicolas Capens0bac2852016-05-07 06:09:58 -04002130 // Turn the argument list itself into a constructor
2131 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2132 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
2133 if(constConstructor)
2134 {
2135 return constConstructor;
2136 }
John Bauman66b8ab22014-05-06 15:57:45 -04002137
Nicolas Capens0bac2852016-05-07 06:09:58 -04002138 return constructor;
John Bauman66b8ab22014-05-06 15:57:45 -04002139}
2140
2141TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2142{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002143 aggrNode->setType(type);
2144 if (aggrNode->isConstantFoldable()) {
2145 bool returnVal = false;
2146 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2147 if (aggrNode->getSequence().size() == 1) {
2148 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
2149 }
2150 else {
2151 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
2152 }
2153 if (returnVal)
Alexis Hetuf0005a12016-09-28 15:52:21 -04002154 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002155
Nicolas Capens0bac2852016-05-07 06:09:58 -04002156 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2157 }
John Bauman66b8ab22014-05-06 15:57:45 -04002158
Alexis Hetuf0005a12016-09-28 15:52:21 -04002159 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002160}
2161
John Bauman66b8ab22014-05-06 15:57:45 -04002162//
2163// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2164// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2165// 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 -04002166// 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 -04002167// a constant matrix.
2168//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002169TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002170{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002171 TIntermTyped* typedNode;
2172 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002173
Nicolas Capens0bac2852016-05-07 06:09:58 -04002174 ConstantUnion *unionArray;
2175 if (tempConstantNode) {
2176 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002177
Nicolas Capens0bac2852016-05-07 06:09:58 -04002178 if (!unionArray) {
2179 return node;
2180 }
2181 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2182 error(line, "Cannot offset into the vector", "Error");
2183 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002184
Alexis Hetuf0005a12016-09-28 15:52:21 -04002185 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002186 }
John Bauman66b8ab22014-05-06 15:57:45 -04002187
Nicolas Capens0bac2852016-05-07 06:09:58 -04002188 ConstantUnion* constArray = new ConstantUnion[fields.num];
John Bauman66b8ab22014-05-06 15:57:45 -04002189
Alexis Hetub34591a2016-06-28 15:48:35 -04002190 int objSize = static_cast<int>(node->getType().getObjectSize());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002191 for (int i = 0; i < fields.num; i++) {
Alexis Hetub34591a2016-06-28 15:48:35 -04002192 if (fields.offsets[i] >= objSize) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002193 std::stringstream extraInfoStream;
2194 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2195 std::string extraInfo = extraInfoStream.str();
2196 error(line, "", "[", extraInfo.c_str());
2197 recover();
2198 fields.offsets[i] = 0;
2199 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002200
Nicolas Capens0bac2852016-05-07 06:09:58 -04002201 constArray[i] = unionArray[fields.offsets[i]];
John Bauman66b8ab22014-05-06 15:57:45 -04002202
Nicolas Capens0bac2852016-05-07 06:09:58 -04002203 }
2204 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2205 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002206}
2207
2208//
2209// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002210// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2211// 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 -04002212// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2213//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002214TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002215{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002216 TIntermTyped* typedNode;
2217 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002218
Nicolas Capens0bac2852016-05-07 06:09:58 -04002219 if (index >= node->getType().getNominalSize()) {
2220 std::stringstream extraInfoStream;
2221 extraInfoStream << "matrix field selection out of range '" << index << "'";
2222 std::string extraInfo = extraInfoStream.str();
2223 error(line, "", "[", extraInfo.c_str());
2224 recover();
2225 index = 0;
2226 }
John Bauman66b8ab22014-05-06 15:57:45 -04002227
Nicolas Capens0bac2852016-05-07 06:09:58 -04002228 if (tempConstantNode) {
2229 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2230 int size = tempConstantNode->getType().getNominalSize();
2231 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2232 } else {
2233 error(line, "Cannot offset into the matrix", "Error");
2234 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002235
Alexis Hetuf0005a12016-09-28 15:52:21 -04002236 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002237 }
John Bauman66b8ab22014-05-06 15:57:45 -04002238
Nicolas Capens0bac2852016-05-07 06:09:58 -04002239 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002240}
2241
2242
2243//
2244// 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 -04002245// the symbol table and parse-tree is built for the type of the element. The input
2246// 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 -04002247// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2248//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002249TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002250{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002251 TIntermTyped* typedNode;
2252 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2253 TType arrayElementType = node->getType();
2254 arrayElementType.clearArrayness();
John Bauman66b8ab22014-05-06 15:57:45 -04002255
Nicolas Capens0bac2852016-05-07 06:09:58 -04002256 if (index >= node->getType().getArraySize()) {
2257 std::stringstream extraInfoStream;
2258 extraInfoStream << "array field selection out of range '" << index << "'";
2259 std::string extraInfo = extraInfoStream.str();
2260 error(line, "", "[", extraInfo.c_str());
2261 recover();
2262 index = 0;
2263 }
John Bauman66b8ab22014-05-06 15:57:45 -04002264
Nicolas Capens0bac2852016-05-07 06:09:58 -04002265 size_t arrayElementSize = arrayElementType.getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002266
Nicolas Capens0bac2852016-05-07 06:09:58 -04002267 if (tempConstantNode) {
2268 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2269 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2270 } else {
2271 error(line, "Cannot offset into the array", "Error");
2272 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002273
Alexis Hetuf0005a12016-09-28 15:52:21 -04002274 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002275 }
John Bauman66b8ab22014-05-06 15:57:45 -04002276
Nicolas Capens0bac2852016-05-07 06:09:58 -04002277 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002278}
2279
2280
2281//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002282// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002283// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2284// function and returns the parse-tree with the values of the embedded/nested struct.
2285//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002286TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002287{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002288 const TFieldList &fields = node->getType().getStruct()->fields();
2289 TIntermTyped *typedNode;
2290 size_t instanceSize = 0;
2291 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002292
Nicolas Capens0bac2852016-05-07 06:09:58 -04002293 for(size_t index = 0; index < fields.size(); ++index) {
2294 if (fields[index]->name() == identifier) {
2295 break;
2296 } else {
2297 instanceSize += fields[index]->type()->getObjectSize();
2298 }
2299 }
John Bauman66b8ab22014-05-06 15:57:45 -04002300
Nicolas Capens0bac2852016-05-07 06:09:58 -04002301 if (tempConstantNode) {
2302 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002303
Nicolas Capens0bac2852016-05-07 06:09:58 -04002304 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2305 } else {
2306 error(line, "Cannot offset into the structure", "Error");
2307 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002308
Alexis Hetuf0005a12016-09-28 15:52:21 -04002309 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002310 }
John Bauman66b8ab22014-05-06 15:57:45 -04002311
Nicolas Capens0bac2852016-05-07 06:09:58 -04002312 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002313}
2314
Alexis Hetuad6b8752015-06-09 16:15:30 -04002315//
Alexis Hetua35d8232015-06-11 17:11:06 -04002316// Interface/uniform blocks
2317//
2318TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
Nicolas Capens0bac2852016-05-07 06:09:58 -04002319 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
Alexis Hetua35d8232015-06-11 17:11:06 -04002320{
2321 if(reservedErrorCheck(nameLine, blockName))
2322 recover();
2323
2324 if(typeQualifier.qualifier != EvqUniform)
2325 {
2326 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2327 recover();
2328 }
2329
2330 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2331 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2332 {
2333 recover();
2334 }
2335
2336 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2337 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002338 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002339 }
2340
2341 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2342 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002343 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002344 }
2345
2346 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2347 if(!symbolTable.declare(*blockNameSymbol)) {
2348 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2349 recover();
2350 }
2351
2352 // check for sampler types and apply layout qualifiers
2353 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2354 TField* field = (*fieldList)[memberIndex];
2355 TType* fieldType = field->type();
2356 if(IsSampler(fieldType->getBasicType())) {
2357 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2358 recover();
2359 }
2360
2361 const TQualifier qualifier = fieldType->getQualifier();
2362 switch(qualifier)
2363 {
2364 case EvqGlobal:
2365 case EvqUniform:
2366 break;
2367 default:
2368 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2369 recover();
2370 break;
2371 }
2372
2373 // check layout qualifiers
2374 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2375 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2376 {
2377 recover();
2378 }
2379
2380 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2381 {
2382 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2383 recover();
2384 }
2385
2386 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2387 {
2388 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2389 }
2390 else if(!fieldType->isMatrix())
2391 {
2392 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
2393 recover();
2394 }
2395
2396 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2397 }
2398
2399 // add array index
2400 int arraySize = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002401 if(arrayIndex)
Alexis Hetua35d8232015-06-11 17:11:06 -04002402 {
2403 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2404 recover();
2405 }
2406
2407 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2408 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2409
2410 TString symbolName = "";
2411 int symbolId = 0;
2412
2413 if(!instanceName)
2414 {
2415 // define symbols for the members of the interface block
2416 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2417 {
2418 TField* field = (*fieldList)[memberIndex];
2419 TType* fieldType = field->type();
2420
2421 // set parent pointer of the field variable
2422 fieldType->setInterfaceBlock(interfaceBlock);
2423
2424 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2425 fieldVariable->setQualifier(typeQualifier.qualifier);
2426
2427 if(!symbolTable.declare(*fieldVariable)) {
2428 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2429 recover();
2430 }
2431 }
2432 }
2433 else
2434 {
2435 // add a symbol for this interface block
2436 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2437 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2438
2439 if(!symbolTable.declare(*instanceTypeDef)) {
2440 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2441 recover();
2442 }
2443
2444 symbolId = instanceTypeDef->getUniqueId();
2445 symbolName = instanceTypeDef->getName();
2446 }
2447
2448 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2449 aggregate->setOp(EOpDeclaration);
2450
2451 exitStructDeclaration();
2452 return aggregate;
2453}
2454
2455//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002456// Parse an array index expression
2457//
2458TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2459{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002460 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002461
2462 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2463 {
2464 if(baseExpression->getAsSymbolNode())
2465 {
2466 error(location, " left of '[' is not of type array, matrix, or vector ",
2467 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2468 }
2469 else
2470 {
2471 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2472 }
2473 recover();
2474 }
2475
2476 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2477
2478 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2479 {
2480 int index = indexConstantUnion->getIConst(0);
2481 if(index < 0)
2482 {
2483 std::stringstream infoStream;
2484 infoStream << index;
2485 std::string info = infoStream.str();
2486 error(location, "negative index", info.c_str());
2487 recover();
2488 index = 0;
2489 }
2490 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2491 {
2492 if(baseExpression->isArray())
2493 {
2494 // constant folding for arrays
2495 indexedExpression = addConstArrayNode(index, baseExpression, location);
2496 }
2497 else if(baseExpression->isVector())
2498 {
2499 // constant folding for vectors
2500 TVectorFields fields;
2501 fields.num = 1;
2502 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2503 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2504 }
2505 else if(baseExpression->isMatrix())
2506 {
2507 // constant folding for matrices
2508 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2509 }
2510 }
2511 else
2512 {
2513 int safeIndex = -1;
2514
2515 if(baseExpression->isArray())
2516 {
2517 if(index >= baseExpression->getType().getArraySize())
2518 {
2519 std::stringstream extraInfoStream;
2520 extraInfoStream << "array index out of range '" << index << "'";
2521 std::string extraInfo = extraInfoStream.str();
2522 error(location, "", "[", extraInfo.c_str());
2523 recover();
2524 safeIndex = baseExpression->getType().getArraySize() - 1;
2525 }
2526 }
2527 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2528 baseExpression->getType().getNominalSize() <= index)
2529 {
2530 std::stringstream extraInfoStream;
2531 extraInfoStream << "field selection out of range '" << index << "'";
2532 std::string extraInfo = extraInfoStream.str();
2533 error(location, "", "[", extraInfo.c_str());
2534 recover();
2535 safeIndex = baseExpression->getType().getNominalSize() - 1;
2536 }
2537
2538 // Don't modify the data of the previous constant union, because it can point
2539 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2540 if(safeIndex != -1)
2541 {
2542 ConstantUnion *safeConstantUnion = new ConstantUnion();
2543 safeConstantUnion->setIConst(safeIndex);
2544 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2545 }
2546
2547 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2548 }
2549 }
2550 else
2551 {
2552 if(baseExpression->isInterfaceBlock())
2553 {
2554 error(location, "",
2555 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2556 recover();
2557 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002558 else if(baseExpression->getQualifier() == EvqFragmentOut)
2559 {
2560 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2561 recover();
2562 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002563
2564 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2565 }
2566
2567 if(indexedExpression == 0)
2568 {
2569 ConstantUnion *unionArray = new ConstantUnion[1];
2570 unionArray->setFConst(0.0f);
2571 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2572 }
2573 else if(baseExpression->isArray())
2574 {
2575 const TType &baseType = baseExpression->getType();
2576 if(baseType.getStruct())
2577 {
2578 TType copyOfType(baseType.getStruct());
2579 indexedExpression->setType(copyOfType);
2580 }
2581 else if(baseType.isInterfaceBlock())
2582 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002583 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002584 indexedExpression->setType(copyOfType);
2585 }
2586 else
2587 {
2588 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2589 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2590 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2591 }
2592
2593 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2594 {
2595 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2596 }
2597 }
2598 else if(baseExpression->isMatrix())
2599 {
2600 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2601 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2602 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2603 }
2604 else if(baseExpression->isVector())
2605 {
2606 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2607 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2608 }
2609 else
2610 {
2611 indexedExpression->setType(baseExpression->getType());
2612 }
2613
2614 return indexedExpression;
2615}
2616
2617TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2618 const TString &fieldString, const TSourceLoc &fieldLocation)
2619{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002620 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002621
2622 if(baseExpression->isArray())
2623 {
2624 error(fieldLocation, "cannot apply dot operator to an array", ".");
2625 recover();
2626 }
2627
2628 if(baseExpression->isVector())
2629 {
2630 TVectorFields fields;
2631 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2632 {
2633 fields.num = 1;
2634 fields.offsets[0] = 0;
2635 recover();
2636 }
2637
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002638 if(baseExpression->getAsConstantUnion())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002639 {
2640 // constant folding for vector fields
2641 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2642 if(indexedExpression == 0)
2643 {
2644 recover();
2645 indexedExpression = baseExpression;
2646 }
2647 else
2648 {
2649 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2650 EvqConstExpr, (unsigned char)(fieldString).size()));
2651 }
2652 }
2653 else
2654 {
2655 TString vectorString = fieldString;
2656 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2657 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2658 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
Nicolas Capens341afbb2016-04-10 01:54:50 -04002659 baseExpression->getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary, (unsigned char)vectorString.size()));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002660 }
2661 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002662 else if(baseExpression->getBasicType() == EbtStruct)
2663 {
2664 bool fieldFound = false;
2665 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2666 if(fields.empty())
2667 {
2668 error(dotLocation, "structure has no fields", "Internal Error");
2669 recover();
2670 indexedExpression = baseExpression;
2671 }
2672 else
2673 {
2674 unsigned int i;
2675 for(i = 0; i < fields.size(); ++i)
2676 {
2677 if(fields[i]->name() == fieldString)
2678 {
2679 fieldFound = true;
2680 break;
2681 }
2682 }
2683 if(fieldFound)
2684 {
2685 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2686 {
2687 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2688 if(indexedExpression == 0)
2689 {
2690 recover();
2691 indexedExpression = baseExpression;
2692 }
2693 else
2694 {
2695 indexedExpression->setType(*fields[i]->type());
2696 // change the qualifier of the return type, not of the structure field
2697 // as the structure definition is shared between various structures.
2698 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2699 }
2700 }
2701 else
2702 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002703 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2704 index->setLine(fieldLocation);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002705 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2706 indexedExpression->setType(*fields[i]->type());
2707 }
2708 }
2709 else
2710 {
2711 error(dotLocation, " no such field in structure", fieldString.c_str());
2712 recover();
2713 indexedExpression = baseExpression;
2714 }
2715 }
2716 }
2717 else if(baseExpression->isInterfaceBlock())
2718 {
2719 bool fieldFound = false;
2720 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2721 if(fields.empty())
2722 {
2723 error(dotLocation, "interface block has no fields", "Internal Error");
2724 recover();
2725 indexedExpression = baseExpression;
2726 }
2727 else
2728 {
2729 unsigned int i;
2730 for(i = 0; i < fields.size(); ++i)
2731 {
2732 if(fields[i]->name() == fieldString)
2733 {
2734 fieldFound = true;
2735 break;
2736 }
2737 }
2738 if(fieldFound)
2739 {
2740 ConstantUnion *unionArray = new ConstantUnion[1];
2741 unionArray->setIConst(i);
2742 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2743 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2744 dotLocation);
2745 indexedExpression->setType(*fields[i]->type());
2746 }
2747 else
2748 {
2749 error(dotLocation, " no such field in interface block", fieldString.c_str());
2750 recover();
2751 indexedExpression = baseExpression;
2752 }
2753 }
2754 }
2755 else
2756 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002757 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002758 {
Nicolas Capensc09073f2017-11-09 09:49:03 -05002759 error(dotLocation, " field selection requires structure or vector on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002760 fieldString.c_str());
2761 }
2762 else
2763 {
2764 error(dotLocation,
Nicolas Capensc09073f2017-11-09 09:49:03 -05002765 " field selection requires structure, vector, or interface block on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002766 fieldString.c_str());
2767 }
2768 recover();
2769 indexedExpression = baseExpression;
2770 }
2771
2772 return indexedExpression;
2773}
2774
Nicolas Capens7d626792015-02-17 17:58:31 -05002775TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2776{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002777 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002778
Nicolas Capens0bac2852016-05-07 06:09:58 -04002779 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002780 qualifier.matrixPacking = EmpUnspecified;
2781 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002782
Alexis Hetuad6b8752015-06-09 16:15:30 -04002783 if(qualifierType == "shared")
2784 {
2785 qualifier.blockStorage = EbsShared;
2786 }
2787 else if(qualifierType == "packed")
2788 {
2789 qualifier.blockStorage = EbsPacked;
2790 }
2791 else if(qualifierType == "std140")
2792 {
2793 qualifier.blockStorage = EbsStd140;
2794 }
2795 else if(qualifierType == "row_major")
2796 {
2797 qualifier.matrixPacking = EmpRowMajor;
2798 }
2799 else if(qualifierType == "column_major")
2800 {
2801 qualifier.matrixPacking = EmpColumnMajor;
2802 }
2803 else if(qualifierType == "location")
Nicolas Capens0bac2852016-05-07 06:09:58 -04002804 {
2805 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2806 recover();
2807 }
2808 else
2809 {
2810 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2811 recover();
2812 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002813
Nicolas Capens0bac2852016-05-07 06:09:58 -04002814 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002815}
2816
2817TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2818{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002819 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002820
Nicolas Capens0bac2852016-05-07 06:09:58 -04002821 qualifier.location = -1;
2822 qualifier.matrixPacking = EmpUnspecified;
2823 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002824
Nicolas Capens0bac2852016-05-07 06:09:58 -04002825 if (qualifierType != "location")
2826 {
2827 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2828 recover();
2829 }
2830 else
2831 {
2832 // must check that location is non-negative
2833 if (intValue < 0)
2834 {
2835 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2836 recover();
2837 }
2838 else
2839 {
2840 qualifier.location = intValue;
2841 }
2842 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002843
Nicolas Capens0bac2852016-05-07 06:09:58 -04002844 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002845}
2846
2847TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2848{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002849 TLayoutQualifier joinedQualifier = leftQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002850
Nicolas Capens0bac2852016-05-07 06:09:58 -04002851 if (rightQualifier.location != -1)
2852 {
2853 joinedQualifier.location = rightQualifier.location;
2854 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002855 if(rightQualifier.matrixPacking != EmpUnspecified)
2856 {
2857 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2858 }
2859 if(rightQualifier.blockStorage != EbsUnspecified)
2860 {
2861 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2862 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002863
Nicolas Capens0bac2852016-05-07 06:09:58 -04002864 return joinedQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002865}
2866
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002867
2868TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2869 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2870{
2871 TQualifier mergedQualifier = EvqSmoothIn;
2872
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002873 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002874 if(interpolationQualifier == EvqSmooth)
2875 mergedQualifier = EvqSmoothIn;
2876 else if(interpolationQualifier == EvqFlat)
2877 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002878 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002879 }
2880 else if(storageQualifier == EvqCentroidIn) {
2881 if(interpolationQualifier == EvqSmooth)
2882 mergedQualifier = EvqCentroidIn;
2883 else if(interpolationQualifier == EvqFlat)
2884 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002885 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002886 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002887 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002888 if(interpolationQualifier == EvqSmooth)
2889 mergedQualifier = EvqSmoothOut;
2890 else if(interpolationQualifier == EvqFlat)
2891 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002892 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002893 }
2894 else if(storageQualifier == EvqCentroidOut) {
2895 if(interpolationQualifier == EvqSmooth)
2896 mergedQualifier = EvqCentroidOut;
2897 else if(interpolationQualifier == EvqFlat)
2898 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002899 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002900 }
2901 else {
2902 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2903 recover();
2904
2905 mergedQualifier = storageQualifier;
2906 }
2907
2908 TPublicType type;
2909 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2910 return type;
2911}
2912
Alexis Hetuad6b8752015-06-09 16:15:30 -04002913TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2914{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002915 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002916 {
2917 recover();
2918 }
2919
2920 for(unsigned int i = 0; i < fieldList->size(); ++i)
2921 {
2922 //
2923 // Careful not to replace already known aspects of type, like array-ness
2924 //
2925 TType *type = (*fieldList)[i]->type();
2926 type->setBasicType(typeSpecifier.type);
2927 type->setNominalSize(typeSpecifier.primarySize);
2928 type->setSecondarySize(typeSpecifier.secondarySize);
2929 type->setPrecision(typeSpecifier.precision);
2930 type->setQualifier(typeSpecifier.qualifier);
2931 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2932
2933 // don't allow arrays of arrays
2934 if(type->isArray())
2935 {
2936 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2937 recover();
2938 }
2939 if(typeSpecifier.array)
2940 type->setArraySize(typeSpecifier.arraySize);
2941 if(typeSpecifier.userDef)
2942 {
2943 type->setStruct(typeSpecifier.userDef->getStruct());
2944 }
2945
2946 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2947 {
2948 recover();
2949 }
2950 }
2951
2952 return fieldList;
2953}
2954
2955TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2956 const TString *structName, TFieldList *fieldList)
2957{
2958 TStructure *structure = new TStructure(structName, fieldList);
2959 TType *structureType = new TType(structure);
2960
2961 // Store a bool in the struct if we're at global scope, to allow us to
2962 // skip the local struct scoping workaround in HLSL.
2963 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2964 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2965
2966 if(!structName->empty())
2967 {
2968 if(reservedErrorCheck(nameLine, *structName))
2969 {
2970 recover();
2971 }
2972 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2973 if(!symbolTable.declare(*userTypeDef))
2974 {
2975 error(nameLine, "redefinition", structName->c_str(), "struct");
2976 recover();
2977 }
2978 }
2979
2980 // ensure we do not specify any storage qualifiers on the struct members
2981 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2982 {
2983 const TField &field = *(*fieldList)[typeListIndex];
2984 const TQualifier qualifier = field.type()->getQualifier();
2985 switch(qualifier)
2986 {
2987 case EvqGlobal:
2988 case EvqTemporary:
2989 break;
2990 default:
2991 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2992 recover();
2993 break;
2994 }
2995 }
2996
2997 TPublicType publicType;
2998 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2999 publicType.userDef = structureType;
3000 exitStructDeclaration();
3001
3002 return publicType;
3003}
3004
Alexis Hetufe1269e2015-06-16 12:43:32 -04003005bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04003006{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003007 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003008
Nicolas Capens0bac2852016-05-07 06:09:58 -04003009 // Embedded structure definitions are not supported per GLSL ES spec.
3010 // They aren't allowed in GLSL either, but we need to detect this here
3011 // so we don't rely on the GLSL compiler to catch it.
3012 if (mStructNestingLevel > 1) {
3013 error(line, "", "Embedded struct definitions are not allowed");
3014 return true;
3015 }
John Bauman66b8ab22014-05-06 15:57:45 -04003016
Nicolas Capens0bac2852016-05-07 06:09:58 -04003017 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04003018}
3019
3020void TParseContext::exitStructDeclaration()
3021{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003022 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003023}
3024
Alexis Hetuad6b8752015-06-09 16:15:30 -04003025bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3026{
3027 static const int kWebGLMaxStructNesting = 4;
3028
3029 if(field.type()->getBasicType() != EbtStruct)
3030 {
3031 return false;
3032 }
3033
3034 // We're already inside a structure definition at this point, so add
3035 // one to the field's struct nesting.
3036 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3037 {
3038 std::stringstream reasonStream;
3039 reasonStream << "Reference of struct type "
3040 << field.type()->getStruct()->name().c_str()
3041 << " exceeds maximum allowed nesting level of "
3042 << kWebGLMaxStructNesting;
3043 std::string reason = reasonStream.str();
3044 error(line, reason.c_str(), field.name().c_str(), "");
3045 return true;
3046 }
3047
3048 return false;
3049}
3050
3051TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3052{
3053 if(child == nullptr)
3054 {
3055 return nullptr;
3056 }
3057
3058 switch(op)
3059 {
3060 case EOpLogicalNot:
3061 if(child->getBasicType() != EbtBool ||
3062 child->isMatrix() ||
3063 child->isArray() ||
3064 child->isVector())
3065 {
3066 return nullptr;
3067 }
3068 break;
3069 case EOpBitwiseNot:
3070 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3071 child->isMatrix() ||
3072 child->isArray())
3073 {
3074 return nullptr;
3075 }
3076 break;
3077 case EOpPostIncrement:
3078 case EOpPreIncrement:
3079 case EOpPostDecrement:
3080 case EOpPreDecrement:
3081 case EOpNegative:
3082 if(child->getBasicType() == EbtStruct ||
3083 child->getBasicType() == EbtBool ||
3084 child->isArray())
3085 {
3086 return nullptr;
3087 }
3088 // Operators for built-ins are already type checked against their prototype.
3089 default:
3090 break;
3091 }
3092
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -04003093 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Alexis Hetuad6b8752015-06-09 16:15:30 -04003094}
3095
3096TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3097{
3098 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3099 if(node == nullptr)
3100 {
3101 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3102 recover();
3103 return child;
3104 }
3105 return node;
3106}
3107
3108TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3109{
3110 if(lValueErrorCheck(loc, getOperatorString(op), child))
3111 recover();
3112 return addUnaryMath(op, child, loc);
3113}
3114
3115bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3116{
3117 if(left->isArray() || right->isArray())
3118 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003119 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003120 {
3121 error(loc, "Invalid operation for arrays", getOperatorString(op));
3122 return false;
3123 }
3124
3125 if(left->isArray() != right->isArray())
3126 {
3127 error(loc, "array / non-array mismatch", getOperatorString(op));
3128 return false;
3129 }
3130
3131 switch(op)
3132 {
3133 case EOpEqual:
3134 case EOpNotEqual:
3135 case EOpAssign:
3136 case EOpInitialize:
3137 break;
3138 default:
3139 error(loc, "Invalid operation for arrays", getOperatorString(op));
3140 return false;
3141 }
3142 // At this point, size of implicitly sized arrays should be resolved.
3143 if(left->getArraySize() != right->getArraySize())
3144 {
3145 error(loc, "array size mismatch", getOperatorString(op));
3146 return false;
3147 }
3148 }
3149
3150 // Check ops which require integer / ivec parameters
3151 bool isBitShift = false;
3152 switch(op)
3153 {
3154 case EOpBitShiftLeft:
3155 case EOpBitShiftRight:
3156 case EOpBitShiftLeftAssign:
3157 case EOpBitShiftRightAssign:
3158 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3159 // check that the basic type is an integer type.
3160 isBitShift = true;
3161 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3162 {
3163 return false;
3164 }
3165 break;
3166 case EOpBitwiseAnd:
3167 case EOpBitwiseXor:
3168 case EOpBitwiseOr:
3169 case EOpBitwiseAndAssign:
3170 case EOpBitwiseXorAssign:
3171 case EOpBitwiseOrAssign:
3172 // It is enough to check the type of only one operand, since later it
3173 // is checked that the operand types match.
3174 if(!IsInteger(left->getBasicType()))
3175 {
3176 return false;
3177 }
3178 break;
3179 default:
3180 break;
3181 }
3182
3183 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3184 // So the basic type should usually match.
3185 if(!isBitShift && left->getBasicType() != right->getBasicType())
3186 {
3187 return false;
3188 }
3189
3190 // Check that type sizes match exactly on ops that require that.
3191 // Also check restrictions for structs that contain arrays or samplers.
3192 switch(op)
3193 {
3194 case EOpAssign:
3195 case EOpInitialize:
3196 case EOpEqual:
3197 case EOpNotEqual:
3198 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003199 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003200 {
3201 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3202 return false;
3203 }
3204 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3205 // we interpret the spec so that this extends to structs containing samplers,
3206 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003207 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003208 left->getType().isStructureContainingSamplers())
3209 {
3210 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3211 return false;
3212 }
3213 case EOpLessThan:
3214 case EOpGreaterThan:
3215 case EOpLessThanEqual:
3216 case EOpGreaterThanEqual:
3217 if((left->getNominalSize() != right->getNominalSize()) ||
3218 (left->getSecondarySize() != right->getSecondarySize()))
3219 {
3220 return false;
3221 }
Alexis Hetuec93b1d2016-12-09 16:01:29 -05003222 break;
3223 case EOpAdd:
3224 case EOpSub:
3225 case EOpDiv:
3226 case EOpIMod:
3227 case EOpBitShiftLeft:
3228 case EOpBitShiftRight:
3229 case EOpBitwiseAnd:
3230 case EOpBitwiseXor:
3231 case EOpBitwiseOr:
3232 case EOpAddAssign:
3233 case EOpSubAssign:
3234 case EOpDivAssign:
3235 case EOpIModAssign:
3236 case EOpBitShiftLeftAssign:
3237 case EOpBitShiftRightAssign:
3238 case EOpBitwiseAndAssign:
3239 case EOpBitwiseXorAssign:
3240 case EOpBitwiseOrAssign:
3241 if((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3242 {
3243 return false;
3244 }
3245
3246 // Are the sizes compatible?
3247 if(left->getNominalSize() != right->getNominalSize() || left->getSecondarySize() != right->getSecondarySize())
3248 {
3249 // If the nominal sizes of operands do not match:
3250 // One of them must be a scalar.
3251 if(!left->isScalar() && !right->isScalar())
3252 return false;
3253
3254 // In the case of compound assignment other than multiply-assign,
3255 // the right side needs to be a scalar. Otherwise a vector/matrix
3256 // would be assigned to a scalar. A scalar can't be shifted by a
3257 // vector either.
3258 if(!right->isScalar() && (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3259 return false;
3260 }
3261 break;
Alexis Hetuad6b8752015-06-09 16:15:30 -04003262 default:
3263 break;
3264 }
3265
3266 return true;
3267}
3268
Alexis Hetu76a343a2015-06-04 17:21:22 -04003269TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3270{
3271 TBasicType switchType = init->getBasicType();
3272 if((switchType != EbtInt && switchType != EbtUInt) ||
3273 init->isMatrix() ||
3274 init->isArray() ||
3275 init->isVector())
3276 {
3277 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3278 recover();
3279 return nullptr;
3280 }
3281
3282 if(statementList)
3283 {
3284 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3285 {
3286 recover();
3287 return nullptr;
3288 }
3289 }
3290
3291 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3292 if(node == nullptr)
3293 {
3294 error(loc, "erroneous switch statement", "switch");
3295 recover();
3296 return nullptr;
3297 }
3298 return node;
3299}
3300
3301TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3302{
Alexis Hetu0a655842015-06-22 16:52:11 -04003303 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003304 {
3305 error(loc, "case labels need to be inside switch statements", "case");
3306 recover();
3307 return nullptr;
3308 }
3309 if(condition == nullptr)
3310 {
3311 error(loc, "case label must have a condition", "case");
3312 recover();
3313 return nullptr;
3314 }
3315 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3316 condition->isMatrix() ||
3317 condition->isArray() ||
3318 condition->isVector())
3319 {
3320 error(condition->getLine(), "case label must be a scalar integer", "case");
3321 recover();
3322 }
3323 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3324 if(conditionConst == nullptr)
3325 {
3326 error(condition->getLine(), "case label must be constant", "case");
3327 recover();
3328 }
3329 TIntermCase *node = intermediate.addCase(condition, loc);
3330 if(node == nullptr)
3331 {
3332 error(loc, "erroneous case statement", "case");
3333 recover();
3334 return nullptr;
3335 }
3336 return node;
3337}
3338
3339TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3340{
Alexis Hetu0a655842015-06-22 16:52:11 -04003341 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003342 {
3343 error(loc, "default labels need to be inside switch statements", "default");
3344 recover();
3345 return nullptr;
3346 }
3347 TIntermCase *node = intermediate.addCase(nullptr, loc);
3348 if(node == nullptr)
3349 {
3350 error(loc, "erroneous default statement", "default");
3351 recover();
3352 return nullptr;
3353 }
3354 return node;
3355}
Alexis Hetue5246692015-06-18 12:34:52 -04003356TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3357{
3358 if(binaryOpCommonCheck(op, left, right, loc))
3359 {
3360 return intermediate.addAssign(op, left, right, loc);
3361 }
3362 return nullptr;
3363}
3364
3365TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3366{
3367 TIntermTyped *node = createAssign(op, left, right, loc);
3368 if(node == nullptr)
3369 {
3370 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3371 recover();
3372 return left;
3373 }
3374 return node;
3375}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003376
Alexis Hetub4769582015-06-16 12:19:50 -04003377TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3378 const TSourceLoc &loc)
3379{
3380 if(!binaryOpCommonCheck(op, left, right, loc))
3381 return nullptr;
3382
3383 switch(op)
3384 {
3385 case EOpEqual:
3386 case EOpNotEqual:
3387 break;
3388 case EOpLessThan:
3389 case EOpGreaterThan:
3390 case EOpLessThanEqual:
3391 case EOpGreaterThanEqual:
3392 ASSERT(!left->isArray() && !right->isArray());
3393 if(left->isMatrix() || left->isVector() ||
3394 left->getBasicType() == EbtStruct)
3395 {
3396 return nullptr;
3397 }
3398 break;
3399 case EOpLogicalOr:
3400 case EOpLogicalXor:
3401 case EOpLogicalAnd:
3402 ASSERT(!left->isArray() && !right->isArray());
3403 if(left->getBasicType() != EbtBool ||
3404 left->isMatrix() || left->isVector())
3405 {
3406 return nullptr;
3407 }
3408 break;
3409 case EOpAdd:
3410 case EOpSub:
3411 case EOpDiv:
3412 case EOpMul:
3413 ASSERT(!left->isArray() && !right->isArray());
3414 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3415 {
3416 return nullptr;
3417 }
3418 break;
3419 case EOpIMod:
3420 ASSERT(!left->isArray() && !right->isArray());
3421 // Note that this is only for the % operator, not for mod()
3422 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3423 {
3424 return nullptr;
3425 }
3426 break;
3427 // Note that for bitwise ops, type checking is done in promote() to
3428 // share code between ops and compound assignment
3429 default:
3430 break;
3431 }
3432
3433 return intermediate.addBinaryMath(op, left, right, loc);
3434}
3435
3436TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3437{
3438 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3439 if(node == 0)
3440 {
3441 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3442 recover();
3443 return left;
3444 }
3445 return node;
3446}
3447
3448TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3449{
3450 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3451 if(node == 0)
3452 {
3453 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3454 recover();
3455 ConstantUnion *unionArray = new ConstantUnion[1];
3456 unionArray->setBConst(false);
3457 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3458 }
3459 return node;
3460}
3461
Alexis Hetu76a343a2015-06-04 17:21:22 -04003462TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3463{
3464 switch(op)
3465 {
3466 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003467 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003468 {
3469 error(loc, "continue statement only allowed in loops", "");
3470 recover();
3471 }
3472 break;
3473 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003474 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003475 {
3476 error(loc, "break statement only allowed in loops and switch statements", "");
3477 recover();
3478 }
3479 break;
3480 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003481 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003482 {
3483 error(loc, "non-void function must return a value", "return");
3484 recover();
3485 }
3486 break;
3487 default:
3488 // No checks for discard
3489 break;
3490 }
3491 return intermediate.addBranch(op, loc);
3492}
3493
3494TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3495{
3496 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003497 mFunctionReturnsValue = true;
3498 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003499 {
3500 error(loc, "void function cannot return a value", "return");
3501 recover();
3502 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003503 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003504 {
3505 error(loc, "function return is not matching type:", "return");
3506 recover();
3507 }
3508 return intermediate.addBranch(op, returnValue, loc);
3509}
3510
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003511TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3512{
3513 *fatalError = false;
3514 TOperator op = fnCall->getBuiltInOp();
3515 TIntermTyped *callNode = nullptr;
3516
3517 if(thisNode != nullptr)
3518 {
3519 ConstantUnion *unionArray = new ConstantUnion[1];
3520 int arraySize = 0;
3521 TIntermTyped *typedThis = thisNode->getAsTyped();
3522 if(fnCall->getName() != "length")
3523 {
3524 error(loc, "invalid method", fnCall->getName().c_str());
3525 recover();
3526 }
3527 else if(paramNode != nullptr)
3528 {
3529 error(loc, "method takes no parameters", "length");
3530 recover();
3531 }
3532 else if(typedThis == nullptr || !typedThis->isArray())
3533 {
3534 error(loc, "length can only be called on arrays", "length");
3535 recover();
3536 }
3537 else
3538 {
3539 arraySize = typedThis->getArraySize();
3540 if(typedThis->getAsSymbolNode() == nullptr)
3541 {
3542 // This code path can be hit with expressions like these:
3543 // (a = b).length()
3544 // (func()).length()
3545 // (int[3](0, 1, 2)).length()
3546 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3547 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3548 // which allows "An array, vector or matrix expression with the length method applied".
3549 error(loc, "length can only be called on array names, not on array expressions", "length");
3550 recover();
3551 }
3552 }
3553 unionArray->setIConst(arraySize);
3554 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3555 }
3556 else if(op != EOpNull)
3557 {
3558 //
3559 // Then this should be a constructor.
3560 // Don't go through the symbol table for constructors.
3561 // Their parameters will be verified algorithmically.
3562 //
3563 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3564 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3565 {
3566 //
3567 // It's a constructor, of type 'type'.
3568 //
3569 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3570 }
3571
3572 if(callNode == nullptr)
3573 {
3574 recover();
3575 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3576 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003577 }
3578 else
3579 {
3580 //
3581 // Not a constructor. Find it in the symbol table.
3582 //
3583 const TFunction *fnCandidate;
3584 bool builtIn;
3585 fnCandidate = findFunction(loc, fnCall, &builtIn);
3586 if(fnCandidate)
3587 {
3588 //
3589 // A declared function.
3590 //
3591 if(builtIn && !fnCandidate->getExtension().empty() &&
3592 extensionErrorCheck(loc, fnCandidate->getExtension()))
3593 {
3594 recover();
3595 }
3596 op = fnCandidate->getBuiltInOp();
3597 if(builtIn && op != EOpNull)
3598 {
3599 //
3600 // A function call mapped to a built-in operation.
3601 //
3602 if(fnCandidate->getParamCount() == 1)
3603 {
3604 //
3605 // Treat it like a built-in unary operator.
3606 //
3607 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3608 if(callNode == nullptr)
3609 {
3610 std::stringstream extraInfoStream;
3611 extraInfoStream << "built in unary operator function. Type: "
3612 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3613 std::string extraInfo = extraInfoStream.str();
3614 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3615 *fatalError = true;
3616 return nullptr;
3617 }
3618 }
3619 else
3620 {
3621 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3622 aggregate->setType(fnCandidate->getReturnType());
3623
3624 // Some built-in functions have out parameters too.
3625 functionCallLValueErrorCheck(fnCandidate, aggregate);
3626
3627 callNode = aggregate;
Nicolas Capens91dfb972016-04-09 23:45:12 -04003628
3629 if(fnCandidate->getParamCount() == 2)
3630 {
3631 TIntermSequence &parameters = paramNode->getAsAggregate()->getSequence();
3632 TIntermTyped *left = parameters[0]->getAsTyped();
3633 TIntermTyped *right = parameters[1]->getAsTyped();
3634
3635 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
3636 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
3637 if (leftTempConstant && rightTempConstant)
3638 {
3639 TIntermTyped *typedReturnNode = leftTempConstant->fold(op, rightTempConstant, infoSink());
3640
3641 if(typedReturnNode)
3642 {
3643 callNode = typedReturnNode;
3644 }
3645 }
3646 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003647 }
3648 }
3649 else
3650 {
3651 // This is a real function call
3652
3653 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3654 aggregate->setType(fnCandidate->getReturnType());
3655
3656 // this is how we know whether the given function is a builtIn function or a user defined function
3657 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3658 // if builtIn == true, it's definitely a builtIn function with EOpNull
3659 if(!builtIn)
3660 aggregate->setUserDefined();
3661 aggregate->setName(fnCandidate->getMangledName());
3662
3663 callNode = aggregate;
3664
3665 functionCallLValueErrorCheck(fnCandidate, aggregate);
3666 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003667 }
3668 else
3669 {
3670 // error message was put out by findFunction()
3671 // Put on a dummy node for error recovery
3672 ConstantUnion *unionArray = new ConstantUnion[1];
3673 unionArray->setFConst(0.0f);
3674 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3675 recover();
3676 }
3677 }
3678 delete fnCall;
3679 return callNode;
3680}
3681
Alexis Hetueee212e2015-07-07 17:13:30 -04003682TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3683{
3684 if(boolErrorCheck(loc, cond))
3685 recover();
3686
3687 if(trueBlock->getType() != falseBlock->getType())
3688 {
3689 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3690 recover();
3691 return falseBlock;
3692 }
3693 // ESSL1 sections 5.2 and 5.7:
3694 // ESSL3 section 5.7:
3695 // Ternary operator is not among the operators allowed for structures/arrays.
3696 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3697 {
3698 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3699 recover();
3700 return falseBlock;
3701 }
3702 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3703}
3704
John Bauman66b8ab22014-05-06 15:57:45 -04003705//
3706// Parse an array of strings using yyparse.
3707//
3708// Returns 0 for success.
3709//
3710int PaParseStrings(int count, const char* const string[], const int length[],
Nicolas Capens0bac2852016-05-07 06:09:58 -04003711 TParseContext* context) {
3712 if ((count == 0) || !string)
3713 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003714
Nicolas Capens0bac2852016-05-07 06:09:58 -04003715 if (glslang_initialize(context))
3716 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003717
Nicolas Capens0bac2852016-05-07 06:09:58 -04003718 int error = glslang_scan(count, string, length, context);
3719 if (!error)
3720 error = glslang_parse(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003721
Nicolas Capens0bac2852016-05-07 06:09:58 -04003722 glslang_finalize(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003723
Nicolas Capens0bac2852016-05-07 06:09:58 -04003724 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003725}
3726
3727
3728