blob: c5ad8a5327e7500cba53713126145f775bc870e7 [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//== 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//==------------------------------------------------------------------------==//
40// RValue "management" data structures.
41//==------------------------------------------------------------------------==//
42
43namespace clang {
44
45class SymbolID {
46 unsigned Data;
47public:
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; }
53};
54
55class SymbolData {
56 uintptr_t Data;
57public:
58 enum Kind { ParmKind = 0x0, Mask = 0x3 };
59
60 SymbolData(ParmVarDecl* D)
61 : Data(reinterpret_cast<uintptr_t>(D) | ParmKind) {}
62
63 inline Kind getKind() const { return (Kind) (Data & Mask); }
64 inline void* getPtr() const { return reinterpret_cast<void*>(Data & ~Mask); }
65 inline bool operator==(const SymbolData& R) const { return Data == R.Data; }
66};
67
68class SymbolManager {
69 std::vector<SymbolData> SymbolToData;
70
71 typedef llvm::DenseMap<void*,SymbolID> MapTy;
72 MapTy DataToSymbol;
73
74public:
75 SymbolManager();
76 ~SymbolManager();
77
78 SymbolData getSymbolData(SymbolID id) const {
79 assert (id < SymbolToData.size());
80 return SymbolToData[id];
81 }
82
83 SymbolID getSymbol(ParmVarDecl* D);
84};
85
86class ValueManager {
87 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<llvm::APSInt> >
88 APSIntSetTy;
89
90 ASTContext& Ctx;
91 APSIntSetTy APSIntSet;
92 llvm::BumpPtrAllocator BPAlloc;
93
94public:
95 ValueManager(ASTContext& ctx) : Ctx(ctx) {}
96 ~ValueManager();
97
98 ASTContext& getContext() const { return Ctx; }
99 llvm::APSInt& getValue(const llvm::APSInt& X);
100 llvm::APSInt& getValue(uint64_t X, unsigned BitWidth, bool isUnsigned);
101 llvm::APSInt& getValue(uint64_t X, QualType T,
102 SourceLocation Loc = SourceLocation());
103};
104
105//==------------------------------------------------------------------------==//
106// Base RValue types.
107//==------------------------------------------------------------------------==//
108
109class RValue {
110public:
111 enum BaseKind { LValueKind=0x0,
112 NonLValueKind=0x1,
113 UninitializedKind=0x2,
114 InvalidKind=0x3 };
115
116 enum { BaseBits = 2,
117 BaseMask = 0x3 };
118
119private:
120 void* Data;
121 unsigned Kind;
122
123protected:
124 RValue(const void* d, bool isLValue, unsigned ValKind)
125 : Data(const_cast<void*>(d)),
126 Kind((isLValue ? LValueKind : NonLValueKind) | (ValKind << BaseBits)) {}
127
128 explicit RValue(BaseKind k)
129 : Data(0), Kind(k) {}
130
131 void* getRawPtr() const {
132 return reinterpret_cast<void*>(Data);
133 }
134
135public:
136 ~RValue() {};
137
138 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
139
140 unsigned getRawKind() const { return Kind; }
141 BaseKind getBaseKind() const { return (BaseKind) (Kind & BaseMask); }
142 unsigned getSubKind() const { return (Kind & ~BaseMask) >> BaseBits; }
143
144 void Profile(llvm::FoldingSetNodeID& ID) const {
145 ID.AddInteger((unsigned) getRawKind());
146 ID.AddPointer(reinterpret_cast<void*>(Data));
147 }
148
149 bool operator==(const RValue& RHS) const {
150 return getRawKind() == RHS.getRawKind() && Data == RHS.Data;
151 }
152
153 static RValue GetSymbolValue(SymbolManager& SymMgr, ParmVarDecl *D);
154
155 inline bool isValid() const { return getRawKind() != InvalidKind; }
156 inline bool isInvalid() const { return getRawKind() == InvalidKind; }
157
158 void print(std::ostream& OS) const;
159 void print() const { print(*llvm::cerr.stream()); }
160
161 // Implement isa<T> support.
162 static inline bool classof(const RValue*) { return true; }
163};
164
165class InvalidValue : public RValue {
166public:
167 InvalidValue() : RValue(InvalidKind) {}
168
169 static inline bool classof(const RValue* V) {
170 return V->getBaseKind() == InvalidKind;
171 }
172};
173
174class UninitializedValue : public RValue {
175public:
176 UninitializedValue() : RValue(UninitializedKind) {}
177
178 static inline bool classof(const RValue* V) {
179 return V->getBaseKind() == UninitializedKind;
180 }
181};
182
183class NonLValue : public RValue {
184protected:
185 NonLValue(unsigned SubKind, const void* d) : RValue(d, false, SubKind) {}
186
187public:
188 void print(std::ostream& Out) const;
189
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000190 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
191
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000192 // Arithmetic operators.
193 NonLValue Add(ValueManager& ValMgr, const NonLValue& RHS) const;
194 NonLValue Sub(ValueManager& ValMgr, const NonLValue& RHS) const;
195 NonLValue Mul(ValueManager& ValMgr, const NonLValue& RHS) const;
196 NonLValue Div(ValueManager& ValMgr, const NonLValue& RHS) const;
197 NonLValue Rem(ValueManager& ValMgr, const NonLValue& RHS) const;
198 NonLValue UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const;
199
200 // Equality operators.
201 NonLValue EQ(ValueManager& ValMgr, const NonLValue& RHS) const;
202 NonLValue NE(ValueManager& ValMgr, const NonLValue& RHS) const;
203
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000204
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000205 // Utility methods to create NonLValues.
206 static NonLValue GetValue(ValueManager& ValMgr, uint64_t X, QualType T,
207 SourceLocation Loc = SourceLocation());
208
209 static NonLValue GetValue(ValueManager& ValMgr, IntegerLiteral* I);
210
211 static inline NonLValue GetIntTruthValue(ValueManager& ValMgr, bool X) {
212 return GetValue(ValMgr, X ? 1U : 0U, ValMgr.getContext().IntTy);
213 }
214
215 // Implement isa<T> support.
216 static inline bool classof(const RValue* V) {
217 return V->getBaseKind() >= NonLValueKind;
218 }
219};
220
221class LValue : public RValue {
222protected:
Ted Kremenek516f91b2008-01-31 22:17:03 +0000223 LValue(unsigned SubKind, const void* D) : RValue(const_cast<void*>(D),
224 true, SubKind) {}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000225
226public:
227 void print(std::ostream& Out) const;
228
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000229 RValue Cast(ValueManager& ValMgr, Expr* CastExpr) const;
230
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000231 // Equality operators.
232 NonLValue EQ(ValueManager& ValMgr, const LValue& RHS) const;
233 NonLValue NE(ValueManager& ValMgr, const LValue& RHS) const;
234
235 // Implement isa<T> support.
236 static inline bool classof(const RValue* V) {
237 return V->getBaseKind() == LValueKind;
238 }
239};
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000240
241//==------------------------------------------------------------------------==//
242// Subclasses of NonLValue.
243//==------------------------------------------------------------------------==//
244
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000245enum NonLValueKind { SymbolicNonLValueKind, ConcreteIntKind, NumNonLValueKind };
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000246
247class SymbolicNonLValue : public NonLValue {
248public:
249 SymbolicNonLValue(unsigned SymID)
250 : NonLValue(SymbolicNonLValueKind,
251 reinterpret_cast<void*>((uintptr_t) SymID)) {}
252
253 SymbolID getSymbolID() const {
254 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
255 }
256
257 static inline bool classof(const RValue* V) {
258 return V->getSubKind() == SymbolicNonLValueKind;
259 }
260};
261
262class ConcreteInt : public NonLValue {
263public:
264 ConcreteInt(const llvm::APSInt& V) : NonLValue(ConcreteIntKind, &V) {}
265
266 const llvm::APSInt& getValue() const {
267 return *static_cast<llvm::APSInt*>(getRawPtr());
268 }
269
270 // Arithmetic operators.
271
272 ConcreteInt Add(ValueManager& ValMgr, const ConcreteInt& V) const {
273 return ValMgr.getValue(getValue() + V.getValue());
274 }
275
276 ConcreteInt Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
277 return ValMgr.getValue(getValue() - V.getValue());
278 }
279
280 ConcreteInt Mul(ValueManager& ValMgr, const ConcreteInt& V) const {
281 return ValMgr.getValue(getValue() * V.getValue());
282 }
283
284 ConcreteInt Div(ValueManager& ValMgr, const ConcreteInt& V) const {
285 return ValMgr.getValue(getValue() / V.getValue());
286 }
287
288 ConcreteInt Rem(ValueManager& ValMgr, const ConcreteInt& V) const {
289 return ValMgr.getValue(getValue() % V.getValue());
290 }
291
292 ConcreteInt UnaryMinus(ValueManager& ValMgr, UnaryOperator* U) const {
293 assert (U->getType() == U->getSubExpr()->getType());
294 assert (U->getType()->isIntegerType());
295 return ValMgr.getValue(-getValue());
296 }
297
298 // Casting.
299
300 ConcreteInt Cast(ValueManager& ValMgr, Expr* CastExpr) const {
301 assert (CastExpr->getType()->isIntegerType());
302
303 llvm::APSInt X(getValue());
304 X.extOrTrunc(ValMgr.getContext().getTypeSize(CastExpr->getType(),
305 CastExpr->getLocStart()));
306 return ValMgr.getValue(X);
307 }
308
309 // Equality operators.
310
311 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteInt& V) const {
312 const llvm::APSInt& Val = getValue();
313 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
314 Val.getBitWidth(), Val.isUnsigned());
315 }
316
317 ConcreteInt NE(ValueManager& ValMgr, const ConcreteInt& V) const {
318 const llvm::APSInt& Val = getValue();
319 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
320 Val.getBitWidth(), Val.isUnsigned());
321 }
322
323 // Implement isa<T> support.
324 static inline bool classof(const RValue* V) {
325 return V->getSubKind() == ConcreteIntKind;
326 }
327};
Ted Kremenek516f91b2008-01-31 22:17:03 +0000328
329//==------------------------------------------------------------------------==//
330// Subclasses of LValue.
331//==------------------------------------------------------------------------==//
332
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000333enum LValueKind { SymbolicLValueKind, LValueDeclKind, ConcreteIntLValueKind,
334 NumLValueKind };
335
Ted Kremenek516f91b2008-01-31 22:17:03 +0000336class SymbolicLValue : public LValue {
337public:
338 SymbolicLValue(unsigned SymID)
339 : LValue(SymbolicLValueKind, reinterpret_cast<void*>((uintptr_t) SymID)) {}
340
341 SymbolID getSymbolID() const {
342 return (SymbolID) reinterpret_cast<uintptr_t>(getRawPtr());
343 }
344
345 static inline bool classof(const RValue* V) {
346 return V->getSubKind() == SymbolicLValueKind;
347 }
348};
349
350class LValueDecl : public LValue {
351public:
352 LValueDecl(const ValueDecl* vd) : LValue(LValueDeclKind,vd) {}
353
354 ValueDecl* getDecl() const {
355 return static_cast<ValueDecl*>(getRawPtr());
356 }
357
358 inline bool operator==(const LValueDecl& R) const {
359 return getDecl() == R.getDecl();
360 }
361
362 inline bool operator!=(const LValueDecl& R) const {
363 return getDecl() != R.getDecl();
364 }
365
366 // Implement isa<T> support.
367 static inline bool classof(const RValue* V) {
368 return V->getSubKind() == LValueDeclKind;
369 }
370};
371
372class ConcreteIntLValue : public LValue {
373public:
374 ConcreteIntLValue(const llvm::APSInt& V) : LValue(ConcreteIntLValueKind, &V) {}
375
376 const llvm::APSInt& getValue() const {
377 return *static_cast<llvm::APSInt*>(getRawPtr());
378 }
379
380 // Arithmetic operators.
381
382 ConcreteIntLValue Add(ValueManager& ValMgr, const ConcreteInt& V) const {
383 return ValMgr.getValue(getValue() + V.getValue());
384 }
385
386 ConcreteIntLValue Sub(ValueManager& ValMgr, const ConcreteInt& V) const {
387 return ValMgr.getValue(getValue() - V.getValue());
388 }
389
390 // Equality operators.
391
392 ConcreteInt EQ(ValueManager& ValMgr, const ConcreteIntLValue& V) const {
393 const llvm::APSInt& Val = getValue();
394 return ValMgr.getValue(Val == V.getValue() ? 1U : 0U,
395 Val.getBitWidth(), Val.isUnsigned());
396 }
397
398 ConcreteInt NE(ValueManager& ValMgr, const ConcreteIntLValue& V) const {
399 const llvm::APSInt& Val = getValue();
400 return ValMgr.getValue(Val != V.getValue() ? 1U : 0U,
401 Val.getBitWidth(), Val.isUnsigned());
402 }
403
404 // Implement isa<T> support.
405 static inline bool classof(const RValue* V) {
406 return V->getSubKind() == ConcreteIntLValueKind;
407 }
408};
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000409
410} // end clang namespace
411
412//==------------------------------------------------------------------------==//
413// Casting machinery to get cast<> and dyn_cast<> working with SymbolData.
414//==------------------------------------------------------------------------==//
415
416namespace llvm {
417
418template<> inline bool
419isa<clang::ParmVarDecl,clang::SymbolData>(const clang::SymbolData& V) {
420 return V.getKind() == clang::SymbolData::ParmKind;
421}
422
423template<> struct cast_retty_impl<clang::ParmVarDecl,clang::SymbolData> {
424 typedef const clang::ParmVarDecl* ret_type;
425};
426
427template<> struct simplify_type<clang::SymbolData> {
428 typedef void* SimpleType;
429 static inline SimpleType getSimplifiedValue(const clang::SymbolData &V) {
430 return V.getPtr();
431 }
432};
433
434} // end llvm namespace
435
436#endif