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