blob: 2d9b3d5bc2ebbeddc434b35aa1debbc5b08a01bc [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;
326 default:
327
328 //
329 // Type that can't be written to?
330 //
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400331 if(IsSampler(node->getBasicType()))
332 {
John Bauman66b8ab22014-05-06 15:57:45 -0400333 message = "can't modify a sampler";
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400334 }
335 else if(node->getBasicType() == EbtVoid)
336 {
John Bauman66b8ab22014-05-06 15:57:45 -0400337 message = "can't modify void";
John Bauman66b8ab22014-05-06 15:57:45 -0400338 }
339 }
340
341 if (message == 0 && binaryNode == 0 && symNode == 0) {
342 error(line, " l-value required", op);
343
344 return true;
345 }
346
347
348 //
349 // Everything else is okay, no error.
350 //
351 if (message == 0)
352 return false;
353
354 //
355 // If we get here, we have an error and a message.
356 //
357 if (symNode) {
358 std::stringstream extraInfoStream;
359 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
360 std::string extraInfo = extraInfoStream.str();
361 error(line, " l-value required", op, extraInfo.c_str());
362 }
363 else {
364 std::stringstream extraInfoStream;
365 extraInfoStream << "(" << message << ")";
366 std::string extraInfo = extraInfoStream.str();
367 error(line, " l-value required", op, extraInfo.c_str());
368 }
369
370 return true;
371}
372
373//
374// Both test, and if necessary spit out an error, to see if the node is really
375// a constant.
376//
377// Returns true if the was an error.
378//
379bool TParseContext::constErrorCheck(TIntermTyped* node)
380{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500381 if (node->getQualifier() == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400382 return false;
383
384 error(node->getLine(), "constant expression required", "");
385
386 return true;
387}
388
389//
390// Both test, and if necessary spit out an error, to see if the node is really
391// an integer.
392//
393// Returns true if the was an error.
394//
395bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
396{
Nicolas Capens3c20f802015-02-17 17:17:20 -0500397 if (node->isScalarInt())
John Bauman66b8ab22014-05-06 15:57:45 -0400398 return false;
399
400 error(node->getLine(), "integer expression required", token);
401
402 return true;
403}
404
405//
406// Both test, and if necessary spit out an error, to see if we are currently
407// globally scoped.
408//
409// Returns true if the was an error.
410//
411bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
412{
413 if (global)
414 return false;
415
416 error(line, "only allowed at global scope", token);
417
418 return true;
419}
420
421//
422// For now, keep it simple: if it starts "gl_", it's reserved, independent
423// of scope. Except, if the symbol table is at the built-in push-level,
424// which is when we are parsing built-ins.
425// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
426// webgl shader.
427//
428// Returns true if there was an error.
429//
430bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
431{
432 static const char* reservedErrMsg = "reserved built-in name";
433 if (!symbolTable.atBuiltInLevel()) {
434 if (identifier.compare(0, 3, "gl_") == 0) {
435 error(line, reservedErrMsg, "gl_");
436 return true;
437 }
John Bauman66b8ab22014-05-06 15:57:45 -0400438 if (identifier.find("__") != TString::npos) {
439 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
440 return true;
441 }
442 }
443
444 return false;
445}
446
447//
448// Make sure there is enough data provided to the constructor to build
449// something of the type of the constructor. Also returns the type of
450// the constructor.
451//
452// Returns true if there was an error in construction.
453//
454bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
455{
456 *type = function.getReturnType();
457
458 bool constructingMatrix = false;
459 switch(op) {
460 case EOpConstructMat2:
461 case EOpConstructMat3:
462 case EOpConstructMat4:
463 constructingMatrix = true;
464 break;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400465 default:
John Bauman66b8ab22014-05-06 15:57:45 -0400466 break;
467 }
468
469 //
470 // Note: It's okay to have too many components available, but not okay to have unused
471 // arguments. 'full' will go to true when enough args have been seen. If we loop
472 // again, there is an extra argument, so 'overfull' will become true.
473 //
474
475 int size = 0;
476 bool constType = true;
477 bool full = false;
478 bool overFull = false;
479 bool matrixInMatrix = false;
480 bool arrayArg = false;
481 for (int i = 0; i < function.getParamCount(); ++i) {
482 const TParameter& param = function.getParam(i);
483 size += param.type->getObjectSize();
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400484
John Bauman66b8ab22014-05-06 15:57:45 -0400485 if (constructingMatrix && param.type->isMatrix())
486 matrixInMatrix = true;
487 if (full)
488 overFull = true;
489 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
490 full = true;
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500491 if (param.type->getQualifier() != EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400492 constType = false;
493 if (param.type->isArray())
494 arrayArg = true;
495 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400496
John Bauman66b8ab22014-05-06 15:57:45 -0400497 if (constType)
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500498 type->setQualifier(EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -0400499
500 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
501 error(line, "array constructor needs one argument per array element", "constructor");
502 return true;
503 }
504
505 if (arrayArg && op != EOpConstructStruct) {
506 error(line, "constructing from a non-dereferenced array", "constructor");
507 return true;
508 }
509
510 if (matrixInMatrix && !type->isArray()) {
511 if (function.getParamCount() != 1) {
512 error(line, "constructing matrix from matrix can only take one argument", "constructor");
513 return true;
514 }
515 }
516
517 if (overFull) {
518 error(line, "too many arguments", "constructor");
519 return true;
520 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400521
John Bauman66b8ab22014-05-06 15:57:45 -0400522 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
523 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
524 return true;
525 }
526
527 if (!type->isMatrix() || !matrixInMatrix) {
528 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
529 (op == EOpConstructStruct && size < type->getObjectSize())) {
530 error(line, "not enough data provided for construction", "constructor");
531 return true;
532 }
533 }
534
535 TIntermTyped *typed = node ? node->getAsTyped() : 0;
536 if (typed == 0) {
537 error(line, "constructor argument does not have a type", "constructor");
538 return true;
539 }
540 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
541 error(line, "cannot convert a sampler", "constructor");
542 return true;
543 }
544 if (typed->getBasicType() == EbtVoid) {
545 error(line, "cannot convert a void", "constructor");
546 return true;
547 }
548
549 return false;
550}
551
552// This function checks to see if a void variable has been declared and raise an error message for such a case
553//
554// returns true in case of an error
555//
556bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
557{
558 if (pubType.type == EbtVoid) {
559 error(line, "illegal use of type 'void'", identifier.c_str());
560 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400561 }
John Bauman66b8ab22014-05-06 15:57:45 -0400562
563 return false;
564}
565
566// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
567//
568// returns true in case of an error
569//
570bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
571{
572 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
573 error(line, "boolean expression expected", "");
574 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400575 }
John Bauman66b8ab22014-05-06 15:57:45 -0400576
577 return false;
578}
579
580// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
581//
582// returns true in case of an error
583//
584bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
585{
Alexis Hetub14178b2015-04-13 13:23:20 -0400586 if (pType.type != EbtBool || pType.array || (pType.primarySize > 1) || (pType.secondarySize > 1)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400587 error(line, "boolean expression expected", "");
588 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400589 }
John Bauman66b8ab22014-05-06 15:57:45 -0400590
591 return false;
592}
593
594bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
595{
596 if (pType.type == EbtStruct) {
597 if (containsSampler(*pType.userDef)) {
598 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400599
John Bauman66b8ab22014-05-06 15:57:45 -0400600 return true;
601 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400602
John Bauman66b8ab22014-05-06 15:57:45 -0400603 return false;
604 } else if (IsSampler(pType.type)) {
605 error(line, reason, getBasicString(pType.type));
606
607 return true;
608 }
609
610 return false;
611}
612
613bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
614{
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400615 switch(pType.qualifier)
616 {
617 case EvqVaryingOut:
618 case EvqSmooth:
619 case EvqFlat:
620 case EvqCentroidOut:
621 case EvqVaryingIn:
622 case EvqSmoothIn:
623 case EvqFlatIn:
624 case EvqCentroidIn:
625 case EvqAttribute:
626 if(pType.type == EbtStruct)
627 {
628 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400629
Alexis Hetu55a2cbc2015-04-16 10:49:45 -0400630 return true;
631 }
632 break;
633 default:
634 break;
635 }
John Bauman66b8ab22014-05-06 15:57:45 -0400636
637 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
638 return true;
639
640 return false;
641}
642
643bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
644{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400645 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
John Bauman66b8ab22014-05-06 15:57:45 -0400646 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
647 error(line, "samplers cannot be output parameters", type.getBasicString());
648 return true;
649 }
650
651 return false;
652}
653
654bool TParseContext::containsSampler(TType& type)
655{
656 if (IsSampler(type.getBasicType()))
657 return true;
658
659 if (type.getBasicType() == EbtStruct) {
660 TTypeList& structure = *type.getStruct();
661 for (unsigned int i = 0; i < structure.size(); ++i) {
662 if (containsSampler(*structure[i].type))
663 return true;
664 }
665 }
666
667 return false;
668}
669
670//
671// Do size checking for an array type's size.
672//
673// Returns true if there was an error.
674//
675bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
676{
677 TIntermConstantUnion* constant = expr->getAsConstantUnion();
Nicolas Capens3c20f802015-02-17 17:17:20 -0500678
679 if (constant == 0 || !constant->isScalarInt())
680 {
John Bauman66b8ab22014-05-06 15:57:45 -0400681 error(line, "array size must be a constant integer expression", "");
682 return true;
683 }
684
Nicolas Capens3c20f802015-02-17 17:17:20 -0500685 if (constant->getBasicType() == EbtUInt)
686 {
687 unsigned int uintSize = constant->getUConst(0);
688 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
689 {
690 error(line, "array size too large", "");
691 size = 1;
692 return true;
693 }
John Bauman66b8ab22014-05-06 15:57:45 -0400694
Nicolas Capens3c20f802015-02-17 17:17:20 -0500695 size = static_cast<int>(uintSize);
696 }
697 else
698 {
699 size = constant->getIConst(0);
700
701 if (size <= 0)
702 {
703 error(line, "array size must be a positive integer", "");
704 size = 1;
705 return true;
706 }
John Bauman66b8ab22014-05-06 15:57:45 -0400707 }
708
709 return false;
710}
711
712//
713// See if this qualifier can be an array.
714//
715// Returns true if there is an error.
716//
717bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
718{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500719 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400720 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
721 return true;
722 }
723
724 return false;
725}
726
727//
728// See if this type can be an array.
729//
730// Returns true if there is an error.
731//
732bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
733{
734 //
735 // Can the type be an array?
736 //
737 if (type.array) {
738 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
739 return true;
740 }
741
742 return false;
743}
744
745//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400746// Do all the semantic checking for declaring an array, with and
John Bauman66b8ab22014-05-06 15:57:45 -0400747// without a size, and make the right changes to the symbol table.
748//
749// size == 0 means no specified size.
750//
751// Returns true if there was an error.
752//
753bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
754{
755 //
756 // Don't check for reserved word use until after we know it's not in the symbol table,
757 // because reserved arrays can be redeclared.
758 //
759
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400760 bool builtIn = false;
John Bauman66b8ab22014-05-06 15:57:45 -0400761 bool sameScope = false;
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500762 TSymbol* symbol = symbolTable.find(identifier, shaderVersion, &builtIn, &sameScope);
John Bauman66b8ab22014-05-06 15:57:45 -0400763 if (symbol == 0 || !sameScope) {
764 if (reservedErrorCheck(line, identifier))
765 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400766
John Bauman66b8ab22014-05-06 15:57:45 -0400767 variable = new TVariable(&identifier, TType(type));
768
769 if (type.arraySize)
770 variable->getType().setArraySize(type.arraySize);
771
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500772 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400773 delete variable;
774 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
775 return true;
776 }
777 } else {
778 if (! symbol->isVariable()) {
779 error(line, "variable expected", identifier.c_str());
780 return true;
781 }
782
783 variable = static_cast<TVariable*>(symbol);
784 if (! variable->getType().isArray()) {
785 error(line, "redeclaring non-array as array", identifier.c_str());
786 return true;
787 }
788 if (variable->getType().getArraySize() > 0) {
789 error(line, "redeclaration of array with size", identifier.c_str());
790 return true;
791 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400792
John Bauman66b8ab22014-05-06 15:57:45 -0400793 if (! variable->getType().sameElementType(TType(type))) {
794 error(line, "redeclaration of array with a different type", identifier.c_str());
795 return true;
796 }
797
798 TType* t = variable->getArrayInformationType();
799 while (t != 0) {
800 if (t->getMaxArraySize() > type.arraySize) {
801 error(line, "higher index value already used for the array", identifier.c_str());
802 return true;
803 }
804 t->setArraySize(type.arraySize);
805 t = t->getArrayInformationType();
806 }
807
808 if (type.arraySize)
809 variable->getType().setArraySize(type.arraySize);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400810 }
John Bauman66b8ab22014-05-06 15:57:45 -0400811
812 if (voidErrorCheck(line, identifier, type))
813 return true;
814
815 return false;
816}
817
818bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
819{
820 bool builtIn = false;
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500821 TSymbol* symbol = symbolTable.find(node->getSymbol(), shaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400822 if (symbol == 0) {
823 error(line, " undeclared identifier", node->getSymbol().c_str());
824 return true;
825 }
826 TVariable* variable = static_cast<TVariable*>(symbol);
827
828 type->setArrayInformationType(variable->getArrayInformationType());
829 variable->updateArrayInformationType(type);
830
831 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
832 // its an error
833 if (node->getSymbol() == "gl_FragData") {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500834 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", shaderVersion, &builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400835 ASSERT(fragData);
836
837 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
838 if (fragDataValue <= size) {
839 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
840 return true;
841 }
842 }
843
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400844 // 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 -0400845 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
846 if (!updateFlag)
847 return false;
848
849 size++;
850 variable->getType().setMaxArraySize(size);
851 type->setMaxArraySize(size);
852 TType* tt = type;
853
854 while(tt->getArrayInformationType() != 0) {
855 tt = tt->getArrayInformationType();
856 tt->setMaxArraySize(size);
857 }
858
859 return false;
860}
861
862//
863// Enforce non-initializer type/qualifier rules.
864//
865// Returns true if there was an error.
866//
867bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
868{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500869 if (type.qualifier == EvqConstExpr)
John Bauman66b8ab22014-05-06 15:57:45 -0400870 {
871 // Make the qualifier make sense.
872 type.qualifier = EvqTemporary;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400873
John Bauman66b8ab22014-05-06 15:57:45 -0400874 if (array)
875 {
876 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
877 }
878 else if (type.isStructureContainingArrays())
879 {
880 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
881 }
882 else
883 {
884 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
885 }
886
887 return true;
888 }
889
890 return false;
891}
892
893//
894// Do semantic checking for a variable declaration that has no initializer,
895// and update the symbol table.
896//
897// Returns true if there was an error.
898//
899bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
900{
901 if (reservedErrorCheck(line, identifier))
902 recover();
903
904 variable = new TVariable(&identifier, TType(type));
905
Nicolas Capensd603ecd2015-02-18 14:52:21 -0500906 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -0400907 error(line, "redefinition", variable->getName().c_str());
908 delete variable;
909 variable = 0;
910 return true;
911 }
912
913 if (voidErrorCheck(line, identifier, type))
914 return true;
915
916 return false;
917}
918
919bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400920{
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500921 if (qualifier != EvqConstReadOnly && qualifier != EvqTemporary) {
John Bauman66b8ab22014-05-06 15:57:45 -0400922 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
923 return true;
924 }
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500925 if (qualifier == EvqConstReadOnly && paramQualifier != EvqIn) {
John Bauman66b8ab22014-05-06 15:57:45 -0400926 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
927 return true;
928 }
929
Nicolas Capensb1e911a2015-02-26 13:16:00 -0500930 if (qualifier == EvqConstReadOnly)
John Bauman66b8ab22014-05-06 15:57:45 -0400931 type->setQualifier(EvqConstReadOnly);
932 else
933 type->setQualifier(paramQualifier);
934
935 return false;
936}
937
938bool TParseContext::extensionErrorCheck(int line, const TString& extension)
939{
940 const TExtensionBehavior& extBehavior = extensionBehavior();
941 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
942 if (iter == extBehavior.end()) {
943 error(line, "extension", extension.c_str(), "is not supported");
944 return true;
945 }
946 // In GLSL ES, an extension's default behavior is "disable".
947 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
948 error(line, "extension", extension.c_str(), "is disabled");
949 return true;
950 }
951 if (iter->second == EBhWarn) {
952 warning(line, "extension", extension.c_str(), "is being used");
953 return false;
954 }
955
956 return false;
957}
958
959bool TParseContext::supportsExtension(const char* extension)
960{
961 const TExtensionBehavior& extbehavior = extensionBehavior();
962 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
963 return (iter != extbehavior.end());
964}
965
966void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
967{
968 pp::SourceLocation loc;
969 DecodeSourceLoc(line, &loc.file, &loc.line);
970 directiveHandler.handleExtension(loc, extName, behavior);
971}
972
973void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
974{
975 pp::SourceLocation loc;
976 DecodeSourceLoc(line, &loc.file, &loc.line);
977 directiveHandler.handlePragma(loc, name, value);
978}
979
980/////////////////////////////////////////////////////////////////////////////////
981//
982// Non-Errors.
983//
984/////////////////////////////////////////////////////////////////////////////////
985
986//
987// Look up a function name in the symbol table, and make sure it is a function.
988//
989// Return the function symbol if found, otherwise 0.
990//
991const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
992{
993 // First find by unmangled name to check whether the function name has been
994 // hidden by a variable name or struct typename.
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500995 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400996 if (symbol == 0) {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -0500997 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
John Bauman66b8ab22014-05-06 15:57:45 -0400998 }
999
1000 if (symbol == 0) {
1001 error(line, "no matching overloaded function found", call->getName().c_str());
1002 return 0;
1003 }
1004
1005 if (!symbol->isFunction()) {
1006 error(line, "function name expected", call->getName().c_str());
1007 return 0;
1008 }
1009
1010 return static_cast<const TFunction*>(symbol);
1011}
1012
1013//
1014// Initializers show up in several places in the grammar. Have one set of
1015// code to handle them here.
1016//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001017bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
John Bauman66b8ab22014-05-06 15:57:45 -04001018 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
1019{
1020 TType type = TType(pType);
1021
1022 if (variable == 0) {
1023 if (reservedErrorCheck(line, identifier))
1024 return true;
1025
1026 if (voidErrorCheck(line, identifier, pType))
1027 return true;
1028
1029 //
1030 // add variable to symbol table
1031 //
1032 variable = new TVariable(&identifier, type);
Nicolas Capensd603ecd2015-02-18 14:52:21 -05001033 if (! symbolTable.declare(*variable)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001034 error(line, "redefinition", variable->getName().c_str());
1035 return true;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001036 // don't delete variable, it's used by error recovery, and the pool
John Bauman66b8ab22014-05-06 15:57:45 -04001037 // pop will take care of the memory
1038 }
1039 }
1040
1041 //
1042 // identifier must be of type constant, a global, or a temporary
1043 //
1044 TQualifier qualifier = variable->getType().getQualifier();
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001045 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConstExpr)) {
John Bauman66b8ab22014-05-06 15:57:45 -04001046 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1047 return true;
1048 }
1049 //
1050 // test for and propagate constant
1051 //
1052
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001053 if (qualifier == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001054 if (qualifier != initializer->getType().getQualifier()) {
1055 std::stringstream extraInfoStream;
1056 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1057 std::string extraInfo = extraInfoStream.str();
1058 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1059 variable->getType().setQualifier(EvqTemporary);
1060 return true;
1061 }
1062 if (type != initializer->getType()) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001063 error(line, " non-matching types for const initializer ",
John Bauman66b8ab22014-05-06 15:57:45 -04001064 variable->getType().getQualifierString());
1065 variable->getType().setQualifier(EvqTemporary);
1066 return true;
1067 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001068 if (initializer->getAsConstantUnion()) {
John Bauman66b8ab22014-05-06 15:57:45 -04001069 ConstantUnion* unionArray = variable->getConstPointer();
1070
1071 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1072 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1073 } else {
1074 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1075 }
1076 } else if (initializer->getAsSymbolNode()) {
Nicolas Capens0a7f0c22015-02-18 14:47:31 -05001077 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), shaderVersion);
John Bauman66b8ab22014-05-06 15:57:45 -04001078 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1079
1080 ConstantUnion* constArray = tVar->getConstPointer();
1081 variable->shareConstPointer(constArray);
1082 } else {
1083 std::stringstream extraInfoStream;
1084 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1085 std::string extraInfo = extraInfoStream.str();
1086 error(line, " cannot assign to", "=", extraInfo.c_str());
1087 variable->getType().setQualifier(EvqTemporary);
1088 return true;
1089 }
1090 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001091
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001092 if (qualifier != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -04001093 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1094 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1095 if (intermNode == 0) {
1096 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1097 return true;
1098 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001099 } else
John Bauman66b8ab22014-05-06 15:57:45 -04001100 intermNode = 0;
1101
1102 return false;
1103}
1104
1105bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1106{
1107 ASSERT(aggrNode != NULL);
1108 if (!aggrNode->isConstructor())
1109 return false;
1110
1111 bool allConstant = true;
1112
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001113 // check if all the child nodes are constants so that they can be inserted into
John Bauman66b8ab22014-05-06 15:57:45 -04001114 // the parent node
1115 TIntermSequence &sequence = aggrNode->getSequence() ;
1116 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1117 if (!(*p)->getAsTyped()->getAsConstantUnion())
1118 return false;
1119 }
1120
1121 return allConstant;
1122}
1123
1124// This function is used to test for the correctness of the parameters passed to various constructor functions
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001125// and also convert them to the right datatype if it is allowed and required.
John Bauman66b8ab22014-05-06 15:57:45 -04001126//
1127// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1128//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001129TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -04001130{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001131 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
John Bauman66b8ab22014-05-06 15:57:45 -04001132
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001133 if(!aggregateArguments)
1134 {
1135 aggregateArguments = new TIntermAggregate;
1136 aggregateArguments->getSequence().push_back(arguments);
John Bauman66b8ab22014-05-06 15:57:45 -04001137 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001138
1139 if(op == EOpConstructStruct)
1140 {
1141 TTypeList &fields = *type->getStruct();
1142 TIntermSequence &args = aggregateArguments->getSequence();
1143
1144 for(size_t i = 0; i < fields.size(); i++)
1145 {
1146 if(args[i]->getAsTyped()->getType() != *fields[i].type)
1147 {
1148 error(line, "Structure constructor arguments do not match structure fields", "Error");
1149 recover();
1150
1151 return 0;
1152 }
John Bauman66b8ab22014-05-06 15:57:45 -04001153 }
1154 }
1155
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001156 // Turn the argument list itself into a constructor
1157 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1158 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1159 if(constConstructor)
1160 {
John Bauman66b8ab22014-05-06 15:57:45 -04001161 return constConstructor;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001162 }
John Bauman66b8ab22014-05-06 15:57:45 -04001163
1164 return constructor;
1165}
1166
1167TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1168{
1169 bool canBeFolded = areAllChildConst(aggrNode);
1170 aggrNode->setType(type);
1171 if (canBeFolded) {
1172 bool returnVal = false;
1173 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1174 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04001175 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04001176 }
1177 else {
John Baumand4ae8632014-05-06 16:18:33 -04001178 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04001179 }
1180 if (returnVal)
1181 return 0;
1182
1183 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1184 }
1185
1186 return 0;
1187}
1188
John Bauman66b8ab22014-05-06 15:57:45 -04001189//
1190// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1191// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1192// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1193// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1194// a constant matrix.
1195//
1196TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1197{
1198 TIntermTyped* typedNode;
1199 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1200
1201 ConstantUnion *unionArray;
1202 if (tempConstantNode) {
1203 unionArray = tempConstantNode->getUnionArrayPointer();
John Bauman66b8ab22014-05-06 15:57:45 -04001204
1205 if (!unionArray) {
1206 return node;
1207 }
1208 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1209 error(line, "Cannot offset into the vector", "Error");
1210 recover();
1211
1212 return 0;
1213 }
1214
1215 ConstantUnion* constArray = new ConstantUnion[fields.num];
1216
1217 for (int i = 0; i < fields.num; i++) {
1218 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1219 std::stringstream extraInfoStream;
1220 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1221 std::string extraInfo = extraInfoStream.str();
1222 error(line, "", "[", extraInfo.c_str());
1223 recover();
1224 fields.offsets[i] = 0;
1225 }
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001226
John Bauman66b8ab22014-05-06 15:57:45 -04001227 constArray[i] = unionArray[fields.offsets[i]];
1228
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001229 }
John Bauman66b8ab22014-05-06 15:57:45 -04001230 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1231 return typedNode;
1232}
1233
1234//
1235// This function returns the column being accessed from a constant matrix. The values are retrieved from
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001236// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1237// 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 -04001238// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1239//
1240TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1241{
1242 TIntermTyped* typedNode;
1243 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1244
1245 if (index >= node->getType().getNominalSize()) {
1246 std::stringstream extraInfoStream;
1247 extraInfoStream << "matrix field selection out of range '" << index << "'";
1248 std::string extraInfo = extraInfoStream.str();
1249 error(line, "", "[", extraInfo.c_str());
1250 recover();
1251 index = 0;
1252 }
1253
1254 if (tempConstantNode) {
1255 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1256 int size = tempConstantNode->getType().getNominalSize();
1257 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1258 } else {
1259 error(line, "Cannot offset into the matrix", "Error");
1260 recover();
1261
1262 return 0;
1263 }
1264
1265 return typedNode;
1266}
1267
1268
1269//
1270// 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 -04001271// the symbol table and parse-tree is built for the type of the element. The input
1272// 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 -04001273// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1274//
1275TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1276{
1277 TIntermTyped* typedNode;
1278 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1279 TType arrayElementType = node->getType();
1280 arrayElementType.clearArrayness();
1281
1282 if (index >= node->getType().getArraySize()) {
1283 std::stringstream extraInfoStream;
1284 extraInfoStream << "array field selection out of range '" << index << "'";
1285 std::string extraInfo = extraInfoStream.str();
1286 error(line, "", "[", extraInfo.c_str());
1287 recover();
1288 index = 0;
1289 }
1290
1291 int arrayElementSize = arrayElementType.getObjectSize();
1292
1293 if (tempConstantNode) {
1294 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1295 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1296 } else {
1297 error(line, "Cannot offset into the array", "Error");
1298 recover();
1299
1300 return 0;
1301 }
1302
1303 return typedNode;
1304}
1305
1306
1307//
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -04001308// This function returns the value of a particular field inside a constant structure from the symbol table.
John Bauman66b8ab22014-05-06 15:57:45 -04001309// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1310// function and returns the parse-tree with the values of the embedded/nested struct.
1311//
1312TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1313{
1314 const TTypeList* fields = node->getType().getStruct();
1315 TIntermTyped *typedNode;
1316 int instanceSize = 0;
1317 unsigned int index = 0;
1318 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
1319
1320 for ( index = 0; index < fields->size(); ++index) {
1321 if ((*fields)[index].type->getFieldName() == identifier) {
1322 break;
1323 } else {
1324 instanceSize += (*fields)[index].type->getObjectSize();
1325 }
1326 }
1327
1328 if (tempConstantNode) {
1329 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
1330
1331 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1332 } else {
1333 error(line, "Cannot offset into the structure", "Error");
1334 recover();
1335
1336 return 0;
1337 }
1338
1339 return typedNode;
1340}
1341
Nicolas Capens7d626792015-02-17 17:58:31 -05001342TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
1343{
1344 TLayoutQualifier qualifier;
1345
1346 qualifier.location = -1;
1347
1348 if (qualifierType == "location")
1349 {
1350 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
1351 recover();
1352 }
1353 else
1354 {
1355 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
1356 recover();
1357 }
1358
1359 return qualifier;
1360}
1361
1362TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
1363{
1364 TLayoutQualifier qualifier;
1365
1366 qualifier.location = -1;
1367
1368 if (qualifierType != "location")
1369 {
1370 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
1371 recover();
1372 }
1373 else
1374 {
1375 // must check that location is non-negative
1376 if (intValue < 0)
1377 {
1378 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
1379 recover();
1380 }
1381 else
1382 {
1383 qualifier.location = intValue;
1384 }
1385 }
1386
1387 return qualifier;
1388}
1389
1390TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
1391{
1392 TLayoutQualifier joinedQualifier = leftQualifier;
1393
1394 if (rightQualifier.location != -1)
1395 {
1396 joinedQualifier.location = rightQualifier.location;
1397 }
1398
1399 return joinedQualifier;
1400}
1401
Alexis Hetu55a2cbc2015-04-16 10:49:45 -04001402
1403TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
1404 const TSourceLoc &storageLoc, TQualifier storageQualifier)
1405{
1406 TQualifier mergedQualifier = EvqSmoothIn;
1407
1408 if(storageQualifier == EvqVaryingIn) {
1409 if(interpolationQualifier == EvqSmooth)
1410 mergedQualifier = EvqSmoothIn;
1411 else if(interpolationQualifier == EvqFlat)
1412 mergedQualifier = EvqFlatIn;
1413 else UNREACHABLE();
1414 }
1415 else if(storageQualifier == EvqCentroidIn) {
1416 if(interpolationQualifier == EvqSmooth)
1417 mergedQualifier = EvqCentroidIn;
1418 else if(interpolationQualifier == EvqFlat)
1419 mergedQualifier = EvqFlatIn;
1420 else UNREACHABLE();
1421 }
1422 else if(storageQualifier == EvqVaryingOut) {
1423 if(interpolationQualifier == EvqSmooth)
1424 mergedQualifier = EvqSmoothOut;
1425 else if(interpolationQualifier == EvqFlat)
1426 mergedQualifier = EvqFlatOut;
1427 else UNREACHABLE();
1428 }
1429 else if(storageQualifier == EvqCentroidOut) {
1430 if(interpolationQualifier == EvqSmooth)
1431 mergedQualifier = EvqCentroidOut;
1432 else if(interpolationQualifier == EvqFlat)
1433 mergedQualifier = EvqFlatOut;
1434 else UNREACHABLE();
1435 }
1436 else {
1437 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString(interpolationQualifier));
1438 recover();
1439
1440 mergedQualifier = storageQualifier;
1441 }
1442
1443 TPublicType type;
1444 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
1445 return type;
1446}
1447
John Bauman66b8ab22014-05-06 15:57:45 -04001448bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1449{
1450 ++structNestingLevel;
1451
1452 // Embedded structure definitions are not supported per GLSL ES spec.
1453 // They aren't allowed in GLSL either, but we need to detect this here
1454 // so we don't rely on the GLSL compiler to catch it.
1455 if (structNestingLevel > 1) {
1456 error(line, "", "Embedded struct definitions are not allowed");
1457 return true;
1458 }
1459
1460 return false;
1461}
1462
1463void TParseContext::exitStructDeclaration()
1464{
1465 --structNestingLevel;
1466}
1467
John Bauman66b8ab22014-05-06 15:57:45 -04001468//
1469// Parse an array of strings using yyparse.
1470//
1471// Returns 0 for success.
1472//
1473int PaParseStrings(int count, const char* const string[], const int length[],
1474 TParseContext* context) {
1475 if ((count == 0) || (string == NULL))
1476 return 1;
1477
1478 if (glslang_initialize(context))
1479 return 1;
1480
1481 int error = glslang_scan(count, string, length, context);
1482 if (!error)
1483 error = glslang_parse(context);
1484
1485 glslang_finalize(context);
1486
1487 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
1488}
1489
1490
1491