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