blob: f79bbb816cf6db365622122961dee10c4336790a [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//
25bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line)
26{
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//
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, int 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//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000178void TParseContext::error(TSourceLoc loc,
179 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;
183 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
184 diagnostics.writeInfo(pp::Diagnostics::ERROR,
185 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000186
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187}
188
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000189void TParseContext::warning(TSourceLoc loc,
190 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000191 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000192 pp::SourceLocation srcLoc;
193 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
194 diagnostics.writeInfo(pp::Diagnostics::WARNING,
195 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000196}
197
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000198void TParseContext::trace(const char* str)
199{
200 diagnostics.writeDebug(str);
201}
202
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203//
204// Same error message for all places assignments don't work.
205//
206void TParseContext::assignError(int line, const char* op, TString left, TString right)
207{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000208 std::stringstream extraInfoStream;
209 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
210 std::string extraInfo = extraInfoStream.str();
211 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212}
213
214//
215// Same error message for all places unary operations don't work.
216//
217void TParseContext::unaryOpError(int line, const char* op, TString operand)
218{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::stringstream extraInfoStream;
220 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
221 << " (or there is no acceptable conversion)";
222 std::string extraInfo = extraInfoStream.str();
223 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224}
225
226//
227// Same error message for all binary operations don't work.
228//
229void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
230{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000231 std::stringstream extraInfoStream;
232 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
233 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
234 std::string extraInfo = extraInfoStream.str();
235 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236}
237
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000238bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000239 if (!checksPrecisionErrors)
240 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 switch( type ){
242 case EbtFloat:
243 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000244 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000245 return true;
246 }
247 break;
248 case EbtInt:
249 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000250 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000251 return true;
252 }
253 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000254 default:
255 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000256 }
257 return false;
258}
259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260//
261// Both test and if necessary, spit out an error, to see if the node is really
262// an l-value that can be operated on this way.
263//
264// Returns true if the was an error.
265//
266bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
267{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000268 TIntermSymbol* symNode = node->getAsSymbolNode();
269 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000271 if (binaryNode) {
272 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000274 switch(binaryNode->getOp()) {
275 case EOpIndexDirect:
276 case EOpIndexIndirect:
277 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000278 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000279 return lValueErrorCheck(line, op, binaryNode->getLeft());
280 case EOpVectorSwizzle:
281 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
282 if (!errorReturn) {
283 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000285 TIntermTyped* rightNode = binaryNode->getRight();
286 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
287
288 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
289 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000290 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000291 offset[value]++;
292 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000293 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 return true;
296 }
297 }
298 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 return errorReturn;
301 default:
302 break;
303 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000304 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 return true;
307 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
309
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000310 const char* symbol = 0;
311 if (symNode != 0)
312 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 const char* message = 0;
315 switch (node->getQualifier()) {
316 case EvqConst: message = "can't modify a const"; break;
317 case EvqConstReadOnly: message = "can't modify a const"; break;
318 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madillb120eac2013-06-12 14:08:13 -0400319 case EvqVertexInput: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000320 case EvqUniform: message = "can't modify a uniform"; break;
321 case EvqVaryingIn: message = "can't modify a varying"; break;
322 case EvqInput: message = "can't modify an input"; break;
323 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
324 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
325 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
326 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 //
329 // Type that can't be written to?
330 //
331 switch (node->getBasicType()) {
332 case EbtSampler2D:
333 case EbtSamplerCube:
334 message = "can't modify a sampler";
335 break;
336 case EbtVoid:
337 message = "can't modify void";
338 break;
339 default:
340 break;
341 }
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000345 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 return true;
348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
350
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 //
352 // Everything else is okay, no error.
353 //
354 if (message == 0)
355 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000357 //
358 // If we get here, we have an error and a message.
359 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000360 if (symNode) {
361 std::stringstream extraInfoStream;
362 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
363 std::string extraInfo = extraInfoStream.str();
364 error(line, " l-value required", op, extraInfo.c_str());
365 }
366 else {
367 std::stringstream extraInfoStream;
368 extraInfoStream << "(" << message << ")";
369 std::string extraInfo = extraInfoStream.str();
370 error(line, " l-value required", op, extraInfo.c_str());
371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000373 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374}
375
376//
377// Both test, and if necessary spit out an error, to see if the node is really
378// a constant.
379//
380// Returns true if the was an error.
381//
382bool TParseContext::constErrorCheck(TIntermTyped* node)
383{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000384 if (node->getQualifier() == EvqConst)
385 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000387 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000389 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
392//
393// Both test, and if necessary spit out an error, to see if the node is really
394// an integer.
395//
396// Returns true if the was an error.
397//
398bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
399{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000400 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000403 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000405 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
408//
409// Both test, and if necessary spit out an error, to see if we are currently
410// globally scoped.
411//
412// Returns true if the was an error.
413//
414bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
415{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 if (global)
417 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000419 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000421 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
424//
425// For now, keep it simple: if it starts "gl_", it's reserved, independent
426// of scope. Except, if the symbol table is at the built-in push-level,
427// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000428// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
429// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430//
431// Returns true if there was an error.
432//
433bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
434{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000435 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000437 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000438 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000439 return true;
440 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000441 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000442 if (identifier.compare(0, 6, "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 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000446 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000447 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000448 return true;
449 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000450 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000451 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000452 return true;
453 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000454 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000456 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000457 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 }
459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000461 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462}
463
464//
465// Make sure there is enough data provided to the constructor to build
466// something of the type of the constructor. Also returns the type of
467// the constructor.
468//
469// Returns true if there was an error in construction.
470//
471bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
472{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000473 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000475 bool constructingMatrix = false;
476 switch(op) {
477 case EOpConstructMat2:
478 case EOpConstructMat3:
479 case EOpConstructMat4:
480 constructingMatrix = true;
481 break;
482 default:
483 break;
484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 //
487 // Note: It's okay to have too many components available, but not okay to have unused
488 // arguments. 'full' will go to true when enough args have been seen. If we loop
489 // again, there is an extra argument, so 'overfull' will become true.
490 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000492 int size = 0;
493 bool constType = true;
494 bool full = false;
495 bool overFull = false;
496 bool matrixInMatrix = false;
497 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000498 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000499 const TParameter& param = function.getParam(i);
500 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000502 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 matrixInMatrix = true;
504 if (full)
505 overFull = true;
506 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
507 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000508 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000510 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000511 arrayArg = true;
512 }
513
514 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000515 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000517 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000518 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 return true;
520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000523 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 return true;
525 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000528 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000529 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000530 return true;
531 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 return true;
537 }
538
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000539 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 return true;
542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000544 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000545 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
546 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000547 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000548 return true;
549 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000552 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000554 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000555 return true;
556 }
557 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 return true;
560 }
561 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000562 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 return true;
564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567}
568
569// This function checks to see if a void variable has been declared and raise an error message for such a case
570//
571// returns true in case of an error
572//
573bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
574{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000576 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 return true;
578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581}
582
583// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
584//
585// returns true in case of an error
586//
587bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
588{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000590 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 return true;
592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595}
596
597// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
598//
599// returns true in case of an error
600//
601bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
602{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000603 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000604 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return true;
606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609}
610
611bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
612{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 if (pType.type == EbtStruct) {
614 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000615 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616
617 return true;
618 }
619
620 return false;
621 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000622 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 return true;
625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628}
629
630bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
631{
Jamie Madillb120eac2013-06-12 14:08:13 -0400632 switch (pType.qualifier)
633 {
634 case EvqVaryingIn:
635 case EvqVaryingOut:
636 case EvqAttribute:
637 case EvqVertexInput:
638 case EvqFragmentOutput:
639 if (pType.type == EbtStruct)
640 {
641 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
642 return true;
643 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
647 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
652bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
653{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000654 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
655 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000656 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 return true;
658 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
663bool TParseContext::containsSampler(TType& type)
664{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 if (IsSampler(type.getBasicType()))
666 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000668 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000669 TTypeList& structure = *type.getStruct();
670 for (unsigned int i = 0; i < structure.size(); ++i) {
671 if (containsSampler(*structure[i].type))
672 return true;
673 }
674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677}
678
679//
680// Do size checking for an array type's size.
681//
682// Returns true if there was an error.
683//
684bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
685{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000687
688 if (constant == 0 || !constant->isScalarInt())
689 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000690 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 return true;
692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000694 if (constant->getBasicType() == EbtUInt)
695 {
696 unsigned int uintSize = constant->getUConst(0);
697 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
698 {
699 error(line, "array size too large", "");
700 size = 1;
701 return true;
702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000704 size = static_cast<int>(uintSize);
705 }
706 else
707 {
708 size = constant->getIConst(0);
709
710 if (size <= 0)
711 {
712 error(line, "array size must be a positive integer", "");
713 size = 1;
714 return true;
715 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000718 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719}
720
721//
722// See if this qualifier can be an array.
723//
724// Returns true if there is an error.
725//
726bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
727{
Jamie Madillb120eac2013-06-12 14:08:13 -0400728 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000729 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000730 return true;
731 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000733 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734}
735
736//
737// See if this type can be an array.
738//
739// Returns true if there is an error.
740//
741bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
742{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 //
744 // Can the type be an array?
745 //
746 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000747 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 return true;
749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752}
753
754//
755// Do all the semantic checking for declaring an array, with and
756// without a size, and make the right changes to the symbol table.
757//
758// size == 0 means no specified size.
759//
760// Returns true if there was an error.
761//
Jamie Madill60ed9812013-06-06 11:56:46 -0400762bool TParseContext::arrayErrorCheck(int line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000764 //
765 // Don't check for reserved word use until after we know it's not in the symbol table,
766 // because reserved arrays can be redeclared.
767 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 bool builtIn = false;
770 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000771 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 if (symbol == 0 || !sameScope) {
773 if (reservedErrorCheck(line, identifier))
774 return true;
775
776 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 if (type.arraySize)
779 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000781 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000782 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000783 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000784 return true;
785 }
786 } else {
787 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000788 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000789 return true;
790 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000792 variable = static_cast<TVariable*>(symbol);
793 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000794 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 return true;
796 }
797 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000798 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 return true;
800 }
801
802 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000803 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 return true;
805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 TType* t = variable->getArrayInformationType();
808 while (t != 0) {
809 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000810 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return true;
812 }
813 t->setArraySize(type.arraySize);
814 t = t->getArrayInformationType();
815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 if (type.arraySize)
818 variable->getType().setArraySize(type.arraySize);
819 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 if (voidErrorCheck(line, identifier, type))
822 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825}
826
827bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
828{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 bool builtIn = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000830 TSymbol* symbol = symbolTable.find(node->getSymbol(), 0, &builtIn);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000831 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000832 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 return true;
834 }
835 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 type->setArrayInformationType(variable->getArrayInformationType());
838 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
841 // its an error
842 if (node->getSymbol() == "gl_FragData") {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000843 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", 0, &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000844 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
847 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000848 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 return true;
850 }
851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
854 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
855 if (!updateFlag)
856 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 size++;
859 variable->getType().setMaxArraySize(size);
860 type->setMaxArraySize(size);
861 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 while(tt->getArrayInformationType() != 0) {
864 tt = tt->getArrayInformationType();
865 tt->setMaxArraySize(size);
866 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869}
870
871//
872// Enforce non-initializer type/qualifier rules.
873//
874// Returns true if there was an error.
875//
Jamie Madill60ed9812013-06-06 11:56:46 -0400876bool TParseContext::nonInitConstErrorCheck(int line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 if (type.qualifier == EvqConst)
879 {
880 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000882
883 if (array)
884 {
885 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
886 }
887 else if (type.isStructureContainingArrays())
888 {
889 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
890 }
891 else
892 {
893 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
894 }
895
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return true;
897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900}
901
902//
903// Do semantic checking for a variable declaration that has no initializer,
904// and update the symbol table.
905//
906// Returns true if there was an error.
907//
Jamie Madill60ed9812013-06-06 11:56:46 -0400908bool TParseContext::nonInitErrorCheck(int line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 if (reservedErrorCheck(line, identifier))
911 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
zmo@google.comfd747b82011-04-23 01:30:07 +0000913 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000915 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000916 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000918 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 return true;
920 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 if (voidErrorCheck(line, identifier, type))
923 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926}
927
928bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
929{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000931 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 return true;
933 }
934 if (qualifier == EvqConst && paramQualifier != EvqIn) {
935 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
936 return true;
937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000939 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000940 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000942 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000944 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945}
946
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000947bool TParseContext::extensionErrorCheck(int line, const TString& extension)
948{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000949 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000950 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
951 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000952 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 return true;
954 }
zmo@google.comf5450912011-09-09 01:37:19 +0000955 // In GLSL ES, an extension's default behavior is "disable".
956 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000957 error(line, "extension", extension.c_str(), "is disabled");
958 return true;
959 }
960 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000961 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000962 return false;
963 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000965 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
zmo@google.com09c323a2011-08-12 18:22:25 +0000968bool TParseContext::supportsExtension(const char* extension)
969{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000970 const TExtensionBehavior& extbehavior = extensionBehavior();
971 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
972 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000973}
974
975void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
976{
977 pp::SourceLocation loc;
978 DecodeSourceLoc(line, &loc.file, &loc.line);
979 directiveHandler.handleExtension(loc, extName, behavior);
980}
981
982void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
983{
984 pp::SourceLocation loc;
985 DecodeSourceLoc(line, &loc.file, &loc.line);
986 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000987}
988
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989/////////////////////////////////////////////////////////////////////////////////
990//
991// Non-Errors.
992//
993/////////////////////////////////////////////////////////////////////////////////
994
995//
996// Look up a function name in the symbol table, and make sure it is a function.
997//
998// Return the function symbol if found, otherwise 0.
999//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001000const TFunction* TParseContext::findFunction(int line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001002 // First find by unmangled name to check whether the function name has been
1003 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001004 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001005 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001006 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001007 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008
alokp@chromium.org0a576182010-08-09 17:16:27 +00001009 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001010 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001011 return 0;
1012 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013
alokp@chromium.org0a576182010-08-09 17:16:27 +00001014 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001015 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001016 return 0;
1017 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001018
1019 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
1022//
1023// Initializers show up in several places in the grammar. Have one set of
1024// code to handle them here.
1025//
Jamie Madill60ed9812013-06-06 11:56:46 -04001026bool TParseContext::executeInitializer(TSourceLoc line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001027 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 if (variable == 0) {
1032 if (reservedErrorCheck(line, identifier))
1033 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 if (voidErrorCheck(line, identifier, pType))
1036 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 //
1039 // add variable to symbol table
1040 //
1041 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001042 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001043 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001044 return true;
1045 // don't delete variable, it's used by error recovery, and the pool
1046 // pop will take care of the memory
1047 }
1048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001050 //
1051 // identifier must be of type constant, a global, or a temporary
1052 //
1053 TQualifier qualifier = variable->getType().getQualifier();
1054 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001055 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001056 return true;
1057 }
1058 //
1059 // test for and propagate constant
1060 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 if (qualifier == EvqConst) {
1063 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001064 std::stringstream extraInfoStream;
1065 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1066 std::string extraInfo = extraInfoStream.str();
1067 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001068 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 return true;
1070 }
1071 if (type != initializer->getType()) {
1072 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001073 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001074 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 return true;
1076 }
1077 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001078 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1081 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1082 } else {
1083 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1084 }
1085 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001086 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001089 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 variable->shareConstPointer(constArray);
1091 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001092 std::stringstream extraInfoStream;
1093 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1094 std::string extraInfo = extraInfoStream.str();
1095 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001096 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001097 return true;
1098 }
1099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 if (qualifier != EvqConst) {
1102 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1103 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1104 if (intermNode == 0) {
1105 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1106 return true;
1107 }
1108 } else
1109 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112}
1113
1114bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1115{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001116 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001117 if (!aggrNode->isConstructor())
1118 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 // check if all the child nodes are constants so that they can be inserted into
1123 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001124 TIntermSequence &sequence = aggrNode->getSequence() ;
1125 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1126 if (!(*p)->getAsTyped()->getAsConstantUnion())
1127 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131}
1132
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001133TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, const TPublicType& typeSpecifier)
1134{
1135 TPublicType returnType = typeSpecifier;
1136 returnType.qualifier = qualifier;
1137
1138 if (typeSpecifier.array)
1139 {
1140 error(typeSpecifier.line, "not supported", "first-class array");
1141 recover();
1142 returnType.setArray(false);
1143 }
1144
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001145 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001146 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001147 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1148 {
1149 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1150 recover();
1151 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001152
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001153 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1154 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1155 {
1156 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1157 recover();
1158 }
1159 }
1160 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001161 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001162 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001163 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001164 case EvqVertexInput:
1165 case EvqFragmentOutput:
1166 case EvqVaryingIn:
1167 case EvqVaryingOut:
1168 if (typeSpecifier.type == EbtBool)
1169 {
1170 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1171 recover();
1172 }
1173 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001174
Jamie Madillb120eac2013-06-12 14:08:13 -04001175 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001176 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001177 }
1178
1179 return returnType;
1180}
1181
Jamie Madill60ed9812013-06-06 11:56:46 -04001182TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier)
1183{
1184 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1185 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1186
1187 if (identifier != "")
1188 {
1189 if (structQualifierErrorCheck(identifierLocation, publicType))
1190 recover();
1191
1192 // this error check can mutate the type
1193 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1194 recover();
1195
1196 TVariable* variable = 0;
1197
1198 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1199 recover();
1200
1201 if (variable && symbol)
1202 {
1203 symbol->setId(variable->getUniqueId());
1204 }
1205 }
1206
1207 return aggregate;
1208}
1209
1210TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc indexLocation, TIntermTyped *indexExpression)
1211{
1212 if (structQualifierErrorCheck(identifierLocation, publicType))
1213 recover();
1214
1215 // this error check can mutate the type
1216 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1217 recover();
1218
1219 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1220 {
1221 recover();
1222 }
1223
1224 TPublicType arrayType = publicType;
1225
1226 int size;
1227 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1228 {
1229 recover();
1230 }
1231 else
1232 {
1233 arrayType.setArray(true, size);
1234 }
1235
1236 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1237 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1238 TVariable* variable = 0;
1239
1240 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1241 recover();
1242
1243 if (variable && symbol)
1244 {
1245 symbol->setId(variable->getUniqueId());
1246 }
1247
1248 return aggregate;
1249}
1250
1251TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, TSourceLoc identifierLocation, const TString &identifier, TSourceLoc initLocation, TIntermTyped *initializer)
1252{
1253 if (structQualifierErrorCheck(identifierLocation, publicType))
1254 recover();
1255
1256 TIntermNode* intermNode;
1257 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1258 {
1259 //
1260 // Build intermediate representation
1261 //
1262 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1263 }
1264 else
1265 {
1266 recover();
1267 return NULL;
1268 }
1269}
1270
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001271TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1272{
1273 TOperator op = EOpNull;
1274 if (publicType.userDef)
1275 {
1276 op = EOpConstructStruct;
1277 }
1278 else
1279 {
1280 switch (publicType.type)
1281 {
1282 case EbtFloat:
1283 if (publicType.isMatrix())
1284 {
1285 // TODO: non-square matrices
1286 switch(publicType.getCols())
1287 {
1288 case 2: op = EOpConstructMat2; break;
1289 case 3: op = EOpConstructMat3; break;
1290 case 4: op = EOpConstructMat4; break;
1291 }
1292 }
1293 else
1294 {
1295 switch(publicType.getNominalSize())
1296 {
1297 case 1: op = EOpConstructFloat; break;
1298 case 2: op = EOpConstructVec2; break;
1299 case 3: op = EOpConstructVec3; break;
1300 case 4: op = EOpConstructVec4; break;
1301 }
1302 }
1303 break;
1304
1305 case EbtInt:
1306 switch(publicType.getNominalSize())
1307 {
1308 case 1: op = EOpConstructInt; break;
1309 case 2: op = EOpConstructIVec2; break;
1310 case 3: op = EOpConstructIVec3; break;
1311 case 4: op = EOpConstructIVec4; break;
1312 }
1313 break;
1314
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001315 case EbtUInt:
1316 switch(publicType.getNominalSize())
1317 {
Nicolas Capensab60b932013-06-05 10:31:21 -04001318 case 1: op = EOpConstructUInt; break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001319 case 2: op = EOpConstructUVec2; break;
1320 case 3: op = EOpConstructUVec3; break;
1321 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001322 }
1323 break;
1324
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001325 case EbtBool:
1326 switch(publicType.getNominalSize())
1327 {
1328 case 1: op = EOpConstructBool; break;
1329 case 2: op = EOpConstructBVec2; break;
1330 case 3: op = EOpConstructBVec3; break;
1331 case 4: op = EOpConstructBVec4; break;
1332 }
1333 break;
1334
1335 default: break;
1336 }
1337
1338 if (op == EOpNull)
1339 {
1340 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1341 recover();
1342 publicType.type = EbtFloat;
1343 op = EOpConstructFloat;
1344 }
1345 }
1346
1347 TString tempString;
1348 TType type(publicType);
1349 return new TFunction(&tempString, type, op);
1350}
1351
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001352// This function is used to test for the correctness of the parameters passed to various constructor functions
1353// and also convert them to the right datatype if it is allowed and required.
1354//
1355// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1356//
1357TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1358{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001359 if (node == 0)
1360 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001361
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001362 TIntermAggregate* aggrNode = node->getAsAggregate();
1363
alokp@chromium.org58e54292010-08-24 21:40:03 +00001364 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001365 if (op == EOpConstructStruct)
1366 memberTypes = type->getStruct()->begin();
1367
1368 TType elementType = *type;
1369 if (type->isArray())
1370 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001371
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001372 bool singleArg;
1373 if (aggrNode) {
1374 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1375 singleArg = true;
1376 else
1377 singleArg = false;
1378 } else
1379 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001380
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001381 TIntermTyped *newNode;
1382 if (singleArg) {
1383 // If structure constructor or array constructor is being called
1384 // for only one parameter inside the structure, we need to call constructStruct function once.
1385 if (type->isArray())
1386 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1387 else if (op == EOpConstructStruct)
1388 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1389 else
1390 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001392 if (newNode && newNode->getAsAggregate()) {
1393 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1394 if (constConstructor)
1395 return constConstructor;
1396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001398 return newNode;
1399 }
1400
1401 //
1402 // Handle list of arguments.
1403 //
1404 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1405 // if the structure constructor contains more than one parameter, then construct
1406 // each parameter
1407
1408 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1409
1410 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1411 // to the right type if possible (and allowed).
1412 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1413
1414 for (TIntermSequence::iterator p = sequenceVector.begin();
1415 p != sequenceVector.end(); p++, paramCount++) {
1416 if (type->isArray())
1417 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1418 else if (op == EOpConstructStruct)
1419 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1420 else
1421 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1422
1423 if (newNode) {
1424 *p = newNode;
1425 }
1426 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001428 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1429 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1430 if (constConstructor)
1431 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001433 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434}
1435
1436TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1437{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001438 bool canBeFolded = areAllChildConst(aggrNode);
1439 aggrNode->setType(type);
1440 if (canBeFolded) {
1441 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001442 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001443 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001444 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001445 }
1446 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001447 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001448 }
1449 if (returnVal)
1450 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001451
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001452 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001454
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001455 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456}
1457
1458// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1459// for the parameter to the constructor (passed to this function). Essentially, it converts
1460// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1461// float, then float is converted to int.
1462//
1463// Returns 0 for an error or the constructed node.
1464//
1465TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1466{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001467 TIntermTyped* newNode;
1468 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001470 //
1471 // First, convert types as needed.
1472 //
1473 switch (op) {
1474 case EOpConstructVec2:
1475 case EOpConstructVec3:
1476 case EOpConstructVec4:
1477 case EOpConstructMat2:
1478 case EOpConstructMat3:
1479 case EOpConstructMat4:
1480 case EOpConstructFloat:
1481 basicOp = EOpConstructFloat;
1482 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001484 case EOpConstructIVec2:
1485 case EOpConstructIVec3:
1486 case EOpConstructIVec4:
1487 case EOpConstructInt:
1488 basicOp = EOpConstructInt;
1489 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001491 case EOpConstructUVec2:
1492 case EOpConstructUVec3:
1493 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001494 case EOpConstructUInt:
1495 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001496 break;
1497
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001498 case EOpConstructBVec2:
1499 case EOpConstructBVec3:
1500 case EOpConstructBVec4:
1501 case EOpConstructBool:
1502 basicOp = EOpConstructBool;
1503 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001505 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001506 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001507 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001508
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001509 return 0;
1510 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001511 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001512 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001513 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001514 return 0;
1515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001516
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001517 //
1518 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1519 //
1520
1521 // Otherwise, skip out early.
1522 if (subset || (newNode != node && newNode->getType() == *type))
1523 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001525 // setAggregateOperator will insert a new node for the constructor, as needed.
1526 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527}
1528
1529// This function tests for the type of the parameters to the structures constructors. Raises
1530// an error message if the expected type does not match the parameter passed to the constructor.
1531//
1532// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1533//
1534TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1535{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001536 if (*type == node->getAsTyped()->getType()) {
1537 if (subset)
1538 return node->getAsTyped();
1539 else
1540 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1541 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001542 std::stringstream extraInfoStream;
1543 extraInfoStream << "cannot convert parameter " << paramCount
1544 << " from '" << node->getAsTyped()->getType().getBasicString()
1545 << "' to '" << type->getBasicString() << "'";
1546 std::string extraInfo = extraInfoStream.str();
1547 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001548 recover();
1549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001551 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552}
1553
1554//
1555// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1556// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1557// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1558// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1559// a constant matrix.
1560//
1561TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1562{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001563 TIntermTyped* typedNode;
1564 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001566 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001567 if (tempConstantNode) {
1568 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001570 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001571 return node;
1572 }
1573 } 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 +00001574 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001575 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001577 return 0;
1578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001580 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001582 for (int i = 0; i < fields.num; i++) {
1583 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001584 std::stringstream extraInfoStream;
1585 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1586 std::string extraInfo = extraInfoStream.str();
1587 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001588 recover();
1589 fields.offsets[i] = 0;
1590 }
1591
1592 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001594 }
1595 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1596 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597}
1598
1599//
1600// This function returns the column being accessed from a constant matrix. The values are retrieved from
1601// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1602// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1603// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1604//
1605TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1606{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001607 TIntermTyped* typedNode;
1608 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001610 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001611 std::stringstream extraInfoStream;
1612 extraInfoStream << "matrix field selection out of range '" << index << "'";
1613 std::string extraInfo = extraInfoStream.str();
1614 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001615 recover();
1616 index = 0;
1617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001619 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001620 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001621 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001622 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1623 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001624 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001625 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001627 return 0;
1628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001630 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631}
1632
1633
1634//
1635// This function returns an element of an array accessed from a constant array. The values are retrieved from
1636// the symbol table and parse-tree is built for the type of the element. The input
1637// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1638// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1639//
1640TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1641{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001642 TIntermTyped* typedNode;
1643 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1644 TType arrayElementType = node->getType();
1645 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001647 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001648 std::stringstream extraInfoStream;
1649 extraInfoStream << "array field selection out of range '" << index << "'";
1650 std::string extraInfo = extraInfoStream.str();
1651 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001652 recover();
1653 index = 0;
1654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001656 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001658 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001659 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001660 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1661 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001662 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001663 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001665 return 0;
1666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001668 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669}
1670
1671
1672//
1673// This function returns the value of a particular field inside a constant structure from the symbol table.
1674// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1675// function and returns the parse-tree with the values of the embedded/nested struct.
1676//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001677TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, TSourceLoc line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001678{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001679 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001680 TIntermTyped *typedNode;
1681 int instanceSize = 0;
1682 unsigned int index = 0;
1683 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001685 for ( index = 0; index < fields->size(); ++index) {
1686 if ((*fields)[index].type->getFieldName() == identifier) {
1687 break;
1688 } else {
1689 instanceSize += (*fields)[index].type->getObjectSize();
1690 }
1691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001693 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001694 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001696 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1697 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001698 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001699 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001701 return 0;
1702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001704 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705}
1706
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001707//
1708// Interface/uniform blocks
1709//
1710TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, TSourceLoc nameLine, const TString& blockName, TTypeList* typeList,
1711 const TString& instanceName, TSourceLoc instanceLine, TIntermTyped* arrayIndex, TSourceLoc arrayIndexLine)
1712{
1713 if (reservedErrorCheck(nameLine, blockName))
1714 recover();
1715
1716 if (typeQualifier.qualifier != EvqUniform)
1717 {
1718 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1719 recover();
1720 }
1721
1722 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1723 if (!symbolTable.declare(*blockNameSymbol)) {
1724 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1725 recover();
1726 }
1727
1728 // check for sampler types
1729 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1730 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1731 TType* memberType = memberTypeLine.type;
1732 if (IsSampler(memberType->getBasicType())) {
1733 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1734 recover();
1735 }
1736
1737 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1738 switch (qualifier)
1739 {
1740 case EvqGlobal:
1741 case EvqUniform:
1742 break;
1743 default:
1744 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1745 recover();
1746 break;
1747 }
1748 }
1749
1750 TType* interfaceBlock = new TType(typeList, blockName);
1751 interfaceBlock->setBasicType(EbtInterfaceBlock);
1752 interfaceBlock->setQualifier(typeQualifier.qualifier);
1753
1754 TString symbolName = "";
1755 int symbolId = 0;
1756
1757 if (instanceName == "")
1758 {
1759 // define symbols for the members of the interface block
1760 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1761 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1762 TType* memberType = memberTypeLine.type;
1763 memberType->setInterfaceBlockType(interfaceBlock);
1764 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1765 memberVariable->setQualifier(typeQualifier.qualifier);
1766 if (!symbolTable.declare(*memberVariable)) {
1767 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1768 recover();
1769 }
1770 }
1771 }
1772 else
1773 {
1774 interfaceBlock->setInstanceName(instanceName);
1775
1776 // add array index
1777 if (arrayIndex != NULL)
1778 {
1779 int size;
1780 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
1781 recover();
1782 interfaceBlock->setArraySize(size);
1783 }
1784
1785 // add a symbol for this interface block
1786 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
1787 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1788 if (!symbolTable.declare(*instanceTypeDef)) {
1789 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
1790 recover();
1791 }
1792
1793 symbolId = instanceTypeDef->getUniqueId();
1794 symbolName = instanceTypeDef->getName();
1795 }
1796
1797 exitStructDeclaration();
1798 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
1799 aggregate->setOp(EOpDeclaration);
1800 return aggregate;
1801}
1802
kbr@chromium.org476541f2011-10-27 21:14:51 +00001803bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1804{
1805 ++structNestingLevel;
1806
1807 // Embedded structure definitions are not supported per GLSL ES spec.
1808 // They aren't allowed in GLSL either, but we need to detect this here
1809 // so we don't rely on the GLSL compiler to catch it.
1810 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001811 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001812 return true;
1813 }
1814
1815 return false;
1816}
1817
1818void TParseContext::exitStructDeclaration()
1819{
1820 --structNestingLevel;
1821}
1822
1823namespace {
1824
1825const int kWebGLMaxStructNesting = 4;
1826
1827} // namespace
1828
1829bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1830{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001831 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001832 return false;
1833 }
1834
1835 if (fieldType.getBasicType() != EbtStruct) {
1836 return false;
1837 }
1838
1839 // We're already inside a structure definition at this point, so add
1840 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001841 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001842 std::stringstream extraInfoStream;
1843 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
1844 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
1845 std::string extraInfo = extraInfoStream.str();
1846 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00001847 return true;
1848 }
1849
1850 return false;
1851}
1852
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001853//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001854// Parse an array index expression
1855//
1856TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, TSourceLoc location, TIntermTyped *indexExpression)
1857{
1858 TIntermTyped *indexedExpression = NULL;
1859
1860 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
1861 {
1862 if (baseExpression->getAsSymbolNode())
1863 {
1864 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
1865 }
1866 else
1867 {
1868 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
1869 }
1870 recover();
1871 }
1872 if (baseExpression->getType().getQualifier() == EvqConst && indexExpression->getQualifier() == EvqConst)
1873 {
1874 if (baseExpression->isArray())
1875 {
1876 // constant folding for arrays
1877 indexedExpression = addConstArrayNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
1878 }
1879 else if (baseExpression->isVector())
1880 {
1881 // constant folding for vectors
1882 TVectorFields fields;
1883 fields.num = 1;
1884 fields.offsets[0] = indexExpression->getAsConstantUnion()->getIConst(0); // need to do it this way because v.xy sends fields integer array
1885 indexedExpression = addConstVectorNode(fields, baseExpression, location);
1886 }
1887 else if (baseExpression->isMatrix())
1888 {
1889 // constant folding for matrices
1890 indexedExpression = addConstMatrixNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
1891 }
1892 }
1893 else
1894 {
1895 if (indexExpression->getQualifier() == EvqConst)
1896 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001897 const bool isMatrixOrVector = (baseExpression->isVector() || baseExpression->isMatrix());
1898 const bool indexOOR = (isMatrixOrVector && baseExpression->getType().getNominalSize() <= indexExpression->getAsConstantUnion()->getIConst(0));
1899
1900 // check for index out-of-range
1901 if (indexOOR && !baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001902 {
1903 std::stringstream extraInfoStream;
1904 extraInfoStream << "field selection out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
1905 std::string extraInfo = extraInfoStream.str();
1906 error(location, "", "[", extraInfo.c_str());
1907 recover();
1908 }
1909 else
1910 {
1911 if (baseExpression->isArray())
1912 {
1913 if (baseExpression->getType().getArraySize() == 0)
1914 {
1915 if (baseExpression->getType().getMaxArraySize() <= indexExpression->getAsConstantUnion()->getIConst(0))
1916 {
1917 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), indexExpression->getAsConstantUnion()->getIConst(0), true, location))
1918 recover();
1919 }
1920 else
1921 {
1922 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), 0, false, location))
1923 recover();
1924 }
1925 }
1926 else if ( indexExpression->getAsConstantUnion()->getIConst(0) >= baseExpression->getType().getArraySize())
1927 {
1928 std::stringstream extraInfoStream;
1929 extraInfoStream << "array index out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
1930 std::string extraInfo = extraInfoStream.str();
1931 error(location, "", "[", extraInfo.c_str());
1932 recover();
1933 }
1934 }
1935 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
1936 }
1937 }
1938 else
1939 {
1940 if (baseExpression->isArray() && baseExpression->getType().getArraySize() == 0)
1941 {
1942 error(location, "", "[", "array must be redeclared with a size before being indexed with a variable");
1943 recover();
1944 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00001945 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
1946 {
1947 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
1948 recover();
1949 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001950
1951 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
1952 }
1953 }
1954 if (indexedExpression == 0)
1955 {
1956 ConstantUnion *unionArray = new ConstantUnion[1];
1957 unionArray->setFConst(0.0f);
1958 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
1959 }
1960 else if (baseExpression->isArray())
1961 {
1962 if (baseExpression->getType().getStruct())
1963 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00001964 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
1965 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
1966 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001967 }
1968 else
1969 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001970 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001971 }
1972
1973 if (baseExpression->getType().getQualifier() == EvqConst)
1974 {
1975 indexedExpression->getTypePointer()->setQualifier(EvqConst);
1976 }
1977 }
1978 else if (baseExpression->isMatrix() && baseExpression->getType().getQualifier() == EvqConst)
1979 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001980 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001981 }
1982 else if (baseExpression->isMatrix())
1983 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001984 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001985 }
1986 else if (baseExpression->isVector() && baseExpression->getType().getQualifier() == EvqConst)
1987 {
1988 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst));
1989 }
1990 else if (baseExpression->isVector())
1991 {
1992 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
1993 }
1994 else
1995 {
1996 indexedExpression->setType(baseExpression->getType());
1997 }
1998
1999 return indexedExpression;
2000}
2001
2002TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, TSourceLoc dotLocation, const TString &fieldString, TSourceLoc fieldLocation)
2003{
2004 TIntermTyped *indexedExpression = NULL;
2005
2006 if (baseExpression->isArray())
2007 {
2008 error(fieldLocation, "cannot apply dot operator to an array", ".");
2009 recover();
2010 }
2011
2012 if (baseExpression->isVector())
2013 {
2014 TVectorFields fields;
2015 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2016 {
2017 fields.num = 1;
2018 fields.offsets[0] = 0;
2019 recover();
2020 }
2021
2022 if (baseExpression->getType().getQualifier() == EvqConst)
2023 {
2024 // constant folding for vector fields
2025 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2026 if (indexedExpression == 0)
2027 {
2028 recover();
2029 indexedExpression = baseExpression;
2030 }
2031 else
2032 {
2033 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2034 }
2035 }
2036 else
2037 {
2038 TString vectorString = fieldString;
2039 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2040 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2041 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2042 }
2043 }
2044 else if (baseExpression->isMatrix())
2045 {
2046 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002047 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002048 {
2049 fields.wholeRow = false;
2050 fields.wholeCol = false;
2051 fields.row = 0;
2052 fields.col = 0;
2053 recover();
2054 }
2055
2056 if (fields.wholeRow || fields.wholeCol)
2057 {
2058 error(dotLocation, " non-scalar fields not implemented yet", ".");
2059 recover();
2060 ConstantUnion *unionArray = new ConstantUnion[1];
2061 unionArray->setIConst(0);
2062 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2063 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002064 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002065 }
2066 else
2067 {
2068 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002069 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002070 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2071 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2072 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2073 }
2074 }
2075 else if (baseExpression->getBasicType() == EbtStruct)
2076 {
2077 bool fieldFound = false;
2078 const TTypeList* fields = baseExpression->getType().getStruct();
2079 if (fields == 0)
2080 {
2081 error(dotLocation, "structure has no fields", "Internal Error");
2082 recover();
2083 indexedExpression = baseExpression;
2084 }
2085 else
2086 {
2087 unsigned int i;
2088 for (i = 0; i < fields->size(); ++i)
2089 {
2090 if ((*fields)[i].type->getFieldName() == fieldString)
2091 {
2092 fieldFound = true;
2093 break;
2094 }
2095 }
2096 if (fieldFound)
2097 {
2098 if (baseExpression->getType().getQualifier() == EvqConst)
2099 {
2100 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2101 if (indexedExpression == 0)
2102 {
2103 recover();
2104 indexedExpression = baseExpression;
2105 }
2106 else
2107 {
2108 indexedExpression->setType(*(*fields)[i].type);
2109 // change the qualifier of the return type, not of the structure field
2110 // as the structure definition is shared between various structures.
2111 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2112 }
2113 }
2114 else
2115 {
2116 ConstantUnion *unionArray = new ConstantUnion[1];
2117 unionArray->setIConst(i);
2118 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2119 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2120 indexedExpression->setType(*(*fields)[i].type);
2121 }
2122 }
2123 else
2124 {
2125 error(dotLocation, " no such field in structure", fieldString.c_str());
2126 recover();
2127 indexedExpression = baseExpression;
2128 }
2129 }
2130 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002131 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2132 {
2133 bool fieldFound = false;
2134 const TTypeList* fields = baseExpression->getType().getStruct();
2135 if (fields == 0)
2136 {
2137 error(dotLocation, "interface block has no fields", "Internal Error");
2138 recover();
2139 indexedExpression = baseExpression;
2140 }
2141 else
2142 {
2143 unsigned int i;
2144 for (i = 0; i < fields->size(); ++i)
2145 {
2146 if ((*fields)[i].type->getFieldName() == fieldString)
2147 {
2148 fieldFound = true;
2149 break;
2150 }
2151 }
2152 if (fieldFound)
2153 {
2154 ConstantUnion *unionArray = new ConstantUnion[1];
2155 unionArray->setIConst(i);
2156 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2157 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2158 indexedExpression->setType(*(*fields)[i].type);
2159 }
2160 else
2161 {
2162 error(dotLocation, " no such field in interface block", fieldString.c_str());
2163 recover();
2164 indexedExpression = baseExpression;
2165 }
2166 }
2167 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002168 else
2169 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002170 if (shaderVersion < 300)
2171 {
2172 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2173 }
2174 else
2175 {
2176 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2177 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002178 recover();
2179 indexedExpression = baseExpression;
2180 }
2181
2182 return indexedExpression;
2183}
2184
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002185TLayoutQualifierId TParseContext::addLayoutQualifierId(const TString &qualifierType, TSourceLoc qualifierTypeLine)
2186{
2187 TLayoutQualifierId qualifierId;
2188
2189 qualifierId.type = ElqError;
2190 qualifierId.location = -1;
2191
2192 if (qualifierType == "shared")
2193 {
2194 qualifierId.type = ElqShared;
2195 }
2196 else if (qualifierType == "packed")
2197 {
2198 qualifierId.type = ElqPacked;
2199 }
2200 else if (qualifierType == "std140")
2201 {
2202 qualifierId.type = ElqStd140;
2203 }
2204 else if (qualifierType == "row_major")
2205 {
2206 qualifierId.type = ElqRowMajor;
2207 }
2208 else if (qualifierType == "column_major")
2209 {
2210 qualifierId.type = ElqColumnMajor;
2211 }
2212 else if (qualifierType == "location")
2213 {
2214 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2215 recover();
2216 }
2217 else
2218 {
2219 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2220 recover();
2221 }
2222
2223 return qualifierId;
2224}
2225
2226TLayoutQualifierId TParseContext::addLayoutQualifierId(const TString &qualifierType, TSourceLoc qualifierTypeLine, const TString &intValueString, int intValue, TSourceLoc intValueLine)
2227{
2228 TLayoutQualifierId qualifierId;
2229
2230 qualifierId.type = ElqError;
2231 qualifierId.location = -1;
2232
2233 if (qualifierType != "location")
2234 {
2235 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2236 recover();
2237 }
2238 else
2239 {
2240 if (intValue < 0)
2241 {
2242 error(intValueLine, "out of range", intValueString.c_str(), "value must be non-negative and < MAX_DRAW_BUFFERS");
2243 recover();
2244 }
2245 else
2246 {
2247 qualifierId.location = intValue;
2248 }
2249
2250 // TODO: must check that location is < MAX_DRAW_BUFFERS
2251 }
2252
2253 return qualifierId;
2254}
2255
2256TLayoutQualifier* TParseContext::makeLayoutQualifierFromId(TLayoutQualifierId layoutQualifierId)
2257{
2258 return NULL;
2259}
2260
2261TLayoutQualifier* TParseContext::extendLayoutQualifier(TLayoutQualifier *layoutQualifier, TLayoutQualifierId layoutQualifierId)
2262{
2263 return NULL;
2264}
2265
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002266TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2267{
2268 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2269 recover();
2270 }
2271
2272 for (unsigned int i = 0; i < typeList->size(); ++i) {
2273 //
2274 // Careful not to replace already known aspects of type, like array-ness
2275 //
2276 TType* type = (*typeList)[i].type;
2277 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002278 type->setPrimarySize(typeSpecifier.primarySize);
2279 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002280 type->setPrecision(typeSpecifier.precision);
2281 type->setQualifier(typeSpecifier.qualifier);
2282
2283 // don't allow arrays of arrays
2284 if (type->isArray()) {
2285 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2286 recover();
2287 }
2288 if (typeSpecifier.array)
2289 type->setArraySize(typeSpecifier.arraySize);
2290 if (typeSpecifier.userDef) {
2291 type->setStruct(typeSpecifier.userDef->getStruct());
2292 type->setTypeName(typeSpecifier.userDef->getTypeName());
2293 }
2294
2295 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2296 recover();
2297 }
2298 }
2299
2300 return typeList;
2301}
2302
2303TPublicType TParseContext::addStructure(TSourceLoc structLine, TSourceLoc nameLine, const TString &structName, TTypeList* typeList)
2304{
2305 TType* structure = new TType(typeList, structName);
2306
2307 if (!structName.empty())
2308 {
2309 if (reservedErrorCheck(nameLine, structName))
2310 {
2311 recover();
2312 }
2313 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2314 if (!symbolTable.declare(*userTypeDef)) {
2315 error(nameLine, "redefinition", structName.c_str(), "struct");
2316 recover();
2317 }
2318 }
2319
2320 // ensure we do not specify any storage qualifiers on the struct members
2321 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2322 {
2323 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2324 const TQualifier qualifier = typeLine.type->getQualifier();
2325 switch (qualifier)
2326 {
2327 case EvqGlobal:
2328 case EvqTemporary:
2329 break;
2330 default:
2331 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2332 recover();
2333 break;
2334 }
2335 }
2336
2337 TPublicType publicType;
2338 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2339 publicType.userDef = structure;
2340 exitStructDeclaration();
2341
2342 return publicType;
2343}
2344
2345//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002346// Parse an array of strings using yyparse.
2347//
2348// Returns 0 for success.
2349//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002350int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002351 TParseContext* context) {
2352 if ((count == 0) || (string == NULL))
2353 return 1;
2354
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002355 if (glslang_initialize(context))
2356 return 1;
2357
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002358 int error = glslang_scan(count, string, length, context);
2359 if (!error)
2360 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002361
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002362 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002363
alokp@chromium.org6b495712012-06-29 00:06:58 +00002364 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002365}
2366
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002368