]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/GRState.h
MFV of tzdata2011b, r219147
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / PathSensitive / GRState.h
1 //== GRState.h - Path-sensitive "State" for tracking values -----*- 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 SymbolRef, ExprBindKey, and GRState*.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_GR_VALUESTATE_H
15 #define LLVM_CLANG_GR_VALUESTATE_H
16
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/Environment.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/ADT/ImmutableMap.h"
24 #include "llvm/Support/Casting.h"
25
26 namespace llvm {
27 class APSInt;
28 class BumpPtrAllocator;
29 class raw_ostream;
30 }
31
32 namespace clang {
33 class ASTContext;
34
35 namespace ento {
36
37 class GRStateManager;
38 class Checker;
39
40 typedef ConstraintManager* (*ConstraintManagerCreator)(GRStateManager&,
41                                                        SubEngine&);
42 typedef StoreManager* (*StoreManagerCreator)(GRStateManager&);
43
44 //===----------------------------------------------------------------------===//
45 // GRStateTrait - Traits used by the Generic Data Map of a GRState.
46 //===----------------------------------------------------------------------===//
47
48 template <typename T> struct GRStatePartialTrait;
49
50 template <typename T> struct GRStateTrait {
51   typedef typename T::data_type data_type;
52   static inline void* GDMIndex() { return &T::TagInt; }
53   static inline void* MakeVoidPtr(data_type D) { return (void*) D; }
54   static inline data_type MakeData(void* const* P) {
55     return P ? (data_type) *P : (data_type) 0;
56   }
57 };
58
59 class GRStateManager;
60
61 /// GRState - This class encapsulates:
62 ///
63 ///    1. A mapping from expressions to values (Environment)
64 ///    2. A mapping from locations to values (Store)
65 ///    3. Constraints on symbolic values (GenericDataMap)
66 ///
67 ///  Together these represent the "abstract state" of a program.
68 ///
69 ///  GRState is intended to be used as a functional object; that is,
70 ///  once it is created and made "persistent" in a FoldingSet, its
71 ///  values will never change.
72 class GRState : public llvm::FoldingSetNode {
73 public:
74   typedef llvm::ImmutableSet<llvm::APSInt*>                IntSetTy;
75   typedef llvm::ImmutableMap<void*, void*>                 GenericDataMap;
76
77 private:
78   void operator=(const GRState& R) const; // Do not implement.
79
80   friend class GRStateManager;
81   friend class ExplodedGraph;
82   friend class ExplodedNode;
83
84   GRStateManager *stateMgr;
85   Environment Env;           // Maps a Stmt to its current SVal.
86   Store store;               // Maps a location to its current value.
87   GenericDataMap   GDM;      // Custom data stored by a client of this class.
88   unsigned refCount;
89
90   /// makeWithStore - Return a GRState with the same values as the current
91   ///  state with the exception of using the specified Store.
92   const GRState *makeWithStore(const StoreRef &store) const;
93
94   void setStore(const StoreRef &storeRef);
95
96 public:
97
98   /// This ctor is used when creating the first GRState object.
99   GRState(GRStateManager *mgr, const Environment& env,
100           StoreRef st, GenericDataMap gdm);
101     
102   /// Copy ctor - We must explicitly define this or else the "Next" ptr
103   ///  in FoldingSetNode will also get copied.
104   GRState(const GRState& RHS);
105   
106   ~GRState();
107
108   /// Return the GRStateManager associated with this state.
109   GRStateManager &getStateManager() const { return *stateMgr; }
110
111   /// Return true if this state is referenced by a persistent ExplodedNode.
112   bool referencedByExplodedNode() const { return refCount > 0; }
113
114   /// getEnvironment - Return the environment associated with this state.
115   ///  The environment is the mapping from expressions to values.
116   const Environment& getEnvironment() const { return Env; }
117
118   /// Return the store associated with this state.  The store
119   ///  is a mapping from locations to values.
120   Store getStore() const { return store; }
121
122   
123   /// getGDM - Return the generic data map associated with this state.
124   GenericDataMap getGDM() const { return GDM; }
125
126   void setGDM(GenericDataMap gdm) { GDM = gdm; }
127
128   /// Profile - Profile the contents of a GRState object for use in a
129   ///  FoldingSet.  Two GRState objects are considered equal if they
130   ///  have the same Environment, Store, and GenericDataMap.
131   static void Profile(llvm::FoldingSetNodeID& ID, const GRState* V) {
132     V->Env.Profile(ID);
133     ID.AddPointer(V->store);
134     V->GDM.Profile(ID);
135   }
136
137   /// Profile - Used to profile the contents of this object for inclusion
138   ///  in a FoldingSet.
139   void Profile(llvm::FoldingSetNodeID& ID) const {
140     Profile(ID, this);
141   }
142
143   BasicValueFactory &getBasicVals() const;
144   SymbolManager &getSymbolManager() const;
145
146   //==---------------------------------------------------------------------==//
147   // Constraints on values.
148   //==---------------------------------------------------------------------==//
149   //
150   // Each GRState records constraints on symbolic values.  These constraints
151   // are managed using the ConstraintManager associated with a GRStateManager.
152   // As constraints gradually accrue on symbolic values, added constraints
153   // may conflict and indicate that a state is infeasible (as no real values
154   // could satisfy all the constraints).  This is the principal mechanism
155   // for modeling path-sensitivity in ExprEngine/GRState.
156   //
157   // Various "assume" methods form the interface for adding constraints to
158   // symbolic values.  A call to 'assume' indicates an assumption being placed
159   // on one or symbolic values.  'assume' methods take the following inputs:
160   //
161   //  (1) A GRState object representing the current state.
162   //
163   //  (2) The assumed constraint (which is specific to a given "assume" method).
164   //
165   //  (3) A binary value "Assumption" that indicates whether the constraint is
166   //      assumed to be true or false.
167   //
168   // The output of "assume*" is a new GRState object with the added constraints.
169   // If no new state is feasible, NULL is returned.
170   //
171
172   const GRState *assume(DefinedOrUnknownSVal cond, bool assumption) const;
173
174   /// This method assumes both "true" and "false" for 'cond', and
175   ///  returns both corresponding states.  It's shorthand for doing
176   ///  'assume' twice.
177   std::pair<const GRState*, const GRState*>
178   assume(DefinedOrUnknownSVal cond) const;
179
180   const GRState *assumeInBound(DefinedOrUnknownSVal idx,
181                                DefinedOrUnknownSVal upperBound,
182                                bool assumption) const;
183
184   //==---------------------------------------------------------------------==//
185   // Utility methods for getting regions.
186   //==---------------------------------------------------------------------==//
187
188   const VarRegion* getRegion(const VarDecl *D, const LocationContext *LC) const;
189
190   //==---------------------------------------------------------------------==//
191   // Binding and retrieving values to/from the environment and symbolic store.
192   //==---------------------------------------------------------------------==//
193
194   /// BindCompoundLiteral - Return the state that has the bindings currently
195   ///  in this state plus the bindings for the CompoundLiteral.
196   const GRState *bindCompoundLiteral(const CompoundLiteralExpr* CL,
197                                      const LocationContext *LC,
198                                      SVal V) const;
199
200   /// Create a new state by binding the value 'V' to the statement 'S' in the
201   /// state's environment.
202   const GRState *BindExpr(const Stmt *S, SVal V, bool Invalidate = true) const;
203
204   /// Create a new state by binding the value 'V' and location 'locaton' to the
205   /// statement 'S' in the state's environment.
206   const GRState *bindExprAndLocation(const Stmt *S, SVal location, SVal V)
207     const;
208   
209   const GRState *bindDecl(const VarRegion *VR, SVal V) const;
210
211   const GRState *bindDeclWithNoInit(const VarRegion *VR) const;
212
213   const GRState *bindLoc(Loc location, SVal V) const;
214
215   const GRState *bindLoc(SVal location, SVal V) const;
216
217   const GRState *bindDefault(SVal loc, SVal V) const;
218
219   const GRState *unbindLoc(Loc LV) const;
220
221   /// invalidateRegion - Returns the state with bindings for the given region
222   ///  cleared from the store. See invalidateRegions.
223   const GRState *invalidateRegion(const MemRegion *R,
224                                   const Expr *E, unsigned BlockCount,
225                                   StoreManager::InvalidatedSymbols *IS = NULL)
226                                   const {
227     return invalidateRegions(&R, &R+1, E, BlockCount, IS, false);
228   }
229
230   /// invalidateRegions - Returns the state with bindings for the given regions
231   ///  cleared from the store. The regions are provided as a continuous array
232   ///  from Begin to End. Optionally invalidates global regions as well.
233   const GRState *invalidateRegions(const MemRegion * const *Begin,
234                                    const MemRegion * const *End,
235                                    const Expr *E, unsigned BlockCount,
236                                    StoreManager::InvalidatedSymbols *IS,
237                                    bool invalidateGlobals) const;
238
239   /// enterStackFrame - Returns the state for entry to the given stack frame,
240   ///  preserving the current state.
241   const GRState *enterStackFrame(const StackFrameContext *frame) const;
242
243   /// Get the lvalue for a variable reference.
244   Loc getLValue(const VarDecl *D, const LocationContext *LC) const;
245
246   /// Get the lvalue for a StringLiteral.
247   Loc getLValue(const StringLiteral *literal) const;
248
249   Loc getLValue(const CompoundLiteralExpr *literal, 
250                 const LocationContext *LC) const;
251
252   /// Get the lvalue for an ivar reference.
253   SVal getLValue(const ObjCIvarDecl *decl, SVal base) const;
254
255   /// Get the lvalue for a field reference.
256   SVal getLValue(const FieldDecl *decl, SVal Base) const;
257
258   /// Get the lvalue for an array index.
259   SVal getLValue(QualType ElementType, SVal Idx, SVal Base) const;
260
261   const llvm::APSInt *getSymVal(SymbolRef sym) const;
262
263   /// Returns the SVal bound to the statement 'S' in the state's environment.
264   SVal getSVal(const Stmt* S) const;
265   
266   SVal getSValAsScalarOrLoc(const Stmt *Ex) const;
267
268   SVal getSVal(Loc LV, QualType T = QualType()) const;
269
270   /// Returns the "raw" SVal bound to LV before any value simplfication.
271   SVal getRawSVal(Loc LV, QualType T= QualType()) const;
272
273   SVal getSVal(const MemRegion* R) const;
274
275   SVal getSValAsScalarOrLoc(const MemRegion *R) const;
276   
277   const llvm::APSInt *getSymVal(SymbolRef sym);
278
279   bool scanReachableSymbols(SVal val, SymbolVisitor& visitor) const;
280   
281   bool scanReachableSymbols(const SVal *I, const SVal *E,
282                             SymbolVisitor &visitor) const;
283   
284   bool scanReachableSymbols(const MemRegion * const *I, 
285                             const MemRegion * const *E,
286                             SymbolVisitor &visitor) const;
287
288   template <typename CB> CB scanReachableSymbols(SVal val) const;
289   template <typename CB> CB scanReachableSymbols(const SVal *beg,
290                                                  const SVal *end) const;
291   
292   template <typename CB> CB
293   scanReachableSymbols(const MemRegion * const *beg,
294                        const MemRegion * const *end) const;
295
296   //==---------------------------------------------------------------------==//
297   // Accessing the Generic Data Map (GDM).
298   //==---------------------------------------------------------------------==//
299
300   void* const* FindGDM(void* K) const;
301
302   template<typename T>
303   const GRState *add(typename GRStateTrait<T>::key_type K) const;
304
305   template <typename T>
306   typename GRStateTrait<T>::data_type
307   get() const {
308     return GRStateTrait<T>::MakeData(FindGDM(GRStateTrait<T>::GDMIndex()));
309   }
310
311   template<typename T>
312   typename GRStateTrait<T>::lookup_type
313   get(typename GRStateTrait<T>::key_type key) const {
314     void* const* d = FindGDM(GRStateTrait<T>::GDMIndex());
315     return GRStateTrait<T>::Lookup(GRStateTrait<T>::MakeData(d), key);
316   }
317
318   template <typename T>
319   typename GRStateTrait<T>::context_type get_context() const;
320
321
322   template<typename T>
323   const GRState *remove(typename GRStateTrait<T>::key_type K) const;
324
325   template<typename T>
326   const GRState *remove(typename GRStateTrait<T>::key_type K,
327                         typename GRStateTrait<T>::context_type C) const;
328   template <typename T>
329   const GRState *remove() const;
330
331   template<typename T>
332   const GRState *set(typename GRStateTrait<T>::data_type D) const;
333
334   template<typename T>
335   const GRState *set(typename GRStateTrait<T>::key_type K,
336                      typename GRStateTrait<T>::value_type E) const;
337
338   template<typename T>
339   const GRState *set(typename GRStateTrait<T>::key_type K,
340                      typename GRStateTrait<T>::value_type E,
341                      typename GRStateTrait<T>::context_type C) const;
342
343   template<typename T>
344   bool contains(typename GRStateTrait<T>::key_type key) const {
345     void* const* d = FindGDM(GRStateTrait<T>::GDMIndex());
346     return GRStateTrait<T>::Contains(GRStateTrait<T>::MakeData(d), key);
347   }
348
349   // State pretty-printing.
350   class Printer {
351   public:
352     virtual ~Printer() {}
353     virtual void Print(llvm::raw_ostream& Out, const GRState* state,
354                        const char* nl, const char* sep) = 0;
355   };
356
357   // Pretty-printing.
358   void print(llvm::raw_ostream& Out, CFG &C, const char *nl = "\n",
359              const char *sep = "") const;
360
361   void printStdErr(CFG &C) const;
362
363   void printDOT(llvm::raw_ostream& Out, CFG &C) const;
364
365 private:
366   /// Increments the number of times this state is referenced by ExplodeNodes.
367   void incrementReferenceCount() { ++refCount; }
368
369   /// Decrement the number of times this state is referenced by ExplodeNodes.
370   void decrementReferenceCount() {
371     assert(refCount > 0);
372     --refCount;
373   }
374 };
375
376 class GRStateSet {
377   typedef llvm::SmallPtrSet<const GRState*,5> ImplTy;
378   ImplTy Impl;
379 public:
380   GRStateSet() {}
381
382   inline void Add(const GRState* St) {
383     Impl.insert(St);
384   }
385
386   typedef ImplTy::const_iterator iterator;
387
388   inline unsigned size() const { return Impl.size();  }
389   inline bool empty()    const { return Impl.empty(); }
390
391   inline iterator begin() const { return Impl.begin(); }
392   inline iterator end() const { return Impl.end();   }
393
394   class AutoPopulate {
395     GRStateSet& S;
396     unsigned StartSize;
397     const GRState* St;
398   public:
399     AutoPopulate(GRStateSet& s, const GRState* st)
400       : S(s), StartSize(S.size()), St(st) {}
401
402     ~AutoPopulate() {
403       if (StartSize == S.size())
404         S.Add(St);
405     }
406   };
407 };
408
409 //===----------------------------------------------------------------------===//
410 // GRStateManager - Factory object for GRStates.
411 //===----------------------------------------------------------------------===//
412
413 class GRStateManager {
414   friend class GRState;
415   friend class ExprEngine; // FIXME: Remove.
416 private:
417   /// Eng - The SubEngine that owns this state manager.
418   SubEngine *Eng; /* Can be null. */
419
420   EnvironmentManager                   EnvMgr;
421   llvm::OwningPtr<StoreManager>        StoreMgr;
422   llvm::OwningPtr<ConstraintManager>   ConstraintMgr;
423
424   GRState::GenericDataMap::Factory     GDMFactory;
425
426   typedef llvm::DenseMap<void*,std::pair<void*,void (*)(void*)> > GDMContextsTy;
427   GDMContextsTy GDMContexts;
428
429   /// Printers - A set of printer objects used for pretty-printing a GRState.
430   ///  GRStateManager owns these objects.
431   std::vector<GRState::Printer*> Printers;
432
433   /// StateSet - FoldingSet containing all the states created for analyzing
434   ///  a particular function.  This is used to unique states.
435   llvm::FoldingSet<GRState> StateSet;
436
437   /// Object that manages the data for all created SVals.
438   llvm::OwningPtr<SValBuilder> svalBuilder;
439
440   /// A BumpPtrAllocator to allocate states.
441   llvm::BumpPtrAllocator &Alloc;
442
443   /// A vector of recently allocated GRStates that can potentially be
444   /// reused.
445   std::vector<GRState *> recentlyAllocatedStates;
446   
447   /// A vector of GRStates that we can reuse.
448   std::vector<GRState *> freeStates;
449
450 public:
451   GRStateManager(ASTContext& Ctx,
452                  StoreManagerCreator CreateStoreManager,
453                  ConstraintManagerCreator CreateConstraintManager,
454                  llvm::BumpPtrAllocator& alloc,
455                  SubEngine &subeng)
456     : Eng(&subeng),
457       EnvMgr(alloc),
458       GDMFactory(alloc),
459       svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
460       Alloc(alloc) {
461     StoreMgr.reset((*CreateStoreManager)(*this));
462     ConstraintMgr.reset((*CreateConstraintManager)(*this, subeng));
463   }
464
465   GRStateManager(ASTContext& Ctx,
466                  StoreManagerCreator CreateStoreManager,
467                  ConstraintManager* ConstraintManagerPtr,
468                  llvm::BumpPtrAllocator& alloc)
469     : Eng(0),
470       EnvMgr(alloc),
471       GDMFactory(alloc),
472       svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
473       Alloc(alloc) {
474     StoreMgr.reset((*CreateStoreManager)(*this));
475     ConstraintMgr.reset(ConstraintManagerPtr);
476   }
477
478   ~GRStateManager();
479
480   const GRState *getInitialState(const LocationContext *InitLoc);
481
482   ASTContext &getContext() { return svalBuilder->getContext(); }
483   const ASTContext &getContext() const { return svalBuilder->getContext(); }
484
485   BasicValueFactory &getBasicVals() {
486     return svalBuilder->getBasicValueFactory();
487   }
488   const BasicValueFactory& getBasicVals() const {
489     return svalBuilder->getBasicValueFactory();
490   }
491
492   SValBuilder &getSValBuilder() {
493     return *svalBuilder;
494   }
495
496   SymbolManager &getSymbolManager() {
497     return svalBuilder->getSymbolManager();
498   }
499   const SymbolManager &getSymbolManager() const {
500     return svalBuilder->getSymbolManager();
501   }
502
503   llvm::BumpPtrAllocator& getAllocator() { return Alloc; }
504
505   MemRegionManager& getRegionManager() {
506     return svalBuilder->getRegionManager();
507   }
508   const MemRegionManager& getRegionManager() const {
509     return svalBuilder->getRegionManager();
510   }
511
512   StoreManager& getStoreManager() { return *StoreMgr; }
513   ConstraintManager& getConstraintManager() { return *ConstraintMgr; }
514   SubEngine* getOwningEngine() { return Eng; }
515
516   const GRState* removeDeadBindings(const GRState* St,
517                                     const StackFrameContext *LCtx,
518                                     SymbolReaper& SymReaper);
519
520   /// Marshal a new state for the callee in another translation unit.
521   /// 'state' is owned by the caller's engine.
522   const GRState *MarshalState(const GRState *state, const StackFrameContext *L);
523
524 public:
525
526   SVal ArrayToPointer(Loc Array) {
527     return StoreMgr->ArrayToPointer(Array);
528   }
529
530   // Methods that manipulate the GDM.
531   const GRState* addGDM(const GRState* St, void* Key, void* Data);
532   const GRState *removeGDM(const GRState *state, void *Key);
533
534   // Methods that query & manipulate the Store.
535
536   void iterBindings(const GRState* state, StoreManager::BindingsHandler& F) {
537     StoreMgr->iterBindings(state->getStore(), F);
538   }
539
540   const GRState* getPersistentState(GRState& Impl);
541   
542   /// Periodically called by ExprEngine to recycle GRStates that were
543   /// created but never used for creating an ExplodedNode.
544   void recycleUnusedStates();
545
546   //==---------------------------------------------------------------------==//
547   // Generic Data Map methods.
548   //==---------------------------------------------------------------------==//
549   //
550   // GRStateManager and GRState support a "generic data map" that allows
551   // different clients of GRState objects to embed arbitrary data within a
552   // GRState object.  The generic data map is essentially an immutable map
553   // from a "tag" (that acts as the "key" for a client) and opaque values.
554   // Tags/keys and values are simply void* values.  The typical way that clients
555   // generate unique tags are by taking the address of a static variable.
556   // Clients are responsible for ensuring that data values referred to by a
557   // the data pointer are immutable (and thus are essentially purely functional
558   // data).
559   //
560   // The templated methods below use the GRStateTrait<T> class
561   // to resolve keys into the GDM and to return data values to clients.
562   //
563
564   // Trait based GDM dispatch.
565   template <typename T>
566   const GRState* set(const GRState* st, typename GRStateTrait<T>::data_type D) {
567     return addGDM(st, GRStateTrait<T>::GDMIndex(),
568                   GRStateTrait<T>::MakeVoidPtr(D));
569   }
570
571   template<typename T>
572   const GRState* set(const GRState* st,
573                      typename GRStateTrait<T>::key_type K,
574                      typename GRStateTrait<T>::value_type V,
575                      typename GRStateTrait<T>::context_type C) {
576
577     return addGDM(st, GRStateTrait<T>::GDMIndex(),
578      GRStateTrait<T>::MakeVoidPtr(GRStateTrait<T>::Set(st->get<T>(), K, V, C)));
579   }
580
581   template <typename T>
582   const GRState* add(const GRState* st,
583                      typename GRStateTrait<T>::key_type K,
584                      typename GRStateTrait<T>::context_type C) {
585     return addGDM(st, GRStateTrait<T>::GDMIndex(),
586         GRStateTrait<T>::MakeVoidPtr(GRStateTrait<T>::Add(st->get<T>(), K, C)));
587   }
588
589   template <typename T>
590   const GRState* remove(const GRState* st,
591                         typename GRStateTrait<T>::key_type K,
592                         typename GRStateTrait<T>::context_type C) {
593
594     return addGDM(st, GRStateTrait<T>::GDMIndex(),
595      GRStateTrait<T>::MakeVoidPtr(GRStateTrait<T>::Remove(st->get<T>(), K, C)));
596   }
597
598   template <typename T>
599   const GRState *remove(const GRState *st) {
600     return removeGDM(st, GRStateTrait<T>::GDMIndex());
601   }
602
603   void* FindGDMContext(void* index,
604                        void* (*CreateContext)(llvm::BumpPtrAllocator&),
605                        void  (*DeleteContext)(void*));
606
607   template <typename T>
608   typename GRStateTrait<T>::context_type get_context() {
609     void* p = FindGDMContext(GRStateTrait<T>::GDMIndex(),
610                              GRStateTrait<T>::CreateContext,
611                              GRStateTrait<T>::DeleteContext);
612
613     return GRStateTrait<T>::MakeContext(p);
614   }
615
616   const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) {
617     return ConstraintMgr->getSymVal(St, sym);
618   }
619
620   void EndPath(const GRState* St) {
621     ConstraintMgr->EndPath(St);
622   }
623 };
624
625
626 //===----------------------------------------------------------------------===//
627 // Out-of-line method definitions for GRState.
628 //===----------------------------------------------------------------------===//
629
630 inline const llvm::APSInt *GRState::getSymVal(SymbolRef sym) {
631   return getStateManager().getSymVal(this, sym);
632 }
633   
634 inline const VarRegion* GRState::getRegion(const VarDecl *D,
635                                            const LocationContext *LC) const {
636   return getStateManager().getRegionManager().getVarRegion(D, LC);
637 }
638
639 inline const GRState *GRState::assume(DefinedOrUnknownSVal Cond,
640                                       bool Assumption) const {
641   if (Cond.isUnknown())
642     return this;
643   
644   return getStateManager().ConstraintMgr->assume(this, cast<DefinedSVal>(Cond),
645                                                  Assumption);
646 }
647   
648 inline std::pair<const GRState*, const GRState*>
649 GRState::assume(DefinedOrUnknownSVal Cond) const {
650   if (Cond.isUnknown())
651     return std::make_pair(this, this);
652   
653   return getStateManager().ConstraintMgr->assumeDual(this,
654                                                      cast<DefinedSVal>(Cond));
655 }
656
657 inline const GRState *GRState::bindLoc(SVal LV, SVal V) const {
658   return !isa<Loc>(LV) ? this : bindLoc(cast<Loc>(LV), V);
659 }
660
661 inline Loc GRState::getLValue(const VarDecl* VD,
662                                const LocationContext *LC) const {
663   return getStateManager().StoreMgr->getLValueVar(VD, LC);
664 }
665
666 inline Loc GRState::getLValue(const StringLiteral *literal) const {
667   return getStateManager().StoreMgr->getLValueString(literal);
668 }
669
670 inline Loc GRState::getLValue(const CompoundLiteralExpr *literal,
671                                const LocationContext *LC) const {
672   return getStateManager().StoreMgr->getLValueCompoundLiteral(literal, LC);
673 }
674
675 inline SVal GRState::getLValue(const ObjCIvarDecl *D, SVal Base) const {
676   return getStateManager().StoreMgr->getLValueIvar(D, Base);
677 }
678
679 inline SVal GRState::getLValue(const FieldDecl* D, SVal Base) const {
680   return getStateManager().StoreMgr->getLValueField(D, Base);
681 }
682
683 inline SVal GRState::getLValue(QualType ElementType, SVal Idx, SVal Base) const{
684   if (NonLoc *N = dyn_cast<NonLoc>(&Idx))
685     return getStateManager().StoreMgr->getLValueElement(ElementType, *N, Base);
686   return UnknownVal();
687 }
688
689 inline const llvm::APSInt *GRState::getSymVal(SymbolRef sym) const {
690   return getStateManager().getSymVal(this, sym);
691 }
692
693 inline SVal GRState::getSVal(const Stmt* Ex) const {
694   return Env.getSVal(Ex, *getStateManager().svalBuilder);
695 }
696
697 inline SVal GRState::getSValAsScalarOrLoc(const Stmt *S) const {
698   if (const Expr *Ex = dyn_cast<Expr>(S)) {
699     QualType T = Ex->getType();
700     if (Loc::isLocType(T) || T->isIntegerType())
701       return getSVal(S);
702   }
703
704   return UnknownVal();
705 }
706
707 inline SVal GRState::getRawSVal(Loc LV, QualType T) const {
708   return getStateManager().StoreMgr->Retrieve(getStore(), LV, T);
709 }
710
711 inline SVal GRState::getSVal(const MemRegion* R) const {
712   return getStateManager().StoreMgr->Retrieve(getStore(), loc::MemRegionVal(R));
713 }
714
715 inline BasicValueFactory &GRState::getBasicVals() const {
716   return getStateManager().getBasicVals();
717 }
718
719 inline SymbolManager &GRState::getSymbolManager() const {
720   return getStateManager().getSymbolManager();
721 }
722
723 template<typename T>
724 const GRState *GRState::add(typename GRStateTrait<T>::key_type K) const {
725   return getStateManager().add<T>(this, K, get_context<T>());
726 }
727
728 template <typename T>
729 typename GRStateTrait<T>::context_type GRState::get_context() const {
730   return getStateManager().get_context<T>();
731 }
732
733 template<typename T>
734 const GRState *GRState::remove(typename GRStateTrait<T>::key_type K) const {
735   return getStateManager().remove<T>(this, K, get_context<T>());
736 }
737
738 template<typename T>
739 const GRState *GRState::remove(typename GRStateTrait<T>::key_type K,
740                                typename GRStateTrait<T>::context_type C) const {
741   return getStateManager().remove<T>(this, K, C);
742 }
743
744 template <typename T>
745 const GRState *GRState::remove() const {
746   return getStateManager().remove<T>(this);
747 }
748
749 template<typename T>
750 const GRState *GRState::set(typename GRStateTrait<T>::data_type D) const {
751   return getStateManager().set<T>(this, D);
752 }
753
754 template<typename T>
755 const GRState *GRState::set(typename GRStateTrait<T>::key_type K,
756                             typename GRStateTrait<T>::value_type E) const {
757   return getStateManager().set<T>(this, K, E, get_context<T>());
758 }
759
760 template<typename T>
761 const GRState *GRState::set(typename GRStateTrait<T>::key_type K,
762                             typename GRStateTrait<T>::value_type E,
763                             typename GRStateTrait<T>::context_type C) const {
764   return getStateManager().set<T>(this, K, E, C);
765 }
766
767 template <typename CB>
768 CB GRState::scanReachableSymbols(SVal val) const {
769   CB cb(this);
770   scanReachableSymbols(val, cb);
771   return cb;
772 }
773   
774 template <typename CB>
775 CB GRState::scanReachableSymbols(const SVal *beg, const SVal *end) const {
776   CB cb(this);
777   scanReachableSymbols(beg, end, cb);
778   return cb;
779 }
780
781 template <typename CB>
782 CB GRState::scanReachableSymbols(const MemRegion * const *beg,
783                                  const MemRegion * const *end) const {
784   CB cb(this);
785   scanReachableSymbols(beg, end, cb);
786   return cb;
787 }
788
789 } // end GR namespace
790
791 } // end clang namespace
792
793 #endif