blob: 39aa7e320ef85732c634b9003843d6d9bdda2f5f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/ParseHelper.h"
8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000012#include "compiler/glslang.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
alokp@chromium.org8b851c62012-06-15 16:25:11 +000015///////////////////////////////////////////////////////////////////////
16//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017// 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//
Jamie Madill075edd82013-07-08 13:30:19 -040025bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000027 fields.num = (int) compString.size();
28 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000029 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 return false;
31 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 enum {
34 exyzw,
35 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000036 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000037 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 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:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000091 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000092 return false;
93 }
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000096 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 return false;
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 return false;
106 }
107 }
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
Jamie Madill075edd82013-07-08 13:30:19 -0400118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000126 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 return false;
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000132 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 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') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 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') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000154 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000155 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 return false;
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
Jamie Madill075edd82013-07-08 13:30:19 -0400178void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000180 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000182 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000185 diagnostics.writeInfo(pp::Diagnostics::ERROR,
186 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madill075edd82013-07-08 13:30:19 -0400190void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000191 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000192 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000193 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000196 diagnostics.writeInfo(pp::Diagnostics::WARNING,
197 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000198}
199
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000200void TParseContext::trace(const char* str)
201{
202 diagnostics.writeDebug(str);
203}
204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205//
206// Same error message for all places assignments don't work.
207//
Jamie Madill075edd82013-07-08 13:30:19 -0400208void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
216//
217// Same error message for all places unary operations don't work.
218//
Jamie Madill075edd82013-07-08 13:30:19 -0400219void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Same error message for all binary operations don't work.
230//
Jamie Madill075edd82013-07-08 13:30:19 -0400231void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
Jamie Madill075edd82013-07-08 13:30:19 -0400240bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000241 if (!checksPrecisionErrors)
242 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 switch( type ){
244 case EbtFloat:
245 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000246 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 return true;
248 }
249 break;
250 case EbtInt:
251 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253 return true;
254 }
255 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000256 default:
257 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000258 }
259 return false;
260}
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262//
263// Both test and if necessary, spit out an error, to see if the node is really
264// an l-value that can be operated on this way.
265//
266// Returns true if the was an error.
267//
Jamie Madill075edd82013-07-08 13:30:19 -0400268bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 if (binaryNode) {
274 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 switch(binaryNode->getOp()) {
277 case EOpIndexDirect:
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000280 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
284 if (!errorReturn) {
285 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
289
290 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
291 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 offset[value]++;
294 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 return true;
298 }
299 }
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return errorReturn;
303 default:
304 break;
305 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000306 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return true;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* symbol = 0;
313 if (symNode != 0)
314 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madillb120eac2013-06-12 14:08:13 -0400321 case EvqVertexInput: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 case EvqUniform: message = "can't modify a uniform"; break;
323 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
325 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
326 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
327 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000329 //
330 // Type that can't be written to?
331 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400332 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000333 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400334 }
335 if (IsSampler(node->getBasicType())) {
336 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 }
338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000341 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000343 return true;
344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 //
348 // Everything else is okay, no error.
349 //
350 if (message == 0)
351 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000353 //
354 // If we get here, we have an error and a message.
355 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000356 if (symNode) {
357 std::stringstream extraInfoStream;
358 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
359 std::string extraInfo = extraInfoStream.str();
360 error(line, " l-value required", op, extraInfo.c_str());
361 }
362 else {
363 std::stringstream extraInfoStream;
364 extraInfoStream << "(" << message << ")";
365 std::string extraInfo = extraInfoStream.str();
366 error(line, " l-value required", op, extraInfo.c_str());
367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370}
371
372//
373// Both test, and if necessary spit out an error, to see if the node is really
374// a constant.
375//
376// Returns true if the was an error.
377//
378bool TParseContext::constErrorCheck(TIntermTyped* node)
379{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000380 if (node->getQualifier() == EvqConst)
381 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000383 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000385 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386}
387
388//
389// Both test, and if necessary spit out an error, to see if the node is really
390// an integer.
391//
392// Returns true if the was an error.
393//
394bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
395{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000396 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000397 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000399 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402}
403
404//
405// Both test, and if necessary spit out an error, to see if we are currently
406// globally scoped.
407//
408// Returns true if the was an error.
409//
Jamie Madill075edd82013-07-08 13:30:19 -0400410bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000412 if (global)
413 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000415 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000417 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418}
419
420//
421// For now, keep it simple: if it starts "gl_", it's reserved, independent
422// of scope. Except, if the symbol table is at the built-in push-level,
423// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000424// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
425// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426//
427// Returns true if there was an error.
428//
Jamie Madill075edd82013-07-08 13:30:19 -0400429bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000431 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000433 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000434 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
436 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000437 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000438 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000439 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000440 return true;
441 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000442 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000443 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000444 return true;
445 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000446 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000447 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000448 return true;
449 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000450 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000453 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 }
455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458}
459
460//
461// Make sure there is enough data provided to the constructor to build
462// something of the type of the constructor. Also returns the type of
463// the constructor.
464//
465// Returns true if there was an error in construction.
466//
Jamie Madill075edd82013-07-08 13:30:19 -0400467bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 bool constructingMatrix = false;
472 switch(op) {
473 case EOpConstructMat2:
474 case EOpConstructMat3:
475 case EOpConstructMat4:
476 constructingMatrix = true;
477 break;
478 default:
479 break;
480 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 //
483 // Note: It's okay to have too many components available, but not okay to have unused
484 // arguments. 'full' will go to true when enough args have been seen. If we loop
485 // again, there is an extra argument, so 'overfull' will become true.
486 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
Jamie Madill94bf7f22013-07-08 13:31:15 -0400488 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 bool constType = true;
490 bool full = false;
491 bool overFull = false;
492 bool matrixInMatrix = false;
493 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000494 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000495 const TParameter& param = function.getParam(i);
496 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000498 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000499 matrixInMatrix = true;
500 if (full)
501 overFull = true;
502 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
503 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000504 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000506 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 arrayArg = true;
508 }
509
510 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000511 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000513 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000514 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000515 return true;
516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000519 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 return true;
521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000524 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000525 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000526 return true;
527 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000531 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 return true;
533 }
534
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000535 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000536 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 return true;
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000540 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000541 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
542 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000543 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000544 return true;
545 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000548 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000550 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 return true;
552 }
553 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000554 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 return true;
556 }
557 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 return true;
560 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563}
564
565// This function checks to see if a void variable has been declared and raise an error message for such a case
566//
567// returns true in case of an error
568//
Jamie Madill075edd82013-07-08 13:30:19 -0400569bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000572 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 return true;
574 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577}
578
579// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
580//
581// returns true in case of an error
582//
Jamie Madill075edd82013-07-08 13:30:19 -0400583bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000586 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 return true;
588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591}
592
593// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
594//
595// returns true in case of an error
596//
Jamie Madill075edd82013-07-08 13:30:19 -0400597bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000599 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000600 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 return true;
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605}
606
Jamie Madill075edd82013-07-08 13:30:19 -0400607bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 if (pType.type == EbtStruct) {
610 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000611 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612
613 return true;
614 }
615
616 return false;
617 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000618 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000620 return true;
621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
Jamie Madill075edd82013-07-08 13:30:19 -0400626bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627{
Jamie Madillb120eac2013-06-12 14:08:13 -0400628 switch (pType.qualifier)
629 {
630 case EvqVaryingIn:
631 case EvqVaryingOut:
632 case EvqAttribute:
633 case EvqVertexInput:
634 case EvqFragmentOutput:
635 if (pType.type == EbtStruct)
636 {
637 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
638 return true;
639 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
643 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646}
647
Jamie Madill075edd82013-07-08 13:30:19 -0400648bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400649{
650 if (pType.layoutQualifier.location != -1)
651 {
652 error(line, "location must only be specified for a single input or output variable", "location");
653 return true;
654 }
655
656 return false;
657}
658
Jamie Madill075edd82013-07-08 13:30:19 -0400659bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
662 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000663 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return true;
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670bool TParseContext::containsSampler(TType& type)
671{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 if (IsSampler(type.getBasicType()))
673 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000675 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 TTypeList& structure = *type.getStruct();
677 for (unsigned int i = 0; i < structure.size(); ++i) {
678 if (containsSampler(*structure[i].type))
679 return true;
680 }
681 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684}
685
686//
687// Do size checking for an array type's size.
688//
689// Returns true if there was an error.
690//
Jamie Madill075edd82013-07-08 13:30:19 -0400691bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000694
695 if (constant == 0 || !constant->isScalarInt())
696 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000697 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 return true;
699 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000701 if (constant->getBasicType() == EbtUInt)
702 {
703 unsigned int uintSize = constant->getUConst(0);
704 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
705 {
706 error(line, "array size too large", "");
707 size = 1;
708 return true;
709 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000711 size = static_cast<int>(uintSize);
712 }
713 else
714 {
715 size = constant->getIConst(0);
716
717 if (size <= 0)
718 {
719 error(line, "array size must be a positive integer", "");
720 size = 1;
721 return true;
722 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726}
727
728//
729// See if this qualifier can be an array.
730//
731// Returns true if there is an error.
732//
Jamie Madill075edd82013-07-08 13:30:19 -0400733bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734{
Jamie Madillb120eac2013-06-12 14:08:13 -0400735 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000736 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 return true;
738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
743//
744// See if this type can be an array.
745//
746// Returns true if there is an error.
747//
Jamie Madill075edd82013-07-08 13:30:19 -0400748bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 //
751 // Can the type be an array?
752 //
753 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000754 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 return true;
756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759}
760
761//
762// Do all the semantic checking for declaring an array, with and
763// without a size, and make the right changes to the symbol table.
764//
765// size == 0 means no specified size.
766//
767// Returns true if there was an error.
768//
Jamie Madill075edd82013-07-08 13:30:19 -0400769bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000771 //
772 // Don't check for reserved word use until after we know it's not in the symbol table,
773 // because reserved arrays can be redeclared.
774 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 bool builtIn = false;
777 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000778 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000779 if (symbol == 0 || !sameScope) {
780 if (reservedErrorCheck(line, identifier))
781 return true;
782
783 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000785 if (type.arraySize)
786 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000788 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000790 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 return true;
792 }
793 } else {
794 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000795 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 return true;
797 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 variable = static_cast<TVariable*>(symbol);
800 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000801 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000802 return true;
803 }
804 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000805 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 return true;
807 }
808
809 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000810 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return true;
812 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 if (type.arraySize)
815 variable->getType().setArraySize(type.arraySize);
816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 if (voidErrorCheck(line, identifier, type))
819 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822}
823
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824//
825// Enforce non-initializer type/qualifier rules.
826//
827// Returns true if there was an error.
828//
Jamie Madill075edd82013-07-08 13:30:19 -0400829bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000831 if (type.qualifier == EvqConst)
832 {
833 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000835
836 if (array)
837 {
838 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
839 }
840 else if (type.isStructureContainingArrays())
841 {
842 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
843 }
844 else
845 {
846 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
847 }
848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 return true;
850 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853}
854
855//
856// Do semantic checking for a variable declaration that has no initializer,
857// and update the symbol table.
858//
859// Returns true if there was an error.
860//
Jamie Madill075edd82013-07-08 13:30:19 -0400861bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 if (reservedErrorCheck(line, identifier))
864 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
zmo@google.comfd747b82011-04-23 01:30:07 +0000866 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000868 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000869 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000871 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return true;
873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 if (voidErrorCheck(line, identifier, type))
876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
Jamie Madill075edd82013-07-08 13:30:19 -0400881bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000884 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return true;
886 }
887 if (qualifier == EvqConst && paramQualifier != EvqIn) {
888 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
889 return true;
890 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000893 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000895 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
Jamie Madill075edd82013-07-08 13:30:19 -0400900bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000901{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000902 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000903 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
904 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000905 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 return true;
907 }
zmo@google.comf5450912011-09-09 01:37:19 +0000908 // In GLSL ES, an extension's default behavior is "disable".
909 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000910 error(line, "extension", extension.c_str(), "is disabled");
911 return true;
912 }
913 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000914 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000915 return false;
916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
Jamie Madill075edd82013-07-08 13:30:19 -0400921bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400922{
923 if (structQualifierErrorCheck(identifierLocation, publicType))
924 return true;
925
926 // check for layout qualifier issues
927 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
928
929 if (layoutQualifier.matrixPacking != EmpUnspecified)
930 {
931 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400932 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400933 }
934
935 if (layoutQualifier.blockStorage != EbsUnspecified)
936 {
937 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400938 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400939 }
940
941 if (publicType.qualifier != EvqVertexInput && publicType.qualifier != EvqFragmentOutput && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
942 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400943 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400944 }
945
946 return false;
947}
948
Jamie Madill075edd82013-07-08 13:30:19 -0400949bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400950{
951 if (layoutQualifier.location != -1)
952 {
953 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
954 return true;
955 }
956
957 return false;
958}
959
zmo@google.com09c323a2011-08-12 18:22:25 +0000960bool TParseContext::supportsExtension(const char* extension)
961{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000962 const TExtensionBehavior& extbehavior = extensionBehavior();
963 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
964 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000965}
966
Jamie Madill075edd82013-07-08 13:30:19 -0400967void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
968{
969 pp::SourceLocation srcLoc;
970 srcLoc.file = loc.first_file;
971 srcLoc.line = loc.first_line;
972 directiveHandler.handleExtension(srcLoc, extName, behavior);
973}
974
975void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
976{
977 pp::SourceLocation srcLoc;
978 srcLoc.file = loc.first_file;
979 srcLoc.line = loc.first_line;
980 directiveHandler.handlePragma(srcLoc, name, value);
981}
982
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983/////////////////////////////////////////////////////////////////////////////////
984//
985// Non-Errors.
986//
987/////////////////////////////////////////////////////////////////////////////////
988
989//
990// Look up a function name in the symbol table, and make sure it is a function.
991//
992// Return the function symbol if found, otherwise 0.
993//
Jamie Madill075edd82013-07-08 13:30:19 -0400994const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000996 // First find by unmangled name to check whether the function name has been
997 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000998 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +0000999 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001000 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001001 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002
alokp@chromium.org0a576182010-08-09 17:16:27 +00001003 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001004 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 return 0;
1006 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
alokp@chromium.org0a576182010-08-09 17:16:27 +00001008 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001009 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 return 0;
1011 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001012
1013 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014}
1015
1016//
1017// Initializers show up in several places in the grammar. Have one set of
1018// code to handle them here.
1019//
Jamie Madill075edd82013-07-08 13:30:19 -04001020bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001021 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001025 if (variable == 0) {
1026 if (reservedErrorCheck(line, identifier))
1027 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 if (voidErrorCheck(line, identifier, pType))
1030 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001032 //
1033 // add variable to symbol table
1034 //
1035 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001036 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001037 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 return true;
1039 // don't delete variable, it's used by error recovery, and the pool
1040 // pop will take care of the memory
1041 }
1042 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001044 //
1045 // identifier must be of type constant, a global, or a temporary
1046 //
1047 TQualifier qualifier = variable->getType().getQualifier();
1048 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001049 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 return true;
1051 }
1052 //
1053 // test for and propagate constant
1054 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001056 if (qualifier == EvqConst) {
1057 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001058 std::stringstream extraInfoStream;
1059 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1060 std::string extraInfo = extraInfoStream.str();
1061 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001062 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001063 return true;
1064 }
1065 if (type != initializer->getType()) {
1066 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001067 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001068 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 return true;
1070 }
1071 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001072 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001074 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001077 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 variable->shareConstPointer(constArray);
1079 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001080 std::stringstream extraInfoStream;
1081 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1082 std::string extraInfo = extraInfoStream.str();
1083 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001084 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 return true;
1086 }
1087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 if (qualifier != EvqConst) {
1090 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1091 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1092 if (intermNode == 0) {
1093 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1094 return true;
1095 }
1096 } else
1097 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100}
1101
1102bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1103{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001104 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001105 if (!aggrNode->isConstructor())
1106 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 // check if all the child nodes are constants so that they can be inserted into
1111 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001112 TIntermSequence &sequence = aggrNode->getSequence() ;
1113 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1114 if (!(*p)->getAsTyped()->getAsConstantUnion())
1115 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001116 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119}
1120
Jamie Madilla5efff92013-06-06 11:56:47 -04001121TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001122{
1123 TPublicType returnType = typeSpecifier;
1124 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001125 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001126
1127 if (typeSpecifier.array)
1128 {
1129 error(typeSpecifier.line, "not supported", "first-class array");
1130 recover();
1131 returnType.setArray(false);
1132 }
1133
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001134 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001135 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001136 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1137 {
1138 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1139 recover();
1140 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001141
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001142 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1143 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1144 {
1145 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1146 recover();
1147 }
1148 }
1149 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001150 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001151 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001152 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001153 case EvqVertexInput:
1154 case EvqFragmentOutput:
1155 case EvqVaryingIn:
1156 case EvqVaryingOut:
1157 if (typeSpecifier.type == EbtBool)
1158 {
1159 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1160 recover();
1161 }
1162 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001163
Jamie Madillb120eac2013-06-12 14:08:13 -04001164 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001165 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001166 }
1167
1168 return returnType;
1169}
1170
Jamie Madill075edd82013-07-08 13:30:19 -04001171TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001172{
1173 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1174 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1175
1176 if (identifier != "")
1177 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001178 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001179 recover();
1180
1181 // this error check can mutate the type
1182 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1183 recover();
1184
1185 TVariable* variable = 0;
1186
1187 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1188 recover();
1189
1190 if (variable && symbol)
1191 {
1192 symbol->setId(variable->getUniqueId());
1193 }
1194 }
1195
1196 return aggregate;
1197}
1198
Jamie Madill075edd82013-07-08 13:30:19 -04001199TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001200{
Jamie Madill51a53c72013-06-19 09:24:43 -04001201 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001202 recover();
1203
1204 // this error check can mutate the type
1205 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1206 recover();
1207
1208 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1209 {
1210 recover();
1211 }
1212
1213 TPublicType arrayType = publicType;
1214
1215 int size;
1216 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1217 {
1218 recover();
1219 }
1220 else
1221 {
1222 arrayType.setArray(true, size);
1223 }
1224
1225 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1226 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1227 TVariable* variable = 0;
1228
1229 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1230 recover();
1231
1232 if (variable && symbol)
1233 {
1234 symbol->setId(variable->getUniqueId());
1235 }
1236
1237 return aggregate;
1238}
1239
Jamie Madill075edd82013-07-08 13:30:19 -04001240TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001241{
Jamie Madilla5efff92013-06-06 11:56:47 -04001242 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001243 recover();
1244
1245 TIntermNode* intermNode;
1246 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1247 {
1248 //
1249 // Build intermediate representation
1250 //
1251 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1252 }
1253 else
1254 {
1255 recover();
1256 return NULL;
1257 }
1258}
1259
Jamie Madill075edd82013-07-08 13:30:19 -04001260TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001261{
1262 if (publicType.type == EbtInvariant && !identifierSymbol)
1263 {
1264 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1265 recover();
1266 }
1267
1268 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1269 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1270
1271 if (structQualifierErrorCheck(identifierLocation, publicType))
1272 recover();
1273
Jamie Madill0bd18df2013-06-20 11:55:52 -04001274 if (locationDeclaratorListCheck(identifierLocation, publicType))
1275 recover();
1276
Jamie Madill502d66f2013-06-20 11:55:52 -04001277 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1278 recover();
1279
1280 TVariable* variable = 0;
1281 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1282 recover();
1283 if (symbol && variable)
1284 symbol->setId(variable->getUniqueId());
1285
1286 return intermAggregate;
1287}
1288
Jamie Madill075edd82013-07-08 13:30:19 -04001289TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001290{
1291 if (structQualifierErrorCheck(identifierLocation, publicType))
1292 recover();
1293
Jamie Madill0bd18df2013-06-20 11:55:52 -04001294 if (locationDeclaratorListCheck(identifierLocation, publicType))
1295 recover();
1296
Jamie Madill502d66f2013-06-20 11:55:52 -04001297 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1298 recover();
1299
1300 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1301 {
1302 recover();
1303 }
1304 else if (indexExpression)
1305 {
1306 int size;
1307 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1308 recover();
1309 TPublicType arrayType(publicType);
1310 arrayType.setArray(true, size);
1311 TVariable* variable = NULL;
1312 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1313 recover();
1314 TType type = TType(arrayType);
1315 type.setArraySize(size);
1316
1317 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1318 }
1319 else
1320 {
1321 TPublicType arrayType(publicType);
1322 arrayType.setArray(true);
1323 TVariable* variable = NULL;
1324 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1325 recover();
1326 }
1327
1328 return NULL;
1329}
1330
Jamie Madill075edd82013-07-08 13:30:19 -04001331TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001332{
1333 if (structQualifierErrorCheck(identifierLocation, publicType))
1334 recover();
1335
Jamie Madill0bd18df2013-06-20 11:55:52 -04001336 if (locationDeclaratorListCheck(identifierLocation, publicType))
1337 recover();
1338
Jamie Madill502d66f2013-06-20 11:55:52 -04001339 TIntermNode* intermNode;
1340 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1341 {
1342 //
1343 // build the intermediate representation
1344 //
1345 if (intermNode)
1346 {
1347 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1348 }
1349 else
1350 {
1351 return declaratorList;
1352 }
1353 }
1354 else
1355 {
1356 recover();
1357 return NULL;
1358 }
1359}
1360
Jamie Madilla295edf2013-06-06 11:56:48 -04001361void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1362{
1363 if (typeQualifier.qualifier != EvqUniform)
1364 {
1365 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1366 recover();
1367 return;
1368 }
1369
1370 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1371 ASSERT(!layoutQualifier.isEmpty());
1372
1373 if (shaderVersion < 300)
1374 {
1375 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1376 recover();
1377 return;
1378 }
1379
1380 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1381 {
1382 recover();
1383 return;
1384 }
1385
Jamie Madill099c0f32013-06-20 11:55:52 -04001386 if (layoutQualifier.matrixPacking != EmpUnspecified)
1387 {
1388 defaultMatrixPacking = layoutQualifier.matrixPacking;
1389 }
1390
Jamie Madill1566ef72013-06-20 11:55:54 -04001391 if (layoutQualifier.blockStorage != EmpUnspecified)
1392 {
1393 defaultBlockStorage = layoutQualifier.blockStorage;
1394 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001395}
1396
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001397TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1398{
1399 TOperator op = EOpNull;
1400 if (publicType.userDef)
1401 {
1402 op = EOpConstructStruct;
1403 }
1404 else
1405 {
1406 switch (publicType.type)
1407 {
1408 case EbtFloat:
1409 if (publicType.isMatrix())
1410 {
1411 // TODO: non-square matrices
1412 switch(publicType.getCols())
1413 {
Jamie Madill28b97422013-07-08 14:01:38 -04001414 case 2: op = EOpConstructMat2; break;
1415 case 3: op = EOpConstructMat3; break;
1416 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001417 }
1418 }
1419 else
1420 {
1421 switch(publicType.getNominalSize())
1422 {
Jamie Madill28b97422013-07-08 14:01:38 -04001423 case 1: op = EOpConstructFloat; break;
1424 case 2: op = EOpConstructVec2; break;
1425 case 3: op = EOpConstructVec3; break;
1426 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001427 }
1428 }
1429 break;
1430
1431 case EbtInt:
1432 switch(publicType.getNominalSize())
1433 {
Jamie Madill28b97422013-07-08 14:01:38 -04001434 case 1: op = EOpConstructInt; break;
1435 case 2: op = EOpConstructIVec2; break;
1436 case 3: op = EOpConstructIVec3; break;
1437 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001438 }
1439 break;
1440
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001441 case EbtUInt:
1442 switch(publicType.getNominalSize())
1443 {
Jamie Madill28b97422013-07-08 14:01:38 -04001444 case 1: op = EOpConstructUInt; break;
1445 case 2: op = EOpConstructUVec2; break;
1446 case 3: op = EOpConstructUVec3; break;
1447 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001448 }
1449 break;
1450
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001451 case EbtBool:
1452 switch(publicType.getNominalSize())
1453 {
Jamie Madill28b97422013-07-08 14:01:38 -04001454 case 1: op = EOpConstructBool; break;
1455 case 2: op = EOpConstructBVec2; break;
1456 case 3: op = EOpConstructBVec3; break;
1457 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001458 }
1459 break;
1460
1461 default: break;
1462 }
1463
1464 if (op == EOpNull)
1465 {
1466 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1467 recover();
1468 publicType.type = EbtFloat;
1469 op = EOpConstructFloat;
1470 }
1471 }
1472
1473 TString tempString;
1474 TType type(publicType);
1475 return new TFunction(&tempString, type, op);
1476}
1477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478// This function is used to test for the correctness of the parameters passed to various constructor functions
1479// and also convert them to the right datatype if it is allowed and required.
1480//
1481// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1482//
Jamie Madill075edd82013-07-08 13:30:19 -04001483TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001485 if (node == 0)
1486 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001488 TIntermAggregate* aggrNode = node->getAsAggregate();
1489
alokp@chromium.org58e54292010-08-24 21:40:03 +00001490 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001491 if (op == EOpConstructStruct)
1492 memberTypes = type->getStruct()->begin();
1493
1494 TType elementType = *type;
1495 if (type->isArray())
1496 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001497
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001498 bool singleArg;
1499 if (aggrNode) {
1500 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1501 singleArg = true;
1502 else
1503 singleArg = false;
1504 } else
1505 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001507 TIntermTyped *newNode;
1508 if (singleArg) {
1509 // If structure constructor or array constructor is being called
1510 // for only one parameter inside the structure, we need to call constructStruct function once.
1511 if (type->isArray())
1512 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1513 else if (op == EOpConstructStruct)
1514 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1515 else
1516 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001518 if (newNode && newNode->getAsAggregate()) {
1519 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1520 if (constConstructor)
1521 return constConstructor;
1522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001524 return newNode;
1525 }
1526
1527 //
1528 // Handle list of arguments.
1529 //
1530 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1531 // if the structure constructor contains more than one parameter, then construct
1532 // each parameter
1533
1534 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1535
1536 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1537 // to the right type if possible (and allowed).
1538 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1539
1540 for (TIntermSequence::iterator p = sequenceVector.begin();
1541 p != sequenceVector.end(); p++, paramCount++) {
1542 if (type->isArray())
1543 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1544 else if (op == EOpConstructStruct)
1545 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1546 else
1547 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1548
1549 if (newNode) {
1550 *p = newNode;
1551 }
1552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001553
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001554 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1555 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1556 if (constConstructor)
1557 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001559 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560}
1561
1562TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1563{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001564 bool canBeFolded = areAllChildConst(aggrNode);
1565 aggrNode->setType(type);
1566 if (canBeFolded) {
1567 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001568 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001570 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001571 }
1572 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001573 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001574 }
1575 if (returnVal)
1576 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001578 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1579 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001581 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582}
1583
1584// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1585// for the parameter to the constructor (passed to this function). Essentially, it converts
1586// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1587// float, then float is converted to int.
1588//
1589// Returns 0 for an error or the constructed node.
1590//
Jamie Madill075edd82013-07-08 13:30:19 -04001591TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001593 TIntermTyped* newNode;
1594 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001596 //
1597 // First, convert types as needed.
1598 //
1599 switch (op) {
1600 case EOpConstructVec2:
1601 case EOpConstructVec3:
1602 case EOpConstructVec4:
1603 case EOpConstructMat2:
1604 case EOpConstructMat3:
1605 case EOpConstructMat4:
1606 case EOpConstructFloat:
1607 basicOp = EOpConstructFloat;
1608 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001610 case EOpConstructIVec2:
1611 case EOpConstructIVec3:
1612 case EOpConstructIVec4:
1613 case EOpConstructInt:
1614 basicOp = EOpConstructInt;
1615 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001617 case EOpConstructUVec2:
1618 case EOpConstructUVec3:
1619 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001620 case EOpConstructUInt:
1621 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001622 break;
1623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001624 case EOpConstructBVec2:
1625 case EOpConstructBVec3:
1626 case EOpConstructBVec4:
1627 case EOpConstructBool:
1628 basicOp = EOpConstructBool;
1629 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001631 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001632 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001633 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001635 return 0;
1636 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001637 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001638 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001639 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001640 return 0;
1641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001643 //
1644 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1645 //
1646
1647 // Otherwise, skip out early.
1648 if (subset || (newNode != node && newNode->getType() == *type))
1649 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001651 // setAggregateOperator will insert a new node for the constructor, as needed.
1652 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653}
1654
1655// This function tests for the type of the parameters to the structures constructors. Raises
1656// an error message if the expected type does not match the parameter passed to the constructor.
1657//
1658// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1659//
Jamie Madill075edd82013-07-08 13:30:19 -04001660TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001662 if (*type == node->getAsTyped()->getType()) {
1663 if (subset)
1664 return node->getAsTyped();
1665 else
1666 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1667 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001668 std::stringstream extraInfoStream;
1669 extraInfoStream << "cannot convert parameter " << paramCount
1670 << " from '" << node->getAsTyped()->getType().getBasicString()
1671 << "' to '" << type->getBasicString() << "'";
1672 std::string extraInfo = extraInfoStream.str();
1673 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001674 recover();
1675 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001677 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678}
1679
1680//
1681// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1682// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1683// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1684// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1685// a constant matrix.
1686//
Jamie Madill075edd82013-07-08 13:30:19 -04001687TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001689 TIntermTyped* typedNode;
1690 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001692 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 if (tempConstantNode) {
1694 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001696 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001697 return node;
1698 }
1699 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001700 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001701 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001703 return 0;
1704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001706 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001708 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001709 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001710 std::stringstream extraInfoStream;
1711 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1712 std::string extraInfo = extraInfoStream.str();
1713 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001714 recover();
1715 fields.offsets[i] = 0;
1716 }
1717
1718 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 }
1721 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1722 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723}
1724
1725//
1726// This function returns the column being accessed from a constant matrix. The values are retrieved from
1727// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1728// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1729// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1730//
Jamie Madill075edd82013-07-08 13:30:19 -04001731TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001733 TIntermTyped* typedNode;
1734 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001736 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001737 std::stringstream extraInfoStream;
1738 extraInfoStream << "matrix field selection out of range '" << index << "'";
1739 std::string extraInfo = extraInfoStream.str();
1740 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001741 recover();
1742 index = 0;
1743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001745 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001746 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001747 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001748 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1749 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001750 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001753 return 0;
1754 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001756 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001757}
1758
1759
1760//
1761// This function returns an element of an array accessed from a constant array. The values are retrieved from
1762// the symbol table and parse-tree is built for the type of the element. The input
1763// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1764// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1765//
Jamie Madill075edd82013-07-08 13:30:19 -04001766TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001768 TIntermTyped* typedNode;
1769 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1770 TType arrayElementType = node->getType();
1771 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001773 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001774 std::stringstream extraInfoStream;
1775 extraInfoStream << "array field selection out of range '" << index << "'";
1776 std::string extraInfo = extraInfoStream.str();
1777 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001778 recover();
1779 index = 0;
1780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001783 size_t arrayElementSize = arrayElementType.getObjectSize();
1784 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1785 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001787 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001788 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001790 return 0;
1791 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001793 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794}
1795
1796
1797//
1798// This function returns the value of a particular field inside a constant structure from the symbol table.
1799// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1800// function and returns the parse-tree with the values of the embedded/nested struct.
1801//
Jamie Madill075edd82013-07-08 13:30:19 -04001802TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001804 const TTypeList* fields = node->getType().getStruct();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001805 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806
Jamie Madill94bf7f22013-07-08 13:31:15 -04001807 for (size_t index = 0; index < fields->size(); ++index) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001808 if ((*fields)[index].type->getFieldName() == identifier) {
1809 break;
1810 } else {
1811 instanceSize += (*fields)[index].type->getObjectSize();
1812 }
1813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814
Jamie Madill94bf7f22013-07-08 13:31:15 -04001815 TIntermTyped *typedNode;
1816 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001817 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001818 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001820 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1821 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001822 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 return 0;
1826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001828 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829}
1830
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001831//
1832// Interface/uniform blocks
1833//
Jamie Madill075edd82013-07-08 13:30:19 -04001834TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TTypeList* typeList,
1835 const TString& instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001836{
1837 if (reservedErrorCheck(nameLine, blockName))
1838 recover();
1839
1840 if (typeQualifier.qualifier != EvqUniform)
1841 {
1842 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1843 recover();
1844 }
1845
Jamie Madill099c0f32013-06-20 11:55:52 -04001846 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1847 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001848 {
1849 recover();
1850 }
1851
Jamie Madill099c0f32013-06-20 11:55:52 -04001852 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1853 {
1854 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1855 }
1856
Jamie Madill1566ef72013-06-20 11:55:54 -04001857 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1858 {
1859 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1860 }
1861
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001862 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1863 if (!symbolTable.declare(*blockNameSymbol)) {
1864 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1865 recover();
1866 }
1867
1868 // check for sampler types
1869 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1870 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1871 TType* memberType = memberTypeLine.type;
1872 if (IsSampler(memberType->getBasicType())) {
1873 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1874 recover();
1875 }
1876
1877 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1878 switch (qualifier)
1879 {
1880 case EvqGlobal:
1881 case EvqUniform:
1882 break;
1883 default:
1884 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1885 recover();
1886 break;
1887 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001888
1889 // check layout qualifiers
Jamie Madill099c0f32013-06-20 11:55:52 -04001890 TLayoutQualifier memberLayoutQualifier = memberType->getLayoutQualifier();
1891 if (layoutLocationErrorCheck(memberTypeLine.line, memberLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001892 {
1893 recover();
1894 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001895
Jamie Madill1566ef72013-06-20 11:55:54 -04001896 if (memberLayoutQualifier.blockStorage != EbsUnspecified)
1897 {
1898 error(memberTypeLine.line, "invalid layout qualifier:", getBlockStorageString(memberLayoutQualifier.blockStorage), "cannot be used here");
1899 recover();
1900 }
1901
Jamie Madill099c0f32013-06-20 11:55:52 -04001902 if (memberLayoutQualifier.matrixPacking == EmpUnspecified)
1903 {
1904 memberLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1905 }
1906 else if (!memberType->isMatrix())
1907 {
1908 error(memberTypeLine.line, "invalid layout qualifier:", getMatrixPackingString(memberLayoutQualifier.matrixPacking), "can only be used on matrix types");
1909 recover();
1910 }
1911
1912 memberType->setLayoutQualifier(memberLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001913 }
1914
1915 TType* interfaceBlock = new TType(typeList, blockName);
1916 interfaceBlock->setBasicType(EbtInterfaceBlock);
1917 interfaceBlock->setQualifier(typeQualifier.qualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04001918 interfaceBlock->setLayoutQualifier(blockLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001919
1920 TString symbolName = "";
1921 int symbolId = 0;
1922
1923 if (instanceName == "")
1924 {
1925 // define symbols for the members of the interface block
1926 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1927 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1928 TType* memberType = memberTypeLine.type;
1929 memberType->setInterfaceBlockType(interfaceBlock);
1930 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1931 memberVariable->setQualifier(typeQualifier.qualifier);
1932 if (!symbolTable.declare(*memberVariable)) {
1933 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1934 recover();
1935 }
1936 }
1937 }
1938 else
1939 {
1940 interfaceBlock->setInstanceName(instanceName);
1941
1942 // add array index
1943 if (arrayIndex != NULL)
1944 {
1945 int size;
1946 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
1947 recover();
1948 interfaceBlock->setArraySize(size);
1949 }
1950
1951 // add a symbol for this interface block
1952 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
1953 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1954 if (!symbolTable.declare(*instanceTypeDef)) {
1955 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
1956 recover();
1957 }
1958
1959 symbolId = instanceTypeDef->getUniqueId();
1960 symbolName = instanceTypeDef->getName();
1961 }
1962
1963 exitStructDeclaration();
1964 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
1965 aggregate->setOp(EOpDeclaration);
1966 return aggregate;
1967}
1968
Jamie Madill075edd82013-07-08 13:30:19 -04001969bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001970{
1971 ++structNestingLevel;
1972
1973 // Embedded structure definitions are not supported per GLSL ES spec.
1974 // They aren't allowed in GLSL either, but we need to detect this here
1975 // so we don't rely on the GLSL compiler to catch it.
1976 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001977 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001978 return true;
1979 }
1980
1981 return false;
1982}
1983
1984void TParseContext::exitStructDeclaration()
1985{
1986 --structNestingLevel;
1987}
1988
1989namespace {
1990
1991const int kWebGLMaxStructNesting = 4;
1992
1993} // namespace
1994
Jamie Madill075edd82013-07-08 13:30:19 -04001995bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TType& fieldType)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001996{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001997 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001998 return false;
1999 }
2000
2001 if (fieldType.getBasicType() != EbtStruct) {
2002 return false;
2003 }
2004
2005 // We're already inside a structure definition at this point, so add
2006 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00002007 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002008 std::stringstream extraInfoStream;
2009 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
2010 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2011 std::string extraInfo = extraInfoStream.str();
2012 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002013 return true;
2014 }
2015
2016 return false;
2017}
2018
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002019//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002020// Parse an array index expression
2021//
Jamie Madill075edd82013-07-08 13:30:19 -04002022TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002023{
2024 TIntermTyped *indexedExpression = NULL;
2025
2026 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2027 {
2028 if (baseExpression->getAsSymbolNode())
2029 {
2030 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2031 }
2032 else
2033 {
2034 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2035 }
2036 recover();
2037 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002038
Jamie Madill7164cf42013-07-08 13:30:59 -04002039 if (indexExpression->getQualifier() == EvqConst)
2040 {
2041 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2042 if (index < 0)
2043 {
2044 std::stringstream infoStream;
2045 infoStream << index;
2046 std::string info = infoStream.str();
2047 error(location, "negative index", info.c_str());
2048 recover();
2049 index = 0;
2050 }
2051 if (baseExpression->getType().getQualifier() == EvqConst)
2052 {
2053 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002054 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002055 // constant folding for arrays
2056 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002057 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002058 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002059 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002060 // constant folding for vectors
2061 TVectorFields fields;
2062 fields.num = 1;
2063 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2064 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2065 }
2066 else if (baseExpression->isMatrix())
2067 {
2068 // constant folding for matrices
2069 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002070 }
2071 }
2072 else
2073 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002074 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002075 {
Jamie Madill18464b52013-07-08 14:01:55 -04002076 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002077 {
2078 std::stringstream extraInfoStream;
2079 extraInfoStream << "array index out of range '" << index << "'";
2080 std::string extraInfo = extraInfoStream.str();
2081 error(location, "", "[", extraInfo.c_str());
2082 recover();
2083 index = baseExpression->getType().getArraySize() - 1;
2084 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002085 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002086 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002087 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002088 std::stringstream extraInfoStream;
2089 extraInfoStream << "field selection out of range '" << index << "'";
2090 std::string extraInfo = extraInfoStream.str();
2091 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002092 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002093 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002094 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002095
Jamie Madill7164cf42013-07-08 13:30:59 -04002096 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2097 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002098 }
2099 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002100 else
2101 {
Jamie Madill18464b52013-07-08 14:01:55 -04002102 if (baseExpression->getBasicType() == EbtInterfaceBlock)
Jamie Madill7164cf42013-07-08 13:30:59 -04002103 {
2104 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2105 recover();
2106 }
2107 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2108 {
2109 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2110 recover();
2111 }
2112
2113 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2114 }
2115
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002116 if (indexedExpression == 0)
2117 {
2118 ConstantUnion *unionArray = new ConstantUnion[1];
2119 unionArray->setFConst(0.0f);
2120 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2121 }
2122 else if (baseExpression->isArray())
2123 {
2124 if (baseExpression->getType().getStruct())
2125 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002126 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
2127 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
2128 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002129 }
2130 else
2131 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002132 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002133 }
2134
2135 if (baseExpression->getType().getQualifier() == EvqConst)
2136 {
2137 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2138 }
2139 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002140 else if (baseExpression->isMatrix())
2141 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002142 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2143 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002144 }
2145 else if (baseExpression->isVector())
2146 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002147 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2148 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002149 }
2150 else
2151 {
2152 indexedExpression->setType(baseExpression->getType());
2153 }
2154
2155 return indexedExpression;
2156}
2157
Jamie Madill075edd82013-07-08 13:30:19 -04002158TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002159{
2160 TIntermTyped *indexedExpression = NULL;
2161
2162 if (baseExpression->isArray())
2163 {
2164 error(fieldLocation, "cannot apply dot operator to an array", ".");
2165 recover();
2166 }
2167
2168 if (baseExpression->isVector())
2169 {
2170 TVectorFields fields;
2171 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2172 {
2173 fields.num = 1;
2174 fields.offsets[0] = 0;
2175 recover();
2176 }
2177
2178 if (baseExpression->getType().getQualifier() == EvqConst)
2179 {
2180 // constant folding for vector fields
2181 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2182 if (indexedExpression == 0)
2183 {
2184 recover();
2185 indexedExpression = baseExpression;
2186 }
2187 else
2188 {
2189 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2190 }
2191 }
2192 else
2193 {
2194 TString vectorString = fieldString;
2195 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2196 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2197 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2198 }
2199 }
2200 else if (baseExpression->isMatrix())
2201 {
2202 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002203 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002204 {
2205 fields.wholeRow = false;
2206 fields.wholeCol = false;
2207 fields.row = 0;
2208 fields.col = 0;
2209 recover();
2210 }
2211
2212 if (fields.wholeRow || fields.wholeCol)
2213 {
2214 error(dotLocation, " non-scalar fields not implemented yet", ".");
2215 recover();
2216 ConstantUnion *unionArray = new ConstantUnion[1];
2217 unionArray->setIConst(0);
2218 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2219 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002220 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002221 }
2222 else
2223 {
2224 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002225 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002226 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2227 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2228 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2229 }
2230 }
2231 else if (baseExpression->getBasicType() == EbtStruct)
2232 {
2233 bool fieldFound = false;
2234 const TTypeList* fields = baseExpression->getType().getStruct();
2235 if (fields == 0)
2236 {
2237 error(dotLocation, "structure has no fields", "Internal Error");
2238 recover();
2239 indexedExpression = baseExpression;
2240 }
2241 else
2242 {
2243 unsigned int i;
2244 for (i = 0; i < fields->size(); ++i)
2245 {
2246 if ((*fields)[i].type->getFieldName() == fieldString)
2247 {
2248 fieldFound = true;
2249 break;
2250 }
2251 }
2252 if (fieldFound)
2253 {
2254 if (baseExpression->getType().getQualifier() == EvqConst)
2255 {
2256 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2257 if (indexedExpression == 0)
2258 {
2259 recover();
2260 indexedExpression = baseExpression;
2261 }
2262 else
2263 {
2264 indexedExpression->setType(*(*fields)[i].type);
2265 // change the qualifier of the return type, not of the structure field
2266 // as the structure definition is shared between various structures.
2267 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2268 }
2269 }
2270 else
2271 {
2272 ConstantUnion *unionArray = new ConstantUnion[1];
2273 unionArray->setIConst(i);
2274 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2275 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2276 indexedExpression->setType(*(*fields)[i].type);
2277 }
2278 }
2279 else
2280 {
2281 error(dotLocation, " no such field in structure", fieldString.c_str());
2282 recover();
2283 indexedExpression = baseExpression;
2284 }
2285 }
2286 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002287 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2288 {
2289 bool fieldFound = false;
2290 const TTypeList* fields = baseExpression->getType().getStruct();
2291 if (fields == 0)
2292 {
2293 error(dotLocation, "interface block has no fields", "Internal Error");
2294 recover();
2295 indexedExpression = baseExpression;
2296 }
2297 else
2298 {
2299 unsigned int i;
2300 for (i = 0; i < fields->size(); ++i)
2301 {
2302 if ((*fields)[i].type->getFieldName() == fieldString)
2303 {
2304 fieldFound = true;
2305 break;
2306 }
2307 }
2308 if (fieldFound)
2309 {
2310 ConstantUnion *unionArray = new ConstantUnion[1];
2311 unionArray->setIConst(i);
2312 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2313 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2314 indexedExpression->setType(*(*fields)[i].type);
2315 }
2316 else
2317 {
2318 error(dotLocation, " no such field in interface block", fieldString.c_str());
2319 recover();
2320 indexedExpression = baseExpression;
2321 }
2322 }
2323 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002324 else
2325 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002326 if (shaderVersion < 300)
2327 {
2328 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2329 }
2330 else
2331 {
2332 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2333 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002334 recover();
2335 indexedExpression = baseExpression;
2336 }
2337
2338 return indexedExpression;
2339}
2340
Jamie Madill075edd82013-07-08 13:30:19 -04002341TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002342{
Jamie Madilla5efff92013-06-06 11:56:47 -04002343 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002344
Jamie Madilla5efff92013-06-06 11:56:47 -04002345 qualifier.location = -1;
2346 qualifier.matrixPacking = EmpUnspecified;
2347 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002348
2349 if (qualifierType == "shared")
2350 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002351 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002352 }
2353 else if (qualifierType == "packed")
2354 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002355 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002356 }
2357 else if (qualifierType == "std140")
2358 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002359 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002360 }
2361 else if (qualifierType == "row_major")
2362 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002363 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002364 }
2365 else if (qualifierType == "column_major")
2366 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002367 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002368 }
2369 else if (qualifierType == "location")
2370 {
2371 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2372 recover();
2373 }
2374 else
2375 {
2376 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2377 recover();
2378 }
2379
Jamie Madilla5efff92013-06-06 11:56:47 -04002380 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002381}
2382
Jamie Madill075edd82013-07-08 13:30:19 -04002383TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002384{
Jamie Madilla5efff92013-06-06 11:56:47 -04002385 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002386
Jamie Madilla5efff92013-06-06 11:56:47 -04002387 qualifier.location = -1;
2388 qualifier.matrixPacking = EmpUnspecified;
2389 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002390
2391 if (qualifierType != "location")
2392 {
2393 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2394 recover();
2395 }
2396 else
2397 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002398 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002399 if (intValue < 0)
2400 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002401 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002402 recover();
2403 }
2404 else
2405 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002406 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002407 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002408 }
2409
Jamie Madilla5efff92013-06-06 11:56:47 -04002410 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002411}
2412
Jamie Madilla5efff92013-06-06 11:56:47 -04002413TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002414{
Jamie Madilla5efff92013-06-06 11:56:47 -04002415 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002416
Jamie Madilla5efff92013-06-06 11:56:47 -04002417 if (rightQualifier.location != -1)
2418 {
2419 joinedQualifier.location = rightQualifier.location;
2420 }
2421 if (rightQualifier.matrixPacking != EmpUnspecified)
2422 {
2423 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2424 }
2425 if (rightQualifier.blockStorage != EbsUnspecified)
2426 {
2427 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2428 }
2429
2430 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002431}
2432
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002433TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2434{
2435 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2436 recover();
2437 }
2438
2439 for (unsigned int i = 0; i < typeList->size(); ++i) {
2440 //
2441 // Careful not to replace already known aspects of type, like array-ness
2442 //
2443 TType* type = (*typeList)[i].type;
2444 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002445 type->setPrimarySize(typeSpecifier.primarySize);
2446 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002447 type->setPrecision(typeSpecifier.precision);
2448 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002449 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002450
2451 // don't allow arrays of arrays
2452 if (type->isArray()) {
2453 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2454 recover();
2455 }
2456 if (typeSpecifier.array)
2457 type->setArraySize(typeSpecifier.arraySize);
2458 if (typeSpecifier.userDef) {
2459 type->setStruct(typeSpecifier.userDef->getStruct());
2460 type->setTypeName(typeSpecifier.userDef->getTypeName());
2461 }
2462
2463 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2464 recover();
2465 }
2466 }
2467
2468 return typeList;
2469}
2470
Jamie Madill075edd82013-07-08 13:30:19 -04002471TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString &structName, TTypeList* typeList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002472{
2473 TType* structure = new TType(typeList, structName);
2474
2475 if (!structName.empty())
2476 {
2477 if (reservedErrorCheck(nameLine, structName))
2478 {
2479 recover();
2480 }
2481 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2482 if (!symbolTable.declare(*userTypeDef)) {
2483 error(nameLine, "redefinition", structName.c_str(), "struct");
2484 recover();
2485 }
2486 }
2487
2488 // ensure we do not specify any storage qualifiers on the struct members
2489 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2490 {
2491 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2492 const TQualifier qualifier = typeLine.type->getQualifier();
2493 switch (qualifier)
2494 {
2495 case EvqGlobal:
2496 case EvqTemporary:
2497 break;
2498 default:
2499 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2500 recover();
2501 break;
2502 }
2503 }
2504
2505 TPublicType publicType;
2506 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2507 publicType.userDef = structure;
2508 exitStructDeclaration();
2509
2510 return publicType;
2511}
2512
2513//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002514// Parse an array of strings using yyparse.
2515//
2516// Returns 0 for success.
2517//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002518int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002519 TParseContext* context) {
2520 if ((count == 0) || (string == NULL))
2521 return 1;
2522
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002523 if (glslang_initialize(context))
2524 return 1;
2525
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002526 int error = glslang_scan(count, string, length, context);
2527 if (!error)
2528 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002529
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002530 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002531
alokp@chromium.org6b495712012-06-29 00:06:58 +00002532 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002533}
2534
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002536