Merge pull request #1288 from amdrexu/bugfix
[platform/upstream/glslang.git] / glslang / Include / Common.h
1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2012-2013 LunarG, Inc.
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
35 //
36
37 #ifndef _COMMON_INCLUDED_
38 #define _COMMON_INCLUDED_
39
40
41 #if defined(__ANDROID__) || _MSC_VER < 1700
42 #include <sstream>
43 namespace std {
44 template<typename T>
45 std::string to_string(const T& val) {
46   std::ostringstream os;
47   os << val;
48   return os.str();
49 }
50 }
51 #endif
52
53 #if (defined(_MSC_VER) && _MSC_VER < 1900 /*vs2015*/) || defined MINGW_HAS_SECURE_API
54     #include <basetsd.h>
55     #ifndef snprintf
56     #define snprintf sprintf_s
57     #endif
58     #define safe_vsprintf(buf,max,format,args) vsnprintf_s((buf), (max), (max), (format), (args))
59 #elif defined (solaris)
60     #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
61     #include <sys/int_types.h>
62     #define UINT_PTR uintptr_t
63 #else
64     #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
65     #include <stdint.h>
66     #define UINT_PTR uintptr_t
67 #endif
68
69 #if defined(_MSC_VER) && _MSC_VER < 1800
70 inline long long int strtoll (const char* str, char** endptr, int base)
71 {
72   return _strtoi64(str, endptr, base);
73 }
74 inline unsigned long long int strtoull (const char* str, char** endptr, int base)
75 {
76   return _strtoui64(str, endptr, base);
77 }
78 inline long long int atoll (const char* str)
79 {
80   return strtoll(str, NULL, 10);
81 }
82 #endif
83
84 #if defined(_MSC_VER)
85 #define strdup _strdup
86 #endif
87
88 /* windows only pragma */
89 #ifdef _MSC_VER
90     #pragma warning(disable : 4786) // Don't warn about too long identifiers
91     #pragma warning(disable : 4514) // unused inline method
92     #pragma warning(disable : 4201) // nameless union
93 #endif
94
95 #include <set>
96 #include <unordered_set>
97 #include <vector>
98 #include <map>
99 #include <unordered_map>
100 #include <list>
101 #include <algorithm>
102 #include <string>
103 #include <cstdio>
104 #include <cassert>
105
106 #include "PoolAlloc.h"
107
108 //
109 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
110 //
111 #define POOL_ALLOCATOR_NEW_DELETE(A)                                  \
112     void* operator new(size_t s) { return (A).allocate(s); }          \
113     void* operator new(size_t, void *_Where) { return (_Where); }     \
114     void operator delete(void*) { }                                   \
115     void operator delete(void *, void *) { }                          \
116     void* operator new[](size_t s) { return (A).allocate(s); }        \
117     void* operator new[](size_t, void *_Where) { return (_Where); }   \
118     void operator delete[](void*) { }                                 \
119     void operator delete[](void *, void *) { }
120
121 namespace glslang {
122
123     //
124     // Pool version of string.
125     //
126     typedef pool_allocator<char> TStringAllocator;
127     typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
128
129 } // end namespace glslang
130
131 // Repackage the std::hash for use by unordered map/set with a TString key.
132 namespace std {
133
134     template<> struct hash<glslang::TString> {
135         std::size_t operator()(const glslang::TString& s) const
136         {
137             const unsigned _FNV_offset_basis = 2166136261U;
138             const unsigned _FNV_prime = 16777619U;
139             unsigned _Val = _FNV_offset_basis;
140             size_t _Count = s.size();
141             const char* _First = s.c_str();
142             for (size_t _Next = 0; _Next < _Count; ++_Next)
143             {
144                 _Val ^= (unsigned)_First[_Next];
145                 _Val *= _FNV_prime;
146             }
147
148             return _Val;
149         }
150     };
151 }
152
153 namespace glslang {
154
155 inline TString* NewPoolTString(const char* s)
156 {
157     void* memory = GetThreadPoolAllocator().allocate(sizeof(TString));
158     return new(memory) TString(s);
159 }
160
161 template<class T> inline T* NewPoolObject(T*)
162 {
163     return new(GetThreadPoolAllocator().allocate(sizeof(T))) T;
164 }
165
166 template<class T> inline T* NewPoolObject(T, int instances)
167 {
168     return new(GetThreadPoolAllocator().allocate(instances * sizeof(T))) T[instances];
169 }
170
171 //
172 // Pool allocator versions of vectors, lists, and maps
173 //
174 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
175 public:
176     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
177
178     typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
179     TVector() : std::vector<T, pool_allocator<T> >() {}
180     TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
181     TVector(size_type i) : std::vector<T, pool_allocator<T> >(i) {}
182     TVector(size_type i, const T& val) : std::vector<T, pool_allocator<T> >(i, val) {}
183 };
184
185 template <class T> class TList  : public std::list<T, pool_allocator<T> > {
186 };
187
188 template <class K, class D, class CMP = std::less<K> >
189 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<K const, D> > > {
190 };
191
192 template <class K, class D, class HASH = std::hash<K>, class PRED = std::equal_to<K> >
193 class TUnorderedMap : public std::unordered_map<K, D, HASH, PRED, pool_allocator<std::pair<K const, D> > > {
194 };
195
196 //
197 // Persistent string memory.  Should only be used for strings that survive
198 // across compiles/links.
199 //
200 typedef std::basic_string<char> TPersistString;
201
202 //
203 // templatized min and max functions.
204 //
205 template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
206 template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
207
208 //
209 // Create a TString object from an integer.
210 //
211 #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
212 inline const TString String(const int i, const int base = 10)
213 {
214     char text[16];     // 32 bit ints are at most 10 digits in base 10
215     _itoa_s(i, text, sizeof(text), base);
216     return text;
217 }
218 #else
219 inline const TString String(const int i, const int /*base*/ = 10)
220 {
221     char text[16];     // 32 bit ints are at most 10 digits in base 10
222
223     // we assume base 10 for all cases
224     snprintf(text, sizeof(text), "%d", i);
225
226     return text;
227 }
228 #endif
229
230 struct TSourceLoc {
231     void init() { name = nullptr; string = 0; line = 0; column = 0; }
232     void init(int stringNum) { init(); string = stringNum; }
233     // Returns the name if it exists. Otherwise, returns the string number.
234     std::string getStringNameOrNum(bool quoteStringName = true) const
235     {
236         if (name != nullptr)
237             return quoteStringName ? ("\"" + std::string(name) + "\"") : name;
238         return std::to_string((long long)string);
239     }
240     const char* name; // descriptive name for this string
241     int string;
242     int line;
243     int column;
244 };
245
246 class TPragmaTable : public TMap<TString, TString> {
247 public:
248     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
249 };
250
251 const int MaxTokenLength = 1024;
252
253 template <class T> bool IsPow2(T powerOf2)
254 {
255     if (powerOf2 <= 0)
256         return false;
257
258     return (powerOf2 & (powerOf2 - 1)) == 0;
259 }
260
261 // Round number up to a multiple of the given powerOf2, which is not
262 // a power, just a number that must be a power of 2.
263 template <class T> void RoundToPow2(T& number, int powerOf2)
264 {
265     assert(IsPow2(powerOf2));
266     number = (number + powerOf2 - 1) & ~(powerOf2 - 1);
267 }
268
269 template <class T> bool IsMultipleOfPow2(T number, int powerOf2)
270 {
271     assert(IsPow2(powerOf2));
272     return ! (number & (powerOf2 - 1));
273 }
274
275 } // end namespace glslang
276
277 #endif // _COMMON_INCLUDED_