1 //
2 // Copyright 2002 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 #ifndef COMPILER_TRANSLATOR_COMMON_H_
8 #define COMPILER_TRANSLATOR_COMMON_H_
9
10 #include <stdio.h>
11 #include <limits>
12 #include <map>
13 #include <sstream>
14 #include <string>
15 #include <string_view>
16 #include <unordered_map>
17 #include <unordered_set>
18 #include <vector>
19
20 #include "common/angleutils.h"
21 #include "common/debug.h"
22 #include "compiler/translator/PoolAlloc.h"
23
24 namespace sh
25 {
26
27 struct TSourceLoc
28 {
29 int first_file;
30 int first_line;
31 int last_file;
32 int last_line;
33 };
34
35 constexpr TSourceLoc kNoSourceLoc{-1, -1, -1, -1};
36
37 //
38 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
39 //
40 #define POOL_ALLOCATOR_NEW_DELETE \
41 void *operator new(size_t s) \
42 { \
43 return GetGlobalPoolAllocator()->allocate(s); \
44 } \
45 void *operator new(size_t, void *_Where) \
46 { \
47 return (_Where); \
48 } \
49 void operator delete(void *) \
50 {} \
51 void operator delete(void *, void *) \
52 {} \
53 void *operator new[](size_t s) \
54 { \
55 return GetGlobalPoolAllocator()->allocate(s); \
56 } \
57 void *operator new[](size_t, void *_Where) \
58 { \
59 return (_Where); \
60 } \
61 void operator delete[](void *) \
62 {} \
63 void operator delete[](void *, void *) \
64 {}
65
66 //
67 // Pool version of string.
68 //
69 typedef pool_allocator<char> TStringAllocator;
70 typedef std::basic_string<char, std::char_traits<char>, TStringAllocator> TString;
71 typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
72
73 //
74 // Persistent memory. Should only be used for strings that survive across compiles.
75 //
76 using TPersistString = std::string;
77 using TPersistStringStream = std::ostringstream;
78
79 //
80 // Pool allocator versions of vectors, lists, and maps
81 //
82 template <class T>
83 class TVector : public std::vector<T, pool_allocator<T>>
84 {
85 public:
86 static constexpr bool is_pool_allocated = true;
87 POOL_ALLOCATOR_NEW_DELETE
88
89 typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
TVector()90 TVector() : std::vector<T, pool_allocator<T>>() {}
TVector(const pool_allocator<T> & a)91 TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
TVector(size_type i)92 TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
TVector(size_type i,const T & value)93 TVector(size_type i, const T &value) : std::vector<T, pool_allocator<T>>(i, value) {}
94 template <typename InputIt>
TVector(InputIt first,InputIt last)95 TVector(InputIt first, InputIt last) : std::vector<T, pool_allocator<T>>(first, last)
96 {}
TVector(std::initializer_list<T> init)97 TVector(std::initializer_list<T> init) : std::vector<T, pool_allocator<T>>(init) {}
98 };
99
100 template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>>
101 class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>>
102 {
103 public:
104 POOL_ALLOCATOR_NEW_DELETE
105 typedef pool_allocator<std::pair<const K, D>> tAllocator;
106
TUnorderedMap()107 TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {}
108 // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedMap(const tAllocator & a)109 TUnorderedMap(const tAllocator &a)
110 : std::unordered_map<K, D, H, CMP, tAllocator>(
111 std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(),
112 a)
113 {}
114 };
115
116 template <class K, class H = std::hash<K>, class CMP = std::equal_to<K>>
117 class TUnorderedSet : public std::unordered_set<K, H, CMP, pool_allocator<K>>
118 {
119 public:
120 POOL_ALLOCATOR_NEW_DELETE
121 typedef pool_allocator<K> tAllocator;
122
TUnorderedSet()123 TUnorderedSet() : std::unordered_set<K, H, CMP, tAllocator>() {}
124 // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedSet(const tAllocator & a)125 TUnorderedSet(const tAllocator &a)
126 : std::unordered_set<K, H, CMP, tAllocator>(
127 std::unordered_set<K, H, CMP, tAllocator>::key_compare(),
128 a)
129 {}
130 };
131
132 template <class K, class D, class CMP = std::less<K>>
133 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
134 {
135 public:
136 POOL_ALLOCATOR_NEW_DELETE
137 typedef pool_allocator<std::pair<const K, D>> tAllocator;
138
TMap()139 TMap() : std::map<K, D, CMP, tAllocator>() {}
140 // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)141 TMap(const tAllocator &a)
142 : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
143 {}
144 };
145
146 template <class K, class CMP = std::less<K>>
147 class TSet : public std::set<K, CMP, pool_allocator<K>>
148 {
149 public:
150 POOL_ALLOCATOR_NEW_DELETE
151 typedef pool_allocator<K> tAllocator;
152
TSet()153 TSet() : std::set<K, CMP, tAllocator>() {}
154 // use correct two-stage name lookup supported in gcc 3.4 and above
TSet(const tAllocator & a)155 TSet(const tAllocator &a)
156 : std::set<K, CMP, tAllocator>(std::map<K, CMP, tAllocator>::key_compare(), a)
157 {}
158 };
159
160 // Integer to TString conversion
161 template <typename T>
str(T i)162 inline TString str(T i)
163 {
164 ASSERT(std::numeric_limits<T>::is_integer);
165 char buffer[((8 * sizeof(T)) / 3) + 3];
166 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
167 snprintf(buffer, sizeof(buffer), formatStr, i);
168 return buffer;
169 }
170
171 // Allocate a char array in the global memory pool. str must be a null terminated string. strLength
172 // is the length without the null terminator.
AllocatePoolCharArray(const char * str,size_t strLength)173 inline const char *AllocatePoolCharArray(const char *str, size_t strLength)
174 {
175 size_t requiredSize = strLength + 1;
176 char *buffer = static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));
177 memcpy(buffer, str, requiredSize);
178 ASSERT(buffer[strLength] == '\0');
179 return buffer;
180 }
181
182 // Initialize a new stream which must be imbued with the classic locale
183 template <typename T>
InitializeStream()184 T InitializeStream()
185 {
186 T stream;
187 stream.imbue(std::locale::classic());
188 return stream;
189 }
190
191 } // namespace sh
192
193 namespace std
194 {
195 template <>
196 struct hash<sh::TString>
197 {
198 size_t operator()(const sh::TString &s) const
199 {
200 auto v = std::string_view(s.data(), static_cast<int>(s.length()));
201 return std::hash<std::string_view>{}(v);
202 }
203 };
204 } // namespace std
205
206 #endif // COMPILER_TRANSLATOR_COMMON_H_
207