blob: 0b4e58de5f07a7b23e97763781287ada7ca93e33 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/glslang.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
alokp@chromium.org8b851c62012-06-15 16:25:11 +000015///////////////////////////////////////////////////////////////////////
16//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017// Sub- vector and matrix fields
18//
19////////////////////////////////////////////////////////////////////////
20
21//
22// Look at a '.' field selector string and change it into offsets
23// for a vector.
24//
Jamie Madill075edd82013-07-08 13:30:19 -040025bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000027 fields.num = (int) compString.size();
28 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000029 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 return false;
31 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 enum {
34 exyzw,
35 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000036 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000037 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
41 case 'x':
42 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
45 case 'r':
46 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
53 case 'y':
54 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
57 case 'g':
58 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
65 case 'z':
66 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
69 case 'b':
70 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
77
78 case 'w':
79 fields.offsets[i] = 3;
80 fieldSet[i] = exyzw;
81 break;
82 case 'a':
83 fields.offsets[i] = 3;
84 fieldSet[i] = ergba;
85 break;
86 case 'q':
87 fields.offsets[i] = 3;
88 fieldSet[i] = estpq;
89 break;
90 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000091 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000092 return false;
93 }
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000096 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 return false;
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 return false;
106 }
107 }
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
Jamie Madill075edd82013-07-08 13:30:19 -0400118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000126 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 return false;
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000132 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 return false;
134 }
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 return false;
141 }
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
144 } else {
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000154 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000155 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 return false;
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
Jamie Madill075edd82013-07-08 13:30:19 -0400178void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000180 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000182 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500185 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000186 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madill075edd82013-07-08 13:30:19 -0400190void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000191 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000192 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000193 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500196 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000197 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000198}
199
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000200void TParseContext::trace(const char* str)
201{
202 diagnostics.writeDebug(str);
203}
204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205//
206// Same error message for all places assignments don't work.
207//
Jamie Madill075edd82013-07-08 13:30:19 -0400208void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
216//
217// Same error message for all places unary operations don't work.
218//
Jamie Madill075edd82013-07-08 13:30:19 -0400219void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Same error message for all binary operations don't work.
230//
Jamie Madill075edd82013-07-08 13:30:19 -0400231void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
Jamie Madill075edd82013-07-08 13:30:19 -0400240bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000241 if (!checksPrecisionErrors)
242 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 switch( type ){
244 case EbtFloat:
245 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000246 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 return true;
248 }
249 break;
250 case EbtInt:
251 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253 return true;
254 }
255 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000256 default:
257 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000258 }
259 return false;
260}
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262//
263// Both test and if necessary, spit out an error, to see if the node is really
264// an l-value that can be operated on this way.
265//
266// Returns true if the was an error.
267//
Jamie Madill075edd82013-07-08 13:30:19 -0400268bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 if (binaryNode) {
274 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 switch(binaryNode->getOp()) {
277 case EOpIndexDirect:
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000280 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
284 if (!errorReturn) {
285 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700289
290 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
291 p != aggrNode->getSequence()->end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700293 offset[value]++;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 return true;
298 }
299 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return errorReturn;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700303 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 break;
305 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000306 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return true;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* symbol = 0;
313 if (symNode != 0)
314 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700321 case EvqFragmentIn: message = "can't modify an input"; break;
322 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 case EvqUniform: message = "can't modify a uniform"; break;
324 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
326 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
327 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
328 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 //
331 // Type that can't be written to?
332 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400333 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400335 }
336 if (IsSampler(node->getBasicType())) {
337 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 }
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000342 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 return true;
345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000348 //
349 // Everything else is okay, no error.
350 //
351 if (message == 0)
352 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 //
355 // If we get here, we have an error and a message.
356 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000357 if (symNode) {
358 std::stringstream extraInfoStream;
359 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
360 std::string extraInfo = extraInfoStream.str();
361 error(line, " l-value required", op, extraInfo.c_str());
362 }
363 else {
364 std::stringstream extraInfoStream;
365 extraInfoStream << "(" << message << ")";
366 std::string extraInfo = extraInfoStream.str();
367 error(line, " l-value required", op, extraInfo.c_str());
368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
373//
374// Both test, and if necessary spit out an error, to see if the node is really
375// a constant.
376//
377// Returns true if the was an error.
378//
379bool TParseContext::constErrorCheck(TIntermTyped* node)
380{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000381 if (node->getQualifier() == EvqConst)
382 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000384 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000386 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389//
390// Both test, and if necessary spit out an error, to see if the node is really
391// an integer.
392//
393// Returns true if the was an error.
394//
395bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
396{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000397 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000400 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Both test, and if necessary spit out an error, to see if we are currently
407// globally scoped.
408//
409// Returns true if the was an error.
410//
Jamie Madill075edd82013-07-08 13:30:19 -0400411bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000413 if (global)
414 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000416 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
421//
422// For now, keep it simple: if it starts "gl_", it's reserved, independent
423// of scope. Except, if the symbol table is at the built-in push-level,
424// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000425// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
426// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427//
428// Returns true if there was an error.
429//
Jamie Madill075edd82013-07-08 13:30:19 -0400430bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000432 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000434 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000435 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 return true;
437 }
Jamie Madill5508f392014-02-20 13:31:36 -0500438 if (IsWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000439 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000440 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000441 return true;
442 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000443 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000444 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000445 return true;
446 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000447 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000448 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000449 return true;
450 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000451 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000454 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 }
456 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459}
460
461//
462// Make sure there is enough data provided to the constructor to build
463// something of the type of the constructor. Also returns the type of
464// the constructor.
465//
466// Returns true if there was an error in construction.
467//
Jamie Madill075edd82013-07-08 13:30:19 -0400468bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 bool constructingMatrix = false;
473 switch(op) {
474 case EOpConstructMat2:
475 case EOpConstructMat3:
476 case EOpConstructMat4:
477 constructingMatrix = true;
478 break;
479 default:
480 break;
481 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 //
484 // Note: It's okay to have too many components available, but not okay to have unused
485 // arguments. 'full' will go to true when enough args have been seen. If we loop
486 // again, there is an extra argument, so 'overfull' will become true.
487 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488
Jamie Madill94bf7f22013-07-08 13:31:15 -0400489 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 bool constType = true;
491 bool full = false;
492 bool overFull = false;
493 bool matrixInMatrix = false;
494 bool arrayArg = false;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000495 for (size_t i = 0; i < function.getParamCount(); ++i) {
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000496 const TParameter& param = function.getParam(i);
497 size += param.type->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000499 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000500 matrixInMatrix = true;
501 if (full)
502 overFull = true;
503 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
504 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000505 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000507 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 arrayArg = true;
509 }
510
511 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000512 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513
shannon.woods@transgaming.com18bd2ec2013-02-28 23:19:46 +0000514 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000515 error(line, "array constructor needs one argument per array element", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 return true;
517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 if (arrayArg && op != EOpConstructStruct) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000520 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 return true;
522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000524 if (matrixInMatrix && !type->isArray()) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000525 if (function.getParamCount() != 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000526 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000527 return true;
528 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 if (overFull) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000532 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 return true;
534 }
535
Brendan Longeaa84062013-12-08 18:26:50 +0100536 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000537 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 return true;
539 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540
daniel@transgaming.com67d72522011-11-29 17:23:51 +0000541 if (!type->isMatrix() || !matrixInMatrix) {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000542 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
543 (op == EOpConstructStruct && size < type->getObjectSize())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000544 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000545 return true;
546 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000549 TIntermTyped *typed = node ? node->getAsTyped() : 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (typed == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000551 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 return true;
553 }
554 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000555 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 return true;
557 }
558 if (typed->getBasicType() == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000559 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 return true;
561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564}
565
566// This function checks to see if a void variable has been declared and raise an error message for such a case
567//
568// returns true in case of an error
569//
Jamie Madill075edd82013-07-08 13:30:19 -0400570bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 if (pubType.type == EbtVoid) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000573 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 return true;
575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578}
579
580// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
581//
582// returns true in case of an error
583//
Jamie Madill075edd82013-07-08 13:30:19 -0400584bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 return true;
589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592}
593
594// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
595//
596// returns true in case of an error
597//
Jamie Madill075edd82013-07-08 13:30:19 -0400598bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599{
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000600 if (pType.type != EbtBool || pType.isAggregate()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000601 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000602 return true;
603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606}
607
Jamie Madill075edd82013-07-08 13:30:19 -0400608bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 if (pType.type == EbtStruct) {
611 if (containsSampler(*pType.userDef)) {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000612 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613
614 return true;
615 }
616
617 return false;
618 } else if (IsSampler(pType.type)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000619 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 return true;
622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625}
626
Jamie Madill075edd82013-07-08 13:30:19 -0400627bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628{
Jamie Madillb120eac2013-06-12 14:08:13 -0400629 switch (pType.qualifier)
630 {
631 case EvqVaryingIn:
632 case EvqVaryingOut:
633 case EvqAttribute:
Jamie Madill19571812013-08-12 15:26:34 -0700634 case EvqVertexIn:
635 case EvqFragmentOut:
Jamie Madillb120eac2013-06-12 14:08:13 -0400636 if (pType.type == EbtStruct)
637 {
638 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
639 return true;
640 }
Jamie Madill10567262014-04-17 16:40:00 -0400641
642 default: break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
646 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madill075edd82013-07-08 13:30:19 -0400651bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400652{
653 if (pType.layoutQualifier.location != -1)
654 {
655 error(line, "location must only be specified for a single input or output variable", "location");
656 return true;
657 }
658
659 return false;
660}
661
Jamie Madill075edd82013-07-08 13:30:19 -0400662bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
665 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000666 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return true;
668 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000670 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671}
672
673bool TParseContext::containsSampler(TType& type)
674{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 if (IsSampler(type.getBasicType()))
676 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677
Jamie Madill98493dd2013-07-08 14:39:03 -0400678 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
679 const TFieldList& fields = type.getStruct()->fields();
680 for (unsigned int i = 0; i < fields.size(); ++i) {
681 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return true;
683 }
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000686 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687}
688
689//
690// Do size checking for an array type's size.
691//
692// Returns true if there was an error.
693//
Jamie Madill075edd82013-07-08 13:30:19 -0400694bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000697
698 if (constant == 0 || !constant->isScalarInt())
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 return true;
702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703
Nicolas Capens906744a2014-06-06 15:18:07 -0400704 unsigned int unsignedSize = 0;
705
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000706 if (constant->getBasicType() == EbtUInt)
707 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400708 unsignedSize = constant->getUConst(0);
709 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000710 }
711 else
712 {
713 size = constant->getIConst(0);
714
Nicolas Capens906744a2014-06-06 15:18:07 -0400715 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000716 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400717 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000718 size = 1;
719 return true;
720 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400721
722 unsignedSize = static_cast<unsigned int>(size);
723 }
724
725 if (size == 0)
726 {
727 error(line, "array size must be greater than zero", "");
728 size = 1;
729 return true;
730 }
731
732 // The size of arrays is restricted here to prevent issues further down the
733 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
734 // 4096 registers so this should be reasonable even for aggressively optimizable code.
735 const unsigned int sizeLimit = 65536;
736
737 if (unsignedSize > sizeLimit)
738 {
739 error(line, "array size too large", "");
740 size = 1;
741 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
747//
748// See if this qualifier can be an array.
749//
750// Returns true if there is an error.
751//
Jamie Madill075edd82013-07-08 13:30:19 -0400752bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
Jamie Madill19571812013-08-12 15:26:34 -0700754 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000755 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 return true;
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
762//
763// See if this type can be an array.
764//
765// Returns true if there is an error.
766//
Jamie Madill075edd82013-07-08 13:30:19 -0400767bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 //
770 // Can the type be an array?
771 //
772 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000773 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000774 return true;
775 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
780//
781// Do all the semantic checking for declaring an array, with and
782// without a size, and make the right changes to the symbol table.
783//
784// size == 0 means no specified size.
785//
786// Returns true if there was an error.
787//
Jamie Madill075edd82013-07-08 13:30:19 -0400788bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 //
791 // Don't check for reserved word use until after we know it's not in the symbol table,
792 // because reserved arrays can be redeclared.
793 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 bool builtIn = false;
796 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000797 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 if (symbol == 0 || !sameScope) {
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100799 bool needsReservedErrorCheck = true;
800
801 // gl_LastFragData may be redeclared with a new precision qualifier
802 if (identifier.compare(0, 15, "gl_LastFragData") == 0) {
803 if (type.arraySize == static_cast<const TVariable*>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", shaderVersion))->getConstPointer()->getIConst()) {
804 if (TSymbol* builtInSymbol = symbolTable.findBuiltIn(identifier, shaderVersion)) {
805 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
806 }
807 } else {
808 error(line, "redeclaration of array with size != gl_MaxDrawBuffers", identifier.c_str());
809 return true;
810 }
811 }
812
813 if (needsReservedErrorCheck)
814 if (reservedErrorCheck(line, identifier))
815 return true;
816
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 if (type.arraySize)
820 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821
Nicolas Capensadfffe42014-06-17 02:13:36 -0400822 if (! symbolTable.declare(variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000824 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000825 return true;
826 }
827 } else {
828 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000829 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return true;
831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 variable = static_cast<TVariable*>(symbol);
834 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000835 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000836 return true;
837 }
838 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000839 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 return true;
841 }
842
843 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000848 if (type.arraySize)
849 variable->getType().setArraySize(type.arraySize);
850 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 if (voidErrorCheck(line, identifier, type))
853 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858//
859// Enforce non-initializer type/qualifier rules.
860//
861// Returns true if there was an error.
862//
Jamie Madill075edd82013-07-08 13:30:19 -0400863bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000865 if (type.qualifier == EvqConst)
866 {
867 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000869
870 if (array)
871 {
872 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
873 }
874 else if (type.isStructureContainingArrays())
875 {
876 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
877 }
878 else
879 {
880 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
881 }
882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 return true;
884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
889//
890// Do semantic checking for a variable declaration that has no initializer,
891// and update the symbol table.
892//
893// Returns true if there was an error.
894//
Jamie Madill075edd82013-07-08 13:30:19 -0400895bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 if (reservedErrorCheck(line, identifier))
898 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
zmo@google.comfd747b82011-04-23 01:30:07 +0000900 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901
Nicolas Capensadfffe42014-06-17 02:13:36 -0400902 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000903 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000905 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 return true;
907 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 if (voidErrorCheck(line, identifier, type))
910 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000912 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
Jamie Madill075edd82013-07-08 13:30:19 -0400915bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000918 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 return true;
920 }
921 if (qualifier == EvqConst && paramQualifier != EvqIn) {
922 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
923 return true;
924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000927 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000929 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
933
Jamie Madill075edd82013-07-08 13:30:19 -0400934bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000935{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000936 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000937 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
938 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000939 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 return true;
941 }
zmo@google.comf5450912011-09-09 01:37:19 +0000942 // In GLSL ES, an extension's default behavior is "disable".
943 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000944 error(line, "extension", extension.c_str(), "is disabled");
945 return true;
946 }
947 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000948 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000949 return false;
950 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953}
954
Jamie Madill075edd82013-07-08 13:30:19 -0400955bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400956{
957 if (structQualifierErrorCheck(identifierLocation, publicType))
958 return true;
959
960 // check for layout qualifier issues
961 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
962
963 if (layoutQualifier.matrixPacking != EmpUnspecified)
964 {
965 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400966 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400967 }
968
969 if (layoutQualifier.blockStorage != EbsUnspecified)
970 {
971 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400972 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400973 }
974
Jamie Madill19571812013-08-12 15:26:34 -0700975 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400976 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400977 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400978 }
979
980 return false;
981}
982
Jamie Madill075edd82013-07-08 13:30:19 -0400983bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400984{
985 if (layoutQualifier.location != -1)
986 {
987 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
988 return true;
989 }
990
991 return false;
992}
993
Olli Etuahob6e07a62015-02-16 12:22:10 +0200994bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
995{
996 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
997 {
998 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
999 if (qual == EvqOut || qual == EvqInOut)
1000 {
1001 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1002 if (lValueErrorCheck(node->getLine(), "assign", node))
1003 {
1004 error(node->getLine(),
1005 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1006 recover();
1007 return true;
1008 }
1009 }
1010 }
1011 return false;
1012}
1013
zmo@google.com09c323a2011-08-12 18:22:25 +00001014bool TParseContext::supportsExtension(const char* extension)
1015{
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001016 const TExtensionBehavior& extbehavior = extensionBehavior();
1017 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1018 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001019}
1020
Jamie Madill5d287f52013-07-12 15:38:19 -04001021bool TParseContext::isExtensionEnabled(const char* extension) const
1022{
1023 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001024 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001025
1026 if (iter == extbehavior.end())
1027 {
1028 return false;
1029 }
1030
1031 return (iter->second == EBhEnable || iter->second == EBhRequire);
1032}
1033
Jamie Madill075edd82013-07-08 13:30:19 -04001034void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1035{
1036 pp::SourceLocation srcLoc;
1037 srcLoc.file = loc.first_file;
1038 srcLoc.line = loc.first_line;
1039 directiveHandler.handleExtension(srcLoc, extName, behavior);
1040}
1041
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001042void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001043{
1044 pp::SourceLocation srcLoc;
1045 srcLoc.file = loc.first_file;
1046 srcLoc.line = loc.first_line;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001047 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001048}
1049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050/////////////////////////////////////////////////////////////////////////////////
1051//
1052// Non-Errors.
1053//
1054/////////////////////////////////////////////////////////////////////////////////
1055
Jamie Madill5c097022014-08-20 16:38:32 -04001056const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1057 const TString *name,
1058 const TSymbol *symbol)
1059{
1060 const TVariable *variable = NULL;
1061
1062 if (!symbol)
1063 {
1064 error(location, "undeclared identifier", name->c_str());
1065 recover();
1066 }
1067 else if (!symbol->isVariable())
1068 {
1069 error(location, "variable expected", name->c_str());
1070 recover();
1071 }
1072 else
1073 {
1074 variable = static_cast<const TVariable*>(symbol);
1075
1076 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1077 !variable->getExtension().empty() &&
1078 extensionErrorCheck(location, variable->getExtension()))
1079 {
1080 recover();
1081 }
1082 }
1083
1084 if (!variable)
1085 {
1086 TType type(EbtFloat, EbpUndefined);
1087 TVariable *fakeVariable = new TVariable(name, type);
1088 symbolTable.declare(fakeVariable);
1089 variable = fakeVariable;
1090 }
1091
1092 return variable;
1093}
1094
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095//
1096// Look up a function name in the symbol table, and make sure it is a function.
1097//
1098// Return the function symbol if found, otherwise 0.
1099//
Austin Kinross3ae64652015-01-26 15:51:39 -08001100const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int inputShaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001102 // First find by unmangled name to check whether the function name has been
1103 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001104 // If a function is found, check for one with a matching argument list.
Austin Kinross3ae64652015-01-26 15:51:39 -08001105 const TSymbol* symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001106 if (symbol == 0 || symbol->isFunction()) {
Austin Kinross3ae64652015-01-26 15:51:39 -08001107 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
alokp@chromium.org0a576182010-08-09 17:16:27 +00001110 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001111 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001112 return 0;
1113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
alokp@chromium.org0a576182010-08-09 17:16:27 +00001115 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001116 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001117 return 0;
1118 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001119
1120 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121}
1122
1123//
1124// Initializers show up in several places in the grammar. Have one set of
1125// code to handle them here.
1126//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001127// Returns true on error, false if no error
1128//
Jamie Madill075edd82013-07-08 13:30:19 -04001129bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001130 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001132 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001134 if (variable == 0) {
1135 if (reservedErrorCheck(line, identifier))
1136 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 if (voidErrorCheck(line, identifier, pType))
1139 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001141 //
1142 // add variable to symbol table
1143 //
1144 variable = new TVariable(&identifier, type);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001145 if (! symbolTable.declare(variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001146 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001147 return true;
1148 // don't delete variable, it's used by error recovery, and the pool
1149 // pop will take care of the memory
1150 }
1151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001153 //
1154 // identifier must be of type constant, a global, or a temporary
1155 //
1156 TQualifier qualifier = variable->getType().getQualifier();
1157 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001158 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001159 return true;
1160 }
1161 //
1162 // test for and propagate constant
1163 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001165 if (qualifier == EvqConst) {
1166 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001167 std::stringstream extraInfoStream;
1168 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1169 std::string extraInfo = extraInfoStream.str();
1170 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001171 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001172 return true;
1173 }
1174 if (type != initializer->getType()) {
1175 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001176 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001177 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001178 return true;
1179 }
1180 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001181 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001182 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001183 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001184 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001186 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001187 variable->shareConstPointer(constArray);
1188 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001189 std::stringstream extraInfoStream;
1190 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1191 std::string extraInfo = extraInfoStream.str();
1192 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001193 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001194 return true;
1195 }
1196 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001197
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001198 if (qualifier != EvqConst) {
1199 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1200 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1201 if (intermNode == 0) {
1202 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1203 return true;
1204 }
1205 } else
1206 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001208 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001209}
1210
1211bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1212{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001213 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001214 if (!aggrNode->isConstructor())
1215 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001216
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001217 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001218
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001219 // check if all the child nodes are constants so that they can be inserted into
1220 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001221 TIntermSequence *sequence = aggrNode->getSequence() ;
1222 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001223 if (!(*p)->getAsTyped()->getAsConstantUnion())
1224 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001225 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001227 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228}
1229
Jamie Madilla5efff92013-06-06 11:56:47 -04001230TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001231{
1232 TPublicType returnType = typeSpecifier;
1233 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001234 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001235
1236 if (typeSpecifier.array)
1237 {
1238 error(typeSpecifier.line, "not supported", "first-class array");
1239 recover();
1240 returnType.setArray(false);
1241 }
1242
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001243 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001244 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001245 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1246 {
1247 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1248 recover();
1249 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001250
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001251 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1252 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1253 {
1254 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1255 recover();
1256 }
1257 }
1258 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001259 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001260 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001261 {
Jamie Madill19571812013-08-12 15:26:34 -07001262 case EvqSmoothIn:
1263 case EvqSmoothOut:
1264 case EvqVertexOut:
1265 case EvqFragmentIn:
1266 case EvqCentroidOut:
1267 case EvqCentroidIn:
1268 if (typeSpecifier.type == EbtBool)
1269 {
1270 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1271 recover();
1272 }
1273 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1274 {
1275 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1276 recover();
1277 }
1278 break;
1279
1280 case EvqVertexIn:
1281 case EvqFragmentOut:
1282 case EvqFlatIn:
1283 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001284 if (typeSpecifier.type == EbtBool)
1285 {
1286 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1287 recover();
1288 }
1289 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001290
Jamie Madillb120eac2013-06-12 14:08:13 -04001291 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001292 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001293 }
1294
1295 return returnType;
1296}
1297
Jamie Madill075edd82013-07-08 13:30:19 -04001298TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001299{
1300 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1301 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1302
1303 if (identifier != "")
1304 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001305 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001306 recover();
1307
1308 // this error check can mutate the type
1309 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1310 recover();
1311
1312 TVariable* variable = 0;
1313
1314 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1315 recover();
1316
1317 if (variable && symbol)
1318 {
1319 symbol->setId(variable->getUniqueId());
1320 }
1321 }
1322
1323 return aggregate;
1324}
1325
Jamie Madill075edd82013-07-08 13:30:19 -04001326TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001327{
Jamie Madill51a53c72013-06-19 09:24:43 -04001328 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001329 recover();
1330
1331 // this error check can mutate the type
1332 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1333 recover();
1334
1335 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1336 {
1337 recover();
1338 }
1339
1340 TPublicType arrayType = publicType;
1341
1342 int size;
1343 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1344 {
1345 recover();
1346 }
1347 else
1348 {
1349 arrayType.setArray(true, size);
1350 }
1351
1352 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1353 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1354 TVariable* variable = 0;
1355
1356 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1357 recover();
1358
1359 if (variable && symbol)
1360 {
1361 symbol->setId(variable->getUniqueId());
1362 }
1363
1364 return aggregate;
1365}
1366
Jamie Madill075edd82013-07-08 13:30:19 -04001367TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001368{
Jamie Madilla5efff92013-06-06 11:56:47 -04001369 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001370 recover();
1371
1372 TIntermNode* intermNode;
1373 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1374 {
1375 //
1376 // Build intermediate representation
1377 //
1378 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1379 }
1380 else
1381 {
1382 recover();
1383 return NULL;
1384 }
1385}
1386
Jamie Madill47e3ec02014-08-20 16:38:33 -04001387TIntermAggregate* TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1388 const TSourceLoc &identifierLoc,
1389 const TString *identifier,
1390 const TSymbol *symbol)
1391{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001392 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001393 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1394 {
1395 recover();
1396 }
1397
1398 if (!symbol)
1399 {
1400 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1401 recover();
Jamie Madill47e3ec02014-08-20 16:38:33 -04001402 return NULL;
1403 }
1404 else
1405 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001406 const TString kGlFrontFacing("gl_FrontFacing");
1407 if (*identifier == kGlFrontFacing)
1408 {
1409 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1410 recover();
1411 return NULL;
1412 }
Jamie Madill2c433252014-12-03 12:36:54 -05001413 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001414 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1415 ASSERT(variable);
1416 const TType &type = variable->getType();
1417 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1418 *identifier, type, identifierLoc);
1419
1420 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1421 aggregate->setOp(EOpInvariantDeclaration);
1422 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001423 }
1424}
1425
Jamie Madill075edd82013-07-08 13:30:19 -04001426TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001427{
Jamie Madill502d66f2013-06-20 11:55:52 -04001428 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1429 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1430
1431 if (structQualifierErrorCheck(identifierLocation, publicType))
1432 recover();
1433
Jamie Madill0bd18df2013-06-20 11:55:52 -04001434 if (locationDeclaratorListCheck(identifierLocation, publicType))
1435 recover();
1436
Jamie Madill502d66f2013-06-20 11:55:52 -04001437 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1438 recover();
1439
1440 TVariable* variable = 0;
1441 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1442 recover();
1443 if (symbol && variable)
1444 symbol->setId(variable->getUniqueId());
1445
1446 return intermAggregate;
1447}
1448
Jamie Madill075edd82013-07-08 13:30:19 -04001449TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001450{
1451 if (structQualifierErrorCheck(identifierLocation, publicType))
1452 recover();
1453
Jamie Madill0bd18df2013-06-20 11:55:52 -04001454 if (locationDeclaratorListCheck(identifierLocation, publicType))
1455 recover();
1456
Jamie Madill502d66f2013-06-20 11:55:52 -04001457 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1458 recover();
1459
1460 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1461 {
1462 recover();
1463 }
1464 else if (indexExpression)
1465 {
1466 int size;
1467 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1468 recover();
1469 TPublicType arrayType(publicType);
1470 arrayType.setArray(true, size);
1471 TVariable* variable = NULL;
1472 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1473 recover();
1474 TType type = TType(arrayType);
1475 type.setArraySize(size);
1476
1477 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1478 }
1479 else
1480 {
1481 TPublicType arrayType(publicType);
1482 arrayType.setArray(true);
1483 TVariable* variable = NULL;
1484 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1485 recover();
1486 }
1487
1488 return NULL;
1489}
1490
Jamie Madill075edd82013-07-08 13:30:19 -04001491TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001492{
1493 if (structQualifierErrorCheck(identifierLocation, publicType))
1494 recover();
1495
Jamie Madill0bd18df2013-06-20 11:55:52 -04001496 if (locationDeclaratorListCheck(identifierLocation, publicType))
1497 recover();
1498
Jamie Madill502d66f2013-06-20 11:55:52 -04001499 TIntermNode* intermNode;
1500 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1501 {
1502 //
1503 // build the intermediate representation
1504 //
1505 if (intermNode)
1506 {
1507 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1508 }
1509 else
1510 {
1511 return declaratorList;
1512 }
1513 }
1514 else
1515 {
1516 recover();
1517 return NULL;
1518 }
1519}
1520
Jamie Madilla295edf2013-06-06 11:56:48 -04001521void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1522{
1523 if (typeQualifier.qualifier != EvqUniform)
1524 {
1525 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1526 recover();
1527 return;
1528 }
1529
1530 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1531 ASSERT(!layoutQualifier.isEmpty());
1532
1533 if (shaderVersion < 300)
1534 {
1535 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1536 recover();
1537 return;
1538 }
1539
1540 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1541 {
1542 recover();
1543 return;
1544 }
1545
Jamie Madill099c0f32013-06-20 11:55:52 -04001546 if (layoutQualifier.matrixPacking != EmpUnspecified)
1547 {
1548 defaultMatrixPacking = layoutQualifier.matrixPacking;
1549 }
1550
Geoff Langc6856732014-02-11 09:38:55 -05001551 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001552 {
1553 defaultBlockStorage = layoutQualifier.blockStorage;
1554 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001555}
1556
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001557TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1558{
1559 TOperator op = EOpNull;
1560 if (publicType.userDef)
1561 {
1562 op = EOpConstructStruct;
1563 }
1564 else
1565 {
1566 switch (publicType.type)
1567 {
1568 case EbtFloat:
1569 if (publicType.isMatrix())
1570 {
1571 // TODO: non-square matrices
1572 switch(publicType.getCols())
1573 {
Jamie Madill28b97422013-07-08 14:01:38 -04001574 case 2: op = EOpConstructMat2; break;
1575 case 3: op = EOpConstructMat3; break;
1576 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001577 }
1578 }
1579 else
1580 {
1581 switch(publicType.getNominalSize())
1582 {
Jamie Madill28b97422013-07-08 14:01:38 -04001583 case 1: op = EOpConstructFloat; break;
1584 case 2: op = EOpConstructVec2; break;
1585 case 3: op = EOpConstructVec3; break;
1586 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001587 }
1588 }
1589 break;
1590
1591 case EbtInt:
1592 switch(publicType.getNominalSize())
1593 {
Jamie Madill28b97422013-07-08 14:01:38 -04001594 case 1: op = EOpConstructInt; break;
1595 case 2: op = EOpConstructIVec2; break;
1596 case 3: op = EOpConstructIVec3; break;
1597 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001598 }
1599 break;
1600
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001601 case EbtUInt:
1602 switch(publicType.getNominalSize())
1603 {
Jamie Madill28b97422013-07-08 14:01:38 -04001604 case 1: op = EOpConstructUInt; break;
1605 case 2: op = EOpConstructUVec2; break;
1606 case 3: op = EOpConstructUVec3; break;
1607 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001608 }
1609 break;
1610
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001611 case EbtBool:
1612 switch(publicType.getNominalSize())
1613 {
Jamie Madill28b97422013-07-08 14:01:38 -04001614 case 1: op = EOpConstructBool; break;
1615 case 2: op = EOpConstructBVec2; break;
1616 case 3: op = EOpConstructBVec3; break;
1617 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001618 }
1619 break;
1620
1621 default: break;
1622 }
1623
1624 if (op == EOpNull)
1625 {
1626 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1627 recover();
1628 publicType.type = EbtFloat;
1629 op = EOpConstructFloat;
1630 }
1631 }
1632
1633 TString tempString;
1634 TType type(publicType);
1635 return new TFunction(&tempString, type, op);
1636}
1637
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638// This function is used to test for the correctness of the parameters passed to various constructor functions
1639// and also convert them to the right datatype if it is allowed and required.
1640//
1641// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1642//
Olli Etuaho21203702014-11-13 16:16:21 +02001643TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001644{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001645 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001647 if (!aggregateArguments)
1648 {
1649 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001650 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001651 }
1652
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001653 if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001654 {
1655 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001656 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001658 for (size_t i = 0; i < fields.size(); i++)
1659 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001660 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001661 {
1662 error(line, "Structure constructor arguments do not match structure fields", "Error");
1663 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001665 return 0;
1666 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001667 }
1668 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001670 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001671 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1672 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001673 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001674 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001675 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001676 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677
Olli Etuaho21203702014-11-13 16:16:21 +02001678 // Structs should not be precision qualified, the individual members may be.
1679 // Built-in types on the other hand should be precision qualified.
1680 if (op != EOpConstructStruct)
1681 {
1682 constructor->setPrecisionFromChildren();
1683 type->setPrecision(constructor->getPrecision());
1684 }
1685
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001686 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687}
1688
1689TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1690{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001691 bool canBeFolded = areAllChildConst(aggrNode);
1692 aggrNode->setType(type);
1693 if (canBeFolded) {
1694 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001695 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001696 if (aggrNode->getSequence()->size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001697 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001698 }
1699 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001700 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001701 }
1702 if (returnVal)
1703 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001705 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1706 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001708 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709}
1710
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711//
1712// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1713// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1714// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1715// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1716// a constant matrix.
1717//
Jamie Madill075edd82013-07-08 13:30:19 -04001718TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 TIntermTyped* typedNode;
1721 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001723 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001724 if (tempConstantNode) {
1725 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001727 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001728 return node;
1729 }
1730 } 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 +00001731 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001732 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001733
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 return 0;
1735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001737 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001739 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001740 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001741 std::stringstream extraInfoStream;
1742 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1743 std::string extraInfo = extraInfoStream.str();
1744 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001745 recover();
1746 fields.offsets[i] = 0;
1747 }
1748
1749 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 }
1752 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1753 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754}
1755
1756//
1757// This function returns the column being accessed from a constant matrix. The values are retrieved from
1758// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1759// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1760// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1761//
Jamie Madill075edd82013-07-08 13:30:19 -04001762TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 TIntermTyped* typedNode;
1765 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001767 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001768 std::stringstream extraInfoStream;
1769 extraInfoStream << "matrix field selection out of range '" << index << "'";
1770 std::string extraInfo = extraInfoStream.str();
1771 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001772 recover();
1773 index = 0;
1774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001776 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001777 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001778 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001779 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1780 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001781 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 return 0;
1785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001787 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788}
1789
1790
1791//
1792// This function returns an element of an array accessed from a constant array. The values are retrieved from
1793// the symbol table and parse-tree is built for the type of the element. The input
1794// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1795// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1796//
Jamie Madill075edd82013-07-08 13:30:19 -04001797TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001799 TIntermTyped* typedNode;
1800 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1801 TType arrayElementType = node->getType();
1802 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001805 std::stringstream extraInfoStream;
1806 extraInfoStream << "array field selection out of range '" << index << "'";
1807 std::string extraInfo = extraInfoStream.str();
1808 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001809 recover();
1810 index = 0;
1811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001813 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001814 size_t arrayElementSize = arrayElementType.getObjectSize();
1815 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1816 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001817 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001818 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 return 0;
1822 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001824 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825}
1826
1827
1828//
1829// This function returns the value of a particular field inside a constant structure from the symbol table.
1830// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1831// function and returns the parse-tree with the values of the embedded/nested struct.
1832//
Jamie Madill075edd82013-07-08 13:30:19 -04001833TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834{
Jamie Madill98493dd2013-07-08 14:39:03 -04001835 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001836 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
Jamie Madill98493dd2013-07-08 14:39:03 -04001838 for (size_t index = 0; index < fields.size(); ++index) {
1839 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 break;
1841 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001842 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001843 }
1844 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845
Jamie Madill94bf7f22013-07-08 13:31:15 -04001846 TIntermTyped *typedNode;
1847 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001848 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001849 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001851 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1852 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001853 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001854 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 return 0;
1857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001859 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860}
1861
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001862//
1863// Interface/uniform blocks
1864//
Jamie Madill98493dd2013-07-08 14:39:03 -04001865TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1866 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001867{
1868 if (reservedErrorCheck(nameLine, blockName))
1869 recover();
1870
1871 if (typeQualifier.qualifier != EvqUniform)
1872 {
1873 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1874 recover();
1875 }
1876
Jamie Madill099c0f32013-06-20 11:55:52 -04001877 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1878 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001879 {
1880 recover();
1881 }
1882
Jamie Madill099c0f32013-06-20 11:55:52 -04001883 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1884 {
1885 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1886 }
1887
Jamie Madill1566ef72013-06-20 11:55:54 -04001888 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1889 {
1890 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1891 }
1892
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001893 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
Nicolas Capensadfffe42014-06-17 02:13:36 -04001894 if (!symbolTable.declare(blockNameSymbol)) {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001895 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1896 recover();
1897 }
1898
Jamie Madill98493dd2013-07-08 14:39:03 -04001899 // check for sampler types and apply layout qualifiers
1900 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1901 TField* field = (*fieldList)[memberIndex];
1902 TType* fieldType = field->type();
1903 if (IsSampler(fieldType->getBasicType())) {
1904 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001905 recover();
1906 }
1907
Jamie Madill98493dd2013-07-08 14:39:03 -04001908 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001909 switch (qualifier)
1910 {
1911 case EvqGlobal:
1912 case EvqUniform:
1913 break;
1914 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001916 recover();
1917 break;
1918 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001919
1920 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001921 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1922 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001923 {
1924 recover();
1925 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001926
Jamie Madill98493dd2013-07-08 14:39:03 -04001927 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001928 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001930 recover();
1931 }
1932
Jamie Madill98493dd2013-07-08 14:39:03 -04001933 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001934 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001935 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001936 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001937 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001938 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001939 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001940 recover();
1941 }
1942
Jamie Madill98493dd2013-07-08 14:39:03 -04001943 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001944 }
1945
Jamie Madill98493dd2013-07-08 14:39:03 -04001946 // add array index
1947 int arraySize = 0;
1948 if (arrayIndex != NULL)
1949 {
1950 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1951 recover();
1952 }
1953
1954 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1955 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001956
1957 TString symbolName = "";
1958 int symbolId = 0;
1959
Jamie Madill98493dd2013-07-08 14:39:03 -04001960 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001961 {
1962 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001963 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1964 {
1965 TField* field = (*fieldList)[memberIndex];
1966 TType* fieldType = field->type();
1967
1968 // set parent pointer of the field variable
1969 fieldType->setInterfaceBlock(interfaceBlock);
1970
1971 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1972 fieldVariable->setQualifier(typeQualifier.qualifier);
1973
Nicolas Capensadfffe42014-06-17 02:13:36 -04001974 if (!symbolTable.declare(fieldVariable)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001975 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001976 recover();
1977 }
1978 }
1979 }
1980 else
1981 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001982 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001983 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001984 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001985
Nicolas Capensadfffe42014-06-17 02:13:36 -04001986 if (!symbolTable.declare(instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001987 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001988 recover();
1989 }
1990
1991 symbolId = instanceTypeDef->getUniqueId();
1992 symbolName = instanceTypeDef->getName();
1993 }
1994
Jamie Madill98493dd2013-07-08 14:39:03 -04001995 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001996 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001997
1998 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001999 return aggregate;
2000}
2001
Jamie Madill075edd82013-07-08 13:30:19 -04002002bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002003{
2004 ++structNestingLevel;
2005
2006 // Embedded structure definitions are not supported per GLSL ES spec.
2007 // They aren't allowed in GLSL either, but we need to detect this here
2008 // so we don't rely on the GLSL compiler to catch it.
2009 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002010 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002011 return true;
2012 }
2013
2014 return false;
2015}
2016
2017void TParseContext::exitStructDeclaration()
2018{
2019 --structNestingLevel;
2020}
2021
2022namespace {
2023
2024const int kWebGLMaxStructNesting = 4;
2025
2026} // namespace
2027
Jamie Madill98493dd2013-07-08 14:39:03 -04002028bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002029{
Jamie Madill5508f392014-02-20 13:31:36 -05002030 if (!IsWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002031 return false;
2032 }
2033
Jamie Madill98493dd2013-07-08 14:39:03 -04002034 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002035 return false;
2036 }
2037
2038 // We're already inside a structure definition at this point, so add
2039 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002040 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
Jamie Madill41a49272014-03-18 16:10:13 -04002041 std::stringstream reasonStream;
2042 reasonStream << "Reference of struct type "
2043 << field.type()->getStruct()->name().c_str()
2044 << " exceeds maximum allowed nesting level of "
2045 << kWebGLMaxStructNesting;
2046 std::string reason = reasonStream.str();
2047 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002048 return true;
2049 }
2050
2051 return false;
2052}
2053
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002054//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002055// Parse an array index expression
2056//
Jamie Madill075edd82013-07-08 13:30:19 -04002057TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002058{
2059 TIntermTyped *indexedExpression = NULL;
2060
2061 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2062 {
2063 if (baseExpression->getAsSymbolNode())
2064 {
2065 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2066 }
2067 else
2068 {
2069 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2070 }
2071 recover();
2072 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002073
Jamie Madill21c1e452014-12-29 11:33:41 -05002074 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2075
2076 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002077 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002078 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002079 if (index < 0)
2080 {
2081 std::stringstream infoStream;
2082 infoStream << index;
2083 std::string info = infoStream.str();
2084 error(location, "negative index", info.c_str());
2085 recover();
2086 index = 0;
2087 }
2088 if (baseExpression->getType().getQualifier() == EvqConst)
2089 {
2090 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002091 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002092 // constant folding for arrays
2093 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002094 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002095 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002096 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002097 // constant folding for vectors
2098 TVectorFields fields;
2099 fields.num = 1;
2100 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2101 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2102 }
2103 else if (baseExpression->isMatrix())
2104 {
2105 // constant folding for matrices
2106 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002107 }
2108 }
2109 else
2110 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002111 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002112 {
Jamie Madill18464b52013-07-08 14:01:55 -04002113 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002114 {
2115 std::stringstream extraInfoStream;
2116 extraInfoStream << "array index out of range '" << index << "'";
2117 std::string extraInfo = extraInfoStream.str();
2118 error(location, "", "[", extraInfo.c_str());
2119 recover();
2120 index = baseExpression->getType().getArraySize() - 1;
2121 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002122 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2123 {
2124 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2125 recover();
2126 index = 0;
2127 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002128 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002129 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002130 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002131 std::stringstream extraInfoStream;
2132 extraInfoStream << "field selection out of range '" << index << "'";
2133 std::string extraInfo = extraInfoStream.str();
2134 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002135 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002136 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002137 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002138
Jamie Madill21c1e452014-12-29 11:33:41 -05002139 indexConstantUnion->getUnionArrayPointer()->setIConst(index);
Jamie Madill7164cf42013-07-08 13:30:59 -04002140 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002141 }
2142 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002143 else
2144 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002145 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002146 {
Jamie Madill19571812013-08-12 15:26:34 -07002147 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002148 recover();
2149 }
Jamie Madill19571812013-08-12 15:26:34 -07002150 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002151 {
Jamie Madill19571812013-08-12 15:26:34 -07002152 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002153 recover();
2154 }
2155
2156 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2157 }
2158
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002159 if (indexedExpression == 0)
2160 {
2161 ConstantUnion *unionArray = new ConstantUnion[1];
2162 unionArray->setFConst(0.0f);
2163 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2164 }
2165 else if (baseExpression->isArray())
2166 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002167 const TType &baseType = baseExpression->getType();
2168 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002169 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002170 TType copyOfType(baseType.getStruct());
2171 indexedExpression->setType(copyOfType);
2172 }
2173 else if (baseType.isInterfaceBlock())
2174 {
2175 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002176 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002177 }
2178 else
2179 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002180 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002181 }
2182
2183 if (baseExpression->getType().getQualifier() == EvqConst)
2184 {
2185 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2186 }
2187 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002188 else if (baseExpression->isMatrix())
2189 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002190 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2191 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002192 }
2193 else if (baseExpression->isVector())
2194 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002195 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2196 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002197 }
2198 else
2199 {
2200 indexedExpression->setType(baseExpression->getType());
2201 }
2202
2203 return indexedExpression;
2204}
2205
Jamie Madill075edd82013-07-08 13:30:19 -04002206TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002207{
2208 TIntermTyped *indexedExpression = NULL;
2209
2210 if (baseExpression->isArray())
2211 {
2212 error(fieldLocation, "cannot apply dot operator to an array", ".");
2213 recover();
2214 }
2215
2216 if (baseExpression->isVector())
2217 {
2218 TVectorFields fields;
2219 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2220 {
2221 fields.num = 1;
2222 fields.offsets[0] = 0;
2223 recover();
2224 }
2225
2226 if (baseExpression->getType().getQualifier() == EvqConst)
2227 {
2228 // constant folding for vector fields
2229 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2230 if (indexedExpression == 0)
2231 {
2232 recover();
2233 indexedExpression = baseExpression;
2234 }
2235 else
2236 {
2237 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2238 }
2239 }
2240 else
2241 {
2242 TString vectorString = fieldString;
2243 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2244 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2245 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2246 }
2247 }
2248 else if (baseExpression->isMatrix())
2249 {
2250 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002251 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002252 {
2253 fields.wholeRow = false;
2254 fields.wholeCol = false;
2255 fields.row = 0;
2256 fields.col = 0;
2257 recover();
2258 }
2259
2260 if (fields.wholeRow || fields.wholeCol)
2261 {
2262 error(dotLocation, " non-scalar fields not implemented yet", ".");
2263 recover();
2264 ConstantUnion *unionArray = new ConstantUnion[1];
2265 unionArray->setIConst(0);
2266 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2267 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002268 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002269 }
2270 else
2271 {
2272 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002273 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002274 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2275 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2276 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2277 }
2278 }
2279 else if (baseExpression->getBasicType() == EbtStruct)
2280 {
2281 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002282 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2283 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002284 {
2285 error(dotLocation, "structure has no fields", "Internal Error");
2286 recover();
2287 indexedExpression = baseExpression;
2288 }
2289 else
2290 {
2291 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002293 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002294 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002295 {
2296 fieldFound = true;
2297 break;
2298 }
2299 }
2300 if (fieldFound)
2301 {
2302 if (baseExpression->getType().getQualifier() == EvqConst)
2303 {
2304 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2305 if (indexedExpression == 0)
2306 {
2307 recover();
2308 indexedExpression = baseExpression;
2309 }
2310 else
2311 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002312 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002313 // change the qualifier of the return type, not of the structure field
2314 // as the structure definition is shared between various structures.
2315 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2316 }
2317 }
2318 else
2319 {
2320 ConstantUnion *unionArray = new ConstantUnion[1];
2321 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002322 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002323 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002324 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002325 }
2326 }
2327 else
2328 {
2329 error(dotLocation, " no such field in structure", fieldString.c_str());
2330 recover();
2331 indexedExpression = baseExpression;
2332 }
2333 }
2334 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002335 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002336 {
2337 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002338 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2339 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002340 {
2341 error(dotLocation, "interface block has no fields", "Internal Error");
2342 recover();
2343 indexedExpression = baseExpression;
2344 }
2345 else
2346 {
2347 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002348 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002349 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002350 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002351 {
2352 fieldFound = true;
2353 break;
2354 }
2355 }
2356 if (fieldFound)
2357 {
2358 ConstantUnion *unionArray = new ConstantUnion[1];
2359 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002360 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002361 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002362 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002363 }
2364 else
2365 {
2366 error(dotLocation, " no such field in interface block", fieldString.c_str());
2367 recover();
2368 indexedExpression = baseExpression;
2369 }
2370 }
2371 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002372 else
2373 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002374 if (shaderVersion < 300)
2375 {
2376 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2377 }
2378 else
2379 {
2380 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2381 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002382 recover();
2383 indexedExpression = baseExpression;
2384 }
2385
2386 return indexedExpression;
2387}
2388
Jamie Madill075edd82013-07-08 13:30:19 -04002389TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002390{
Jamie Madilla5efff92013-06-06 11:56:47 -04002391 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002392
Jamie Madilla5efff92013-06-06 11:56:47 -04002393 qualifier.location = -1;
2394 qualifier.matrixPacking = EmpUnspecified;
2395 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002396
2397 if (qualifierType == "shared")
2398 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002399 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002400 }
2401 else if (qualifierType == "packed")
2402 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002403 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002404 }
2405 else if (qualifierType == "std140")
2406 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002407 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002408 }
2409 else if (qualifierType == "row_major")
2410 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002411 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002412 }
2413 else if (qualifierType == "column_major")
2414 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002415 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002416 }
2417 else if (qualifierType == "location")
2418 {
2419 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2420 recover();
2421 }
2422 else
2423 {
2424 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2425 recover();
2426 }
2427
Jamie Madilla5efff92013-06-06 11:56:47 -04002428 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002429}
2430
Jamie Madill075edd82013-07-08 13:30:19 -04002431TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002432{
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002434
Jamie Madilla5efff92013-06-06 11:56:47 -04002435 qualifier.location = -1;
2436 qualifier.matrixPacking = EmpUnspecified;
2437 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002438
2439 if (qualifierType != "location")
2440 {
2441 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2442 recover();
2443 }
2444 else
2445 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002446 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002447 if (intValue < 0)
2448 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002449 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002450 recover();
2451 }
2452 else
2453 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002454 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002455 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002456 }
2457
Jamie Madilla5efff92013-06-06 11:56:47 -04002458 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002459}
2460
Jamie Madilla5efff92013-06-06 11:56:47 -04002461TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002462{
Jamie Madilla5efff92013-06-06 11:56:47 -04002463 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002464
Jamie Madilla5efff92013-06-06 11:56:47 -04002465 if (rightQualifier.location != -1)
2466 {
2467 joinedQualifier.location = rightQualifier.location;
2468 }
2469 if (rightQualifier.matrixPacking != EmpUnspecified)
2470 {
2471 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2472 }
2473 if (rightQualifier.blockStorage != EbsUnspecified)
2474 {
2475 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2476 }
2477
2478 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002479}
2480
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002481TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2482 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2483{
2484 TQualifier mergedQualifier = EvqSmoothIn;
2485
Jamie Madill19571812013-08-12 15:26:34 -07002486 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002487 if (interpolationQualifier == EvqSmooth)
2488 mergedQualifier = EvqSmoothIn;
2489 else if (interpolationQualifier == EvqFlat)
2490 mergedQualifier = EvqFlatIn;
2491 else UNREACHABLE();
2492 }
2493 else if (storageQualifier == EvqCentroidIn) {
2494 if (interpolationQualifier == EvqSmooth)
2495 mergedQualifier = EvqCentroidIn;
2496 else if (interpolationQualifier == EvqFlat)
2497 mergedQualifier = EvqFlatIn;
2498 else UNREACHABLE();
2499 }
Jamie Madill19571812013-08-12 15:26:34 -07002500 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002501 if (interpolationQualifier == EvqSmooth)
2502 mergedQualifier = EvqSmoothOut;
2503 else if (interpolationQualifier == EvqFlat)
2504 mergedQualifier = EvqFlatOut;
2505 else UNREACHABLE();
2506 }
2507 else if (storageQualifier == EvqCentroidOut) {
2508 if (interpolationQualifier == EvqSmooth)
2509 mergedQualifier = EvqCentroidOut;
2510 else if (interpolationQualifier == EvqFlat)
2511 mergedQualifier = EvqFlatOut;
2512 else UNREACHABLE();
2513 }
2514 else {
2515 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2516 recover();
2517
2518 mergedQualifier = storageQualifier;
2519 }
2520
2521 TPublicType type;
2522 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2523 return type;
2524}
2525
Jamie Madill98493dd2013-07-08 14:39:03 -04002526TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002527{
Jamie Madill98493dd2013-07-08 14:39:03 -04002528 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002529 recover();
2530 }
2531
Jamie Madill98493dd2013-07-08 14:39:03 -04002532 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002533 //
2534 // Careful not to replace already known aspects of type, like array-ness
2535 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002536 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002537 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002538 type->setPrimarySize(typeSpecifier.primarySize);
2539 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002540 type->setPrecision(typeSpecifier.precision);
2541 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002542 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002543
2544 // don't allow arrays of arrays
2545 if (type->isArray()) {
2546 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2547 recover();
2548 }
2549 if (typeSpecifier.array)
2550 type->setArraySize(typeSpecifier.arraySize);
2551 if (typeSpecifier.userDef) {
2552 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002553 }
2554
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002556 recover();
2557 }
2558 }
2559
Jamie Madill98493dd2013-07-08 14:39:03 -04002560 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002561}
2562
Jamie Madill98493dd2013-07-08 14:39:03 -04002563TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002564{
Jamie Madill98493dd2013-07-08 14:39:03 -04002565 TStructure* structure = new TStructure(structName, fieldList);
2566 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002567
Jamie Madill9b820842015-02-12 10:40:10 -05002568 // Store a bool in the struct if we're at global scope, to allow us to
2569 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002570 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002571 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002572
Jamie Madill98493dd2013-07-08 14:39:03 -04002573 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002574 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002575 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002576 {
2577 recover();
2578 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002579 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
Nicolas Capensadfffe42014-06-17 02:13:36 -04002580 if (!symbolTable.declare(userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002581 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002582 recover();
2583 }
2584 }
2585
2586 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002587 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002588 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002589 const TField &field = *(*fieldList)[typeListIndex];
2590 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002591 switch (qualifier)
2592 {
2593 case EvqGlobal:
2594 case EvqTemporary:
2595 break;
2596 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002597 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002598 recover();
2599 break;
2600 }
2601 }
2602
2603 TPublicType publicType;
2604 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002605 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002606 exitStructDeclaration();
2607
2608 return publicType;
2609}
2610
Olli Etuahoa3a36662015-02-17 13:46:51 +02002611TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2612{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002613 TBasicType switchType = init->getBasicType();
2614 if ((switchType != EbtInt && switchType != EbtUInt) ||
2615 init->isMatrix() ||
2616 init->isArray() ||
2617 init->isVector())
2618 {
2619 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2620 recover();
2621 return nullptr;
2622 }
2623
Olli Etuahoa3a36662015-02-17 13:46:51 +02002624 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2625 if (node == nullptr)
2626 {
2627 error(loc, "erroneous switch statement", "switch");
2628 recover();
2629 return nullptr;
2630 }
2631 return node;
2632}
2633
2634TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
2635{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002636 if (mSwitchNestingLevel == 0)
2637 {
2638 error(loc, "case labels need to be inside switch statements", "case");
2639 recover();
2640 return nullptr;
2641 }
2642 if (condition == nullptr)
2643 {
2644 error(loc, "case label must have a condition", "case");
2645 recover();
2646 return nullptr;
2647 }
2648 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
2649 condition->isMatrix() ||
2650 condition->isArray() ||
2651 condition->isVector())
2652 {
2653 error(condition->getLine(), "case label must be a scalar integer", "case");
2654 recover();
2655 }
2656 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
2657 if (conditionConst == nullptr)
2658 {
2659 error(condition->getLine(), "case label must be constant", "case");
2660 recover();
2661 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002662 TIntermCase *node = intermediate.addCase(condition, loc);
2663 if (node == nullptr)
2664 {
2665 error(loc, "erroneous case statement", "case");
2666 recover();
2667 return nullptr;
2668 }
2669 return node;
2670}
2671
2672TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
2673{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002674 if (mSwitchNestingLevel == 0)
2675 {
2676 error(loc, "default labels need to be inside switch statements", "default");
2677 recover();
2678 return nullptr;
2679 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02002680 TIntermCase *node = intermediate.addCase(nullptr, loc);
2681 if (node == nullptr)
2682 {
2683 error(loc, "erroneous default statement", "default");
2684 recover();
2685 return nullptr;
2686 }
2687 return node;
2688}
2689
Olli Etuaho09b22472015-02-11 11:47:26 +02002690TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2691{
2692 TIntermTyped *node = intermediate.addUnaryMath(op, child, loc);
2693 if (node == 0)
2694 {
2695 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
2696 recover();
2697 return child;
2698 }
2699 return node;
2700}
2701
2702TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
2703{
2704 if (lValueErrorCheck(loc, GetOperatorString(op), child))
2705 recover();
2706 return addUnaryMath(op, child, loc);
2707}
2708
2709TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
2710 const TSourceLoc &loc)
2711{
2712 TIntermTyped *node = intermediate.addBinaryMath(op, left, right, loc);
2713 if (node == 0)
2714 {
2715 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2716 recover();
2717 return left;
2718 }
2719 return node;
2720}
2721
2722TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
2723 const TSourceLoc &loc)
2724{
2725 TIntermTyped *node = intermediate.addBinaryMath(op, left, right, loc);
2726 if (node == 0)
2727 {
2728 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
2729 recover();
2730 ConstantUnion *unionArray = new ConstantUnion[1];
2731 unionArray->setBConst(false);
2732 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
2733 }
2734 return node;
2735}
2736
Olli Etuaho49300862015-02-20 14:54:49 +02002737TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
2738{
2739 switch (op)
2740 {
2741 case EOpContinue:
2742 if (mLoopNestingLevel <= 0)
2743 {
2744 error(loc, "continue statement only allowed in loops", "");
2745 recover();
2746 }
2747 break;
2748 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02002749 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02002750 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02002751 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02002752 recover();
2753 }
2754 break;
2755 case EOpReturn:
2756 if (currentFunctionType->getBasicType() != EbtVoid)
2757 {
2758 error(loc, "non-void function must return a value", "return");
2759 recover();
2760 }
2761 break;
2762 default:
2763 // No checks for discard
2764 break;
2765 }
2766 return intermediate.addBranch(op, loc);
2767}
2768
2769TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
2770{
2771 ASSERT(op == EOpReturn);
2772 mFunctionReturnsValue = true;
2773 if (currentFunctionType->getBasicType() == EbtVoid)
2774 {
2775 error(loc, "void function cannot return a value", "return");
2776 recover();
2777 }
2778 else if (*currentFunctionType != returnValue->getType())
2779 {
2780 error(loc, "function return is not matching type:", "return");
2781 recover();
2782 }
2783 return intermediate.addBranch(op, returnValue, loc);
2784}
2785
2786
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002788// Parse an array of strings using yyparse.
2789//
2790// Returns 0 for success.
2791//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002792int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002793 TParseContext* context) {
2794 if ((count == 0) || (string == NULL))
2795 return 1;
2796
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002797 if (glslang_initialize(context))
2798 return 1;
2799
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002800 int error = glslang_scan(count, string, length, context);
2801 if (!error)
2802 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002803
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002804 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002805
alokp@chromium.org6b495712012-06-29 00:06:58 +00002806 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002807}
2808
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002810