1 //ProgramStateTrait.h - Partial implementations of ProgramStateTrait -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines partial implementations of template specializations of
11 // the class ProgramStateTrait<>. ProgramStateTrait<> is used by ProgramState
12 // to implement set/get methods for mapulating a ProgramState's
15 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CLANG_GR_PROGRAMSTATETRAIT_H
19 #define LLVM_CLANG_GR_PROGRAMSTATETRAIT_H
22 class BumpPtrAllocator;
23 template <typename K, typename D, typename I> class ImmutableMap;
24 template <typename K, typename I> class ImmutableSet;
25 template <typename T> class ImmutableList;
26 template <typename T> class ImmutableListImpl;
32 template <typename T> struct ProgramStatePartialTrait;
34 // Partial-specialization for ImmutableMap.
36 template <typename Key, typename Data, typename Info>
37 struct ProgramStatePartialTrait< llvm::ImmutableMap<Key,Data,Info> > {
38 typedef llvm::ImmutableMap<Key,Data,Info> data_type;
39 typedef typename data_type::Factory& context_type;
41 typedef Data value_type;
42 typedef const value_type* lookup_type;
44 static inline data_type MakeData(void *const* p) {
45 return p ? data_type((typename data_type::TreeTy*) *p) : data_type(0);
47 static inline void *MakeVoidPtr(data_type B) {
50 static lookup_type Lookup(data_type B, key_type K) {
53 static data_type Set(data_type B, key_type K, value_type E,context_type F){
54 return F.add(B, K, E);
57 static data_type Remove(data_type B, key_type K, context_type F) {
58 return F.remove(B, K);
61 static inline context_type MakeContext(void *p) {
62 return *((typename data_type::Factory*) p);
65 static void *CreateContext(llvm::BumpPtrAllocator& Alloc) {
66 return new typename data_type::Factory(Alloc);
69 static void DeleteContext(void *Ctx) {
70 delete (typename data_type::Factory*) Ctx;
75 // Partial-specialization for ImmutableSet.
77 template <typename Key, typename Info>
78 struct ProgramStatePartialTrait< llvm::ImmutableSet<Key,Info> > {
79 typedef llvm::ImmutableSet<Key,Info> data_type;
80 typedef typename data_type::Factory& context_type;
83 static inline data_type MakeData(void *const* p) {
84 return p ? data_type((typename data_type::TreeTy*) *p) : data_type(0);
87 static inline void *MakeVoidPtr(data_type B) {
91 static data_type Add(data_type B, key_type K, context_type F) {
95 static data_type Remove(data_type B, key_type K, context_type F) {
96 return F.remove(B, K);
99 static bool Contains(data_type B, key_type K) {
100 return B.contains(K);
103 static inline context_type MakeContext(void *p) {
104 return *((typename data_type::Factory*) p);
107 static void *CreateContext(llvm::BumpPtrAllocator& Alloc) {
108 return new typename data_type::Factory(Alloc);
111 static void DeleteContext(void *Ctx) {
112 delete (typename data_type::Factory*) Ctx;
116 // Partial-specialization for ImmutableList.
118 template <typename T>
119 struct ProgramStatePartialTrait< llvm::ImmutableList<T> > {
120 typedef llvm::ImmutableList<T> data_type;
122 typedef typename data_type::Factory& context_type;
124 static data_type Add(data_type L, key_type K, context_type F) {
128 static bool Contains(data_type L, key_type K) {
129 return L.contains(K);
132 static inline data_type MakeData(void *const* p) {
133 return p ? data_type((const llvm::ImmutableListImpl<T>*) *p)
137 static inline void *MakeVoidPtr(data_type D) {
138 return (void*) D.getInternalPointer();
141 static inline context_type MakeContext(void *p) {
142 return *((typename data_type::Factory*) p);
145 static void *CreateContext(llvm::BumpPtrAllocator& Alloc) {
146 return new typename data_type::Factory(Alloc);
149 static void DeleteContext(void *Ctx) {
150 delete (typename data_type::Factory*) Ctx;
154 // Partial specialization for bool.
155 template <> struct ProgramStatePartialTrait<bool> {
156 typedef bool data_type;
158 static inline data_type MakeData(void *const* p) {
159 return p ? (data_type) (uintptr_t) *p
162 static inline void *MakeVoidPtr(data_type d) {
163 return (void*) (uintptr_t) d;
167 // Partial specialization for unsigned.
168 template <> struct ProgramStatePartialTrait<unsigned> {
169 typedef unsigned data_type;
171 static inline data_type MakeData(void *const* p) {
172 return p ? (data_type) (uintptr_t) *p
175 static inline void *MakeVoidPtr(data_type d) {
176 return (void*) (uintptr_t) d;
180 } // end GR namespace
182 } // end clang namespace