Reapply my previous asmprinter changes now with more testing and two
[oota-llvm.git] / include / llvm / Support / Casting.h
index 2c072d1bb445dc92035c90e01bfc2ace24b501c0..48988f8a6bb8354c71326e634364acbb2895b4b2 100644 (file)
@@ -1,14 +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<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
 // and dyn_cast_or_null<X>() templates.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef SUPPORT_CASTING_H
-#define SUPPORT_CASTING_H
+#ifndef LLVM_SUPPORT_CASTING_H
+#define LLVM_SUPPORT_CASTING_H
 
-#include <assert.h>
+#include <cassert>
+
+namespace llvm {
 
 //===----------------------------------------------------------------------===//
 //                          isa<x> Support Templates
@@ -30,7 +39,7 @@ template<typename From> struct simplify_type {
 template<typename From> struct simplify_type<const From> {
   typedef const From SimpleType;
   static SimpleType &getSimplifiedValue(const From &Val) {
-    return simplify_type<From>::getSimplifiedValue((From&)Val);
+    return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
   }
 };
 
@@ -41,7 +50,7 @@ template<typename From> struct simplify_type<const From> {
 //  if (isa<Type*>(myVal)) { ... }
 //
 template <typename To, typename From>
-inline bool isa_impl(const From &Val) { 
+inline bool isa_impl(const From &Val) {
   return To::classof(&Val);
 }
 
@@ -50,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<const SimpleType>::template 
+    return isa_impl_cl<const SimpleType>::template
                     isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
   }
 };
@@ -64,14 +73,14 @@ struct isa_impl_wrap<To, const FromTy, const FromTy> {
 };
 
 // isa_impl_cl - Use class partial specialization to transform types to a single
-// cannonical form for isa_impl.
+// canonical form for isa_impl.
 //
 template<typename FromCl>
 struct isa_impl_cl {
   template<class ToCl>
   static bool isa(const FromCl &Val) {
     return isa_impl_wrap<ToCl,const FromCl,
-                         simplify_type<const FromCl>::SimpleType>::doit(Val);
+                   typename simplify_type<const FromCl>::SimpleType>::doit(Val);
   }
 };
 
@@ -152,8 +161,8 @@ struct cast_retty_wrap<To, FromTy, FromTy> {
 
 template<class To, class From>
 struct cast_retty {
-  typedef typename cast_retty_wrap<To, From, 
-                         simplify_type<From>::SimpleType>::ret_type ret_type;
+  typedef typename cast_retty_wrap<To, From,
+                   typename simplify_type<From>::SimpleType>::ret_type ret_type;
 };
 
 // Ensure the non-simple values are converted using the simplify_type template
@@ -161,17 +170,18 @@ struct cast_retty {
 //
 template<class To, class From, class SimpleFrom> struct cast_convert_val {
   // This is not a simple type, use the template to simplify it...
-  static cast_retty<To, From>::ret_type doit(const From &Val) {
+  static typename cast_retty<To, From>::ret_type doit(const From &Val) {
     return cast_convert_val<To, SimpleFrom,
-      simplify_type<SimpleFrom>::SimpleType>::doit(
+      typename simplify_type<SimpleFrom>::SimpleType>::doit(
                           simplify_type<From>::getSimplifiedValue(Val));
   }
 };
 
 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
   // This _is_ a simple type, just cast it.
-  static cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
-    return (cast_retty<To, FromTy>::ret_type)Val;
+  static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
+    return reinterpret_cast<typename cast_retty<To, FromTy>::ret_type>(
+                         const_cast<FromTy&>(Val));
   }
 };
 
@@ -185,18 +195,19 @@ template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
 //  cast<Instruction>(myVal)->getParent()
 //
 template <class X, class Y>
-inline cast_retty<X, Y>::ret_type cast(const Y &Val) {
-  assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
-  return cast_convert_val<X, Y, simplify_type<Y>::SimpleType>::doit(Val);
+inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
+  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
+  return cast_convert_val<X, Y,
+                          typename simplify_type<Y>::SimpleType>::doit(Val);
 }
 
 // cast_or_null<X> - Functionally identical to cast, except that a null value is
 // accepted.
 //
 template <class X, class Y>
-inline cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
+inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
   if (Val == 0) return 0;
-  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of uncompatible type!");
+  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
   return cast<X>(Val);
 }
 
@@ -206,25 +217,25 @@ inline cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
 // 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)) { ... }
+//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
 //
 
 template <class X, class Y>
-inline cast_retty<X, Y*>::ret_type dyn_cast(Y *Val) {
-  return isa<X>(Val) ? cast<X, Y*>(Val) : 0;
+inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
+  return isa<X>(Val) ? cast<X, Y>(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 cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
-  return (Val && isa<X>(Val)) ? cast<X, Y*>(Val) : 0;
+inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) {
+  return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
 }
 
 
 #ifdef DEBUG_CAST_OPERATORS
-#include <iostream>
+#include "llvm/Support/Streams.h"
 
 struct bar {
   bar() {}
@@ -239,7 +250,7 @@ struct foo {
     }*/
 };
 
-template <> inline bool isa_impl<foo,bar>(const bar &Val) { 
+template <> inline bool isa_impl<foo,bar>(const bar &Val) {
   cerr << "Classof: " << &Val << "\n";
   return true;
 }
@@ -270,7 +281,7 @@ void test(bar &B1, const bar *B2) {
   const foo *F12 = cast_or_null<foo>(B2);
   const foo *F13 = cast_or_null<foo>(B4);
   const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
-  
+
   // These lines are errors...
   //foo *F20 = cast<foo>(B2);  // Yields const foo*
   //foo &F21 = cast<foo>(B3);  // Yields const foo&
@@ -287,4 +298,6 @@ void main() {
 
 #endif
 
+} // End llvm namespace
+
 #endif