daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 7 | #include "compiler/ParseHelper.h" |
| 8 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | #include <stdarg.h> |
apatrick@chromium.org | 8187fa8 | 2010-06-15 22:09:28 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 12 | #include "compiler/glslang.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 13 | #include "compiler/osinclude.h" |
| 14 | #include "compiler/InitializeParseContext.h" |
| 15 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 16 | extern "C" { |
| 17 | extern int InitPreprocessor(); |
| 18 | extern int FinalizePreprocessor(); |
| 19 | extern void PredefineIntMacro(const char *name, int value); |
| 20 | } |
| 21 | |
| 22 | static void ReportInfo(TInfoSinkBase& sink, |
| 23 | TPrefixType type, TSourceLoc loc, |
| 24 | const char* reason, const char* token, |
| 25 | const char* extraInfo) |
| 26 | { |
| 27 | /* VC++ format: file(linenum) : error #: 'token' : extrainfo */ |
| 28 | sink.prefix(type); |
| 29 | sink.location(loc); |
| 30 | sink << "'" << token << "' : " << reason << " " << extraInfo << "\n"; |
| 31 | } |
| 32 | |
| 33 | static void DefineExtensionMacros(const TExtensionBehavior& extBehavior) |
| 34 | { |
| 35 | for (TExtensionBehavior::const_iterator iter = extBehavior.begin(); |
| 36 | iter != extBehavior.end(); ++iter) { |
| 37 | PredefineIntMacro(iter->first.c_str(), 1); |
| 38 | } |
| 39 | } |
| 40 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 41 | /////////////////////////////////////////////////////////////////////// |
| 42 | // |
| 43 | // Sub- vector and matrix fields |
| 44 | // |
| 45 | //////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | // |
| 48 | // Look at a '.' field selector string and change it into offsets |
| 49 | // for a vector. |
| 50 | // |
| 51 | bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line) |
| 52 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 53 | fields.num = (int) compString.size(); |
| 54 | if (fields.num > 4) { |
| 55 | error(line, "illegal vector field selection", compString.c_str(), ""); |
| 56 | return false; |
| 57 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 58 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 59 | enum { |
| 60 | exyzw, |
| 61 | ergba, |
| 62 | estpq, |
| 63 | } fieldSet[4]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 64 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 65 | for (int i = 0; i < fields.num; ++i) { |
| 66 | switch (compString[i]) { |
| 67 | case 'x': |
| 68 | fields.offsets[i] = 0; |
| 69 | fieldSet[i] = exyzw; |
| 70 | break; |
| 71 | case 'r': |
| 72 | fields.offsets[i] = 0; |
| 73 | fieldSet[i] = ergba; |
| 74 | break; |
| 75 | case 's': |
| 76 | fields.offsets[i] = 0; |
| 77 | fieldSet[i] = estpq; |
| 78 | break; |
| 79 | case 'y': |
| 80 | fields.offsets[i] = 1; |
| 81 | fieldSet[i] = exyzw; |
| 82 | break; |
| 83 | case 'g': |
| 84 | fields.offsets[i] = 1; |
| 85 | fieldSet[i] = ergba; |
| 86 | break; |
| 87 | case 't': |
| 88 | fields.offsets[i] = 1; |
| 89 | fieldSet[i] = estpq; |
| 90 | break; |
| 91 | case 'z': |
| 92 | fields.offsets[i] = 2; |
| 93 | fieldSet[i] = exyzw; |
| 94 | break; |
| 95 | case 'b': |
| 96 | fields.offsets[i] = 2; |
| 97 | fieldSet[i] = ergba; |
| 98 | break; |
| 99 | case 'p': |
| 100 | fields.offsets[i] = 2; |
| 101 | fieldSet[i] = estpq; |
| 102 | break; |
| 103 | |
| 104 | case 'w': |
| 105 | fields.offsets[i] = 3; |
| 106 | fieldSet[i] = exyzw; |
| 107 | break; |
| 108 | case 'a': |
| 109 | fields.offsets[i] = 3; |
| 110 | fieldSet[i] = ergba; |
| 111 | break; |
| 112 | case 'q': |
| 113 | fields.offsets[i] = 3; |
| 114 | fieldSet[i] = estpq; |
| 115 | break; |
| 116 | default: |
| 117 | error(line, "illegal vector field selection", compString.c_str(), ""); |
| 118 | return false; |
| 119 | } |
| 120 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 121 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 122 | for (int i = 0; i < fields.num; ++i) { |
| 123 | if (fields.offsets[i] >= vecSize) { |
| 124 | error(line, "vector field selection out of range", compString.c_str(), ""); |
| 125 | return false; |
| 126 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 128 | if (i > 0) { |
| 129 | if (fieldSet[i] != fieldSet[i-1]) { |
| 130 | error(line, "illegal - vector component fields not from the same set", compString.c_str(), ""); |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 136 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // |
| 141 | // Look at a '.' field selector string and change it into offsets |
| 142 | // for a matrix. |
| 143 | // |
| 144 | bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line) |
| 145 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 146 | fields.wholeRow = false; |
| 147 | fields.wholeCol = false; |
| 148 | fields.row = -1; |
| 149 | fields.col = -1; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 151 | if (compString.size() != 2) { |
| 152 | error(line, "illegal length of matrix field selection", compString.c_str(), ""); |
| 153 | return false; |
| 154 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 155 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 156 | if (compString[0] == '_') { |
| 157 | if (compString[1] < '0' || compString[1] > '3') { |
| 158 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 159 | return false; |
| 160 | } |
| 161 | fields.wholeCol = true; |
| 162 | fields.col = compString[1] - '0'; |
| 163 | } else if (compString[1] == '_') { |
| 164 | if (compString[0] < '0' || compString[0] > '3') { |
| 165 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 166 | return false; |
| 167 | } |
| 168 | fields.wholeRow = true; |
| 169 | fields.row = compString[0] - '0'; |
| 170 | } else { |
| 171 | if (compString[0] < '0' || compString[0] > '3' || |
| 172 | compString[1] < '0' || compString[1] > '3') { |
| 173 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 174 | return false; |
| 175 | } |
| 176 | fields.row = compString[0] - '0'; |
| 177 | fields.col = compString[1] - '0'; |
| 178 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 180 | if (fields.row >= matSize || fields.col >= matSize) { |
| 181 | error(line, "matrix field selection out of range", compString.c_str(), ""); |
| 182 | return false; |
| 183 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 184 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 185 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /////////////////////////////////////////////////////////////////////// |
| 189 | // |
| 190 | // Errors |
| 191 | // |
| 192 | //////////////////////////////////////////////////////////////////////// |
| 193 | |
| 194 | // |
| 195 | // Track whether errors have occurred. |
| 196 | // |
| 197 | void TParseContext::recover() |
| 198 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // |
| 202 | // Used by flex/bison to output all syntax and parsing errors. |
| 203 | // |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 204 | void TParseContext::error(TSourceLoc loc, |
| 205 | const char* reason, const char* token, |
| 206 | const char* extraInfoFormat, ...) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 207 | { |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 208 | char extraInfo[512]; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 209 | va_list marker; |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 210 | va_start(marker, extraInfoFormat); |
| 211 | vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker); |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 212 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 213 | ReportInfo(infoSink.info, EPrefixError, loc, reason, token, extraInfo); |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 214 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 215 | va_end(marker); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 216 | ++numErrors; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 219 | void TParseContext::warning(TSourceLoc loc, |
| 220 | const char* reason, const char* token, |
| 221 | const char* extraInfoFormat, ...) { |
| 222 | char extraInfo[512]; |
| 223 | va_list marker; |
| 224 | va_start(marker, extraInfoFormat); |
| 225 | vsnprintf(extraInfo, sizeof(extraInfo), extraInfoFormat, marker); |
| 226 | |
| 227 | ReportInfo(infoSink.info, EPrefixWarning, loc, reason, token, extraInfo); |
| 228 | |
| 229 | va_end(marker); |
| 230 | } |
| 231 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 232 | // |
| 233 | // Same error message for all places assignments don't work. |
| 234 | // |
| 235 | void TParseContext::assignError(int line, const char* op, TString left, TString right) |
| 236 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 237 | error(line, "", op, "cannot convert from '%s' to '%s'", |
| 238 | right.c_str(), left.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // |
| 242 | // Same error message for all places unary operations don't work. |
| 243 | // |
| 244 | void TParseContext::unaryOpError(int line, const char* op, TString operand) |
| 245 | { |
| 246 | error(line, " wrong operand type", op, |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 247 | "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)", |
| 248 | op, operand.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // |
| 252 | // Same error message for all binary operations don't work. |
| 253 | // |
| 254 | void TParseContext::binaryOpError(int line, const char* op, TString left, TString right) |
| 255 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 256 | error(line, " wrong operand types ", op, |
| 257 | "no operation '%s' exists that takes a left-hand operand of type '%s' and " |
| 258 | "a right operand of type '%s' (or there is no acceptable conversion)", |
| 259 | op, left.c_str(), right.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 260 | } |
| 261 | |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 262 | bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){ |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 263 | if (!checksPrecisionErrors) |
| 264 | return false; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 265 | switch( type ){ |
| 266 | case EbtFloat: |
| 267 | if( precision == EbpUndefined ){ |
| 268 | error( line, "No precision specified for (float)", "", "" ); |
| 269 | return true; |
| 270 | } |
| 271 | break; |
| 272 | case EbtInt: |
| 273 | if( precision == EbpUndefined ){ |
| 274 | error( line, "No precision specified (int)", "", "" ); |
| 275 | return true; |
| 276 | } |
| 277 | break; |
daniel@transgaming.com | 0eb64c3 | 2011-03-15 18:23:51 +0000 | [diff] [blame] | 278 | default: |
| 279 | return false; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 284 | // |
| 285 | // Both test and if necessary, spit out an error, to see if the node is really |
| 286 | // an l-value that can be operated on this way. |
| 287 | // |
| 288 | // Returns true if the was an error. |
| 289 | // |
| 290 | bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node) |
| 291 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 292 | TIntermSymbol* symNode = node->getAsSymbolNode(); |
| 293 | TIntermBinary* binaryNode = node->getAsBinaryNode(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 294 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 295 | if (binaryNode) { |
| 296 | bool errorReturn; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 297 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 298 | switch(binaryNode->getOp()) { |
| 299 | case EOpIndexDirect: |
| 300 | case EOpIndexIndirect: |
| 301 | case EOpIndexDirectStruct: |
| 302 | return lValueErrorCheck(line, op, binaryNode->getLeft()); |
| 303 | case EOpVectorSwizzle: |
| 304 | errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft()); |
| 305 | if (!errorReturn) { |
| 306 | int offset[4] = {0,0,0,0}; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 307 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 308 | TIntermTyped* rightNode = binaryNode->getRight(); |
| 309 | TIntermAggregate *aggrNode = rightNode->getAsAggregate(); |
| 310 | |
| 311 | for (TIntermSequence::iterator p = aggrNode->getSequence().begin(); |
| 312 | p != aggrNode->getSequence().end(); p++) { |
| 313 | int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst(); |
| 314 | offset[value]++; |
| 315 | if (offset[value] > 1) { |
| 316 | error(line, " l-value of swizzle cannot have duplicate components", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 317 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 322 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 323 | return errorReturn; |
| 324 | default: |
| 325 | break; |
| 326 | } |
| 327 | error(line, " l-value required", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 328 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 329 | return true; |
| 330 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 331 | |
| 332 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 333 | const char* symbol = 0; |
| 334 | if (symNode != 0) |
| 335 | symbol = symNode->getSymbol().c_str(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 336 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 337 | const char* message = 0; |
| 338 | switch (node->getQualifier()) { |
| 339 | case EvqConst: message = "can't modify a const"; break; |
| 340 | case EvqConstReadOnly: message = "can't modify a const"; break; |
| 341 | case EvqAttribute: message = "can't modify an attribute"; break; |
| 342 | case EvqUniform: message = "can't modify a uniform"; break; |
| 343 | case EvqVaryingIn: message = "can't modify a varying"; break; |
| 344 | case EvqInput: message = "can't modify an input"; break; |
| 345 | case EvqFragCoord: message = "can't modify gl_FragCoord"; break; |
| 346 | case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break; |
| 347 | case EvqPointCoord: message = "can't modify gl_PointCoord"; break; |
| 348 | default: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 349 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 350 | // |
| 351 | // Type that can't be written to? |
| 352 | // |
| 353 | switch (node->getBasicType()) { |
| 354 | case EbtSampler2D: |
| 355 | case EbtSamplerCube: |
| 356 | message = "can't modify a sampler"; |
| 357 | break; |
| 358 | case EbtVoid: |
| 359 | message = "can't modify void"; |
| 360 | break; |
| 361 | default: |
| 362 | break; |
| 363 | } |
| 364 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 365 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 366 | if (message == 0 && binaryNode == 0 && symNode == 0) { |
| 367 | error(line, " l-value required", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 368 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 371 | |
| 372 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 373 | // |
| 374 | // Everything else is okay, no error. |
| 375 | // |
| 376 | if (message == 0) |
| 377 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 378 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 379 | // |
| 380 | // If we get here, we have an error and a message. |
| 381 | // |
| 382 | if (symNode) |
| 383 | error(line, " l-value required", op, "\"%s\" (%s)", symbol, message); |
| 384 | else |
| 385 | error(line, " l-value required", op, "(%s)", message); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 386 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 387 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // |
| 391 | // Both test, and if necessary spit out an error, to see if the node is really |
| 392 | // a constant. |
| 393 | // |
| 394 | // Returns true if the was an error. |
| 395 | // |
| 396 | bool TParseContext::constErrorCheck(TIntermTyped* node) |
| 397 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 398 | if (node->getQualifier() == EvqConst) |
| 399 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 400 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 401 | error(node->getLine(), "constant expression required", "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 402 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 403 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // |
| 407 | // Both test, and if necessary spit out an error, to see if the node is really |
| 408 | // an integer. |
| 409 | // |
| 410 | // Returns true if the was an error. |
| 411 | // |
| 412 | bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token) |
| 413 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 414 | if (node->getBasicType() == EbtInt && node->getNominalSize() == 1) |
| 415 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 416 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 417 | error(node->getLine(), "integer expression required", token, ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 418 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 419 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // |
| 423 | // Both test, and if necessary spit out an error, to see if we are currently |
| 424 | // globally scoped. |
| 425 | // |
| 426 | // Returns true if the was an error. |
| 427 | // |
| 428 | bool TParseContext::globalErrorCheck(int line, bool global, const char* token) |
| 429 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 430 | if (global) |
| 431 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 432 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 433 | error(line, "only allowed at global scope", token, ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 434 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 435 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // |
| 439 | // For now, keep it simple: if it starts "gl_", it's reserved, independent |
| 440 | // of scope. Except, if the symbol table is at the built-in push-level, |
| 441 | // which is when we are parsing built-ins. |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 442 | // Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a |
| 443 | // webgl shader. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 444 | // |
| 445 | // Returns true if there was an error. |
| 446 | // |
| 447 | bool TParseContext::reservedErrorCheck(int line, const TString& identifier) |
| 448 | { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 449 | static const char* reservedErrMsg = "reserved built-in name"; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 450 | if (!symbolTable.atBuiltInLevel()) { |
daniel@transgaming.com | 51db7fb | 2011-09-20 16:11:06 +0000 | [diff] [blame] | 451 | if (identifier.compare(0, 3, "gl_") == 0) { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 452 | error(line, reservedErrMsg, "gl_", ""); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 453 | return true; |
| 454 | } |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 455 | if (shaderSpec == SH_WEBGL_SPEC) { |
daniel@transgaming.com | 51db7fb | 2011-09-20 16:11:06 +0000 | [diff] [blame] | 456 | if (identifier.compare(0, 6, "webgl_") == 0) { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 457 | error(line, reservedErrMsg, "webgl_", ""); |
| 458 | return true; |
| 459 | } |
daniel@transgaming.com | 51db7fb | 2011-09-20 16:11:06 +0000 | [diff] [blame] | 460 | if (identifier.compare(0, 7, "_webgl_") == 0) { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 461 | error(line, reservedErrMsg, "_webgl_", ""); |
| 462 | return true; |
| 463 | } |
| 464 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 465 | if (identifier.find("__") != TString::npos) { |
| 466 | //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", ""); |
| 467 | //return true; |
| 468 | infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line); |
| 469 | return false; |
| 470 | } |
| 471 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 472 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 473 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // |
| 477 | // Make sure there is enough data provided to the constructor to build |
| 478 | // something of the type of the constructor. Also returns the type of |
| 479 | // the constructor. |
| 480 | // |
| 481 | // Returns true if there was an error in construction. |
| 482 | // |
| 483 | bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type) |
| 484 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 485 | *type = function.getReturnType(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 486 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 487 | bool constructingMatrix = false; |
| 488 | switch(op) { |
| 489 | case EOpConstructMat2: |
| 490 | case EOpConstructMat3: |
| 491 | case EOpConstructMat4: |
| 492 | constructingMatrix = true; |
| 493 | break; |
| 494 | default: |
| 495 | break; |
| 496 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 497 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 498 | // |
| 499 | // Note: It's okay to have too many components available, but not okay to have unused |
| 500 | // arguments. 'full' will go to true when enough args have been seen. If we loop |
| 501 | // again, there is an extra argument, so 'overfull' will become true. |
| 502 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 503 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 504 | int size = 0; |
| 505 | bool constType = true; |
| 506 | bool full = false; |
| 507 | bool overFull = false; |
| 508 | bool matrixInMatrix = false; |
| 509 | bool arrayArg = false; |
| 510 | for (int i = 0; i < function.getParamCount(); ++i) { |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 511 | const TParameter& param = function.getParam(i); |
| 512 | size += param.type->getObjectSize(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 513 | |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 514 | if (constructingMatrix && param.type->isMatrix()) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 515 | matrixInMatrix = true; |
| 516 | if (full) |
| 517 | overFull = true; |
| 518 | if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize()) |
| 519 | full = true; |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 520 | if (param.type->getQualifier() != EvqConst) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 521 | constType = false; |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 522 | if (param.type->isArray()) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 523 | arrayArg = true; |
| 524 | } |
| 525 | |
| 526 | if (constType) |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 527 | type->setQualifier(EvqConst); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 528 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 529 | if (type->isArray() && type->getArraySize() != function.getParamCount()) { |
| 530 | error(line, "array constructor needs one argument per array element", "constructor", ""); |
| 531 | return true; |
| 532 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 533 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 534 | if (arrayArg && op != EOpConstructStruct) { |
| 535 | error(line, "constructing from a non-dereferenced array", "constructor", ""); |
| 536 | return true; |
| 537 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 538 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 539 | if (matrixInMatrix && !type->isArray()) { |
daniel@transgaming.com | bef0b6d | 2010-04-29 03:32:39 +0000 | [diff] [blame] | 540 | if (function.getParamCount() != 1) { |
| 541 | error(line, "constructing matrix from matrix can only take one argument", "constructor", ""); |
| 542 | return true; |
| 543 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 544 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 545 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 546 | if (overFull) { |
| 547 | error(line, "too many arguments", "constructor", ""); |
| 548 | return true; |
| 549 | } |
| 550 | |
daniel@transgaming.com | 7b17fac | 2010-12-12 08:52:58 +0000 | [diff] [blame] | 551 | if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 552 | error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", ""); |
| 553 | return true; |
| 554 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 555 | |
daniel@transgaming.com | 67d7252 | 2011-11-29 17:23:51 +0000 | [diff] [blame] | 556 | if (!type->isMatrix() || !matrixInMatrix) { |
daniel@transgaming.com | bef0b6d | 2010-04-29 03:32:39 +0000 | [diff] [blame] | 557 | if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) || |
| 558 | (op == EOpConstructStruct && size < type->getObjectSize())) { |
| 559 | error(line, "not enough data provided for construction", "constructor", ""); |
| 560 | return true; |
| 561 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 562 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 563 | |
daniel@transgaming.com | 0b53fc0 | 2011-03-09 15:12:12 +0000 | [diff] [blame] | 564 | TIntermTyped *typed = node ? node->getAsTyped() : 0; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 565 | if (typed == 0) { |
| 566 | error(line, "constructor argument does not have a type", "constructor", ""); |
| 567 | return true; |
| 568 | } |
| 569 | if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) { |
| 570 | error(line, "cannot convert a sampler", "constructor", ""); |
| 571 | return true; |
| 572 | } |
| 573 | if (typed->getBasicType() == EbtVoid) { |
| 574 | error(line, "cannot convert a void", "constructor", ""); |
| 575 | return true; |
| 576 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 577 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 578 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | // This function checks to see if a void variable has been declared and raise an error message for such a case |
| 582 | // |
| 583 | // returns true in case of an error |
| 584 | // |
| 585 | bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType) |
| 586 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 587 | if (pubType.type == EbtVoid) { |
| 588 | error(line, "illegal use of type 'void'", identifier.c_str(), ""); |
| 589 | return true; |
| 590 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 591 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 592 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // This function checks to see if the node (for the expression) contains a scalar boolean expression or not |
| 596 | // |
| 597 | // returns true in case of an error |
| 598 | // |
| 599 | bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type) |
| 600 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 601 | if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) { |
| 602 | error(line, "boolean expression expected", "", ""); |
| 603 | return true; |
| 604 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 605 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 606 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // This function checks to see if the node (for the expression) contains a scalar boolean expression or not |
| 610 | // |
| 611 | // returns true in case of an error |
| 612 | // |
| 613 | bool TParseContext::boolErrorCheck(int line, const TPublicType& pType) |
| 614 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 615 | if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) { |
| 616 | error(line, "boolean expression expected", "", ""); |
| 617 | return true; |
| 618 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 619 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 620 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason) |
| 624 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 625 | if (pType.type == EbtStruct) { |
| 626 | if (containsSampler(*pType.userDef)) { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 627 | error(line, reason, getBasicString(pType.type), "(structure contains a sampler)"); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 628 | |
| 629 | return true; |
| 630 | } |
| 631 | |
| 632 | return false; |
| 633 | } else if (IsSampler(pType.type)) { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 634 | error(line, reason, getBasicString(pType.type), ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 635 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 636 | return true; |
| 637 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 638 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 639 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType) |
| 643 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 644 | if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) && |
| 645 | pType.type == EbtStruct) { |
| 646 | error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), ""); |
| 647 | |
| 648 | return true; |
| 649 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 650 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 651 | if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform")) |
| 652 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 653 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 654 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type) |
| 658 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 659 | if ((qualifier == EvqOut || qualifier == EvqInOut) && |
| 660 | type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) { |
| 661 | error(line, "samplers cannot be output parameters", type.getBasicString(), ""); |
| 662 | return true; |
| 663 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 664 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 665 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | bool TParseContext::containsSampler(TType& type) |
| 669 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 670 | if (IsSampler(type.getBasicType())) |
| 671 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 672 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 673 | if (type.getBasicType() == EbtStruct) { |
| 674 | TTypeList& structure = *type.getStruct(); |
| 675 | for (unsigned int i = 0; i < structure.size(); ++i) { |
| 676 | if (containsSampler(*structure[i].type)) |
| 677 | return true; |
| 678 | } |
| 679 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 680 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 681 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // |
| 685 | // Do size checking for an array type's size. |
| 686 | // |
| 687 | // Returns true if there was an error. |
| 688 | // |
| 689 | bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size) |
| 690 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 691 | TIntermConstantUnion* constant = expr->getAsConstantUnion(); |
| 692 | if (constant == 0 || constant->getBasicType() != EbtInt) { |
| 693 | error(line, "array size must be a constant integer expression", "", ""); |
| 694 | return true; |
| 695 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 696 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 697 | size = constant->getUnionArrayPointer()->getIConst(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 698 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 699 | if (size <= 0) { |
| 700 | error(line, "array size must be a positive integer", "", ""); |
| 701 | size = 1; |
| 702 | return true; |
| 703 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 704 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 705 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // |
| 709 | // See if this qualifier can be an array. |
| 710 | // |
| 711 | // Returns true if there is an error. |
| 712 | // |
| 713 | bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type) |
| 714 | { |
alokp@chromium.org | 8f0f24a | 2010-09-01 21:06:24 +0000 | [diff] [blame] | 715 | if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 716 | error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), ""); |
| 717 | return true; |
| 718 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 719 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 720 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // |
| 724 | // See if this type can be an array. |
| 725 | // |
| 726 | // Returns true if there is an error. |
| 727 | // |
| 728 | bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type) |
| 729 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 730 | // |
| 731 | // Can the type be an array? |
| 732 | // |
| 733 | if (type.array) { |
| 734 | error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), ""); |
| 735 | return true; |
| 736 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 737 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 738 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | // |
| 742 | // Do all the semantic checking for declaring an array, with and |
| 743 | // without a size, and make the right changes to the symbol table. |
| 744 | // |
| 745 | // size == 0 means no specified size. |
| 746 | // |
| 747 | // Returns true if there was an error. |
| 748 | // |
| 749 | bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable) |
| 750 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 751 | // |
| 752 | // Don't check for reserved word use until after we know it's not in the symbol table, |
| 753 | // because reserved arrays can be redeclared. |
| 754 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 755 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 756 | bool builtIn = false; |
| 757 | bool sameScope = false; |
| 758 | TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope); |
| 759 | if (symbol == 0 || !sameScope) { |
| 760 | if (reservedErrorCheck(line, identifier)) |
| 761 | return true; |
| 762 | |
| 763 | variable = new TVariable(&identifier, TType(type)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 764 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 765 | if (type.arraySize) |
| 766 | variable->getType().setArraySize(type.arraySize); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 767 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 768 | if (! symbolTable.insert(*variable)) { |
| 769 | delete variable; |
| 770 | error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), ""); |
| 771 | return true; |
| 772 | } |
| 773 | } else { |
| 774 | if (! symbol->isVariable()) { |
| 775 | error(line, "variable expected", identifier.c_str(), ""); |
| 776 | return true; |
| 777 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 778 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 779 | variable = static_cast<TVariable*>(symbol); |
| 780 | if (! variable->getType().isArray()) { |
| 781 | error(line, "redeclaring non-array as array", identifier.c_str(), ""); |
| 782 | return true; |
| 783 | } |
| 784 | if (variable->getType().getArraySize() > 0) { |
| 785 | error(line, "redeclaration of array with size", identifier.c_str(), ""); |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | if (! variable->getType().sameElementType(TType(type))) { |
| 790 | error(line, "redeclaration of array with a different type", identifier.c_str(), ""); |
| 791 | return true; |
| 792 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 793 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 794 | TType* t = variable->getArrayInformationType(); |
| 795 | while (t != 0) { |
| 796 | if (t->getMaxArraySize() > type.arraySize) { |
| 797 | error(line, "higher index value already used for the array", identifier.c_str(), ""); |
| 798 | return true; |
| 799 | } |
| 800 | t->setArraySize(type.arraySize); |
| 801 | t = t->getArrayInformationType(); |
| 802 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 803 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 804 | if (type.arraySize) |
| 805 | variable->getType().setArraySize(type.arraySize); |
| 806 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 807 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 808 | if (voidErrorCheck(line, identifier, type)) |
| 809 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 810 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 811 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line) |
| 815 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 816 | bool builtIn = false; |
| 817 | TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn); |
| 818 | if (symbol == 0) { |
| 819 | error(line, " undeclared identifier", node->getSymbol().c_str(), ""); |
| 820 | return true; |
| 821 | } |
| 822 | TVariable* variable = static_cast<TVariable*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 823 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 824 | type->setArrayInformationType(variable->getArrayInformationType()); |
| 825 | variable->updateArrayInformationType(type); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 826 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 827 | // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers |
| 828 | // its an error |
| 829 | if (node->getSymbol() == "gl_FragData") { |
| 830 | TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn); |
| 831 | if (fragData == 0) { |
| 832 | infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line); |
| 833 | return true; |
| 834 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 835 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 836 | int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst(); |
| 837 | if (fragDataValue <= size) { |
| 838 | error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", ""); |
| 839 | return true; |
| 840 | } |
| 841 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 842 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 843 | // we dont want to update the maxArraySize when this flag is not set, we just want to include this |
| 844 | // node type in the chain of node types so that its updated when a higher maxArraySize comes in. |
| 845 | if (!updateFlag) |
| 846 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 847 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 848 | size++; |
| 849 | variable->getType().setMaxArraySize(size); |
| 850 | type->setMaxArraySize(size); |
| 851 | TType* tt = type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 852 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 853 | while(tt->getArrayInformationType() != 0) { |
| 854 | tt = tt->getArrayInformationType(); |
| 855 | tt->setMaxArraySize(size); |
| 856 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 857 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 858 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | // |
| 862 | // Enforce non-initializer type/qualifier rules. |
| 863 | // |
| 864 | // Returns true if there was an error. |
| 865 | // |
| 866 | bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type) |
| 867 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 868 | // |
| 869 | // Make the qualifier make sense. |
| 870 | // |
| 871 | if (type.qualifier == EvqConst) { |
| 872 | type.qualifier = EvqTemporary; |
| 873 | error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), ""); |
| 874 | return true; |
| 875 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 876 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 877 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | // |
| 881 | // Do semantic checking for a variable declaration that has no initializer, |
| 882 | // and update the symbol table. |
| 883 | // |
| 884 | // Returns true if there was an error. |
| 885 | // |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 886 | bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 887 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 888 | if (reservedErrorCheck(line, identifier)) |
| 889 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 890 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 891 | variable = new TVariable(&identifier, TType(type)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 892 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 893 | if (! symbolTable.insert(*variable)) { |
| 894 | error(line, "redefinition", variable->getName().c_str(), ""); |
| 895 | delete variable; |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 896 | variable = 0; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 897 | return true; |
| 898 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 899 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 900 | if (voidErrorCheck(line, identifier, type)) |
| 901 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 902 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 903 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type) |
| 907 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 908 | if (qualifier != EvqConst && qualifier != EvqTemporary) { |
| 909 | error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), ""); |
| 910 | return true; |
| 911 | } |
| 912 | if (qualifier == EvqConst && paramQualifier != EvqIn) { |
| 913 | error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier)); |
| 914 | return true; |
| 915 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 916 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 917 | if (qualifier == EvqConst) |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 918 | type->setQualifier(EvqConstReadOnly); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 919 | else |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 920 | type->setQualifier(paramQualifier); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 921 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 922 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 923 | } |
| 924 | |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 925 | bool TParseContext::extensionErrorCheck(int line, const TString& extension) |
| 926 | { |
| 927 | TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension); |
| 928 | if (iter == extensionBehavior.end()) { |
| 929 | error(line, "extension", extension.c_str(), "is not supported"); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 930 | return true; |
| 931 | } |
zmo@google.com | f545091 | 2011-09-09 01:37:19 +0000 | [diff] [blame] | 932 | // In GLSL ES, an extension's default behavior is "disable". |
| 933 | if (iter->second == EBhDisable || iter->second == EBhUndefined) { |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 934 | error(line, "extension", extension.c_str(), "is disabled"); |
| 935 | return true; |
| 936 | } |
| 937 | if (iter->second == EBhWarn) { |
| 938 | TString msg = "extension " + extension + " is being used"; |
| 939 | infoSink.info.message(EPrefixWarning, msg.c_str(), line); |
| 940 | return false; |
| 941 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 942 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 943 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 944 | } |
| 945 | |
zmo@google.com | 09c323a | 2011-08-12 18:22:25 +0000 | [diff] [blame] | 946 | bool TParseContext::supportsExtension(const char* extension) |
| 947 | { |
| 948 | TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension); |
| 949 | return (iter != extensionBehavior.end()); |
| 950 | } |
| 951 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 952 | ///////////////////////////////////////////////////////////////////////////////// |
| 953 | // |
| 954 | // Non-Errors. |
| 955 | // |
| 956 | ///////////////////////////////////////////////////////////////////////////////// |
| 957 | |
| 958 | // |
| 959 | // Look up a function name in the symbol table, and make sure it is a function. |
| 960 | // |
| 961 | // Return the function symbol if found, otherwise 0. |
| 962 | // |
| 963 | const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn) |
| 964 | { |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 965 | // First find by unmangled name to check whether the function name has been |
| 966 | // hidden by a variable name or struct typename. |
| 967 | const TSymbol* symbol = symbolTable.find(call->getName(), builtIn); |
| 968 | if (symbol == 0) { |
| 969 | symbol = symbolTable.find(call->getMangledName(), builtIn); |
| 970 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 971 | |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 972 | if (symbol == 0) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 973 | error(line, "no matching overloaded function found", call->getName().c_str(), ""); |
| 974 | return 0; |
| 975 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 976 | |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 977 | if (!symbol->isFunction()) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 978 | error(line, "function name expected", call->getName().c_str(), ""); |
| 979 | return 0; |
| 980 | } |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 981 | |
| 982 | return static_cast<const TFunction*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | // |
| 986 | // Initializers show up in several places in the grammar. Have one set of |
| 987 | // code to handle them here. |
| 988 | // |
| 989 | bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType, |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 990 | TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 991 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 992 | TType type = TType(pType); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 993 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 994 | if (variable == 0) { |
| 995 | if (reservedErrorCheck(line, identifier)) |
| 996 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 997 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 998 | if (voidErrorCheck(line, identifier, pType)) |
| 999 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1000 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1001 | // |
| 1002 | // add variable to symbol table |
| 1003 | // |
| 1004 | variable = new TVariable(&identifier, type); |
| 1005 | if (! symbolTable.insert(*variable)) { |
| 1006 | error(line, "redefinition", variable->getName().c_str(), ""); |
| 1007 | return true; |
| 1008 | // don't delete variable, it's used by error recovery, and the pool |
| 1009 | // pop will take care of the memory |
| 1010 | } |
| 1011 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1012 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1013 | // |
| 1014 | // identifier must be of type constant, a global, or a temporary |
| 1015 | // |
| 1016 | TQualifier qualifier = variable->getType().getQualifier(); |
| 1017 | if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) { |
| 1018 | error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), ""); |
| 1019 | return true; |
| 1020 | } |
| 1021 | // |
| 1022 | // test for and propagate constant |
| 1023 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1024 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1025 | if (qualifier == EvqConst) { |
| 1026 | if (qualifier != initializer->getType().getQualifier()) { |
| 1027 | error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str()); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1028 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | if (type != initializer->getType()) { |
| 1032 | error(line, " non-matching types for const initializer ", |
| 1033 | variable->getType().getQualifierString(), ""); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1034 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1035 | return true; |
| 1036 | } |
| 1037 | if (initializer->getAsConstantUnion()) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1038 | ConstantUnion* unionArray = variable->getConstPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1039 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1040 | if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) { |
| 1041 | *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0]; |
| 1042 | } else { |
| 1043 | variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer()); |
| 1044 | } |
| 1045 | } else if (initializer->getAsSymbolNode()) { |
| 1046 | const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol()); |
| 1047 | const TVariable* tVar = static_cast<const TVariable*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1048 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1049 | ConstantUnion* constArray = tVar->getConstPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1050 | variable->shareConstPointer(constArray); |
| 1051 | } else { |
| 1052 | error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str()); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1053 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1057 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1058 | if (qualifier != EvqConst) { |
| 1059 | TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line); |
| 1060 | intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line); |
| 1061 | if (intermNode == 0) { |
| 1062 | assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString()); |
| 1063 | return true; |
| 1064 | } |
| 1065 | } else |
| 1066 | intermNode = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1067 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1068 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode) |
| 1072 | { |
alokp@chromium.org | d300f5b | 2010-10-14 16:10:20 +0000 | [diff] [blame] | 1073 | ASSERT(aggrNode != NULL); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1074 | if (!aggrNode->isConstructor()) |
| 1075 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1076 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1077 | bool allConstant = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1078 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1079 | // check if all the child nodes are constants so that they can be inserted into |
| 1080 | // the parent node |
alokp@chromium.org | d300f5b | 2010-10-14 16:10:20 +0000 | [diff] [blame] | 1081 | TIntermSequence &sequence = aggrNode->getSequence() ; |
| 1082 | for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) { |
| 1083 | if (!(*p)->getAsTyped()->getAsConstantUnion()) |
| 1084 | return false; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1085 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1086 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1087 | return allConstant; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | // This function is used to test for the correctness of the parameters passed to various constructor functions |
| 1091 | // and also convert them to the right datatype if it is allowed and required. |
| 1092 | // |
| 1093 | // Returns 0 for an error or the constructed node (aggregate or typed) for no error. |
| 1094 | // |
| 1095 | TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line) |
| 1096 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1097 | if (node == 0) |
| 1098 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1099 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1100 | TIntermAggregate* aggrNode = node->getAsAggregate(); |
| 1101 | |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1102 | TTypeList::const_iterator memberTypes; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1103 | if (op == EOpConstructStruct) |
| 1104 | memberTypes = type->getStruct()->begin(); |
| 1105 | |
| 1106 | TType elementType = *type; |
| 1107 | if (type->isArray()) |
| 1108 | elementType.clearArrayness(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1109 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1110 | bool singleArg; |
| 1111 | if (aggrNode) { |
| 1112 | if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1) |
| 1113 | singleArg = true; |
| 1114 | else |
| 1115 | singleArg = false; |
| 1116 | } else |
| 1117 | singleArg = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1118 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1119 | TIntermTyped *newNode; |
| 1120 | if (singleArg) { |
| 1121 | // If structure constructor or array constructor is being called |
| 1122 | // for only one parameter inside the structure, we need to call constructStruct function once. |
| 1123 | if (type->isArray()) |
| 1124 | newNode = constructStruct(node, &elementType, 1, node->getLine(), false); |
| 1125 | else if (op == EOpConstructStruct) |
| 1126 | newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false); |
| 1127 | else |
| 1128 | newNode = constructBuiltIn(type, op, node, node->getLine(), false); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1129 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1130 | if (newNode && newNode->getAsAggregate()) { |
| 1131 | TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type); |
| 1132 | if (constConstructor) |
| 1133 | return constConstructor; |
| 1134 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1135 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1136 | return newNode; |
| 1137 | } |
| 1138 | |
| 1139 | // |
| 1140 | // Handle list of arguments. |
| 1141 | // |
| 1142 | TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor |
| 1143 | // if the structure constructor contains more than one parameter, then construct |
| 1144 | // each parameter |
| 1145 | |
| 1146 | int paramCount = 0; // keeps a track of the constructor parameter number being checked |
| 1147 | |
| 1148 | // for each parameter to the constructor call, check to see if the right type is passed or convert them |
| 1149 | // to the right type if possible (and allowed). |
| 1150 | // for structure constructors, just check if the right type is passed, no conversion is allowed. |
| 1151 | |
| 1152 | for (TIntermSequence::iterator p = sequenceVector.begin(); |
| 1153 | p != sequenceVector.end(); p++, paramCount++) { |
| 1154 | if (type->isArray()) |
| 1155 | newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true); |
| 1156 | else if (op == EOpConstructStruct) |
| 1157 | newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true); |
| 1158 | else |
| 1159 | newNode = constructBuiltIn(type, op, *p, node->getLine(), true); |
| 1160 | |
| 1161 | if (newNode) { |
| 1162 | *p = newNode; |
| 1163 | } |
| 1164 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1165 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1166 | TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line); |
| 1167 | TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type); |
| 1168 | if (constConstructor) |
| 1169 | return constConstructor; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1170 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1171 | return constructor; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type) |
| 1175 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1176 | bool canBeFolded = areAllChildConst(aggrNode); |
| 1177 | aggrNode->setType(type); |
| 1178 | if (canBeFolded) { |
| 1179 | bool returnVal = false; |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1180 | ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()]; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1181 | if (aggrNode->getSequence().size() == 1) { |
| 1182 | returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true); |
| 1183 | } |
| 1184 | else { |
| 1185 | returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type); |
| 1186 | } |
| 1187 | if (returnVal) |
| 1188 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1189 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1190 | return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine()); |
| 1191 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1192 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1193 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | // Function for constructor implementation. Calls addUnaryMath with appropriate EOp value |
| 1197 | // for the parameter to the constructor (passed to this function). Essentially, it converts |
| 1198 | // the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a |
| 1199 | // float, then float is converted to int. |
| 1200 | // |
| 1201 | // Returns 0 for an error or the constructed node. |
| 1202 | // |
| 1203 | TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset) |
| 1204 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1205 | TIntermTyped* newNode; |
| 1206 | TOperator basicOp; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1207 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1208 | // |
| 1209 | // First, convert types as needed. |
| 1210 | // |
| 1211 | switch (op) { |
| 1212 | case EOpConstructVec2: |
| 1213 | case EOpConstructVec3: |
| 1214 | case EOpConstructVec4: |
| 1215 | case EOpConstructMat2: |
| 1216 | case EOpConstructMat3: |
| 1217 | case EOpConstructMat4: |
| 1218 | case EOpConstructFloat: |
| 1219 | basicOp = EOpConstructFloat; |
| 1220 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1221 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1222 | case EOpConstructIVec2: |
| 1223 | case EOpConstructIVec3: |
| 1224 | case EOpConstructIVec4: |
| 1225 | case EOpConstructInt: |
| 1226 | basicOp = EOpConstructInt; |
| 1227 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1228 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1229 | case EOpConstructBVec2: |
| 1230 | case EOpConstructBVec3: |
| 1231 | case EOpConstructBVec4: |
| 1232 | case EOpConstructBool: |
| 1233 | basicOp = EOpConstructBool; |
| 1234 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1235 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1236 | default: |
| 1237 | error(line, "unsupported construction", "", ""); |
| 1238 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1239 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1240 | return 0; |
| 1241 | } |
| 1242 | newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable); |
| 1243 | if (newNode == 0) { |
| 1244 | error(line, "can't convert", "constructor", ""); |
| 1245 | return 0; |
| 1246 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1247 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1248 | // |
| 1249 | // Now, if there still isn't an operation to do the construction, and we need one, add one. |
| 1250 | // |
| 1251 | |
| 1252 | // Otherwise, skip out early. |
| 1253 | if (subset || (newNode != node && newNode->getType() == *type)) |
| 1254 | return newNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1255 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1256 | // setAggregateOperator will insert a new node for the constructor, as needed. |
| 1257 | return intermediate.setAggregateOperator(newNode, op, line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | // This function tests for the type of the parameters to the structures constructors. Raises |
| 1261 | // an error message if the expected type does not match the parameter passed to the constructor. |
| 1262 | // |
| 1263 | // Returns 0 for an error or the input node itself if the expected and the given parameter types match. |
| 1264 | // |
| 1265 | TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset) |
| 1266 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1267 | if (*type == node->getAsTyped()->getType()) { |
| 1268 | if (subset) |
| 1269 | return node->getAsTyped(); |
| 1270 | else |
| 1271 | return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line); |
| 1272 | } else { |
| 1273 | error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount, |
| 1274 | node->getAsTyped()->getType().getBasicString(), type->getBasicString()); |
| 1275 | recover(); |
| 1276 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1277 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1278 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | // |
| 1282 | // This function returns the tree representation for the vector field(s) being accessed from contant vector. |
| 1283 | // If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is |
| 1284 | // returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol |
| 1285 | // node or it could be the intermediate tree representation of accessing fields in a constant structure or column of |
| 1286 | // a constant matrix. |
| 1287 | // |
| 1288 | TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line) |
| 1289 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1290 | TIntermTyped* typedNode; |
| 1291 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1292 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1293 | ConstantUnion *unionArray; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1294 | if (tempConstantNode) { |
| 1295 | unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1296 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1297 | if (!unionArray) { // this error message should never be raised |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1298 | infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1299 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1300 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1301 | return node; |
| 1302 | } |
| 1303 | } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error |
| 1304 | error(line, "Cannot offset into the vector", "Error", ""); |
| 1305 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1306 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1307 | return 0; |
| 1308 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1309 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1310 | ConstantUnion* constArray = new ConstantUnion[fields.num]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1311 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1312 | for (int i = 0; i < fields.num; i++) { |
| 1313 | if (fields.offsets[i] >= node->getType().getObjectSize()) { |
| 1314 | error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]); |
| 1315 | recover(); |
| 1316 | fields.offsets[i] = 0; |
| 1317 | } |
| 1318 | |
| 1319 | constArray[i] = unionArray[fields.offsets[i]]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1320 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1321 | } |
| 1322 | typedNode = intermediate.addConstantUnion(constArray, node->getType(), line); |
| 1323 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | // |
| 1327 | // This function returns the column being accessed from a constant matrix. The values are retrieved from |
| 1328 | // the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input |
| 1329 | // to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a |
| 1330 | // constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure) |
| 1331 | // |
| 1332 | TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line) |
| 1333 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1334 | TIntermTyped* typedNode; |
| 1335 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1336 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1337 | if (index >= node->getType().getNominalSize()) { |
| 1338 | error(line, "", "[", "matrix field selection out of range '%d'", index); |
| 1339 | recover(); |
| 1340 | index = 0; |
| 1341 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1342 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1343 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1344 | ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1345 | int size = tempConstantNode->getType().getNominalSize(); |
| 1346 | typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line); |
| 1347 | } else { |
| 1348 | error(line, "Cannot offset into the matrix", "Error", ""); |
| 1349 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1350 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1351 | return 0; |
| 1352 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1353 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1354 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | // |
| 1359 | // This function returns an element of an array accessed from a constant array. The values are retrieved from |
| 1360 | // the symbol table and parse-tree is built for the type of the element. The input |
| 1361 | // to the function could either be a symbol node (a[0] where a is a constant array)that represents a |
| 1362 | // constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure) |
| 1363 | // |
| 1364 | TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line) |
| 1365 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1366 | TIntermTyped* typedNode; |
| 1367 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
| 1368 | TType arrayElementType = node->getType(); |
| 1369 | arrayElementType.clearArrayness(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1370 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1371 | if (index >= node->getType().getArraySize()) { |
| 1372 | error(line, "", "[", "array field selection out of range '%d'", index); |
| 1373 | recover(); |
| 1374 | index = 0; |
| 1375 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1376 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1377 | int arrayElementSize = arrayElementType.getObjectSize(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1378 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1379 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1380 | ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1381 | typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line); |
| 1382 | } else { |
| 1383 | error(line, "Cannot offset into the array", "Error", ""); |
| 1384 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1385 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1388 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1389 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | |
| 1393 | // |
| 1394 | // This function returns the value of a particular field inside a constant structure from the symbol table. |
| 1395 | // If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr |
| 1396 | // function and returns the parse-tree with the values of the embedded/nested struct. |
| 1397 | // |
| 1398 | TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line) |
| 1399 | { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1400 | const TTypeList* fields = node->getType().getStruct(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1401 | TIntermTyped *typedNode; |
| 1402 | int instanceSize = 0; |
| 1403 | unsigned int index = 0; |
| 1404 | TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1405 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1406 | for ( index = 0; index < fields->size(); ++index) { |
| 1407 | if ((*fields)[index].type->getFieldName() == identifier) { |
| 1408 | break; |
| 1409 | } else { |
| 1410 | instanceSize += (*fields)[index].type->getObjectSize(); |
| 1411 | } |
| 1412 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1413 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1414 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1415 | ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1416 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1417 | typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function |
| 1418 | } else { |
| 1419 | error(line, "Cannot offset into the structure", "Error", ""); |
| 1420 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1421 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1422 | return 0; |
| 1423 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1424 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1425 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 1428 | bool TParseContext::enterStructDeclaration(int line, const TString& identifier) |
| 1429 | { |
| 1430 | ++structNestingLevel; |
| 1431 | |
| 1432 | // Embedded structure definitions are not supported per GLSL ES spec. |
| 1433 | // They aren't allowed in GLSL either, but we need to detect this here |
| 1434 | // so we don't rely on the GLSL compiler to catch it. |
| 1435 | if (structNestingLevel > 1) { |
| 1436 | error(line, "", "Embedded struct definitions are not allowed", ""); |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
| 1443 | void TParseContext::exitStructDeclaration() |
| 1444 | { |
| 1445 | --structNestingLevel; |
| 1446 | } |
| 1447 | |
| 1448 | namespace { |
| 1449 | |
| 1450 | const int kWebGLMaxStructNesting = 4; |
| 1451 | |
| 1452 | } // namespace |
| 1453 | |
| 1454 | bool TParseContext::structNestingErrorCheck(TSourceLoc line, const TType& fieldType) |
| 1455 | { |
| 1456 | if (shaderSpec != SH_WEBGL_SPEC) { |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
| 1460 | if (fieldType.getBasicType() != EbtStruct) { |
| 1461 | return false; |
| 1462 | } |
| 1463 | |
| 1464 | // We're already inside a structure definition at this point, so add |
| 1465 | // one to the field's struct nesting. |
kbr@chromium.org | 9a4d112 | 2012-01-05 20:06:28 +0000 | [diff] [blame^] | 1466 | if (1 + fieldType.getDeepestStructNesting() > kWebGLMaxStructNesting) { |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 1467 | error(line, "", "", "Reference of struct type %s exceeds maximum struct nesting of %d", |
| 1468 | fieldType.getTypeName().c_str(), kWebGLMaxStructNesting); |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
alokp@chromium.org | 044a5cf | 2010-11-12 15:42:16 +0000 | [diff] [blame] | 1475 | // |
| 1476 | // Parse an array of strings using yyparse. |
| 1477 | // |
| 1478 | // Returns 0 for success. |
| 1479 | // |
| 1480 | int PaParseStrings(int count, const char* const string[], const int length[], |
| 1481 | TParseContext* context) { |
| 1482 | if ((count == 0) || (string == NULL)) |
| 1483 | return 1; |
| 1484 | |
| 1485 | // setup preprocessor. |
| 1486 | if (InitPreprocessor()) |
| 1487 | return 1; |
| 1488 | DefineExtensionMacros(context->extensionBehavior); |
| 1489 | |
| 1490 | if (glslang_initialize(context)) |
| 1491 | return 1; |
| 1492 | |
| 1493 | glslang_scan(count, string, length, context); |
| 1494 | int error = glslang_parse(context); |
| 1495 | |
| 1496 | glslang_finalize(context); |
| 1497 | FinalizePreprocessor(); |
| 1498 | return (error == 0) && (context->numErrors == 0) ? 0 : 1; |
| 1499 | } |
| 1500 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1501 | OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 1502 | |
| 1503 | bool InitializeParseContextIndex() |
| 1504 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1505 | if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) { |
| 1506 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1507 | return false; |
| 1508 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1509 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1510 | // |
| 1511 | // Allocate a TLS index. |
| 1512 | // |
| 1513 | GlobalParseContextIndex = OS_AllocTLSIndex(); |
| 1514 | |
| 1515 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1516 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1517 | return false; |
| 1518 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1519 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1520 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1523 | bool FreeParseContextIndex() |
| 1524 | { |
| 1525 | OS_TLSIndex tlsiIndex = GlobalParseContextIndex; |
| 1526 | |
| 1527 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1528 | assert(0 && "FreeParseContextIndex(): Parse Context index not initalised"); |
| 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 1533 | |
| 1534 | return OS_FreeTLSIndex(tlsiIndex); |
| 1535 | } |
| 1536 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1537 | bool InitializeGlobalParseContext() |
| 1538 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1539 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1540 | assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised"); |
| 1541 | return false; |
| 1542 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1543 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1544 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 1545 | if (lpParseContext != 0) { |
| 1546 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1547 | return false; |
| 1548 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1549 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1550 | TThreadParseContext *lpThreadData = new TThreadParseContext(); |
| 1551 | if (lpThreadData == 0) { |
| 1552 | assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context"); |
| 1553 | return false; |
| 1554 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1555 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1556 | lpThreadData->lpGlobalParseContext = 0; |
| 1557 | OS_SetTLSValue(GlobalParseContextIndex, lpThreadData); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1558 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1559 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1562 | bool FreeParseContext() |
| 1563 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1564 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1565 | assert(0 && "FreeParseContext(): Parse Context index not initalised"); |
| 1566 | return false; |
| 1567 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1568 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1569 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 1570 | if (lpParseContext) |
| 1571 | delete lpParseContext; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1572 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1573 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1576 | TParseContextPointer& GetGlobalParseContext() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1577 | { |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1578 | // |
| 1579 | // Minimal error checking for speed |
| 1580 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1581 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1582 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1583 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1584 | return lpParseContext->lpGlobalParseContext; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1585 | } |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1586 | |