blob: db1323dd9d922263d5d1a5233585f3d5483f52cb [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +00002// Copyright (c) 2002-2012 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"
alokp@chromium.org8b851c62012-06-15 16:25:11 +000013#include "compiler/preprocessor/new/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,
36 estpq,
37 } 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:
278 return lValueErrorCheck(line, op, binaryNode->getLeft());
279 case EOpVectorSwizzle:
280 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
281 if (!errorReturn) {
282 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000284 TIntermTyped* rightNode = binaryNode->getRight();
285 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
286
287 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
288 p != aggrNode->getSequence().end(); p++) {
289 int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
290 offset[value]++;
291 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000292 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 return true;
295 }
296 }
297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 return errorReturn;
300 default:
301 break;
302 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000303 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000305 return true;
306 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
308
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 const char* symbol = 0;
310 if (symNode != 0)
311 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 const char* message = 0;
314 switch (node->getQualifier()) {
315 case EvqConst: message = "can't modify a const"; break;
316 case EvqConstReadOnly: message = "can't modify a const"; break;
317 case EvqAttribute: message = "can't modify an attribute"; break;
318 case EvqUniform: message = "can't modify a uniform"; break;
319 case EvqVaryingIn: message = "can't modify a varying"; break;
320 case EvqInput: message = "can't modify an input"; break;
321 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
322 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
323 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
324 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000326 //
327 // Type that can't be written to?
328 //
329 switch (node->getBasicType()) {
330 case EbtSampler2D:
331 case EbtSamplerCube:
332 message = "can't modify a sampler";
333 break;
334 case EbtVoid:
335 message = "can't modify void";
336 break;
337 default:
338 break;
339 }
340 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000342 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000343 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347
348
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000349 //
350 // Everything else is okay, no error.
351 //
352 if (message == 0)
353 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000355 //
356 // If we get here, we have an error and a message.
357 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 if (symNode) {
359 std::stringstream extraInfoStream;
360 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
361 std::string extraInfo = extraInfoStream.str();
362 error(line, " l-value required", op, extraInfo.c_str());
363 }
364 else {
365 std::stringstream extraInfoStream;
366 extraInfoStream << "(" << message << ")";
367 std::string extraInfo = extraInfoStream.str();
368 error(line, " l-value required", op, extraInfo.c_str());
369 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000371 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372}
373
374//
375// Both test, and if necessary spit out an error, to see if the node is really
376// a constant.
377//
378// Returns true if the was an error.
379//
380bool TParseContext::constErrorCheck(TIntermTyped* node)
381{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000382 if (node->getQualifier() == EvqConst)
383 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000385 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// an integer.
393//
394// Returns true if the was an error.
395//
396bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if we are currently
408// globally scoped.
409//
410// Returns true if the was an error.
411//
412bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
413{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 if (global)
415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// For now, keep it simple: if it starts "gl_", it's reserved, independent
424// of scope. Except, if the symbol table is at the built-in push-level,
425// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000426// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
427// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428//
429// Returns true if there was an error.
430//
431bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
432{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000433 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000435 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000437 return true;
438 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000439 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000440 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000441 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442 return true;
443 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000444 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000445 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000446 return true;
447 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000448 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000450 return true;
451 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000452 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000453 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000455 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 }
457 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460}
461
462//
463// Make sure there is enough data provided to the constructor to build
464// something of the type of the constructor. Also returns the type of
465// the constructor.
466//
467// Returns true if there was an error in construction.
468//
469bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
470{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000473 bool constructingMatrix = false;
474 switch(op) {
475 case EOpConstructMat2:
476 case EOpConstructMat3:
477 case EOpConstructMat4:
478 constructingMatrix = true;
479 break;
480 default:
481 break;
482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000484 //
485 // Note: It's okay to have too many components available, but not okay to have unused
486 // arguments. 'full' will go to true when enough args have been seen. If we loop
487 // again, there is an extra argument, so 'overfull' will become true.
488 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 int size = 0;
491 bool constType = true;
492 bool full = false;
493 bool overFull = false;
494 bool matrixInMatrix = false;
495 bool arrayArg = false;
496 for (int i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000497 const TParameter& param = function.getParam(i);
498 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000499
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000500 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 matrixInMatrix = true;
502 if (full)
503 overFull = true;
504 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
505 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000506 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000508 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 arrayArg = true;
510 }
511
512 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000513 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000515 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000516 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 return true;
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000521 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 return true;
523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000526 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000527 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000528 return true;
529 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000533 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 return true;
535 }
536
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000537 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000538 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 return true;
540 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000542 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000543 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
544 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000545 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000546 return true;
547 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000550 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000552 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 return true;
554 }
555 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000556 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000557 return true;
558 }
559 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000560 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 return true;
562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
567// This function checks to see if a void variable has been declared and raise an error message for such a case
568//
569// returns true in case of an error
570//
571bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
572{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000574 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 return true;
576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579}
580
581// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
582//
583// returns true in case of an error
584//
585bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
586{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000587 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000588 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return true;
590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593}
594
595// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
596//
597// returns true in case of an error
598//
599bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
600{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 return true;
604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000606 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607}
608
609bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
610{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 if (pType.type == EbtStruct) {
612 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000613 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000614
615 return true;
616 }
617
618 return false;
619 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000621
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 return true;
623 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
628bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
629{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
631 pType.type == EbtStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633
634 return true;
635 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000637 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
638 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641}
642
643bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
644{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
646 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000647 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return true;
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654bool TParseContext::containsSampler(TType& type)
655{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 if (IsSampler(type.getBasicType()))
657 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000659 if (type.getBasicType() == EbtStruct) {
660 TTypeList& structure = *type.getStruct();
661 for (unsigned int i = 0; i < structure.size(); ++i) {
662 if (containsSampler(*structure[i].type))
663 return true;
664 }
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670//
671// Do size checking for an array type's size.
672//
673// Returns true if there was an error.
674//
675bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
676{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 TIntermConstantUnion* constant = expr->getAsConstantUnion();
678 if (constant == 0 || constant->getBasicType() != EbtInt) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000679 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return true;
681 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 size = constant->getUnionArrayPointer()->getIConst();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 if (size <= 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000686 error(line, "array size must be a positive integer", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 size = 1;
688 return true;
689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692}
693
694//
695// See if this qualifier can be an array.
696//
697// Returns true if there is an error.
698//
699bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
700{
alokp@chromium.org8f0f24a2010-09-01 21:06:24 +0000701 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000702 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000703 return true;
704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000706 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707}
708
709//
710// See if this type can be an array.
711//
712// Returns true if there is an error.
713//
714bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
715{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 //
717 // Can the type be an array?
718 //
719 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000720 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000721 return true;
722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725}
726
727//
728// Do all the semantic checking for declaring an array, with and
729// without a size, and make the right changes to the symbol table.
730//
731// size == 0 means no specified size.
732//
733// Returns true if there was an error.
734//
735bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
736{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 //
738 // Don't check for reserved word use until after we know it's not in the symbol table,
739 // because reserved arrays can be redeclared.
740 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 bool builtIn = false;
743 bool sameScope = false;
744 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
745 if (symbol == 0 || !sameScope) {
746 if (reservedErrorCheck(line, identifier))
747 return true;
748
749 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 if (type.arraySize)
752 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000754 if (! symbolTable.insert(*variable)) {
755 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000756 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 return true;
758 }
759 } else {
760 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000761 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return true;
763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000765 variable = static_cast<TVariable*>(symbol);
766 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000767 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
770 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000771 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 return true;
773 }
774
775 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000776 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return true;
778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 TType* t = variable->getArrayInformationType();
781 while (t != 0) {
782 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000783 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000784 return true;
785 }
786 t->setArraySize(type.arraySize);
787 t = t->getArrayInformationType();
788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 if (type.arraySize)
791 variable->getType().setArraySize(type.arraySize);
792 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000793
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000794 if (voidErrorCheck(line, identifier, type))
795 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000797 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
800bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
801{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000802 bool builtIn = false;
803 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
804 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000805 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 return true;
807 }
808 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 type->setArrayInformationType(variable->getArrayInformationType());
811 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000813 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
814 // its an error
815 if (node->getSymbol() == "gl_FragData") {
816 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000817 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
820 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000821 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000822 return true;
823 }
824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
827 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
828 if (!updateFlag)
829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000831 size++;
832 variable->getType().setMaxArraySize(size);
833 type->setMaxArraySize(size);
834 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000836 while(tt->getArrayInformationType() != 0) {
837 tt = tt->getArrayInformationType();
838 tt->setMaxArraySize(size);
839 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000841 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
844//
845// Enforce non-initializer type/qualifier rules.
846//
847// Returns true if there was an error.
848//
849bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type)
850{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 //
852 // Make the qualifier make sense.
853 //
854 if (type.qualifier == EvqConst) {
855 type.qualifier = EvqTemporary;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000856 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return true;
858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863//
864// Do semantic checking for a variable declaration that has no initializer,
865// and update the symbol table.
866//
867// Returns true if there was an error.
868//
zmo@google.comfd747b82011-04-23 01:30:07 +0000869bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 if (reservedErrorCheck(line, identifier))
872 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
zmo@google.comfd747b82011-04-23 01:30:07 +0000874 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 if (! symbolTable.insert(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000877 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000879 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000880 return true;
881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 if (voidErrorCheck(line, identifier, type))
884 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
890{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000891 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000892 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 return true;
894 }
895 if (qualifier == EvqConst && paramQualifier != EvqIn) {
896 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
897 return true;
898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000900 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000901 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000903 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000905 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906}
907
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000908bool TParseContext::extensionErrorCheck(int line, const TString& extension)
909{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000910 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000911 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
912 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000913 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000914 return true;
915 }
zmo@google.comf5450912011-09-09 01:37:19 +0000916 // In GLSL ES, an extension's default behavior is "disable".
917 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000918 error(line, "extension", extension.c_str(), "is disabled");
919 return true;
920 }
921 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000922 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000923 return false;
924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927}
928
zmo@google.com09c323a2011-08-12 18:22:25 +0000929bool TParseContext::supportsExtension(const char* extension)
930{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000931 const TExtensionBehavior& extbehavior = extensionBehavior();
932 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
933 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000934}
935
936void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
937{
938 pp::SourceLocation loc;
939 DecodeSourceLoc(line, &loc.file, &loc.line);
940 directiveHandler.handleExtension(loc, extName, behavior);
941}
942
943void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
944{
945 pp::SourceLocation loc;
946 DecodeSourceLoc(line, &loc.file, &loc.line);
947 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000948}
949
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950/////////////////////////////////////////////////////////////////////////////////
951//
952// Non-Errors.
953//
954/////////////////////////////////////////////////////////////////////////////////
955
956//
957// Look up a function name in the symbol table, and make sure it is a function.
958//
959// Return the function symbol if found, otherwise 0.
960//
961const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
962{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000963 // First find by unmangled name to check whether the function name has been
964 // hidden by a variable name or struct typename.
965 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
966 if (symbol == 0) {
967 symbol = symbolTable.find(call->getMangledName(), builtIn);
968 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969
alokp@chromium.org0a576182010-08-09 17:16:27 +0000970 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000971 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return 0;
973 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974
alokp@chromium.org0a576182010-08-09 17:16:27 +0000975 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000976 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000977 return 0;
978 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000979
980 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
983//
984// Initializers show up in several places in the grammar. Have one set of
985// code to handle them here.
986//
987bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000988 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 if (variable == 0) {
993 if (reservedErrorCheck(line, identifier))
994 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 if (voidErrorCheck(line, identifier, pType))
997 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000999 //
1000 // add variable to symbol table
1001 //
1002 variable = new TVariable(&identifier, type);
1003 if (! symbolTable.insert(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001004 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 return true;
1006 // don't delete variable, it's used by error recovery, and the pool
1007 // pop will take care of the memory
1008 }
1009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001011 //
1012 // identifier must be of type constant, a global, or a temporary
1013 //
1014 TQualifier qualifier = variable->getType().getQualifier();
1015 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001016 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001017 return true;
1018 }
1019 //
1020 // test for and propagate constant
1021 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 if (qualifier == EvqConst) {
1024 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001025 std::stringstream extraInfoStream;
1026 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1027 std::string extraInfo = extraInfoStream.str();
1028 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001029 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001030 return true;
1031 }
1032 if (type != initializer->getType()) {
1033 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001034 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001035 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 return true;
1037 }
1038 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001039 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001041 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1042 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1043 } else {
1044 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1045 }
1046 } else if (initializer->getAsSymbolNode()) {
1047 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1048 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001050 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001051 variable->shareConstPointer(constArray);
1052 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001053 std::stringstream extraInfoStream;
1054 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1055 std::string extraInfo = extraInfoStream.str();
1056 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001057 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 return true;
1059 }
1060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 if (qualifier != EvqConst) {
1063 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1064 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1065 if (intermNode == 0) {
1066 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1067 return true;
1068 }
1069 } else
1070 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001072 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
1075bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1076{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001077 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 if (!aggrNode->isConstructor())
1079 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001080
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001081 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001083 // check if all the child nodes are constants so that they can be inserted into
1084 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001085 TIntermSequence &sequence = aggrNode->getSequence() ;
1086 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1087 if (!(*p)->getAsTyped()->getAsConstantUnion())
1088 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094// This function is used to test for the correctness of the parameters passed to various constructor functions
1095// and also convert them to the right datatype if it is allowed and required.
1096//
1097// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1098//
1099TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1100{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 if (node == 0)
1102 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 TIntermAggregate* aggrNode = node->getAsAggregate();
1105
alokp@chromium.org58e54292010-08-24 21:40:03 +00001106 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001107 if (op == EOpConstructStruct)
1108 memberTypes = type->getStruct()->begin();
1109
1110 TType elementType = *type;
1111 if (type->isArray())
1112 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 bool singleArg;
1115 if (aggrNode) {
1116 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1117 singleArg = true;
1118 else
1119 singleArg = false;
1120 } else
1121 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001123 TIntermTyped *newNode;
1124 if (singleArg) {
1125 // If structure constructor or array constructor is being called
1126 // for only one parameter inside the structure, we need to call constructStruct function once.
1127 if (type->isArray())
1128 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1129 else if (op == EOpConstructStruct)
1130 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1131 else
1132 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 if (newNode && newNode->getAsAggregate()) {
1135 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1136 if (constConstructor)
1137 return constConstructor;
1138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 return newNode;
1141 }
1142
1143 //
1144 // Handle list of arguments.
1145 //
1146 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1147 // if the structure constructor contains more than one parameter, then construct
1148 // each parameter
1149
1150 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1151
1152 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1153 // to the right type if possible (and allowed).
1154 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1155
1156 for (TIntermSequence::iterator p = sequenceVector.begin();
1157 p != sequenceVector.end(); p++, paramCount++) {
1158 if (type->isArray())
1159 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1160 else if (op == EOpConstructStruct)
1161 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1162 else
1163 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1164
1165 if (newNode) {
1166 *p = newNode;
1167 }
1168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1171 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1172 if (constConstructor)
1173 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176}
1177
1178TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1179{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 bool canBeFolded = areAllChildConst(aggrNode);
1181 aggrNode->setType(type);
1182 if (canBeFolded) {
1183 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001184 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 if (aggrNode->getSequence().size() == 1) {
1186 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1187 }
1188 else {
1189 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1190 }
1191 if (returnVal)
1192 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001194 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001197 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001198}
1199
1200// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1201// for the parameter to the constructor (passed to this function). Essentially, it converts
1202// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1203// float, then float is converted to int.
1204//
1205// Returns 0 for an error or the constructed node.
1206//
1207TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1208{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001209 TIntermTyped* newNode;
1210 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001211
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001212 //
1213 // First, convert types as needed.
1214 //
1215 switch (op) {
1216 case EOpConstructVec2:
1217 case EOpConstructVec3:
1218 case EOpConstructVec4:
1219 case EOpConstructMat2:
1220 case EOpConstructMat3:
1221 case EOpConstructMat4:
1222 case EOpConstructFloat:
1223 basicOp = EOpConstructFloat;
1224 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001226 case EOpConstructIVec2:
1227 case EOpConstructIVec3:
1228 case EOpConstructIVec4:
1229 case EOpConstructInt:
1230 basicOp = EOpConstructInt;
1231 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001233 case EOpConstructBVec2:
1234 case EOpConstructBVec3:
1235 case EOpConstructBVec4:
1236 case EOpConstructBool:
1237 basicOp = EOpConstructBool;
1238 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001240 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001241 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001242 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001243
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001244 return 0;
1245 }
1246 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1247 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001248 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001249 return 0;
1250 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001252 //
1253 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1254 //
1255
1256 // Otherwise, skip out early.
1257 if (subset || (newNode != node && newNode->getType() == *type))
1258 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001260 // setAggregateOperator will insert a new node for the constructor, as needed.
1261 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262}
1263
1264// This function tests for the type of the parameters to the structures constructors. Raises
1265// an error message if the expected type does not match the parameter passed to the constructor.
1266//
1267// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1268//
1269TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1270{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001271 if (*type == node->getAsTyped()->getType()) {
1272 if (subset)
1273 return node->getAsTyped();
1274 else
1275 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1276 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001277 std::stringstream extraInfoStream;
1278 extraInfoStream << "cannot convert parameter " << paramCount
1279 << " from '" << node->getAsTyped()->getType().getBasicString()
1280 << "' to '" << type->getBasicString() << "'";
1281 std::string extraInfo = extraInfoStream.str();
1282 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001283 recover();
1284 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001285
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287}
1288
1289//
1290// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1291// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1292// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1293// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1294// a constant matrix.
1295//
1296TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1297{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 TIntermTyped* typedNode;
1299 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001301 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001302 if (tempConstantNode) {
1303 unionArray = tempConstantNode->getUnionArrayPointer();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001304 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001306 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 return node;
1308 }
1309 } 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 +00001310 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001311 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001313 return 0;
1314 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001316 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001318 for (int i = 0; i < fields.num; i++) {
1319 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001320 std::stringstream extraInfoStream;
1321 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1322 std::string extraInfo = extraInfoStream.str();
1323 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 recover();
1325 fields.offsets[i] = 0;
1326 }
1327
1328 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 }
1331 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1332 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333}
1334
1335//
1336// This function returns the column being accessed from a constant matrix. The values are retrieved from
1337// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1338// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1339// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1340//
1341TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1342{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 TIntermTyped* typedNode;
1344 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001345
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001346 if (index >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001347 std::stringstream extraInfoStream;
1348 extraInfoStream << "matrix field selection out of range '" << index << "'";
1349 std::string extraInfo = extraInfoStream.str();
1350 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001351 recover();
1352 index = 0;
1353 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001354
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001355 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001356 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001357 int size = tempConstantNode->getType().getNominalSize();
1358 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1359 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001360 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001361 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 return 0;
1364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001366 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367}
1368
1369
1370//
1371// This function returns an element of an array accessed from a constant array. The values are retrieved from
1372// the symbol table and parse-tree is built for the type of the element. The input
1373// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1374// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1375//
1376TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1377{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001378 TIntermTyped* typedNode;
1379 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1380 TType arrayElementType = node->getType();
1381 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001383 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001384 std::stringstream extraInfoStream;
1385 extraInfoStream << "array field selection out of range '" << index << "'";
1386 std::string extraInfo = extraInfoStream.str();
1387 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001388 recover();
1389 index = 0;
1390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001392 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001393
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001394 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001395 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001396 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1397 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001398 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001399 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001400
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001401 return 0;
1402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001404 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405}
1406
1407
1408//
1409// This function returns the value of a particular field inside a constant structure from the symbol table.
1410// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1411// function and returns the parse-tree with the values of the embedded/nested struct.
1412//
1413TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1414{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001415 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001416 TIntermTyped *typedNode;
1417 int instanceSize = 0;
1418 unsigned int index = 0;
1419 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001421 for ( index = 0; index < fields->size(); ++index) {
1422 if ((*fields)[index].type->getFieldName() == identifier) {
1423 break;
1424 } else {
1425 instanceSize += (*fields)[index].type->getObjectSize();
1426 }
1427 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001428
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001429 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001430 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001432 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1433 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001434 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001435 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001437 return 0;
1438 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001439
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001440 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441}
1442
kbr@chromium.org476541f2011-10-27 21:14:51 +00001443bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1444{
1445 ++structNestingLevel;
1446
1447 // Embedded structure definitions are not supported per GLSL ES spec.
1448 // They aren't allowed in GLSL either, but we need to detect this here
1449 // so we don't rely on the GLSL compiler to catch it.
1450 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001451 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001452 return true;
1453 }
1454
1455 return false;
1456}
1457
1458void TParseContext::exitStructDeclaration()
1459{
1460 --structNestingLevel;
1461}
1462
1463namespace {
1464
1465const int kWebGLMaxStructNesting = 4;
1466
1467} // namespace
1468
1469bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1470{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001471 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001472 return false;
1473 }
1474
1475 if (fieldType.getBasicType() != EbtStruct) {
1476 return false;
1477 }
1478
1479 // We're already inside a structure definition at this point, so add
1480 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001481 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001482 std::stringstream extraInfoStream;
1483 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
1484 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
1485 std::string extraInfo = extraInfoStream.str();
1486 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00001487 return true;
1488 }
1489
1490 return false;
1491}
1492
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001493//
1494// Parse an array of strings using yyparse.
1495//
1496// Returns 0 for success.
1497//
1498int PaParseStrings(int count, const char* const string[], const int length[],
1499 TParseContext* context) {
1500 if ((count == 0) || (string == NULL))
1501 return 1;
1502
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001503 if (glslang_initialize(context))
1504 return 1;
1505
alokp@chromium.org408c45e2012-04-05 15:54:43 +00001506 int error = glslang_scan(count, string, length, context);
1507 if (!error)
1508 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001509
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001510 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001511
alokp@chromium.org6b495712012-06-29 00:06:58 +00001512 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001513}
1514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001516