Home | History | Annotate | Download | only in compiler
      1 //
      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 
      7 #ifndef _COMMON_INCLUDED_
      8 #define _COMMON_INCLUDED_
      9 
     10 #include <map>
     11 #include <sstream>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "compiler/PoolAlloc.h"
     16 
     17 struct TSourceLoc {
     18     int first_file;
     19     int first_line;
     20     int last_file;
     21     int last_line;
     22 };
     23 
     24 //
     25 // Put POOL_ALLOCATOR_NEW_DELETE in base classes to make them use this scheme.
     26 //
     27 #define POOL_ALLOCATOR_NEW_DELETE()                                                  \
     28     void* operator new(size_t s) { return GetGlobalPoolAllocator()->allocate(s); }   \
     29     void* operator new(size_t, void *_Where) { return (_Where); }                    \
     30     void operator delete(void*) { }                                                  \
     31     void operator delete(void *, void *) { }                                         \
     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 
     37 //
     38 // Pool version of string.
     39 //
     40 typedef pool_allocator<char> TStringAllocator;
     41 typedef std::basic_string <char, std::char_traits<char>, TStringAllocator> TString;
     42 typedef std::basic_ostringstream<char, std::char_traits<char>, TStringAllocator> TStringStream;
     43 inline TString* NewPoolTString(const char* s)
     44 {
     45 	void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TString));
     46 	return new(memory) TString(s);
     47 }
     48 
     49 //
     50 // Persistent string memory.  Should only be used for strings that survive
     51 // across compiles.
     52 //
     53 #define TPersistString std::string
     54 #define TPersistStringStream std::ostringstream
     55 
     56 //
     57 // Pool allocator versions of vectors, lists, and maps
     58 //
     59 template <class T> class TVector : public std::vector<T, pool_allocator<T> > {
     60 public:
     61     typedef typename std::vector<T, pool_allocator<T> >::size_type size_type;
     62     TVector() : std::vector<T, pool_allocator<T> >() {}
     63     TVector(const pool_allocator<T>& a) : std::vector<T, pool_allocator<T> >(a) {}
     64     TVector(size_type i): std::vector<T, pool_allocator<T> >(i) {}
     65 };
     66 
     67 template <class K, class D, class CMP = std::less<K> >
     68 class TMap : public std::map<K, D, CMP, pool_allocator<std::pair<const K, D> > > {
     69 public:
     70     typedef pool_allocator<std::pair<const K, D> > tAllocator;
     71 
     72     TMap() : std::map<K, D, CMP, tAllocator>() {}
     73     // use correct two-stage name lookup supported in gcc 3.4 and above
     74     TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
     75 };
     76 
     77 #endif // _COMMON_INCLUDED_
     78