blob: 6ab99fe84b109e5642b3d0fc437a7596d56bae8b [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"
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,
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//
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000849bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000851 if (type.qualifier == EvqConst)
852 {
853 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000855
856 if (array)
857 {
858 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
859 }
860 else if (type.isStructureContainingArrays())
861 {
862 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
863 }
864 else
865 {
866 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
867 }
868
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 return true;
870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873}
874
875//
876// Do semantic checking for a variable declaration that has no initializer,
877// and update the symbol table.
878//
879// Returns true if there was an error.
880//
zmo@google.comfd747b82011-04-23 01:30:07 +0000881bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 if (reservedErrorCheck(line, identifier))
884 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
zmo@google.comfd747b82011-04-23 01:30:07 +0000886 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000888 if (! symbolTable.insert(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000889 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000891 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 return true;
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000895 if (voidErrorCheck(line, identifier, type))
896 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
901bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
902{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000903 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000904 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000905 return true;
906 }
907 if (qualifier == EvqConst && paramQualifier != EvqIn) {
908 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
909 return true;
910 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000912 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000913 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000914 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000915 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000920bool TParseContext::extensionErrorCheck(int line, const TString& extension)
921{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000922 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000923 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
924 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000925 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 return true;
927 }
zmo@google.comf5450912011-09-09 01:37:19 +0000928 // In GLSL ES, an extension's default behavior is "disable".
929 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000930 error(line, "extension", extension.c_str(), "is disabled");
931 return true;
932 }
933 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000934 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000935 return false;
936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939}
940
zmo@google.com09c323a2011-08-12 18:22:25 +0000941bool TParseContext::supportsExtension(const char* extension)
942{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000943 const TExtensionBehavior& extbehavior = extensionBehavior();
944 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
945 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000946}
947
948void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
949{
950 pp::SourceLocation loc;
951 DecodeSourceLoc(line, &loc.file, &loc.line);
952 directiveHandler.handleExtension(loc, extName, behavior);
953}
954
955void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
956{
957 pp::SourceLocation loc;
958 DecodeSourceLoc(line, &loc.file, &loc.line);
959 directiveHandler.handlePragma(loc, name, value);
zmo@google.com09c323a2011-08-12 18:22:25 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962/////////////////////////////////////////////////////////////////////////////////
963//
964// Non-Errors.
965//
966/////////////////////////////////////////////////////////////////////////////////
967
968//
969// Look up a function name in the symbol table, and make sure it is a function.
970//
971// Return the function symbol if found, otherwise 0.
972//
973const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
974{
alokp@chromium.org0a576182010-08-09 17:16:27 +0000975 // First find by unmangled name to check whether the function name has been
976 // hidden by a variable name or struct typename.
977 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
978 if (symbol == 0) {
979 symbol = symbolTable.find(call->getMangledName(), builtIn);
980 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981
alokp@chromium.org0a576182010-08-09 17:16:27 +0000982 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000983 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 return 0;
985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
alokp@chromium.org0a576182010-08-09 17:16:27 +0000987 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000988 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000989 return 0;
990 }
alokp@chromium.org0a576182010-08-09 17:16:27 +0000991
992 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
995//
996// Initializers show up in several places in the grammar. Have one set of
997// code to handle them here.
998//
999bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001000 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001002 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001004 if (variable == 0) {
1005 if (reservedErrorCheck(line, identifier))
1006 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001008 if (voidErrorCheck(line, identifier, pType))
1009 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001011 //
1012 // add variable to symbol table
1013 //
1014 variable = new TVariable(&identifier, type);
1015 if (! symbolTable.insert(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001016 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001017 return true;
1018 // don't delete variable, it's used by error recovery, and the pool
1019 // pop will take care of the memory
1020 }
1021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 //
1024 // identifier must be of type constant, a global, or a temporary
1025 //
1026 TQualifier qualifier = variable->getType().getQualifier();
1027 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001028 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001029 return true;
1030 }
1031 //
1032 // test for and propagate constant
1033 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 if (qualifier == EvqConst) {
1036 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001037 std::stringstream extraInfoStream;
1038 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1039 std::string extraInfo = extraInfoStream.str();
1040 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001041 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001042 return true;
1043 }
1044 if (type != initializer->getType()) {
1045 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001046 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001047 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001048 return true;
1049 }
1050 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001051 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001053 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1054 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1055 } else {
1056 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1057 }
1058 } else if (initializer->getAsSymbolNode()) {
1059 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1060 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001062 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001063 variable->shareConstPointer(constArray);
1064 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001065 std::stringstream extraInfoStream;
1066 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1067 std::string extraInfo = extraInfoStream.str();
1068 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001069 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001070 return true;
1071 }
1072 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001074 if (qualifier != EvqConst) {
1075 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1076 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1077 if (intermNode == 0) {
1078 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1079 return true;
1080 }
1081 } else
1082 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1088{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001089 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 if (!aggrNode->isConstructor())
1091 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001093 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001095 // check if all the child nodes are constants so that they can be inserted into
1096 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001097 TIntermSequence &sequence = aggrNode->getSequence() ;
1098 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1099 if (!(*p)->getAsTyped()->getAsConstantUnion())
1100 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
1106// This function is used to test for the correctness of the parameters passed to various constructor functions
1107// and also convert them to the right datatype if it is allowed and required.
1108//
1109// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1110//
1111TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1112{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 if (node == 0)
1114 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001116 TIntermAggregate* aggrNode = node->getAsAggregate();
1117
alokp@chromium.org58e54292010-08-24 21:40:03 +00001118 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001119 if (op == EOpConstructStruct)
1120 memberTypes = type->getStruct()->begin();
1121
1122 TType elementType = *type;
1123 if (type->isArray())
1124 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001126 bool singleArg;
1127 if (aggrNode) {
1128 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1129 singleArg = true;
1130 else
1131 singleArg = false;
1132 } else
1133 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001135 TIntermTyped *newNode;
1136 if (singleArg) {
1137 // If structure constructor or array constructor is being called
1138 // for only one parameter inside the structure, we need to call constructStruct function once.
1139 if (type->isArray())
1140 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1141 else if (op == EOpConstructStruct)
1142 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1143 else
1144 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001145
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001146 if (newNode && newNode->getAsAggregate()) {
1147 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1148 if (constConstructor)
1149 return constConstructor;
1150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001152 return newNode;
1153 }
1154
1155 //
1156 // Handle list of arguments.
1157 //
1158 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1159 // if the structure constructor contains more than one parameter, then construct
1160 // each parameter
1161
1162 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1163
1164 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1165 // to the right type if possible (and allowed).
1166 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1167
1168 for (TIntermSequence::iterator p = sequenceVector.begin();
1169 p != sequenceVector.end(); p++, paramCount++) {
1170 if (type->isArray())
1171 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1172 else if (op == EOpConstructStruct)
1173 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1174 else
1175 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1176
1177 if (newNode) {
1178 *p = newNode;
1179 }
1180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001182 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1183 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1184 if (constConstructor)
1185 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001187 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001188}
1189
1190TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1191{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001192 bool canBeFolded = areAllChildConst(aggrNode);
1193 aggrNode->setType(type);
1194 if (canBeFolded) {
1195 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001196 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001197 if (aggrNode->getSequence().size() == 1) {
1198 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true);
1199 }
1200 else {
1201 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type);
1202 }
1203 if (returnVal)
1204 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001205
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001206 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1207 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001208
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001209 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210}
1211
1212// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1213// for the parameter to the constructor (passed to this function). Essentially, it converts
1214// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1215// float, then float is converted to int.
1216//
1217// Returns 0 for an error or the constructed node.
1218//
1219TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset)
1220{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001221 TIntermTyped* newNode;
1222 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001224 //
1225 // First, convert types as needed.
1226 //
1227 switch (op) {
1228 case EOpConstructVec2:
1229 case EOpConstructVec3:
1230 case EOpConstructVec4:
1231 case EOpConstructMat2:
1232 case EOpConstructMat3:
1233 case EOpConstructMat4:
1234 case EOpConstructFloat:
1235 basicOp = EOpConstructFloat;
1236 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001237
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001238 case EOpConstructIVec2:
1239 case EOpConstructIVec3:
1240 case EOpConstructIVec4:
1241 case EOpConstructInt:
1242 basicOp = EOpConstructInt;
1243 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001245 case EOpConstructBVec2:
1246 case EOpConstructBVec3:
1247 case EOpConstructBVec4:
1248 case EOpConstructBool:
1249 basicOp = EOpConstructBool;
1250 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001252 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001253 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001254 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001256 return 0;
1257 }
1258 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable);
1259 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001260 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001261 return 0;
1262 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001263
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001264 //
1265 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1266 //
1267
1268 // Otherwise, skip out early.
1269 if (subset || (newNode != node && newNode->getType() == *type))
1270 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 // setAggregateOperator will insert a new node for the constructor, as needed.
1273 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274}
1275
1276// This function tests for the type of the parameters to the structures constructors. Raises
1277// an error message if the expected type does not match the parameter passed to the constructor.
1278//
1279// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1280//
1281TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset)
1282{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001283 if (*type == node->getAsTyped()->getType()) {
1284 if (subset)
1285 return node->getAsTyped();
1286 else
1287 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1288 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001289 std::stringstream extraInfoStream;
1290 extraInfoStream << "cannot convert parameter " << paramCount
1291 << " from '" << node->getAsTyped()->getType().getBasicString()
1292 << "' to '" << type->getBasicString() << "'";
1293 std::string extraInfo = extraInfoStream.str();
1294 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001295 recover();
1296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001299}
1300
1301//
1302// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1303// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1304// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1305// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1306// a constant matrix.
1307//
1308TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1309{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001310 TIntermTyped* typedNode;
1311 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001313 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 if (tempConstantNode) {
1315 unionArray = tempConstantNode->getUnionArrayPointer();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001316 ASSERT(unionArray);
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//
1425TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1426{
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
kbr@chromium.org476541f2011-10-27 21:14:51 +00001455bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1456{
1457 ++structNestingLevel;
1458
1459 // Embedded structure definitions are not supported per GLSL ES spec.
1460 // They aren't allowed in GLSL either, but we need to detect this here
1461 // so we don't rely on the GLSL compiler to catch it.
1462 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001463 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001464 return true;
1465 }
1466
1467 return false;
1468}
1469
1470void TParseContext::exitStructDeclaration()
1471{
1472 --structNestingLevel;
1473}
1474
1475namespace {
1476
1477const int kWebGLMaxStructNesting = 4;
1478
1479} // namespace
1480
1481bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType)
1482{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00001483 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00001484 return false;
1485 }
1486
1487 if (fieldType.getBasicType() != EbtStruct) {
1488 return false;
1489 }
1490
1491 // We're already inside a structure definition at this point, so add
1492 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00001493 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001494 std::stringstream extraInfoStream;
1495 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
1496 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
1497 std::string extraInfo = extraInfoStream.str();
1498 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00001499 return true;
1500 }
1501
1502 return false;
1503}
1504
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001505//
1506// Parse an array of strings using yyparse.
1507//
1508// Returns 0 for success.
1509//
1510int PaParseStrings(int count, const char* const string[], const int length[],
1511 TParseContext* context) {
1512 if ((count == 0) || (string == NULL))
1513 return 1;
1514
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001515 if (glslang_initialize(context))
1516 return 1;
1517
alokp@chromium.org408c45e2012-04-05 15:54:43 +00001518 int error = glslang_scan(count, string, length, context);
1519 if (!error)
1520 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001521
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001522 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001523
alokp@chromium.org6b495712012-06-29 00:06:58 +00001524 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00001525}
1526
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00001528