1218887Sdim//=== BasicValueFactory.h - Basic values for Path Sens analysis --*- C++ -*---// 2218887Sdim// 3218887Sdim// The LLVM Compiler Infrastructure 4218887Sdim// 5218887Sdim// This file is distributed under the University of Illinois Open Source 6218887Sdim// License. See LICENSE.TXT for details. 7218887Sdim// 8218887Sdim//===----------------------------------------------------------------------===// 9218887Sdim// 10218887Sdim// This file defines BasicValueFactory, a class that manages the lifetime 11218887Sdim// of APSInt objects and symbolic constraints used by ExprEngine 12218887Sdim// and related classes. 13218887Sdim// 14218887Sdim//===----------------------------------------------------------------------===// 15218887Sdim 16218887Sdim#ifndef LLVM_CLANG_GR_BASICVALUEFACTORY_H 17218887Sdim#define LLVM_CLANG_GR_BASICVALUEFACTORY_H 18218887Sdim 19249423Sdim#include "clang/AST/ASTContext.h" 20239462Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 21249423Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 22221345Sdim#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h" 23218887Sdim 24218887Sdimnamespace clang { 25218887Sdimnamespace ento { 26218887Sdim 27218887Sdimclass CompoundValData : public llvm::FoldingSetNode { 28218887Sdim QualType T; 29218887Sdim llvm::ImmutableList<SVal> L; 30218887Sdim 31218887Sdimpublic: 32218887Sdim CompoundValData(QualType t, llvm::ImmutableList<SVal> l) 33218887Sdim : T(t), L(l) {} 34218887Sdim 35218887Sdim typedef llvm::ImmutableList<SVal>::iterator iterator; 36218887Sdim iterator begin() const { return L.begin(); } 37218887Sdim iterator end() const { return L.end(); } 38218887Sdim 39218887Sdim static void Profile(llvm::FoldingSetNodeID& ID, QualType T, 40218887Sdim llvm::ImmutableList<SVal> L); 41218887Sdim 42218887Sdim void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, T, L); } 43218887Sdim}; 44218887Sdim 45218887Sdimclass LazyCompoundValData : public llvm::FoldingSetNode { 46221345Sdim StoreRef store; 47226633Sdim const TypedValueRegion *region; 48218887Sdimpublic: 49226633Sdim LazyCompoundValData(const StoreRef &st, const TypedValueRegion *r) 50218887Sdim : store(st), region(r) {} 51218887Sdim 52221345Sdim const void *getStore() const { return store.getStore(); } 53226633Sdim const TypedValueRegion *getRegion() const { return region; } 54218887Sdim 55221345Sdim static void Profile(llvm::FoldingSetNodeID& ID, 56221345Sdim const StoreRef &store, 57226633Sdim const TypedValueRegion *region); 58218887Sdim 59218887Sdim void Profile(llvm::FoldingSetNodeID& ID) { Profile(ID, store, region); } 60218887Sdim}; 61218887Sdim 62218887Sdimclass BasicValueFactory { 63218887Sdim typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> > 64218887Sdim APSIntSetTy; 65218887Sdim 66226633Sdim ASTContext &Ctx; 67218887Sdim llvm::BumpPtrAllocator& BPAlloc; 68218887Sdim 69218887Sdim APSIntSetTy APSIntSet; 70226633Sdim void * PersistentSVals; 71226633Sdim void * PersistentSValPairs; 72218887Sdim 73218887Sdim llvm::ImmutableList<SVal>::Factory SValListFactory; 74218887Sdim llvm::FoldingSet<CompoundValData> CompoundValDataSet; 75218887Sdim llvm::FoldingSet<LazyCompoundValData> LazyCompoundValDataSet; 76218887Sdim 77243830Sdim // This is private because external clients should use the factory 78243830Sdim // method that takes a QualType. 79243830Sdim const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); 80243830Sdim 81218887Sdimpublic: 82226633Sdim BasicValueFactory(ASTContext &ctx, llvm::BumpPtrAllocator& Alloc) 83218887Sdim : Ctx(ctx), BPAlloc(Alloc), PersistentSVals(0), PersistentSValPairs(0), 84218887Sdim SValListFactory(Alloc) {} 85218887Sdim 86218887Sdim ~BasicValueFactory(); 87218887Sdim 88226633Sdim ASTContext &getContext() const { return Ctx; } 89218887Sdim 90218887Sdim const llvm::APSInt& getValue(const llvm::APSInt& X); 91218887Sdim const llvm::APSInt& getValue(const llvm::APInt& X, bool isUnsigned); 92218887Sdim const llvm::APSInt& getValue(uint64_t X, QualType T); 93218887Sdim 94239462Sdim /// Returns the type of the APSInt used to store values of the given QualType. 95239462Sdim APSIntType getAPSIntType(QualType T) const { 96251662Sdim assert(T->isIntegralOrEnumerationType() || Loc::isLocType(T)); 97239462Sdim return APSIntType(Ctx.getTypeSize(T), 98239462Sdim !T->isSignedIntegerOrEnumerationType()); 99239462Sdim } 100239462Sdim 101218887Sdim /// Convert - Create a new persistent APSInt with the same value as 'From' 102218887Sdim /// but with the bitwidth and signedness of 'To'. 103218887Sdim const llvm::APSInt &Convert(const llvm::APSInt& To, 104218887Sdim const llvm::APSInt& From) { 105239462Sdim APSIntType TargetType(To); 106239462Sdim if (TargetType == APSIntType(From)) 107218887Sdim return From; 108218887Sdim 109239462Sdim return getValue(TargetType.convert(From)); 110218887Sdim } 111218887Sdim 112218887Sdim const llvm::APSInt &Convert(QualType T, const llvm::APSInt &From) { 113239462Sdim APSIntType TargetType = getAPSIntType(T); 114239462Sdim if (TargetType == APSIntType(From)) 115218887Sdim return From; 116218887Sdim 117239462Sdim return getValue(TargetType.convert(From)); 118218887Sdim } 119218887Sdim 120218887Sdim const llvm::APSInt& getIntValue(uint64_t X, bool isUnsigned) { 121218887Sdim QualType T = isUnsigned ? Ctx.UnsignedIntTy : Ctx.IntTy; 122218887Sdim return getValue(X, T); 123218887Sdim } 124218887Sdim 125218887Sdim inline const llvm::APSInt& getMaxValue(const llvm::APSInt &v) { 126239462Sdim return getValue(APSIntType(v).getMaxValue()); 127218887Sdim } 128218887Sdim 129218887Sdim inline const llvm::APSInt& getMinValue(const llvm::APSInt &v) { 130239462Sdim return getValue(APSIntType(v).getMinValue()); 131218887Sdim } 132218887Sdim 133218887Sdim inline const llvm::APSInt& getMaxValue(QualType T) { 134239462Sdim return getValue(getAPSIntType(T).getMaxValue()); 135218887Sdim } 136218887Sdim 137218887Sdim inline const llvm::APSInt& getMinValue(QualType T) { 138239462Sdim return getValue(getAPSIntType(T).getMinValue()); 139218887Sdim } 140218887Sdim 141218887Sdim inline const llvm::APSInt& Add1(const llvm::APSInt& V) { 142218887Sdim llvm::APSInt X = V; 143218887Sdim ++X; 144218887Sdim return getValue(X); 145218887Sdim } 146218887Sdim 147218887Sdim inline const llvm::APSInt& Sub1(const llvm::APSInt& V) { 148218887Sdim llvm::APSInt X = V; 149218887Sdim --X; 150218887Sdim return getValue(X); 151218887Sdim } 152218887Sdim 153218887Sdim inline const llvm::APSInt& getZeroWithPtrWidth(bool isUnsigned = true) { 154218887Sdim return getValue(0, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned); 155218887Sdim } 156218887Sdim 157218887Sdim inline const llvm::APSInt &getIntWithPtrWidth(uint64_t X, bool isUnsigned) { 158218887Sdim return getValue(X, Ctx.getTypeSize(Ctx.VoidPtrTy), isUnsigned); 159218887Sdim } 160218887Sdim 161218887Sdim inline const llvm::APSInt& getTruthValue(bool b, QualType T) { 162218887Sdim return getValue(b ? 1 : 0, Ctx.getTypeSize(T), false); 163218887Sdim } 164218887Sdim 165218887Sdim inline const llvm::APSInt& getTruthValue(bool b) { 166218887Sdim return getTruthValue(b, Ctx.getLogicalOperationType()); 167218887Sdim } 168218887Sdim 169218887Sdim const CompoundValData *getCompoundValData(QualType T, 170218887Sdim llvm::ImmutableList<SVal> Vals); 171218887Sdim 172221345Sdim const LazyCompoundValData *getLazyCompoundValData(const StoreRef &store, 173226633Sdim const TypedValueRegion *region); 174218887Sdim 175218887Sdim llvm::ImmutableList<SVal> getEmptySValList() { 176218887Sdim return SValListFactory.getEmptyList(); 177218887Sdim } 178218887Sdim 179218887Sdim llvm::ImmutableList<SVal> consVals(SVal X, llvm::ImmutableList<SVal> L) { 180218887Sdim return SValListFactory.add(X, L); 181218887Sdim } 182218887Sdim 183218887Sdim const llvm::APSInt* evalAPSInt(BinaryOperator::Opcode Op, 184218887Sdim const llvm::APSInt& V1, 185218887Sdim const llvm::APSInt& V2); 186218887Sdim 187218887Sdim const std::pair<SVal, uintptr_t>& 188218887Sdim getPersistentSValWithData(const SVal& V, uintptr_t Data); 189218887Sdim 190218887Sdim const std::pair<SVal, SVal>& 191218887Sdim getPersistentSValPair(const SVal& V1, const SVal& V2); 192218887Sdim 193218887Sdim const SVal* getPersistentSVal(SVal X); 194218887Sdim}; 195218887Sdim 196218887Sdim} // end GR namespace 197218887Sdim 198218887Sdim} // end clang namespace 199218887Sdim 200218887Sdim#endif 201