| 1 | // |
| 2 | // Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
| 3 | // Copyright (C) 2013 LunarG, Inc. |
| 4 | // Copyright (C) 2015-2018 Google, Inc. |
| 5 | // |
| 6 | // All rights reserved. |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions |
| 10 | // are met: |
| 11 | // |
| 12 | // Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // |
| 15 | // Redistributions in binary form must reproduce the above |
| 16 | // copyright notice, this list of conditions and the following |
| 17 | // disclaimer in the documentation and/or other materials provided |
| 18 | // with the distribution. |
| 19 | // |
| 20 | // Neither the name of 3Dlabs Inc. Ltd. nor the names of its |
| 21 | // contributors may be used to endorse or promote products derived |
| 22 | // from this software without specific prior written permission. |
| 23 | // |
| 24 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 | // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 31 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 34 | // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 | // POSSIBILITY OF SUCH DAMAGE. |
| 36 | // |
| 37 | |
| 38 | #ifndef _SYMBOL_TABLE_INCLUDED_ |
| 39 | #define _SYMBOL_TABLE_INCLUDED_ |
| 40 | |
| 41 | // |
| 42 | // Symbol table for parsing. Has these design characteristics: |
| 43 | // |
| 44 | // * Same symbol table can be used to compile many shaders, to preserve |
| 45 | // effort of creating and loading with the large numbers of built-in |
| 46 | // symbols. |
| 47 | // |
| 48 | // --> This requires a copy mechanism, so initial pools used to create |
| 49 | // the shared information can be popped. Done through "clone" |
| 50 | // methods. |
| 51 | // |
| 52 | // * Name mangling will be used to give each function a unique name |
| 53 | // so that symbol table lookups are never ambiguous. This allows |
| 54 | // a simpler symbol table structure. |
| 55 | // |
| 56 | // * Pushing and popping of scope, so symbol table will really be a stack |
| 57 | // of symbol tables. Searched from the top, with new inserts going into |
| 58 | // the top. |
| 59 | // |
| 60 | // * Constants: Compile time constant symbols will keep their values |
| 61 | // in the symbol table. The parser can substitute constants at parse |
| 62 | // time, including doing constant folding and constant propagation. |
| 63 | // |
| 64 | // * No temporaries: Temporaries made from operations (+, --, .xy, etc.) |
| 65 | // are tracked in the intermediate representation, not the symbol table. |
| 66 | // |
| 67 | |
| 68 | #include "../Include/Common.h" |
| 69 | #include "../Include/intermediate.h" |
| 70 | #include "../Include/InfoSink.h" |
| 71 | #include <cstdint> |
| 72 | |
| 73 | namespace QtShaderTools { |
| 74 | namespace glslang { |
| 75 | |
| 76 | // |
| 77 | // Symbol base class. (Can build functions or variables out of these...) |
| 78 | // |
| 79 | |
| 80 | class TVariable; |
| 81 | class TFunction; |
| 82 | class TAnonMember; |
| 83 | |
| 84 | typedef TVector<const char*> TExtensionList; |
| 85 | |
| 86 | class TSymbol { |
| 87 | public: |
| 88 | POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator()) |
| 89 | explicit TSymbol(const TString *n, const TString *mn) : name(n), mangledName(mn), uniqueId(0), extensions(nullptr), writable(true) { } |
| 90 | explicit TSymbol(const TString *n) : TSymbol(n, n) { } |
| 91 | virtual TSymbol* clone() const = 0; |
| 92 | virtual ~TSymbol() { } // rely on all symbol owned memory coming from the pool |
| 93 | |
| 94 | virtual const TString& getName() const { return *name; } |
| 95 | virtual void changeName(const TString* newName) { name = newName; } |
| 96 | virtual void addPrefix(const char* prefix) |
| 97 | { |
| 98 | TString newName(prefix); |
| 99 | newName.append(str: *name); |
| 100 | changeName(newName: NewPoolTString(s: newName.c_str())); |
| 101 | } |
| 102 | virtual const TString& getMangledName() const { return *mangledName; } |
| 103 | virtual TFunction* getAsFunction() { return nullptr; } |
| 104 | virtual const TFunction* getAsFunction() const { return nullptr; } |
| 105 | virtual TVariable* getAsVariable() { return nullptr; } |
| 106 | virtual const TVariable* getAsVariable() const { return nullptr; } |
| 107 | virtual const TAnonMember* getAsAnonMember() const { return nullptr; } |
| 108 | virtual const TType& getType() const = 0; |
| 109 | virtual TType& getWritableType() = 0; |
| 110 | virtual void setUniqueId(long long id) { uniqueId = id; } |
| 111 | virtual long long getUniqueId() const { return uniqueId; } |
| 112 | virtual void setExtensions(int numExts, const char* const exts[]) |
| 113 | { |
| 114 | assert(extensions == nullptr); |
| 115 | assert(numExts > 0); |
| 116 | extensions = NewPoolObject(extensions); |
| 117 | for (int e = 0; e < numExts; ++e) |
| 118 | extensions->push_back(x: exts[e]); |
| 119 | } |
| 120 | virtual int getNumExtensions() const { return extensions == nullptr ? 0 : (int)extensions->size(); } |
| 121 | virtual const char** getExtensions() const { return extensions->data(); } |
| 122 | |
| 123 | virtual void dump(TInfoSink& infoSink, bool complete = false) const = 0; |
| 124 | void dumpExtensions(TInfoSink& infoSink) const; |
| 125 | |
| 126 | virtual bool isReadOnly() const { return ! writable; } |
| 127 | virtual void makeReadOnly() { writable = false; } |
| 128 | |
| 129 | protected: |
| 130 | explicit TSymbol(const TSymbol&); |
| 131 | TSymbol& operator=(const TSymbol&); |
| 132 | |
| 133 | const TString *name; |
| 134 | const TString *mangledName; |
| 135 | unsigned long long uniqueId; // For cross-scope comparing during code generation |
| 136 | |
| 137 | // For tracking what extensions must be present |
| 138 | // (don't use if correct version/profile is present). |
| 139 | TExtensionList* extensions; // an array of pointers to existing constant char strings |
| 140 | |
| 141 | // |
| 142 | // N.B.: Non-const functions that will be generally used should assert on this, |
| 143 | // to avoid overwriting shared symbol-table information. |
| 144 | // |
| 145 | bool writable; |
| 146 | }; |
| 147 | |
| 148 | // |
| 149 | // Variable class, meaning a symbol that's not a function. |
| 150 | // |
| 151 | // There could be a separate class hierarchy for Constant variables; |
| 152 | // Only one of int, bool, or float, (or none) is correct for |
| 153 | // any particular use, but it's easy to do this way, and doesn't |
| 154 | // seem worth having separate classes, and "getConst" can't simply return |
| 155 | // different values for different types polymorphically, so this is |
| 156 | // just simple and pragmatic. |
| 157 | // |
| 158 | class TVariable : public TSymbol { |
| 159 | public: |
| 160 | TVariable(const TString *name, const TType& t, bool uT = false ) |
| 161 | : TVariable(name, name, t, uT) {} |
| 162 | TVariable(const TString *name, const TString *mangledName, const TType& t, bool uT = false ) |
| 163 | : TSymbol(name, mangledName), |
| 164 | userType(uT), |
| 165 | constSubtree(nullptr), |
| 166 | memberExtensions(nullptr), |
| 167 | anonId(-1) |
| 168 | { type.shallowCopy(copyOf: t); } |
| 169 | virtual TVariable* clone() const; |
| 170 | virtual ~TVariable() { } |
| 171 | |
| 172 | virtual TVariable* getAsVariable() { return this; } |
| 173 | virtual const TVariable* getAsVariable() const { return this; } |
| 174 | virtual const TType& getType() const { return type; } |
| 175 | virtual TType& getWritableType() { assert(writable); return type; } |
| 176 | virtual bool isUserType() const { return userType; } |
| 177 | virtual const TConstUnionArray& getConstArray() const { return constArray; } |
| 178 | virtual TConstUnionArray& getWritableConstArray() { assert(writable); return constArray; } |
| 179 | virtual void setConstArray(const TConstUnionArray& array) { constArray = array; } |
| 180 | virtual void setConstSubtree(TIntermTyped* subtree) { constSubtree = subtree; } |
| 181 | virtual TIntermTyped* getConstSubtree() const { return constSubtree; } |
| 182 | virtual void setAnonId(int i) { anonId = i; } |
| 183 | virtual int getAnonId() const { return anonId; } |
| 184 | |
| 185 | virtual void setMemberExtensions(int member, int numExts, const char* const exts[]) |
| 186 | { |
| 187 | assert(type.isStruct()); |
| 188 | assert(numExts > 0); |
| 189 | if (memberExtensions == nullptr) { |
| 190 | memberExtensions = NewPoolObject(memberExtensions); |
| 191 | memberExtensions->resize(new_size: type.getStruct()->size()); |
| 192 | } |
| 193 | for (int e = 0; e < numExts; ++e) |
| 194 | (*memberExtensions)[member].push_back(x: exts[e]); |
| 195 | } |
| 196 | virtual bool hasMemberExtensions() const { return memberExtensions != nullptr; } |
| 197 | virtual int getNumMemberExtensions(int member) const |
| 198 | { |
| 199 | return memberExtensions == nullptr ? 0 : (int)(*memberExtensions)[member].size(); |
| 200 | } |
| 201 | virtual const char** getMemberExtensions(int member) const { return (*memberExtensions)[member].data(); } |
| 202 | |
| 203 | virtual void dump(TInfoSink& infoSink, bool complete = false) const; |
| 204 | |
| 205 | protected: |
| 206 | explicit TVariable(const TVariable&); |
| 207 | TVariable& operator=(const TVariable&); |
| 208 | |
| 209 | TType type; |
| 210 | bool userType; |
| 211 | |
| 212 | // we are assuming that Pool Allocator will free the memory allocated to unionArray |
| 213 | // when this object is destroyed |
| 214 | |
| 215 | TConstUnionArray constArray; // for compile-time constant value |
| 216 | TIntermTyped* constSubtree; // for specialization constant computation |
| 217 | TVector<TExtensionList>* memberExtensions; // per-member extension list, allocated only when needed |
| 218 | int anonId; // the ID used for anonymous blocks: TODO: see if uniqueId could serve a dual purpose |
| 219 | }; |
| 220 | |
| 221 | // |
| 222 | // The function sub-class of symbols and the parser will need to |
| 223 | // share this definition of a function parameter. |
| 224 | // |
| 225 | struct TParameter { |
| 226 | TString *name; |
| 227 | TType* type; |
| 228 | TIntermTyped* defaultValue; |
| 229 | TParameter& copyParam(const TParameter& param) |
| 230 | { |
| 231 | if (param.name) |
| 232 | name = NewPoolTString(s: param.name->c_str()); |
| 233 | else |
| 234 | name = nullptr; |
| 235 | type = param.type->clone(); |
| 236 | defaultValue = param.defaultValue; |
| 237 | return *this; |
| 238 | } |
| 239 | TBuiltInVariable getDeclaredBuiltIn() const { return type->getQualifier().declaredBuiltIn; } |
| 240 | }; |
| 241 | |
| 242 | // |
| 243 | // The function sub-class of a symbol. |
| 244 | // |
| 245 | class TFunction : public TSymbol { |
| 246 | public: |
| 247 | explicit TFunction(TOperator o) : |
| 248 | TSymbol(nullptr), |
| 249 | op(o), |
| 250 | defined(false), prototyped(false), implicitThis(false), illegalImplicitThis(false), defaultParamCount(0) { } |
| 251 | TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull) : |
| 252 | TSymbol(name), |
| 253 | mangledName(*name + '('), |
| 254 | op(tOp), |
| 255 | defined(false), prototyped(false), implicitThis(false), illegalImplicitThis(false), defaultParamCount(0), |
| 256 | linkType(ELinkNone) |
| 257 | { |
| 258 | returnType.shallowCopy(copyOf: retType); |
| 259 | declaredBuiltIn = retType.getQualifier().builtIn; |
| 260 | } |
| 261 | virtual TFunction* clone() const override; |
| 262 | virtual ~TFunction(); |
| 263 | |
| 264 | virtual TFunction* getAsFunction() override { return this; } |
| 265 | virtual const TFunction* getAsFunction() const override { return this; } |
| 266 | |
| 267 | // Install 'p' as the (non-'this') last parameter. |
| 268 | // Non-'this' parameters are reflected in both the list of parameters and the |
| 269 | // mangled name. |
| 270 | virtual void addParameter(TParameter& p) |
| 271 | { |
| 272 | assert(writable); |
| 273 | parameters.push_back(x: p); |
| 274 | p.type->appendMangledName(name&: mangledName); |
| 275 | |
| 276 | if (p.defaultValue != nullptr) |
| 277 | defaultParamCount++; |
| 278 | } |
| 279 | |
| 280 | // Install 'this' as the first parameter. |
| 281 | // 'this' is reflected in the list of parameters, but not the mangled name. |
| 282 | virtual void addThisParameter(TType& type, const char* name) |
| 283 | { |
| 284 | TParameter p = { .name: NewPoolTString(s: name), .type: new TType, .defaultValue: nullptr }; |
| 285 | p.type->shallowCopy(copyOf: type); |
| 286 | parameters.insert(position: parameters.begin(), x: p); |
| 287 | } |
| 288 | |
| 289 | virtual void addPrefix(const char* prefix) override |
| 290 | { |
| 291 | TSymbol::addPrefix(prefix); |
| 292 | mangledName.insert(pos: 0, s: prefix); |
| 293 | } |
| 294 | |
| 295 | virtual void removePrefix(const TString& prefix) |
| 296 | { |
| 297 | assert(mangledName.compare(0, prefix.size(), prefix) == 0); |
| 298 | mangledName.erase(pos: 0, n: prefix.size()); |
| 299 | } |
| 300 | |
| 301 | virtual const TString& getMangledName() const override { return mangledName; } |
| 302 | virtual const TType& getType() const override { return returnType; } |
| 303 | virtual TBuiltInVariable getDeclaredBuiltInType() const { return declaredBuiltIn; } |
| 304 | virtual TType& getWritableType() override { return returnType; } |
| 305 | virtual void relateToOperator(TOperator o) { assert(writable); op = o; } |
| 306 | virtual TOperator getBuiltInOp() const { return op; } |
| 307 | virtual void setDefined() { assert(writable); defined = true; } |
| 308 | virtual bool isDefined() const { return defined; } |
| 309 | virtual void setPrototyped() { assert(writable); prototyped = true; } |
| 310 | virtual bool isPrototyped() const { return prototyped; } |
| 311 | virtual void setImplicitThis() { assert(writable); implicitThis = true; } |
| 312 | virtual bool hasImplicitThis() const { return implicitThis; } |
| 313 | virtual void setIllegalImplicitThis() { assert(writable); illegalImplicitThis = true; } |
| 314 | virtual bool hasIllegalImplicitThis() const { return illegalImplicitThis; } |
| 315 | |
| 316 | // Return total number of parameters |
| 317 | virtual int getParamCount() const { return static_cast<int>(parameters.size()); } |
| 318 | // Return number of parameters with default values. |
| 319 | virtual int getDefaultParamCount() const { return defaultParamCount; } |
| 320 | // Return number of fixed parameters (without default values) |
| 321 | virtual int getFixedParamCount() const { return getParamCount() - getDefaultParamCount(); } |
| 322 | |
| 323 | virtual TParameter& operator[](int i) { assert(writable); return parameters[i]; } |
| 324 | virtual const TParameter& operator[](int i) const { return parameters[i]; } |
| 325 | const TQualifier& getQualifier() const { return returnType.getQualifier(); } |
| 326 | |
| 327 | virtual void setSpirvInstruction(const TSpirvInstruction& inst) |
| 328 | { |
| 329 | relateToOperator(o: EOpSpirvInst); |
| 330 | spirvInst = inst; |
| 331 | } |
| 332 | virtual const TSpirvInstruction& getSpirvInstruction() const { return spirvInst; } |
| 333 | |
| 334 | virtual void dump(TInfoSink& infoSink, bool complete = false) const override; |
| 335 | |
| 336 | void setExport() { linkType = ELinkExport; } |
| 337 | TLinkType getLinkType() const { return linkType; } |
| 338 | |
| 339 | protected: |
| 340 | explicit TFunction(const TFunction&); |
| 341 | TFunction& operator=(const TFunction&); |
| 342 | |
| 343 | typedef TVector<TParameter> TParamList; |
| 344 | TParamList parameters; |
| 345 | TType returnType; |
| 346 | TBuiltInVariable declaredBuiltIn; |
| 347 | |
| 348 | TString mangledName; |
| 349 | TOperator op; |
| 350 | bool defined; |
| 351 | bool prototyped; |
| 352 | bool implicitThis; // True if this function is allowed to see all members of 'this' |
| 353 | bool illegalImplicitThis; // True if this function is not supposed to have access to dynamic members of 'this', |
| 354 | // even if it finds member variables in the symbol table. |
| 355 | // This is important for a static member function that has member variables in scope, |
| 356 | // but is not allowed to use them, or see hidden symbols instead. |
| 357 | int defaultParamCount; |
| 358 | |
| 359 | TSpirvInstruction spirvInst; // SPIR-V instruction qualifiers |
| 360 | TLinkType linkType; |
| 361 | }; |
| 362 | |
| 363 | // |
| 364 | // Members of anonymous blocks are a kind of TSymbol. They are not hidden in |
| 365 | // the symbol table behind a container; rather they are visible and point to |
| 366 | // their anonymous container. (The anonymous container is found through the |
| 367 | // member, not the other way around.) |
| 368 | // |
| 369 | class TAnonMember : public TSymbol { |
| 370 | public: |
| 371 | TAnonMember(const TString* n, unsigned int m, TVariable& a, int an) : TSymbol(n), anonContainer(a), memberNumber(m), anonId(an) { } |
| 372 | virtual TAnonMember* clone() const override; |
| 373 | virtual ~TAnonMember() { } |
| 374 | |
| 375 | virtual const TAnonMember* getAsAnonMember() const override { return this; } |
| 376 | virtual const TVariable& getAnonContainer() const { return anonContainer; } |
| 377 | virtual unsigned int getMemberNumber() const { return memberNumber; } |
| 378 | |
| 379 | virtual const TType& getType() const override |
| 380 | { |
| 381 | const TTypeList& types = *anonContainer.getType().getStruct(); |
| 382 | return *types[memberNumber].type; |
| 383 | } |
| 384 | |
| 385 | virtual TType& getWritableType() override |
| 386 | { |
| 387 | assert(writable); |
| 388 | const TTypeList& types = *anonContainer.getType().getStruct(); |
| 389 | return *types[memberNumber].type; |
| 390 | } |
| 391 | |
| 392 | virtual void setExtensions(int numExts, const char* const exts[]) override |
| 393 | { |
| 394 | anonContainer.setMemberExtensions(member: memberNumber, numExts, exts); |
| 395 | } |
| 396 | virtual int getNumExtensions() const override { return anonContainer.getNumMemberExtensions(member: memberNumber); } |
| 397 | virtual const char** getExtensions() const override { return anonContainer.getMemberExtensions(member: memberNumber); } |
| 398 | |
| 399 | virtual int getAnonId() const { return anonId; } |
| 400 | virtual void dump(TInfoSink& infoSink, bool complete = false) const override; |
| 401 | |
| 402 | protected: |
| 403 | explicit TAnonMember(const TAnonMember&); |
| 404 | TAnonMember& operator=(const TAnonMember&); |
| 405 | |
| 406 | TVariable& anonContainer; |
| 407 | unsigned int memberNumber; |
| 408 | int anonId; |
| 409 | }; |
| 410 | |
| 411 | class TSymbolTableLevel { |
| 412 | public: |
| 413 | POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator()) |
| 414 | TSymbolTableLevel() : defaultPrecision(nullptr), anonId(0), thisLevel(false) { } |
| 415 | ~TSymbolTableLevel(); |
| 416 | |
| 417 | bool insert(const TString& name, TSymbol* symbol) { |
| 418 | return level.insert(x: tLevelPair(name, symbol)).second; |
| 419 | } |
| 420 | |
| 421 | bool insert(TSymbol& symbol, bool separateNameSpaces, const TString& forcedKeyName = TString()) |
| 422 | { |
| 423 | // |
| 424 | // returning true means symbol was added to the table with no semantic errors |
| 425 | // |
| 426 | const TString& name = symbol.getName(); |
| 427 | if (forcedKeyName.length()) { |
| 428 | return level.insert(x: tLevelPair(forcedKeyName, &symbol)).second; |
| 429 | } |
| 430 | else if (name == "" ) { |
| 431 | symbol.getAsVariable()->setAnonId(anonId++); |
| 432 | // An empty name means an anonymous container, exposing its members to the external scope. |
| 433 | // Give it a name and insert its members in the symbol table, pointing to the container. |
| 434 | char buf[20]; |
| 435 | snprintf(s: buf, maxlen: 20, format: "%s%d" , AnonymousPrefix, symbol.getAsVariable()->getAnonId()); |
| 436 | symbol.changeName(newName: NewPoolTString(s: buf)); |
| 437 | |
| 438 | return insertAnonymousMembers(symbol, firstMember: 0); |
| 439 | } else { |
| 440 | // Check for redefinition errors: |
| 441 | // - STL itself will tell us if there is a direct name collision, with name mangling, at this level |
| 442 | // - additionally, check for function-redefining-variable name collisions |
| 443 | const TString& insertName = symbol.getMangledName(); |
| 444 | if (symbol.getAsFunction()) { |
| 445 | // make sure there isn't a variable of this name |
| 446 | if (! separateNameSpaces && level.find(x: name) != level.end()) |
| 447 | return false; |
| 448 | |
| 449 | // insert, and whatever happens is okay |
| 450 | level.insert(x: tLevelPair(insertName, &symbol)); |
| 451 | |
| 452 | return true; |
| 453 | } else |
| 454 | return level.insert(x: tLevelPair(insertName, &symbol)).second; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Add more members to an already inserted aggregate object |
| 459 | bool amend(TSymbol& symbol, int firstNewMember) |
| 460 | { |
| 461 | // See insert() for comments on basic explanation of insert. |
| 462 | // This operates similarly, but more simply. |
| 463 | // Only supporting amend of anonymous blocks so far. |
| 464 | if (IsAnonymous(name: symbol.getName())) |
| 465 | return insertAnonymousMembers(symbol, firstMember: firstNewMember); |
| 466 | else |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | bool insertAnonymousMembers(TSymbol& symbol, int firstMember) |
| 471 | { |
| 472 | const TTypeList& types = *symbol.getAsVariable()->getType().getStruct(); |
| 473 | for (unsigned int m = firstMember; m < types.size(); ++m) { |
| 474 | TAnonMember* member = new TAnonMember(&types[m].type->getFieldName(), m, *symbol.getAsVariable(), symbol.getAsVariable()->getAnonId()); |
| 475 | if (! level.insert(x: tLevelPair(member->getMangledName(), member)).second) |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | void retargetSymbol(const TString& from, const TString& to) { |
| 483 | tLevel::const_iterator fromIt = level.find(x: from); |
| 484 | tLevel::const_iterator toIt = level.find(x: to); |
| 485 | if (fromIt == level.end() || toIt == level.end()) |
| 486 | return; |
| 487 | delete fromIt->second; |
| 488 | level[from] = toIt->second; |
| 489 | retargetedSymbols.push_back(x: {from, to}); |
| 490 | } |
| 491 | |
| 492 | TSymbol* find(const TString& name) const |
| 493 | { |
| 494 | tLevel::const_iterator it = level.find(x: name); |
| 495 | if (it == level.end()) |
| 496 | return nullptr; |
| 497 | else |
| 498 | return (*it).second; |
| 499 | } |
| 500 | |
| 501 | void findFunctionNameList(const TString& name, TVector<const TFunction*>& list) |
| 502 | { |
| 503 | size_t parenAt = name.find_first_of(c: '('); |
| 504 | TString base(name, 0, parenAt + 1); |
| 505 | |
| 506 | tLevel::const_iterator begin = level.lower_bound(x: base); |
| 507 | base[parenAt] = ')'; // assume ')' is lexically after '(' |
| 508 | tLevel::const_iterator end = level.upper_bound(x: base); |
| 509 | for (tLevel::const_iterator it = begin; it != end; ++it) |
| 510 | list.push_back(x: it->second->getAsFunction()); |
| 511 | } |
| 512 | |
| 513 | // See if there is already a function in the table having the given non-function-style name. |
| 514 | bool hasFunctionName(const TString& name) const |
| 515 | { |
| 516 | tLevel::const_iterator candidate = level.lower_bound(x: name); |
| 517 | if (candidate != level.end()) { |
| 518 | const TString& candidateName = (*candidate).first; |
| 519 | TString::size_type parenAt = candidateName.find_first_of(c: '('); |
| 520 | if (parenAt != candidateName.npos && candidateName.compare(pos: 0, n: parenAt, str: name) == 0) |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | // See if there is a variable at this level having the given non-function-style name. |
| 529 | // Return true if name is found, and set variable to true if the name was a variable. |
| 530 | bool findFunctionVariableName(const TString& name, bool& variable) const |
| 531 | { |
| 532 | tLevel::const_iterator candidate = level.lower_bound(x: name); |
| 533 | if (candidate != level.end()) { |
| 534 | const TString& candidateName = (*candidate).first; |
| 535 | TString::size_type parenAt = candidateName.find_first_of(c: '('); |
| 536 | if (parenAt == candidateName.npos) { |
| 537 | // not a mangled name |
| 538 | if (candidateName == name) { |
| 539 | // found a variable name match |
| 540 | variable = true; |
| 541 | return true; |
| 542 | } |
| 543 | } else { |
| 544 | // a mangled name |
| 545 | if (candidateName.compare(pos: 0, n: parenAt, str: name) == 0) { |
| 546 | // found a function name match |
| 547 | variable = false; |
| 548 | return true; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | // Use this to do a lazy 'push' of precision defaults the first time |
| 557 | // a precision statement is seen in a new scope. Leave it at 0 for |
| 558 | // when no push was needed. Thus, it is not the current defaults, |
| 559 | // it is what to restore the defaults to when popping a level. |
| 560 | void setPreviousDefaultPrecisions(const TPrecisionQualifier *p) |
| 561 | { |
| 562 | // can call multiple times at one scope, will only latch on first call, |
| 563 | // as we're tracking the previous scope's values, not the current values |
| 564 | if (defaultPrecision != nullptr) |
| 565 | return; |
| 566 | |
| 567 | defaultPrecision = new TPrecisionQualifier[EbtNumTypes]; |
| 568 | for (int t = 0; t < EbtNumTypes; ++t) |
| 569 | defaultPrecision[t] = p[t]; |
| 570 | } |
| 571 | |
| 572 | void getPreviousDefaultPrecisions(TPrecisionQualifier *p) |
| 573 | { |
| 574 | // can be called for table level pops that didn't set the |
| 575 | // defaults |
| 576 | if (defaultPrecision == nullptr || p == nullptr) |
| 577 | return; |
| 578 | |
| 579 | for (int t = 0; t < EbtNumTypes; ++t) |
| 580 | p[t] = defaultPrecision[t]; |
| 581 | } |
| 582 | |
| 583 | void relateToOperator(const char* name, TOperator op); |
| 584 | void setFunctionExtensions(const char* name, int num, const char* const extensions[]); |
| 585 | void setSingleFunctionExtensions(const char* name, int num, const char* const extensions[]); |
| 586 | void dump(TInfoSink& infoSink, bool complete = false) const; |
| 587 | TSymbolTableLevel* clone() const; |
| 588 | void readOnly(); |
| 589 | |
| 590 | void setThisLevel() { thisLevel = true; } |
| 591 | bool isThisLevel() const { return thisLevel; } |
| 592 | |
| 593 | protected: |
| 594 | explicit TSymbolTableLevel(TSymbolTableLevel&); |
| 595 | TSymbolTableLevel& operator=(TSymbolTableLevel&); |
| 596 | |
| 597 | typedef std::map<TString, TSymbol*, std::less<TString>, pool_allocator<std::pair<const TString, TSymbol*> > > tLevel; |
| 598 | typedef const tLevel::value_type tLevelPair; |
| 599 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 600 | |
| 601 | tLevel level; // named mappings |
| 602 | TPrecisionQualifier *defaultPrecision; |
| 603 | // pair<FromName, ToName> |
| 604 | TVector<std::pair<TString, TString>> retargetedSymbols; |
| 605 | int anonId; |
| 606 | bool thisLevel; // True if this level of the symbol table is a structure scope containing member function |
| 607 | // that are supposed to see anonymous access to member variables. |
| 608 | }; |
| 609 | |
| 610 | class TSymbolTable { |
| 611 | public: |
| 612 | TSymbolTable() : uniqueId(0), noBuiltInRedeclarations(false), separateNameSpaces(false), adoptedLevels(0) |
| 613 | { |
| 614 | // |
| 615 | // This symbol table cannot be used until push() is called. |
| 616 | // |
| 617 | } |
| 618 | ~TSymbolTable() |
| 619 | { |
| 620 | // this can be called explicitly; safest to code it so it can be called multiple times |
| 621 | |
| 622 | // don't deallocate levels passed in from elsewhere |
| 623 | while (table.size() > adoptedLevels) |
| 624 | pop(p: nullptr); |
| 625 | } |
| 626 | |
| 627 | void adoptLevels(TSymbolTable& symTable) |
| 628 | { |
| 629 | for (unsigned int level = 0; level < symTable.table.size(); ++level) { |
| 630 | table.push_back(x: symTable.table[level]); |
| 631 | ++adoptedLevels; |
| 632 | } |
| 633 | uniqueId = symTable.uniqueId; |
| 634 | noBuiltInRedeclarations = symTable.noBuiltInRedeclarations; |
| 635 | separateNameSpaces = symTable.separateNameSpaces; |
| 636 | } |
| 637 | |
| 638 | // |
| 639 | // While level adopting is generic, the methods below enact a the following |
| 640 | // convention for levels: |
| 641 | // 0: common built-ins shared across all stages, all compiles, only one copy for all symbol tables |
| 642 | // 1: per-stage built-ins, shared across all compiles, but a different copy per stage |
| 643 | // 2: built-ins specific to a compile, like resources that are context-dependent, or redeclared built-ins |
| 644 | // 3: user-shader globals |
| 645 | // |
| 646 | protected: |
| 647 | static const uint32_t LevelFlagBitOffset = 56; |
| 648 | static const int globalLevel = 3; |
| 649 | static bool isSharedLevel(int level) { return level <= 1; } // exclude all per-compile levels |
| 650 | static bool isBuiltInLevel(int level) { return level <= 2; } // exclude user globals |
| 651 | static bool isGlobalLevel(int level) { return level <= globalLevel; } // include user globals |
| 652 | public: |
| 653 | bool isEmpty() { return table.size() == 0; } |
| 654 | bool atBuiltInLevel() { return isBuiltInLevel(level: currentLevel()); } |
| 655 | bool atGlobalLevel() { return isGlobalLevel(level: currentLevel()); } |
| 656 | static bool isBuiltInSymbol(long long uniqueId) { |
| 657 | int level = static_cast<int>(uniqueId >> LevelFlagBitOffset); |
| 658 | return isBuiltInLevel(level); |
| 659 | } |
| 660 | static constexpr uint64_t uniqueIdMask = (1LL << LevelFlagBitOffset) - 1; |
| 661 | static const uint32_t MaxLevelInUniqueID = 127; |
| 662 | void setNoBuiltInRedeclarations() { noBuiltInRedeclarations = true; } |
| 663 | void setSeparateNameSpaces() { separateNameSpaces = true; } |
| 664 | |
| 665 | void push() |
| 666 | { |
| 667 | table.push_back(x: new TSymbolTableLevel); |
| 668 | updateUniqueIdLevelFlag(); |
| 669 | } |
| 670 | |
| 671 | // Make a new symbol-table level to represent the scope introduced by a structure |
| 672 | // containing member functions, such that the member functions can find anonymous |
| 673 | // references to member variables. |
| 674 | // |
| 675 | // 'thisSymbol' should have a name of "" to trigger anonymous structure-member |
| 676 | // symbol finds. |
| 677 | void pushThis(TSymbol& thisSymbol) |
| 678 | { |
| 679 | assert(thisSymbol.getName().size() == 0); |
| 680 | table.push_back(x: new TSymbolTableLevel); |
| 681 | updateUniqueIdLevelFlag(); |
| 682 | table.back()->setThisLevel(); |
| 683 | insert(symbol&: thisSymbol); |
| 684 | } |
| 685 | |
| 686 | void pop(TPrecisionQualifier *p) |
| 687 | { |
| 688 | table[currentLevel()]->getPreviousDefaultPrecisions(p); |
| 689 | delete table.back(); |
| 690 | table.pop_back(); |
| 691 | updateUniqueIdLevelFlag(); |
| 692 | } |
| 693 | |
| 694 | // |
| 695 | // Insert a visible symbol into the symbol table so it can |
| 696 | // be found later by name. |
| 697 | // |
| 698 | // Returns false if the was a name collision. |
| 699 | // |
| 700 | bool insert(TSymbol& symbol) |
| 701 | { |
| 702 | symbol.setUniqueId(++uniqueId); |
| 703 | |
| 704 | // make sure there isn't a function of this variable name |
| 705 | if (! separateNameSpaces && ! symbol.getAsFunction() && table[currentLevel()]->hasFunctionName(name: symbol.getName())) |
| 706 | return false; |
| 707 | |
| 708 | // check for not overloading or redefining a built-in function |
| 709 | if (noBuiltInRedeclarations) { |
| 710 | if (atGlobalLevel() && currentLevel() > 0) { |
| 711 | if (table[0]->hasFunctionName(name: symbol.getName())) |
| 712 | return false; |
| 713 | if (currentLevel() > 1 && table[1]->hasFunctionName(name: symbol.getName())) |
| 714 | return false; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return table[currentLevel()]->insert(symbol, separateNameSpaces); |
| 719 | } |
| 720 | |
| 721 | // Add more members to an already inserted aggregate object |
| 722 | bool amend(TSymbol& symbol, int firstNewMember) |
| 723 | { |
| 724 | // See insert() for comments on basic explanation of insert. |
| 725 | // This operates similarly, but more simply. |
| 726 | return table[currentLevel()]->amend(symbol, firstNewMember); |
| 727 | } |
| 728 | |
| 729 | // Update the level info in symbol's unique ID to current level |
| 730 | void amendSymbolIdLevel(TSymbol& symbol) |
| 731 | { |
| 732 | // clamp level to avoid overflow |
| 733 | uint64_t level = (uint32_t)currentLevel() > MaxLevelInUniqueID ? MaxLevelInUniqueID : currentLevel(); |
| 734 | uint64_t symbolId = symbol.getUniqueId(); |
| 735 | symbolId &= uniqueIdMask; |
| 736 | symbolId |= (level << LevelFlagBitOffset); |
| 737 | symbol.setUniqueId(symbolId); |
| 738 | } |
| 739 | // |
| 740 | // To allocate an internal temporary, which will need to be uniquely |
| 741 | // identified by the consumer of the AST, but never need to |
| 742 | // found by doing a symbol table search by name, hence allowed an |
| 743 | // arbitrary name in the symbol with no worry of collision. |
| 744 | // |
| 745 | void makeInternalVariable(TSymbol& symbol) |
| 746 | { |
| 747 | symbol.setUniqueId(++uniqueId); |
| 748 | } |
| 749 | |
| 750 | // |
| 751 | // Copy a variable or anonymous member's structure from a shared level so that |
| 752 | // it can be added (soon after return) to the symbol table where it can be |
| 753 | // modified without impacting other users of the shared table. |
| 754 | // |
| 755 | TSymbol* copyUpDeferredInsert(TSymbol* shared) |
| 756 | { |
| 757 | if (shared->getAsVariable()) { |
| 758 | TSymbol* copy = shared->clone(); |
| 759 | copy->setUniqueId(shared->getUniqueId()); |
| 760 | return copy; |
| 761 | } else { |
| 762 | const TAnonMember* anon = shared->getAsAnonMember(); |
| 763 | assert(anon); |
| 764 | TVariable* container = anon->getAnonContainer().clone(); |
| 765 | container->changeName(newName: NewPoolTString(s: "" )); |
| 766 | container->setUniqueId(anon->getAnonContainer().getUniqueId()); |
| 767 | return container; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | TSymbol* copyUp(TSymbol* shared) |
| 772 | { |
| 773 | TSymbol* copy = copyUpDeferredInsert(shared); |
| 774 | table[globalLevel]->insert(symbol&: *copy, separateNameSpaces); |
| 775 | if (shared->getAsVariable()) |
| 776 | return copy; |
| 777 | else { |
| 778 | // return the copy of the anonymous member |
| 779 | return table[globalLevel]->find(name: shared->getName()); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // Normal find of a symbol, that can optionally say whether the symbol was found |
| 784 | // at a built-in level or the current top-scope level. |
| 785 | TSymbol* find(const TString& name, bool* builtIn = nullptr, bool* currentScope = nullptr, int* thisDepthP = nullptr) |
| 786 | { |
| 787 | int level = currentLevel(); |
| 788 | TSymbol* symbol; |
| 789 | int thisDepth = 0; |
| 790 | do { |
| 791 | if (table[level]->isThisLevel()) |
| 792 | ++thisDepth; |
| 793 | symbol = table[level]->find(name); |
| 794 | --level; |
| 795 | } while (symbol == nullptr && level >= 0); |
| 796 | level++; |
| 797 | if (builtIn) |
| 798 | *builtIn = isBuiltInLevel(level); |
| 799 | if (currentScope) |
| 800 | *currentScope = isGlobalLevel(level: currentLevel()) || level == currentLevel(); // consider shared levels as "current scope" WRT user globals |
| 801 | if (thisDepthP != nullptr) { |
| 802 | if (! table[level]->isThisLevel()) |
| 803 | thisDepth = 0; |
| 804 | *thisDepthP = thisDepth; |
| 805 | } |
| 806 | |
| 807 | return symbol; |
| 808 | } |
| 809 | |
| 810 | void retargetSymbol(const TString& from, const TString& to) { |
| 811 | int level = currentLevel(); |
| 812 | table[level]->retargetSymbol(from, to); |
| 813 | } |
| 814 | |
| 815 | |
| 816 | // Find of a symbol that returns how many layers deep of nested |
| 817 | // structures-with-member-functions ('this' scopes) deep the symbol was |
| 818 | // found in. |
| 819 | TSymbol* find(const TString& name, int& thisDepth) |
| 820 | { |
| 821 | int level = currentLevel(); |
| 822 | TSymbol* symbol; |
| 823 | thisDepth = 0; |
| 824 | do { |
| 825 | if (table[level]->isThisLevel()) |
| 826 | ++thisDepth; |
| 827 | symbol = table[level]->find(name); |
| 828 | --level; |
| 829 | } while (symbol == nullptr && level >= 0); |
| 830 | |
| 831 | if (! table[level + 1]->isThisLevel()) |
| 832 | thisDepth = 0; |
| 833 | |
| 834 | return symbol; |
| 835 | } |
| 836 | |
| 837 | bool isFunctionNameVariable(const TString& name) const |
| 838 | { |
| 839 | if (separateNameSpaces) |
| 840 | return false; |
| 841 | |
| 842 | int level = currentLevel(); |
| 843 | do { |
| 844 | bool variable; |
| 845 | bool found = table[level]->findFunctionVariableName(name, variable); |
| 846 | if (found) |
| 847 | return variable; |
| 848 | --level; |
| 849 | } while (level >= 0); |
| 850 | |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | void findFunctionNameList(const TString& name, TVector<const TFunction*>& list, bool& builtIn) |
| 855 | { |
| 856 | // For user levels, return the set found in the first scope with a match |
| 857 | builtIn = false; |
| 858 | int level = currentLevel(); |
| 859 | do { |
| 860 | table[level]->findFunctionNameList(name, list); |
| 861 | --level; |
| 862 | } while (list.empty() && level >= globalLevel); |
| 863 | |
| 864 | if (! list.empty()) |
| 865 | return; |
| 866 | |
| 867 | // Gather across all built-in levels; they don't hide each other |
| 868 | builtIn = true; |
| 869 | do { |
| 870 | table[level]->findFunctionNameList(name, list); |
| 871 | --level; |
| 872 | } while (level >= 0); |
| 873 | } |
| 874 | |
| 875 | void relateToOperator(const char* name, TOperator op) |
| 876 | { |
| 877 | for (unsigned int level = 0; level < table.size(); ++level) |
| 878 | table[level]->relateToOperator(name, op); |
| 879 | } |
| 880 | |
| 881 | void setFunctionExtensions(const char* name, int num, const char* const extensions[]) |
| 882 | { |
| 883 | for (unsigned int level = 0; level < table.size(); ++level) |
| 884 | table[level]->setFunctionExtensions(name, num, extensions); |
| 885 | } |
| 886 | |
| 887 | void setSingleFunctionExtensions(const char* name, int num, const char* const extensions[]) |
| 888 | { |
| 889 | for (unsigned int level = 0; level < table.size(); ++level) |
| 890 | table[level]->setSingleFunctionExtensions(name, num, extensions); |
| 891 | } |
| 892 | |
| 893 | void setVariableExtensions(const char* name, int numExts, const char* const extensions[]) |
| 894 | { |
| 895 | TSymbol* symbol = find(name: TString(name)); |
| 896 | if (symbol == nullptr) |
| 897 | return; |
| 898 | |
| 899 | symbol->setExtensions(numExts, exts: extensions); |
| 900 | } |
| 901 | |
| 902 | void setVariableExtensions(const char* blockName, const char* name, int numExts, const char* const extensions[]) |
| 903 | { |
| 904 | TSymbol* symbol = find(name: TString(blockName)); |
| 905 | if (symbol == nullptr) |
| 906 | return; |
| 907 | TVariable* variable = symbol->getAsVariable(); |
| 908 | assert(variable != nullptr); |
| 909 | |
| 910 | const TTypeList& structure = *variable->getAsVariable()->getType().getStruct(); |
| 911 | for (int member = 0; member < (int)structure.size(); ++member) { |
| 912 | if (structure[member].type->getFieldName().compare(s: name) == 0) { |
| 913 | variable->setMemberExtensions(member, numExts, exts: extensions); |
| 914 | return; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | long long getMaxSymbolId() { return uniqueId; } |
| 920 | void dump(TInfoSink& infoSink, bool complete = false) const; |
| 921 | void copyTable(const TSymbolTable& copyOf); |
| 922 | |
| 923 | void setPreviousDefaultPrecisions(TPrecisionQualifier *p) { table[currentLevel()]->setPreviousDefaultPrecisions(p); } |
| 924 | |
| 925 | void readOnly() |
| 926 | { |
| 927 | for (unsigned int level = 0; level < table.size(); ++level) |
| 928 | table[level]->readOnly(); |
| 929 | } |
| 930 | |
| 931 | // Add current level in the high-bits of unique id |
| 932 | void updateUniqueIdLevelFlag() { |
| 933 | // clamp level to avoid overflow |
| 934 | uint64_t level = (uint32_t)currentLevel() > MaxLevelInUniqueID ? MaxLevelInUniqueID : currentLevel(); |
| 935 | uniqueId &= uniqueIdMask; |
| 936 | uniqueId |= (level << LevelFlagBitOffset); |
| 937 | } |
| 938 | |
| 939 | void overwriteUniqueId(long long id) |
| 940 | { |
| 941 | uniqueId = id; |
| 942 | updateUniqueIdLevelFlag(); |
| 943 | } |
| 944 | |
| 945 | protected: |
| 946 | TSymbolTable(TSymbolTable&); |
| 947 | TSymbolTable& operator=(TSymbolTableLevel&); |
| 948 | |
| 949 | int currentLevel() const { return static_cast<int>(table.size()) - 1; } |
| 950 | std::vector<TSymbolTableLevel*> table; |
| 951 | long long uniqueId; // for unique identification in code generation |
| 952 | bool noBuiltInRedeclarations; |
| 953 | bool separateNameSpaces; |
| 954 | unsigned int adoptedLevels; |
| 955 | }; |
| 956 | |
| 957 | } // end namespace glslang |
| 958 | } // namespace QtShaderTools |
| 959 | |
| 960 | #endif // _SYMBOL_TABLE_INCLUDED_ |
| 961 | |