blob: 853317891f6681b94cb81e708ffade12c6021a3d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 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
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_COMMON_H_
8#define COMPILER_TRANSLATOR_COMMON_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
alokp@chromium.org4e89d232010-05-14 19:37:21 +000010#include <map>
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000011#include <sstream>
alokp@chromium.org4e89d232010-05-14 19:37:21 +000012#include <string>
13#include <vector>
Jamie Madille53c98b2014-02-03 11:57:13 -050014#include <limits>
15#include <stdio.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
Jamie Madilla6564902015-04-27 14:30:32 +000017#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030018#include "common/debug.h"
19#include "compiler/translator/PoolAlloc.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
Jamie Madill075edd82013-07-08 13:30:19 -040024struct TSourceLoc {
25 int first_file;
26 int first_line;
27 int last_file;
28 int last_line;
29};
alokp@chromium.org4e89d232010-05-14 19:37:21 +000030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031//
32// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
33//
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040034#define POOL_ALLOCATOR_NEW_DELETE() \
35 void* operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
36 void* operator new(size_t, void *_Where) { return (_Where); } \
37 void operator delete(void*) { } \
38 void operator delete(void *, void *) { } \
39 void* operator new[](size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
40 void* operator new[](size_t, void *_Where) { return (_Where); } \
41 void operator delete[](void*) { } \
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042 void operator delete[](void *, void *) { }
43
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044//
45// Pool version of string.
46//
47typedef pool_allocator<char> TStringAllocator;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000048typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
49typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050inline TString* NewPoolTString(const char* s)
51{
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040052 void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053 return new(memory) TString(s);
54}
55
56//
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000057// Persistent string memory. Should only be used for strings that survive
alokp@chromium.org774d7062010-07-21 18:55:45 +000058// across compiles.
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000059//
60#define TPersistString std::string
61#define TPersistStringStream std::ostringstream
62
63//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064// Pool allocator versions of vectors, lists, and maps
65//
Corentin Wallez1eabcf42016-02-02 13:54:00 -050066template <class T>
67class TVector : public std::vector<T, pool_allocator<T>>
68{
69 public:
70 typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
71 TVector() : std::vector<T, pool_allocator<T>>() {}
72 TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
73 TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074};
75
Corentin Wallez1eabcf42016-02-02 13:54:00 -050076template <class K, class D, class CMP = std::less<K>>
77class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
78{
79 public:
80 typedef pool_allocator<std::pair<const K, D>> tAllocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081
alokp@chromium.org91a01a12010-05-12 18:39:04 +000082 TMap() : std::map<K, D, CMP, tAllocator>() {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083 // use correct two-stage name lookup supported in gcc 3.4 and above
alokp@chromium.org91a01a12010-05-12 18:39:04 +000084 TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085};
86
Jamie Madille53c98b2014-02-03 11:57:13 -050087// Integer to TString conversion
88template <typename T>
89inline TString str(T i)
90{
91 ASSERT(std::numeric_limits<T>::is_integer);
92 char buffer[((8 * sizeof(T)) / 3) + 3];
93 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
94 snprintf(buffer, sizeof(buffer), formatStr, i);
95 return buffer;
96}
97
Jamie Madill45bcc782016-11-07 13:58:48 -050098} // namespace sh
99
Geoff Lang0a73dd82014-11-19 16:18:08 -0500100#endif // COMPILER_TRANSLATOR_COMMON_H_