blob: aa347db458238ec69eeb3e47c977d1b9614f35e4 [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
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020013#include "compiler/translator/glslang.h"
14#include "compiler/translator/ValidateSwitch.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
alokp@chromium.org8b851c62012-06-15 16:25:11 +000016///////////////////////////////////////////////////////////////////////
17//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018// Sub- vector and matrix fields
19//
20////////////////////////////////////////////////////////////////////////
21
22//
23// Look at a '.' field selector string and change it into offsets
24// for a vector.
25//
Jamie Madill075edd82013-07-08 13:30:19 -040026bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000028 fields.num = (int) compString.size();
29 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000030 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000031 return false;
32 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000034 enum {
35 exyzw,
36 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000037 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000040 for (int i = 0; i < fields.num; ++i) {
41 switch (compString[i]) {
42 case 'x':
43 fields.offsets[i] = 0;
44 fieldSet[i] = exyzw;
45 break;
46 case 'r':
47 fields.offsets[i] = 0;
48 fieldSet[i] = ergba;
49 break;
50 case 's':
51 fields.offsets[i] = 0;
52 fieldSet[i] = estpq;
53 break;
54 case 'y':
55 fields.offsets[i] = 1;
56 fieldSet[i] = exyzw;
57 break;
58 case 'g':
59 fields.offsets[i] = 1;
60 fieldSet[i] = ergba;
61 break;
62 case 't':
63 fields.offsets[i] = 1;
64 fieldSet[i] = estpq;
65 break;
66 case 'z':
67 fields.offsets[i] = 2;
68 fieldSet[i] = exyzw;
69 break;
70 case 'b':
71 fields.offsets[i] = 2;
72 fieldSet[i] = ergba;
73 break;
74 case 'p':
75 fields.offsets[i] = 2;
76 fieldSet[i] = estpq;
77 break;
78
79 case 'w':
80 fields.offsets[i] = 3;
81 fieldSet[i] = exyzw;
82 break;
83 case 'a':
84 fields.offsets[i] = 3;
85 fieldSet[i] = ergba;
86 break;
87 case 'q':
88 fields.offsets[i] = 3;
89 fieldSet[i] = estpq;
90 break;
91 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000092 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000093 return false;
94 }
95 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000097 for (int i = 0; i < fields.num; ++i) {
98 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000099 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000100 return false;
101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000103 if (i > 0) {
104 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000105 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000106 return false;
107 }
108 }
109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000111 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112}
113
114
115//
116// Look at a '.' field selector string and change it into offsets
117// for a matrix.
118//
Jamie Madill075edd82013-07-08 13:30:19 -0400119bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 fields.wholeRow = false;
122 fields.wholeCol = false;
123 fields.row = -1;
124 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000127 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 return false;
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000131 if (compString[0] == '_') {
132 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000133 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000134 return false;
135 }
136 fields.wholeCol = true;
137 fields.col = compString[1] - '0';
138 } else if (compString[1] == '_') {
139 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000140 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000141 return false;
142 }
143 fields.wholeRow = true;
144 fields.row = compString[0] - '0';
145 } else {
146 if (compString[0] < '0' || compString[0] > '3' ||
147 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000148 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000149 return false;
150 }
151 fields.row = compString[0] - '0';
152 fields.col = compString[1] - '0';
153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000155 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000156 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000157 return false;
158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000160 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161}
162
163///////////////////////////////////////////////////////////////////////
164//
165// Errors
166//
167////////////////////////////////////////////////////////////////////////
168
169//
170// Track whether errors have occurred.
171//
172void TParseContext::recover()
173{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176//
177// Used by flex/bison to output all syntax and parsing errors.
178//
Jamie Madill075edd82013-07-08 13:30:19 -0400179void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000180 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000181 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000183 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400184 srcLoc.file = loc.first_file;
185 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500186 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000187 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
Jamie Madill075edd82013-07-08 13:30:19 -0400191void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000192 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000193 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000194 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400195 srcLoc.file = loc.first_file;
196 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500197 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000198 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000199}
200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201//
202// Same error message for all places assignments don't work.
203//
Jamie Madill075edd82013-07-08 13:30:19 -0400204void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000206 std::stringstream extraInfoStream;
207 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
208 std::string extraInfo = extraInfoStream.str();
209 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210}
211
212//
213// Same error message for all places unary operations don't work.
214//
Jamie Madill075edd82013-07-08 13:30:19 -0400215void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000217 std::stringstream extraInfoStream;
218 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
219 << " (or there is no acceptable conversion)";
220 std::string extraInfo = extraInfoStream.str();
221 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222}
223
224//
225// Same error message for all binary operations don't work.
226//
Jamie Madill075edd82013-07-08 13:30:19 -0400227void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000229 std::stringstream extraInfoStream;
230 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
231 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
232 std::string extraInfo = extraInfoStream.str();
233 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
Jamie Madill075edd82013-07-08 13:30:19 -0400236bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000237 if (!checksPrecisionErrors)
238 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000239 switch( type ){
240 case EbtFloat:
241 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000242 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 return true;
244 }
245 break;
246 case EbtInt:
247 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000248 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000249 return true;
250 }
251 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000252 default:
253 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000254 }
255 return false;
256}
257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258//
259// Both test and if necessary, spit out an error, to see if the node is really
260// an l-value that can be operated on this way.
261//
262// Returns true if the was an error.
263//
Jamie Madill075edd82013-07-08 13:30:19 -0400264bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000266 TIntermSymbol* symNode = node->getAsSymbolNode();
267 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000269 if (binaryNode) {
270 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000272 switch(binaryNode->getOp()) {
273 case EOpIndexDirect:
274 case EOpIndexIndirect:
275 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000276 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 return lValueErrorCheck(line, op, binaryNode->getLeft());
278 case EOpVectorSwizzle:
279 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
280 if (!errorReturn) {
281 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000283 TIntermTyped* rightNode = binaryNode->getRight();
284 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700285
286 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
287 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000288 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700289 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000290 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000291 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 return true;
294 }
295 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700299 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 break;
301 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000302 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 return true;
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 const char* symbol = 0;
309 if (symNode != 0)
310 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* message = 0;
313 switch (node->getQualifier()) {
314 case EvqConst: message = "can't modify a const"; break;
315 case EvqConstReadOnly: message = "can't modify a const"; break;
316 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700317 case EvqFragmentIn: message = "can't modify an input"; break;
318 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000319 case EvqUniform: message = "can't modify a uniform"; break;
320 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000321 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
322 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
323 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
324 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000326 //
327 // Type that can't be written to?
328 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400329 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400331 }
332 if (IsSampler(node->getBasicType())) {
333 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 }
335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000337 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000338 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return true;
341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 //
345 // Everything else is okay, no error.
346 //
347 if (message == 0)
348 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000350 //
351 // If we get here, we have an error and a message.
352 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000353 if (symNode) {
354 std::stringstream extraInfoStream;
355 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
356 std::string extraInfo = extraInfoStream.str();
357 error(line, " l-value required", op, extraInfo.c_str());
358 }
359 else {
360 std::stringstream extraInfoStream;
361 extraInfoStream << "(" << message << ")";
362 std::string extraInfo = extraInfoStream.str();
363 error(line, " l-value required", op, extraInfo.c_str());
364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000366 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
369//
370// Both test, and if necessary spit out an error, to see if the node is really
371// a constant.
372//
373// Returns true if the was an error.
374//
375bool TParseContext::constErrorCheck(TIntermTyped* node)
376{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000377 if (node->getQualifier() == EvqConst)
378 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000380 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000382 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
384
385//
386// Both test, and if necessary spit out an error, to see if the node is really
387// an integer.
388//
389// Returns true if the was an error.
390//
391bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
392{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000393 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000394 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000396 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399}
400
401//
402// Both test, and if necessary spit out an error, to see if we are currently
403// globally scoped.
404//
405// Returns true if the was an error.
406//
Jamie Madill075edd82013-07-08 13:30:19 -0400407bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000409 if (global)
410 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000412 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
417//
418// For now, keep it simple: if it starts "gl_", it's reserved, independent
419// of scope. Except, if the symbol table is at the built-in push-level,
420// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000421// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
422// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423//
424// Returns true if there was an error.
425//
Jamie Madill075edd82013-07-08 13:30:19 -0400426bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000428 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000429 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000430 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000431 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 return true;
433 }
Jamie Madill5508f392014-02-20 13:31:36 -0500434 if (IsWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000435 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000437 return true;
438 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000439 if (identifier.compare(0, 7, "_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 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000443 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000444 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000445 return true;
446 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000447 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000450 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 }
452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
456
457//
458// Make sure there is enough data provided to the constructor to build
459// something of the type of the constructor. Also returns the type of
460// the constructor.
461//
462// Returns true if there was an error in construction.
463//
Jamie Madill075edd82013-07-08 13:30:19 -0400464bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000468 bool constructingMatrix = false;
469 switch(op) {
470 case EOpConstructMat2:
471 case EOpConstructMat3:
472 case EOpConstructMat4:
473 constructingMatrix = true;
474 break;
475 default:
476 break;
477 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000479 //
480 // Note: It's okay to have too many components available, but not okay to have unused
481 // arguments. 'full' will go to true when enough args have been seen. If we loop
482 // again, there is an extra argument, so 'overfull' will become true.
483 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
Jamie Madill94bf7f22013-07-08 13:31:15 -0400485 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 bool constType = true;
487 bool full = false;
488 bool overFull = false;
489 bool matrixInMatrix = false;
490 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000491 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000492 const TParameter& param = function.getParam(i);
493 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000495 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496 matrixInMatrix = true;
497 if (full)
498 overFull = true;
499 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
500 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000501 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000503 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 arrayArg = true;
505 }
506
507 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000508 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509
Olli Etuaho376f1b52015-04-13 13:23:41 +0300510 if (type->isArray())
511 {
512 if (type->isUnsizedArray())
513 {
514 type->setArraySize(function.getParamCount());
515 }
516 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
517 {
518 error(line, "array constructor needs one argument per array element", "constructor");
519 return true;
520 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000524 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 return true;
526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000528 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000529 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000530 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000531 return true;
532 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000536 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 return true;
538 }
539
Brendan Longeaa84062013-12-08 18:26:50 +0100540 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000541 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 return true;
543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000545 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000546 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
547 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000548 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000549 return true;
550 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000553 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000555 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 return true;
557 }
558 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000559 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 return true;
561 }
562 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000563 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 return true;
565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000567 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568}
569
570// This function checks to see if a void variable has been declared and raise an error message for such a case
571//
572// returns true in case of an error
573//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300574bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300576 if (type == EbtVoid)
577 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000578 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583}
584
585// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
586//
587// returns true in case of an error
588//
Jamie Madill075edd82013-07-08 13:30:19 -0400589bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000592 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 return true;
594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
599// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
600//
601// returns true in case of an error
602//
Jamie Madill075edd82013-07-08 13:30:19 -0400603bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000605 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000606 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 return true;
608 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611}
612
Jamie Madill075edd82013-07-08 13:30:19 -0400613bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 if (pType.type == EbtStruct) {
616 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000617 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618
619 return true;
620 }
621
622 return false;
623 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 return true;
627 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630}
631
Jamie Madill075edd82013-07-08 13:30:19 -0400632bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400633{
634 if (pType.layoutQualifier.location != -1)
635 {
636 error(line, "location must only be specified for a single input or output variable", "location");
637 return true;
638 }
639
640 return false;
641}
642
Jamie Madill075edd82013-07-08 13:30:19 -0400643bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
646 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000647 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return true;
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654bool TParseContext::containsSampler(TType& type)
655{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 if (IsSampler(type.getBasicType()))
657 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658
Jamie Madill98493dd2013-07-08 14:39:03 -0400659 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
660 const TFieldList& fields = type.getStruct()->fields();
661 for (unsigned int i = 0; i < fields.size(); ++i) {
662 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 return true;
664 }
665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670//
671// Do size checking for an array type's size.
672//
673// Returns true if there was an error.
674//
Jamie Madill075edd82013-07-08 13:30:19 -0400675bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000678
Olli Etuahoe7847b02015-03-16 11:56:12 +0200679 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000680 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000681 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200682 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 return true;
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
Nicolas Capens906744a2014-06-06 15:18:07 -0400686 unsigned int unsignedSize = 0;
687
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000688 if (constant->getBasicType() == EbtUInt)
689 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400690 unsignedSize = constant->getUConst(0);
691 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000692 }
693 else
694 {
695 size = constant->getIConst(0);
696
Nicolas Capens906744a2014-06-06 15:18:07 -0400697 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000698 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400699 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000700 size = 1;
701 return true;
702 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400703
704 unsignedSize = static_cast<unsigned int>(size);
705 }
706
707 if (size == 0)
708 {
709 error(line, "array size must be greater than zero", "");
710 size = 1;
711 return true;
712 }
713
714 // The size of arrays is restricted here to prevent issues further down the
715 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
716 // 4096 registers so this should be reasonable even for aggressively optimizable code.
717 const unsigned int sizeLimit = 65536;
718
719 if (unsignedSize > sizeLimit)
720 {
721 error(line, "array size too large", "");
722 size = 1;
723 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729//
730// See if this qualifier can be an array.
731//
732// Returns true if there is an error.
733//
Olli Etuaho3739d232015-04-08 12:23:44 +0300734bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Olli Etuaho3739d232015-04-08 12:23:44 +0300736 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
737 (type.qualifier == EvqConst && shaderVersion < 300))
738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return true;
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
746//
747// See if this type can be an array.
748//
749// Returns true if there is an error.
750//
Jamie Madill075edd82013-07-08 13:30:19 -0400751bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 //
754 // Can the type be an array?
755 //
756 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000757 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000758 return true;
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
764//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765// Enforce non-initializer type/qualifier rules.
766//
767// Returns true if there was an error.
768//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300769bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Olli Etuaho3739d232015-04-08 12:23:44 +0300771 ASSERT(type != nullptr);
772 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000773 {
774 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300775 type->qualifier = EvqTemporary;
776
777 // Generate informative error messages for ESSL1.
778 // In ESSL3 arrays and structures containing arrays can be constant.
779 if (shaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000780 {
781 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
782 }
783 else
784 {
785 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
786 }
787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000788 return true;
789 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300790 if (type->isUnsizedArray())
791 {
792 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
793 return true;
794 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
Olli Etuaho2935c582015-04-08 14:32:06 +0300798// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799// and update the symbol table.
800//
Olli Etuaho2935c582015-04-08 14:32:06 +0300801// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802//
Olli Etuaho2935c582015-04-08 14:32:06 +0300803bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
804 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805{
Olli Etuaho2935c582015-04-08 14:32:06 +0300806 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807
Olli Etuaho2935c582015-04-08 14:32:06 +0300808 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
Olli Etuaho2935c582015-04-08 14:32:06 +0300810 // gl_LastFragData may be redeclared with a new precision qualifier
811 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
812 {
813 const TVariable *maxDrawBuffers =
814 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion));
815 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
816 {
817 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion))
818 {
819 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
820 }
821 }
822 else
823 {
824 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
825 return false;
826 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
Olli Etuaho2935c582015-04-08 14:32:06 +0300829 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
Olli Etuaho2935c582015-04-08 14:32:06 +0300832 (*variable) = new TVariable(&identifier, type);
833 if (!symbolTable.declare(*variable))
834 {
835 error(line, "redefinition", identifier.c_str());
836 delete (*variable);
837 (*variable) = nullptr;
838 return false;
839 }
840
841 if (voidErrorCheck(line, identifier, type.getBasicType()))
842 return false;
843
844 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
Jamie Madill075edd82013-07-08 13:30:19 -0400847bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000850 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 return true;
852 }
853 if (qualifier == EvqConst && paramQualifier != EvqIn) {
854 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
855 return true;
856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000859 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000861 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000863 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864}
865
Jamie Madill075edd82013-07-08 13:30:19 -0400866bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000867{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000868 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000869 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
870 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000871 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 return true;
873 }
zmo@google.comf5450912011-09-09 01:37:19 +0000874 // In GLSL ES, an extension's default behavior is "disable".
875 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000876 error(line, "extension", extension.c_str(), "is disabled");
877 return true;
878 }
879 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000880 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000881 return false;
882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885}
886
Olli Etuahofa33d582015-04-09 14:33:12 +0300887// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
888// declaration.
889//
890bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400891{
Olli Etuahofa33d582015-04-09 14:33:12 +0300892 switch (publicType.qualifier)
893 {
894 case EvqVaryingIn:
895 case EvqVaryingOut:
896 case EvqAttribute:
897 case EvqVertexIn:
898 case EvqFragmentOut:
899 if (publicType.type == EbtStruct)
900 {
901 error(identifierLocation, "cannot be used with a structure",
902 getQualifierString(publicType.qualifier));
903 return true;
904 }
905
906 default: break;
907 }
908
909 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
910 "samplers must be uniform"))
911 {
Jamie Madilla5efff92013-06-06 11:56:47 -0400912 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +0300913 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400914
915 // check for layout qualifier issues
916 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
917
918 if (layoutQualifier.matrixPacking != EmpUnspecified)
919 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300920 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
921 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400922 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400923 }
924
925 if (layoutQualifier.blockStorage != EbsUnspecified)
926 {
Olli Etuahofa33d582015-04-09 14:33:12 +0300927 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
928 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400929 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400930 }
931
Olli Etuahofa33d582015-04-09 14:33:12 +0300932 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
933 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400934 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400935 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400936 }
937
938 return false;
939}
940
Jamie Madill075edd82013-07-08 13:30:19 -0400941bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400942{
943 if (layoutQualifier.location != -1)
944 {
945 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
946 return true;
947 }
948
949 return false;
950}
951
Olli Etuahob6e07a62015-02-16 12:22:10 +0200952bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
953{
954 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
955 {
956 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
957 if (qual == EvqOut || qual == EvqInOut)
958 {
959 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
960 if (lValueErrorCheck(node->getLine(), "assign", node))
961 {
962 error(node->getLine(),
963 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
964 recover();
965 return true;
966 }
967 }
968 }
969 return false;
970}
971
zmo@google.com09c323a2011-08-12 18:22:25 +0000972bool TParseContext::supportsExtension(const char* extension)
973{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000974 const TExtensionBehavior& extbehavior = extensionBehavior();
975 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
976 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000977}
978
Jamie Madill5d287f52013-07-12 15:38:19 -0400979bool TParseContext::isExtensionEnabled(const char* extension) const
980{
981 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400982 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400983
984 if (iter == extbehavior.end())
985 {
986 return false;
987 }
988
989 return (iter->second == EBhEnable || iter->second == EBhRequire);
990}
991
Jamie Madill075edd82013-07-08 13:30:19 -0400992void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
993{
994 pp::SourceLocation srcLoc;
995 srcLoc.file = loc.first_file;
996 srcLoc.line = loc.first_line;
997 directiveHandler.handleExtension(srcLoc, extName, behavior);
998}
999
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001000void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001001{
1002 pp::SourceLocation srcLoc;
1003 srcLoc.file = loc.first_file;
1004 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001005 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001006}
1007
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008/////////////////////////////////////////////////////////////////////////////////
1009//
1010// Non-Errors.
1011//
1012/////////////////////////////////////////////////////////////////////////////////
1013
Jamie Madill5c097022014-08-20 16:38:32 -04001014const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1015 const TString *name,
1016 const TSymbol *symbol)
1017{
1018 const TVariable *variable = NULL;
1019
1020 if (!symbol)
1021 {
1022 error(location, "undeclared identifier", name->c_str());
1023 recover();
1024 }
1025 else if (!symbol->isVariable())
1026 {
1027 error(location, "variable expected", name->c_str());
1028 recover();
1029 }
1030 else
1031 {
1032 variable = static_cast<const TVariable*>(symbol);
1033
1034 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1035 !variable->getExtension().empty() &&
1036 extensionErrorCheck(location, variable->getExtension()))
1037 {
1038 recover();
1039 }
1040 }
1041
1042 if (!variable)
1043 {
1044 TType type(EbtFloat, EbpUndefined);
1045 TVariable *fakeVariable = new TVariable(name, type);
1046 symbolTable.declare(fakeVariable);
1047 variable = fakeVariable;
1048 }
1049
1050 return variable;
1051}
1052
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053//
1054// Look up a function name in the symbol table, and make sure it is a function.
1055//
1056// Return the function symbol if found, otherwise 0.
1057//
Austin Kinross3ae64652015-01-26 15:51:39 -08001058const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001060 // First find by unmangled name to check whether the function name has been
1061 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001062 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001063 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001064 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001065 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067
alokp@chromium.org0a576182010-08-09 17:16:27 +00001068 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001069 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001070 return 0;
1071 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072
alokp@chromium.org0a576182010-08-09 17:16:27 +00001073 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001074 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001075 return 0;
1076 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001077
1078 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081//
1082// Initializers show up in several places in the grammar. Have one set of
1083// code to handle them here.
1084//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001085// Returns true on error, false if no error
1086//
Olli Etuaho2935c582015-04-08 14:32:06 +03001087bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001088 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001090 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
Olli Etuaho2935c582015-04-08 14:32:06 +03001093 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001094 if (type.isUnsizedArray())
1095 {
1096 type.setArraySize(initializer->getArraySize());
1097 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 if (!declareVariable(line, identifier, type, &variable))
1099 {
1100 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001103 //
1104 // identifier must be of type constant, a global, or a temporary
1105 //
1106 TQualifier qualifier = variable->getType().getQualifier();
1107 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001108 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001109 return true;
1110 }
1111 //
1112 // test for and propagate constant
1113 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001115 if (qualifier == EvqConst) {
1116 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001117 std::stringstream extraInfoStream;
1118 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1119 std::string extraInfo = extraInfoStream.str();
1120 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001121 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001122 return true;
1123 }
1124 if (type != initializer->getType()) {
1125 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001126 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001127 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001128 return true;
1129 }
1130 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001131 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001133 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001136 TConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001137 variable->shareConstPointer(constArray);
1138 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001139 std::stringstream extraInfoStream;
1140 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1141 std::string extraInfo = extraInfoStream.str();
1142 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001143 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001144 return true;
1145 }
1146 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001147
1148 if (qualifier != EvqConst)
1149 {
1150 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1151 variable->getType(), line);
1152 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1153 if (*intermNode == nullptr)
1154 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001155 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1156 return true;
1157 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001158 }
1159 else
1160 {
1161 *intermNode = nullptr;
1162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001164 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165}
1166
1167bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1168{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001169 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001170 if (!aggrNode->isConstructor())
1171 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001173 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001175 // check if all the child nodes are constants so that they can be inserted into
1176 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001177 TIntermSequence *sequence = aggrNode->getSequence() ;
1178 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001179 if (!(*p)->getAsTyped()->getAsConstantUnion())
1180 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001183 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
Olli Etuaho214c2d82015-04-27 14:49:13 +03001186TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
1187 const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001188{
1189 TPublicType returnType = typeSpecifier;
1190 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001191 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001192 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001193
1194 if (typeSpecifier.array)
1195 {
1196 error(typeSpecifier.line, "not supported", "first-class array");
1197 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001198 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001199 }
1200
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001201 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001202 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001203 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1204 {
1205 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1206 recover();
1207 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001208
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001209 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1210 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1211 {
1212 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1213 recover();
1214 }
1215 }
1216 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001217 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001218 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001219 {
Jamie Madill19571812013-08-12 15:26:34 -07001220 case EvqSmoothIn:
1221 case EvqSmoothOut:
1222 case EvqVertexOut:
1223 case EvqFragmentIn:
1224 case EvqCentroidOut:
1225 case EvqCentroidIn:
1226 if (typeSpecifier.type == EbtBool)
1227 {
1228 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1229 recover();
1230 }
1231 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1232 {
1233 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1234 recover();
1235 }
1236 break;
1237
1238 case EvqVertexIn:
1239 case EvqFragmentOut:
1240 case EvqFlatIn:
1241 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001242 if (typeSpecifier.type == EbtBool)
1243 {
1244 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1245 recover();
1246 }
1247 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001248
Jamie Madillb120eac2013-06-12 14:08:13 -04001249 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001250 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001251 }
1252
1253 return returnType;
1254}
1255
Olli Etuahofa33d582015-04-09 14:33:12 +03001256TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1257 const TSourceLoc &identifierOrTypeLocation,
1258 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001259{
Olli Etuahofa33d582015-04-09 14:33:12 +03001260 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001261
Olli Etuahobab4c082015-04-24 16:38:49 +03001262 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001263
Olli Etuahobab4c082015-04-24 16:38:49 +03001264 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1265
1266 if (emptyDeclaration)
1267 {
1268 if (publicType.isUnsizedArray())
1269 {
1270 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1271 // It is assumed that this applies to empty declarations as well.
1272 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1273 }
1274 }
1275 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001276 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001277 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001278 recover();
1279
Olli Etuaho376f1b52015-04-13 13:23:41 +03001280 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001281 recover();
1282
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001284 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001285 recover();
1286
1287 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001288 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001289 }
1290
Olli Etuahoe7847b02015-03-16 11:56:12 +02001291 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001292}
1293
Olli Etuahoe7847b02015-03-16 11:56:12 +02001294TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1295 const TSourceLoc &identifierLocation,
1296 const TString &identifier,
1297 const TSourceLoc &indexLocation,
1298 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001299{
Olli Etuahofa33d582015-04-09 14:33:12 +03001300 mDeferredSingleDeclarationErrorCheck = false;
1301
1302 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001303 recover();
1304
Olli Etuaho376f1b52015-04-13 13:23:41 +03001305 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001306 recover();
1307
1308 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1309 {
1310 recover();
1311 }
1312
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001313 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001314
1315 int size;
1316 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1317 {
1318 recover();
1319 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001320 // Make the type an array even if size check failed.
1321 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1322 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001323
Olli Etuaho2935c582015-04-08 14:32:06 +03001324 TVariable *variable = nullptr;
1325 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001326 recover();
1327
Olli Etuahoe7847b02015-03-16 11:56:12 +02001328 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001329 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001330 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001331
Olli Etuahoe7847b02015-03-16 11:56:12 +02001332 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001333}
1334
Olli Etuahoe7847b02015-03-16 11:56:12 +02001335TIntermAggregate *TParseContext::parseSingleInitDeclaration(TPublicType &publicType,
1336 const TSourceLoc &identifierLocation,
1337 const TString &identifier,
1338 const TSourceLoc &initLocation,
1339 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001340{
Olli Etuahofa33d582015-04-09 14:33:12 +03001341 mDeferredSingleDeclarationErrorCheck = false;
1342
1343 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001344 recover();
1345
Olli Etuahoe7847b02015-03-16 11:56:12 +02001346 TIntermNode *intermNode = nullptr;
1347 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001348 {
1349 //
1350 // Build intermediate representation
1351 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001352 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001353 }
1354 else
1355 {
1356 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001357 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001358 }
1359}
1360
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001361TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1362 const TSourceLoc &identifierLocation,
1363 const TString &identifier,
1364 const TSourceLoc &indexLocation,
1365 TIntermTyped *indexExpression,
1366 const TSourceLoc &initLocation,
1367 TIntermTyped *initializer)
1368{
1369 mDeferredSingleDeclarationErrorCheck = false;
1370
1371 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1372 recover();
1373
1374 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1375 {
1376 recover();
1377 }
1378
1379 TPublicType arrayType(publicType);
1380
Olli Etuaho376f1b52015-04-13 13:23:41 +03001381 int size = 0;
1382 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1383 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001384 {
1385 recover();
1386 }
1387 // Make the type an array even if size check failed.
1388 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1389 arrayType.setArraySize(size);
1390
1391 // initNode will correspond to the whole of "type b[n] = initializer".
1392 TIntermNode *initNode = nullptr;
1393 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1394 {
1395 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1396 }
1397 else
1398 {
1399 recover();
1400 return nullptr;
1401 }
1402}
1403
Olli Etuahoe7847b02015-03-16 11:56:12 +02001404TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001405 const TSourceLoc &identifierLoc,
1406 const TString *identifier,
1407 const TSymbol *symbol)
1408{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001409 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001410 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1411 {
1412 recover();
1413 }
1414
1415 if (!symbol)
1416 {
1417 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1418 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001419 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001420 }
1421 else
1422 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001423 const TString kGlFrontFacing("gl_FrontFacing");
1424 if (*identifier == kGlFrontFacing)
1425 {
1426 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1427 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001428 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001429 }
Jamie Madill2c433252014-12-03 12:36:54 -05001430 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001431 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1432 ASSERT(variable);
1433 const TType &type = variable->getType();
1434 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1435 *identifier, type, identifierLoc);
1436
1437 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1438 aggregate->setOp(EOpInvariantDeclaration);
1439 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001440 }
1441}
1442
Olli Etuahoe7847b02015-03-16 11:56:12 +02001443TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1444 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001445{
Olli Etuahofa33d582015-04-09 14:33:12 +03001446 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1447 if (mDeferredSingleDeclarationErrorCheck)
1448 {
1449 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1450 recover();
1451 mDeferredSingleDeclarationErrorCheck = false;
1452 }
1453
Jamie Madill0bd18df2013-06-20 11:55:52 -04001454 if (locationDeclaratorListCheck(identifierLocation, publicType))
1455 recover();
1456
Olli Etuaho376f1b52015-04-13 13:23:41 +03001457 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001458 recover();
1459
Olli Etuaho2935c582015-04-08 14:32:06 +03001460 TVariable *variable = nullptr;
1461 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001462 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001463
1464 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1465 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001466 symbol->setId(variable->getUniqueId());
1467
Olli Etuahoe7847b02015-03-16 11:56:12 +02001468 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001469}
1470
Olli Etuahoe7847b02015-03-16 11:56:12 +02001471TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1472 const TSourceLoc &identifierLocation, const TString &identifier,
1473 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001474{
Olli Etuahofa33d582015-04-09 14:33:12 +03001475 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1476 if (mDeferredSingleDeclarationErrorCheck)
1477 {
1478 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1479 recover();
1480 mDeferredSingleDeclarationErrorCheck = false;
1481 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001482
Jamie Madill0bd18df2013-06-20 11:55:52 -04001483 if (locationDeclaratorListCheck(identifierLocation, publicType))
1484 recover();
1485
Olli Etuaho376f1b52015-04-13 13:23:41 +03001486 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001487 recover();
1488
1489 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1490 {
1491 recover();
1492 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001493 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001494 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001495 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001496 int size;
1497 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001498 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001499 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001500 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001501 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001502
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001503 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001504 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001505 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001506
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001507 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1508 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001509 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001510
1511 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001512 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001513
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001514 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001515}
1516
Olli Etuahoe7847b02015-03-16 11:56:12 +02001517TIntermAggregate *TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1518 const TSourceLoc &identifierLocation, const TString &identifier,
1519 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001520{
Olli Etuahofa33d582015-04-09 14:33:12 +03001521 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1522 if (mDeferredSingleDeclarationErrorCheck)
1523 {
1524 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1525 recover();
1526 mDeferredSingleDeclarationErrorCheck = false;
1527 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001528
Jamie Madill0bd18df2013-06-20 11:55:52 -04001529 if (locationDeclaratorListCheck(identifierLocation, publicType))
1530 recover();
1531
Olli Etuahoe7847b02015-03-16 11:56:12 +02001532 TIntermNode *intermNode = nullptr;
1533 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001534 {
1535 //
1536 // build the intermediate representation
1537 //
1538 if (intermNode)
1539 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001540 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001541 }
1542 else
1543 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001544 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001545 }
1546 }
1547 else
1548 {
1549 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001550 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001551 }
1552}
1553
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001554TIntermAggregate *TParseContext::parseArrayInitDeclarator(TPublicType &publicType,
1555 TIntermAggregate *aggregateDeclaration,
1556 const TSourceLoc& identifierLocation,
1557 const TString &identifier,
1558 const TSourceLoc& indexLocation,
1559 TIntermTyped *indexExpression,
1560 const TSourceLoc &initLocation, TIntermTyped *initializer)
1561{
1562 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1563 if (mDeferredSingleDeclarationErrorCheck)
1564 {
1565 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1566 recover();
1567 mDeferredSingleDeclarationErrorCheck = false;
1568 }
1569
1570 if (locationDeclaratorListCheck(identifierLocation, publicType))
1571 recover();
1572
1573 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1574 {
1575 recover();
1576 }
1577
1578 TPublicType arrayType(publicType);
1579
Olli Etuaho376f1b52015-04-13 13:23:41 +03001580 int size = 0;
1581 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1582 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001583 {
1584 recover();
1585 }
1586 // Make the type an array even if size check failed.
1587 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1588 arrayType.setArraySize(size);
1589
1590 // initNode will correspond to the whole of "b[n] = initializer".
1591 TIntermNode *initNode = nullptr;
1592 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1593 {
1594 if (initNode)
1595 {
1596 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1597 }
1598 else
1599 {
1600 return aggregateDeclaration;
1601 }
1602 }
1603 else
1604 {
1605 recover();
1606 return nullptr;
1607 }
1608}
1609
Jamie Madilla295edf2013-06-06 11:56:48 -04001610void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1611{
1612 if (typeQualifier.qualifier != EvqUniform)
1613 {
1614 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1615 recover();
1616 return;
1617 }
1618
1619 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1620 ASSERT(!layoutQualifier.isEmpty());
1621
1622 if (shaderVersion < 300)
1623 {
1624 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1625 recover();
1626 return;
1627 }
1628
1629 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1630 {
1631 recover();
1632 return;
1633 }
1634
Jamie Madill099c0f32013-06-20 11:55:52 -04001635 if (layoutQualifier.matrixPacking != EmpUnspecified)
1636 {
1637 defaultMatrixPacking = layoutQualifier.matrixPacking;
1638 }
1639
Geoff Langc6856732014-02-11 09:38:55 -05001640 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001641 {
1642 defaultBlockStorage = layoutQualifier.blockStorage;
1643 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001644}
1645
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001646TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1647{
1648 TOperator op = EOpNull;
1649 if (publicType.userDef)
1650 {
1651 op = EOpConstructStruct;
1652 }
1653 else
1654 {
1655 switch (publicType.type)
1656 {
1657 case EbtFloat:
1658 if (publicType.isMatrix())
1659 {
1660 // TODO: non-square matrices
1661 switch(publicType.getCols())
1662 {
Jamie Madill28b97422013-07-08 14:01:38 -04001663 case 2: op = EOpConstructMat2; break;
1664 case 3: op = EOpConstructMat3; break;
1665 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001666 }
1667 }
1668 else
1669 {
1670 switch(publicType.getNominalSize())
1671 {
Jamie Madill28b97422013-07-08 14:01:38 -04001672 case 1: op = EOpConstructFloat; break;
1673 case 2: op = EOpConstructVec2; break;
1674 case 3: op = EOpConstructVec3; break;
1675 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001676 }
1677 }
1678 break;
1679
1680 case EbtInt:
1681 switch(publicType.getNominalSize())
1682 {
Jamie Madill28b97422013-07-08 14:01:38 -04001683 case 1: op = EOpConstructInt; break;
1684 case 2: op = EOpConstructIVec2; break;
1685 case 3: op = EOpConstructIVec3; break;
1686 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001687 }
1688 break;
1689
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001690 case EbtUInt:
1691 switch(publicType.getNominalSize())
1692 {
Jamie Madill28b97422013-07-08 14:01:38 -04001693 case 1: op = EOpConstructUInt; break;
1694 case 2: op = EOpConstructUVec2; break;
1695 case 3: op = EOpConstructUVec3; break;
1696 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001697 }
1698 break;
1699
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001700 case EbtBool:
1701 switch(publicType.getNominalSize())
1702 {
Jamie Madill28b97422013-07-08 14:01:38 -04001703 case 1: op = EOpConstructBool; break;
1704 case 2: op = EOpConstructBVec2; break;
1705 case 3: op = EOpConstructBVec3; break;
1706 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001707 }
1708 break;
1709
1710 default: break;
1711 }
1712
1713 if (op == EOpNull)
1714 {
1715 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1716 recover();
1717 publicType.type = EbtFloat;
1718 op = EOpConstructFloat;
1719 }
1720 }
1721
1722 TString tempString;
1723 TType type(publicType);
1724 return new TFunction(&tempString, type, op);
1725}
1726
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727// This function is used to test for the correctness of the parameters passed to various constructor functions
1728// and also convert them to the right datatype if it is allowed and required.
1729//
1730// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1731//
Olli Etuaho21203702014-11-13 16:16:21 +02001732TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001734 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001736 if (!aggregateArguments)
1737 {
1738 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001739 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001740 }
1741
Olli Etuahof40319e2015-03-10 14:33:00 +02001742 if (type->isArray())
1743 {
1744 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1745 TIntermSequence *args = aggregateArguments->getSequence();
1746 for (size_t i = 0; i < args->size(); i++)
1747 {
1748 const TType &argType = (*args)[i]->getAsTyped()->getType();
1749 // It has already been checked that the argument is not an array.
1750 ASSERT(!argType.isArray());
1751 if (!argType.sameElementType(*type))
1752 {
1753 error(line, "Array constructor argument has an incorrect type", "Error");
1754 recover();
1755 return nullptr;
1756 }
1757 }
1758 }
1759 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001760 {
1761 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001762 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001764 for (size_t i = 0; i < fields.size(); i++)
1765 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001766 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001767 {
1768 error(line, "Structure constructor arguments do not match structure fields", "Error");
1769 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001771 return 0;
1772 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001773 }
1774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001776 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001777 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1778 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001779 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001780 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001781 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001782 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783
Olli Etuaho21203702014-11-13 16:16:21 +02001784 // Structs should not be precision qualified, the individual members may be.
1785 // Built-in types on the other hand should be precision qualified.
1786 if (op != EOpConstructStruct)
1787 {
1788 constructor->setPrecisionFromChildren();
1789 type->setPrecision(constructor->getPrecision());
1790 }
1791
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001792 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793}
1794
1795TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1796{
Olli Etuahof40319e2015-03-10 14:33:00 +02001797 // TODO: Add support for folding array constructors
1798 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001799 aggrNode->setType(type);
1800 if (canBeFolded) {
1801 bool returnVal = false;
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001802 TConstantUnion* unionArray = new TConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001803 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001804 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001805 }
1806 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001807 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001808 }
1809 if (returnVal)
1810 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001812 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001815 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001816}
1817
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818//
1819// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1820// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1821// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1822// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1823// a constant matrix.
1824//
Jamie Madill075edd82013-07-08 13:30:19 -04001825TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 TIntermTyped* typedNode;
1828 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829
Jamie Madillb11e2482015-05-04 14:21:22 -04001830 const TConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 if (tempConstantNode) {
1832 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001834 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001835 return node;
1836 }
1837 } 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 +00001838 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001839 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 return 0;
1842 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001844 TConstantUnion* constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001846 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001847 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001848 std::stringstream extraInfoStream;
1849 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1850 std::string extraInfo = extraInfoStream.str();
1851 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001852 recover();
1853 fields.offsets[i] = 0;
1854 }
1855
1856 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001858 }
1859 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1860 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861}
1862
1863//
1864// This function returns the column being accessed from a constant matrix. The values are retrieved from
1865// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1866// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1867// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1868//
Jamie Madill075edd82013-07-08 13:30:19 -04001869TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001871 TIntermTyped* typedNode;
1872 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001874 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001875 std::stringstream extraInfoStream;
1876 extraInfoStream << "matrix field selection out of range '" << index << "'";
1877 std::string extraInfo = extraInfoStream.str();
1878 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001879 recover();
1880 index = 0;
1881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001883 if (tempConstantNode) {
Jamie Madillb11e2482015-05-04 14:21:22 -04001884 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001885 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001886 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1887 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001888 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001889 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001891 return 0;
1892 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001894 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895}
1896
1897
1898//
1899// This function returns an element of an array accessed from a constant array. The values are retrieved from
1900// the symbol table and parse-tree is built for the type of the element. The input
1901// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1902// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1903//
Jamie Madill075edd82013-07-08 13:30:19 -04001904TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001906 TIntermTyped* typedNode;
1907 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1908 TType arrayElementType = node->getType();
1909 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001911 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001912 std::stringstream extraInfoStream;
1913 extraInfoStream << "array field selection out of range '" << index << "'";
1914 std::string extraInfo = extraInfoStream.str();
1915 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001916 recover();
1917 index = 0;
1918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001920 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001921 size_t arrayElementSize = arrayElementType.getObjectSize();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001922 TConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001923 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001924 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001925 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001926 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001928 return 0;
1929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001931 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932}
1933
1934
1935//
1936// This function returns the value of a particular field inside a constant structure from the symbol table.
1937// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1938// function and returns the parse-tree with the values of the embedded/nested struct.
1939//
Jamie Madill075edd82013-07-08 13:30:19 -04001940TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941{
Jamie Madill98493dd2013-07-08 14:39:03 -04001942 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001943 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
Jamie Madill98493dd2013-07-08 14:39:03 -04001945 for (size_t index = 0; index < fields.size(); ++index) {
1946 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001947 break;
1948 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001949 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 }
1951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952
Jamie Madill94bf7f22013-07-08 13:31:15 -04001953 TIntermTyped *typedNode;
1954 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001955 if (tempConstantNode) {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04001956 TConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1959 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001960 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001961 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001963 return 0;
1964 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967}
1968
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001969//
1970// Interface/uniform blocks
1971//
Jamie Madill98493dd2013-07-08 14:39:03 -04001972TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1973 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001974{
1975 if (reservedErrorCheck(nameLine, blockName))
1976 recover();
1977
1978 if (typeQualifier.qualifier != EvqUniform)
1979 {
1980 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1981 recover();
1982 }
1983
Jamie Madill099c0f32013-06-20 11:55:52 -04001984 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1985 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001986 {
1987 recover();
1988 }
1989
Jamie Madill099c0f32013-06-20 11:55:52 -04001990 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1991 {
1992 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1993 }
1994
Jamie Madill1566ef72013-06-20 11:55:54 -04001995 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1996 {
1997 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1998 }
1999
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002000 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002001 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002002 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2003 recover();
2004 }
2005
Jamie Madill98493dd2013-07-08 14:39:03 -04002006 // check for sampler types and apply layout qualifiers
2007 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
2008 TField* field = (*fieldList)[memberIndex];
2009 TType* fieldType = field->type();
2010 if (IsSampler(fieldType->getBasicType())) {
2011 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002012 recover();
2013 }
2014
Jamie Madill98493dd2013-07-08 14:39:03 -04002015 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002016 switch (qualifier)
2017 {
2018 case EvqGlobal:
2019 case EvqUniform:
2020 break;
2021 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002022 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002023 recover();
2024 break;
2025 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002026
2027 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002028 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2029 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002030 {
2031 recover();
2032 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002033
Jamie Madill98493dd2013-07-08 14:39:03 -04002034 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002035 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002036 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002037 recover();
2038 }
2039
Jamie Madill98493dd2013-07-08 14:39:03 -04002040 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002041 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002042 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002043 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002044 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002045 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002046 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002047 recover();
2048 }
2049
Jamie Madill98493dd2013-07-08 14:39:03 -04002050 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002051 }
2052
Jamie Madill98493dd2013-07-08 14:39:03 -04002053 // add array index
2054 int arraySize = 0;
2055 if (arrayIndex != NULL)
2056 {
2057 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2058 recover();
2059 }
2060
2061 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2062 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002063
2064 TString symbolName = "";
2065 int symbolId = 0;
2066
Jamie Madill98493dd2013-07-08 14:39:03 -04002067 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002068 {
2069 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002070 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2071 {
2072 TField* field = (*fieldList)[memberIndex];
2073 TType* fieldType = field->type();
2074
2075 // set parent pointer of the field variable
2076 fieldType->setInterfaceBlock(interfaceBlock);
2077
2078 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
2079 fieldVariable->setQualifier(typeQualifier.qualifier);
2080
Nicolas Capensadfffe42014-06-17 02:13:36 -04002081 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002082 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002083 recover();
2084 }
2085 }
2086 }
2087 else
2088 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002089 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002090 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002091 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002092
Nicolas Capensadfffe42014-06-17 02:13:36 -04002093 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002094 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002095 recover();
2096 }
2097
2098 symbolId = instanceTypeDef->getUniqueId();
2099 symbolName = instanceTypeDef->getName();
2100 }
2101
Jamie Madill98493dd2013-07-08 14:39:03 -04002102 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002103 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002104
2105 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002106 return aggregate;
2107}
2108
Jamie Madill075edd82013-07-08 13:30:19 -04002109bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002110{
2111 ++structNestingLevel;
2112
2113 // Embedded structure definitions are not supported per GLSL ES spec.
2114 // They aren't allowed in GLSL either, but we need to detect this here
2115 // so we don't rely on the GLSL compiler to catch it.
2116 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002117 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002118 return true;
2119 }
2120
2121 return false;
2122}
2123
2124void TParseContext::exitStructDeclaration()
2125{
2126 --structNestingLevel;
2127}
2128
2129namespace {
2130
2131const int kWebGLMaxStructNesting = 4;
2132
2133} // namespace
2134
Jamie Madill98493dd2013-07-08 14:39:03 -04002135bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002136{
Jamie Madill5508f392014-02-20 13:31:36 -05002137 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002138 return false;
2139 }
2140
Jamie Madill98493dd2013-07-08 14:39:03 -04002141 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002142 return false;
2143 }
2144
2145 // We're already inside a structure definition at this point, so add
2146 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002147 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002148 std::stringstream reasonStream;
2149 reasonStream << "Reference of struct type "
2150 << field.type()->getStruct()->name().c_str()
2151 << " exceeds maximum allowed nesting level of "
2152 << kWebGLMaxStructNesting;
2153 std::string reason = reasonStream.str();
2154 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002155 return true;
2156 }
2157
2158 return false;
2159}
2160
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002161//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002162// Parse an array index expression
2163//
Jamie Madill075edd82013-07-08 13:30:19 -04002164TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002165{
2166 TIntermTyped *indexedExpression = NULL;
2167
2168 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2169 {
2170 if (baseExpression->getAsSymbolNode())
2171 {
2172 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2173 }
2174 else
2175 {
2176 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2177 }
2178 recover();
2179 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002180
Jamie Madill21c1e452014-12-29 11:33:41 -05002181 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2182
2183 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002184 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002185 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002186 if (index < 0)
2187 {
2188 std::stringstream infoStream;
2189 infoStream << index;
2190 std::string info = infoStream.str();
2191 error(location, "negative index", info.c_str());
2192 recover();
2193 index = 0;
2194 }
2195 if (baseExpression->getType().getQualifier() == EvqConst)
2196 {
2197 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002198 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002199 // constant folding for arrays
2200 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002201 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002202 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002203 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002204 // constant folding for vectors
2205 TVectorFields fields;
2206 fields.num = 1;
2207 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2208 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2209 }
2210 else if (baseExpression->isMatrix())
2211 {
2212 // constant folding for matrices
2213 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002214 }
2215 }
2216 else
2217 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002218 int safeIndex = -1;
2219
Jamie Madill7164cf42013-07-08 13:30:59 -04002220 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002221 {
Jamie Madill18464b52013-07-08 14:01:55 -04002222 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002223 {
2224 std::stringstream extraInfoStream;
2225 extraInfoStream << "array index out of range '" << index << "'";
2226 std::string extraInfo = extraInfoStream.str();
2227 error(location, "", "[", extraInfo.c_str());
2228 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002229 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002230 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002231 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2232 {
2233 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2234 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002235 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002236 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002237 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002238 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002239 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002240 std::stringstream extraInfoStream;
2241 extraInfoStream << "field selection out of range '" << index << "'";
2242 std::string extraInfo = extraInfoStream.str();
2243 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002244 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002245 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002246 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002247
Jamie Madillb11e2482015-05-04 14:21:22 -04002248 // Don't modify the data of the previous constant union, because it can point
2249 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2250 if (safeIndex != -1)
2251 {
2252 TConstantUnion *safeConstantUnion = new TConstantUnion();
2253 safeConstantUnion->setIConst(safeIndex);
2254 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2255 }
2256
Jamie Madill7164cf42013-07-08 13:30:59 -04002257 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002258 }
2259 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002260 else
2261 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002262 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002263 {
Jamie Madill19571812013-08-12 15:26:34 -07002264 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002265 recover();
2266 }
Jamie Madill19571812013-08-12 15:26:34 -07002267 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002268 {
Jamie Madill19571812013-08-12 15:26:34 -07002269 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002270 recover();
2271 }
2272
2273 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2274 }
2275
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002276 if (indexedExpression == 0)
2277 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002278 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002279 unionArray->setFConst(0.0f);
2280 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2281 }
2282 else if (baseExpression->isArray())
2283 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002284 const TType &baseType = baseExpression->getType();
2285 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002286 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002287 TType copyOfType(baseType.getStruct());
2288 indexedExpression->setType(copyOfType);
2289 }
2290 else if (baseType.isInterfaceBlock())
2291 {
2292 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002293 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002294 }
2295 else
2296 {
Minmin Gong794e0002015-04-07 18:31:54 -07002297 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2298 static_cast<unsigned char>(baseExpression->getNominalSize()), static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002299 }
2300
2301 if (baseExpression->getType().getQualifier() == EvqConst)
2302 {
2303 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2304 }
2305 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002306 else if (baseExpression->isMatrix())
2307 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002308 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Minmin Gong794e0002015-04-07 18:31:54 -07002309 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002310 }
2311 else if (baseExpression->isVector())
2312 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002313 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2314 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002315 }
2316 else
2317 {
2318 indexedExpression->setType(baseExpression->getType());
2319 }
2320
2321 return indexedExpression;
2322}
2323
Jamie Madill075edd82013-07-08 13:30:19 -04002324TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002325{
2326 TIntermTyped *indexedExpression = NULL;
2327
2328 if (baseExpression->isArray())
2329 {
2330 error(fieldLocation, "cannot apply dot operator to an array", ".");
2331 recover();
2332 }
2333
2334 if (baseExpression->isVector())
2335 {
2336 TVectorFields fields;
2337 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2338 {
2339 fields.num = 1;
2340 fields.offsets[0] = 0;
2341 recover();
2342 }
2343
2344 if (baseExpression->getType().getQualifier() == EvqConst)
2345 {
2346 // constant folding for vector fields
2347 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2348 if (indexedExpression == 0)
2349 {
2350 recover();
2351 indexedExpression = baseExpression;
2352 }
2353 else
2354 {
Minmin Gong794e0002015-04-07 18:31:54 -07002355 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002356 }
2357 }
2358 else
2359 {
2360 TString vectorString = fieldString;
2361 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2362 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002363 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002364 }
2365 }
2366 else if (baseExpression->isMatrix())
2367 {
2368 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002369 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 {
2371 fields.wholeRow = false;
2372 fields.wholeCol = false;
2373 fields.row = 0;
2374 fields.col = 0;
2375 recover();
2376 }
2377
2378 if (fields.wholeRow || fields.wholeCol)
2379 {
2380 error(dotLocation, " non-scalar fields not implemented yet", ".");
2381 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002382 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002383 unionArray->setIConst(0);
2384 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2385 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Minmin Gong794e0002015-04-07 18:31:54 -07002386 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary,
2387 static_cast<unsigned char>(baseExpression->getCols()), static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002388 }
2389 else
2390 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002391 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002392 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002393 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2394 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2395 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2396 }
2397 }
2398 else if (baseExpression->getBasicType() == EbtStruct)
2399 {
2400 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002401 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2402 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002403 {
2404 error(dotLocation, "structure has no fields", "Internal Error");
2405 recover();
2406 indexedExpression = baseExpression;
2407 }
2408 else
2409 {
2410 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002411 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002412 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002413 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002414 {
2415 fieldFound = true;
2416 break;
2417 }
2418 }
2419 if (fieldFound)
2420 {
2421 if (baseExpression->getType().getQualifier() == EvqConst)
2422 {
2423 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2424 if (indexedExpression == 0)
2425 {
2426 recover();
2427 indexedExpression = baseExpression;
2428 }
2429 else
2430 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002431 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002432 // change the qualifier of the return type, not of the structure field
2433 // as the structure definition is shared between various structures.
2434 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2435 }
2436 }
2437 else
2438 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002439 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002440 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002441 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002442 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002443 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002444 }
2445 }
2446 else
2447 {
2448 error(dotLocation, " no such field in structure", fieldString.c_str());
2449 recover();
2450 indexedExpression = baseExpression;
2451 }
2452 }
2453 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002454 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455 {
2456 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002457 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2458 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002459 {
2460 error(dotLocation, "interface block has no fields", "Internal Error");
2461 recover();
2462 indexedExpression = baseExpression;
2463 }
2464 else
2465 {
2466 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002467 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002468 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002469 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002470 {
2471 fieldFound = true;
2472 break;
2473 }
2474 }
2475 if (fieldFound)
2476 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002477 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002478 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002479 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002480 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002481 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002482 }
2483 else
2484 {
2485 error(dotLocation, " no such field in interface block", fieldString.c_str());
2486 recover();
2487 indexedExpression = baseExpression;
2488 }
2489 }
2490 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002491 else
2492 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002493 if (shaderVersion < 300)
2494 {
2495 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2496 }
2497 else
2498 {
2499 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2500 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002501 recover();
2502 indexedExpression = baseExpression;
2503 }
2504
2505 return indexedExpression;
2506}
2507
Jamie Madill075edd82013-07-08 13:30:19 -04002508TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002509{
Jamie Madilla5efff92013-06-06 11:56:47 -04002510 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002511
Jamie Madilla5efff92013-06-06 11:56:47 -04002512 qualifier.location = -1;
2513 qualifier.matrixPacking = EmpUnspecified;
2514 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002515
2516 if (qualifierType == "shared")
2517 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002518 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002519 }
2520 else if (qualifierType == "packed")
2521 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002522 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002523 }
2524 else if (qualifierType == "std140")
2525 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002526 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002527 }
2528 else if (qualifierType == "row_major")
2529 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002530 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002531 }
2532 else if (qualifierType == "column_major")
2533 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002534 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002535 }
2536 else if (qualifierType == "location")
2537 {
2538 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2539 recover();
2540 }
2541 else
2542 {
2543 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2544 recover();
2545 }
2546
Jamie Madilla5efff92013-06-06 11:56:47 -04002547 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002548}
2549
Jamie Madill075edd82013-07-08 13:30:19 -04002550TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002551{
Jamie Madilla5efff92013-06-06 11:56:47 -04002552 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002553
Jamie Madilla5efff92013-06-06 11:56:47 -04002554 qualifier.location = -1;
2555 qualifier.matrixPacking = EmpUnspecified;
2556 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002557
2558 if (qualifierType != "location")
2559 {
2560 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2561 recover();
2562 }
2563 else
2564 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002565 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002566 if (intValue < 0)
2567 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002568 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002569 recover();
2570 }
2571 else
2572 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002573 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002574 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002575 }
2576
Jamie Madilla5efff92013-06-06 11:56:47 -04002577 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002578}
2579
Jamie Madilla5efff92013-06-06 11:56:47 -04002580TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002581{
Jamie Madilla5efff92013-06-06 11:56:47 -04002582 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002583
Jamie Madilla5efff92013-06-06 11:56:47 -04002584 if (rightQualifier.location != -1)
2585 {
2586 joinedQualifier.location = rightQualifier.location;
2587 }
2588 if (rightQualifier.matrixPacking != EmpUnspecified)
2589 {
2590 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2591 }
2592 if (rightQualifier.blockStorage != EbsUnspecified)
2593 {
2594 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2595 }
2596
2597 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002598}
2599
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002600TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2601 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2602{
2603 TQualifier mergedQualifier = EvqSmoothIn;
2604
Jamie Madill19571812013-08-12 15:26:34 -07002605 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002606 if (interpolationQualifier == EvqSmooth)
2607 mergedQualifier = EvqSmoothIn;
2608 else if (interpolationQualifier == EvqFlat)
2609 mergedQualifier = EvqFlatIn;
2610 else UNREACHABLE();
2611 }
2612 else if (storageQualifier == EvqCentroidIn) {
2613 if (interpolationQualifier == EvqSmooth)
2614 mergedQualifier = EvqCentroidIn;
2615 else if (interpolationQualifier == EvqFlat)
2616 mergedQualifier = EvqFlatIn;
2617 else UNREACHABLE();
2618 }
Jamie Madill19571812013-08-12 15:26:34 -07002619 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002620 if (interpolationQualifier == EvqSmooth)
2621 mergedQualifier = EvqSmoothOut;
2622 else if (interpolationQualifier == EvqFlat)
2623 mergedQualifier = EvqFlatOut;
2624 else UNREACHABLE();
2625 }
2626 else if (storageQualifier == EvqCentroidOut) {
2627 if (interpolationQualifier == EvqSmooth)
2628 mergedQualifier = EvqCentroidOut;
2629 else if (interpolationQualifier == EvqFlat)
2630 mergedQualifier = EvqFlatOut;
2631 else UNREACHABLE();
2632 }
2633 else {
2634 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2635 recover();
2636
2637 mergedQualifier = storageQualifier;
2638 }
2639
2640 TPublicType type;
2641 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2642 return type;
2643}
2644
Jamie Madill98493dd2013-07-08 14:39:03 -04002645TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002646{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002647 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2648 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002649 recover();
2650 }
2651
Jamie Madill98493dd2013-07-08 14:39:03 -04002652 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002653 //
2654 // Careful not to replace already known aspects of type, like array-ness
2655 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002656 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002657 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002658 type->setPrimarySize(typeSpecifier.primarySize);
2659 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002660 type->setPrecision(typeSpecifier.precision);
2661 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002662 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002663
2664 // don't allow arrays of arrays
2665 if (type->isArray()) {
2666 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2667 recover();
2668 }
2669 if (typeSpecifier.array)
2670 type->setArraySize(typeSpecifier.arraySize);
2671 if (typeSpecifier.userDef) {
2672 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002673 }
2674
Jamie Madill98493dd2013-07-08 14:39:03 -04002675 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002676 recover();
2677 }
2678 }
2679
Jamie Madill98493dd2013-07-08 14:39:03 -04002680 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002681}
2682
Jamie Madill98493dd2013-07-08 14:39:03 -04002683TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002684{
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 TStructure* structure = new TStructure(structName, fieldList);
2686 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002687
Jamie Madill9b820842015-02-12 10:40:10 -05002688 // Store a bool in the struct if we're at global scope, to allow us to
2689 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002690 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002691 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002692
Jamie Madill98493dd2013-07-08 14:39:03 -04002693 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002694 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002695 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002696 {
2697 recover();
2698 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002699 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002700 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002701 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002702 recover();
2703 }
2704 }
2705
2706 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002707 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002709 const TField &field = *(*fieldList)[typeListIndex];
2710 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002711 switch (qualifier)
2712 {
2713 case EvqGlobal:
2714 case EvqTemporary:
2715 break;
2716 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002717 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002718 recover();
2719 break;
2720 }
2721 }
2722
2723 TPublicType publicType;
2724 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002725 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002726 exitStructDeclaration();
2727
2728 return publicType;
2729}
2730
Olli Etuahoa3a36662015-02-17 13:46:51 +02002731TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2732{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002733 TBasicType switchType = init->getBasicType();
2734 if ((switchType != EbtInt && switchType != EbtUInt) ||
2735 init->isMatrix() ||
2736 init->isArray() ||
2737 init->isVector())
2738 {
2739 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2740 recover();
2741 return nullptr;
2742 }
2743
Olli Etuahoac5274d2015-02-20 10:19:08 +02002744 if (statementList)
2745 {
2746 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2747 {
2748 recover();
2749 return nullptr;
2750 }
2751 }
2752
Olli Etuahoa3a36662015-02-17 13:46:51 +02002753 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2754 if (node == nullptr)
2755 {
2756 error(loc, "erroneous switch statement", "switch");
2757 recover();
2758 return nullptr;
2759 }
2760 return node;
2761}
2762
2763TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2764{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002765 if (mSwitchNestingLevel == 0)
2766 {
2767 error(loc, "case labels need to be inside switch statements", "case");
2768 recover();
2769 return nullptr;
2770 }
2771 if (condition == nullptr)
2772 {
2773 error(loc, "case label must have a condition", "case");
2774 recover();
2775 return nullptr;
2776 }
2777 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2778 condition->isMatrix() ||
2779 condition->isArray() ||
2780 condition->isVector())
2781 {
2782 error(condition->getLine(), "case label must be a scalar integer", "case");
2783 recover();
2784 }
2785 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2786 if (conditionConst == nullptr)
2787 {
2788 error(condition->getLine(), "case label must be constant", "case");
2789 recover();
2790 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002791 TIntermCase *node = intermediate.addCase(condition, loc);
2792 if (node == nullptr)
2793 {
2794 error(loc, "erroneous case statement", "case");
2795 recover();
2796 return nullptr;
2797 }
2798 return node;
2799}
2800
2801TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2802{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002803 if (mSwitchNestingLevel == 0)
2804 {
2805 error(loc, "default labels need to be inside switch statements", "default");
2806 recover();
2807 return nullptr;
2808 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002809 TIntermCase *node = intermediate.addCase(nullptr, loc);
2810 if (node == nullptr)
2811 {
2812 error(loc, "erroneous default statement", "default");
2813 recover();
2814 return nullptr;
2815 }
2816 return node;
2817}
2818
Olli Etuahof6c694b2015-03-26 14:50:53 +02002819TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
2820 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02002821{
2822 if (child == nullptr)
2823 {
2824 return nullptr;
2825 }
2826
2827 switch (op)
2828 {
2829 case EOpLogicalNot:
2830 if (child->getBasicType() != EbtBool ||
2831 child->isMatrix() ||
2832 child->isArray() ||
2833 child->isVector())
2834 {
2835 return nullptr;
2836 }
2837 break;
2838 case EOpBitwiseNot:
2839 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
2840 child->isMatrix() ||
2841 child->isArray())
2842 {
2843 return nullptr;
2844 }
2845 break;
2846 case EOpPostIncrement:
2847 case EOpPreIncrement:
2848 case EOpPostDecrement:
2849 case EOpPreDecrement:
2850 case EOpNegative:
2851 case EOpPositive:
2852 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02002853 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02002854 child->isArray())
2855 {
2856 return nullptr;
2857 }
Olli Etuahodca3e792015-03-26 13:24:04 +02002858 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02002859 default:
2860 break;
2861 }
2862
Olli Etuahof6c694b2015-03-26 14:50:53 +02002863 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002864}
2865
Olli Etuaho09b22472015-02-11 11:47:26 +02002866TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2867{
Olli Etuahof6c694b2015-03-26 14:50:53 +02002868 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02002869 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02002870 {
2871 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2872 recover();
2873 return child;
2874 }
2875 return node;
2876}
2877
2878TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2879{
2880 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2881 recover();
2882 return addUnaryMath(op, child, loc);
2883}
2884
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002885bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02002886 const TSourceLoc &loc)
2887{
2888 if (left->isArray() || right->isArray())
2889 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02002890 if (shaderVersion < 300)
2891 {
2892 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2893 return false;
2894 }
2895
2896 if (left->isArray() != right->isArray())
2897 {
2898 error(loc, "array / non-array mismatch", GetOperatorString(op));
2899 return false;
2900 }
2901
2902 switch (op)
2903 {
2904 case EOpEqual:
2905 case EOpNotEqual:
2906 case EOpAssign:
2907 case EOpInitialize:
2908 break;
2909 default:
2910 error(loc, "Invalid operation for arrays", GetOperatorString(op));
2911 return false;
2912 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03002913 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02002914 if (left->getArraySize() != right->getArraySize())
2915 {
2916 error(loc, "array size mismatch", GetOperatorString(op));
2917 return false;
2918 }
Olli Etuahod6b14282015-03-17 14:31:35 +02002919 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002920
2921 // Check ops which require integer / ivec parameters
2922 bool isBitShift = false;
2923 switch (op)
2924 {
2925 case EOpBitShiftLeft:
2926 case EOpBitShiftRight:
2927 case EOpBitShiftLeftAssign:
2928 case EOpBitShiftRightAssign:
2929 // Unsigned can be bit-shifted by signed and vice versa, but we need to
2930 // check that the basic type is an integer type.
2931 isBitShift = true;
2932 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
2933 {
2934 return false;
2935 }
2936 break;
2937 case EOpBitwiseAnd:
2938 case EOpBitwiseXor:
2939 case EOpBitwiseOr:
2940 case EOpBitwiseAndAssign:
2941 case EOpBitwiseXorAssign:
2942 case EOpBitwiseOrAssign:
2943 // It is enough to check the type of only one operand, since later it
2944 // is checked that the operand types match.
2945 if (!IsInteger(left->getBasicType()))
2946 {
2947 return false;
2948 }
2949 break;
2950 default:
2951 break;
2952 }
2953
2954 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
2955 // So the basic type should usually match.
2956 if (!isBitShift && left->getBasicType() != right->getBasicType())
2957 {
2958 return false;
2959 }
2960
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002961 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02002962 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002963 switch(op)
2964 {
2965 case EOpAssign:
2966 case EOpInitialize:
2967 case EOpEqual:
2968 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02002969 // ESSL 1.00 sections 5.7, 5.8, 5.9
2970 if (shaderVersion < 300 && left->getType().isStructureContainingArrays())
2971 {
2972 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
2973 return false;
2974 }
Olli Etuahoff699002015-03-23 14:38:42 +02002975 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
2976 // we interpret the spec so that this extends to structs containing samplers,
2977 // similarly to ESSL 1.00 spec.
2978 if ((shaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
2979 left->getType().isStructureContainingSamplers())
2980 {
2981 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
2982 return false;
2983 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02002984 case EOpLessThan:
2985 case EOpGreaterThan:
2986 case EOpLessThanEqual:
2987 case EOpGreaterThanEqual:
2988 if ((left->getNominalSize() != right->getNominalSize()) ||
2989 (left->getSecondarySize() != right->getSecondarySize()))
2990 {
2991 return false;
2992 }
2993 default:
2994 break;
2995 }
2996
Olli Etuahod6b14282015-03-17 14:31:35 +02002997 return true;
2998}
2999
Olli Etuahofc1806e2015-03-17 13:03:11 +02003000TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3001 const TSourceLoc &loc)
3002{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003003 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003004 return nullptr;
3005
Olli Etuahofc1806e2015-03-17 13:03:11 +02003006 switch (op)
3007 {
3008 case EOpEqual:
3009 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003010 break;
3011 case EOpLessThan:
3012 case EOpGreaterThan:
3013 case EOpLessThanEqual:
3014 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003015 ASSERT(!left->isArray() && !right->isArray());
3016 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003017 left->getBasicType() == EbtStruct)
3018 {
3019 return nullptr;
3020 }
3021 break;
3022 case EOpLogicalOr:
3023 case EOpLogicalXor:
3024 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003025 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003026 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003027 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003028 {
3029 return nullptr;
3030 }
3031 break;
3032 case EOpAdd:
3033 case EOpSub:
3034 case EOpDiv:
3035 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003036 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003037 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3038 {
3039 return nullptr;
3040 }
3041 break;
3042 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003043 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003044 // Note that this is only for the % operator, not for mod()
3045 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3046 {
3047 return nullptr;
3048 }
3049 break;
3050 // Note that for bitwise ops, type checking is done in promote() to
3051 // share code between ops and compound assignment
3052 default:
3053 break;
3054 }
3055
Olli Etuahofc1806e2015-03-17 13:03:11 +02003056 return intermediate.addBinaryMath(op, left, right, loc);
3057}
3058
Olli Etuaho09b22472015-02-11 11:47:26 +02003059TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3060 const TSourceLoc &loc)
3061{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003062 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003063 if (node == 0)
3064 {
3065 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3066 recover();
3067 return left;
3068 }
3069 return node;
3070}
3071
3072TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3073 const TSourceLoc &loc)
3074{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003075 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003076 if (node == 0)
3077 {
3078 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3079 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003080 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003081 unionArray->setBConst(false);
3082 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3083 }
3084 return node;
3085}
3086
Olli Etuahod6b14282015-03-17 14:31:35 +02003087TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3088 const TSourceLoc &loc)
3089{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003090 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003091 {
3092 return intermediate.addAssign(op, left, right, loc);
3093 }
3094 return nullptr;
3095}
3096
3097TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3098 const TSourceLoc &loc)
3099{
3100 TIntermTyped *node = createAssign(op, left, right, loc);
3101 if (node == nullptr)
3102 {
3103 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3104 recover();
3105 return left;
3106 }
3107 return node;
3108}
3109
Olli Etuaho49300862015-02-20 14:54:49 +02003110TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3111{
3112 switch (op)
3113 {
3114 case EOpContinue:
3115 if (mLoopNestingLevel <= 0)
3116 {
3117 error(loc, "continue statement only allowed in loops", "");
3118 recover();
3119 }
3120 break;
3121 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003122 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003123 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003124 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003125 recover();
3126 }
3127 break;
3128 case EOpReturn:
3129 if (currentFunctionType->getBasicType() != EbtVoid)
3130 {
3131 error(loc, "non-void function must return a value", "return");
3132 recover();
3133 }
3134 break;
3135 default:
3136 // No checks for discard
3137 break;
3138 }
3139 return intermediate.addBranch(op, loc);
3140}
3141
3142TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3143{
3144 ASSERT(op == EOpReturn);
3145 mFunctionReturnsValue = true;
3146 if (currentFunctionType->getBasicType() == EbtVoid)
3147 {
3148 error(loc, "void function cannot return a value", "return");
3149 recover();
3150 }
3151 else if (*currentFunctionType != returnValue->getType())
3152 {
3153 error(loc, "function return is not matching type:", "return");
3154 recover();
3155 }
3156 return intermediate.addBranch(op, returnValue, loc);
3157}
3158
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003159TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3160 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003161{
3162 *fatalError = false;
3163 TOperator op = fnCall->getBuiltInOp();
3164 TIntermTyped *callNode = nullptr;
3165
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003166 if (thisNode != nullptr)
3167 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003168 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003169 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003170 TIntermTyped *typedThis = thisNode->getAsTyped();
3171 if (fnCall->getName() != "length")
3172 {
3173 error(loc, "invalid method", fnCall->getName().c_str());
3174 recover();
3175 }
3176 else if (paramNode != nullptr)
3177 {
3178 error(loc, "method takes no parameters", "length");
3179 recover();
3180 }
3181 else if (typedThis == nullptr || !typedThis->isArray())
3182 {
3183 error(loc, "length can only be called on arrays", "length");
3184 recover();
3185 }
3186 else
3187 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003188 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003189 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003190 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003191 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003192 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003193 // (func()).length()
3194 // (int[3](0, 1, 2)).length()
3195 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3196 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3197 // which allows "An array, vector or matrix expression with the length method applied".
3198 error(loc, "length can only be called on array names, not on array expressions", "length");
3199 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003200 }
3201 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003202 unionArray->setIConst(arraySize);
3203 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003204 }
3205 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003206 {
3207 //
3208 // Then this should be a constructor.
3209 // Don't go through the symbol table for constructors.
3210 // Their parameters will be verified algorithmically.
3211 //
3212 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003213 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003214 {
3215 //
3216 // It's a constructor, of type 'type'.
3217 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003218 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003219 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003220
3221 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003222 {
3223 recover();
3224 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3225 }
3226 callNode->setType(type);
3227 }
3228 else
3229 {
3230 //
3231 // Not a constructor. Find it in the symbol table.
3232 //
3233 const TFunction* fnCandidate;
3234 bool builtIn;
3235 fnCandidate = findFunction(loc, fnCall, shaderVersion, &builtIn);
3236 if (fnCandidate)
3237 {
3238 //
3239 // A declared function.
3240 //
3241 if (builtIn && !fnCandidate->getExtension().empty() &&
3242 extensionErrorCheck(loc, fnCandidate->getExtension()))
3243 {
3244 recover();
3245 }
3246 op = fnCandidate->getBuiltInOp();
3247 if (builtIn && op != EOpNull)
3248 {
3249 //
3250 // A function call mapped to a built-in operation.
3251 //
3252 if (fnCandidate->getParamCount() == 1)
3253 {
3254 //
3255 // Treat it like a built-in unary operator.
3256 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003257 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003258 if (callNode == nullptr)
3259 {
3260 std::stringstream extraInfoStream;
3261 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003262 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003263 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003264 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003265 *fatalError = true;
3266 return nullptr;
3267 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003268 }
3269 else
3270 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003271 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003272 aggregate->setType(fnCandidate->getReturnType());
3273 aggregate->setPrecisionFromChildren();
3274 callNode = aggregate;
3275
3276 // Some built-in functions have out parameters too.
3277 functionCallLValueErrorCheck(fnCandidate, aggregate);
3278 }
3279 }
3280 else
3281 {
3282 // This is a real function call
3283
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003284 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003285 aggregate->setType(fnCandidate->getReturnType());
3286
3287 // this is how we know whether the given function is a builtIn function or a user defined function
3288 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3289 // if builtIn == true, it's definitely a builtIn function with EOpNull
3290 if (!builtIn)
3291 aggregate->setUserDefined();
3292 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003293 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003294
3295 // This needs to happen after the name is set
3296 if (builtIn)
3297 aggregate->setBuiltInFunctionPrecision();
3298
3299 callNode = aggregate;
3300
3301 functionCallLValueErrorCheck(fnCandidate, aggregate);
3302 }
3303 }
3304 else
3305 {
3306 // error message was put out by findFunction()
3307 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003308 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003309 unionArray->setFConst(0.0f);
3310 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3311 recover();
3312 }
3313 }
3314 delete fnCall;
3315 return callNode;
3316}
3317
Olli Etuaho52901742015-04-15 13:42:45 +03003318TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3319 const TSourceLoc &loc)
3320{
3321 if (boolErrorCheck(loc, cond))
3322 recover();
3323
3324 if (trueBlock->getType() != falseBlock->getType())
3325 {
3326 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3327 recover();
3328 return falseBlock;
3329 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003330 // ESSL1 sections 5.2 and 5.7:
3331 // ESSL3 section 5.7:
3332 // Ternary operator is not among the operators allowed for structures/arrays.
3333 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3334 {
3335 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3336 recover();
3337 return falseBlock;
3338 }
Olli Etuaho52901742015-04-15 13:42:45 +03003339 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3340}
Olli Etuaho49300862015-02-20 14:54:49 +02003341
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003342//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003343// Parse an array of strings using yyparse.
3344//
3345// Returns 0 for success.
3346//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00003347int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003348 TParseContext* context) {
3349 if ((count == 0) || (string == NULL))
3350 return 1;
3351
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003352 if (glslang_initialize(context))
3353 return 1;
3354
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003355 int error = glslang_scan(count, string, length, context);
3356 if (!error)
3357 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003358
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003359 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003360
alokp@chromium.org6b495712012-06-29 00:06:58 +00003361 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003362}
3363
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003364
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003365