LLVM API Documentation

AbstractTypeUser.h

Go to the documentation of this file.
00001 //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the AbstractTypeUser class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ABSTRACT_TYPE_USER_H
00015 #define LLVM_ABSTRACT_TYPE_USER_H
00016 
00017 // This is the "master" include for <cassert> Whether this file needs it or not,
00018 // it must always include <cassert> for the files which include
00019 // llvm/AbstractTypeUser.h
00020 //
00021 // In this way, most every LLVM source file will have access to the assert()
00022 // macro without having to #include <cassert> directly.
00023 //
00024 #include <cassert>
00025 
00026 namespace llvm {
00027 
00028 class Type;
00029 class DerivedType;
00030 
00031 /// The AbstractTypeUser class is an interface to be implemented by classes who
00032 /// could possibly use an abstract type.  Abstract types are denoted by the
00033 /// isAbstract flag set to true in the Type class.  These are classes that
00034 /// contain an Opaque type in their structure somewhere.
00035 ///
00036 /// Classes must implement this interface so that they may be notified when an
00037 /// abstract type is resolved.  Abstract types may be resolved into more 
00038 /// concrete types through: linking, parsing, and bytecode reading.  When this 
00039 /// happens, all of the users of the type must be updated to reference the new,
00040 /// more concrete type.  They are notified through the AbstractTypeUser 
00041 /// interface.
00042 ///
00043 /// In addition to this, AbstractTypeUsers must keep the use list of the
00044 /// potentially abstract type that they reference up-to-date.  To do this in a
00045 /// nice, transparent way, the PATypeHandle class is used to hold "Potentially
00046 /// Abstract Types", and keep the use list of the abstract types up-to-date.
00047 /// @brief LLVM Abstract Type User Representation
00048 class AbstractTypeUser {
00049 protected:
00050   virtual ~AbstractTypeUser();                        // Derive from me
00051 public:
00052 
00053   /// refineAbstractType - The callback method invoked when an abstract type is
00054   /// resolved to another type.  An object must override this method to update
00055   /// its internal state to reference NewType instead of OldType.
00056   ///
00057   virtual void refineAbstractType(const DerivedType *OldTy,
00058                                   const Type *NewTy) = 0;
00059 
00060   /// The other case which AbstractTypeUsers must be aware of is when a type
00061   /// makes the transition from being abstract (where it has clients on it's
00062   /// AbstractTypeUsers list) to concrete (where it does not).  This method
00063   /// notifies ATU's when this occurs for a type.
00064   ///
00065   virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
00066 
00067   // for debugging...
00068   virtual void dump() const = 0;
00069 };
00070 
00071 
00072 /// PATypeHandle - Handle to a Type subclass.  This class is used to keep the
00073 /// use list of abstract types up-to-date.
00074 ///
00075 class PATypeHandle {
00076   const Type *Ty;
00077   AbstractTypeUser * const User;
00078 
00079   // These functions are defined at the bottom of Type.h.  See the comment there
00080   // for justification.
00081   void addUser();
00082   void removeUser();
00083 public:
00084   // ctor - Add use to type if abstract.  Note that Ty must not be null
00085   inline PATypeHandle(const Type *ty, AbstractTypeUser *user)
00086     : Ty(ty), User(user) {
00087     addUser();
00088   }
00089 
00090   // ctor - Add use to type if abstract.
00091   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
00092     addUser();
00093   }
00094 
00095   // dtor - Remove reference to type...
00096   inline ~PATypeHandle() { removeUser(); }
00097 
00098   // Automatic casting operator so that the handle may be used naturally
00099   inline operator Type *() const { return const_cast<Type*>(Ty); }
00100   inline Type *get() const { return const_cast<Type*>(Ty); }
00101 
00102   // operator= - Allow assignment to handle
00103   inline Type *operator=(const Type *ty) {
00104     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
00105       removeUser();
00106       Ty = ty;
00107       addUser();
00108     }
00109     return get();
00110   }
00111 
00112   // operator= - Allow assignment to handle
00113   inline const Type *operator=(const PATypeHandle &T) {
00114     return operator=(T.Ty);
00115   }
00116 
00117   inline bool operator==(const Type *ty) {
00118     return Ty == ty;
00119   }
00120 
00121   // operator-> - Allow user to dereference handle naturally...
00122   inline const Type *operator->() const { return Ty; }
00123 };
00124 
00125 
00126 /// PATypeHolder - Holder class for a potentially abstract type.  This uses
00127 /// efficient union-find techniques to handle dynamic type resolution.  Unless
00128 /// you need to do custom processing when types are resolved, you should always
00129 /// use PATypeHolders in preference to PATypeHandles.
00130 ///
00131 class PATypeHolder {
00132   mutable const Type *Ty;
00133 public:
00134   PATypeHolder(const Type *ty) : Ty(ty) {
00135     addRef();
00136   }
00137   PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
00138     addRef();
00139   }
00140 
00141   ~PATypeHolder() { dropRef(); }
00142 
00143   operator Type *() const { return get(); }
00144   Type *get() const;
00145 
00146   // operator-> - Allow user to dereference handle naturally...
00147   Type *operator->() const { return get(); }
00148 
00149   // operator= - Allow assignment to handle
00150   Type *operator=(const Type *ty) {
00151     if (Ty != ty) {   // Don't accidentally drop last ref to Ty.
00152       dropRef();
00153       Ty = ty;
00154       addRef();
00155     }
00156     return get();
00157   }
00158   Type *operator=(const PATypeHolder &H) {
00159     return operator=(H.Ty);
00160   }
00161 
00162   /// getRawType - This should only be used to implement the vmcore library.
00163   ///
00164   const Type *getRawType() const { return Ty; }
00165 
00166 private:
00167   void addRef();
00168   void dropRef();
00169 };
00170 
00171 } // End llvm namespace
00172 
00173 #endif