aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-10-01 13:58:13 +0000
committerChris Lattner <sabre@nondot.org>2001-10-01 13:58:13 +0000
commit7295eb4ea3e3a81e697600cbca681674e4b35a20 (patch)
tree142d6b6d26c0be5a9588e6c99970b83ad25744af
parent6bad546c2a4eac51eabc6ac398861dcf7d5f18bb (diff)
Add support for newer cleaner isa, cast, dyn_cast
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@693 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/BasicBlock.h4
-rw-r--r--include/llvm/Instruction.h7
-rw-r--r--include/llvm/Type.h6
-rw-r--r--include/llvm/Value.h51
4 files changed, 65 insertions, 3 deletions
diff --git a/include/llvm/BasicBlock.h b/include/llvm/BasicBlock.h
index 11c76a3647..08516c8ca8 100644
--- a/include/llvm/BasicBlock.h
+++ b/include/llvm/BasicBlock.h
@@ -163,7 +163,7 @@ public:
// TODO: This is bad
// Loop to ignore constant pool references
while (It != BB->use_end() &&
- ((!(*It)->isInstruction()) ||
+ ((!isa<Instruction>(*It)) ||
!(((Instruction*)(*It))->isTerminator())))
++It;
}
@@ -177,7 +177,7 @@ public:
inline bool operator!=(const _Self& x) const { return !operator==(x); }
inline pointer operator*() const {
- return (*It)->castInstructionAsserting()->getParent();
+ return cast<Instruction>(*It)->getParent();
}
inline pointer *operator->() const { return &(operator*()); }
diff --git a/include/llvm/Instruction.h b/include/llvm/Instruction.h
index 3bbb8e7031..93b68a2c62 100644
--- a/include/llvm/Instruction.h
+++ b/include/llvm/Instruction.h
@@ -85,6 +85,13 @@ public:
// and then drops all references to its operands.
//
void dropAllReferences();
+
+
+ // Methods for support type inquiry through isa, cast, and dyn_cast:
+ static inline bool isa(const Instruction *I) { return true; }
+ static inline bool isa(const Value *V) {
+ return V->getValueType() == Value::InstructionVal;
+ }
//----------------------------------------------------------------------
// Exported enumerations...
diff --git a/include/llvm/Type.h b/include/llvm/Type.h
index 5854062df0..2a43153acd 100644
--- a/include/llvm/Type.h
+++ b/include/llvm/Type.h
@@ -195,6 +195,12 @@ public:
return (const DerivedType*)this;
}
+ // Methods for support type inquiry through isa, cast, and dyn_cast:
+ static inline bool isa(const Type *T) { return true; }
+ static inline bool isa(const Value *V) {
+ return V->getValueType() == Value::TypeVal;
+ }
+
// Methods for determining the subtype of this Type. The cast*() methods are
// equilivent to using dynamic_cast<>... if the cast is successful, this is
// returned, otherwise you get a null pointer, allowing expressions like this:
diff --git a/include/llvm/Value.h b/include/llvm/Value.h
index 7d214e2ab4..14794dc4c9 100644
--- a/include/llvm/Value.h
+++ b/include/llvm/Value.h
@@ -3,6 +3,10 @@
// This file defines the very important Value class. This is subclassed by a
// bunch of other important classes, like Def, Method, Module, Type, etc...
//
+// This file also defines the Use<> template for users of value.
+//
+// This file also defines the isa<X>(), cast<X>(), and dyn_cast<X>() templates.
+//
//===----------------------------------------------------------------------===//
#ifndef LLVM_VALUE_H
@@ -156,6 +160,11 @@ public:
void killUse(User *I);
};
+
+//===----------------------------------------------------------------------===//
+// UseTy Class
+//===----------------------------------------------------------------------===//
+
// UseTy and it's friendly typedefs (Use) are here to make keeping the "use"
// list of a definition node up-to-date really easy.
//
@@ -196,6 +205,46 @@ public:
}
};
-typedef UseTy<Value> Use;
+typedef UseTy<Value> Use; // Provide Use as a common UseTy type
+
+
+//===----------------------------------------------------------------------===//
+// Type Checking Templates
+//===----------------------------------------------------------------------===//
+
+// isa<X> - Return true if the parameter to the template is an instance of the
+// template type argument. Used like this:
+//
+// if (isa<Type>(myVal)) { ... }
+//
+template <class X, class Y>
+bool isa(Y *Val) { return X::isa(Val); }
+
+
+// cast<X> - Return the argument parameter cast to the specified type. This
+// casting operator asserts that the type is correct, so it does not return null
+// on failure. Used Like this:
+//
+// cast< Instruction>(myVal)->getParent()
+// cast<const Instruction>(myVal)->getParent()
+//
+template <class X, class Y>
+X *cast(Y *Val) {
+ assert(isa<X>(Val) && "Invalid cast argument type!");
+ return (X*)Val;
+}
+
+
+// dyn_cast<X> - Return the argument parameter cast to the specified type. This
+// casting operator returns null if the argument is of the wrong type, so it can
+// be used to test for a type as well as cast if successful. This should be
+// used in the context of an if statement like this:
+//
+// if (const Instruction *I = dyn_cast<const Instruction>(myVal)) { ... }
+//
+template <class X, class Y>
+X *dyn_cast(Y *Val) {
+ return isa<X>(Val) ? (X*)Val : 0;
+}
#endif