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