• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
18 
19 #include "common/angleutils.h"
20 #include "common/debug.h"
21 #include "compiler/translator/PoolAlloc.h"
22 
23 namespace sh
24 {
25 
26 struct TSourceLoc
27 {
28     int first_file;
29     int first_line;
30     int last_file;
31     int last_line;
32 };
33 
34 constexpr TSourceLoc kNoSourceLoc{-1, -1, -1, -1};
35 
36 //
37 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
38 //
39 #define POOL_ALLOCATOR_NEW_DELETE                     \
40     void *operator new(size_t s)                      \
41     {                                                 \
42         return GetGlobalPoolAllocator()->allocate(s); \
43     }                                                 \
44     void *operator new(size_t, void *_Where)          \
45     {                                                 \
46         return (_Where);                              \
47     }                                                 \
48     void operator delete(void *)                      \
49     {}                                                \
50     void operator delete(void *, void *)              \
51     {}                                                \
52     void *operator new[](size_t s)                    \
53     {                                                 \
54         return GetGlobalPoolAllocator()->allocate(s); \
55     }                                                 \
56     void *operator new[](size_t, void *_Where)        \
57     {                                                 \
58         return (_Where);                              \
59     }                                                 \
60     void operator delete[](void *)                    \
61     {}                                                \
62     void operator delete[](void *, void *)            \
63     {}
64 
65 //
66 // Pool version of string.
67 //
68 typedef pool_allocator<char> TStringAllocator;
69 typedef std::basic_string<char, std::char_traits<char>, TStringAllocator> TString;
70 typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
71 
72 //
73 // Persistent memory.  Should only be used for strings that survive across compiles.
74 //
75 using TPersistString       = std::string;
76 using TPersistStringStream = std::ostringstream;
77 
78 //
79 // Pool allocator versions of vectors, lists, and maps
80 //
81 template <class T>
82 class TVector : public std::vector<T, pool_allocator<T>>
83 {
84   public:
85     POOL_ALLOCATOR_NEW_DELETE
86 
87     typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
TVector()88     TVector() : std::vector<T, pool_allocator<T>>() {}
TVector(const pool_allocator<T> & a)89     TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
TVector(size_type i)90     TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
TVector(size_type i,const T & value)91     TVector(size_type i, const T &value) : std::vector<T, pool_allocator<T>>(i, value) {}
92     template <typename InputIt>
TVector(InputIt first,InputIt last)93     TVector(InputIt first, InputIt last) : std::vector<T, pool_allocator<T>>(first, last)
94     {}
TVector(std::initializer_list<T> init)95     TVector(std::initializer_list<T> init) : std::vector<T, pool_allocator<T>>(init) {}
96 };
97 
98 template <class K, class D, class H = std::hash<K>, class CMP = std::equal_to<K>>
99 class TUnorderedMap : public std::unordered_map<K, D, H, CMP, pool_allocator<std::pair<const K, D>>>
100 {
101   public:
102     POOL_ALLOCATOR_NEW_DELETE
103     typedef pool_allocator<std::pair<const K, D>> tAllocator;
104 
TUnorderedMap()105     TUnorderedMap() : std::unordered_map<K, D, H, CMP, tAllocator>() {}
106     // use correct two-stage name lookup supported in gcc 3.4 and above
TUnorderedMap(const tAllocator & a)107     TUnorderedMap(const tAllocator &a)
108         : std::unordered_map<K, D, H, CMP, tAllocator>(
109               std::unordered_map<K, D, H, CMP, tAllocator>::key_compare(),
110               a)
111     {}
112 };
113 
114 template <class K, class D, class CMP = std::less<K>>
115 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
116 {
117   public:
118     POOL_ALLOCATOR_NEW_DELETE
119     typedef pool_allocator<std::pair<const K, D>> tAllocator;
120 
TMap()121     TMap() : std::map<K, D, CMP, tAllocator>() {}
122     // use correct two-stage name lookup supported in gcc 3.4 and above
TMap(const tAllocator & a)123     TMap(const tAllocator &a)
124         : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
125     {}
126 };
127 
128 // Basic implementation of C++20's span for use with pool-allocated containers (TVector) or static
129 // arrays.  This is used by the array sizes member of TType to allow arrayed types to be
130 // constexpr-constructed.
131 // See the reference for std::span here: https://en.cppreference.com/w/cpp/container/span
132 template <typename T>
133 class TSpan
134 {
135   public:
136     typedef size_t size_type;
137 
TSpan()138     constexpr TSpan() {}
TSpan(T * ptr,size_type size)139     constexpr TSpan(T *ptr, size_type size) : mData(ptr), mSize(size) {}
140 
TSpan(const TSpan & that)141     constexpr TSpan(const TSpan &that) : mData(that.mData), mSize(that.mSize) {}
142     constexpr TSpan &operator=(const TSpan &that)
143     {
144         mData = that.mData;
145         mSize = that.mSize;
146         return *this;
147     }
148 
149     // Note: the pointer is taken out of the TVector because TVector's memory is pool allocated,
150     // so the memory will live on even if the TVector is destroyed.
151     template <typename S>
TSpan(const TVector<S> & vec)152     TSpan(const TVector<S> &vec) : mData(vec.data()), mSize(vec.size())
153     {}
154     template <typename S>
155     TSpan &operator=(const TVector<S> &vec)
156     {
157         mData = vec.data();
158         mSize = vec.size();
159         return *this;
160     }
161 
162     constexpr bool operator==(const TSpan &that) const
163     {
164         if (mSize != that.mSize)
165         {
166             return false;
167         }
168 
169         if (mData == that.mData)
170         {
171             return true;
172         }
173 
174         for (size_type index = 0; index < mSize; ++index)
175         {
176             if (mData[index] != that.mData[index])
177             {
178                 return false;
179             }
180         }
181 
182         return true;
183     }
184     constexpr bool operator!=(const TSpan &that) const { return !(*this == that); }
185 
data()186     constexpr T *data() const { return mData; }
size()187     constexpr size_type size() const { return mSize; }
empty()188     constexpr bool empty() const { return mSize == 0; }
189 
190     constexpr T &operator[](size_type index) const { return mData[index]; }
front()191     constexpr T &front() const { return mData[0]; }
back()192     constexpr T &back() const { return mData[mSize - 1]; }
193 
begin()194     constexpr T *begin() const { return mData; }
end()195     constexpr T *end() const { return mData + mSize; }
196 
rbegin()197     constexpr std::reverse_iterator<T *> rbegin() const
198     {
199         return std::make_reverse_iterator(end());
200     }
rend()201     constexpr std::reverse_iterator<T *> rend() const
202     {
203         return std::make_reverse_iterator(begin());
204     }
205 
first(size_type count)206     constexpr TSpan first(size_type count) const
207     {
208         ASSERT(count <= mSize);
209         return count == 0 ? TSpan() : TSpan(mData, count);
210     }
last(size_type count)211     constexpr TSpan last(size_type count) const
212     {
213         ASSERT(count <= mSize);
214         return count == 0 ? TSpan() : TSpan(mData + mSize - count, count);
215     }
subspan(size_type offset,size_type count)216     constexpr TSpan subspan(size_type offset, size_type count) const
217     {
218         ASSERT(offset + count <= mSize);
219         return count == 0 ? TSpan() : TSpan(mData + offset, count);
220     }
221 
222   private:
223     T *mData     = nullptr;
224     size_t mSize = 0;
225 };
226 
227 // Integer to TString conversion
228 template <typename T>
str(T i)229 inline TString str(T i)
230 {
231     ASSERT(std::numeric_limits<T>::is_integer);
232     char buffer[((8 * sizeof(T)) / 3) + 3];
233     const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
234     snprintf(buffer, sizeof(buffer), formatStr, i);
235     return buffer;
236 }
237 
238 // Allocate a char array in the global memory pool. str must be a null terminated string. strLength
239 // is the length without the null terminator.
AllocatePoolCharArray(const char * str,size_t strLength)240 inline const char *AllocatePoolCharArray(const char *str, size_t strLength)
241 {
242     size_t requiredSize = strLength + 1;
243     char *buffer        = static_cast<char *>(GetGlobalPoolAllocator()->allocate(requiredSize));
244     memcpy(buffer, str, requiredSize);
245     ASSERT(buffer[strLength] == '\0');
246     return buffer;
247 }
248 
249 // Initialize a new stream which must be imbued with the classic locale
250 template <typename T>
InitializeStream()251 T InitializeStream()
252 {
253     T stream;
254     stream.imbue(std::locale::classic());
255     return stream;
256 }
257 
258 }  // namespace sh
259 
260 namespace std
261 {
262 template <>
263 struct hash<sh::TString>
264 {
265     size_t operator()(const sh::TString &s) const
266     {
267         auto v = std::string_view(s.data(), static_cast<int>(s.length()));
268         return std::hash<std::string_view>{}(v);
269     }
270 };
271 }  // namespace std
272 
273 #endif  // COMPILER_TRANSLATOR_COMMON_H_
274