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