blob: 3e12d2add10678be90d1407ae8229ae2d4ef1375 [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 Madilld7b1ab52016-12-12 14:42:19 -050024struct TSourceLoc
25{
Jamie Madill075edd82013-07-08 13:30:19 -040026 int first_file;
27 int first_line;
28 int last_file;
29 int last_line;
30};
alokp@chromium.org4e89d232010-05-14 19:37:21 +000031
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032//
33// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
34//
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040035#define POOL_ALLOCATOR_NEW_DELETE() \
Jamie Madilld7b1ab52016-12-12 14:42:19 -050036 void *operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
37 void *operator new(size_t, void *_Where) { return (_Where); } \
38 void operator delete(void *) {} \
39 void operator delete(void *, void *) {} \
40 void *operator new[](size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
41 void *operator new[](size_t, void *_Where) { return (_Where); } \
42 void operator delete[](void *) {} \
43 void operator delete[](void *, void *) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045//
46// Pool version of string.
47//
48typedef pool_allocator<char> TStringAllocator;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050049typedef std::basic_string<char, std::char_traits<char>, TStringAllocator> TString;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000050typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050051inline TString *NewPoolTString(const char *s)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050053 void *memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
54 return new (memory) TString(s);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055}
56
57//
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000058// Persistent string memory. Should only be used for strings that survive
alokp@chromium.org774d7062010-07-21 18:55:45 +000059// across compiles.
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000060//
61#define TPersistString std::string
62#define TPersistStringStream std::ostringstream
63
64//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065// Pool allocator versions of vectors, lists, and maps
66//
Corentin Wallez1eabcf42016-02-02 13:54:00 -050067template <class T>
68class TVector : public std::vector<T, pool_allocator<T>>
69{
70 public:
Corentin Wallez810026b2017-02-02 12:37:27 -050071 POOL_ALLOCATOR_NEW_DELETE();
72
Corentin Wallez1eabcf42016-02-02 13:54:00 -050073 typedef typename std::vector<T, pool_allocator<T>>::size_type size_type;
74 TVector() : std::vector<T, pool_allocator<T>>() {}
75 TVector(const pool_allocator<T> &a) : std::vector<T, pool_allocator<T>>(a) {}
76 TVector(size_type i) : std::vector<T, pool_allocator<T>>(i) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077};
78
Corentin Wallez1eabcf42016-02-02 13:54:00 -050079template <class K, class D, class CMP = std::less<K>>
80class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D>>>
81{
82 public:
Corentin Wallez810026b2017-02-02 12:37:27 -050083 POOL_ALLOCATOR_NEW_DELETE();
Corentin Wallez1eabcf42016-02-02 13:54:00 -050084 typedef pool_allocator<std::pair<const K, D>> tAllocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085
alokp@chromium.org91a01a12010-05-12 18:39:04 +000086 TMap() : std::map<K, D, CMP, tAllocator>() {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 // use correct two-stage name lookup supported in gcc 3.4 and above
Jamie Madilld7b1ab52016-12-12 14:42:19 -050088 TMap(const tAllocator &a)
89 : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a)
90 {
91 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092};
93
Jamie Madille53c98b2014-02-03 11:57:13 -050094// Integer to TString conversion
95template <typename T>
96inline TString str(T i)
97{
98 ASSERT(std::numeric_limits<T>::is_integer);
99 char buffer[((8 * sizeof(T)) / 3) + 3];
100 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
101 snprintf(buffer, sizeof(buffer), formatStr, i);
102 return buffer;
103}
104
Jamie Madill45bcc782016-11-07 13:58:48 -0500105} // namespace sh
106
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500107#endif // COMPILER_TRANSLATOR_COMMON_H_