blob: 52aced6b7cca000dfdb28ad4e40651bc6d1c069a [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 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
Nicolas Capens906744a2014-06-06 15:18:07 -0400704 unsigned int unsignedSize = 0;
705
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000706 if (constant->getBasicType() == EbtUInt)
707 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400708 unsignedSize = constant->getUConst(0);
709 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000710 }
711 else
712 {
713 size = constant->getIConst(0);
714
Nicolas Capens906744a2014-06-06 15:18:07 -0400715 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000716 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400717 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000718 size = 1;
719 return true;
720 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400721
722 unsignedSize = static_cast<unsigned int>(size);
723 }
724
725 if (size == 0)
726 {
727 error(line, "array size must be greater than zero", "");
728 size = 1;
729 return true;
730 }
731
732 // The size of arrays is restricted here to prevent issues further down the
733 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
734 // 4096 registers so this should be reasonable even for aggressively optimizable code.
735 const unsigned int sizeLimit = 65536;
736
737 if (unsignedSize > sizeLimit)
738 {
739 error(line, "array size too large", "");
740 size = 1;
741 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
747//
748// See if this qualifier can be an array.
749//
750// Returns true if there is an error.
751//
Jamie Madill075edd82013-07-08 13:30:19 -0400752bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
Jamie Madill19571812013-08-12 15:26:34 -0700754 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000755 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 return true;
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
762//
763// See if this type can be an array.
764//
765// Returns true if there is an error.
766//
Jamie Madill075edd82013-07-08 13:30:19 -0400767bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 //
770 // Can the type be an array?
771 //
772 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000773 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000774 return true;
775 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
780//
781// Do all the semantic checking for declaring an array, with and
782// without a size, and make the right changes to the symbol table.
783//
784// size == 0 means no specified size.
785//
786// Returns true if there was an error.
787//
Jamie Madill075edd82013-07-08 13:30:19 -0400788bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 //
791 // Don't check for reserved word use until after we know it's not in the symbol table,
792 // because reserved arrays can be redeclared.
793 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 bool builtIn = false;
796 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000797 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 if (symbol == 0 || !sameScope) {
799 if (reservedErrorCheck(line, identifier))
800 return true;
801
802 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 if (type.arraySize)
805 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000807 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000808 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000809 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 return true;
811 }
812 } else {
813 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000814 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 return true;
816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 variable = static_cast<TVariable*>(symbol);
819 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000820 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 return true;
822 }
823 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000824 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000825 return true;
826 }
827
828 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000829 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return true;
831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 if (type.arraySize)
834 variable->getType().setArraySize(type.arraySize);
835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 if (voidErrorCheck(line, identifier, type))
838 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841}
842
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843//
844// Enforce non-initializer type/qualifier rules.
845//
846// Returns true if there was an error.
847//
Jamie Madill075edd82013-07-08 13:30:19 -0400848bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000850 if (type.qualifier == EvqConst)
851 {
852 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000854
855 if (array)
856 {
857 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
858 }
859 else if (type.isStructureContainingArrays())
860 {
861 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
862 }
863 else
864 {
865 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
866 }
867
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 return true;
869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872}
873
874//
875// Do semantic checking for a variable declaration that has no initializer,
876// and update the symbol table.
877//
878// Returns true if there was an error.
879//
Jamie Madill075edd82013-07-08 13:30:19 -0400880bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000882 if (reservedErrorCheck(line, identifier))
883 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884
zmo@google.comfd747b82011-04-23 01:30:07 +0000885 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000887 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000888 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000890 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000891 return true;
892 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 if (voidErrorCheck(line, identifier, type))
895 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
Jamie Madill075edd82013-07-08 13:30:19 -0400900bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000903 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 return true;
905 }
906 if (qualifier == EvqConst && paramQualifier != EvqIn) {
907 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
908 return true;
909 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000912 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000914 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
Jamie Madill075edd82013-07-08 13:30:19 -0400919bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000920{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000921 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000922 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
923 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000924 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 return true;
926 }
zmo@google.comf5450912011-09-09 01:37:19 +0000927 // In GLSL ES, an extension's default behavior is "disable".
928 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000929 error(line, "extension", extension.c_str(), "is disabled");
930 return true;
931 }
932 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000933 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000934 return false;
935 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000937 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
Jamie Madill075edd82013-07-08 13:30:19 -0400940bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400941{
942 if (structQualifierErrorCheck(identifierLocation, publicType))
943 return true;
944
945 // check for layout qualifier issues
946 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
947
948 if (layoutQualifier.matrixPacking != EmpUnspecified)
949 {
950 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400951 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400952 }
953
954 if (layoutQualifier.blockStorage != EbsUnspecified)
955 {
956 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400957 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400958 }
959
Jamie Madill19571812013-08-12 15:26:34 -0700960 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400961 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400962 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400963 }
964
965 return false;
966}
967
Jamie Madill075edd82013-07-08 13:30:19 -0400968bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400969{
970 if (layoutQualifier.location != -1)
971 {
972 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
973 return true;
974 }
975
976 return false;
977}
978
zmo@google.com09c323a2011-08-12 18:22:25 +0000979bool TParseContext::supportsExtension(const char* extension)
980{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000981 const TExtensionBehavior& extbehavior = extensionBehavior();
982 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
983 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000984}
985
Jamie Madill5d287f52013-07-12 15:38:19 -0400986bool TParseContext::isExtensionEnabled(const char* extension) const
987{
988 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400989 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400990
991 if (iter == extbehavior.end())
992 {
993 return false;
994 }
995
996 return (iter->second == EBhEnable || iter->second == EBhRequire);
997}
998
Jamie Madill075edd82013-07-08 13:30:19 -0400999void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1000{
1001 pp::SourceLocation srcLoc;
1002 srcLoc.file = loc.first_file;
1003 srcLoc.line = loc.first_line;
1004 directiveHandler.handleExtension(srcLoc, extName, behavior);
1005}
1006
1007void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
1008{
1009 pp::SourceLocation srcLoc;
1010 srcLoc.file = loc.first_file;
1011 srcLoc.line = loc.first_line;
1012 directiveHandler.handlePragma(srcLoc, name, value);
1013}
1014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015/////////////////////////////////////////////////////////////////////////////////
1016//
1017// Non-Errors.
1018//
1019/////////////////////////////////////////////////////////////////////////////////
1020
1021//
1022// Look up a function name in the symbol table, and make sure it is a function.
1023//
1024// Return the function symbol if found, otherwise 0.
1025//
Jamie Madill075edd82013-07-08 13:30:19 -04001026const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001028 // First find by unmangled name to check whether the function name has been
1029 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001030 // If a function is found, check for one with a matching argument list.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001031 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001032 if (symbol == 0 || symbol->isFunction()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001033 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
alokp@chromium.org0a576182010-08-09 17:16:27 +00001036 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001037 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 return 0;
1039 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040
alokp@chromium.org0a576182010-08-09 17:16:27 +00001041 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001042 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001043 return 0;
1044 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001045
1046 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047}
1048
1049//
1050// Initializers show up in several places in the grammar. Have one set of
1051// code to handle them here.
1052//
Jamie Madill075edd82013-07-08 13:30:19 -04001053bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001056 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001058 if (variable == 0) {
1059 if (reservedErrorCheck(line, identifier))
1060 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001062 if (voidErrorCheck(line, identifier, pType))
1063 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001065 //
1066 // add variable to symbol table
1067 //
1068 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001069 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001070 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001071 return true;
1072 // don't delete variable, it's used by error recovery, and the pool
1073 // pop will take care of the memory
1074 }
1075 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 //
1078 // identifier must be of type constant, a global, or a temporary
1079 //
1080 TQualifier qualifier = variable->getType().getQualifier();
1081 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001082 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001083 return true;
1084 }
1085 //
1086 // test for and propagate constant
1087 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001089 if (qualifier == EvqConst) {
1090 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001091 std::stringstream extraInfoStream;
1092 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1093 std::string extraInfo = extraInfoStream.str();
1094 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001095 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001096 return true;
1097 }
1098 if (type != initializer->getType()) {
1099 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001100 variable->getType().getQualifierString());
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 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001105 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001106 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001107 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001108 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001110 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 variable->shareConstPointer(constArray);
1112 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001113 std::stringstream extraInfoStream;
1114 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1115 std::string extraInfo = extraInfoStream.str();
1116 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001117 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001118 return true;
1119 }
1120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 if (qualifier != EvqConst) {
1123 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1124 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1125 if (intermNode == 0) {
1126 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1127 return true;
1128 }
1129 } else
1130 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133}
1134
1135bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1136{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001137 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 if (!aggrNode->isConstructor())
1139 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001141 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001143 // check if all the child nodes are constants so that they can be inserted into
1144 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001145 TIntermSequence &sequence = aggrNode->getSequence() ;
1146 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1147 if (!(*p)->getAsTyped()->getAsConstantUnion())
1148 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001149 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001151 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152}
1153
Jamie Madilla5efff92013-06-06 11:56:47 -04001154TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001155{
1156 TPublicType returnType = typeSpecifier;
1157 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001158 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001159
1160 if (typeSpecifier.array)
1161 {
1162 error(typeSpecifier.line, "not supported", "first-class array");
1163 recover();
1164 returnType.setArray(false);
1165 }
1166
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001167 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001168 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001169 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1170 {
1171 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1172 recover();
1173 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001174
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001175 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1176 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1177 {
1178 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1179 recover();
1180 }
1181 }
1182 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001183 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001184 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001185 {
Jamie Madill19571812013-08-12 15:26:34 -07001186 case EvqSmoothIn:
1187 case EvqSmoothOut:
1188 case EvqVertexOut:
1189 case EvqFragmentIn:
1190 case EvqCentroidOut:
1191 case EvqCentroidIn:
1192 if (typeSpecifier.type == EbtBool)
1193 {
1194 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1195 recover();
1196 }
1197 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1198 {
1199 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1200 recover();
1201 }
1202 break;
1203
1204 case EvqVertexIn:
1205 case EvqFragmentOut:
1206 case EvqFlatIn:
1207 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001208 if (typeSpecifier.type == EbtBool)
1209 {
1210 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1211 recover();
1212 }
1213 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001214
Jamie Madillb120eac2013-06-12 14:08:13 -04001215 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001216 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001217 }
1218
1219 return returnType;
1220}
1221
Jamie Madill075edd82013-07-08 13:30:19 -04001222TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001223{
1224 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1225 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1226
1227 if (identifier != "")
1228 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001229 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001230 recover();
1231
1232 // this error check can mutate the type
1233 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1234 recover();
1235
1236 TVariable* variable = 0;
1237
1238 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1239 recover();
1240
1241 if (variable && symbol)
1242 {
1243 symbol->setId(variable->getUniqueId());
1244 }
1245 }
1246
1247 return aggregate;
1248}
1249
Jamie Madill075edd82013-07-08 13:30:19 -04001250TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001251{
Jamie Madill51a53c72013-06-19 09:24:43 -04001252 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001253 recover();
1254
1255 // this error check can mutate the type
1256 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1257 recover();
1258
1259 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1260 {
1261 recover();
1262 }
1263
1264 TPublicType arrayType = publicType;
1265
1266 int size;
1267 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1268 {
1269 recover();
1270 }
1271 else
1272 {
1273 arrayType.setArray(true, size);
1274 }
1275
1276 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1277 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1278 TVariable* variable = 0;
1279
1280 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1281 recover();
1282
1283 if (variable && symbol)
1284 {
1285 symbol->setId(variable->getUniqueId());
1286 }
1287
1288 return aggregate;
1289}
1290
Jamie Madill075edd82013-07-08 13:30:19 -04001291TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001292{
Jamie Madilla5efff92013-06-06 11:56:47 -04001293 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001294 recover();
1295
1296 TIntermNode* intermNode;
1297 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1298 {
1299 //
1300 // Build intermediate representation
1301 //
1302 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1303 }
1304 else
1305 {
1306 recover();
1307 return NULL;
1308 }
1309}
1310
Jamie Madill075edd82013-07-08 13:30:19 -04001311TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001312{
1313 if (publicType.type == EbtInvariant && !identifierSymbol)
1314 {
1315 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1316 recover();
1317 }
1318
1319 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1320 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1321
1322 if (structQualifierErrorCheck(identifierLocation, publicType))
1323 recover();
1324
Jamie Madill0bd18df2013-06-20 11:55:52 -04001325 if (locationDeclaratorListCheck(identifierLocation, publicType))
1326 recover();
1327
Jamie Madill502d66f2013-06-20 11:55:52 -04001328 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1329 recover();
1330
1331 TVariable* variable = 0;
1332 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1333 recover();
1334 if (symbol && variable)
1335 symbol->setId(variable->getUniqueId());
1336
1337 return intermAggregate;
1338}
1339
Jamie Madill075edd82013-07-08 13:30:19 -04001340TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001341{
1342 if (structQualifierErrorCheck(identifierLocation, publicType))
1343 recover();
1344
Jamie Madill0bd18df2013-06-20 11:55:52 -04001345 if (locationDeclaratorListCheck(identifierLocation, publicType))
1346 recover();
1347
Jamie Madill502d66f2013-06-20 11:55:52 -04001348 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1349 recover();
1350
1351 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1352 {
1353 recover();
1354 }
1355 else if (indexExpression)
1356 {
1357 int size;
1358 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1359 recover();
1360 TPublicType arrayType(publicType);
1361 arrayType.setArray(true, size);
1362 TVariable* variable = NULL;
1363 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1364 recover();
1365 TType type = TType(arrayType);
1366 type.setArraySize(size);
1367
1368 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1369 }
1370 else
1371 {
1372 TPublicType arrayType(publicType);
1373 arrayType.setArray(true);
1374 TVariable* variable = NULL;
1375 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1376 recover();
1377 }
1378
1379 return NULL;
1380}
1381
Jamie Madill075edd82013-07-08 13:30:19 -04001382TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001383{
1384 if (structQualifierErrorCheck(identifierLocation, publicType))
1385 recover();
1386
Jamie Madill0bd18df2013-06-20 11:55:52 -04001387 if (locationDeclaratorListCheck(identifierLocation, publicType))
1388 recover();
1389
Jamie Madill502d66f2013-06-20 11:55:52 -04001390 TIntermNode* intermNode;
1391 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1392 {
1393 //
1394 // build the intermediate representation
1395 //
1396 if (intermNode)
1397 {
1398 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1399 }
1400 else
1401 {
1402 return declaratorList;
1403 }
1404 }
1405 else
1406 {
1407 recover();
1408 return NULL;
1409 }
1410}
1411
Jamie Madilla295edf2013-06-06 11:56:48 -04001412void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1413{
1414 if (typeQualifier.qualifier != EvqUniform)
1415 {
1416 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1417 recover();
1418 return;
1419 }
1420
1421 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1422 ASSERT(!layoutQualifier.isEmpty());
1423
1424 if (shaderVersion < 300)
1425 {
1426 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1427 recover();
1428 return;
1429 }
1430
1431 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1432 {
1433 recover();
1434 return;
1435 }
1436
Jamie Madill099c0f32013-06-20 11:55:52 -04001437 if (layoutQualifier.matrixPacking != EmpUnspecified)
1438 {
1439 defaultMatrixPacking = layoutQualifier.matrixPacking;
1440 }
1441
Geoff Langc6856732014-02-11 09:38:55 -05001442 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001443 {
1444 defaultBlockStorage = layoutQualifier.blockStorage;
1445 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001446}
1447
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001448TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1449{
1450 TOperator op = EOpNull;
1451 if (publicType.userDef)
1452 {
1453 op = EOpConstructStruct;
1454 }
1455 else
1456 {
1457 switch (publicType.type)
1458 {
1459 case EbtFloat:
1460 if (publicType.isMatrix())
1461 {
1462 // TODO: non-square matrices
1463 switch(publicType.getCols())
1464 {
Jamie Madill28b97422013-07-08 14:01:38 -04001465 case 2: op = EOpConstructMat2; break;
1466 case 3: op = EOpConstructMat3; break;
1467 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001468 }
1469 }
1470 else
1471 {
1472 switch(publicType.getNominalSize())
1473 {
Jamie Madill28b97422013-07-08 14:01:38 -04001474 case 1: op = EOpConstructFloat; break;
1475 case 2: op = EOpConstructVec2; break;
1476 case 3: op = EOpConstructVec3; break;
1477 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001478 }
1479 }
1480 break;
1481
1482 case EbtInt:
1483 switch(publicType.getNominalSize())
1484 {
Jamie Madill28b97422013-07-08 14:01:38 -04001485 case 1: op = EOpConstructInt; break;
1486 case 2: op = EOpConstructIVec2; break;
1487 case 3: op = EOpConstructIVec3; break;
1488 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001489 }
1490 break;
1491
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001492 case EbtUInt:
1493 switch(publicType.getNominalSize())
1494 {
Jamie Madill28b97422013-07-08 14:01:38 -04001495 case 1: op = EOpConstructUInt; break;
1496 case 2: op = EOpConstructUVec2; break;
1497 case 3: op = EOpConstructUVec3; break;
1498 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001499 }
1500 break;
1501
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001502 case EbtBool:
1503 switch(publicType.getNominalSize())
1504 {
Jamie Madill28b97422013-07-08 14:01:38 -04001505 case 1: op = EOpConstructBool; break;
1506 case 2: op = EOpConstructBVec2; break;
1507 case 3: op = EOpConstructBVec3; break;
1508 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001509 }
1510 break;
1511
1512 default: break;
1513 }
1514
1515 if (op == EOpNull)
1516 {
1517 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1518 recover();
1519 publicType.type = EbtFloat;
1520 op = EOpConstructFloat;
1521 }
1522 }
1523
1524 TString tempString;
1525 TType type(publicType);
1526 return new TFunction(&tempString, type, op);
1527}
1528
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529// This function is used to test for the correctness of the parameters passed to various constructor functions
1530// and also convert them to the right datatype if it is allowed and required.
1531//
1532// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1533//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001534TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, const TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001535{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001536 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001538 if (!aggregateArguments)
1539 {
1540 aggregateArguments = new TIntermAggregate;
1541 aggregateArguments->getSequence().push_back(arguments);
1542 }
1543
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001544 if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001545 {
1546 const TFieldList &fields = type->getStruct()->fields();
1547 TIntermSequence &args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001548
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001549 for (size_t i = 0; i < fields.size(); i++)
1550 {
1551 if (args[i]->getAsTyped()->getType() != *fields[i]->type())
1552 {
1553 error(line, "Structure constructor arguments do not match structure fields", "Error");
1554 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001555
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001556 return 0;
1557 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001558 }
1559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001561 // Turn the argument list itself into a constructor
1562 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1563 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001564 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001565 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001566 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001570}
1571
1572TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1573{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001574 bool canBeFolded = areAllChildConst(aggrNode);
1575 aggrNode->setType(type);
1576 if (canBeFolded) {
1577 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001578 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001579 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001580 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001581 }
1582 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001583 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001584 }
1585 if (returnVal)
1586 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001588 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001591 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592}
1593
1594// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1595// for the parameter to the constructor (passed to this function). Essentially, it converts
1596// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1597// float, then float is converted to int.
1598//
1599// Returns 0 for an error or the constructed node.
1600//
Jamie Madill075edd82013-07-08 13:30:19 -04001601TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001603 TIntermTyped* newNode;
1604 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001606 //
1607 // First, convert types as needed.
1608 //
1609 switch (op) {
1610 case EOpConstructVec2:
1611 case EOpConstructVec3:
1612 case EOpConstructVec4:
1613 case EOpConstructMat2:
1614 case EOpConstructMat3:
1615 case EOpConstructMat4:
1616 case EOpConstructFloat:
1617 basicOp = EOpConstructFloat;
1618 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001620 case EOpConstructIVec2:
1621 case EOpConstructIVec3:
1622 case EOpConstructIVec4:
1623 case EOpConstructInt:
1624 basicOp = EOpConstructInt;
1625 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001627 case EOpConstructUVec2:
1628 case EOpConstructUVec3:
1629 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001630 case EOpConstructUInt:
1631 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001632 break;
1633
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001634 case EOpConstructBVec2:
1635 case EOpConstructBVec3:
1636 case EOpConstructBVec4:
1637 case EOpConstructBool:
1638 basicOp = EOpConstructBool;
1639 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001641 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001642 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001643 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001645 return 0;
1646 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001647 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001648 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001649 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001650 return 0;
1651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001653 //
1654 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1655 //
1656
1657 // Otherwise, skip out early.
1658 if (subset || (newNode != node && newNode->getType() == *type))
1659 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001661 // setAggregateOperator will insert a new node for the constructor, as needed.
1662 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663}
1664
1665// This function tests for the type of the parameters to the structures constructors. Raises
1666// an error message if the expected type does not match the parameter passed to the constructor.
1667//
1668// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1669//
Jamie Madill075edd82013-07-08 13:30:19 -04001670TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001672 if (*type == node->getAsTyped()->getType()) {
1673 if (subset)
1674 return node->getAsTyped();
1675 else
1676 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1677 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001678 std::stringstream extraInfoStream;
1679 extraInfoStream << "cannot convert parameter " << paramCount
1680 << " from '" << node->getAsTyped()->getType().getBasicString()
1681 << "' to '" << type->getBasicString() << "'";
1682 std::string extraInfo = extraInfoStream.str();
1683 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001684 recover();
1685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001687 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688}
1689
1690//
1691// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1692// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1693// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1694// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1695// a constant matrix.
1696//
Jamie Madill075edd82013-07-08 13:30:19 -04001697TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001699 TIntermTyped* typedNode;
1700 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001702 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001703 if (tempConstantNode) {
1704 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001706 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001707 return node;
1708 }
1709 } 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 +00001710 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001711 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001713 return 0;
1714 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001716 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001718 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001719 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001720 std::stringstream extraInfoStream;
1721 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1722 std::string extraInfo = extraInfoStream.str();
1723 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 recover();
1725 fields.offsets[i] = 0;
1726 }
1727
1728 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001730 }
1731 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1732 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733}
1734
1735//
1736// This function returns the column being accessed from a constant matrix. The values are retrieved from
1737// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1738// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1739// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1740//
Jamie Madill075edd82013-07-08 13:30:19 -04001741TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001743 TIntermTyped* typedNode;
1744 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001746 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001747 std::stringstream extraInfoStream;
1748 extraInfoStream << "matrix field selection out of range '" << index << "'";
1749 std::string extraInfo = extraInfoStream.str();
1750 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 recover();
1752 index = 0;
1753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001756 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001757 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001758 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1759 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001760 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001761 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001763 return 0;
1764 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001766 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767}
1768
1769
1770//
1771// This function returns an element of an array accessed from a constant array. The values are retrieved from
1772// the symbol table and parse-tree is built for the type of the element. The input
1773// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1774// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1775//
Jamie Madill075edd82013-07-08 13:30:19 -04001776TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001778 TIntermTyped* typedNode;
1779 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1780 TType arrayElementType = node->getType();
1781 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001782
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001783 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001784 std::stringstream extraInfoStream;
1785 extraInfoStream << "array field selection out of range '" << index << "'";
1786 std::string extraInfo = extraInfoStream.str();
1787 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001788 recover();
1789 index = 0;
1790 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001793 size_t arrayElementSize = arrayElementType.getObjectSize();
1794 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1795 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001796 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001797 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001798 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 return 0;
1801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001802
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001803 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804}
1805
1806
1807//
1808// This function returns the value of a particular field inside a constant structure from the symbol table.
1809// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1810// function and returns the parse-tree with the values of the embedded/nested struct.
1811//
Jamie Madill075edd82013-07-08 13:30:19 -04001812TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813{
Jamie Madill98493dd2013-07-08 14:39:03 -04001814 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001815 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816
Jamie Madill98493dd2013-07-08 14:39:03 -04001817 for (size_t index = 0; index < fields.size(); ++index) {
1818 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 break;
1820 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001822 }
1823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
Jamie Madill94bf7f22013-07-08 13:31:15 -04001825 TIntermTyped *typedNode;
1826 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001828 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001830 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1831 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001832 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001833 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001835 return 0;
1836 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001838 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839}
1840
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001841//
1842// Interface/uniform blocks
1843//
Jamie Madill98493dd2013-07-08 14:39:03 -04001844TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1845 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001846{
1847 if (reservedErrorCheck(nameLine, blockName))
1848 recover();
1849
1850 if (typeQualifier.qualifier != EvqUniform)
1851 {
1852 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1853 recover();
1854 }
1855
Jamie Madill099c0f32013-06-20 11:55:52 -04001856 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1857 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001858 {
1859 recover();
1860 }
1861
Jamie Madill099c0f32013-06-20 11:55:52 -04001862 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1863 {
1864 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1865 }
1866
Jamie Madill1566ef72013-06-20 11:55:54 -04001867 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1868 {
1869 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1870 }
1871
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001872 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1873 if (!symbolTable.declare(*blockNameSymbol)) {
1874 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1875 recover();
1876 }
1877
Jamie Madill98493dd2013-07-08 14:39:03 -04001878 // check for sampler types and apply layout qualifiers
1879 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1880 TField* field = (*fieldList)[memberIndex];
1881 TType* fieldType = field->type();
1882 if (IsSampler(fieldType->getBasicType())) {
1883 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001884 recover();
1885 }
1886
Jamie Madill98493dd2013-07-08 14:39:03 -04001887 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001888 switch (qualifier)
1889 {
1890 case EvqGlobal:
1891 case EvqUniform:
1892 break;
1893 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001895 recover();
1896 break;
1897 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001898
1899 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001900 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1901 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001902 {
1903 recover();
1904 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001905
Jamie Madill98493dd2013-07-08 14:39:03 -04001906 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001907 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001908 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001909 recover();
1910 }
1911
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001913 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001914 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001915 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001916 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001917 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 recover();
1920 }
1921
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001923 }
1924
Jamie Madill98493dd2013-07-08 14:39:03 -04001925 // add array index
1926 int arraySize = 0;
1927 if (arrayIndex != NULL)
1928 {
1929 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1930 recover();
1931 }
1932
1933 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1934 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001935
1936 TString symbolName = "";
1937 int symbolId = 0;
1938
Jamie Madill98493dd2013-07-08 14:39:03 -04001939 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001940 {
1941 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001942 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1943 {
1944 TField* field = (*fieldList)[memberIndex];
1945 TType* fieldType = field->type();
1946
1947 // set parent pointer of the field variable
1948 fieldType->setInterfaceBlock(interfaceBlock);
1949
1950 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1951 fieldVariable->setQualifier(typeQualifier.qualifier);
1952
1953 if (!symbolTable.declare(*fieldVariable)) {
1954 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001955 recover();
1956 }
1957 }
1958 }
1959 else
1960 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001961 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001962 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001963 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001964
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001965 if (!symbolTable.declare(*instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001966 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001967 recover();
1968 }
1969
1970 symbolId = instanceTypeDef->getUniqueId();
1971 symbolName = instanceTypeDef->getName();
1972 }
1973
Jamie Madill98493dd2013-07-08 14:39:03 -04001974 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001975 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001976
1977 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001978 return aggregate;
1979}
1980
Jamie Madill075edd82013-07-08 13:30:19 -04001981bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00001982{
1983 ++structNestingLevel;
1984
1985 // Embedded structure definitions are not supported per GLSL ES spec.
1986 // They aren't allowed in GLSL either, but we need to detect this here
1987 // so we don't rely on the GLSL compiler to catch it.
1988 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001989 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00001990 return true;
1991 }
1992
1993 return false;
1994}
1995
1996void TParseContext::exitStructDeclaration()
1997{
1998 --structNestingLevel;
1999}
2000
2001namespace {
2002
2003const int kWebGLMaxStructNesting = 4;
2004
2005} // namespace
2006
Jamie Madill98493dd2013-07-08 14:39:03 -04002007bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002008{
Jamie Madill5508f392014-02-20 13:31:36 -05002009 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002010 return false;
2011 }
2012
Jamie Madill98493dd2013-07-08 14:39:03 -04002013 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002014 return false;
2015 }
2016
2017 // We're already inside a structure definition at this point, so add
2018 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002019 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002020 std::stringstream reasonStream;
2021 reasonStream << "Reference of struct type "
2022 << field.type()->getStruct()->name().c_str()
2023 << " exceeds maximum allowed nesting level of "
2024 << kWebGLMaxStructNesting;
2025 std::string reason = reasonStream.str();
2026 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002027 return true;
2028 }
2029
2030 return false;
2031}
2032
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002033//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002034// Parse an array index expression
2035//
Jamie Madill075edd82013-07-08 13:30:19 -04002036TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002037{
2038 TIntermTyped *indexedExpression = NULL;
2039
2040 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2041 {
2042 if (baseExpression->getAsSymbolNode())
2043 {
2044 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2045 }
2046 else
2047 {
2048 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2049 }
2050 recover();
2051 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002052
Jamie Madill7164cf42013-07-08 13:30:59 -04002053 if (indexExpression->getQualifier() == EvqConst)
2054 {
2055 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2056 if (index < 0)
2057 {
2058 std::stringstream infoStream;
2059 infoStream << index;
2060 std::string info = infoStream.str();
2061 error(location, "negative index", info.c_str());
2062 recover();
2063 index = 0;
2064 }
2065 if (baseExpression->getType().getQualifier() == EvqConst)
2066 {
2067 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002068 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002069 // constant folding for arrays
2070 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002071 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002072 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002073 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002074 // constant folding for vectors
2075 TVectorFields fields;
2076 fields.num = 1;
2077 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2078 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2079 }
2080 else if (baseExpression->isMatrix())
2081 {
2082 // constant folding for matrices
2083 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002084 }
2085 }
2086 else
2087 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002088 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002089 {
Jamie Madill18464b52013-07-08 14:01:55 -04002090 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002091 {
2092 std::stringstream extraInfoStream;
2093 extraInfoStream << "array index out of range '" << index << "'";
2094 std::string extraInfo = extraInfoStream.str();
2095 error(location, "", "[", extraInfo.c_str());
2096 recover();
2097 index = baseExpression->getType().getArraySize() - 1;
2098 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002099 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2100 {
2101 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2102 recover();
2103 index = 0;
2104 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002105 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002106 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002107 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002108 std::stringstream extraInfoStream;
2109 extraInfoStream << "field selection out of range '" << index << "'";
2110 std::string extraInfo = extraInfoStream.str();
2111 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002112 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002113 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002114 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002115
Jamie Madill7164cf42013-07-08 13:30:59 -04002116 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2117 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002118 }
2119 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002120 else
2121 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002122 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002123 {
Jamie Madill19571812013-08-12 15:26:34 -07002124 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002125 recover();
2126 }
Jamie Madill19571812013-08-12 15:26:34 -07002127 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002128 {
Jamie Madill19571812013-08-12 15:26:34 -07002129 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002130 recover();
2131 }
2132
2133 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2134 }
2135
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002136 if (indexedExpression == 0)
2137 {
2138 ConstantUnion *unionArray = new ConstantUnion[1];
2139 unionArray->setFConst(0.0f);
2140 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2141 }
2142 else if (baseExpression->isArray())
2143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002144 const TType &baseType = baseExpression->getType();
2145 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002146 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002147 TType copyOfType(baseType.getStruct());
2148 indexedExpression->setType(copyOfType);
2149 }
2150 else if (baseType.isInterfaceBlock())
2151 {
2152 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002153 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002154 }
2155 else
2156 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002157 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002158 }
2159
2160 if (baseExpression->getType().getQualifier() == EvqConst)
2161 {
2162 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2163 }
2164 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002165 else if (baseExpression->isMatrix())
2166 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002167 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2168 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002169 }
2170 else if (baseExpression->isVector())
2171 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002172 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2173 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002174 }
2175 else
2176 {
2177 indexedExpression->setType(baseExpression->getType());
2178 }
2179
2180 return indexedExpression;
2181}
2182
Jamie Madill075edd82013-07-08 13:30:19 -04002183TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002184{
2185 TIntermTyped *indexedExpression = NULL;
2186
2187 if (baseExpression->isArray())
2188 {
2189 error(fieldLocation, "cannot apply dot operator to an array", ".");
2190 recover();
2191 }
2192
2193 if (baseExpression->isVector())
2194 {
2195 TVectorFields fields;
2196 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2197 {
2198 fields.num = 1;
2199 fields.offsets[0] = 0;
2200 recover();
2201 }
2202
2203 if (baseExpression->getType().getQualifier() == EvqConst)
2204 {
2205 // constant folding for vector fields
2206 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2207 if (indexedExpression == 0)
2208 {
2209 recover();
2210 indexedExpression = baseExpression;
2211 }
2212 else
2213 {
2214 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2215 }
2216 }
2217 else
2218 {
2219 TString vectorString = fieldString;
2220 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2221 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2222 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2223 }
2224 }
2225 else if (baseExpression->isMatrix())
2226 {
2227 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002228 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002229 {
2230 fields.wholeRow = false;
2231 fields.wholeCol = false;
2232 fields.row = 0;
2233 fields.col = 0;
2234 recover();
2235 }
2236
2237 if (fields.wholeRow || fields.wholeCol)
2238 {
2239 error(dotLocation, " non-scalar fields not implemented yet", ".");
2240 recover();
2241 ConstantUnion *unionArray = new ConstantUnion[1];
2242 unionArray->setIConst(0);
2243 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2244 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002245 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002246 }
2247 else
2248 {
2249 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002250 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002251 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2252 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2253 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2254 }
2255 }
2256 else if (baseExpression->getBasicType() == EbtStruct)
2257 {
2258 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002259 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2260 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002261 {
2262 error(dotLocation, "structure has no fields", "Internal Error");
2263 recover();
2264 indexedExpression = baseExpression;
2265 }
2266 else
2267 {
2268 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002269 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002270 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002271 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002272 {
2273 fieldFound = true;
2274 break;
2275 }
2276 }
2277 if (fieldFound)
2278 {
2279 if (baseExpression->getType().getQualifier() == EvqConst)
2280 {
2281 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2282 if (indexedExpression == 0)
2283 {
2284 recover();
2285 indexedExpression = baseExpression;
2286 }
2287 else
2288 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002289 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002290 // change the qualifier of the return type, not of the structure field
2291 // as the structure definition is shared between various structures.
2292 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2293 }
2294 }
2295 else
2296 {
2297 ConstantUnion *unionArray = new ConstantUnion[1];
2298 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002299 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002300 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002301 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002302 }
2303 }
2304 else
2305 {
2306 error(dotLocation, " no such field in structure", fieldString.c_str());
2307 recover();
2308 indexedExpression = baseExpression;
2309 }
2310 }
2311 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002312 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002313 {
2314 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002315 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2316 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002317 {
2318 error(dotLocation, "interface block has no fields", "Internal Error");
2319 recover();
2320 indexedExpression = baseExpression;
2321 }
2322 else
2323 {
2324 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002325 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002326 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002327 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002328 {
2329 fieldFound = true;
2330 break;
2331 }
2332 }
2333 if (fieldFound)
2334 {
2335 ConstantUnion *unionArray = new ConstantUnion[1];
2336 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002337 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002338 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002339 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002340 }
2341 else
2342 {
2343 error(dotLocation, " no such field in interface block", fieldString.c_str());
2344 recover();
2345 indexedExpression = baseExpression;
2346 }
2347 }
2348 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002349 else
2350 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002351 if (shaderVersion < 300)
2352 {
2353 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2354 }
2355 else
2356 {
2357 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2358 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002359 recover();
2360 indexedExpression = baseExpression;
2361 }
2362
2363 return indexedExpression;
2364}
2365
Jamie Madill075edd82013-07-08 13:30:19 -04002366TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002367{
Jamie Madilla5efff92013-06-06 11:56:47 -04002368 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002369
Jamie Madilla5efff92013-06-06 11:56:47 -04002370 qualifier.location = -1;
2371 qualifier.matrixPacking = EmpUnspecified;
2372 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002373
2374 if (qualifierType == "shared")
2375 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002376 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002377 }
2378 else if (qualifierType == "packed")
2379 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002380 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002381 }
2382 else if (qualifierType == "std140")
2383 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002384 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002385 }
2386 else if (qualifierType == "row_major")
2387 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002388 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002389 }
2390 else if (qualifierType == "column_major")
2391 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002392 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002393 }
2394 else if (qualifierType == "location")
2395 {
2396 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2397 recover();
2398 }
2399 else
2400 {
2401 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2402 recover();
2403 }
2404
Jamie Madilla5efff92013-06-06 11:56:47 -04002405 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002406}
2407
Jamie Madill075edd82013-07-08 13:30:19 -04002408TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002409{
Jamie Madilla5efff92013-06-06 11:56:47 -04002410 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002411
Jamie Madilla5efff92013-06-06 11:56:47 -04002412 qualifier.location = -1;
2413 qualifier.matrixPacking = EmpUnspecified;
2414 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002415
2416 if (qualifierType != "location")
2417 {
2418 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2419 recover();
2420 }
2421 else
2422 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002423 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002424 if (intValue < 0)
2425 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002426 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002427 recover();
2428 }
2429 else
2430 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002431 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002432 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002433 }
2434
Jamie Madilla5efff92013-06-06 11:56:47 -04002435 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002436}
2437
Jamie Madilla5efff92013-06-06 11:56:47 -04002438TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002439{
Jamie Madilla5efff92013-06-06 11:56:47 -04002440 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002441
Jamie Madilla5efff92013-06-06 11:56:47 -04002442 if (rightQualifier.location != -1)
2443 {
2444 joinedQualifier.location = rightQualifier.location;
2445 }
2446 if (rightQualifier.matrixPacking != EmpUnspecified)
2447 {
2448 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2449 }
2450 if (rightQualifier.blockStorage != EbsUnspecified)
2451 {
2452 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2453 }
2454
2455 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002456}
2457
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002458TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2459 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2460{
2461 TQualifier mergedQualifier = EvqSmoothIn;
2462
Jamie Madill19571812013-08-12 15:26:34 -07002463 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002464 if (interpolationQualifier == EvqSmooth)
2465 mergedQualifier = EvqSmoothIn;
2466 else if (interpolationQualifier == EvqFlat)
2467 mergedQualifier = EvqFlatIn;
2468 else UNREACHABLE();
2469 }
2470 else if (storageQualifier == EvqCentroidIn) {
2471 if (interpolationQualifier == EvqSmooth)
2472 mergedQualifier = EvqCentroidIn;
2473 else if (interpolationQualifier == EvqFlat)
2474 mergedQualifier = EvqFlatIn;
2475 else UNREACHABLE();
2476 }
Jamie Madill19571812013-08-12 15:26:34 -07002477 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002478 if (interpolationQualifier == EvqSmooth)
2479 mergedQualifier = EvqSmoothOut;
2480 else if (interpolationQualifier == EvqFlat)
2481 mergedQualifier = EvqFlatOut;
2482 else UNREACHABLE();
2483 }
2484 else if (storageQualifier == EvqCentroidOut) {
2485 if (interpolationQualifier == EvqSmooth)
2486 mergedQualifier = EvqCentroidOut;
2487 else if (interpolationQualifier == EvqFlat)
2488 mergedQualifier = EvqFlatOut;
2489 else UNREACHABLE();
2490 }
2491 else {
2492 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2493 recover();
2494
2495 mergedQualifier = storageQualifier;
2496 }
2497
2498 TPublicType type;
2499 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2500 return type;
2501}
2502
Jamie Madill98493dd2013-07-08 14:39:03 -04002503TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002504{
Jamie Madill98493dd2013-07-08 14:39:03 -04002505 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002506 recover();
2507 }
2508
Jamie Madill98493dd2013-07-08 14:39:03 -04002509 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002510 //
2511 // Careful not to replace already known aspects of type, like array-ness
2512 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002513 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002514 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002515 type->setPrimarySize(typeSpecifier.primarySize);
2516 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002517 type->setPrecision(typeSpecifier.precision);
2518 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002519 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002520
2521 // don't allow arrays of arrays
2522 if (type->isArray()) {
2523 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2524 recover();
2525 }
2526 if (typeSpecifier.array)
2527 type->setArraySize(typeSpecifier.arraySize);
2528 if (typeSpecifier.userDef) {
2529 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002530 }
2531
Jamie Madill98493dd2013-07-08 14:39:03 -04002532 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002533 recover();
2534 }
2535 }
2536
Jamie Madill98493dd2013-07-08 14:39:03 -04002537 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002538}
2539
Jamie Madill98493dd2013-07-08 14:39:03 -04002540TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002541{
Jamie Madill98493dd2013-07-08 14:39:03 -04002542 TStructure* structure = new TStructure(structName, fieldList);
2543 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002544
Jamie Madillbfa91f42014-06-05 15:45:18 -04002545 structure->setUniqueId(TSymbolTable::nextUniqueId());
2546
Jamie Madill98493dd2013-07-08 14:39:03 -04002547 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002548 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002549 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002550 {
2551 recover();
2552 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002554 if (!symbolTable.declare(*userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002556 recover();
2557 }
2558 }
2559
2560 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002561 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002563 const TField &field = *(*fieldList)[typeListIndex];
2564 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002565 switch (qualifier)
2566 {
2567 case EvqGlobal:
2568 case EvqTemporary:
2569 break;
2570 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002571 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002572 recover();
2573 break;
2574 }
2575 }
2576
2577 TPublicType publicType;
2578 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002579 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002580 exitStructDeclaration();
2581
2582 return publicType;
2583}
2584
2585//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002586// Parse an array of strings using yyparse.
2587//
2588// Returns 0 for success.
2589//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002590int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002591 TParseContext* context) {
2592 if ((count == 0) || (string == NULL))
2593 return 1;
2594
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002595 if (glslang_initialize(context))
2596 return 1;
2597
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002598 int error = glslang_scan(count, string, length, context);
2599 if (!error)
2600 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002601
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002602 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002603
alokp@chromium.org6b495712012-06-29 00:06:58 +00002604 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002605}
2606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002608