blob: 5759302243166f2973f3609db6406e50736ec5be [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 Madill075edd82013-07-08 13:30:19 -040021struct TSourceLoc {
22 int first_file;
23 int first_line;
24 int last_file;
25 int last_line;
26};
alokp@chromium.org4e89d232010-05-14 19:37:21 +000027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028//
29// Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
30//
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040031#define POOL_ALLOCATOR_NEW_DELETE() \
32 void* operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); } \
33 void* operator new(size_t, void *_Where) { return (_Where); } \
34 void operator delete(void*) { } \
35 void operator delete(void *, void *) { } \
36 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*) { } \
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039 void operator delete[](void *, void *) { }
40
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041//
42// Pool version of string.
43//
44typedef pool_allocator<char> TStringAllocator;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000045typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
46typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047inline TString* NewPoolTString(const char* s)
48{
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040049 void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 return new(memory) TString(s);
51}
52
53//
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000054// Persistent string memory. Should only be used for strings that survive
alokp@chromium.org774d7062010-07-21 18:55:45 +000055// across compiles.
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000056//
57#define TPersistString std::string
58#define TPersistStringStream std::ostringstream
59
60//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061// Pool allocator versions of vectors, lists, and maps
62//
63template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
64public:
65 typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
66 TVector() : std::vector<T, pool_allocator<T> >() {}
67 TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
68 TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
69};
70
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071template <class K, class D, class CMP = std::less<K> >
alokp@chromium.org91a01a12010-05-12 18:39:04 +000072class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +000074 typedef pool_allocator<std::pair<const K, D> > tAllocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075
alokp@chromium.org91a01a12010-05-12 18:39:04 +000076 TMap() : std::map<K, D, CMP, tAllocator>() {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 // use correct two-stage name lookup supported in gcc 3.4 and above
alokp@chromium.org91a01a12010-05-12 18:39:04 +000078 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 +000079};
80
Jamie Madille53c98b2014-02-03 11:57:13 -050081// Integer to TString conversion
82template <typename T>
83inline TString str(T i)
84{
85 ASSERT(std::numeric_limits<T>::is_integer);
86 char buffer[((8 * sizeof(T)) / 3) + 3];
87 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
88 snprintf(buffer, sizeof(buffer), formatStr, i);
89 return buffer;
90}
91
Geoff Lang0a73dd82014-11-19 16:18:08 -050092#endif // COMPILER_TRANSLATOR_COMMON_H_