blob: c7100ba9b41cab71fefd9b90fc44dcc408e1e1a0 [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
Nicolas Capenscbd20d92017-12-19 00:07:50 -050017#include <limits>
John Bauman66b8ab22014-05-06 15:57:45 -040018#include <stdarg.h>
19#include <stdio.h>
20
Nicolas Capenscc863da2015-01-21 15:50:55 -050021#include "glslang.h"
22#include "preprocessor/SourceLocation.h"
Alexis Hetu76a343a2015-06-04 17:21:22 -040023#include "ValidateSwitch.h"
John Bauman66b8ab22014-05-06 15:57:45 -040024
25///////////////////////////////////////////////////////////////////////
26//
27// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Alexis Hetuec93b1d2016-12-09 16:01:29 -050031namespace
32{
33 bool IsVaryingOut(TQualifier qualifier)
34 {
35 switch(qualifier)
36 {
37 case EvqVaryingOut:
38 case EvqSmoothOut:
39 case EvqFlatOut:
40 case EvqCentroidOut:
41 case EvqVertexOut:
42 return true;
43
44 default: break;
45 }
46
47 return false;
48 }
49
50 bool IsVaryingIn(TQualifier qualifier)
51 {
52 switch(qualifier)
53 {
54 case EvqVaryingIn:
55 case EvqSmoothIn:
56 case EvqFlatIn:
57 case EvqCentroidIn:
58 case EvqFragmentIn:
59 return true;
60
61 default: break;
62 }
63
64 return false;
65 }
66
67 bool IsVarying(TQualifier qualifier)
68 {
69 return IsVaryingIn(qualifier) || IsVaryingOut(qualifier);
70 }
71
72 bool IsAssignment(TOperator op)
73 {
74 switch(op)
75 {
76 case EOpPostIncrement:
77 case EOpPostDecrement:
78 case EOpPreIncrement:
79 case EOpPreDecrement:
80 case EOpAssign:
81 case EOpAddAssign:
82 case EOpSubAssign:
83 case EOpMulAssign:
84 case EOpVectorTimesMatrixAssign:
85 case EOpVectorTimesScalarAssign:
86 case EOpMatrixTimesScalarAssign:
87 case EOpMatrixTimesMatrixAssign:
88 case EOpDivAssign:
89 case EOpIModAssign:
90 case EOpBitShiftLeftAssign:
91 case EOpBitShiftRightAssign:
92 case EOpBitwiseAndAssign:
93 case EOpBitwiseXorAssign:
94 case EOpBitwiseOrAssign:
95 return true;
96 default:
97 return false;
98 }
99 }
100}
101
John Bauman66b8ab22014-05-06 15:57:45 -0400102//
103// Look at a '.' field selector string and change it into offsets
104// for a vector.
105//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400106bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400107{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400108 fields.num = (int) compString.size();
109 if (fields.num > 4) {
110 error(line, "illegal vector field selection", compString.c_str());
111 return false;
112 }
John Bauman66b8ab22014-05-06 15:57:45 -0400113
Nicolas Capens0bac2852016-05-07 06:09:58 -0400114 enum {
115 exyzw,
116 ergba,
117 estpq
118 } fieldSet[4];
John Bauman66b8ab22014-05-06 15:57:45 -0400119
Nicolas Capens0bac2852016-05-07 06:09:58 -0400120 for (int i = 0; i < fields.num; ++i) {
121 switch (compString[i]) {
122 case 'x':
123 fields.offsets[i] = 0;
124 fieldSet[i] = exyzw;
125 break;
126 case 'r':
127 fields.offsets[i] = 0;
128 fieldSet[i] = ergba;
129 break;
130 case 's':
131 fields.offsets[i] = 0;
132 fieldSet[i] = estpq;
133 break;
134 case 'y':
135 fields.offsets[i] = 1;
136 fieldSet[i] = exyzw;
137 break;
138 case 'g':
139 fields.offsets[i] = 1;
140 fieldSet[i] = ergba;
141 break;
142 case 't':
143 fields.offsets[i] = 1;
144 fieldSet[i] = estpq;
145 break;
146 case 'z':
147 fields.offsets[i] = 2;
148 fieldSet[i] = exyzw;
149 break;
150 case 'b':
151 fields.offsets[i] = 2;
152 fieldSet[i] = ergba;
153 break;
154 case 'p':
155 fields.offsets[i] = 2;
156 fieldSet[i] = estpq;
157 break;
158 case 'w':
159 fields.offsets[i] = 3;
160 fieldSet[i] = exyzw;
161 break;
162 case 'a':
163 fields.offsets[i] = 3;
164 fieldSet[i] = ergba;
165 break;
166 case 'q':
167 fields.offsets[i] = 3;
168 fieldSet[i] = estpq;
169 break;
170 default:
171 error(line, "illegal vector field selection", compString.c_str());
172 return false;
173 }
174 }
John Bauman66b8ab22014-05-06 15:57:45 -0400175
Nicolas Capens0bac2852016-05-07 06:09:58 -0400176 for (int i = 0; i < fields.num; ++i) {
177 if (fields.offsets[i] >= vecSize) {
178 error(line, "vector field selection out of range", compString.c_str());
179 return false;
180 }
John Bauman66b8ab22014-05-06 15:57:45 -0400181
Nicolas Capens0bac2852016-05-07 06:09:58 -0400182 if (i > 0) {
183 if (fieldSet[i] != fieldSet[i-1]) {
184 error(line, "illegal - vector component fields not from the same set", compString.c_str());
185 return false;
186 }
187 }
188 }
John Bauman66b8ab22014-05-06 15:57:45 -0400189
Nicolas Capens0bac2852016-05-07 06:09:58 -0400190 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400191}
192
John Bauman66b8ab22014-05-06 15:57:45 -0400193///////////////////////////////////////////////////////////////////////
194//
195// Errors
196//
197////////////////////////////////////////////////////////////////////////
198
199//
200// Track whether errors have occurred.
201//
202void TParseContext::recover()
203{
204}
205
206//
207// Used by flex/bison to output all syntax and parsing errors.
208//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400209void TParseContext::error(const TSourceLoc& loc,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400210 const char* reason, const char* token,
211 const char* extraInfo)
John Bauman66b8ab22014-05-06 15:57:45 -0400212{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400213 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
214 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
215 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400216
217}
218
Alexis Hetufe1269e2015-06-16 12:43:32 -0400219void TParseContext::warning(const TSourceLoc& loc,
Nicolas Capens0bac2852016-05-07 06:09:58 -0400220 const char* reason, const char* token,
221 const char* extraInfo) {
222 pp::SourceLocation srcLoc(loc.first_file, loc.first_line);
223 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
224 srcLoc, reason, token, extraInfo);
John Bauman66b8ab22014-05-06 15:57:45 -0400225}
226
227void TParseContext::trace(const char* str)
228{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400229 mDiagnostics.writeDebug(str);
John Bauman66b8ab22014-05-06 15:57:45 -0400230}
231
232//
233// Same error message for all places assignments don't work.
234//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400235void TParseContext::assignError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400236{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400237 std::stringstream extraInfoStream;
238 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
239 std::string extraInfo = extraInfoStream.str();
240 error(line, "", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400241}
242
243//
244// Same error message for all places unary operations don't work.
245//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400246void TParseContext::unaryOpError(const TSourceLoc &line, const char* op, TString operand)
John Bauman66b8ab22014-05-06 15:57:45 -0400247{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400248 std::stringstream extraInfoStream;
249 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
250 << " (or there is no acceptable conversion)";
251 std::string extraInfo = extraInfoStream.str();
252 error(line, " wrong operand type", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400253}
254
255//
256// Same error message for all binary operations don't work.
257//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400258void TParseContext::binaryOpError(const TSourceLoc &line, const char* op, TString left, TString right)
John Bauman66b8ab22014-05-06 15:57:45 -0400259{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400260 std::stringstream extraInfoStream;
261 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
262 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
263 std::string extraInfo = extraInfoStream.str();
264 error(line, " wrong operand types ", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400265}
266
Alexis Hetufe1269e2015-06-16 12:43:32 -0400267bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type){
Nicolas Capens0bac2852016-05-07 06:09:58 -0400268 if (!mChecksPrecisionErrors)
269 return false;
270 switch( type ){
271 case EbtFloat:
272 if( precision == EbpUndefined ){
273 error( line, "No precision specified for (float)", "" );
274 return true;
275 }
276 break;
277 case EbtInt:
278 if( precision == EbpUndefined ){
279 error( line, "No precision specified (int)", "" );
280 return true;
281 }
282 break;
283 default:
284 return false;
285 }
286 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400287}
288
289//
290// Both test and if necessary, spit out an error, to see if the node is really
291// an l-value that can be operated on this way.
292//
293// Returns true if the was an error.
294//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400295bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char* op, TIntermTyped* node)
John Bauman66b8ab22014-05-06 15:57:45 -0400296{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400297 TIntermSymbol* symNode = node->getAsSymbolNode();
298 TIntermBinary* binaryNode = node->getAsBinaryNode();
John Bauman66b8ab22014-05-06 15:57:45 -0400299
Nicolas Capens0bac2852016-05-07 06:09:58 -0400300 if (binaryNode) {
301 bool errorReturn;
John Bauman66b8ab22014-05-06 15:57:45 -0400302
Nicolas Capens0bac2852016-05-07 06:09:58 -0400303 switch(binaryNode->getOp()) {
304 case EOpIndexDirect:
305 case EOpIndexIndirect:
306 case EOpIndexDirectStruct:
307 return lValueErrorCheck(line, op, binaryNode->getLeft());
308 case EOpVectorSwizzle:
309 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
310 if (!errorReturn) {
311 int offset[4] = {0,0,0,0};
John Bauman66b8ab22014-05-06 15:57:45 -0400312
Nicolas Capens0bac2852016-05-07 06:09:58 -0400313 TIntermTyped* rightNode = binaryNode->getRight();
314 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400315
Nicolas Capens0bac2852016-05-07 06:09:58 -0400316 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
317 p != aggrNode->getSequence().end(); p++) {
318 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
319 offset[value]++;
320 if (offset[value] > 1) {
321 error(line, " l-value of swizzle cannot have duplicate components", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400322
Nicolas Capens0bac2852016-05-07 06:09:58 -0400323 return true;
324 }
325 }
326 }
John Bauman66b8ab22014-05-06 15:57:45 -0400327
Nicolas Capens0bac2852016-05-07 06:09:58 -0400328 return errorReturn;
Nicolas Capens91b425b2017-11-15 14:39:15 -0500329 case EOpIndexDirectInterfaceBlock:
Nicolas Capens0bac2852016-05-07 06:09:58 -0400330 default:
331 break;
332 }
333 error(line, " l-value required", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400334
Nicolas Capens0bac2852016-05-07 06:09:58 -0400335 return true;
336 }
John Bauman66b8ab22014-05-06 15:57:45 -0400337
338
Nicolas Capens0bac2852016-05-07 06:09:58 -0400339 const char* symbol = 0;
340 if (symNode != 0)
341 symbol = symNode->getSymbol().c_str();
John Bauman66b8ab22014-05-06 15:57:45 -0400342
Nicolas Capens0bac2852016-05-07 06:09:58 -0400343 const char* message = 0;
344 switch (node->getQualifier()) {
345 case EvqConstExpr: message = "can't modify a const"; break;
346 case EvqConstReadOnly: message = "can't modify a const"; break;
347 case EvqAttribute: message = "can't modify an attribute"; break;
348 case EvqFragmentIn: message = "can't modify an input"; break;
349 case EvqVertexIn: message = "can't modify an input"; break;
350 case EvqUniform: message = "can't modify a uniform"; break;
351 case EvqSmoothIn:
352 case EvqFlatIn:
353 case EvqCentroidIn:
354 case EvqVaryingIn: message = "can't modify a varying"; break;
355 case EvqInput: message = "can't modify an input"; break;
356 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
357 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
358 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
359 case EvqInstanceID: message = "can't modify gl_InstanceID"; break;
Alexis Hetu877ddfc2017-07-25 17:48:00 -0400360 case EvqVertexID: message = "can't modify gl_VertexID"; break;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400361 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400362
Nicolas Capens0bac2852016-05-07 06:09:58 -0400363 //
364 // Type that can't be written to?
365 //
366 if(IsSampler(node->getBasicType()))
367 {
368 message = "can't modify a sampler";
369 }
370 else if(node->getBasicType() == EbtVoid)
371 {
372 message = "can't modify void";
373 }
374 }
John Bauman66b8ab22014-05-06 15:57:45 -0400375
Nicolas Capens0bac2852016-05-07 06:09:58 -0400376 if (message == 0 && binaryNode == 0 && symNode == 0) {
377 error(line, " l-value required", op);
John Bauman66b8ab22014-05-06 15:57:45 -0400378
Nicolas Capens0bac2852016-05-07 06:09:58 -0400379 return true;
380 }
John Bauman66b8ab22014-05-06 15:57:45 -0400381
382
Nicolas Capens0bac2852016-05-07 06:09:58 -0400383 //
384 // Everything else is okay, no error.
385 //
386 if (message == 0)
387 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400388
Nicolas Capens0bac2852016-05-07 06:09:58 -0400389 //
390 // If we get here, we have an error and a message.
391 //
392 if (symNode) {
393 std::stringstream extraInfoStream;
394 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
395 std::string extraInfo = extraInfoStream.str();
396 error(line, " l-value required", op, extraInfo.c_str());
397 }
398 else {
399 std::stringstream extraInfoStream;
400 extraInfoStream << "(" << message << ")";
401 std::string extraInfo = extraInfoStream.str();
402 error(line, " l-value required", op, extraInfo.c_str());
403 }
John Bauman66b8ab22014-05-06 15:57:45 -0400404
Nicolas Capens0bac2852016-05-07 06:09:58 -0400405 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400406}
407
408//
409// Both test, and if necessary spit out an error, to see if the node is really
410// a constant.
411//
412// Returns true if the was an error.
413//
414bool TParseContext::constErrorCheck(TIntermTyped* node)
415{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400416 if (node->getQualifier() == EvqConstExpr)
417 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400418
Nicolas Capens0bac2852016-05-07 06:09:58 -0400419 error(node->getLine(), "constant expression required", "");
John Bauman66b8ab22014-05-06 15:57:45 -0400420
Nicolas Capens0bac2852016-05-07 06:09:58 -0400421 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400422}
423
424//
425// Both test, and if necessary spit out an error, to see if the node is really
426// an integer.
427//
428// Returns true if the was an error.
429//
430bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
431{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400432 if (node->isScalarInt())
433 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400434
Nicolas Capens0bac2852016-05-07 06:09:58 -0400435 error(node->getLine(), "integer expression required", token);
John Bauman66b8ab22014-05-06 15:57:45 -0400436
Nicolas Capens0bac2852016-05-07 06:09:58 -0400437 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400438}
439
440//
441// Both test, and if necessary spit out an error, to see if we are currently
442// globally scoped.
443//
444// Returns true if the was an error.
445//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400446bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char* token)
John Bauman66b8ab22014-05-06 15:57:45 -0400447{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400448 if (global)
449 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400450
Nicolas Capens0bac2852016-05-07 06:09:58 -0400451 error(line, "only allowed at global scope", token);
John Bauman66b8ab22014-05-06 15:57:45 -0400452
Nicolas Capens0bac2852016-05-07 06:09:58 -0400453 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400454}
455
456//
457// For now, keep it simple: if it starts "gl_", it's reserved, independent
458// of scope. Except, if the symbol table is at the built-in push-level,
459// which is when we are parsing built-ins.
460// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
461// webgl shader.
462//
463// Returns true if there was an error.
464//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400465bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -0400466{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400467 static const char* reservedErrMsg = "reserved built-in name";
468 if (!symbolTable.atBuiltInLevel()) {
469 if (identifier.compare(0, 3, "gl_") == 0) {
470 error(line, reservedErrMsg, "gl_");
471 return true;
472 }
473 if (identifier.find("__") != TString::npos) {
474 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
475 return true;
476 }
477 }
John Bauman66b8ab22014-05-06 15:57:45 -0400478
Nicolas Capens0bac2852016-05-07 06:09:58 -0400479 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400480}
481
482//
483// Make sure there is enough data provided to the constructor to build
484// something of the type of the constructor. Also returns the type of
485// the constructor.
486//
487// Returns true if there was an error in construction.
488//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400489bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400490{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400491 *type = function.getReturnType();
John Bauman66b8ab22014-05-06 15:57:45 -0400492
Nicolas Capens0bac2852016-05-07 06:09:58 -0400493 bool constructingMatrix = false;
494 switch(op) {
495 case EOpConstructMat2:
496 case EOpConstructMat2x3:
497 case EOpConstructMat2x4:
498 case EOpConstructMat3x2:
499 case EOpConstructMat3:
500 case EOpConstructMat3x4:
501 case EOpConstructMat4x2:
502 case EOpConstructMat4x3:
503 case EOpConstructMat4:
504 constructingMatrix = true;
505 break;
506 default:
507 break;
508 }
John Bauman66b8ab22014-05-06 15:57:45 -0400509
Nicolas Capens0bac2852016-05-07 06:09:58 -0400510 //
511 // Note: It's okay to have too many components available, but not okay to have unused
512 // arguments. 'full' will go to true when enough args have been seen. If we loop
513 // again, there is an extra argument, so 'overfull' will become true.
514 //
John Bauman66b8ab22014-05-06 15:57:45 -0400515
Nicolas Capens0bac2852016-05-07 06:09:58 -0400516 size_t size = 0;
517 bool full = false;
518 bool overFull = false;
519 bool matrixInMatrix = false;
520 bool arrayArg = false;
521 for (size_t i = 0; i < function.getParamCount(); ++i) {
522 const TParameter& param = function.getParam(i);
523 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400524
Nicolas Capens0bac2852016-05-07 06:09:58 -0400525 if (constructingMatrix && param.type->isMatrix())
526 matrixInMatrix = true;
527 if (full)
528 overFull = true;
529 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
530 full = true;
531 if (param.type->isArray())
532 arrayArg = true;
533 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400534
Nicolas Capens0bac2852016-05-07 06:09:58 -0400535 if(type->isArray()) {
536 if(type->getArraySize() == 0) {
537 type->setArraySize(function.getParamCount());
538 } else if(type->getArraySize() != (int)function.getParamCount()) {
539 error(line, "array constructor needs one argument per array element", "constructor");
540 return true;
541 }
542 }
John Bauman66b8ab22014-05-06 15:57:45 -0400543
Nicolas Capens0bac2852016-05-07 06:09:58 -0400544 if (arrayArg && op != EOpConstructStruct) {
545 error(line, "constructing from a non-dereferenced array", "constructor");
546 return true;
547 }
John Bauman66b8ab22014-05-06 15:57:45 -0400548
Nicolas Capens0bac2852016-05-07 06:09:58 -0400549 if (matrixInMatrix && !type->isArray()) {
550 if (function.getParamCount() != 1) {
551 error(line, "constructing matrix from matrix can only take one argument", "constructor");
552 return true;
553 }
554 }
John Bauman66b8ab22014-05-06 15:57:45 -0400555
Nicolas Capens0bac2852016-05-07 06:09:58 -0400556 if (overFull) {
557 error(line, "too many arguments", "constructor");
558 return true;
559 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400560
Nicolas Capens0bac2852016-05-07 06:09:58 -0400561 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
562 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
563 return true;
564 }
John Bauman66b8ab22014-05-06 15:57:45 -0400565
Nicolas Capens0bac2852016-05-07 06:09:58 -0400566 if (!type->isMatrix() || !matrixInMatrix) {
567 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
568 (op == EOpConstructStruct && size < type->getObjectSize())) {
569 error(line, "not enough data provided for construction", "constructor");
570 return true;
571 }
572 }
John Bauman66b8ab22014-05-06 15:57:45 -0400573
Nicolas Capens0bac2852016-05-07 06:09:58 -0400574 TIntermTyped *typed = node ? node->getAsTyped() : 0;
575 if (typed == 0) {
576 error(line, "constructor argument does not have a type", "constructor");
577 return true;
578 }
579 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
580 error(line, "cannot convert a sampler", "constructor");
581 return true;
582 }
583 if (typed->getBasicType() == EbtVoid) {
584 error(line, "cannot convert a void", "constructor");
585 return true;
586 }
John Bauman66b8ab22014-05-06 15:57:45 -0400587
Nicolas Capens0bac2852016-05-07 06:09:58 -0400588 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400589}
590
591// This function checks to see if a void variable has been declared and raise an error message for such a case
592//
593// returns true in case of an error
594//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400595bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString& identifier, const TBasicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400596{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400597 if(type == EbtVoid) {
598 error(line, "illegal use of type 'void'", identifier.c_str());
599 return true;
600 }
John Bauman66b8ab22014-05-06 15:57:45 -0400601
Nicolas Capens0bac2852016-05-07 06:09:58 -0400602 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400603}
604
605// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
606//
607// returns true in case of an error
608//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400609bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped* type)
John Bauman66b8ab22014-05-06 15:57:45 -0400610{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400611 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
612 error(line, "boolean expression expected", "");
613 return true;
614 }
John Bauman66b8ab22014-05-06 15:57:45 -0400615
Nicolas Capens0bac2852016-05-07 06:09:58 -0400616 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400617}
618
619// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
620//
621// returns true in case of an error
622//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400623bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400624{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400625 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
626 error(line, "boolean expression expected", "");
627 return true;
628 }
John Bauman66b8ab22014-05-06 15:57:45 -0400629
Nicolas Capens0bac2852016-05-07 06:09:58 -0400630 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400631}
632
Alexis Hetufe1269e2015-06-16 12:43:32 -0400633bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType& pType, const char* reason)
John Bauman66b8ab22014-05-06 15:57:45 -0400634{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400635 if (pType.type == EbtStruct) {
636 if (containsSampler(*pType.userDef)) {
637 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400638
Nicolas Capens0bac2852016-05-07 06:09:58 -0400639 return true;
640 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400641
Nicolas Capens0bac2852016-05-07 06:09:58 -0400642 return false;
643 } else if (IsSampler(pType.type)) {
644 error(line, reason, getBasicString(pType.type));
John Bauman66b8ab22014-05-06 15:57:45 -0400645
Nicolas Capens0bac2852016-05-07 06:09:58 -0400646 return true;
647 }
John Bauman66b8ab22014-05-06 15:57:45 -0400648
Nicolas Capens0bac2852016-05-07 06:09:58 -0400649 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400650}
651
Alexis Hetufe1269e2015-06-16 12:43:32 -0400652bool TParseContext::structQualifierErrorCheck(const TSourceLoc &line, const TPublicType& pType)
John Bauman66b8ab22014-05-06 15:57:45 -0400653{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400654 switch(pType.qualifier)
655 {
656 case EvqVaryingOut:
657 case EvqSmooth:
658 case EvqFlat:
659 case EvqCentroidOut:
660 case EvqVaryingIn:
661 case EvqSmoothIn:
662 case EvqFlatIn:
663 case EvqCentroidIn:
664 case EvqAttribute:
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400665 case EvqVertexIn:
666 case EvqFragmentOut:
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400667 if(pType.type == EbtStruct)
668 {
669 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400670
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400671 return true;
672 }
673 break;
674 default:
675 break;
676 }
John Bauman66b8ab22014-05-06 15:57:45 -0400677
Nicolas Capens0bac2852016-05-07 06:09:58 -0400678 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
679 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400680
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400681 // check for layout qualifier issues
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400682 if (pType.qualifier != EvqVertexIn && pType.qualifier != EvqFragmentOut &&
Nicolas Capens0bac2852016-05-07 06:09:58 -0400683 layoutLocationErrorCheck(line, pType.layoutQualifier))
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400684 {
685 return true;
686 }
687
Nicolas Capens0bac2852016-05-07 06:09:58 -0400688 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400689}
690
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400691// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
692// declaration.
693//
694bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
695{
696 switch(publicType.qualifier)
697 {
698 case EvqVaryingIn:
699 case EvqVaryingOut:
700 case EvqAttribute:
701 case EvqVertexIn:
702 case EvqFragmentOut:
703 if(publicType.type == EbtStruct)
704 {
705 error(identifierLocation, "cannot be used with a structure",
706 getQualifierString(publicType.qualifier));
707 return true;
708 }
709
710 default: break;
711 }
712
713 if(publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
714 "samplers must be uniform"))
715 {
716 return true;
717 }
718
719 // check for layout qualifier issues
720 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
721
722 if(layoutQualifier.matrixPacking != EmpUnspecified)
723 {
724 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
725 "only valid for interface blocks");
726 return true;
727 }
728
729 if(layoutQualifier.blockStorage != EbsUnspecified)
730 {
731 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
732 "only valid for interface blocks");
733 return true;
734 }
735
736 if(publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
737 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
738 {
739 return true;
740 }
741
742 return false;
743}
744
Nicolas Capens3713cd42015-06-22 10:41:54 -0400745bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
746{
747 if(layoutQualifier.location != -1)
748 {
749 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
750 return true;
751 }
752
753 return false;
754}
Alexis Hetu42ff6b12015-06-03 16:03:48 -0400755
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400756bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
757{
758 if(pType.layoutQualifier.location != -1)
759 {
760 error(line, "location must only be specified for a single input or output variable", "location");
761 return true;
762 }
763
764 return false;
765}
766
Alexis Hetufe1269e2015-06-16 12:43:32 -0400767bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400768{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400769 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
770 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
771 error(line, "samplers cannot be output parameters", type.getBasicString());
772 return true;
773 }
John Bauman66b8ab22014-05-06 15:57:45 -0400774
Nicolas Capens0bac2852016-05-07 06:09:58 -0400775 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400776}
777
778bool TParseContext::containsSampler(TType& type)
779{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400780 if (IsSampler(type.getBasicType()))
781 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400782
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500783 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
Alexis Hetud2742532018-01-23 16:53:41 -0500784 for(const auto &field : type.getStruct()->fields()) {
785 if (containsSampler(*(field->type())))
Nicolas Capens0bac2852016-05-07 06:09:58 -0400786 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);
Nicolas Capens0b7471a2018-01-09 21:39:21 -05001020 if(!symbolTable.declare(*variable))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001021 {
1022 error(line, "redefinition", identifier.c_str());
1023 delete (*variable);
1024 (*variable) = nullptr;
1025 return false;
1026 }
1027
1028 if(voidErrorCheck(line, identifier, type.getBasicType()))
1029 return false;
1030
1031 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001032}
1033
Alexis Hetufe1269e2015-06-16 12:43:32 -04001034bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001035{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001036 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
1037 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
1038 return true;
1039 }
1040 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
1041 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
1042 return true;
1043 }
John Bauman66b8ab22014-05-06 15:57:45 -04001044
Nicolas Capens0bac2852016-05-07 06:09:58 -04001045 if (qualifier == EvqConstReadOnly)
1046 type->setQualifier(EvqConstReadOnly);
1047 else
1048 type->setQualifier(paramQualifier);
John Bauman66b8ab22014-05-06 15:57:45 -04001049
Nicolas Capens0bac2852016-05-07 06:09:58 -04001050 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001051}
1052
Alexis Hetufe1269e2015-06-16 12:43:32 -04001053bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString& extension)
John Bauman66b8ab22014-05-06 15:57:45 -04001054{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001055 const TExtensionBehavior& extBehavior = extensionBehavior();
1056 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
1057 if (iter == extBehavior.end()) {
1058 error(line, "extension", extension.c_str(), "is not supported");
1059 return true;
1060 }
1061 // In GLSL ES, an extension's default behavior is "disable".
1062 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
1063 error(line, "extension", extension.c_str(), "is disabled");
1064 return true;
1065 }
1066 if (iter->second == EBhWarn) {
1067 warning(line, "extension", extension.c_str(), "is being used");
1068 return false;
1069 }
John Bauman66b8ab22014-05-06 15:57:45 -04001070
Nicolas Capens0bac2852016-05-07 06:09:58 -04001071 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001072}
1073
Alexis Hetuad6b8752015-06-09 16:15:30 -04001074bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1075{
1076 for(size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1077 {
1078 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1079 if(qual == EvqOut || qual == EvqInOut)
1080 {
1081 TIntermTyped *node = (aggregate->getSequence())[i]->getAsTyped();
1082 if(lValueErrorCheck(node->getLine(), "assign", node))
1083 {
1084 error(node->getLine(),
1085 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1086 recover();
1087 return true;
1088 }
1089 }
1090 }
1091 return false;
1092}
1093
Alexis Hetuad527752015-07-07 13:31:44 -04001094void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1095{
1096 switch(qualifier)
1097 {
1098 case EvqVaryingOut:
1099 case EvqSmoothOut:
1100 case EvqFlatOut:
1101 case EvqCentroidOut:
1102 case EvqVertexOut:
1103 case EvqFragmentOut:
1104 break;
1105 default:
1106 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1107 recover();
1108 break;
1109 }
1110}
1111
John Bauman66b8ab22014-05-06 15:57:45 -04001112bool TParseContext::supportsExtension(const char* extension)
1113{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001114 const TExtensionBehavior& extbehavior = extensionBehavior();
1115 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1116 return (iter != extbehavior.end());
John Bauman66b8ab22014-05-06 15:57:45 -04001117}
1118
Alexis Hetufe1269e2015-06-16 12:43:32 -04001119void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001120{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001121 pp::SourceLocation loc(line.first_file, line.first_line);
1122 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001123}
1124
Alexis Hetue13238e2017-12-15 18:01:07 -05001125void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value, bool stdgl)
John Bauman66b8ab22014-05-06 15:57:45 -04001126{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001127 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetue13238e2017-12-15 18:01:07 -05001128 mDirectiveHandler.handlePragma(loc, name, value, stdgl);
John Bauman66b8ab22014-05-06 15:57:45 -04001129}
1130
1131/////////////////////////////////////////////////////////////////////////////////
1132//
1133// Non-Errors.
1134//
1135/////////////////////////////////////////////////////////////////////////////////
1136
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001137const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1138 const TString *name,
1139 const TSymbol *symbol)
1140{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001141 const TVariable *variable = nullptr;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001142
1143 if(!symbol)
1144 {
1145 error(location, "undeclared identifier", name->c_str());
1146 recover();
1147 }
1148 else if(!symbol->isVariable())
1149 {
1150 error(location, "variable expected", name->c_str());
1151 recover();
1152 }
1153 else
1154 {
1155 variable = static_cast<const TVariable*>(symbol);
1156
Alexis Hetu0a655842015-06-22 16:52:11 -04001157 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001158 {
1159 recover();
1160 }
1161
1162 // Reject shaders using both gl_FragData and gl_FragColor
1163 TQualifier qualifier = variable->getType().getQualifier();
1164 if(qualifier == EvqFragData)
1165 {
1166 mUsesFragData = true;
1167 }
1168 else if(qualifier == EvqFragColor)
1169 {
1170 mUsesFragColor = true;
1171 }
1172
1173 // This validation is not quite correct - it's only an error to write to
1174 // both FragData and FragColor. For simplicity, and because users shouldn't
Nicolas Capens8b124c12016-04-18 14:09:37 -04001175 // be rewarded for reading from undefined variables, return an error
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001176 // if they are both referenced, rather than assigned.
1177 if(mUsesFragData && mUsesFragColor)
1178 {
1179 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1180 recover();
1181 }
1182 }
1183
1184 if(!variable)
1185 {
1186 TType type(EbtFloat, EbpUndefined);
1187 TVariable *fakeVariable = new TVariable(name, type);
Nicolas Capens0b7471a2018-01-09 21:39:21 -05001188 symbolTable.declare(fakeVariable);
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001189 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);
Nicolas Capens0b7471a2018-01-09 21:39:21 -05001205 if (!symbol || symbol->isFunction()) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001206 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
1207 }
John Bauman66b8ab22014-05-06 15:57:45 -04001208
Nicolas Capens0b7471a2018-01-09 21:39:21 -05001209 if (!symbol) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001210 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 Capens7cbb1de2017-12-22 08:54:18 -05001299 // Constants which aren't indexable arrays get propagated by value
1300 // and thus don't need to initialize the symbol.
1301 if (variable->isConstant() && !(type.isArray() && type.getArraySize() > 1))
1302 {
1303 *intermNode = nullptr;
1304 }
1305 else
1306 {
Nicolas Capens0bac2852016-05-07 06:09:58 -04001307 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1308 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1309 if(*intermNode == nullptr) {
1310 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1311 return true;
1312 }
Nicolas Capens7cbb1de2017-12-22 08:54:18 -05001313 }
John Bauman66b8ab22014-05-06 15:57:45 -04001314
Nicolas Capens0bac2852016-05-07 06:09:58 -04001315 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001316}
1317
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001318TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1319{
1320 TPublicType returnType = typeSpecifier;
1321 returnType.qualifier = qualifier;
1322 returnType.invariant = invariant;
1323 returnType.layoutQualifier = layoutQualifier;
1324
Alexis Hetu0a655842015-06-22 16:52:11 -04001325 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001326 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001327 if(typeSpecifier.array)
1328 {
1329 error(typeSpecifier.line, "not supported", "first-class array");
1330 returnType.clearArrayness();
1331 }
1332
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001333 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1334 {
1335 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1336 recover();
1337 }
1338
1339 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1340 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1341 {
1342 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1343 recover();
1344 }
1345 }
1346 else
1347 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001348 if(!returnType.layoutQualifier.isEmpty())
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001349 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001350 globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout");
1351 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001352
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001353 if(IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn || returnType.qualifier == EvqFragmentOut)
1354 {
1355 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier, typeSpecifier.line);
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001356 }
1357 }
1358
1359 return returnType;
1360}
1361
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001362void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1363 const TPublicType &type,
1364 const TSourceLoc &qualifierLocation)
1365{
1366 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1367 if(type.type == EbtBool)
1368 {
1369 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1370 }
1371
1372 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1373 switch(qualifier)
1374 {
1375 case EvqVertexIn:
1376 // ESSL 3.00 section 4.3.4
1377 if(type.array)
1378 {
1379 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1380 }
1381 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1382 return;
1383 case EvqFragmentOut:
1384 // ESSL 3.00 section 4.3.6
1385 if(type.isMatrix())
1386 {
1387 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1388 }
1389 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1390 return;
1391 default:
1392 break;
1393 }
1394
1395 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1396 // restrictions.
1397 bool typeContainsIntegers = (type.type == EbtInt || type.type == EbtUInt ||
1398 type.isStructureContainingType(EbtInt) ||
1399 type.isStructureContainingType(EbtUInt));
1400 if(typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1401 {
1402 error(qualifierLocation, "must use 'flat' interpolation here", getQualifierString(qualifier));
1403 }
1404
1405 if(type.type == EbtStruct)
1406 {
1407 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1408 // These restrictions are only implied by the ESSL 3.00 spec, but
1409 // the ESSL 3.10 spec lists these restrictions explicitly.
1410 if(type.array)
1411 {
1412 error(qualifierLocation, "cannot be an array of structures", getQualifierString(qualifier));
1413 }
1414 if(type.isStructureContainingArrays())
1415 {
1416 error(qualifierLocation, "cannot be a structure containing an array", getQualifierString(qualifier));
1417 }
1418 if(type.isStructureContainingType(EbtStruct))
1419 {
1420 error(qualifierLocation, "cannot be a structure containing a structure", getQualifierString(qualifier));
1421 }
1422 if(type.isStructureContainingType(EbtBool))
1423 {
1424 error(qualifierLocation, "cannot be a structure containing a bool", getQualifierString(qualifier));
1425 }
1426 }
1427}
1428
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001429TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1430 const TSourceLoc &identifierOrTypeLocation,
1431 const TString &identifier)
1432{
1433 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1434
1435 bool emptyDeclaration = (identifier == "");
1436
1437 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1438
1439 if(emptyDeclaration)
1440 {
1441 if(publicType.isUnsizedArray())
1442 {
1443 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1444 // It is assumed that this applies to empty declarations as well.
1445 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1446 }
1447 }
1448 else
1449 {
1450 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1451 recover();
1452
1453 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1454 recover();
1455
1456 TVariable *variable = nullptr;
1457 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1458 recover();
1459
1460 if(variable && symbol)
1461 symbol->setId(variable->getUniqueId());
1462 }
1463
1464 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1465}
1466
1467TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1468 const TSourceLoc &identifierLocation,
1469 const TString &identifier,
1470 const TSourceLoc &indexLocation,
1471 TIntermTyped *indexExpression)
1472{
1473 mDeferredSingleDeclarationErrorCheck = false;
1474
1475 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1476 recover();
1477
1478 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1479 recover();
1480
1481 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1482 {
1483 recover();
1484 }
1485
1486 TType arrayType(publicType);
1487
1488 int size;
1489 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1490 {
1491 recover();
1492 }
1493 // Make the type an array even if size check failed.
1494 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1495 arrayType.setArraySize(size);
1496
1497 TVariable *variable = nullptr;
1498 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1499 recover();
1500
1501 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1502 if(variable && symbol)
1503 symbol->setId(variable->getUniqueId());
1504
1505 return intermediate.makeAggregate(symbol, identifierLocation);
1506}
1507
1508TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1509 const TSourceLoc &identifierLocation,
1510 const TString &identifier,
1511 const TSourceLoc &initLocation,
1512 TIntermTyped *initializer)
1513{
1514 mDeferredSingleDeclarationErrorCheck = false;
1515
1516 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1517 recover();
1518
1519 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001520 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001521 {
1522 //
1523 // Build intermediate representation
1524 //
1525 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1526 }
1527 else
1528 {
1529 recover();
1530 return nullptr;
1531 }
1532}
1533
1534TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1535 const TSourceLoc &identifierLocation,
1536 const TString &identifier,
1537 const TSourceLoc &indexLocation,
1538 TIntermTyped *indexExpression,
1539 const TSourceLoc &initLocation,
1540 TIntermTyped *initializer)
1541{
1542 mDeferredSingleDeclarationErrorCheck = false;
1543
1544 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1545 recover();
1546
1547 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1548 {
1549 recover();
1550 }
1551
1552 TPublicType arrayType(publicType);
1553
1554 int size = 0;
1555 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1556 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1557 {
1558 recover();
1559 }
1560 // Make the type an array even if size check failed.
1561 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1562 arrayType.setArray(true, size);
1563
1564 // initNode will correspond to the whole of "type b[n] = initializer".
1565 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001566 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001567 {
1568 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1569 }
1570 else
1571 {
1572 recover();
1573 return nullptr;
1574 }
1575}
1576
1577TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1578 const TSourceLoc &identifierLoc,
1579 const TString *identifier,
1580 const TSymbol *symbol)
1581{
1582 // invariant declaration
1583 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1584 {
1585 recover();
1586 }
1587
1588 if(!symbol)
1589 {
1590 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1591 recover();
1592 return nullptr;
1593 }
1594 else
1595 {
1596 const TString kGlFrontFacing("gl_FrontFacing");
1597 if(*identifier == kGlFrontFacing)
1598 {
1599 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1600 recover();
1601 return nullptr;
1602 }
1603 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1604 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1605 ASSERT(variable);
1606 const TType &type = variable->getType();
1607 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1608 *identifier, type, identifierLoc);
1609
1610 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1611 aggregate->setOp(EOpInvariantDeclaration);
1612 return aggregate;
1613 }
1614}
1615
1616TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1617 const TSourceLoc &identifierLocation, const TString &identifier)
1618{
1619 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1620 if(mDeferredSingleDeclarationErrorCheck)
1621 {
1622 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1623 recover();
1624 mDeferredSingleDeclarationErrorCheck = false;
1625 }
1626
1627 if(locationDeclaratorListCheck(identifierLocation, publicType))
1628 recover();
1629
1630 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1631 recover();
1632
1633 TVariable *variable = nullptr;
1634 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1635 recover();
1636
1637 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1638 if(variable && symbol)
1639 symbol->setId(variable->getUniqueId());
1640
1641 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1642}
1643
1644TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1645 const TSourceLoc &identifierLocation, const TString &identifier,
1646 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1647{
1648 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1649 if(mDeferredSingleDeclarationErrorCheck)
1650 {
1651 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1652 recover();
1653 mDeferredSingleDeclarationErrorCheck = false;
1654 }
1655
1656 if(locationDeclaratorListCheck(identifierLocation, publicType))
1657 recover();
1658
1659 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1660 recover();
1661
1662 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1663 {
1664 recover();
1665 }
1666 else
1667 {
1668 TType arrayType = TType(publicType);
1669 int size;
1670 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1671 {
1672 recover();
1673 }
1674 arrayType.setArraySize(size);
1675
1676 TVariable *variable = nullptr;
1677 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1678 recover();
1679
1680 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1681 if(variable && symbol)
1682 symbol->setId(variable->getUniqueId());
1683
1684 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1685 }
1686
1687 return nullptr;
1688}
1689
1690TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1691 const TSourceLoc &identifierLocation, const TString &identifier,
1692 const TSourceLoc &initLocation, TIntermTyped *initializer)
1693{
1694 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1695 if(mDeferredSingleDeclarationErrorCheck)
1696 {
1697 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1698 recover();
1699 mDeferredSingleDeclarationErrorCheck = false;
1700 }
1701
1702 if(locationDeclaratorListCheck(identifierLocation, publicType))
1703 recover();
1704
1705 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001706 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001707 {
1708 //
1709 // build the intermediate representation
1710 //
1711 if(intermNode)
1712 {
1713 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1714 }
1715 else
1716 {
1717 return aggregateDeclaration;
1718 }
1719 }
1720 else
1721 {
1722 recover();
1723 return nullptr;
1724 }
1725}
1726
1727TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1728 TIntermAggregate *aggregateDeclaration,
1729 const TSourceLoc &identifierLocation,
1730 const TString &identifier,
1731 const TSourceLoc &indexLocation,
1732 TIntermTyped *indexExpression,
1733 const TSourceLoc &initLocation, TIntermTyped *initializer)
1734{
1735 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1736 if(mDeferredSingleDeclarationErrorCheck)
1737 {
1738 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1739 recover();
1740 mDeferredSingleDeclarationErrorCheck = false;
1741 }
1742
1743 if(locationDeclaratorListCheck(identifierLocation, publicType))
1744 recover();
1745
1746 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1747 {
1748 recover();
1749 }
1750
1751 TPublicType arrayType(publicType);
1752
1753 int size = 0;
1754 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1755 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1756 {
1757 recover();
1758 }
1759 // Make the type an array even if size check failed.
1760 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1761 arrayType.setArray(true, size);
1762
1763 // initNode will correspond to the whole of "b[n] = initializer".
1764 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001765 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001766 {
1767 if(initNode)
1768 {
1769 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1770 }
1771 else
1772 {
1773 return aggregateDeclaration;
1774 }
1775 }
1776 else
1777 {
1778 recover();
1779 return nullptr;
1780 }
1781}
1782
Alexis Hetua35d8232015-06-11 17:11:06 -04001783void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1784{
Alexis Hetu0a655842015-06-22 16:52:11 -04001785 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001786 {
1787 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1788 recover();
1789 return;
1790 }
1791
1792 if(typeQualifier.qualifier != EvqUniform)
1793 {
1794 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1795 recover();
1796 return;
1797 }
1798
1799 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1800 ASSERT(!layoutQualifier.isEmpty());
1801
1802 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1803 {
1804 recover();
1805 return;
1806 }
1807
1808 if(layoutQualifier.matrixPacking != EmpUnspecified)
1809 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001810 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001811 }
1812
1813 if(layoutQualifier.blockStorage != EbsUnspecified)
1814 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001815 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001816 }
1817}
1818
Alexis Hetu407813b2016-02-24 16:46:13 -05001819TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1820{
1821 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1822 // Either way the instance in the symbol table is used to track whether the function is declared
1823 // multiple times.
1824 TFunction *symbolTableFunction =
1825 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1826 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1827 {
1828 // ESSL 1.00.17 section 4.2.7.
1829 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1830 error(location, "duplicate function prototype declarations are not allowed", "function");
1831 recover();
1832 }
1833 symbolTableFunction->setHasPrototypeDeclaration();
1834
1835 TIntermAggregate *prototype = new TIntermAggregate;
1836 prototype->setType(function.getReturnType());
1837 prototype->setName(function.getMangledName());
1838
1839 for(size_t i = 0; i < function.getParamCount(); i++)
1840 {
1841 const TParameter &param = function.getParam(i);
1842 if(param.name != 0)
1843 {
1844 TVariable variable(param.name, *param.type);
1845
1846 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1847 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1848 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1849 }
1850 else
1851 {
1852 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1853 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1854 }
1855 }
1856
1857 prototype->setOp(EOpPrototype);
1858
1859 symbolTable.pop();
1860
1861 if(!symbolTable.atGlobalLevel())
1862 {
1863 // ESSL 3.00.4 section 4.2.4.
1864 error(location, "local function prototype declarations are not allowed", "function");
1865 recover();
1866 }
1867
1868 return prototype;
1869}
1870
1871TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1872{
1873 //?? Check that all paths return a value if return type != void ?
1874 // May be best done as post process phase on intermediate code
1875 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1876 {
1877 error(location, "function does not return a value:", "", function.getName().c_str());
1878 recover();
1879 }
1880
1881 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1882 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1883 aggregate->setName(function.getMangledName().c_str());
1884 aggregate->setType(function.getReturnType());
1885
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001886 // store the pragma information for debug and optimize and other vendor specific
1887 // information. This information can be queried from the parse tree
1888 aggregate->setOptimize(pragma().optimize);
Alexis Hetu407813b2016-02-24 16:46:13 -05001889 aggregate->setDebug(pragma().debug);
1890
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001891 if(functionBody && functionBody->getAsAggregate())
Alexis Hetu407813b2016-02-24 16:46:13 -05001892 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1893
1894 symbolTable.pop();
1895 return aggregate;
1896}
1897
1898void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1899{
1900 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1901
1902 if(builtIn)
1903 {
1904 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1905 recover();
1906 }
1907
1908 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1909 //
1910 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1911 // as it would have just been put in the symbol table. Otherwise, we're looking up
1912 // an earlier occurance.
1913 //
1914 if(prevDec->isDefined())
1915 {
1916 // Then this function already has a body.
1917 error(location, "function already has a body", function->getName().c_str());
1918 recover();
1919 }
1920 prevDec->setDefined();
1921 //
1922 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1923 // Eventually we will probably want to have only a single definition and just swap the
1924 // arguments to be the definition's arguments.
1925 //
1926 function->setUniqueId(prevDec->getUniqueId());
1927
1928 // Raise error message if main function takes any parameters or return anything other than void
1929 if(function->getName() == "main")
1930 {
1931 if(function->getParamCount() > 0)
1932 {
1933 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1934 recover();
1935 }
1936 if(function->getReturnType().getBasicType() != EbtVoid)
1937 {
1938 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1939 recover();
1940 }
1941 }
1942
1943 //
1944 // Remember the return type for later checking for RETURN statements.
1945 //
1946 mCurrentFunctionType = &(prevDec->getReturnType());
1947 mFunctionReturnsValue = false;
1948
1949 //
1950 // Insert parameters into the symbol table.
1951 // If the parameter has no name, it's not an error, just don't insert it
1952 // (could be used for unused args).
1953 //
1954 // Also, accumulate the list of parameters into the HIL, so lower level code
1955 // knows where to find parameters.
1956 //
1957 TIntermAggregate *paramNodes = new TIntermAggregate;
1958 for(size_t i = 0; i < function->getParamCount(); i++)
1959 {
1960 const TParameter &param = function->getParam(i);
1961 if(param.name != 0)
1962 {
1963 TVariable *variable = new TVariable(param.name, *param.type);
1964 //
1965 // Insert the parameters with name in the symbol table.
1966 //
Nicolas Capens0b7471a2018-01-09 21:39:21 -05001967 if(!symbolTable.declare(variable))
Alexis Hetu407813b2016-02-24 16:46:13 -05001968 {
1969 error(location, "redefinition", variable->getName().c_str());
1970 recover();
1971 paramNodes = intermediate.growAggregate(
1972 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1973 continue;
1974 }
1975
1976 //
1977 // Add the parameter to the HIL
1978 //
1979 TIntermSymbol *symbol = intermediate.addSymbol(
1980 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1981
1982 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1983 }
1984 else
1985 {
1986 paramNodes = intermediate.growAggregate(
1987 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1988 }
1989 }
1990 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1991 *aggregateOut = paramNodes;
1992 setLoopNestingLevel(0);
1993}
1994
1995TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1996{
1997 //
1998 // We don't know at this point whether this is a function definition or a prototype.
1999 // The definition production code will check for redefinitions.
2000 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
2001 //
2002 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2003 // here.
2004 //
2005 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002006 if(getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2007 {
2008 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2009 // Therefore overloading or redefining builtin functions is an error.
2010 error(location, "Name of a built-in function cannot be redeclared as function", function->getName().c_str());
2011 }
2012 else if(prevDec)
Alexis Hetu407813b2016-02-24 16:46:13 -05002013 {
2014 if(prevDec->getReturnType() != function->getReturnType())
2015 {
2016 error(location, "overloaded functions must have the same return type",
2017 function->getReturnType().getBasicString());
2018 recover();
2019 }
2020 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
2021 {
2022 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
2023 {
2024 error(location, "overloaded functions must have the same parameter qualifiers",
2025 function->getParam(i).type->getQualifierString());
2026 recover();
2027 }
2028 }
2029 }
2030
2031 //
2032 // Check for previously declared variables using the same name.
2033 //
2034 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
2035 if(prevSym)
2036 {
2037 if(!prevSym->isFunction())
2038 {
2039 error(location, "redefinition", function->getName().c_str(), "function");
2040 recover();
2041 }
2042 }
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002043 else
2044 {
2045 // Insert the unmangled name to detect potential future redefinition as a variable.
2046 TFunction *unmangledFunction = new TFunction(NewPoolTString(function->getName().c_str()), function->getReturnType());
2047 symbolTable.getOuterLevel()->insertUnmangled(unmangledFunction);
2048 }
Alexis Hetu407813b2016-02-24 16:46:13 -05002049
2050 // We're at the inner scope level of the function's arguments and body statement.
2051 // Add the function prototype to the surrounding scope instead.
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002052 symbolTable.getOuterLevel()->insert(function);
Alexis Hetu407813b2016-02-24 16:46:13 -05002053
2054 //
2055 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2056 // variable names from this one, and not the one that's
2057 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2058 //
2059 return function;
2060}
2061
Alexis Hetue5246692015-06-18 12:34:52 -04002062TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
2063{
2064 TPublicType publicType = publicTypeIn;
2065 TOperator op = EOpNull;
2066 if(publicType.userDef)
2067 {
2068 op = EOpConstructStruct;
2069 }
2070 else
2071 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002072 op = TypeToConstructorOperator(TType(publicType));
Alexis Hetue5246692015-06-18 12:34:52 -04002073 if(op == EOpNull)
2074 {
2075 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2076 recover();
2077 publicType.type = EbtFloat;
2078 op = EOpConstructFloat;
2079 }
2080 }
2081
2082 TString tempString;
2083 TType type(publicType);
2084 return new TFunction(&tempString, type, op);
2085}
2086
John Bauman66b8ab22014-05-06 15:57:45 -04002087// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002088// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002089//
2090// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2091//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002092TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002093{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002094 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002095
Nicolas Capens0bac2852016-05-07 06:09:58 -04002096 if(!aggregateArguments)
2097 {
2098 aggregateArguments = new TIntermAggregate;
2099 aggregateArguments->getSequence().push_back(arguments);
2100 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002101
Alexis Hetu2a198552016-09-27 20:50:45 -04002102 if(type->isArray())
2103 {
2104 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2105 // the array.
2106 for(TIntermNode *&argNode : aggregateArguments->getSequence())
2107 {
2108 const TType &argType = argNode->getAsTyped()->getType();
2109 // It has already been checked that the argument is not an array.
2110 ASSERT(!argType.isArray());
2111 if(!argType.sameElementType(*type))
2112 {
2113 error(line, "Array constructor argument has an incorrect type", "Error");
Alexis Hetuf0005a12016-09-28 15:52:21 -04002114 return nullptr;
Alexis Hetu2a198552016-09-27 20:50:45 -04002115 }
2116 }
2117 }
2118 else if(op == EOpConstructStruct)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002119 {
2120 const TFieldList &fields = type->getStruct()->fields();
2121 TIntermSequence &args = aggregateArguments->getSequence();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002122
Nicolas Capens0bac2852016-05-07 06:09:58 -04002123 for(size_t i = 0; i < fields.size(); i++)
2124 {
2125 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
2126 {
2127 error(line, "Structure constructor arguments do not match structure fields", "Error");
2128 recover();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002129
Alexis Hetuf0005a12016-09-28 15:52:21 -04002130 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002131 }
2132 }
2133 }
John Bauman66b8ab22014-05-06 15:57:45 -04002134
Nicolas Capens0bac2852016-05-07 06:09:58 -04002135 // Turn the argument list itself into a constructor
2136 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2137 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
2138 if(constConstructor)
2139 {
2140 return constConstructor;
2141 }
John Bauman66b8ab22014-05-06 15:57:45 -04002142
Nicolas Capens0bac2852016-05-07 06:09:58 -04002143 return constructor;
John Bauman66b8ab22014-05-06 15:57:45 -04002144}
2145
2146TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2147{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002148 aggrNode->setType(type);
2149 if (aggrNode->isConstantFoldable()) {
2150 bool returnVal = false;
2151 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2152 if (aggrNode->getSequence().size() == 1) {
2153 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
2154 }
2155 else {
2156 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
2157 }
2158 if (returnVal)
Alexis Hetuf0005a12016-09-28 15:52:21 -04002159 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002160
Nicolas Capens0bac2852016-05-07 06:09:58 -04002161 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2162 }
John Bauman66b8ab22014-05-06 15:57:45 -04002163
Alexis Hetuf0005a12016-09-28 15:52:21 -04002164 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002165}
2166
John Bauman66b8ab22014-05-06 15:57:45 -04002167//
2168// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2169// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2170// 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 -04002171// 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 -04002172// a constant matrix.
2173//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002174TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002175{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002176 TIntermTyped* typedNode;
2177 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002178
Nicolas Capens0bac2852016-05-07 06:09:58 -04002179 ConstantUnion *unionArray;
2180 if (tempConstantNode) {
2181 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002182
Nicolas Capens0bac2852016-05-07 06:09:58 -04002183 if (!unionArray) {
2184 return node;
2185 }
2186 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2187 error(line, "Cannot offset into the vector", "Error");
2188 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002189
Alexis Hetuf0005a12016-09-28 15:52:21 -04002190 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002191 }
John Bauman66b8ab22014-05-06 15:57:45 -04002192
Nicolas Capens0bac2852016-05-07 06:09:58 -04002193 ConstantUnion* constArray = new ConstantUnion[fields.num];
John Bauman66b8ab22014-05-06 15:57:45 -04002194
Alexis Hetub34591a2016-06-28 15:48:35 -04002195 int objSize = static_cast<int>(node->getType().getObjectSize());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002196 for (int i = 0; i < fields.num; i++) {
Alexis Hetub34591a2016-06-28 15:48:35 -04002197 if (fields.offsets[i] >= objSize) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002198 std::stringstream extraInfoStream;
2199 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2200 std::string extraInfo = extraInfoStream.str();
2201 error(line, "", "[", extraInfo.c_str());
2202 recover();
2203 fields.offsets[i] = 0;
2204 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002205
Nicolas Capens0bac2852016-05-07 06:09:58 -04002206 constArray[i] = unionArray[fields.offsets[i]];
John Bauman66b8ab22014-05-06 15:57:45 -04002207
Nicolas Capens0bac2852016-05-07 06:09:58 -04002208 }
Alexis Hetu2cf121d2018-02-07 14:17:15 -05002209
2210 TType type(node->getType().getBasicType(), node->getType().getPrecision(), EvqConstExpr, fields.num);
2211 typedNode = intermediate.addConstantUnion(constArray, type, line);
Nicolas Capens0bac2852016-05-07 06:09:58 -04002212 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002213}
2214
2215//
2216// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002217// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2218// 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 -04002219// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2220//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002221TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002222{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002223 TIntermTyped* typedNode;
2224 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002225
Nicolas Capens0bac2852016-05-07 06:09:58 -04002226 if (index >= node->getType().getNominalSize()) {
2227 std::stringstream extraInfoStream;
2228 extraInfoStream << "matrix field selection out of range '" << index << "'";
2229 std::string extraInfo = extraInfoStream.str();
2230 error(line, "", "[", extraInfo.c_str());
2231 recover();
2232 index = 0;
2233 }
John Bauman66b8ab22014-05-06 15:57:45 -04002234
Nicolas Capens0bac2852016-05-07 06:09:58 -04002235 if (tempConstantNode) {
2236 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2237 int size = tempConstantNode->getType().getNominalSize();
2238 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2239 } else {
2240 error(line, "Cannot offset into the matrix", "Error");
2241 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002242
Alexis Hetuf0005a12016-09-28 15:52:21 -04002243 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002244 }
John Bauman66b8ab22014-05-06 15:57:45 -04002245
Nicolas Capens0bac2852016-05-07 06:09:58 -04002246 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002247}
2248
2249
2250//
2251// 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 -04002252// the symbol table and parse-tree is built for the type of the element. The input
2253// 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 -04002254// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2255//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002256TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002257{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002258 TIntermTyped* typedNode;
2259 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2260 TType arrayElementType = node->getType();
2261 arrayElementType.clearArrayness();
John Bauman66b8ab22014-05-06 15:57:45 -04002262
Nicolas Capens0bac2852016-05-07 06:09:58 -04002263 if (index >= node->getType().getArraySize()) {
2264 std::stringstream extraInfoStream;
2265 extraInfoStream << "array field selection out of range '" << index << "'";
2266 std::string extraInfo = extraInfoStream.str();
2267 error(line, "", "[", extraInfo.c_str());
2268 recover();
2269 index = 0;
2270 }
John Bauman66b8ab22014-05-06 15:57:45 -04002271
Nicolas Capens0bac2852016-05-07 06:09:58 -04002272 size_t arrayElementSize = arrayElementType.getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002273
Nicolas Capens0bac2852016-05-07 06:09:58 -04002274 if (tempConstantNode) {
2275 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2276 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2277 } else {
2278 error(line, "Cannot offset into the array", "Error");
2279 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002280
Alexis Hetuf0005a12016-09-28 15:52:21 -04002281 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002282 }
John Bauman66b8ab22014-05-06 15:57:45 -04002283
Nicolas Capens0bac2852016-05-07 06:09:58 -04002284 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002285}
2286
2287
2288//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002289// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002290// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2291// function and returns the parse-tree with the values of the embedded/nested struct.
2292//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002293TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002294{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002295 const TFieldList &fields = node->getType().getStruct()->fields();
2296 TIntermTyped *typedNode;
2297 size_t instanceSize = 0;
2298 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002299
Alexis Hetud2742532018-01-23 16:53:41 -05002300 for(const auto &field : fields) {
2301 if (field->name() == identifier) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002302 break;
2303 } else {
Alexis Hetud2742532018-01-23 16:53:41 -05002304 instanceSize += field->type()->getObjectSize();
Nicolas Capens0bac2852016-05-07 06:09:58 -04002305 }
2306 }
John Bauman66b8ab22014-05-06 15:57:45 -04002307
Nicolas Capens0bac2852016-05-07 06:09:58 -04002308 if (tempConstantNode) {
2309 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002310
Nicolas Capens0bac2852016-05-07 06:09:58 -04002311 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2312 } else {
2313 error(line, "Cannot offset into the structure", "Error");
2314 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002315
Alexis Hetuf0005a12016-09-28 15:52:21 -04002316 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002317 }
John Bauman66b8ab22014-05-06 15:57:45 -04002318
Nicolas Capens0bac2852016-05-07 06:09:58 -04002319 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002320}
2321
Alexis Hetuad6b8752015-06-09 16:15:30 -04002322//
Alexis Hetua35d8232015-06-11 17:11:06 -04002323// Interface/uniform blocks
2324//
2325TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
Nicolas Capens0bac2852016-05-07 06:09:58 -04002326 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
Alexis Hetua35d8232015-06-11 17:11:06 -04002327{
2328 if(reservedErrorCheck(nameLine, blockName))
2329 recover();
2330
2331 if(typeQualifier.qualifier != EvqUniform)
2332 {
2333 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2334 recover();
2335 }
2336
2337 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2338 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2339 {
2340 recover();
2341 }
2342
2343 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2344 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002345 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002346 }
2347
2348 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2349 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002350 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002351 }
2352
2353 TSymbol* blockNameSymbol = new TSymbol(&blockName);
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002354 if(!symbolTable.declare(blockNameSymbol)) {
Alexis Hetua35d8232015-06-11 17:11:06 -04002355 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2356 recover();
2357 }
2358
2359 // check for sampler types and apply layout qualifiers
Alexis Hetud2742532018-01-23 16:53:41 -05002360 for(const auto &field : *fieldList) {
Alexis Hetua35d8232015-06-11 17:11:06 -04002361 TType* fieldType = field->type();
2362 if(IsSampler(fieldType->getBasicType())) {
2363 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2364 recover();
2365 }
2366
2367 const TQualifier qualifier = fieldType->getQualifier();
2368 switch(qualifier)
2369 {
2370 case EvqGlobal:
2371 case EvqUniform:
2372 break;
2373 default:
2374 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2375 recover();
2376 break;
2377 }
2378
2379 // check layout qualifiers
2380 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2381 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2382 {
2383 recover();
2384 }
2385
2386 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2387 {
2388 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2389 recover();
2390 }
2391
2392 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2393 {
2394 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2395 }
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002396 else if(!fieldType->isMatrix() && (fieldType->getBasicType() != EbtStruct))
Alexis Hetua35d8232015-06-11 17:11:06 -04002397 {
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002398 warning(field->line(), "extraneous layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Alexis Hetua35d8232015-06-11 17:11:06 -04002399 }
2400
2401 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Alexis Hetud2742532018-01-23 16:53:41 -05002402
2403 // Recursively propagate the matrix packing setting down to all block/structure members
2404 fieldType->setMatrixPackingIfUnspecified(fieldLayoutQualifier.matrixPacking);
Alexis Hetua35d8232015-06-11 17:11:06 -04002405 }
2406
2407 // add array index
2408 int arraySize = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002409 if(arrayIndex)
Alexis Hetua35d8232015-06-11 17:11:06 -04002410 {
2411 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2412 recover();
2413 }
2414
2415 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2416 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2417
2418 TString symbolName = "";
2419 int symbolId = 0;
2420
2421 if(!instanceName)
2422 {
2423 // define symbols for the members of the interface block
Alexis Hetud2742532018-01-23 16:53:41 -05002424 for(const auto &field : *fieldList)
Alexis Hetua35d8232015-06-11 17:11:06 -04002425 {
Alexis Hetua35d8232015-06-11 17:11:06 -04002426 TType* fieldType = field->type();
2427
2428 // set parent pointer of the field variable
2429 fieldType->setInterfaceBlock(interfaceBlock);
2430
2431 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2432 fieldVariable->setQualifier(typeQualifier.qualifier);
2433
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002434 if(!symbolTable.declare(fieldVariable)) {
Alexis Hetua35d8232015-06-11 17:11:06 -04002435 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2436 recover();
2437 }
2438 }
2439 }
2440 else
2441 {
Alexis Hetu152997c2018-01-05 16:17:56 -05002442 if(reservedErrorCheck(nameLine, *instanceName))
2443 recover();
2444
Alexis Hetua35d8232015-06-11 17:11:06 -04002445 // add a symbol for this interface block
2446 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2447 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2448
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002449 if(!symbolTable.declare(instanceTypeDef)) {
Alexis Hetua35d8232015-06-11 17:11:06 -04002450 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2451 recover();
2452 }
2453
2454 symbolId = instanceTypeDef->getUniqueId();
2455 symbolName = instanceTypeDef->getName();
2456 }
2457
2458 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2459 aggregate->setOp(EOpDeclaration);
2460
2461 exitStructDeclaration();
2462 return aggregate;
2463}
2464
2465//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002466// Parse an array index expression
2467//
2468TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2469{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002470 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002471
2472 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2473 {
2474 if(baseExpression->getAsSymbolNode())
2475 {
2476 error(location, " left of '[' is not of type array, matrix, or vector ",
2477 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2478 }
2479 else
2480 {
2481 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2482 }
2483 recover();
2484 }
2485
2486 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2487
Nicolas Capens5d507bc2017-12-22 17:28:33 -05002488 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion) // TODO: Qualifier check redundant?
Alexis Hetuad6b8752015-06-09 16:15:30 -04002489 {
2490 int index = indexConstantUnion->getIConst(0);
2491 if(index < 0)
2492 {
2493 std::stringstream infoStream;
2494 infoStream << index;
2495 std::string info = infoStream.str();
2496 error(location, "negative index", info.c_str());
2497 recover();
2498 index = 0;
2499 }
Nicolas Capens5d507bc2017-12-22 17:28:33 -05002500 if(baseExpression->getType().getQualifier() == EvqConstExpr && baseExpression->getAsConstantUnion()) // TODO: Qualifier check redundant?
Alexis Hetuad6b8752015-06-09 16:15:30 -04002501 {
2502 if(baseExpression->isArray())
2503 {
2504 // constant folding for arrays
2505 indexedExpression = addConstArrayNode(index, baseExpression, location);
2506 }
2507 else if(baseExpression->isVector())
2508 {
2509 // constant folding for vectors
2510 TVectorFields fields;
2511 fields.num = 1;
2512 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2513 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2514 }
2515 else if(baseExpression->isMatrix())
2516 {
2517 // constant folding for matrices
2518 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2519 }
2520 }
2521 else
2522 {
2523 int safeIndex = -1;
2524
2525 if(baseExpression->isArray())
2526 {
2527 if(index >= baseExpression->getType().getArraySize())
2528 {
2529 std::stringstream extraInfoStream;
2530 extraInfoStream << "array index out of range '" << index << "'";
2531 std::string extraInfo = extraInfoStream.str();
2532 error(location, "", "[", extraInfo.c_str());
2533 recover();
2534 safeIndex = baseExpression->getType().getArraySize() - 1;
2535 }
2536 }
2537 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2538 baseExpression->getType().getNominalSize() <= index)
2539 {
2540 std::stringstream extraInfoStream;
2541 extraInfoStream << "field selection out of range '" << index << "'";
2542 std::string extraInfo = extraInfoStream.str();
2543 error(location, "", "[", extraInfo.c_str());
2544 recover();
2545 safeIndex = baseExpression->getType().getNominalSize() - 1;
2546 }
2547
2548 // Don't modify the data of the previous constant union, because it can point
2549 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2550 if(safeIndex != -1)
2551 {
2552 ConstantUnion *safeConstantUnion = new ConstantUnion();
2553 safeConstantUnion->setIConst(safeIndex);
2554 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2555 }
2556
2557 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2558 }
2559 }
2560 else
2561 {
2562 if(baseExpression->isInterfaceBlock())
2563 {
2564 error(location, "",
2565 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2566 recover();
2567 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002568 else if(baseExpression->getQualifier() == EvqFragmentOut)
2569 {
2570 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2571 recover();
2572 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002573
2574 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2575 }
2576
2577 if(indexedExpression == 0)
2578 {
2579 ConstantUnion *unionArray = new ConstantUnion[1];
2580 unionArray->setFConst(0.0f);
2581 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2582 }
2583 else if(baseExpression->isArray())
2584 {
2585 const TType &baseType = baseExpression->getType();
2586 if(baseType.getStruct())
2587 {
2588 TType copyOfType(baseType.getStruct());
2589 indexedExpression->setType(copyOfType);
2590 }
2591 else if(baseType.isInterfaceBlock())
2592 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002593 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002594 indexedExpression->setType(copyOfType);
2595 }
2596 else
2597 {
2598 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2599 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2600 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2601 }
2602
2603 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2604 {
2605 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2606 }
2607 }
2608 else if(baseExpression->isMatrix())
2609 {
2610 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2611 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2612 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2613 }
2614 else if(baseExpression->isVector())
2615 {
2616 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2617 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2618 }
2619 else
2620 {
2621 indexedExpression->setType(baseExpression->getType());
2622 }
2623
2624 return indexedExpression;
2625}
2626
2627TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2628 const TString &fieldString, const TSourceLoc &fieldLocation)
2629{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002630 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002631
2632 if(baseExpression->isArray())
2633 {
2634 error(fieldLocation, "cannot apply dot operator to an array", ".");
2635 recover();
2636 }
2637
2638 if(baseExpression->isVector())
2639 {
2640 TVectorFields fields;
2641 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2642 {
2643 fields.num = 1;
2644 fields.offsets[0] = 0;
2645 recover();
2646 }
2647
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002648 if(baseExpression->getAsConstantUnion())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002649 {
2650 // constant folding for vector fields
2651 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2652 if(indexedExpression == 0)
2653 {
2654 recover();
2655 indexedExpression = baseExpression;
2656 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002657 }
2658 else
2659 {
2660 TString vectorString = fieldString;
2661 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2662 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2663 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
Nicolas Capens341afbb2016-04-10 01:54:50 -04002664 baseExpression->getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary, (unsigned char)vectorString.size()));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002665 }
2666 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002667 else if(baseExpression->getBasicType() == EbtStruct)
2668 {
2669 bool fieldFound = false;
2670 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2671 if(fields.empty())
2672 {
2673 error(dotLocation, "structure has no fields", "Internal Error");
2674 recover();
2675 indexedExpression = baseExpression;
2676 }
2677 else
2678 {
2679 unsigned int i;
2680 for(i = 0; i < fields.size(); ++i)
2681 {
2682 if(fields[i]->name() == fieldString)
2683 {
2684 fieldFound = true;
2685 break;
2686 }
2687 }
2688 if(fieldFound)
2689 {
2690 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2691 {
2692 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2693 if(indexedExpression == 0)
2694 {
2695 recover();
2696 indexedExpression = baseExpression;
2697 }
2698 else
2699 {
2700 indexedExpression->setType(*fields[i]->type());
2701 // change the qualifier of the return type, not of the structure field
2702 // as the structure definition is shared between various structures.
2703 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2704 }
2705 }
2706 else
2707 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002708 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2709 index->setLine(fieldLocation);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002710 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2711 indexedExpression->setType(*fields[i]->type());
2712 }
2713 }
2714 else
2715 {
2716 error(dotLocation, " no such field in structure", fieldString.c_str());
2717 recover();
2718 indexedExpression = baseExpression;
2719 }
2720 }
2721 }
2722 else if(baseExpression->isInterfaceBlock())
2723 {
2724 bool fieldFound = false;
2725 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2726 if(fields.empty())
2727 {
2728 error(dotLocation, "interface block has no fields", "Internal Error");
2729 recover();
2730 indexedExpression = baseExpression;
2731 }
2732 else
2733 {
2734 unsigned int i;
2735 for(i = 0; i < fields.size(); ++i)
2736 {
2737 if(fields[i]->name() == fieldString)
2738 {
2739 fieldFound = true;
2740 break;
2741 }
2742 }
2743 if(fieldFound)
2744 {
2745 ConstantUnion *unionArray = new ConstantUnion[1];
2746 unionArray->setIConst(i);
2747 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2748 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2749 dotLocation);
2750 indexedExpression->setType(*fields[i]->type());
2751 }
2752 else
2753 {
2754 error(dotLocation, " no such field in interface block", fieldString.c_str());
2755 recover();
2756 indexedExpression = baseExpression;
2757 }
2758 }
2759 }
2760 else
2761 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002762 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002763 {
Nicolas Capensc09073f2017-11-09 09:49:03 -05002764 error(dotLocation, " field selection requires structure or vector on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002765 fieldString.c_str());
2766 }
2767 else
2768 {
2769 error(dotLocation,
Nicolas Capensc09073f2017-11-09 09:49:03 -05002770 " field selection requires structure, vector, or interface block on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002771 fieldString.c_str());
2772 }
2773 recover();
2774 indexedExpression = baseExpression;
2775 }
2776
2777 return indexedExpression;
2778}
2779
Nicolas Capens7d626792015-02-17 17:58:31 -05002780TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2781{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002782 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002783
Nicolas Capens0bac2852016-05-07 06:09:58 -04002784 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002785 qualifier.matrixPacking = EmpUnspecified;
2786 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002787
Alexis Hetuad6b8752015-06-09 16:15:30 -04002788 if(qualifierType == "shared")
2789 {
2790 qualifier.blockStorage = EbsShared;
2791 }
2792 else if(qualifierType == "packed")
2793 {
2794 qualifier.blockStorage = EbsPacked;
2795 }
2796 else if(qualifierType == "std140")
2797 {
2798 qualifier.blockStorage = EbsStd140;
2799 }
2800 else if(qualifierType == "row_major")
2801 {
2802 qualifier.matrixPacking = EmpRowMajor;
2803 }
2804 else if(qualifierType == "column_major")
2805 {
2806 qualifier.matrixPacking = EmpColumnMajor;
2807 }
2808 else if(qualifierType == "location")
Nicolas Capens0bac2852016-05-07 06:09:58 -04002809 {
2810 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2811 recover();
2812 }
2813 else
2814 {
2815 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2816 recover();
2817 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002818
Nicolas Capens0bac2852016-05-07 06:09:58 -04002819 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002820}
2821
2822TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2823{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002824 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002825
Alexis Hetu3c1d6cf2018-02-06 10:54:49 -05002826 qualifier.location = -1; // -1 isn't a valid location, it means the value isn't set. Negative values are checked lower in this function.
Nicolas Capens0bac2852016-05-07 06:09:58 -04002827 qualifier.matrixPacking = EmpUnspecified;
2828 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002829
Nicolas Capens0bac2852016-05-07 06:09:58 -04002830 if (qualifierType != "location")
2831 {
2832 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2833 recover();
2834 }
2835 else
2836 {
2837 // must check that location is non-negative
2838 if (intValue < 0)
2839 {
2840 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2841 recover();
2842 }
2843 else
2844 {
2845 qualifier.location = intValue;
2846 }
2847 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002848
Nicolas Capens0bac2852016-05-07 06:09:58 -04002849 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002850}
2851
2852TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2853{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002854 TLayoutQualifier joinedQualifier = leftQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002855
Nicolas Capens0bac2852016-05-07 06:09:58 -04002856 if (rightQualifier.location != -1)
2857 {
2858 joinedQualifier.location = rightQualifier.location;
2859 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002860 if(rightQualifier.matrixPacking != EmpUnspecified)
2861 {
2862 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2863 }
2864 if(rightQualifier.blockStorage != EbsUnspecified)
2865 {
2866 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2867 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002868
Nicolas Capens0bac2852016-05-07 06:09:58 -04002869 return joinedQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002870}
2871
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002872
2873TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2874 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2875{
2876 TQualifier mergedQualifier = EvqSmoothIn;
2877
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002878 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002879 if(interpolationQualifier == EvqSmooth)
2880 mergedQualifier = EvqSmoothIn;
2881 else if(interpolationQualifier == EvqFlat)
2882 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002883 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002884 }
2885 else if(storageQualifier == EvqCentroidIn) {
2886 if(interpolationQualifier == EvqSmooth)
2887 mergedQualifier = EvqCentroidIn;
2888 else if(interpolationQualifier == EvqFlat)
2889 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002890 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002891 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002892 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002893 if(interpolationQualifier == EvqSmooth)
2894 mergedQualifier = EvqSmoothOut;
2895 else if(interpolationQualifier == EvqFlat)
2896 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002897 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002898 }
2899 else if(storageQualifier == EvqCentroidOut) {
2900 if(interpolationQualifier == EvqSmooth)
2901 mergedQualifier = EvqCentroidOut;
2902 else if(interpolationQualifier == EvqFlat)
2903 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002904 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002905 }
2906 else {
2907 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2908 recover();
2909
2910 mergedQualifier = storageQualifier;
2911 }
2912
2913 TPublicType type;
2914 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2915 return type;
2916}
2917
Alexis Hetuad6b8752015-06-09 16:15:30 -04002918TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2919{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002920 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002921 {
2922 recover();
2923 }
2924
Alexis Hetud2742532018-01-23 16:53:41 -05002925 for(const auto &field : *fieldList)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002926 {
2927 //
2928 // Careful not to replace already known aspects of type, like array-ness
2929 //
Alexis Hetud2742532018-01-23 16:53:41 -05002930 TType *type = field->type();
Alexis Hetuad6b8752015-06-09 16:15:30 -04002931 type->setBasicType(typeSpecifier.type);
2932 type->setNominalSize(typeSpecifier.primarySize);
2933 type->setSecondarySize(typeSpecifier.secondarySize);
2934 type->setPrecision(typeSpecifier.precision);
2935 type->setQualifier(typeSpecifier.qualifier);
2936 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2937
2938 // don't allow arrays of arrays
2939 if(type->isArray())
2940 {
2941 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2942 recover();
2943 }
2944 if(typeSpecifier.array)
2945 type->setArraySize(typeSpecifier.arraySize);
2946 if(typeSpecifier.userDef)
2947 {
2948 type->setStruct(typeSpecifier.userDef->getStruct());
2949 }
2950
Alexis Hetud2742532018-01-23 16:53:41 -05002951 if(structNestingErrorCheck(typeSpecifier.line, *field))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002952 {
2953 recover();
2954 }
2955 }
2956
2957 return fieldList;
2958}
2959
2960TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2961 const TString *structName, TFieldList *fieldList)
2962{
2963 TStructure *structure = new TStructure(structName, fieldList);
2964 TType *structureType = new TType(structure);
2965
2966 // Store a bool in the struct if we're at global scope, to allow us to
2967 // skip the local struct scoping workaround in HLSL.
2968 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2969 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2970
2971 if(!structName->empty())
2972 {
2973 if(reservedErrorCheck(nameLine, *structName))
2974 {
2975 recover();
2976 }
2977 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capens0b7471a2018-01-09 21:39:21 -05002978 if(!symbolTable.declare(userTypeDef))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002979 {
2980 error(nameLine, "redefinition", structName->c_str(), "struct");
2981 recover();
2982 }
2983 }
2984
2985 // ensure we do not specify any storage qualifiers on the struct members
Alexis Hetud2742532018-01-23 16:53:41 -05002986 for(const auto &field : *fieldList)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002987 {
Alexis Hetud2742532018-01-23 16:53:41 -05002988 const TQualifier qualifier = field->type()->getQualifier();
Alexis Hetuad6b8752015-06-09 16:15:30 -04002989 switch(qualifier)
2990 {
2991 case EvqGlobal:
2992 case EvqTemporary:
2993 break;
2994 default:
Alexis Hetud2742532018-01-23 16:53:41 -05002995 error(field->line(), "invalid qualifier on struct member", getQualifierString(qualifier));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002996 recover();
2997 break;
2998 }
2999 }
3000
3001 TPublicType publicType;
3002 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
3003 publicType.userDef = structureType;
3004 exitStructDeclaration();
3005
3006 return publicType;
3007}
3008
Alexis Hetufe1269e2015-06-16 12:43:32 -04003009bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04003010{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003011 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003012
Nicolas Capens0bac2852016-05-07 06:09:58 -04003013 // Embedded structure definitions are not supported per GLSL ES spec.
3014 // They aren't allowed in GLSL either, but we need to detect this here
3015 // so we don't rely on the GLSL compiler to catch it.
3016 if (mStructNestingLevel > 1) {
3017 error(line, "", "Embedded struct definitions are not allowed");
3018 return true;
3019 }
John Bauman66b8ab22014-05-06 15:57:45 -04003020
Nicolas Capens0bac2852016-05-07 06:09:58 -04003021 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04003022}
3023
3024void TParseContext::exitStructDeclaration()
3025{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003026 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003027}
3028
Alexis Hetuad6b8752015-06-09 16:15:30 -04003029bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3030{
3031 static const int kWebGLMaxStructNesting = 4;
3032
3033 if(field.type()->getBasicType() != EbtStruct)
3034 {
3035 return false;
3036 }
3037
3038 // We're already inside a structure definition at this point, so add
3039 // one to the field's struct nesting.
3040 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3041 {
3042 std::stringstream reasonStream;
3043 reasonStream << "Reference of struct type "
3044 << field.type()->getStruct()->name().c_str()
3045 << " exceeds maximum allowed nesting level of "
3046 << kWebGLMaxStructNesting;
3047 std::string reason = reasonStream.str();
3048 error(line, reason.c_str(), field.name().c_str(), "");
3049 return true;
3050 }
3051
3052 return false;
3053}
3054
3055TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3056{
3057 if(child == nullptr)
3058 {
3059 return nullptr;
3060 }
3061
3062 switch(op)
3063 {
3064 case EOpLogicalNot:
3065 if(child->getBasicType() != EbtBool ||
3066 child->isMatrix() ||
3067 child->isArray() ||
3068 child->isVector())
3069 {
3070 return nullptr;
3071 }
3072 break;
3073 case EOpBitwiseNot:
3074 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3075 child->isMatrix() ||
3076 child->isArray())
3077 {
3078 return nullptr;
3079 }
3080 break;
3081 case EOpPostIncrement:
3082 case EOpPreIncrement:
3083 case EOpPostDecrement:
3084 case EOpPreDecrement:
3085 case EOpNegative:
3086 if(child->getBasicType() == EbtStruct ||
3087 child->getBasicType() == EbtBool ||
3088 child->isArray())
3089 {
3090 return nullptr;
3091 }
3092 // Operators for built-ins are already type checked against their prototype.
3093 default:
3094 break;
3095 }
3096
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -04003097 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Alexis Hetuad6b8752015-06-09 16:15:30 -04003098}
3099
3100TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3101{
3102 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3103 if(node == nullptr)
3104 {
3105 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3106 recover();
3107 return child;
3108 }
3109 return node;
3110}
3111
3112TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3113{
3114 if(lValueErrorCheck(loc, getOperatorString(op), child))
3115 recover();
3116 return addUnaryMath(op, child, loc);
3117}
3118
3119bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3120{
3121 if(left->isArray() || right->isArray())
3122 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003123 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003124 {
3125 error(loc, "Invalid operation for arrays", getOperatorString(op));
3126 return false;
3127 }
3128
3129 if(left->isArray() != right->isArray())
3130 {
3131 error(loc, "array / non-array mismatch", getOperatorString(op));
3132 return false;
3133 }
3134
3135 switch(op)
3136 {
3137 case EOpEqual:
3138 case EOpNotEqual:
3139 case EOpAssign:
3140 case EOpInitialize:
3141 break;
3142 default:
3143 error(loc, "Invalid operation for arrays", getOperatorString(op));
3144 return false;
3145 }
3146 // At this point, size of implicitly sized arrays should be resolved.
3147 if(left->getArraySize() != right->getArraySize())
3148 {
3149 error(loc, "array size mismatch", getOperatorString(op));
3150 return false;
3151 }
3152 }
3153
3154 // Check ops which require integer / ivec parameters
3155 bool isBitShift = false;
3156 switch(op)
3157 {
3158 case EOpBitShiftLeft:
3159 case EOpBitShiftRight:
3160 case EOpBitShiftLeftAssign:
3161 case EOpBitShiftRightAssign:
3162 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3163 // check that the basic type is an integer type.
3164 isBitShift = true;
3165 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3166 {
3167 return false;
3168 }
3169 break;
3170 case EOpBitwiseAnd:
3171 case EOpBitwiseXor:
3172 case EOpBitwiseOr:
3173 case EOpBitwiseAndAssign:
3174 case EOpBitwiseXorAssign:
3175 case EOpBitwiseOrAssign:
3176 // It is enough to check the type of only one operand, since later it
3177 // is checked that the operand types match.
3178 if(!IsInteger(left->getBasicType()))
3179 {
3180 return false;
3181 }
3182 break;
3183 default:
3184 break;
3185 }
3186
3187 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3188 // So the basic type should usually match.
3189 if(!isBitShift && left->getBasicType() != right->getBasicType())
3190 {
3191 return false;
3192 }
3193
3194 // Check that type sizes match exactly on ops that require that.
3195 // Also check restrictions for structs that contain arrays or samplers.
3196 switch(op)
3197 {
3198 case EOpAssign:
3199 case EOpInitialize:
3200 case EOpEqual:
3201 case EOpNotEqual:
3202 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003203 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003204 {
3205 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3206 return false;
3207 }
3208 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3209 // we interpret the spec so that this extends to structs containing samplers,
3210 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003211 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003212 left->getType().isStructureContainingSamplers())
3213 {
3214 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3215 return false;
3216 }
3217 case EOpLessThan:
3218 case EOpGreaterThan:
3219 case EOpLessThanEqual:
3220 case EOpGreaterThanEqual:
3221 if((left->getNominalSize() != right->getNominalSize()) ||
3222 (left->getSecondarySize() != right->getSecondarySize()))
3223 {
3224 return false;
3225 }
Alexis Hetuec93b1d2016-12-09 16:01:29 -05003226 break;
3227 case EOpAdd:
3228 case EOpSub:
3229 case EOpDiv:
3230 case EOpIMod:
3231 case EOpBitShiftLeft:
3232 case EOpBitShiftRight:
3233 case EOpBitwiseAnd:
3234 case EOpBitwiseXor:
3235 case EOpBitwiseOr:
3236 case EOpAddAssign:
3237 case EOpSubAssign:
3238 case EOpDivAssign:
3239 case EOpIModAssign:
3240 case EOpBitShiftLeftAssign:
3241 case EOpBitShiftRightAssign:
3242 case EOpBitwiseAndAssign:
3243 case EOpBitwiseXorAssign:
3244 case EOpBitwiseOrAssign:
3245 if((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3246 {
3247 return false;
3248 }
3249
3250 // Are the sizes compatible?
3251 if(left->getNominalSize() != right->getNominalSize() || left->getSecondarySize() != right->getSecondarySize())
3252 {
3253 // If the nominal sizes of operands do not match:
3254 // One of them must be a scalar.
3255 if(!left->isScalar() && !right->isScalar())
3256 return false;
3257
3258 // In the case of compound assignment other than multiply-assign,
3259 // the right side needs to be a scalar. Otherwise a vector/matrix
3260 // would be assigned to a scalar. A scalar can't be shifted by a
3261 // vector either.
3262 if(!right->isScalar() && (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3263 return false;
3264 }
3265 break;
Alexis Hetuad6b8752015-06-09 16:15:30 -04003266 default:
3267 break;
3268 }
3269
3270 return true;
3271}
3272
Alexis Hetu76a343a2015-06-04 17:21:22 -04003273TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3274{
3275 TBasicType switchType = init->getBasicType();
3276 if((switchType != EbtInt && switchType != EbtUInt) ||
3277 init->isMatrix() ||
3278 init->isArray() ||
3279 init->isVector())
3280 {
3281 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3282 recover();
3283 return nullptr;
3284 }
3285
3286 if(statementList)
3287 {
3288 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3289 {
3290 recover();
3291 return nullptr;
3292 }
3293 }
3294
3295 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3296 if(node == nullptr)
3297 {
3298 error(loc, "erroneous switch statement", "switch");
3299 recover();
3300 return nullptr;
3301 }
3302 return node;
3303}
3304
3305TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3306{
Alexis Hetu0a655842015-06-22 16:52:11 -04003307 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003308 {
3309 error(loc, "case labels need to be inside switch statements", "case");
3310 recover();
3311 return nullptr;
3312 }
3313 if(condition == nullptr)
3314 {
3315 error(loc, "case label must have a condition", "case");
3316 recover();
3317 return nullptr;
3318 }
3319 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3320 condition->isMatrix() ||
3321 condition->isArray() ||
3322 condition->isVector())
3323 {
3324 error(condition->getLine(), "case label must be a scalar integer", "case");
3325 recover();
3326 }
3327 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3328 if(conditionConst == nullptr)
3329 {
3330 error(condition->getLine(), "case label must be constant", "case");
3331 recover();
3332 }
3333 TIntermCase *node = intermediate.addCase(condition, loc);
3334 if(node == nullptr)
3335 {
3336 error(loc, "erroneous case statement", "case");
3337 recover();
3338 return nullptr;
3339 }
3340 return node;
3341}
3342
3343TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3344{
Alexis Hetu0a655842015-06-22 16:52:11 -04003345 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003346 {
3347 error(loc, "default labels need to be inside switch statements", "default");
3348 recover();
3349 return nullptr;
3350 }
3351 TIntermCase *node = intermediate.addCase(nullptr, loc);
3352 if(node == nullptr)
3353 {
3354 error(loc, "erroneous default statement", "default");
3355 recover();
3356 return nullptr;
3357 }
3358 return node;
3359}
Alexis Hetue5246692015-06-18 12:34:52 -04003360TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3361{
3362 if(binaryOpCommonCheck(op, left, right, loc))
3363 {
3364 return intermediate.addAssign(op, left, right, loc);
3365 }
3366 return nullptr;
3367}
3368
3369TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3370{
3371 TIntermTyped *node = createAssign(op, left, right, loc);
3372 if(node == nullptr)
3373 {
3374 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3375 recover();
3376 return left;
3377 }
3378 return node;
3379}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003380
Alexis Hetub4769582015-06-16 12:19:50 -04003381TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3382 const TSourceLoc &loc)
3383{
3384 if(!binaryOpCommonCheck(op, left, right, loc))
3385 return nullptr;
3386
3387 switch(op)
3388 {
3389 case EOpEqual:
3390 case EOpNotEqual:
3391 break;
3392 case EOpLessThan:
3393 case EOpGreaterThan:
3394 case EOpLessThanEqual:
3395 case EOpGreaterThanEqual:
3396 ASSERT(!left->isArray() && !right->isArray());
3397 if(left->isMatrix() || left->isVector() ||
3398 left->getBasicType() == EbtStruct)
3399 {
3400 return nullptr;
3401 }
3402 break;
3403 case EOpLogicalOr:
3404 case EOpLogicalXor:
3405 case EOpLogicalAnd:
3406 ASSERT(!left->isArray() && !right->isArray());
3407 if(left->getBasicType() != EbtBool ||
3408 left->isMatrix() || left->isVector())
3409 {
3410 return nullptr;
3411 }
3412 break;
3413 case EOpAdd:
3414 case EOpSub:
3415 case EOpDiv:
3416 case EOpMul:
3417 ASSERT(!left->isArray() && !right->isArray());
3418 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3419 {
3420 return nullptr;
3421 }
3422 break;
3423 case EOpIMod:
3424 ASSERT(!left->isArray() && !right->isArray());
3425 // Note that this is only for the % operator, not for mod()
3426 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3427 {
3428 return nullptr;
3429 }
3430 break;
3431 // Note that for bitwise ops, type checking is done in promote() to
3432 // share code between ops and compound assignment
3433 default:
3434 break;
3435 }
3436
3437 return intermediate.addBinaryMath(op, left, right, loc);
3438}
3439
3440TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3441{
3442 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3443 if(node == 0)
3444 {
3445 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3446 recover();
3447 return left;
3448 }
3449 return node;
3450}
3451
3452TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3453{
3454 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3455 if(node == 0)
3456 {
3457 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3458 recover();
3459 ConstantUnion *unionArray = new ConstantUnion[1];
3460 unionArray->setBConst(false);
3461 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3462 }
3463 return node;
3464}
3465
Alexis Hetu76a343a2015-06-04 17:21:22 -04003466TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3467{
3468 switch(op)
3469 {
3470 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003471 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003472 {
3473 error(loc, "continue statement only allowed in loops", "");
3474 recover();
3475 }
3476 break;
3477 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003478 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003479 {
3480 error(loc, "break statement only allowed in loops and switch statements", "");
3481 recover();
3482 }
3483 break;
3484 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003485 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003486 {
3487 error(loc, "non-void function must return a value", "return");
3488 recover();
3489 }
3490 break;
3491 default:
3492 // No checks for discard
3493 break;
3494 }
3495 return intermediate.addBranch(op, loc);
3496}
3497
3498TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3499{
3500 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003501 mFunctionReturnsValue = true;
3502 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003503 {
3504 error(loc, "void function cannot return a value", "return");
3505 recover();
3506 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003507 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003508 {
3509 error(loc, "function return is not matching type:", "return");
3510 recover();
3511 }
3512 return intermediate.addBranch(op, returnValue, loc);
3513}
3514
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003515TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3516{
3517 *fatalError = false;
3518 TOperator op = fnCall->getBuiltInOp();
3519 TIntermTyped *callNode = nullptr;
3520
3521 if(thisNode != nullptr)
3522 {
3523 ConstantUnion *unionArray = new ConstantUnion[1];
3524 int arraySize = 0;
3525 TIntermTyped *typedThis = thisNode->getAsTyped();
3526 if(fnCall->getName() != "length")
3527 {
3528 error(loc, "invalid method", fnCall->getName().c_str());
3529 recover();
3530 }
3531 else if(paramNode != nullptr)
3532 {
3533 error(loc, "method takes no parameters", "length");
3534 recover();
3535 }
3536 else if(typedThis == nullptr || !typedThis->isArray())
3537 {
3538 error(loc, "length can only be called on arrays", "length");
3539 recover();
3540 }
3541 else
3542 {
3543 arraySize = typedThis->getArraySize();
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003544 }
3545 unionArray->setIConst(arraySize);
3546 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3547 }
3548 else if(op != EOpNull)
3549 {
3550 //
3551 // Then this should be a constructor.
3552 // Don't go through the symbol table for constructors.
3553 // Their parameters will be verified algorithmically.
3554 //
3555 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3556 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3557 {
3558 //
3559 // It's a constructor, of type 'type'.
3560 //
3561 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3562 }
3563
3564 if(callNode == nullptr)
3565 {
3566 recover();
3567 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3568 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003569 }
3570 else
3571 {
3572 //
3573 // Not a constructor. Find it in the symbol table.
3574 //
3575 const TFunction *fnCandidate;
3576 bool builtIn;
3577 fnCandidate = findFunction(loc, fnCall, &builtIn);
3578 if(fnCandidate)
3579 {
3580 //
3581 // A declared function.
3582 //
3583 if(builtIn && !fnCandidate->getExtension().empty() &&
3584 extensionErrorCheck(loc, fnCandidate->getExtension()))
3585 {
3586 recover();
3587 }
3588 op = fnCandidate->getBuiltInOp();
3589 if(builtIn && op != EOpNull)
3590 {
3591 //
3592 // A function call mapped to a built-in operation.
3593 //
3594 if(fnCandidate->getParamCount() == 1)
3595 {
3596 //
3597 // Treat it like a built-in unary operator.
3598 //
3599 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3600 if(callNode == nullptr)
3601 {
3602 std::stringstream extraInfoStream;
3603 extraInfoStream << "built in unary operator function. Type: "
3604 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3605 std::string extraInfo = extraInfoStream.str();
3606 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3607 *fatalError = true;
3608 return nullptr;
3609 }
3610 }
3611 else
3612 {
3613 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3614 aggregate->setType(fnCandidate->getReturnType());
3615
3616 // Some built-in functions have out parameters too.
3617 functionCallLValueErrorCheck(fnCandidate, aggregate);
3618
3619 callNode = aggregate;
Nicolas Capens91dfb972016-04-09 23:45:12 -04003620
3621 if(fnCandidate->getParamCount() == 2)
3622 {
3623 TIntermSequence &parameters = paramNode->getAsAggregate()->getSequence();
3624 TIntermTyped *left = parameters[0]->getAsTyped();
3625 TIntermTyped *right = parameters[1]->getAsTyped();
3626
3627 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
3628 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
3629 if (leftTempConstant && rightTempConstant)
3630 {
3631 TIntermTyped *typedReturnNode = leftTempConstant->fold(op, rightTempConstant, infoSink());
3632
3633 if(typedReturnNode)
3634 {
3635 callNode = typedReturnNode;
3636 }
3637 }
3638 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003639 }
3640 }
3641 else
3642 {
3643 // This is a real function call
3644
3645 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3646 aggregate->setType(fnCandidate->getReturnType());
3647
3648 // this is how we know whether the given function is a builtIn function or a user defined function
3649 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3650 // if builtIn == true, it's definitely a builtIn function with EOpNull
3651 if(!builtIn)
3652 aggregate->setUserDefined();
3653 aggregate->setName(fnCandidate->getMangledName());
3654
3655 callNode = aggregate;
3656
3657 functionCallLValueErrorCheck(fnCandidate, aggregate);
3658 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003659 }
3660 else
3661 {
3662 // error message was put out by findFunction()
3663 // Put on a dummy node for error recovery
3664 ConstantUnion *unionArray = new ConstantUnion[1];
3665 unionArray->setFConst(0.0f);
3666 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3667 recover();
3668 }
3669 }
3670 delete fnCall;
3671 return callNode;
3672}
3673
Alexis Hetueee212e2015-07-07 17:13:30 -04003674TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3675{
3676 if(boolErrorCheck(loc, cond))
3677 recover();
3678
3679 if(trueBlock->getType() != falseBlock->getType())
3680 {
3681 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3682 recover();
3683 return falseBlock;
3684 }
3685 // ESSL1 sections 5.2 and 5.7:
3686 // ESSL3 section 5.7:
3687 // Ternary operator is not among the operators allowed for structures/arrays.
3688 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3689 {
3690 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3691 recover();
3692 return falseBlock;
3693 }
3694 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3695}
3696
John Bauman66b8ab22014-05-06 15:57:45 -04003697//
3698// Parse an array of strings using yyparse.
3699//
3700// Returns 0 for success.
3701//
3702int PaParseStrings(int count, const char* const string[], const int length[],
Nicolas Capens0bac2852016-05-07 06:09:58 -04003703 TParseContext* context) {
3704 if ((count == 0) || !string)
3705 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003706
Nicolas Capens0bac2852016-05-07 06:09:58 -04003707 if (glslang_initialize(context))
3708 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003709
Nicolas Capens0bac2852016-05-07 06:09:58 -04003710 int error = glslang_scan(count, string, length, context);
3711 if (!error)
3712 error = glslang_parse(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003713
Nicolas Capens0bac2852016-05-07 06:09:58 -04003714 glslang_finalize(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003715
Nicolas Capens0bac2852016-05-07 06:09:58 -04003716 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003717}
3718
3719
3720