1//
2// Copyright (c) 2017 The ANGLE Project Authors. All rights reserved.
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#include "compiler/translator/HashNames.h"
8
9#include "compiler/translator/ImmutableString.h"
10#include "compiler/translator/ImmutableStringBuilder.h"
11#include "compiler/translator/IntermNode.h"
12#include "compiler/translator/Symbol.h"
13
14namespace sh
15{
16
17namespace
18{
19constexpr const ImmutableString kHashedNamePrefix("webgl_");
20
21// Can't prefix with just _ because then we might introduce a double underscore, which is not safe
22// in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for
23// use by the underlying implementation). u is short for user-defined.
24constexpr const ImmutableString kUnhashedNamePrefix("_u");
25
26ImmutableString HashName(const ImmutableString &name, ShHashFunction64 hashFunction)
27{
28 ASSERT(!name.empty());
29 ASSERT(hashFunction);
30 khronos_uint64_t number = (*hashFunction)(name.data(), name.length());
31
32 // Build the hashed name in place.
33 static const unsigned int kHexStrMaxLength = sizeof(number) * 2;
34 static const size_t kHashedNameMaxLength = kHashedNamePrefix.length() + kHexStrMaxLength;
35
36 ImmutableStringBuilder hashedName(kHashedNameMaxLength);
37 hashedName << kHashedNamePrefix;
38
39 hashedName.appendHex(number);
40
41 return hashedName;
42}
43
44} // anonymous namespace
45
46ImmutableString HashName(const ImmutableString &name,
47 ShHashFunction64 hashFunction,
48 NameMap *nameMap)
49{
50 if (hashFunction == nullptr)
51 {
52 if (name.length() + kUnhashedNamePrefix.length() > kESSLMaxIdentifierLength)
53 {
54 // If the identifier length is already close to the limit, we can't prefix it. This is
55 // not a problem since there are no builtins or ANGLE's internal variables that would
56 // have as long names and could conflict.
57 return name;
58 }
59 ImmutableStringBuilder prefixedName(kUnhashedNamePrefix.length() + name.length());
60 prefixedName << kUnhashedNamePrefix << name;
61 return prefixedName;
62 }
63 if (nameMap)
64 {
65 NameMap::const_iterator it = nameMap->find(name.data());
66 if (it != nameMap->end())
67 {
68 // TODO(oetuaho): Would be nice if we didn't need to allocate a string here.
69 return ImmutableString(it->second);
70 }
71 }
72 ImmutableString hashedName = HashName(name, hashFunction);
73 if (nameMap)
74 {
75 (*nameMap)[name.data()] = hashedName.data();
76 }
77 return hashedName;
78}
79
80ImmutableString HashName(const TSymbol *symbol, ShHashFunction64 hashFunction, NameMap *nameMap)
81{
82 if (symbol->symbolType() == SymbolType::Empty)
83 {
84 return kEmptyImmutableString;
85 }
86 if (symbol->symbolType() == SymbolType::AngleInternal ||
87 symbol->symbolType() == SymbolType::BuiltIn)
88 {
89 return symbol->name();
90 }
91 return HashName(symbol->name(), hashFunction, nameMap);
92}
93
94} // namespace sh
95