blob: 0be554f5ea5aa9cac45dc243501d3135afaa40e2 [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()) {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400784 const TFieldList& fields = type.getStruct()->fields();
785 for(unsigned int i = 0; i < fields.size(); ++i) {
786 if (containsSampler(*fields[i]->type()))
787 return true;
788 }
789 }
John Bauman66b8ab22014-05-06 15:57:45 -0400790
Nicolas Capens0bac2852016-05-07 06:09:58 -0400791 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400792}
793
794//
795// Do size checking for an array type's size.
796//
797// Returns true if there was an error.
798//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400799bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped* expr, int& size)
John Bauman66b8ab22014-05-06 15:57:45 -0400800{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400801 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500802
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500803 if (expr->getQualifier() != EvqConstExpr || constant == 0 || !constant->isScalarInt())
Nicolas Capens0bac2852016-05-07 06:09:58 -0400804 {
805 error(line, "array size must be a constant integer expression", "");
806 return true;
807 }
John Bauman66b8ab22014-05-06 15:57:45 -0400808
Nicolas Capens0bac2852016-05-07 06:09:58 -0400809 if (constant->getBasicType() == EbtUInt)
810 {
811 unsigned int uintSize = constant->getUConst(0);
812 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
813 {
814 error(line, "array size too large", "");
815 size = 1;
816 return true;
817 }
John Bauman66b8ab22014-05-06 15:57:45 -0400818
Nicolas Capens0bac2852016-05-07 06:09:58 -0400819 size = static_cast<int>(uintSize);
820 }
821 else
822 {
823 size = constant->getIConst(0);
Nicolas Capens3c20f802015-02-17 17:17:20 -0500824
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500825 if (size < 0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400826 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500827 error(line, "array size must be non-negative", "");
Nicolas Capens0bac2852016-05-07 06:09:58 -0400828 size = 1;
829 return true;
830 }
831 }
John Bauman66b8ab22014-05-06 15:57:45 -0400832
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500833 if(size == 0)
834 {
835 error(line, "array size must be greater than zero", "");
836 return true;
837 }
838
Nicolas Capens0bac2852016-05-07 06:09:58 -0400839 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400840}
841
842//
843// See if this qualifier can be an array.
844//
845// Returns true if there is an error.
846//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400847bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400848{
Alexis Hetud4e2ba52016-12-01 17:02:33 -0500849 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConstExpr && mShaderVersion < 300)) {
Nicolas Capens0bac2852016-05-07 06:09:58 -0400850 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
851 return true;
852 }
John Bauman66b8ab22014-05-06 15:57:45 -0400853
Nicolas Capens0bac2852016-05-07 06:09:58 -0400854 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400855}
856
857//
858// See if this type can be an array.
859//
860// Returns true if there is an error.
861//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400862bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, TPublicType type)
John Bauman66b8ab22014-05-06 15:57:45 -0400863{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400864 //
865 // Can the type be an array?
866 //
867 if (type.array) {
868 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
869 return true;
870 }
John Bauman66b8ab22014-05-06 15:57:45 -0400871
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500872 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
873 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section 4.3.4).
874 if(mShaderVersion >= 300 && type.type == EbtStruct && IsVarying(type.qualifier))
875 {
876 error(line, "cannot declare arrays of structs of this qualifier",
877 TType(type).getCompleteString().c_str());
878 return true;
879 }
880
Nicolas Capens0bac2852016-05-07 06:09:58 -0400881 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400882}
883
Alexis Hetufe1269e2015-06-16 12:43:32 -0400884bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -0400885{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400886 bool builtIn = false;
887 TSymbol* symbol = symbolTable.find(node->getSymbol(), mShaderVersion, &builtIn);
888 if (symbol == 0) {
889 error(line, " undeclared identifier", node->getSymbol().c_str());
890 return true;
891 }
892 TVariable* variable = static_cast<TVariable*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -0400893
Nicolas Capens0bac2852016-05-07 06:09:58 -0400894 type->setArrayInformationType(variable->getArrayInformationType());
895 variable->updateArrayInformationType(type);
John Bauman66b8ab22014-05-06 15:57:45 -0400896
Nicolas Capens0bac2852016-05-07 06:09:58 -0400897 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
898 // its an error
899 if (node->getSymbol() == "gl_FragData") {
900 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", mShaderVersion, &builtIn);
901 ASSERT(fragData);
John Bauman66b8ab22014-05-06 15:57:45 -0400902
Nicolas Capens0bac2852016-05-07 06:09:58 -0400903 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
904 if (fragDataValue <= size) {
905 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
906 return true;
907 }
908 }
John Bauman66b8ab22014-05-06 15:57:45 -0400909
Nicolas Capens0bac2852016-05-07 06:09:58 -0400910 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
911 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
912 if (!updateFlag)
913 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400914
Nicolas Capens0bac2852016-05-07 06:09:58 -0400915 size++;
916 variable->getType().setMaxArraySize(size);
917 type->setMaxArraySize(size);
918 TType* tt = type;
John Bauman66b8ab22014-05-06 15:57:45 -0400919
Nicolas Capens0bac2852016-05-07 06:09:58 -0400920 while(tt->getArrayInformationType() != 0) {
921 tt = tt->getArrayInformationType();
922 tt->setMaxArraySize(size);
923 }
John Bauman66b8ab22014-05-06 15:57:45 -0400924
Nicolas Capens0bac2852016-05-07 06:09:58 -0400925 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400926}
927
928//
929// Enforce non-initializer type/qualifier rules.
930//
931// Returns true if there was an error.
932//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400933bool TParseContext::nonInitConstErrorCheck(const TSourceLoc &line, TString& identifier, TPublicType& type, bool array)
John Bauman66b8ab22014-05-06 15:57:45 -0400934{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400935 if (type.qualifier == EvqConstExpr)
936 {
937 // Make the qualifier make sense.
938 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400939
Nicolas Capens0bac2852016-05-07 06:09:58 -0400940 if (array)
941 {
942 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
943 }
944 else if (type.isStructureContainingArrays())
945 {
946 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
947 }
948 else
949 {
950 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
951 }
John Bauman66b8ab22014-05-06 15:57:45 -0400952
Nicolas Capens0bac2852016-05-07 06:09:58 -0400953 return true;
954 }
John Bauman66b8ab22014-05-06 15:57:45 -0400955
Nicolas Capens0bac2852016-05-07 06:09:58 -0400956 return false;
John Bauman66b8ab22014-05-06 15:57:45 -0400957}
958
959//
960// Do semantic checking for a variable declaration that has no initializer,
961// and update the symbol table.
962//
963// Returns true if there was an error.
964//
Alexis Hetufe1269e2015-06-16 12:43:32 -0400965bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString& identifier, TPublicType& type)
John Bauman66b8ab22014-05-06 15:57:45 -0400966{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400967 if(type.qualifier == EvqConstExpr)
968 {
969 // Make the qualifier make sense.
970 type.qualifier = EvqTemporary;
John Bauman66b8ab22014-05-06 15:57:45 -0400971
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400972 // Generate informative error messages for ESSL1.
973 // In ESSL3 arrays and structures containing arrays can be constant.
Alexis Hetu0a655842015-06-22 16:52:11 -0400974 if(mShaderVersion < 300 && type.isStructureContainingArrays())
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400975 {
976 error(line,
977 "structures containing arrays may not be declared constant since they cannot be initialized",
978 identifier.c_str());
979 }
980 else
981 {
982 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
983 }
John Bauman66b8ab22014-05-06 15:57:45 -0400984
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400985 return true;
986 }
987 if(type.isUnsizedArray())
988 {
989 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
990 return true;
991 }
992 return false;
993}
John Bauman66b8ab22014-05-06 15:57:45 -0400994
Alexis Hetudd7ff7a2015-06-11 08:25:30 -0400995// Do some simple checks that are shared between all variable declarations,
996// and update the symbol table.
997//
998// Returns true if declaring the variable succeeded.
999//
1000bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
1001 TVariable **variable)
1002{
1003 ASSERT((*variable) == nullptr);
John Bauman66b8ab22014-05-06 15:57:45 -04001004
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001005 // gl_LastFragData may be redeclared with a new precision qualifier
1006 if(type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1007 {
1008 const TVariable *maxDrawBuffers =
Alexis Hetu0a655842015-06-22 16:52:11 -04001009 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001010 if(type.getArraySize() != maxDrawBuffers->getConstPointer()->getIConst())
1011 {
1012 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
1013 return false;
1014 }
1015 }
1016
1017 if(reservedErrorCheck(line, identifier))
1018 return false;
1019
1020 (*variable) = new TVariable(&identifier, type);
1021 if(!symbolTable.declare(**variable))
1022 {
1023 error(line, "redefinition", identifier.c_str());
1024 delete (*variable);
1025 (*variable) = nullptr;
1026 return false;
1027 }
1028
1029 if(voidErrorCheck(line, identifier, type.getBasicType()))
1030 return false;
1031
1032 return true;
John Bauman66b8ab22014-05-06 15:57:45 -04001033}
1034
Alexis Hetufe1269e2015-06-16 12:43:32 -04001035bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001036{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001037 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
1038 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
1039 return true;
1040 }
1041 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
1042 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
1043 return true;
1044 }
John Bauman66b8ab22014-05-06 15:57:45 -04001045
Nicolas Capens0bac2852016-05-07 06:09:58 -04001046 if (qualifier == EvqConstReadOnly)
1047 type->setQualifier(EvqConstReadOnly);
1048 else
1049 type->setQualifier(paramQualifier);
John Bauman66b8ab22014-05-06 15:57:45 -04001050
Nicolas Capens0bac2852016-05-07 06:09:58 -04001051 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001052}
1053
Alexis Hetufe1269e2015-06-16 12:43:32 -04001054bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString& extension)
John Bauman66b8ab22014-05-06 15:57:45 -04001055{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001056 const TExtensionBehavior& extBehavior = extensionBehavior();
1057 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
1058 if (iter == extBehavior.end()) {
1059 error(line, "extension", extension.c_str(), "is not supported");
1060 return true;
1061 }
1062 // In GLSL ES, an extension's default behavior is "disable".
1063 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
1064 error(line, "extension", extension.c_str(), "is disabled");
1065 return true;
1066 }
1067 if (iter->second == EBhWarn) {
1068 warning(line, "extension", extension.c_str(), "is being used");
1069 return false;
1070 }
John Bauman66b8ab22014-05-06 15:57:45 -04001071
Nicolas Capens0bac2852016-05-07 06:09:58 -04001072 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001073}
1074
Alexis Hetuad6b8752015-06-09 16:15:30 -04001075bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1076{
1077 for(size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1078 {
1079 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1080 if(qual == EvqOut || qual == EvqInOut)
1081 {
1082 TIntermTyped *node = (aggregate->getSequence())[i]->getAsTyped();
1083 if(lValueErrorCheck(node->getLine(), "assign", node))
1084 {
1085 error(node->getLine(),
1086 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1087 recover();
1088 return true;
1089 }
1090 }
1091 }
1092 return false;
1093}
1094
Alexis Hetuad527752015-07-07 13:31:44 -04001095void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1096{
1097 switch(qualifier)
1098 {
1099 case EvqVaryingOut:
1100 case EvqSmoothOut:
1101 case EvqFlatOut:
1102 case EvqCentroidOut:
1103 case EvqVertexOut:
1104 case EvqFragmentOut:
1105 break;
1106 default:
1107 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1108 recover();
1109 break;
1110 }
1111}
1112
John Bauman66b8ab22014-05-06 15:57:45 -04001113bool TParseContext::supportsExtension(const char* extension)
1114{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001115 const TExtensionBehavior& extbehavior = extensionBehavior();
1116 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1117 return (iter != extbehavior.end());
John Bauman66b8ab22014-05-06 15:57:45 -04001118}
1119
Alexis Hetufe1269e2015-06-16 12:43:32 -04001120void TParseContext::handleExtensionDirective(const TSourceLoc &line, const char* extName, const char* behavior)
John Bauman66b8ab22014-05-06 15:57:45 -04001121{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001122 pp::SourceLocation loc(line.first_file, line.first_line);
1123 mDirectiveHandler.handleExtension(loc, extName, behavior);
John Bauman66b8ab22014-05-06 15:57:45 -04001124}
1125
Alexis Hetue13238e2017-12-15 18:01:07 -05001126void TParseContext::handlePragmaDirective(const TSourceLoc &line, const char* name, const char* value, bool stdgl)
John Bauman66b8ab22014-05-06 15:57:45 -04001127{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001128 pp::SourceLocation loc(line.first_file, line.first_line);
Alexis Hetue13238e2017-12-15 18:01:07 -05001129 mDirectiveHandler.handlePragma(loc, name, value, stdgl);
John Bauman66b8ab22014-05-06 15:57:45 -04001130}
1131
1132/////////////////////////////////////////////////////////////////////////////////
1133//
1134// Non-Errors.
1135//
1136/////////////////////////////////////////////////////////////////////////////////
1137
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001138const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1139 const TString *name,
1140 const TSymbol *symbol)
1141{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001142 const TVariable *variable = nullptr;
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001143
1144 if(!symbol)
1145 {
1146 error(location, "undeclared identifier", name->c_str());
1147 recover();
1148 }
1149 else if(!symbol->isVariable())
1150 {
1151 error(location, "variable expected", name->c_str());
1152 recover();
1153 }
1154 else
1155 {
1156 variable = static_cast<const TVariable*>(symbol);
1157
Alexis Hetu0a655842015-06-22 16:52:11 -04001158 if(symbolTable.findBuiltIn(variable->getName(), mShaderVersion))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001159 {
1160 recover();
1161 }
1162
1163 // Reject shaders using both gl_FragData and gl_FragColor
1164 TQualifier qualifier = variable->getType().getQualifier();
1165 if(qualifier == EvqFragData)
1166 {
1167 mUsesFragData = true;
1168 }
1169 else if(qualifier == EvqFragColor)
1170 {
1171 mUsesFragColor = true;
1172 }
1173
1174 // This validation is not quite correct - it's only an error to write to
1175 // both FragData and FragColor. For simplicity, and because users shouldn't
Nicolas Capens8b124c12016-04-18 14:09:37 -04001176 // be rewarded for reading from undefined variables, return an error
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001177 // if they are both referenced, rather than assigned.
1178 if(mUsesFragData && mUsesFragColor)
1179 {
1180 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1181 recover();
1182 }
1183 }
1184
1185 if(!variable)
1186 {
1187 TType type(EbtFloat, EbpUndefined);
1188 TVariable *fakeVariable = new TVariable(name, type);
1189 symbolTable.declare(*fakeVariable);
1190 variable = fakeVariable;
1191 }
1192
1193 return variable;
1194}
1195
John Bauman66b8ab22014-05-06 15:57:45 -04001196//
1197// Look up a function name in the symbol table, and make sure it is a function.
1198//
1199// Return the function symbol if found, otherwise 0.
1200//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001201const TFunction* TParseContext::findFunction(const TSourceLoc &line, TFunction* call, bool *builtIn)
John Bauman66b8ab22014-05-06 15:57:45 -04001202{
Nicolas Capens0bac2852016-05-07 06:09:58 -04001203 // First find by unmangled name to check whether the function name has been
1204 // hidden by a variable name or struct typename.
1205 const TSymbol* symbol = symbolTable.find(call->getName(), mShaderVersion, builtIn);
1206 if (symbol == 0) {
1207 symbol = symbolTable.find(call->getMangledName(), mShaderVersion, builtIn);
1208 }
John Bauman66b8ab22014-05-06 15:57:45 -04001209
Nicolas Capens0bac2852016-05-07 06:09:58 -04001210 if (symbol == 0) {
1211 error(line, "no matching overloaded function found", call->getName().c_str());
Alexis Hetuf0005a12016-09-28 15:52:21 -04001212 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001213 }
John Bauman66b8ab22014-05-06 15:57:45 -04001214
Nicolas Capens0bac2852016-05-07 06:09:58 -04001215 if (!symbol->isFunction()) {
1216 error(line, "function name expected", call->getName().c_str());
Alexis Hetuf0005a12016-09-28 15:52:21 -04001217 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04001218 }
John Bauman66b8ab22014-05-06 15:57:45 -04001219
Nicolas Capens0bac2852016-05-07 06:09:58 -04001220 return static_cast<const TFunction*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -04001221}
1222
1223//
1224// Initializers show up in several places in the grammar. Have one set of
1225// code to handle them here.
1226//
Alexis Hetufe1269e2015-06-16 12:43:32 -04001227bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, const TPublicType& pType,
Nicolas Capens0bac2852016-05-07 06:09:58 -04001228 TIntermTyped *initializer, TIntermNode **intermNode)
John Bauman66b8ab22014-05-06 15:57:45 -04001229{
Alexis Hetue5246692015-06-18 12:34:52 -04001230 ASSERT(intermNode != nullptr);
1231 TType type = TType(pType);
John Bauman66b8ab22014-05-06 15:57:45 -04001232
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001233 if(type.isUnsizedArray())
Alexis Hetue5246692015-06-18 12:34:52 -04001234 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001235 // We have not checked yet whether the initializer actually is an array or not.
1236 if(initializer->isArray())
1237 {
1238 type.setArraySize(initializer->getArraySize());
1239 }
1240 else
1241 {
1242 // Having a non-array initializer for an unsized array will result in an error later,
1243 // so we don't generate an error message here.
1244 type.setArraySize(1u);
1245 }
Alexis Hetue5246692015-06-18 12:34:52 -04001246 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001247
1248 TVariable *variable = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001249 if(!declareVariable(line, identifier, type, &variable))
1250 {
1251 return true;
1252 }
John Bauman66b8ab22014-05-06 15:57:45 -04001253
Nicolas Capensbf1307b2016-04-07 01:03:14 -04001254 if(symbolTable.atGlobalLevel() && initializer->getQualifier() != EvqConstExpr)
Alexis Hetue5246692015-06-18 12:34:52 -04001255 {
Alexis Hetue5246692015-06-18 12:34:52 -04001256 error(line, "global variable initializers must be constant expressions", "=");
1257 return true;
1258 }
John Bauman66b8ab22014-05-06 15:57:45 -04001259
Nicolas Capens0bac2852016-05-07 06:09:58 -04001260 //
1261 // identifier must be of type constant, a global, or a temporary
1262 //
1263 TQualifier qualifier = type.getQualifier();
1264 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
1265 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1266 return true;
1267 }
1268 //
1269 // test for and propagate constant
1270 //
John Bauman66b8ab22014-05-06 15:57:45 -04001271
Nicolas Capens0bac2852016-05-07 06:09:58 -04001272 if (qualifier == EvqConstExpr) {
1273 if (qualifier != initializer->getQualifier()) {
1274 std::stringstream extraInfoStream;
1275 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1276 std::string extraInfo = extraInfoStream.str();
1277 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1278 variable->getType().setQualifier(EvqTemporary);
1279 return true;
1280 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001281
Nicolas Capens0bac2852016-05-07 06:09:58 -04001282 if (type != initializer->getType()) {
1283 error(line, " non-matching types for const initializer ",
1284 variable->getType().getQualifierString());
1285 variable->getType().setQualifier(EvqTemporary);
1286 return true;
1287 }
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001288
Nicolas Capens0bac2852016-05-07 06:09:58 -04001289 if (initializer->getAsConstantUnion()) {
1290 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1291 } else if (initializer->getAsSymbolNode()) {
1292 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1293 const TVariable* tVar = static_cast<const TVariable*>(symbol);
John Bauman66b8ab22014-05-06 15:57:45 -04001294
Nicolas Capens0bac2852016-05-07 06:09:58 -04001295 ConstantUnion* constArray = tVar->getConstPointer();
1296 variable->shareConstPointer(constArray);
1297 }
1298 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001299
Nicolas Capens0bac2852016-05-07 06:09:58 -04001300 if (!variable->isConstant()) {
1301 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1302 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1303 if(*intermNode == nullptr) {
1304 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1305 return true;
1306 }
1307 } else
1308 *intermNode = nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04001309
Nicolas Capens0bac2852016-05-07 06:09:58 -04001310 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04001311}
1312
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001313TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier, const TPublicType &typeSpecifier)
1314{
1315 TPublicType returnType = typeSpecifier;
1316 returnType.qualifier = qualifier;
1317 returnType.invariant = invariant;
1318 returnType.layoutQualifier = layoutQualifier;
1319
Alexis Hetu0a655842015-06-22 16:52:11 -04001320 if(mShaderVersion < 300)
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001321 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001322 if(typeSpecifier.array)
1323 {
1324 error(typeSpecifier.line, "not supported", "first-class array");
1325 returnType.clearArrayness();
1326 }
1327
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001328 if(qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1329 {
1330 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1331 recover();
1332 }
1333
1334 if((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1335 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1336 {
1337 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1338 recover();
1339 }
1340 }
1341 else
1342 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001343 if(!returnType.layoutQualifier.isEmpty())
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001344 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001345 globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout");
1346 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001347
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001348 if(IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn || returnType.qualifier == EvqFragmentOut)
1349 {
1350 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier, typeSpecifier.line);
Alexis Hetu42ff6b12015-06-03 16:03:48 -04001351 }
1352 }
1353
1354 return returnType;
1355}
1356
Alexis Hetuec93b1d2016-12-09 16:01:29 -05001357void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1358 const TPublicType &type,
1359 const TSourceLoc &qualifierLocation)
1360{
1361 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1362 if(type.type == EbtBool)
1363 {
1364 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1365 }
1366
1367 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1368 switch(qualifier)
1369 {
1370 case EvqVertexIn:
1371 // ESSL 3.00 section 4.3.4
1372 if(type.array)
1373 {
1374 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1375 }
1376 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1377 return;
1378 case EvqFragmentOut:
1379 // ESSL 3.00 section 4.3.6
1380 if(type.isMatrix())
1381 {
1382 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1383 }
1384 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1385 return;
1386 default:
1387 break;
1388 }
1389
1390 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1391 // restrictions.
1392 bool typeContainsIntegers = (type.type == EbtInt || type.type == EbtUInt ||
1393 type.isStructureContainingType(EbtInt) ||
1394 type.isStructureContainingType(EbtUInt));
1395 if(typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1396 {
1397 error(qualifierLocation, "must use 'flat' interpolation here", getQualifierString(qualifier));
1398 }
1399
1400 if(type.type == EbtStruct)
1401 {
1402 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1403 // These restrictions are only implied by the ESSL 3.00 spec, but
1404 // the ESSL 3.10 spec lists these restrictions explicitly.
1405 if(type.array)
1406 {
1407 error(qualifierLocation, "cannot be an array of structures", getQualifierString(qualifier));
1408 }
1409 if(type.isStructureContainingArrays())
1410 {
1411 error(qualifierLocation, "cannot be a structure containing an array", getQualifierString(qualifier));
1412 }
1413 if(type.isStructureContainingType(EbtStruct))
1414 {
1415 error(qualifierLocation, "cannot be a structure containing a structure", getQualifierString(qualifier));
1416 }
1417 if(type.isStructureContainingType(EbtBool))
1418 {
1419 error(qualifierLocation, "cannot be a structure containing a bool", getQualifierString(qualifier));
1420 }
1421 }
1422}
1423
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001424TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1425 const TSourceLoc &identifierOrTypeLocation,
1426 const TString &identifier)
1427{
1428 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
1429
1430 bool emptyDeclaration = (identifier == "");
1431
1432 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1433
1434 if(emptyDeclaration)
1435 {
1436 if(publicType.isUnsizedArray())
1437 {
1438 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1439 // It is assumed that this applies to empty declarations as well.
1440 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1441 }
1442 }
1443 else
1444 {
1445 if(singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
1446 recover();
1447
1448 if(nonInitErrorCheck(identifierOrTypeLocation, identifier, publicType))
1449 recover();
1450
1451 TVariable *variable = nullptr;
1452 if(!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
1453 recover();
1454
1455 if(variable && symbol)
1456 symbol->setId(variable->getUniqueId());
1457 }
1458
1459 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
1460}
1461
1462TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1463 const TSourceLoc &identifierLocation,
1464 const TString &identifier,
1465 const TSourceLoc &indexLocation,
1466 TIntermTyped *indexExpression)
1467{
1468 mDeferredSingleDeclarationErrorCheck = false;
1469
1470 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1471 recover();
1472
1473 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1474 recover();
1475
1476 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1477 {
1478 recover();
1479 }
1480
1481 TType arrayType(publicType);
1482
1483 int size;
1484 if(arraySizeErrorCheck(identifierLocation, indexExpression, size))
1485 {
1486 recover();
1487 }
1488 // Make the type an array even if size check failed.
1489 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1490 arrayType.setArraySize(size);
1491
1492 TVariable *variable = nullptr;
1493 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1494 recover();
1495
1496 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1497 if(variable && symbol)
1498 symbol->setId(variable->getUniqueId());
1499
1500 return intermediate.makeAggregate(symbol, identifierLocation);
1501}
1502
1503TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1504 const TSourceLoc &identifierLocation,
1505 const TString &identifier,
1506 const TSourceLoc &initLocation,
1507 TIntermTyped *initializer)
1508{
1509 mDeferredSingleDeclarationErrorCheck = false;
1510
1511 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1512 recover();
1513
1514 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001515 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001516 {
1517 //
1518 // Build intermediate representation
1519 //
1520 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
1521 }
1522 else
1523 {
1524 recover();
1525 return nullptr;
1526 }
1527}
1528
1529TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1530 const TSourceLoc &identifierLocation,
1531 const TString &identifier,
1532 const TSourceLoc &indexLocation,
1533 TIntermTyped *indexExpression,
1534 const TSourceLoc &initLocation,
1535 TIntermTyped *initializer)
1536{
1537 mDeferredSingleDeclarationErrorCheck = false;
1538
1539 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1540 recover();
1541
1542 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1543 {
1544 recover();
1545 }
1546
1547 TPublicType arrayType(publicType);
1548
1549 int size = 0;
1550 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1551 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1552 {
1553 recover();
1554 }
1555 // Make the type an array even if size check failed.
1556 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1557 arrayType.setArray(true, size);
1558
1559 // initNode will correspond to the whole of "type b[n] = initializer".
1560 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001561 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001562 {
1563 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1564 }
1565 else
1566 {
1567 recover();
1568 return nullptr;
1569 }
1570}
1571
1572TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1573 const TSourceLoc &identifierLoc,
1574 const TString *identifier,
1575 const TSymbol *symbol)
1576{
1577 // invariant declaration
1578 if(globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1579 {
1580 recover();
1581 }
1582
1583 if(!symbol)
1584 {
1585 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1586 recover();
1587 return nullptr;
1588 }
1589 else
1590 {
1591 const TString kGlFrontFacing("gl_FrontFacing");
1592 if(*identifier == kGlFrontFacing)
1593 {
1594 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1595 recover();
1596 return nullptr;
1597 }
1598 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1599 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1600 ASSERT(variable);
1601 const TType &type = variable->getType();
1602 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1603 *identifier, type, identifierLoc);
1604
1605 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1606 aggregate->setOp(EOpInvariantDeclaration);
1607 return aggregate;
1608 }
1609}
1610
1611TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1612 const TSourceLoc &identifierLocation, const TString &identifier)
1613{
1614 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1615 if(mDeferredSingleDeclarationErrorCheck)
1616 {
1617 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1618 recover();
1619 mDeferredSingleDeclarationErrorCheck = false;
1620 }
1621
1622 if(locationDeclaratorListCheck(identifierLocation, publicType))
1623 recover();
1624
1625 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1626 recover();
1627
1628 TVariable *variable = nullptr;
1629 if(!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
1630 recover();
1631
1632 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1633 if(variable && symbol)
1634 symbol->setId(variable->getUniqueId());
1635
1636 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1637}
1638
1639TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1640 const TSourceLoc &identifierLocation, const TString &identifier,
1641 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
1642{
1643 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1644 if(mDeferredSingleDeclarationErrorCheck)
1645 {
1646 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1647 recover();
1648 mDeferredSingleDeclarationErrorCheck = false;
1649 }
1650
1651 if(locationDeclaratorListCheck(identifierLocation, publicType))
1652 recover();
1653
1654 if(nonInitErrorCheck(identifierLocation, identifier, publicType))
1655 recover();
1656
1657 if(arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1658 {
1659 recover();
1660 }
1661 else
1662 {
1663 TType arrayType = TType(publicType);
1664 int size;
1665 if(arraySizeErrorCheck(arrayLocation, indexExpression, size))
1666 {
1667 recover();
1668 }
1669 arrayType.setArraySize(size);
1670
1671 TVariable *variable = nullptr;
1672 if(!declareVariable(identifierLocation, identifier, arrayType, &variable))
1673 recover();
1674
1675 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1676 if(variable && symbol)
1677 symbol->setId(variable->getUniqueId());
1678
1679 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1680 }
1681
1682 return nullptr;
1683}
1684
1685TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1686 const TSourceLoc &identifierLocation, const TString &identifier,
1687 const TSourceLoc &initLocation, TIntermTyped *initializer)
1688{
1689 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1690 if(mDeferredSingleDeclarationErrorCheck)
1691 {
1692 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1693 recover();
1694 mDeferredSingleDeclarationErrorCheck = false;
1695 }
1696
1697 if(locationDeclaratorListCheck(identifierLocation, publicType))
1698 recover();
1699
1700 TIntermNode *intermNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001701 if(!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001702 {
1703 //
1704 // build the intermediate representation
1705 //
1706 if(intermNode)
1707 {
1708 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
1709 }
1710 else
1711 {
1712 return aggregateDeclaration;
1713 }
1714 }
1715 else
1716 {
1717 recover();
1718 return nullptr;
1719 }
1720}
1721
1722TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
1723 TIntermAggregate *aggregateDeclaration,
1724 const TSourceLoc &identifierLocation,
1725 const TString &identifier,
1726 const TSourceLoc &indexLocation,
1727 TIntermTyped *indexExpression,
1728 const TSourceLoc &initLocation, TIntermTyped *initializer)
1729{
1730 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1731 if(mDeferredSingleDeclarationErrorCheck)
1732 {
1733 if(singleDeclarationErrorCheck(publicType, identifierLocation))
1734 recover();
1735 mDeferredSingleDeclarationErrorCheck = false;
1736 }
1737
1738 if(locationDeclaratorListCheck(identifierLocation, publicType))
1739 recover();
1740
1741 if(arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1742 {
1743 recover();
1744 }
1745
1746 TPublicType arrayType(publicType);
1747
1748 int size = 0;
1749 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1750 if(indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
1751 {
1752 recover();
1753 }
1754 // Make the type an array even if size check failed.
1755 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1756 arrayType.setArray(true, size);
1757
1758 // initNode will correspond to the whole of "b[n] = initializer".
1759 TIntermNode *initNode = nullptr;
Alexis Hetue5246692015-06-18 12:34:52 -04001760 if(!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04001761 {
1762 if(initNode)
1763 {
1764 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1765 }
1766 else
1767 {
1768 return aggregateDeclaration;
1769 }
1770 }
1771 else
1772 {
1773 recover();
1774 return nullptr;
1775 }
1776}
1777
Alexis Hetua35d8232015-06-11 17:11:06 -04001778void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1779{
Alexis Hetu0a655842015-06-22 16:52:11 -04001780 if(mShaderVersion < 300)
Alexis Hetua35d8232015-06-11 17:11:06 -04001781 {
1782 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1783 recover();
1784 return;
1785 }
1786
1787 if(typeQualifier.qualifier != EvqUniform)
1788 {
1789 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1790 recover();
1791 return;
1792 }
1793
1794 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1795 ASSERT(!layoutQualifier.isEmpty());
1796
1797 if(layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1798 {
1799 recover();
1800 return;
1801 }
1802
1803 if(layoutQualifier.matrixPacking != EmpUnspecified)
1804 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001805 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04001806 }
1807
1808 if(layoutQualifier.blockStorage != EbsUnspecified)
1809 {
Alexis Hetu0a655842015-06-22 16:52:11 -04001810 mDefaultBlockStorage = layoutQualifier.blockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04001811 }
1812}
1813
Alexis Hetu407813b2016-02-24 16:46:13 -05001814TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function, const TSourceLoc &location)
1815{
1816 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1817 // Either way the instance in the symbol table is used to track whether the function is declared
1818 // multiple times.
1819 TFunction *symbolTableFunction =
1820 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1821 if(symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1822 {
1823 // ESSL 1.00.17 section 4.2.7.
1824 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1825 error(location, "duplicate function prototype declarations are not allowed", "function");
1826 recover();
1827 }
1828 symbolTableFunction->setHasPrototypeDeclaration();
1829
1830 TIntermAggregate *prototype = new TIntermAggregate;
1831 prototype->setType(function.getReturnType());
1832 prototype->setName(function.getMangledName());
1833
1834 for(size_t i = 0; i < function.getParamCount(); i++)
1835 {
1836 const TParameter &param = function.getParam(i);
1837 if(param.name != 0)
1838 {
1839 TVariable variable(param.name, *param.type);
1840
1841 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1842 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1843 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1844 }
1845 else
1846 {
1847 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1848 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1849 }
1850 }
1851
1852 prototype->setOp(EOpPrototype);
1853
1854 symbolTable.pop();
1855
1856 if(!symbolTable.atGlobalLevel())
1857 {
1858 // ESSL 3.00.4 section 4.2.4.
1859 error(location, "local function prototype declarations are not allowed", "function");
1860 recover();
1861 }
1862
1863 return prototype;
1864}
1865
1866TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function, TIntermAggregate *functionPrototype, TIntermAggregate *functionBody, const TSourceLoc &location)
1867{
1868 //?? Check that all paths return a value if return type != void ?
1869 // May be best done as post process phase on intermediate code
1870 if(mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1871 {
1872 error(location, "function does not return a value:", "", function.getName().c_str());
1873 recover();
1874 }
1875
1876 TIntermAggregate *aggregate = intermediate.growAggregate(functionPrototype, functionBody, location);
1877 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
1878 aggregate->setName(function.getMangledName().c_str());
1879 aggregate->setType(function.getReturnType());
1880
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001881 // store the pragma information for debug and optimize and other vendor specific
1882 // information. This information can be queried from the parse tree
1883 aggregate->setOptimize(pragma().optimize);
Alexis Hetu407813b2016-02-24 16:46:13 -05001884 aggregate->setDebug(pragma().debug);
1885
Nicolas Capens0863f0d2016-04-10 00:30:02 -04001886 if(functionBody && functionBody->getAsAggregate())
Alexis Hetu407813b2016-02-24 16:46:13 -05001887 aggregate->setEndLine(functionBody->getAsAggregate()->getEndLine());
1888
1889 symbolTable.pop();
1890 return aggregate;
1891}
1892
1893void TParseContext::parseFunctionPrototype(const TSourceLoc &location, TFunction *function, TIntermAggregate **aggregateOut)
1894{
1895 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1896
1897 if(builtIn)
1898 {
1899 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1900 recover();
1901 }
1902
1903 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1904 //
1905 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1906 // as it would have just been put in the symbol table. Otherwise, we're looking up
1907 // an earlier occurance.
1908 //
1909 if(prevDec->isDefined())
1910 {
1911 // Then this function already has a body.
1912 error(location, "function already has a body", function->getName().c_str());
1913 recover();
1914 }
1915 prevDec->setDefined();
1916 //
1917 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1918 // Eventually we will probably want to have only a single definition and just swap the
1919 // arguments to be the definition's arguments.
1920 //
1921 function->setUniqueId(prevDec->getUniqueId());
1922
1923 // Raise error message if main function takes any parameters or return anything other than void
1924 if(function->getName() == "main")
1925 {
1926 if(function->getParamCount() > 0)
1927 {
1928 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1929 recover();
1930 }
1931 if(function->getReturnType().getBasicType() != EbtVoid)
1932 {
1933 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1934 recover();
1935 }
1936 }
1937
1938 //
1939 // Remember the return type for later checking for RETURN statements.
1940 //
1941 mCurrentFunctionType = &(prevDec->getReturnType());
1942 mFunctionReturnsValue = false;
1943
1944 //
1945 // Insert parameters into the symbol table.
1946 // If the parameter has no name, it's not an error, just don't insert it
1947 // (could be used for unused args).
1948 //
1949 // Also, accumulate the list of parameters into the HIL, so lower level code
1950 // knows where to find parameters.
1951 //
1952 TIntermAggregate *paramNodes = new TIntermAggregate;
1953 for(size_t i = 0; i < function->getParamCount(); i++)
1954 {
1955 const TParameter &param = function->getParam(i);
1956 if(param.name != 0)
1957 {
1958 TVariable *variable = new TVariable(param.name, *param.type);
1959 //
1960 // Insert the parameters with name in the symbol table.
1961 //
1962 if(!symbolTable.declare(*variable))
1963 {
1964 error(location, "redefinition", variable->getName().c_str());
1965 recover();
1966 paramNodes = intermediate.growAggregate(
1967 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1968 continue;
1969 }
1970
1971 //
1972 // Add the parameter to the HIL
1973 //
1974 TIntermSymbol *symbol = intermediate.addSymbol(
1975 variable->getUniqueId(), variable->getName(), variable->getType(), location);
1976
1977 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1978 }
1979 else
1980 {
1981 paramNodes = intermediate.growAggregate(
1982 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1983 }
1984 }
1985 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1986 *aggregateOut = paramNodes;
1987 setLoopNestingLevel(0);
1988}
1989
1990TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
1991{
1992 //
1993 // We don't know at this point whether this is a function definition or a prototype.
1994 // The definition production code will check for redefinitions.
1995 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
1996 //
1997 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
1998 // here.
1999 //
2000 TFunction *prevDec = static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002001 if(getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2002 {
2003 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2004 // Therefore overloading or redefining builtin functions is an error.
2005 error(location, "Name of a built-in function cannot be redeclared as function", function->getName().c_str());
2006 }
2007 else if(prevDec)
Alexis Hetu407813b2016-02-24 16:46:13 -05002008 {
2009 if(prevDec->getReturnType() != function->getReturnType())
2010 {
2011 error(location, "overloaded functions must have the same return type",
2012 function->getReturnType().getBasicString());
2013 recover();
2014 }
2015 for(size_t i = 0; i < prevDec->getParamCount(); ++i)
2016 {
2017 if(prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
2018 {
2019 error(location, "overloaded functions must have the same parameter qualifiers",
2020 function->getParam(i).type->getQualifierString());
2021 recover();
2022 }
2023 }
2024 }
2025
2026 //
2027 // Check for previously declared variables using the same name.
2028 //
2029 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
2030 if(prevSym)
2031 {
2032 if(!prevSym->isFunction())
2033 {
2034 error(location, "redefinition", function->getName().c_str(), "function");
2035 recover();
2036 }
2037 }
2038
2039 // We're at the inner scope level of the function's arguments and body statement.
2040 // Add the function prototype to the surrounding scope instead.
2041 symbolTable.getOuterLevel()->insert(*function);
2042
2043 //
2044 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2045 // variable names from this one, and not the one that's
2046 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2047 //
2048 return function;
2049}
2050
Alexis Hetue5246692015-06-18 12:34:52 -04002051TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
2052{
2053 TPublicType publicType = publicTypeIn;
2054 TOperator op = EOpNull;
2055 if(publicType.userDef)
2056 {
2057 op = EOpConstructStruct;
2058 }
2059 else
2060 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002061 op = TypeToConstructorOperator(TType(publicType));
Alexis Hetue5246692015-06-18 12:34:52 -04002062 if(op == EOpNull)
2063 {
2064 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2065 recover();
2066 publicType.type = EbtFloat;
2067 op = EOpConstructFloat;
2068 }
2069 }
2070
2071 TString tempString;
2072 TType type(publicType);
2073 return new TFunction(&tempString, type, op);
2074}
2075
John Bauman66b8ab22014-05-06 15:57:45 -04002076// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002077// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04002078//
2079// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2080//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002081TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002082{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002083 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04002084
Nicolas Capens0bac2852016-05-07 06:09:58 -04002085 if(!aggregateArguments)
2086 {
2087 aggregateArguments = new TIntermAggregate;
2088 aggregateArguments->getSequence().push_back(arguments);
2089 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002090
Alexis Hetu2a198552016-09-27 20:50:45 -04002091 if(type->isArray())
2092 {
2093 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2094 // the array.
2095 for(TIntermNode *&argNode : aggregateArguments->getSequence())
2096 {
2097 const TType &argType = argNode->getAsTyped()->getType();
2098 // It has already been checked that the argument is not an array.
2099 ASSERT(!argType.isArray());
2100 if(!argType.sameElementType(*type))
2101 {
2102 error(line, "Array constructor argument has an incorrect type", "Error");
Alexis Hetuf0005a12016-09-28 15:52:21 -04002103 return nullptr;
Alexis Hetu2a198552016-09-27 20:50:45 -04002104 }
2105 }
2106 }
2107 else if(op == EOpConstructStruct)
Nicolas Capens0bac2852016-05-07 06:09:58 -04002108 {
2109 const TFieldList &fields = type->getStruct()->fields();
2110 TIntermSequence &args = aggregateArguments->getSequence();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002111
Nicolas Capens0bac2852016-05-07 06:09:58 -04002112 for(size_t i = 0; i < fields.size(); i++)
2113 {
2114 if(args[i]->getAsTyped()->getType() != *fields[i]->type())
2115 {
2116 error(line, "Structure constructor arguments do not match structure fields", "Error");
2117 recover();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002118
Alexis Hetuf0005a12016-09-28 15:52:21 -04002119 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002120 }
2121 }
2122 }
John Bauman66b8ab22014-05-06 15:57:45 -04002123
Nicolas Capens0bac2852016-05-07 06:09:58 -04002124 // Turn the argument list itself into a constructor
2125 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2126 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
2127 if(constConstructor)
2128 {
2129 return constConstructor;
2130 }
John Bauman66b8ab22014-05-06 15:57:45 -04002131
Nicolas Capens0bac2852016-05-07 06:09:58 -04002132 return constructor;
John Bauman66b8ab22014-05-06 15:57:45 -04002133}
2134
2135TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
2136{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002137 aggrNode->setType(type);
2138 if (aggrNode->isConstantFoldable()) {
2139 bool returnVal = false;
2140 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
2141 if (aggrNode->getSequence().size() == 1) {
2142 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
2143 }
2144 else {
2145 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
2146 }
2147 if (returnVal)
Alexis Hetuf0005a12016-09-28 15:52:21 -04002148 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002149
Nicolas Capens0bac2852016-05-07 06:09:58 -04002150 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2151 }
John Bauman66b8ab22014-05-06 15:57:45 -04002152
Alexis Hetuf0005a12016-09-28 15:52:21 -04002153 return nullptr;
John Bauman66b8ab22014-05-06 15:57:45 -04002154}
2155
John Bauman66b8ab22014-05-06 15:57:45 -04002156//
2157// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2158// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2159// 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 -04002160// 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 -04002161// a constant matrix.
2162//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002163TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002164{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002165 TIntermTyped* typedNode;
2166 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002167
Nicolas Capens0bac2852016-05-07 06:09:58 -04002168 ConstantUnion *unionArray;
2169 if (tempConstantNode) {
2170 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002171
Nicolas Capens0bac2852016-05-07 06:09:58 -04002172 if (!unionArray) {
2173 return node;
2174 }
2175 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
2176 error(line, "Cannot offset into the vector", "Error");
2177 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002178
Alexis Hetuf0005a12016-09-28 15:52:21 -04002179 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002180 }
John Bauman66b8ab22014-05-06 15:57:45 -04002181
Nicolas Capens0bac2852016-05-07 06:09:58 -04002182 ConstantUnion* constArray = new ConstantUnion[fields.num];
John Bauman66b8ab22014-05-06 15:57:45 -04002183
Alexis Hetub34591a2016-06-28 15:48:35 -04002184 int objSize = static_cast<int>(node->getType().getObjectSize());
Nicolas Capens0bac2852016-05-07 06:09:58 -04002185 for (int i = 0; i < fields.num; i++) {
Alexis Hetub34591a2016-06-28 15:48:35 -04002186 if (fields.offsets[i] >= objSize) {
Nicolas Capens0bac2852016-05-07 06:09:58 -04002187 std::stringstream extraInfoStream;
2188 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2189 std::string extraInfo = extraInfoStream.str();
2190 error(line, "", "[", extraInfo.c_str());
2191 recover();
2192 fields.offsets[i] = 0;
2193 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002194
Nicolas Capens0bac2852016-05-07 06:09:58 -04002195 constArray[i] = unionArray[fields.offsets[i]];
John Bauman66b8ab22014-05-06 15:57:45 -04002196
Nicolas Capens0bac2852016-05-07 06:09:58 -04002197 }
2198 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2199 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002200}
2201
2202//
2203// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002204// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2205// 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 -04002206// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2207//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002208TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002209{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002210 TIntermTyped* typedNode;
2211 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002212
Nicolas Capens0bac2852016-05-07 06:09:58 -04002213 if (index >= node->getType().getNominalSize()) {
2214 std::stringstream extraInfoStream;
2215 extraInfoStream << "matrix field selection out of range '" << index << "'";
2216 std::string extraInfo = extraInfoStream.str();
2217 error(line, "", "[", extraInfo.c_str());
2218 recover();
2219 index = 0;
2220 }
John Bauman66b8ab22014-05-06 15:57:45 -04002221
Nicolas Capens0bac2852016-05-07 06:09:58 -04002222 if (tempConstantNode) {
2223 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2224 int size = tempConstantNode->getType().getNominalSize();
2225 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
2226 } else {
2227 error(line, "Cannot offset into the matrix", "Error");
2228 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002229
Alexis Hetuf0005a12016-09-28 15:52:21 -04002230 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002231 }
John Bauman66b8ab22014-05-06 15:57:45 -04002232
Nicolas Capens0bac2852016-05-07 06:09:58 -04002233 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002234}
2235
2236
2237//
2238// 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 -04002239// the symbol table and parse-tree is built for the type of the element. The input
2240// 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 -04002241// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2242//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002243TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002244{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002245 TIntermTyped* typedNode;
2246 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
2247 TType arrayElementType = node->getType();
2248 arrayElementType.clearArrayness();
John Bauman66b8ab22014-05-06 15:57:45 -04002249
Nicolas Capens0bac2852016-05-07 06:09:58 -04002250 if (index >= node->getType().getArraySize()) {
2251 std::stringstream extraInfoStream;
2252 extraInfoStream << "array field selection out of range '" << index << "'";
2253 std::string extraInfo = extraInfoStream.str();
2254 error(line, "", "[", extraInfo.c_str());
2255 recover();
2256 index = 0;
2257 }
John Bauman66b8ab22014-05-06 15:57:45 -04002258
Nicolas Capens0bac2852016-05-07 06:09:58 -04002259 size_t arrayElementSize = arrayElementType.getObjectSize();
John Bauman66b8ab22014-05-06 15:57:45 -04002260
Nicolas Capens0bac2852016-05-07 06:09:58 -04002261 if (tempConstantNode) {
2262 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
2263 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
2264 } else {
2265 error(line, "Cannot offset into the array", "Error");
2266 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002267
Alexis Hetuf0005a12016-09-28 15:52:21 -04002268 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002269 }
John Bauman66b8ab22014-05-06 15:57:45 -04002270
Nicolas Capens0bac2852016-05-07 06:09:58 -04002271 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002272}
2273
2274
2275//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04002276// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04002277// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2278// function and returns the parse-tree with the values of the embedded/nested struct.
2279//
Alexis Hetufe1269e2015-06-16 12:43:32 -04002280TIntermTyped* TParseContext::addConstStruct(const TString& identifier, TIntermTyped* node, const TSourceLoc &line)
John Bauman66b8ab22014-05-06 15:57:45 -04002281{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002282 const TFieldList &fields = node->getType().getStruct()->fields();
2283 TIntermTyped *typedNode;
2284 size_t instanceSize = 0;
2285 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
John Bauman66b8ab22014-05-06 15:57:45 -04002286
Nicolas Capens0bac2852016-05-07 06:09:58 -04002287 for(size_t index = 0; index < fields.size(); ++index) {
2288 if (fields[index]->name() == identifier) {
2289 break;
2290 } else {
2291 instanceSize += fields[index]->type()->getObjectSize();
2292 }
2293 }
John Bauman66b8ab22014-05-06 15:57:45 -04002294
Nicolas Capens0bac2852016-05-07 06:09:58 -04002295 if (tempConstantNode) {
2296 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04002297
Nicolas Capens0bac2852016-05-07 06:09:58 -04002298 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
2299 } else {
2300 error(line, "Cannot offset into the structure", "Error");
2301 recover();
John Bauman66b8ab22014-05-06 15:57:45 -04002302
Alexis Hetuf0005a12016-09-28 15:52:21 -04002303 return nullptr;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002304 }
John Bauman66b8ab22014-05-06 15:57:45 -04002305
Nicolas Capens0bac2852016-05-07 06:09:58 -04002306 return typedNode;
John Bauman66b8ab22014-05-06 15:57:45 -04002307}
2308
Alexis Hetuad6b8752015-06-09 16:15:30 -04002309//
Alexis Hetua35d8232015-06-11 17:11:06 -04002310// Interface/uniform blocks
2311//
2312TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
Nicolas Capens0bac2852016-05-07 06:09:58 -04002313 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
Alexis Hetua35d8232015-06-11 17:11:06 -04002314{
2315 if(reservedErrorCheck(nameLine, blockName))
2316 recover();
2317
2318 if(typeQualifier.qualifier != EvqUniform)
2319 {
2320 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
2321 recover();
2322 }
2323
2324 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2325 if(layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
2326 {
2327 recover();
2328 }
2329
2330 if(blockLayoutQualifier.matrixPacking == EmpUnspecified)
2331 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002332 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Alexis Hetua35d8232015-06-11 17:11:06 -04002333 }
2334
2335 if(blockLayoutQualifier.blockStorage == EbsUnspecified)
2336 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002337 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Alexis Hetua35d8232015-06-11 17:11:06 -04002338 }
2339
2340 TSymbol* blockNameSymbol = new TSymbol(&blockName);
2341 if(!symbolTable.declare(*blockNameSymbol)) {
2342 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2343 recover();
2344 }
2345
2346 // check for sampler types and apply layout qualifiers
2347 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2348 TField* field = (*fieldList)[memberIndex];
2349 TType* fieldType = field->type();
2350 if(IsSampler(fieldType->getBasicType())) {
2351 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
2352 recover();
2353 }
2354
2355 const TQualifier qualifier = fieldType->getQualifier();
2356 switch(qualifier)
2357 {
2358 case EvqGlobal:
2359 case EvqUniform:
2360 break;
2361 default:
2362 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
2363 recover();
2364 break;
2365 }
2366
2367 // check layout qualifiers
2368 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2369 if(layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
2370 {
2371 recover();
2372 }
2373
2374 if(fieldLayoutQualifier.blockStorage != EbsUnspecified)
2375 {
2376 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
2377 recover();
2378 }
2379
2380 if(fieldLayoutQualifier.matrixPacking == EmpUnspecified)
2381 {
2382 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
2383 }
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002384 else if(!fieldType->isMatrix() && (fieldType->getBasicType() != EbtStruct))
Alexis Hetua35d8232015-06-11 17:11:06 -04002385 {
Alexis Hetu536ffcc2017-11-28 09:57:37 -05002386 warning(field->line(), "extraneous layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Alexis Hetua35d8232015-06-11 17:11:06 -04002387 }
2388
2389 fieldType->setLayoutQualifier(fieldLayoutQualifier);
2390 }
2391
2392 // add array index
2393 int arraySize = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -04002394 if(arrayIndex)
Alexis Hetua35d8232015-06-11 17:11:06 -04002395 {
2396 if(arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2397 recover();
2398 }
2399
2400 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2401 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
2402
2403 TString symbolName = "";
2404 int symbolId = 0;
2405
2406 if(!instanceName)
2407 {
2408 // define symbols for the members of the interface block
2409 for(size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2410 {
2411 TField* field = (*fieldList)[memberIndex];
2412 TType* fieldType = field->type();
2413
2414 // set parent pointer of the field variable
2415 fieldType->setInterfaceBlock(interfaceBlock);
2416
2417 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2418 fieldVariable->setQualifier(typeQualifier.qualifier);
2419
2420 if(!symbolTable.declare(*fieldVariable)) {
2421 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
2422 recover();
2423 }
2424 }
2425 }
2426 else
2427 {
2428 // add a symbol for this interface block
2429 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
2430 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2431
2432 if(!symbolTable.declare(*instanceTypeDef)) {
2433 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
2434 recover();
2435 }
2436
2437 symbolId = instanceTypeDef->getUniqueId();
2438 symbolName = instanceTypeDef->getName();
2439 }
2440
2441 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
2442 aggregate->setOp(EOpDeclaration);
2443
2444 exitStructDeclaration();
2445 return aggregate;
2446}
2447
2448//
Alexis Hetuad6b8752015-06-09 16:15:30 -04002449// Parse an array index expression
2450//
2451TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location, TIntermTyped *indexExpression)
2452{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002453 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002454
2455 if(!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2456 {
2457 if(baseExpression->getAsSymbolNode())
2458 {
2459 error(location, " left of '[' is not of type array, matrix, or vector ",
2460 baseExpression->getAsSymbolNode()->getSymbol().c_str());
2461 }
2462 else
2463 {
2464 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2465 }
2466 recover();
2467 }
2468
2469 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2470
2471 if(indexExpression->getQualifier() == EvqConstExpr && indexConstantUnion)
2472 {
2473 int index = indexConstantUnion->getIConst(0);
2474 if(index < 0)
2475 {
2476 std::stringstream infoStream;
2477 infoStream << index;
2478 std::string info = infoStream.str();
2479 error(location, "negative index", info.c_str());
2480 recover();
2481 index = 0;
2482 }
2483 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2484 {
2485 if(baseExpression->isArray())
2486 {
2487 // constant folding for arrays
2488 indexedExpression = addConstArrayNode(index, baseExpression, location);
2489 }
2490 else if(baseExpression->isVector())
2491 {
2492 // constant folding for vectors
2493 TVectorFields fields;
2494 fields.num = 1;
2495 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2496 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2497 }
2498 else if(baseExpression->isMatrix())
2499 {
2500 // constant folding for matrices
2501 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2502 }
2503 }
2504 else
2505 {
2506 int safeIndex = -1;
2507
2508 if(baseExpression->isArray())
2509 {
2510 if(index >= baseExpression->getType().getArraySize())
2511 {
2512 std::stringstream extraInfoStream;
2513 extraInfoStream << "array index out of range '" << index << "'";
2514 std::string extraInfo = extraInfoStream.str();
2515 error(location, "", "[", extraInfo.c_str());
2516 recover();
2517 safeIndex = baseExpression->getType().getArraySize() - 1;
2518 }
2519 }
2520 else if((baseExpression->isVector() || baseExpression->isMatrix()) &&
2521 baseExpression->getType().getNominalSize() <= index)
2522 {
2523 std::stringstream extraInfoStream;
2524 extraInfoStream << "field selection out of range '" << index << "'";
2525 std::string extraInfo = extraInfoStream.str();
2526 error(location, "", "[", extraInfo.c_str());
2527 recover();
2528 safeIndex = baseExpression->getType().getNominalSize() - 1;
2529 }
2530
2531 // Don't modify the data of the previous constant union, because it can point
2532 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2533 if(safeIndex != -1)
2534 {
2535 ConstantUnion *safeConstantUnion = new ConstantUnion();
2536 safeConstantUnion->setIConst(safeIndex);
2537 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2538 }
2539
2540 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2541 }
2542 }
2543 else
2544 {
2545 if(baseExpression->isInterfaceBlock())
2546 {
2547 error(location, "",
2548 "[", "array indexes for interface blocks arrays must be constant integral expressions");
2549 recover();
2550 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002551 else if(baseExpression->getQualifier() == EvqFragmentOut)
2552 {
2553 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2554 recover();
2555 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002556
2557 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2558 }
2559
2560 if(indexedExpression == 0)
2561 {
2562 ConstantUnion *unionArray = new ConstantUnion[1];
2563 unionArray->setFConst(0.0f);
2564 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConstExpr), location);
2565 }
2566 else if(baseExpression->isArray())
2567 {
2568 const TType &baseType = baseExpression->getType();
2569 if(baseType.getStruct())
2570 {
2571 TType copyOfType(baseType.getStruct());
2572 indexedExpression->setType(copyOfType);
2573 }
2574 else if(baseType.isInterfaceBlock())
2575 {
Alexis Hetu6c7ac3c2016-01-12 16:13:37 -05002576 TType copyOfType(baseType.getInterfaceBlock(), EvqTemporary, baseType.getLayoutQualifier(), 0);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002577 indexedExpression->setType(copyOfType);
2578 }
2579 else
2580 {
2581 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2582 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2583 static_cast<unsigned char>(baseExpression->getSecondarySize())));
2584 }
2585
2586 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2587 {
2588 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2589 }
2590 }
2591 else if(baseExpression->isMatrix())
2592 {
2593 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2594 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2595 qualifier, static_cast<unsigned char>(baseExpression->getSecondarySize())));
2596 }
2597 else if(baseExpression->isVector())
2598 {
2599 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary;
2600 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2601 }
2602 else
2603 {
2604 indexedExpression->setType(baseExpression->getType());
2605 }
2606
2607 return indexedExpression;
2608}
2609
2610TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2611 const TString &fieldString, const TSourceLoc &fieldLocation)
2612{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002613 TIntermTyped *indexedExpression = nullptr;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002614
2615 if(baseExpression->isArray())
2616 {
2617 error(fieldLocation, "cannot apply dot operator to an array", ".");
2618 recover();
2619 }
2620
2621 if(baseExpression->isVector())
2622 {
2623 TVectorFields fields;
2624 if(!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2625 {
2626 fields.num = 1;
2627 fields.offsets[0] = 0;
2628 recover();
2629 }
2630
Nicolas Capens0863f0d2016-04-10 00:30:02 -04002631 if(baseExpression->getAsConstantUnion())
Alexis Hetuad6b8752015-06-09 16:15:30 -04002632 {
2633 // constant folding for vector fields
2634 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2635 if(indexedExpression == 0)
2636 {
2637 recover();
2638 indexedExpression = baseExpression;
2639 }
2640 else
2641 {
2642 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2643 EvqConstExpr, (unsigned char)(fieldString).size()));
2644 }
2645 }
2646 else
2647 {
2648 TString vectorString = fieldString;
2649 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
2650 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2651 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
Nicolas Capens341afbb2016-04-10 01:54:50 -04002652 baseExpression->getQualifier() == EvqConstExpr ? EvqConstExpr : EvqTemporary, (unsigned char)vectorString.size()));
Alexis Hetuad6b8752015-06-09 16:15:30 -04002653 }
2654 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002655 else if(baseExpression->getBasicType() == EbtStruct)
2656 {
2657 bool fieldFound = false;
2658 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
2659 if(fields.empty())
2660 {
2661 error(dotLocation, "structure has no fields", "Internal Error");
2662 recover();
2663 indexedExpression = baseExpression;
2664 }
2665 else
2666 {
2667 unsigned int i;
2668 for(i = 0; i < fields.size(); ++i)
2669 {
2670 if(fields[i]->name() == fieldString)
2671 {
2672 fieldFound = true;
2673 break;
2674 }
2675 }
2676 if(fieldFound)
2677 {
2678 if(baseExpression->getType().getQualifier() == EvqConstExpr)
2679 {
2680 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2681 if(indexedExpression == 0)
2682 {
2683 recover();
2684 indexedExpression = baseExpression;
2685 }
2686 else
2687 {
2688 indexedExpression->setType(*fields[i]->type());
2689 // change the qualifier of the return type, not of the structure field
2690 // as the structure definition is shared between various structures.
2691 indexedExpression->getTypePointer()->setQualifier(EvqConstExpr);
2692 }
2693 }
2694 else
2695 {
Alexis Hetuec93b1d2016-12-09 16:01:29 -05002696 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2697 index->setLine(fieldLocation);
Alexis Hetuad6b8752015-06-09 16:15:30 -04002698 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2699 indexedExpression->setType(*fields[i]->type());
2700 }
2701 }
2702 else
2703 {
2704 error(dotLocation, " no such field in structure", fieldString.c_str());
2705 recover();
2706 indexedExpression = baseExpression;
2707 }
2708 }
2709 }
2710 else if(baseExpression->isInterfaceBlock())
2711 {
2712 bool fieldFound = false;
2713 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
2714 if(fields.empty())
2715 {
2716 error(dotLocation, "interface block has no fields", "Internal Error");
2717 recover();
2718 indexedExpression = baseExpression;
2719 }
2720 else
2721 {
2722 unsigned int i;
2723 for(i = 0; i < fields.size(); ++i)
2724 {
2725 if(fields[i]->name() == fieldString)
2726 {
2727 fieldFound = true;
2728 break;
2729 }
2730 }
2731 if(fieldFound)
2732 {
2733 ConstantUnion *unionArray = new ConstantUnion[1];
2734 unionArray->setIConst(i);
2735 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2736 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2737 dotLocation);
2738 indexedExpression->setType(*fields[i]->type());
2739 }
2740 else
2741 {
2742 error(dotLocation, " no such field in interface block", fieldString.c_str());
2743 recover();
2744 indexedExpression = baseExpression;
2745 }
2746 }
2747 }
2748 else
2749 {
Alexis Hetu0a655842015-06-22 16:52:11 -04002750 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04002751 {
Nicolas Capensc09073f2017-11-09 09:49:03 -05002752 error(dotLocation, " field selection requires structure or vector on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002753 fieldString.c_str());
2754 }
2755 else
2756 {
2757 error(dotLocation,
Nicolas Capensc09073f2017-11-09 09:49:03 -05002758 " field selection requires structure, vector, or interface block on left hand side",
Alexis Hetuad6b8752015-06-09 16:15:30 -04002759 fieldString.c_str());
2760 }
2761 recover();
2762 indexedExpression = baseExpression;
2763 }
2764
2765 return indexedExpression;
2766}
2767
Nicolas Capens7d626792015-02-17 17:58:31 -05002768TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2769{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002770 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002771
Nicolas Capens0bac2852016-05-07 06:09:58 -04002772 qualifier.location = -1;
Alexis Hetuad6b8752015-06-09 16:15:30 -04002773 qualifier.matrixPacking = EmpUnspecified;
2774 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002775
Alexis Hetuad6b8752015-06-09 16:15:30 -04002776 if(qualifierType == "shared")
2777 {
2778 qualifier.blockStorage = EbsShared;
2779 }
2780 else if(qualifierType == "packed")
2781 {
2782 qualifier.blockStorage = EbsPacked;
2783 }
2784 else if(qualifierType == "std140")
2785 {
2786 qualifier.blockStorage = EbsStd140;
2787 }
2788 else if(qualifierType == "row_major")
2789 {
2790 qualifier.matrixPacking = EmpRowMajor;
2791 }
2792 else if(qualifierType == "column_major")
2793 {
2794 qualifier.matrixPacking = EmpColumnMajor;
2795 }
2796 else if(qualifierType == "location")
Nicolas Capens0bac2852016-05-07 06:09:58 -04002797 {
2798 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2799 recover();
2800 }
2801 else
2802 {
2803 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2804 recover();
2805 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002806
Nicolas Capens0bac2852016-05-07 06:09:58 -04002807 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002808}
2809
2810TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2811{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002812 TLayoutQualifier qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002813
Nicolas Capens0bac2852016-05-07 06:09:58 -04002814 qualifier.location = -1;
2815 qualifier.matrixPacking = EmpUnspecified;
2816 qualifier.blockStorage = EbsUnspecified;
Nicolas Capens7d626792015-02-17 17:58:31 -05002817
Nicolas Capens0bac2852016-05-07 06:09:58 -04002818 if (qualifierType != "location")
2819 {
2820 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2821 recover();
2822 }
2823 else
2824 {
2825 // must check that location is non-negative
2826 if (intValue < 0)
2827 {
2828 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2829 recover();
2830 }
2831 else
2832 {
2833 qualifier.location = intValue;
2834 }
2835 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002836
Nicolas Capens0bac2852016-05-07 06:09:58 -04002837 return qualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002838}
2839
2840TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2841{
Nicolas Capens0bac2852016-05-07 06:09:58 -04002842 TLayoutQualifier joinedQualifier = leftQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002843
Nicolas Capens0bac2852016-05-07 06:09:58 -04002844 if (rightQualifier.location != -1)
2845 {
2846 joinedQualifier.location = rightQualifier.location;
2847 }
Alexis Hetuad6b8752015-06-09 16:15:30 -04002848 if(rightQualifier.matrixPacking != EmpUnspecified)
2849 {
2850 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2851 }
2852 if(rightQualifier.blockStorage != EbsUnspecified)
2853 {
2854 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2855 }
Nicolas Capens7d626792015-02-17 17:58:31 -05002856
Nicolas Capens0bac2852016-05-07 06:09:58 -04002857 return joinedQualifier;
Nicolas Capens7d626792015-02-17 17:58:31 -05002858}
2859
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002860
2861TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2862 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2863{
2864 TQualifier mergedQualifier = EvqSmoothIn;
2865
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002866 if(storageQualifier == EvqFragmentIn) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002867 if(interpolationQualifier == EvqSmooth)
2868 mergedQualifier = EvqSmoothIn;
2869 else if(interpolationQualifier == EvqFlat)
2870 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002871 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002872 }
2873 else if(storageQualifier == EvqCentroidIn) {
2874 if(interpolationQualifier == EvqSmooth)
2875 mergedQualifier = EvqCentroidIn;
2876 else if(interpolationQualifier == EvqFlat)
2877 mergedQualifier = EvqFlatIn;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002878 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002879 }
Alexis Hetu42ff6b12015-06-03 16:03:48 -04002880 else if(storageQualifier == EvqVertexOut) {
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002881 if(interpolationQualifier == EvqSmooth)
2882 mergedQualifier = EvqSmoothOut;
2883 else if(interpolationQualifier == EvqFlat)
2884 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002885 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002886 }
2887 else if(storageQualifier == EvqCentroidOut) {
2888 if(interpolationQualifier == EvqSmooth)
2889 mergedQualifier = EvqCentroidOut;
2890 else if(interpolationQualifier == EvqFlat)
2891 mergedQualifier = EvqFlatOut;
Nicolas Capens3713cd42015-06-22 10:41:54 -04002892 else UNREACHABLE(interpolationQualifier);
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04002893 }
2894 else {
2895 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
2896 recover();
2897
2898 mergedQualifier = storageQualifier;
2899 }
2900
2901 TPublicType type;
2902 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2903 return type;
2904}
2905
Alexis Hetuad6b8752015-06-09 16:15:30 -04002906TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
2907{
Alexis Hetudd7ff7a2015-06-11 08:25:30 -04002908 if(voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
Alexis Hetuad6b8752015-06-09 16:15:30 -04002909 {
2910 recover();
2911 }
2912
2913 for(unsigned int i = 0; i < fieldList->size(); ++i)
2914 {
2915 //
2916 // Careful not to replace already known aspects of type, like array-ness
2917 //
2918 TType *type = (*fieldList)[i]->type();
2919 type->setBasicType(typeSpecifier.type);
2920 type->setNominalSize(typeSpecifier.primarySize);
2921 type->setSecondarySize(typeSpecifier.secondarySize);
2922 type->setPrecision(typeSpecifier.precision);
2923 type->setQualifier(typeSpecifier.qualifier);
2924 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2925
2926 // don't allow arrays of arrays
2927 if(type->isArray())
2928 {
2929 if(arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2930 recover();
2931 }
2932 if(typeSpecifier.array)
2933 type->setArraySize(typeSpecifier.arraySize);
2934 if(typeSpecifier.userDef)
2935 {
2936 type->setStruct(typeSpecifier.userDef->getStruct());
2937 }
2938
2939 if(structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2940 {
2941 recover();
2942 }
2943 }
2944
2945 return fieldList;
2946}
2947
2948TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2949 const TString *structName, TFieldList *fieldList)
2950{
2951 TStructure *structure = new TStructure(structName, fieldList);
2952 TType *structureType = new TType(structure);
2953
2954 // Store a bool in the struct if we're at global scope, to allow us to
2955 // skip the local struct scoping workaround in HLSL.
2956 structure->setUniqueId(TSymbolTableLevel::nextUniqueId());
2957 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
2958
2959 if(!structName->empty())
2960 {
2961 if(reservedErrorCheck(nameLine, *structName))
2962 {
2963 recover();
2964 }
2965 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2966 if(!symbolTable.declare(*userTypeDef))
2967 {
2968 error(nameLine, "redefinition", structName->c_str(), "struct");
2969 recover();
2970 }
2971 }
2972
2973 // ensure we do not specify any storage qualifiers on the struct members
2974 for(unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2975 {
2976 const TField &field = *(*fieldList)[typeListIndex];
2977 const TQualifier qualifier = field.type()->getQualifier();
2978 switch(qualifier)
2979 {
2980 case EvqGlobal:
2981 case EvqTemporary:
2982 break;
2983 default:
2984 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2985 recover();
2986 break;
2987 }
2988 }
2989
2990 TPublicType publicType;
2991 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2992 publicType.userDef = structureType;
2993 exitStructDeclaration();
2994
2995 return publicType;
2996}
2997
Alexis Hetufe1269e2015-06-16 12:43:32 -04002998bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString& identifier)
John Bauman66b8ab22014-05-06 15:57:45 -04002999{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003000 ++mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003001
Nicolas Capens0bac2852016-05-07 06:09:58 -04003002 // Embedded structure definitions are not supported per GLSL ES spec.
3003 // They aren't allowed in GLSL either, but we need to detect this here
3004 // so we don't rely on the GLSL compiler to catch it.
3005 if (mStructNestingLevel > 1) {
3006 error(line, "", "Embedded struct definitions are not allowed");
3007 return true;
3008 }
John Bauman66b8ab22014-05-06 15:57:45 -04003009
Nicolas Capens0bac2852016-05-07 06:09:58 -04003010 return false;
John Bauman66b8ab22014-05-06 15:57:45 -04003011}
3012
3013void TParseContext::exitStructDeclaration()
3014{
Nicolas Capens0bac2852016-05-07 06:09:58 -04003015 --mStructNestingLevel;
John Bauman66b8ab22014-05-06 15:57:45 -04003016}
3017
Alexis Hetuad6b8752015-06-09 16:15:30 -04003018bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
3019{
3020 static const int kWebGLMaxStructNesting = 4;
3021
3022 if(field.type()->getBasicType() != EbtStruct)
3023 {
3024 return false;
3025 }
3026
3027 // We're already inside a structure definition at this point, so add
3028 // one to the field's struct nesting.
3029 if(1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3030 {
3031 std::stringstream reasonStream;
3032 reasonStream << "Reference of struct type "
3033 << field.type()->getStruct()->name().c_str()
3034 << " exceeds maximum allowed nesting level of "
3035 << kWebGLMaxStructNesting;
3036 std::string reason = reasonStream.str();
3037 error(line, reason.c_str(), field.name().c_str(), "");
3038 return true;
3039 }
3040
3041 return false;
3042}
3043
3044TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc, const TType *funcReturnType)
3045{
3046 if(child == nullptr)
3047 {
3048 return nullptr;
3049 }
3050
3051 switch(op)
3052 {
3053 case EOpLogicalNot:
3054 if(child->getBasicType() != EbtBool ||
3055 child->isMatrix() ||
3056 child->isArray() ||
3057 child->isVector())
3058 {
3059 return nullptr;
3060 }
3061 break;
3062 case EOpBitwiseNot:
3063 if((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3064 child->isMatrix() ||
3065 child->isArray())
3066 {
3067 return nullptr;
3068 }
3069 break;
3070 case EOpPostIncrement:
3071 case EOpPreIncrement:
3072 case EOpPostDecrement:
3073 case EOpPreDecrement:
3074 case EOpNegative:
3075 if(child->getBasicType() == EbtStruct ||
3076 child->getBasicType() == EbtBool ||
3077 child->isArray())
3078 {
3079 return nullptr;
3080 }
3081 // Operators for built-ins are already type checked against their prototype.
3082 default:
3083 break;
3084 }
3085
Nicolas Capensd3d9b9c2016-04-10 01:53:59 -04003086 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Alexis Hetuad6b8752015-06-09 16:15:30 -04003087}
3088
3089TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3090{
3091 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
3092 if(node == nullptr)
3093 {
3094 unaryOpError(loc, getOperatorString(op), child->getCompleteString());
3095 recover();
3096 return child;
3097 }
3098 return node;
3099}
3100
3101TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3102{
3103 if(lValueErrorCheck(loc, getOperatorString(op), child))
3104 recover();
3105 return addUnaryMath(op, child, loc);
3106}
3107
3108bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3109{
3110 if(left->isArray() || right->isArray())
3111 {
Alexis Hetu0a655842015-06-22 16:52:11 -04003112 if(mShaderVersion < 300)
Alexis Hetuad6b8752015-06-09 16:15:30 -04003113 {
3114 error(loc, "Invalid operation for arrays", getOperatorString(op));
3115 return false;
3116 }
3117
3118 if(left->isArray() != right->isArray())
3119 {
3120 error(loc, "array / non-array mismatch", getOperatorString(op));
3121 return false;
3122 }
3123
3124 switch(op)
3125 {
3126 case EOpEqual:
3127 case EOpNotEqual:
3128 case EOpAssign:
3129 case EOpInitialize:
3130 break;
3131 default:
3132 error(loc, "Invalid operation for arrays", getOperatorString(op));
3133 return false;
3134 }
3135 // At this point, size of implicitly sized arrays should be resolved.
3136 if(left->getArraySize() != right->getArraySize())
3137 {
3138 error(loc, "array size mismatch", getOperatorString(op));
3139 return false;
3140 }
3141 }
3142
3143 // Check ops which require integer / ivec parameters
3144 bool isBitShift = false;
3145 switch(op)
3146 {
3147 case EOpBitShiftLeft:
3148 case EOpBitShiftRight:
3149 case EOpBitShiftLeftAssign:
3150 case EOpBitShiftRightAssign:
3151 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3152 // check that the basic type is an integer type.
3153 isBitShift = true;
3154 if(!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3155 {
3156 return false;
3157 }
3158 break;
3159 case EOpBitwiseAnd:
3160 case EOpBitwiseXor:
3161 case EOpBitwiseOr:
3162 case EOpBitwiseAndAssign:
3163 case EOpBitwiseXorAssign:
3164 case EOpBitwiseOrAssign:
3165 // It is enough to check the type of only one operand, since later it
3166 // is checked that the operand types match.
3167 if(!IsInteger(left->getBasicType()))
3168 {
3169 return false;
3170 }
3171 break;
3172 default:
3173 break;
3174 }
3175
3176 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3177 // So the basic type should usually match.
3178 if(!isBitShift && left->getBasicType() != right->getBasicType())
3179 {
3180 return false;
3181 }
3182
3183 // Check that type sizes match exactly on ops that require that.
3184 // Also check restrictions for structs that contain arrays or samplers.
3185 switch(op)
3186 {
3187 case EOpAssign:
3188 case EOpInitialize:
3189 case EOpEqual:
3190 case EOpNotEqual:
3191 // ESSL 1.00 sections 5.7, 5.8, 5.9
Alexis Hetu0a655842015-06-22 16:52:11 -04003192 if(mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Alexis Hetuad6b8752015-06-09 16:15:30 -04003193 {
3194 error(loc, "undefined operation for structs containing arrays", getOperatorString(op));
3195 return false;
3196 }
3197 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3198 // we interpret the spec so that this extends to structs containing samplers,
3199 // similarly to ESSL 1.00 spec.
Alexis Hetu0a655842015-06-22 16:52:11 -04003200 if((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Alexis Hetuad6b8752015-06-09 16:15:30 -04003201 left->getType().isStructureContainingSamplers())
3202 {
3203 error(loc, "undefined operation for structs containing samplers", getOperatorString(op));
3204 return false;
3205 }
3206 case EOpLessThan:
3207 case EOpGreaterThan:
3208 case EOpLessThanEqual:
3209 case EOpGreaterThanEqual:
3210 if((left->getNominalSize() != right->getNominalSize()) ||
3211 (left->getSecondarySize() != right->getSecondarySize()))
3212 {
3213 return false;
3214 }
Alexis Hetuec93b1d2016-12-09 16:01:29 -05003215 break;
3216 case EOpAdd:
3217 case EOpSub:
3218 case EOpDiv:
3219 case EOpIMod:
3220 case EOpBitShiftLeft:
3221 case EOpBitShiftRight:
3222 case EOpBitwiseAnd:
3223 case EOpBitwiseXor:
3224 case EOpBitwiseOr:
3225 case EOpAddAssign:
3226 case EOpSubAssign:
3227 case EOpDivAssign:
3228 case EOpIModAssign:
3229 case EOpBitShiftLeftAssign:
3230 case EOpBitShiftRightAssign:
3231 case EOpBitwiseAndAssign:
3232 case EOpBitwiseXorAssign:
3233 case EOpBitwiseOrAssign:
3234 if((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3235 {
3236 return false;
3237 }
3238
3239 // Are the sizes compatible?
3240 if(left->getNominalSize() != right->getNominalSize() || left->getSecondarySize() != right->getSecondarySize())
3241 {
3242 // If the nominal sizes of operands do not match:
3243 // One of them must be a scalar.
3244 if(!left->isScalar() && !right->isScalar())
3245 return false;
3246
3247 // In the case of compound assignment other than multiply-assign,
3248 // the right side needs to be a scalar. Otherwise a vector/matrix
3249 // would be assigned to a scalar. A scalar can't be shifted by a
3250 // vector either.
3251 if(!right->isScalar() && (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3252 return false;
3253 }
3254 break;
Alexis Hetuad6b8752015-06-09 16:15:30 -04003255 default:
3256 break;
3257 }
3258
3259 return true;
3260}
3261
Alexis Hetu76a343a2015-06-04 17:21:22 -04003262TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3263{
3264 TBasicType switchType = init->getBasicType();
3265 if((switchType != EbtInt && switchType != EbtUInt) ||
3266 init->isMatrix() ||
3267 init->isArray() ||
3268 init->isVector())
3269 {
3270 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3271 recover();
3272 return nullptr;
3273 }
3274
3275 if(statementList)
3276 {
3277 if(!ValidateSwitch::validate(switchType, this, statementList, loc))
3278 {
3279 recover();
3280 return nullptr;
3281 }
3282 }
3283
3284 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3285 if(node == nullptr)
3286 {
3287 error(loc, "erroneous switch statement", "switch");
3288 recover();
3289 return nullptr;
3290 }
3291 return node;
3292}
3293
3294TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3295{
Alexis Hetu0a655842015-06-22 16:52:11 -04003296 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003297 {
3298 error(loc, "case labels need to be inside switch statements", "case");
3299 recover();
3300 return nullptr;
3301 }
3302 if(condition == nullptr)
3303 {
3304 error(loc, "case label must have a condition", "case");
3305 recover();
3306 return nullptr;
3307 }
3308 if((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3309 condition->isMatrix() ||
3310 condition->isArray() ||
3311 condition->isVector())
3312 {
3313 error(condition->getLine(), "case label must be a scalar integer", "case");
3314 recover();
3315 }
3316 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3317 if(conditionConst == nullptr)
3318 {
3319 error(condition->getLine(), "case label must be constant", "case");
3320 recover();
3321 }
3322 TIntermCase *node = intermediate.addCase(condition, loc);
3323 if(node == nullptr)
3324 {
3325 error(loc, "erroneous case statement", "case");
3326 recover();
3327 return nullptr;
3328 }
3329 return node;
3330}
3331
3332TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3333{
Alexis Hetu0a655842015-06-22 16:52:11 -04003334 if(mSwitchNestingLevel == 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003335 {
3336 error(loc, "default labels need to be inside switch statements", "default");
3337 recover();
3338 return nullptr;
3339 }
3340 TIntermCase *node = intermediate.addCase(nullptr, loc);
3341 if(node == nullptr)
3342 {
3343 error(loc, "erroneous default statement", "default");
3344 recover();
3345 return nullptr;
3346 }
3347 return node;
3348}
Alexis Hetue5246692015-06-18 12:34:52 -04003349TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3350{
3351 if(binaryOpCommonCheck(op, left, right, loc))
3352 {
3353 return intermediate.addAssign(op, left, right, loc);
3354 }
3355 return nullptr;
3356}
3357
3358TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3359{
3360 TIntermTyped *node = createAssign(op, left, right, loc);
3361 if(node == nullptr)
3362 {
3363 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3364 recover();
3365 return left;
3366 }
3367 return node;
3368}
Alexis Hetu76a343a2015-06-04 17:21:22 -04003369
Alexis Hetub4769582015-06-16 12:19:50 -04003370TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3371 const TSourceLoc &loc)
3372{
3373 if(!binaryOpCommonCheck(op, left, right, loc))
3374 return nullptr;
3375
3376 switch(op)
3377 {
3378 case EOpEqual:
3379 case EOpNotEqual:
3380 break;
3381 case EOpLessThan:
3382 case EOpGreaterThan:
3383 case EOpLessThanEqual:
3384 case EOpGreaterThanEqual:
3385 ASSERT(!left->isArray() && !right->isArray());
3386 if(left->isMatrix() || left->isVector() ||
3387 left->getBasicType() == EbtStruct)
3388 {
3389 return nullptr;
3390 }
3391 break;
3392 case EOpLogicalOr:
3393 case EOpLogicalXor:
3394 case EOpLogicalAnd:
3395 ASSERT(!left->isArray() && !right->isArray());
3396 if(left->getBasicType() != EbtBool ||
3397 left->isMatrix() || left->isVector())
3398 {
3399 return nullptr;
3400 }
3401 break;
3402 case EOpAdd:
3403 case EOpSub:
3404 case EOpDiv:
3405 case EOpMul:
3406 ASSERT(!left->isArray() && !right->isArray());
3407 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3408 {
3409 return nullptr;
3410 }
3411 break;
3412 case EOpIMod:
3413 ASSERT(!left->isArray() && !right->isArray());
3414 // Note that this is only for the % operator, not for mod()
3415 if(left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3416 {
3417 return nullptr;
3418 }
3419 break;
3420 // Note that for bitwise ops, type checking is done in promote() to
3421 // share code between ops and compound assignment
3422 default:
3423 break;
3424 }
3425
3426 return intermediate.addBinaryMath(op, left, right, loc);
3427}
3428
3429TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3430{
3431 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3432 if(node == 0)
3433 {
3434 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3435 recover();
3436 return left;
3437 }
3438 return node;
3439}
3440
3441TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right, const TSourceLoc &loc)
3442{
3443 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
3444 if(node == 0)
3445 {
3446 binaryOpError(loc, getOperatorString(op), left->getCompleteString(), right->getCompleteString());
3447 recover();
3448 ConstantUnion *unionArray = new ConstantUnion[1];
3449 unionArray->setBConst(false);
3450 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), loc);
3451 }
3452 return node;
3453}
3454
Alexis Hetu76a343a2015-06-04 17:21:22 -04003455TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3456{
3457 switch(op)
3458 {
3459 case EOpContinue:
Alexis Hetu0a655842015-06-22 16:52:11 -04003460 if(mLoopNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003461 {
3462 error(loc, "continue statement only allowed in loops", "");
3463 recover();
3464 }
3465 break;
3466 case EOpBreak:
Alexis Hetu0a655842015-06-22 16:52:11 -04003467 if(mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003468 {
3469 error(loc, "break statement only allowed in loops and switch statements", "");
3470 recover();
3471 }
3472 break;
3473 case EOpReturn:
Alexis Hetu0a655842015-06-22 16:52:11 -04003474 if(mCurrentFunctionType->getBasicType() != EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003475 {
3476 error(loc, "non-void function must return a value", "return");
3477 recover();
3478 }
3479 break;
3480 default:
3481 // No checks for discard
3482 break;
3483 }
3484 return intermediate.addBranch(op, loc);
3485}
3486
3487TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3488{
3489 ASSERT(op == EOpReturn);
Alexis Hetu0a655842015-06-22 16:52:11 -04003490 mFunctionReturnsValue = true;
3491 if(mCurrentFunctionType->getBasicType() == EbtVoid)
Alexis Hetu76a343a2015-06-04 17:21:22 -04003492 {
3493 error(loc, "void function cannot return a value", "return");
3494 recover();
3495 }
Alexis Hetu0a655842015-06-22 16:52:11 -04003496 else if(*mCurrentFunctionType != returnValue->getType())
Alexis Hetu76a343a2015-06-04 17:21:22 -04003497 {
3498 error(loc, "function return is not matching type:", "return");
3499 recover();
3500 }
3501 return intermediate.addBranch(op, returnValue, loc);
3502}
3503
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003504TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode, const TSourceLoc &loc, bool *fatalError)
3505{
3506 *fatalError = false;
3507 TOperator op = fnCall->getBuiltInOp();
3508 TIntermTyped *callNode = nullptr;
3509
3510 if(thisNode != nullptr)
3511 {
3512 ConstantUnion *unionArray = new ConstantUnion[1];
3513 int arraySize = 0;
3514 TIntermTyped *typedThis = thisNode->getAsTyped();
3515 if(fnCall->getName() != "length")
3516 {
3517 error(loc, "invalid method", fnCall->getName().c_str());
3518 recover();
3519 }
3520 else if(paramNode != nullptr)
3521 {
3522 error(loc, "method takes no parameters", "length");
3523 recover();
3524 }
3525 else if(typedThis == nullptr || !typedThis->isArray())
3526 {
3527 error(loc, "length can only be called on arrays", "length");
3528 recover();
3529 }
3530 else
3531 {
3532 arraySize = typedThis->getArraySize();
3533 if(typedThis->getAsSymbolNode() == nullptr)
3534 {
3535 // This code path can be hit with expressions like these:
3536 // (a = b).length()
3537 // (func()).length()
3538 // (int[3](0, 1, 2)).length()
3539 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3540 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3541 // which allows "An array, vector or matrix expression with the length method applied".
3542 error(loc, "length can only be called on array names, not on array expressions", "length");
3543 recover();
3544 }
3545 }
3546 unionArray->setIConst(arraySize);
3547 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), loc);
3548 }
3549 else if(op != EOpNull)
3550 {
3551 //
3552 // Then this should be a constructor.
3553 // Don't go through the symbol table for constructors.
3554 // Their parameters will be verified algorithmically.
3555 //
3556 TType type(EbtVoid, EbpUndefined); // use this to get the type back
3557 if(!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
3558 {
3559 //
3560 // It's a constructor, of type 'type'.
3561 //
3562 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
3563 }
3564
3565 if(callNode == nullptr)
3566 {
3567 recover();
3568 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3569 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003570 }
3571 else
3572 {
3573 //
3574 // Not a constructor. Find it in the symbol table.
3575 //
3576 const TFunction *fnCandidate;
3577 bool builtIn;
3578 fnCandidate = findFunction(loc, fnCall, &builtIn);
3579 if(fnCandidate)
3580 {
3581 //
3582 // A declared function.
3583 //
3584 if(builtIn && !fnCandidate->getExtension().empty() &&
3585 extensionErrorCheck(loc, fnCandidate->getExtension()))
3586 {
3587 recover();
3588 }
3589 op = fnCandidate->getBuiltInOp();
3590 if(builtIn && op != EOpNull)
3591 {
3592 //
3593 // A function call mapped to a built-in operation.
3594 //
3595 if(fnCandidate->getParamCount() == 1)
3596 {
3597 //
3598 // Treat it like a built-in unary operator.
3599 //
3600 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
3601 if(callNode == nullptr)
3602 {
3603 std::stringstream extraInfoStream;
3604 extraInfoStream << "built in unary operator function. Type: "
3605 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
3606 std::string extraInfo = extraInfoStream.str();
3607 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
3608 *fatalError = true;
3609 return nullptr;
3610 }
3611 }
3612 else
3613 {
3614 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
3615 aggregate->setType(fnCandidate->getReturnType());
3616
3617 // Some built-in functions have out parameters too.
3618 functionCallLValueErrorCheck(fnCandidate, aggregate);
3619
3620 callNode = aggregate;
Nicolas Capens91dfb972016-04-09 23:45:12 -04003621
3622 if(fnCandidate->getParamCount() == 2)
3623 {
3624 TIntermSequence &parameters = paramNode->getAsAggregate()->getSequence();
3625 TIntermTyped *left = parameters[0]->getAsTyped();
3626 TIntermTyped *right = parameters[1]->getAsTyped();
3627
3628 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
3629 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
3630 if (leftTempConstant && rightTempConstant)
3631 {
3632 TIntermTyped *typedReturnNode = leftTempConstant->fold(op, rightTempConstant, infoSink());
3633
3634 if(typedReturnNode)
3635 {
3636 callNode = typedReturnNode;
3637 }
3638 }
3639 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003640 }
3641 }
3642 else
3643 {
3644 // This is a real function call
3645
3646 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
3647 aggregate->setType(fnCandidate->getReturnType());
3648
3649 // this is how we know whether the given function is a builtIn function or a user defined function
3650 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3651 // if builtIn == true, it's definitely a builtIn function with EOpNull
3652 if(!builtIn)
3653 aggregate->setUserDefined();
3654 aggregate->setName(fnCandidate->getMangledName());
3655
3656 callNode = aggregate;
3657
3658 functionCallLValueErrorCheck(fnCandidate, aggregate);
3659 }
Alexis Hetub3ff42c2015-07-03 18:19:57 -04003660 }
3661 else
3662 {
3663 // error message was put out by findFunction()
3664 // Put on a dummy node for error recovery
3665 ConstantUnion *unionArray = new ConstantUnion[1];
3666 unionArray->setFConst(0.0f);
3667 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), loc);
3668 recover();
3669 }
3670 }
3671 delete fnCall;
3672 return callNode;
3673}
3674
Alexis Hetueee212e2015-07-07 17:13:30 -04003675TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock, const TSourceLoc &loc)
3676{
3677 if(boolErrorCheck(loc, cond))
3678 recover();
3679
3680 if(trueBlock->getType() != falseBlock->getType())
3681 {
3682 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3683 recover();
3684 return falseBlock;
3685 }
3686 // ESSL1 sections 5.2 and 5.7:
3687 // ESSL3 section 5.7:
3688 // Ternary operator is not among the operators allowed for structures/arrays.
3689 if(trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3690 {
3691 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3692 recover();
3693 return falseBlock;
3694 }
3695 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3696}
3697
John Bauman66b8ab22014-05-06 15:57:45 -04003698//
3699// Parse an array of strings using yyparse.
3700//
3701// Returns 0 for success.
3702//
3703int PaParseStrings(int count, const char* const string[], const int length[],
Nicolas Capens0bac2852016-05-07 06:09:58 -04003704 TParseContext* context) {
3705 if ((count == 0) || !string)
3706 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003707
Nicolas Capens0bac2852016-05-07 06:09:58 -04003708 if (glslang_initialize(context))
3709 return 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003710
Nicolas Capens0bac2852016-05-07 06:09:58 -04003711 int error = glslang_scan(count, string, length, context);
3712 if (!error)
3713 error = glslang_parse(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003714
Nicolas Capens0bac2852016-05-07 06:09:58 -04003715 glslang_finalize(context);
John Bauman66b8ab22014-05-06 15:57:45 -04003716
Nicolas Capens0bac2852016-05-07 06:09:58 -04003717 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
John Bauman66b8ab22014-05-06 15:57:45 -04003718}
3719
3720
3721