blob: 455097b0673caacd71f3859a48eada8b3a006fd8 [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//
118bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
119{
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
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000154 if (fields.row >= matSize || fields.col >= matSize) {
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;
319 case EvqUniform: message = "can't modify a uniform"; break;
320 case EvqVaryingIn: message = "can't modify a varying"; break;
321 case EvqInput: message = "can't modify an input"; break;
322 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
323 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
324 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
325 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000327 //
328 // Type that can't be written to?
329 //
330 switch (node->getBasicType()) {
331 case EbtSampler2D:
332 case EbtSamplerCube:
333 message = "can't modify a sampler";
334 break;
335 case EbtVoid:
336 message = "can't modify void";
337 break;
338 default:
339 break;
340 }
341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000343 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000344 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000346 return true;
347 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000350 //
351 // Everything else is okay, no error.
352 //
353 if (message == 0)
354 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000356 //
357 // If we get here, we have an error and a message.
358 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000359 if (symNode) {
360 std::stringstream extraInfoStream;
361 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
362 std::string extraInfo = extraInfoStream.str();
363 error(line, " l-value required", op, extraInfo.c_str());
364 }
365 else {
366 std::stringstream extraInfoStream;
367 extraInfoStream << "(" << message << ")";
368 std::string extraInfo = extraInfoStream.str();
369 error(line, " l-value required", op, extraInfo.c_str());
370 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373}
374
375//
376// Both test, and if necessary spit out an error, to see if the node is really
377// a constant.
378//
379// Returns true if the was an error.
380//
381bool TParseContext::constErrorCheck(TIntermTyped* node)
382{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 if (node->getQualifier() == EvqConst)
384 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000386 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389}
390
391//
392// Both test, and if necessary spit out an error, to see if the node is really
393// an integer.
394//
395// Returns true if the was an error.
396//
397bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
398{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
400 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000402 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Both test, and if necessary spit out an error, to see if we are currently
409// globally scoped.
410//
411// Returns true if the was an error.
412//
413bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
414{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 if (global)
416 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000418 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423//
424// For now, keep it simple: if it starts "gl_", it's reserved, independent
425// of scope. Except, if the symbol table is at the built-in push-level,
426// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000427// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
428// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429//
430// Returns true if there was an error.
431//
432bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
433{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000434 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000436 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000437 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000438 return true;
439 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000440 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000441 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000442 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443 return true;
444 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000445 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000446 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000447 return true;
448 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000449 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000450 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000451 return true;
452 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000453 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000455 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000456 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 }
458 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461}
462
463//
464// Make sure there is enough data provided to the constructor to build
465// something of the type of the constructor. Also returns the type of
466// the constructor.
467//
468// Returns true if there was an error in construction.
469//
470bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
471{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 bool constructingMatrix = false;
475 switch(op) {
476 case EOpConstructMat2:
477 case EOpConstructMat3:
478 case EOpConstructMat4:
479 constructingMatrix = true;
480 break;
481 default:
482 break;
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 //
486 // Note: It's okay to have too many components available, but not okay to have unused
487 // arguments. 'full' will go to true when enough args have been seen. If we loop
488 // again, there is an extra argument, so 'overfull' will become true.
489 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 int size = 0;
492 bool constType = true;
493 bool full = false;
494 bool overFull = false;
495 bool matrixInMatrix = false;
496 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000497 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000498 const TParameter& param = function.getParam(i);
499 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000500
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000501 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 matrixInMatrix = true;
503 if (full)
504 overFull = true;
505 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
506 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000507 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000509 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 arrayArg = true;
511 }
512
513 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000514 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000516 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000517 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 return true;
519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000522 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 return true;
524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000527 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000528 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000529 return true;
530 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000534 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 return true;
536 }
537
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000538 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000539 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 return true;
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000543 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000544 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
545 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000546 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000547 return true;
548 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000551 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000553 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 return true;
555 }
556 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000557 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000558 return true;
559 }
560 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000561 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000562 return true;
563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566}
567
568// This function checks to see if a void variable has been declared and raise an error message for such a case
569//
570// returns true in case of an error
571//
572bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
573{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000575 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 return true;
577 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580}
581
582// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
583//
584// returns true in case of an error
585//
586bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
587{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000589 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 return true;
591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
596// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
597//
598// returns true in case of an error
599//
600bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
601{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000603 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 return true;
605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
610bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
611{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000612 if (pType.type == EbtStruct) {
613 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000614 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615
616 return true;
617 }
618
619 return false;
620 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 return true;
624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
629bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
630{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
632 pType.type == EbtStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000633 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634
635 return true;
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
639 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
644bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
645{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
647 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
655bool TParseContext::containsSampler(TType& type)
656{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 if (IsSampler(type.getBasicType()))
658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000660 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 TTypeList& structure = *type.getStruct();
662 for (unsigned int i = 0; i < structure.size(); ++i) {
663 if (containsSampler(*structure[i].type))
664 return true;
665 }
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671//
672// Do size checking for an array type's size.
673//
674// Returns true if there was an error.
675//
676bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
677{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 TIntermConstantUnion* constant = expr->getAsConstantUnion();
679 if (constant == 0 || constant->getBasicType() != EbtInt) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000680 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 return true;
682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000684 size = constant->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 if (size <= 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000687 error(line, "array size must be a positive integer", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 size = 1;
689 return true;
690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000692 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693}
694
695//
696// See if this qualifier can be an array.
697//
698// Returns true if there is an error.
699//
700bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
701{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000702 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000703 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000704 return true;
705 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000707 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708}
709
710//
711// See if this type can be an array.
712//
713// Returns true if there is an error.
714//
715bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
716{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 //
718 // Can the type be an array?
719 //
720 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000721 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000722 return true;
723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726}
727
728//
729// Do all the semantic checking for declaring an array, with and
730// without a size, and make the right changes to the symbol table.
731//
732// size == 0 means no specified size.
733//
734// Returns true if there was an error.
735//
736bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
737{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 //
739 // Don't check for reserved word use until after we know it's not in the symbol table,
740 // because reserved arrays can be redeclared.
741 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 bool builtIn = false;
744 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000745 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000746 if (symbol == 0 || !sameScope) {
747 if (reservedErrorCheck(line, identifier))
748 return true;
749
750 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000752 if (type.arraySize)
753 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000755 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000757 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return true;
759 }
760 } else {
761 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000762 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000763 return true;
764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000766 variable = static_cast<TVariable*>(symbol);
767 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000768 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 return true;
770 }
771 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000772 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000773 return true;
774 }
775
776 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000777 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000778 return true;
779 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000781 TType* t = variable->getArrayInformationType();
782 while (t != 0) {
783 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000784 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000785 return true;
786 }
787 t->setArraySize(type.arraySize);
788 t = t->getArrayInformationType();
789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 if (type.arraySize)
792 variable->getType().setArraySize(type.arraySize);
793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 if (voidErrorCheck(line, identifier, type))
796 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799}
800
801bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
802{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 bool builtIn = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000804 TSymbol* symbol = symbolTable.find(node->getSymbol(), 0, &builtIn);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000806 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 return true;
808 }
809 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 type->setArrayInformationType(variable->getArrayInformationType());
812 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
815 // its an error
816 if (node->getSymbol() == "gl_FragData") {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000817 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", 0, &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000818 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
821 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000822 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 return true;
824 }
825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
828 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
829 if (!updateFlag)
830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 size++;
833 variable->getType().setMaxArraySize(size);
834 type->setMaxArraySize(size);
835 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 while(tt->getArrayInformationType() != 0) {
838 tt = tt->getArrayInformationType();
839 tt->setMaxArraySize(size);
840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000842 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843}
844
845//
846// Enforce non-initializer type/qualifier rules.
847//
848// Returns true if there was an error.
849//
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000850bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000852 if (type.qualifier == EvqConst)
853 {
854 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000856
857 if (array)
858 {
859 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
860 }
861 else if (type.isStructureContainingArrays())
862 {
863 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
864 }
865 else
866 {
867 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
868 }
869
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 return true;
871 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
876//
877// Do semantic checking for a variable declaration that has no initializer,
878// and update the symbol table.
879//
880// Returns true if there was an error.
881//
zmo@google.comfd747b82011-04-23 01:30:07 +0000882bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 if (reservedErrorCheck(line, identifier))
885 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
zmo@google.comfd747b82011-04-23 01:30:07 +0000887 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000889 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000890 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000891 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000892 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 return true;
894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 if (voidErrorCheck(line, identifier, type))
897 return true;
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
902bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
903{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000905 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 return true;
907 }
908 if (qualifier == EvqConst && paramQualifier != EvqIn) {
909 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
910 return true;
911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000914 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000915 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000916 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000921bool TParseContext::extensionErrorCheck(int line, const TString& extension)
922{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000923 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000924 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
925 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000926 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000927 return true;
928 }
zmo@google.comf5450912011-09-09 01:37:19 +0000929 // In GLSL ES, an extension's default behavior is "disable".
930 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000931 error(line, "extension", extension.c_str(), "is disabled");
932 return true;
933 }
934 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000935 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000936 return false;
937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000939 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
zmo@google.com09c323a2011-08-12 18:22:25 +0000942bool TParseContext::supportsExtension(const char* extension)
943{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000944 const TExtensionBehavior& extbehavior = extensionBehavior();
945 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
946 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000947}
948
949void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
950{
951 pp::SourceLocation loc;
952 DecodeSourceLoc(line, &loc.file, &loc.line);
953 directiveHandler.handleExtension(loc, extName, behavior);
954}
955
956void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
957{
958 pp::SourceLocation loc;
959 DecodeSourceLoc(line, &loc.file, &loc.line);
960 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000961}
962
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963/////////////////////////////////////////////////////////////////////////////////
964//
965// Non-Errors.
966//
967/////////////////////////////////////////////////////////////////////////////////
968
969//
970// Look up a function name in the symbol table, and make sure it is a function.
971//
972// Return the function symbol if found, otherwise 0.
973//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000974const TFunction* TParseContext::findFunction(int line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000976 // First find by unmangled name to check whether the function name has been
977 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000978 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +0000979 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000980 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +0000981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
alokp@chromium.org0a576182010-08-09 17:16:27 +0000983 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000984 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000985 return 0;
986 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987
alokp@chromium.org0a576182010-08-09 17:16:27 +0000988 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000989 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 return 0;
991 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000992
993 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994}
995
996//
997// Initializers show up in several places in the grammar. Have one set of
998// code to handle them here.
999//
1000bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001001 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001003 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 if (variable == 0) {
1006 if (reservedErrorCheck(line, identifier))
1007 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001009 if (voidErrorCheck(line, identifier, pType))
1010 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001012 //
1013 // add variable to symbol table
1014 //
1015 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001016 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001017 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001018 return true;
1019 // don't delete variable, it's used by error recovery, and the pool
1020 // pop will take care of the memory
1021 }
1022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 //
1025 // identifier must be of type constant, a global, or a temporary
1026 //
1027 TQualifier qualifier = variable->getType().getQualifier();
1028 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001029 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001030 return true;
1031 }
1032 //
1033 // test for and propagate constant
1034 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 if (qualifier == EvqConst) {
1037 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001038 std::stringstream extraInfoStream;
1039 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1040 std::string extraInfo = extraInfoStream.str();
1041 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001042 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001043 return true;
1044 }
1045 if (type != initializer->getType()) {
1046 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001047 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001048 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001049 return true;
1050 }
1051 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001052 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1055 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1056 } else {
1057 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1058 }
1059 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001060 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001063 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001064 variable->shareConstPointer(constArray);
1065 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001066 std::stringstream extraInfoStream;
1067 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1068 std::string extraInfo = extraInfoStream.str();
1069 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001070 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001071 return true;
1072 }
1073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 if (qualifier != EvqConst) {
1076 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1077 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1078 if (intermNode == 0) {
1079 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1080 return true;
1081 }
1082 } else
1083 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1089{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001090 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 if (!aggrNode->isConstructor())
1092 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001096 // check if all the child nodes are constants so that they can be inserted into
1097 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001098 TIntermSequence &sequence = aggrNode->getSequence() ;
1099 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1100 if (!(*p)->getAsTyped()->getAsConstantUnion())
1101 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
1107// This function is used to test for the correctness of the parameters passed to various constructor functions
1108// and also convert them to the right datatype if it is allowed and required.
1109//
1110// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1111//
1112TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1113{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 if (node == 0)
1115 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001117 TIntermAggregate* aggrNode = node->getAsAggregate();
1118
alokp@chromium.org58e54292010-08-24 21:40:03 +00001119 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 if (op == EOpConstructStruct)
1121 memberTypes = type->getStruct()->begin();
1122
1123 TType elementType = *type;
1124 if (type->isArray())
1125 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001127 bool singleArg;
1128 if (aggrNode) {
1129 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1130 singleArg = true;
1131 else
1132 singleArg = false;
1133 } else
1134 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001136 TIntermTyped *newNode;
1137 if (singleArg) {
1138 // If structure constructor or array constructor is being called
1139 // for only one parameter inside the structure, we need to call constructStruct function once.
1140 if (type->isArray())
1141 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1142 else if (op == EOpConstructStruct)
1143 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1144 else
1145 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001147 if (newNode && newNode->getAsAggregate()) {
1148 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1149 if (constConstructor)
1150 return constConstructor;
1151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001153 return newNode;
1154 }
1155
1156 //
1157 // Handle list of arguments.
1158 //
1159 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1160 // if the structure constructor contains more than one parameter, then construct
1161 // each parameter
1162
1163 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1164
1165 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1166 // to the right type if possible (and allowed).
1167 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1168
1169 for (TIntermSequence::iterator p = sequenceVector.begin();
1170 p != sequenceVector.end(); p++, paramCount++) {
1171 if (type->isArray())
1172 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1173 else if (op == EOpConstructStruct)
1174 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1175 else
1176 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1177
1178 if (newNode) {
1179 *p = newNode;
1180 }
1181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001183 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1184 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1185 if (constConstructor)
1186 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001188 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001189}
1190
1191TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1192{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001193 bool canBeFolded = areAllChildConst(aggrNode);
1194 aggrNode->setType(type);
1195 if (canBeFolded) {
1196 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001197 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001198 if (aggrNode->getSequence().size() == 1) {
1199 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1200 }
1201 else {
1202 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1203 }
1204 if (returnVal)
1205 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001207 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1208 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001209
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001210 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001211}
1212
1213// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1214// for the parameter to the constructor (passed to this function). Essentially, it converts
1215// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1216// float, then float is converted to int.
1217//
1218// Returns 0 for an error or the constructed node.
1219//
1220TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1221{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001222 TIntermTyped* newNode;
1223 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001224
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001225 //
1226 // First, convert types as needed.
1227 //
1228 switch (op) {
1229 case EOpConstructVec2:
1230 case EOpConstructVec3:
1231 case EOpConstructVec4:
1232 case EOpConstructMat2:
1233 case EOpConstructMat3:
1234 case EOpConstructMat4:
1235 case EOpConstructFloat:
1236 basicOp = EOpConstructFloat;
1237 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001239 case EOpConstructIVec2:
1240 case EOpConstructIVec3:
1241 case EOpConstructIVec4:
1242 case EOpConstructInt:
1243 basicOp = EOpConstructInt;
1244 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001245
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001246 case EOpConstructBVec2:
1247 case EOpConstructBVec3:
1248 case EOpConstructBVec4:
1249 case EOpConstructBool:
1250 basicOp = EOpConstructBool;
1251 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001252
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001253 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001254 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001255 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
1259 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1260 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001261 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001262 return 0;
1263 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001265 //
1266 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1267 //
1268
1269 // Otherwise, skip out early.
1270 if (subset || (newNode != node && newNode->getType() == *type))
1271 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001273 // setAggregateOperator will insert a new node for the constructor, as needed.
1274 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275}
1276
1277// This function tests for the type of the parameters to the structures constructors. Raises
1278// an error message if the expected type does not match the parameter passed to the constructor.
1279//
1280// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1281//
1282TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1283{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001284 if (*type == node->getAsTyped()->getType()) {
1285 if (subset)
1286 return node->getAsTyped();
1287 else
1288 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1289 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001290 std::stringstream extraInfoStream;
1291 extraInfoStream << "cannot convert parameter " << paramCount
1292 << " from '" << node->getAsTyped()->getType().getBasicString()
1293 << "' to '" << type->getBasicString() << "'";
1294 std::string extraInfo = extraInfoStream.str();
1295 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001296 recover();
1297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001299 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300}
1301
1302//
1303// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1304// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1305// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1306// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1307// a constant matrix.
1308//
1309TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1310{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001311 TIntermTyped* typedNode;
1312 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001314 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 if (tempConstantNode) {
1316 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001318 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001319 return node;
1320 }
1321 } 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 +00001322 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001325 return 0;
1326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001328 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 for (int i = 0; i < fields.num; i++) {
1331 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001332 std::stringstream extraInfoStream;
1333 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1334 std::string extraInfo = extraInfoStream.str();
1335 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 recover();
1337 fields.offsets[i] = 0;
1338 }
1339
1340 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 }
1343 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1344 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001345}
1346
1347//
1348// This function returns the column being accessed from a constant matrix. The values are retrieved from
1349// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1350// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1351// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1352//
1353TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1354{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001355 TIntermTyped* typedNode;
1356 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001358 if (index >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001359 std::stringstream extraInfoStream;
1360 extraInfoStream << "matrix field selection out of range '" << index << "'";
1361 std::string extraInfo = extraInfoStream.str();
1362 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 recover();
1364 index = 0;
1365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001367 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001368 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001369 int size = tempConstantNode->getType().getNominalSize();
1370 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1371 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001372 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001373 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return 0;
1376 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379}
1380
1381
1382//
1383// This function returns an element of an array accessed from a constant array. The values are retrieved from
1384// the symbol table and parse-tree is built for the type of the element. The input
1385// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1386// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1387//
1388TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1389{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001390 TIntermTyped* typedNode;
1391 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1392 TType arrayElementType = node->getType();
1393 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001394
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001395 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001396 std::stringstream extraInfoStream;
1397 extraInfoStream << "array field selection out of range '" << index << "'";
1398 std::string extraInfo = extraInfoStream.str();
1399 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001400 recover();
1401 index = 0;
1402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001404 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001407 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001408 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1409 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001410 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001411 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001413 return 0;
1414 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001416 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001417}
1418
1419
1420//
1421// This function returns the value of a particular field inside a constant structure from the symbol table.
1422// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1423// function and returns the parse-tree with the values of the embedded/nested struct.
1424//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001425TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, TSourceLoc line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001426{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001427 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001428 TIntermTyped *typedNode;
1429 int instanceSize = 0;
1430 unsigned int index = 0;
1431 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001433 for ( index = 0; index < fields->size(); ++index) {
1434 if ((*fields)[index].type->getFieldName() == identifier) {
1435 break;
1436 } else {
1437 instanceSize += (*fields)[index].type->getObjectSize();
1438 }
1439 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001441 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001442 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001444 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1445 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001446 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001447 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001448
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001449 return 0;
1450 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001451
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001452 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453}
1454
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001455//
1456// Interface/uniform blocks
1457//
1458TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, TSourceLoc nameLine, const TString& blockName, TTypeList* typeList,
1459 const TString& instanceName, TSourceLoc instanceLine, TIntermTyped* arrayIndex, TSourceLoc arrayIndexLine)
1460{
1461 if (reservedErrorCheck(nameLine, blockName))
1462 recover();
1463
1464 if (typeQualifier.qualifier != EvqUniform)
1465 {
1466 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1467 recover();
1468 }
1469
1470 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1471 if (!symbolTable.declare(*blockNameSymbol)) {
1472 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1473 recover();
1474 }
1475
1476 // check for sampler types
1477 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1478 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1479 TType* memberType = memberTypeLine.type;
1480 if (IsSampler(memberType->getBasicType())) {
1481 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1482 recover();
1483 }
1484
1485 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1486 switch (qualifier)
1487 {
1488 case EvqGlobal:
1489 case EvqUniform:
1490 break;
1491 default:
1492 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1493 recover();
1494 break;
1495 }
1496 }
1497
1498 TType* interfaceBlock = new TType(typeList, blockName);
1499 interfaceBlock->setBasicType(EbtInterfaceBlock);
1500 interfaceBlock->setQualifier(typeQualifier.qualifier);
1501
1502 TString symbolName = "";
1503 int symbolId = 0;
1504
1505 if (instanceName == "")
1506 {
1507 // define symbols for the members of the interface block
1508 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1509 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1510 TType* memberType = memberTypeLine.type;
1511 memberType->setInterfaceBlockType(interfaceBlock);
1512 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1513 memberVariable->setQualifier(typeQualifier.qualifier);
1514 if (!symbolTable.declare(*memberVariable)) {
1515 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1516 recover();
1517 }
1518 }
1519 }
1520 else
1521 {
1522 interfaceBlock->setInstanceName(instanceName);
1523
1524 // add array index
1525 if (arrayIndex != NULL)
1526 {
1527 int size;
1528 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
1529 recover();
1530 interfaceBlock->setArraySize(size);
1531 }
1532
1533 // add a symbol for this interface block
1534 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
1535 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1536 if (!symbolTable.declare(*instanceTypeDef)) {
1537 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
1538 recover();
1539 }
1540
1541 symbolId = instanceTypeDef->getUniqueId();
1542 symbolName = instanceTypeDef->getName();
1543 }
1544
1545 exitStructDeclaration();
1546 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
1547 aggregate->setOp(EOpDeclaration);
1548 return aggregate;
1549}
1550
kbr@chromium.org476541f2011-10-27 21:14:51 +00001551bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1552{
1553 ++structNestingLevel;
1554
1555 // Embedded structure definitions are not supported per GLSL ES spec.
1556 // They aren't allowed in GLSL either, but we need to detect this here
1557 // so we don't rely on the GLSL compiler to catch it.
1558 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001559 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001560 return true;
1561 }
1562
1563 return false;
1564}
1565
1566void TParseContext::exitStructDeclaration()
1567{
1568 --structNestingLevel;
1569}
1570
1571namespace {
1572
1573const int kWebGLMaxStructNesting = 4;
1574
1575} // namespace
1576
1577bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1578{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001579 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001580 return false;
1581 }
1582
1583 if (fieldType.getBasicType() != EbtStruct) {
1584 return false;
1585 }
1586
1587 // We're already inside a structure definition at this point, so add
1588 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001589 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001590 std::stringstream extraInfoStream;
1591 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
1592 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
1593 std::string extraInfo = extraInfoStream.str();
1594 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00001595 return true;
1596 }
1597
1598 return false;
1599}
1600
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001601//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001602// Parse an array index expression
1603//
1604TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, TSourceLoc location, TIntermTyped *indexExpression)
1605{
1606 TIntermTyped *indexedExpression = NULL;
1607
1608 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
1609 {
1610 if (baseExpression->getAsSymbolNode())
1611 {
1612 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
1613 }
1614 else
1615 {
1616 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
1617 }
1618 recover();
1619 }
1620 if (baseExpression->getType().getQualifier() == EvqConst && indexExpression->getQualifier() == EvqConst)
1621 {
1622 if (baseExpression->isArray())
1623 {
1624 // constant folding for arrays
1625 indexedExpression = addConstArrayNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
1626 }
1627 else if (baseExpression->isVector())
1628 {
1629 // constant folding for vectors
1630 TVectorFields fields;
1631 fields.num = 1;
1632 fields.offsets[0] = indexExpression->getAsConstantUnion()->getIConst(0); // need to do it this way because v.xy sends fields integer array
1633 indexedExpression = addConstVectorNode(fields, baseExpression, location);
1634 }
1635 else if (baseExpression->isMatrix())
1636 {
1637 // constant folding for matrices
1638 indexedExpression = addConstMatrixNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
1639 }
1640 }
1641 else
1642 {
1643 if (indexExpression->getQualifier() == EvqConst)
1644 {
1645 if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= indexExpression->getAsConstantUnion()->getIConst(0) && !baseExpression->isArray() )
1646 {
1647 std::stringstream extraInfoStream;
1648 extraInfoStream << "field selection out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
1649 std::string extraInfo = extraInfoStream.str();
1650 error(location, "", "[", extraInfo.c_str());
1651 recover();
1652 }
1653 else
1654 {
1655 if (baseExpression->isArray())
1656 {
1657 if (baseExpression->getType().getArraySize() == 0)
1658 {
1659 if (baseExpression->getType().getMaxArraySize() <= indexExpression->getAsConstantUnion()->getIConst(0))
1660 {
1661 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), indexExpression->getAsConstantUnion()->getIConst(0), true, location))
1662 recover();
1663 }
1664 else
1665 {
1666 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), 0, false, location))
1667 recover();
1668 }
1669 }
1670 else if ( indexExpression->getAsConstantUnion()->getIConst(0) >= baseExpression->getType().getArraySize())
1671 {
1672 std::stringstream extraInfoStream;
1673 extraInfoStream << "array index out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
1674 std::string extraInfo = extraInfoStream.str();
1675 error(location, "", "[", extraInfo.c_str());
1676 recover();
1677 }
1678 }
1679 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
1680 }
1681 }
1682 else
1683 {
1684 if (baseExpression->isArray() && baseExpression->getType().getArraySize() == 0)
1685 {
1686 error(location, "", "[", "array must be redeclared with a size before being indexed with a variable");
1687 recover();
1688 }
1689
1690 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
1691 }
1692 }
1693 if (indexedExpression == 0)
1694 {
1695 ConstantUnion *unionArray = new ConstantUnion[1];
1696 unionArray->setFConst(0.0f);
1697 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
1698 }
1699 else if (baseExpression->isArray())
1700 {
1701 if (baseExpression->getType().getStruct())
1702 {
1703 indexedExpression->setType(TType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName()));
1704 }
1705 else
1706 {
1707 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->isMatrix()));
1708 }
1709
1710 if (baseExpression->getType().getQualifier() == EvqConst)
1711 {
1712 indexedExpression->getTypePointer()->setQualifier(EvqConst);
1713 }
1714 }
1715 else if (baseExpression->isMatrix() && baseExpression->getType().getQualifier() == EvqConst)
1716 {
1717 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, baseExpression->getNominalSize()));
1718 }
1719 else if (baseExpression->isMatrix())
1720 {
1721 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize()));
1722 }
1723 else if (baseExpression->isVector() && baseExpression->getType().getQualifier() == EvqConst)
1724 {
1725 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst));
1726 }
1727 else if (baseExpression->isVector())
1728 {
1729 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
1730 }
1731 else
1732 {
1733 indexedExpression->setType(baseExpression->getType());
1734 }
1735
1736 return indexedExpression;
1737}
1738
1739TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, TSourceLoc dotLocation, const TString &fieldString, TSourceLoc fieldLocation)
1740{
1741 TIntermTyped *indexedExpression = NULL;
1742
1743 if (baseExpression->isArray())
1744 {
1745 error(fieldLocation, "cannot apply dot operator to an array", ".");
1746 recover();
1747 }
1748
1749 if (baseExpression->isVector())
1750 {
1751 TVectorFields fields;
1752 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
1753 {
1754 fields.num = 1;
1755 fields.offsets[0] = 0;
1756 recover();
1757 }
1758
1759 if (baseExpression->getType().getQualifier() == EvqConst)
1760 {
1761 // constant folding for vector fields
1762 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
1763 if (indexedExpression == 0)
1764 {
1765 recover();
1766 indexedExpression = baseExpression;
1767 }
1768 else
1769 {
1770 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
1771 }
1772 }
1773 else
1774 {
1775 TString vectorString = fieldString;
1776 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
1777 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
1778 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
1779 }
1780 }
1781 else if (baseExpression->isMatrix())
1782 {
1783 TMatrixFields fields;
1784 if (!parseMatrixFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
1785 {
1786 fields.wholeRow = false;
1787 fields.wholeCol = false;
1788 fields.row = 0;
1789 fields.col = 0;
1790 recover();
1791 }
1792
1793 if (fields.wholeRow || fields.wholeCol)
1794 {
1795 error(dotLocation, " non-scalar fields not implemented yet", ".");
1796 recover();
1797 ConstantUnion *unionArray = new ConstantUnion[1];
1798 unionArray->setIConst(0);
1799 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
1800 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
1801 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getNominalSize()));
1802 }
1803 else
1804 {
1805 ConstantUnion *unionArray = new ConstantUnion[1];
1806 unionArray->setIConst(fields.col * baseExpression->getNominalSize() + fields.row);
1807 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
1808 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
1809 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
1810 }
1811 }
1812 else if (baseExpression->getBasicType() == EbtStruct)
1813 {
1814 bool fieldFound = false;
1815 const TTypeList* fields = baseExpression->getType().getStruct();
1816 if (fields == 0)
1817 {
1818 error(dotLocation, "structure has no fields", "Internal Error");
1819 recover();
1820 indexedExpression = baseExpression;
1821 }
1822 else
1823 {
1824 unsigned int i;
1825 for (i = 0; i < fields->size(); ++i)
1826 {
1827 if ((*fields)[i].type->getFieldName() == fieldString)
1828 {
1829 fieldFound = true;
1830 break;
1831 }
1832 }
1833 if (fieldFound)
1834 {
1835 if (baseExpression->getType().getQualifier() == EvqConst)
1836 {
1837 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
1838 if (indexedExpression == 0)
1839 {
1840 recover();
1841 indexedExpression = baseExpression;
1842 }
1843 else
1844 {
1845 indexedExpression->setType(*(*fields)[i].type);
1846 // change the qualifier of the return type, not of the structure field
1847 // as the structure definition is shared between various structures.
1848 indexedExpression->getTypePointer()->setQualifier(EvqConst);
1849 }
1850 }
1851 else
1852 {
1853 ConstantUnion *unionArray = new ConstantUnion[1];
1854 unionArray->setIConst(i);
1855 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
1856 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
1857 indexedExpression->setType(*(*fields)[i].type);
1858 }
1859 }
1860 else
1861 {
1862 error(dotLocation, " no such field in structure", fieldString.c_str());
1863 recover();
1864 indexedExpression = baseExpression;
1865 }
1866 }
1867 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001868 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
1869 {
1870 bool fieldFound = false;
1871 const TTypeList* fields = baseExpression->getType().getStruct();
1872 if (fields == 0)
1873 {
1874 error(dotLocation, "interface block has no fields", "Internal Error");
1875 recover();
1876 indexedExpression = baseExpression;
1877 }
1878 else
1879 {
1880 unsigned int i;
1881 for (i = 0; i < fields->size(); ++i)
1882 {
1883 if ((*fields)[i].type->getFieldName() == fieldString)
1884 {
1885 fieldFound = true;
1886 break;
1887 }
1888 }
1889 if (fieldFound)
1890 {
1891 ConstantUnion *unionArray = new ConstantUnion[1];
1892 unionArray->setIConst(i);
1893 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
1894 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
1895 indexedExpression->setType(*(*fields)[i].type);
1896 }
1897 else
1898 {
1899 error(dotLocation, " no such field in interface block", fieldString.c_str());
1900 recover();
1901 indexedExpression = baseExpression;
1902 }
1903 }
1904 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001905 else
1906 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001907 if (shaderVersion < 300)
1908 {
1909 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
1910 }
1911 else
1912 {
1913 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
1914 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00001915 recover();
1916 indexedExpression = baseExpression;
1917 }
1918
1919 return indexedExpression;
1920}
1921
1922TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
1923{
1924 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
1925 recover();
1926 }
1927
1928 for (unsigned int i = 0; i < typeList->size(); ++i) {
1929 //
1930 // Careful not to replace already known aspects of type, like array-ness
1931 //
1932 TType* type = (*typeList)[i].type;
1933 type->setBasicType(typeSpecifier.type);
1934 type->setNominalSize(typeSpecifier.size);
1935 type->setMatrix(typeSpecifier.matrix);
1936 type->setPrecision(typeSpecifier.precision);
1937 type->setQualifier(typeSpecifier.qualifier);
1938
1939 // don't allow arrays of arrays
1940 if (type->isArray()) {
1941 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
1942 recover();
1943 }
1944 if (typeSpecifier.array)
1945 type->setArraySize(typeSpecifier.arraySize);
1946 if (typeSpecifier.userDef) {
1947 type->setStruct(typeSpecifier.userDef->getStruct());
1948 type->setTypeName(typeSpecifier.userDef->getTypeName());
1949 }
1950
1951 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
1952 recover();
1953 }
1954 }
1955
1956 return typeList;
1957}
1958
1959TPublicType TParseContext::addStructure(TSourceLoc structLine, TSourceLoc nameLine, const TString &structName, TTypeList* typeList)
1960{
1961 TType* structure = new TType(typeList, structName);
1962
1963 if (!structName.empty())
1964 {
1965 if (reservedErrorCheck(nameLine, structName))
1966 {
1967 recover();
1968 }
1969 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
1970 if (!symbolTable.declare(*userTypeDef)) {
1971 error(nameLine, "redefinition", structName.c_str(), "struct");
1972 recover();
1973 }
1974 }
1975
1976 // ensure we do not specify any storage qualifiers on the struct members
1977 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
1978 {
1979 const TTypeLine &typeLine = (*typeList)[typeListIndex];
1980 const TQualifier qualifier = typeLine.type->getQualifier();
1981 switch (qualifier)
1982 {
1983 case EvqGlobal:
1984 case EvqTemporary:
1985 break;
1986 default:
1987 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
1988 recover();
1989 break;
1990 }
1991 }
1992
1993 TPublicType publicType;
1994 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
1995 publicType.userDef = structure;
1996 exitStructDeclaration();
1997
1998 return publicType;
1999}
2000
2001//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002002// Parse an array of strings using yyparse.
2003//
2004// Returns 0 for success.
2005//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002006int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002007 TParseContext* context) {
2008 if ((count == 0) || (string == NULL))
2009 return 1;
2010
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002011 if (glslang_initialize(context))
2012 return 1;
2013
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002014 int error = glslang_scan(count, string, length, context);
2015 if (!error)
2016 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002017
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002018 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002019
alokp@chromium.org6b495712012-06-29 00:06:58 +00002020 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002021}
2022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002024