Path: blob/main_old/src/compiler/translator/HashNames.cpp
1693 views
//1// Copyright 2017 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#include "compiler/translator/HashNames.h"78#include "compiler/translator/ImmutableString.h"9#include "compiler/translator/ImmutableStringBuilder.h"10#include "compiler/translator/IntermNode.h"11#include "compiler/translator/Symbol.h"1213namespace sh14{1516namespace17{18constexpr const ImmutableString kHashedNamePrefix("webgl_");1920ImmutableString HashName(const ImmutableString &name, ShHashFunction64 hashFunction)21{22ASSERT(!name.empty());23ASSERT(hashFunction);24khronos_uint64_t number = (*hashFunction)(name.data(), name.length());2526// Build the hashed name in place.27static const unsigned int kHexStrMaxLength = sizeof(number) * 2;28static const size_t kHashedNameMaxLength = kHashedNamePrefix.length() + kHexStrMaxLength;2930ImmutableStringBuilder hashedName(kHashedNameMaxLength);31hashedName << kHashedNamePrefix;3233hashedName.appendHex(number);3435return hashedName;36}3738void AddToNameMapIfNotMapped(const ImmutableString &name,39const ImmutableString &hashedName,40NameMap *nameMap)41{42if (nameMap)43{44NameMap::const_iterator it = nameMap->find(name.data());45if (it != nameMap->end())46{47// (How bout returning?)48return;49}50(*nameMap)[name.data()] = hashedName.data();51}52}5354} // anonymous namespace5556ImmutableString HashName(const ImmutableString &name,57ShHashFunction64 hashFunction,58NameMap *nameMap)59{60const ImmutableString kUnhashedNamePrefix(kUserDefinedNamePrefix);6162if (hashFunction == nullptr)63{64if (name.length() + kUnhashedNamePrefix.length() > kESSLMaxIdentifierLength)65{66// If the identifier length is already close to the limit, we can't prefix it. This is67// not a problem since there are no builtins or ANGLE's internal variables that would68// have as long names and could conflict.69return name;70}71ImmutableStringBuilder prefixedName(kUnhashedNamePrefix.length() + name.length());72prefixedName << kUnhashedNamePrefix << name;73ImmutableString res = prefixedName;74AddToNameMapIfNotMapped(name, res, nameMap);75return res;76}7778// Has a hash function79ImmutableString hashedName = HashName(name, hashFunction);80AddToNameMapIfNotMapped(name, hashedName, nameMap);81return hashedName;82}8384ImmutableString HashName(const TSymbol *symbol, ShHashFunction64 hashFunction, NameMap *nameMap)85{86if (symbol->symbolType() == SymbolType::Empty)87{88return kEmptyImmutableString;89}90if (symbol->symbolType() == SymbolType::AngleInternal ||91symbol->symbolType() == SymbolType::BuiltIn)92{93return symbol->name();94}95return HashName(symbol->name(), hashFunction, nameMap);96}9798} // namespace sh99100101