1//
2// Copyright (c) 2018 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// ImmutableString.cpp: Wrapper for static or pool allocated char arrays, that are guaranteed to be
7// valid and unchanged for the duration of the compilation.
8//
9
10#include "compiler/translator/ImmutableString.h"
11
12std::ostream &operator<<(std::ostream &os, const sh::ImmutableString &str)
13{
14 return os.write(str.data(), str.length());
15}
16
17#if defined(_MSC_VER)
18# pragma warning(disable : 4309) // truncation of constant value
19#endif
20
21namespace sh
22{
23
24template <>
25const size_t ImmutableString::FowlerNollVoHash<4>::kFnvPrime = 16777619u;
26
27template <>
28const size_t ImmutableString::FowlerNollVoHash<4>::kFnvOffsetBasis = 0x811c9dc5u;
29
30template <>
31const size_t ImmutableString::FowlerNollVoHash<8>::kFnvPrime =
32 static_cast<size_t>(1099511628211ull);
33
34template <>
35const size_t ImmutableString::FowlerNollVoHash<8>::kFnvOffsetBasis =
36 static_cast<size_t>(0xcbf29ce484222325ull);
37
38uint32_t ImmutableString::mangledNameHash() const
39{
40 const char *dataPtr = data();
41 uint32_t hash = static_cast<uint32_t>(FowlerNollVoHash<4>::kFnvOffsetBasis);
42 const uint32_t kMaxSixBitValue = (1u << 6) - 1u;
43 uint32_t parenLocation = kMaxSixBitValue;
44 uint32_t hasArrayOrBlockParamBit = 0u;
45 uint32_t index = 0;
46 while (dataPtr[index] != '\0')
47 {
48 hash = hash ^ dataPtr[index];
49 hash = hash * static_cast<uint32_t>(FowlerNollVoHash<4>::kFnvPrime);
50 if (dataPtr[index] == '(')
51 {
52 // We should only reach here once, since this function should not be called with invalid
53 // mangled names.
54 ASSERT(parenLocation == kMaxSixBitValue);
55 parenLocation = index;
56 }
57 else if (dataPtr[index] == '{' || dataPtr[index] == '[')
58 {
59 hasArrayOrBlockParamBit = 1u;
60 }
61 ++index;
62 }
63 // Should not be called with strings longer than 63 characters.
64 ASSERT(index <= kMaxSixBitValue);
65 return ((hash >> 13) ^ (hash & 0x1fff)) | (index << 19) | (parenLocation << 25) |
66 (hasArrayOrBlockParamBit << 31);
67}
68
69} // namespace sh
70