| 1 | // |
| 2 | // Copyright (c) 2002-2013 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 | // Symbol table for parsing. The design principles and most of the functionality are documented in |
| 7 | // the header file. |
| 8 | // |
| 9 | |
| 10 | #if defined(_MSC_VER) |
| 11 | # pragma warning(disable : 4718) |
| 12 | #endif |
| 13 | |
| 14 | #include "compiler/translator/SymbolTable.h" |
| 15 | |
| 16 | #include "angle_gl.h" |
| 17 | #include "compiler/translator/ImmutableString.h" |
| 18 | #include "compiler/translator/IntermNode.h" |
| 19 | #include "compiler/translator/StaticType.h" |
| 20 | |
| 21 | namespace sh |
| 22 | { |
| 23 | |
| 24 | class TSymbolTable::TSymbolTableLevel |
| 25 | { |
| 26 | public: |
| 27 | TSymbolTableLevel() = default; |
| 28 | |
| 29 | bool insert(TSymbol *symbol); |
| 30 | |
| 31 | // Insert a function using its unmangled name as the key. |
| 32 | void insertUnmangled(TFunction *function); |
| 33 | |
| 34 | TSymbol *find(const ImmutableString &name) const; |
| 35 | |
| 36 | private: |
| 37 | using tLevel = TUnorderedMap<ImmutableString, |
| 38 | TSymbol *, |
| 39 | ImmutableString::FowlerNollVoHash<sizeof(size_t)>>; |
| 40 | using tLevelPair = const tLevel::value_type; |
| 41 | using tInsertResult = std::pair<tLevel::iterator, bool>; |
| 42 | |
| 43 | tLevel level; |
| 44 | }; |
| 45 | |
| 46 | bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol) |
| 47 | { |
| 48 | // returning true means symbol was added to the table |
| 49 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
| 50 | return result.second; |
| 51 | } |
| 52 | |
| 53 | void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function) |
| 54 | { |
| 55 | level.insert(tLevelPair(function->name(), function)); |
| 56 | } |
| 57 | |
| 58 | TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const |
| 59 | { |
| 60 | tLevel::const_iterator it = level.find(name); |
| 61 | if (it == level.end()) |
| 62 | return nullptr; |
| 63 | else |
| 64 | return (*it).second; |
| 65 | } |
| 66 | |
| 67 | TSymbolTable::TSymbolTable() |
| 68 | : mGlobalInvariant(false), |
| 69 | mUniqueIdCounter(0), |
| 70 | mShaderType(GL_FRAGMENT_SHADER), |
| 71 | mGlInVariableWithArraySize(nullptr) |
| 72 | {} |
| 73 | |
| 74 | TSymbolTable::~TSymbolTable() = default; |
| 75 | |
| 76 | bool TSymbolTable::isEmpty() const |
| 77 | { |
| 78 | return mTable.empty(); |
| 79 | } |
| 80 | |
| 81 | bool TSymbolTable::atGlobalLevel() const |
| 82 | { |
| 83 | return mTable.size() == 1u; |
| 84 | } |
| 85 | |
| 86 | void TSymbolTable::push() |
| 87 | { |
| 88 | mTable.emplace_back(new TSymbolTableLevel); |
| 89 | mPrecisionStack.emplace_back(new PrecisionStackLevel); |
| 90 | } |
| 91 | |
| 92 | void TSymbolTable::pop() |
| 93 | { |
| 94 | mTable.pop_back(); |
| 95 | mPrecisionStack.pop_back(); |
| 96 | } |
| 97 | |
| 98 | const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration( |
| 99 | const ImmutableString &mangledName, |
| 100 | bool *hadPrototypeDeclarationOut) const |
| 101 | { |
| 102 | TFunction *function = findUserDefinedFunction(mangledName); |
| 103 | *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration(); |
| 104 | function->setHasPrototypeDeclaration(); |
| 105 | return function; |
| 106 | } |
| 107 | |
| 108 | const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function, |
| 109 | bool *wasDefinedOut) const |
| 110 | { |
| 111 | TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName()); |
| 112 | ASSERT(firstDeclaration); |
| 113 | // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as |
| 114 | // it would have just been put in the symbol table. Otherwise, we're looking up an earlier |
| 115 | // occurance. |
| 116 | if (function != firstDeclaration) |
| 117 | { |
| 118 | // The previous declaration should have the same parameters as the function definition |
| 119 | // (parameter names may differ). |
| 120 | firstDeclaration->shareParameters(*function); |
| 121 | } |
| 122 | |
| 123 | *wasDefinedOut = firstDeclaration->isDefined(); |
| 124 | firstDeclaration->setDefined(); |
| 125 | return firstDeclaration; |
| 126 | } |
| 127 | |
| 128 | bool TSymbolTable::setGlInArraySize(unsigned int inputArraySize) |
| 129 | { |
| 130 | if (mGlInVariableWithArraySize) |
| 131 | { |
| 132 | return mGlInVariableWithArraySize->getType().getOutermostArraySize() == inputArraySize; |
| 133 | } |
| 134 | const TInterfaceBlock *glPerVertex = mVar_gl_PerVertex; |
| 135 | TType *glInType = new TType(glPerVertex, EvqPerVertexIn, TLayoutQualifier::Create()); |
| 136 | glInType->makeArray(inputArraySize); |
| 137 | mGlInVariableWithArraySize = |
| 138 | new TVariable(this, ImmutableString("gl_in" ), glInType, SymbolType::BuiltIn, |
| 139 | TExtension::EXT_geometry_shader); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | TVariable *TSymbolTable::getGlInVariableWithArraySize() const |
| 144 | { |
| 145 | return mGlInVariableWithArraySize; |
| 146 | } |
| 147 | |
| 148 | const TVariable *TSymbolTable::gl_FragData() const |
| 149 | { |
| 150 | return mVar_gl_FragData; |
| 151 | } |
| 152 | |
| 153 | const TVariable *TSymbolTable::gl_SecondaryFragDataEXT() const |
| 154 | { |
| 155 | return mVar_gl_SecondaryFragDataEXT; |
| 156 | } |
| 157 | |
| 158 | TSymbolTable::VariableMetadata *TSymbolTable::getOrCreateVariableMetadata(const TVariable &variable) |
| 159 | { |
| 160 | int id = variable.uniqueId().get(); |
| 161 | auto iter = mVariableMetadata.find(id); |
| 162 | if (iter == mVariableMetadata.end()) |
| 163 | { |
| 164 | iter = mVariableMetadata.insert(std::make_pair(id, VariableMetadata())).first; |
| 165 | } |
| 166 | return &iter->second; |
| 167 | } |
| 168 | |
| 169 | void TSymbolTable::markStaticWrite(const TVariable &variable) |
| 170 | { |
| 171 | auto metadata = getOrCreateVariableMetadata(variable); |
| 172 | metadata->staticWrite = true; |
| 173 | } |
| 174 | |
| 175 | void TSymbolTable::markStaticRead(const TVariable &variable) |
| 176 | { |
| 177 | auto metadata = getOrCreateVariableMetadata(variable); |
| 178 | metadata->staticRead = true; |
| 179 | } |
| 180 | |
| 181 | bool TSymbolTable::isStaticallyUsed(const TVariable &variable) const |
| 182 | { |
| 183 | ASSERT(!variable.getConstPointer()); |
| 184 | int id = variable.uniqueId().get(); |
| 185 | auto iter = mVariableMetadata.find(id); |
| 186 | return iter != mVariableMetadata.end() && (iter->second.staticRead || iter->second.staticWrite); |
| 187 | } |
| 188 | |
| 189 | void TSymbolTable::addInvariantVarying(const TVariable &variable) |
| 190 | { |
| 191 | ASSERT(atGlobalLevel()); |
| 192 | auto metadata = getOrCreateVariableMetadata(variable); |
| 193 | metadata->invariant = true; |
| 194 | } |
| 195 | |
| 196 | bool TSymbolTable::isVaryingInvariant(const TVariable &variable) const |
| 197 | { |
| 198 | ASSERT(atGlobalLevel()); |
| 199 | if (mGlobalInvariant) |
| 200 | { |
| 201 | return true; |
| 202 | } |
| 203 | int id = variable.uniqueId().get(); |
| 204 | auto iter = mVariableMetadata.find(id); |
| 205 | return iter != mVariableMetadata.end() && iter->second.invariant; |
| 206 | } |
| 207 | |
| 208 | void TSymbolTable::setGlobalInvariant(bool invariant) |
| 209 | { |
| 210 | ASSERT(atGlobalLevel()); |
| 211 | mGlobalInvariant = invariant; |
| 212 | } |
| 213 | |
| 214 | const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const |
| 215 | { |
| 216 | const TSymbol *userSymbol = findUserDefined(name); |
| 217 | if (userSymbol) |
| 218 | { |
| 219 | return userSymbol; |
| 220 | } |
| 221 | |
| 222 | return findBuiltIn(name, shaderVersion); |
| 223 | } |
| 224 | |
| 225 | const TSymbol *TSymbolTable::findUserDefined(const ImmutableString &name) const |
| 226 | { |
| 227 | int userDefinedLevel = static_cast<int>(mTable.size()) - 1; |
| 228 | while (userDefinedLevel >= 0) |
| 229 | { |
| 230 | const TSymbol *symbol = mTable[userDefinedLevel]->find(name); |
| 231 | if (symbol) |
| 232 | { |
| 233 | return symbol; |
| 234 | } |
| 235 | userDefinedLevel--; |
| 236 | } |
| 237 | |
| 238 | return nullptr; |
| 239 | } |
| 240 | |
| 241 | TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const |
| 242 | { |
| 243 | // User-defined functions are always declared at the global level. |
| 244 | ASSERT(!mTable.empty()); |
| 245 | return static_cast<TFunction *>(mTable[0]->find(name)); |
| 246 | } |
| 247 | |
| 248 | const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const |
| 249 | { |
| 250 | ASSERT(!mTable.empty()); |
| 251 | return mTable[0]->find(name); |
| 252 | } |
| 253 | |
| 254 | bool TSymbolTable::declare(TSymbol *symbol) |
| 255 | { |
| 256 | ASSERT(!mTable.empty()); |
| 257 | ASSERT(symbol->symbolType() == SymbolType::UserDefined); |
| 258 | ASSERT(!symbol->isFunction()); |
| 259 | return mTable.back()->insert(symbol); |
| 260 | } |
| 261 | |
| 262 | bool TSymbolTable::declareInternal(TSymbol *symbol) |
| 263 | { |
| 264 | ASSERT(!mTable.empty()); |
| 265 | ASSERT(symbol->symbolType() == SymbolType::AngleInternal); |
| 266 | ASSERT(!symbol->isFunction()); |
| 267 | return mTable.back()->insert(symbol); |
| 268 | } |
| 269 | |
| 270 | void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName) |
| 271 | { |
| 272 | ASSERT(!mTable.empty()); |
| 273 | if (insertUnmangledName) |
| 274 | { |
| 275 | // Insert the unmangled name to detect potential future redefinition as a variable. |
| 276 | mTable[0]->insertUnmangled(function); |
| 277 | } |
| 278 | mTable[0]->insert(function); |
| 279 | } |
| 280 | |
| 281 | void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec) |
| 282 | { |
| 283 | int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1; |
| 284 | // Uses map operator [], overwrites the current value |
| 285 | (*mPrecisionStack[indexOfLastElement])[type] = prec; |
| 286 | } |
| 287 | |
| 288 | TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const |
| 289 | { |
| 290 | if (!SupportsPrecision(type)) |
| 291 | return EbpUndefined; |
| 292 | |
| 293 | // unsigned integers use the same precision as signed |
| 294 | TBasicType baseType = (type == EbtUInt) ? EbtInt : type; |
| 295 | |
| 296 | int level = static_cast<int>(mPrecisionStack.size()) - 1; |
| 297 | ASSERT(level >= 0); // Just to be safe. Should not happen. |
| 298 | // If we dont find anything we return this. Some types don't have predefined default precision. |
| 299 | TPrecision prec = EbpUndefined; |
| 300 | while (level >= 0) |
| 301 | { |
| 302 | PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType); |
| 303 | if (it != mPrecisionStack[level]->end()) |
| 304 | { |
| 305 | prec = (*it).second; |
| 306 | break; |
| 307 | } |
| 308 | level--; |
| 309 | } |
| 310 | return prec; |
| 311 | } |
| 312 | |
| 313 | void TSymbolTable::clearCompilationResults() |
| 314 | { |
| 315 | mGlobalInvariant = false; |
| 316 | mUniqueIdCounter = kLastBuiltInId + 1; |
| 317 | mVariableMetadata.clear(); |
| 318 | mGlInVariableWithArraySize = nullptr; |
| 319 | |
| 320 | // User-defined scopes should have already been cleared when the compilation finished. |
| 321 | ASSERT(mTable.empty()); |
| 322 | } |
| 323 | |
| 324 | int TSymbolTable::nextUniqueIdValue() |
| 325 | { |
| 326 | ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max()); |
| 327 | return ++mUniqueIdCounter; |
| 328 | } |
| 329 | |
| 330 | void TSymbolTable::initializeBuiltIns(sh::GLenum type, |
| 331 | ShShaderSpec spec, |
| 332 | const ShBuiltInResources &resources) |
| 333 | { |
| 334 | mShaderType = type; |
| 335 | mResources = resources; |
| 336 | |
| 337 | // We need just one precision stack level for predefined precisions. |
| 338 | mPrecisionStack.emplace_back(new PrecisionStackLevel); |
| 339 | |
| 340 | switch (type) |
| 341 | { |
| 342 | case GL_FRAGMENT_SHADER: |
| 343 | setDefaultPrecision(EbtInt, EbpMedium); |
| 344 | break; |
| 345 | case GL_VERTEX_SHADER: |
| 346 | case GL_COMPUTE_SHADER: |
| 347 | case GL_GEOMETRY_SHADER_EXT: |
| 348 | setDefaultPrecision(EbtInt, EbpHigh); |
| 349 | setDefaultPrecision(EbtFloat, EbpHigh); |
| 350 | break; |
| 351 | default: |
| 352 | UNREACHABLE(); |
| 353 | } |
| 354 | // Set defaults for sampler types that have default precision, even those that are |
| 355 | // only available if an extension exists. |
| 356 | // New sampler types in ESSL3 don't have default precision. ESSL1 types do. |
| 357 | initSamplerDefaultPrecision(EbtSampler2D); |
| 358 | initSamplerDefaultPrecision(EbtSamplerCube); |
| 359 | // SamplerExternalOES is specified in the extension to have default precision. |
| 360 | initSamplerDefaultPrecision(EbtSamplerExternalOES); |
| 361 | // SamplerExternal2DY2YEXT is specified in the extension to have default precision. |
| 362 | initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT); |
| 363 | // It isn't specified whether Sampler2DRect has default precision. |
| 364 | initSamplerDefaultPrecision(EbtSampler2DRect); |
| 365 | |
| 366 | setDefaultPrecision(EbtAtomicCounter, EbpHigh); |
| 367 | |
| 368 | initializeBuiltInVariables(type, spec, resources); |
| 369 | mUniqueIdCounter = kLastBuiltInId + 1; |
| 370 | } |
| 371 | |
| 372 | void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType) |
| 373 | { |
| 374 | ASSERT(samplerType >= EbtGuardSamplerBegin && samplerType <= EbtGuardSamplerEnd); |
| 375 | setDefaultPrecision(samplerType, EbpLow); |
| 376 | } |
| 377 | |
| 378 | TSymbolTable::VariableMetadata::VariableMetadata() |
| 379 | : staticRead(false), staticWrite(false), invariant(false) |
| 380 | {} |
| 381 | } // namespace sh |
| 382 | |