Home | History | Annotate | Download | only in PathSensitive
      1 //==- GRStateTrait.h - Partial implementations of GRStateTrait -----*- C++ -*-//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines partial implementations of template specializations of
     11 //  the class GRStateTrait<>.  GRStateTrait<> is used by GRState to implement
     12 //  set/get methods for mapulating a GRState's generic data map.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 
     17 #ifndef LLVM_CLANG_GR_GRSTATETRAIT_H
     18 #define LLVM_CLANG_GR_GRSTATETRAIT_H
     19 
     20 namespace llvm {
     21   class BumpPtrAllocator;
     22   template <typename K, typename D, typename I> class ImmutableMap;
     23   template <typename K, typename I> class ImmutableSet;
     24   template <typename T> class ImmutableList;
     25   template <typename T> class ImmutableListImpl;
     26 }
     27 
     28 namespace clang {
     29 
     30 namespace ento {
     31   template <typename T> struct GRStatePartialTrait;
     32 
     33   // Partial-specialization for ImmutableMap.
     34 
     35   template <typename Key, typename Data, typename Info>
     36   struct GRStatePartialTrait< llvm::ImmutableMap<Key,Data,Info> > {
     37     typedef llvm::ImmutableMap<Key,Data,Info> data_type;
     38     typedef typename data_type::Factory&      context_type;
     39     typedef Key                               key_type;
     40     typedef Data                              value_type;
     41     typedef const value_type*                 lookup_type;
     42 
     43     static inline data_type MakeData(void* const* p) {
     44       return p ? data_type((typename data_type::TreeTy*) *p) : data_type(0);
     45     }
     46     static inline void* MakeVoidPtr(data_type B) {
     47       return B.getRoot();
     48     }
     49     static lookup_type Lookup(data_type B, key_type K) {
     50       return B.lookup(K);
     51     }
     52     static data_type Set(data_type B, key_type K, value_type E,context_type F){
     53       return F.add(B, K, E);
     54     }
     55 
     56     static data_type Remove(data_type B, key_type K, context_type F) {
     57       return F.remove(B, K);
     58     }
     59 
     60     static inline context_type MakeContext(void* p) {
     61       return *((typename data_type::Factory*) p);
     62     }
     63 
     64     static void* CreateContext(llvm::BumpPtrAllocator& Alloc) {
     65       return new typename data_type::Factory(Alloc);
     66     }
     67 
     68     static void DeleteContext(void* Ctx) {
     69       delete (typename data_type::Factory*) Ctx;
     70     }
     71   };
     72 
     73 
     74   // Partial-specialization for ImmutableSet.
     75 
     76   template <typename Key, typename Info>
     77   struct GRStatePartialTrait< llvm::ImmutableSet<Key,Info> > {
     78     typedef llvm::ImmutableSet<Key,Info>      data_type;
     79     typedef typename data_type::Factory&      context_type;
     80     typedef Key                               key_type;
     81 
     82     static inline data_type MakeData(void* const* p) {
     83       return p ? data_type((typename data_type::TreeTy*) *p) : data_type(0);
     84     }
     85 
     86     static inline void* MakeVoidPtr(data_type B) {
     87       return B.getRoot();
     88     }
     89 
     90     static data_type Add(data_type B, key_type K, context_type F) {
     91       return F.add(B, K);
     92     }
     93 
     94     static data_type Remove(data_type B, key_type K, context_type F) {
     95       return F.remove(B, K);
     96     }
     97 
     98     static bool Contains(data_type B, key_type K) {
     99       return B.contains(K);
    100     }
    101 
    102     static inline context_type MakeContext(void* p) {
    103       return *((typename data_type::Factory*) p);
    104     }
    105 
    106     static void* CreateContext(llvm::BumpPtrAllocator& Alloc) {
    107       return new typename data_type::Factory(Alloc);
    108     }
    109 
    110     static void DeleteContext(void* Ctx) {
    111       delete (typename data_type::Factory*) Ctx;
    112     }
    113   };
    114 
    115   // Partial-specialization for ImmutableList.
    116 
    117   template <typename T>
    118   struct GRStatePartialTrait< llvm::ImmutableList<T> > {
    119     typedef llvm::ImmutableList<T>            data_type;
    120     typedef T                                 key_type;
    121     typedef typename data_type::Factory&      context_type;
    122 
    123     static data_type Add(data_type L, key_type K, context_type F) {
    124       return F.add(K, L);
    125     }
    126 
    127     static bool Contains(data_type L, key_type K) {
    128       return L.contains(K);
    129     }
    130 
    131     static inline data_type MakeData(void* const* p) {
    132       return p ? data_type((const llvm::ImmutableListImpl<T>*) *p)
    133                : data_type(0);
    134     }
    135 
    136     static inline void* MakeVoidPtr(data_type D) {
    137       return  (void*) D.getInternalPointer();
    138     }
    139 
    140     static inline context_type MakeContext(void* p) {
    141       return *((typename data_type::Factory*) p);
    142     }
    143 
    144     static void* CreateContext(llvm::BumpPtrAllocator& Alloc) {
    145       return new typename data_type::Factory(Alloc);
    146     }
    147 
    148     static void DeleteContext(void* Ctx) {
    149       delete (typename data_type::Factory*) Ctx;
    150     }
    151   };
    152 
    153   // Partial specialization for bool.
    154   template <> struct GRStatePartialTrait<bool> {
    155     typedef bool data_type;
    156 
    157     static inline data_type MakeData(void* const* p) {
    158       return p ? (data_type) (uintptr_t) *p
    159                : data_type();
    160     }
    161     static inline void *MakeVoidPtr(data_type d) {
    162       return (void*) (uintptr_t) d;
    163     }
    164   };
    165 
    166   // Partial specialization for unsigned.
    167   template <> struct GRStatePartialTrait<unsigned> {
    168     typedef unsigned data_type;
    169 
    170     static inline data_type MakeData(void* const* p) {
    171       return p ? (data_type) (uintptr_t) *p
    172                : data_type();
    173     }
    174     static inline void *MakeVoidPtr(data_type d) {
    175       return (void*) (uintptr_t) d;
    176     }
    177   };
    178 
    179 } // end GR namespace
    180 
    181 } // end clang namespace
    182 
    183 #endif
    184