blob: 35bf68e2cdba12258dfbd2242adf7954158fee03 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/ParseHelper.h"
8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000012#include "compiler/glslang.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
alokp@chromium.org8b851c62012-06-15 16:25:11 +000015///////////////////////////////////////////////////////////////////////
16//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017// Sub- vector and matrix fields
18//
19////////////////////////////////////////////////////////////////////////
20
21//
22// Look at a '.' field selector string and change it into offsets
23// for a vector.
24//
Jamie Madill075edd82013-07-08 13:30:19 -040025bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000027 fields.num = (int) compString.size();
28 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000029 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 return false;
31 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 enum {
34 exyzw,
35 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000036 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000037 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
41 case 'x':
42 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
45 case 'r':
46 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
53 case 'y':
54 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
57 case 'g':
58 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
65 case 'z':
66 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
69 case 'b':
70 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
77
78 case 'w':
79 fields.offsets[i] = 3;
80 fieldSet[i] = exyzw;
81 break;
82 case 'a':
83 fields.offsets[i] = 3;
84 fieldSet[i] = ergba;
85 break;
86 case 'q':
87 fields.offsets[i] = 3;
88 fieldSet[i] = estpq;
89 break;
90 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000091 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000092 return false;
93 }
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000096 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 return false;
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 return false;
106 }
107 }
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
Jamie Madill075edd82013-07-08 13:30:19 -0400118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000126 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 return false;
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000132 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 return false;
134 }
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 return false;
141 }
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
144 } else {
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000154 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000155 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 return false;
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
Jamie Madill075edd82013-07-08 13:30:19 -0400178void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000180 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000182 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000185 diagnostics.writeInfo(pp::Diagnostics::ERROR,
186 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madill075edd82013-07-08 13:30:19 -0400190void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000191 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000192 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000193 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000196 diagnostics.writeInfo(pp::Diagnostics::WARNING,
197 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000198}
199
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000200void TParseContext::trace(const char* str)
201{
202 diagnostics.writeDebug(str);
203}
204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205//
206// Same error message for all places assignments don't work.
207//
Jamie Madill075edd82013-07-08 13:30:19 -0400208void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
216//
217// Same error message for all places unary operations don't work.
218//
Jamie Madill075edd82013-07-08 13:30:19 -0400219void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Same error message for all binary operations don't work.
230//
Jamie Madill075edd82013-07-08 13:30:19 -0400231void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
Jamie Madill075edd82013-07-08 13:30:19 -0400240bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000241 if (!checksPrecisionErrors)
242 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 switch( type ){
244 case EbtFloat:
245 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000246 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 return true;
248 }
249 break;
250 case EbtInt:
251 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253 return true;
254 }
255 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000256 default:
257 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000258 }
259 return false;
260}
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262//
263// Both test and if necessary, spit out an error, to see if the node is really
264// an l-value that can be operated on this way.
265//
266// Returns true if the was an error.
267//
Jamie Madill075edd82013-07-08 13:30:19 -0400268bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 if (binaryNode) {
274 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 switch(binaryNode->getOp()) {
277 case EOpIndexDirect:
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000280 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
284 if (!errorReturn) {
285 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
289
290 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
291 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 offset[value]++;
294 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 return true;
298 }
299 }
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return errorReturn;
303 default:
304 break;
305 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000306 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return true;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* symbol = 0;
313 if (symNode != 0)
314 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madillb120eac2013-06-12 14:08:13 -0400321 case EvqVertexInput: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 case EvqUniform: message = "can't modify a uniform"; break;
323 case EvqVaryingIn: message = "can't modify a varying"; break;
324 case EvqInput: message = "can't modify an input"; break;
325 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
326 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
327 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
328 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 //
331 // Type that can't be written to?
332 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400333 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400335 }
336 if (IsSampler(node->getBasicType())) {
337 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 }
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000342 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 return true;
345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000348 //
349 // Everything else is okay, no error.
350 //
351 if (message == 0)
352 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 //
355 // If we get here, we have an error and a message.
356 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000357 if (symNode) {
358 std::stringstream extraInfoStream;
359 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
360 std::string extraInfo = extraInfoStream.str();
361 error(line, " l-value required", op, extraInfo.c_str());
362 }
363 else {
364 std::stringstream extraInfoStream;
365 extraInfoStream << "(" << message << ")";
366 std::string extraInfo = extraInfoStream.str();
367 error(line, " l-value required", op, extraInfo.c_str());
368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
373//
374// Both test, and if necessary spit out an error, to see if the node is really
375// a constant.
376//
377// Returns true if the was an error.
378//
379bool TParseContext::constErrorCheck(TIntermTyped* node)
380{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000381 if (node->getQualifier() == EvqConst)
382 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000384 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000386 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389//
390// Both test, and if necessary spit out an error, to see if the node is really
391// an integer.
392//
393// Returns true if the was an error.
394//
395bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
396{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000397 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000400 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Both test, and if necessary spit out an error, to see if we are currently
407// globally scoped.
408//
409// Returns true if the was an error.
410//
Jamie Madill075edd82013-07-08 13:30:19 -0400411bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000413 if (global)
414 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000416 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
421//
422// For now, keep it simple: if it starts "gl_", it's reserved, independent
423// of scope. Except, if the symbol table is at the built-in push-level,
424// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000425// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
426// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427//
428// Returns true if there was an error.
429//
Jamie Madill075edd82013-07-08 13:30:19 -0400430bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000432 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000434 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000435 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 return true;
437 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000438 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000439 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000440 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000441 return true;
442 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000443 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000444 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000445 return true;
446 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000447 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000448 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000449 return true;
450 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000451 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000454 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 }
456 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459}
460
461//
462// Make sure there is enough data provided to the constructor to build
463// something of the type of the constructor. Also returns the type of
464// the constructor.
465//
466// Returns true if there was an error in construction.
467//
Jamie Madill075edd82013-07-08 13:30:19 -0400468bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 bool constructingMatrix = false;
473 switch(op) {
474 case EOpConstructMat2:
475 case EOpConstructMat3:
476 case EOpConstructMat4:
477 constructingMatrix = true;
478 break;
479 default:
480 break;
481 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 //
484 // Note: It's okay to have too many components available, but not okay to have unused
485 // arguments. 'full' will go to true when enough args have been seen. If we loop
486 // again, there is an extra argument, so 'overfull' will become true.
487 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 int size = 0;
490 bool constType = true;
491 bool full = false;
492 bool overFull = false;
493 bool matrixInMatrix = false;
494 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000495 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000496 const TParameter& param = function.getParam(i);
497 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000499 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000500 matrixInMatrix = true;
501 if (full)
502 overFull = true;
503 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
504 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000505 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000507 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 arrayArg = true;
509 }
510
511 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000512 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000514 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000515 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 return true;
517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000520 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 return true;
522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000525 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000526 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000527 return true;
528 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000532 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 return true;
534 }
535
daniel@transgaming.com7b17fac2010-12-12 08:52:58 +0000536 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000537 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 return true;
539 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000541 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000542 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
543 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000544 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000545 return true;
546 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000549 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000551 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 return true;
553 }
554 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000555 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 return true;
557 }
558 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000559 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 return true;
561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564}
565
566// This function checks to see if a void variable has been declared and raise an error message for such a case
567//
568// returns true in case of an error
569//
Jamie Madill075edd82013-07-08 13:30:19 -0400570bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000573 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 return true;
575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578}
579
580// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
581//
582// returns true in case of an error
583//
Jamie Madill075edd82013-07-08 13:30:19 -0400584bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 return true;
589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592}
593
594// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
595//
596// returns true in case of an error
597//
Jamie Madill075edd82013-07-08 13:30:19 -0400598bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000600 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000601 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 return true;
603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606}
607
Jamie Madill075edd82013-07-08 13:30:19 -0400608bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 if (pType.type == EbtStruct) {
611 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000612 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613
614 return true;
615 }
616
617 return false;
618 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000619 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 return true;
622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625}
626
Jamie Madill075edd82013-07-08 13:30:19 -0400627bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628{
Jamie Madillb120eac2013-06-12 14:08:13 -0400629 switch (pType.qualifier)
630 {
631 case EvqVaryingIn:
632 case EvqVaryingOut:
633 case EvqAttribute:
634 case EvqVertexInput:
635 case EvqFragmentOutput:
636 if (pType.type == EbtStruct)
637 {
638 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
639 return true;
640 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
644 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
Jamie Madill075edd82013-07-08 13:30:19 -0400649bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400650{
651 if (pType.layoutQualifier.location != -1)
652 {
653 error(line, "location must only be specified for a single input or output variable", "location");
654 return true;
655 }
656
657 return false;
658}
659
Jamie Madill075edd82013-07-08 13:30:19 -0400660bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000662 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
663 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000664 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return true;
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671bool TParseContext::containsSampler(TType& type)
672{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 if (IsSampler(type.getBasicType()))
674 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000676 if (type.getBasicType() == EbtStruct || type.getBasicType() == EbtInterfaceBlock) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 TTypeList& structure = *type.getStruct();
678 for (unsigned int i = 0; i < structure.size(); ++i) {
679 if (containsSampler(*structure[i].type))
680 return true;
681 }
682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685}
686
687//
688// Do size checking for an array type's size.
689//
690// Returns true if there was an error.
691//
Jamie Madill075edd82013-07-08 13:30:19 -0400692bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000695
696 if (constant == 0 || !constant->isScalarInt())
697 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000698 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000699 return true;
700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000702 if (constant->getBasicType() == EbtUInt)
703 {
704 unsigned int uintSize = constant->getUConst(0);
705 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
706 {
707 error(line, "array size too large", "");
708 size = 1;
709 return true;
710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000712 size = static_cast<int>(uintSize);
713 }
714 else
715 {
716 size = constant->getIConst(0);
717
718 if (size <= 0)
719 {
720 error(line, "array size must be a positive integer", "");
721 size = 1;
722 return true;
723 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729//
730// See if this qualifier can be an array.
731//
732// Returns true if there is an error.
733//
Jamie Madill075edd82013-07-08 13:30:19 -0400734bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Jamie Madillb120eac2013-06-12 14:08:13 -0400736 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexInput) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000737 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 return true;
739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
744//
745// See if this type can be an array.
746//
747// Returns true if there is an error.
748//
Jamie Madill075edd82013-07-08 13:30:19 -0400749bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 //
752 // Can the type be an array?
753 //
754 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000755 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 return true;
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
762//
763// Do all the semantic checking for declaring an array, with and
764// without a size, and make the right changes to the symbol table.
765//
766// size == 0 means no specified size.
767//
768// Returns true if there was an error.
769//
Jamie Madill075edd82013-07-08 13:30:19 -0400770bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 //
773 // Don't check for reserved word use until after we know it's not in the symbol table,
774 // because reserved arrays can be redeclared.
775 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 bool builtIn = false;
778 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000779 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 if (symbol == 0 || !sameScope) {
781 if (reservedErrorCheck(line, identifier))
782 return true;
783
784 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000786 if (type.arraySize)
787 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000789 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000791 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000792 return true;
793 }
794 } else {
795 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000796 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000797 return true;
798 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 variable = static_cast<TVariable*>(symbol);
801 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000802 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 return true;
804 }
805 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000806 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 return true;
808 }
809
810 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000811 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 return true;
813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 TType* t = variable->getArrayInformationType();
816 while (t != 0) {
817 if (t->getMaxArraySize() > type.arraySize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000818 error(line, "higher index value already used for the array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 return true;
820 }
821 t->setArraySize(type.arraySize);
822 t = t->getArrayInformationType();
823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000825 if (type.arraySize)
826 variable->getType().setArraySize(type.arraySize);
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 if (voidErrorCheck(line, identifier, type))
830 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000832 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
Jamie Madill075edd82013-07-08 13:30:19 -0400835bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 bool builtIn = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000838 TSymbol* symbol = symbolTable.find(node->getSymbol(), 0, &builtIn);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000840 error(line, " undeclared identifier", node->getSymbol().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000841 return true;
842 }
843 TVariable* variable = static_cast<TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 type->setArrayInformationType(variable->getArrayInformationType());
846 variable->updateArrayInformationType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000848 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
849 // its an error
850 if (node->getSymbol() == "gl_FragData") {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000851 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", 0, &builtIn);
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000852 ASSERT(fragData);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
855 if (fragDataValue <= size) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000856 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return true;
858 }
859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
862 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
863 if (!updateFlag)
864 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000866 size++;
867 variable->getType().setMaxArraySize(size);
868 type->setMaxArraySize(size);
869 TType* tt = type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 while(tt->getArrayInformationType() != 0) {
872 tt = tt->getArrayInformationType();
873 tt->setMaxArraySize(size);
874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
879//
880// Enforce non-initializer type/qualifier rules.
881//
882// Returns true if there was an error.
883//
Jamie Madill075edd82013-07-08 13:30:19 -0400884bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000886 if (type.qualifier == EvqConst)
887 {
888 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000890
891 if (array)
892 {
893 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
894 }
895 else if (type.isStructureContainingArrays())
896 {
897 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
898 }
899 else
900 {
901 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
902 }
903
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 return true;
905 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908}
909
910//
911// Do semantic checking for a variable declaration that has no initializer,
912// and update the symbol table.
913//
914// Returns true if there was an error.
915//
Jamie Madill075edd82013-07-08 13:30:19 -0400916bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 if (reservedErrorCheck(line, identifier))
919 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
zmo@google.comfd747b82011-04-23 01:30:07 +0000921 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000923 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000924 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000926 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000927 return true;
928 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 if (voidErrorCheck(line, identifier, type))
931 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934}
935
Jamie Madill075edd82013-07-08 13:30:19 -0400936bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000938 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000939 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 return true;
941 }
942 if (qualifier == EvqConst && paramQualifier != EvqIn) {
943 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
944 return true;
945 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000947 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000948 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000949 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000950 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953}
954
Jamie Madill075edd82013-07-08 13:30:19 -0400955bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000956{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000957 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000958 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
959 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000960 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 return true;
962 }
zmo@google.comf5450912011-09-09 01:37:19 +0000963 // In GLSL ES, an extension's default behavior is "disable".
964 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000965 error(line, "extension", extension.c_str(), "is disabled");
966 return true;
967 }
968 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000969 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000970 return false;
971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
Jamie Madill075edd82013-07-08 13:30:19 -0400976bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400977{
978 if (structQualifierErrorCheck(identifierLocation, publicType))
979 return true;
980
981 // check for layout qualifier issues
982 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
983
984 if (layoutQualifier.matrixPacking != EmpUnspecified)
985 {
986 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400987 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400988 }
989
990 if (layoutQualifier.blockStorage != EbsUnspecified)
991 {
992 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400993 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400994 }
995
996 if (publicType.qualifier != EvqVertexInput && publicType.qualifier != EvqFragmentOutput && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
997 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400998 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400999 }
1000
1001 return false;
1002}
1003
Jamie Madill075edd82013-07-08 13:30:19 -04001004bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001005{
1006 if (layoutQualifier.location != -1)
1007 {
1008 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1009 return true;
1010 }
1011
1012 return false;
1013}
1014
zmo@google.com09c323a2011-08-12 18:22:25 +00001015bool TParseContext::supportsExtension(const char* extension)
1016{
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001017 const TExtensionBehavior& extbehavior = extensionBehavior();
1018 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1019 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001020}
1021
Jamie Madill075edd82013-07-08 13:30:19 -04001022void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1023{
1024 pp::SourceLocation srcLoc;
1025 srcLoc.file = loc.first_file;
1026 srcLoc.line = loc.first_line;
1027 directiveHandler.handleExtension(srcLoc, extName, behavior);
1028}
1029
1030void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
1031{
1032 pp::SourceLocation srcLoc;
1033 srcLoc.file = loc.first_file;
1034 srcLoc.line = loc.first_line;
1035 directiveHandler.handlePragma(srcLoc, name, value);
1036}
1037
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038/////////////////////////////////////////////////////////////////////////////////
1039//
1040// Non-Errors.
1041//
1042/////////////////////////////////////////////////////////////////////////////////
1043
1044//
1045// Look up a function name in the symbol table, and make sure it is a function.
1046//
1047// Return the function symbol if found, otherwise 0.
1048//
Jamie Madill075edd82013-07-08 13:30:19 -04001049const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001051 // First find by unmangled name to check whether the function name has been
1052 // hidden by a variable name or struct typename.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001053 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001054 if (symbol == 0) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001055 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057
alokp@chromium.org0a576182010-08-09 17:16:27 +00001058 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001059 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 return 0;
1061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062
alokp@chromium.org0a576182010-08-09 17:16:27 +00001063 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001064 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001065 return 0;
1066 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001067
1068 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
1071//
1072// Initializers show up in several places in the grammar. Have one set of
1073// code to handle them here.
1074//
Jamie Madill075edd82013-07-08 13:30:19 -04001075bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001076 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 if (variable == 0) {
1081 if (reservedErrorCheck(line, identifier))
1082 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 if (voidErrorCheck(line, identifier, pType))
1085 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 //
1088 // add variable to symbol table
1089 //
1090 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001091 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001092 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001093 return true;
1094 // don't delete variable, it's used by error recovery, and the pool
1095 // pop will take care of the memory
1096 }
1097 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 //
1100 // identifier must be of type constant, a global, or a temporary
1101 //
1102 TQualifier qualifier = variable->getType().getQualifier();
1103 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001104 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001105 return true;
1106 }
1107 //
1108 // test for and propagate constant
1109 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 if (qualifier == EvqConst) {
1112 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001113 std::stringstream extraInfoStream;
1114 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1115 std::string extraInfo = extraInfoStream.str();
1116 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001117 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 return true;
1119 }
1120 if (type != initializer->getType()) {
1121 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001122 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001123 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001124 return true;
1125 }
1126 if (initializer->getAsConstantUnion()) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001127 ConstantUnion* unionArray = variable->getConstPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001129 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1130 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1131 } else {
1132 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1133 }
1134 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001135 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001136 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001138 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001139 variable->shareConstPointer(constArray);
1140 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001141 std::stringstream extraInfoStream;
1142 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1143 std::string extraInfo = extraInfoStream.str();
1144 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001145 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001146 return true;
1147 }
1148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001150 if (qualifier != EvqConst) {
1151 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1152 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1153 if (intermNode == 0) {
1154 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1155 return true;
1156 }
1157 } else
1158 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001160 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161}
1162
1163bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1164{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001165 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001166 if (!aggrNode->isConstructor())
1167 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001169 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001171 // check if all the child nodes are constants so that they can be inserted into
1172 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001173 TIntermSequence &sequence = aggrNode->getSequence() ;
1174 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1175 if (!(*p)->getAsTyped()->getAsConstantUnion())
1176 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001180}
1181
Jamie Madilla5efff92013-06-06 11:56:47 -04001182TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001183{
1184 TPublicType returnType = typeSpecifier;
1185 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001186 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001187
1188 if (typeSpecifier.array)
1189 {
1190 error(typeSpecifier.line, "not supported", "first-class array");
1191 recover();
1192 returnType.setArray(false);
1193 }
1194
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001195 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001196 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001197 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1198 {
1199 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1200 recover();
1201 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001202
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001203 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1204 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1205 {
1206 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1207 recover();
1208 }
1209 }
1210 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001211 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001212 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001213 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001214 case EvqVertexInput:
1215 case EvqFragmentOutput:
1216 case EvqVaryingIn:
1217 case EvqVaryingOut:
1218 if (typeSpecifier.type == EbtBool)
1219 {
1220 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1221 recover();
1222 }
1223 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001224
Jamie Madillb120eac2013-06-12 14:08:13 -04001225 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001226 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001227 }
1228
1229 return returnType;
1230}
1231
Jamie Madill075edd82013-07-08 13:30:19 -04001232TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001233{
1234 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1235 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1236
1237 if (identifier != "")
1238 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001239 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001240 recover();
1241
1242 // this error check can mutate the type
1243 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1244 recover();
1245
1246 TVariable* variable = 0;
1247
1248 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1249 recover();
1250
1251 if (variable && symbol)
1252 {
1253 symbol->setId(variable->getUniqueId());
1254 }
1255 }
1256
1257 return aggregate;
1258}
1259
Jamie Madill075edd82013-07-08 13:30:19 -04001260TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001261{
Jamie Madill51a53c72013-06-19 09:24:43 -04001262 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001263 recover();
1264
1265 // this error check can mutate the type
1266 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1267 recover();
1268
1269 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1270 {
1271 recover();
1272 }
1273
1274 TPublicType arrayType = publicType;
1275
1276 int size;
1277 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1278 {
1279 recover();
1280 }
1281 else
1282 {
1283 arrayType.setArray(true, size);
1284 }
1285
1286 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1287 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1288 TVariable* variable = 0;
1289
1290 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1291 recover();
1292
1293 if (variable && symbol)
1294 {
1295 symbol->setId(variable->getUniqueId());
1296 }
1297
1298 return aggregate;
1299}
1300
Jamie Madill075edd82013-07-08 13:30:19 -04001301TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001302{
Jamie Madilla5efff92013-06-06 11:56:47 -04001303 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001304 recover();
1305
1306 TIntermNode* intermNode;
1307 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1308 {
1309 //
1310 // Build intermediate representation
1311 //
1312 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1313 }
1314 else
1315 {
1316 recover();
1317 return NULL;
1318 }
1319}
1320
Jamie Madill075edd82013-07-08 13:30:19 -04001321TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001322{
1323 if (publicType.type == EbtInvariant && !identifierSymbol)
1324 {
1325 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1326 recover();
1327 }
1328
1329 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1330 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1331
1332 if (structQualifierErrorCheck(identifierLocation, publicType))
1333 recover();
1334
Jamie Madill0bd18df2013-06-20 11:55:52 -04001335 if (locationDeclaratorListCheck(identifierLocation, publicType))
1336 recover();
1337
Jamie Madill502d66f2013-06-20 11:55:52 -04001338 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1339 recover();
1340
1341 TVariable* variable = 0;
1342 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1343 recover();
1344 if (symbol && variable)
1345 symbol->setId(variable->getUniqueId());
1346
1347 return intermAggregate;
1348}
1349
Jamie Madill075edd82013-07-08 13:30:19 -04001350TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001351{
1352 if (structQualifierErrorCheck(identifierLocation, publicType))
1353 recover();
1354
Jamie Madill0bd18df2013-06-20 11:55:52 -04001355 if (locationDeclaratorListCheck(identifierLocation, publicType))
1356 recover();
1357
Jamie Madill502d66f2013-06-20 11:55:52 -04001358 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1359 recover();
1360
1361 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1362 {
1363 recover();
1364 }
1365 else if (indexExpression)
1366 {
1367 int size;
1368 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1369 recover();
1370 TPublicType arrayType(publicType);
1371 arrayType.setArray(true, size);
1372 TVariable* variable = NULL;
1373 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1374 recover();
1375 TType type = TType(arrayType);
1376 type.setArraySize(size);
1377
1378 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1379 }
1380 else
1381 {
1382 TPublicType arrayType(publicType);
1383 arrayType.setArray(true);
1384 TVariable* variable = NULL;
1385 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1386 recover();
1387 }
1388
1389 return NULL;
1390}
1391
Jamie Madill075edd82013-07-08 13:30:19 -04001392TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001393{
1394 if (structQualifierErrorCheck(identifierLocation, publicType))
1395 recover();
1396
Jamie Madill0bd18df2013-06-20 11:55:52 -04001397 if (locationDeclaratorListCheck(identifierLocation, publicType))
1398 recover();
1399
Jamie Madill502d66f2013-06-20 11:55:52 -04001400 TIntermNode* intermNode;
1401 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1402 {
1403 //
1404 // build the intermediate representation
1405 //
1406 if (intermNode)
1407 {
1408 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1409 }
1410 else
1411 {
1412 return declaratorList;
1413 }
1414 }
1415 else
1416 {
1417 recover();
1418 return NULL;
1419 }
1420}
1421
Jamie Madilla295edf2013-06-06 11:56:48 -04001422void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1423{
1424 if (typeQualifier.qualifier != EvqUniform)
1425 {
1426 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1427 recover();
1428 return;
1429 }
1430
1431 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1432 ASSERT(!layoutQualifier.isEmpty());
1433
1434 if (shaderVersion < 300)
1435 {
1436 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1437 recover();
1438 return;
1439 }
1440
1441 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1442 {
1443 recover();
1444 return;
1445 }
1446
Jamie Madill099c0f32013-06-20 11:55:52 -04001447 if (layoutQualifier.matrixPacking != EmpUnspecified)
1448 {
1449 defaultMatrixPacking = layoutQualifier.matrixPacking;
1450 }
1451
Jamie Madill1566ef72013-06-20 11:55:54 -04001452 if (layoutQualifier.blockStorage != EmpUnspecified)
1453 {
1454 defaultBlockStorage = layoutQualifier.blockStorage;
1455 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001456}
1457
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001458TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1459{
1460 TOperator op = EOpNull;
1461 if (publicType.userDef)
1462 {
1463 op = EOpConstructStruct;
1464 }
1465 else
1466 {
1467 switch (publicType.type)
1468 {
1469 case EbtFloat:
1470 if (publicType.isMatrix())
1471 {
1472 // TODO: non-square matrices
1473 switch(publicType.getCols())
1474 {
1475 case 2: op = EOpConstructMat2; break;
1476 case 3: op = EOpConstructMat3; break;
1477 case 4: op = EOpConstructMat4; break;
1478 }
1479 }
1480 else
1481 {
1482 switch(publicType.getNominalSize())
1483 {
1484 case 1: op = EOpConstructFloat; break;
1485 case 2: op = EOpConstructVec2; break;
1486 case 3: op = EOpConstructVec3; break;
1487 case 4: op = EOpConstructVec4; break;
1488 }
1489 }
1490 break;
1491
1492 case EbtInt:
1493 switch(publicType.getNominalSize())
1494 {
1495 case 1: op = EOpConstructInt; break;
1496 case 2: op = EOpConstructIVec2; break;
1497 case 3: op = EOpConstructIVec3; break;
1498 case 4: op = EOpConstructIVec4; break;
1499 }
1500 break;
1501
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001502 case EbtUInt:
1503 switch(publicType.getNominalSize())
1504 {
Nicolas Capensab60b932013-06-05 10:31:21 -04001505 case 1: op = EOpConstructUInt; break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001506 case 2: op = EOpConstructUVec2; break;
1507 case 3: op = EOpConstructUVec3; break;
1508 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001509 }
1510 break;
1511
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001512 case EbtBool:
1513 switch(publicType.getNominalSize())
1514 {
1515 case 1: op = EOpConstructBool; break;
1516 case 2: op = EOpConstructBVec2; break;
1517 case 3: op = EOpConstructBVec3; break;
1518 case 4: op = EOpConstructBVec4; break;
1519 }
1520 break;
1521
1522 default: break;
1523 }
1524
1525 if (op == EOpNull)
1526 {
1527 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1528 recover();
1529 publicType.type = EbtFloat;
1530 op = EOpConstructFloat;
1531 }
1532 }
1533
1534 TString tempString;
1535 TType type(publicType);
1536 return new TFunction(&tempString, type, op);
1537}
1538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539// This function is used to test for the correctness of the parameters passed to various constructor functions
1540// and also convert them to the right datatype if it is allowed and required.
1541//
1542// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1543//
Jamie Madill075edd82013-07-08 13:30:19 -04001544TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001546 if (node == 0)
1547 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001548
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001549 TIntermAggregate* aggrNode = node->getAsAggregate();
1550
alokp@chromium.org58e54292010-08-24 21:40:03 +00001551 TTypeList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001552 if (op == EOpConstructStruct)
1553 memberTypes = type->getStruct()->begin();
1554
1555 TType elementType = *type;
1556 if (type->isArray())
1557 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001559 bool singleArg;
1560 if (aggrNode) {
1561 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1562 singleArg = true;
1563 else
1564 singleArg = false;
1565 } else
1566 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001568 TIntermTyped *newNode;
1569 if (singleArg) {
1570 // If structure constructor or array constructor is being called
1571 // for only one parameter inside the structure, we need to call constructStruct function once.
1572 if (type->isArray())
1573 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1574 else if (op == EOpConstructStruct)
1575 newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false);
1576 else
1577 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001578
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001579 if (newNode && newNode->getAsAggregate()) {
1580 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1581 if (constConstructor)
1582 return constConstructor;
1583 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001585 return newNode;
1586 }
1587
1588 //
1589 // Handle list of arguments.
1590 //
1591 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1592 // if the structure constructor contains more than one parameter, then construct
1593 // each parameter
1594
1595 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1596
1597 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1598 // to the right type if possible (and allowed).
1599 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1600
1601 for (TIntermSequence::iterator p = sequenceVector.begin();
1602 p != sequenceVector.end(); p++, paramCount++) {
1603 if (type->isArray())
1604 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1605 else if (op == EOpConstructStruct)
1606 newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true);
1607 else
1608 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1609
1610 if (newNode) {
1611 *p = newNode;
1612 }
1613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001615 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1616 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1617 if (constConstructor)
1618 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001620 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621}
1622
1623TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1624{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001625 bool canBeFolded = areAllChildConst(aggrNode);
1626 aggrNode->setType(type);
1627 if (canBeFolded) {
1628 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001629 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001630 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001631 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001632 }
1633 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001634 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001635 }
1636 if (returnVal)
1637 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001639 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001642 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643}
1644
1645// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1646// for the parameter to the constructor (passed to this function). Essentially, it converts
1647// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1648// float, then float is converted to int.
1649//
1650// Returns 0 for an error or the constructed node.
1651//
Jamie Madill075edd82013-07-08 13:30:19 -04001652TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001654 TIntermTyped* newNode;
1655 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001657 //
1658 // First, convert types as needed.
1659 //
1660 switch (op) {
1661 case EOpConstructVec2:
1662 case EOpConstructVec3:
1663 case EOpConstructVec4:
1664 case EOpConstructMat2:
1665 case EOpConstructMat3:
1666 case EOpConstructMat4:
1667 case EOpConstructFloat:
1668 basicOp = EOpConstructFloat;
1669 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001671 case EOpConstructIVec2:
1672 case EOpConstructIVec3:
1673 case EOpConstructIVec4:
1674 case EOpConstructInt:
1675 basicOp = EOpConstructInt;
1676 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001678 case EOpConstructUVec2:
1679 case EOpConstructUVec3:
1680 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001681 case EOpConstructUInt:
1682 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001683 break;
1684
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001685 case EOpConstructBVec2:
1686 case EOpConstructBVec3:
1687 case EOpConstructBVec4:
1688 case EOpConstructBool:
1689 basicOp = EOpConstructBool;
1690 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001692 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001693 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001694 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001696 return 0;
1697 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001698 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001699 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001700 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001701 return 0;
1702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001704 //
1705 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1706 //
1707
1708 // Otherwise, skip out early.
1709 if (subset || (newNode != node && newNode->getType() == *type))
1710 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001712 // setAggregateOperator will insert a new node for the constructor, as needed.
1713 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714}
1715
1716// This function tests for the type of the parameters to the structures constructors. Raises
1717// an error message if the expected type does not match the parameter passed to the constructor.
1718//
1719// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1720//
Jamie Madill075edd82013-07-08 13:30:19 -04001721TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001723 if (*type == node->getAsTyped()->getType()) {
1724 if (subset)
1725 return node->getAsTyped();
1726 else
1727 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1728 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001729 std::stringstream extraInfoStream;
1730 extraInfoStream << "cannot convert parameter " << paramCount
1731 << " from '" << node->getAsTyped()->getType().getBasicString()
1732 << "' to '" << type->getBasicString() << "'";
1733 std::string extraInfo = extraInfoStream.str();
1734 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001735 recover();
1736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001738 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739}
1740
1741//
1742// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1743// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1744// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1745// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1746// a constant matrix.
1747//
Jamie Madill075edd82013-07-08 13:30:19 -04001748TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001750 TIntermTyped* typedNode;
1751 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001753 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001754 if (tempConstantNode) {
1755 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001757 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001758 return node;
1759 }
1760 } 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 +00001761 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001762 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 return 0;
1765 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001767 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001769 for (int i = 0; i < fields.num; i++) {
1770 if (fields.offsets[i] >= node->getType().getObjectSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001771 std::stringstream extraInfoStream;
1772 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1773 std::string extraInfo = extraInfoStream.str();
1774 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001775 recover();
1776 fields.offsets[i] = 0;
1777 }
1778
1779 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001781 }
1782 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1783 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784}
1785
1786//
1787// This function returns the column being accessed from a constant matrix. The values are retrieved from
1788// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1789// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1790// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1791//
Jamie Madill075edd82013-07-08 13:30:19 -04001792TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001794 TIntermTyped* typedNode;
1795 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001797 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001798 std::stringstream extraInfoStream;
1799 extraInfoStream << "matrix field selection out of range '" << index << "'";
1800 std::string extraInfo = extraInfoStream.str();
1801 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 recover();
1803 index = 0;
1804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001807 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001808 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001809 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1810 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001811 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001812 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001814 return 0;
1815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001817 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818}
1819
1820
1821//
1822// This function returns an element of an array accessed from a constant array. The values are retrieved from
1823// the symbol table and parse-tree is built for the type of the element. The input
1824// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1825// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1826//
Jamie Madill075edd82013-07-08 13:30:19 -04001827TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001829 TIntermTyped* typedNode;
1830 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1831 TType arrayElementType = node->getType();
1832 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001835 std::stringstream extraInfoStream;
1836 extraInfoStream << "array field selection out of range '" << index << "'";
1837 std::string extraInfo = extraInfoStream.str();
1838 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 recover();
1840 index = 0;
1841 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001843 int arrayElementSize = arrayElementType.getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001846 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001847 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1848 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001849 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001850 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001852 return 0;
1853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001855 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856}
1857
1858
1859//
1860// This function returns the value of a particular field inside a constant structure from the symbol table.
1861// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1862// function and returns the parse-tree with the values of the embedded/nested struct.
1863//
Jamie Madill075edd82013-07-08 13:30:19 -04001864TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865{
alokp@chromium.org58e54292010-08-24 21:40:03 +00001866 const TTypeList* fields = node->getType().getStruct();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001867 TIntermTyped *typedNode;
1868 int instanceSize = 0;
1869 unsigned int index = 0;
1870 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001872 for ( index = 0; index < fields->size(); ++index) {
1873 if ((*fields)[index].type->getFieldName() == identifier) {
1874 break;
1875 } else {
1876 instanceSize += (*fields)[index].type->getObjectSize();
1877 }
1878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001880 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001881 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001883 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1884 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001885 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001886 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001888 return 0;
1889 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001891 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892}
1893
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001894//
1895// Interface/uniform blocks
1896//
Jamie Madill075edd82013-07-08 13:30:19 -04001897TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TTypeList* typeList,
1898 const TString& instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001899{
1900 if (reservedErrorCheck(nameLine, blockName))
1901 recover();
1902
1903 if (typeQualifier.qualifier != EvqUniform)
1904 {
1905 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1906 recover();
1907 }
1908
Jamie Madill099c0f32013-06-20 11:55:52 -04001909 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1910 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001911 {
1912 recover();
1913 }
1914
Jamie Madill099c0f32013-06-20 11:55:52 -04001915 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1916 {
1917 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1918 }
1919
Jamie Madill1566ef72013-06-20 11:55:54 -04001920 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1921 {
1922 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1923 }
1924
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001925 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1926 if (!symbolTable.declare(*blockNameSymbol)) {
1927 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1928 recover();
1929 }
1930
1931 // check for sampler types
1932 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1933 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1934 TType* memberType = memberTypeLine.type;
1935 if (IsSampler(memberType->getBasicType())) {
1936 error(memberTypeLine.line, "unsupported type", memberType->getBasicString(), "sampler types are not allowed in interface blocks");
1937 recover();
1938 }
1939
1940 const TQualifier qualifier = memberTypeLine.type->getQualifier();
1941 switch (qualifier)
1942 {
1943 case EvqGlobal:
1944 case EvqUniform:
1945 break;
1946 default:
1947 error(memberTypeLine.line, "invalid qualifier on interface block member", getQualifierString(qualifier));
1948 recover();
1949 break;
1950 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001951
1952 // check layout qualifiers
Jamie Madill099c0f32013-06-20 11:55:52 -04001953 TLayoutQualifier memberLayoutQualifier = memberType->getLayoutQualifier();
1954 if (layoutLocationErrorCheck(memberTypeLine.line, memberLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001955 {
1956 recover();
1957 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001958
Jamie Madill1566ef72013-06-20 11:55:54 -04001959 if (memberLayoutQualifier.blockStorage != EbsUnspecified)
1960 {
1961 error(memberTypeLine.line, "invalid layout qualifier:", getBlockStorageString(memberLayoutQualifier.blockStorage), "cannot be used here");
1962 recover();
1963 }
1964
Jamie Madill099c0f32013-06-20 11:55:52 -04001965 if (memberLayoutQualifier.matrixPacking == EmpUnspecified)
1966 {
1967 memberLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1968 }
1969 else if (!memberType->isMatrix())
1970 {
1971 error(memberTypeLine.line, "invalid layout qualifier:", getMatrixPackingString(memberLayoutQualifier.matrixPacking), "can only be used on matrix types");
1972 recover();
1973 }
1974
1975 memberType->setLayoutQualifier(memberLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001976 }
1977
1978 TType* interfaceBlock = new TType(typeList, blockName);
1979 interfaceBlock->setBasicType(EbtInterfaceBlock);
1980 interfaceBlock->setQualifier(typeQualifier.qualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04001981 interfaceBlock->setLayoutQualifier(blockLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001982
1983 TString symbolName = "";
1984 int symbolId = 0;
1985
1986 if (instanceName == "")
1987 {
1988 // define symbols for the members of the interface block
1989 for (size_t memberIndex = 0; memberIndex < typeList->size(); ++memberIndex) {
1990 const TTypeLine& memberTypeLine = (*typeList)[memberIndex];
1991 TType* memberType = memberTypeLine.type;
1992 memberType->setInterfaceBlockType(interfaceBlock);
1993 TVariable* memberVariable = new TVariable(&memberType->getFieldName(), *memberType);
1994 memberVariable->setQualifier(typeQualifier.qualifier);
1995 if (!symbolTable.declare(*memberVariable)) {
1996 error(memberTypeLine.line, "redefinition", memberType->getFieldName().c_str(), "interface block member name");
1997 recover();
1998 }
1999 }
2000 }
2001 else
2002 {
2003 interfaceBlock->setInstanceName(instanceName);
2004
2005 // add array index
2006 if (arrayIndex != NULL)
2007 {
2008 int size;
2009 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, size))
2010 recover();
2011 interfaceBlock->setArraySize(size);
2012 }
2013
2014 // add a symbol for this interface block
2015 TVariable* instanceTypeDef = new TVariable(&instanceName, *interfaceBlock, false);
2016 instanceTypeDef->setQualifier(typeQualifier.qualifier);
2017 if (!symbolTable.declare(*instanceTypeDef)) {
2018 error(instanceLine, "redefinition", instanceName.c_str(), "interface block instance name");
2019 recover();
2020 }
2021
2022 symbolId = instanceTypeDef->getUniqueId();
2023 symbolName = instanceTypeDef->getName();
2024 }
2025
2026 exitStructDeclaration();
2027 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, *interfaceBlock, typeQualifier.line), nameLine);
2028 aggregate->setOp(EOpDeclaration);
2029 return aggregate;
2030}
2031
Jamie Madill075edd82013-07-08 13:30:19 -04002032bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002033{
2034 ++structNestingLevel;
2035
2036 // Embedded structure definitions are not supported per GLSL ES spec.
2037 // They aren't allowed in GLSL either, but we need to detect this here
2038 // so we don't rely on the GLSL compiler to catch it.
2039 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002040 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002041 return true;
2042 }
2043
2044 return false;
2045}
2046
2047void TParseContext::exitStructDeclaration()
2048{
2049 --structNestingLevel;
2050}
2051
2052namespace {
2053
2054const int kWebGLMaxStructNesting = 4;
2055
2056} // namespace
2057
Jamie Madill075edd82013-07-08 13:30:19 -04002058bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TType& fieldType)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002059{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002060 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002061 return false;
2062 }
2063
2064 if (fieldType.getBasicType() != EbtStruct) {
2065 return false;
2066 }
2067
2068 // We're already inside a structure definition at this point, so add
2069 // one to the field's struct nesting.
kbr@chromium.org9a4d1122012-01-05 20:06:28 +00002070 if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002071 std::stringstream extraInfoStream;
2072 extraInfoStream << "Reference of struct type " << fieldType.getTypeName()
2073 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2074 std::string extraInfo = extraInfoStream.str();
2075 error(line, "", "", extraInfo.c_str());
kbr@chromium.org476541f2011-10-27 21:14:51 +00002076 return true;
2077 }
2078
2079 return false;
2080}
2081
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002082//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002083// Parse an array index expression
2084//
Jamie Madill075edd82013-07-08 13:30:19 -04002085TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002086{
2087 TIntermTyped *indexedExpression = NULL;
2088
2089 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2090 {
2091 if (baseExpression->getAsSymbolNode())
2092 {
2093 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2094 }
2095 else
2096 {
2097 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2098 }
2099 recover();
2100 }
2101 if (baseExpression->getType().getQualifier() == EvqConst && indexExpression->getQualifier() == EvqConst)
2102 {
2103 if (baseExpression->isArray())
2104 {
2105 // constant folding for arrays
2106 indexedExpression = addConstArrayNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2107 }
2108 else if (baseExpression->isVector())
2109 {
2110 // constant folding for vectors
2111 TVectorFields fields;
2112 fields.num = 1;
2113 fields.offsets[0] = indexExpression->getAsConstantUnion()->getIConst(0); // need to do it this way because v.xy sends fields integer array
2114 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2115 }
2116 else if (baseExpression->isMatrix())
2117 {
2118 // constant folding for matrices
2119 indexedExpression = addConstMatrixNode(indexExpression->getAsConstantUnion()->getIConst(0), baseExpression, location);
2120 }
2121 }
2122 else
2123 {
2124 if (indexExpression->getQualifier() == EvqConst)
2125 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002126 const bool isMatrixOrVector = (baseExpression->isVector() || baseExpression->isMatrix());
2127 const bool indexOOR = (isMatrixOrVector && baseExpression->getType().getNominalSize() <= indexExpression->getAsConstantUnion()->getIConst(0));
2128
2129 // check for index out-of-range
2130 if (indexOOR && !baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002131 {
2132 std::stringstream extraInfoStream;
2133 extraInfoStream << "field selection out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2134 std::string extraInfo = extraInfoStream.str();
2135 error(location, "", "[", extraInfo.c_str());
2136 recover();
2137 }
2138 else
2139 {
2140 if (baseExpression->isArray())
2141 {
2142 if (baseExpression->getType().getArraySize() == 0)
2143 {
2144 if (baseExpression->getType().getMaxArraySize() <= indexExpression->getAsConstantUnion()->getIConst(0))
2145 {
2146 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), indexExpression->getAsConstantUnion()->getIConst(0), true, location))
2147 recover();
2148 }
2149 else
2150 {
2151 if (arraySetMaxSize(baseExpression->getAsSymbolNode(), baseExpression->getTypePointer(), 0, false, location))
2152 recover();
2153 }
2154 }
2155 else if ( indexExpression->getAsConstantUnion()->getIConst(0) >= baseExpression->getType().getArraySize())
2156 {
2157 std::stringstream extraInfoStream;
2158 extraInfoStream << "array index out of range '" << indexExpression->getAsConstantUnion()->getIConst(0) << "'";
2159 std::string extraInfo = extraInfoStream.str();
2160 error(location, "", "[", extraInfo.c_str());
2161 recover();
2162 }
2163 }
2164 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2165 }
2166 }
2167 else
2168 {
2169 if (baseExpression->isArray() && baseExpression->getType().getArraySize() == 0)
2170 {
2171 error(location, "", "[", "array must be redeclared with a size before being indexed with a variable");
2172 recover();
2173 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002174 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2175 {
2176 error(location, "", "[", "array indexes for interface blocks arrays must be constant integeral expressions");
2177 recover();
2178 }
Jamie Madill46131a32013-06-20 11:55:50 -04002179 else if (baseExpression->getQualifier() == EvqFragmentOutput)
2180 {
2181 error(location, "", "[", "array indexes for output variables must be constant integeral expressions");
2182 recover();
2183 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002184
2185 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2186 }
2187 }
2188 if (indexedExpression == 0)
2189 {
2190 ConstantUnion *unionArray = new ConstantUnion[1];
2191 unionArray->setFConst(0.0f);
2192 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2193 }
2194 else if (baseExpression->isArray())
2195 {
2196 if (baseExpression->getType().getStruct())
2197 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002198 TType copyOfType(baseExpression->getType().getStruct(), baseExpression->getType().getTypeName());
2199 copyOfType.setBasicType(baseExpression->getType().getBasicType()); // necessary to preserve interface block basic type
2200 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002201 }
2202 else
2203 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002204 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002205 }
2206
2207 if (baseExpression->getType().getQualifier() == EvqConst)
2208 {
2209 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2210 }
2211 }
2212 else if (baseExpression->isMatrix() && baseExpression->getType().getQualifier() == EvqConst)
2213 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002214 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002215 }
2216 else if (baseExpression->isMatrix())
2217 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002218 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002219 }
2220 else if (baseExpression->isVector() && baseExpression->getType().getQualifier() == EvqConst)
2221 {
2222 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst));
2223 }
2224 else if (baseExpression->isVector())
2225 {
2226 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
2227 }
2228 else
2229 {
2230 indexedExpression->setType(baseExpression->getType());
2231 }
2232
2233 return indexedExpression;
2234}
2235
Jamie Madill075edd82013-07-08 13:30:19 -04002236TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002237{
2238 TIntermTyped *indexedExpression = NULL;
2239
2240 if (baseExpression->isArray())
2241 {
2242 error(fieldLocation, "cannot apply dot operator to an array", ".");
2243 recover();
2244 }
2245
2246 if (baseExpression->isVector())
2247 {
2248 TVectorFields fields;
2249 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2250 {
2251 fields.num = 1;
2252 fields.offsets[0] = 0;
2253 recover();
2254 }
2255
2256 if (baseExpression->getType().getQualifier() == EvqConst)
2257 {
2258 // constant folding for vector fields
2259 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2260 if (indexedExpression == 0)
2261 {
2262 recover();
2263 indexedExpression = baseExpression;
2264 }
2265 else
2266 {
2267 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2268 }
2269 }
2270 else
2271 {
2272 TString vectorString = fieldString;
2273 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2274 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2275 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2276 }
2277 }
2278 else if (baseExpression->isMatrix())
2279 {
2280 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002281 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002282 {
2283 fields.wholeRow = false;
2284 fields.wholeCol = false;
2285 fields.row = 0;
2286 fields.col = 0;
2287 recover();
2288 }
2289
2290 if (fields.wholeRow || fields.wholeCol)
2291 {
2292 error(dotLocation, " non-scalar fields not implemented yet", ".");
2293 recover();
2294 ConstantUnion *unionArray = new ConstantUnion[1];
2295 unionArray->setIConst(0);
2296 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2297 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002298 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002299 }
2300 else
2301 {
2302 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002303 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002304 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2305 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2306 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2307 }
2308 }
2309 else if (baseExpression->getBasicType() == EbtStruct)
2310 {
2311 bool fieldFound = false;
2312 const TTypeList* fields = baseExpression->getType().getStruct();
2313 if (fields == 0)
2314 {
2315 error(dotLocation, "structure has no fields", "Internal Error");
2316 recover();
2317 indexedExpression = baseExpression;
2318 }
2319 else
2320 {
2321 unsigned int i;
2322 for (i = 0; i < fields->size(); ++i)
2323 {
2324 if ((*fields)[i].type->getFieldName() == fieldString)
2325 {
2326 fieldFound = true;
2327 break;
2328 }
2329 }
2330 if (fieldFound)
2331 {
2332 if (baseExpression->getType().getQualifier() == EvqConst)
2333 {
2334 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2335 if (indexedExpression == 0)
2336 {
2337 recover();
2338 indexedExpression = baseExpression;
2339 }
2340 else
2341 {
2342 indexedExpression->setType(*(*fields)[i].type);
2343 // change the qualifier of the return type, not of the structure field
2344 // as the structure definition is shared between various structures.
2345 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2346 }
2347 }
2348 else
2349 {
2350 ConstantUnion *unionArray = new ConstantUnion[1];
2351 unionArray->setIConst(i);
2352 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2353 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2354 indexedExpression->setType(*(*fields)[i].type);
2355 }
2356 }
2357 else
2358 {
2359 error(dotLocation, " no such field in structure", fieldString.c_str());
2360 recover();
2361 indexedExpression = baseExpression;
2362 }
2363 }
2364 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002365 else if (baseExpression->getBasicType() == EbtInterfaceBlock)
2366 {
2367 bool fieldFound = false;
2368 const TTypeList* fields = baseExpression->getType().getStruct();
2369 if (fields == 0)
2370 {
2371 error(dotLocation, "interface block has no fields", "Internal Error");
2372 recover();
2373 indexedExpression = baseExpression;
2374 }
2375 else
2376 {
2377 unsigned int i;
2378 for (i = 0; i < fields->size(); ++i)
2379 {
2380 if ((*fields)[i].type->getFieldName() == fieldString)
2381 {
2382 fieldFound = true;
2383 break;
2384 }
2385 }
2386 if (fieldFound)
2387 {
2388 ConstantUnion *unionArray = new ConstantUnion[1];
2389 unionArray->setIConst(i);
2390 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *(*fields)[i].type, fieldLocation);
2391 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2392 indexedExpression->setType(*(*fields)[i].type);
2393 }
2394 else
2395 {
2396 error(dotLocation, " no such field in interface block", fieldString.c_str());
2397 recover();
2398 indexedExpression = baseExpression;
2399 }
2400 }
2401 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002402 else
2403 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002404 if (shaderVersion < 300)
2405 {
2406 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2407 }
2408 else
2409 {
2410 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2411 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002412 recover();
2413 indexedExpression = baseExpression;
2414 }
2415
2416 return indexedExpression;
2417}
2418
Jamie Madill075edd82013-07-08 13:30:19 -04002419TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002420{
Jamie Madilla5efff92013-06-06 11:56:47 -04002421 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002422
Jamie Madilla5efff92013-06-06 11:56:47 -04002423 qualifier.location = -1;
2424 qualifier.matrixPacking = EmpUnspecified;
2425 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002426
2427 if (qualifierType == "shared")
2428 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002429 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002430 }
2431 else if (qualifierType == "packed")
2432 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002434 }
2435 else if (qualifierType == "std140")
2436 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002437 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002438 }
2439 else if (qualifierType == "row_major")
2440 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002441 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002442 }
2443 else if (qualifierType == "column_major")
2444 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002445 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002446 }
2447 else if (qualifierType == "location")
2448 {
2449 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2450 recover();
2451 }
2452 else
2453 {
2454 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2455 recover();
2456 }
2457
Jamie Madilla5efff92013-06-06 11:56:47 -04002458 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002459}
2460
Jamie Madill075edd82013-07-08 13:30:19 -04002461TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002462{
Jamie Madilla5efff92013-06-06 11:56:47 -04002463 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002464
Jamie Madilla5efff92013-06-06 11:56:47 -04002465 qualifier.location = -1;
2466 qualifier.matrixPacking = EmpUnspecified;
2467 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002468
2469 if (qualifierType != "location")
2470 {
2471 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2472 recover();
2473 }
2474 else
2475 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002476 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002477 if (intValue < 0)
2478 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002479 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002480 recover();
2481 }
2482 else
2483 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002484 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002485 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002486 }
2487
Jamie Madilla5efff92013-06-06 11:56:47 -04002488 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002489}
2490
Jamie Madilla5efff92013-06-06 11:56:47 -04002491TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002492{
Jamie Madilla5efff92013-06-06 11:56:47 -04002493 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002494
Jamie Madilla5efff92013-06-06 11:56:47 -04002495 if (rightQualifier.location != -1)
2496 {
2497 joinedQualifier.location = rightQualifier.location;
2498 }
2499 if (rightQualifier.matrixPacking != EmpUnspecified)
2500 {
2501 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2502 }
2503 if (rightQualifier.blockStorage != EbsUnspecified)
2504 {
2505 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2506 }
2507
2508 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002509}
2510
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002511TTypeList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TTypeList *typeList)
2512{
2513 if (voidErrorCheck(typeSpecifier.line, (*typeList)[0].type->getFieldName(), typeSpecifier)) {
2514 recover();
2515 }
2516
2517 for (unsigned int i = 0; i < typeList->size(); ++i) {
2518 //
2519 // Careful not to replace already known aspects of type, like array-ness
2520 //
2521 TType* type = (*typeList)[i].type;
2522 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002523 type->setPrimarySize(typeSpecifier.primarySize);
2524 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002525 type->setPrecision(typeSpecifier.precision);
2526 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002527 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002528
2529 // don't allow arrays of arrays
2530 if (type->isArray()) {
2531 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2532 recover();
2533 }
2534 if (typeSpecifier.array)
2535 type->setArraySize(typeSpecifier.arraySize);
2536 if (typeSpecifier.userDef) {
2537 type->setStruct(typeSpecifier.userDef->getStruct());
2538 type->setTypeName(typeSpecifier.userDef->getTypeName());
2539 }
2540
2541 if (structNestingErrorCheck(typeSpecifier.line, *type)) {
2542 recover();
2543 }
2544 }
2545
2546 return typeList;
2547}
2548
Jamie Madill075edd82013-07-08 13:30:19 -04002549TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString &structName, TTypeList* typeList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002550{
2551 TType* structure = new TType(typeList, structName);
2552
2553 if (!structName.empty())
2554 {
2555 if (reservedErrorCheck(nameLine, structName))
2556 {
2557 recover();
2558 }
2559 TVariable* userTypeDef = new TVariable(&structName, *structure, true);
2560 if (!symbolTable.declare(*userTypeDef)) {
2561 error(nameLine, "redefinition", structName.c_str(), "struct");
2562 recover();
2563 }
2564 }
2565
2566 // ensure we do not specify any storage qualifiers on the struct members
2567 for (unsigned int typeListIndex = 0; typeListIndex < typeList->size(); typeListIndex++)
2568 {
2569 const TTypeLine &typeLine = (*typeList)[typeListIndex];
2570 const TQualifier qualifier = typeLine.type->getQualifier();
2571 switch (qualifier)
2572 {
2573 case EvqGlobal:
2574 case EvqTemporary:
2575 break;
2576 default:
2577 error(typeLine.line, "invalid qualifier on struct member", getQualifierString(qualifier));
2578 recover();
2579 break;
2580 }
2581 }
2582
2583 TPublicType publicType;
2584 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2585 publicType.userDef = structure;
2586 exitStructDeclaration();
2587
2588 return publicType;
2589}
2590
2591//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002592// Parse an array of strings using yyparse.
2593//
2594// Returns 0 for success.
2595//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002596int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002597 TParseContext* context) {
2598 if ((count == 0) || (string == NULL))
2599 return 1;
2600
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002601 if (glslang_initialize(context))
2602 return 1;
2603
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002604 int error = glslang_scan(count, string, length, context);
2605 if (!error)
2606 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002607
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002608 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002609
alokp@chromium.org6b495712012-06-29 00:06:58 +00002610 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002611}
2612
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002614