John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // |
| 8 | // Symbol table for parsing. Most functionaliy and main ideas |
| 9 | // are documented in the header file. |
| 10 | // |
| 11 | |
| 12 | #if defined(_MSC_VER) |
| 13 | #pragma warning(disable: 4718) |
| 14 | #endif |
| 15 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 16 | #include "SymbolTable.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 17 | |
| 18 | #include <stdio.h> |
Nicolas Capens | 94352d9 | 2015-06-11 08:53:40 -0500 | [diff] [blame^] | 19 | #include <limits.h> |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
| 22 | #if defined(_MSC_VER) |
| 23 | #define snprintf _snprintf |
| 24 | #endif |
| 25 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 26 | int TSymbolTableLevel::uniqueId = 0; |
| 27 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 28 | TType::TType(const TPublicType &p) : |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 29 | type(p.type), precision(p.precision), primarySize(p.primarySize), secondarySize(p.secondarySize), qualifier(p.qualifier), array(p.array), arraySize(p.arraySize), |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 30 | maxArraySize(0), arrayInformationType(0), structure(0), deepestStructNesting(0), mangled(0) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 31 | { |
| 32 | if (p.userDef) |
| 33 | { |
| 34 | structure = p.userDef->getStruct(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 35 | computeDeepestStructNesting(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // |
| 40 | // Recursively generate mangled names. |
| 41 | // |
| 42 | void TType::buildMangledName(TString& mangledName) |
| 43 | { |
| 44 | if (isMatrix()) |
| 45 | mangledName += 'm'; |
| 46 | else if (isVector()) |
| 47 | mangledName += 'v'; |
| 48 | |
| 49 | switch (type) { |
| 50 | case EbtFloat: mangledName += 'f'; break; |
| 51 | case EbtInt: mangledName += 'i'; break; |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 52 | case EbtUInt: mangledName += 'u'; break; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 53 | case EbtBool: mangledName += 'b'; break; |
| 54 | case EbtSampler2D: mangledName += "s2"; break; |
Alexis Hetu | b027aa9 | 2015-01-19 15:56:12 -0500 | [diff] [blame] | 55 | case EbtSampler3D: mangledName += "s3"; break; |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 56 | case EbtSamplerCube: mangledName += "sC"; break; |
| 57 | case EbtSampler2DArray: mangledName += "s2a"; break; |
| 58 | case EbtSamplerExternalOES: mangledName += "sext"; break; |
| 59 | case EbtISampler2D: mangledName += "is2"; break; |
| 60 | case EbtISampler3D: mangledName += "is3"; break; |
| 61 | case EbtISamplerCube: mangledName += "isC"; break; |
| 62 | case EbtISampler2DArray: mangledName += "is2a"; break; |
| 63 | case EbtUSampler2D: mangledName += "us2"; break; |
| 64 | case EbtUSampler3D: mangledName += "us3"; break; |
| 65 | case EbtUSamplerCube: mangledName += "usC"; break; |
| 66 | case EbtUSampler2DArray: mangledName += "us2a"; break; |
| 67 | case EbtSampler2DShadow: mangledName += "s2s"; break; |
| 68 | case EbtSamplerCubeShadow: mangledName += "sCs"; break; |
| 69 | case EbtSampler2DArrayShadow: mangledName += "s2as"; break; |
| 70 | case EbtStruct: mangledName += structure->mangledName(); break; |
| 71 | case EbtInterfaceBlock: mangledName += interfaceBlock->mangledName(); break; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | mangledName += static_cast<char>('0' + getNominalSize()); |
Alexis Hetu | 3935865 | 2015-05-26 14:34:54 -0400 | [diff] [blame] | 77 | if(isMatrix()) { |
| 78 | mangledName += static_cast<char>('0' + getSecondarySize()); |
| 79 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 80 | if (isArray()) { |
| 81 | char buf[20]; |
| 82 | snprintf(buf, sizeof(buf), "%d", arraySize); |
| 83 | mangledName += '['; |
| 84 | mangledName += buf; |
| 85 | mangledName += ']'; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int TType::getStructSize() const |
| 90 | { |
| 91 | if (!getStruct()) { |
| 92 | assert(false && "Not a struct"); |
| 93 | return 0; |
| 94 | } |
| 95 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 96 | return getStruct()->objectSize(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void TType::computeDeepestStructNesting() |
| 100 | { |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 101 | deepestStructNesting = structure ? structure->deepestNesting() : 0; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 104 | bool TStructure::containsArrays() const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 105 | { |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 106 | for(size_t i = 0; i < mFields->size(); ++i) |
| 107 | { |
| 108 | const TType *fieldType = (*mFields)[i]->type(); |
| 109 | if(fieldType->isArray() || fieldType->isStructureContainingArrays()) |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 114 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 115 | bool TStructure::containsSamplers() const |
| 116 | { |
| 117 | for(size_t i = 0; i < mFields->size(); ++i) |
| 118 | { |
| 119 | const TType *fieldType = (*mFields)[i]->type(); |
| 120 | if(IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers()) |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 125 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 126 | TString TFieldListCollection::buildMangledName() const |
| 127 | { |
| 128 | TString mangledName(mangledNamePrefix()); |
| 129 | mangledName += *mName; |
| 130 | for(size_t i = 0; i < mFields->size(); ++i) |
| 131 | { |
| 132 | mangledName += '-'; |
| 133 | mangledName += (*mFields)[i]->type()->getMangledName(); |
| 134 | } |
| 135 | return mangledName; |
| 136 | } |
| 137 | |
| 138 | size_t TFieldListCollection::calculateObjectSize() const |
| 139 | { |
| 140 | size_t size = 0; |
| 141 | for(size_t i = 0; i < mFields->size(); ++i) |
| 142 | { |
| 143 | size_t fieldSize = (*mFields)[i]->type()->getObjectSize(); |
| 144 | if(fieldSize > INT_MAX - size) |
| 145 | size = INT_MAX; |
| 146 | else |
| 147 | size += fieldSize; |
| 148 | } |
| 149 | return size; |
| 150 | } |
| 151 | |
| 152 | int TStructure::calculateDeepestNesting() const |
| 153 | { |
| 154 | int maxNesting = 0; |
| 155 | for(size_t i = 0; i < mFields->size(); ++i) |
| 156 | maxNesting = std::max(maxNesting, (*mFields)[i]->type()->getDeepestStructNesting()); |
| 157 | return 1 + maxNesting; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 161 | // Functions have buried pointers to delete. |
| 162 | // |
| 163 | TFunction::~TFunction() |
| 164 | { |
| 165 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 166 | delete (*i).type; |
| 167 | } |
| 168 | |
| 169 | // |
| 170 | // Symbol table levels are a map of pointers to symbols that have to be deleted. |
| 171 | // |
| 172 | TSymbolTableLevel::~TSymbolTableLevel() |
| 173 | { |
| 174 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 175 | delete (*it).second; |
| 176 | } |
| 177 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 178 | TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) const |
| 179 | { |
| 180 | int level = currentLevel(); |
| 181 | TSymbol *symbol = nullptr; |
| 182 | |
| 183 | do |
| 184 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 185 | while((level == ESSL3_BUILTINS && shaderVersion != 300) || |
| 186 | (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels |
| 187 | { |
| 188 | --level; |
| 189 | } |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 190 | |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 191 | symbol = table[level]->find(name); |
| 192 | } |
| 193 | while(!symbol && --level >= 0); // Doesn't decrement level when a symbol was found |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 194 | |
| 195 | if(builtIn) |
| 196 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 197 | *builtIn = (level <= LAST_BUILTIN_LEVEL); |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | if(sameScope) |
| 201 | { |
| 202 | *sameScope = (level == currentLevel()); |
| 203 | } |
| 204 | |
| 205 | return symbol; |
| 206 | } |
| 207 | |
| 208 | TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const |
| 209 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 210 | for(int level = LAST_BUILTIN_LEVEL; level >= 0; --level) |
| 211 | { |
| 212 | while((level == ESSL3_BUILTINS && shaderVersion != 300) || |
| 213 | (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels |
| 214 | { |
| 215 | --level; |
| 216 | } |
| 217 | |
| 218 | TSymbol *symbol = table[level]->find(name); |
| 219 | |
| 220 | if(symbol) |
| 221 | { |
| 222 | return symbol; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return 0; |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 227 | } |
| 228 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 229 | TSymbol::TSymbol(const TSymbol& copyOf) |
| 230 | { |
| 231 | name = NewPoolTString(copyOf.name->c_str()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 232 | } |