blob: 21a3fe7471b0c549816b0a6bf9a35ba0d4ecb65e [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
7#include "compiler/ParseHelper.h"
8
9#include <stdarg.h>
10#include <stdio.h>
11
12#include "compiler/glslang.h"
13#include "compiler/preprocessor/SourceLocation.h"
14
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]) {
41 case 'x':
42 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
45 case 'r':
46 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
53 case 'y':
54 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
57 case 'g':
58 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
65 case 'z':
66 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
69 case 'b':
70 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
77
78 case 'w':
79 fields.offsets[i] = 3;
80 fieldSet[i] = exyzw;
81 break;
82 case 'a':
83 fields.offsets[i] = 3;
84 fieldSet[i] = ergba;
85 break;
86 case 'q':
87 fields.offsets[i] = 3;
88 fieldSet[i] = estpq;
89 break;
90 default:
91 error(line, "illegal vector field selection", compString.c_str());
92 return false;
93 }
94 }
95
96 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
98 error(line, "vector field selection out of range", compString.c_str());
99 return false;
100 }
101
102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
105 return false;
106 }
107 }
108 }
109
110 return true;
111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
118bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
119{
120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
124
125 if (compString.size() != 2) {
126 error(line, "illegal length of matrix field selection", compString.c_str());
127 return false;
128 }
129
130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
132 error(line, "illegal matrix field selection", compString.c_str());
133 return false;
134 }
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
139 error(line, "illegal matrix field selection", compString.c_str());
140 return false;
141 }
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
144 } else {
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
147 error(line, "illegal matrix field selection", compString.c_str());
148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
153
154 if (fields.row >= matSize || fields.col >= matSize) {
155 error(line, "matrix field selection out of range", compString.c_str());
156 return false;
157 }
158
159 return true;
160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
178void TParseContext::error(TSourceLoc loc,
179 const char* reason, const char* token,
180 const char* extraInfo)
181{
182 pp::SourceLocation srcLoc;
183 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
184 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
185 srcLoc, reason, token, extraInfo);
186
187}
188
189void TParseContext::warning(TSourceLoc loc,
190 const char* reason, const char* token,
191 const char* extraInfo) {
192 pp::SourceLocation srcLoc;
193 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
194 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
195 srcLoc, reason, token, extraInfo);
196}
197
198void TParseContext::trace(const char* str)
199{
200 diagnostics.writeDebug(str);
201}
202
203//
204// Same error message for all places assignments don't work.
205//
206void TParseContext::assignError(int line, const char* op, TString left, TString right)
207{
208 std::stringstream extraInfoStream;
209 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
210 std::string extraInfo = extraInfoStream.str();
211 error(line, "", op, extraInfo.c_str());
212}
213
214//
215// Same error message for all places unary operations don't work.
216//
217void TParseContext::unaryOpError(int line, const char* op, TString operand)
218{
219 std::stringstream extraInfoStream;
220 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
221 << " (or there is no acceptable conversion)";
222 std::string extraInfo = extraInfoStream.str();
223 error(line, " wrong operand type", op, extraInfo.c_str());
224}
225
226//
227// Same error message for all binary operations don't work.
228//
229void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
230{
231 std::stringstream extraInfoStream;
232 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
233 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
234 std::string extraInfo = extraInfoStream.str();
235 error(line, " wrong operand types ", op, extraInfo.c_str());
236}
237
238bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
239 if (!checksPrecisionErrors)
240 return false;
241 switch( type ){
242 case EbtFloat:
243 if( precision == EbpUndefined ){
244 error( line, "No precision specified for (float)", "" );
245 return true;
246 }
247 break;
248 case EbtInt:
249 if( precision == EbpUndefined ){
250 error( line, "No precision specified (int)", "" );
251 return true;
252 }
253 break;
254 default:
255 return false;
256 }
257 return false;
258}
259
260//
261// Both test and if necessary, spit out an error, to see if the node is really
262// an l-value that can be operated on this way.
263//
264// Returns true if the was an error.
265//
266bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
267{
268 TIntermSymbol* symNode = node->getAsSymbolNode();
269 TIntermBinary* binaryNode = node->getAsBinaryNode();
270
271 if (binaryNode) {
272 bool errorReturn;
273
274 switch(binaryNode->getOp()) {
275 case EOpIndexDirect:
276 case EOpIndexIndirect:
277 case EOpIndexDirectStruct:
278 return lValueErrorCheck(line, op, binaryNode->getLeft());
279 case EOpVectorSwizzle:
280 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
281 if (!errorReturn) {
282 int offset[4] = {0,0,0,0};
283
284 TIntermTyped* rightNode = binaryNode->getRight();
285 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
286
287 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
288 p != aggrNode->getSequence().end(); p++) {
289 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
290 offset[value]++;
291 if (offset[value] > 1) {
292 error(line, " l-value of swizzle cannot have duplicate components", op);
293
294 return true;
295 }
296 }
297 }
298
299 return errorReturn;
300 default:
301 break;
302 }
303 error(line, " l-value required", op);
304
305 return true;
306 }
307
308
309 const char* symbol = 0;
310 if (symNode != 0)
311 symbol = symNode->getSymbol().c_str();
312
313 const char* message = 0;
314 switch (node->getQualifier()) {
315 case EvqConst: message = "can't modify a const"; break;
316 case EvqConstReadOnly: message = "can't modify a const"; break;
317 case EvqAttribute: message = "can't modify an attribute"; break;
318 case EvqUniform: message = "can't modify a uniform"; break;
319 case EvqVaryingIn: message = "can't modify a varying"; break;
320 case EvqInput: message = "can't modify an input"; break;
321 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
322 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
323 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
324 default:
325
326 //
327 // Type that can't be written to?
328 //
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400329 if(IsSampler(node->getBasicType()))
330 {
John Bauman66b8ab22014-05-06 15:57:45 -0400331 message = "can't modify a sampler";
Nicolas Capense9c5e4f2014-05-28 22:46:43 -0400332 }
333 else if(node->getBasicType() == EbtVoid)
334 {
John Bauman66b8ab22014-05-06 15:57:45 -0400335 message = "can't modify void";
John Bauman66b8ab22014-05-06 15:57:45 -0400336 }
337 }
338
339 if (message == 0 && binaryNode == 0 && symNode == 0) {
340 error(line, " l-value required", op);
341
342 return true;
343 }
344
345
346 //
347 // Everything else is okay, no error.
348 //
349 if (message == 0)
350 return false;
351
352 //
353 // If we get here, we have an error and a message.
354 //
355 if (symNode) {
356 std::stringstream extraInfoStream;
357 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
358 std::string extraInfo = extraInfoStream.str();
359 error(line, " l-value required", op, extraInfo.c_str());
360 }
361 else {
362 std::stringstream extraInfoStream;
363 extraInfoStream << "(" << message << ")";
364 std::string extraInfo = extraInfoStream.str();
365 error(line, " l-value required", op, extraInfo.c_str());
366 }
367
368 return true;
369}
370
371//
372// Both test, and if necessary spit out an error, to see if the node is really
373// a constant.
374//
375// Returns true if the was an error.
376//
377bool TParseContext::constErrorCheck(TIntermTyped* node)
378{
379 if (node->getQualifier() == EvqConst)
380 return false;
381
382 error(node->getLine(), "constant expression required", "");
383
384 return true;
385}
386
387//
388// Both test, and if necessary spit out an error, to see if the node is really
389// an integer.
390//
391// Returns true if the was an error.
392//
393bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
394{
395 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
396 return false;
397
398 error(node->getLine(), "integer expression required", token);
399
400 return true;
401}
402
403//
404// Both test, and if necessary spit out an error, to see if we are currently
405// globally scoped.
406//
407// Returns true if the was an error.
408//
409bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
410{
411 if (global)
412 return false;
413
414 error(line, "only allowed at global scope", token);
415
416 return true;
417}
418
419//
420// For now, keep it simple: if it starts "gl_", it's reserved, independent
421// of scope. Except, if the symbol table is at the built-in push-level,
422// which is when we are parsing built-ins.
423// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
424// webgl shader.
425//
426// Returns true if there was an error.
427//
428bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
429{
430 static const char* reservedErrMsg = "reserved built-in name";
431 if (!symbolTable.atBuiltInLevel()) {
432 if (identifier.compare(0, 3, "gl_") == 0) {
433 error(line, reservedErrMsg, "gl_");
434 return true;
435 }
436 if (shaderSpec == SH_WEBGL_SPEC) {
437 if (identifier.compare(0, 6, "webgl_") == 0) {
438 error(line, reservedErrMsg, "webgl_");
439 return true;
440 }
441 if (identifier.compare(0, 7, "_webgl_") == 0) {
442 error(line, reservedErrMsg, "_webgl_");
443 return true;
444 }
445 }
446 if (identifier.find("__") != TString::npos) {
447 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
448 return true;
449 }
450 }
451
452 return false;
453}
454
455//
456// Make sure there is enough data provided to the constructor to build
457// something of the type of the constructor. Also returns the type of
458// the constructor.
459//
460// Returns true if there was an error in construction.
461//
462bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
463{
464 *type = function.getReturnType();
465
466 bool constructingMatrix = false;
467 switch(op) {
468 case EOpConstructMat2:
469 case EOpConstructMat3:
470 case EOpConstructMat4:
471 constructingMatrix = true;
472 break;
473 default:
474 break;
475 }
476
477 //
478 // Note: It's okay to have too many components available, but not okay to have unused
479 // arguments. 'full' will go to true when enough args have been seen. If we loop
480 // again, there is an extra argument, so 'overfull' will become true.
481 //
482
483 int size = 0;
484 bool constType = true;
485 bool full = false;
486 bool overFull = false;
487 bool matrixInMatrix = false;
488 bool arrayArg = false;
489 for (int i = 0; i < function.getParamCount(); ++i) {
490 const TParameter& param = function.getParam(i);
491 size += param.type->getObjectSize();
492
493 if (constructingMatrix && param.type->isMatrix())
494 matrixInMatrix = true;
495 if (full)
496 overFull = true;
497 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
498 full = true;
499 if (param.type->getQualifier() != EvqConst)
500 constType = false;
501 if (param.type->isArray())
502 arrayArg = true;
503 }
504
505 if (constType)
506 type->setQualifier(EvqConst);
507
508 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
509 error(line, "array constructor needs one argument per array element", "constructor");
510 return true;
511 }
512
513 if (arrayArg && op != EOpConstructStruct) {
514 error(line, "constructing from a non-dereferenced array", "constructor");
515 return true;
516 }
517
518 if (matrixInMatrix && !type->isArray()) {
519 if (function.getParamCount() != 1) {
520 error(line, "constructing matrix from matrix can only take one argument", "constructor");
521 return true;
522 }
523 }
524
525 if (overFull) {
526 error(line, "too many arguments", "constructor");
527 return true;
528 }
529
530 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
531 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
532 return true;
533 }
534
535 if (!type->isMatrix() || !matrixInMatrix) {
536 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
537 (op == EOpConstructStruct && size < type->getObjectSize())) {
538 error(line, "not enough data provided for construction", "constructor");
539 return true;
540 }
541 }
542
543 TIntermTyped *typed = node ? node->getAsTyped() : 0;
544 if (typed == 0) {
545 error(line, "constructor argument does not have a type", "constructor");
546 return true;
547 }
548 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
549 error(line, "cannot convert a sampler", "constructor");
550 return true;
551 }
552 if (typed->getBasicType() == EbtVoid) {
553 error(line, "cannot convert a void", "constructor");
554 return true;
555 }
556
557 return false;
558}
559
560// This function checks to see if a void variable has been declared and raise an error message for such a case
561//
562// returns true in case of an error
563//
564bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
565{
566 if (pubType.type == EbtVoid) {
567 error(line, "illegal use of type 'void'", identifier.c_str());
568 return true;
569 }
570
571 return false;
572}
573
574// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
575//
576// returns true in case of an error
577//
578bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
579{
580 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
581 error(line, "boolean expression expected", "");
582 return true;
583 }
584
585 return false;
586}
587
588// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
589//
590// returns true in case of an error
591//
592bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
593{
594 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
595 error(line, "boolean expression expected", "");
596 return true;
597 }
598
599 return false;
600}
601
602bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
603{
604 if (pType.type == EbtStruct) {
605 if (containsSampler(*pType.userDef)) {
606 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
607
608 return true;
609 }
610
611 return false;
612 } else if (IsSampler(pType.type)) {
613 error(line, reason, getBasicString(pType.type));
614
615 return true;
616 }
617
618 return false;
619}
620
621bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
622{
623 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
624 pType.type == EbtStruct) {
625 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
626
627 return true;
628 }
629
630 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
631 return true;
632
633 return false;
634}
635
636bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
637{
638 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
639 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
640 error(line, "samplers cannot be output parameters", type.getBasicString());
641 return true;
642 }
643
644 return false;
645}
646
647bool TParseContext::containsSampler(TType& type)
648{
649 if (IsSampler(type.getBasicType()))
650 return true;
651
652 if (type.getBasicType() == EbtStruct) {
653 TTypeList& structure = *type.getStruct();
654 for (unsigned int i = 0; i < structure.size(); ++i) {
655 if (containsSampler(*structure[i].type))
656 return true;
657 }
658 }
659
660 return false;
661}
662
663//
664// Do size checking for an array type's size.
665//
666// Returns true if there was an error.
667//
668bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
669{
670 TIntermConstantUnion* constant = expr->getAsConstantUnion();
671 if (constant == 0 || constant->getBasicType() != EbtInt) {
672 error(line, "array size must be a constant integer expression", "");
673 return true;
674 }
675
676 size = constant->getUnionArrayPointer()->getIConst();
677
678 if (size <= 0) {
679 error(line, "array size must be a positive integer", "");
680 size = 1;
681 return true;
682 }
683
684 return false;
685}
686
687//
688// See if this qualifier can be an array.
689//
690// Returns true if there is an error.
691//
692bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
693{
694 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
695 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
696 return true;
697 }
698
699 return false;
700}
701
702//
703// See if this type can be an array.
704//
705// Returns true if there is an error.
706//
707bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
708{
709 //
710 // Can the type be an array?
711 //
712 if (type.array) {
713 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
714 return true;
715 }
716
717 return false;
718}
719
720//
721// Do all the semantic checking for declaring an array, with and
722// without a size, and make the right changes to the symbol table.
723//
724// size == 0 means no specified size.
725//
726// Returns true if there was an error.
727//
728bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
729{
730 //
731 // Don't check for reserved word use until after we know it's not in the symbol table,
732 // because reserved arrays can be redeclared.
733 //
734
735 bool builtIn = false;
736 bool sameScope = false;
737 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
738 if (symbol == 0 || !sameScope) {
739 if (reservedErrorCheck(line, identifier))
740 return true;
741
742 variable = new TVariable(&identifier, TType(type));
743
744 if (type.arraySize)
745 variable->getType().setArraySize(type.arraySize);
746
747 if (! symbolTable.insert(*variable)) {
748 delete variable;
749 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
750 return true;
751 }
752 } else {
753 if (! symbol->isVariable()) {
754 error(line, "variable expected", identifier.c_str());
755 return true;
756 }
757
758 variable = static_cast<TVariable*>(symbol);
759 if (! variable->getType().isArray()) {
760 error(line, "redeclaring non-array as array", identifier.c_str());
761 return true;
762 }
763 if (variable->getType().getArraySize() > 0) {
764 error(line, "redeclaration of array with size", identifier.c_str());
765 return true;
766 }
767
768 if (! variable->getType().sameElementType(TType(type))) {
769 error(line, "redeclaration of array with a different type", identifier.c_str());
770 return true;
771 }
772
773 TType* t = variable->getArrayInformationType();
774 while (t != 0) {
775 if (t->getMaxArraySize() > type.arraySize) {
776 error(line, "higher index value already used for the array", identifier.c_str());
777 return true;
778 }
779 t->setArraySize(type.arraySize);
780 t = t->getArrayInformationType();
781 }
782
783 if (type.arraySize)
784 variable->getType().setArraySize(type.arraySize);
785 }
786
787 if (voidErrorCheck(line, identifier, type))
788 return true;
789
790 return false;
791}
792
793bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
794{
795 bool builtIn = false;
796 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
797 if (symbol == 0) {
798 error(line, " undeclared identifier", node->getSymbol().c_str());
799 return true;
800 }
801 TVariable* variable = static_cast<TVariable*>(symbol);
802
803 type->setArrayInformationType(variable->getArrayInformationType());
804 variable->updateArrayInformationType(type);
805
806 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
807 // its an error
808 if (node->getSymbol() == "gl_FragData") {
809 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
810 ASSERT(fragData);
811
812 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
813 if (fragDataValue <= size) {
814 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
815 return true;
816 }
817 }
818
819 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
820 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
821 if (!updateFlag)
822 return false;
823
824 size++;
825 variable->getType().setMaxArraySize(size);
826 type->setMaxArraySize(size);
827 TType* tt = type;
828
829 while(tt->getArrayInformationType() != 0) {
830 tt = tt->getArrayInformationType();
831 tt->setMaxArraySize(size);
832 }
833
834 return false;
835}
836
837//
838// Enforce non-initializer type/qualifier rules.
839//
840// Returns true if there was an error.
841//
842bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
843{
844 if (type.qualifier == EvqConst)
845 {
846 // Make the qualifier make sense.
847 type.qualifier = EvqTemporary;
848
849 if (array)
850 {
851 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
852 }
853 else if (type.isStructureContainingArrays())
854 {
855 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
856 }
857 else
858 {
859 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
860 }
861
862 return true;
863 }
864
865 return false;
866}
867
868//
869// Do semantic checking for a variable declaration that has no initializer,
870// and update the symbol table.
871//
872// Returns true if there was an error.
873//
874bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
875{
876 if (reservedErrorCheck(line, identifier))
877 recover();
878
879 variable = new TVariable(&identifier, TType(type));
880
881 if (! symbolTable.insert(*variable)) {
882 error(line, "redefinition", variable->getName().c_str());
883 delete variable;
884 variable = 0;
885 return true;
886 }
887
888 if (voidErrorCheck(line, identifier, type))
889 return true;
890
891 return false;
892}
893
894bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
895{
896 if (qualifier != EvqConst && qualifier != EvqTemporary) {
897 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
898 return true;
899 }
900 if (qualifier == EvqConst && paramQualifier != EvqIn) {
901 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
902 return true;
903 }
904
905 if (qualifier == EvqConst)
906 type->setQualifier(EvqConstReadOnly);
907 else
908 type->setQualifier(paramQualifier);
909
910 return false;
911}
912
913bool TParseContext::extensionErrorCheck(int line, const TString& extension)
914{
915 const TExtensionBehavior& extBehavior = extensionBehavior();
916 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
917 if (iter == extBehavior.end()) {
918 error(line, "extension", extension.c_str(), "is not supported");
919 return true;
920 }
921 // In GLSL ES, an extension's default behavior is "disable".
922 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
923 error(line, "extension", extension.c_str(), "is disabled");
924 return true;
925 }
926 if (iter->second == EBhWarn) {
927 warning(line, "extension", extension.c_str(), "is being used");
928 return false;
929 }
930
931 return false;
932}
933
934bool TParseContext::supportsExtension(const char* extension)
935{
936 const TExtensionBehavior& extbehavior = extensionBehavior();
937 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
938 return (iter != extbehavior.end());
939}
940
941void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
942{
943 pp::SourceLocation loc;
944 DecodeSourceLoc(line, &loc.file, &loc.line);
945 directiveHandler.handleExtension(loc, extName, behavior);
946}
947
948void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
949{
950 pp::SourceLocation loc;
951 DecodeSourceLoc(line, &loc.file, &loc.line);
952 directiveHandler.handlePragma(loc, name, value);
953}
954
955/////////////////////////////////////////////////////////////////////////////////
956//
957// Non-Errors.
958//
959/////////////////////////////////////////////////////////////////////////////////
960
961//
962// Look up a function name in the symbol table, and make sure it is a function.
963//
964// Return the function symbol if found, otherwise 0.
965//
966const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
967{
968 // First find by unmangled name to check whether the function name has been
969 // hidden by a variable name or struct typename.
970 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
971 if (symbol == 0) {
972 symbol = symbolTable.find(call->getMangledName(), builtIn);
973 }
974
975 if (symbol == 0) {
976 error(line, "no matching overloaded function found", call->getName().c_str());
977 return 0;
978 }
979
980 if (!symbol->isFunction()) {
981 error(line, "function name expected", call->getName().c_str());
982 return 0;
983 }
984
985 return static_cast<const TFunction*>(symbol);
986}
987
988//
989// Initializers show up in several places in the grammar. Have one set of
990// code to handle them here.
991//
992bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
993 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
994{
995 TType type = TType(pType);
996
997 if (variable == 0) {
998 if (reservedErrorCheck(line, identifier))
999 return true;
1000
1001 if (voidErrorCheck(line, identifier, pType))
1002 return true;
1003
1004 //
1005 // add variable to symbol table
1006 //
1007 variable = new TVariable(&identifier, type);
1008 if (! symbolTable.insert(*variable)) {
1009 error(line, "redefinition", variable->getName().c_str());
1010 return true;
1011 // don't delete variable, it's used by error recovery, and the pool
1012 // pop will take care of the memory
1013 }
1014 }
1015
1016 //
1017 // identifier must be of type constant, a global, or a temporary
1018 //
1019 TQualifier qualifier = variable->getType().getQualifier();
1020 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1021 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1022 return true;
1023 }
1024 //
1025 // test for and propagate constant
1026 //
1027
1028 if (qualifier == EvqConst) {
1029 if (qualifier != initializer->getType().getQualifier()) {
1030 std::stringstream extraInfoStream;
1031 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1032 std::string extraInfo = extraInfoStream.str();
1033 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1034 variable->getType().setQualifier(EvqTemporary);
1035 return true;
1036 }
1037 if (type != initializer->getType()) {
1038 error(line, " non-matching types for const initializer ",
1039 variable->getType().getQualifierString());
1040 variable->getType().setQualifier(EvqTemporary);
1041 return true;
1042 }
1043 if (initializer->getAsConstantUnion()) {
1044 ConstantUnion* unionArray = variable->getConstPointer();
1045
1046 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1047 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1048 } else {
1049 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1050 }
1051 } else if (initializer->getAsSymbolNode()) {
1052 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1053 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1054
1055 ConstantUnion* constArray = tVar->getConstPointer();
1056 variable->shareConstPointer(constArray);
1057 } else {
1058 std::stringstream extraInfoStream;
1059 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1060 std::string extraInfo = extraInfoStream.str();
1061 error(line, " cannot assign to", "=", extraInfo.c_str());
1062 variable->getType().setQualifier(EvqTemporary);
1063 return true;
1064 }
1065 }
1066
1067 if (qualifier != EvqConst) {
1068 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1069 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1070 if (intermNode == 0) {
1071 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1072 return true;
1073 }
1074 } else
1075 intermNode = 0;
1076
1077 return false;
1078}
1079
1080bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1081{
1082 ASSERT(aggrNode != NULL);
1083 if (!aggrNode->isConstructor())
1084 return false;
1085
1086 bool allConstant = true;
1087
1088 // check if all the child nodes are constants so that they can be inserted into
1089 // the parent node
1090 TIntermSequence &sequence = aggrNode->getSequence() ;
1091 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1092 if (!(*p)->getAsTyped()->getAsConstantUnion())
1093 return false;
1094 }
1095
1096 return allConstant;
1097}
1098
1099// This function is used to test for the correctness of the parameters passed to various constructor functions
1100// and also convert them to the right datatype if it is allowed and required.
1101//
1102// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1103//
1104TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1105{
1106 if (node == 0)
1107 return 0;
1108
1109 TIntermAggregate* aggrNode = node->getAsAggregate();
1110
1111 TTypeList::const_iterator memberTypes;
1112 if (op == EOpConstructStruct)
1113 memberTypes = type->getStruct()->begin();
1114
1115 TType elementType = *type;
1116 if (type->isArray())
1117 elementType.clearArrayness();
1118
1119 bool singleArg;
1120 if (aggrNode) {
1121 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1122 singleArg = true;
1123 else
1124 singleArg = false;
1125 } else
1126 singleArg = true;
1127
1128 TIntermTyped *newNode;
1129 if (singleArg) {
1130 // If structure constructor or array constructor is being called
1131 // for only one parameter inside the structure, we need to call constructStruct function once.
1132 if (type->isArray())
1133 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1134 else if (op == EOpConstructStruct)
1135 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1136 else
1137 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
1138
1139 if (newNode && newNode->getAsAggregate()) {
1140 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1141 if (constConstructor)
1142 return constConstructor;
1143 }
1144
1145 return newNode;
1146 }
1147
1148 //
1149 // Handle list of arguments.
1150 //
1151 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1152 // if the structure constructor contains more than one parameter, then construct
1153 // each parameter
1154
1155 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1156
1157 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1158 // to the right type if possible (and allowed).
1159 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1160
1161 for (TIntermSequence::iterator p = sequenceVector.begin();
1162 p != sequenceVector.end(); p++, paramCount++) {
1163 if (type->isArray())
1164 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1165 else if (op == EOpConstructStruct)
1166 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1167 else
1168 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1169
1170 if (newNode) {
1171 *p = newNode;
1172 }
1173 }
1174
1175 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1176 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1177 if (constConstructor)
1178 return constConstructor;
1179
1180 return constructor;
1181}
1182
1183TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1184{
1185 bool canBeFolded = areAllChildConst(aggrNode);
1186 aggrNode->setType(type);
1187 if (canBeFolded) {
1188 bool returnVal = false;
1189 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1190 if (aggrNode->getSequence().size() == 1) {
John Baumand4ae8632014-05-06 16:18:33 -04001191 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
John Bauman66b8ab22014-05-06 15:57:45 -04001192 }
1193 else {
John Baumand4ae8632014-05-06 16:18:33 -04001194 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
John Bauman66b8ab22014-05-06 15:57:45 -04001195 }
1196 if (returnVal)
1197 return 0;
1198
1199 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1200 }
1201
1202 return 0;
1203}
1204
1205// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1206// for the parameter to the constructor (passed to this function). Essentially, it converts
1207// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1208// float, then float is converted to int.
1209//
1210// Returns 0 for an error or the constructed node.
1211//
1212TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1213{
1214 TIntermTyped* newNode;
1215 TOperator basicOp;
1216
1217 //
1218 // First, convert types as needed.
1219 //
1220 switch (op) {
1221 case EOpConstructVec2:
1222 case EOpConstructVec3:
1223 case EOpConstructVec4:
1224 case EOpConstructMat2:
1225 case EOpConstructMat3:
1226 case EOpConstructMat4:
1227 case EOpConstructFloat:
1228 basicOp = EOpConstructFloat;
1229 break;
1230
1231 case EOpConstructIVec2:
1232 case EOpConstructIVec3:
1233 case EOpConstructIVec4:
1234 case EOpConstructInt:
1235 basicOp = EOpConstructInt;
1236 break;
1237
1238 case EOpConstructBVec2:
1239 case EOpConstructBVec3:
1240 case EOpConstructBVec4:
1241 case EOpConstructBool:
1242 basicOp = EOpConstructBool;
1243 break;
1244
1245 default:
1246 error(line, "unsupported construction", "");
1247 recover();
1248
1249 return 0;
1250 }
John Baumand4ae8632014-05-06 16:18:33 -04001251 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001252 if (newNode == 0) {
1253 error(line, "can't convert", "constructor");
1254 return 0;
1255 }
1256
1257 //
1258 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1259 //
1260
1261 // Otherwise, skip out early.
1262 if (subset || (newNode != node && newNode->getType() == *type))
1263 return newNode;
1264
1265 // setAggregateOperator will insert a new node for the constructor, as needed.
1266 return intermediate.setAggregateOperator(newNode, op, line);
1267}
1268
1269// This function tests for the type of the parameters to the structures constructors. Raises
1270// an error message if the expected type does not match the parameter passed to the constructor.
1271//
1272// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1273//
1274TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1275{
1276 if (*type == node->getAsTyped()->getType()) {
1277 if (subset)
1278 return node->getAsTyped();
1279 else
1280 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1281 } else {
1282 std::stringstream extraInfoStream;
1283 extraInfoStream << "cannot convert parameter " << paramCount
1284 << " from '" << node->getAsTyped()->getType().getBasicString()
1285 << "' to '" << type->getBasicString() << "'";
1286 std::string extraInfo = extraInfoStream.str();
1287 error(line, "", "constructor", extraInfo.c_str());
1288 recover();
1289 }
1290
1291 return 0;
1292}
1293
1294//
1295// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1296// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1297// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1298// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1299// a constant matrix.
1300//
1301TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1302{
1303 TIntermTyped* typedNode;
1304 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1305
1306 ConstantUnion *unionArray;
1307 if (tempConstantNode) {
1308 unionArray = tempConstantNode->getUnionArrayPointer();
1309 ASSERT(unionArray);
1310
1311 if (!unionArray) {
1312 return node;
1313 }
1314 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1315 error(line, "Cannot offset into the vector", "Error");
1316 recover();
1317
1318 return 0;
1319 }
1320
1321 ConstantUnion* constArray = new ConstantUnion[fields.num];
1322
1323 for (int i = 0; i < fields.num; i++) {
1324 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1325 std::stringstream extraInfoStream;
1326 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1327 std::string extraInfo = extraInfoStream.str();
1328 error(line, "", "[", extraInfo.c_str());
1329 recover();
1330 fields.offsets[i] = 0;
1331 }
1332
1333 constArray[i] = unionArray[fields.offsets[i]];
1334
1335 }
1336 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1337 return typedNode;
1338}
1339
1340//
1341// This function returns the column being accessed from a constant matrix. The values are retrieved from
1342// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1343// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1344// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1345//
1346TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1347{
1348 TIntermTyped* typedNode;
1349 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1350
1351 if (index >= node->getType().getNominalSize()) {
1352 std::stringstream extraInfoStream;
1353 extraInfoStream << "matrix field selection out of range '" << index << "'";
1354 std::string extraInfo = extraInfoStream.str();
1355 error(line, "", "[", extraInfo.c_str());
1356 recover();
1357 index = 0;
1358 }
1359
1360 if (tempConstantNode) {
1361 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1362 int size = tempConstantNode->getType().getNominalSize();
1363 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1364 } else {
1365 error(line, "Cannot offset into the matrix", "Error");
1366 recover();
1367
1368 return 0;
1369 }
1370
1371 return typedNode;
1372}
1373
1374
1375//
1376// This function returns an element of an array accessed from a constant array. The values are retrieved from
1377// the symbol table and parse-tree is built for the type of the element. The input
1378// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1379// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1380//
1381TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1382{
1383 TIntermTyped* typedNode;
1384 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1385 TType arrayElementType = node->getType();
1386 arrayElementType.clearArrayness();
1387
1388 if (index >= node->getType().getArraySize()) {
1389 std::stringstream extraInfoStream;
1390 extraInfoStream << "array field selection out of range '" << index << "'";
1391 std::string extraInfo = extraInfoStream.str();
1392 error(line, "", "[", extraInfo.c_str());
1393 recover();
1394 index = 0;
1395 }
1396
1397 int arrayElementSize = arrayElementType.getObjectSize();
1398
1399 if (tempConstantNode) {
1400 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1401 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1402 } else {
1403 error(line, "Cannot offset into the array", "Error");
1404 recover();
1405
1406 return 0;
1407 }
1408
1409 return typedNode;
1410}
1411
1412
1413//
1414// This function returns the value of a particular field inside a constant structure from the symbol table.
1415// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1416// function and returns the parse-tree with the values of the embedded/nested struct.
1417//
1418TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1419{
1420 const TTypeList* fields = node->getType().getStruct();
1421 TIntermTyped *typedNode;
1422 int instanceSize = 0;
1423 unsigned int index = 0;
1424 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
1425
1426 for ( index = 0; index < fields->size(); ++index) {
1427 if ((*fields)[index].type->getFieldName() == identifier) {
1428 break;
1429 } else {
1430 instanceSize += (*fields)[index].type->getObjectSize();
1431 }
1432 }
1433
1434 if (tempConstantNode) {
1435 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
1436
1437 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1438 } else {
1439 error(line, "Cannot offset into the structure", "Error");
1440 recover();
1441
1442 return 0;
1443 }
1444
1445 return typedNode;
1446}
1447
1448bool 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
1468namespace {
1469
1470const int kWebGLMaxStructNesting = 4;
1471
1472} // namespace
1473
1474bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1475{
1476 if (shaderSpec != SH_WEBGL_SPEC) {
1477 return false;
1478 }
1479
1480 if (fieldType.getBasicType() != EbtStruct) {
1481 return false;
1482 }
1483
1484 // We're already inside a structure definition at this point, so add
1485 // one to the field's struct nesting.
1486 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
1487 std::stringstream extraInfoStream;
1488 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
1489 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
1490 std::string extraInfo = extraInfoStream.str();
1491 error(line, "", "", extraInfo.c_str());
1492 return true;
1493 }
1494
1495 return false;
1496}
1497
1498//
1499// Parse an array of strings using yyparse.
1500//
1501// Returns 0 for success.
1502//
1503int PaParseStrings(int count, const char* const string[], const int length[],
1504 TParseContext* context) {
1505 if ((count == 0) || (string == NULL))
1506 return 1;
1507
1508 if (glslang_initialize(context))
1509 return 1;
1510
1511 int error = glslang_scan(count, string, length, context);
1512 if (!error)
1513 error = glslang_parse(context);
1514
1515 glslang_finalize(context);
1516
1517 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
1518}
1519
1520
1521