blob: a2e5d89a706de31bc2abb1a3fba020bffed65da8 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/glslang.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
alokp@chromium.org8b851c62012-06-15 16:25:11 +000015///////////////////////////////////////////////////////////////////////
16//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017// Sub- vector and matrix fields
18//
19////////////////////////////////////////////////////////////////////////
20
21//
22// Look at a '.' field selector string and change it into offsets
23// for a vector.
24//
Jamie Madill075edd82013-07-08 13:30:19 -040025bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000027 fields.num = (int) compString.size();
28 if (fields.num > 4) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000029 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000030 return false;
31 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000033 enum {
34 exyzw,
35 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000036 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000037 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
41 case 'x':
42 fields.offsets[i] = 0;
43 fieldSet[i] = exyzw;
44 break;
45 case 'r':
46 fields.offsets[i] = 0;
47 fieldSet[i] = ergba;
48 break;
49 case 's':
50 fields.offsets[i] = 0;
51 fieldSet[i] = estpq;
52 break;
53 case 'y':
54 fields.offsets[i] = 1;
55 fieldSet[i] = exyzw;
56 break;
57 case 'g':
58 fields.offsets[i] = 1;
59 fieldSet[i] = ergba;
60 break;
61 case 't':
62 fields.offsets[i] = 1;
63 fieldSet[i] = estpq;
64 break;
65 case 'z':
66 fields.offsets[i] = 2;
67 fieldSet[i] = exyzw;
68 break;
69 case 'b':
70 fields.offsets[i] = 2;
71 fieldSet[i] = ergba;
72 break;
73 case 'p':
74 fields.offsets[i] = 2;
75 fieldSet[i] = estpq;
76 break;
77
78 case 'w':
79 fields.offsets[i] = 3;
80 fieldSet[i] = exyzw;
81 break;
82 case 'a':
83 fields.offsets[i] = 3;
84 fieldSet[i] = ergba;
85 break;
86 case 'q':
87 fields.offsets[i] = 3;
88 fieldSet[i] = estpq;
89 break;
90 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000091 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000092 return false;
93 }
94 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000096 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 return false;
100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000102 if (i > 0) {
103 if (fieldSet[i] != fieldSet[i-1]) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000105 return false;
106 }
107 }
108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
113
114//
115// Look at a '.' field selector string and change it into offsets
116// for a matrix.
117//
Jamie Madill075edd82013-07-08 13:30:19 -0400118bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000120 fields.wholeRow = false;
121 fields.wholeCol = false;
122 fields.row = -1;
123 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000125 if (compString.size() != 2) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000126 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000127 return false;
128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000132 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 return false;
134 }
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 return false;
141 }
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
144 } else {
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 return false;
149 }
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
152 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000154 if (fields.row >= matRows || fields.col >= matCols) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000155 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000156 return false;
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
162///////////////////////////////////////////////////////////////////////
163//
164// Errors
165//
166////////////////////////////////////////////////////////////////////////
167
168//
169// Track whether errors have occurred.
170//
171void TParseContext::recover()
172{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173}
174
175//
176// Used by flex/bison to output all syntax and parsing errors.
177//
Jamie Madill075edd82013-07-08 13:30:19 -0400178void TParseContext::error(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000179 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000180 const char* extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000182 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500185 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000186 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Jamie Madill075edd82013-07-08 13:30:19 -0400190void TParseContext::warning(const TSourceLoc& loc,
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000191 const char* reason, const char* token,
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000192 const char* extraInfo) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000193 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
Shannon Woods7f2d7942013-11-19 15:07:58 -0500196 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000197 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000198}
199
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000200void TParseContext::trace(const char* str)
201{
202 diagnostics.writeDebug(str);
203}
204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205//
206// Same error message for all places assignments don't work.
207//
Jamie Madill075edd82013-07-08 13:30:19 -0400208void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
216//
217// Same error message for all places unary operations don't work.
218//
Jamie Madill075edd82013-07-08 13:30:19 -0400219void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226}
227
228//
229// Same error message for all binary operations don't work.
230//
Jamie Madill075edd82013-07-08 13:30:19 -0400231void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
Jamie Madill075edd82013-07-08 13:30:19 -0400240bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000241 if (!checksPrecisionErrors)
242 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000243 switch( type ){
244 case EbtFloat:
245 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000246 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 return true;
248 }
249 break;
250 case EbtInt:
251 if( precision == EbpUndefined ){
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253 return true;
254 }
255 break;
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000256 default:
257 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000258 }
259 return false;
260}
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262//
263// Both test and if necessary, spit out an error, to see if the node is really
264// an l-value that can be operated on this way.
265//
266// Returns true if the was an error.
267//
Jamie Madill075edd82013-07-08 13:30:19 -0400268bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 if (binaryNode) {
274 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 switch(binaryNode->getOp()) {
277 case EOpIndexDirect:
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000280 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
284 if (!errorReturn) {
285 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
289
290 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
291 p != aggrNode->getSequence().end(); p++) {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 offset[value]++;
294 if (offset[value] > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 return true;
298 }
299 }
300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return errorReturn;
303 default:
304 break;
305 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000306 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 return true;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
311
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 const char* symbol = 0;
313 if (symNode != 0)
314 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
Jamie Madill19571812013-08-12 15:26:34 -0700321 case EvqFragmentIn: message = "can't modify an input"; break;
322 case EvqVertexIn: message = "can't modify an input"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 case EvqUniform: message = "can't modify a uniform"; break;
324 case EvqVaryingIn: message = "can't modify a varying"; break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
326 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
327 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
328 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 //
331 // Type that can't be written to?
332 //
Nicolas Capens344e7142013-06-24 15:39:21 -0400333 if (node->getBasicType() == EbtVoid) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000334 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400335 }
336 if (IsSampler(node->getBasicType())) {
337 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 }
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 if (message == 0 && binaryNode == 0 && symNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000342 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 return true;
345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000348 //
349 // Everything else is okay, no error.
350 //
351 if (message == 0)
352 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 //
355 // If we get here, we have an error and a message.
356 //
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000357 if (symNode) {
358 std::stringstream extraInfoStream;
359 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
360 std::string extraInfo = extraInfoStream.str();
361 error(line, " l-value required", op, extraInfo.c_str());
362 }
363 else {
364 std::stringstream extraInfoStream;
365 extraInfoStream << "(" << message << ")";
366 std::string extraInfo = extraInfoStream.str();
367 error(line, " l-value required", op, extraInfo.c_str());
368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371}
372
373//
374// Both test, and if necessary spit out an error, to see if the node is really
375// a constant.
376//
377// Returns true if the was an error.
378//
379bool TParseContext::constErrorCheck(TIntermTyped* node)
380{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000381 if (node->getQualifier() == EvqConst)
382 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000384 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000386 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389//
390// Both test, and if necessary spit out an error, to see if the node is really
391// an integer.
392//
393// Returns true if the was an error.
394//
395bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
396{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000397 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000400 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
405//
406// Both test, and if necessary spit out an error, to see if we are currently
407// globally scoped.
408//
409// Returns true if the was an error.
410//
Jamie Madill075edd82013-07-08 13:30:19 -0400411bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000413 if (global)
414 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000416 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000418 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419}
420
421//
422// For now, keep it simple: if it starts "gl_", it's reserved, independent
423// of scope. Except, if the symbol table is at the built-in push-level,
424// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000425// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
426// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427//
428// Returns true if there was an error.
429//
Jamie Madill075edd82013-07-08 13:30:19 -0400430bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
alokp@chromium.org613ef312010-07-21 18:54:22 +0000432 static const char* reservedErrMsg = "reserved built-in name";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 if (!symbolTable.atBuiltInLevel()) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000434 if (identifier.compare(0, 3, "gl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000435 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 return true;
437 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000438 if (isWebGLBasedSpec(shaderSpec)) {
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000439 if (identifier.compare(0, 6, "webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000440 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000441 return true;
442 }
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +0000443 if (identifier.compare(0, 7, "_webgl_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000444 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000445 return true;
446 }
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000447 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000448 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000449 return true;
450 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000451 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 if (identifier.find("__") != TString::npos) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000454 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 }
456 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000458 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000459}
460
461//
462// Make sure there is enough data provided to the constructor to build
463// something of the type of the constructor. Also returns the type of
464// the constructor.
465//
466// Returns true if there was an error in construction.
467//
Jamie Madill075edd82013-07-08 13:30:19 -0400468bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 bool constructingMatrix = false;
473 switch(op) {
474 case EOpConstructMat2:
475 case EOpConstructMat3:
476 case EOpConstructMat4:
477 constructingMatrix = true;
478 break;
479 default:
480 break;
481 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 //
484 // Note: It's okay to have too many components available, but not okay to have unused
485 // arguments. 'full' will go to true when enough args have been seen. If we loop
486 // again, there is an extra argument, so 'overfull' will become true.
487 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488
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
Jamie Madill98493dd2013-07-08 14:39:03 -0400536 if (op == EOpConstructStruct && !type->isArray() && int(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 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
644 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
Jamie Madill075edd82013-07-08 13:30:19 -0400649bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400650{
651 if (pType.layoutQualifier.location != -1)
652 {
653 error(line, "location must only be specified for a single input or output variable", "location");
654 return true;
655 }
656
657 return false;
658}
659
Jamie Madill075edd82013-07-08 13:30:19 -0400660bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000662 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
663 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000664 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return true;
666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
671bool TParseContext::containsSampler(TType& type)
672{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 if (IsSampler(type.getBasicType()))
674 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675
Jamie Madill98493dd2013-07-08 14:39:03 -0400676 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
677 const TFieldList& fields = type.getStruct()->fields();
678 for (unsigned int i = 0; i < fields.size(); ++i) {
679 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return true;
681 }
682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685}
686
687//
688// Do size checking for an array type's size.
689//
690// Returns true if there was an error.
691//
Jamie Madill075edd82013-07-08 13:30:19 -0400692bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 TIntermConstantUnion* constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000695
696 if (constant == 0 || !constant->isScalarInt())
697 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000698 error(line, "array size must be a constant integer expression", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000699 return true;
700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000702 if (constant->getBasicType() == EbtUInt)
703 {
704 unsigned int uintSize = constant->getUConst(0);
705 if (uintSize > static_cast<unsigned int>(std::numeric_limits<int>::max()))
706 {
707 error(line, "array size too large", "");
708 size = 1;
709 return true;
710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000712 size = static_cast<int>(uintSize);
713 }
714 else
715 {
716 size = constant->getIConst(0);
717
718 if (size <= 0)
719 {
720 error(line, "array size must be a positive integer", "");
721 size = 1;
722 return true;
723 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
729//
730// See if this qualifier can be an array.
731//
732// Returns true if there is an error.
733//
Jamie Madill075edd82013-07-08 13:30:19 -0400734bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Jamie Madill19571812013-08-12 15:26:34 -0700736 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000737 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 return true;
739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
744//
745// See if this type can be an array.
746//
747// Returns true if there is an error.
748//
Jamie Madill075edd82013-07-08 13:30:19 -0400749bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 //
752 // Can the type be an array?
753 //
754 if (type.array) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000755 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 return true;
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
762//
763// Do all the semantic checking for declaring an array, with and
764// without a size, and make the right changes to the symbol table.
765//
766// size == 0 means no specified size.
767//
768// Returns true if there was an error.
769//
Jamie Madill075edd82013-07-08 13:30:19 -0400770bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 //
773 // Don't check for reserved word use until after we know it's not in the symbol table,
774 // because reserved arrays can be redeclared.
775 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 bool builtIn = false;
778 bool sameScope = false;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000779 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 if (symbol == 0 || !sameScope) {
781 if (reservedErrorCheck(line, identifier))
782 return true;
783
784 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000786 if (type.arraySize)
787 variable->getType().setArraySize(type.arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000789 if (! symbolTable.declare(*variable)) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000790 delete variable;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000791 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000792 return true;
793 }
794 } else {
795 if (! symbol->isVariable()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000796 error(line, "variable expected", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000797 return true;
798 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 variable = static_cast<TVariable*>(symbol);
801 if (! variable->getType().isArray()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000802 error(line, "redeclaring non-array as array", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 return true;
804 }
805 if (variable->getType().getArraySize() > 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000806 error(line, "redeclaration of array with size", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 return true;
808 }
809
810 if (! variable->getType().sameElementType(TType(type))) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000811 error(line, "redeclaration of array with a different type", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 return true;
813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 if (type.arraySize)
816 variable->getType().setArraySize(type.arraySize);
817 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 if (voidErrorCheck(line, identifier, type))
820 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000822 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823}
824
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825//
826// Enforce non-initializer type/qualifier rules.
827//
828// Returns true if there was an error.
829//
Jamie Madill075edd82013-07-08 13:30:19 -0400830bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831{
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000832 if (type.qualifier == EvqConst)
833 {
834 // Make the qualifier make sense.
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000835 type.qualifier = EvqTemporary;
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000836
837 if (array)
838 {
839 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
840 }
841 else if (type.isStructureContainingArrays())
842 {
843 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
844 }
845 else
846 {
847 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
848 }
849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 return true;
851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
856//
857// Do semantic checking for a variable declaration that has no initializer,
858// and update the symbol table.
859//
860// Returns true if there was an error.
861//
Jamie Madill075edd82013-07-08 13:30:19 -0400862bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000864 if (reservedErrorCheck(line, identifier))
865 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866
zmo@google.comfd747b82011-04-23 01:30:07 +0000867 variable = new TVariable(&identifier, TType(type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000869 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000871 delete variable;
zmo@google.comfd747b82011-04-23 01:30:07 +0000872 variable = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 return true;
874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000876 if (voidErrorCheck(line, identifier, type))
877 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000879 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880}
881
Jamie Madill075edd82013-07-08 13:30:19 -0400882bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 if (qualifier != EvqConst && qualifier != EvqTemporary) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000885 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 return true;
887 }
888 if (qualifier == EvqConst && paramQualifier != EvqIn) {
889 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
890 return true;
891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000894 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000895 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000896 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
Jamie Madill075edd82013-07-08 13:30:19 -0400901bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000902{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000903 const TExtensionBehavior& extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000904 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
905 if (iter == extBehavior.end()) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000906 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000907 return true;
908 }
zmo@google.comf5450912011-09-09 01:37:19 +0000909 // In GLSL ES, an extension's default behavior is "disable".
910 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000911 error(line, "extension", extension.c_str(), "is disabled");
912 return true;
913 }
914 if (iter->second == EBhWarn) {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000915 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000916 return false;
917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000919 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920}
921
Jamie Madill075edd82013-07-08 13:30:19 -0400922bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400923{
924 if (structQualifierErrorCheck(identifierLocation, publicType))
925 return true;
926
927 // check for layout qualifier issues
928 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
929
930 if (layoutQualifier.matrixPacking != EmpUnspecified)
931 {
932 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400933 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400934 }
935
936 if (layoutQualifier.blockStorage != EbsUnspecified)
937 {
938 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -0400939 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400940 }
941
Jamie Madill19571812013-08-12 15:26:34 -0700942 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -0400943 {
Jamie Madill51a53c72013-06-19 09:24:43 -0400944 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -0400945 }
946
947 return false;
948}
949
Jamie Madill075edd82013-07-08 13:30:19 -0400950bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -0400951{
952 if (layoutQualifier.location != -1)
953 {
954 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
955 return true;
956 }
957
958 return false;
959}
960
zmo@google.com09c323a2011-08-12 18:22:25 +0000961bool TParseContext::supportsExtension(const char* extension)
962{
alokp@chromium.org73bc2982012-06-19 18:48:05 +0000963 const TExtensionBehavior& extbehavior = extensionBehavior();
964 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
965 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000966}
967
Jamie Madill5d287f52013-07-12 15:38:19 -0400968bool TParseContext::isExtensionEnabled(const char* extension) const
969{
970 const TExtensionBehavior& extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -0400971 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -0400972
973 if (iter == extbehavior.end())
974 {
975 return false;
976 }
977
978 return (iter->second == EBhEnable || iter->second == EBhRequire);
979}
980
Jamie Madill075edd82013-07-08 13:30:19 -0400981void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
982{
983 pp::SourceLocation srcLoc;
984 srcLoc.file = loc.first_file;
985 srcLoc.line = loc.first_line;
986 directiveHandler.handleExtension(srcLoc, extName, behavior);
987}
988
989void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value)
990{
991 pp::SourceLocation srcLoc;
992 srcLoc.file = loc.first_file;
993 srcLoc.line = loc.first_line;
994 directiveHandler.handlePragma(srcLoc, name, value);
995}
996
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997/////////////////////////////////////////////////////////////////////////////////
998//
999// Non-Errors.
1000//
1001/////////////////////////////////////////////////////////////////////////////////
1002
1003//
1004// Look up a function name in the symbol table, and make sure it is a function.
1005//
1006// Return the function symbol if found, otherwise 0.
1007//
Jamie Madill075edd82013-07-08 13:30:19 -04001008const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001010 // First find by unmangled name to check whether the function name has been
1011 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001012 // If a function is found, check for one with a matching argument list.
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001013 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001014 if (symbol == 0 || symbol->isFunction()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001015 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017
alokp@chromium.org0a576182010-08-09 17:16:27 +00001018 if (symbol == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001019 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001020 return 0;
1021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
alokp@chromium.org0a576182010-08-09 17:16:27 +00001023 if (!symbol->isFunction()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001024 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001025 return 0;
1026 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001027
1028 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
1031//
1032// Initializers show up in several places in the grammar. Have one set of
1033// code to handle them here.
1034//
Jamie Madill075edd82013-07-08 13:30:19 -04001035bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001036 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 if (variable == 0) {
1041 if (reservedErrorCheck(line, identifier))
1042 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001044 if (voidErrorCheck(line, identifier, pType))
1045 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001047 //
1048 // add variable to symbol table
1049 //
1050 variable = new TVariable(&identifier, type);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +00001051 if (! symbolTable.declare(*variable)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001052 error(line, "redefinition", variable->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001053 return true;
1054 // don't delete variable, it's used by error recovery, and the pool
1055 // pop will take care of the memory
1056 }
1057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001059 //
1060 // identifier must be of type constant, a global, or a temporary
1061 //
1062 TQualifier qualifier = variable->getType().getQualifier();
1063 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001064 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001065 return true;
1066 }
1067 //
1068 // test for and propagate constant
1069 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001071 if (qualifier == EvqConst) {
1072 if (qualifier != initializer->getType().getQualifier()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001073 std::stringstream extraInfoStream;
1074 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1075 std::string extraInfo = extraInfoStream.str();
1076 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001077 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 return true;
1079 }
1080 if (type != initializer->getType()) {
1081 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001082 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001083 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001084 return true;
1085 }
1086 if (initializer->getAsConstantUnion()) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001087 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001088 } else if (initializer->getAsSymbolNode()) {
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00001089 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001090 const TVariable* tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001092 ConstantUnion* constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001093 variable->shareConstPointer(constArray);
1094 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001095 std::stringstream extraInfoStream;
1096 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1097 std::string extraInfo = extraInfoStream.str();
1098 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001099 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 return true;
1101 }
1102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001104 if (qualifier != EvqConst) {
1105 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1106 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1107 if (intermNode == 0) {
1108 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1109 return true;
1110 }
1111 } else
1112 intermNode = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115}
1116
1117bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1118{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001119 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 if (!aggrNode->isConstructor())
1121 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001123 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001125 // check if all the child nodes are constants so that they can be inserted into
1126 // the parent node
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001127 TIntermSequence &sequence = aggrNode->getSequence() ;
1128 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1129 if (!(*p)->getAsTyped()->getAsConstantUnion())
1130 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001131 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001133 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134}
1135
Jamie Madilla5efff92013-06-06 11:56:47 -04001136TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001137{
1138 TPublicType returnType = typeSpecifier;
1139 returnType.qualifier = qualifier;
Jamie Madilla5efff92013-06-06 11:56:47 -04001140 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001141
1142 if (typeSpecifier.array)
1143 {
1144 error(typeSpecifier.line, "not supported", "first-class array");
1145 recover();
1146 returnType.setArray(false);
1147 }
1148
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001149 if (shaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001150 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001151 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1152 {
1153 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1154 recover();
1155 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001156
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001157 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1158 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1159 {
1160 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1161 recover();
1162 }
1163 }
1164 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001165 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001166 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001167 {
Jamie Madill19571812013-08-12 15:26:34 -07001168 case EvqSmoothIn:
1169 case EvqSmoothOut:
1170 case EvqVertexOut:
1171 case EvqFragmentIn:
1172 case EvqCentroidOut:
1173 case EvqCentroidIn:
1174 if (typeSpecifier.type == EbtBool)
1175 {
1176 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1177 recover();
1178 }
1179 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1180 {
1181 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1182 recover();
1183 }
1184 break;
1185
1186 case EvqVertexIn:
1187 case EvqFragmentOut:
1188 case EvqFlatIn:
1189 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001190 if (typeSpecifier.type == EbtBool)
1191 {
1192 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1193 recover();
1194 }
1195 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001196
Jamie Madillb120eac2013-06-12 14:08:13 -04001197 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001198 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001199 }
1200
1201 return returnType;
1202}
1203
Jamie Madill075edd82013-07-08 13:30:19 -04001204TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001205{
1206 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1207 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1208
1209 if (identifier != "")
1210 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001211 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001212 recover();
1213
1214 // this error check can mutate the type
1215 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1216 recover();
1217
1218 TVariable* variable = 0;
1219
1220 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1221 recover();
1222
1223 if (variable && symbol)
1224 {
1225 symbol->setId(variable->getUniqueId());
1226 }
1227 }
1228
1229 return aggregate;
1230}
1231
Jamie Madill075edd82013-07-08 13:30:19 -04001232TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001233{
Jamie Madill51a53c72013-06-19 09:24:43 -04001234 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001235 recover();
1236
1237 // this error check can mutate the type
1238 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1239 recover();
1240
1241 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1242 {
1243 recover();
1244 }
1245
1246 TPublicType arrayType = publicType;
1247
1248 int size;
1249 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1250 {
1251 recover();
1252 }
1253 else
1254 {
1255 arrayType.setArray(true, size);
1256 }
1257
1258 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1259 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1260 TVariable* variable = 0;
1261
1262 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1263 recover();
1264
1265 if (variable && symbol)
1266 {
1267 symbol->setId(variable->getUniqueId());
1268 }
1269
1270 return aggregate;
1271}
1272
Jamie Madill075edd82013-07-08 13:30:19 -04001273TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001274{
Jamie Madilla5efff92013-06-06 11:56:47 -04001275 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
Jamie Madill60ed9812013-06-06 11:56:46 -04001276 recover();
1277
1278 TIntermNode* intermNode;
1279 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1280 {
1281 //
1282 // Build intermediate representation
1283 //
1284 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1285 }
1286 else
1287 {
1288 recover();
1289 return NULL;
1290 }
1291}
1292
Jamie Madill075edd82013-07-08 13:30:19 -04001293TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001294{
1295 if (publicType.type == EbtInvariant && !identifierSymbol)
1296 {
1297 error(identifierLocation, "undeclared identifier declared as invariant", identifier.c_str());
1298 recover();
1299 }
1300
1301 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1302 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1303
1304 if (structQualifierErrorCheck(identifierLocation, publicType))
1305 recover();
1306
Jamie Madill0bd18df2013-06-20 11:55:52 -04001307 if (locationDeclaratorListCheck(identifierLocation, publicType))
1308 recover();
1309
Jamie Madill502d66f2013-06-20 11:55:52 -04001310 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1311 recover();
1312
1313 TVariable* variable = 0;
1314 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1315 recover();
1316 if (symbol && variable)
1317 symbol->setId(variable->getUniqueId());
1318
1319 return intermAggregate;
1320}
1321
Jamie Madill075edd82013-07-08 13:30:19 -04001322TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001323{
1324 if (structQualifierErrorCheck(identifierLocation, publicType))
1325 recover();
1326
Jamie Madill0bd18df2013-06-20 11:55:52 -04001327 if (locationDeclaratorListCheck(identifierLocation, publicType))
1328 recover();
1329
Jamie Madill502d66f2013-06-20 11:55:52 -04001330 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1331 recover();
1332
1333 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1334 {
1335 recover();
1336 }
1337 else if (indexExpression)
1338 {
1339 int size;
1340 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1341 recover();
1342 TPublicType arrayType(publicType);
1343 arrayType.setArray(true, size);
1344 TVariable* variable = NULL;
1345 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1346 recover();
1347 TType type = TType(arrayType);
1348 type.setArraySize(size);
1349
1350 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1351 }
1352 else
1353 {
1354 TPublicType arrayType(publicType);
1355 arrayType.setArray(true);
1356 TVariable* variable = NULL;
1357 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1358 recover();
1359 }
1360
1361 return NULL;
1362}
1363
Jamie Madill075edd82013-07-08 13:30:19 -04001364TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001365{
1366 if (structQualifierErrorCheck(identifierLocation, publicType))
1367 recover();
1368
Jamie Madill0bd18df2013-06-20 11:55:52 -04001369 if (locationDeclaratorListCheck(identifierLocation, publicType))
1370 recover();
1371
Jamie Madill502d66f2013-06-20 11:55:52 -04001372 TIntermNode* intermNode;
1373 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1374 {
1375 //
1376 // build the intermediate representation
1377 //
1378 if (intermNode)
1379 {
1380 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1381 }
1382 else
1383 {
1384 return declaratorList;
1385 }
1386 }
1387 else
1388 {
1389 recover();
1390 return NULL;
1391 }
1392}
1393
Jamie Madilla295edf2013-06-06 11:56:48 -04001394void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1395{
1396 if (typeQualifier.qualifier != EvqUniform)
1397 {
1398 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1399 recover();
1400 return;
1401 }
1402
1403 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1404 ASSERT(!layoutQualifier.isEmpty());
1405
1406 if (shaderVersion < 300)
1407 {
1408 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1409 recover();
1410 return;
1411 }
1412
1413 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1414 {
1415 recover();
1416 return;
1417 }
1418
Jamie Madill099c0f32013-06-20 11:55:52 -04001419 if (layoutQualifier.matrixPacking != EmpUnspecified)
1420 {
1421 defaultMatrixPacking = layoutQualifier.matrixPacking;
1422 }
1423
Jamie Madill1566ef72013-06-20 11:55:54 -04001424 if (layoutQualifier.blockStorage != EmpUnspecified)
1425 {
1426 defaultBlockStorage = layoutQualifier.blockStorage;
1427 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001428}
1429
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001430TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1431{
1432 TOperator op = EOpNull;
1433 if (publicType.userDef)
1434 {
1435 op = EOpConstructStruct;
1436 }
1437 else
1438 {
1439 switch (publicType.type)
1440 {
1441 case EbtFloat:
1442 if (publicType.isMatrix())
1443 {
1444 // TODO: non-square matrices
1445 switch(publicType.getCols())
1446 {
Jamie Madill28b97422013-07-08 14:01:38 -04001447 case 2: op = EOpConstructMat2; break;
1448 case 3: op = EOpConstructMat3; break;
1449 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001450 }
1451 }
1452 else
1453 {
1454 switch(publicType.getNominalSize())
1455 {
Jamie Madill28b97422013-07-08 14:01:38 -04001456 case 1: op = EOpConstructFloat; break;
1457 case 2: op = EOpConstructVec2; break;
1458 case 3: op = EOpConstructVec3; break;
1459 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001460 }
1461 }
1462 break;
1463
1464 case EbtInt:
1465 switch(publicType.getNominalSize())
1466 {
Jamie Madill28b97422013-07-08 14:01:38 -04001467 case 1: op = EOpConstructInt; break;
1468 case 2: op = EOpConstructIVec2; break;
1469 case 3: op = EOpConstructIVec3; break;
1470 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001471 }
1472 break;
1473
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001474 case EbtUInt:
1475 switch(publicType.getNominalSize())
1476 {
Jamie Madill28b97422013-07-08 14:01:38 -04001477 case 1: op = EOpConstructUInt; break;
1478 case 2: op = EOpConstructUVec2; break;
1479 case 3: op = EOpConstructUVec3; break;
1480 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001481 }
1482 break;
1483
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001484 case EbtBool:
1485 switch(publicType.getNominalSize())
1486 {
Jamie Madill28b97422013-07-08 14:01:38 -04001487 case 1: op = EOpConstructBool; break;
1488 case 2: op = EOpConstructBVec2; break;
1489 case 3: op = EOpConstructBVec3; break;
1490 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001491 }
1492 break;
1493
1494 default: break;
1495 }
1496
1497 if (op == EOpNull)
1498 {
1499 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1500 recover();
1501 publicType.type = EbtFloat;
1502 op = EOpConstructFloat;
1503 }
1504 }
1505
1506 TString tempString;
1507 TType type(publicType);
1508 return new TFunction(&tempString, type, op);
1509}
1510
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511// This function is used to test for the correctness of the parameters passed to various constructor functions
1512// and also convert them to the right datatype if it is allowed and required.
1513//
1514// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1515//
Jamie Madill075edd82013-07-08 13:30:19 -04001516TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001518 if (node == 0)
1519 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001521 TIntermAggregate* aggrNode = node->getAsAggregate();
1522
Jamie Madill98493dd2013-07-08 14:39:03 -04001523 TFieldList::const_iterator memberTypes;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001524 if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001525 memberTypes = type->getStruct()->fields().begin();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001526
1527 TType elementType = *type;
1528 if (type->isArray())
1529 elementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001531 bool singleArg;
1532 if (aggrNode) {
1533 if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1)
1534 singleArg = true;
1535 else
1536 singleArg = false;
1537 } else
1538 singleArg = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001540 TIntermTyped *newNode;
1541 if (singleArg) {
1542 // If structure constructor or array constructor is being called
1543 // for only one parameter inside the structure, we need to call constructStruct function once.
1544 if (type->isArray())
1545 newNode = constructStruct(node, &elementType, 1, node->getLine(), false);
1546 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001547 newNode = constructStruct(node, (*memberTypes)->type(), 1, node->getLine(), false);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001548 else
1549 newNode = constructBuiltIn(type, op, node, node->getLine(), false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001551 if (newNode && newNode->getAsAggregate()) {
1552 TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type);
1553 if (constConstructor)
1554 return constConstructor;
1555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001557 return newNode;
1558 }
1559
1560 //
1561 // Handle list of arguments.
1562 //
1563 TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor
1564 // if the structure constructor contains more than one parameter, then construct
1565 // each parameter
1566
1567 int paramCount = 0; // keeps a track of the constructor parameter number being checked
1568
1569 // for each parameter to the constructor call, check to see if the right type is passed or convert them
1570 // to the right type if possible (and allowed).
1571 // for structure constructors, just check if the right type is passed, no conversion is allowed.
1572
1573 for (TIntermSequence::iterator p = sequenceVector.begin();
1574 p != sequenceVector.end(); p++, paramCount++) {
1575 if (type->isArray())
1576 newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true);
1577 else if (op == EOpConstructStruct)
Jamie Madill98493dd2013-07-08 14:39:03 -04001578 newNode = constructStruct(*p, (memberTypes[paramCount])->type(), paramCount+1, node->getLine(), true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001579 else
1580 newNode = constructBuiltIn(type, op, *p, node->getLine(), true);
1581
1582 if (newNode) {
1583 *p = newNode;
1584 }
1585 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001587 TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line);
1588 TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1589 if (constConstructor)
1590 return constConstructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001592 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593}
1594
1595TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1596{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001597 bool canBeFolded = areAllChildConst(aggrNode);
1598 aggrNode->setType(type);
1599 if (canBeFolded) {
1600 bool returnVal = false;
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001601 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001602 if (aggrNode->getSequence().size() == 1) {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001603 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001604 }
1605 else {
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001606 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001607 }
1608 if (returnVal)
1609 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001611 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1612 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001614 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615}
1616
1617// Function for constructor implementation. Calls addUnaryMath with appropriate EOp value
1618// for the parameter to the constructor (passed to this function). Essentially, it converts
1619// the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a
1620// float, then float is converted to int.
1621//
1622// Returns 0 for an error or the constructed node.
1623//
Jamie Madill075edd82013-07-08 13:30:19 -04001624TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 TIntermTyped* newNode;
1627 TOperator basicOp;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001629 //
1630 // First, convert types as needed.
1631 //
1632 switch (op) {
1633 case EOpConstructVec2:
1634 case EOpConstructVec3:
1635 case EOpConstructVec4:
1636 case EOpConstructMat2:
1637 case EOpConstructMat3:
1638 case EOpConstructMat4:
1639 case EOpConstructFloat:
1640 basicOp = EOpConstructFloat;
1641 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001643 case EOpConstructIVec2:
1644 case EOpConstructIVec3:
1645 case EOpConstructIVec4:
1646 case EOpConstructInt:
1647 basicOp = EOpConstructInt;
1648 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001650 case EOpConstructUVec2:
1651 case EOpConstructUVec3:
1652 case EOpConstructUVec4:
Nicolas Capensab60b932013-06-05 10:31:21 -04001653 case EOpConstructUInt:
1654 basicOp = EOpConstructUInt;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001655 break;
1656
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001657 case EOpConstructBVec2:
1658 case EOpConstructBVec3:
1659 case EOpConstructBVec4:
1660 case EOpConstructBool:
1661 basicOp = EOpConstructBool;
1662 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001664 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001665 error(line, "unsupported construction", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001666 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001668 return 0;
1669 }
shannonwoods@chromium.org298f9072013-05-30 00:21:17 +00001670 newNode = intermediate.addUnaryMath(basicOp, node, node->getLine());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001671 if (newNode == 0) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001672 error(line, "can't convert", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001673 return 0;
1674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001676 //
1677 // Now, if there still isn't an operation to do the construction, and we need one, add one.
1678 //
1679
1680 // Otherwise, skip out early.
1681 if (subset || (newNode != node && newNode->getType() == *type))
1682 return newNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001684 // setAggregateOperator will insert a new node for the constructor, as needed.
1685 return intermediate.setAggregateOperator(newNode, op, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686}
1687
1688// This function tests for the type of the parameters to the structures constructors. Raises
1689// an error message if the expected type does not match the parameter passed to the constructor.
1690//
1691// Returns 0 for an error or the input node itself if the expected and the given parameter types match.
1692//
Jamie Madill075edd82013-07-08 13:30:19 -04001693TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001695 if (*type == node->getAsTyped()->getType()) {
1696 if (subset)
1697 return node->getAsTyped();
1698 else
1699 return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line);
1700 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001701 std::stringstream extraInfoStream;
1702 extraInfoStream << "cannot convert parameter " << paramCount
1703 << " from '" << node->getAsTyped()->getType().getBasicString()
1704 << "' to '" << type->getBasicString() << "'";
1705 std::string extraInfo = extraInfoStream.str();
1706 error(line, "", "constructor", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001707 recover();
1708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001710 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711}
1712
1713//
1714// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1715// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1716// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1717// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1718// a constant matrix.
1719//
Jamie Madill075edd82013-07-08 13:30:19 -04001720TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001722 TIntermTyped* typedNode;
1723 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001725 ConstantUnion *unionArray;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001726 if (tempConstantNode) {
1727 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001729 if (!unionArray) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001730 return node;
1731 }
1732 } 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 +00001733 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 return 0;
1737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001739 ConstantUnion* constArray = new ConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001741 for (int i = 0; i < fields.num; i++) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001742 if (fields.offsets[i] >= node->getType().getNominalSize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001743 std::stringstream extraInfoStream;
1744 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1745 std::string extraInfo = extraInfoStream.str();
1746 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001747 recover();
1748 fields.offsets[i] = 0;
1749 }
1750
1751 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001753 }
1754 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1755 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756}
1757
1758//
1759// This function returns the column being accessed from a constant matrix. The values are retrieved from
1760// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1761// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1762// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1763//
Jamie Madill075edd82013-07-08 13:30:19 -04001764TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001766 TIntermTyped* typedNode;
1767 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001769 if (index >= node->getType().getCols()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001770 std::stringstream extraInfoStream;
1771 extraInfoStream << "matrix field selection out of range '" << index << "'";
1772 std::string extraInfo = extraInfoStream.str();
1773 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001774 recover();
1775 index = 0;
1776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001778 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001779 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00001780 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001781 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1782 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001783 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001784 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 return 0;
1787 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001788
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001789 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790}
1791
1792
1793//
1794// This function returns an element of an array accessed from a constant array. The values are retrieved from
1795// the symbol table and parse-tree is built for the type of the element. The input
1796// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1797// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1798//
Jamie Madill075edd82013-07-08 13:30:19 -04001799TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001801 TIntermTyped* typedNode;
1802 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1803 TType arrayElementType = node->getType();
1804 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 if (index >= node->getType().getArraySize()) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001807 std::stringstream extraInfoStream;
1808 extraInfoStream << "array field selection out of range '" << index << "'";
1809 std::string extraInfo = extraInfoStream.str();
1810 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001811 recover();
1812 index = 0;
1813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001815 if (tempConstantNode) {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001816 size_t arrayElementSize = arrayElementType.getObjectSize();
1817 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1818 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001819 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001820 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001821 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001823 return 0;
1824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001826 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827}
1828
1829
1830//
1831// This function returns the value of a particular field inside a constant structure from the symbol table.
1832// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1833// function and returns the parse-tree with the values of the embedded/nested struct.
1834//
Jamie Madill075edd82013-07-08 13:30:19 -04001835TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836{
Jamie Madill98493dd2013-07-08 14:39:03 -04001837 const TFieldList& fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04001838 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
Jamie Madill98493dd2013-07-08 14:39:03 -04001840 for (size_t index = 0; index < fields.size(); ++index) {
1841 if (fields[index]->name() == identifier) {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001842 break;
1843 } else {
Jamie Madill98493dd2013-07-08 14:39:03 -04001844 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001845 }
1846 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001847
Jamie Madill94bf7f22013-07-08 13:31:15 -04001848 TIntermTyped *typedNode;
1849 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001850 if (tempConstantNode) {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +00001851 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001853 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1854 } else {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001855 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001858 return 0;
1859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001861 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862}
1863
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001864//
1865// Interface/uniform blocks
1866//
Jamie Madill98493dd2013-07-08 14:39:03 -04001867TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1868 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001869{
1870 if (reservedErrorCheck(nameLine, blockName))
1871 recover();
1872
1873 if (typeQualifier.qualifier != EvqUniform)
1874 {
1875 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1876 recover();
1877 }
1878
Jamie Madill099c0f32013-06-20 11:55:52 -04001879 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1880 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001881 {
1882 recover();
1883 }
1884
Jamie Madill099c0f32013-06-20 11:55:52 -04001885 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1886 {
1887 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1888 }
1889
Jamie Madill1566ef72013-06-20 11:55:54 -04001890 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1891 {
1892 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1893 }
1894
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001895 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1896 if (!symbolTable.declare(*blockNameSymbol)) {
1897 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1898 recover();
1899 }
1900
Jamie Madill98493dd2013-07-08 14:39:03 -04001901 // check for sampler types and apply layout qualifiers
1902 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1903 TField* field = (*fieldList)[memberIndex];
1904 TType* fieldType = field->type();
1905 if (IsSampler(fieldType->getBasicType())) {
1906 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001907 recover();
1908 }
1909
Jamie Madill98493dd2013-07-08 14:39:03 -04001910 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001911 switch (qualifier)
1912 {
1913 case EvqGlobal:
1914 case EvqUniform:
1915 break;
1916 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04001917 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001918 recover();
1919 break;
1920 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001921
1922 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04001923 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1924 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001925 {
1926 recover();
1927 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001928
Jamie Madill98493dd2013-07-08 14:39:03 -04001929 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001930 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001931 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04001932 recover();
1933 }
1934
Jamie Madill98493dd2013-07-08 14:39:03 -04001935 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04001936 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001937 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001938 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001939 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04001940 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001941 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04001942 recover();
1943 }
1944
Jamie Madill98493dd2013-07-08 14:39:03 -04001945 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001946 }
1947
Jamie Madill98493dd2013-07-08 14:39:03 -04001948 // add array index
1949 int arraySize = 0;
1950 if (arrayIndex != NULL)
1951 {
1952 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1953 recover();
1954 }
1955
1956 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1957 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001958
1959 TString symbolName = "";
1960 int symbolId = 0;
1961
Jamie Madill98493dd2013-07-08 14:39:03 -04001962 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001963 {
1964 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001965 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1966 {
1967 TField* field = (*fieldList)[memberIndex];
1968 TType* fieldType = field->type();
1969
1970 // set parent pointer of the field variable
1971 fieldType->setInterfaceBlock(interfaceBlock);
1972
1973 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1974 fieldVariable->setQualifier(typeQualifier.qualifier);
1975
1976 if (!symbolTable.declare(*fieldVariable)) {
1977 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001978 recover();
1979 }
1980 }
1981 }
1982 else
1983 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001984 // add a symbol for this interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04001985 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001986 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04001987
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001988 if (!symbolTable.declare(*instanceTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04001989 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001990 recover();
1991 }
1992
1993 symbolId = instanceTypeDef->getUniqueId();
1994 symbolName = instanceTypeDef->getName();
1995 }
1996
Jamie Madill98493dd2013-07-08 14:39:03 -04001997 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00001998 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04001999
2000 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002001 return aggregate;
2002}
2003
Jamie Madill075edd82013-07-08 13:30:19 -04002004bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002005{
2006 ++structNestingLevel;
2007
2008 // Embedded structure definitions are not supported per GLSL ES spec.
2009 // They aren't allowed in GLSL either, but we need to detect this here
2010 // so we don't rely on the GLSL compiler to catch it.
2011 if (structNestingLevel > 1) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002012 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002013 return true;
2014 }
2015
2016 return false;
2017}
2018
2019void TParseContext::exitStructDeclaration()
2020{
2021 --structNestingLevel;
2022}
2023
2024namespace {
2025
2026const int kWebGLMaxStructNesting = 4;
2027
2028} // namespace
2029
Jamie Madill98493dd2013-07-08 14:39:03 -04002030bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002031{
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +00002032 if (!isWebGLBasedSpec(shaderSpec)) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002033 return false;
2034 }
2035
Jamie Madill98493dd2013-07-08 14:39:03 -04002036 if (field.type()->getBasicType() != EbtStruct) {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002037 return false;
2038 }
2039
2040 // We're already inside a structure definition at this point, so add
2041 // one to the field's struct nesting.
Jamie Madill98493dd2013-07-08 14:39:03 -04002042 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002043 std::stringstream extraInfoStream;
Jamie Madill98493dd2013-07-08 14:39:03 -04002044 extraInfoStream << "Reference of struct type " << field.type()->getStruct()->name()
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002045 << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting;
2046 std::string extraInfo = extraInfoStream.str();
2047 error(line, "", "", extraInfo.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 Madill7164cf42013-07-08 13:30:59 -04002074 if (indexExpression->getQualifier() == EvqConst)
2075 {
2076 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2077 if (index < 0)
2078 {
2079 std::stringstream infoStream;
2080 infoStream << index;
2081 std::string info = infoStream.str();
2082 error(location, "negative index", info.c_str());
2083 recover();
2084 index = 0;
2085 }
2086 if (baseExpression->getType().getQualifier() == EvqConst)
2087 {
2088 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002089 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002090 // constant folding for arrays
2091 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002092 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002093 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002094 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002095 // constant folding for vectors
2096 TVectorFields fields;
2097 fields.num = 1;
2098 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2099 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2100 }
2101 else if (baseExpression->isMatrix())
2102 {
2103 // constant folding for matrices
2104 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002105 }
2106 }
2107 else
2108 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002109 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002110 {
Jamie Madill18464b52013-07-08 14:01:55 -04002111 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002112 {
2113 std::stringstream extraInfoStream;
2114 extraInfoStream << "array index out of range '" << index << "'";
2115 std::string extraInfo = extraInfoStream.str();
2116 error(location, "", "[", extraInfo.c_str());
2117 recover();
2118 index = baseExpression->getType().getArraySize() - 1;
2119 }
Jamie Madill5d287f52013-07-12 15:38:19 -04002120 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2121 {
2122 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2123 recover();
2124 index = 0;
2125 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002126 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002127 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002128 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002129 std::stringstream extraInfoStream;
2130 extraInfoStream << "field selection out of range '" << index << "'";
2131 std::string extraInfo = extraInfoStream.str();
2132 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002133 recover();
Jamie Madill7164cf42013-07-08 13:30:59 -04002134 index = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002135 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002136
Jamie Madill7164cf42013-07-08 13:30:59 -04002137 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2138 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002139 }
2140 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002141 else
2142 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002143 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002144 {
Jamie Madill19571812013-08-12 15:26:34 -07002145 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002146 recover();
2147 }
Jamie Madill19571812013-08-12 15:26:34 -07002148 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002149 {
Jamie Madill19571812013-08-12 15:26:34 -07002150 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002151 recover();
2152 }
2153
2154 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2155 }
2156
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002157 if (indexedExpression == 0)
2158 {
2159 ConstantUnion *unionArray = new ConstantUnion[1];
2160 unionArray->setFConst(0.0f);
2161 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2162 }
2163 else if (baseExpression->isArray())
2164 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002165 const TType &baseType = baseExpression->getType();
2166 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002167 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002168 TType copyOfType(baseType.getStruct());
2169 indexedExpression->setType(copyOfType);
2170 }
2171 else if (baseType.isInterfaceBlock())
2172 {
2173 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002174 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002175 }
2176 else
2177 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002178 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002179 }
2180
2181 if (baseExpression->getType().getQualifier() == EvqConst)
2182 {
2183 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2184 }
2185 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002186 else if (baseExpression->isMatrix())
2187 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002188 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2189 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002190 }
2191 else if (baseExpression->isVector())
2192 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002193 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2194 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002195 }
2196 else
2197 {
2198 indexedExpression->setType(baseExpression->getType());
2199 }
2200
2201 return indexedExpression;
2202}
2203
Jamie Madill075edd82013-07-08 13:30:19 -04002204TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002205{
2206 TIntermTyped *indexedExpression = NULL;
2207
2208 if (baseExpression->isArray())
2209 {
2210 error(fieldLocation, "cannot apply dot operator to an array", ".");
2211 recover();
2212 }
2213
2214 if (baseExpression->isVector())
2215 {
2216 TVectorFields fields;
2217 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2218 {
2219 fields.num = 1;
2220 fields.offsets[0] = 0;
2221 recover();
2222 }
2223
2224 if (baseExpression->getType().getQualifier() == EvqConst)
2225 {
2226 // constant folding for vector fields
2227 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2228 if (indexedExpression == 0)
2229 {
2230 recover();
2231 indexedExpression = baseExpression;
2232 }
2233 else
2234 {
2235 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2236 }
2237 }
2238 else
2239 {
2240 TString vectorString = fieldString;
2241 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2242 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2243 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2244 }
2245 }
2246 else if (baseExpression->isMatrix())
2247 {
2248 TMatrixFields fields;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002249 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002250 {
2251 fields.wholeRow = false;
2252 fields.wholeCol = false;
2253 fields.row = 0;
2254 fields.col = 0;
2255 recover();
2256 }
2257
2258 if (fields.wholeRow || fields.wholeCol)
2259 {
2260 error(dotLocation, " non-scalar fields not implemented yet", ".");
2261 recover();
2262 ConstantUnion *unionArray = new ConstantUnion[1];
2263 unionArray->setIConst(0);
2264 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2265 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002266 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002267 }
2268 else
2269 {
2270 ConstantUnion *unionArray = new ConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002271 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002272 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2273 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2274 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2275 }
2276 }
2277 else if (baseExpression->getBasicType() == EbtStruct)
2278 {
2279 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002280 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2281 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002282 {
2283 error(dotLocation, "structure has no fields", "Internal Error");
2284 recover();
2285 indexedExpression = baseExpression;
2286 }
2287 else
2288 {
2289 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002290 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002291 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002293 {
2294 fieldFound = true;
2295 break;
2296 }
2297 }
2298 if (fieldFound)
2299 {
2300 if (baseExpression->getType().getQualifier() == EvqConst)
2301 {
2302 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2303 if (indexedExpression == 0)
2304 {
2305 recover();
2306 indexedExpression = baseExpression;
2307 }
2308 else
2309 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002310 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002311 // change the qualifier of the return type, not of the structure field
2312 // as the structure definition is shared between various structures.
2313 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2314 }
2315 }
2316 else
2317 {
2318 ConstantUnion *unionArray = new ConstantUnion[1];
2319 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002320 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002321 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002322 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002323 }
2324 }
2325 else
2326 {
2327 error(dotLocation, " no such field in structure", fieldString.c_str());
2328 recover();
2329 indexedExpression = baseExpression;
2330 }
2331 }
2332 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002333 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002334 {
2335 bool fieldFound = false;
Jamie Madill98493dd2013-07-08 14:39:03 -04002336 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2337 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002338 {
2339 error(dotLocation, "interface block has no fields", "Internal Error");
2340 recover();
2341 indexedExpression = baseExpression;
2342 }
2343 else
2344 {
2345 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002346 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002347 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002348 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002349 {
2350 fieldFound = true;
2351 break;
2352 }
2353 }
2354 if (fieldFound)
2355 {
2356 ConstantUnion *unionArray = new ConstantUnion[1];
2357 unionArray->setIConst(i);
Jamie Madill98493dd2013-07-08 14:39:03 -04002358 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002359 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002360 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002361 }
2362 else
2363 {
2364 error(dotLocation, " no such field in interface block", fieldString.c_str());
2365 recover();
2366 indexedExpression = baseExpression;
2367 }
2368 }
2369 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002370 else
2371 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002372 if (shaderVersion < 300)
2373 {
2374 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2375 }
2376 else
2377 {
2378 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2379 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 recover();
2381 indexedExpression = baseExpression;
2382 }
2383
2384 return indexedExpression;
2385}
2386
Jamie Madill075edd82013-07-08 13:30:19 -04002387TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002388{
Jamie Madilla5efff92013-06-06 11:56:47 -04002389 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002390
Jamie Madilla5efff92013-06-06 11:56:47 -04002391 qualifier.location = -1;
2392 qualifier.matrixPacking = EmpUnspecified;
2393 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002394
2395 if (qualifierType == "shared")
2396 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002397 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002398 }
2399 else if (qualifierType == "packed")
2400 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002401 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002402 }
2403 else if (qualifierType == "std140")
2404 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002405 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002406 }
2407 else if (qualifierType == "row_major")
2408 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002409 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002410 }
2411 else if (qualifierType == "column_major")
2412 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002413 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002414 }
2415 else if (qualifierType == "location")
2416 {
2417 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2418 recover();
2419 }
2420 else
2421 {
2422 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2423 recover();
2424 }
2425
Jamie Madilla5efff92013-06-06 11:56:47 -04002426 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002427}
2428
Jamie Madill075edd82013-07-08 13:30:19 -04002429TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002430{
Jamie Madilla5efff92013-06-06 11:56:47 -04002431 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002432
Jamie Madilla5efff92013-06-06 11:56:47 -04002433 qualifier.location = -1;
2434 qualifier.matrixPacking = EmpUnspecified;
2435 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002436
2437 if (qualifierType != "location")
2438 {
2439 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2440 recover();
2441 }
2442 else
2443 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002444 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002445 if (intValue < 0)
2446 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002447 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002448 recover();
2449 }
2450 else
2451 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002452 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002453 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002454 }
2455
Jamie Madilla5efff92013-06-06 11:56:47 -04002456 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002457}
2458
Jamie Madilla5efff92013-06-06 11:56:47 -04002459TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002460{
Jamie Madilla5efff92013-06-06 11:56:47 -04002461 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002462
Jamie Madilla5efff92013-06-06 11:56:47 -04002463 if (rightQualifier.location != -1)
2464 {
2465 joinedQualifier.location = rightQualifier.location;
2466 }
2467 if (rightQualifier.matrixPacking != EmpUnspecified)
2468 {
2469 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2470 }
2471 if (rightQualifier.blockStorage != EbsUnspecified)
2472 {
2473 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2474 }
2475
2476 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002477}
2478
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002479TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2480 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2481{
2482 TQualifier mergedQualifier = EvqSmoothIn;
2483
Jamie Madill19571812013-08-12 15:26:34 -07002484 if (storageQualifier == EvqFragmentIn) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002485 if (interpolationQualifier == EvqSmooth)
2486 mergedQualifier = EvqSmoothIn;
2487 else if (interpolationQualifier == EvqFlat)
2488 mergedQualifier = EvqFlatIn;
2489 else UNREACHABLE();
2490 }
2491 else if (storageQualifier == EvqCentroidIn) {
2492 if (interpolationQualifier == EvqSmooth)
2493 mergedQualifier = EvqCentroidIn;
2494 else if (interpolationQualifier == EvqFlat)
2495 mergedQualifier = EvqFlatIn;
2496 else UNREACHABLE();
2497 }
Jamie Madill19571812013-08-12 15:26:34 -07002498 else if (storageQualifier == EvqVertexOut) {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002499 if (interpolationQualifier == EvqSmooth)
2500 mergedQualifier = EvqSmoothOut;
2501 else if (interpolationQualifier == EvqFlat)
2502 mergedQualifier = EvqFlatOut;
2503 else UNREACHABLE();
2504 }
2505 else if (storageQualifier == EvqCentroidOut) {
2506 if (interpolationQualifier == EvqSmooth)
2507 mergedQualifier = EvqCentroidOut;
2508 else if (interpolationQualifier == EvqFlat)
2509 mergedQualifier = EvqFlatOut;
2510 else UNREACHABLE();
2511 }
2512 else {
2513 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2514 recover();
2515
2516 mergedQualifier = storageQualifier;
2517 }
2518
2519 TPublicType type;
2520 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2521 return type;
2522}
2523
Jamie Madill98493dd2013-07-08 14:39:03 -04002524TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002525{
Jamie Madill98493dd2013-07-08 14:39:03 -04002526 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002527 recover();
2528 }
2529
Jamie Madill98493dd2013-07-08 14:39:03 -04002530 for (unsigned int i = 0; i < fieldList->size(); ++i) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002531 //
2532 // Careful not to replace already known aspects of type, like array-ness
2533 //
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 TType* type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002535 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002536 type->setPrimarySize(typeSpecifier.primarySize);
2537 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002538 type->setPrecision(typeSpecifier.precision);
2539 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002540 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002541
2542 // don't allow arrays of arrays
2543 if (type->isArray()) {
2544 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2545 recover();
2546 }
2547 if (typeSpecifier.array)
2548 type->setArraySize(typeSpecifier.arraySize);
2549 if (typeSpecifier.userDef) {
2550 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002551 }
2552
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002554 recover();
2555 }
2556 }
2557
Jamie Madill98493dd2013-07-08 14:39:03 -04002558 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002559}
2560
Jamie Madill98493dd2013-07-08 14:39:03 -04002561TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002562{
Jamie Madill98493dd2013-07-08 14:39:03 -04002563 TStructure* structure = new TStructure(structName, fieldList);
2564 TType* structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002565
Jamie Madill98493dd2013-07-08 14:39:03 -04002566 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002568 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002569 {
2570 recover();
2571 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002572 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002573 if (!symbolTable.declare(*userTypeDef)) {
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002575 recover();
2576 }
2577 }
2578
2579 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002580 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002581 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002582 const TField &field = *(*fieldList)[typeListIndex];
2583 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002584 switch (qualifier)
2585 {
2586 case EvqGlobal:
2587 case EvqTemporary:
2588 break;
2589 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002591 recover();
2592 break;
2593 }
2594 }
2595
2596 TPublicType publicType;
2597 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002598 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002599 exitStructDeclaration();
2600
2601 return publicType;
2602}
2603
2604//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002605// Parse an array of strings using yyparse.
2606//
2607// Returns 0 for success.
2608//
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +00002609int PaParseStrings(size_t count, const char* const string[], const int length[],
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002610 TParseContext* context) {
2611 if ((count == 0) || (string == NULL))
2612 return 1;
2613
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002614 if (glslang_initialize(context))
2615 return 1;
2616
alokp@chromium.org408c45e2012-04-05 15:54:43 +00002617 int error = glslang_scan(count, string, length, context);
2618 if (!error)
2619 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002620
alokp@chromium.org73bc2982012-06-19 18:48:05 +00002621 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00002622
alokp@chromium.org6b495712012-06-29 00:06:58 +00002623 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002624}
2625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00002627