From 42a695c2f2627a04b1fc8e2160d608c39f1dd6ac Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 8 Apr 2002 21:51:32 +0000 Subject: [PATCH] * 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 --- include/llvm/Value.h | 88 ++++++++------------------------------------ 1 file changed, 16 insertions(+), 72 deletions(-) diff --git a/include/llvm/Value.h b/include/llvm/Value.h index ce822fe7958..7ffcf1b7507 100644 --- a/include/llvm/Value.h +++ b/include/llvm/Value.h @@ -6,8 +6,6 @@ // // This file also defines the Use<> template for users of value. // -// This file also defines the isa(), cast(), and dyn_cast() templates. -// //===----------------------------------------------------------------------===// #ifndef LLVM_VALUE_H @@ -16,6 +14,7 @@ #include #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 << " value!\n"; + else + V->print(OS); + return OS; +} + //===----------------------------------------------------------------------===// // UseTy Class @@ -172,79 +181,14 @@ public: typedef UseTy 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 real_type { typedef X Type; }; template class real_type > { typedef X *Type; }; -//===----------------------------------------------------------------------===// -// Type Checking Templates -//===----------------------------------------------------------------------===// -// isa - Return true if the parameter to the template is an instance of the -// template type argument. Used like this: -// -// if (isa(myVal)) { ... } -// -template -inline bool isa(Y Val) { - assert(Val && "isa(NULL) invoked!"); - return X::classof(Val); -} - - -// cast - 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(myVal)->getParent() -// -template -inline X *cast(Y Val) { - assert(isa(Val) && "cast() argument of uncompatible type!"); - return (X*)(real_type::Type)Val; -} - -// cast_or_null - Functionally identical to cast, except that a null value is -// accepted. -// -template -inline X *cast_or_null(Y Val) { - assert((Val == 0 || isa(Val)) && - "cast_or_null() argument of uncompatible type!"); - return (X*)(real_type::Type)Val; -} - - -// dyn_cast - 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(myVal)) { ... } -// - -template -inline X *dyn_cast(Y Val) { - return isa(Val) ? cast(Val) : 0; -} - -// dyn_cast_or_null - Functionally identical to dyn_cast, except that a null -// value is accepted. -// -template -inline X *dyn_cast_or_null(Y Val) { - return (Val && isa(Val)) ? cast(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(const Value *Val) { return Val->getValueType() == Value::TypeVal;