* Method::getType should return type cast as MethodType, eliminate getMethodType
authorChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2001 20:06:17 +0000 (20:06 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 10 Sep 2001 20:06:17 +0000 (20:06 +0000)
* Make Type::*Ty not be const types
* Add a new Type.def file to provide info about types
* Add a full complement of casting methods to the Type class

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@533 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Function.h
include/llvm/Type.def [new file with mode: 0644]
include/llvm/Type.h

index 2be84d4e4cce92561999c980a364e8860237f062..6ec28a87a3330124a474a09736e787583a976968 100644 (file)
@@ -51,7 +51,9 @@ public:
   virtual void setName(const string &name, SymbolTable *ST = 0);
 
   const Type *getReturnType() const;
-  const MethodType *getMethodType() const;
+  const MethodType *getType() const {
+    return (const MethodType*)Value::getType(); 
+  }
 
   // Is the body of this method unknown? (the basic block list is empty if so)
   // this is true for external methods, defined as forward "declare"ations
diff --git a/include/llvm/Type.def b/include/llvm/Type.def
new file mode 100644 (file)
index 0000000..ea4cdc1
--- /dev/null
@@ -0,0 +1,58 @@
+//===-- llvm/Type.def - File that describes various LLVM types ---*- C++ -*--=//
+//
+// This file contains descriptions of the various LLVM types and derived types.
+// This file serves as a source of in source documentation and a can be
+// #included to do something on all of the type definitions.
+//
+//===----------------------------------------------------------------------===//
+
+// NOTE: NO INCLUDE GUARD DESIRED!
+
+
+// If the user didn't specify one of the macros, give a default noop defn.
+//
+#ifndef HANDLE_PRIM_TYPE
+#define HANDLE_PRIM_TYPE(x,y)
+#endif
+#ifndef HANDLE_DERV_TYPE
+#define HANDLE_DERV_TYPE(x,y)
+#endif
+
+
+// HANDLE_PRIM_TYPE - Parameterized #define that takes the following fields to
+// declare a primitive type:
+//
+//   Type Name:  This is the symbolic name of the type, without the trailing Ty.
+//   Type Size:  This is the size or precision of the type.
+//
+HANDLE_PRIM_TYPE(Void  , 1)
+HANDLE_PRIM_TYPE(Bool  , 1)
+HANDLE_PRIM_TYPE(SByte , 1)
+HANDLE_PRIM_TYPE(UByte , 1)
+HANDLE_PRIM_TYPE(Short , 2)
+HANDLE_PRIM_TYPE(UShort, 2)
+HANDLE_PRIM_TYPE(Int   , 4)
+HANDLE_PRIM_TYPE(UInt  , 4)
+HANDLE_PRIM_TYPE(Long  , 8)
+HANDLE_PRIM_TYPE(ULong , 8)
+HANDLE_PRIM_TYPE(Float , 4)
+HANDLE_PRIM_TYPE(Double, 8)
+HANDLE_PRIM_TYPE(Type  , 0)
+HANDLE_PRIM_TYPE(Label , 8)
+
+
+// HANDLE_DERV_TYPE - Parameterized #define that takes the following fields to
+// declare a derived type:
+//
+//   Type Name:  This is the symbolic name of the type, without the trailing Ty.
+//   Class Name: This is the subclass that implements the derived type.
+//
+HANDLE_DERV_TYPE(Method , MethodType)
+HANDLE_DERV_TYPE(Array  , ArrayType)
+HANDLE_DERV_TYPE(Pointer, PointerType)
+HANDLE_DERV_TYPE(Struct , StructType)
+HANDLE_DERV_TYPE(Opaque , OpaqueType)
+
+// Kill the macros on exit...
+#undef HANDLE_PRIM_TYPE
+#undef HANDLE_DERV_TYPE
index afc197049d088a727861720254ac885462e2659d..ed39d120ec38f9847d0f8322c517bed98bb0fdc8 100644 (file)
@@ -54,13 +54,12 @@ public:
 
     TypeTyID,                           // 12   : Type definitions
     LabelTyID     ,                     // 13   : Labels... 
-    /*LockTyID , */                     // 14   : mutex - TODO
 
     // Derived types... see DerivedTypes.h file...
     // Make sure FirstDerivedTyID stays up to date!!!
-    MethodTyID    , ModuleTyID,         // Methods... Modules...
+    MethodTyID    , StructTyID,         // Methods... Structs...
     ArrayTyID     , PointerTyID,        // Array... pointer...
-    StructTyID    , OpaqueTyID,         // Structure... Opaque type instances...
+    OpaqueTyID,                         // Opaque type instances...
     //PackedTyID  ,                     // SIMD 'packed' format... TODO
     //...
 
@@ -172,23 +171,21 @@ public:
   //===--------------------------------------------------------------------===//
   // These are the builtin types that are always available...
   //
-  static const Type *VoidTy , *BoolTy;
-  static const Type *SByteTy, *UByteTy,
-                    *ShortTy, *UShortTy,
-                    *IntTy  , *UIntTy, 
-                    *LongTy , *ULongTy;
-  static const Type *FloatTy, *DoubleTy;
+  static Type *VoidTy , *BoolTy;
+  static Type *SByteTy, *UByteTy,
+              *ShortTy, *UShortTy,
+              *IntTy  , *UIntTy, 
+              *LongTy , *ULongTy;
+  static Type *FloatTy, *DoubleTy;
 
-  static const Type *TypeTy , *LabelTy; //, *LockTy;
+  static Type *TypeTy , *LabelTy;
 
   // Here are some useful little methods to query what type derived types are
   // Note that all other types can just compare to see if this == Type::xxxTy;
   //
-  inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
   inline bool isPrimitiveType() const { return ID < FirstDerivedTyID;  }
 
-  inline bool isLabelType()     const { return this == LabelTy; }
-
+  inline bool isDerivedType()   const { return ID >= FirstDerivedTyID; }
   inline const DerivedType *castDerivedType() const {
     return isDerivedType() ? (const DerivedType*)this : 0;
   }
@@ -197,22 +194,38 @@ public:
     return (const DerivedType*)this;
   }
 
-  inline const MethodType *isMethodType() const {
-    return ID == MethodTyID ? (const MethodType*)this : 0;
-  }
-  inline bool isModuleType()    const { return ID == ModuleTyID;     }
-  inline const ArrayType *isArrayType() const { 
-    return ID == ArrayTyID ? (const ArrayType*)this : 0;
-  }
-  inline const PointerType *isPointerType() const { 
-    return ID == PointerTyID ? (const PointerType*)this : 0;
-  }
-  inline const StructType *isStructType() const {
-    return ID == StructTyID ? (const StructType*)this : 0;
-  }
-  inline const OpaqueType *isOpaqueType() const {
-    return ID == OpaqueTyID ? (const OpaqueType*)this : 0;
+  // 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:
+  //
+  // if (MethodType *MTy = Ty->dyncastMethodType()) { ... }
+  //
+  // This section also defines a family of isArrayType(), isLabelType(), 
+  // etc functions...
+  //
+  // The family of functions Ty->cast<type>() is used in the same way as the
+  // Ty->dyncast<type>() instructions, but they assert the expected type instead
+  // of checking it at runtime.
+  //
+#define HANDLE_PRIM_TYPE(NAME, SIZE)                                      \
+  inline bool is##NAME##Type() const { return ID == NAME##TyID; }
+#define HANDLE_DERV_TYPE(NAME, CLASS)                                     \
+  inline bool is##NAME##Type() const { return ID == NAME##TyID; }         \
+  inline const CLASS *dyncast##NAME##Type() const { /*const version */    \
+    return is##NAME##Type() ? (const CLASS*)this : 0;                     \
+  }                                                                       \
+  inline CLASS *dyncast##NAME##Type() {         /* nonconst version */    \
+    return is##NAME##Type() ? (CLASS*)this : 0;                           \
+  }                                                                       \
+  inline const CLASS *cast##NAME##Type() const {    /*const version */    \
+    assert(is##NAME##Type() && "Expected TypeTy: " #NAME);                \
+    return (const CLASS*)this;                                            \
+  }                                                                       \
+  inline CLASS *cast##NAME##Type() {            /* nonconst version */    \
+    assert(is##NAME##Type() && "Expected TypeTy: " #NAME);                \
+    return (CLASS*)this;                                                  \
   }
+#include "llvm/Type.def"
 
 private:
   class TypeIterator : public std::bidirectional_iterator<const Type,