blob: 1471fc26da72153eba61cecc9a465d963455b30d [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
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001016bool TParseContext::isVariableBuiltIn(const TVariable* var)
1017{
1018 // First find by unmangled name to check whether the function name has been
1019 // hidden by a variable name or struct typename.
1020 const TSymbol* symbol = symbolTable.findBuiltIn(var->getName(), shaderVersion);
1021 if (symbol == 0) {
1022 symbol = symbolTable.findBuiltIn(var->getMangledName(), shaderVersion);
1023 }
1024
1025 if (symbol == 0) {
1026 return false;
1027 }
1028
1029 return symbol->isVariable();
1030}
1031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032//
1033// Initializers show up in several places in the grammar. Have one set of
1034// code to handle them here.
1035//
Jamie Madill075edd82013-07-08 13:30:19 -04001036bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001039 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001041 if (variable == 0) {
1042 if (reservedErrorCheck(line, identifier))
1043 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001045 if (voidErrorCheck(line, identifier, pType))
1046 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 //
1049 // add variable to symbol table
1050 //
1051 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001052 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001053 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 return true;
1055 // don't delete variable, it's used by error recovery, and the pool
1056 // pop will take care of the memory
1057 }
1058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 //
1061 // identifier must be of type constant, a global, or a temporary
1062 //
1063 TQualifier qualifier = variable->getType().getQualifier();
1064 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001065 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001066 return true;
1067 }
1068 //
1069 // test for and propagate constant
1070 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001072 if (qualifier == EvqConst) {
1073 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 std::stringstream extraInfoStream;
1075 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1076 std::string extraInfo = extraInfoStream.str();
1077 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001078 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 return true;
1080 }
1081 if (type != initializer->getType()) {
1082 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001083 variable->getType().getQualifierString());
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 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001088 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001090 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001093 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 variable->shareConstPointer(constArray);
1095 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001096 std::stringstream extraInfoStream;
1097 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1098 std::string extraInfo = extraInfoStream.str();
1099 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001100 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 return true;
1102 }
1103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001105 if (qualifier != EvqConst) {
1106 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1107 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1108 if (intermNode == 0) {
1109 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1110 return true;
1111 }
1112 } else
1113 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001115 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
1118bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1119{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001120 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001121 if (!aggrNode->isConstructor())
1122 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001126 // check if all the child nodes are constants so that they can be inserted into
1127 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001128 TIntermSequence &sequence = aggrNode->getSequence() ;
1129 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1130 if (!(*p)->getAsTyped()->getAsConstantUnion())
1131 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135}
1136
Jamie Madilla5efff92013-06-06 11:56:47 -04001137TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001138{
1139 TPublicType returnType = typeSpecifier;
1140 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001141 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001142
1143 if (typeSpecifier.array)
1144 {
1145 error(typeSpecifier.line, "not supported", "first-class array");
1146 recover();
1147 returnType.setArray(false);
1148 }
1149
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001150 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001151 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001152 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1153 {
1154 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1155 recover();
1156 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001157
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001158 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1159 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1160 {
1161 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1162 recover();
1163 }
1164 }
1165 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001166 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001167 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001168 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001169 case EvqVertexInput:
1170 case EvqFragmentOutput:
1171 case EvqVaryingIn:
1172 case EvqVaryingOut:
1173 if (typeSpecifier.type == EbtBool)
1174 {
1175 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1176 recover();
1177 }
1178 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001179
Jamie Madillb120eac2013-06-12 14:08:13 -04001180 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001181 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001182 }
1183
1184 return returnType;
1185}
1186
Jamie Madill075edd82013-07-08 13:30:19 -04001187TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001188{
1189 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1190 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1191
1192 if (identifier != "")
1193 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001194 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001195 recover();
1196
1197 // this error check can mutate the type
1198 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1199 recover();
1200
1201 TVariable* variable = 0;
1202
1203 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1204 recover();
1205
1206 if (variable && symbol)
1207 {
1208 symbol->setId(variable->getUniqueId());
1209 }
1210 }
1211
1212 return aggregate;
1213}
1214
Jamie Madill075edd82013-07-08 13:30:19 -04001215TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001216{
Jamie Madill51a53c72013-06-19 09:24:43 -04001217 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001218 recover();
1219
1220 // this error check can mutate the type
1221 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1222 recover();
1223
1224 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1225 {
1226 recover();
1227 }
1228
1229 TPublicType arrayType = publicType;
1230
1231 int size;
1232 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1233 {
1234 recover();
1235 }
1236 else
1237 {
1238 arrayType.setArray(true, size);
1239 }
1240
1241 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1242 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1243 TVariable* variable = 0;
1244
1245 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1246 recover();
1247
1248 if (variable && symbol)
1249 {
1250 symbol->setId(variable->getUniqueId());
1251 }
1252
1253 return aggregate;
1254}
1255
Jamie Madill075edd82013-07-08 13:30:19 -04001256TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001257{
Jamie Madilla5efff92013-06-06 11:56:47 -04001258 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001259 recover();
1260
1261 TIntermNode* intermNode;
1262 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1263 {
1264 //
1265 // Build intermediate representation
1266 //
1267 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1268 }
1269 else
1270 {
1271 recover();
1272 return NULL;
1273 }
1274}
1275
Jamie Madill075edd82013-07-08 13:30:19 -04001276TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001277{
1278 if (publicType.type == EbtInvariant && !identifierSymbol)
1279 {
1280 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1281 recover();
1282 }
1283
1284 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1285 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1286
1287 if (structQualifierErrorCheck(identifierLocation, publicType))
1288 recover();
1289
Jamie Madill0bd18df2013-06-20 11:55:52 -04001290 if (locationDeclaratorListCheck(identifierLocation, publicType))
1291 recover();
1292
Jamie Madill502d66f2013-06-20 11:55:52 -04001293 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1294 recover();
1295
1296 TVariable* variable = 0;
1297 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1298 recover();
1299 if (symbol && variable)
1300 symbol->setId(variable->getUniqueId());
1301
1302 return intermAggregate;
1303}
1304
Jamie Madill075edd82013-07-08 13:30:19 -04001305TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001306{
1307 if (structQualifierErrorCheck(identifierLocation, publicType))
1308 recover();
1309
Jamie Madill0bd18df2013-06-20 11:55:52 -04001310 if (locationDeclaratorListCheck(identifierLocation, publicType))
1311 recover();
1312
Jamie Madill502d66f2013-06-20 11:55:52 -04001313 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1314 recover();
1315
1316 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1317 {
1318 recover();
1319 }
1320 else if (indexExpression)
1321 {
1322 int size;
1323 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1324 recover();
1325 TPublicType arrayType(publicType);
1326 arrayType.setArray(true, size);
1327 TVariable* variable = NULL;
1328 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1329 recover();
1330 TType type = TType(arrayType);
1331 type.setArraySize(size);
1332
1333 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1334 }
1335 else
1336 {
1337 TPublicType arrayType(publicType);
1338 arrayType.setArray(true);
1339 TVariable* variable = NULL;
1340 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1341 recover();
1342 }
1343
1344 return NULL;
1345}
1346
Jamie Madill075edd82013-07-08 13:30:19 -04001347TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001348{
1349 if (structQualifierErrorCheck(identifierLocation, publicType))
1350 recover();
1351
Jamie Madill0bd18df2013-06-20 11:55:52 -04001352 if (locationDeclaratorListCheck(identifierLocation, publicType))
1353 recover();
1354
Jamie Madill502d66f2013-06-20 11:55:52 -04001355 TIntermNode* intermNode;
1356 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1357 {
1358 //
1359 // build the intermediate representation
1360 //
1361 if (intermNode)
1362 {
1363 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1364 }
1365 else
1366 {
1367 return declaratorList;
1368 }
1369 }
1370 else
1371 {
1372 recover();
1373 return NULL;
1374 }
1375}
1376
Jamie Madilla295edf2013-06-06 11:56:48 -04001377void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1378{
1379 if (typeQualifier.qualifier != EvqUniform)
1380 {
1381 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1382 recover();
1383 return;
1384 }
1385
1386 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1387 ASSERT(!layoutQualifier.isEmpty());
1388
1389 if (shaderVersion < 300)
1390 {
1391 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1392 recover();
1393 return;
1394 }
1395
1396 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1397 {
1398 recover();
1399 return;
1400 }
1401
Jamie Madill099c0f32013-06-20 11:55:52 -04001402 if (layoutQualifier.matrixPacking != EmpUnspecified)
1403 {
1404 defaultMatrixPacking = layoutQualifier.matrixPacking;
1405 }
1406
Jamie Madill1566ef72013-06-20 11:55:54 -04001407 if (layoutQualifier.blockStorage != EmpUnspecified)
1408 {
1409 defaultBlockStorage = layoutQualifier.blockStorage;
1410 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001411}
1412
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001413TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1414{
1415 TOperator op = EOpNull;
1416 if (publicType.userDef)
1417 {
1418 op = EOpConstructStruct;
1419 }
1420 else
1421 {
1422 switch (publicType.type)
1423 {
1424 case EbtFloat:
1425 if (publicType.isMatrix())
1426 {
1427 // TODO: non-square matrices
1428 switch(publicType.getCols())
1429 {
Jamie Madill28b97422013-07-08 14:01:38 -04001430 case 2: op = EOpConstructMat2; break;
1431 case 3: op = EOpConstructMat3; break;
1432 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001433 }
1434 }
1435 else
1436 {
1437 switch(publicType.getNominalSize())
1438 {
Jamie Madill28b97422013-07-08 14:01:38 -04001439 case 1: op = EOpConstructFloat; break;
1440 case 2: op = EOpConstructVec2; break;
1441 case 3: op = EOpConstructVec3; break;
1442 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001443 }
1444 }
1445 break;
1446
1447 case EbtInt:
1448 switch(publicType.getNominalSize())
1449 {
Jamie Madill28b97422013-07-08 14:01:38 -04001450 case 1: op = EOpConstructInt; break;
1451 case 2: op = EOpConstructIVec2; break;
1452 case 3: op = EOpConstructIVec3; break;
1453 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001454 }
1455 break;
1456
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001457 case EbtUInt:
1458 switch(publicType.getNominalSize())
1459 {
Jamie Madill28b97422013-07-08 14:01:38 -04001460 case 1: op = EOpConstructUInt; break;
1461 case 2: op = EOpConstructUVec2; break;
1462 case 3: op = EOpConstructUVec3; break;
1463 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001464 }
1465 break;
1466
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001467 case EbtBool:
1468 switch(publicType.getNominalSize())
1469 {
Jamie Madill28b97422013-07-08 14:01:38 -04001470 case 1: op = EOpConstructBool; break;
1471 case 2: op = EOpConstructBVec2; break;
1472 case 3: op = EOpConstructBVec3; break;
1473 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001474 }
1475 break;
1476
1477 default: break;
1478 }
1479
1480 if (op == EOpNull)
1481 {
1482 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1483 recover();
1484 publicType.type = EbtFloat;
1485 op = EOpConstructFloat;
1486 }
1487 }
1488
1489 TString tempString;
1490 TType type(publicType);
1491 return new TFunction(&tempString, type, op);
1492}
1493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494// This function is used to test for the correctness of the parameters passed to various constructor functions
1495// and also convert them to the right datatype if it is allowed and required.
1496//
1497// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1498//
Jamie Madill075edd82013-07-08 13:30:19 -04001499TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001501 if (node == 0)
1502 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001504 TIntermAggregate* aggrNode = node->getAsAggregate();
1505
alokp@chromium.org58e54292010-08-24 21:40:03 +00001506 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001507 if (op == EOpConstructStruct)
1508 memberTypes = type->getStruct()->begin();
1509
1510 TType elementType = *type;
1511 if (type->isArray())
1512 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001514 bool singleArg;
1515 if (aggrNode) {
1516 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1517 singleArg = true;
1518 else
1519 singleArg = false;
1520 } else
1521 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001523 TIntermTyped *newNode;
1524 if (singleArg) {
1525 // If structure constructor or array constructor is being called
1526 // for only one parameter inside the structure, we need to call constructStruct function once.
1527 if (type->isArray())
1528 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1529 else if (op == EOpConstructStruct)
1530 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1531 else
1532 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001534 if (newNode && newNode->getAsAggregate()) {
1535 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1536 if (constConstructor)
1537 return constConstructor;
1538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001540 return newNode;
1541 }
1542
1543 //
1544 // Handle list of arguments.
1545 //
1546 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1547 // if the structure constructor contains more than one parameter, then construct
1548 // each parameter
1549
1550 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1551
1552 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1553 // to the right type if possible (and allowed).
1554 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1555
1556 for (TIntermSequence::iterator p = sequenceVector.begin();
1557 p != sequenceVector.end(); p++, paramCount++) {
1558 if (type->isArray())
1559 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1560 else if (op == EOpConstructStruct)
1561 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1562 else
1563 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1564
1565 if (newNode) {
1566 *p = newNode;
1567 }
1568 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001570 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1571 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1572 if (constConstructor)
1573 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001575 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576}
1577
1578TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1579{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001580 bool canBeFolded = areAllChildConst(aggrNode);
1581 aggrNode->setType(type);
1582 if (canBeFolded) {
1583 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001584 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001585 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001586 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001587 }
1588 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001589 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001590 }
1591 if (returnVal)
1592 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001594 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001597 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598}
1599
1600// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1601// for the parameter to the constructor (passed to this function). Essentially, it converts
1602// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1603// float, then float is converted to int.
1604//
1605// Returns 0 for an error or the constructed node.
1606//
Jamie Madill075edd82013-07-08 13:30:19 -04001607TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001609 TIntermTyped* newNode;
1610 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001612 //
1613 // First, convert types as needed.
1614 //
1615 switch (op) {
1616 case EOpConstructVec2:
1617 case EOpConstructVec3:
1618 case EOpConstructVec4:
1619 case EOpConstructMat2:
1620 case EOpConstructMat3:
1621 case EOpConstructMat4:
1622 case EOpConstructFloat:
1623 basicOp = EOpConstructFloat;
1624 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 case EOpConstructIVec2:
1627 case EOpConstructIVec3:
1628 case EOpConstructIVec4:
1629 case EOpConstructInt:
1630 basicOp = EOpConstructInt;
1631 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001633 case EOpConstructUVec2:
1634 case EOpConstructUVec3:
1635 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001636 case EOpConstructUInt:
1637 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001638 break;
1639
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001640 case EOpConstructBVec2:
1641 case EOpConstructBVec3:
1642 case EOpConstructBVec4:
1643 case EOpConstructBool:
1644 basicOp = EOpConstructBool;
1645 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001647 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001648 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001649 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001651 return 0;
1652 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001653 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001654 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001655 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001656 return 0;
1657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001659 //
1660 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1661 //
1662
1663 // Otherwise, skip out early.
1664 if (subset || (newNode != node && newNode->getType() == *type))
1665 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001667 // setAggregateOperator will insert a new node for the constructor, as needed.
1668 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669}
1670
1671// This function tests for the type of the parameters to the structures constructors. Raises
1672// an error message if the expected type does not match the parameter passed to the constructor.
1673//
1674// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1675//
Jamie Madill075edd82013-07-08 13:30:19 -04001676TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 if (*type == node->getAsTyped()->getType()) {
1679 if (subset)
1680 return node->getAsTyped();
1681 else
1682 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1683 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001684 std::stringstream extraInfoStream;
1685 extraInfoStream << "cannot convert parameter " << paramCount
1686 << " from '" << node->getAsTyped()->getType().getBasicString()
1687 << "' to '" << type->getBasicString() << "'";
1688 std::string extraInfo = extraInfoStream.str();
1689 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001690 recover();
1691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694}
1695
1696//
1697// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1698// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1699// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1700// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1701// a constant matrix.
1702//
Jamie Madill075edd82013-07-08 13:30:19 -04001703TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001705 TIntermTyped* typedNode;
1706 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001708 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001709 if (tempConstantNode) {
1710 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001712 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001713 return node;
1714 }
1715 } 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 +00001716 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001717 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001719 return 0;
1720 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001722 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001725 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001726 std::stringstream extraInfoStream;
1727 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1728 std::string extraInfo = extraInfoStream.str();
1729 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001730 recover();
1731 fields.offsets[i] = 0;
1732 }
1733
1734 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 }
1737 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1738 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739}
1740
1741//
1742// This function returns the column being accessed from a constant matrix. The values are retrieved from
1743// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1744// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1745// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1746//
Jamie Madill075edd82013-07-08 13:30:19 -04001747TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001748{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001749 TIntermTyped* typedNode;
1750 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001752 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001753 std::stringstream extraInfoStream;
1754 extraInfoStream << "matrix field selection out of range '" << index << "'";
1755 std::string extraInfo = extraInfoStream.str();
1756 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001757 recover();
1758 index = 0;
1759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001761 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001762 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001763 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1765 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001766 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001769 return 0;
1770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001772 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773}
1774
1775
1776//
1777// This function returns an element of an array accessed from a constant array. The values are retrieved from
1778// the symbol table and parse-tree is built for the type of the element. The input
1779// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1780// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1781//
Jamie Madill075edd82013-07-08 13:30:19 -04001782TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 TIntermTyped* typedNode;
1785 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1786 TType arrayElementType = node->getType();
1787 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001789 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001790 std::stringstream extraInfoStream;
1791 extraInfoStream << "array field selection out of range '" << index << "'";
1792 std::string extraInfo = extraInfoStream.str();
1793 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001794 recover();
1795 index = 0;
1796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001797
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001798 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001799 size_t arrayElementSize = arrayElementType.getObjectSize();
1800 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1801 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001803 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 return 0;
1807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001808
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001809 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810}
1811
1812
1813//
1814// This function returns the value of a particular field inside a constant structure from the symbol table.
1815// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1816// function and returns the parse-tree with the values of the embedded/nested struct.
1817//
Jamie Madill075edd82013-07-08 13:30:19 -04001818TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001820 const TTypeList* fields = node->getType().getStruct();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001821 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822
Jamie Madill94bf7f22013-07-08 13:31:15 -04001823 for (size_t index = 0; index < fields->size(); ++index) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001824 if ((*fields)[index].type->getFieldName() == identifier) {
1825 break;
1826 } else {
1827 instanceSize += (*fields)[index].type->getObjectSize();
1828 }
1829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830
Jamie Madill94bf7f22013-07-08 13:31:15 -04001831 TIntermTyped *typedNode;
1832 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001833 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001834 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001836 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1837 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001838 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 return 0;
1842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845}
1846
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001847//
1848// Interface/uniform blocks
1849//
Jamie Madill075edd82013-07-08 13:30:19 -04001850TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TTypeList* typeList,
1851 const TString& instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001852{
1853 if (reservedErrorCheck(nameLine, blockName))
1854 recover();
1855
1856 if (typeQualifier.qualifier != EvqUniform)
1857 {
1858 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1859 recover();
1860 }
1861
Jamie Madill099c0f32013-06-20 11:55:52 -04001862 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1863 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001864 {
1865 recover();
1866 }
1867
Jamie Madill099c0f32013-06-20 11:55:52 -04001868 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1869 {
1870 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1871 }
1872
Jamie Madill1566ef72013-06-20 11:55:54 -04001873 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1874 {
1875 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1876 }
1877
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001878 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1879 if (!symbolTable.declare(*blockNameSymbol)) {
1880 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1881 recover();
1882 }
1883
1884 // check for sampler types
1885 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1886 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1887 TType* memberType = memberTypeLine.type;
1888 if (IsSampler(memberType->getBasicType())) {
1889 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1890 recover();
1891 }
1892
1893 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1894 switch (qualifier)
1895 {
1896 case EvqGlobal:
1897 case EvqUniform:
1898 break;
1899 default:
1900 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1901 recover();
1902 break;
1903 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001904
1905 // check layout qualifiers
Jamie Madill099c0f32013-06-20 11:55:52 -04001906 TLayoutQualifier memberLayoutQualifier = memberType->getLayoutQualifier();
1907 if (layoutLocationErrorCheck(memberTypeLine.line, memberLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001908 {
1909 recover();
1910 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001911
Jamie Madill1566ef72013-06-20 11:55:54 -04001912 if (memberLayoutQualifier.blockStorage != EbsUnspecified)
1913 {
1914 error(memberTypeLine.line, "invalid layout qualifier:", getBlockStorageString(memberLayoutQualifier.blockStorage), "cannot be used here");
1915 recover();
1916 }
1917
Jamie Madill099c0f32013-06-20 11:55:52 -04001918 if (memberLayoutQualifier.matrixPacking == EmpUnspecified)
1919 {
1920 memberLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1921 }
1922 else if (!memberType->isMatrix())
1923 {
1924 error(memberTypeLine.line, "invalid layout qualifier:", getMatrixPackingString(memberLayoutQualifier.matrixPacking), "can only be used on matrix types");
1925 recover();
1926 }
1927
1928 memberType->setLayoutQualifier(memberLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001929 }
1930
1931 TType* interfaceBlock = new TType(typeList, blockName);
1932 interfaceBlock->setBasicType(EbtInterfaceBlock);
1933 interfaceBlock->setQualifier(typeQualifier.qualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04001934 interfaceBlock->setLayoutQualifier(blockLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001935
1936 TString symbolName = "";
1937 int symbolId = 0;
1938
1939 if (instanceName == "")
1940 {
1941 // define symbols for the members of the interface block
1942 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1943 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1944 TType* memberType = memberTypeLine.type;
1945 memberType->setInterfaceBlockType(interfaceBlock);
1946 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1947 memberVariable->setQualifier(typeQualifier.qualifier);
1948 if (!symbolTable.declare(*memberVariable)) {
1949 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1950 recover();
1951 }
1952 }
1953 }
1954 else
1955 {
1956 interfaceBlock->setInstanceName(instanceName);
1957
1958 // add array index
1959 if (arrayIndex != NULL)
1960 {
1961 int size;
1962 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
1963 recover();
1964 interfaceBlock->setArraySize(size);
1965 }
1966
1967 // add a symbol for this interface block
1968 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
1969 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1970 if (!symbolTable.declare(*instanceTypeDef)) {
1971 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
1972 recover();
1973 }
1974
1975 symbolId = instanceTypeDef->getUniqueId();
1976 symbolName = instanceTypeDef->getName();
1977 }
1978
1979 exitStructDeclaration();
1980 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
1981 aggregate->setOp(EOpDeclaration);
1982 return aggregate;
1983}
1984
Jamie Madill075edd82013-07-08 13:30:19 -04001985bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001986{
1987 ++structNestingLevel;
1988
1989 // Embedded structure definitions are not supported per GLSL ES spec.
1990 // They aren't allowed in GLSL either, but we need to detect this here
1991 // so we don't rely on the GLSL compiler to catch it.
1992 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001993 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001994 return true;
1995 }
1996
1997 return false;
1998}
1999
2000void TParseContext::exitStructDeclaration()
2001{
2002 --structNestingLevel;
2003}
2004
2005namespace {
2006
2007const int kWebGLMaxStructNesting = 4;
2008
2009} // namespace
2010
Jamie Madill075edd82013-07-08 13:30:19 -04002011bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TType& fieldType)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002012{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002013 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014 return false;
2015 }
2016
2017 if (fieldType.getBasicType() != EbtStruct) {
2018 return false;
2019 }
2020
2021 // We're already inside a structure definition at this point, so add
2022 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00002023 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002024 std::stringstream extraInfoStream;
2025 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
2026 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2027 std::string extraInfo = extraInfoStream.str();
2028 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002029 return true;
2030 }
2031
2032 return false;
2033}
2034
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002035//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002036// Parse an array index expression
2037//
Jamie Madill075edd82013-07-08 13:30:19 -04002038TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002039{
2040 TIntermTyped *indexedExpression = NULL;
2041
2042 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2043 {
2044 if (baseExpression->getAsSymbolNode())
2045 {
2046 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2047 }
2048 else
2049 {
2050 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2051 }
2052 recover();
2053 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002054
Jamie Madill7164cf42013-07-08 13:30:59 -04002055 if (indexExpression->getQualifier() == EvqConst)
2056 {
2057 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2058 if (index < 0)
2059 {
2060 std::stringstream infoStream;
2061 infoStream << index;
2062 std::string info = infoStream.str();
2063 error(location, "negative index", info.c_str());
2064 recover();
2065 index = 0;
2066 }
2067 if (baseExpression->getType().getQualifier() == EvqConst)
2068 {
2069 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002070 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002071 // constant folding for arrays
2072 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002073 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002074 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002075 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002076 // constant folding for vectors
2077 TVectorFields fields;
2078 fields.num = 1;
2079 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2080 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2081 }
2082 else if (baseExpression->isMatrix())
2083 {
2084 // constant folding for matrices
2085 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002086 }
2087 }
2088 else
2089 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002090 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002091 {
Jamie Madill18464b52013-07-08 14:01:55 -04002092 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002093 {
2094 std::stringstream extraInfoStream;
2095 extraInfoStream << "array index out of range '" << index << "'";
2096 std::string extraInfo = extraInfoStream.str();
2097 error(location, "", "[", extraInfo.c_str());
2098 recover();
2099 index = baseExpression->getType().getArraySize() - 1;
2100 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002101 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002102 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002103 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002104 std::stringstream extraInfoStream;
2105 extraInfoStream << "field selection out of range '" << index << "'";
2106 std::string extraInfo = extraInfoStream.str();
2107 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002108 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002109 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002110 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002111
Jamie Madill7164cf42013-07-08 13:30:59 -04002112 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2113 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002114 }
2115 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002116 else
2117 {
Jamie Madill18464b52013-07-08 14:01:55 -04002118 if (baseExpression->getBasicType() == EbtInterfaceBlock)
Jamie Madill7164cf42013-07-08 13:30:59 -04002119 {
2120 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2121 recover();
2122 }
2123 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2124 {
2125 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2126 recover();
2127 }
2128
2129 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2130 }
2131
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002132 if (indexedExpression == 0)
2133 {
2134 ConstantUnion *unionArray = new ConstantUnion[1];
2135 unionArray->setFConst(0.0f);
2136 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2137 }
2138 else if (baseExpression->isArray())
2139 {
2140 if (baseExpression->getType().getStruct())
2141 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002142 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
2143 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
2144 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002145 }
2146 else
2147 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002148 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002149 }
2150
2151 if (baseExpression->getType().getQualifier() == EvqConst)
2152 {
2153 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2154 }
2155 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002156 else if (baseExpression->isMatrix())
2157 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002158 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2159 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002160 }
2161 else if (baseExpression->isVector())
2162 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002163 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2164 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002165 }
2166 else
2167 {
2168 indexedExpression->setType(baseExpression->getType());
2169 }
2170
2171 return indexedExpression;
2172}
2173
Jamie Madill075edd82013-07-08 13:30:19 -04002174TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002175{
2176 TIntermTyped *indexedExpression = NULL;
2177
2178 if (baseExpression->isArray())
2179 {
2180 error(fieldLocation, "cannot apply dot operator to an array", ".");
2181 recover();
2182 }
2183
2184 if (baseExpression->isVector())
2185 {
2186 TVectorFields fields;
2187 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2188 {
2189 fields.num = 1;
2190 fields.offsets[0] = 0;
2191 recover();
2192 }
2193
2194 if (baseExpression->getType().getQualifier() == EvqConst)
2195 {
2196 // constant folding for vector fields
2197 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2198 if (indexedExpression == 0)
2199 {
2200 recover();
2201 indexedExpression = baseExpression;
2202 }
2203 else
2204 {
2205 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2206 }
2207 }
2208 else
2209 {
2210 TString vectorString = fieldString;
2211 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2212 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2213 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2214 }
2215 }
2216 else if (baseExpression->isMatrix())
2217 {
2218 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002219 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002220 {
2221 fields.wholeRow = false;
2222 fields.wholeCol = false;
2223 fields.row = 0;
2224 fields.col = 0;
2225 recover();
2226 }
2227
2228 if (fields.wholeRow || fields.wholeCol)
2229 {
2230 error(dotLocation, " non-scalar fields not implemented yet", ".");
2231 recover();
2232 ConstantUnion *unionArray = new ConstantUnion[1];
2233 unionArray->setIConst(0);
2234 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2235 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002236 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002237 }
2238 else
2239 {
2240 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002241 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002242 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2243 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2244 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2245 }
2246 }
2247 else if (baseExpression->getBasicType() == EbtStruct)
2248 {
2249 bool fieldFound = false;
2250 const TTypeList* fields = baseExpression->getType().getStruct();
2251 if (fields == 0)
2252 {
2253 error(dotLocation, "structure has no fields", "Internal Error");
2254 recover();
2255 indexedExpression = baseExpression;
2256 }
2257 else
2258 {
2259 unsigned int i;
2260 for (i = 0; i < fields->size(); ++i)
2261 {
2262 if ((*fields)[i].type->getFieldName() == fieldString)
2263 {
2264 fieldFound = true;
2265 break;
2266 }
2267 }
2268 if (fieldFound)
2269 {
2270 if (baseExpression->getType().getQualifier() == EvqConst)
2271 {
2272 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2273 if (indexedExpression == 0)
2274 {
2275 recover();
2276 indexedExpression = baseExpression;
2277 }
2278 else
2279 {
2280 indexedExpression->setType(*(*fields)[i].type);
2281 // change the qualifier of the return type, not of the structure field
2282 // as the structure definition is shared between various structures.
2283 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2284 }
2285 }
2286 else
2287 {
2288 ConstantUnion *unionArray = new ConstantUnion[1];
2289 unionArray->setIConst(i);
2290 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2291 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2292 indexedExpression->setType(*(*fields)[i].type);
2293 }
2294 }
2295 else
2296 {
2297 error(dotLocation, " no such field in structure", fieldString.c_str());
2298 recover();
2299 indexedExpression = baseExpression;
2300 }
2301 }
2302 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002303 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2304 {
2305 bool fieldFound = false;
2306 const TTypeList* fields = baseExpression->getType().getStruct();
2307 if (fields == 0)
2308 {
2309 error(dotLocation, "interface block has no fields", "Internal Error");
2310 recover();
2311 indexedExpression = baseExpression;
2312 }
2313 else
2314 {
2315 unsigned int i;
2316 for (i = 0; i < fields->size(); ++i)
2317 {
2318 if ((*fields)[i].type->getFieldName() == fieldString)
2319 {
2320 fieldFound = true;
2321 break;
2322 }
2323 }
2324 if (fieldFound)
2325 {
2326 ConstantUnion *unionArray = new ConstantUnion[1];
2327 unionArray->setIConst(i);
2328 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2329 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2330 indexedExpression->setType(*(*fields)[i].type);
2331 }
2332 else
2333 {
2334 error(dotLocation, " no such field in interface block", fieldString.c_str());
2335 recover();
2336 indexedExpression = baseExpression;
2337 }
2338 }
2339 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002340 else
2341 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002342 if (shaderVersion < 300)
2343 {
2344 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2345 }
2346 else
2347 {
2348 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2349 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002350 recover();
2351 indexedExpression = baseExpression;
2352 }
2353
2354 return indexedExpression;
2355}
2356
Jamie Madill075edd82013-07-08 13:30:19 -04002357TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002358{
Jamie Madilla5efff92013-06-06 11:56:47 -04002359 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002360
Jamie Madilla5efff92013-06-06 11:56:47 -04002361 qualifier.location = -1;
2362 qualifier.matrixPacking = EmpUnspecified;
2363 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002364
2365 if (qualifierType == "shared")
2366 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002367 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002368 }
2369 else if (qualifierType == "packed")
2370 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002371 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002372 }
2373 else if (qualifierType == "std140")
2374 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002375 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002376 }
2377 else if (qualifierType == "row_major")
2378 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002379 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002380 }
2381 else if (qualifierType == "column_major")
2382 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002383 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002384 }
2385 else if (qualifierType == "location")
2386 {
2387 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2388 recover();
2389 }
2390 else
2391 {
2392 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2393 recover();
2394 }
2395
Jamie Madilla5efff92013-06-06 11:56:47 -04002396 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002397}
2398
Jamie Madill075edd82013-07-08 13:30:19 -04002399TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002400{
Jamie Madilla5efff92013-06-06 11:56:47 -04002401 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002402
Jamie Madilla5efff92013-06-06 11:56:47 -04002403 qualifier.location = -1;
2404 qualifier.matrixPacking = EmpUnspecified;
2405 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002406
2407 if (qualifierType != "location")
2408 {
2409 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2410 recover();
2411 }
2412 else
2413 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002414 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002415 if (intValue < 0)
2416 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002417 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002418 recover();
2419 }
2420 else
2421 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002422 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002423 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002424 }
2425
Jamie Madilla5efff92013-06-06 11:56:47 -04002426 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002427}
2428
Jamie Madilla5efff92013-06-06 11:56:47 -04002429TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002430{
Jamie Madilla5efff92013-06-06 11:56:47 -04002431 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002432
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 if (rightQualifier.location != -1)
2434 {
2435 joinedQualifier.location = rightQualifier.location;
2436 }
2437 if (rightQualifier.matrixPacking != EmpUnspecified)
2438 {
2439 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2440 }
2441 if (rightQualifier.blockStorage != EbsUnspecified)
2442 {
2443 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2444 }
2445
2446 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002447}
2448
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002449TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2450{
2451 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2452 recover();
2453 }
2454
2455 for (unsigned int i = 0; i < typeList->size(); ++i) {
2456 //
2457 // Careful not to replace already known aspects of type, like array-ness
2458 //
2459 TType* type = (*typeList)[i].type;
2460 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002461 type->setPrimarySize(typeSpecifier.primarySize);
2462 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002463 type->setPrecision(typeSpecifier.precision);
2464 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002465 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002466
2467 // don't allow arrays of arrays
2468 if (type->isArray()) {
2469 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2470 recover();
2471 }
2472 if (typeSpecifier.array)
2473 type->setArraySize(typeSpecifier.arraySize);
2474 if (typeSpecifier.userDef) {
2475 type->setStruct(typeSpecifier.userDef->getStruct());
2476 type->setTypeName(typeSpecifier.userDef->getTypeName());
2477 }
2478
2479 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2480 recover();
2481 }
2482 }
2483
2484 return typeList;
2485}
2486
Jamie Madill075edd82013-07-08 13:30:19 -04002487TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString &structName, TTypeList* typeList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002488{
2489 TType* structure = new TType(typeList, structName);
2490
2491 if (!structName.empty())
2492 {
2493 if (reservedErrorCheck(nameLine, structName))
2494 {
2495 recover();
2496 }
2497 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2498 if (!symbolTable.declare(*userTypeDef)) {
2499 error(nameLine, "redefinition", structName.c_str(), "struct");
2500 recover();
2501 }
2502 }
2503
2504 // ensure we do not specify any storage qualifiers on the struct members
2505 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2506 {
2507 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2508 const TQualifier qualifier = typeLine.type->getQualifier();
2509 switch (qualifier)
2510 {
2511 case EvqGlobal:
2512 case EvqTemporary:
2513 break;
2514 default:
2515 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2516 recover();
2517 break;
2518 }
2519 }
2520
2521 TPublicType publicType;
2522 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2523 publicType.userDef = structure;
2524 exitStructDeclaration();
2525
2526 return publicType;
2527}
2528
2529//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002530// Parse an array of strings using yyparse.
2531//
2532// Returns 0 for success.
2533//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002534int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002535 TParseContext* context) {
2536 if ((count == 0) || (string == NULL))
2537 return 1;
2538
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002539 if (glslang_initialize(context))
2540 return 1;
2541
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002542 int error = glslang_scan(count, string, length, context);
2543 if (!error)
2544 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002545
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002546 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002547
alokp@chromium.org6b495712012-06-29 00:06:58 +00002548 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002549}
2550
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002552