xref: /aosp_15_r20/external/angle/src/compiler/translator/Common.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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     POOL_ALLOCATOR_NEW_DELETE
87 
88     typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
TVector()89     TVector() : std::vector<T, pool_allocator<T>>() {}
TVector(const pool_allocator<T> & a)90     TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
TVector(size_type i)91     TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
TVector(size_type i,const T & value)92     TVector(size_type i, const T &value) : std::vector<T, pool_allocator<T>>(i, value) {}
93     template <typename InputIt>
TVector(InputIt first,InputIt last)94     TVector(InputIt first, InputIt last) : std::vector<T, pool_allocator<T>>(first, last)
95     {}
TVector(std::initializer_list<T> init)96     TVector(std::initializer_list<T> init) : std::vector<T, pool_allocator<T>>(init) {}
97 };
98 
99 template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>>
100 class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>>
101 {
102   public:
103     POOL_ALLOCATOR_NEW_DELETE
104     typedef pool_allocator<std::pair<const K, D>> tAllocator;
105 
TUnorderedMap()106     TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {}
107     // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedMap(const tAllocator & a)108     TUnorderedMap(const tAllocator &a)
109         : std::unordered_map<K, D, H, CMP, tAllocator>(
110               std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(),
111               a)
112     {}
113 };
114 
115 template <class K, class H = std::hash<K>, class CMP = std::equal_to<K>>
116 class TUnorderedSet : public std::unordered_set<K, H, CMP, pool_allocator<K>>
117 {
118   public:
119     POOL_ALLOCATOR_NEW_DELETE
120     typedef pool_allocator<K> tAllocator;
121 
TUnorderedSet()122     TUnorderedSet() : std::unordered_set<K, H, CMP, tAllocator>() {}
123     // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedSet(const tAllocator & a)124     TUnorderedSet(const tAllocator &a)
125         : std::unordered_set<K, H, CMP, tAllocator>(
126               std::unordered_set<K, H, CMP, tAllocator>::key_compare(),
127               a)
128     {}
129 };
130 
131 template <class K, class D, class CMP = std::less<K>>
132 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
133 {
134   public:
135     POOL_ALLOCATOR_NEW_DELETE
136     typedef pool_allocator<std::pair<const K, D>> tAllocator;
137 
TMap()138     TMap() : std::map<K, D, CMP, tAllocator>() {}
139     // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)140     TMap(const tAllocator &a)
141         : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
142     {}
143 };
144 
145 // Basic implementation of C++20's span for use with pool-allocated containers (TVector) or static
146 // arrays.  This is used by the array sizes member of TType to allow arrayed types to be
147 // constexpr-constructed.
148 // See the reference for std::span here: https://en.cppreference.com/w/cpp/container/span
149 template <typename T>
150 class TSpan
151 {
152   public:
153     typedef size_t size_type;
154 
TSpan()155     constexpr TSpan() {}
TSpan(T * ptr,size_type size)156     constexpr TSpan(T *ptr, size_type size) : mData(ptr), mSize(size) {}
157 
TSpan(const TSpan & that)158     constexpr TSpan(const TSpan &that) : mData(that.mData), mSize(that.mSize) {}
159     constexpr TSpan &operator=(const TSpan &that)
160     {
161         mData = that.mData;
162         mSize = that.mSize;
163         return *this;
164     }
165 
166     // Note: the pointer is taken out of the TVector because TVector's memory is pool allocated,
167     // so the memory will live on even if the TVector is destroyed.
168     template <typename S>
TSpan(const TVector<S> & vec)169     TSpan(const TVector<S> &vec) : mData(vec.data()), mSize(vec.size())
170     {}
171     template <typename S>
172     TSpan &operator=(const TVector<S> &vec)
173     {
174         mData = vec.data();
175         mSize = vec.size();
176         return *this;
177     }
178 
179     constexpr bool operator==(const TSpan &that) const
180     {
181         if (mSize != that.mSize)
182         {
183             return false;
184         }
185 
186         if (mData == that.mData)
187         {
188             return true;
189         }
190 
191         for (size_type index = 0; index < mSize; ++index)
192         {
193             if (mData[index] != that.mData[index])
194             {
195                 return false;
196             }
197         }
198 
199         return true;
200     }
201     constexpr bool operator!=(const TSpan &that) const { return !(*this == that); }
202 
data()203     constexpr T *data() const { return mData; }
size()204     constexpr size_type size() const { return mSize; }
empty()205     constexpr bool empty() const { return mSize == 0; }
206 
207     constexpr T &operator[](size_type index) const { return mData[index]; }
front()208     constexpr T &front() const { return mData[0]; }
back()209     constexpr T &back() const { return mData[mSize - 1]; }
210 
begin()211     constexpr T *begin() const { return mData; }
end()212     constexpr T *end() const { return mData + mSize; }
213 
rbegin()214     constexpr std::reverse_iterator<T *> rbegin() const
215     {
216         return std::make_reverse_iterator(end());
217     }
rend()218     constexpr std::reverse_iterator<T *> rend() const
219     {
220         return std::make_reverse_iterator(begin());
221     }
222 
first(size_type count)223     constexpr TSpan first(size_type count) const
224     {
225         ASSERT(count <= mSize);
226         return count == 0 ? TSpan() : TSpan(mData, count);
227     }
last(size_type count)228     constexpr TSpan last(size_type count) const
229     {
230         ASSERT(count <= mSize);
231         return count == 0 ? TSpan() : TSpan(mData + mSize - count, count);
232     }
subspan(size_type offset,size_type count)233     constexpr TSpan subspan(size_type offset, size_type count) const
234     {
235         ASSERT(offset + count <= mSize);
236         return count == 0 ? TSpan() : TSpan(mData + offset, count);
237     }
238 
239   private:
240     T *mData     = nullptr;
241     size_t mSize = 0;
242 };
243 
244 // Integer to TString conversion
245 template <typename T>
str(T i)246 inline TString str(T i)
247 {
248     ASSERT(std::numeric_limits<T>::is_integer);
249     char buffer[((8 * sizeof(T)) / 3) + 3];
250     const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
251     snprintf(buffer, sizeof(buffer), formatStr, i);
252     return buffer;
253 }
254 
255 // Allocate a char array in the global memory pool. str must be a null terminated string. strLength
256 // is the length without the null terminator.
AllocatePoolCharArray(const char * str,size_t strLength)257 inline const char *AllocatePoolCharArray(const char *str, size_t strLength)
258 {
259     size_t requiredSize = strLength + 1;
260     char *buffer        = static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));
261     memcpy(buffer, str, requiredSize);
262     ASSERT(buffer[strLength] == '\0');
263     return buffer;
264 }
265 
266 // Initialize a new stream which must be imbued with the classic locale
267 template <typename T>
InitializeStream()268 T InitializeStream()
269 {
270     T stream;
271     stream.imbue(std::locale::classic());
272     return stream;
273 }
274 
275 }  // namespace sh
276 
277 namespace std
278 {
279 template <>
280 struct hash<sh::TString>
281 {
282     size_t operator()(const sh::TString &s) const
283     {
284         auto v = std::string_view(s.data(), static_cast<int>(s.length()));
285         return std::hash<std::string_view>{}(v);
286     }
287 };
288 }  // namespace std
289 
290 #endif  // COMPILER_TRANSLATOR_COMMON_H_
291