daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [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 | |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 12 | #if defined(_MSC_VER) |
| 13 | #pragma warning(disable: 4718) |
| 14 | #endif |
| 15 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 16 | #include "compiler/translator/SymbolTable.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
apatrick@chromium.org | 8187fa8 | 2010-06-15 22:09:28 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 21 | int TSymbolTable::uniqueIdCounter = 0; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 22 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | // Functions have buried pointers to delete. |
| 25 | // |
| 26 | TFunction::~TFunction() |
| 27 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 28 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 29 | delete (*i).type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // |
| 33 | // Symbol table levels are a map of pointers to symbols that have to be deleted. |
| 34 | // |
| 35 | TSymbolTableLevel::~TSymbolTableLevel() |
| 36 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 37 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 38 | delete (*it).second; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 41 | bool TSymbolTableLevel::insert(TSymbol *symbol) |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 42 | { |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 43 | symbol->setUniqueId(TSymbolTable::nextUniqueId()); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 44 | |
| 45 | // returning true means symbol was added to the table |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 46 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 47 | |
| 48 | return result.second; |
| 49 | } |
| 50 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 51 | TSymbol *TSymbolTableLevel::find(const TString &name) const |
| 52 | { |
| 53 | tLevel::const_iterator it = level.find(name); |
| 54 | if (it == level.end()) |
| 55 | return 0; |
| 56 | else |
| 57 | return (*it).second; |
| 58 | } |
| 59 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 60 | TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, |
| 61 | bool *builtIn, bool *sameScope) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 62 | { |
| 63 | int level = currentLevel(); |
| 64 | TSymbol *symbol; |
| 65 | |
| 66 | do |
| 67 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 68 | if (level == ESSL3_BUILTINS && shaderVersion != 300) |
| 69 | level--; |
| 70 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 71 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 72 | |
| 73 | symbol = table[level]->find(name); |
| 74 | } |
| 75 | while (symbol == 0 && --level >= 0); |
| 76 | |
| 77 | if (builtIn) |
| 78 | *builtIn = (level <= LAST_BUILTIN_LEVEL); |
| 79 | if (sameScope) |
| 80 | *sameScope = (level == currentLevel()); |
| 81 | |
| 82 | return symbol; |
| 83 | } |
| 84 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 85 | TSymbol *TSymbolTable::findBuiltIn( |
| 86 | const TString &name, int shaderVersion) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 87 | { |
| 88 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--) |
| 89 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 90 | if (level == ESSL3_BUILTINS && shaderVersion != 300) |
| 91 | level--; |
| 92 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 93 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 94 | |
| 95 | TSymbol *symbol = table[level]->find(name); |
| 96 | |
| 97 | if (symbol) |
| 98 | return symbol; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 103 | |
| 104 | TSymbolTable::~TSymbolTable() |
| 105 | { |
| 106 | while (table.size() > 0) |
| 107 | pop(); |
| 108 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 109 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 110 | bool IsGenType(const TType *type) |
| 111 | { |
| 112 | if (type) |
| 113 | { |
| 114 | TBasicType basicType = type->getBasicType(); |
| 115 | return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType; |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | bool IsVecType(const TType *type) |
| 122 | { |
| 123 | if (type) |
| 124 | { |
| 125 | TBasicType basicType = type->getBasicType(); |
| 126 | return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec; |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | TType *SpecificType(TType *type, int size) |
| 133 | { |
| 134 | ASSERT(size >= 1 && size <= 4); |
| 135 | |
| 136 | if (!type) |
| 137 | { |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | ASSERT(!IsVecType(type)); |
| 142 | |
| 143 | switch(type->getBasicType()) |
| 144 | { |
| 145 | case EbtGenType: return new TType(EbtFloat, size); |
| 146 | case EbtGenIType: return new TType(EbtInt, size); |
| 147 | case EbtGenUType: return new TType(EbtUInt, size); |
| 148 | case EbtGenBType: return new TType(EbtBool, size); |
| 149 | default: return type; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | TType *VectorType(TType *type, int size) |
| 154 | { |
| 155 | ASSERT(size >= 2 && size <= 4); |
| 156 | |
| 157 | if (!type) |
| 158 | { |
| 159 | return nullptr; |
| 160 | } |
| 161 | |
| 162 | ASSERT(!IsGenType(type)); |
| 163 | |
| 164 | switch(type->getBasicType()) |
| 165 | { |
| 166 | case EbtVec: return new TType(EbtFloat, size); |
| 167 | case EbtIVec: return new TType(EbtInt, size); |
| 168 | case EbtUVec: return new TType(EbtUInt, size); |
| 169 | case EbtBVec: return new TType(EbtBool, size); |
| 170 | default: return type; |
| 171 | } |
| 172 | } |
| 173 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame^] | 174 | void TSymbolTable::insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name, |
| 175 | TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 176 | { |
| 177 | if (ptype1->getBasicType() == EbtGSampler2D) |
| 178 | { |
| 179 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 180 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5); |
| 181 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5); |
| 182 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 183 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 184 | else if (ptype1->getBasicType() == EbtGSampler3D) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 185 | { |
| 186 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 187 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5); |
| 188 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5); |
| 189 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 190 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 191 | else if (ptype1->getBasicType() == EbtGSamplerCube) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 192 | { |
| 193 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 194 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 195 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 196 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 197 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 198 | else if (ptype1->getBasicType() == EbtGSampler2DArray) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 199 | { |
| 200 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 201 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 202 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 203 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 204 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 205 | else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3)) |
| 206 | { |
| 207 | ASSERT(!ptype4 && !ptype5); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame^] | 208 | insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1)); |
| 209 | insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2)); |
| 210 | insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3)); |
| 211 | insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4)); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 212 | } |
| 213 | else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3)) |
| 214 | { |
| 215 | ASSERT(!ptype4 && !ptype5); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame^] | 216 | insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2)); |
| 217 | insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3)); |
| 218 | insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4)); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame^] | 222 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 223 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 224 | TParameter param1 = {0, ptype1}; |
| 225 | function->addParameter(param1); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 226 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 227 | if (ptype2) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 228 | { |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 229 | TParameter param2 = {0, ptype2}; |
| 230 | function->addParameter(param2); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 231 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 232 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 233 | if (ptype3) |
| 234 | { |
| 235 | TParameter param3 = {0, ptype3}; |
| 236 | function->addParameter(param3); |
| 237 | } |
| 238 | |
| 239 | if (ptype4) |
| 240 | { |
| 241 | TParameter param4 = {0, ptype4}; |
| 242 | function->addParameter(param4); |
| 243 | } |
| 244 | |
| 245 | if (ptype5) |
| 246 | { |
| 247 | TParameter param5 = {0, ptype5}; |
| 248 | function->addParameter(param5); |
| 249 | } |
| 250 | |
| 251 | insert(level, function); |
| 252 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 255 | TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 256 | { |
| 257 | if (!SupportsPrecision(type)) |
| 258 | return EbpUndefined; |
| 259 | |
| 260 | // unsigned integers use the same precision as signed |
| 261 | TBasicType baseType = (type == EbtUInt) ? EbtInt : type; |
| 262 | |
| 263 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 264 | assert(level >= 0); // Just to be safe. Should not happen. |
| 265 | // If we dont find anything we return this. Should we error check this? |
| 266 | TPrecision prec = EbpUndefined; |
| 267 | while (level >= 0) |
| 268 | { |
| 269 | PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType); |
| 270 | if (it != precisionStack[level]->end()) |
| 271 | { |
| 272 | prec = (*it).second; |
| 273 | break; |
| 274 | } |
| 275 | level--; |
| 276 | } |
| 277 | return prec; |
| 278 | } |