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 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 64 | // SymbolData: Used to record meta data about symbols. |
| 65 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 66 | class SymbolData { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 67 | public: |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 68 | enum Kind { UninitKind, ParmKind, ContentsOfKind }; |
| 69 | |
| 70 | private: |
| 71 | uintptr_t Data; |
| 72 | Kind K; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 74 | protected: |
| 75 | SymbolData(uintptr_t D, Kind k) : Data(D), K(k) {} |
| 76 | SymbolData(void* D, Kind k) : Data(reinterpret_cast<uintptr_t>(D)), K(k) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 78 | void* getPtr() const { |
| 79 | assert (K != UninitKind); |
| 80 | return reinterpret_cast<void*>(Data); |
| 81 | } |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 83 | uintptr_t getInt() const { |
| 84 | assert (K != UninitKind); |
| 85 | return Data; |
| 86 | } |
| 87 | |
| 88 | public: |
| 89 | SymbolData() : Data(0), K(UninitKind) {} |
| 90 | |
| 91 | Kind getKind() const { return K; } |
| 92 | |
| 93 | inline bool operator==(const SymbolData& R) const { |
| 94 | return K == R.K && Data == R.Data; |
| 95 | } |
| 96 | |
| 97 | QualType getType() const; |
| 98 | |
| 99 | // Implement isa<T> support. |
| 100 | static inline bool classof(const SymbolData*) { return true; } |
| 101 | }; |
| 102 | |
| 103 | class SymbolDataParmVar : public SymbolData { |
| 104 | public: |
| 105 | SymbolDataParmVar(ParmVarDecl* VD) : SymbolData(VD, ParmKind) {} |
| 106 | |
| 107 | ParmVarDecl* getDecl() const { return (ParmVarDecl*) getPtr(); } |
| 108 | |
| 109 | // Implement isa<T> support. |
| 110 | static inline bool classof(const SymbolData* D) { |
| 111 | return D->getKind() == ParmKind; |
| 112 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 113 | }; |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 115 | class SymbolDataContentsOf : public SymbolData { |
| 116 | public: |
| 117 | SymbolDataContentsOf(SymbolID ID) : SymbolData(ID, ContentsOfKind) {} |
| 118 | |
| 119 | SymbolID getSymbol() const { return (SymbolID) getInt(); } |
| 120 | |
| 121 | // Implement isa<T> support. |
| 122 | static inline bool classof(const SymbolData* D) { |
| 123 | return D->getKind() == ContentsOfKind; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // Constraints on symbols. Usually wrapped by RValues. |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 128 | |
| 129 | class SymIntConstraint : public llvm::FoldingSetNode { |
| 130 | SymbolID Symbol; |
| 131 | BinaryOperator::Opcode Op; |
| 132 | const llvm::APSInt& Val; |
| 133 | public: |
| 134 | SymIntConstraint(SymbolID sym, BinaryOperator::Opcode op, |
| 135 | const llvm::APSInt& V) |
| 136 | : Symbol(sym), |
| 137 | Op(op), Val(V) {} |
| 138 | |
| 139 | BinaryOperator::Opcode getOpcode() const { return Op; } |
| 140 | SymbolID getSymbol() const { return Symbol; } |
| 141 | const llvm::APSInt& getInt() const { return Val; } |
| 142 | |
| 143 | static inline void Profile(llvm::FoldingSetNodeID& ID, |
| 144 | const SymbolID& Symbol, |
| 145 | BinaryOperator::Opcode Op, |
| 146 | const llvm::APSInt& Val) { |
| 147 | Symbol.Profile(ID); |
| 148 | ID.AddInteger(Op); |
| 149 | ID.AddPointer(&Val); |
| 150 | } |
| 151 | |
| 152 | void Profile(llvm::FoldingSetNodeID& ID) { |
| 153 | Profile(ID, Symbol, Op, Val); |
| 154 | } |
| 155 | }; |
| 156 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 157 | |
| 158 | class SymbolManager { |
| 159 | std::vector<SymbolData> SymbolToData; |
| 160 | |
| 161 | typedef llvm::DenseMap<void*,SymbolID> MapTy; |
| 162 | MapTy DataToSymbol; |
| 163 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 164 | void* getKey(void* P) const { |
| 165 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(P) | 0x1); |
| 166 | } |
| 167 | |
| 168 | void* getKey(SymbolID sym) const { |
| 169 | return reinterpret_cast<void*>((uintptr_t) (sym << 1)); |
| 170 | } |
| 171 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 172 | public: |
| 173 | SymbolManager(); |
| 174 | ~SymbolManager(); |
| 175 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 176 | SymbolID getSymbol(ParmVarDecl* D); |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 177 | SymbolID getContentsOfSymbol(SymbolID sym); |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 179 | inline const SymbolData& getSymbolData(SymbolID ID) const { |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 180 | assert (ID < SymbolToData.size()); |
| 181 | return SymbolToData[ID]; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ted Kremenek | feb01f6 | 2008-02-06 17:32:17 +0000 | [diff] [blame] | 184 | inline QualType getType(SymbolID ID) const { |
| 185 | return getSymbolData(ID).getType(); |
| 186 | } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 187 | }; |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 189 | |
| 190 | class ValueManager { |
| 191 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> > |
| 192 | APSIntSetTy; |
| 193 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 194 | typedef llvm::FoldingSet<SymIntConstraint> |
| 195 | SymIntCSetTy; |
| 196 | |
| 197 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 198 | ASTContext& Ctx; |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 199 | llvm::BumpPtrAllocator& BPAlloc; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 201 | APSIntSetTy APSIntSet; |
| 202 | SymIntCSetTy SymIntCSet; |
| 203 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 204 | public: |
Ted Kremenek | 768ad16 | 2008-02-05 05:15:51 +0000 | [diff] [blame] | 205 | ValueManager(ASTContext& ctx, llvm::BumpPtrAllocator& Alloc) |
| 206 | : Ctx(ctx), BPAlloc(Alloc) {} |
| 207 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 208 | ~ValueManager(); |
| 209 | |
| 210 | ASTContext& getContext() const { return Ctx; } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 212 | const llvm::APSInt& getValue(const llvm::APSInt& X); |
| 213 | const llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned); |
| 214 | const llvm::APSInt& getValue(uint64_t X, QualType T, |
| 215 | SourceLocation Loc = SourceLocation()); |
| 216 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 217 | inline const llvm::APSInt& getZeroWithPtrWidth() { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 218 | return getValue( 0, |
| 219 | Ctx.getTypeSize(Ctx.VoidPtrTy, SourceLocation()), |
| 220 | true ); |
| 221 | } |
| 222 | |
| 223 | inline const llvm::APSInt& getTruthValue(bool b) { |
| 224 | return getValue( b ? 1 : 0, |
| 225 | Ctx.getTypeSize(Ctx.IntTy, SourceLocation()), |
| 226 | false ); |
| 227 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Ted Kremenek | 1fbdb02 | 2008-02-05 21:32:43 +0000 | [diff] [blame] | 230 | const SymIntConstraint& getConstraint(SymbolID sym, BinaryOperator::Opcode Op, |
| 231 | const llvm::APSInt& V); |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 232 | }; |
Ted Kremenek | 0f10d50 | 2008-02-05 22:21:54 +0000 | [diff] [blame] | 233 | |
| 234 | } // end clang namespace |
| 235 | |
| 236 | //==------------------------------------------------------------------------==// |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 237 | // Base RValue types. |
| 238 | //==------------------------------------------------------------------------==// |
| 239 | |
Ted Kremenek | 0f10d50 | 2008-02-05 22:21:54 +0000 | [diff] [blame] | 240 | namespace clang { |
| 241 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 242 | class RValue { |
| 243 | public: |
| 244 | enum BaseKind { LValueKind=0x0, |
| 245 | NonLValueKind=0x1, |
| 246 | UninitializedKind=0x2, |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 247 | UnknownKind=0x3 }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 248 | |
| 249 | enum { BaseBits = 2, |
| 250 | BaseMask = 0x3 }; |
| 251 | |
| 252 | private: |
| 253 | void* Data; |
| 254 | unsigned Kind; |
| 255 | |
| 256 | protected: |
| 257 | RValue(const void* d, bool isLValue, unsigned ValKind) |
| 258 | : Data(const_cast<void*>(d)), |
| 259 | Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {} |
| 260 | |
| 261 | explicit RValue(BaseKind k) |
| 262 | : Data(0), Kind(k) {} |
| 263 | |
| 264 | void* getRawPtr() const { |
| 265 | return reinterpret_cast<void*>(Data); |
| 266 | } |
| 267 | |
| 268 | public: |
| 269 | ~RValue() {}; |
| 270 | |
Ted Kremenek | cba2e43 | 2008-02-05 19:35:18 +0000 | [diff] [blame] | 271 | /// BufferTy - A temporary buffer to hold a set of RValues. |
| 272 | typedef llvm::SmallVector<RValue,5> BufferTy; |
| 273 | |
| 274 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 275 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 276 | |
| 277 | unsigned getRawKind() const { return Kind; } |
| 278 | BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); } |
| 279 | unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; } |
| 280 | |
| 281 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 282 | ID.AddInteger((unsigned) getRawKind()); |
| 283 | ID.AddPointer(reinterpret_cast<void*>(Data)); |
| 284 | } |
| 285 | |
| 286 | bool operator==(const RValue& RHS) const { |
| 287 | return getRawKind() == RHS.getRawKind() && Data == RHS.Data; |
| 288 | } |
| 289 | |
| 290 | static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D); |
| 291 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 293 | inline bool isKnown() const { return getRawKind() != UnknownKind; } |
| 294 | inline bool isUnknown() const { return getRawKind() == UnknownKind; } |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 295 | |
| 296 | void print(std::ostream& OS) const; |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 297 | void print() const; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 298 | |
| 299 | // Implement isa<T> support. |
| 300 | static inline bool classof(const RValue*) { return true; } |
| 301 | }; |
| 302 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 303 | class UnknownVal : public RValue { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 304 | public: |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 305 | UnknownVal() : RValue(UnknownKind) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 306 | |
| 307 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 308 | return V->getBaseKind() == UnknownKind; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 309 | } |
| 310 | }; |
| 311 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 312 | class UninitializedVal : public RValue { |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 313 | public: |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 314 | UninitializedVal() : RValue(UninitializedKind) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 315 | |
| 316 | static inline bool classof(const RValue* V) { |
| 317 | return V->getBaseKind() == UninitializedKind; |
| 318 | } |
| 319 | }; |
| 320 | |
| 321 | class NonLValue : public RValue { |
| 322 | protected: |
| 323 | NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {} |
| 324 | |
| 325 | public: |
| 326 | void print(std::ostream& Out) const; |
| 327 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 328 | NonLValue EvalBinaryOp(ValueManager& ValMgr, |
| 329 | BinaryOperator::Opcode Op, |
| 330 | const NonLValue& RHS) const; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 331 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 332 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
| 333 | NonLValue EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
| 334 | NonLValue EvalComplement(ValueManager& ValMgr) const; |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 336 | // Utility methods to create NonLValues. |
| 337 | static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T, |
| 338 | SourceLocation Loc = SourceLocation()); |
| 339 | |
| 340 | static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I); |
| 341 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 342 | static NonLValue GetIntTruthValue(ValueManager& ValMgr, bool b); |
| 343 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 344 | // Implement isa<T> support. |
| 345 | static inline bool classof(const RValue* V) { |
| 346 | return V->getBaseKind() >= NonLValueKind; |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | class LValue : public RValue { |
| 351 | protected: |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 352 | LValue(unsigned SubKind, const void* D) : RValue(const_cast<void*>(D), |
| 353 | true, SubKind) {} |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 355 | // Equality operators. |
| 356 | NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const; |
| 357 | NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const; |
| 358 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 359 | public: |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 360 | void print(std::ostream& Out) const; |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 361 | |
| 362 | RValue EvalBinaryOp(ValueManager& ValMgr, BinaryOperator::Opcode Op, |
| 363 | const LValue& RHS) const; |
| 364 | |
| 365 | RValue EvalCast(ValueManager& ValMgr, Expr* CastExpr) const; |
| 366 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 367 | static LValue GetValue(AddrLabelExpr* E); |
| 368 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 369 | // Implement isa<T> support. |
| 370 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 371 | return V->getBaseKind() != NonLValueKind; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 372 | } |
| 373 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 374 | |
| 375 | //==------------------------------------------------------------------------==// |
| 376 | // Subclasses of NonLValue. |
| 377 | //==------------------------------------------------------------------------==// |
| 378 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 379 | namespace nonlval { |
| 380 | |
| 381 | enum Kind { SymbolValKind, |
Ted Kremenek | 9466aa8 | 2008-02-05 22:10:48 +0000 | [diff] [blame] | 382 | SymIntConstraintValKind, |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 383 | ConcreteIntKind, |
| 384 | NumKind }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 385 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 386 | class SymbolVal : public NonLValue { |
| 387 | public: |
| 388 | SymbolVal(unsigned SymID) |
| 389 | : NonLValue(SymbolValKind, |
| 390 | reinterpret_cast<void*>((uintptr_t) SymID)) {} |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 392 | SymbolID getSymbol() const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 393 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 394 | } |
| 395 | |
| 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() == SymbolValKind; |
| 398 | } |
| 399 | }; |
| 400 | |
| 401 | class SymIntConstraintVal : public NonLValue { |
| 402 | public: |
| 403 | SymIntConstraintVal(const SymIntConstraint& C) |
| 404 | : NonLValue(SymIntConstraintValKind, reinterpret_cast<const void*>(&C)) {} |
| 405 | |
| 406 | const SymIntConstraint& getConstraint() const { |
| 407 | return *reinterpret_cast<SymIntConstraint*>(getRawPtr()); |
| 408 | } |
| 409 | |
| 410 | static inline bool classof(const RValue* V) { |
| 411 | return isa<NonLValue>(V) && V->getSubKind() == SymIntConstraintValKind; |
| 412 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | class ConcreteInt : public NonLValue { |
| 416 | public: |
| 417 | ConcreteInt(const llvm::APSInt& V) : NonLValue(ConcreteIntKind, &V) {} |
| 418 | |
| 419 | const llvm::APSInt& getValue() const { |
| 420 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 421 | } |
| 422 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 423 | // Transfer functions for binary/unary operations on ConcreteInts. |
| 424 | ConcreteInt EvalBinaryOp(ValueManager& ValMgr, |
| 425 | BinaryOperator::Opcode Op, |
| 426 | const ConcreteInt& RHS) const; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 428 | ConcreteInt EvalComplement(ValueManager& ValMgr) const; |
| 429 | ConcreteInt EvalMinus(ValueManager& ValMgr, UnaryOperator* U) const; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 430 | |
| 431 | // Implement isa<T> support. |
| 432 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 9466aa8 | 2008-02-05 22:10:48 +0000 | [diff] [blame] | 433 | return isa<NonLValue>(V) && V->getSubKind() == ConcreteIntKind; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 434 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 435 | |
| 436 | static inline bool classof(const NonLValue* V) { |
| 437 | return V->getSubKind() == ConcreteIntKind; |
| 438 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 439 | }; |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 441 | } // end namespace clang::nonlval |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 442 | |
| 443 | //==------------------------------------------------------------------------==// |
| 444 | // Subclasses of LValue. |
| 445 | //==------------------------------------------------------------------------==// |
| 446 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 447 | namespace lval { |
Ted Kremenek | a6e4d21 | 2008-02-01 06:36:40 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 449 | enum Kind { SymbolValKind, |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 450 | GotoLabelKind, |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 451 | DeclValKind, |
| 452 | ConcreteIntKind, |
| 453 | NumKind }; |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 455 | class SymbolVal : public LValue { |
| 456 | public: |
| 457 | SymbolVal(unsigned SymID) |
| 458 | : LValue(SymbolValKind, reinterpret_cast<void*>((uintptr_t) SymID)) {} |
| 459 | |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 460 | SymbolID getSymbol() const { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 461 | return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr()); |
| 462 | } |
| 463 | |
| 464 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 8158a0e | 2008-02-11 23:12:59 +0000 | [diff] [blame] | 465 | return isa<LValue>(V) && V->getSubKind() == SymbolValKind; |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static inline bool classof(const LValue* V) { |
| 469 | return V->getSubKind() == SymbolValKind; |
| 470 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 471 | }; |
Ted Kremenek | 0806acf | 2008-02-05 23:08:41 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 473 | class GotoLabel : public LValue { |
| 474 | public: |
| 475 | GotoLabel(LabelStmt* Label) : LValue(GotoLabelKind, Label) {} |
| 476 | |
| 477 | LabelStmt* getLabel() const { |
| 478 | return static_cast<LabelStmt*>(getRawPtr()); |
| 479 | } |
| 480 | |
| 481 | static inline bool classof(const RValue* V) { |
| 482 | return isa<LValue>(V) && V->getSubKind() == GotoLabelKind; |
| 483 | } |
| 484 | |
| 485 | static inline bool classof(const LValue* V) { |
| 486 | return V->getSubKind() == GotoLabelKind; |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 491 | class DeclVal : public LValue { |
| 492 | public: |
| 493 | DeclVal(const ValueDecl* vd) : LValue(DeclValKind,vd) {} |
| 494 | |
| 495 | ValueDecl* getDecl() const { |
| 496 | return static_cast<ValueDecl*>(getRawPtr()); |
| 497 | } |
| 498 | |
| 499 | inline bool operator==(const DeclVal& R) const { |
| 500 | return getDecl() == R.getDecl(); |
| 501 | } |
| 502 | |
| 503 | inline bool operator!=(const DeclVal& R) const { |
| 504 | return getDecl() != R.getDecl(); |
| 505 | } |
| 506 | |
| 507 | // Implement isa<T> support. |
| 508 | static inline bool classof(const RValue* V) { |
Ted Kremenek | 8158a0e | 2008-02-11 23:12:59 +0000 | [diff] [blame] | 509 | return isa<LValue>(V) && V->getSubKind() == DeclValKind; |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 510 | } |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 511 | |
| 512 | static inline bool classof(const LValue* V) { |
| 513 | return V->getSubKind() == DeclValKind; |
| 514 | } |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 515 | }; |
Ted Kremenek | 516f91b | 2008-01-31 22:17:03 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 517 | class ConcreteInt : public LValue { |
| 518 | public: |
| 519 | ConcreteInt(const llvm::APSInt& V) : LValue(ConcreteIntKind, &V) {} |
| 520 | |
| 521 | const llvm::APSInt& getValue() const { |
| 522 | return *static_cast<llvm::APSInt*>(getRawPtr()); |
| 523 | } |
| 524 | |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 525 | |
| 526 | // Transfer functions for binary/unary operations on ConcreteInts. |
| 527 | ConcreteInt EvalBinaryOp(ValueManager& ValMgr, |
| 528 | BinaryOperator::Opcode Op, |
| 529 | const ConcreteInt& RHS) const; |
| 530 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 531 | // Implement isa<T> support. |
| 532 | static inline bool classof(const RValue* V) { |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 533 | return isa<LValue>(V) && V->getSubKind() == ConcreteIntKind; |
| 534 | } |
| 535 | |
| 536 | static inline bool classof(const LValue* V) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 537 | return V->getSubKind() == ConcreteIntKind; |
| 538 | } |
Ted Kremenek | cf78b6a | 2008-02-06 22:50:25 +0000 | [diff] [blame] | 539 | |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 540 | }; |
| 541 | } // end clang::lval namespace |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 542 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 543 | } // end clang namespace |
| 544 | |
Ted Kremenek | a90ccfe | 2008-01-31 19:34:24 +0000 | [diff] [blame] | 545 | #endif |