X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FCasting.h;h=48988f8a6bb8354c71326e634364acbb2895b4b2;hb=6d53f55291c8541a508a8c26d847b942196f6f1c;hp=476d3ae681ff4a0cf46505c0de7280a456542f94;hpb=7a73b80b9052136c8cd2234eb3433a07df7cf38e;p=oota-llvm.git diff --git a/include/llvm/Support/Casting.h b/include/llvm/Support/Casting.h index 476d3ae681f..48988f8a6bb 100644 --- a/include/llvm/Support/Casting.h +++ b/include/llvm/Support/Casting.h @@ -1,12 +1,23 @@ -//===-- Support/Casting.h - Allow flexible, checked, casts -------*- C++ -*--=// +//===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// // // This file defines the isa(), cast(), dyn_cast(), cast_or_null(), // and dyn_cast_or_null() templates. // //===----------------------------------------------------------------------===// -#ifndef SUPPORT_CASTING_H -#define SUPPORT_CASTING_H +#ifndef LLVM_SUPPORT_CASTING_H +#define LLVM_SUPPORT_CASTING_H + +#include + +namespace llvm { //===----------------------------------------------------------------------===// // isa Support Templates @@ -28,7 +39,7 @@ template struct simplify_type { template struct simplify_type { typedef const From SimpleType; static SimpleType &getSimplifiedValue(const From &Val) { - return simplify_type::getSimplifiedValue((From&)Val); + return simplify_type::getSimplifiedValue(static_cast(Val)); } }; @@ -39,7 +50,7 @@ template struct simplify_type { // if (isa(myVal)) { ... } // template -inline bool isa_impl(const From &Val) { +inline bool isa_impl(const From &Val) { return To::classof(&Val); } @@ -48,7 +59,7 @@ struct isa_impl_wrap { // When From != SimplifiedType, we can simplify the type some more by using // the simplify_type template. static bool doit(const From &Val) { - return isa_impl_cl::template + return isa_impl_cl::template isa(simplify_type::getSimplifiedValue(Val)); } }; @@ -62,7 +73,7 @@ struct isa_impl_wrap { }; // isa_impl_cl - Use class partial specialization to transform types to a single -// cannonical form for isa_impl. +// canonical form for isa_impl. // template struct isa_impl_cl { @@ -150,7 +161,7 @@ struct cast_retty_wrap { template struct cast_retty { - typedef typename cast_retty_wrap::SimpleType>::ret_type ret_type; }; @@ -169,7 +180,8 @@ template struct cast_convert_val { template struct cast_convert_val { // This _is_ a simple type, just cast it. static typename cast_retty::ret_type doit(const FromTy &Val) { - return (typename cast_retty::ret_type)Val; + return reinterpret_cast::ret_type>( + const_cast(Val)); } }; @@ -184,7 +196,7 @@ template struct cast_convert_val { // template inline typename cast_retty::ret_type cast(const Y &Val) { - assert(isa(Val) && "cast() argument of uncompatible type!"); + assert(isa(Val) && "cast() argument of incompatible type!"); return cast_convert_val::SimpleType>::doit(Val); } @@ -195,7 +207,7 @@ inline typename cast_retty::ret_type cast(const Y &Val) { template inline typename cast_retty::ret_type cast_or_null(Y *Val) { if (Val == 0) return 0; - assert(isa(Val) && "cast_or_null() argument of uncompatible type!"); + assert(isa(Val) && "cast_or_null() argument of incompatible type!"); return cast(Val); } @@ -209,7 +221,7 @@ inline typename cast_retty::ret_type cast_or_null(Y *Val) { // template -inline typename cast_retty::ret_type dyn_cast(Y Val) { +inline typename cast_retty::ret_type dyn_cast(const Y &Val) { return isa(Val) ? cast(Val) : 0; } @@ -217,13 +229,13 @@ inline typename cast_retty::ret_type dyn_cast(Y Val) { // value is accepted. // template -inline typename cast_retty::ret_type dyn_cast_or_null(Y Val) { +inline typename cast_retty::ret_type dyn_cast_or_null(const Y &Val) { return (Val && isa(Val)) ? cast(Val) : 0; } #ifdef DEBUG_CAST_OPERATORS -#include +#include "llvm/Support/Streams.h" struct bar { bar() {} @@ -238,7 +250,7 @@ struct foo { }*/ }; -template <> inline bool isa_impl(const bar &Val) { +template <> inline bool isa_impl(const bar &Val) { cerr << "Classof: " << &Val << "\n"; return true; } @@ -269,7 +281,7 @@ void test(bar &B1, const bar *B2) { const foo *F12 = cast_or_null(B2); const foo *F13 = cast_or_null(B4); const foo *F14 = cast_or_null(fub()); // Shouldn't print. - + // These lines are errors... //foo *F20 = cast(B2); // Yields const foo* //foo &F21 = cast(B3); // Yields const foo& @@ -286,4 +298,6 @@ void main() { #endif +} // End llvm namespace + #endif