Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 1 | //== RValues.h - Abstract RValues for Path-Sens. Value Tracking -*- 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 files defines RValue, LValue, and NonLValue, classes that represent |
| 11 | // abstract r-values for use with path-sensitive value tracking. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_ANALYSIS_RVALUE_H |
| 16 | #define LLVM_CLANG_ANALYSIS_RVALUE_H |
| 17 | |
| 18 | // FIXME: reduce the number of includes. |
| 19 | |
| 20 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/ASTContext.h" |
| 24 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 25 | |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/DataTypes.h" |
| 28 | #include "llvm/ADT/APSInt.h" |
| 29 | #include "llvm/ADT/FoldingSet.h" |
| 30 | #include "llvm/ADT/ImmutableMap.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/Support/Allocator.h" |
| 34 | #include "llvm/Support/Compiler.h" |
| 35 | #include "llvm/Support/Streams.h" |
| 36 | |
| 37 | #include <functional> |
| 38 | |
| 39 | //==------------------------------------------------------------------------==// |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 40 | // Values and ValueManager. |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 41 | //==------------------------------------------------------------------------==// |
| 42 | |
| 43 | namespace clang { |
| 44 | |
| 45 | class SymbolID { |
| 46 | unsigned Data; |
| 47 | public: |
| 48 | SymbolID() : Data(~0) {} |
| 49 | SymbolID(unsigned x) : Data(x) {} |
| 50 | |
| 51 | bool isInitialized() const { return Data != (unsigned) ~0; } |
| 52 | operator unsigned() const { assert (isInitialized()); return Data; } |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 54 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 55 | assert (isInitialized()); |
| 56 | ID.AddInteger(Data); |
| 57 | } |
Ted Kremenek | 174aea4 | 2008-02-05 18:51:06 +0000 | [diff] [blame] | 58 | |
| 59 | static inline void Profile(llvm::FoldingSetNodeID& ID, SymbolID X) { |
| 60 | X.Profile(ID); |
| 61 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | class SymbolData { |
| 65 | uintptr_t Data; |
| 66 | public: |
| 67 | enum Kind { ParmKind = 0x0, Mask = 0x3 }; |
| 68 | |
| 69 | SymbolData(ParmVarDecl* D) |
| 70 | : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {} |
| 71 | |
| 72 | inline Kind getKind() const { return (Kind) (Data & Mask); } |
| 73 | inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); } |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 74 | inline bool operator==(const SymbolData& R) const { return Data == R.Data; } |
| 75 | |
| 76 | QualType getType() const; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 77 | }; |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | class SymIntConstraint : public llvm::FoldingSetNode { |
| 81 | SymbolID Symbol; |
| 82 | BinaryOperator::Opcode Op; |
| 83 | const llvm::APSInt& Val; |
| 84 | public: |
| 85 | SymIntConstraint(SymbolID sym, BinaryOperator::Opcode op, |
| 86 | const llvm::APSInt& V) |
| 87 | : Symbol(sym), |
| 88 | Op(op), Val(V) {} |
| 89 | |
| 90 | BinaryOperator::Opcode getOpcode() const { return Op; } |
| 91 | SymbolID getSymbol() const { return Symbol; } |
| 92 | const llvm::APSInt& getInt() const { return Val; } |
| 93 | |
| 94 | static inline void Profile(llvm::FoldingSetNodeID& ID, |
| 95 | const SymbolID& Symbol, |
| 96 | BinaryOperator::Opcode Op, |
| 97 | const llvm::APSInt& Val) { |
| 98 | Symbol.Profile(ID); |
| 99 | ID.AddInteger(Op); |
| 100 | ID.AddPointer(&Val); |
| 101 | } |
| 102 | |
| 103 | void Profile(llvm::FoldingSetNodeID& ID) { |
| 104 | Profile(ID, Symbol, Op, Val); |
| 105 | } |
| 106 | }; |
| 107 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 108 | |
| 109 | class SymbolManager { |
| 110 | std::vector<SymbolData> SymbolToData; |
| 111 | |
| 112 | typedef llvm::DenseMap<void*,SymbolID> MapTy; |
| 113 | MapTy DataToSymbol; |
| 114 | |
| 115 | public: |
| 116 | SymbolManager(); |
| 117 | ~SymbolManager(); |
| 118 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 119 | SymbolID getSymbol(ParmVarDecl* D); |
| 120 | |
| 121 | inline SymbolData getSymbolData(SymbolID ID) const { |
| 122 | assert (ID < SymbolToData.size()); |
| 123 | return SymbolToData[ID]; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 126 | inline QualType getType(SymbolID ID) const { |
| 127 | return getSymbolData(ID).getType(); |
| 128 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 129 | }; |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 131 | |
| 132 | class ValueManager { |
| 133 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> > |
| 134 | APSIntSetTy; |
| 135 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 136 | typedef llvm::FoldingSet<SymIntConstraint> |
| 137 | SymIntCSetTy; |
| 138 | |
| 139 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 140 | ASTContext& Ctx; |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 141 | llvm::BumpPtrAllocator& BPAlloc; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 143 | APSIntSetTy APSIntSet; |
| 144 | SymIntCSetTy SymIntCSet; |
| 145 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 146 | public: |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 147 | ValueManager(ASTContext& ctx, llvm::BumpPtrAllocator& Alloc) |
| 148 | : Ctx(ctx), BPAlloc(Alloc) {} |
| 149 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 150 | ~ValueManager(); |
| 151 | |
| 152 | ASTContext& getContext() const { return Ctx; } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 153 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 154 | const llvm::APSInt& getValue(const llvm::APSInt& X); |
| 155 | const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
| 156 | const llvm::APSInt& getValue(uint64_t X, QualType T, |
| 157 | SourceLocation Loc = SourceLocation()); |
| 158 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 159 | inline const llvm::APSInt& getZeroWithPtrWidth() { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 160 | return getValue( 0, |
| 161 | Ctx.getTypeSize(Ctx.VoidPtrTy, SourceLocation()), |
| 162 | true ); |
| 163 | } |
| 164 | |
| 165 | inline const llvm::APSInt& getTruthValue(bool b) { |
| 166 | return getValue( b ? 1 : 0, |
| 167 | Ctx.getTypeSize(Ctx.IntTy, SourceLocation()), |
| 168 | false ); |
| 169 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 172 | const SymIntConstraint& getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
| 173 | const llvm::APSInt& V); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 174 | }; |
Ted Kremenek | 0f10d50 | 2008-02-05 22:21:54 +0000 | [diff] [blame] | 175 | |
| 176 | } // end clang namespace |
| 177 | |
| 178 | //==------------------------------------------------------------------------==// |
| 179 | // Casting machinery to get cast<> and dyn_cast<> working with SymbolData. |
| 180 | //==------------------------------------------------------------------------==// |
| 181 | |
| 182 | namespace llvm { |
| 183 | |
| 184 | template<> inline bool |
| 185 | isa<clang::ParmVarDecl,clang::SymbolData>(const clang::SymbolData& V) { |
| 186 | return V.getKind() == clang::SymbolData::ParmKind; |
| 187 | } |
| 188 | |
| 189 | template<> struct cast_retty_impl<clang::ParmVarDecl,clang::SymbolData> { |
| 190 | typedef const clang::ParmVarDecl* ret_type; |
| 191 | }; |
| 192 | |
| 193 | template<> struct simplify_type<clang::SymbolData> { |
| 194 | typedef void* SimpleType; |
| 195 | static inline SimpleType getSimplifiedValue(const clang::SymbolData &V) { |
| 196 | return V.getPtr(); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | } // end llvm namespace |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 201 | |
| 202 | //==------------------------------------------------------------------------==// |
| 203 | // Base RValue types. |
| 204 | //==------------------------------------------------------------------------==// |
| 205 | |
Ted Kremenek | 0f10d50 | 2008-02-05 22:21:54 +0000 | [diff] [blame] | 206 | namespace clang { |
| 207 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 208 | class RValue { |
| 209 | public: |
| 210 | enum BaseKind { LValueKind=0x0, |
| 211 | NonLValueKind=0x1, |
| 212 | UninitializedKind=0x2, |
| 213 | InvalidKind=0x3 }; |
| 214 | |
| 215 | enum { BaseBits = 2, |
| 216 | BaseMask = 0x3 }; |
| 217 | |
| 218 | private: |
| 219 | void* Data; |
| 220 | unsigned Kind; |
| 221 | |
| 222 | protected: |
| 223 | RValue(const void* d, bool isLValue, unsigned ValKind) |
| 224 | : Data(const_cast<void*>(d)), |
| 225 | Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {} |
| 226 | |
| 227 | explicit RValue(BaseKind k) |
| 228 | : Data(0), Kind(k) {} |
| 229 | |
| 230 | void* getRawPtr() const { |
| 231 | return reinterpret_cast<void*>(Data); |
| 232 | } |
| 233 | |
| 234 | public: |
| 235 | ~RValue() {}; |
| 236 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 237 | /// BufferTy - A temporary buffer to hold a set of RValues. |
| 238 | typedef llvm::SmallVector<RValue,5> BufferTy; |
| 239 | |
| 240 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 241 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 242 | |
| 243 | unsigned getRawKind() const { return Kind; } |
| 244 | BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); } |
| 245 | unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; } |
| 246 | |
| 247 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 248 | ID.AddInteger((unsigned) getRawKind()); |
| 249 | ID.AddPointer(reinterpret_cast<void*>(Data)); |
| 250 | } |
| 251 | |
| 252 | bool operator==(const RValue& RHS) const { |
| 253 | return getRawKind() == RHS.getRawKind() && Data == RHS.Data; |
| 254 | } |
| 255 | |
| 256 | static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D); |
| 257 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 258 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 259 | inline bool isValid() const { return getRawKind() != InvalidKind; } |
| 260 | inline bool isInvalid() const { return getRawKind() == InvalidKind; } |
| 261 | |
| 262 | void print(std::ostream& OS) const; |
| 263 | void print() const { print(*llvm::cerr.stream()); } |
| 264 | |
| 265 | // Implement isa<T> support. |
| 266 | static inline bool classof(const RValue*) { return true; } |
| 267 | }; |
| 268 | |
| 269 | class InvalidValue : public RValue { |
| 270 | public: |
| 271 | InvalidValue() : RValue(InvalidKind) {} |
| 272 | |
| 273 | static inline bool classof(const RValue* V) { |
| 274 | return V->getBaseKind() == InvalidKind; |
| 275 | } |
| 276 | }; |
| 277 | |
| 278 | class UninitializedValue : public RValue { |
| 279 | public: |
| 280 | UninitializedValue() : RValue(UninitializedKind) {} |
| 281 | |
| 282 | static inline bool classof(const RValue* V) { |
| 283 | return V->getBaseKind() == UninitializedKind; |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | class NonLValue : public RValue { |
| 288 | protected: |
| 289 | NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {} |
| 290 | |
| 291 | public: |
| 292 | void print(std::ostream& Out) const; |
| 293 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 294 | NonLValue EvalBinaryOp(ValueManager& ValMgr, |
| 295 | BinaryOperator::Opcode Op, |
| 296 | const NonLValue& RHS) const; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 298 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
| 299 | NonLValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
| 300 | NonLValue EvalComplement(ValueManager& ValMgr) const; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 302 | // Utility methods to create NonLValues. |
| 303 | static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 304 | SourceLocation Loc = SourceLocation()); |
| 305 | |
| 306 | static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I); |
| 307 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 308 | static NonLValue GetIntTruthValue(ValueManager& ValMgr, bool b); |
| 309 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 310 | // Implement isa<T> support. |
| 311 | static inline bool classof(const RValue* V) { |
| 312 | return V->getBaseKind() >= NonLValueKind; |
| 313 | } |
| 314 | }; |
| 315 | |
| 316 | class LValue : public RValue { |
| 317 | protected: |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 318 | LValue(unsigned SubKind, const void* D) : RValue(const_cast<void*>(D), |
| 319 | true, SubKind) {} |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 321 | // Equality operators. |
| 322 | NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const; |
| 323 | NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const; |
| 324 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 325 | public: |
| 326 | void print(std::ostream& Out) const; |
| 327 | |
| 328 | RValue EvalBinaryOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 329 | const LValue& RHS) const; |
| 330 | |
| 331 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
| 332 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 333 | // Implement isa<T> support. |
| 334 | static inline bool classof(const RValue* V) { |
| 335 | return V->getBaseKind() == LValueKind; |
| 336 | } |
| 337 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 338 | |
| 339 | //==------------------------------------------------------------------------==// |
| 340 | // Subclasses of NonLValue. |
| 341 | //==------------------------------------------------------------------------==// |
| 342 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 343 | namespace nonlval { |
| 344 | |
| 345 | enum Kind { SymbolValKind, |
Ted Kremenek | 9466aa8 | 2008-02-05 22:10:48 +0000 | [diff] [blame] | 346 | SymIntConstraintValKind, |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 347 | ConcreteIntKind, |
| 348 | NumKind }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 350 | class SymbolVal : public NonLValue { |
| 351 | public: |
| 352 | SymbolVal(unsigned SymID) |
| 353 | : NonLValue(SymbolValKind, |
| 354 | reinterpret_cast<void*>((uintptr_t) SymID)) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 355 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 356 | SymbolID getSymbol() const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 357 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 358 | } |
| 359 | |
| 360 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 9466aa8 | 2008-02-05 22:10:48 +0000 | [diff] [blame] | 361 | return isa<NonLValue>(V) && V->getSubKind() == SymbolValKind; |
| 362 | } |
| 363 | }; |
| 364 | |
| 365 | class SymIntConstraintVal : public NonLValue { |
| 366 | public: |
| 367 | SymIntConstraintVal(const SymIntConstraint& C) |
| 368 | : NonLValue(SymIntConstraintValKind, reinterpret_cast<const void*>(&C)) {} |
| 369 | |
| 370 | const SymIntConstraint& getConstraint() const { |
| 371 | return *reinterpret_cast<SymIntConstraint*>(getRawPtr()); |
| 372 | } |
| 373 | |
| 374 | static inline bool classof(const RValue* V) { |
| 375 | return isa<NonLValue>(V) && V->getSubKind() == SymIntConstraintValKind; |
| 376 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | class ConcreteInt : public NonLValue { |
| 380 | public: |
| 381 | ConcreteInt(const llvm::APSInt& V) : NonLValue(ConcreteIntKind, &V) {} |
| 382 | |
| 383 | const llvm::APSInt& getValue() const { |
| 384 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 385 | } |
| 386 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 387 | // Transfer functions for binary/unary operations on ConcreteInts. |
| 388 | ConcreteInt EvalBinaryOp(ValueManager& ValMgr, |
| 389 | BinaryOperator::Opcode Op, |
| 390 | const ConcreteInt& RHS) const; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 392 | ConcreteInt EvalComplement(ValueManager& ValMgr) const; |
| 393 | ConcreteInt EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 394 | |
| 395 | // Implement isa<T> support. |
| 396 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 9466aa8 | 2008-02-05 22:10:48 +0000 | [diff] [blame] | 397 | return isa<NonLValue>(V) && V->getSubKind() == ConcreteIntKind; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 398 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 399 | |
| 400 | static inline bool classof(const NonLValue* V) { |
| 401 | return V->getSubKind() == ConcreteIntKind; |
| 402 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 403 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 405 | } // end namespace clang::nonlval |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 406 | |
| 407 | //==------------------------------------------------------------------------==// |
| 408 | // Subclasses of LValue. |
| 409 | //==------------------------------------------------------------------------==// |
| 410 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 411 | namespace lval { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 413 | enum Kind { SymbolValKind, |
| 414 | DeclValKind, |
| 415 | ConcreteIntKind, |
| 416 | NumKind }; |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 418 | class SymbolVal : public LValue { |
| 419 | public: |
| 420 | SymbolVal(unsigned SymID) |
| 421 | : LValue(SymbolValKind, reinterpret_cast<void*>((uintptr_t) SymID)) {} |
| 422 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 423 | SymbolID getSymbol() const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 424 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 425 | } |
| 426 | |
| 427 | static inline bool classof(const RValue* V) { |
| 428 | return V->getSubKind() == SymbolValKind; |
| 429 | } |
| 430 | }; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 432 | class DeclVal : public LValue { |
| 433 | public: |
| 434 | DeclVal(const ValueDecl* vd) : LValue(DeclValKind,vd) {} |
| 435 | |
| 436 | ValueDecl* getDecl() const { |
| 437 | return static_cast<ValueDecl*>(getRawPtr()); |
| 438 | } |
| 439 | |
| 440 | inline bool operator==(const DeclVal& R) const { |
| 441 | return getDecl() == R.getDecl(); |
| 442 | } |
| 443 | |
| 444 | inline bool operator!=(const DeclVal& R) const { |
| 445 | return getDecl() != R.getDecl(); |
| 446 | } |
| 447 | |
| 448 | // Implement isa<T> support. |
| 449 | static inline bool classof(const RValue* V) { |
| 450 | return V->getSubKind() == DeclValKind; |
| 451 | } |
| 452 | }; |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 453 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 454 | class ConcreteInt : public LValue { |
| 455 | public: |
| 456 | ConcreteInt(const llvm::APSInt& V) : LValue(ConcreteIntKind, &V) {} |
| 457 | |
| 458 | const llvm::APSInt& getValue() const { |
| 459 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 460 | } |
| 461 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 462 | |
| 463 | // Transfer functions for binary/unary operations on ConcreteInts. |
| 464 | ConcreteInt EvalBinaryOp(ValueManager& ValMgr, |
| 465 | BinaryOperator::Opcode Op, |
| 466 | const ConcreteInt& RHS) const; |
| 467 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 468 | // Implement isa<T> support. |
| 469 | static inline bool classof(const RValue* V) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 470 | return isa<LValue>(V) && V->getSubKind() == ConcreteIntKind; |
| 471 | } |
| 472 | |
| 473 | static inline bool classof(const LValue* V) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 474 | return V->getSubKind() == ConcreteIntKind; |
| 475 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame^] | 476 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 477 | }; |
| 478 | } // end clang::lval namespace |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 479 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 480 | |
| 481 | } // end clang namespace |
| 482 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 483 | #endif |