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