Merge pull request #1050 from amdrexu/feature
[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 #if (defined(_MSC_VER) && _MSC_VER < 1900 /*vs2015*/) || defined MINGW_HAS_SECURE_API
41     #include <basetsd.h>
42     #define snprintf sprintf_s
43     #define safe_vsprintf(buf,max,format,args) vsnprintf_s((buf), (max), (max), (format), (args))
44 #elif defined (solaris)
45     #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
46     #include <sys/int_types.h>
47     #define UINT_PTR uintptr_t
48 #else
49     #define safe_vsprintf(buf,max,format,args) vsnprintf((buf), (max), (format), (args))
50     #include <stdint.h>
51     #define UINT_PTR uintptr_t
52 #endif
53
54 #if defined(__ANDROID__) || _MSC_VER < 1700
55 #include <sstream>
56 namespace std {
57 template<typename T>
58 std::string to_string(const T& val) {
59   std::ostringstream os;
60   os << val;
61   return os.str();
62 }
63 }
64 #endif
65
66 #if defined(_MSC_VER) && _MSC_VER < 1800
67 inline long long int strtoll (const char* str, char** endptr, int base)
68 {
69   return _strtoi64(str, endptr, base);
70 }
71 inline unsigned long long int strtoull (const char* str, char** endptr, int base)
72 {
73   return _strtoui64(str, endptr, base);
74 }
75 inline long long int atoll (const char* str)
76 {
77   return strtoll(str, NULL, 10);
78 }
79 #endif
80
81 #if defined(_MSC_VER)
82 #define strdup _strdup
83 #endif
84
85 /* windows only pragma */
86 #ifdef _MSC_VER
87     #pragma warning(disable : 4786) // Don't warn about too long identifiers
88     #pragma warning(disable : 4514) // unused inline method
89     #pragma warning(disable : 4201) // nameless union
90 #endif
91
92 #include <set>
93 #include <unordered_set>
94 #include <vector>
95 #include <map>
96 #include <unordered_map>
97 #include <list>
98 #include <algorithm>
99 #include <string>
100 #include <cstdio>
101 #include <cassert>
102
103 #include "PoolAlloc.h"
104
105 //
106 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
107 //
108 #define POOL_ALLOCATOR_NEW_DELETE(A)                                  \
109     void* operator new(size_t s) { return (A).allocate(s); }          \
110     void* operator new(size_t, void *_Where) { return (_Where); }     \
111     void operator delete(void*) { }                                   \
112     void operator delete(void *, void *) { }                          \
113     void* operator new[](size_t s) { return (A).allocate(s); }        \
114     void* operator new[](size_t, void *_Where) { return (_Where); }   \
115     void operator delete[](void*) { }                                 \
116     void operator delete[](void *, void *) { }
117
118 namespace glslang {
119
120     //
121     // Pool version of string.
122     //
123     typedef pool_allocator<char> TStringAllocator;
124     typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
125
126 } // end namespace glslang
127
128 // Repackage the std::hash for use by unordered map/set with a TString key.
129 namespace std {
130
131     template<> struct hash<glslang::TString> {
132         std::size_t operator()(const glslang::TString& s) const
133         {
134             const unsigned _FNV_offset_basis = 2166136261U;
135             const unsigned _FNV_prime = 16777619U;
136             unsigned _Val = _FNV_offset_basis;
137             size_t _Count = s.size();
138             const char* _First = s.c_str();
139             for (size_t _Next = 0; _Next < _Count; ++_Next)
140             {
141                 _Val ^= (unsigned)_First[_Next];
142                 _Val *= _FNV_prime;
143             }
144
145             return _Val;
146         }
147     };
148 }
149
150 namespace glslang {
151
152 inline TString* NewPoolTString(const char* s)
153 {
154     void* memory = GetThreadPoolAllocator().allocate(sizeof(TString));
155     return new(memory) TString(s);
156 }
157
158 template<class T> inline T* NewPoolObject(T)
159 {
160     return new(GetThreadPoolAllocator().allocate(sizeof(T))) T;
161 }
162
163 template<class T> inline T* NewPoolObject(T, int instances)
164 {
165     return new(GetThreadPoolAllocator().allocate(instances * sizeof(T))) T[instances];
166 }
167
168 //
169 // Pool allocator versions of vectors, lists, and maps
170 //
171 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
172 public:
173     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
174
175     typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
176     TVector() : std::vector<T, pool_allocator<T> >() {}
177     TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
178     TVector(size_type i) : std::vector<T, pool_allocator<T> >(i) {}
179     TVector(size_type i, const T& val) : std::vector<T, pool_allocator<T> >(i, val) {}
180 };
181
182 template <class T> class TList  : public std::list<T, pool_allocator<T> > {
183 };
184
185 template <class K, class D, class CMP = std::less<K> >
186 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<K const, D> > > {
187 };
188
189 template <class K, class D, class HASH = std::hash<K>, class PRED = std::equal_to<K> >
190 class TUnorderedMap : public std::unordered_map<K, D, HASH, PRED, pool_allocator<std::pair<K const, D> > > {
191 };
192
193 //
194 // Persistent string memory.  Should only be used for strings that survive
195 // across compiles/links.
196 //
197 typedef std::basic_string<char> TPersistString;
198
199 //
200 // templatized min and max functions.
201 //
202 template <class T> T Min(const T a, const T b) { return a < b ? a : b; }
203 template <class T> T Max(const T a, const T b) { return a > b ? a : b; }
204
205 //
206 // Create a TString object from an integer.
207 //
208 #if defined _MSC_VER || defined MINGW_HAS_SECURE_API
209 inline const TString String(const int i, const int base = 10)
210 {
211     char text[16];     // 32 bit ints are at most 10 digits in base 10
212     _itoa_s(i, text, sizeof(text), base);
213     return text;
214 }
215 #else
216 inline const TString String(const int i, const int /*base*/ = 10)
217 {
218     char text[16];     // 32 bit ints are at most 10 digits in base 10
219
220     // we assume base 10 for all cases
221     snprintf(text, sizeof(text), "%d", i);
222
223     return text;
224 }
225 #endif
226
227 struct TSourceLoc {
228     void init() { name = nullptr; string = 0; line = 0; column = 0; }
229     void init(int stringNum) { init(); string = stringNum; }
230     // Returns the name if it exists. Otherwise, returns the string number.
231     std::string getStringNameOrNum(bool quoteStringName = true) const
232     {
233         if (name != nullptr)
234             return quoteStringName ? ("\"" + std::string(name) + "\"") : name;
235         return std::to_string((long long)string);
236     }
237     const char* name; // descriptive name for this string
238     int string;
239     int line;
240     int column;
241 };
242
243 typedef TMap<TString, TString> TPragmaTable;
244
245 const int MaxTokenLength = 1024;
246
247 template <class T> bool IsPow2(T powerOf2)
248 {
249     if (powerOf2 <= 0)
250         return false;
251
252     return (powerOf2 & (powerOf2 - 1)) == 0;
253 }
254
255 // Round number up to a multiple of the given powerOf2, which is not
256 // a power, just a number that must be a power of 2.
257 template <class T> void RoundToPow2(T& number, int powerOf2)
258 {
259     assert(IsPow2(powerOf2));
260     number = (number + powerOf2 - 1) & ~(powerOf2 - 1);
261 }
262
263 template <class T> bool IsMultipleOfPow2(T number, int powerOf2)
264 {
265     assert(IsPow2(powerOf2));
266     return ! (number & (powerOf2 - 1));
267 }
268
269 } // end namespace glslang
270
271 #endif // _COMMON_INCLUDED_