From 83cfc5db5e014a04247d8e496524bb6f43e10617 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Thu, 4 Jun 2009 02:53:24 +0000 Subject: Implement simplify_type for ref<> - This allows dyn_cast<> and friends to be applied to a ref. For example, if (ConstantExpr *CE = dyn_cast(foo)) { ... do something with CE ... } - This makes working with refs much more convenient, with the downside that it hides a potentially dangerous operation. Since the result is not itself a ref<>; clients are resposible for making sure the returned value is only used inside the lifetime of some other ref<>. git-svn-id: https://llvm.org/svn/llvm-project/klee/trunk@72839 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/klee/util/Ref.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/klee/util/Ref.h b/include/klee/util/Ref.h index 39598b16..b56d5d6a 100644 --- a/include/klee/util/Ref.h +++ b/include/klee/util/Ref.h @@ -115,7 +115,6 @@ public: bool operator!=(const ref &rhs) const { return compare(rhs)!=0; } }; - template inline std::ostream &operator<<(std::ostream &os, const ref &e) { os << *e; @@ -144,6 +143,26 @@ const U* static_ref_cast(const ref &src) { return static_cast(src.ptr); } +} // end namespace klee + +namespace llvm { + // simplify_type implementation for ref<>, which allows dyn_cast from on a + // ref<> to apply to the wrapper type. Conceptually the result of such a + // dyn_cast should probably be a ref of the casted type, but that breaks the + // idiom of initializing a variable to the result of a dyn_cast inside an if + // condition, or we would have to implement operator(bool) for ref<> with + // isNull semantics, which doesn't seem like a good idea. +template +struct simplify_type > { + typedef T* SimpleType; + static SimpleType getSimplifiedValue(const ::klee::ref &Ref) { + return Ref.get(); + } +}; + +template +struct simplify_type< ::klee::ref > + : public simplify_type > {}; } #endif /* KLEE_REF_H */ -- cgit 1.4.1