* Move casting stuff out to Support/Casting.h

* Add top level virtual print function, disallows instantiating Value's
  directly.
* Provide operator<< for values here, instead of in Assembly/Writer.h


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2168 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-04-08 21:51:32 +00:00
parent 12e8ad6858
commit 42a695c2f2

View File

@ -6,8 +6,6 @@
//
// 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
@ -16,6 +14,7 @@
#include <vector>
#include "llvm/Annotation.h"
#include "llvm/AbstractTypeUser.h"
#include "Support/Casting.h"
class User;
class Type;
@ -25,7 +24,6 @@ class Instruction;
class BasicBlock;
class GlobalValue;
class Function;
typedef Function Method;
class GlobalVariable;
class Module;
class SymbolTable;
@ -65,6 +63,9 @@ public:
// Support for debugging
void dump() const;
// Implement operator<< on Value...
virtual void print(std::ostream &O) const = 0;
// All values can potentially be typed
inline const Type *getType() const { return Ty; }
@ -122,6 +123,14 @@ public:
void killUse(User *I);
};
inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
if (V == 0)
OS << "<null> value!\n";
else
V->print(OS);
return OS;
}
//===----------------------------------------------------------------------===//
// UseTy Class
@ -172,79 +181,14 @@ public:
typedef UseTy<Value> Use; // Provide Use as a common UseTy type
// real_type - Provide a macro to get the real type of a value that might be
// a use. This provides a typedef 'Type' that is the argument type for all
// non UseTy types, and is the contained pointer type of the use if it is a
// UseTy.
// Provide a specialization of real_type to work with use's... to make them a
// bit more transparent.
//
template <class X> class real_type { typedef X Type; };
template <class X> class real_type <class UseTy<X> > { typedef X *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>
inline bool isa(Y Val) {
assert(Val && "isa<Ty>(NULL) invoked!");
return X::classof(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. But it will correctly return NULL when the input is NULL.
// Used Like this:
//
// cast< Instruction>(myVal)->getParent()
// cast<const Instruction>(myVal)->getParent()
//
template <class X, class Y>
inline X *cast(Y Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
return (X*)(real_type<Y>::Type)Val;
}
// cast_or_null<X> - Functionally identical to cast, except that a null value is
// accepted.
//
template <class X, class Y>
inline X *cast_or_null(Y Val) {
assert((Val == 0 || isa<X>(Val)) &&
"cast_or_null<Ty>() argument of uncompatible type!");
return (X*)(real_type<Y>::Type)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>
inline X *dyn_cast(Y Val) {
return isa<X>(Val) ? cast<X>(Val) : 0;
}
// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
// value is accepted.
//
template <class X, class Y>
inline X *dyn_cast_or_null(Y Val) {
return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
}
// isa - Provide some specializations of isa so that we have to include the
// subtype header files to test to see if the value is a subclass...
// isa - Provide some specializations of isa so that we don't have to include
// the subtype header files to test to see if the value is a subclass...
//
template <> inline bool isa<Type, const Value*>(const Value *Val) {
return Val->getValueType() == Value::TypeVal;