blob: 4fda8272d3b7f5f5862bb80fb346d1d02175ac11 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capenscc863da2015-01-21 15:50:55 -05007#include "ParseHelper.h"
John Bauman66b8ab22014-05-06 15:57:45 -04008
9#include <stdarg.h>
10#include <stdio.h>
11
Nicolas Capenscc863da2015-01-21 15:50:55 -050012#include "glslang.h"
13#include "preprocessor/SourceLocation.h"
John Bauman66b8ab22014-05-06 15:57:45 -040014
15///////////////////////////////////////////////////////////////////////
16//
17// Sub- vector and matrix fields
18//
19////////////////////////////////////////////////////////////////////////
20
21//
22// Look at a '.' field selector string and change it into offsets
23// for a vector.
24//
25bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
26{
27 fields.num = (int) compString.size();
28 if (fields.num > 4) {
29 error(line, "illegal vector field selection", compString.c_str());
30 return false;
31 }
32
33 enum {
34 exyzw,
35 ergba,
John Baumand4ae8632014-05-06 16:18:33 -040036 estpq
John Bauman66b8ab22014-05-06 15:57:45 -040037 } fieldSet[4];
38
39 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040041 case 'x':
John Bauman66b8ab22014-05-06 15:57:45 -040042 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040045 case 'r':
John Bauman66b8ab22014-05-06 15:57:45 -040046 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040053 case 'y':
John Bauman66b8ab22014-05-06 15:57:45 -040054 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040057 case 'g':
John Bauman66b8ab22014-05-06 15:57:45 -040058 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040065 case 'z':
John Bauman66b8ab22014-05-06 15:57:45 -040066 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040069 case 'b':
John Bauman66b8ab22014-05-06 15:57:45 -040070 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040077 case 'w':
John Bauman66b8ab22014-05-06 15:57:45 -040078 fields.offsets[i] = 3;
79 fieldSet[i] = exyzw;
80 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040081 case 'a':
John Bauman66b8ab22014-05-06 15:57:45 -040082 fields.offsets[i] = 3;
83 fieldSet[i] = ergba;
84 break;
85 case 'q':
86 fields.offsets[i] = 3;
87 fieldSet[i] = estpq;
88 break;
89 default:
90 error(line, "illegal vector field selection", compString.c_str());
91 return false;
92 }
93 }
94
95 for (int i = 0; i < fields.num; ++i) {
96 if (fields.offsets[i] >= vecSize) {
97 error(line, "vector field selection out of range", compString.c_str());
98 return false;
99 }
100
101 if (i > 0) {
102 if (fieldSet[i] != fieldSet[i-1]) {
103 error(line, "illegal - vector component fields not from the same set", compString.c_str());
104 return false;
105 }
106 }
107 }
108
109 return true;
110}
111
112
113//
114// Look at a '.' field selector string and change it into offsets
115// for a matrix.
116//
Alexis Hetu00106d42015-04-23 11:45:35 -0400117bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, int line)
John Bauman66b8ab22014-05-06 15:57:45 -0400118{
119 fields.wholeRow = false;
120 fields.wholeCol = false;
121 fields.row = -1;
122 fields.col = -1;
123
124 if (compString.size() != 2) {
125 error(line, "illegal length of matrix field selection", compString.c_str());
126 return false;
127 }
128
129 if (compString[0] == '_') {
130 if (compString[1] < '0' || compString[1] > '3') {
131 error(line, "illegal matrix field selection", compString.c_str());
132 return false;
133 }
134 fields.wholeCol = true;
135 fields.col = compString[1] - '0';
136 } else if (compString[1] == '_') {
137 if (compString[0] < '0' || compString[0] > '3') {
138 error(line, "illegal matrix field selection", compString.c_str());
139 return false;
140 }
141 fields.wholeRow = true;
142 fields.row = compString[0] - '0';
143 } else {
144 if (compString[0] < '0' || compString[0] > '3' ||
145 compString[1] < '0' || compString[1] > '3') {
146 error(line, "illegal matrix field selection", compString.c_str());
147 return false;
148 }
149 fields.row = compString[0] - '0';
150 fields.col = compString[1] - '0';
151 }
152
Alexis Hetu00106d42015-04-23 11:45:35 -0400153 if (fields.row >= matRows || fields.col >= matCols) {
John Bauman66b8ab22014-05-06 15:57:45 -0400154 error(line, "matrix field selection out of range", compString.c_str());
155 return false;
156 }
157
158 return true;
159}
160
161///////////////////////////////////////////////////////////////////////
162//
163// Errors
164//
165////////////////////////////////////////////////////////////////////////
166
167//
168// Track whether errors have occurred.
169//
170void TParseContext::recover()
171{
172}
173
174//
175// Used by flex/bison to output all syntax and parsing errors.
176//
177void TParseContext::error(TSourceLoc loc,
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400178 const char* reason, const char* token,
John Bauman66b8ab22014-05-06 15:57:45 -0400179 const char* extraInfo)
180{
181 pp::SourceLocation srcLoc;
182 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
183 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
184 srcLoc, reason, token, extraInfo);
185
186}
187
188void TParseContext::warning(TSourceLoc loc,
189 const char* reason, const char* token,
190 const char* extraInfo) {
191 pp::SourceLocation srcLoc;
192 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
193 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
194 srcLoc, reason, token, extraInfo);
195}
196
197void TParseContext::trace(const char* str)
198{
199 diagnostics.writeDebug(str);
200}
201
202//
203// Same error message for all places assignments don't work.
204//
205void TParseContext::assignError(int line, const char* op, TString left, TString right)
206{
207 std::stringstream extraInfoStream;
208 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
209 std::string extraInfo = extraInfoStream.str();
210 error(line, "", op, extraInfo.c_str());
211}
212
213//
214// Same error message for all places unary operations don't work.
215//
216void TParseContext::unaryOpError(int line, const char* op, TString operand)
217{
218 std::stringstream extraInfoStream;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400219 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
John Bauman66b8ab22014-05-06 15:57:45 -0400220 << " (or there is no acceptable conversion)";
221 std::string extraInfo = extraInfoStream.str();
222 error(line, " wrong operand type", op, extraInfo.c_str());
223}
224
225//
226// Same error message for all binary operations don't work.
227//
228void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
229{
230 std::stringstream extraInfoStream;
231 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
232 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
233 std::string extraInfo = extraInfoStream.str();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400234 error(line, " wrong operand types ", op, extraInfo.c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400235}
236
237bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
238 if (!checksPrecisionErrors)
239 return false;
240 switch( type ){
241 case EbtFloat:
242 if( precision == EbpUndefined ){
243 error( line, "No precision specified for (float)", "" );
244 return true;
245 }
246 break;
247 case EbtInt:
248 if( precision == EbpUndefined ){
249 error( line, "No precision specified (int)", "" );
250 return true;
251 }
252 break;
253 default:
254 return false;
255 }
256 return false;
257}
258
259//
260// Both test and if necessary, spit out an error, to see if the node is really
261// an l-value that can be operated on this way.
262//
263// Returns true if the was an error.
264//
265bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
266{
267 TIntermSymbol* symNode = node->getAsSymbolNode();
268 TIntermBinary* binaryNode = node->getAsBinaryNode();
269
270 if (binaryNode) {
271 bool errorReturn;
272
273 switch(binaryNode->getOp()) {
274 case EOpIndexDirect:
275 case EOpIndexIndirect:
276 case EOpIndexDirectStruct:
277 return lValueErrorCheck(line, op, binaryNode->getLeft());
278 case EOpVectorSwizzle:
279 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
280 if (!errorReturn) {
281 int offset[4] = {0,0,0,0};
282
283 TIntermTyped* rightNode = binaryNode->getRight();
284 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400285
286 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
John Bauman66b8ab22014-05-06 15:57:45 -0400287 p != aggrNode->getSequence().end(); p++) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500288 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400289 offset[value]++;
John Bauman66b8ab22014-05-06 15:57:45 -0400290 if (offset[value] > 1) {
291 error(line, " l-value of swizzle cannot have duplicate components", op);
292
293 return true;
294 }
295 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400296 }
John Bauman66b8ab22014-05-06 15:57:45 -0400297
298 return errorReturn;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400299 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400300 break;
301 }
302 error(line, " l-value required", op);
303
304 return true;
305 }
306
307
308 const char* symbol = 0;
309 if (symNode != 0)
310 symbol = symNode->getSymbol().c_str();
311
312 const char* message = 0;
313 switch (node->getQualifier()) {
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500314 case EvqConstExpr: message = "can't modify a const"; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400315 case EvqConstReadOnly: message = "can't modify a const"; break;
316 case EvqAttribute: message = "can't modify an attribute"; break;
317 case EvqUniform: message = "can't modify a uniform"; break;
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400318 case EvqSmoothIn:
319 case EvqFlatIn:
320 case EvqCentroidIn:
John Bauman66b8ab22014-05-06 15:57:45 -0400321 case EvqVaryingIn: message = "can't modify a varying"; break;
322 case EvqInput: message = "can't modify an input"; break;
323 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
324 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
325 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
Alexis Hetu6743bbf2015-04-21 17:06:14 -0400326 case EvqInstanceID: message = "can't modify gl_InstanceID"; break;
John Bauman66b8ab22014-05-06 15:57:45 -0400327 default:
328
329 //
330 // Type that can't be written to?
331 //
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400332 if(IsSampler(node->getBasicType()))
333 {
John Bauman66b8ab22014-05-06 15:57:45 -0400334 message = "can't modify a sampler";
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400335 }
336 else if(node->getBasicType() == EbtVoid)
337 {
John Bauman66b8ab22014-05-06 15:57:45 -0400338 message = "can't modify void";
John Bauman66b8ab22014-05-06 15:57:45 -0400339 }
340 }
341
342 if (message == 0 && binaryNode == 0 && symNode == 0) {
343 error(line, " l-value required", op);
344
345 return true;
346 }
347
348
349 //
350 // Everything else is okay, no error.
351 //
352 if (message == 0)
353 return false;
354
355 //
356 // If we get here, we have an error and a message.
357 //
358 if (symNode) {
359 std::stringstream extraInfoStream;
360 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
361 std::string extraInfo = extraInfoStream.str();
362 error(line, " l-value required", op, extraInfo.c_str());
363 }
364 else {
365 std::stringstream extraInfoStream;
366 extraInfoStream << "(" << message << ")";
367 std::string extraInfo = extraInfoStream.str();
368 error(line, " l-value required", op, extraInfo.c_str());
369 }
370
371 return true;
372}
373
374//
375// Both test, and if necessary spit out an error, to see if the node is really
376// a constant.
377//
378// Returns true if the was an error.
379//
380bool TParseContext::constErrorCheck(TIntermTyped* node)
381{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500382 if (node->getQualifier() == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400383 return false;
384
385 error(node->getLine(), "constant expression required", "");
386
387 return true;
388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// an integer.
393//
394// Returns true if the was an error.
395//
396bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
397{
Nicolas Capens3c20f802015-02-17 17:17:20 -0500398 if (node->isScalarInt())
John Bauman66b8ab22014-05-06 15:57:45 -0400399 return false;
400
401 error(node->getLine(), "integer expression required", token);
402
403 return true;
404}
405
406//
407// Both test, and if necessary spit out an error, to see if we are currently
408// globally scoped.
409//
410// Returns true if the was an error.
411//
412bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
413{
414 if (global)
415 return false;
416
417 error(line, "only allowed at global scope", token);
418
419 return true;
420}
421
422//
423// For now, keep it simple: if it starts "gl_", it's reserved, independent
424// of scope. Except, if the symbol table is at the built-in push-level,
425// which is when we are parsing built-ins.
426// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
427// webgl shader.
428//
429// Returns true if there was an error.
430//
431bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
432{
433 static const char* reservedErrMsg = "reserved built-in name";
434 if (!symbolTable.atBuiltInLevel()) {
435 if (identifier.compare(0, 3, "gl_") == 0) {
436 error(line, reservedErrMsg, "gl_");
437 return true;
438 }
John Bauman66b8ab22014-05-06 15:57:45 -0400439 if (identifier.find("__") != TString::npos) {
440 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
441 return true;
442 }
443 }
444
445 return false;
446}
447
448//
449// Make sure there is enough data provided to the constructor to build
450// something of the type of the constructor. Also returns the type of
451// the constructor.
452//
453// Returns true if there was an error in construction.
454//
455bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
456{
457 *type = function.getReturnType();
458
459 bool constructingMatrix = false;
460 switch(op) {
461 case EOpConstructMat2:
462 case EOpConstructMat3:
463 case EOpConstructMat4:
464 constructingMatrix = true;
465 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400466 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400467 break;
468 }
469
470 //
471 // Note: It's okay to have too many components available, but not okay to have unused
472 // arguments. 'full' will go to true when enough args have been seen. If we loop
473 // again, there is an extra argument, so 'overfull' will become true.
474 //
475
476 int size = 0;
477 bool constType = true;
478 bool full = false;
479 bool overFull = false;
480 bool matrixInMatrix = false;
481 bool arrayArg = false;
482 for (int i = 0; i < function.getParamCount(); ++i) {
483 const TParameter& param = function.getParam(i);
484 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400485
John Bauman66b8ab22014-05-06 15:57:45 -0400486 if (constructingMatrix && param.type->isMatrix())
487 matrixInMatrix = true;
488 if (full)
489 overFull = true;
490 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
491 full = true;
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500492 if (param.type->getQualifier() != EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400493 constType = false;
494 if (param.type->isArray())
495 arrayArg = true;
496 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400497
John Bauman66b8ab22014-05-06 15:57:45 -0400498 if (constType)
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500499 type->setQualifier(EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -0400500
501 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
502 error(line, "array constructor needs one argument per array element", "constructor");
503 return true;
504 }
505
506 if (arrayArg && op != EOpConstructStruct) {
507 error(line, "constructing from a non-dereferenced array", "constructor");
508 return true;
509 }
510
511 if (matrixInMatrix && !type->isArray()) {
512 if (function.getParamCount() != 1) {
513 error(line, "constructing matrix from matrix can only take one argument", "constructor");
514 return true;
515 }
516 }
517
518 if (overFull) {
519 error(line, "too many arguments", "constructor");
520 return true;
521 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400522
John Bauman66b8ab22014-05-06 15:57:45 -0400523 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
524 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
525 return true;
526 }
527
528 if (!type->isMatrix() || !matrixInMatrix) {
529 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
530 (op == EOpConstructStruct && size < type->getObjectSize())) {
531 error(line, "not enough data provided for construction", "constructor");
532 return true;
533 }
534 }
535
536 TIntermTyped *typed = node ? node->getAsTyped() : 0;
537 if (typed == 0) {
538 error(line, "constructor argument does not have a type", "constructor");
539 return true;
540 }
541 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
542 error(line, "cannot convert a sampler", "constructor");
543 return true;
544 }
545 if (typed->getBasicType() == EbtVoid) {
546 error(line, "cannot convert a void", "constructor");
547 return true;
548 }
549
550 return false;
551}
552
553// This function checks to see if a void variable has been declared and raise an error message for such a case
554//
555// returns true in case of an error
556//
557bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
558{
559 if (pubType.type == EbtVoid) {
560 error(line, "illegal use of type 'void'", identifier.c_str());
561 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400562 }
John Bauman66b8ab22014-05-06 15:57:45 -0400563
564 return false;
565}
566
567// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
568//
569// returns true in case of an error
570//
571bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
572{
573 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
574 error(line, "boolean expression expected", "");
575 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400576 }
John Bauman66b8ab22014-05-06 15:57:45 -0400577
578 return false;
579}
580
581// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
582//
583// returns true in case of an error
584//
585bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
586{
Alexis Hetub14178b2015-04-13 13:23:20 -0400587 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400588 error(line, "boolean expression expected", "");
589 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400590 }
John Bauman66b8ab22014-05-06 15:57:45 -0400591
592 return false;
593}
594
595bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
596{
597 if (pType.type == EbtStruct) {
598 if (containsSampler(*pType.userDef)) {
599 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400600
John Bauman66b8ab22014-05-06 15:57:45 -0400601 return true;
602 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400603
John Bauman66b8ab22014-05-06 15:57:45 -0400604 return false;
605 } else if (IsSampler(pType.type)) {
606 error(line, reason, getBasicString(pType.type));
607
608 return true;
609 }
610
611 return false;
612}
613
614bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
615{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400616 switch(pType.qualifier)
617 {
618 case EvqVaryingOut:
619 case EvqSmooth:
620 case EvqFlat:
621 case EvqCentroidOut:
622 case EvqVaryingIn:
623 case EvqSmoothIn:
624 case EvqFlatIn:
625 case EvqCentroidIn:
626 case EvqAttribute:
627 if(pType.type == EbtStruct)
628 {
629 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400630
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400631 return true;
632 }
633 break;
634 default:
635 break;
636 }
John Bauman66b8ab22014-05-06 15:57:45 -0400637
638 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
639 return true;
640
641 return false;
642}
643
644bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
645{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400646 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
John Bauman66b8ab22014-05-06 15:57:45 -0400647 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
648 error(line, "samplers cannot be output parameters", type.getBasicString());
649 return true;
650 }
651
652 return false;
653}
654
655bool TParseContext::containsSampler(TType& type)
656{
657 if (IsSampler(type.getBasicType()))
658 return true;
659
660 if (type.getBasicType() == EbtStruct) {
661 TTypeList& structure = *type.getStruct();
662 for (unsigned int i = 0; i < structure.size(); ++i) {
663 if (containsSampler(*structure[i].type))
664 return true;
665 }
666 }
667
668 return false;
669}
670
671//
672// Do size checking for an array type's size.
673//
674// Returns true if there was an error.
675//
676bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
677{
678 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500679
680 if (constant == 0 || !constant->isScalarInt())
681 {
John Bauman66b8ab22014-05-06 15:57:45 -0400682 error(line, "array size must be a constant integer expression", "");
683 return true;
684 }
685
Nicolas Capens3c20f802015-02-17 17:17:20 -0500686 if (constant->getBasicType() == EbtUInt)
687 {
688 unsigned int uintSize = constant->getUConst(0);
689 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
690 {
691 error(line, "array size too large", "");
692 size = 1;
693 return true;
694 }
John Bauman66b8ab22014-05-06 15:57:45 -0400695
Nicolas Capens3c20f802015-02-17 17:17:20 -0500696 size = static_cast<int>(uintSize);
697 }
698 else
699 {
700 size = constant->getIConst(0);
701
702 if (size <= 0)
703 {
704 error(line, "array size must be a positive integer", "");
705 size = 1;
706 return true;
707 }
John Bauman66b8ab22014-05-06 15:57:45 -0400708 }
709
710 return false;
711}
712
713//
714// See if this qualifier can be an array.
715//
716// Returns true if there is an error.
717//
718bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
719{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500720 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400721 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
722 return true;
723 }
724
725 return false;
726}
727
728//
729// See if this type can be an array.
730//
731// Returns true if there is an error.
732//
733bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
734{
735 //
736 // Can the type be an array?
737 //
738 if (type.array) {
739 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
740 return true;
741 }
742
743 return false;
744}
745
746//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400747// Do all the semantic checking for declaring an array, with and
John Bauman66b8ab22014-05-06 15:57:45 -0400748// without a size, and make the right changes to the symbol table.
749//
750// size == 0 means no specified size.
751//
752// Returns true if there was an error.
753//
754bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
755{
756 //
757 // Don't check for reserved word use until after we know it's not in the symbol table,
758 // because reserved arrays can be redeclared.
759 //
760
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400761 bool builtIn = false;
John Bauman66b8ab22014-05-06 15:57:45 -0400762 bool sameScope = false;
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500763 TSymbol* symbol = symbolTable.find(identifier, shaderVersion, &builtIn, &sameScope);
John Bauman66b8ab22014-05-06 15:57:45 -0400764 if (symbol == 0 || !sameScope) {
765 if (reservedErrorCheck(line, identifier))
766 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400767
John Bauman66b8ab22014-05-06 15:57:45 -0400768 variable = new TVariable(&identifier, TType(type));
769
770 if (type.arraySize)
771 variable->getType().setArraySize(type.arraySize);
772
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500773 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400774 delete variable;
775 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
776 return true;
777 }
778 } else {
779 if (! symbol->isVariable()) {
780 error(line, "variable expected", identifier.c_str());
781 return true;
782 }
783
784 variable = static_cast<TVariable*>(symbol);
785 if (! variable->getType().isArray()) {
786 error(line, "redeclaring non-array as array", identifier.c_str());
787 return true;
788 }
789 if (variable->getType().getArraySize() > 0) {
790 error(line, "redeclaration of array with size", identifier.c_str());
791 return true;
792 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400793
John Bauman66b8ab22014-05-06 15:57:45 -0400794 if (! variable->getType().sameElementType(TType(type))) {
795 error(line, "redeclaration of array with a different type", identifier.c_str());
796 return true;
797 }
798
799 TType* t = variable->getArrayInformationType();
800 while (t != 0) {
801 if (t->getMaxArraySize() > type.arraySize) {
802 error(line, "higher index value already used for the array", identifier.c_str());
803 return true;
804 }
805 t->setArraySize(type.arraySize);
806 t = t->getArrayInformationType();
807 }
808
809 if (type.arraySize)
810 variable->getType().setArraySize(type.arraySize);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400811 }
John Bauman66b8ab22014-05-06 15:57:45 -0400812
813 if (voidErrorCheck(line, identifier, type))
814 return true;
815
816 return false;
817}
818
819bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
820{
821 bool builtIn = false;
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500822 TSymbol* symbol = symbolTable.find(node->getSymbol(), shaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400823 if (symbol == 0) {
824 error(line, " undeclared identifier", node->getSymbol().c_str());
825 return true;
826 }
827 TVariable* variable = static_cast<TVariable*>(symbol);
828
829 type->setArrayInformationType(variable->getArrayInformationType());
830 variable->updateArrayInformationType(type);
831
832 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
833 // its an error
834 if (node->getSymbol() == "gl_FragData") {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500835 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", shaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400836 ASSERT(fragData);
837
838 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
839 if (fragDataValue <= size) {
840 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
841 return true;
842 }
843 }
844
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400845 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
John Bauman66b8ab22014-05-06 15:57:45 -0400846 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
847 if (!updateFlag)
848 return false;
849
850 size++;
851 variable->getType().setMaxArraySize(size);
852 type->setMaxArraySize(size);
853 TType* tt = type;
854
855 while(tt->getArrayInformationType() != 0) {
856 tt = tt->getArrayInformationType();
857 tt->setMaxArraySize(size);
858 }
859
860 return false;
861}
862
863//
864// Enforce non-initializer type/qualifier rules.
865//
866// Returns true if there was an error.
867//
868bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
869{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500870 if (type.qualifier == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400871 {
872 // Make the qualifier make sense.
873 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400874
John Bauman66b8ab22014-05-06 15:57:45 -0400875 if (array)
876 {
877 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
878 }
879 else if (type.isStructureContainingArrays())
880 {
881 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
882 }
883 else
884 {
885 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
886 }
887
888 return true;
889 }
890
891 return false;
892}
893
894//
895// Do semantic checking for a variable declaration that has no initializer,
896// and update the symbol table.
897//
898// Returns true if there was an error.
899//
900bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
901{
902 if (reservedErrorCheck(line, identifier))
903 recover();
904
905 variable = new TVariable(&identifier, TType(type));
906
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500907 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400908 error(line, "redefinition", variable->getName().c_str());
909 delete variable;
910 variable = 0;
911 return true;
912 }
913
914 if (voidErrorCheck(line, identifier, type))
915 return true;
916
917 return false;
918}
919
920bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400921{
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500922 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
John Bauman66b8ab22014-05-06 15:57:45 -0400923 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
924 return true;
925 }
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500926 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
John Bauman66b8ab22014-05-06 15:57:45 -0400927 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
928 return true;
929 }
930
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500931 if (qualifier == EvqConstReadOnly)
John Bauman66b8ab22014-05-06 15:57:45 -0400932 type->setQualifier(EvqConstReadOnly);
933 else
934 type->setQualifier(paramQualifier);
935
936 return false;
937}
938
939bool TParseContext::extensionErrorCheck(int line, const TString& extension)
940{
941 const TExtensionBehavior& extBehavior = extensionBehavior();
942 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
943 if (iter == extBehavior.end()) {
944 error(line, "extension", extension.c_str(), "is not supported");
945 return true;
946 }
947 // In GLSL ES, an extension's default behavior is "disable".
948 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
949 error(line, "extension", extension.c_str(), "is disabled");
950 return true;
951 }
952 if (iter->second == EBhWarn) {
953 warning(line, "extension", extension.c_str(), "is being used");
954 return false;
955 }
956
957 return false;
958}
959
960bool TParseContext::supportsExtension(const char* extension)
961{
962 const TExtensionBehavior& extbehavior = extensionBehavior();
963 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
964 return (iter != extbehavior.end());
965}
966
967void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
968{
969 pp::SourceLocation loc;
970 DecodeSourceLoc(line, &loc.file, &loc.line);
971 directiveHandler.handleExtension(loc, extName, behavior);
972}
973
974void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
975{
976 pp::SourceLocation loc;
977 DecodeSourceLoc(line, &loc.file, &loc.line);
978 directiveHandler.handlePragma(loc, name, value);
979}
980
981/////////////////////////////////////////////////////////////////////////////////
982//
983// Non-Errors.
984//
985/////////////////////////////////////////////////////////////////////////////////
986
987//
988// Look up a function name in the symbol table, and make sure it is a function.
989//
990// Return the function symbol if found, otherwise 0.
991//
992const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
993{
994 // First find by unmangled name to check whether the function name has been
995 // hidden by a variable name or struct typename.
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500996 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400997 if (symbol == 0) {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500998 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400999 }
1000
1001 if (symbol == 0) {
1002 error(line, "no matching overloaded function found", call->getName().c_str());
1003 return 0;
1004 }
1005
1006 if (!symbol->isFunction()) {
1007 error(line, "function name expected", call->getName().c_str());
1008 return 0;
1009 }
1010
1011 return static_cast<const TFunction*>(symbol);
1012}
1013
1014//
1015// Initializers show up in several places in the grammar. Have one set of
1016// code to handle them here.
1017//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001018bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
John Bauman66b8ab22014-05-06 15:57:45 -04001019 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
1020{
1021 TType type = TType(pType);
1022
1023 if (variable == 0) {
1024 if (reservedErrorCheck(line, identifier))
1025 return true;
1026
1027 if (voidErrorCheck(line, identifier, pType))
1028 return true;
1029
1030 //
1031 // add variable to symbol table
1032 //
1033 variable = new TVariable(&identifier, type);
Nicolas Capensd603ecd2015-02-18 14:52:21 -05001034 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001035 error(line, "redefinition", variable->getName().c_str());
1036 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001037 // don't delete variable, it's used by error recovery, and the pool
John Bauman66b8ab22014-05-06 15:57:45 -04001038 // pop will take care of the memory
1039 }
1040 }
1041
1042 //
1043 // identifier must be of type constant, a global, or a temporary
1044 //
1045 TQualifier qualifier = variable->getType().getQualifier();
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001046 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001047 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1048 return true;
1049 }
1050 //
1051 // test for and propagate constant
1052 //
1053
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001054 if (qualifier == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001055 if (qualifier != initializer->getType().getQualifier()) {
1056 std::stringstream extraInfoStream;
1057 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1058 std::string extraInfo = extraInfoStream.str();
1059 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1060 variable->getType().setQualifier(EvqTemporary);
1061 return true;
1062 }
1063 if (type != initializer->getType()) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001064 error(line, " non-matching types for const initializer ",
John Bauman66b8ab22014-05-06 15:57:45 -04001065 variable->getType().getQualifierString());
1066 variable->getType().setQualifier(EvqTemporary);
1067 return true;
1068 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001069 if (initializer->getAsConstantUnion()) {
John Bauman66b8ab22014-05-06 15:57:45 -04001070 ConstantUnion* unionArray = variable->getConstPointer();
1071
1072 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1073 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1074 } else {
1075 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1076 }
1077 } else if (initializer->getAsSymbolNode()) {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -05001078 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), shaderVersion);
John Bauman66b8ab22014-05-06 15:57:45 -04001079 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1080
1081 ConstantUnion* constArray = tVar->getConstPointer();
1082 variable->shareConstPointer(constArray);
1083 } else {
1084 std::stringstream extraInfoStream;
1085 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1086 std::string extraInfo = extraInfoStream.str();
1087 error(line, " cannot assign to", "=", extraInfo.c_str());
1088 variable->getType().setQualifier(EvqTemporary);
1089 return true;
1090 }
1091 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001092
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001093 if (qualifier != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001094 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1095 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1096 if (intermNode == 0) {
1097 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1098 return true;
1099 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001100 } else
John Bauman66b8ab22014-05-06 15:57:45 -04001101 intermNode = 0;
1102
1103 return false;
1104}
1105
1106bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1107{
1108 ASSERT(aggrNode != NULL);
1109 if (!aggrNode->isConstructor())
1110 return false;
1111
1112 bool allConstant = true;
1113
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001114 // check if all the child nodes are constants so that they can be inserted into
John Bauman66b8ab22014-05-06 15:57:45 -04001115 // the parent node
1116 TIntermSequence &sequence = aggrNode->getSequence() ;
1117 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1118 if (!(*p)->getAsTyped()->getAsConstantUnion())
1119 return false;
1120 }
1121
1122 return allConstant;
1123}
1124
1125// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001126// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04001127//
1128// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1129//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001130TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -04001131{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001132 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04001133
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001134 if(!aggregateArguments)
1135 {
1136 aggregateArguments = new TIntermAggregate;
1137 aggregateArguments->getSequence().push_back(arguments);
John Bauman66b8ab22014-05-06 15:57:45 -04001138 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001139
1140 if(op == EOpConstructStruct)
1141 {
1142 TTypeList &fields = *type->getStruct();
1143 TIntermSequence &args = aggregateArguments->getSequence();
1144
1145 for(size_t i = 0; i < fields.size(); i++)
1146 {
1147 if(args[i]->getAsTyped()->getType() != *fields[i].type)
1148 {
1149 error(line, "Structure constructor arguments do not match structure fields", "Error");
1150 recover();
1151
1152 return 0;
1153 }
John Bauman66b8ab22014-05-06 15:57:45 -04001154 }
1155 }
1156
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001157 // Turn the argument list itself into a constructor
1158 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1159 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1160 if(constConstructor)
1161 {
John Bauman66b8ab22014-05-06 15:57:45 -04001162 return constConstructor;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001163 }
John Bauman66b8ab22014-05-06 15:57:45 -04001164
1165 return constructor;
1166}
1167
1168TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1169{
1170 bool canBeFolded = areAllChildConst(aggrNode);
1171 aggrNode->setType(type);
1172 if (canBeFolded) {
1173 bool returnVal = false;
1174 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1175 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04001176 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04001177 }
1178 else {
John Baumand4ae8632014-05-06 16:18:33 -04001179 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04001180 }
1181 if (returnVal)
1182 return 0;
1183
1184 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1185 }
1186
1187 return 0;
1188}
1189
John Bauman66b8ab22014-05-06 15:57:45 -04001190//
1191// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1192// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1193// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1194// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1195// a constant matrix.
1196//
1197TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1198{
1199 TIntermTyped* typedNode;
1200 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1201
1202 ConstantUnion *unionArray;
1203 if (tempConstantNode) {
1204 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04001205
1206 if (!unionArray) {
1207 return node;
1208 }
1209 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1210 error(line, "Cannot offset into the vector", "Error");
1211 recover();
1212
1213 return 0;
1214 }
1215
1216 ConstantUnion* constArray = new ConstantUnion[fields.num];
1217
1218 for (int i = 0; i < fields.num; i++) {
1219 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1220 std::stringstream extraInfoStream;
1221 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1222 std::string extraInfo = extraInfoStream.str();
1223 error(line, "", "[", extraInfo.c_str());
1224 recover();
1225 fields.offsets[i] = 0;
1226 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001227
John Bauman66b8ab22014-05-06 15:57:45 -04001228 constArray[i] = unionArray[fields.offsets[i]];
1229
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001230 }
John Bauman66b8ab22014-05-06 15:57:45 -04001231 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1232 return typedNode;
1233}
1234
1235//
1236// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001237// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1238// 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 -04001239// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1240//
1241TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1242{
1243 TIntermTyped* typedNode;
1244 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1245
1246 if (index >= node->getType().getNominalSize()) {
1247 std::stringstream extraInfoStream;
1248 extraInfoStream << "matrix field selection out of range '" << index << "'";
1249 std::string extraInfo = extraInfoStream.str();
1250 error(line, "", "[", extraInfo.c_str());
1251 recover();
1252 index = 0;
1253 }
1254
1255 if (tempConstantNode) {
1256 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1257 int size = tempConstantNode->getType().getNominalSize();
1258 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1259 } else {
1260 error(line, "Cannot offset into the matrix", "Error");
1261 recover();
1262
1263 return 0;
1264 }
1265
1266 return typedNode;
1267}
1268
1269
1270//
1271// 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 -04001272// the symbol table and parse-tree is built for the type of the element. The input
1273// 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 -04001274// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1275//
1276TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1277{
1278 TIntermTyped* typedNode;
1279 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1280 TType arrayElementType = node->getType();
1281 arrayElementType.clearArrayness();
1282
1283 if (index >= node->getType().getArraySize()) {
1284 std::stringstream extraInfoStream;
1285 extraInfoStream << "array field selection out of range '" << index << "'";
1286 std::string extraInfo = extraInfoStream.str();
1287 error(line, "", "[", extraInfo.c_str());
1288 recover();
1289 index = 0;
1290 }
1291
1292 int arrayElementSize = arrayElementType.getObjectSize();
1293
1294 if (tempConstantNode) {
1295 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1296 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1297 } else {
1298 error(line, "Cannot offset into the array", "Error");
1299 recover();
1300
1301 return 0;
1302 }
1303
1304 return typedNode;
1305}
1306
1307
1308//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001309// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04001310// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1311// function and returns the parse-tree with the values of the embedded/nested struct.
1312//
1313TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1314{
1315 const TTypeList* fields = node->getType().getStruct();
1316 TIntermTyped *typedNode;
1317 int instanceSize = 0;
1318 unsigned int index = 0;
1319 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
1320
1321 for ( index = 0; index < fields->size(); ++index) {
1322 if ((*fields)[index].type->getFieldName() == identifier) {
1323 break;
1324 } else {
1325 instanceSize += (*fields)[index].type->getObjectSize();
1326 }
1327 }
1328
1329 if (tempConstantNode) {
1330 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
1331
1332 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1333 } else {
1334 error(line, "Cannot offset into the structure", "Error");
1335 recover();
1336
1337 return 0;
1338 }
1339
1340 return typedNode;
1341}
1342
Nicolas Capens7d626792015-02-17 17:58:31 -05001343TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
1344{
1345 TLayoutQualifier qualifier;
1346
1347 qualifier.location = -1;
1348
1349 if (qualifierType == "location")
1350 {
1351 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
1352 recover();
1353 }
1354 else
1355 {
1356 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
1357 recover();
1358 }
1359
1360 return qualifier;
1361}
1362
1363TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
1364{
1365 TLayoutQualifier qualifier;
1366
1367 qualifier.location = -1;
1368
1369 if (qualifierType != "location")
1370 {
1371 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
1372 recover();
1373 }
1374 else
1375 {
1376 // must check that location is non-negative
1377 if (intValue < 0)
1378 {
1379 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
1380 recover();
1381 }
1382 else
1383 {
1384 qualifier.location = intValue;
1385 }
1386 }
1387
1388 return qualifier;
1389}
1390
1391TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
1392{
1393 TLayoutQualifier joinedQualifier = leftQualifier;
1394
1395 if (rightQualifier.location != -1)
1396 {
1397 joinedQualifier.location = rightQualifier.location;
1398 }
1399
1400 return joinedQualifier;
1401}
1402
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04001403
1404TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
1405 const TSourceLoc &storageLoc, TQualifier storageQualifier)
1406{
1407 TQualifier mergedQualifier = EvqSmoothIn;
1408
1409 if(storageQualifier == EvqVaryingIn) {
1410 if(interpolationQualifier == EvqSmooth)
1411 mergedQualifier = EvqSmoothIn;
1412 else if(interpolationQualifier == EvqFlat)
1413 mergedQualifier = EvqFlatIn;
1414 else UNREACHABLE();
1415 }
1416 else if(storageQualifier == EvqCentroidIn) {
1417 if(interpolationQualifier == EvqSmooth)
1418 mergedQualifier = EvqCentroidIn;
1419 else if(interpolationQualifier == EvqFlat)
1420 mergedQualifier = EvqFlatIn;
1421 else UNREACHABLE();
1422 }
1423 else if(storageQualifier == EvqVaryingOut) {
1424 if(interpolationQualifier == EvqSmooth)
1425 mergedQualifier = EvqSmoothOut;
1426 else if(interpolationQualifier == EvqFlat)
1427 mergedQualifier = EvqFlatOut;
1428 else UNREACHABLE();
1429 }
1430 else if(storageQualifier == EvqCentroidOut) {
1431 if(interpolationQualifier == EvqSmooth)
1432 mergedQualifier = EvqCentroidOut;
1433 else if(interpolationQualifier == EvqFlat)
1434 mergedQualifier = EvqFlatOut;
1435 else UNREACHABLE();
1436 }
1437 else {
1438 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
1439 recover();
1440
1441 mergedQualifier = storageQualifier;
1442 }
1443
1444 TPublicType type;
1445 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
1446 return type;
1447}
1448
John Bauman66b8ab22014-05-06 15:57:45 -04001449bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1450{
1451 ++structNestingLevel;
1452
1453 // Embedded structure definitions are not supported per GLSL ES spec.
1454 // They aren't allowed in GLSL either, but we need to detect this here
1455 // so we don't rely on the GLSL compiler to catch it.
1456 if (structNestingLevel > 1) {
1457 error(line, "", "Embedded struct definitions are not allowed");
1458 return true;
1459 }
1460
1461 return false;
1462}
1463
1464void TParseContext::exitStructDeclaration()
1465{
1466 --structNestingLevel;
1467}
1468
John Bauman66b8ab22014-05-06 15:57:45 -04001469//
1470// Parse an array of strings using yyparse.
1471//
1472// Returns 0 for success.
1473//
1474int PaParseStrings(int count, const char* const string[], const int length[],
1475 TParseContext* context) {
1476 if ((count == 0) || (string == NULL))
1477 return 1;
1478
1479 if (glslang_initialize(context))
1480 return 1;
1481
1482 int error = glslang_scan(count, string, length, context);
1483 if (!error)
1484 error = glslang_parse(context);
1485
1486 glslang_finalize(context);
1487
1488 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
1489}
1490
1491
1492