blob: 8ff9d30efbad903250f673553aac69c25ead12e9 [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
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
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
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/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;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500185 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000186 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;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500196 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000197 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 Madill19571812013-08-12 15:26:34 -0700321 case EvqFragmentIn: message = "can't modify an input"; break;
322 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 case EvqUniform: message = "can't modify a uniform"; break;
324 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 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 }
Jamie Madill5508f392014-02-20 13:31:36 -0500438 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
Jamie Madill94bf7f22013-07-08 13:31:15 -0400489 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 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
Brendan Longeaa84062013-12-08 18:26:50 +0100536 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().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:
Jamie Madill19571812013-08-12 15:26:34 -0700634 case EvqVertexIn:
635 case EvqFragmentOut:
Jamie Madillb120eac2013-06-12 14:08:13 -0400636 if (pType.type == EbtStruct)
637 {
638 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
639 return true;
640 }
Jamie Madill10567262014-04-17 16:40:00 -0400641
642 default: break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
646 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madill075edd82013-07-08 13:30:19 -0400651bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400652{
653 if (pType.layoutQualifier.location != -1)
654 {
655 error(line, "location must only be specified for a single input or output variable", "location");
656 return true;
657 }
658
659 return false;
660}
661
Jamie Madill075edd82013-07-08 13:30:19 -0400662bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
665 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000666 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return true;
668 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000670 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671}
672
673bool TParseContext::containsSampler(TType& type)
674{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 if (IsSampler(type.getBasicType()))
676 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677
Jamie Madill98493dd2013-07-08 14:39:03 -0400678 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
679 const TFieldList& fields = type.getStruct()->fields();
680 for (unsigned int i = 0; i < fields.size(); ++i) {
681 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return true;
683 }
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687}
688
689//
690// Do size checking for an array type's size.
691//
692// Returns true if there was an error.
693//
Jamie Madill075edd82013-07-08 13:30:19 -0400694bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000697
698 if (constant == 0 || !constant->isScalarInt())
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 return true;
702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000704 if (constant->getBasicType() == EbtUInt)
705 {
706 unsigned int uintSize = constant->getUConst(0);
707 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
708 {
709 error(line, "array size too large", "");
710 size = 1;
711 return true;
712 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000714 size = static_cast<int>(uintSize);
715 }
716 else
717 {
718 size = constant->getIConst(0);
719
720 if (size <= 0)
721 {
722 error(line, "array size must be a positive integer", "");
723 size = 1;
724 return true;
725 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729}
730
731//
732// See if this qualifier can be an array.
733//
734// Returns true if there is an error.
735//
Jamie Madill075edd82013-07-08 13:30:19 -0400736bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737{
Jamie Madill19571812013-08-12 15:26:34 -0700738 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return true;
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
746//
747// See if this type can be an array.
748//
749// Returns true if there is an error.
750//
Jamie Madill075edd82013-07-08 13:30:19 -0400751bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 //
754 // Can the type be an array?
755 //
756 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000757 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return true;
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
764//
765// Do all the semantic checking for declaring an array, with and
766// without a size, and make the right changes to the symbol table.
767//
768// size == 0 means no specified size.
769//
770// Returns true if there was an error.
771//
Jamie Madill075edd82013-07-08 13:30:19 -0400772bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000774 //
775 // Don't check for reserved word use until after we know it's not in the symbol table,
776 // because reserved arrays can be redeclared.
777 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000779 bool builtIn = false;
780 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000781 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000782 if (symbol == 0 || !sameScope) {
783 if (reservedErrorCheck(line, identifier))
784 return true;
785
786 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000788 if (type.arraySize)
789 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000791 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000792 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000793 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000794 return true;
795 }
796 } else {
797 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000798 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 return true;
800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000802 variable = static_cast<TVariable*>(symbol);
803 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000804 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 return true;
806 }
807 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000808 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 return true;
810 }
811
812 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000813 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 return true;
815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 if (type.arraySize)
818 variable->getType().setArraySize(type.arraySize);
819 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 if (voidErrorCheck(line, identifier, type))
822 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000824 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825}
826
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827//
828// Enforce non-initializer type/qualifier rules.
829//
830// Returns true if there was an error.
831//
Jamie Madill075edd82013-07-08 13:30:19 -0400832bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000834 if (type.qualifier == EvqConst)
835 {
836 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000838
839 if (array)
840 {
841 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
842 }
843 else if (type.isStructureContainingArrays())
844 {
845 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
846 }
847 else
848 {
849 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
850 }
851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 return true;
853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858//
859// Do semantic checking for a variable declaration that has no initializer,
860// and update the symbol table.
861//
862// Returns true if there was an error.
863//
Jamie Madill075edd82013-07-08 13:30:19 -0400864bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000866 if (reservedErrorCheck(line, identifier))
867 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
zmo@google.comfd747b82011-04-23 01:30:07 +0000869 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000871 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000872 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000874 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 return true;
876 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 if (voidErrorCheck(line, identifier, type))
879 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
Jamie Madill075edd82013-07-08 13:30:19 -0400884bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000887 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000888 return true;
889 }
890 if (qualifier == EvqConst && paramQualifier != EvqIn) {
891 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
892 return true;
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000895 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000896 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000898 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000900 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
Jamie Madill075edd82013-07-08 13:30:19 -0400903bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000904{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000905 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000906 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
907 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000908 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 return true;
910 }
zmo@google.comf5450912011-09-09 01:37:19 +0000911 // In GLSL ES, an extension's default behavior is "disable".
912 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000913 error(line, "extension", extension.c_str(), "is disabled");
914 return true;
915 }
916 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000917 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000918 return false;
919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922}
923
Jamie Madill075edd82013-07-08 13:30:19 -0400924bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400925{
926 if (structQualifierErrorCheck(identifierLocation, publicType))
927 return true;
928
929 // check for layout qualifier issues
930 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
931
932 if (layoutQualifier.matrixPacking != EmpUnspecified)
933 {
934 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400935 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400936 }
937
938 if (layoutQualifier.blockStorage != EbsUnspecified)
939 {
940 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400941 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400942 }
943
Jamie Madill19571812013-08-12 15:26:34 -0700944 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400945 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400946 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400947 }
948
949 return false;
950}
951
Jamie Madill075edd82013-07-08 13:30:19 -0400952bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400953{
954 if (layoutQualifier.location != -1)
955 {
956 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
957 return true;
958 }
959
960 return false;
961}
962
zmo@google.com09c323a2011-08-12 18:22:25 +0000963bool TParseContext::supportsExtension(const char* extension)
964{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000965 const TExtensionBehavior& extbehavior = extensionBehavior();
966 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
967 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000968}
969
Jamie Madill5d287f52013-07-12 15:38:19 -0400970bool TParseContext::isExtensionEnabled(const char* extension) const
971{
972 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400973 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400974
975 if (iter == extbehavior.end())
976 {
977 return false;
978 }
979
980 return (iter->second == EBhEnable || iter->second == EBhRequire);
981}
982
Jamie Madill075edd82013-07-08 13:30:19 -0400983void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
984{
985 pp::SourceLocation srcLoc;
986 srcLoc.file = loc.first_file;
987 srcLoc.line = loc.first_line;
988 directiveHandler.handleExtension(srcLoc, extName, behavior);
989}
990
991void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
992{
993 pp::SourceLocation srcLoc;
994 srcLoc.file = loc.first_file;
995 srcLoc.line = loc.first_line;
996 directiveHandler.handlePragma(srcLoc, name, value);
997}
998
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999/////////////////////////////////////////////////////////////////////////////////
1000//
1001// Non-Errors.
1002//
1003/////////////////////////////////////////////////////////////////////////////////
1004
1005//
1006// Look up a function name in the symbol table, and make sure it is a function.
1007//
1008// Return the function symbol if found, otherwise 0.
1009//
Jamie Madill075edd82013-07-08 13:30:19 -04001010const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001012 // First find by unmangled name to check whether the function name has been
1013 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001014 // If a function is found, check for one with a matching argument list.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001015 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001016 if (symbol == 0 || symbol->isFunction()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001017 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001018 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019
alokp@chromium.org0a576182010-08-09 17:16:27 +00001020 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001021 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 return 0;
1023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
alokp@chromium.org0a576182010-08-09 17:16:27 +00001025 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001026 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001027 return 0;
1028 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001029
1030 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031}
1032
1033//
1034// Initializers show up in several places in the grammar. Have one set of
1035// code to handle them here.
1036//
Jamie Madill075edd82013-07-08 13:30:19 -04001037bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001042 if (variable == 0) {
1043 if (reservedErrorCheck(line, identifier))
1044 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001046 if (voidErrorCheck(line, identifier, pType))
1047 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001049 //
1050 // add variable to symbol table
1051 //
1052 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001053 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001054 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001055 return true;
1056 // don't delete variable, it's used by error recovery, and the pool
1057 // pop will take care of the memory
1058 }
1059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 //
1062 // identifier must be of type constant, a global, or a temporary
1063 //
1064 TQualifier qualifier = variable->getType().getQualifier();
1065 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001066 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001067 return true;
1068 }
1069 //
1070 // test for and propagate constant
1071 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 if (qualifier == EvqConst) {
1074 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001075 std::stringstream extraInfoStream;
1076 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1077 std::string extraInfo = extraInfoStream.str();
1078 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001079 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 return true;
1081 }
1082 if (type != initializer->getType()) {
1083 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001084 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001085 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001086 return true;
1087 }
1088 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001089 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001091 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001092 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001094 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001095 variable->shareConstPointer(constArray);
1096 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001097 std::stringstream extraInfoStream;
1098 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1099 std::string extraInfo = extraInfoStream.str();
1100 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001101 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 return true;
1103 }
1104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001106 if (qualifier != EvqConst) {
1107 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1108 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1109 if (intermNode == 0) {
1110 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1111 return true;
1112 }
1113 } else
1114 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001116 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117}
1118
1119bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1120{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001121 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 if (!aggrNode->isConstructor())
1123 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001127 // check if all the child nodes are constants so that they can be inserted into
1128 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001129 TIntermSequence &sequence = aggrNode->getSequence() ;
1130 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1131 if (!(*p)->getAsTyped()->getAsConstantUnion())
1132 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001135 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136}
1137
Jamie Madilla5efff92013-06-06 11:56:47 -04001138TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001139{
1140 TPublicType returnType = typeSpecifier;
1141 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001142 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001143
1144 if (typeSpecifier.array)
1145 {
1146 error(typeSpecifier.line, "not supported", "first-class array");
1147 recover();
1148 returnType.setArray(false);
1149 }
1150
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001151 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001152 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001153 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1154 {
1155 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1156 recover();
1157 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001158
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001159 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1160 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1161 {
1162 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1163 recover();
1164 }
1165 }
1166 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001167 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001168 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001169 {
Jamie Madill19571812013-08-12 15:26:34 -07001170 case EvqSmoothIn:
1171 case EvqSmoothOut:
1172 case EvqVertexOut:
1173 case EvqFragmentIn:
1174 case EvqCentroidOut:
1175 case EvqCentroidIn:
1176 if (typeSpecifier.type == EbtBool)
1177 {
1178 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1179 recover();
1180 }
1181 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1182 {
1183 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1184 recover();
1185 }
1186 break;
1187
1188 case EvqVertexIn:
1189 case EvqFragmentOut:
1190 case EvqFlatIn:
1191 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001192 if (typeSpecifier.type == EbtBool)
1193 {
1194 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1195 recover();
1196 }
1197 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001198
Jamie Madillb120eac2013-06-12 14:08:13 -04001199 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001200 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001201 }
1202
1203 return returnType;
1204}
1205
Jamie Madill075edd82013-07-08 13:30:19 -04001206TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001207{
1208 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1209 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1210
1211 if (identifier != "")
1212 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001213 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001214 recover();
1215
1216 // this error check can mutate the type
1217 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1218 recover();
1219
1220 TVariable* variable = 0;
1221
1222 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1223 recover();
1224
1225 if (variable && symbol)
1226 {
1227 symbol->setId(variable->getUniqueId());
1228 }
1229 }
1230
1231 return aggregate;
1232}
1233
Jamie Madill075edd82013-07-08 13:30:19 -04001234TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001235{
Jamie Madill51a53c72013-06-19 09:24:43 -04001236 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001237 recover();
1238
1239 // this error check can mutate the type
1240 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1241 recover();
1242
1243 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1244 {
1245 recover();
1246 }
1247
1248 TPublicType arrayType = publicType;
1249
1250 int size;
1251 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1252 {
1253 recover();
1254 }
1255 else
1256 {
1257 arrayType.setArray(true, size);
1258 }
1259
1260 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1261 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1262 TVariable* variable = 0;
1263
1264 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1265 recover();
1266
1267 if (variable && symbol)
1268 {
1269 symbol->setId(variable->getUniqueId());
1270 }
1271
1272 return aggregate;
1273}
1274
Jamie Madill075edd82013-07-08 13:30:19 -04001275TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001276{
Jamie Madilla5efff92013-06-06 11:56:47 -04001277 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001278 recover();
1279
1280 TIntermNode* intermNode;
1281 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1282 {
1283 //
1284 // Build intermediate representation
1285 //
1286 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1287 }
1288 else
1289 {
1290 recover();
1291 return NULL;
1292 }
1293}
1294
Jamie Madill075edd82013-07-08 13:30:19 -04001295TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001296{
1297 if (publicType.type == EbtInvariant && !identifierSymbol)
1298 {
1299 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1300 recover();
1301 }
1302
1303 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1304 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1305
1306 if (structQualifierErrorCheck(identifierLocation, publicType))
1307 recover();
1308
Jamie Madill0bd18df2013-06-20 11:55:52 -04001309 if (locationDeclaratorListCheck(identifierLocation, publicType))
1310 recover();
1311
Jamie Madill502d66f2013-06-20 11:55:52 -04001312 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1313 recover();
1314
1315 TVariable* variable = 0;
1316 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1317 recover();
1318 if (symbol && variable)
1319 symbol->setId(variable->getUniqueId());
1320
1321 return intermAggregate;
1322}
1323
Jamie Madill075edd82013-07-08 13:30:19 -04001324TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001325{
1326 if (structQualifierErrorCheck(identifierLocation, publicType))
1327 recover();
1328
Jamie Madill0bd18df2013-06-20 11:55:52 -04001329 if (locationDeclaratorListCheck(identifierLocation, publicType))
1330 recover();
1331
Jamie Madill502d66f2013-06-20 11:55:52 -04001332 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1333 recover();
1334
1335 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1336 {
1337 recover();
1338 }
1339 else if (indexExpression)
1340 {
1341 int size;
1342 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1343 recover();
1344 TPublicType arrayType(publicType);
1345 arrayType.setArray(true, size);
1346 TVariable* variable = NULL;
1347 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1348 recover();
1349 TType type = TType(arrayType);
1350 type.setArraySize(size);
1351
1352 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1353 }
1354 else
1355 {
1356 TPublicType arrayType(publicType);
1357 arrayType.setArray(true);
1358 TVariable* variable = NULL;
1359 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1360 recover();
1361 }
1362
1363 return NULL;
1364}
1365
Jamie Madill075edd82013-07-08 13:30:19 -04001366TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001367{
1368 if (structQualifierErrorCheck(identifierLocation, publicType))
1369 recover();
1370
Jamie Madill0bd18df2013-06-20 11:55:52 -04001371 if (locationDeclaratorListCheck(identifierLocation, publicType))
1372 recover();
1373
Jamie Madill502d66f2013-06-20 11:55:52 -04001374 TIntermNode* intermNode;
1375 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1376 {
1377 //
1378 // build the intermediate representation
1379 //
1380 if (intermNode)
1381 {
1382 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1383 }
1384 else
1385 {
1386 return declaratorList;
1387 }
1388 }
1389 else
1390 {
1391 recover();
1392 return NULL;
1393 }
1394}
1395
Jamie Madilla295edf2013-06-06 11:56:48 -04001396void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1397{
1398 if (typeQualifier.qualifier != EvqUniform)
1399 {
1400 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1401 recover();
1402 return;
1403 }
1404
1405 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1406 ASSERT(!layoutQualifier.isEmpty());
1407
1408 if (shaderVersion < 300)
1409 {
1410 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1411 recover();
1412 return;
1413 }
1414
1415 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1416 {
1417 recover();
1418 return;
1419 }
1420
Jamie Madill099c0f32013-06-20 11:55:52 -04001421 if (layoutQualifier.matrixPacking != EmpUnspecified)
1422 {
1423 defaultMatrixPacking = layoutQualifier.matrixPacking;
1424 }
1425
Geoff Langc6856732014-02-11 09:38:55 -05001426 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001427 {
1428 defaultBlockStorage = layoutQualifier.blockStorage;
1429 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001430}
1431
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001432TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1433{
1434 TOperator op = EOpNull;
1435 if (publicType.userDef)
1436 {
1437 op = EOpConstructStruct;
1438 }
1439 else
1440 {
1441 switch (publicType.type)
1442 {
1443 case EbtFloat:
1444 if (publicType.isMatrix())
1445 {
1446 // TODO: non-square matrices
1447 switch(publicType.getCols())
1448 {
Jamie Madill28b97422013-07-08 14:01:38 -04001449 case 2: op = EOpConstructMat2; break;
1450 case 3: op = EOpConstructMat3; break;
1451 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001452 }
1453 }
1454 else
1455 {
1456 switch(publicType.getNominalSize())
1457 {
Jamie Madill28b97422013-07-08 14:01:38 -04001458 case 1: op = EOpConstructFloat; break;
1459 case 2: op = EOpConstructVec2; break;
1460 case 3: op = EOpConstructVec3; break;
1461 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001462 }
1463 }
1464 break;
1465
1466 case EbtInt:
1467 switch(publicType.getNominalSize())
1468 {
Jamie Madill28b97422013-07-08 14:01:38 -04001469 case 1: op = EOpConstructInt; break;
1470 case 2: op = EOpConstructIVec2; break;
1471 case 3: op = EOpConstructIVec3; break;
1472 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001473 }
1474 break;
1475
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001476 case EbtUInt:
1477 switch(publicType.getNominalSize())
1478 {
Jamie Madill28b97422013-07-08 14:01:38 -04001479 case 1: op = EOpConstructUInt; break;
1480 case 2: op = EOpConstructUVec2; break;
1481 case 3: op = EOpConstructUVec3; break;
1482 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001483 }
1484 break;
1485
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001486 case EbtBool:
1487 switch(publicType.getNominalSize())
1488 {
Jamie Madill28b97422013-07-08 14:01:38 -04001489 case 1: op = EOpConstructBool; break;
1490 case 2: op = EOpConstructBVec2; break;
1491 case 3: op = EOpConstructBVec3; break;
1492 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001493 }
1494 break;
1495
1496 default: break;
1497 }
1498
1499 if (op == EOpNull)
1500 {
1501 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1502 recover();
1503 publicType.type = EbtFloat;
1504 op = EOpConstructFloat;
1505 }
1506 }
1507
1508 TString tempString;
1509 TType type(publicType);
1510 return new TFunction(&tempString, type, op);
1511}
1512
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513// This function is used to test for the correctness of the parameters passed to various constructor functions
1514// and also convert them to the right datatype if it is allowed and required.
1515//
1516// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1517//
Jamie Madill075edd82013-07-08 13:30:19 -04001518TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001520 if (node == 0)
1521 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001523 TIntermAggregate* aggrNode = node->getAsAggregate();
1524
Jamie Madill98493dd2013-07-08 14:39:03 -04001525 TFieldList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001526 if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001527 memberTypes = type->getStruct()->fields().begin();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001528
1529 TType elementType = *type;
1530 if (type->isArray())
1531 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001533 bool singleArg;
1534 if (aggrNode) {
1535 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1536 singleArg = true;
1537 else
1538 singleArg = false;
1539 } else
1540 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001541
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001542 TIntermTyped *newNode;
1543 if (singleArg) {
1544 // If structure constructor or array constructor is being called
1545 // for only one parameter inside the structure, we need to call constructStruct function once.
1546 if (type->isArray())
1547 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1548 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001549 newNode = constructStruct(node, (*memberTypes)->type(), 1, node->getLine(), false);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001550 else
1551 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001553 if (newNode && newNode->getAsAggregate()) {
1554 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1555 if (constConstructor)
1556 return constConstructor;
1557 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001559 return newNode;
1560 }
1561
1562 //
1563 // Handle list of arguments.
1564 //
1565 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1566 // if the structure constructor contains more than one parameter, then construct
1567 // each parameter
1568
1569 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1570
1571 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1572 // to the right type if possible (and allowed).
1573 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1574
1575 for (TIntermSequence::iterator p = sequenceVector.begin();
1576 p != sequenceVector.end(); p++, paramCount++) {
1577 if (type->isArray())
1578 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1579 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001580 newNode = constructStruct(*p, (memberTypes[paramCount])->type(), paramCount+1, node->getLine(), true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001581 else
1582 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1583
1584 if (newNode) {
1585 *p = newNode;
1586 }
1587 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001589 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1590 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1591 if (constConstructor)
1592 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001594 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595}
1596
1597TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1598{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001599 bool canBeFolded = areAllChildConst(aggrNode);
1600 aggrNode->setType(type);
1601 if (canBeFolded) {
1602 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001603 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001604 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001605 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001606 }
1607 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001608 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001609 }
1610 if (returnVal)
1611 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001613 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001616 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001617}
1618
1619// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1620// for the parameter to the constructor (passed to this function). Essentially, it converts
1621// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1622// float, then float is converted to int.
1623//
1624// Returns 0 for an error or the constructed node.
1625//
Jamie Madill075edd82013-07-08 13:30:19 -04001626TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001627{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001628 TIntermTyped* newNode;
1629 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001631 //
1632 // First, convert types as needed.
1633 //
1634 switch (op) {
1635 case EOpConstructVec2:
1636 case EOpConstructVec3:
1637 case EOpConstructVec4:
1638 case EOpConstructMat2:
1639 case EOpConstructMat3:
1640 case EOpConstructMat4:
1641 case EOpConstructFloat:
1642 basicOp = EOpConstructFloat;
1643 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001645 case EOpConstructIVec2:
1646 case EOpConstructIVec3:
1647 case EOpConstructIVec4:
1648 case EOpConstructInt:
1649 basicOp = EOpConstructInt;
1650 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001651
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001652 case EOpConstructUVec2:
1653 case EOpConstructUVec3:
1654 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001655 case EOpConstructUInt:
1656 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001657 break;
1658
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001659 case EOpConstructBVec2:
1660 case EOpConstructBVec3:
1661 case EOpConstructBVec4:
1662 case EOpConstructBool:
1663 basicOp = EOpConstructBool;
1664 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001666 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001667 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001668 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001670 return 0;
1671 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001672 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001673 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001674 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001675 return 0;
1676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001678 //
1679 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1680 //
1681
1682 // Otherwise, skip out early.
1683 if (subset || (newNode != node && newNode->getType() == *type))
1684 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001686 // setAggregateOperator will insert a new node for the constructor, as needed.
1687 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688}
1689
1690// This function tests for the type of the parameters to the structures constructors. Raises
1691// an error message if the expected type does not match the parameter passed to the constructor.
1692//
1693// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1694//
Jamie Madill075edd82013-07-08 13:30:19 -04001695TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001697 if (*type == node->getAsTyped()->getType()) {
1698 if (subset)
1699 return node->getAsTyped();
1700 else
1701 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1702 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001703 std::stringstream extraInfoStream;
1704 extraInfoStream << "cannot convert parameter " << paramCount
1705 << " from '" << node->getAsTyped()->getType().getBasicString()
1706 << "' to '" << type->getBasicString() << "'";
1707 std::string extraInfo = extraInfoStream.str();
1708 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001709 recover();
1710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001712 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001713}
1714
1715//
1716// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1717// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1718// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1719// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1720// a constant matrix.
1721//
Jamie Madill075edd82013-07-08 13:30:19 -04001722TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 TIntermTyped* typedNode;
1725 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001727 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001728 if (tempConstantNode) {
1729 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001731 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001732 return node;
1733 }
1734 } 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 +00001735 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001738 return 0;
1739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001741 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001743 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001744 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001745 std::stringstream extraInfoStream;
1746 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1747 std::string extraInfo = extraInfoStream.str();
1748 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001749 recover();
1750 fields.offsets[i] = 0;
1751 }
1752
1753 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 }
1756 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1757 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758}
1759
1760//
1761// This function returns the column being accessed from a constant matrix. The values are retrieved from
1762// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1763// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1764// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1765//
Jamie Madill075edd82013-07-08 13:30:19 -04001766TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001768 TIntermTyped* typedNode;
1769 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001771 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001772 std::stringstream extraInfoStream;
1773 extraInfoStream << "matrix field selection out of range '" << index << "'";
1774 std::string extraInfo = extraInfoStream.str();
1775 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001776 recover();
1777 index = 0;
1778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001780 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001781 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001782 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001783 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1784 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001785 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001788 return 0;
1789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001791 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792}
1793
1794
1795//
1796// This function returns an element of an array accessed from a constant array. The values are retrieved from
1797// the symbol table and parse-tree is built for the type of the element. The input
1798// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1799// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1800//
Jamie Madill075edd82013-07-08 13:30:19 -04001801TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001803 TIntermTyped* typedNode;
1804 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1805 TType arrayElementType = node->getType();
1806 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001808 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001809 std::stringstream extraInfoStream;
1810 extraInfoStream << "array field selection out of range '" << index << "'";
1811 std::string extraInfo = extraInfoStream.str();
1812 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001813 recover();
1814 index = 0;
1815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001817 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001818 size_t arrayElementSize = arrayElementType.getObjectSize();
1819 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1820 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001822 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001825 return 0;
1826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001828 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829}
1830
1831
1832//
1833// This function returns the value of a particular field inside a constant structure from the symbol table.
1834// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1835// function and returns the parse-tree with the values of the embedded/nested struct.
1836//
Jamie Madill075edd82013-07-08 13:30:19 -04001837TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838{
Jamie Madill98493dd2013-07-08 14:39:03 -04001839 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001840 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
Jamie Madill98493dd2013-07-08 14:39:03 -04001842 for (size_t index = 0; index < fields.size(); ++index) {
1843 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 break;
1845 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001846 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001847 }
1848 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849
Jamie Madill94bf7f22013-07-08 13:31:15 -04001850 TIntermTyped *typedNode;
1851 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001852 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001853 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001855 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1856 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001857 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001858 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001859
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001860 return 0;
1861 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001863 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864}
1865
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001866//
1867// Interface/uniform blocks
1868//
Jamie Madill98493dd2013-07-08 14:39:03 -04001869TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1870 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001871{
1872 if (reservedErrorCheck(nameLine, blockName))
1873 recover();
1874
1875 if (typeQualifier.qualifier != EvqUniform)
1876 {
1877 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1878 recover();
1879 }
1880
Jamie Madill099c0f32013-06-20 11:55:52 -04001881 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1882 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001883 {
1884 recover();
1885 }
1886
Jamie Madill099c0f32013-06-20 11:55:52 -04001887 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1888 {
1889 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1890 }
1891
Jamie Madill1566ef72013-06-20 11:55:54 -04001892 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1893 {
1894 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1895 }
1896
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001897 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1898 if (!symbolTable.declare(*blockNameSymbol)) {
1899 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1900 recover();
1901 }
1902
Jamie Madill98493dd2013-07-08 14:39:03 -04001903 // check for sampler types and apply layout qualifiers
1904 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1905 TField* field = (*fieldList)[memberIndex];
1906 TType* fieldType = field->type();
1907 if (IsSampler(fieldType->getBasicType())) {
1908 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001909 recover();
1910 }
1911
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001913 switch (qualifier)
1914 {
1915 case EvqGlobal:
1916 case EvqUniform:
1917 break;
1918 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001919 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001920 recover();
1921 break;
1922 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001923
1924 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001925 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1926 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001927 {
1928 recover();
1929 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001930
Jamie Madill98493dd2013-07-08 14:39:03 -04001931 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001932 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001933 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001934 recover();
1935 }
1936
Jamie Madill98493dd2013-07-08 14:39:03 -04001937 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001938 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001939 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001940 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001941 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001942 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001943 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001944 recover();
1945 }
1946
Jamie Madill98493dd2013-07-08 14:39:03 -04001947 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001948 }
1949
Jamie Madill98493dd2013-07-08 14:39:03 -04001950 // add array index
1951 int arraySize = 0;
1952 if (arrayIndex != NULL)
1953 {
1954 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1955 recover();
1956 }
1957
1958 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1959 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001960
1961 TString symbolName = "";
1962 int symbolId = 0;
1963
Jamie Madill98493dd2013-07-08 14:39:03 -04001964 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001965 {
1966 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001967 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1968 {
1969 TField* field = (*fieldList)[memberIndex];
1970 TType* fieldType = field->type();
1971
1972 // set parent pointer of the field variable
1973 fieldType->setInterfaceBlock(interfaceBlock);
1974
1975 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1976 fieldVariable->setQualifier(typeQualifier.qualifier);
1977
1978 if (!symbolTable.declare(*fieldVariable)) {
1979 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001980 recover();
1981 }
1982 }
1983 }
1984 else
1985 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001986 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001987 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001988 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001989
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001990 if (!symbolTable.declare(*instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001991 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001992 recover();
1993 }
1994
1995 symbolId = instanceTypeDef->getUniqueId();
1996 symbolName = instanceTypeDef->getName();
1997 }
1998
Jamie Madill98493dd2013-07-08 14:39:03 -04001999 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002000 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002001
2002 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002003 return aggregate;
2004}
2005
Jamie Madill075edd82013-07-08 13:30:19 -04002006bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002007{
2008 ++structNestingLevel;
2009
2010 // Embedded structure definitions are not supported per GLSL ES spec.
2011 // They aren't allowed in GLSL either, but we need to detect this here
2012 // so we don't rely on the GLSL compiler to catch it.
2013 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002014 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002015 return true;
2016 }
2017
2018 return false;
2019}
2020
2021void TParseContext::exitStructDeclaration()
2022{
2023 --structNestingLevel;
2024}
2025
2026namespace {
2027
2028const int kWebGLMaxStructNesting = 4;
2029
2030} // namespace
2031
Jamie Madill98493dd2013-07-08 14:39:03 -04002032bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002033{
Jamie Madill5508f392014-02-20 13:31:36 -05002034 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002035 return false;
2036 }
2037
Jamie Madill98493dd2013-07-08 14:39:03 -04002038 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002039 return false;
2040 }
2041
2042 // We're already inside a structure definition at this point, so add
2043 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002044 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002045 std::stringstream reasonStream;
2046 reasonStream << "Reference of struct type "
2047 << field.type()->getStruct()->name().c_str()
2048 << " exceeds maximum allowed nesting level of "
2049 << kWebGLMaxStructNesting;
2050 std::string reason = reasonStream.str();
2051 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002052 return true;
2053 }
2054
2055 return false;
2056}
2057
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002058//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002059// Parse an array index expression
2060//
Jamie Madill075edd82013-07-08 13:30:19 -04002061TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002062{
2063 TIntermTyped *indexedExpression = NULL;
2064
2065 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2066 {
2067 if (baseExpression->getAsSymbolNode())
2068 {
2069 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2070 }
2071 else
2072 {
2073 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2074 }
2075 recover();
2076 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002077
Jamie Madill7164cf42013-07-08 13:30:59 -04002078 if (indexExpression->getQualifier() == EvqConst)
2079 {
2080 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2081 if (index < 0)
2082 {
2083 std::stringstream infoStream;
2084 infoStream << index;
2085 std::string info = infoStream.str();
2086 error(location, "negative index", info.c_str());
2087 recover();
2088 index = 0;
2089 }
2090 if (baseExpression->getType().getQualifier() == EvqConst)
2091 {
2092 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002093 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002094 // constant folding for arrays
2095 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002096 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002097 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002098 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002099 // constant folding for vectors
2100 TVectorFields fields;
2101 fields.num = 1;
2102 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2103 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2104 }
2105 else if (baseExpression->isMatrix())
2106 {
2107 // constant folding for matrices
2108 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002109 }
2110 }
2111 else
2112 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002113 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002114 {
Jamie Madill18464b52013-07-08 14:01:55 -04002115 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002116 {
2117 std::stringstream extraInfoStream;
2118 extraInfoStream << "array index out of range '" << index << "'";
2119 std::string extraInfo = extraInfoStream.str();
2120 error(location, "", "[", extraInfo.c_str());
2121 recover();
2122 index = baseExpression->getType().getArraySize() - 1;
2123 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002124 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2125 {
2126 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2127 recover();
2128 index = 0;
2129 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002130 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002131 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002132 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002133 std::stringstream extraInfoStream;
2134 extraInfoStream << "field selection out of range '" << index << "'";
2135 std::string extraInfo = extraInfoStream.str();
2136 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002137 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002138 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002139 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002140
Jamie Madill7164cf42013-07-08 13:30:59 -04002141 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2142 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002143 }
2144 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002145 else
2146 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002147 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002148 {
Jamie Madill19571812013-08-12 15:26:34 -07002149 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002150 recover();
2151 }
Jamie Madill19571812013-08-12 15:26:34 -07002152 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002153 {
Jamie Madill19571812013-08-12 15:26:34 -07002154 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002155 recover();
2156 }
2157
2158 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2159 }
2160
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002161 if (indexedExpression == 0)
2162 {
2163 ConstantUnion *unionArray = new ConstantUnion[1];
2164 unionArray->setFConst(0.0f);
2165 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2166 }
2167 else if (baseExpression->isArray())
2168 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002169 const TType &baseType = baseExpression->getType();
2170 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002171 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002172 TType copyOfType(baseType.getStruct());
2173 indexedExpression->setType(copyOfType);
2174 }
2175 else if (baseType.isInterfaceBlock())
2176 {
2177 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002178 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002179 }
2180 else
2181 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002182 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002183 }
2184
2185 if (baseExpression->getType().getQualifier() == EvqConst)
2186 {
2187 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2188 }
2189 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002190 else if (baseExpression->isMatrix())
2191 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002192 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2193 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002194 }
2195 else if (baseExpression->isVector())
2196 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002197 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2198 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002199 }
2200 else
2201 {
2202 indexedExpression->setType(baseExpression->getType());
2203 }
2204
2205 return indexedExpression;
2206}
2207
Jamie Madill075edd82013-07-08 13:30:19 -04002208TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002209{
2210 TIntermTyped *indexedExpression = NULL;
2211
2212 if (baseExpression->isArray())
2213 {
2214 error(fieldLocation, "cannot apply dot operator to an array", ".");
2215 recover();
2216 }
2217
2218 if (baseExpression->isVector())
2219 {
2220 TVectorFields fields;
2221 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2222 {
2223 fields.num = 1;
2224 fields.offsets[0] = 0;
2225 recover();
2226 }
2227
2228 if (baseExpression->getType().getQualifier() == EvqConst)
2229 {
2230 // constant folding for vector fields
2231 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2232 if (indexedExpression == 0)
2233 {
2234 recover();
2235 indexedExpression = baseExpression;
2236 }
2237 else
2238 {
2239 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2240 }
2241 }
2242 else
2243 {
2244 TString vectorString = fieldString;
2245 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2246 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2247 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2248 }
2249 }
2250 else if (baseExpression->isMatrix())
2251 {
2252 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002253 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002254 {
2255 fields.wholeRow = false;
2256 fields.wholeCol = false;
2257 fields.row = 0;
2258 fields.col = 0;
2259 recover();
2260 }
2261
2262 if (fields.wholeRow || fields.wholeCol)
2263 {
2264 error(dotLocation, " non-scalar fields not implemented yet", ".");
2265 recover();
2266 ConstantUnion *unionArray = new ConstantUnion[1];
2267 unionArray->setIConst(0);
2268 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2269 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002270 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002271 }
2272 else
2273 {
2274 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002275 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002276 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2277 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2278 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2279 }
2280 }
2281 else if (baseExpression->getBasicType() == EbtStruct)
2282 {
2283 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002284 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2285 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002286 {
2287 error(dotLocation, "structure has no fields", "Internal Error");
2288 recover();
2289 indexedExpression = baseExpression;
2290 }
2291 else
2292 {
2293 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002294 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002295 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002296 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002297 {
2298 fieldFound = true;
2299 break;
2300 }
2301 }
2302 if (fieldFound)
2303 {
2304 if (baseExpression->getType().getQualifier() == EvqConst)
2305 {
2306 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2307 if (indexedExpression == 0)
2308 {
2309 recover();
2310 indexedExpression = baseExpression;
2311 }
2312 else
2313 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002314 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002315 // change the qualifier of the return type, not of the structure field
2316 // as the structure definition is shared between various structures.
2317 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2318 }
2319 }
2320 else
2321 {
2322 ConstantUnion *unionArray = new ConstantUnion[1];
2323 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002324 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002325 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002326 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002327 }
2328 }
2329 else
2330 {
2331 error(dotLocation, " no such field in structure", fieldString.c_str());
2332 recover();
2333 indexedExpression = baseExpression;
2334 }
2335 }
2336 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002337 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002338 {
2339 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002340 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2341 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002342 {
2343 error(dotLocation, "interface block has no fields", "Internal Error");
2344 recover();
2345 indexedExpression = baseExpression;
2346 }
2347 else
2348 {
2349 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002350 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002351 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002352 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002353 {
2354 fieldFound = true;
2355 break;
2356 }
2357 }
2358 if (fieldFound)
2359 {
2360 ConstantUnion *unionArray = new ConstantUnion[1];
2361 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002362 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002363 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002364 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002365 }
2366 else
2367 {
2368 error(dotLocation, " no such field in interface block", fieldString.c_str());
2369 recover();
2370 indexedExpression = baseExpression;
2371 }
2372 }
2373 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002374 else
2375 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002376 if (shaderVersion < 300)
2377 {
2378 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2379 }
2380 else
2381 {
2382 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2383 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002384 recover();
2385 indexedExpression = baseExpression;
2386 }
2387
2388 return indexedExpression;
2389}
2390
Jamie Madill075edd82013-07-08 13:30:19 -04002391TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002392{
Jamie Madilla5efff92013-06-06 11:56:47 -04002393 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002394
Jamie Madilla5efff92013-06-06 11:56:47 -04002395 qualifier.location = -1;
2396 qualifier.matrixPacking = EmpUnspecified;
2397 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002398
2399 if (qualifierType == "shared")
2400 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002401 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002402 }
2403 else if (qualifierType == "packed")
2404 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002405 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002406 }
2407 else if (qualifierType == "std140")
2408 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002409 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002410 }
2411 else if (qualifierType == "row_major")
2412 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002413 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002414 }
2415 else if (qualifierType == "column_major")
2416 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002417 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002418 }
2419 else if (qualifierType == "location")
2420 {
2421 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2422 recover();
2423 }
2424 else
2425 {
2426 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2427 recover();
2428 }
2429
Jamie Madilla5efff92013-06-06 11:56:47 -04002430 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002431}
2432
Jamie Madill075edd82013-07-08 13:30:19 -04002433TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002434{
Jamie Madilla5efff92013-06-06 11:56:47 -04002435 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002436
Jamie Madilla5efff92013-06-06 11:56:47 -04002437 qualifier.location = -1;
2438 qualifier.matrixPacking = EmpUnspecified;
2439 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002440
2441 if (qualifierType != "location")
2442 {
2443 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2444 recover();
2445 }
2446 else
2447 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002448 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002449 if (intValue < 0)
2450 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002451 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002452 recover();
2453 }
2454 else
2455 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002456 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002457 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002458 }
2459
Jamie Madilla5efff92013-06-06 11:56:47 -04002460 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002461}
2462
Jamie Madilla5efff92013-06-06 11:56:47 -04002463TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002464{
Jamie Madilla5efff92013-06-06 11:56:47 -04002465 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002466
Jamie Madilla5efff92013-06-06 11:56:47 -04002467 if (rightQualifier.location != -1)
2468 {
2469 joinedQualifier.location = rightQualifier.location;
2470 }
2471 if (rightQualifier.matrixPacking != EmpUnspecified)
2472 {
2473 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2474 }
2475 if (rightQualifier.blockStorage != EbsUnspecified)
2476 {
2477 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2478 }
2479
2480 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002481}
2482
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002483TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2484 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2485{
2486 TQualifier mergedQualifier = EvqSmoothIn;
2487
Jamie Madill19571812013-08-12 15:26:34 -07002488 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002489 if (interpolationQualifier == EvqSmooth)
2490 mergedQualifier = EvqSmoothIn;
2491 else if (interpolationQualifier == EvqFlat)
2492 mergedQualifier = EvqFlatIn;
2493 else UNREACHABLE();
2494 }
2495 else if (storageQualifier == EvqCentroidIn) {
2496 if (interpolationQualifier == EvqSmooth)
2497 mergedQualifier = EvqCentroidIn;
2498 else if (interpolationQualifier == EvqFlat)
2499 mergedQualifier = EvqFlatIn;
2500 else UNREACHABLE();
2501 }
Jamie Madill19571812013-08-12 15:26:34 -07002502 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002503 if (interpolationQualifier == EvqSmooth)
2504 mergedQualifier = EvqSmoothOut;
2505 else if (interpolationQualifier == EvqFlat)
2506 mergedQualifier = EvqFlatOut;
2507 else UNREACHABLE();
2508 }
2509 else if (storageQualifier == EvqCentroidOut) {
2510 if (interpolationQualifier == EvqSmooth)
2511 mergedQualifier = EvqCentroidOut;
2512 else if (interpolationQualifier == EvqFlat)
2513 mergedQualifier = EvqFlatOut;
2514 else UNREACHABLE();
2515 }
2516 else {
2517 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2518 recover();
2519
2520 mergedQualifier = storageQualifier;
2521 }
2522
2523 TPublicType type;
2524 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2525 return type;
2526}
2527
Jamie Madill98493dd2013-07-08 14:39:03 -04002528TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002529{
Jamie Madill98493dd2013-07-08 14:39:03 -04002530 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002531 recover();
2532 }
2533
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002535 //
2536 // Careful not to replace already known aspects of type, like array-ness
2537 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002538 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002539 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002540 type->setPrimarySize(typeSpecifier.primarySize);
2541 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002542 type->setPrecision(typeSpecifier.precision);
2543 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002544 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002545
2546 // don't allow arrays of arrays
2547 if (type->isArray()) {
2548 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2549 recover();
2550 }
2551 if (typeSpecifier.array)
2552 type->setArraySize(typeSpecifier.arraySize);
2553 if (typeSpecifier.userDef) {
2554 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002555 }
2556
Jamie Madill98493dd2013-07-08 14:39:03 -04002557 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002558 recover();
2559 }
2560 }
2561
Jamie Madill98493dd2013-07-08 14:39:03 -04002562 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002563}
2564
Jamie Madill98493dd2013-07-08 14:39:03 -04002565TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002566{
Jamie Madill98493dd2013-07-08 14:39:03 -04002567 TStructure* structure = new TStructure(structName, fieldList);
2568 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002569
Jamie Madill98493dd2013-07-08 14:39:03 -04002570 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002571 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002572 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002573 {
2574 recover();
2575 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002576 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002577 if (!symbolTable.declare(*userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002579 recover();
2580 }
2581 }
2582
2583 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002584 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002585 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002586 const TField &field = *(*fieldList)[typeListIndex];
2587 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002588 switch (qualifier)
2589 {
2590 case EvqGlobal:
2591 case EvqTemporary:
2592 break;
2593 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002594 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002595 recover();
2596 break;
2597 }
2598 }
2599
2600 TPublicType publicType;
2601 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002602 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002603 exitStructDeclaration();
2604
2605 return publicType;
2606}
2607
2608//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002609// Parse an array of strings using yyparse.
2610//
2611// Returns 0 for success.
2612//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002613int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002614 TParseContext* context) {
2615 if ((count == 0) || (string == NULL))
2616 return 1;
2617
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002618 if (glslang_initialize(context))
2619 return 1;
2620
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002621 int error = glslang_scan(count, string, length, context);
2622 if (!error)
2623 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002624
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002625 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002626
alokp@chromium.org6b495712012-06-29 00:06:58 +00002627 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002628}
2629
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002631